1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 IBM Corporation and others.
3  *
4  * This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License 2.0
6  * which accompanies this distribution, and is available at
7  * https://www.eclipse.org/legal/epl-2.0/
8  *
9  * SPDX-License-Identifier: EPL-2.0
10  *
11  * Contributors:
12  *     IBM Corporation - initial API and implementation
13  *******************************************************************************/
14 package org.eclipse.team.internal.ccvs.core.client;
15 
16 
17 import java.util.Collection;
18 
19 import org.eclipse.core.resources.IResource;
20 import org.eclipse.core.runtime.*;
21 import org.eclipse.core.runtime.Status;
22 import org.eclipse.osgi.util.NLS;
23 import org.eclipse.team.internal.ccvs.core.*;
24 import org.eclipse.team.internal.ccvs.core.client.listeners.ICommandOutputListener;
25 
26 public class Commit extends Command {
27 	/*** Local options: specific to commit ***/
28 	// Forces a file to be committed even if it has not been modified; implies -l.
29 	// NOTE: This option is not fully supported -- a file will not be sent
30 	//       unless it is dirty.  The primary use is to resend a file that may
31 	//       or may not be changed (e.g. could depend on CR/LF translations, etc...)
32 	//       and force the server to create a new revision and reply Checked-in.
33 	public static final LocalOption FORCE = new LocalOption("-f"); //$NON-NLS-1$
34 
Commit()35 	protected Commit() { }
getRequestId()36 	protected String getRequestId() {
37 		return "ci"; //$NON-NLS-1$
38 	}
39 
40 	/**
41 	 * Send all files under the workingFolder as changed files to
42 	 * the server.
43 	 */
sendLocalResourceState(Session session, GlobalOption[] globalOptions, LocalOption[] localOptions, ICVSResource[] resources, IProgressMonitor monitor)44 	protected ICVSResource[] sendLocalResourceState(Session session, GlobalOption[] globalOptions,
45 		LocalOption[] localOptions, ICVSResource[] resources, IProgressMonitor monitor)
46 		throws CVSException {
47 
48 		// Get the folders we want to work on
49 		checkResourcesManaged(session, resources);
50 
51 		// Send all changed files to the server
52 		ModifiedFileSender visitor = new ModifiedFileSender(session, localOptions);
53 		visitor.visit(session, resources, monitor);
54 
55 		// Send the changed files as arguments (because this is what other cvs clients do)
56 		ICVSFile[] changedFiles = visitor.getModifiedFiles();
57 		for (ICVSFile changedFile : changedFiles) {
58 			// escape file names, see bug 149683
59 			String fileName = changedFile.getRelativePath(session.getLocalRoot());
60 			if(fileName.startsWith("-")){ //$NON-NLS-1$
61 				fileName = "./" + fileName; //$NON-NLS-1$
62 			}
63 			session.sendArgument(fileName);
64 		}
65 		return changedFiles;
66 	}
67 
68 	/**
69 	 * On successful finish, prune empty directories if the -P or -D option was specified.
70 	 */
commandFinished(Session session, GlobalOption[] globalOptions, LocalOption[] localOptions, ICVSResource[] resources, IProgressMonitor monitor, IStatus status)71 	protected IStatus commandFinished(Session session, GlobalOption[] globalOptions,
72 		LocalOption[] localOptions, ICVSResource[] resources, IProgressMonitor monitor,
73 		IStatus status) throws CVSException {
74 		// If we didn't succeed, don't do any post processing
75 		if (status.getCode() == CVSStatus.SERVER_ERROR) {
76 			return status;
77 		}
78 
79 		// If pruning is enable, prune empty directories after a commit
80 		if (CVSProviderPlugin.getPlugin().getPruneEmptyDirectories()) {
81 			new PruneFolderVisitor().visit(session, resources);
82 		}
83 
84 		// Reset the timestamps of any committed files that are still dirty.
85 		// Only do so if there were no E messages from the server
86 		if (status.isOK()) {
87 			for (ICVSResource resource : resources) {
88 				if (!resource.isFolder()) {
89 					ICVSFile cvsFile = (ICVSFile) resource;
90 					if (cvsFile.exists() && cvsFile.isModified(null)) {
91 						status = mergeStatus(status, clearModifiedState(cvsFile));
92 					}
93 				}
94 			}
95 		}
96 		return status;
97 	}
98 
clearModifiedState(ICVSFile cvsFile)99 	protected IStatus clearModifiedState(ICVSFile cvsFile) throws CVSException {
100 		byte[] info = cvsFile.getSyncBytes();
101 		IResource resource = cvsFile.getIResource();
102 		String filePath;
103 		if (resource == null) {
104 			filePath = cvsFile.getRepositoryRelativePath();
105 		} else {
106 			filePath = resource.getFullPath().toString();
107 		}
108 		if (info == null) {
109 			// There should be sync info. Log the problem
110 			return new Status(IStatus.WARNING, CVSProviderPlugin.ID, 0, NLS.bind(CVSMessages.Commit_syncInfoMissing, new String[] { filePath }), null);
111 		}
112 		cvsFile.checkedIn(null, true /* commit in progress */);
113 		return new Status(IStatus.INFO, CVSProviderPlugin.ID, 0, NLS.bind(CVSMessages.Commit_timestampReset, new String[] { filePath }), null); //;
114 	}
115 
116 	/**
117 	 * We do not want to send the arguments here, because we send
118 	 * them in sendRequestsToServer (special handling).
119 	 */
sendArguments(Session session, String[] arguments)120 	protected void sendArguments(Session session, String[] arguments) throws CVSException {
121 	}
122 
execute(Session session, GlobalOption[] globalOptions, LocalOption[] localOptions, ICVSResource[] arguments, Collection filesToCommitAsText, ICommandOutputListener listener, IProgressMonitor pm)123 	public final IStatus execute(Session session, GlobalOption[] globalOptions, LocalOption[] localOptions,
124 		ICVSResource[] arguments, Collection filesToCommitAsText,
125 		ICommandOutputListener listener, IProgressMonitor pm) throws CVSException {
126 
127 		session.setTextTransferOverride(filesToCommitAsText);
128 		try {
129 			return super.execute(session, globalOptions, localOptions, arguments, listener, pm);
130 		} finally {
131 			session.setTextTransferOverride(null);
132 		}
133 	}
134 
getDisplayText()135 	protected String getDisplayText() {
136 		return "commit"; //$NON-NLS-1$
137 	}
138 }
139