9
9
import java .io .IOException ;
10
10
import java .nio .file .Files ;
11
11
import java .time .ZonedDateTime ;
12
+ import java .util .ArrayList ;
12
13
import java .util .List ;
13
14
import java .util .function .Supplier ;
14
15
import java .util .regex .Pattern ;
16
+ import java .util .stream .Collectors ;
15
17
16
18
import static java .time .Instant .EPOCH ;
17
19
import static java .time .ZoneOffset .UTC ;
18
20
import static java .util .Collections .emptyList ;
19
21
import static java .util .Objects .requireNonNull ;
22
+ import static java .util .stream .Collectors .toList ;
20
23
import static me .qoomon .gitversioning .commons .GitUtil .NO_COMMIT ;
21
24
import static org .eclipse .jgit .lib .Constants .HEAD ;
22
25
@@ -80,8 +83,19 @@ public String getBranch() {
80
83
return branch .get ();
81
84
}
82
85
83
- public void setBranch (String branch ) {
84
- this .branch = () -> branch ;
86
+ protected void setBranch (String branch ) {
87
+ if (branch != null ) {
88
+ if (branch .startsWith ("refs/tags/" )) {
89
+ throw new IllegalArgumentException ("invalid branch ref" + branch );
90
+ }
91
+ branch = branch
92
+ // support default branches (heads)
93
+ .replaceFirst ("^refs/heads/" , "" )
94
+ // support other refs e.g. GitHub pull requests refs/pull/1000/head
95
+ .replaceFirst ("^refs/" , "" );
96
+ }
97
+ final String finalBranch = branch ;
98
+ this .branch = () -> finalBranch ;
85
99
}
86
100
87
101
public boolean isDetached () {
@@ -92,8 +106,38 @@ public List<String> getTags() {
92
106
return tags .get ();
93
107
}
94
108
95
- public void setTags (List <String > tags ) {
96
- this .tags = () -> requireNonNull (tags );
109
+ protected void addTag (String tag ) {
110
+ requireNonNull (tag );
111
+
112
+ if (tag .startsWith ("refs/" ) && !tag .startsWith ("refs/tags/" )) {
113
+ throw new IllegalArgumentException ("invalid tag ref" + tag );
114
+ }
115
+
116
+ final String finalTag = tag .replaceFirst ("^refs/tags/" , "" );
117
+
118
+ final Supplier <List <String >> currentTags = this .tags ;
119
+ this .tags = Lazy .by (() -> {
120
+ List <String > tags = new ArrayList <>(currentTags .get ());
121
+ tags .add (finalTag );
122
+ return tags ;
123
+ });
124
+ }
125
+
126
+ protected void setTags (List <String > tags ) {
127
+ requireNonNull (tags );
128
+ tags .forEach (tag -> {
129
+ requireNonNull (tag );
130
+ if (tag .startsWith ("refs/" ) && !tag .startsWith ("refs/tags/" )) {
131
+ throw new IllegalArgumentException ("invalid tag ref" + tag );
132
+ }
133
+ });
134
+
135
+ tags = tags .stream ()
136
+ .map (tag -> tag .replaceFirst ("^refs/tags/" , "" ))
137
+ .collect (toList ());
138
+
139
+ final List <String > finalTags = tags ;
140
+ this .tags = () -> finalTags ;
97
141
}
98
142
99
143
public boolean isClean () {
0 commit comments