diff --git a/src/main/java/hudson/plugins/scm_sync_configuration/SCMManipulator.java b/src/main/java/hudson/plugins/scm_sync_configuration/SCMManipulator.java index 24de088f..3aafced7 100644 --- a/src/main/java/hudson/plugins/scm_sync_configuration/SCMManipulator.java +++ b/src/main/java/hudson/plugins/scm_sync_configuration/SCMManipulator.java @@ -2,6 +2,16 @@ 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; @@ -9,6 +19,7 @@ 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; @@ -124,7 +135,17 @@ public List 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; diff --git a/src/main/java/hudson/plugins/scm_sync_configuration/ScmSyncConfigurationBusiness.java b/src/main/java/hudson/plugins/scm_sync_configuration/ScmSyncConfigurationBusiness.java index a2feba7b..4383a35e 100644 --- a/src/main/java/hudson/plugins/scm_sync_configuration/ScmSyncConfigurationBusiness.java +++ b/src/main/java/hudson/plugins/scm_sync_configuration/ScmSyncConfigurationBusiness.java @@ -39,6 +39,7 @@ public class ScmSyncConfigurationBusiness { private SCMManipulator scmManipulator; private File checkoutScmDirectory = null; private ScmSyncConfigurationStatusManager scmSyncConfigurationStatusManager = null; + private List manualSynchronizationIncludes = new ArrayList(); /** * Use of a size 1 thread pool frees us from worrying about accidental thread death and @@ -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); } @@ -212,7 +222,8 @@ private void processCommitsQueue() { } for(Path path : commit.getChangeset().getPathsToDelete()){ List deletedFiles = deleteHierarchy(commit.getScmContext(), path); - updatedFiles.addAll(deletedFiles); + if(deletedFiles != null) + updatedFiles.addAll(deletedFiles); } if(updatedFiles.isEmpty()){ @@ -245,6 +256,15 @@ private void processCommitsQueue() { } } + public List getManualSynchronizationIncludes() { + return manualSynchronizationIncludes; + } + + public void setManualSynchronizationIncludes( + List manualSynchronizationIncludes) { + this.manualSynchronizationIncludes = manualSynchronizationIncludes; + } + private boolean writeScmContentOnlyIfItDiffers(Path pathRelativeToJenkinsRoot, byte[] content, File fileTranslatedInScm) throws LoggableException { boolean scmContentUpdated = false; diff --git a/src/main/java/hudson/plugins/scm_sync_configuration/ScmSyncConfigurationPlugin.java b/src/main/java/hudson/plugins/scm_sync_configuration/ScmSyncConfigurationPlugin.java index ef1b7da9..12774461 100644 --- a/src/main/java/hudson/plugins/scm_sync_configuration/ScmSyncConfigurationPlugin.java +++ b/src/main/java/hudson/plugins/scm_sync_configuration/ScmSyncConfigurationPlugin.java @@ -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 @@ -227,6 +228,8 @@ public void configure(StaplerRequest req, JSONObject formData) } else { this.manualSynchronizationIncludes = new ArrayList(); } + + this.business.setManualSynchronizationIncludes(manualSynchronizationIncludes); // Repo initialization should be made _before_ plugin save, in order to let scm-sync-configuration.xml // file synchronizable diff --git a/src/main/java/hudson/plugins/scm_sync_configuration/model/ChangeSet.java b/src/main/java/hudson/plugins/scm_sync_configuration/model/ChangeSet.java index d2927b29..7514f3ac 100644 --- a/src/main/java/hudson/plugins/scm_sync_configuration/model/ChangeSet.java +++ b/src/main/java/hudson/plugins/scm_sync_configuration/model/ChangeSet.java @@ -75,7 +75,7 @@ public Map 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); } }