forked from guacsec/trustify-da-java-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGitVersionControlSystemImpl.java
More file actions
154 lines (139 loc) · 5.54 KB
/
Copy pathGitVersionControlSystemImpl.java
File metadata and controls
154 lines (139 loc) · 5.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/*
* Copyright © 2023 Red Hat, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.redhat.exhort.vcs;
import com.redhat.exhort.tools.Operations;
import java.nio.file.Path;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Arrays;
import java.util.regex.Pattern;
public class GitVersionControlSystemImpl implements VersionControlSystem {
private final String gitBinary;
public GitVersionControlSystemImpl() {
gitBinary = Operations.getCustomPathOrElse("git");
}
@Override
public TagInfo getLatestTag(Path repoLocation) {
TagInfo tagInfo = new TagInfo();
// get current commit hash digest
String commitHash =
Operations.runProcessGetOutput(repoLocation, gitBinary, "rev-parse", "HEAD").trim();
if (Pattern.matches("^[a-f0-9]+", commitHash)) {
tagInfo.setCurrentCommitDigest(commitHash);
// get current commit timestamp.
String timeStampFromGit =
Operations.runProcessGetOutput(
repoLocation,
gitBinary,
"show",
"HEAD",
"--format=%cI",
"--date",
"local",
"--quiet");
LocalDateTime commitTimestamp =
LocalDateTime.parse(timeStampFromGit.trim(), DateTimeFormatter.ISO_OFFSET_DATE_TIME);
tagInfo.setCommitTimestamp(commitTimestamp);
// go get last annotated tag
String resultFromInvocation =
Operations.runProcessGetOutput(repoLocation, gitBinary, "describe", "--abbrev=12").trim();
// if there are only unannotated tag, fetch last one.
if (resultFromInvocation.contains("there were unannotated tags")) {
// fetch last unannotated tag
resultFromInvocation =
Operations.runProcessGetOutput(
repoLocation, gitBinary, "describe", "--tags", "--abbrev=12")
.trim();
fetchLatestTag(tagInfo, resultFromInvocation);
} else {
if (resultFromInvocation.startsWith("fatal: No names found")) {
tagInfo.setCurrentCommitPointedByTag(false);
tagInfo.setTagName("");
}
// fetch last annotated tag
else {
fetchLatestTag(tagInfo, resultFromInvocation);
}
}
}
// empty git repo with no commits
else {
tagInfo.setTagName("");
tagInfo.setCurrentCommitPointedByTag(false);
tagInfo.setCommitTimestamp(
LocalDateTime.parse(LocalDateTime.MIN.toString(), DateTimeFormatter.ISO_LOCAL_DATE_TIME));
tagInfo.setCurrentCommitDigest("");
}
return tagInfo;
}
@Override
public boolean isDirectoryRepo(Path repoLocation) {
try {
String resultFromInvocation =
Operations.runProcessGetOutput(
repoLocation, gitBinary, "rev-parse", "--is-inside-work-tree");
return resultFromInvocation.trim().equals("true");
} catch (Exception e) {
return false;
}
}
@Override
public String getNextTagVersion(TagInfo tagInfo) {
String result = "";
// if tag version ends with a digit, then increment it by one, and append to the end -0.
if (Pattern.matches(".*[0-9]$", tagInfo.getTagName())) {
int length = tagInfo.getTagName().length();
Integer lastDigit = Integer.parseInt(tagInfo.getTagName().substring(length - 1, length));
lastDigit++;
result = String.format("%s%s-0", tagInfo.getTagName().substring(0, length - 1), lastDigit);
} else {
// if tag version ends with some suffix starting with '.' or '-', then just append to the end
// -0.
if (Pattern.matches(".*-[a-zA-Z0-9]+$|.*\\.[a-zA-Z0-9]+$", tagInfo.getTagName())) {
result = String.format("%s-0", tagInfo.getTagName());
}
}
return result;
}
public String getPseudoVersion(TagInfo tagInfo, String newTagVersion) {
String stringTS =
tagInfo.getCommitTimestamp().format(DateTimeFormatter.ofPattern("yyyyMMddHHmmss"));
String commitHash12 = tagInfo.getCurrentCommitDigest().substring(0, 12);
return String.format("%s.%s-%s", newTagVersion, stringTS, commitHash12);
}
private static void fetchLatestTag(TagInfo tagInfo, String resultFromInvocation) {
String[] parts = resultFromInvocation.split("-");
if (parts.length > 1) {
analyzeGitDescribeResult(tagInfo, parts);
} else {
tagInfo.setCurrentCommitPointedByTag(true);
tagInfo.setTagName(parts[0]);
}
}
private static void analyzeGitDescribeResult(TagInfo tagInfo, String[] parts) {
if (Pattern.matches("g[0-9a-f]{12}", parts[parts.length - 1])
&& Pattern.matches("[1-9]*", parts[parts.length - 2])) {
String[] tagNameParts = Arrays.copyOfRange(parts, 0, parts.length - 2);
tagInfo.setTagName(String.join("-", tagNameParts));
tagInfo.setCurrentCommitDigest(parts[parts.length - 1].replace("g", ""));
tagInfo.setCurrentCommitPointedByTag(false);
} else {
String[] tagNameParts = Arrays.copyOfRange(parts, 0, parts.length - 2);
tagInfo.setTagName(String.join("-", tagNameParts));
tagInfo.setCurrentCommitPointedByTag(true);
}
}
}