1
1
package me .qoomon .gitversioning .commons ;
2
2
3
+ import org .eclipse .jgit .lib .ObjectId ;
4
+ import org .eclipse .jgit .lib .Ref ;
5
+ import org .eclipse .jgit .lib .Repository ;
3
6
4
7
import java .io .File ;
5
- import java .time . Instant ;
8
+ import java .io . IOException ;
6
9
import java .time .ZonedDateTime ;
7
10
import java .util .List ;
11
+ import java .util .Map ;
12
+ import java .util .regex .Pattern ;
8
13
14
+ import static java .time .Instant .EPOCH ;
9
15
import static java .time .ZoneOffset .UTC ;
10
16
import static java .util .Collections .emptyList ;
11
- import static java .util .Objects .requireNonNull ;
17
+ import static me .qoomon .gitversioning .commons .GitUtil .NO_COMMIT ;
18
+ import static org .eclipse .jgit .lib .Constants .HEAD ;
12
19
20
+ //TODO rename to GitSituation
13
21
public class GitSituation {
14
22
15
- public static String NO_COMMIT = "0000000000000000000000000000000000000000" ;
16
-
23
+ private final Repository repository ;
17
24
private final File rootDirectory ;
18
- private final String headCommit ;
19
- private final long headCommitTimestamp ;
20
- private final String headBranch ;
21
- private final List <String > headTags ;
22
- private final boolean clean ;
23
25
24
- public GitSituation (File rootDirectory , String headCommit , long headCommitTimestamp , String headBranch , List <String > headTags , boolean clean ) {
25
- this .rootDirectory = requireNonNull (rootDirectory );
26
- this .headCommit = requireNonNull (headCommit );
27
- this .headCommitTimestamp = headCommitTimestamp ;
28
- this .headBranch = headBranch ;
29
- this .headTags = requireNonNull (headTags );
30
- this .clean = clean ;
26
+ private final ObjectId head ;
27
+ private final String hash ;
28
+ private final Lazy <ZonedDateTime > timestamp = Lazy .of (this ::timestamp );
29
+ private Lazy <String > branch = Lazy .of (this ::branch );
30
+
31
+ private final Lazy <Map <ObjectId , List <Ref >>> reverseTagRefMap = Lazy .of (this ::reverseTagRefMap );
32
+ private Lazy <List <String >> tags = Lazy .of (this ::tags );
33
+
34
+ private final Lazy <Boolean > clean = Lazy .of (this ::clean );
35
+
36
+ private final Pattern describeTagPattern ;
37
+ private final Lazy <GitDescription > description = Lazy .of (this ::describe );
38
+
39
+ public GitSituation (Repository repository , Pattern describeTagPattern ) throws IOException {
40
+ this .repository = repository ;
41
+ this .rootDirectory = repository .getWorkTree ();
42
+ this .head = repository .resolve (HEAD );
43
+ this .hash = head != null ? head .getName () : NO_COMMIT ;
44
+ this .describeTagPattern = describeTagPattern != null ? describeTagPattern : Pattern .compile (".*" );
45
+ }
46
+
47
+ public GitSituation (Repository repository ) throws IOException {
48
+ this (repository , null );
31
49
}
32
50
33
51
public File getRootDirectory () {
34
52
return rootDirectory ;
35
53
}
36
54
37
- public String getHeadCommit () {
38
- return headCommit ;
55
+ public String getHash () {
56
+ return hash ;
39
57
}
40
58
41
- public long getHeadCommitTimestamp () {
42
- return headCommitTimestamp ;
59
+ public ZonedDateTime getTimestamp () {
60
+ return timestamp . get () ;
43
61
}
44
62
45
- public ZonedDateTime getHeadCommitDateTime () {
46
- Instant headCommitTimestamp = Instant .ofEpochSecond (getHeadCommitTimestamp ());
47
- return ZonedDateTime .ofInstant (headCommitTimestamp , UTC );
63
+ public String getBranch () {
64
+ return branch .get ();
48
65
}
49
66
50
- public String getHeadBranch ( ) {
51
- return headBranch ;
67
+ public void setBranch ( String branch ) {
68
+ this . branch = Lazy . of ( branch ) ;
52
69
}
53
70
54
- public List <String > getHeadTags () {
55
- return headTags ;
71
+ public boolean isDetached () {
72
+ return branch .get () == null ;
73
+ }
74
+
75
+ public List <String > getTags () {
76
+ return tags .get ();
77
+ }
78
+
79
+ public void setTags (List <String > tags ) {
80
+ this .tags = Lazy .of (tags );
56
81
}
57
82
58
83
public boolean isClean () {
59
- return clean ;
84
+ return clean . get () ;
60
85
}
61
86
62
- public boolean isDetached () {
63
- return headBranch == null ;
64
- }
65
-
66
- public static class Builder {
67
- private File rootDirectory ;
68
- private String headCommit = NO_COMMIT ;
69
- private long headCommitTimestamp = 0 ;
70
- private String headBranch = null ;
71
- private List <String > headTags = emptyList ();
72
- private boolean clean = true ;
73
-
74
-
75
- public Builder setRootDirectory (File rootDirectory ) {
76
- this .rootDirectory = rootDirectory ;
77
- return this ;
78
- }
79
-
80
- public Builder setHeadCommit (String headCommit ) {
81
- this .headCommit = headCommit ;
82
- return this ;
83
- }
84
-
85
- public Builder setHeadCommitTimestamp (long headCommitTimestamp ) {
86
- this .headCommitTimestamp = headCommitTimestamp ;
87
- return this ;
88
- }
89
-
90
- public Builder setHeadBranch (String headBranch ) {
91
- this .headBranch = headBranch ;
92
- return this ;
93
- }
94
-
95
- public Builder setHeadTags (List <String > headTags ) {
96
- this .headTags = headTags ;
97
- return this ;
98
- }
99
-
100
- public Builder setClean (boolean clean ) {
101
- this .clean = clean ;
102
- return this ;
103
- }
104
-
105
- public GitSituation build () {
106
- return new GitSituation (rootDirectory , headCommit , headCommitTimestamp , headBranch , headTags , clean );
107
- }
108
-
109
- public static Builder of (GitSituation gitSituation ) {
110
- return new Builder ()
111
- .setRootDirectory (gitSituation .rootDirectory )
112
- .setClean (gitSituation .clean )
113
- .setHeadCommit (gitSituation .headCommit )
114
- .setHeadCommitTimestamp (gitSituation .headCommitTimestamp )
115
- .setHeadBranch (gitSituation .headBranch )
116
- .setHeadTags (gitSituation .headTags );
117
- }
87
+ public GitDescription getDescription () {
88
+ return description .get ();
89
+ }
90
+
91
+ // ----- initialization methods ------------------------------------------------------------------------------------
92
+
93
+ private ZonedDateTime timestamp () throws IOException {
94
+ return head != null
95
+ ? GitUtil .revTimestamp (repository , head )
96
+ : ZonedDateTime .ofInstant (EPOCH , UTC );
97
+ }
98
+
99
+ private String branch () throws IOException {
100
+ return GitUtil .branch (repository );
101
+ }
102
+
103
+ private List <String > tags () {
104
+ return head != null ? GitUtil .tagsPointAt (repository , head , reverseTagRefMap .get ()) : emptyList ();
105
+ }
106
+
107
+ private boolean clean () {
108
+ return GitUtil .status (repository ).isClean ();
109
+ }
110
+
111
+ private GitDescription describe () throws IOException {
112
+ return GitUtil .describe (repository , head , describeTagPattern , reverseTagRefMap .get ());
113
+ }
114
+
115
+ private Map <ObjectId , List <Ref >> reverseTagRefMap () throws IOException {
116
+ return GitUtil .reverseTagRefMap (repository );
118
117
}
119
118
}
0 commit comments