Skip to content

Commit

Permalink
Added option for overwrite on build configurations
Browse files Browse the repository at this point in the history
  • Loading branch information
pescuma authored and jpricket committed Jun 27, 2017
1 parent 6629d4f commit 8b9d691
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 48 deletions.
14 changes: 12 additions & 2 deletions tfs/src/main/java/hudson/plugins/tfs/TeamFoundationServerScm.java
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,8 @@ public class TeamFoundationServerScm extends SCM {
private String userName;
private CredentialsConfigurer credentialsConfigurer;
private boolean useUpdate;

private boolean useOverwrite;

private TeamFoundationServerRepositoryBrowser repositoryBrowser;

private transient String normalizedWorkspaceName;
Expand Down Expand Up @@ -175,6 +176,15 @@ public void setUseUpdate(final boolean useUpdate) {
this.useUpdate = useUpdate;
}

public boolean isUseOverwrite() {
return useOverwrite;
}

@DataBoundSetter
public void setUseOverwrite(final boolean useOverwrite) {
this.useOverwrite = useOverwrite;
}

public String getUserPassword() {
return Secret.toString(password);
}
Expand Down Expand Up @@ -307,7 +317,7 @@ public boolean checkout(AbstractBuild<?, ?> build, Launcher launcher, FilePath w
final Project project = server.getProject(projectPath);
final int changeSet = recordWorkspaceChangesetVersion(build, listener, project, projectPath, singleVersionSpec);

CheckoutAction action = new CheckoutAction(workspaceConfiguration.getWorkspaceName(), workspaceConfiguration.getProjectPath(), workspaceConfiguration.getCloakedPaths(), workspaceConfiguration.getWorkfolder(), isUseUpdate());
CheckoutAction action = new CheckoutAction(workspaceConfiguration.getWorkspaceName(), workspaceConfiguration.getProjectPath(), workspaceConfiguration.getCloakedPaths(), workspaceConfiguration.getWorkfolder(), isUseUpdate(), isUseOverwrite());
List<ChangeSet> list;
if (StringUtils.isNotEmpty(singleVersionSpec)) {
list = action.checkoutBySingleVersionSpec(server, workspaceFilePath, singleVersionSpec);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,15 @@ public class CheckoutAction {
private final Collection<String> cloakedPaths;
private final String localFolder;
private final boolean useUpdate;
private final boolean useOverwrite;

public CheckoutAction(String workspaceName, String projectPath, Collection<String> cloakedPaths, String localFolder, boolean useUpdate) {
public CheckoutAction(String workspaceName, String projectPath, Collection<String> cloakedPaths, String localFolder, boolean useUpdate, boolean useOverwrite) {
this.workspaceName = workspaceName;
this.projectPath = projectPath;
this.cloakedPaths = cloakedPaths;
this.localFolder = localFolder;
this.useUpdate = useUpdate;
this.useOverwrite = useOverwrite;
}

public List<ChangeSet> checkout(Server server, FilePath workspacePath, Calendar lastBuildTimestamp, Calendar currentBuildTimestamp) throws IOException, InterruptedException, ParseException {
Expand All @@ -57,7 +59,7 @@ public List<ChangeSet> checkout(final Server server, final FilePath workspacePat

final String versionSpecString = RemoteChangesetVersionCommand.toString(currentBuildVersionSpec);
final String normalizedFolder = determineCheckoutPath(workspacePath, localFolder);
project.getFiles(normalizedFolder, versionSpecString);
project.getFiles(normalizedFolder, versionSpecString, useOverwrite);

if (lastBuildVersionSpec != null) {
return project.getVCCHistory(lastBuildVersionSpec, currentBuildVersionSpec, true, Integer.MAX_VALUE);
Expand All @@ -69,7 +71,7 @@ public List<ChangeSet> checkout(final Server server, final FilePath workspacePat
public List<ChangeSet> checkoutBySingleVersionSpec(Server server, FilePath workspacePath, String singleVersionSpec) throws IOException, InterruptedException {
Project project = getProject(server, workspacePath);
final String normalizedFolder = determineCheckoutPath(workspacePath, localFolder);
project.getFiles(normalizedFolder, singleVersionSpec);
project.getFiles(normalizedFolder, singleVersionSpec, useOverwrite);

return project.getDetailedHistory(singleVersionSpec);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,22 @@ public class GetFilesToWorkFolderCommand extends AbstractCallableCommand<Void, E

private final String workFolder;
private final String versionSpec;
private final boolean useOverwrite;
private final boolean shouldLogEachGet;
private PrintStream logger;
private int getCount = 0;

public GetFilesToWorkFolderCommand(final ServerConfigurationProvider server, final String workFolder, final String versionSpec) {
this(server, workFolder, versionSpec, false);
public GetFilesToWorkFolderCommand(final ServerConfigurationProvider server, final String workFolder, final String versionSpec, boolean useOverwrite) {
this(server, workFolder, versionSpec, useOverwrite, false);
// using shouldLogEachGet false as default, could be controlled by a config option at a later stage if desired, just adds noise to log though
}

public GetFilesToWorkFolderCommand(final ServerConfigurationProvider server, final String workFolder, final String versionSpec,
public GetFilesToWorkFolderCommand(final ServerConfigurationProvider server, final String workFolder, final String versionSpec, boolean useOverwrite,
final boolean shouldLogEachGet) {
super(server);
this.workFolder = workFolder;
this.versionSpec = versionSpec;
this.useOverwrite = useOverwrite;
this.shouldLogEachGet = shouldLogEachGet;
}

Expand Down Expand Up @@ -66,7 +68,7 @@ public Void call() throws Exception {
final Workspace workspace = vcc.getWorkspace(workFolder);
final VersionControlEventEngine eventEngine = vcc.getEventEngine();
eventEngine.addGetListener(this);
workspace.get(getVersionSpec, GetOptions.OVERWRITE);
workspace.get(getVersionSpec, useOverwrite ? GetOptions.OVERWRITE : GetOptions.NONE);
eventEngine.removeGetListener(this);

final String gotMessage = String.format(GotTemplate, versionSpecString, getCount);
Expand Down
8 changes: 3 additions & 5 deletions tfs/src/main/java/hudson/plugins/tfs/model/Project.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@
import hudson.plugins.tfs.commands.RemoteChangesetVersionCommand;
import hudson.plugins.tfs.model.ChangeSet.Item;

import java.io.IOException;
import java.io.Reader;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collection;
Expand Down Expand Up @@ -255,9 +252,10 @@ static boolean isChangesetFullyCloaked(final Collection<String> changesetPaths,
* Gets all files from server.
* @param localPath the local path to get all files into
* @param versionSpec the version spec to use when getting the files
* @param useOverwrite if should overwrite changes
*/
public void getFiles(String localPath, String versionSpec) {
GetFilesToWorkFolderCommand command = new GetFilesToWorkFolderCommand(server, localPath, versionSpec);
public void getFiles(String localPath, String versionSpec, boolean useOverwrite) {
GetFilesToWorkFolderCommand command = new GetFilesToWorkFolderCommand(server, localPath, versionSpec, useOverwrite);
server.execute(command.getCallable());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@
<f:checkbox default="true"/>
</f:entry>

<f:entry field="useOverwrite" title="Use overwrite" description="If checked, all changed files in workspace will be overwritten by files from the server.">
<f:checkbox default="true"/>
</f:entry>

<f:entry field="localPath" title="Local workfolder">
<f:textbox default="."
clazz="required" checkMessage="${%Local workfolder is mandatory, empty field will use job workspace as workfolder.}"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ public class TeamFoundationServerScmTest {
" <userName>example\\tfsbuilder</userName>\n" +
" <credentialsConfigurer class=\"hudson.plugins.tfs.model.ManualCredentialsConfigurer\"/>\n" +
" <useUpdate>false</useUpdate>\n" +
" <useOverwrite>false</useOverwrite>\n" +
"</hudson.plugins.tfs.TeamFoundationServerScm>";

final String actualUpgradedXml = serializer.toXML(tfsScmObject);
Expand Down
Loading

0 comments on commit 8b9d691

Please sign in to comment.