-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRepositoryIdentity.java
More file actions
58 lines (55 loc) · 2.27 KB
/
Copy pathRepositoryIdentity.java
File metadata and controls
58 lines (55 loc) · 2.27 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
package io.github.randomcodespace.iq.intelligence;
import java.nio.charset.StandardCharsets;
import java.time.Instant;
/**
* Identity snapshot of a repository at analysis time.
* Populated from git metadata during the {@code index} command.
*
* @param repoUrl Remote origin URL (null for local-only repos).
* @param commitSha Full SHA-1 of HEAD (null if not a git repo).
* @param branch Current branch name (null if detached HEAD or not git).
* @param buildTimestamp When the analysis run started.
*/
public record RepositoryIdentity(
String repoUrl,
String commitSha,
String branch,
Instant buildTimestamp
) {
/**
* Resolve repository identity from a local path using git commands.
* Fields that cannot be determined are set to null gracefully.
*/
public static RepositoryIdentity resolve(java.nio.file.Path repoPath) {
String repoUrl = runGit(repoPath, "remote", "get-url", "origin");
String commitSha = runGit(repoPath, "rev-parse", "HEAD");
String branch = runGit(repoPath, "rev-parse", "--abbrev-ref", "HEAD");
// Detached HEAD produces "HEAD" rather than a branch name — normalise to null
if ("HEAD".equals(branch)) branch = null;
return new RepositoryIdentity(repoUrl, commitSha, branch, Instant.now());
}
/** Returns null on any error. */
private static String runGit(java.nio.file.Path repoPath, String... args) {
try {
var cmd = new java.util.ArrayList<String>();
cmd.add("git");
cmd.addAll(java.util.Arrays.asList(args));
var pb = new ProcessBuilder(cmd)
.directory(repoPath.toFile())
.redirectErrorStream(true);
var proc = pb.start();
try (var is = proc.getInputStream()) {
String out = new String(is.readAllBytes(), StandardCharsets.UTF_8).trim();
int exit = proc.waitFor();
return (exit == 0 && !out.isBlank()) ? out : null;
} finally {
proc.destroy();
}
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
return null;
} catch (Exception e) {
return null;
}
}
}