Skip to content

[WIP] Happy days git integration wp3 #12773

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 97 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
97 commits
Select commit Hold shift + click to select a range
01dec74
added methods to and boolean to see if file is under version control …
Feb 26, 2025
eb1b9a8
Add CheckForVersionControlAction
11raphael Feb 26, 2025
c761db5
Added unit tests for - CheckForVersionControlAction
Ary006 Feb 26, 2025
2c9dad4
Added unit tests for - CheckForVersionControlAction
Ary006 Feb 27, 2025
ed6eb4a
added functionality for tagging as versioned and pulling only if pref…
Feb 27, 2025
9cbdd76
moved test file and amended shouldAutoPull method to test.org.jabref.…
Feb 27, 2025
62defc4
Remove RuntimeException from CheckForVersionControlAction
11raphael Mar 1, 2025
bbc6b38
Remove redundant methods from GitHandler
11raphael Mar 1, 2025
69e0f9a
modified the test cases for CheckForVersionControlAction
Ary006 Mar 1, 2025
d578bd5
Merge remote-tracking branch 'origin/library-under-version-control-an…
Ary006 Mar 1, 2025
cf32088
modified the test cases for CheckForVersionControlAction
Ary006 Mar 1, 2025
c670625
modified the test cases for CheckForVersionControlAction
Ary006 Mar 1, 2025
9026eb9
modified the test cases for CheckForVersionControlAction
Ary006 Mar 1, 2025
40cdd64
modified the test cases for CheckForVersionControlAction
Ary006 Mar 1, 2025
8db799c
Remove preference in CheckForVersionControlAction
11raphael Mar 3, 2025
af0c3c8
Add git preferences
11raphael Mar 3, 2025
1f2de0a
Add GitPreferences to CliPreferences
11raphael Mar 3, 2025
cfd46db
added autoPushEnabled attributes and set methods
Mar 3, 2025
6ecf1a8
modified tab view model to handle auto push
Mar 3, 2025
a0e2dc7
Add getGitPreferences to JabRefCliPreferences
11raphael Mar 3, 2025
c8be361
Merge remote-tracking branch 'origin/Happy-Days-Git-Integration-WP3' …
11raphael Mar 3, 2025
5127bca
Merge branch 'Happy-Days-Git-Integration-WP3' into library-under-vers…
11raphael Mar 3, 2025
2bf349d
Remove redundant shouldAutoPull from CliPreferences
11raphael Mar 3, 2025
bff9857
Added a rudimentary first draft preference selector under Saving in g…
SihasA Mar 3, 2025
c197264
Merge remote-tracking branch 'origin/library-under-version-control-an…
11raphael Mar 3, 2025
f7c21c4
removed duplicate boolean
Mar 4, 2025
980c748
Cleaned up UI - "Auto push" Combo Box no longer is in all Caps
SihasA Mar 5, 2025
caf405b
Added "Git" submenu with "Git Pull" and "Git Push" options
Ary006 Mar 6, 2025
4ed64cd
constructed get git preferences
Mar 9, 2025
45c453e
Merge branch 'library-under-version-control-and-pull' into Happy-Days…
11raphael Mar 9, 2025
3ff8bc3
Add autoPushMode to GitPreferences
11raphael Mar 10, 2025
63d86f1
Add git functionality on database save to GitHandler and SaveDatabase…
11raphael Mar 10, 2025
837311f
Create GitPullAction for git pull menu item
11raphael Mar 10, 2025
53286d4
Fix: Rename GitPullAction and correct GitHandler repository path in c…
11raphael Mar 12, 2025
cd6ff23
modified getGitPreferences to account for getAutoPushModeProperty() m…
Mar 12, 2025
f2fae4b
Remove redundant parameter from GeneralTabViewModel
11raphael Mar 12, 2025
7b0e930
Clean 'Failed to push' logger message
11raphael Mar 12, 2025
c28b84f
removed comments
Mar 12, 2025
0e0ddba
Fixed error regarding preferences not saving correctly
SihasA Mar 13, 2025
b9ca978
Merge remote-tracking branch 'origin/Happy-Days-Git-Integration-WP3' …
SihasA Mar 13, 2025
e71d3ad
Fixed error regarding preferences not saving correctly
SihasA Mar 13, 2025
27b0355
Merge remote-tracking branch 'origin/Happy-Days-Git-Integration-WP3' …
11raphael Mar 13, 2025
6a7e013
Update GitHandler with staging methods and add GitPullAction
11raphael Mar 13, 2025
e3885b7
Cleaned up UI Preferences
SihasA Mar 13, 2025
0fd12cc
Merge remote-tracking branch 'origin/Happy-Days-Git-Integration-WP3' …
11raphael Mar 13, 2025
b1458b2
Capitalization
11raphael Mar 13, 2025
98ec8a8
Remove redundant methods from GitHandler
11raphael Mar 13, 2025
d9162bc
added tests for git preferences to ensure that auto push functionalit…
Mar 13, 2025
e800a28
moved location of gitpreferences test
Mar 13, 2025
0c51c0e
Add unit tests for AutoPushMode - covering getDisplayName(), toString…
Ary006 Mar 17, 2025
5e0015c
Added Git Username and Passkey fields to preferences
SihasA Mar 17, 2025
d5b72bb
Add git credentials logic
11raphael Mar 17, 2025
2586df7
Merge remote-tracking branch 'origin/Happy-Days-Git-Integration-WP3' …
11raphael Mar 17, 2025
bed8cc7
Fix preferences in GitHandler
11raphael Mar 17, 2025
4b71771
demo
11raphael Mar 17, 2025
d52d62a
Merge remote-tracking branch 'origin/Happy-Days-Git-Integration-WP3' …
SihasA Mar 17, 2025
f232305
Fixed merge conflicts and added github passkey and username to prefer…
SihasA Mar 17, 2025
4d91985
Revert "Added Git Username and Passkey fields to preferences"
SihasA Mar 17, 2025
2ea0d72
Fixed Preferences and finished GitFunctionality for basic pull and push
SihasA Mar 17, 2025
2fe1066
Implement auto push on database save
11raphael Mar 17, 2025
8f683f0
Removed Manually option from combobox, will remove combobox in future.
SihasA Mar 17, 2025
cdb4c76
Merge remote-tracking branch 'origin/Happy-Days-Git-Integration-WP3' …
SihasA Mar 17, 2025
1884dd7
Revert "Implement auto push on database save"
SihasA Mar 17, 2025
bde293a
Revert "Revert "Implement auto push on database save""
SihasA Mar 17, 2025
37e0b92
Fix various issues
11raphael Mar 18, 2025
f87ab7a
Fixed/ Looked into Bot Comments
SihasA Mar 18, 2025
dcc5a06
Merge remote-tracking branch 'origin/Happy-Days-Git-Integration-WP3' …
11raphael Mar 18, 2025
b11f155
Fixed CheckStyleIssues and Bot Issue
SihasA Mar 18, 2025
854af5c
Fixing Bot Comments
SihasA Mar 18, 2025
95197c2
Reverted changes to module-info.java and build.gradle.
SihasA Mar 19, 2025
51e6fd6
Refactor git client functionality into GitClientHandler
11raphael Mar 19, 2025
f1fe7f5
Merge remote-tracking branch 'origin/Happy-Days-Git-Integration-WP3' …
11raphael Mar 19, 2025
e3fc84e
Corrected issue - "Method violates fail-fast principle by nesting all…
Ary006 Mar 19, 2025
3070087
Corrected issue - "Method violates fail-fast principle by nesting all…
Ary006 Mar 19, 2025
4e1c577
Corrected issue - "Method violates fail-fast principle by nesting all…
Ary006 Mar 19, 2025
5b278aa
Ran rewriteRun and fixed bot comments
SihasA Mar 19, 2025
c305e53
Added localisation and fixed logger issues.
SihasA Mar 19, 2025
9b0bf8f
Fixed one failing test case and removed problematic test + Bot commen…
SihasA Mar 19, 2025
493775b
Add test cases for Git repository detection
arp-23 Mar 19, 2025
92df862
Merge remote-tracking branch 'origin/Happy-Days-Git-Integration-WP3' …
arp-23 Mar 19, 2025
0c0a094
Refactor postSaveDatabaseAction and GitClientHandler
11raphael Mar 19, 2025
6e731cf
Merge remote-tracking branch 'origin/Happy-Days-Git-Integration-WP3' …
11raphael Mar 19, 2025
355ff89
Update postSaveDatabaseAction JavaDoc
11raphael Mar 19, 2025
86edd20
Removed line in build.gradle
SihasA Mar 20, 2025
14e6d52
Added a new heading under saving for git related preferences in gener…
SihasA Mar 20, 2025
0b7db48
Fix bot comments
arp-23 Mar 20, 2025
6ad7f25
Merge remote-tracking branch 'origin/Happy-Days-Git-Integration-WP3' …
arp-23 Mar 20, 2025
74398a8
Merge branch 'JabRef:main' into Happy-Days-Git-Integration-WP3
SihasA Mar 21, 2025
ee71915
Update GitClientHandler
11raphael Mar 21, 2025
9bf27ad
Add basic GitClientHandlerTest and attempt to fix EndToEndTest
arp-23 Mar 24, 2025
7cfb3e4
Merge branch 'main' into Happy-Days-Git-Integration-WP3
koppor Mar 24, 2025
6775c8b
Merge branch 'main' into Happy-Days-Git-Integration-WP3
koppor Mar 24, 2025
a696800
Merge branch 'main' into Happy-Days-Git-Integration-WP3
koppor Mar 24, 2025
f166255
Merge branch 'main' into Happy-Days-Git-Integration-WP3
SihasA Mar 24, 2025
1b46b89
removed combo box from gitpreferences
SihasA Mar 24, 2025
1c47c25
Merge remote-tracking branch 'origin/Happy-Days-Git-Integration-WP3' …
SihasA Mar 24, 2025
8d8273e
Merge branch 'main' into Happy-Days-Git-Integration-WP3
koppor Mar 25, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/main/java/org/jabref/gui/actions/StandardActions.java
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,10 @@ public enum StandardActions implements Action {
GROUP_SUBGROUP_RENAME(Localization.lang("Rename subgroup"), KeyBinding.GROUP_SUBGROUP_RENAME),
GROUP_ENTRIES_REMOVE(Localization.lang("Remove selected entries from this group")),

GIT(Localization.lang("Git")),
GIT_PULL(Localization.lang("Git Pull")),
GIT_PUSH(Localization.lang("Git Push")),

CLEAR_EMBEDDINGS_CACHE(Localization.lang("Clear embeddings cache"));

private String text;
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/org/jabref/gui/exporter/SaveDatabaseAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import org.jabref.logic.exporter.BibtexDatabaseWriter;
import org.jabref.logic.exporter.SaveException;
import org.jabref.logic.exporter.SelfContainedSaveConfiguration;
import org.jabref.logic.git.GitClientHandler;
import org.jabref.logic.l10n.Encodings;
import org.jabref.logic.l10n.Localization;
import org.jabref.logic.shared.DatabaseLocation;
Expand Down Expand Up @@ -237,6 +238,10 @@ private boolean save(Path targetPath, SaveDatabaseMode mode) {
if (success) {
libraryTab.getUndoManager().markUnchanged();
libraryTab.resetChangedProperties();
new GitClientHandler(targetPath.getParent(),
dialogService,
preferences.getGitPreferences())
.postSaveDatabaseAction();
Comment on lines +241 to +244
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lifecycle is strange - the GitClientHandler should be bound to the BibdatabaseContext (or libraryTab), shouldn't it?

There is also an instance in src/main/java/org/jabref/gui/importer/actions/CheckForVersionControlAction.java. I think, the instance should be shared?

}
dialogService.notify(Localization.lang("Library saved"));
return success;
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/org/jabref/gui/frame/MainMenu.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@
import org.jabref.gui.push.PushToApplicationCommand;
import org.jabref.gui.search.RebuildFulltextSearchIndexAction;
import org.jabref.gui.shared.ConnectToSharedDatabaseCommand;
import org.jabref.gui.shared.GitPullAction;
import org.jabref.gui.shared.GitPushAction;
import org.jabref.gui.shared.PullChangesFromSharedAction;
import org.jabref.gui.sidepane.SidePane;
import org.jabref.gui.sidepane.SidePaneType;
Expand Down Expand Up @@ -160,6 +162,12 @@ private void createMenu() {

new SeparatorMenuItem(),

factory.createSubMenu(StandardActions.GIT,
factory.createMenuItem(StandardActions.GIT_PUSH, new GitPushAction(preferences, dialogService, stateManager)),
factory.createMenuItem(StandardActions.GIT_PULL, new GitPullAction(preferences, dialogService, stateManager))),

new SeparatorMenuItem(),

factory.createSubMenu(StandardActions.IMPORT,
factory.createMenuItem(StandardActions.IMPORT_INTO_CURRENT_LIBRARY, new ImportCommand(frame, ImportCommand.ImportMethod.TO_EXISTING, preferences, stateManager, fileUpdateMonitor, taskExecutor, dialogService)),
factory.createMenuItem(StandardActions.IMPORT_INTO_NEW_LIBRARY, new ImportCommand(frame, ImportCommand.ImportMethod.AS_NEW, preferences, stateManager, fileUpdateMonitor, taskExecutor, dialogService))),
Expand Down Expand Up @@ -407,3 +415,5 @@ private Menu createSendSubMenu(ActionFactory factory,
return sendMenu;
}
}


Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package org.jabref.gui.importer.actions;

import java.io.IOException;
import java.nio.file.Path;
import java.util.Optional;

import org.jabref.gui.DialogService;
import org.jabref.logic.git.GitClientHandler;
import org.jabref.logic.importer.ParserResult;
import org.jabref.logic.preferences.CliPreferences;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* This action checks whether this BIB file is contained in a Git repository. If so,
* then the file is tagged as "versioned" in BibDatabaseContext and a git pull is
* attempted.
*/
public class CheckForVersionControlAction implements GUIPostOpenAction {
private static final Logger LOGGER = LoggerFactory.getLogger(CheckForVersionControlAction.class);
private GitClientHandler gitClientHandler;

@Override
public boolean isActionNecessary(ParserResult parserResult, DialogService dialogService, CliPreferences preferences) {
Optional<Path> path = parserResult.getDatabaseContext().getDatabasePath();
if (path.isEmpty()) {
return false;
}
this.gitClientHandler = new GitClientHandler(path.get(),
dialogService,
preferences.getGitPreferences());
Comment on lines +30 to +32
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a side effect of a simple boolean check. Can't it be done differently?

Maybe bind the GitClienthandler to the BibDatbaseContext?

return gitClientHandler.isGitRepository();
}

@Override
public void performAction(ParserResult parserResult, DialogService dialogService, CliPreferences preferencesService) {
parserResult.getDatabaseContext().setVersioned(true);

try {
this.gitClientHandler.pullOnCurrentBranch();
} catch (IOException e) {
LOGGER.error("Failed to pull.", e);
}
}
}
14 changes: 14 additions & 0 deletions src/main/java/org/jabref/gui/preferences/general/GeneralTab.fxml
Original file line number Diff line number Diff line change
Expand Up @@ -120,4 +120,18 @@
<Insets top="-4.0"/>
</VBox.margin>
</HBox>

<Label styleClass="sectionHeader" text="%Git"/>
<HBox spacing="10">
<Label text="Git Username:"/>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please see two lines above how localiaztion works.

Moreover, no : at the end

<TextField fx:id="gitHubUsernameField" promptText="Enter your username here"/>
</HBox>

<HBox spacing="10">
<Label text="Git Personal Access Token:"/>
<TextField fx:id="gitHubPasskeyField" promptText="Enter your PAT here"/>
</HBox>
<HBox alignment="CENTER_LEFT" spacing="4.0">
<CheckBox fx:id="autoPushCheckbox" text="Auto push on save"/>
</HBox>
</fx:root>
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ public class GeneralTab extends AbstractPreferenceTabView<GeneralTabViewModel> i
@FXML private CheckBox alwaysReformatBib;
@FXML private CheckBox autosaveLocalLibraries;
@FXML private Button autosaveLocalLibrariesHelp;
@FXML private TextField gitHubUsernameField;
@FXML private TextField gitHubPasskeyField;
@FXML private CheckBox autoPushCheckbox;
@FXML private CheckBox createBackup;
@FXML private TextField backupDirectory;
@FXML private CheckBox remoteServer;
Expand Down Expand Up @@ -132,6 +135,12 @@ public void initialize() {

alwaysReformatBib.selectedProperty().bindBidirectional(viewModel.alwaysReformatBibProperty());
autosaveLocalLibraries.selectedProperty().bindBidirectional(viewModel.autosaveLocalLibrariesProperty());

gitHubUsernameField.textProperty().bindBidirectional(viewModel.gitHubUsernameProperty());
gitHubPasskeyField.textProperty().bindBidirectional(viewModel.gitHubPasskeyProperty());

autoPushCheckbox.selectedProperty().bindBidirectional(viewModel.autoPushEnabledProperty());

ActionFactory actionFactory = new ActionFactory();
actionFactory.configureIconButton(StandardActions.HELP, new HelpAction(HelpFile.AUTOSAVE, dialogService, preferences.getExternalApplicationsPreferences()), autosaveLocalLibrariesHelp);
actionFactory.configureIconButton(StandardActions.HELP, new HelpAction(HelpFile.REMOTE, dialogService, preferences.getExternalApplicationsPreferences()), remoteHelp);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import org.jabref.gui.util.FileDialogConfiguration;
import org.jabref.logic.FilePreferences;
import org.jabref.logic.LibraryPreferences;
import org.jabref.logic.git.GitPreferences;
import org.jabref.logic.l10n.Language;
import org.jabref.logic.l10n.Localization;
import org.jabref.logic.net.ssl.TrustStoreManager;
Expand Down Expand Up @@ -94,6 +95,7 @@ public class GeneralTabViewModel implements PreferenceTabViewModel {
private final LibraryPreferences libraryPreferences;
private final FilePreferences filePreferences;
private final RemotePreferences remotePreferences;
private final GitPreferences gitPreferences;

private final Validator fontSizeValidator;
private final Validator customPathToThemeValidator;
Expand All @@ -112,6 +114,7 @@ public GeneralTabViewModel(DialogService dialogService, GuiPreferences preferenc
this.workspacePreferences = preferences.getWorkspacePreferences();
this.libraryPreferences = preferences.getLibraryPreferences();
this.filePreferences = preferences.getFilePreferences();
this.gitPreferences = preferences.getGitPreferences();
this.remotePreferences = preferences.getRemotePreferences();
this.fileUpdateMonitor = fileUpdateMonitor;
this.entryTypesManager = entryTypesManager;
Expand Down Expand Up @@ -195,6 +198,10 @@ public void setValues() {
alwaysReformatBibProperty.setValue(libraryPreferences.shouldAlwaysReformatOnSave());
autosaveLocalLibraries.setValue(libraryPreferences.shouldAutoSave());

autoPushEnabledProperty().setValue(gitPreferences.getAutoPushEnabled());
gitHubUsernameProperty().setValue(gitPreferences.getGitHubUsername());
gitHubPasskeyProperty().setValue(gitPreferences.getGitHubPasskey());

createBackupProperty.setValue(filePreferences.shouldCreateBackup());
backupDirectoryProperty.setValue(filePreferences.getBackupDirectory().toString());

Expand Down Expand Up @@ -237,6 +244,10 @@ public void storeSettings() {
libraryPreferences.setAlwaysReformatOnSave(alwaysReformatBibProperty.getValue());
libraryPreferences.setAutoSave(autosaveLocalLibraries.getValue());

gitPreferences.setAutoPushEnabled(autoPushEnabledProperty().get());
gitPreferences.setGitHubUsername(gitHubUsernameProperty().get());
gitPreferences.setGitHubPasskey(gitHubPasskeyProperty().get());

filePreferences.createBackupProperty().setValue(createBackupProperty.getValue());
filePreferences.backupDirectoryProperty().setValue(Path.of(backupDirectoryProperty.getValue()));

Expand Down Expand Up @@ -440,4 +451,16 @@ private Optional<Integer> getPortAsInt(String value) {
return Optional.empty();
}
}

public BooleanProperty autoPushEnabledProperty() {
return gitPreferences.getAutoPushEnabledProperty();
}

public StringProperty gitHubUsernameProperty() {
return gitPreferences.gitHubUsernameProperty();
}

public StringProperty gitHubPasskeyProperty() {
return gitPreferences.gitHubPasskeyProperty();
}
}
65 changes: 65 additions & 0 deletions src/main/java/org/jabref/gui/shared/GitPullAction.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package org.jabref.gui.shared;

import java.io.IOException;
import java.nio.file.Path;
import java.util.Optional;

import org.jabref.gui.DialogService;
import org.jabref.gui.StateManager;
import org.jabref.gui.actions.SimpleCommand;
import org.jabref.gui.preferences.GuiPreferences;
import org.jabref.logic.git.GitClientHandler;
import org.jabref.model.database.BibDatabaseContext;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class GitPullAction extends SimpleCommand {
private static final Logger LOGGER = LoggerFactory.getLogger(GitPullAction.class);

private final GuiPreferences preferences;
private final DialogService dialogService;
private final StateManager stateManager;

public GitPullAction(
GuiPreferences preferences,
DialogService dialogService,
StateManager stateManager) {
this.preferences = preferences;
this.dialogService = dialogService;
this.stateManager = stateManager;
}
/**
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a space above

* Contains logic for performing Git operations on the active database repository.
* The method verifies that an active database exists and is not empty
* then creates a GitClientHandler with the parent directory of the database path.
* If the directory is a Git repository, performs a pull operation on the current branch.
* Will log any errors that occur during the pull operation.
*/

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove empty line - JavaDoc is connected to the method directly.

@Override
public void execute() {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The method lacks JavaDoc despite being a public method that performs complex Git operations. Complex public methods should have comprehensive documentation.

if (stateManager.getActiveDatabase().isEmpty()) {
return;
}
BibDatabaseContext databaseContext = stateManager.getActiveDatabase().get();

Optional<Path> path = databaseContext.getDatabasePath();
if (path.isEmpty()) {
return;
}

GitClientHandler gitClientHandler = new GitClientHandler(path.get().getParent(),
dialogService,
preferences.getGitPreferences());
if (gitClientHandler.isGitRepository()) {
try {
gitClientHandler.pullOnCurrentBranch();
} catch (IOException e) {
LOGGER.error("Failed to Pull", e);
}
} else {
LOGGER.info("Not a git repository at path: {}", path.get());
}
}
}
60 changes: 60 additions & 0 deletions src/main/java/org/jabref/gui/shared/GitPushAction.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package org.jabref.gui.shared;

import java.io.IOException;
import java.nio.file.Path;
import java.util.Optional;

import org.jabref.gui.DialogService;
import org.jabref.gui.StateManager;
import org.jabref.gui.actions.SimpleCommand;
import org.jabref.gui.preferences.GuiPreferences;
import org.jabref.logic.git.GitClientHandler;
import org.jabref.model.database.BibDatabaseContext;

import org.eclipse.jgit.api.errors.GitAPIException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class GitPushAction extends SimpleCommand {
private static final Logger LOGGER = LoggerFactory.getLogger(GitPushAction.class);

private final GuiPreferences preferences;
private final DialogService dialogService;
private final StateManager stateManager;

public GitPushAction(
GuiPreferences preferences,
DialogService dialogService,
StateManager stateManager) {
this.preferences = preferences;
this.dialogService = dialogService;
this.stateManager = stateManager;
}

@Override
public void execute() {
if (stateManager.getActiveDatabase().isEmpty()) {
return;
}
BibDatabaseContext databaseContext = stateManager.getActiveDatabase().get();

Optional<Path> path = databaseContext.getDatabasePath();
if (path.isEmpty()) {
return;
}

GitClientHandler gitClientHandler = new GitClientHandler(path.get().getParent(),
dialogService,
preferences.getGitPreferences());
if (gitClientHandler.isGitRepository()) {
try {
gitClientHandler.createCommitOnCurrentBranch("Automatic update via JabRef", false);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This string appears twice in your code - please introduce a constant.

gitClientHandler.pushCommitsToRemoteRepository();
} catch (IOException | GitAPIException e) {
dialogService.showErrorDialogAndWait(e);
}
} else {
LOGGER.info("Not a git repository at path: {}", path);
}
}
}
Loading