Skip to content

Commit 37f781c

Browse files
authored
Mercurial vs. merge commits (#4313)
honor the mergeCommitsEnabled tunable for Mercurial repositories
1 parent 5357779 commit 37f781c

File tree

3 files changed

+72
-56
lines changed

3 files changed

+72
-56
lines changed

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,12 @@ Executor getHistoryLogExecutor(File file, String sinceRevision, String tillRevis
245245
}
246246
}
247247

248+
// 'hg log' will display merge changesets by default. To honor the 'mergeCommitsEnabled' tunable,
249+
// add the option no to display merges if it is false.
250+
if (!isMergeCommitsEnabled()) {
251+
cmd.add("--no-merges");
252+
}
253+
248254
cmd.add("--template");
249255
if (revisionsOnly) {
250256
cmd.add(TEMPLATE_REVS);

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

Lines changed: 66 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,16 @@
1818
*/
1919

2020
/*
21-
* Copyright (c) 2009, 2021, Oracle and/or its affiliates. All rights reserved.
21+
* Copyright (c) 2009, 2023, Oracle and/or its affiliates. All rights reserved.
2222
* Portions Copyright (c) 2017, Chris Fraire <[email protected]>.
2323
*/
2424
package org.opengrok.indexer.history;
2525

2626
import org.junit.jupiter.api.AfterEach;
2727
import org.junit.jupiter.api.BeforeEach;
2828
import org.junit.jupiter.api.Test;
29+
import org.junit.jupiter.params.ParameterizedTest;
30+
import org.junit.jupiter.params.provider.ValueSource;
2931
import org.opengrok.indexer.condition.EnabledForRepository;
3032
import org.opengrok.indexer.configuration.RuntimeEnvironment;
3133
import org.opengrok.indexer.util.Executor;
@@ -81,13 +83,16 @@ public class MercurialRepositoryTest {
8183

8284
private TestRepository repository;
8385

86+
private File repositoryRoot;
87+
8488
/**
8589
* Set up a test repository. Should be called by the tests that need it. The
8690
* test repository will be destroyed automatically when the test finishes.
8791
*/
8892
private void setUpTestRepository() throws IOException, URISyntaxException {
8993
repository = new TestRepository();
9094
repository.create(getClass().getResource("/repositories"));
95+
repositoryRoot = new File(repository.getSourceRoot(), "mercurial");
9196
}
9297

9398
@BeforeEach
@@ -122,29 +127,25 @@ public void testGetHistory() throws Exception {
122127

123128
@Test
124129
public void testGetHistorySubdir() throws Exception {
125-
File root = new File(repository.getSourceRoot(), "mercurial");
126-
127130
// Add a subdirectory with some history.
128-
runHgCommand(root, "import",
131+
runHgCommand(repositoryRoot, "import",
129132
Paths.get(getClass().getResource("/history/hg-export-subdir.txt").toURI()).toString());
130133

131-
MercurialRepository mr = (MercurialRepository) RepositoryFactory.getRepository(root);
132-
History hist = mr.getHistory(new File(root, "subdir"));
134+
MercurialRepository mr = (MercurialRepository) RepositoryFactory.getRepository(repositoryRoot);
135+
History hist = mr.getHistory(new File(repositoryRoot, "subdir"));
133136
List<HistoryEntry> entries = hist.getHistoryEntries();
134137
assertEquals(1, entries.size());
135138
}
136139

137140
/**
138141
* Test that subset of changesets can be extracted based on penultimate
139142
* revision number. This works for directories only.
140-
* @throws Exception
141143
*/
142144
@Test
143145
public void testGetHistoryPartial() throws Exception {
144-
File root = new File(repository.getSourceRoot(), "mercurial");
145-
MercurialRepository mr = (MercurialRepository) RepositoryFactory.getRepository(root);
146+
MercurialRepository mr = (MercurialRepository) RepositoryFactory.getRepository(repositoryRoot);
146147
// Get all but the oldest revision.
147-
History hist = mr.getHistory(root, REVISIONS[REVISIONS.length - 1]);
148+
History hist = mr.getHistory(repositoryRoot, REVISIONS[REVISIONS.length - 1]);
148149
List<HistoryEntry> entries = hist.getHistoryEntries();
149150
assertEquals(REVISIONS.length - 1, entries.size());
150151
for (int i = 0; i < entries.size(); i++) {
@@ -171,7 +172,7 @@ public static void runHgCommand(File reposRoot, String... args) {
171172

172173
Executor exec = new Executor(cmdargs, reposRoot);
173174
int exitCode = exec.exec();
174-
assertEquals(0, exitCode, "hg command '" + cmdargs.toString() + "' failed."
175+
assertEquals(0, exitCode, "hg command '" + cmdargs + "' failed."
175176
+ "\nexit code: " + exitCode
176177
+ "\nstdout:\n" + exec.getOutputString()
177178
+ "\nstderr:\n" + exec.getErrorString());
@@ -180,25 +181,21 @@ public static void runHgCommand(File reposRoot, String... args) {
180181
/**
181182
* Test that history of branched repository contains changesets of the
182183
* default branch as well.
183-
* @throws Exception
184184
*/
185185
@Test
186186
public void testGetHistoryBranch() throws Exception {
187-
File root = new File(repository.getSourceRoot(), "mercurial");
188-
189187
// Branch the repo and add one changeset.
190-
runHgCommand(root, "unbundle",
188+
runHgCommand(repositoryRoot, "unbundle",
191189
Paths.get(getClass().getResource("/history/hg-branch.bundle").toURI()).toString());
192190
// Switch to the branch.
193-
runHgCommand(root, "update", "mybranch");
191+
runHgCommand(repositoryRoot, "update", "mybranch");
194192

195193
// Since the above hg commands change the active branch the repository
196194
// needs to be initialized here so that its branch matches.
197-
MercurialRepository mr
198-
= (MercurialRepository) RepositoryFactory.getRepository(root);
195+
MercurialRepository mr = (MercurialRepository) RepositoryFactory.getRepository(repositoryRoot);
199196

200197
// Get all revisions.
201-
History hist = mr.getHistory(root);
198+
History hist = mr.getHistory(repositoryRoot);
202199
List<HistoryEntry> entries = hist.getHistoryEntries();
203200
List<String> both = new ArrayList<>(REVISIONS.length
204201
+ REVISIONS_extra_branch.length);
@@ -218,7 +215,7 @@ public void testGetHistoryBranch() throws Exception {
218215
}
219216

220217
// Get revisions starting with given changeset before the repo was branched.
221-
hist = mr.getHistory(root, "8:6a8c423f5624");
218+
hist = mr.getHistory(repositoryRoot, "8:6a8c423f5624");
222219
entries = hist.getHistoryEntries();
223220
assertEquals(2, entries.size());
224221
assertEquals(REVISIONS_extra_branch[0], entries.get(0).getRevision());
@@ -227,12 +224,10 @@ public void testGetHistoryBranch() throws Exception {
227224

228225
/**
229226
* Test that contents of last revision of a text file match expected content.
230-
* @throws java.lang.Exception
231227
*/
232228
@Test
233229
public void testGetHistoryGet() throws Exception {
234-
File root = new File(repository.getSourceRoot(), "mercurial");
235-
MercurialRepository mr = (MercurialRepository) RepositoryFactory.getRepository(root);
230+
MercurialRepository mr = (MercurialRepository) RepositoryFactory.getRepository(repositoryRoot);
236231
String exp_str = "This will be a first novel of mine.\n"
237232
+ "\n"
238233
+ "Chapter 1.\n"
@@ -242,7 +237,7 @@ public void testGetHistoryGet() throws Exception {
242237
+ "...\n";
243238
byte[] buffer = new byte[1024];
244239

245-
InputStream input = mr.getHistoryGet(root.getCanonicalPath(),
240+
InputStream input = mr.getHistoryGet(repositoryRoot.getCanonicalPath(),
246241
"novel.txt", REVISIONS[0]);
247242
assertNotNull(input);
248243

@@ -257,37 +252,32 @@ public void testGetHistoryGet() throws Exception {
257252

258253
/**
259254
* Test that it is possible to get contents of multiple revisions of a file.
260-
* @throws java.lang.Exception
261255
*/
262256
@Test
263257
public void testgetHistoryGetForAll() throws Exception {
264-
File root = new File(repository.getSourceRoot(), "mercurial");
265-
MercurialRepository mr = (MercurialRepository) RepositoryFactory.getRepository(root);
258+
MercurialRepository mr = (MercurialRepository) RepositoryFactory.getRepository(repositoryRoot);
266259

267260
for (String rev : REVISIONS_novel) {
268-
InputStream input = mr.getHistoryGet(root.getCanonicalPath(),
261+
InputStream input = mr.getHistoryGet(repositoryRoot.getCanonicalPath(),
269262
"novel.txt", rev);
270263
assertNotNull(input);
271264
}
272265
}
273266

274267
/**
275-
* Test that {@code getHistoryGet()} returns historical contents of renamed
276-
* file.
277-
* @throws java.lang.Exception
268+
* Test that {@code getHistoryGet()} returns historical contents of renamed file.
278269
*/
279270
@Test
280271
public void testGetHistoryGetRenamed() throws Exception {
281-
File root = new File(repository.getSourceRoot(), "mercurial");
282-
MercurialRepository mr = (MercurialRepository) RepositoryFactory.getRepository(root);
272+
MercurialRepository mr = (MercurialRepository) RepositoryFactory.getRepository(repositoryRoot);
283273
String exp_str = "This is totally plaintext file.\n";
284274
byte[] buffer = new byte[1024];
285275

286276
/*
287277
* In our test repository the file was renamed twice since
288278
* revision 3.
289279
*/
290-
InputStream input = mr.getHistoryGet(root.getCanonicalPath(),
280+
InputStream input = mr.getHistoryGet(repositoryRoot.getCanonicalPath(),
291281
"novel.txt", "3");
292282
assert (input != null);
293283
int len = input.read(buffer);
@@ -299,12 +289,10 @@ public void testGetHistoryGetRenamed() throws Exception {
299289
/**
300290
* Test that {@code getHistory()} throws an exception if the revision
301291
* argument doesn't match any of the revisions in the history.
302-
* @throws java.lang.Exception
303292
*/
304293
@Test
305294
public void testGetHistoryWithNoSuchRevision() throws Exception {
306-
File root = new File(repository.getSourceRoot(), "mercurial");
307-
MercurialRepository mr = (MercurialRepository) RepositoryFactory.getRepository(root);
295+
MercurialRepository mr = (MercurialRepository) RepositoryFactory.getRepository(repositoryRoot);
308296

309297
// Get the sequence number and the hash from one of the revisions.
310298
String[] revisionParts = REVISIONS[1].split(":");
@@ -314,14 +302,13 @@ public void testGetHistoryWithNoSuchRevision() throws Exception {
314302

315303
// Construct a revision identifier that doesn't exist.
316304
String constructedRevision = (number + 1) + ":" + hash;
317-
assertThrows(HistoryException.class, () -> mr.getHistory(root, constructedRevision));
305+
assertThrows(HistoryException.class, () -> mr.getHistory(repositoryRoot, constructedRevision));
318306
}
319307

320308
@Test
321309
void testGetHistorySinceTillNullNull() throws Exception {
322-
File root = new File(repository.getSourceRoot(), "mercurial");
323-
MercurialRepository hgRepo = (MercurialRepository) RepositoryFactory.getRepository(root);
324-
History history = hgRepo.getHistory(root, null, null);
310+
MercurialRepository hgRepo = (MercurialRepository) RepositoryFactory.getRepository(repositoryRoot);
311+
History history = hgRepo.getHistory(repositoryRoot, null, null);
325312
assertNotNull(history);
326313
assertNotNull(history.getHistoryEntries());
327314
assertEquals(10, history.getHistoryEntries().size());
@@ -332,9 +319,8 @@ void testGetHistorySinceTillNullNull() throws Exception {
332319

333320
@Test
334321
void testGetHistorySinceTillNullRev() throws Exception {
335-
File root = new File(repository.getSourceRoot(), "mercurial");
336-
MercurialRepository hgRepo = (MercurialRepository) RepositoryFactory.getRepository(root);
337-
History history = hgRepo.getHistory(root, null, REVISIONS[4]);
322+
MercurialRepository hgRepo = (MercurialRepository) RepositoryFactory.getRepository(repositoryRoot);
323+
History history = hgRepo.getHistory(repositoryRoot, null, REVISIONS[4]);
338324
assertNotNull(history);
339325
assertNotNull(history.getHistoryEntries());
340326
assertEquals(6, history.getHistoryEntries().size());
@@ -345,9 +331,8 @@ void testGetHistorySinceTillNullRev() throws Exception {
345331

346332
@Test
347333
void testGetHistorySinceTillRevNull() throws Exception {
348-
File root = new File(repository.getSourceRoot(), "mercurial");
349-
MercurialRepository hgRepo = (MercurialRepository) RepositoryFactory.getRepository(root);
350-
History history = hgRepo.getHistory(root, REVISIONS[3], null);
334+
MercurialRepository hgRepo = (MercurialRepository) RepositoryFactory.getRepository(repositoryRoot);
335+
History history = hgRepo.getHistory(repositoryRoot, REVISIONS[3], null);
351336
assertNotNull(history);
352337
assertNotNull(history.getHistoryEntries());
353338
assertEquals(3, history.getHistoryEntries().size());
@@ -358,9 +343,8 @@ void testGetHistorySinceTillRevNull() throws Exception {
358343

359344
@Test
360345
void testGetHistorySinceTillRevRev() throws Exception {
361-
File root = new File(repository.getSourceRoot(), "mercurial");
362-
MercurialRepository hgRepo = (MercurialRepository) RepositoryFactory.getRepository(root);
363-
History history = hgRepo.getHistory(root, REVISIONS[7], REVISIONS[2]);
346+
MercurialRepository hgRepo = (MercurialRepository) RepositoryFactory.getRepository(repositoryRoot);
347+
History history = hgRepo.getHistory(repositoryRoot, REVISIONS[7], REVISIONS[2]);
364348
assertNotNull(history);
365349
assertNotNull(history.getHistoryEntries());
366350
assertEquals(5, history.getHistoryEntries().size());
@@ -372,10 +356,9 @@ void testGetHistorySinceTillRevRev() throws Exception {
372356
@Test
373357
void testGetHistoryRenamedFileTillRev() throws Exception {
374358
RuntimeEnvironment.getInstance().setHandleHistoryOfRenamedFiles(true);
375-
File root = new File(repository.getSourceRoot(), "mercurial");
376-
File file = new File(root, "novel.txt");
359+
File file = new File(repositoryRoot, "novel.txt");
377360
assertTrue(file.exists() && file.isFile());
378-
MercurialRepository hgRepo = (MercurialRepository) RepositoryFactory.getRepository(root);
361+
MercurialRepository hgRepo = (MercurialRepository) RepositoryFactory.getRepository(repositoryRoot);
379362
History history = hgRepo.getHistory(file, null, "7:db1394c05268");
380363
assertNotNull(history);
381364
assertNotNull(history.getHistoryEntries());
@@ -387,12 +370,39 @@ void testGetHistoryRenamedFileTillRev() throws Exception {
387370

388371
@Test
389372
void testGetLastHistoryEntry() throws Exception {
390-
File root = new File(repository.getSourceRoot(), "mercurial");
391-
File file = new File(root, "novel.txt");
373+
File file = new File(repositoryRoot, "novel.txt");
392374
assertTrue(file.exists() && file.isFile());
393-
MercurialRepository hgRepo = (MercurialRepository) RepositoryFactory.getRepository(root);
375+
MercurialRepository hgRepo = (MercurialRepository) RepositoryFactory.getRepository(repositoryRoot);
394376
HistoryEntry historyEntry = hgRepo.getLastHistoryEntry(file, true);
395377
assertNotNull(historyEntry);
396378
assertEquals("8:6a8c423f5624", historyEntry.getRevision());
397379
}
380+
381+
@ParameterizedTest
382+
@ValueSource(booleans = {true, false})
383+
void testMergeCommits(boolean isMergeCommitsEnabled) throws Exception {
384+
// The bundle will add a branch and merge commit in the default branch.
385+
runHgCommand(repositoryRoot, "unbundle",
386+
Paths.get(getClass().getResource("/history/hg-merge.bundle").toURI()).toString());
387+
runHgCommand(repositoryRoot, "update");
388+
389+
RuntimeEnvironment env = RuntimeEnvironment.getInstance();
390+
boolean isMergeCommitsEnabledOrig = env.isMergeCommitsEnabled();
391+
env.setMergeCommitsEnabled(isMergeCommitsEnabled);
392+
393+
MercurialRepository hgRepo = (MercurialRepository) RepositoryFactory.getRepository(repositoryRoot);
394+
History history = hgRepo.getHistory(repositoryRoot, null);
395+
assertNotNull(history);
396+
assertNotNull(history.getHistoryEntries());
397+
if (isMergeCommitsEnabled) {
398+
assertEquals(12, history.getHistoryEntries().size());
399+
assertNotNull(history.getLastHistoryEntry());
400+
assertEquals("merge", history.getLastHistoryEntry().getMessage());
401+
} else {
402+
assertEquals(11, history.getHistoryEntries().size());
403+
}
404+
405+
// Cleanup.
406+
env.setMergeCommitsEnabled(isMergeCommitsEnabledOrig);
407+
}
398408
}
Binary file not shown.

0 commit comments

Comments
 (0)