Skip to content
This repository was archived by the owner on Sep 15, 2023. It is now read-only.

Commit 6ae540e

Browse files
committed
Feat: The plugin now performs a fetch and check for updates when pulling before stashing, this way local changes are not stashed if the local branch is up-to-date
1 parent d843098 commit 6ae540e

File tree

5 files changed

+74
-11
lines changed

5 files changed

+74
-11
lines changed

api/src/main/java/de/adito/git/api/IRepository.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,15 @@ String commit(@NotNull String pMessage, @NotNull List<File> pFileList, @Nullable
152152
*/
153153
Map<String, EPushResult> push(boolean pIsPushTags, @Nullable String pRemoteName) throws AditoGitException;
154154

155+
/**
156+
* Checks if the branch contains all changes from the second branch
157+
*
158+
* @param pBranch the current branch/branch to check
159+
* @param pCompareTo the method checks if the changes from this branch are contained in the first branch. For a pull this would be the upstream branch
160+
* @return true if the changes from the second branch are contained in the first, false otherwise
161+
*/
162+
boolean isUpToDate(@NotNull IBranch pBranch, @NotNull IBranch pCompareTo) throws AditoGitException;
163+
155164
/**
156165
* Pulls the current contents of the tracked remote branch of the currently selected local branch
157166
* from origin via pull --rebase

gui/src/main/java/de/adito/git/gui/actions/ActionUtility.java

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package de.adito.git.gui.actions;
22

3+
import com.google.inject.internal.Messages;
34
import de.adito.git.api.IRepository;
45
import de.adito.git.api.data.EResetType;
56
import de.adito.git.api.data.ICommit;
@@ -10,19 +11,25 @@
1011
import de.adito.git.api.progress.IProgressHandle;
1112
import de.adito.git.gui.dialogs.IDialogProvider;
1213
import de.adito.git.gui.dialogs.results.IStashChangesQuestionDialogResult;
14+
import de.adito.git.impl.Util;
1315
import io.reactivex.Observable;
1416
import org.jetbrains.annotations.NotNull;
1517
import org.jetbrains.annotations.Nullable;
1618

19+
import java.text.MessageFormat;
1720
import java.util.List;
1821
import java.util.Optional;
22+
import java.util.logging.Level;
23+
import java.util.logging.Logger;
1924

2025
/**
2126
* @author m.kaspera, 01.07.2019
2227
*/
2328
class ActionUtility
2429
{
2530

31+
private static final Logger LOGGER = Logger.getLogger(ActionUtility.class.getName());
32+
2633
/**
2734
* Asks the user if he wants to stash the changed files, discard the changes or abort the current action
2835
*
@@ -46,14 +53,17 @@ static boolean handleStash(@NotNull IPrefStore pPrefStore, @NotNull IDialogProvi
4653
if (dialogResult.isStashChanges())
4754
{
4855
if (pHandle != null)
49-
pHandle.setDescription("Stashing existing changes");
50-
pPrefStore.put(pStashKey, pRepository.stashChanges(null, true));
56+
pHandle.setDescription(Util.getResource(ActionUtility.class, "stashProgressMsg"));
57+
String stashId = pRepository.stashChanges(null, true);
58+
LOGGER.log(Level.INFO, () -> MessageFormat.format(Util.getResource(ActionUtility.class, "stashDoneMsg"), stashId));
59+
pPrefStore.put(pStashKey, stashId);
5160
}
5261
else if (dialogResult.isDiscardChanges())
5362
{
5463
ICommit head = pRepository.getCommit(null);
5564
if (head == null)
56-
throw new AditoGitException("Cannot determine HEAD, so the current changes can't be discarded");
65+
throw new AditoGitException(Util.getResource(ActionUtility.class, "stashDiscardErrorMsg"));
66+
LOGGER.log(Level.INFO, () -> Messages.format(Util.getResource(ActionUtility.class, "stashDiscardMsg"), head.getId()));
5767
pRepository.reset(head.getId(), EResetType.HARD);
5868
}
5969
return true;

gui/src/main/java/de/adito/git/gui/actions/PullAction.java

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ class PullAction extends AbstractAction
4141
{
4242
private static final String STASH_ID_KEY = "pull::stashCommitId";
4343
private static final String NO_VALID_REPO_MSG = "no valid repository found";
44+
private final Logger logger = Logger.getLogger(this.getClass().getName());
4445
private final Observable<Optional<IRepository>> repository;
4546
private final IPrefStore prefStore;
4647
private final IDialogProvider dialogProvider;
@@ -67,8 +68,8 @@ class PullAction extends AbstractAction
6768
progressFacade = pProgressFacade;
6869
saveUtil = pSaveUtil;
6970
mergeConflictSequence = pMergeConflictSequence;
70-
putValue(Action.NAME, "Pull");
71-
putValue(Action.SHORT_DESCRIPTION, "Pull all changes from the remote Branch");
71+
putValue(Action.NAME, Util.getResource(this.getClass(), "pullActionName"));
72+
putValue(Action.SHORT_DESCRIPTION, Util.getResource(this.getClass(), "pullTooltipMsg"));
7273
repository = pRepository;
7374
}
7475

@@ -78,7 +79,7 @@ class PullAction extends AbstractAction
7879
@Override
7980
public void actionPerformed(ActionEvent pEvent)
8081
{
81-
progressFacade.executeAndBlockWithProgress("Updating Project...", this::_doRebase);
82+
progressFacade.executeAndBlockWithProgress(Util.getResource(this.getClass(), "pullHandleTitleMsg"), this::_doRebase);
8283
}
8384

8485
/**
@@ -88,12 +89,27 @@ public void actionPerformed(ActionEvent pEvent)
8889
private void _doRebase(@NotNull IProgressHandle pProgressHandle)
8990
{
9091
saveUtil.saveUnsavedFiles();
91-
pProgressHandle.setDescription("Retrieving Repository");
92+
pProgressHandle.setDescription(Util.getResource(this.getClass(), "pullGetRepoHandleMsg"));
9293
IRepository pRepo = repository.blockingFirst().orElseThrow(() -> new RuntimeException(NO_VALID_REPO_MSG));
9394
boolean doAbort = false;
9495
try
9596
{
9697
ICommit head = pRepo.getCommit(null);
98+
IRepositoryState repositoryState = pRepo.getRepositoryState().blockingFirst(Optional.empty()).orElse(null);
99+
if (repositoryState == null || repositoryState.getCurrentRemoteTrackedBranch() == null)
100+
{
101+
notifyUtil.notify(Util.getResource(this.getClass(), "pullAbortTitle"), Util.getResource(this.getClass(), "pullFailedRepoStateMsg"), false);
102+
return;
103+
}
104+
else
105+
{
106+
pProgressHandle.setDescription(Util.getResource(this.getClass(), "pullFetchHandleMsg"));
107+
if (pRepo.isUpToDate(repositoryState.getCurrentBranch(), repositoryState.getCurrentRemoteTrackedBranch()))
108+
{
109+
notifyUtil.notify(Util.getResource(this.getClass(), "pullBrancheUpToDateTitle"), Util.getResource(this.getClass(), "pullBrancheUpToDateMsg"), false);
110+
return;
111+
}
112+
}
97113
if (pRepo.getStatus().blockingFirst().map(pStatus -> !pStatus.getConflicting().isEmpty()).orElse(false))
98114
{
99115
notifyUtil.notify(Util.getResource(this.getClass(), "pullExistingConflictingFilesTitle"),
@@ -111,7 +127,7 @@ private void _doRebase(@NotNull IProgressHandle pProgressHandle)
111127
}
112128
while (!doAbort)
113129
{
114-
pProgressHandle.setDescription("Rebasing");
130+
pProgressHandle.setDescription(Util.getResource(this.getClass(), "pullRebaseHandleMsg"));
115131
IRebaseResult rebaseResult =
116132
pRepo.pull(false);
117133
if (rebaseResult.isSuccess())
@@ -129,7 +145,7 @@ private void _doRebase(@NotNull IProgressHandle pProgressHandle)
129145
}
130146
if (!rebaseResult.getMergeConflicts().isEmpty())
131147
{
132-
pProgressHandle.setDescription("Resolving Conflicts");
148+
pProgressHandle.setDescription(Util.getResource(this.getClass(), "pullResolveConflictsHandleMsg"));
133149
// if the pull should be aborted, _handleConflictDialog returns true
134150
IMergeDetails mergeDetails = new MergeDetailsImpl(rebaseResult.getMergeConflicts(), "Local", "Remote");
135151
doAbort = _handleConflictDialog(pRepo, mergeDetails);
@@ -150,7 +166,7 @@ private void _doRebase(@NotNull IProgressHandle pProgressHandle)
150166
catch (AuthCancelledException pE)
151167
{
152168
notifyUtil.notify(Util.getResource(this.getClass(), "pullAbortTitle"), Util.getResource(this.getClass(), "pullAbortDueToAuthCancel"), false);
153-
Logger.getLogger(this.getClass().getName()).log(Level.WARNING, pE, () -> Util.getResource(this.getClass(), "authCancelledLogMessage"));
169+
logger.log(Level.WARNING, pE, () -> Util.getResource(this.getClass(), "authCancelledLogMessage"));
154170
}
155171
catch (AditoGitException e)
156172
{

gui/src/main/resources/de/adito/git/gui/actions/Bundle.properties

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ mergeSavedStateMsg=Saved the current merge state, use "resolve conflicts" to res
66
mergeBranchesUpToDateTitle=Already up-to-date
77
mergeBranchesUpToDateMsg=Branches are already up-to-date/merged
88
# Pull Action
9+
pullActionName=Pull
10+
pullHandleTitleMsg=Updating Project...
11+
pullTooltipMsg=Pull all changes from the remote Branch
912
pullSuccessTitle=Pull successful
1013
pullSuccessMsg=The pull --rebase was successful, files are now up-to-date
1114
pullAlreadyUpToDate=Files are already up-to-date
@@ -21,6 +24,13 @@ pullExistingConflictingFilesMsg=There are files that have the conflicting state,
2124
pullFailedUpstreamBranchMsg=Pull failed, could not determine upstream branch
2225
pullFailedExceptionMsg=Pull failed due to an exception
2326
rebaseMergeCommitsWarning=<html>Local unpushed commits contain a merge-commit, rebasing a merge-commit can lead to lost data.<br>Do you want to merge the remote branch into the current branch instead?</html>
27+
pullBrancheUpToDateTitle=Already up-to-date
28+
pullBrancheUpToDateMsg=The local branch already has all upstream changes
29+
pullFetchHandleMsg=Checking remote for updates
30+
pullRebaseHandleMsg=Rebasing
31+
pullResolveConflictsHandleMsg=Resolving Conflicts
32+
pullGetRepoHandleMsg=Retrieving Repository
33+
pullFailedRepoStateMsg=Failed to read repository state for the remote tracked branch
2434
#Commit Action
2535
noFilesToCommitMsg=No changed files detected, so there is nothing to commit
2636
noFilesToCommitTitle=No files to commit
@@ -73,7 +83,7 @@ tbnfeDetailsMsg=<html>The plugin is not able to determine the target branch or c
7383
resolveConflictsTitle=Resolve Conflicts
7484
resolveConflictsFailureMsg=Could determine neither the target nor the state before the operation that led to the conflict, please reset the current branch to the commit you based from manually
7585
#StashChangesAction
76-
stashProgressMsg=Stashing changes
86+
stashProgressMsg=Stashing existing changes
7787
stashFailureMsg=An error occurred during the stash process
7888
#UnStashChangesAction
7989
unstashFailureMsg=An error occurred while trying to unstash
@@ -87,3 +97,6 @@ unknownHostMsg=Could not resolve the repository url {0}, make sure it is a valid
8797
renormalizeNewlinesTitle=Renormalize newlines
8898
renormalizeNewlinesFailureNoRepo=Could not find suitable git repo, renormalize newlines was not attempted
8999
renormalizeNewlinesFailureException=Renormalizing the newlines failed
100+
stashDoneMsg=Stashed current changes with commit id {0}
101+
stashDiscardMsg=Git: Discarded current changes by resetting to HEAD commit with id {0}
102+
stashDiscardErrorMsg=Cannot determine HEAD, so the current changes can't be discarded

impl/src/main/java/de/adito/git/impl/RepositoryImpl.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,21 @@ public Map<String, EPushResult> push(boolean pIsPushTags, @Nullable String pRemo
338338
return resultMap;
339339
}
340340

341+
@Override
342+
public boolean isUpToDate(@NotNull IBranch pCommit, @NotNull IBranch pCompareTo) throws AditoGitException
343+
{
344+
fetch();
345+
try
346+
{
347+
String forkPointId = ObjectId.toString(RepositoryImplHelper.findForkPoint(git, pCommit.getName(), pCompareTo.getName()));
348+
return forkPointId.equals(pCompareTo.getId());
349+
}
350+
catch (IOException pE)
351+
{
352+
throw new AditoGitException("Failed to find find fork-point due to an exception", pE);
353+
}
354+
}
355+
341356
private void _setRefSpec(PushCommand pPush, IRepositoryState pRepositoryState)
342357
{
343358
IBranch remoteTrackedBranch = pRepositoryState.getCurrentRemoteTrackedBranch();

0 commit comments

Comments
 (0)