-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
feat: support committing local changes #13743
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
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
8a39c28
feat: support committing local changes
wanling0000 fa96bf7
add GitCommitAction to MainMenu
wanling0000 15a69f5
fix broken jablib tests
wanling0000 664fbe9
fix: add missing localization keys
wanling0000 0d9d3eb
refactor: remove unused field and avoid null parameter in Git commit …
wanling0000 b3d891a
fix: localize error message in GitCommitDialogViewModel
wanling0000 b1ad3fa
chore: bind commit message validator
wanling0000 e0232d4
refactor: extract commit logic in GitCommitDialogViewModel
wanling0000 3162c1a
fix: move Git submenu before remote DB
wanling0000 1c8f4fd
chore: reorder Git menu and apply minor cleanup adjustments
wanling0000 f937b5a
Add early Git status check in GitCommitAction
wanling0000 17b42dc
Merge branch 'main' into feat/gsoc-git-commit
wanling0000 6db4b57
trigger CI
wanling0000 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
jabgui/src/main/java/org/jabref/gui/git/GitCommitAction.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| package org.jabref.gui.git; | ||
|
|
||
| import org.jabref.gui.DialogService; | ||
| import org.jabref.gui.StateManager; | ||
| import org.jabref.gui.actions.ActionHelper; | ||
| import org.jabref.gui.actions.SimpleCommand; | ||
| import org.jabref.logic.git.GitHandler; | ||
| import org.jabref.logic.git.status.GitStatusChecker; | ||
| import org.jabref.logic.l10n.Localization; | ||
|
|
||
| public class GitCommitAction extends SimpleCommand { | ||
|
|
||
| private final DialogService dialogService; | ||
| private final StateManager stateManager; | ||
|
|
||
| public GitCommitAction(DialogService dialogService, StateManager stateManager) { | ||
| this.dialogService = dialogService; | ||
| this.stateManager = stateManager; | ||
|
|
||
| this.executable.bind(ActionHelper.needsDatabase(stateManager)); | ||
| } | ||
|
|
||
| @Override | ||
| public void execute() { | ||
| if (hasNothingToCommit()) { | ||
| dialogService.notify(Localization.lang("Nothing to commit.")); | ||
| return; | ||
| } | ||
|
|
||
| dialogService.showCustomDialogAndWait( | ||
| new GitCommitDialogView() | ||
| ); | ||
| } | ||
|
|
||
| private boolean hasNothingToCommit() { | ||
| return stateManager.getActiveDatabase() | ||
| .flatMap(context -> context.getDatabasePath()) | ||
| .flatMap(GitHandler::fromAnyPath) | ||
| .map(GitStatusChecker::checkStatus) | ||
| .map(status -> !status.uncommittedChanges()) | ||
| .orElse(true); | ||
| } | ||
| } |
66 changes: 66 additions & 0 deletions
66
jabgui/src/main/java/org/jabref/gui/git/GitCommitDialogView.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| package org.jabref.gui.git; | ||
|
|
||
| import javafx.application.Platform; | ||
| import javafx.fxml.FXML; | ||
| import javafx.scene.control.ButtonType; | ||
| import javafx.scene.control.TextArea; | ||
|
|
||
| import org.jabref.gui.DialogService; | ||
| import org.jabref.gui.StateManager; | ||
| import org.jabref.gui.preferences.GuiPreferences; | ||
| import org.jabref.gui.util.BaseDialog; | ||
| import org.jabref.gui.util.IconValidationDecorator; | ||
| import org.jabref.logic.l10n.Localization; | ||
| import org.jabref.logic.util.TaskExecutor; | ||
|
|
||
| import com.airhacks.afterburner.views.ViewLoader; | ||
| import de.saxsys.mvvmfx.utils.validation.visualization.ControlsFxVisualizer; | ||
| import jakarta.inject.Inject; | ||
|
|
||
| public class GitCommitDialogView extends BaseDialog<Void> { | ||
|
|
||
| @FXML private TextArea commitMessage; | ||
| @FXML private ButtonType commitButton; | ||
|
|
||
| private GitCommitDialogViewModel viewModel; | ||
|
|
||
| @Inject | ||
| private StateManager stateManager; | ||
|
|
||
| @Inject | ||
| private DialogService dialogService; | ||
|
|
||
| @Inject | ||
| private GuiPreferences preferences; | ||
| @Inject | ||
| private TaskExecutor taskExecutor; | ||
|
|
||
| private final ControlsFxVisualizer visualizer = new ControlsFxVisualizer(); | ||
|
|
||
| public GitCommitDialogView() { | ||
| ViewLoader.view(this) | ||
| .load() | ||
| .setAsDialogPane(this); | ||
| } | ||
|
|
||
| @FXML | ||
| private void initialize() { | ||
| setTitle(Localization.lang("Git Commit")); | ||
koppor marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| this.viewModel = new GitCommitDialogViewModel(stateManager, dialogService, taskExecutor); | ||
|
|
||
| commitMessage.textProperty().bindBidirectional(viewModel.commitMessageProperty()); | ||
| commitMessage.setPromptText(Localization.lang("Enter commit message here")); | ||
|
|
||
| this.setResultConverter(button -> { | ||
| if (button != ButtonType.CANCEL) { | ||
| viewModel.commit(() -> this.close()); | ||
| } | ||
| return null; | ||
| }); | ||
|
|
||
| Platform.runLater(() -> { | ||
| visualizer.setDecoration(new IconValidationDecorator()); | ||
| visualizer.initVisualization(viewModel.commitMessageValidation(), commitMessage, true); | ||
| }); | ||
| } | ||
| } | ||
132 changes: 132 additions & 0 deletions
132
jabgui/src/main/java/org/jabref/gui/git/GitCommitDialogViewModel.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,132 @@ | ||
| package org.jabref.gui.git; | ||
|
|
||
| import java.io.IOException; | ||
| import java.nio.file.Path; | ||
| import java.util.Optional; | ||
|
|
||
| import javafx.beans.property.BooleanProperty; | ||
| import javafx.beans.property.SimpleBooleanProperty; | ||
| import javafx.beans.property.SimpleStringProperty; | ||
| import javafx.beans.property.StringProperty; | ||
|
|
||
| import org.jabref.gui.AbstractViewModel; | ||
| import org.jabref.gui.DialogService; | ||
| import org.jabref.gui.StateManager; | ||
| import org.jabref.logic.JabRefException; | ||
| import org.jabref.logic.git.GitHandler; | ||
| import org.jabref.logic.git.status.GitStatusChecker; | ||
| import org.jabref.logic.git.status.GitStatusSnapshot; | ||
| import org.jabref.logic.git.util.GitHandlerRegistry; | ||
| import org.jabref.logic.l10n.Localization; | ||
| import org.jabref.logic.util.BackgroundTask; | ||
| import org.jabref.logic.util.TaskExecutor; | ||
| import org.jabref.model.database.BibDatabaseContext; | ||
|
|
||
| import de.saxsys.mvvmfx.utils.validation.FunctionBasedValidator; | ||
| import de.saxsys.mvvmfx.utils.validation.ValidationMessage; | ||
| import de.saxsys.mvvmfx.utils.validation.ValidationStatus; | ||
| import de.saxsys.mvvmfx.utils.validation.Validator; | ||
| import org.eclipse.jgit.api.errors.GitAPIException; | ||
|
|
||
| public class GitCommitDialogViewModel extends AbstractViewModel { | ||
|
|
||
| private final StateManager stateManager; | ||
| private final DialogService dialogService; | ||
| private final TaskExecutor taskExecutor; | ||
|
|
||
| private final StringProperty commitMessage = new SimpleStringProperty(""); | ||
| private final BooleanProperty amend = new SimpleBooleanProperty(false); | ||
|
|
||
| private final Validator commitMessageValidator; | ||
|
|
||
| public GitCommitDialogViewModel( | ||
| StateManager stateManager, | ||
| DialogService dialogService, | ||
| TaskExecutor taskExecutor) { | ||
| this.stateManager = stateManager; | ||
| this.dialogService = dialogService; | ||
| this.taskExecutor = taskExecutor; | ||
|
|
||
| this.commitMessageValidator = new FunctionBasedValidator<>( | ||
| commitMessage, | ||
| message -> message != null && !message.trim().isEmpty(), | ||
| ValidationMessage.error(Localization.lang("Commit message cannot be empty")) | ||
| ); | ||
| } | ||
|
|
||
| public void commit(Runnable onSuccess) { | ||
| commitTask() | ||
| .onSuccess(_-> { | ||
| dialogService.notify(Localization.lang("Committed successfully")); | ||
| onSuccess.run(); | ||
| }) | ||
| .onFailure(ex -> | ||
| dialogService.showErrorDialogAndWait( | ||
| Localization.lang("Git Commit Failed"), | ||
| ex.getMessage(), | ||
| ex | ||
| ) | ||
| ) | ||
| .executeWith(taskExecutor); | ||
| } | ||
|
|
||
| public BackgroundTask<Void> commitTask() { | ||
| return BackgroundTask.wrap(() -> { | ||
| doCommit(); | ||
| return null; | ||
koppor marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| }); | ||
| } | ||
|
|
||
| private void doCommit() throws JabRefException, GitAPIException, IOException { | ||
| Optional<BibDatabaseContext> activeDatabaseOpt = stateManager.getActiveDatabase(); | ||
| if (activeDatabaseOpt.isEmpty()) { | ||
| throw new JabRefException(Localization.lang("No library open")); | ||
| } | ||
|
|
||
| BibDatabaseContext dbContext = activeDatabaseOpt.get(); | ||
| Optional<Path> bibFilePathOpt = dbContext.getDatabasePath(); | ||
| if (bibFilePathOpt.isEmpty()) { | ||
| throw new JabRefException(Localization.lang("No library file path. Please save the library to a file first.")); | ||
| } | ||
|
|
||
| Path bibFilePath = bibFilePathOpt.get(); | ||
| GitHandlerRegistry registry = new GitHandlerRegistry(); | ||
| Optional<Path> repoRootOpt = GitHandler.findRepositoryRoot(bibFilePath); | ||
| if (repoRootOpt.isEmpty()) { | ||
| throw new JabRefException(Localization.lang("Commit aborted: Path is not inside a Git repository.")); | ||
| } | ||
|
|
||
| GitHandler gitHandler = registry.get(repoRootOpt.get()); | ||
|
|
||
| GitStatusSnapshot status = GitStatusChecker.checkStatus(gitHandler); | ||
| if (!status.tracking()) { | ||
| throw new JabRefException(Localization.lang("Commit aborted: The file is not under Git version control.")); | ||
| } | ||
| if (status.conflict()) { | ||
| throw new JabRefException(Localization.lang("Commit aborted: Local repository has unresolved merge conflicts.")); | ||
| } | ||
|
|
||
| String message = commitMessage.get(); | ||
| if (message == null || message.isBlank()) { | ||
| message = Localization.lang("Update references"); | ||
| } | ||
|
|
||
| boolean committed = gitHandler.createCommitOnCurrentBranch(message, amend.get()); | ||
| // TODO: Replace control-flow-by-exception with a proper control structure | ||
| if (!committed) { | ||
| throw new JabRefException(Localization.lang("Nothing to commit.")); | ||
| } | ||
|
Comment on lines
+116
to
+118
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is control flow using exceptions; but changes will take much time --> can be done as follow-up |
||
| } | ||
|
|
||
| public StringProperty commitMessageProperty() { | ||
| return commitMessage; | ||
| } | ||
|
|
||
| public BooleanProperty amendProperty() { | ||
| return amend; | ||
| } | ||
|
|
||
| public ValidationStatus commitMessageValidation() { | ||
| return commitMessageValidator.getValidationStatus(); | ||
| } | ||
| } | ||
31 changes: 31 additions & 0 deletions
31
jabgui/src/main/resources/org/jabref/gui/git/GitCommitDialog.fxml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| <?xml version="1.0" encoding="UTF-8"?> | ||
|
|
||
|
|
||
| <?import javafx.geometry.Insets?> | ||
| <?import javafx.scene.control.ButtonType?> | ||
| <?import javafx.scene.control.DialogPane?> | ||
| <?import javafx.scene.control.TextArea?> | ||
| <?import javafx.scene.layout.VBox?> | ||
| <DialogPane | ||
| xmlns="http://javafx.com/javafx" | ||
| xmlns:fx="http://javafx.com/fxml" | ||
| fx:controller="org.jabref.gui.git.GitCommitDialogView" | ||
| prefHeight="400.0" | ||
| prefWidth="600.0"> | ||
| <content> | ||
| <VBox spacing="15"> | ||
| <padding> | ||
| <Insets top="20" right="20" bottom="20" left="20"/> | ||
| </padding> | ||
|
|
||
| <TextArea fx:id="commitMessage" | ||
| prefRowCount="6" | ||
| wrapText="true" | ||
| VBox.vgrow="ALWAYS"/> | ||
| </VBox> | ||
| </content> | ||
| <buttonTypes> | ||
| <ButtonType fx:id="commitButton" text="%Commit" buttonData="OK_DONE"/> | ||
| <ButtonType fx:constant="CANCEL"/> | ||
| </buttonTypes> | ||
| </DialogPane> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.