Skip to content

Commit 619fc3c

Browse files
committed
feature: auto-track remote branch when the remote branch is first time to checkout (#1616)
Signed-off-by: leo <[email protected]>
1 parent e2bf9cc commit 619fc3c

File tree

3 files changed

+33
-7
lines changed

3 files changed

+33
-7
lines changed

src/Commands/Branch.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,12 @@ public async Task<bool> RenameAsync(string to)
3232
return await ExecAsync().ConfigureAwait(false);
3333
}
3434

35-
public async Task<bool> SetUpstreamAsync(string upstream)
35+
public async Task<bool> SetUpstreamAsync(Models.Branch tracking)
3636
{
37-
if (string.IsNullOrEmpty(upstream))
37+
if (tracking == null)
3838
Args = $"branch {_name} --unset-upstream";
3939
else
40-
Args = $"branch {_name} -u {upstream}";
40+
Args = $"branch {_name} -u {tracking.FriendlyName}";
4141

4242
return await ExecAsync().ConfigureAwait(false);
4343
}

src/ViewModels/CreateBranch.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,24 @@ public override async Task<bool> Sure()
192192
.CreateAsync(_baseOnRevision, _allowOverwrite);
193193
}
194194

195+
if (succ && BasedOn is Models.Branch { IsLocal: false } basedOn)
196+
{
197+
var autoSetUpstream = true;
198+
foreach (var b in _repo.Branches)
199+
{
200+
if (b.IsLocal && b.Upstream.Equals(basedOn.FullName, StringComparison.Ordinal))
201+
{
202+
autoSetUpstream = false;
203+
break;
204+
}
205+
}
206+
207+
if (autoSetUpstream)
208+
await new Commands.Branch(_repo.FullPath, fixedName)
209+
.Use(log)
210+
.SetUpstreamAsync(basedOn);
211+
}
212+
195213
log.Complete();
196214

197215
if (succ && CheckoutAfterCreated)

src/ViewModels/SetUpstream.cs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.Collections.Generic;
1+
using System;
2+
using System.Collections.Generic;
23
using System.Threading.Tasks;
34

45
namespace SourceGit.ViewModels
@@ -53,17 +54,24 @@ public SetUpstream(Repository repo, Models.Branch local, List<Models.Branch> rem
5354
public override async Task<bool> Sure()
5455
{
5556
ProgressDescription = "Setting upstream...";
57+
Models.Branch upstream = _unset ? null : SelectedRemoteBranch;
5658

57-
var upstream = (_unset || SelectedRemoteBranch == null) ? string.Empty : SelectedRemoteBranch.FullName;
58-
if (upstream == Local.Upstream)
59+
if (upstream == null)
60+
{
61+
if (string.IsNullOrEmpty(Local.Upstream))
62+
return true;
63+
}
64+
else if (upstream.FullName.Equals(Local.Upstream, StringComparison.Ordinal))
65+
{
5966
return true;
67+
}
6068

6169
var log = _repo.CreateLog("Set Upstream");
6270
Use(log);
6371

6472
var succ = await new Commands.Branch(_repo.FullPath, Local.Name)
6573
.Use(log)
66-
.SetUpstreamAsync(upstream.Replace("refs/remotes/", ""));
74+
.SetUpstreamAsync(upstream);
6775

6876
log.Complete();
6977
if (succ)

0 commit comments

Comments
 (0)