Skip to content

Commit

Permalink
Add rename action (closes #1183)
Browse files Browse the repository at this point in the history
- slide out approach
  • Loading branch information
mkondratek committed Jan 20, 2023
1 parent c5c0d37 commit 3447023
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 28 deletions.
2 changes: 1 addition & 1 deletion CHANGE-NOTES.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Changelog

## v3.3.2
## v3.4.0
- Added Branch Rename action.

## v3.3.1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,13 @@ public void run(ProgressIndicator indicator) {
val newBranchLayout = branchLayout.rename(currentBranchName, newBranchName);
val branchLayoutWriter = RuntimeBinding.instantiateSoleImplementingClass(IBranchLayoutWriter.class);

renameRunnable.run();
// `renameRunnable` may perform some sneakily-asynchronous operations (e.g. renameBranch).
// The high-level method used within the runnable does not allow us to schedule the tasks after them.
// (Stepping deeper is not an option since we would lose some important logic or become very dependent on the internals of git4idea).
// Hence, we wait for the creation of the branch (with exponential backoff).
waitForCreationOfLocalBranch(gitRepository, newBranchName);

runWriteActionOnUIThread(() -> {
MacheteFileWriter.writeBranchLayout(
macheteFilePath,
Expand All @@ -63,18 +70,6 @@ public void run(ProgressIndicator indicator) {
/* backupOldLayout */ true,
/* requestor */ this);

renameRunnable.run();

// `renameRunnable` may perform some sneakily-asynchronous operations (e.g. renameBranch).
// The high-level method used within the runnable does not allow us to schedule the tasks after them.
// (Stepping deeper is not an option since we would lose some important logic or become very dependent on the internals of git4idea).
// Hence, we wait for the creation of the branch (with exponential backoff).
//
// Theoretically we could not wait for the branch creation.
// However, in would result in a premature execution of GitMacheteRepositoryUpdateBackgroundable,
// and eventually with a waring about a branch listed in the machete file that does not exist.
// GitMacheteRepositoryUpdateBackgroundable will wait for this backgroundable to finish.
waitForCreationOfLocalBranch(gitRepository, newBranchName);
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,13 @@ public class SlideOutBackgroundable extends Task.Backgroundable {

public static final String DELETE_LOCAL_BRANCH_ON_SLIDE_OUT_GIT_CONFIG_KEY = "machete.slideOut.deleteLocalBranch";

public SlideOutBackgroundable(String title, IManagedBranchSnapshot branchToSlideOut,
public SlideOutBackgroundable(IManagedBranchSnapshot branchToSlideOut,
GitRepository gitRepository,
@Nullable IManagedBranchSnapshot currentBranchNameIfManaged,
BranchLayout branchLayout,
BaseEnhancedGraphTable graphTable,
@UI Runnable doInUIThreadWhenReady) {
super(gitRepository.getProject(), title);
super(gitRepository.getProject(), getString("action.GitMachete.BaseSlideOutAction.task.title"));
this.project = gitRepository.getProject();
this.branchToSlideOutName = branchToSlideOut.getName();
this.currentBranchNameIfManaged = currentBranchNameIfManaged != null ? currentBranchNameIfManaged.getName() : null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,12 @@
import org.checkerframework.checker.guieffect.qual.UIEffect;

import com.virtuslab.branchlayout.api.BranchLayout;
import com.virtuslab.gitmachete.backend.api.IManagedBranchSnapshot;
import com.virtuslab.gitmachete.frontend.actions.backgroundables.RenameBackgroundable;
import com.virtuslab.gitmachete.frontend.actions.backgroundables.SlideOutBackgroundable;
import com.virtuslab.gitmachete.frontend.actions.expectedkeys.IExpectsKeyGitMacheteRepository;
import com.virtuslab.gitmachete.frontend.resourcebundles.GitMacheteBundle;
import com.virtuslab.gitmachete.frontend.ui.api.table.BaseEnhancedGraphTable;
import com.virtuslab.gitmachete.frontend.vfsutils.GitVfsUtils;
import com.virtuslab.qual.async.ContinuesInBackground;

Expand Down Expand Up @@ -59,24 +62,27 @@ protected void onUpdate(AnActionEvent anActionEvent) {
public void actionPerformed(AnActionEvent anActionEvent) {
val project = getProject(anActionEvent);
val gitRepository = getSelectedGitRepository(anActionEvent);
val currentBranchName = getNameOfBranchUnderAction(anActionEvent);
val branchName = getNameOfBranchUnderAction(anActionEvent);
val branch = branchName != null ? getManagedBranchByName(anActionEvent, branchName) : null;
val branchLayout = getBranchLayout(anActionEvent);
val graphTable = getGraphTable(anActionEvent);

if (gitRepository == null || currentBranchName == null || branchLayout == null) {
if (gitRepository == null || branchName == null || branchLayout == null || branch == null || graphTable == null) {
return;
}

rename(gitRepository, currentBranchName, branchLayout);
rename(gitRepository, graphTable, branch, branchLayout);
}

@ContinuesInBackground
@UIEffect
private void rename(GitRepository gitRepository, String currentBranchName, BranchLayout branchLayout) {
private void rename(GitRepository gitRepository, BaseEnhancedGraphTable graphTable, IManagedBranchSnapshot branch,
BranchLayout branchLayout) {
val project = gitRepository.getProject();

val gitNewBranchDialog = new GitNewBranchDialog(project, Collections.singletonList(gitRepository),
getString("action.GitMachete.BaseRenameAction.description").fmt(currentBranchName),
currentBranchName,
getString("action.GitMachete.BaseRenameAction.description").fmt(branch.getName()),
branch.getName(),
/* showCheckOutOption */ false,
/* showResetOption */ false,
/* showSetTrackingOption */ false,
Expand All @@ -87,14 +93,20 @@ private void rename(GitRepository gitRepository, String currentBranchName, Branc

if (options != null) {
val gitBrancher = GitBrancher.getInstance(project);
Runnable renameRunnable = () -> gitBrancher.renameBranch(currentBranchName, options.getName(),
Runnable renameRunnable = () -> gitBrancher.renameBranch(branch.getName(), options.getName(),
Collections.singletonList(gitRepository));

new RenameBackgroundable(gitRepository,
Runnable doInUIThreadWhenReady = new RenameBackgroundable(gitRepository,
branchLayout,
renameRunnable,
currentBranchName,
options.getName()).queue();
branch.getName(),
options.getName())::queue;

// a hack with currentBranchNameIfManaged
new SlideOutBackgroundable(branch,
gitRepository, branch, branchLayout,
graphTable, doInUIThreadWhenReady).queue();

}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import static com.virtuslab.gitmachete.frontend.actions.common.ActionUtils.getQuotedStringOrCurrent;
import static com.virtuslab.gitmachete.frontend.resourcebundles.GitMacheteBundle.getNonHtmlString;
import static com.virtuslab.gitmachete.frontend.resourcebundles.GitMacheteBundle.getString;

import com.intellij.openapi.actionSystem.AnActionEvent;
import lombok.experimental.ExtensionMethod;
Expand Down Expand Up @@ -74,7 +73,7 @@ private void doSlideOut(AnActionEvent anActionEvent, IManagedBranchSnapshot bran
} else if (selectedGitRepository == null) {
log().debug("selectedGitRepository is null");
} else {
new SlideOutBackgroundable(getString("action.GitMachete.BaseSlideOutAction.task.title"), branchToSlideOut,
new SlideOutBackgroundable(branchToSlideOut,
selectedGitRepository, getCurrentMacheteBranchIfManaged(anActionEvent), branchLayout,
getGraphTable(anActionEvent), /* doInUIThreadWhenReady */ () -> {}).queue();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ private void handleMergedToParent(
// For a branch merged to its parent, we're not syncing to remote.
// Let's just go straight to the next branch.
Runnable doInUIThreadWhenReady = () -> graphTable.queueRepositoryUpdateAndModelRefresh(traverseNextEntry);
new SlideOutBackgroundable(getString("action.GitMachete.BaseSlideOutAction.task.title"),
new SlideOutBackgroundable(
managedBranch, gitRepository, currentBranchIfManaged, branchLayout, graphTable, doInUIThreadWhenReady);
break;

Expand Down

0 comments on commit 3447023

Please sign in to comment.