Skip to content

Commit 669f22e

Browse files
committed
chore(git): WIP: Add push test #12350
1 parent 1bb1aac commit 669f22e

File tree

3 files changed

+54
-5
lines changed

3 files changed

+54
-5
lines changed

docs/code-howtos/git.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# git
22

33
## Conflict Scenarios
4+
45
- **T1.** Remote changed a field, local did not
56
→ No conflict.
67
The local version remained unchanged, so the remote change can be safely applied.
@@ -68,4 +69,3 @@
6869
- **T17.** Both added the same entry key with identical values
6970
→ No conflict.
7071
Both sides created a new entry with the same citation key and identical fields, so it can be merged safely.
71-

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

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -123,12 +123,38 @@ public MergeResult performSemanticMerge(Git git,
123123
}
124124

125125
// WIP
126-
// TODO: add test
127-
public void push(Path bibFilePath) throws GitAPIException, IOException {
128-
// 1. Auto-commit: commit if there are changes
126+
public void push(Path bibFilePath) throws GitAPIException, IOException, JabRefException{
127+
// 1. 1: Fetch remote changes
128+
gitHandler.fetchOnCurrentBranch();
129+
130+
// 2: Check if local branch is behind remote
131+
if (gitHandler.isBehindRemote()) {
132+
LOGGER.info("Remote changes detected — performing semantic merge");
133+
134+
// 2.1: Resolve commit identifiers
135+
RevCommit baseCommit = gitHandler.findMergeBaseWithRemote();
136+
RevCommit localCommit = gitHandler.resolveHead();
137+
RevCommit remoteCommit = gitHandler.resolveRemoteHead();
138+
139+
// 2.2: Perform semantic merge of the .bib file
140+
MergeResult mergeResult = performSemanticMerge(
141+
gitHandler.getGit(),
142+
baseCommit,
143+
localCommit,
144+
remoteCommit,
145+
bibFilePath
146+
);
147+
148+
if (!mergeResult.isSuccessful()) {
149+
LOGGER.warn("Semantic merge failed — aborting push");
150+
return;
151+
}
152+
}
153+
154+
// 3: Commit changes if there are any
129155
boolean committed = gitHandler.createCommitOnCurrentBranch("Changes committed by JabRef", !AMEND);
130156

131-
// 2. push to remote
157+
// 4: Push to remote only if a commit was made
132158
if (committed) {
133159
gitHandler.pushCommitsToRemoteRepository();
134160
} else {

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,29 @@ void pullTriggersSemanticMergeWhenNoConflicts() throws Exception {
149149
assertEquals(normalize(expected), normalize(merged));
150150
}
151151

152+
@Test
153+
void pushTriggersMergeAndPushWhenNoConflicts() throws Exception {
154+
GitHandler gitHandler = mock(GitHandler.class);
155+
GitSyncService syncService = new GitSyncService(importFormatPreferences, gitHandler);
156+
syncService.push(library);
157+
158+
String pushedContent = GitFileReader.readFileFromCommit(git, git.log().setMaxCount(1).call().iterator().next(), Path.of("library.bib"));
159+
String expected = """
160+
@article{a,
161+
author = {author-a},
162+
doi = {xya},
163+
}
164+
165+
@article{b,
166+
author = {author-b},
167+
doi = {xyz},
168+
}
169+
""";
170+
171+
assertEquals(normalize(expected), normalize(pushedContent));
172+
}
173+
174+
152175
@Test
153176
void readFromCommits() throws Exception {
154177
String base = GitFileReader.readFileFromCommit(git, baseCommit, Path.of("library.bib"));

0 commit comments

Comments
 (0)