Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

vcs: handle lightweight tags in Git #670

wants to merge 1 commit into from
Show file tree
Hide file tree
Changes from all commits
File filter

Filter by extension

Filter by extension

Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -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());

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!"));

var head = repo.commit("Add README", "author", "");

// 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());;
assertEquals(0, pb.start().waitFor());

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

var tag = tags.get(0);

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