Skip to content

Commit 4136697

Browse files
committed
enhance: ensure sourcegit_rebase_jobs.json only being used when orig-head and onto are both matched
Signed-off-by: leo <[email protected]>
1 parent 75b4a4b commit 4136697

File tree

4 files changed

+33
-34
lines changed

4 files changed

+33
-34
lines changed

src/App.axaml.cs

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using System.Reflection;
77
using System.Text;
88
using System.Text.Json;
9+
using System.Text.RegularExpressions;
910
using System.Threading;
1011
using System.Threading.Tasks;
1112

@@ -431,21 +432,37 @@ private static bool TryLaunchAsRebaseMessageEditor(string[] args, out int exitCo
431432
return true;
432433

433434
var gitDir = Path.GetDirectoryName(file)!;
435+
var origHeadFile = Path.Combine(gitDir, "rebase-merge", "orig-head");
436+
var ontoFile = Path.Combine(gitDir, "rebase-merge", "onto");
437+
var doneFile = Path.Combine(gitDir, "rebase-merge", "done");
434438
var jobsFile = Path.Combine(gitDir, "sourcegit_rebase_jobs.json");
435-
if (!File.Exists(jobsFile))
439+
if (!File.Exists(ontoFile) || !File.Exists(origHeadFile) || !File.Exists(doneFile) || !File.Exists(jobsFile))
436440
return true;
437441

442+
var origHead = File.ReadAllText(origHeadFile).Trim();
443+
var onto = File.ReadAllText(ontoFile).Trim();
438444
var collection = JsonSerializer.Deserialize(File.ReadAllText(jobsFile), JsonCodeGen.Default.InteractiveRebaseJobCollection);
439-
var doneFile = Path.Combine(gitDir, "rebase-merge", "done");
440-
if (!File.Exists(doneFile))
445+
if (!collection.Onto.Equals(onto) || !collection.OrigHead.Equals(origHead))
441446
return true;
442447

443-
var done = File.ReadAllText(doneFile).Split(new[] { '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries);
444-
if (done.Length > collection.Jobs.Count)
448+
var done = File.ReadAllText(doneFile).Trim().Split([ '\r', '\n' ], StringSplitOptions.RemoveEmptyEntries);
449+
if (done.Length == 0)
445450
return true;
446451

447-
var job = collection.Jobs[done.Length - 1];
448-
File.WriteAllText(file, job.Message);
452+
var current = done[^1].Trim();
453+
var match = REG_REBASE_TODO().Match(current);
454+
if (!match.Success)
455+
return true;
456+
457+
var sha = match.Groups[1].Value;
458+
foreach (var job in collection.Jobs)
459+
{
460+
if (job.SHA.StartsWith(sha))
461+
{
462+
File.WriteAllText(file, job.Message);
463+
break;
464+
}
465+
}
449466

450467
return true;
451468
}
@@ -621,6 +638,9 @@ private string FixFontFamilyName(string input)
621638
return trimmed.Count > 0 ? string.Join(',', trimmed) : string.Empty;
622639
}
623640

641+
[GeneratedRegex(@"^[a-z]+\s+([a-fA-F0-9]{4,40})(\s+.*)?$")]
642+
private static partial Regex REG_REBASE_TODO();
643+
624644
private Models.IpcChannel _ipcChannel = null;
625645
private ViewModels.Launcher _launcher = null;
626646
private ResourceDictionary _activeLocale = null;

src/Models/InteractiveRebase.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ public class InteractiveRebaseJob
2727

2828
public class InteractiveRebaseJobCollection
2929
{
30+
public string OrigHead { get; set; } = string.Empty;
31+
public string Onto { get; set; } = string.Empty;
3032
public List<InteractiveRebaseJob> Jobs { get; set; } = new List<InteractiveRebaseJob>();
3133
}
3234
}

src/ViewModels/InProgressContexts.cs

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,6 @@ public Models.Commit Onto
105105

106106
public RebaseInProgress(Repository repo) : base(repo.FullPath, "rebase")
107107
{
108-
_gitDir = repo.GitDir;
109-
110108
HeadName = File.ReadAllText(Path.Combine(repo.GitDir, "rebase-merge", "head-name")).Trim();
111109
if (HeadName.StartsWith("refs/heads/"))
112110
HeadName = HeadName.Substring(11);
@@ -129,34 +127,14 @@ public RebaseInProgress(Repository repo) : base(repo.FullPath, "rebase")
129127

130128
public override bool Continue()
131129
{
132-
var succ = new Commands.Command()
130+
return new Commands.Command()
133131
{
134132
WorkingDirectory = _repo,
135133
Context = _repo,
136134
Editor = Commands.Command.EditorType.RebaseEditor,
137135
Args = $"rebase --continue",
138136
}.Exec();
139-
140-
if (succ)
141-
{
142-
var jobsFile = Path.Combine(_gitDir, "sourcegit_rebase_jobs.json");
143-
var rebaseMergeHead = Path.Combine(_gitDir, "REBASE_HEAD");
144-
var rebaseMergeFolder = Path.Combine(_gitDir, "rebase-merge");
145-
var rebaseApplyFolder = Path.Combine(_gitDir, "rebase-apply");
146-
if (File.Exists(jobsFile))
147-
File.Delete(jobsFile);
148-
if (File.Exists(rebaseMergeHead))
149-
File.Delete(rebaseMergeHead);
150-
if (Directory.Exists(rebaseMergeFolder))
151-
Directory.Delete(rebaseMergeFolder);
152-
if (Directory.Exists(rebaseApplyFolder))
153-
Directory.Delete(rebaseApplyFolder);
154-
}
155-
156-
return succ;
157-
}
158-
159-
private string _gitDir;
137+
}
160138
}
161139

162140
public class RevertInProgress : InProgressContext

src/ViewModels/InteractiveRebase.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,8 @@ public Task<bool> Start()
162162

163163
var saveFile = Path.Combine(_repo.GitDir, "sourcegit_rebase_jobs.json");
164164
var collection = new Models.InteractiveRebaseJobCollection();
165+
collection.OrigHead = _repo.CurrentBranch.Head;
166+
collection.Onto = On.SHA;
165167
for (int i = Items.Count - 1; i >= 0; i--)
166168
{
167169
var item = Items[i];
@@ -178,9 +180,6 @@ public Task<bool> Start()
178180
return Task.Run(() =>
179181
{
180182
var succ = new Commands.InteractiveRebase(_repo.FullPath, On.SHA).Use(log).Exec();
181-
if (succ)
182-
File.Delete(saveFile);
183-
184183
log.Complete();
185184
Dispatcher.UIThread.Invoke(() => _repo.SetWatcherEnabled(true));
186185
return succ;

0 commit comments

Comments
 (0)