Skip to content

Commit 1fef7a7

Browse files
committed
perf: only update uninited or outdated submodules
Signed-off-by: leo <[email protected]>
1 parent 1872740 commit 1fef7a7

File tree

8 files changed

+55
-36
lines changed

8 files changed

+55
-36
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text.RegularExpressions;
4+
5+
namespace SourceGit.Commands
6+
{
7+
public partial class QueryUpdatableSubmodules : Command
8+
{
9+
[GeneratedRegex(@"^([U\-\+ ])([0-9a-f]+)\s(.*?)(\s\(.*\))?$")]
10+
private static partial Regex REG_FORMAT_STATUS();
11+
12+
public QueryUpdatableSubmodules(string repo)
13+
{
14+
WorkingDirectory = repo;
15+
Context = repo;
16+
Args = "submodule status";
17+
}
18+
19+
public List<string> Result()
20+
{
21+
var submodules = new List<string>();
22+
var rs = ReadToEnd();
23+
24+
var lines = rs.StdOut.Split(['\r', '\n'], StringSplitOptions.RemoveEmptyEntries);
25+
foreach (var line in lines)
26+
{
27+
var match = REG_FORMAT_STATUS().Match(line);
28+
if (match.Success)
29+
{
30+
var stat = match.Groups[1].Value;
31+
var path = match.Groups[3].Value;
32+
if (!stat.StartsWith(' '))
33+
submodules.Add(path);
34+
}
35+
}
36+
37+
return submodules;
38+
}
39+
}
40+
}

src/Commands/Submodule.cs

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -29,23 +29,7 @@ public bool Add(string url, string relativePath, bool recursive)
2929
}
3030
}
3131

32-
public bool Update(string module, bool init, bool recursive, bool useRemote)
33-
{
34-
Args = "submodule update";
35-
36-
if (init)
37-
Args += " --init";
38-
if (recursive)
39-
Args += " --recursive";
40-
if (useRemote)
41-
Args += " --remote";
42-
if (!string.IsNullOrEmpty(module))
43-
Args += $" -- \"{module}\"";
44-
45-
return Exec();
46-
}
47-
48-
public bool Update(List<Models.Submodule> modules, bool init, bool recursive, bool useRemote)
32+
public bool Update(List<string> modules, bool init, bool recursive, bool useRemote = false)
4933
{
5034
var builder = new StringBuilder();
5135
builder.Append("submodule update");
@@ -60,7 +44,7 @@ public bool Update(List<Models.Submodule> modules, bool init, bool recursive, bo
6044
{
6145
builder.Append(" --");
6246
foreach (var module in modules)
63-
builder.Append($" \"{module.Path}\"");
47+
builder.Append($" \"{module}\"");
6448
}
6549

6650
Args = builder.ToString();

src/ViewModels/Checkout.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,9 @@ public override Task<bool> Sure()
7474
{
7575
if (updateSubmodules)
7676
{
77-
var submodules = new Commands.QuerySubmodules(_repo.FullPath).Result();
77+
var submodules = new Commands.QueryUpdatableSubmodules(_repo.FullPath).Result();
7878
if (submodules.Count > 0)
79-
new Commands.Submodule(_repo.FullPath).Use(log).Update(submodules, true, true, false);
79+
new Commands.Submodule(_repo.FullPath).Use(log).Update(submodules, true, true);
8080
}
8181

8282
if (needPopStash)

src/ViewModels/CheckoutCommit.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,9 @@ public override Task<bool> Sure()
7474
{
7575
if (updateSubmodules)
7676
{
77-
var submodules = new Commands.QuerySubmodules(_repo.FullPath).Result();
77+
var submodules = new Commands.QueryUpdatableSubmodules(_repo.FullPath).Result();
7878
if (submodules.Count > 0)
79-
new Commands.Submodule(_repo.FullPath).Use(log).Update(submodules, true, true, false);
79+
new Commands.Submodule(_repo.FullPath).Use(log).Update(submodules, true, true);
8080
}
8181

8282
if (needPop)

src/ViewModels/Clone.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,9 +140,9 @@ public override Task<bool> Sure()
140140

141141
if (InitAndUpdateSubmodules)
142142
{
143-
var submodules = new Commands.QuerySubmodules(path).Result();
143+
var submodules = new Commands.QueryUpdatableSubmodules(path).Result();
144144
if (submodules.Count > 0)
145-
new Commands.Submodule(path).Use(log).Update(submodules, true, true, false);
145+
new Commands.Submodule(path).Use(log).Update(submodules, true, true);
146146
}
147147

148148
log.Complete();

src/ViewModels/CreateBranch.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,9 +144,9 @@ public override Task<bool> Sure()
144144
{
145145
if (updateSubmodules)
146146
{
147-
var submodules = new Commands.QuerySubmodules(_repo.FullPath).Result();
147+
var submodules = new Commands.QueryUpdatableSubmodules(_repo.FullPath).Result();
148148
if (submodules.Count > 0)
149-
new Commands.Submodule(_repo.FullPath).Use(log).Update(submodules, true, true, false);
149+
new Commands.Submodule(_repo.FullPath).Use(log).Update(submodules, true, true);
150150
}
151151

152152
if (needPopStash)

src/ViewModels/Pull.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,9 +153,9 @@ public override Task<bool> Sure()
153153
{
154154
if (updateSubmodules)
155155
{
156-
var submodules = new Commands.QuerySubmodules(_repo.FullPath).Result();
156+
var submodules = new Commands.QueryUpdatableSubmodules(_repo.FullPath).Result();
157157
if (submodules.Count > 0)
158-
new Commands.Submodule(_repo.FullPath).Use(log).Update(submodules, true, true, false);
158+
new Commands.Submodule(_repo.FullPath).Use(log).Update(submodules, true, true);
159159
}
160160

161161
if (needPopStash)

src/ViewModels/UpdateSubmodules.cs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -65,14 +65,9 @@ public override Task<bool> Sure()
6565

6666
return Task.Run(() =>
6767
{
68-
foreach (var submodule in targets)
69-
{
70-
new Commands.Submodule(_repo.FullPath).Use(log).Update(
71-
submodule,
72-
EnableInit,
73-
EnableRecursive,
74-
EnableRemote);
75-
}
68+
new Commands.Submodule(_repo.FullPath)
69+
.Use(log)
70+
.Update(targets, EnableInit, EnableRecursive, EnableRemote);
7671

7772
log.Complete();
7873
CallUIThread(() => _repo.SetWatcherEnabled(true));

0 commit comments

Comments
 (0)