Skip to content

Commit dbf5431

Browse files
committed
JENKINS-15128: Fix job rename with Git by providing a custom GitExeScmProvider allowing to explicitely set "origin" as remote branch during push (in order to update local reference to avoid problem during subsequent call)
1 parent 5fd7d4b commit dbf5431

File tree

2 files changed

+200
-2
lines changed

2 files changed

+200
-2
lines changed
Lines changed: 198 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,198 @@
1+
package hudson.plugins.scm_sync_configuration.scms.customproviders.git.gitexe;
2+
3+
import org.apache.commons.io.FilenameUtils;
4+
import org.apache.maven.scm.*;
5+
import org.apache.maven.scm.command.checkin.CheckInScmResult;
6+
import org.apache.maven.scm.log.ScmLogger;
7+
import org.apache.maven.scm.provider.ScmProviderRepository;
8+
import org.apache.maven.scm.provider.git.command.GitCommand;
9+
import org.apache.maven.scm.provider.git.gitexe.GitExeScmProvider;
10+
import org.apache.maven.scm.provider.git.gitexe.command.GitCommandLineUtils;
11+
import org.apache.maven.scm.provider.git.gitexe.command.add.GitAddCommand;
12+
import org.apache.maven.scm.provider.git.gitexe.command.branch.GitBranchCommand;
13+
import org.apache.maven.scm.provider.git.gitexe.command.checkin.GitCheckInCommand;
14+
import org.apache.maven.scm.provider.git.gitexe.command.status.GitStatusCommand;
15+
import org.apache.maven.scm.provider.git.gitexe.command.status.GitStatusConsumer;
16+
import org.apache.maven.scm.provider.git.repository.GitScmProviderRepository;
17+
import org.codehaus.plexus.util.FileUtils;
18+
import org.codehaus.plexus.util.cli.CommandLineUtils;
19+
import org.codehaus.plexus.util.cli.Commandline;
20+
21+
import java.io.File;
22+
import java.io.IOException;
23+
import java.util.ArrayList;
24+
import java.util.List;
25+
26+
/**
27+
* @author fcamblor
28+
* Crappy hack because for the moment, in maven-scmprovider-gitext 1.8.1, when we checkIn file,
29+
* checkin is originally made by passing pushUrl instead of a local reference (such as "origin")
30+
* Problem is passing pushUrl doesn't update current local reference once pushed => after push,
31+
* origin/<targetBranch> will not be updated to latest commit => on next push, there will be an
32+
* error saying some pull is needed.
33+
* This workaround could be betterly handled when something like "checkinAndFetch" could be
34+
* implemented generically in maven-scm-api
35+
* (see http://maven.40175.n5.nabble.com/SCM-GitExe-no-fetch-after-push-td5745064.html)
36+
*/
37+
public class ScmSyncGitExeScmProvider extends GitExeScmProvider {
38+
public static class ScmSyncGitCheckInCommand extends GitCheckInCommand {
39+
// Retrieved implementation from GitCheckInCommande v1.8.1, only overriding call to createPushCommandLine()
40+
// by a *custom* implementation
41+
@Override
42+
protected CheckInScmResult executeCheckInCommand(
43+
ScmProviderRepository repo, ScmFileSet fileSet, String message, ScmVersion version ) throws ScmException {
44+
45+
GitScmProviderRepository repository = (GitScmProviderRepository) repo;
46+
47+
CommandLineUtils.StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer();
48+
CommandLineUtils.StringStreamConsumer stdout = new CommandLineUtils.StringStreamConsumer();
49+
50+
int exitCode;
51+
52+
File messageFile = FileUtils.createTempFile("maven-scm-", ".commit", null);
53+
try
54+
{
55+
FileUtils.fileWrite( messageFile.getAbsolutePath(), message );
56+
}
57+
catch ( IOException ex )
58+
{
59+
return new CheckInScmResult( null, "Error while making a temporary file for the commit message: "
60+
+ ex.getMessage(), null, false );
61+
}
62+
63+
try
64+
{
65+
if ( !fileSet.getFileList().isEmpty() )
66+
{
67+
// if specific fileSet is given, we have to git-add them first
68+
// otherwise we will use 'git-commit -a' later
69+
70+
Commandline clAdd = GitAddCommand.createCommandLine(fileSet.getBasedir(), fileSet.getFileList());
71+
72+
exitCode = GitCommandLineUtils.execute(clAdd, stdout, stderr, getLogger());
73+
74+
if ( exitCode != 0 )
75+
{
76+
return new CheckInScmResult( clAdd.toString(), "The git-add command failed.", stderr.getOutput(),
77+
false );
78+
}
79+
80+
}
81+
82+
// git-commit doesn't show single files, but only summary :/
83+
// so we must run git-status and consume the output
84+
// borrow a few things from the git-status command
85+
Commandline clStatus = GitStatusCommand.createCommandLine(repository, fileSet);
86+
87+
GitStatusConsumer statusConsumer = new GitStatusConsumer( getLogger(), fileSet.getBasedir() );
88+
exitCode = GitCommandLineUtils.execute( clStatus, statusConsumer, stderr, getLogger() );
89+
if ( exitCode != 0 )
90+
{
91+
// git-status returns non-zero if nothing to do
92+
if ( getLogger().isInfoEnabled() )
93+
{
94+
getLogger().info( "nothing added to commit but untracked files present (use \"git add\" to " +
95+
"track)" );
96+
}
97+
}
98+
99+
if ( statusConsumer.getChangedFiles().isEmpty() )
100+
{
101+
return new CheckInScmResult( null, statusConsumer.getChangedFiles() );
102+
}
103+
104+
Commandline clCommit = createCommitCommandLine( repository, fileSet, messageFile );
105+
106+
exitCode = GitCommandLineUtils.execute( clCommit, stdout, stderr, getLogger() );
107+
if ( exitCode != 0 )
108+
{
109+
return new CheckInScmResult( clCommit.toString(), "The git-commit command failed.", stderr.getOutput(),
110+
false );
111+
}
112+
113+
if( repo.isPushChanges() )
114+
{
115+
Commandline cl = createSpecificPushCommandLine( getLogger(), repository, fileSet, version );
116+
117+
exitCode = GitCommandLineUtils.execute( cl, stdout, stderr, getLogger() );
118+
if ( exitCode != 0 )
119+
{
120+
return new CheckInScmResult( cl.toString(), "The git-push command failed.", stderr.getOutput(), false );
121+
}
122+
}
123+
124+
List<ScmFile> checkedInFiles = new ArrayList<ScmFile>( statusConsumer.getChangedFiles().size() );
125+
126+
// rewrite all detected files to now have status 'checked_in'
127+
for ( ScmFile changedFile : statusConsumer.getChangedFiles() )
128+
{
129+
ScmFile scmfile = new ScmFile( changedFile.getPath(), ScmFileStatus.CHECKED_IN );
130+
131+
if ( fileSet.getFileList().isEmpty() )
132+
{
133+
checkedInFiles.add( scmfile );
134+
}
135+
else
136+
{
137+
// if a specific fileSet is given, we have to check if the file is really tracked
138+
for ( File f : fileSet.getFileList() )
139+
{
140+
if ( FilenameUtils.separatorsToUnix(f.getPath()).equals( scmfile.getPath() ) )
141+
{
142+
checkedInFiles.add( scmfile );
143+
}
144+
145+
}
146+
}
147+
}
148+
149+
return new CheckInScmResult( clCommit.toString(), checkedInFiles );
150+
}
151+
finally
152+
{
153+
try
154+
{
155+
FileUtils.forceDelete( messageFile );
156+
}
157+
catch ( IOException ex )
158+
{
159+
// ignore
160+
}
161+
}
162+
163+
}
164+
165+
// ----------------------------------------------------------------------
166+
//
167+
// ----------------------------------------------------------------------
168+
169+
public static Commandline createSpecificPushCommandLine( ScmLogger logger, GitScmProviderRepository repository,
170+
ScmFileSet fileSet, ScmVersion version )
171+
throws ScmException
172+
{
173+
Commandline cl = GitCommandLineUtils.getBaseGitCommandLine( fileSet.getBasedir(), "push" );
174+
175+
String branch = GitBranchCommand.getCurrentBranch(logger, repository, fileSet);
176+
177+
if ( branch == null || branch.length() == 0 )
178+
{
179+
throw new ScmException( "Could not detect the current branch. Don't know where I should push to!" );
180+
}
181+
182+
// Overloaded branch name here : if repository.getUrl() is kept, during checkin(), current *local* branch
183+
// reference is not updated, whereas by using origin, it will be done !
184+
cl.createArg().setValue( "origin" );
185+
186+
cl.createArg().setValue( branch + ":" + branch );
187+
188+
return cl;
189+
}
190+
191+
}
192+
193+
@Override
194+
protected GitCommand getCheckInCommand() {
195+
return new ScmSyncGitCheckInCommand();
196+
}
197+
198+
}

src/main/resources/META-INF/plexus/components.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ under the License.
4141
<component>
4242
<role>org.apache.maven.scm.provider.ScmProvider</role>
4343
<role-hint>git</role-hint>
44-
<implementation>org.apache.maven.scm.provider.git.gitexe.GitExeScmProvider</implementation>
44+
<implementation>hudson.plugins.scm_sync_configuration.scms.customproviders.git.gitexe.ScmSyncGitExeScmProvider</implementation>
4545
</component>
4646

4747
</components>
48-
</component-set>
48+
</component-set>

0 commit comments

Comments
 (0)