Skip to content

Commit 54ea6cd

Browse files
committed
code_style: move some codes from App to Models.InteractiveRebaseJobCollection
Signed-off-by: leo <longshuang@msn.cn>
1 parent 1a3a366 commit 54ea6cd

File tree

2 files changed

+53
-44
lines changed

2 files changed

+53
-44
lines changed

src/App.axaml.cs

Lines changed: 3 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
using System.Reflection;
77
using System.Text;
88
using System.Text.Json;
9-
using System.Text.RegularExpressions;
109
using System.Threading;
1110
using System.Threading.Tasks;
1211

@@ -488,23 +487,7 @@ private static bool TryLaunchAsRebaseTodoEditor(string[] args, out int exitCode)
488487

489488
using var stream = File.OpenRead(jobsFile);
490489
var collection = JsonSerializer.Deserialize(stream, JsonCodeGen.Default.InteractiveRebaseJobCollection);
491-
using var writer = new StreamWriter(file);
492-
foreach (var job in collection.Jobs)
493-
{
494-
var code = job.Action switch
495-
{
496-
Models.InteractiveRebaseAction.Pick => 'p',
497-
Models.InteractiveRebaseAction.Edit => 'e',
498-
Models.InteractiveRebaseAction.Reword => 'r',
499-
Models.InteractiveRebaseAction.Squash => 's',
500-
Models.InteractiveRebaseAction.Fixup => 'f',
501-
_ => 'd'
502-
};
503-
writer.WriteLine($"{code} {job.SHA}");
504-
}
505-
506-
writer.Flush();
507-
490+
collection.WriteTodoList(file);
508491
exitCode = 0;
509492
return true;
510493
}
@@ -535,27 +518,8 @@ private static bool TryLaunchAsRebaseMessageEditor(string[] args, out int exitCo
535518
var onto = File.ReadAllText(ontoFile).Trim();
536519
using var stream = File.OpenRead(jobsFile);
537520
var collection = JsonSerializer.Deserialize(stream, JsonCodeGen.Default.InteractiveRebaseJobCollection);
538-
if (!collection.Onto.Equals(onto) || !collection.OrigHead.Equals(origHead))
539-
return true;
540-
541-
var done = File.ReadAllText(doneFile).Trim().Split(['\r', '\n'], StringSplitOptions.RemoveEmptyEntries);
542-
if (done.Length == 0)
543-
return true;
544-
545-
var current = done[^1].Trim();
546-
var match = REG_REBASE_TODO().Match(current);
547-
if (!match.Success)
548-
return true;
549-
550-
var sha = match.Groups[1].Value;
551-
foreach (var job in collection.Jobs)
552-
{
553-
if (job.SHA.StartsWith(sha))
554-
{
555-
File.WriteAllText(file, job.Message);
556-
break;
557-
}
558-
}
521+
if (collection.Onto.StartsWith(onto, StringComparison.OrdinalIgnoreCase) && collection.OrigHead.StartsWith(origHead, StringComparison.OrdinalIgnoreCase))
522+
collection.WriteCommitMessage(doneFile, file);
559523

560524
return true;
561525
}
@@ -806,9 +770,6 @@ private string FixFontFamilyName(string input)
806770
return trimmed.Count > 0 ? string.Join(',', trimmed) : string.Empty;
807771
}
808772

809-
[GeneratedRegex(@"^[a-z]+\s+([a-fA-F0-9]{4,64})(\s+.*)?$")]
810-
private static partial Regex REG_REBASE_TODO();
811-
812773
private Models.IpcChannel _ipcChannel = null;
813774
private ViewModels.Launcher _launcher = null;
814775
private ResourceDictionary _activeLocale = null;

src/Models/InteractiveRebase.cs

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
using System.Collections.Generic;
1+
using System;
2+
using System.Collections.Generic;
3+
using System.IO;
4+
using System.Text.RegularExpressions;
25

36
namespace SourceGit.Models
47
{
@@ -34,10 +37,55 @@ public class InteractiveRebaseJob
3437
public string Message { get; set; } = string.Empty;
3538
}
3639

37-
public class InteractiveRebaseJobCollection
40+
public partial class InteractiveRebaseJobCollection
3841
{
3942
public string OrigHead { get; set; } = string.Empty;
4043
public string Onto { get; set; } = string.Empty;
4144
public List<InteractiveRebaseJob> Jobs { get; set; } = new List<InteractiveRebaseJob>();
45+
46+
public void WriteTodoList(string todoFile)
47+
{
48+
using var writer = new StreamWriter(todoFile);
49+
foreach (var job in Jobs)
50+
{
51+
var code = job.Action switch
52+
{
53+
InteractiveRebaseAction.Pick => 'p',
54+
InteractiveRebaseAction.Edit => 'e',
55+
InteractiveRebaseAction.Reword => 'r',
56+
InteractiveRebaseAction.Squash => 's',
57+
InteractiveRebaseAction.Fixup => 'f',
58+
_ => 'd'
59+
};
60+
writer.WriteLine($"{code} {job.SHA}");
61+
}
62+
63+
writer.Flush();
64+
}
65+
66+
public void WriteCommitMessage(string doneFile, string msgFile)
67+
{
68+
var done = File.ReadAllText(doneFile).Trim().Split(['\r', '\n'], StringSplitOptions.RemoveEmptyEntries);
69+
if (done.Length == 0)
70+
return;
71+
72+
var current = done[^1].Trim();
73+
var match = REG_REBASE_TODO().Match(current);
74+
if (!match.Success)
75+
return;
76+
77+
var sha = match.Groups[1].Value;
78+
foreach (var job in Jobs)
79+
{
80+
if (job.SHA.StartsWith(sha))
81+
{
82+
File.WriteAllText(msgFile, job.Message);
83+
return;
84+
}
85+
}
86+
}
87+
88+
[GeneratedRegex(@"^[a-z]+\s+([a-fA-F0-9]{4,64})(\s+.*)?$")]
89+
private static partial Regex REG_REBASE_TODO();
4290
}
4391
}

0 commit comments

Comments
 (0)