File tree Expand file tree Collapse file tree 3 files changed +54
-5
lines changed
main/java/org/jabref/logic/git
test/java/org/jabref/logic/git Expand file tree Collapse file tree 3 files changed +54
-5
lines changed Original file line number Diff line number Diff line change 1
1
# git
2
2
3
3
## Conflict Scenarios
4
+
4
5
- ** T1.** Remote changed a field, local did not
5
6
→ No conflict.
6
7
The local version remained unchanged, so the remote change can be safely applied.
68
69
- ** T17.** Both added the same entry key with identical values
69
70
→ No conflict.
70
71
Both sides created a new entry with the same citation key and identical fields, so it can be merged safely.
71
-
Original file line number Diff line number Diff line change @@ -123,12 +123,38 @@ public MergeResult performSemanticMerge(Git git,
123
123
}
124
124
125
125
// 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
129
155
boolean committed = gitHandler .createCommitOnCurrentBranch ("Changes committed by JabRef" , !AMEND );
130
156
131
- // 2. push to remote
157
+ // 4: Push to remote only if a commit was made
132
158
if (committed ) {
133
159
gitHandler .pushCommitsToRemoteRepository ();
134
160
} else {
Original file line number Diff line number Diff line change @@ -149,6 +149,29 @@ void pullTriggersSemanticMergeWhenNoConflicts() throws Exception {
149
149
assertEquals (normalize (expected ), normalize (merged ));
150
150
}
151
151
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
+
152
175
@ Test
153
176
void readFromCommits () throws Exception {
154
177
String base = GitFileReader .readFileFromCommit (git , baseCommit , Path .of ("library.bib" ));
You can’t perform that action at this time.
0 commit comments