Skip to content

Commit 0040226

Browse files
authored
Add "Copy Message" to context-menu for Stash item (#1430)
* Refactor: Simplify parsing in QueryStashes, by passing the `-z` argument to `git stash list` for item separation. * Add "Copy Message" command in stash-item context-menu.
1 parent 240db2e commit 0040226

File tree

3 files changed

+47
-47
lines changed

3 files changed

+47
-47
lines changed

src/Commands/QueryStashes.cs

Lines changed: 36 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,9 @@ public class QueryStashes : Command
77
{
88
public QueryStashes(string repo)
99
{
10-
_boundary = $"-----BOUNDARY_OF_COMMIT{Guid.NewGuid()}-----";
11-
1210
WorkingDirectory = repo;
1311
Context = repo;
14-
Args = $"stash list --no-show-signature --format=\"%H%n%P%n%ct%n%gd%n%B%n{_boundary}\"";
12+
Args = $"stash list -z --no-show-signature --format=\"%H%n%P%n%ct%n%gd%n%B\"";
1513
}
1614

1715
public List<Models.Stash> Result()
@@ -21,66 +19,57 @@ public QueryStashes(string repo)
2119
if (!rs.IsSuccess)
2220
return outs;
2321

24-
var nextPartIdx = 0;
25-
var start = 0;
26-
var end = rs.StdOut.IndexOf('\n', start);
27-
while (end > 0)
22+
var items = rs.StdOut.Split('\0', System.StringSplitOptions.RemoveEmptyEntries);
23+
foreach (var item in items)
2824
{
29-
var line = rs.StdOut.Substring(start, end - start);
25+
var current = new Models.Stash();
3026

31-
switch (nextPartIdx)
27+
var nextPartIdx = 0;
28+
var start = 0;
29+
var end = item.IndexOf('\n', start);
30+
while (end > 0 && nextPartIdx < 4)
3231
{
33-
case 0:
34-
_current = new Models.Stash() { SHA = line };
35-
outs.Add(_current);
36-
break;
37-
case 1:
38-
ParseParent(line);
39-
break;
40-
case 2:
41-
_current.Time = ulong.Parse(line);
42-
break;
43-
case 3:
44-
_current.Name = line;
45-
break;
46-
default:
47-
var boundary = rs.StdOut.IndexOf(_boundary, end + 1, StringComparison.Ordinal);
48-
if (boundary > end)
49-
{
50-
_current.Message = rs.StdOut.Substring(start, boundary - start - 1);
51-
end = boundary + _boundary.Length;
52-
}
53-
else
54-
{
55-
_current.Message = rs.StdOut.Substring(start);
56-
end = rs.StdOut.Length - 2;
57-
}
32+
var line = item.Substring(start, end - start);
33+
34+
switch (nextPartIdx)
35+
{
36+
case 0:
37+
current.SHA = line;
38+
break;
39+
case 1:
40+
ParseParent(line, ref current);
41+
break;
42+
case 2:
43+
current.Time = ulong.Parse(line);
44+
break;
45+
case 3:
46+
current.Name = line;
47+
break;
48+
}
5849

59-
nextPartIdx = -1;
50+
nextPartIdx++;
51+
52+
start = end + 1;
53+
if (start >= item.Length - 1)
6054
break;
61-
}
6255

63-
nextPartIdx++;
56+
end = item.IndexOf('\n', start);
57+
}
6458

65-
start = end + 1;
66-
if (start >= rs.StdOut.Length - 1)
67-
break;
59+
if (start < item.Length)
60+
current.Message = item.Substring(start);
6861

69-
end = rs.StdOut.IndexOf('\n', start);
62+
outs.Add(current);
7063
}
71-
7264
return outs;
7365
}
7466

75-
private void ParseParent(string data)
67+
private void ParseParent(string data, ref Models.Stash current)
7668
{
7769
if (data.Length < 8)
7870
return;
7971

80-
_current.Parents.AddRange(data.Split(separator: ' ', options: StringSplitOptions.RemoveEmptyEntries));
72+
current.Parents.AddRange(data.Split(separator: ' ', options: StringSplitOptions.RemoveEmptyEntries));
8173
}
82-
83-
private Models.Stash _current = null;
84-
private readonly string _boundary;
8574
}
8675
}

src/Resources/Locales/en_US.axaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -697,6 +697,7 @@
697697
<x:String x:Key="Text.Stash.TipForSelectedFiles" xml:space="preserve">Both staged and unstaged changes of selected file(s) will be stashed!!!</x:String>
698698
<x:String x:Key="Text.Stash.Title" xml:space="preserve">Stash Local Changes</x:String>
699699
<x:String x:Key="Text.StashCM.Apply" xml:space="preserve">Apply</x:String>
700+
<x:String x:Key="Text.StashCM.CopyMessage" xml:space="preserve">Copy Message</x:String>
700701
<x:String x:Key="Text.StashCM.Drop" xml:space="preserve">Drop</x:String>
701702
<x:String x:Key="Text.StashCM.SaveAsPatch" xml:space="preserve">Save as Patch...</x:String>
702703
<x:String x:Key="Text.StashDropConfirm" xml:space="preserve">Drop Stash</x:String>

src/ViewModels/StashesPage.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,11 +194,21 @@ public ContextMenu MakeContextMenu(Models.Stash stash)
194194
e.Handled = true;
195195
};
196196

197+
var copy = new MenuItem();
198+
copy.Header = App.Text("StashCM.CopyMessage");
199+
copy.Icon = App.CreateMenuIcon("Icons.Copy");
200+
copy.Click += (_, ev) =>
201+
{
202+
App.CopyText(stash.Message);
203+
ev.Handled = true;
204+
};
205+
197206
var menu = new ContextMenu();
198207
menu.Items.Add(apply);
199208
menu.Items.Add(drop);
200209
menu.Items.Add(new MenuItem { Header = "-" });
201210
menu.Items.Add(patch);
211+
menu.Items.Add(copy);
202212
return menu;
203213
}
204214

0 commit comments

Comments
 (0)