Skip to content

Commit 8c772f2

Browse files
committed
enhance: stage/unstage operation
- Do not use `add .` since it may run very slowly - Rename `UnstageChangesForAmend` command to `UpdateIndexInfo` and force using UTF-8 (without BOM) encoding for inputs - Remove unused commands Signed-off-by: leo <longshuang@msn.cn>
1 parent 4692df9 commit 8c772f2

File tree

5 files changed

+19
-68
lines changed

5 files changed

+19
-68
lines changed

src/Commands/Add.cs

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,11 @@
22
{
33
public class Add : Command
44
{
5-
public Add(string repo, bool includeUntracked)
6-
{
7-
WorkingDirectory = repo;
8-
Context = repo;
9-
Args = includeUntracked ? "add ." : "add -u .";
10-
}
11-
12-
public Add(string repo, Models.Change change)
13-
{
14-
WorkingDirectory = repo;
15-
Context = repo;
16-
Args = $"add -- {change.Path.Quoted()}";
17-
}
18-
195
public Add(string repo, string pathspecFromFile)
206
{
217
WorkingDirectory = repo;
228
Context = repo;
23-
Args = $"add --pathspec-from-file={pathspecFromFile.Quoted()}";
9+
Args = $"add --force --verbose --pathspec-from-file={pathspecFromFile.Quoted()}";
2410
}
2511
}
2612
}

src/Commands/Restore.cs

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -4,38 +4,13 @@ namespace SourceGit.Commands
44
{
55
public class Restore : Command
66
{
7-
/// <summary>
8-
/// Only used for single staged change.
9-
/// </summary>
10-
/// <param name="repo"></param>
11-
/// <param name="stagedChange"></param>
12-
public Restore(string repo, Models.Change stagedChange)
13-
{
14-
WorkingDirectory = repo;
15-
Context = repo;
16-
17-
var builder = new StringBuilder();
18-
builder.Append("restore --staged -- ").Append(stagedChange.Path.Quoted());
19-
20-
if (stagedChange.Index == Models.ChangeState.Renamed)
21-
builder.Append(' ').Append(stagedChange.OriginalPath.Quoted());
22-
23-
Args = builder.ToString();
24-
}
25-
26-
/// <summary>
27-
/// Restore changes given in a path-spec file.
28-
/// </summary>
29-
/// <param name="repo"></param>
30-
/// <param name="pathspecFile"></param>
31-
/// <param name="isStaged"></param>
327
public Restore(string repo, string pathspecFile, bool isStaged)
338
{
349
WorkingDirectory = repo;
3510
Context = repo;
3611

3712
var builder = new StringBuilder();
38-
builder.Append("restore ");
13+
builder.Append("restore --progress ");
3914
builder.Append(isStaged ? "--staged " : "--worktree --recurse-submodules ");
4015
builder.Append("--pathspec-from-file=").Append(pathspecFile.Quoted());
4116

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66

77
namespace SourceGit.Commands
88
{
9-
public class UnstageChangesForAmend
9+
public class UpdateIndexInfo
1010
{
11-
public UnstageChangesForAmend(string repo, List<Models.Change> changes)
11+
public UpdateIndexInfo(string repo, List<Models.Change> changes)
1212
{
1313
_repo = repo;
1414

@@ -18,7 +18,7 @@ public UnstageChangesForAmend(string repo, List<Models.Change> changes)
1818
{
1919
_patchBuilder.Append("0 0000000000000000000000000000000000000000\t");
2020
_patchBuilder.Append(c.Path);
21-
_patchBuilder.Append("\0100644 ");
21+
_patchBuilder.Append("\n100644 ");
2222
_patchBuilder.Append(c.DataForAmend.ObjectHash);
2323
_patchBuilder.Append("\t");
2424
_patchBuilder.Append(c.OriginalPath);
@@ -60,6 +60,8 @@ public async Task<bool> ExecAsync()
6060
starter.RedirectStandardInput = true;
6161
starter.RedirectStandardOutput = false;
6262
starter.RedirectStandardError = true;
63+
starter.StandardInputEncoding = new UTF8Encoding(false);
64+
starter.StandardErrorEncoding = Encoding.UTF8;
6365

6466
try
6567
{
@@ -78,7 +80,7 @@ public async Task<bool> ExecAsync()
7880
}
7981
catch (Exception e)
8082
{
81-
App.RaiseException(_repo, "Failed to unstage changes: " + e.Message);
83+
App.RaiseException(_repo, "Failed to update index: " + e.Message);
8284
return false;
8385
}
8486
}

src/ViewModels/Discard.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public override async Task<bool> Sure()
6767
using var lockWatcher = _repo.LockWatcher();
6868
ProgressDescription = _changes == null ? "Discard all local changes ..." : $"Discard total {_changes.Count} changes ...";
6969

70-
var log = _repo.CreateLog("Discard all");
70+
var log = _repo.CreateLog("Discard Changes");
7171
Use(log);
7272

7373
if (Mode is DiscardAllMode all)

src/ViewModels/WorkingCopy.cs

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -348,22 +348,15 @@ public async Task StageChangesAsync(List<Models.Change> changes, Models.Change n
348348
using var lockWatcher = _repo.LockWatcher();
349349

350350
var log = _repo.CreateLog("Stage");
351-
if (count == _unstaged.Count)
351+
var pathSpecFile = Path.GetTempFileName();
352+
await using (var writer = new StreamWriter(pathSpecFile))
352353
{
353-
await new Commands.Add(_repo.FullPath, _repo.IncludeUntracked).Use(log).ExecAsync();
354+
foreach (var c in canStaged)
355+
await writer.WriteLineAsync(c.Path);
354356
}
355-
else
356-
{
357-
var pathSpecFile = Path.GetTempFileName();
358-
await using (var writer = new StreamWriter(pathSpecFile))
359-
{
360-
foreach (var c in canStaged)
361-
await writer.WriteLineAsync(c.Path);
362-
}
363357

364-
await new Commands.Add(_repo.FullPath, pathSpecFile).Use(log).ExecAsync();
365-
File.Delete(pathSpecFile);
366-
}
358+
await new Commands.Add(_repo.FullPath, pathSpecFile).Use(log).ExecAsync();
359+
File.Delete(pathSpecFile);
367360
log.Complete();
368361

369362
_repo.MarkWorkingCopyDirtyManually();
@@ -385,7 +378,7 @@ public async Task UnstageChangesAsync(List<Models.Change> changes, Models.Change
385378
if (_useAmend)
386379
{
387380
log.AppendLine("$ git update-index --index-info ");
388-
await new Commands.UnstageChangesForAmend(_repo.FullPath, changes).ExecAsync();
381+
await new Commands.UpdateIndexInfo(_repo.FullPath, changes).ExecAsync();
389382
}
390383
else
391384
{
@@ -646,16 +639,11 @@ public async Task CommitAsync(bool autoStage, bool autoPush)
646639
IsCommitting = true;
647640
_repo.Settings.PushCommitMessage(_commitMessage);
648641

649-
var log = _repo.CreateLog("Commit");
650-
var succ = true;
651642
if (autoStage && _unstaged.Count > 0)
652-
succ = await new Commands.Add(_repo.FullPath, _repo.IncludeUntracked)
653-
.Use(log)
654-
.ExecAsync()
655-
.ConfigureAwait(false);
643+
await StageChangesAsync(_unstaged, null);
656644

657-
if (succ)
658-
succ = await new Commands.Commit(_repo.FullPath, _commitMessage, EnableSignOff, NoVerifyOnCommit, _useAmend, _resetAuthor)
645+
var log = _repo.CreateLog("Commit");
646+
var succ = await new Commands.Commit(_repo.FullPath, _commitMessage, EnableSignOff, NoVerifyOnCommit, _useAmend, _resetAuthor)
659647
.Use(log)
660648
.RunAsync()
661649
.ConfigureAwait(false);

0 commit comments

Comments
 (0)