Skip to content

Commit 16e5593

Browse files
authored
Merge pull request #49 from 2ndquadrant-it/avoid-failing-when-delete-a-branch
Avoid failing when delete or rename a branch
2 parents 4f76d48 + 16704b2 commit 16e5593

File tree

4 files changed

+44
-7
lines changed

4 files changed

+44
-7
lines changed

src/main/java/hudson/plugins/scm_sync_configuration/SCMManipulator.java

+13-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import org.apache.maven.scm.command.checkin.CheckInScmResult;
1010
import org.apache.maven.scm.command.checkout.CheckOutScmResult;
1111
import org.apache.maven.scm.command.remove.RemoveScmResult;
12+
import org.apache.maven.scm.command.status.StatusScmResult;
1213
import org.apache.maven.scm.command.update.UpdateScmResult;
1314
import org.apache.maven.scm.manager.NoSuchScmProviderException;
1415
import org.apache.maven.scm.manager.ScmManager;
@@ -18,6 +19,7 @@
1819
import java.io.IOException;
1920
import java.util.ArrayList;
2021
import java.util.Arrays;
22+
import java.util.Iterator;
2123
import java.util.List;
2224
import java.util.logging.Level;
2325
import java.util.logging.Logger;
@@ -124,7 +126,17 @@ public List<File> deleteHierarchy(File hierarchyToDelete){
124126

125127
try {
126128
ScmFileSet deleteFileSet = new ScmFileSet(enclosingDirectory, hierarchyToDelete);
127-
RemoveScmResult removeResult = this.scmManager.remove(this.scmRepository, deleteFileSet, "");
129+
StatusScmResult checkForChanges = this.scmManager.status(scmRepository, deleteFileSet);
130+
LOGGER.fine("Checking for changes on SCM hierarchy ["+hierarchyToDelete.getAbsolutePath()+"] from SCM ...");
131+
for (ScmFile changedFile : checkForChanges.getChangedFiles()) {
132+
//check in this change as it affect our hierarchy
133+
LOGGER.fine("[checkForChanges] Found changed file "+changedFile.toString()+", try to check-in...");
134+
CheckInScmResult checkedInChangedFile = scmManager.checkIn(scmRepository, new ScmFileSet(enclosingDirectory.getParentFile(), new File(changedFile.getPath())), "Check-In changes for "+changedFile.getPath());
135+
if(!checkedInChangedFile.isSuccess()){
136+
LOGGER.severe("[checkForChanges] Failed to check-in changed file ["+changedFile.getPath()+"]: "+checkedInChangedFile.getProviderMessage());
137+
}
138+
}
139+
RemoveScmResult removeResult = this.scmManager.remove(this.scmRepository, deleteFileSet, "Delete hierarchy "+hierarchyToDelete.getPath());
128140
if(!removeResult.isSuccess()){
129141
LOGGER.severe("[deleteHierarchy] Problem during remove : "+removeResult.getProviderMessage());
130142
return null;

src/main/java/hudson/plugins/scm_sync_configuration/ScmSyncConfigurationBusiness.java

+23-3
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ public class ScmSyncConfigurationBusiness {
3939
private SCMManipulator scmManipulator;
4040
private File checkoutScmDirectory = null;
4141
private ScmSyncConfigurationStatusManager scmSyncConfigurationStatusManager = null;
42+
private List<String> manualSynchronizationIncludes = new ArrayList<String>();
4243

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

189190
try {
190-
FileUtils.copyDirectory(JenkinsFilesHelper.buildFileFromPathRelativeToHudsonRoot(pathRelativeToJenkinsRoot.getPath()),
191-
fileTranslatedInScm);
191+
File buildFileFromPathRelativeToHudsonRoot = JenkinsFilesHelper.buildFileFromPathRelativeToHudsonRoot(pathRelativeToJenkinsRoot.getPath());
192+
FileUtils.copyDirectory(buildFileFromPathRelativeToHudsonRoot, fileTranslatedInScm, new FileFilter() {
193+
@Override
194+
public boolean accept(File pathname) {
195+
if(pathname.getPath().endsWith(".xml")
196+
|| getManualSynchronizationIncludes().contains(pathname)){
197+
return true;
198+
}
199+
return false;
200+
}
201+
});
192202
} catch (IOException e) {
193203
throw new LoggableException("Error while copying file hierarchy to SCM directory", FileUtils.class, "copyDirectory", e);
194204
}
@@ -212,7 +222,8 @@ private void processCommitsQueue() {
212222
}
213223
for(Path path : commit.getChangeset().getPathsToDelete()){
214224
List<File> deletedFiles = deleteHierarchy(commit.getScmContext(), path);
215-
updatedFiles.addAll(deletedFiles);
225+
if(deletedFiles != null)
226+
updatedFiles.addAll(deletedFiles);
216227
}
217228

218229
if(updatedFiles.isEmpty()){
@@ -245,6 +256,15 @@ private void processCommitsQueue() {
245256
}
246257
}
247258

259+
public List<String> getManualSynchronizationIncludes() {
260+
return manualSynchronizationIncludes;
261+
}
262+
263+
public void setManualSynchronizationIncludes(
264+
List<String> manualSynchronizationIncludes) {
265+
this.manualSynchronizationIncludes = manualSynchronizationIncludes;
266+
}
267+
248268
private boolean writeScmContentOnlyIfItDiffers(Path pathRelativeToJenkinsRoot, byte[] content, File fileTranslatedInScm)
249269
throws LoggableException {
250270
boolean scmContentUpdated = false;

src/main/java/hudson/plugins/scm_sync_configuration/ScmSyncConfigurationPlugin.java

+3
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@ public void loadData(ScmSyncConfigurationPOJO pojo){
164164
this.displayStatus = pojo.isDisplayStatus();
165165
this.commitMessagePattern = pojo.getCommitMessagePattern();
166166
this.manualSynchronizationIncludes = pojo.getManualSynchronizationIncludes();
167+
this.business.setManualSynchronizationIncludes(manualSynchronizationIncludes);
167168
}
168169

169170
protected void initialInit() throws Exception {
@@ -228,6 +229,8 @@ public void configure(StaplerRequest req, JSONObject formData)
228229
this.manualSynchronizationIncludes = new ArrayList<String>();
229230
}
230231

232+
this.business.setManualSynchronizationIncludes(manualSynchronizationIncludes);
233+
231234
// Repo initialization should be made _before_ plugin save, in order to let scm-sync-configuration.xml
232235
// file synchronizable
233236
if(repoInitializationRequired){

src/main/java/hudson/plugins/scm_sync_configuration/model/ChangeSet.java

+5-3
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,10 @@ public void registerPath(String path) {
5858
public void registerPathForDeletion(String path){
5959
// We should determine if path is a directory by watching scm path (and not hudson path) because in most of time,
6060
// when we are here, directory is already deleted in hudson hierarchy...
61-
boolean isDirectory = new Path(path).getScmFile().isDirectory();
62-
pathsToDelete.add(new Path(path, isDirectory));
61+
if(new Path(path).getScmFile().exists()) {
62+
boolean isDirectory = new Path(path).getScmFile().isDirectory();
63+
pathsToDelete.add(new Path(path, isDirectory));
64+
}
6365
}
6466

6567
public boolean isEmpty(){
@@ -75,7 +77,7 @@ public Map<Path, byte[]> getPathContents(){
7577
for(Path pathToAdd : filteredPathContents.keySet()){
7678
for(Path pathToDelete : pathsToDelete){
7779
// Removing paths being both in pathsToDelete and pathContents
78-
if(pathToDelete.contains(pathToAdd)){
80+
if(pathToDelete.equals(pathToAdd)){
7981
filteredPaths.add(pathToAdd);
8082
}
8183
}

0 commit comments

Comments
 (0)