Skip to content

Commit 78c7168

Browse files
committed
enhance: make commit's subject the same with pretty print parameter %s in git log command
1 parent b4e01a8 commit 78c7168

File tree

6 files changed

+97
-82
lines changed

6 files changed

+97
-82
lines changed

src/Commands/QueryCommits.cs

Lines changed: 60 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using System;
22
using System.Collections.Generic;
3-
using System.Text;
43

54
namespace SourceGit.Commands
65
{
@@ -18,62 +17,66 @@ public QueryCommits(string repo, string limits, bool needFindHead = true)
1817

1918
public List<Models.Commit> Result()
2019
{
21-
Exec();
22-
23-
if (_findFirstMerged && !_isHeadFounded && _commits.Count > 0)
24-
MarkFirstMerged();
25-
26-
return _commits;
27-
}
28-
29-
protected override void OnReadline(string line)
30-
{
31-
switch (_nextPartIdx)
20+
var rs = ReadToEnd();
21+
if (!rs.IsSuccess)
22+
return _commits;
23+
24+
var nextPartIdx = 0;
25+
var start = 0;
26+
var end = rs.StdOut.IndexOf('\n', start);
27+
var max = rs.StdOut.Length;
28+
while (end > 0)
3229
{
33-
case 0:
34-
_current = new Models.Commit() { SHA = line };
35-
_isSubjectSet = false;
36-
_commits.Add(_current);
37-
break;
38-
case 1:
39-
ParseParent(line);
40-
break;
41-
case 2:
42-
ParseDecorators(line);
43-
break;
44-
case 3:
45-
_current.Author = Models.User.FindOrAdd(line);
46-
break;
47-
case 4:
48-
_current.AuthorTime = ulong.Parse(line);
49-
break;
50-
case 5:
51-
_current.Committer = Models.User.FindOrAdd(line);
52-
break;
53-
case 6:
54-
_current.CommitterTime = ulong.Parse(line);
55-
break;
56-
default:
57-
if (line.Equals(_endOfBodyToken, StringComparison.Ordinal))
58-
{
59-
_nextPartIdx = 0;
60-
_current.Body = _bodyReader.ToString().TrimEnd();
61-
_bodyReader.Clear();
62-
}
63-
else
64-
{
65-
if (!_isSubjectSet)
30+
var line = rs.StdOut.Substring(start, end - start);
31+
switch (nextPartIdx)
32+
{
33+
case 0:
34+
_current = new Models.Commit() { SHA = line };
35+
_commits.Add(_current);
36+
break;
37+
case 1:
38+
ParseParent(line);
39+
break;
40+
case 2:
41+
ParseDecorators(line);
42+
break;
43+
case 3:
44+
_current.Author = Models.User.FindOrAdd(line);
45+
break;
46+
case 4:
47+
_current.AuthorTime = ulong.Parse(line);
48+
break;
49+
case 5:
50+
_current.Committer = Models.User.FindOrAdd(line);
51+
break;
52+
case 6:
53+
_current.CommitterTime = ulong.Parse(line);
54+
start = end + 1;
55+
end = rs.StdOut.IndexOf(_endOfBodyToken, start, StringComparison.Ordinal);
56+
if (end > 0)
6657
{
67-
_isSubjectSet = true;
68-
_current.SubjectLen = line.Length;
58+
if (end > start)
59+
_current.Body = rs.StdOut.Substring(start, end - start).TrimEnd();
60+
61+
start = end + _endOfBodyToken.Length + 1;
62+
end = start >= max ? -1 : rs.StdOut.IndexOf('\n', start);
6963
}
7064

71-
_bodyReader.AppendLine(line);
72-
}
73-
return;
65+
nextPartIdx = 0;
66+
continue;
67+
default:
68+
break;
69+
}
70+
71+
nextPartIdx++;
72+
start = end + 1;
73+
end = rs.StdOut.IndexOf('\n', start);
7474
}
7575

76-
_nextPartIdx++;
76+
if (_findFirstMerged && !_isHeadFounded && _commits.Count > 0)
77+
MarkFirstMerged();
78+
79+
return _commits;
7780
}
7881

7982
private void ParseParent(string data)
@@ -106,7 +109,7 @@ private void ParseDecorators(string data)
106109
_current.Decorators.Add(new Models.Decorator()
107110
{
108111
Type = Models.DecoratorType.Tag,
109-
Name = d.Substring(15).Trim(),
112+
Name = d.Substring(15),
110113
});
111114
}
112115
else if (d.EndsWith("/HEAD", StringComparison.Ordinal))
@@ -119,7 +122,7 @@ private void ParseDecorators(string data)
119122
_current.Decorators.Add(new Models.Decorator()
120123
{
121124
Type = Models.DecoratorType.CurrentBranchHead,
122-
Name = d.Substring(19).Trim(),
125+
Name = d.Substring(19),
123126
});
124127
}
125128
else if (d.Equals("HEAD"))
@@ -128,37 +131,33 @@ private void ParseDecorators(string data)
128131
_current.Decorators.Add(new Models.Decorator()
129132
{
130133
Type = Models.DecoratorType.CurrentCommitHead,
131-
Name = d.Trim(),
134+
Name = d,
132135
});
133136
}
134137
else if (d.StartsWith("refs/heads/", StringComparison.Ordinal))
135138
{
136139
_current.Decorators.Add(new Models.Decorator()
137140
{
138141
Type = Models.DecoratorType.LocalBranchHead,
139-
Name = d.Substring(11).Trim(),
142+
Name = d.Substring(11),
140143
});
141144
}
142145
else if (d.StartsWith("refs/remotes/", StringComparison.Ordinal))
143146
{
144147
_current.Decorators.Add(new Models.Decorator()
145148
{
146149
Type = Models.DecoratorType.RemoteBranchHead,
147-
Name = d.Substring(13).Trim(),
150+
Name = d.Substring(13),
148151
});
149152
}
150153
}
151154

152155
_current.Decorators.Sort((l, r) =>
153156
{
154157
if (l.Type != r.Type)
155-
{
156158
return (int)l.Type - (int)r.Type;
157-
}
158159
else
159-
{
160160
return l.Name.CompareTo(r.Name);
161-
}
162161
});
163162

164163
if (_current.IsMerged && !_isHeadFounded)
@@ -191,10 +190,7 @@ private void MarkFirstMerged()
191190
private string _endOfBodyToken = string.Empty;
192191
private List<Models.Commit> _commits = new List<Models.Commit>();
193192
private Models.Commit _current = null;
193+
private bool _findFirstMerged = false;
194194
private bool _isHeadFounded = false;
195-
private bool _findFirstMerged = true;
196-
private int _nextPartIdx = 0;
197-
private bool _isSubjectSet = false;
198-
private StringBuilder _bodyReader = new StringBuilder();
199195
}
200196
}

src/Commands/QuerySingleCommit.cs

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ public Models.Commit Result()
3232
commit.AuthorTime = ulong.Parse(lines[4]);
3333
commit.Committer = Models.User.FindOrAdd(lines[5]);
3434
commit.CommitterTime = ulong.Parse(lines[6]);
35-
commit.SubjectLen = lines[7].Length;
3635

3736
StringBuilder builder = new StringBuilder();
3837
for (int i = 7; i < lines.Length; i++)
@@ -58,7 +57,7 @@ private bool ParseDecorators(List<Models.Decorator> decorators, string data)
5857
decorators.Add(new Models.Decorator()
5958
{
6059
Type = Models.DecoratorType.Tag,
61-
Name = d.Substring(15).Trim(),
60+
Name = d.Substring(15),
6261
});
6362
}
6463
else if (d.EndsWith("/HEAD", StringComparison.Ordinal))
@@ -71,7 +70,7 @@ private bool ParseDecorators(List<Models.Decorator> decorators, string data)
7170
decorators.Add(new Models.Decorator()
7271
{
7372
Type = Models.DecoratorType.CurrentBranchHead,
74-
Name = d.Substring(19).Trim(),
73+
Name = d.Substring(19),
7574
});
7675
}
7776
else if (d.Equals("HEAD"))
@@ -80,37 +79,33 @@ private bool ParseDecorators(List<Models.Decorator> decorators, string data)
8079
decorators.Add(new Models.Decorator()
8180
{
8281
Type = Models.DecoratorType.CurrentCommitHead,
83-
Name = d.Trim(),
82+
Name = d,
8483
});
8584
}
8685
else if (d.StartsWith("refs/heads/", StringComparison.Ordinal))
8786
{
8887
decorators.Add(new Models.Decorator()
8988
{
9089
Type = Models.DecoratorType.LocalBranchHead,
91-
Name = d.Substring(11).Trim(),
90+
Name = d.Substring(11),
9291
});
9392
}
9493
else if (d.StartsWith("refs/remotes/", StringComparison.Ordinal))
9594
{
9695
decorators.Add(new Models.Decorator()
9796
{
9897
Type = Models.DecoratorType.RemoteBranchHead,
99-
Name = d.Substring(13).Trim(),
98+
Name = d.Substring(13),
10099
});
101100
}
102101
}
103102

104103
decorators.Sort((l, r) =>
105104
{
106105
if (l.Type != r.Type)
107-
{
108106
return (int)l.Type - (int)r.Type;
109-
}
110107
else
111-
{
112108
return l.Name.CompareTo(r.Name);
113-
}
114109
});
115110

116111
return isHeadOfCurrent;

src/Models/Commit.cs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,31 @@ public class Commit
1212
public ulong AuthorTime { get; set; } = 0;
1313
public User Committer { get; set; } = User.Invalid;
1414
public ulong CommitterTime { get; set; } = 0;
15-
public int SubjectLen { get; set; } = 0;
1615
public string Body { get; set; } = string.Empty;
1716
public List<string> Parents { get; set; } = new List<string>();
1817
public List<Decorator> Decorators { get; set; } = new List<Decorator>();
1918
public bool HasDecorators => Decorators.Count > 0;
2019
public bool IsMerged { get; set; } = false;
2120
public Thickness Margin { get; set; } = new Thickness(0);
2221

23-
public string Subject => string.IsNullOrWhiteSpace(Body) ? string.Empty : Body.Substring(0, SubjectLen);
22+
public string Subject
23+
{
24+
get
25+
{
26+
var end = Body.IndexOf("\r\n\r\n", StringComparison.Ordinal);
27+
if (end == -1)
28+
{
29+
end = Body.IndexOf("\n\n", StringComparison.Ordinal);
30+
if (end > 0)
31+
return Body.Substring(0, end).Replace("\n", "", StringComparison.Ordinal);
32+
33+
return Body.Replace("\n", " ", StringComparison.Ordinal);
34+
}
35+
36+
return Body.Substring(0, end).Replace("\r\n", " ", StringComparison.Ordinal);
37+
}
38+
}
39+
2440
public string AuthorTimeStr => _utcStart.AddSeconds(AuthorTime).ToString("yyyy/MM/dd HH:mm:ss");
2541
public string CommitterTimeStr => _utcStart.AddSeconds(CommitterTime).ToString("yyyy/MM/dd HH:mm:ss");
2642
public string AuthorTimeShortStr => _utcStart.AddSeconds(AuthorTime).ToString("yyyy/MM/dd");

src/ViewModels/FileHistories.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public FileHistories(string repo, string file)
6262

6363
Task.Run(() =>
6464
{
65-
var commits = new Commands.QueryCommits(_repo, $"-n 10000 -- \"{file}\"").Result();
65+
var commits = new Commands.QueryCommits(_repo, $"-n 10000 -- \"{file}\"", false).Result();
6666
Dispatcher.UIThread.Invoke(() =>
6767
{
6868
IsLoading = false;

src/ViewModels/WorkingCopy.cs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,17 +84,25 @@ public bool UseAmend
8484
{
8585
if (SetProperty(ref _useAmend, value) && value)
8686
{
87-
var commits = new Commands.QueryCommits(_repo.FullPath, "-n 1", false).Result();
88-
if (commits.Count == 0)
87+
var currentBranch = _repo.Branches.Find(x => x.IsCurrent);
88+
if (currentBranch == null)
8989
{
9090
App.RaiseException(_repo.FullPath, "No commits to amend!!!");
9191
_useAmend = false;
9292
OnPropertyChanged();
93+
return;
9394
}
94-
else
95+
96+
var head = new Commands.QuerySingleCommit(_repo.FullPath, currentBranch.Head).Result();
97+
if (head == null)
9598
{
96-
CommitMessage = commits[0].Body;
99+
App.RaiseException(_repo.FullPath, "No commits to amend!!!");
100+
_useAmend = false;
101+
OnPropertyChanged();
102+
return;
97103
}
104+
105+
CommitMessage = head.Body;
98106
}
99107

100108
OnPropertyChanged(nameof(IsCommitWithPushVisible));

src/Views/Histories.axaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@
6868
</ItemsControl>
6969

7070
<TextBlock Classes="monospace"
71-
Text="{Binding Subject}"
71+
Text="{Binding Subject}"
7272
Opacity="{Binding IsMerged, Converter={x:Static c:BoolConverters.HalfIfFalse}}"
7373
FontWeight="{Binding IsCurrentHead, Converter={x:Static c:BoolConverters.BoldIfTrue}}"/>
7474
</StackPanel>

0 commit comments

Comments
 (0)