10000 refactor: rewrite checkout/create branch with submodules · sourcegit-scm/sourcegit@0f6c897 · GitHub
[go: up one dir, main page]

Skip to content

Commit 0f6c897

Browse files
committed
refactor: rewrite checkout/create branch with submodules
Signed-off-by: leo <longshuang@msn.cn>
1 parent e446e97 commit 0f6c897

File tree

8 files changed

+136
-65
lines changed

8 files changed

+136
-65
lines changed

src/Commands/Checkout.cs

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,27 @@ public Checkout(string repo)
1111
Context = repo;
1212
}
1313

14-
public bool Branch(string branch)
14+
public bool Branch(string branch, bool force)
1515
{
16-
Args = $"checkout --progress {branch}";
16+
var option = force ? "--force" : string.Empty;
17+
Args = $"checkout {option} --progress {branch}";
1718
return Exec();
1819
}
1920

20-
public bool Branch(string branch, string basedOn)
21+
public bool Branch(string branch, string basedOn, bool force)
2122
{
23+
var option = force ? "--force" : string.Empty;
2224
Args = $"checkout --progress -b {branch} {basedOn}";
2325
return Exec();
2426
}
2527

28+
public bool Commit(string commitId, bool force)
29+
{
30+
var option = force ? "--force" : string.Empty;
31+
Args = $"checkout {option} --detach --progress {commitId}";
32+
return Exec();
33+
}
34+
2635
public bool UseTheirs(List<string> files)
2736
{
2837
var builder = new StringBuilder();
@@ -56,11 +65,5 @@ public bool FileWithRevision(string file, string revision)
5665
Args = $"checkout --no-overlay {revision} -- \"{file}\"";
5766
return Exec();
5867
}
59-
60-
public bool Commit(string commitId)
61-
{
62-
Args = $"checkout --detach --progress {commitId}";
63-
return Exec();
64-
}
6568
}
6669
}

src/Commands/Submodule.cs

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
namespace SourceGit.Commands
1+
using System.Collections.Generic;
2+
using System.Text;
3+
4+
namespace SourceGit.Commands
25
{
36
public class Submodule : Command
47
{
@@ -42,6 +45,28 @@ public bool Update(string module, bool init, bool recursive, bool useRemote)
4245
return Exec();
4346
}
4447

48+
public bool Update(List<Models.Submodule> modules, bool init, bool recursive, bool useRemote)
49+
{
50+
var builder = new StringBuilder();
51+
builder.Append("submodule update");
52+
53+
if (init)
54+
builder.Append(" --init");
55+
if (recursive)
56+
builder.Append(" --recursive");
57+
if (useRemote)
58+
builder.Append(" --remote");
59+
if (modules.Count > 0)
60+
{
61+
builder.Append(" --");
62+
foreach (var module in modules)
63+
builder.Append($" \"{module.Path}\"");
64+
}
65+
66+
Args = builder.ToString();
67+
return Exec();
68+
}
69+
4570
public bool Delete(string relativePath)
4671
{
4772
Args = $"submodule deinit -f \"{relativePath}\"";

src/ViewModels/Checkout.cs

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -46,17 +46,19 @@ public override Task<bool> Sure()
4646
var updateSubmodules = IsRecurseSubmoduleVisible && RecurseSubmodules;
4747
return Task.Run(() =>
4848
{
49-
var changes = new Commands.CountLocalChangesWithoutUntracked(_repo.FullPath).Result();
49+
var succ = false;
5050
var needPopStash = false;
51-
if (changes > 0)
51+
52+
if (DiscardLocalChanges)
5253
{
53-
if (DiscardLocalChanges)
54-
{
55-
Commands.Discard.All(_repo.FullPath, false, log);
56-
}
57-
else
54+
succ = new Commands.Checkout(_repo.FullPath).Use(log).Branch(Branch, true);
55+
}
56+
else
57+
{
58+
var changes = new Commands.CountLocalChangesWithoutUntracked(_repo.FullPath).Result();
59+
if (changes > 0)
5860
{
59-
var succ = new Commands.Stash(_repo.FullPath).Use(log).Push("CHECKOUT_AUTO_STASH");
61+
succ = new Commands.Stash(_repo.FullPath).Use(log).Push("CHECKOUT_AUTO_STASH");
6062
if (!succ)
6163
{
6264
log.Complete();
@@ -66,18 +68,22 @@ public override Task<bool> Sure()
6668

6769
needPopStash = true;
6870
}
71+
72+
succ = new Commands.Checkout(_repo.FullPath).Use(log).Branch(Branch, false);
6973
}
7074

71-
var rs = new Commands.Checkout(_repo.FullPath 8000 ).Use(log).Branch(Branch);
72-
if (rs && updateSubmodules)
75+
if (succ)
7376
{
74-
var submodules = new Commands.QuerySubmodules(_repo.FullPath).Result();
75-
foreach (var submodule in submodules)
76-
new Commands.Submodule(_repo.FullPath).Use(log).Update(submodule.Path, true, true, false);
77-
}
77+
if (updateSubmodules)
78+
{
79+
var submodules = new Commands.QuerySubmodules(_repo.FullPath).Result();
80+
if (submodules.Count > 0)
81+
new Commands.Submodule(_repo.FullPath).Use(log).Update(submodules, true, true, false);
82+
}
7883

79-
if (rs && needPopStash)
80-
rs = new Commands.Stash(_repo.FullPath).Use(log).Pop("stash@{0}");
84+
if (needPopStash)
85+
new Commands.Stash(_repo.FullPath).Use(log).Pop("stash@{0}");
86+
}
8187

8288
log.Complete();
8389

@@ -94,7 +100,7 @@ public override Task<bool> Sure()
94100
});
95101

96102
Task.Delay(400).Wait();
97-
return rs;
103+
return succ;
98104
});
99105
}
100106

src/ViewModels/CheckoutCommit.cs

Lines changed: 41 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,24 @@ public bool DiscardLocalChanges
1515
set;
1616
}
1717

18+
public bool IsRecurseSubmoduleVisible
19+
{
20+
get;
21+
private set;
22+
}
23+
24+
public bool RecurseSubmodules
25+
{
26+
get => _repo.Settings.UpdateSubmodulesOnCheckoutBranch;
27+
set => _repo.Settings.UpdateSubmodulesOnCheckoutBranch = value;
28+
}
29+
1830
public CheckoutCommit(Repository repo, Models.Commit commit)
1931
{
2032
_repo = repo;
2133
Commit = commit;
2234
DiscardLocalChanges = false;
35+
IsRecurseSubmoduleVisible = repo.Submodules.Count > 0;
2336
}
2437

2538
public override Task<bool> Sure()
@@ -30,37 +43,51 @@ public override Task<bool> Sure()
3043
var log = _repo.CreateLog("Checkout Commit");
3144
Use(log);
3245

46+
var updateSubmodules = IsRecurseSubmoduleVisible && RecurseSubmodules;
3347
return Task.Run(() =>
3448
{
35-
var changes = new Commands.CountLocalChangesWithoutUntracked(_repo.FullPath).Result();
36-
var needPopStash = false;
37-
if (changes > 0)
49+
var succ = false;
50+
var needPop = false;
51+
52+
if (DiscardLocalChanges)
3853
{
39-
if (DiscardLocalChanges)
40-
{
41-
Commands.Discard.All(_repo.FullPath, false, log);
42-
}
43-
else
54+
succ = new Commands.Checkout(_repo.FullPath).Use(log).Commit(Commit.SHA, true);
55+
}
56+
else
57+
{
58+
var changes = new Commands.CountLocalChangesWithoutUntracked(_repo.FullPath).Result();
59+
if (changes > 0)
4460
{
45-
var succ = new Commands.Stash(_repo.FullPath).Use(log).Push("CHECKOUT_AUTO_STASH");
61+
succ = new Commands.Stash(_repo.FullPath).Use(log).Push("CHECKOUT_AUTO_STASH");
4662
if (!succ)
4763
{
4864
log.Complete();
4965
CallUIThread(() => _repo.SetWatcherEnabled(true));
5066
return false;
5167
}
5268

53-
needPopStash = true;
69+
needPop = true;
5470
}
71+
72+
succ = new Commands.Checkout(_repo.FullPath).Use(log).Commit(Commit.SHA, false);
5573
}
5674

57-
var rs = new Commands.Checkout(_repo.FullPath).Use(log).Commit(Commit.SHA);
58-
if (needPopStash)
59-
rs = new Commands.Stash(_repo.FullPath).Use(log).Pop("stash@{0}");
75+
if (succ)
76+
{
77+
if (updateSubmodules)
78+
{
79+
var submodules = new Commands.QuerySubmodules(_repo.FullPath).Result();
80+
if (submodules.Count > 0)
81+
new Commands.Submodule(_repo.FullPath).Use(log).Update(submodules, true, true, false);
82+
}
83+
84+
if (needPop)
85+
new Commands.Stash(_repo.FullPath).Use(log).Pop("stash@{0}");
86+
}
6087

6188
log.Complete();
6289
CallUIThread(() => _repo.SetWatcherEnabled(true));
63-
return rs;
90+
return succ;
6491
});
6592
}
6693

src/ViewModels/Clone.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,8 +141,8 @@ public override Task<bool> Sure()
141141
if (InitAndUpdateSubmodules)
142142
{
143143
var submodules = new Commands.QuerySubmodules(path).Result();
144-
foreach (var submodule in submodules)
145-
new Commands.Submodule(path).Use(log).Update(submodule.Path, true, true, false);
144+
if (submodules.Count > 0)
145+
new Commands.Submodule(path).Use(log).Update(submodules, true, true, false);
146146
}
147147

148148
log.Complete();

src/ViewModels/CommitDetail.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -864,7 +864,7 @@ private Task ResetToThisRevision(string path)
864864
{
865865
new Commands.Checkout(_repo.FullPath).Use(log).FileWithRevision(path, $"{_commit.SHA}");
866866
log.Complete();
867-
});
867+
});
868868
}
869869

870870
private Task ResetToParentRevision(Models.Change change)

src/ViewModels/CreateBranch.cs

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -117,18 +117,18 @@ public override Task<bool> Sure()
117117
var updateSubmodules = IsRecurseSubmoduleVisible && RecurseSubmodules;
118118
return Task.Run(() =>
119119
{
120-
bool succ;
120+
bool succ = false;
121121
if (CheckoutAfterCreated && !_repo.IsBare)
122122
{
123-
var changes = new Commands.CountLocalChangesWithoutUntracked(_repo.FullPath).Result();
124123
var needPopStash = false;
125-
if (changes > 0)
124+
if (DiscardLocalChanges)
126125
{
127-
if (DiscardLocalChanges)
128-
{
129-
Commands.Discard.All(_repo.FullPath, false, log);
130-
}
131-
else
126+
succ = new Commands.Checkout(_repo.FullPath).Use(log).Branch(fixedName, _baseOnRevision, true);
127+
}
128+
else
129+
{
130+
var changes = new Commands.CountLocalChangesWithoutUntracked(_repo.FullPath).Result();
131+
if (changes > 0)
132132
{
133133
succ = new Commands.Stash(_repo.FullPath).Use(log).Push("CREATE_BRANCH_AUTO_STASH");
134134
if (!succ)
@@ -140,18 +140,22 @@ public override Task<bool> Sure()
140140

141141
needPopStash = true;
142142
}
143+
144+
succ = new Commands.Checkout(_repo.FullPath).Use(log).Branch(fixedName, _baseOnRevision, false);
143145
}
144146

145-
succ = new Commands.Checkout(_repo.FullPath).Use(log).Branch(fixedName, _baseOnRevision);
146-
if (succ && updateSubmodules)
147+
if (succ)
147148
{
148-
var submodules = new Commands.QuerySubmodules(_repo.FullPath).Result();
149-
foreach (var submodule in submodules)
150-
new Commands.Submodule(_repo.FullPath).Use(log).Update(submodule.Path, true, true, false);
151-
}
149+
if (updateSubmodules)
150+
{
151+
var submodules = new Commands.QuerySubmodules(_repo.FullPath).Result();
152+
if (submodules.Count > 0)
153+
new Commands.Submodule(_repo.FullPath).Use(log).Update(submodules, true, true, false);
154+
}
152155

153-
if (succ && needPopStash)
154-
new Commands.Stash(_repo.FullPath).Use(log).Pop("stash@{0}");
156+
if (needPopStash)
157+
new Commands.Stash(_repo.FullPath).Use(log).Pop("stash@{0}");
158+
}
155159
}
156160
else
157161
{
@@ -174,17 +178,16 @@ public override Task<bool> Sure()
174178

175179
if (_repo.HistoriesFilterMode == Models.FilterMode.Included)
176180
_repo.SetBranchFilterMode(fake, Models.FilterMode.Included, true, false);
181+
182+
ProgressDescription = "Waiting for branch updated...";
177183
}
178184

179185
_repo.MarkBranchesDirtyManually();
180186
_repo.SetWatcherEnabled(true);
181187
});
182188

183189
if (CheckoutAfterCreated)
184-
{
185-
CallUIThread(() => ProgressDescription = "Waiting for branch updated...");
186190
Task.Delay(400).Wait();
187-
}
188191

189192
return true;
190193
});

src/Views/CheckoutCommit.axaml

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
Classes="bold"
1313
Text="{DynamicResource Text.Checkout.Commit}" />
1414

15-
<Grid Margin="0,16,0,0" RowDefinitions="32,Auto,Auto" ColumnDefinitions="Auto,*">
15+
<Grid Margin="0,16,0,0" RowDefinitions="32,Auto,Auto,Auto" ColumnDefinitions="Auto,*">
1616
<TextBlock Grid.Row="0" Grid.Column="0"
1717
HorizontalAlignment="Right" VerticalAlignment="Center"
1818
Margin="0,0,8,0"
@@ -36,7 +36,14 @@
3636
GroupName="LocalChanges"/>
3737
</StackPanel>
3838

39-
<Grid Grid.Row="2" Grid.Column="1" ColumnDefinitions="Auto,*" Margin="0,6,0,0">
39+
<CheckBox Grid.Row="2" Grid.Column="1"
40+
Height="32"
41+
Content="{DynamicResource Text.Checkout.RecurseSubmodules}"
42+
IsChecked="{Binding RecurseSubmodules, Mode=TwoWay}"
43+
IsVisible="{Binding IsRecurseSubmoduleVisible}"
44+
ToolTip.Tip="--recurse-submodules"/>
45+
46+
<Grid Grid.Row="3" Grid.Column="1" ColumnDefinitions="Auto,*" Margin="0,6,0,0">
4047
<Path Grid.Column="0"
4148
Width="14" Height="14"
4249
Data="{StaticResource Icons.Error}"

0 commit comments

Comments
 (0)
0