Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixing renaming and deletion issues #32 #52

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,24 @@

import hudson.plugins.scm_sync_configuration.model.ScmContext;
import hudson.plugins.scm_sync_configuration.scms.SCM;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.apache.maven.scm.ScmException;
import org.apache.maven.scm.ScmFile;
import org.apache.maven.scm.ScmFileSet;
import org.apache.maven.scm.command.add.AddScmResult;
import org.apache.maven.scm.command.checkin.CheckInScmResult;
import org.apache.maven.scm.command.checkout.CheckOutScmResult;
import org.apache.maven.scm.command.remove.RemoveScmResult;
import org.apache.maven.scm.command.status.StatusScmResult;
import org.apache.maven.scm.command.update.UpdateScmResult;
import org.apache.maven.scm.manager.NoSuchScmProviderException;
import org.apache.maven.scm.manager.ScmManager;
Expand Down Expand Up @@ -124,7 +135,17 @@ public List<File> deleteHierarchy(File hierarchyToDelete){

try {
ScmFileSet deleteFileSet = new ScmFileSet(enclosingDirectory, hierarchyToDelete);
RemoveScmResult removeResult = this.scmManager.remove(this.scmRepository, deleteFileSet, "");
StatusScmResult checkForChanges = this.scmManager.status(scmRepository, deleteFileSet);
LOGGER.fine("Checking for changes on SCM hierarchy ["+hierarchyToDelete.getAbsolutePath()+"] from SCM ...");
for (ScmFile changedFile : checkForChanges.getChangedFiles()) {
//check in this change as it affect our hierarchy
LOGGER.fine("[checkForChanges] Found changed file "+changedFile.toString()+", try to check-in...");
CheckInScmResult checkedInChangedFile = scmManager.checkIn(scmRepository, new ScmFileSet(enclosingDirectory.getParentFile(), new File(changedFile.getPath())), "Check-In changes for "+changedFile.getPath());
if(!checkedInChangedFile.isSuccess()){
LOGGER.severe("[checkForChanges] Failed to check-in changed file ["+changedFile.getPath()+"]: "+checkedInChangedFile.getProviderMessage());
}
}
RemoveScmResult removeResult = this.scmManager.remove(this.scmRepository, deleteFileSet, "Delete hierarchy "+hierarchyToDelete.getPath());
if(!removeResult.isSuccess()){
LOGGER.severe("[deleteHierarchy] Problem during remove : "+removeResult.getProviderMessage());
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public class ScmSyncConfigurationBusiness {
private SCMManipulator scmManipulator;
private File checkoutScmDirectory = null;
private ScmSyncConfigurationStatusManager scmSyncConfigurationStatusManager = null;
private List<String> manualSynchronizationIncludes = new ArrayList<String>();

/**
* Use of a size 1 thread pool frees us from worrying about accidental thread death and
Expand Down Expand Up @@ -187,8 +188,17 @@ private void processCommitsQueue() {
String firstNonExistingParentScmPath = pathRelativeToJenkinsRoot.getFirstNonExistingParentScmPath();

try {
FileUtils.copyDirectory(JenkinsFilesHelper.buildFileFromPathRelativeToHudsonRoot(pathRelativeToJenkinsRoot.getPath()),
fileTranslatedInScm);
File buildFileFromPathRelativeToHudsonRoot = JenkinsFilesHelper.buildFileFromPathRelativeToHudsonRoot(pathRelativeToJenkinsRoot.getPath());
FileUtils.copyDirectory(buildFileFromPathRelativeToHudsonRoot, fileTranslatedInScm, new FileFilter() {
@Override
public boolean accept(File pathname) {
if(pathname.getPath().endsWith(".xml")
|| getManualSynchronizationIncludes().contains(pathname)){
return true;
}
return false;
}
});
} catch (IOException e) {
throw new LoggableException("Error while copying file hierarchy to SCM directory", FileUtils.class, "copyDirectory", e);
}
Expand All @@ -212,7 +222,8 @@ private void processCommitsQueue() {
}
for(Path path : commit.getChangeset().getPathsToDelete()){
List<File> deletedFiles = deleteHierarchy(commit.getScmContext(), path);
updatedFiles.addAll(deletedFiles);
if(deletedFiles != null)
updatedFiles.addAll(deletedFiles);
}

if(updatedFiles.isEmpty()){
Expand Down Expand Up @@ -245,6 +256,15 @@ private void processCommitsQueue() {
}
}

public List<String> getManualSynchronizationIncludes() {
return manualSynchronizationIncludes;
}

public void setManualSynchronizationIncludes(
List<String> manualSynchronizationIncludes) {
this.manualSynchronizationIncludes = manualSynchronizationIncludes;
}

private boolean writeScmContentOnlyIfItDiffers(Path pathRelativeToJenkinsRoot, byte[] content, File fileTranslatedInScm)
throws LoggableException {
boolean scmContentUpdated = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,8 @@ public void loadData(ScmSyncConfigurationPOJO pojo){
this.displayStatus = pojo.isDisplayStatus();
this.commitMessagePattern = pojo.getCommitMessagePattern();
this.manualSynchronizationIncludes = pojo.getManualSynchronizationIncludes();
}
this.business.setManualSynchronizationIncludes(manualSynchronizationIncludes);
}

protected void initialInit() throws Exception {
// We need to init() here in addition to ScmSyncConfigurationItemListener.onLoaded() to ensure that we do
Expand Down Expand Up @@ -227,6 +228,8 @@ public void configure(StaplerRequest req, JSONObject formData)
} else {
this.manualSynchronizationIncludes = new ArrayList<String>();
}

this.business.setManualSynchronizationIncludes(manualSynchronizationIncludes);

// Repo initialization should be made _before_ plugin save, in order to let scm-sync-configuration.xml
// file synchronizable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public Map<Path, byte[]> getPathContents(){
for(Path pathToAdd : filteredPathContents.keySet()){
for(Path pathToDelete : pathsToDelete){
// Removing paths being both in pathsToDelete and pathContents
if(pathToDelete.contains(pathToAdd)){
if(pathToDelete.equals(pathToAdd)){
filteredPaths.add(pathToAdd);
}
}
Expand Down