From 5429d1a0cff593bf28186c38fde7e41798a2c0b8 Mon Sep 17 00:00:00 2001 From: Marcel Trautwein Date: Fri, 23 Feb 2018 07:27:52 +0100 Subject: Make JGit describe behaves same as c-git for lightweight tags JGit now considers lightweight tags only if the --tags option is set i.e. `git.describe().setAllTags(true)` has to be set, else the default is now as in c git: Only annotated tags are evaluated unless you pass true equivalent to --tags (or --all) by the option setAllTags. Hint: This (still) doesn't address any difference between c-git `--all` and `!--all --tags` behavior; perhaps this might be a follow up request Bug: 423206 Change-Id: I9a3699756df0b9c6a7c74a7e8887dea0df17c8e7 Signed-off-by: Marcel Trautwein Signed-off-by: Matthias Sohn --- .../src/org/eclipse/jgit/api/DescribeCommand.java | 44 +++++++++++++++++++++- 1 file changed, 42 insertions(+), 2 deletions(-) (limited to 'org.eclipse.jgit/src/org/eclipse') diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/api/DescribeCommand.java b/org.eclipse.jgit/src/org/eclipse/jgit/api/DescribeCommand.java index dc605a91e5..4d5e499571 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/api/DescribeCommand.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/api/DescribeCommand.java @@ -103,6 +103,11 @@ public class DescribeCommand extends GitCommand { */ private List matchers = new ArrayList<>(); + /** + * Whether to use all tags (incl. lightweight) or not + */ + private boolean useTags = false; + /** * Constructor for DescribeCommand. * @@ -173,6 +178,22 @@ public class DescribeCommand extends GitCommand { return this; } + /** + * Instead of using only the annotated tags, use any tag found in refs/tags + * namespace. This option enables matching lightweight (non-annotated) tags + * or not. + * + * @param tags + * true enables matching lightweight (non-annotated) + * tags like setting option --tags in c git + * @return {@code this} + * @since 5.0 + */ + public DescribeCommand setTags(boolean tags) { + this.useTags = tags; + return this; + } + private String longDescription(Ref tag, int depth, ObjectId tip) throws IOException { return String.format( @@ -246,13 +267,14 @@ public class DescribeCommand extends GitCommand { public String call() throws GitAPIException { try { checkCallable(); - - if (target == null) + if (target == null) { setTarget(Constants.HEAD); + } Collection tagList = repo.getRefDatabase() .getRefsByPrefix(R_TAGS); Map> tags = tagList.stream() + .filter(this::filterLightweightTags) .collect(Collectors.groupingBy(this::getObjectIdFromRef)); // combined flags of all the candidate instances @@ -376,4 +398,22 @@ public class DescribeCommand extends GitCommand { w.close(); } } + + /** + * Whether we use lightweight tags or not for describe Candidates + * + * @param ref + * reference under inspection + * @return true if it should be used for describe or not regarding + * {@link org.eclipse.jgit.api.DescribeCommand#useTags} + */ + @SuppressWarnings("null") + private boolean filterLightweightTags(Ref ref) { + ObjectId id = ref.getObjectId(); + try { + return this.useTags || (id != null && (w.parseTag(id) != null)); + } catch (IOException e) { + return false; + } + } } -- cgit v1.2.3