Skip to content

Commit b7db946

Browse files
authored
filter out inactive branch tags in Mercurial (#4742)
fixes #4741
1 parent b7b4d9b commit b7db946

File tree

2 files changed

+49
-9
lines changed

2 files changed

+49
-9
lines changed

opengrok-indexer/src/main/java/org/opengrok/indexer/history/MercurialRepository.java

+5-5
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
*/
1919

2020
/*
21-
* Copyright (c) 2006, 2024, Oracle and/or its affiliates. All rights reserved.
21+
* Copyright (c) 2006, 2025, Oracle and/or its affiliates. All rights reserved.
2222
* Portions Copyright (c) 2017, 2019, Chris Fraire <cfraire@me.com>.
2323
*/
2424
package org.opengrok.indexer.history;
@@ -634,12 +634,12 @@ protected void buildTagList(File directory, CommandTimeoutType cmdType) {
634634
ArrayList<String> argv = new ArrayList<>();
635635
ensureCommand(CMD_PROPERTY_KEY, CMD_FALLBACK);
636636
argv.add(RepoCommand);
637-
argv.add("tags");
638-
argv.add("--template");
637+
argv.add("log");
638+
argv.add("--rev=reverse(0::branch(" + this.getBranch() + ") and tag())");
639639
// Use '|' as a revision separator rather than ':' to avoid collision with the commonly used
640-
// separator within the revision string (which is not used in the 'hg tags' output but better
640+
// separator within the revision string (which is not used in this output but better
641641
// safe than sorry).
642-
argv.add("{rev}|{tag}\\n");
642+
argv.add("--template={latesttag % '{rev}|{tag}\\n'}");
643643

644644
Executor executor = new Executor(argv, directory,
645645
RuntimeEnvironment.getInstance().getCommandTimeout(cmdType));

opengrok-indexer/src/test/java/org/opengrok/indexer/history/MercurialRepositoryTest.java

+44-4
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
*/
1919

2020
/*
21-
* Copyright (c) 2009, 2024, Oracle and/or its affiliates. All rights reserved.
21+
* Copyright (c) 2009, 2025, Oracle and/or its affiliates. All rights reserved.
2222
* Portions Copyright (c) 2017, Chris Fraire <cfraire@me.com>.
2323
*/
2424
package org.opengrok.indexer.history;
@@ -483,8 +483,14 @@ void testBuildTagListInitial() throws Exception {
483483
}
484484

485485
/**
486-
* Clone the original repository, add new tag, check that the extracted tags contain the pre-existing
487-
* and new one.
486+
* 1. Clone the original repository
487+
* 2. create branch and add tag to the branch
488+
* 3. switch back to the original branch, add new tag
489+
* 4. check that the extracted tags contain the pre-existing and new one but not the non-default branch tag.
490+
* 5. add another tag
491+
* 6. switch to the non-default branch
492+
* 7. check that the extracted tags consist of the tags added to the default branch before the branch point
493+
* and also the tags added in that branch
488494
*/
489495
@Test
490496
void testBuildTagListOneMore() throws Exception {
@@ -494,9 +500,25 @@ void testBuildTagListOneMore() throws Exception {
494500
// Clone the internal repository because it will be modified.
495501
// This avoids interference with other tests in this class.
496502
runHgCommand(this.repositoryRoot, "clone", this.repositoryRoot.toString(), repositoryRootPath.toString());
503+
504+
// Branch the repo and add one changeset.
505+
runHgCommand(repositoryRoot, "unbundle",
506+
Paths.get(getClass().getResource("/history/hg-branch.bundle").toURI()).toString());
507+
508+
// Switch to the branch and add tag.
509+
final String myBranch = "mybranch";
510+
runHgCommand(repositoryRoot, "update", myBranch);
511+
final String branchTagName = "branch_tag";
512+
runHgCommand(repositoryRoot, "tag", branchTagName);
513+
514+
// Switch back to the default branch.
515+
runHgCommand(repositoryRoot, "update", "default");
516+
497517
MercurialRepository hgRepo = (MercurialRepository) RepositoryFactory.getRepository(repositoryRoot);
498518
assertNotNull(hgRepo);
499-
// Using double space on purpose to test the parsing of tags.
519+
assertEquals("default", hgRepo.getBranch());
520+
521+
// Add tag. Using double space on purpose to test the parsing of tags.
500522
final String newTagName = "foo bar";
501523
runHgCommand(repositoryRoot, "tag", newTagName);
502524
hgRepo.buildTagList(new File(hgRepo.getDirectoryName()), CommandTimeoutType.INDEXER);
@@ -509,6 +531,24 @@ void testBuildTagListOneMore() throws Exception {
509531
assertEquals(List.of(7, 9), tags.stream().map(TagEntry::getRevision).collect(Collectors.toList()));
510532
List<String> expectedTags = List.of("start_of_novel", newTagName);
511533
assertEquals(expectedTags, tags.stream().map(TagEntry::getTags).collect(Collectors.toList()));
534+
535+
// Add another tag to the default branch.
536+
runHgCommand(repositoryRoot, "tag", "another_tag");
537+
538+
// Switch back to the non-default branch, check tags.
539+
runHgCommand(repositoryRoot, "update", myBranch);
540+
// The repository object has to be recreated to reflect the branch switch.
541+
hgRepo = (MercurialRepository) RepositoryFactory.getRepository(repositoryRoot);
542+
assertNotNull(hgRepo);
543+
assertEquals(myBranch, hgRepo.getBranch());
544+
hgRepo.buildTagList(new File(hgRepo.getDirectoryName()), CommandTimeoutType.INDEXER);
545+
tags = hgRepo.getTagList();
546+
assertNotNull(tags);
547+
assertEquals(3, tags.size());
548+
expectedTags = List.of("start_of_novel", newTagName, branchTagName);
549+
assertEquals(expectedTags, tags.stream().map(TagEntry::getTags).collect(Collectors.toList()));
550+
551+
// cleanup
512552
IOUtils.removeRecursive(repositoryRootPath);
513553
}
514554
}

0 commit comments

Comments
 (0)