22
22
package org .jboss .forge .git ;
23
23
24
24
import java .io .IOException ;
25
+ import java .util .List ;
25
26
26
27
import org .eclipse .jgit .api .CheckoutCommand ;
27
28
import org .eclipse .jgit .api .CreateBranchCommand .SetupUpstreamMode ;
28
29
import org .eclipse .jgit .api .FetchCommand ;
29
30
import org .eclipse .jgit .api .Git ;
31
+ import org .eclipse .jgit .api .ListBranchCommand ;
30
32
import org .eclipse .jgit .api .PullCommand ;
31
33
import org .eclipse .jgit .api .PullResult ;
34
+ import org .eclipse .jgit .api .TagCommand ;
32
35
import org .eclipse .jgit .api .errors .CanceledException ;
36
+ import org .eclipse .jgit .api .errors .ConcurrentRefUpdateException ;
33
37
import org .eclipse .jgit .api .errors .DetachedHeadException ;
34
38
import org .eclipse .jgit .api .errors .InvalidConfigurationException ;
35
39
import org .eclipse .jgit .api .errors .InvalidRefNameException ;
36
40
import org .eclipse .jgit .api .errors .InvalidRemoteException ;
41
+ import org .eclipse .jgit .api .errors .InvalidTagNameException ;
37
42
import org .eclipse .jgit .api .errors .JGitInternalException ;
43
+ import org .eclipse .jgit .api .errors .NoHeadException ;
38
44
import org .eclipse .jgit .api .errors .RefAlreadyExistsException ;
39
45
import org .eclipse .jgit .api .errors .RefNotFoundException ;
40
46
import org .eclipse .jgit .api .errors .WrongRepositoryStateException ;
41
47
import org .eclipse .jgit .lib .Ref ;
42
48
import org .eclipse .jgit .lib .RepositoryBuilder ;
43
49
import org .eclipse .jgit .lib .TextProgressMonitor ;
50
+ import org .eclipse .jgit .revwalk .RevTag ;
44
51
import org .eclipse .jgit .transport .FetchResult ;
45
52
import org .eclipse .jgit .transport .RefSpec ;
46
53
import org .jboss .forge .resources .DirectoryResource ;
54
+ import org .jboss .forge .resources .FileResource ;
47
55
48
56
/**
49
57
* Convenience tools for interacting with the Git version control system.
52
60
*/
53
61
public abstract class GitUtils
54
62
{
55
- public static Git clone (DirectoryResource dir , String repoUri ) throws IOException
63
+ public static Git clone (final DirectoryResource dir , final String repoUri ) throws IOException
56
64
{
57
65
new Clone ().run (dir .getUnderlyingResourceObject (), repoUri );
58
66
return git (dir );
59
67
}
60
68
61
- public static Git git (DirectoryResource dir ) throws IOException
69
+ public static Git git (final DirectoryResource dir ) throws IOException
62
70
{
63
71
RepositoryBuilder db = new RepositoryBuilder ().findGitDir (dir .getUnderlyingResourceObject ());
64
72
return new Git (db .build ());
65
73
}
66
74
67
- public static Ref checkout (Git git , String remote , boolean createBranch , SetupUpstreamMode mode , boolean force )
75
+ public static Ref checkout (final Git git , final String remote , final boolean createBranch ,
76
+ final SetupUpstreamMode mode , final boolean force )
68
77
throws JGitInternalException ,
69
78
RefAlreadyExistsException , RefNotFoundException , InvalidRefNameException
70
79
{
@@ -76,9 +85,22 @@ public static Ref checkout(Git git, String remote, boolean createBranch, SetupUp
76
85
return checkout .call ();
77
86
}
78
87
79
- public static FetchResult fetch (Git git , String remote , String refSpec , int timeout , boolean fsck , boolean dryRun ,
80
- boolean thin ,
81
- boolean prune ) throws JGitInternalException , InvalidRemoteException
88
+ public static Ref checkout (final Git git , final Ref remote , final boolean createBranch ,
89
+ final SetupUpstreamMode mode , final boolean force )
90
+ throws JGitInternalException ,
91
+ RefAlreadyExistsException , RefNotFoundException , InvalidRefNameException
92
+ {
93
+ CheckoutCommand checkout = git .checkout ();
94
+ checkout .setName (remote .getName ());
95
+ checkout .setForce (force );
96
+ checkout .setUpstreamMode (mode );
97
+ return checkout .call ();
98
+ }
99
+
100
+ public static FetchResult fetch (final Git git , final String remote , final String refSpec , final int timeout ,
101
+ final boolean fsck , final boolean dryRun ,
102
+ final boolean thin ,
103
+ final boolean prune ) throws JGitInternalException , InvalidRemoteException
82
104
{
83
105
FetchCommand fetch = git .fetch ();
84
106
fetch .setCheckFetchedObjects (fsck );
@@ -96,7 +118,23 @@ public static FetchResult fetch(Git git, String remote, String refSpec, int time
96
118
return result ;
97
119
}
98
120
99
- public static PullResult pull (Git git , int timeout ) throws WrongRepositoryStateException ,
121
+ /**
122
+ * Initialize a new git repository.
123
+ *
124
+ * @param dir The directory in which to create a new .git/ folder and repository.
125
+ */
126
+ public static Git init (final DirectoryResource dir ) throws IllegalArgumentException , IOException
127
+ {
128
+ FileResource <?> gitDir = dir .getChildDirectory (".git" ).reify (FileResource .class );
129
+ gitDir .mkdirs ();
130
+
131
+ RepositoryBuilder db = new RepositoryBuilder ().setGitDir (gitDir .getUnderlyingResourceObject ()).setup ();
132
+ Git git = new Git (db .build ());
133
+ git .getRepository ().create ();
134
+ return git ;
135
+ }
136
+
137
+ public static PullResult pull (final Git git , final int timeout ) throws WrongRepositoryStateException ,
100
138
InvalidConfigurationException , DetachedHeadException , InvalidRemoteException , CanceledException
101
139
{
102
140
PullCommand pull = git .pull ();
@@ -107,4 +145,23 @@ public static PullResult pull(Git git, int timeout) throws WrongRepositoryStateE
107
145
PullResult result = pull .call ();
108
146
return result ;
109
147
}
148
+
149
+ public static List <Ref > getBranches (final Git repo ) throws JGitInternalException , ConcurrentRefUpdateException ,
150
+ InvalidTagNameException , NoHeadException
151
+ {
152
+ ListBranchCommand branchListCommand = repo .branchList ();
153
+ List <Ref > branchList = branchListCommand .call ();
154
+
155
+ return branchList ;
156
+ }
157
+
158
+ public static List <Ref > getTags (final Git repo ) throws JGitInternalException , ConcurrentRefUpdateException ,
159
+ InvalidTagNameException , NoHeadException
160
+ {
161
+ TagCommand tagCommand = repo .tag ();
162
+ RevTag revTag = tagCommand .call ();
163
+ String tagName = revTag .getTagName ();
164
+
165
+ return null ;
166
+ }
110
167
}
0 commit comments