-
Notifications
You must be signed in to change notification settings - Fork 386
Expand file tree
/
Copy pathInteractiveRebase.cs
More file actions
91 lines (80 loc) · 2.58 KB
/
InteractiveRebase.cs
File metadata and controls
91 lines (80 loc) · 2.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
using System;
using System.Collections.Generic;
using System.IO;
using System.Text.RegularExpressions;
namespace SourceGit.Models
{
public enum InteractiveRebaseAction
{
Pick,
Edit,
Reword,
Squash,
Fixup,
Drop,
}
public enum InteractiveRebasePendingType
{
None = 0,
Target,
Pending,
Ignore,
Last,
}
public class InteractiveCommit
{
public Commit Commit { get; set; } = new Commit();
public string Message { get; set; } = string.Empty;
}
public class InteractiveRebaseJob
{
public string SHA { get; set; } = string.Empty;
public InteractiveRebaseAction Action { get; set; } = InteractiveRebaseAction.Pick;
public string Message { get; set; } = string.Empty;
}
public partial class InteractiveRebaseJobCollection
{
public string OrigHead { get; set; } = string.Empty;
public string Onto { get; set; } = string.Empty;
public List<InteractiveRebaseJob> Jobs { get; set; } = new List<InteractiveRebaseJob>();
public void WriteTodoList(string todoFile)
{
using var writer = new StreamWriter(todoFile);
foreach (var job in Jobs)
{
var code = job.Action switch
{
InteractiveRebaseAction.Pick => 'p',
InteractiveRebaseAction.Edit => 'e',
InteractiveRebaseAction.Reword => 'r',
InteractiveRebaseAction.Squash => 's',
InteractiveRebaseAction.Fixup => 'f',
_ => 'd'
};
writer.WriteLine($"{code} {job.SHA}");
}
writer.Flush();
}
public void WriteCommitMessage(string doneFile, string msgFile)
{
var done = File.ReadAllText(doneFile).Trim().Split(['\r', '\n'], StringSplitOptions.RemoveEmptyEntries);
if (done.Length == 0)
return;
var current = done[^1].Trim();
var match = REG_REBASE_TODO().Match(current);
if (!match.Success)
return;
var sha = match.Groups[1].Value;
foreach (var job in Jobs)
{
if (job.SHA.StartsWith(sha))
{
File.WriteAllText(msgFile, job.Message);
return;
}
}
}
[GeneratedRegex(@"^[a-z]+\s+([a-fA-F0-9]{4,64})(\s+.*)?$")]
private static partial Regex REG_REBASE_TODO();
}
}