Skip to content

Commit

Permalink
FormPush show ahead/behind for multiple branches
Browse files Browse the repository at this point in the history
  • Loading branch information
gerhardol committed Oct 24, 2020
1 parent 5b40447 commit a6db253
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 14 deletions.
1 change: 1 addition & 0 deletions GitCommands/Git/AheadBehindData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ public struct AheadBehindData
// gone: "plumbing" expression, see https://git-scm.com/docs/git-for-each-ref#Documentation/git-for-each-ref.txt-upstream
public static readonly string Gone = "gone";
public string Branch { get; set; }
public string RemoteRef { get; set; }
public string AheadCount { get; set; }
public string BehindCount { get; set; }

Expand Down
1 change: 1 addition & 0 deletions GitCommands/Git/AheadBehindDataProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ private IDictionary<string, AheadBehindData> GetData(Encoding encoding, string b
{
// The information is displayed in the push button, so the push info is preferred (may differ from upstream)
Branch = branch,
RemoteRef = remoteRef,
AheadCount =
// Prefer push to upstream for the count
match.Groups["ahead_p"].Success
Expand Down
17 changes: 17 additions & 0 deletions GitCommands/Git/GitRefName.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,23 @@ public static string GetRemoteName([NotNull] string refName, [NotNull, ItemNotNu
return string.Empty;
}

[Pure, NotNull]
public static string GetRemoteBranch([NotNull] string refName)
{
if (refName.Length <= GitRefName.RefsRemotesPrefix.Length)
{
return string.Empty;
}

var startBranch = refName.IndexOf('/', GitRefName.RefsRemotesPrefix.Length);
if (startBranch < 0)
{
return string.Empty;
}

return refName.Substring(1 + startBranch);
}

[Pure, CanBeNull]
public static string GetFullBranchName([CanBeNull] string branch)
{
Expand Down
2 changes: 1 addition & 1 deletion GitUI/CommandsDialogs/FormPush.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

43 changes: 32 additions & 11 deletions GitUI/CommandsDialogs/FormPush.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public partial class FormPush : GitModuleForm
private const string AllRefs = "[ All ]";
private const string LocalColumnName = "Local";
private const string RemoteColumnName = "Remote";
private const string NewColumnName = "New";
private const string AheadColumnName = "New";
private const string PushColumnName = "Push";
private const string ForceColumnName = "Force";
private const string DeleteColumnName = "Delete";
Expand Down Expand Up @@ -87,6 +87,8 @@ public partial class FormPush : GitModuleForm
new TranslationString("Force push may overwrite changes since your last fetch. Do you want to use the safer force with lease instead?");
private readonly TranslationString _forceWithLeaseTooltips =
new TranslationString("Force with lease is a safer way to force push. It ensures you only overwrite work that you have seen in your local repository");
private readonly TranslationString _pushStatusSame = new TranslationString("same");
private readonly TranslationString _pushStatusDiffer = new TranslationString("differ");

#endregion

Expand Down Expand Up @@ -428,7 +430,11 @@ private bool PushChanges(IWin32Window owner)

if (push || force)
{
pushActions.Add(new GitPushAction(row[LocalColumnName].ToString(), row[RemoteColumnName].ToString(), force));
var remoteBranch = row[RemoteColumnName].ToString();
if (!string.IsNullOrWhiteSpace(remoteBranch))
{
pushActions.Add(new GitPushAction(row[LocalColumnName].ToString(), remoteBranch, force));
}
}
else if (delete)
{
Expand Down Expand Up @@ -951,14 +957,14 @@ private void UpdateMultiBranchView()
_branchTable = new DataTable();
_branchTable.Columns.Add(LocalColumnName, typeof(string));
_branchTable.Columns.Add(RemoteColumnName, typeof(string));
_branchTable.Columns.Add(NewColumnName, typeof(string));
_branchTable.Columns.Add(AheadColumnName, typeof(string));
_branchTable.Columns.Add(PushColumnName, typeof(bool));
_branchTable.Columns.Add(ForceColumnName, typeof(bool));
_branchTable.Columns.Add(DeleteColumnName, typeof(bool));
_branchTable.ColumnChanged += BranchTable_ColumnChanged;
LocalColumn.DataPropertyName = LocalColumnName;
RemoteColumn.DataPropertyName = RemoteColumnName;
NewColumn.DataPropertyName = NewColumnName;
NewColumn.DataPropertyName = AheadColumnName;
PushColumn.DataPropertyName = PushColumnName;
ForceColumn.DataPropertyName = ForceColumnName;
DeleteColumn.DataPropertyName = DeleteColumnName;
Expand Down Expand Up @@ -1033,27 +1039,42 @@ string CleanCommandOutput(string processOutput)
void ProcessHeads(IReadOnlyList<IGitRef> remoteHeads)
{
var localHeads = GetLocalBranches().ToList();
var remoteBranches = remoteHeads.ToHashSet(h => h.LocalName);
var remoteBranches = remoteHeads.ToDictionary(h => h.LocalName, h => h);

_branchTable.BeginLoadData();
AheadBehindDataProvider aheadBehindDataProvider = GitVersion.Current.SupportAheadBehindData
? new AheadBehindDataProvider(() => Module.GitExecutable)
: null;
var aheadBehindData = aheadBehindDataProvider?.GetData();

// Add all the local branches.
foreach (var head in localHeads)
{
var remoteName = head.Remote == remote
? head.MergeWith ?? head.Name
: head.Name;
var isKnownAtRemote = remoteBranches.Contains(remoteName);

var isKnownAtRemote = remoteBranches.ContainsKey(remoteName);
var row = _branchTable.NewRow();

// Check if aheadBehind is relevant for this branch
var isAheadRemote = (aheadBehindData?.ContainsKey(head.Name) ?? false)
&& GitRefName.GetRemoteName(aheadBehindData[head.Name].RemoteRef) == remote;

row[ForceColumnName] = false;
row[DeleteColumnName] = false;
row[LocalColumnName] = head.Name;
row[RemoteColumnName] = remoteName;
row[NewColumnName] = isKnownAtRemote ? Strings.No : Strings.Yes;
row[RemoteColumnName] = isAheadRemote
? GitRefName.GetRemoteBranch(aheadBehindData[head.Name].RemoteRef)
: remoteName;

row[AheadColumnName] = isAheadRemote
? aheadBehindData[head.Name].ToDisplay()
: !isKnownAtRemote
? string.Empty
: head.ObjectId == remoteBranches[remoteName].ObjectId
? _pushStatusSame.Text
: _pushStatusDiffer.Text;
row[PushColumnName] = false;

_branchTable.Rows.Add(row);
}

Expand All @@ -1066,7 +1087,7 @@ void ProcessHeads(IReadOnlyList<IGitRef> remoteHeads)

row[LocalColumnName] = null;
row[RemoteColumnName] = remoteHead.LocalName;
row[NewColumnName] = Strings.No;
row[AheadColumnName] = string.Empty;
row[PushColumnName] = false;
row[ForceColumnName] = false;
row[DeleteColumnName] = false;
Expand Down
10 changes: 9 additions & 1 deletion GitUI/Translation/English.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -5296,7 +5296,7 @@ This will initialize and update all submodules recursive.</source>
<target />
</trans-unit>
<trans-unit id="NewColumn.HeaderText">
<source>New at Remote</source>
<source>Ahead/Behind</source>
<target />
</trans-unit>
<trans-unit id="Pull.Text">
Expand Down Expand Up @@ -5429,6 +5429,14 @@ Would you like to do it now?</source>
<source>Force push with lease</source>
<target />
</trans-unit>
<trans-unit id="_pushStatusDiffer.Text">
<source>differ</source>
<target />
</trans-unit>
<trans-unit id="_pushStatusSame.Text">
<source>same</source>
<target />
</trans-unit>
<trans-unit id="_pushToCaption.Text">
<source>Push to {0}</source>
<target />
Expand Down
12 changes: 11 additions & 1 deletion UnitTests/GitCommands.Tests/Git/GitRefNameTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,16 @@ public void GetRemoteName()
Assert.AreEqual("", GitRefName.GetRemoteName("refs/tags/1.0.0", remotes));
}

[Test]
public void GetRemoteBranch()
{
Assert.AreEqual("master", GitRefName.GetRemoteBranch("refs/remotes/foo/master"));
Assert.AreEqual("tmp/master", GitRefName.GetRemoteBranch("refs/remotes/foo/tmp/master"));

Assert.AreEqual("", GitRefName.GetRemoteBranch("refs/remotes/foo"));
Assert.AreEqual("", GitRefName.GetRemoteBranch("short"));
}

[Test]
public void GetFullBranchName()
{
Expand Down Expand Up @@ -68,4 +78,4 @@ public void IsRemoteHead()
Assert.IsFalse(GitRefName.IsRemoteHead(" refs/remotes/origin/HEAD"));
}
}
}
}

0 comments on commit a6db253

Please sign in to comment.