Skip to content

Commit b1efd65

Browse files
committed
feature: allows to reword HEAD commit while keeping local changes
Signed-off-by: leo <longshuang@msn.cn>
1 parent 943a4eb commit b1efd65

File tree

5 files changed

+63
-29
lines changed

5 files changed

+63
-29
lines changed

src/Commands/InteractiveRebase.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
namespace SourceGit.Commands
2+
{
3+
public class InteractiveRebase : Command
4+
{
5+
public InteractiveRebase(string repo, string basedOn, bool autoStash)
6+
{
7+
WorkingDirectory = repo;
8+
Context = repo;
9+
Editor = EditorType.RebaseEditor;
10+
Args = "rebase -i --autosquash ";
11+
if (autoStash)
12+
Args += "--autostash ";
13+
Args += basedOn;
14+
}
15+
}
16+
}

src/Commands/Rebase.cs

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,4 @@ public Rebase(string repo, string basedOn, bool autoStash)
1212
Args += basedOn;
1313
}
1414
}
15-
16-
public class InteractiveRebase : Command
17-
{
18-
public InteractiveRebase(string repo, string basedOn, bool autoStash)
19-
{
20-
WorkingDirectory = repo;
21-
Context = repo;
22-
Editor = EditorType.RebaseEditor;
23-
Args = "rebase -i --autosquash ";
24-
if (autoStash)
25-
Args += "--autostash ";
26-
Args += basedOn;
27-
}
28-
}
2915
}

src/ViewModels/Histories.cs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -518,12 +518,6 @@ public ContextMenu CreateContextMenuForSelectedCommits(List<Models.Commit> selec
518518
reword.Icon = App.CreateMenuIcon("Icons.Edit");
519519
reword.Click += (_, e) =>
520520
{
521-
if (_repo.LocalChangesCount > 0)
522-
{
523-
App.RaiseException(_repo.FullPath, "You have local changes. Please run stash or discard first.");
524-
return;
525-
}
526-
527521
if (_repo.CanCreatePopup())
528522
_repo.ShowPopup(new Reword(_repo, commit));
529523
e.Handled = true;

src/ViewModels/Reword.cs

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,16 +32,47 @@ public override async Task<bool> Sure()
3232
return true;
3333

3434
_repo.SetWatcherEnabled(false);
35-
ProgressDescription = "Editing head commit message ...";
35+
ProgressDescription = "Editing HEAD message ...";
3636

3737
var log = _repo.CreateLog("Reword HEAD");
3838
Use(log);
3939

40+
var changes = await new Commands.QueryLocalChanges(_repo.FullPath, false).GetResultAsync();
4041
var signOff = _repo.Settings.EnableSignOffForCommit;
41-
var succ = await new Commands.Commit(_repo.FullPath, _message, signOff, true, false)
42+
var needAutoStash = false;
43+
var succ = false;
44+
45+
foreach (var c in changes)
46+
{
47+
if (c.Index != Models.ChangeState.None)
48+
{
49+
needAutoStash = true;
50+
break;
51+
}
52+
}
53+
54+
if (needAutoStash)
55+
{
56+
succ = await new Commands.Stash(_repo.FullPath)
57+
.Use(log)
58+
.PushAsync("REWORD_AUTO_STASH");
59+
if (!succ)
60+
{
61+
log.Complete();
62+
_repo.SetWatcherEnabled(true);
63+
return false;
64+
}
65+
}
66+
67+
succ = await new Commands.Commit(_repo.FullPath, _message, signOff, true, false)
4268
.Use(log)
4369
.RunAsync();
4470

71+
if (succ && needAutoStash)
72+
await new Commands.Stash(_repo.FullPath)
73+
.Use(log)
74+
.PopAsync("stash@{0}");
75+
4576
log.Complete();
4677
_repo.SetWatcherEnabled(true);
4778
return succ;

src/ViewModels/Squash.cs

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,24 +32,31 @@ public override async Task<bool> Sure()
3232
var log = _repo.CreateLog("Squash");
3333
Use(log);
3434

35+
var changes = await new Commands.QueryLocalChanges(_repo.FullPath, false).GetResultAsync();
3536
var signOff = _repo.Settings.EnableSignOffForCommit;
36-
var autoStashed = false;
37-
bool succ;
37+
var needAutoStash = false;
38+
var succ = false;
3839

39-
if (_repo.LocalChangesCount > 0)
40+
foreach (var c in changes)
41+
{
42+
if (c.Index != Models.ChangeState.None)
43+
{
44+
needAutoStash = true;
45+
break;
46+
}
47+
}
48+
49+
if (needAutoStash)
4050
{
4151
succ = await new Commands.Stash(_repo.FullPath)
4252
.Use(log)
4353
.PushAsync("SQUASH_AUTO_STASH");
44-
4554
if (!succ)
4655
{
4756
log.Complete();
4857
_repo.SetWatcherEnabled(true);
4958
return false;
5059
}
51-
52-
autoStashed = true;
5360
}
5461

5562
succ = await new Commands.Reset(_repo.FullPath, Target.SHA, "--soft")
@@ -61,7 +68,7 @@ public override async Task<bool> Sure()
6168
.Use(log)
6269
.RunAsync();
6370

64-
if (succ && autoStashed)
71+
if (succ && needAutoStash)
6572
await new Commands.Stash(_repo.FullPath)
6673
.Use(log)
6774
.PopAsync("stash@{0}");

0 commit comments

Comments
 (0)