Optimize GitComInf

- remove depreciated repro.peel(tag)
- reduce counting loop
pull/461/head
reger24 3 years ago
parent bdb9cdfa15
commit 47840ef866

Binary file not shown.

@ -30,13 +30,14 @@ public class GitComInf extends Properties {
public void execute() { public void execute() {
String branch = null; String branch = null;
String revision = null; String revision = null;
String lastTag = null;
String commitDate = null; String commitDate = null;
Repository repo = null; Repository repo = null;
Git git = null; Git git = null;
RevWalk walk = null; RevWalk walk = null;
Ref lastTag = null;
try { try {
final File src = new File(this.repoPath); final File src = new File(this.repoPath);
repo = new FileRepositoryBuilder().readEnvironment() repo = new FileRepositoryBuilder().readEnvironment()
@ -60,18 +61,15 @@ public class GitComInf extends Properties {
/* Peel known tags */ /* Peel known tags */
final List<Ref> peeledTags = new ArrayList<>(); final List<Ref> peeledTags = new ArrayList<>();
for (final Ref tag : tags) { for (final Ref tag : tags) {
peeledTags.add(repo.peel(tag)); lastTag = tag;
} }
/* Look for the last tag commit and calculate distance with the HEAD commit */ /* Look for the last tag commit and calculate distance with the HEAD commit */
for (final RevCommit commit : walk) { for (final RevCommit commit : walk) {
for (final Ref tag : peeledTags) { if (commit.equals(lastTag.getPeeledObjectId()) || commit.equals(lastTag.getObjectId())) {
if (commit.equals(tag.getPeeledObjectId()) || commit.equals(tag.getObjectId())) { break;
lastTag = commit.getShortMessage();
break;
}
} }
if (lastTag != null || distance++ > 90999) { if (distance++ > 90999) {
break; break;
} }
} }
@ -88,13 +86,11 @@ public class GitComInf extends Properties {
this.setProperty(this.DATEPROP_LABEL, commitDate); this.setProperty(this.DATEPROP_LABEL, commitDate);
this.setProperty("REPL_DATE", commitDate); this.setProperty("REPL_DATE", commitDate);
} catch (final IOException e) { } catch (final IOException e) {
// TODO Auto-generated catch block System.err.println(e.getMessage());
e.printStackTrace();
} catch (GitAPIException e) { } catch (GitAPIException e) {
// TODO Auto-generated catch block System.err.println(e.getMessage());
e.printStackTrace();
} catch (IllegalArgumentException e) { } catch (IllegalArgumentException e) {
System.err.println(e.getMessage());
} finally { } finally {
/* In all cases, properly release resources */ /* In all cases, properly release resources */
if (walk != null) { if (walk != null) {
@ -113,9 +109,8 @@ public class GitComInf extends Properties {
/** /**
* use: GitComInf.jar pathtoGitRepro outputfile gitbuildnumber.properties * use: GitComInf.jar pathtoGitRepro outputfile gitbuildnumber.properties
* *
* @param args = path to Git repository (default . ) * @param args = [0] path to Git repository (default . )
* @param args (optional) = filename for result (default * [1] (optional) = filename for result (default ./gitbuildnumber.properties)
* ./gitbuildnumber.properties)
*/ */
public static void main(String[] args) { public static void main(String[] args) {
GitComInf gitRevTask = new GitComInf(); GitComInf gitRevTask = new GitComInf();
@ -126,23 +121,22 @@ public class GitComInf extends Properties {
} }
gitRevTask.execute(); gitRevTask.execute();
if (gitRevTask.isEmpty()) { if (gitRevTask.isEmpty()) {
System.err.println("no Git repository found" + (args.length == 0 ? "" : " in " + args[0])); System.err.println("no Git repository found" + (args.length == 0 ? " in " + System.getProperty("user.dir") : " in " + args[0]));
} else { } else {
System.out.println("Git Commit Info: " + gitRevTask.toString()); System.out.println("Git Commit Info: " + gitRevTask.toString());
// output result to property file // output result to property file
File f; File f;
if (args.length > 1) { if (args.length > 1) {
f = new File(args[1]); f = new File(args[1]);
if (!f.exists()) {
f.getParentFile().mkdirs(); // just make sure missing dir is not a problem
}
} else { } else {
f = new File("gitbuildnumber.properties"); f = new File("gitbuildnumber.properties");
} }
try { try {
if (!f.exists()) {
f.getParentFile().mkdirs();
// Files.createDirectories(f.getParentFile().toPath()); // just make sure missing dir is not a problem
}
f.createNewFile(); f.createNewFile();
FileWriter w = new FileWriter(f); FileWriter w = new FileWriter(f);
gitRevTask.store(w, ""); gitRevTask.store(w, "");

Loading…
Cancel
Save