Skip to content
Permalink
Browse files
vcs: handle lightweight tags in Git
Reviewed-by: rwestberg
  • Loading branch information
edvbld committed Jun 17, 2020
1 parent ca39c57 commit 28456f5de1032844020d8c408aba3d6de0b7321a
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 3 deletions.
@@ -1399,11 +1399,19 @@ public Optional<Tag.Annotated> annotate(Tag tag) throws IOException {
var lines = await(p).stdout();
if (lines.size() >= 4) {
var name = lines.get(0);
var target = new Hash(lines.get(1));
var author = Author.fromString(lines.get(2));
var targetLine = lines.get(1);
var authorLine = lines.get(2);
var dateLine = lines.get(3);

if (targetLine.isEmpty() && authorLine.equals(" ") && dateLine.isEmpty()) {
// Must be a lightweight tag, no metadata present
return Optional.empty();
}

var target = new Hash(targetLine);
var author = Author.fromString(authorLine);
var formatter = DateTimeFormatter.ISO_OFFSET_DATE_TIME;
var date = ZonedDateTime.parse(lines.get(3), formatter);
var date = ZonedDateTime.parse(dateLine, formatter);
var message = String.join("\n", lines.subList(4, lines.size()));

return Optional.of(new Tag.Annotated(name, target, author, date, message));
@@ -2323,4 +2323,31 @@ void testCommitterDate() throws IOException {
assertEquals(committed, commit.committed());
}
}

@Test
void testLightweightTags() throws IOException, InterruptedException {
try (var dir = new TemporaryDirectory()) {
var repo = Repository.init(dir.path(), VCS.GIT);
var readme = dir.path().resolve("README");
Files.write(readme, List.of("Hello, readme!"));

repo.add(readme);
var head = repo.commit("Add README", "author", "author@openjdk.java.net");

// We don't want to expose making lightweight tags via the Repository class,
// so use a ProcessBuilder and invoke git directly here
var pb = new ProcessBuilder("git", "tag", "test-tag", head.hex());
pb.directory(repo.root().toFile());
assertEquals(0, pb.start().waitFor());

var tags = repo.tags();
assertEquals(1, tags.size());

var tag = tags.get(0);
assertEquals("test-tag", tag.name());

// Lightweight tags can't be annotated
assertEquals(Optional.empty(), repo.annotate(tag));
}
}
}

1 comment on commit 28456f5

@bridgekeeper
Copy link

@bridgekeeper bridgekeeper bot commented on 28456f5 Jun 17, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.