From b74d0185fb46ced1ddb2b7f8392fa7f2d142fc75 Mon Sep 17 00:00:00 2001 From: Laszlo Kishalmi Date: Thu, 25 Nov 2021 13:48:48 -0800 Subject: [PATCH] Small improvement on GitBranchHash which is able to get at least the hash of a detached HEAD --- .../org/netbeans/nbbuild/GitBranchHash.java | 33 ++++++++++++------- 1 file changed, 21 insertions(+), 12 deletions(-) diff --git a/nbbuild/antsrc/org/netbeans/nbbuild/GitBranchHash.java b/nbbuild/antsrc/org/netbeans/nbbuild/GitBranchHash.java index 2d53fe883d88..2418f4b3e865 100644 --- a/nbbuild/antsrc/org/netbeans/nbbuild/GitBranchHash.java +++ b/nbbuild/antsrc/org/netbeans/nbbuild/GitBranchHash.java @@ -79,19 +79,25 @@ public void execute() throws BuildException { if (headroot != null && Files.size(headroot) > 0l) { List lines = Files.readAllLines(headroot); String line = lines.get(0); - String revLink = line.substring(line.indexOf(':')+1).trim(); - branch = revLink.substring(revLink.lastIndexOf('/')+1).trim(); - Path revPath = headroot.getParent().resolve(revLink); - if(Files.isRegularFile(revPath) && Files.size(revPath) > 0l) { - List revlines = Files.readAllLines(revPath); - String revline = revlines.get(0); - if(revline.length()>=12){ - hash = revline.trim(); + if (!line.contains(":") && (line.length() == 40)) { + //Detached HEAD + hash = line; + log("Detached HEAD please specify '" + branchProperty + "' externally.", Project.MSG_WARN); + } else { + String revLink = line.substring(line.indexOf(':')+1).trim(); + branch = revLink.substring(revLink.lastIndexOf('/')+1).trim(); + Path revPath = headroot.getParent().resolve(revLink); + if(Files.isRegularFile(revPath) && Files.size(revPath) > 0l) { + List revlines = Files.readAllLines(revPath); + String revline = revlines.get(0); + if(revline.length()>=12){ + hash = revline.trim(); + } else { + log("no content in " + revPath, Project.MSG_WARN); + } } else { - log("no content in " + revPath, Project.MSG_WARN); + log("unable to find revision info for " + revPath, Project.MSG_WARN); } - } else { - log("unable to find revision info for " + revPath, Project.MSG_WARN); } } else { log("No HEAD found starting from " + file, Project.MSG_WARN); @@ -99,11 +105,14 @@ public void execute() throws BuildException { } catch(IOException ex) { log("Could not read " + headroot + ": " + ex, Project.MSG_WARN); } - if (!branch.isEmpty() && !hash.isEmpty()) { + if (!branch.isEmpty()) { getProject().setNewProperty(branchProperty, branch); + } + if (!hash.isEmpty()) { getProject().setNewProperty(hashProperty, hash); } } } +