Skip to content

Commit f1214d9

Browse files
committed
fix: Repair failing unit tests and CI integration tests #12350
1 parent b3bc033 commit f1214d9

File tree

6 files changed

+89
-23
lines changed

6 files changed

+89
-23
lines changed

jablib/src/main/java/org/jabref/logic/git/GitSyncService.java

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -120,13 +120,18 @@ public MergeResult performSemanticMerge(Git git,
120120
// 2. Conflict detection
121121
List<ThreeWayEntryConflict> conflicts = SemanticConflictDetector.detectConflicts(base, local, remote);
122122

123-
// 3. If there are conflicts, ask strategy to resolve
124-
Optional<BibDatabaseContext> maybeRemote = gitConflictResolverStrategy.resolveConflicts(conflicts, remote);
125-
if (maybeRemote.isEmpty()) {
126-
LOGGER.warn("Merge aborted: Conflict resolution was canceled or denied.");
127-
return MergeResult.failure();
123+
BibDatabaseContext effectiveRemote;
124+
if (conflicts.isEmpty()) {
125+
effectiveRemote = remote;
126+
} else {
127+
// 3. If there are conflicts, ask strategy to resolve
128+
Optional<BibDatabaseContext> maybeRemote = gitConflictResolverStrategy.resolveConflicts(conflicts, remote);
129+
if (maybeRemote.isEmpty()) {
130+
LOGGER.warn("Merge aborted: Conflict resolution was canceled or denied.");
131+
return MergeResult.failure();
132+
}
133+
effectiveRemote = maybeRemote.get();
128134
}
129-
BibDatabaseContext effectiveRemote = maybeRemote.get();
130135

131136
// 4. Apply resolved remote (either original or conflict-resolved) to local
132137
MergePlan plan = SemanticConflictDetector.extractMergePlan(base, effectiveRemote);

jablib/src/main/java/org/jabref/logic/git/status/GitStatusChecker.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public static GitStatusSnapshot checkStatus(Path anyPathInsideRepo) {
4949
Optional.ofNullable(localHead).map(ObjectId::getName)
5050
);
5151
} catch (IOException | GitAPIException e) {
52-
LOGGER.warn("Failed to check Git status: " + e.getMessage());
52+
LOGGER.warn("Failed to check Git status: {}", e.getMessage(), e);
5353
return new GitStatusSnapshot(
5454
true,
5555
SyncStatus.UNKNOWN,

jablib/src/test/java/org/jabref/logic/git/GitHandlerTest.java

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import org.eclipse.jgit.lib.AnyObjectId;
1212
import org.eclipse.jgit.lib.Constants;
1313
import org.eclipse.jgit.revwalk.RevCommit;
14+
import org.eclipse.jgit.transport.RefSpec;
1415
import org.eclipse.jgit.transport.URIish;
1516
import org.junit.jupiter.api.BeforeEach;
1617
import org.junit.jupiter.api.Test;
@@ -22,11 +23,38 @@
2223
class GitHandlerTest {
2324
@TempDir
2425
Path repositoryPath;
26+
Path remoteRepoPath;
2527
private GitHandler gitHandler;
2628

2729
@BeforeEach
28-
void setUpGitHandler() {
30+
void setUpGitHandler() throws IOException, GitAPIException, URISyntaxException {
2931
gitHandler = new GitHandler(repositoryPath);
32+
33+
remoteRepoPath = Files.createTempDirectory("remote-repo");
34+
try (Git remoteGit = Git.init()
35+
.setBare(true)
36+
.setDirectory(remoteRepoPath.toFile())
37+
.call()) {
38+
// Remote repo initialized
39+
}
40+
Path testFile = repositoryPath.resolve("initial.txt");
41+
Files.writeString(testFile, "init");
42+
43+
gitHandler.createCommitOnCurrentBranch("Initial commit", false);
44+
45+
try (Git localGit = Git.open(repositoryPath.toFile())) {
46+
localGit.remoteAdd()
47+
.setName("origin")
48+
.setUri(new URIish(remoteRepoPath.toUri().toString()))
49+
.call();
50+
51+
localGit.push()
52+
.setRemote("origin")
53+
.setRefSpecs(new RefSpec("refs/heads/main:refs/heads/main"))
54+
.call();
55+
}
56+
57+
Files.writeString(remoteRepoPath.resolve("HEAD"), "ref: refs/heads/main");
3058
}
3159

3260
@Test

jablib/src/test/java/org/jabref/logic/git/GitSyncServiceTest.java

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -83,12 +83,11 @@ class GitSyncServiceTest {
8383
}
8484
""";
8585

86-
8786
/**
8887
* Creates a commit graph with a base commit, one modification by Alice and one modification by Bob
8988
* 1. Alice commit initial → push to remote
90-
* 2. Bob clone remote -> update `b` → push
91-
* 3. Alice update `a` → pull
89+
* 2. Bob clone remote -> update b → push
90+
* 3. Alice update a → pull
9291
*/
9392
@BeforeEach
9493
void aliceBobSimple(@TempDir Path tempDir) throws Exception {
@@ -98,7 +97,11 @@ void aliceBobSimple(@TempDir Path tempDir) throws Exception {
9897

9998
// create fake remote repo
10099
Path remoteDir = tempDir.resolve("remote.git");
101-
Git remoteGit = Git.init().setBare(true).setDirectory(remoteDir.toFile()).call();
100+
Git remoteGit = Git.init()
101+
.setBare(true)
102+
.setInitialBranch("main")
103+
.setDirectory(remoteDir.toFile())
104+
.call();
102105

103106
// Alice clone remote -> local repository
104107
Path aliceDir = tempDir.resolve("alice");
@@ -120,7 +123,7 @@ void aliceBobSimple(@TempDir Path tempDir) throws Exception {
120123
.setURI(remoteDir.toUri().toString())
121124
.setDirectory(bobDir.toFile())
122125
.setBranchesToClone(List.of("refs/heads/main"))
123-
.setBranch("refs/heads/main")
126+
.setBranch("main")
124127
.call();
125128
Path bobLibrary = bobDir.resolve("library.bib");
126129
bobCommit = writeAndCommit(bobUpdatedContent, "Exchange a with b", bob, bobLibrary, bobGit);
@@ -184,7 +187,12 @@ void pushTriggersMergeAndPushWhenNoConflicts() throws Exception {
184187
void mergeConflictOnSameFieldTriggersDialogAndUsesUserResolution(@TempDir Path tempDir) throws Exception {
185188
// Setup remote bare repo
186189
Path remoteDir = tempDir.resolve("remote.git");
187-
Git remoteGit = Git.init().setBare(true).setDirectory(remoteDir.toFile()).call();
190+
Git remoteGit = Git.init()
191+
.setBare(true)
192+
.setInitialBranch("main")
193+
.setDirectory(remoteDir.toFile())
194+
.call();
195+
Files.writeString(remoteDir.resolve("HEAD"), "ref: refs/heads/main");
188196

189197
// Clone to local working directory
190198
Path localDir = tempDir.resolve("local");

jablib/src/test/java/org/jabref/logic/git/merge/GitMergeUtilTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,6 @@ void replaceEntriesIgnoresResolvedWithoutCitationKey() {
5757
.withField(StandardField.TITLE, "New Title");
5858

5959
BibDatabaseContext result = GitMergeUtil.replaceEntries(remote, List.of(resolved));
60-
assertEquals("Original Title", result.getDatabase().getEntries().get(0).getField(StandardField.TITLE).orElse(""));
60+
assertEquals("Original Title", result.getDatabase().getEntries().getFirst().getField(StandardField.TITLE).orElse(""));
6161
}
6262
}

jablib/src/test/java/org/jabref/logic/git/status/GitStatusCheckerTest.java

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,13 @@
33
import java.nio.charset.StandardCharsets;
44
import java.nio.file.Files;
55
import java.nio.file.Path;
6+
import java.util.List;
67

78
import org.eclipse.jgit.api.Git;
89
import org.eclipse.jgit.lib.PersonIdent;
910
import org.eclipse.jgit.revwalk.RevCommit;
11+
import org.eclipse.jgit.transport.RefSpec;
12+
import org.eclipse.jgit.transport.URIish;
1013
import org.junit.jupiter.api.BeforeEach;
1114
import org.junit.jupiter.api.Test;
1215
import org.junit.jupiter.api.io.TempDir;
@@ -63,6 +66,24 @@ void setup(@TempDir Path tempDir) throws Exception {
6366
Path remoteDir = tempDir.resolve("remote.git");
6467
remoteGit = Git.init().setBare(true).setDirectory(remoteDir.toFile()).call();
6568

69+
Path seedDir = tempDir.resolve("seed");
70+
Git seedGit = Git.init().setDirectory(seedDir.toFile()).call();
71+
Path seedFile = seedDir.resolve("library.bib");
72+
Files.writeString(seedFile, baseContent, StandardCharsets.UTF_8);
73+
74+
seedGit.add().addFilepattern("library.bib").call();
75+
seedGit.commit().setAuthor(author).setMessage("Initial commit").call();
76+
seedGit.branchCreate().setName("master").call();
77+
78+
seedGit.remoteAdd()
79+
.setName("origin")
80+
.setUri(new URIish(remoteDir.toUri().toString()))
81+
.call();
82+
seedGit.push()
83+
.setRemote("origin")
84+
.setRefSpecs(new RefSpec("refs/heads/master:refs/heads/main"))
85+
.call();
86+
6687
Path localDir = tempDir.resolve("local");
6788
localGit = Git.cloneRepository()
6889
.setURI(remoteDir.toUri().toString())
@@ -71,12 +92,6 @@ void setup(@TempDir Path tempDir) throws Exception {
7192
.call();
7293

7394
this.localLibrary = localDir.resolve("library.bib");
74-
75-
// Initial commit
76-
commitFile(localGit, baseContent, "Initial commit");
77-
78-
// Push to remote
79-
localGit.push().setRemote("origin").call();
8095
}
8196

8297
@Test
@@ -100,10 +115,15 @@ void behindStatusWhenRemoteHasNewCommit(@TempDir Path tempDir) throws Exception
100115
Git remoteClone = Git.cloneRepository()
101116
.setURI(remoteGit.getRepository().getDirectory().toURI().toString())
102117
.setDirectory(remoteWork.toFile())
118+
.setBranchesToClone(List.of("refs/heads/main"))
119+
.setBranch("main")
103120
.call();
104121
Path remoteFile = remoteWork.resolve("library.bib");
105122
commitFile(remoteClone, remoteUpdatedContent, "Remote update");
106-
remoteClone.push().setRemote("origin").call();
123+
remoteClone.push()
124+
.setRemote("origin")
125+
.setRefSpecs(new RefSpec("refs/heads/main:refs/heads/main"))
126+
.call();
107127

108128
localGit.fetch().setRemote("origin").call();
109129
GitStatusSnapshot snapshot = GitStatusChecker.checkStatus(localLibrary);
@@ -125,10 +145,15 @@ void divergedStatusWhenBothSidesHaveCommits(@TempDir Path tempDir) throws Except
125145
Git remoteClone = Git.cloneRepository()
126146
.setURI(remoteGit.getRepository().getDirectory().toURI().toString())
127147
.setDirectory(remoteWork.toFile())
148+
.setBranchesToClone(List.of("refs/heads/main"))
149+
.setBranch("main")
128150
.call();
129151
Path remoteFile = remoteWork.resolve("library.bib");
130152
commitFile(remoteClone, remoteUpdatedContent, "Remote update");
131-
remoteClone.push().setRemote("origin").call();
153+
remoteClone.push()
154+
.setRemote("origin")
155+
.setRefSpecs(new RefSpec("refs/heads/main:refs/heads/main"))
156+
.call();
132157

133158
localGit.fetch().setRemote("origin").call();
134159
GitStatusSnapshot snapshot = GitStatusChecker.checkStatus(localLibrary);

0 commit comments

Comments
 (0)