1 package org.apache.maven.scm.provider.git.jgit.command.tag;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import org.apache.maven.scm.ScmException;
23 import org.apache.maven.scm.ScmFile;
24 import org.apache.maven.scm.ScmFileSet;
25 import org.apache.maven.scm.ScmFileStatus;
26 import org.apache.maven.scm.ScmResult;
27 import org.apache.maven.scm.ScmTagParameters;
28 import org.apache.maven.scm.command.tag.AbstractTagCommand;
29 import org.apache.maven.scm.command.tag.TagScmResult;
30 import org.apache.maven.scm.provider.ScmProviderRepository;
31 import org.apache.maven.scm.provider.git.command.GitCommand;
32 import org.apache.maven.scm.provider.git.jgit.command.JGitUtils;
33 import org.apache.maven.scm.provider.git.repository.GitScmProviderRepository;
34 import org.codehaus.plexus.util.StringUtils;
35 import org.eclipse.jgit.api.Git;
36 import org.eclipse.jgit.lib.Constants;
37 import org.eclipse.jgit.lib.Ref;
38 import org.eclipse.jgit.revwalk.RevCommit;
39 import org.eclipse.jgit.revwalk.RevWalk;
40 import org.eclipse.jgit.transport.RefSpec;
41 import org.eclipse.jgit.treewalk.TreeWalk;
42
43 import java.util.ArrayList;
44 import java.util.List;
45
46
47
48
49
50
51 public class JGitTagCommand
52 extends AbstractTagCommand
53 implements GitCommand
54 {
55
56 public ScmResult executeTagCommand( ScmProviderRepository repo, ScmFileSet fileSet, String tag, String message )
57 throws ScmException
58 {
59 return executeTagCommand( repo, fileSet, tag, new ScmTagParameters( message ) );
60 }
61
62
63
64
65 public ScmResult executeTagCommand( ScmProviderRepository repo, ScmFileSet fileSet, String tag,
66 ScmTagParameters scmTagParameters )
67 throws ScmException
68 {
69 if ( tag == null || StringUtils.isEmpty( tag.trim() ) )
70 {
71 throw new ScmException( "tag name must be specified" );
72 }
73
74 if ( !fileSet.getFileList().isEmpty() )
75 {
76 throw new ScmException( "This provider doesn't support tagging subsets of a directory" );
77 }
78
79 String escapedTagName = tag.trim().replace( ' ', '_' );
80
81 Git git = null;
82 try
83 {
84 git = Git.open( fileSet.getBasedir() );
85
86
87 String tagMessage = scmTagParameters.getMessage();
88 Ref tagRef = git.tag().setName( escapedTagName ).setMessage( tagMessage ).setForceUpdate( false ).call();
89
90 if ( repo.isPushChanges() )
91 {
92 getLogger().info( "push tag [" + escapedTagName + "] to remote..." );
93 JGitUtils.push( getLogger(), git, (GitScmProviderRepository) repo, new RefSpec( Constants.R_TAGS
94 + escapedTagName ) );
95 }
96
97
98 RevWalk revWalk = new RevWalk( git.getRepository() );
99 RevCommit commit = revWalk.parseCommit( tagRef.getObjectId() );
100 revWalk.release();
101
102 final TreeWalk walk = new TreeWalk( git.getRepository() );
103 walk.reset();
104 walk.setRecursive( true );
105 walk.addTree( commit.getTree() );
106
107 List<ScmFile> taggedFiles = new ArrayList<ScmFile>();
108 while ( walk.next() )
109 {
110 taggedFiles.add( new ScmFile( walk.getPathString(), ScmFileStatus.CHECKED_OUT ) );
111 }
112 walk.release();
113
114 return new TagScmResult( "JGit tag", taggedFiles );
115 }
116 catch ( Exception e )
117 {
118 throw new ScmException( "JGit tag failure!", e );
119 }
120 finally
121 {
122 JGitUtils.closeRepo( git );
123 }
124 }
125
126 }