Skip to content

Commit baf85af

Browse files
committed
fix: parsing copied change (#2174)
Signed-off-by: leo <longshuang@msn.cn>
1 parent 75a82fa commit baf85af

File tree

5 files changed

+31
-28
lines changed

5 files changed

+31
-28
lines changed

src/Commands/CompareRevisions.cs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ namespace SourceGit.Commands
77
{
88
public partial class CompareRevisions : Command
99
{
10-
[GeneratedRegex(@"^([MADC])\s+(.+)$")]
10+
[GeneratedRegex(@"^([MAD])\s+(.+)$")]
1111
private static partial Regex REG_FORMAT();
12-
[GeneratedRegex(@"^R[0-9]{0,4}\s+(.+)$")]
12+
[GeneratedRegex(@"^([CR])[0-9]{0,4}\s+(.+)$")]
1313
private static partial Regex REG_RENAME_FORMAT();
1414

1515
public CompareRevisions(string repo, string start, string end)
@@ -47,8 +47,9 @@ public CompareRevisions(string repo, string start, string end, string path)
4747
match = REG_RENAME_FORMAT().Match(line);
4848
if (match.Success)
4949
{
50-
var renamed = new Models.Change() { Path = match.Groups[1].Value };
51-
renamed.Set(Models.ChangeState.Renamed);
50+
var type = match.Groups[1].Value;
51+
var renamed = new Models.Change() { Path = match.Groups[2].Value };
52+
renamed.Set(type == "R" ? Models.ChangeState.Renamed : Models.ChangeState.Copied);
5253
changes.Add(renamed);
5354
}
5455

@@ -72,10 +73,6 @@ public CompareRevisions(string repo, string start, string end, string path)
7273
change.Set(Models.ChangeState.Deleted);
7374
changes.Add(change);
7475
break;
75-
case 'C':
76-
change.Set(Models.ChangeState.Copied);
77-
changes.Add(change);
78-
break;
7976
}
8077
}
8178

src/Commands/QueryFileHistory.cs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ namespace SourceGit.Commands
88
{
99
public partial class QueryFileHistory : Command
1010
{
11-
[GeneratedRegex(@"^([MADC])\s+(.+)$")]
11+
[GeneratedRegex(@"^([MAD])\s+(.+)$")]
1212
private static partial Regex REG_FORMAT();
13-
[GeneratedRegex(@"^R[0-9]{0,4}\s+(.+)$")]
13+
[GeneratedRegex(@"^([CR])[0-9]{0,4}\s+(.+)$")]
1414
private static partial Regex REG_RENAME_FORMAT();
1515

1616
public QueryFileHistory(string repo, string path, string head)
@@ -20,7 +20,7 @@ public QueryFileHistory(string repo, string path, string head)
2020
RaiseError = false;
2121

2222
var builder = new StringBuilder();
23-
builder.Append("log --follow --no-show-signature --date-order -n 10000 --decorate=no --format=\"@%H%x00%P%x00%aN±%aE%x00%at%x00%s\" --name-status ");
23+
builder.Append("log --no-show-signature --date-order -n 10000 --decorate=no --format=\"@%H%x00%P%x00%aN±%aE%x00%at%x00%s\" --follow --name-status ");
2424
if (!string.IsNullOrEmpty(head))
2525
builder.Append(head).Append(" ");
2626
builder.Append("-- ").Append(path.Quoted());
@@ -64,8 +64,9 @@ public QueryFileHistory(string repo, string path, string head)
6464
match = REG_RENAME_FORMAT().Match(line);
6565
if (match.Success)
6666
{
67-
last.Change.Path = match.Groups[1].Value;
68-
last.Change.Set(Models.ChangeState.Renamed);
67+
var type = match.Groups[1].Value;
68+
last.Change.Path = match.Groups[2].Value;
69+
last.Change.Set(type == "R" ? Models.ChangeState.Renamed : Models.ChangeState.Copied);
6970
}
7071

7172
continue;
@@ -85,9 +86,6 @@ public QueryFileHistory(string repo, string path, string head)
8586
case 'D':
8687
last.Change.Set(Models.ChangeState.Deleted);
8788
break;
88-
case 'C':
89-
last.Change.Set(Models.ChangeState.Copied);
90-
break;
9189
}
9290
}
9391
}

src/Commands/QueryStagedChangesWithAmend.cs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ namespace SourceGit.Commands
66
{
77
public partial class QueryStagedChangesWithAmend : Command
88
{
9-
[GeneratedRegex(@"^:[\d]{6} ([\d]{6}) ([0-9a-f]{40}) [0-9a-f]{40} ([ACDMT])\d{0,6}\t(.*)$")]
9+
[GeneratedRegex(@"^:[\d]{6} ([\d]{6}) ([0-9a-f]{40}) [0-9a-f]{40} ([ADMT])\d{0,6}\t(.*)$")]
1010
private static partial Regex REG_FORMAT1();
11-
[GeneratedRegex(@"^:[\d]{6} ([\d]{6}) ([0-9a-f]{40}) [0-9a-f]{40} R\d{0,6}\t(.*\t.*)$")]
11+
[GeneratedRegex(@"^:[\d]{6} ([\d]{6}) ([0-9a-f]{40}) [0-9a-f]{40} ([RC])\d{0,6}\t(.*\t.*)$")]
1212
private static partial Regex REG_FORMAT2();
1313

1414
public QueryStagedChangesWithAmend(string repo, string parent)
@@ -34,15 +34,16 @@ public QueryStagedChangesWithAmend(string repo, string parent)
3434
{
3535
var change = new Models.Change()
3636
{
37-
Path = match.Groups[3].Value,
37+
Path = match.Groups[4].Value,
3838
DataForAmend = new Models.ChangeDataForAmend()
3939
{
4040
FileMode = match.Groups[1].Value,
4141
ObjectHash = match.Groups[2].Value,
4242
ParentSHA = _parent,
4343
},
4444
};
45-
change.Set(Models.ChangeState.Renamed);
45+
var type = match.Groups[3].Value;
46+
change.Set(type == "R" ? Models.ChangeState.Renamed : Models.ChangeState.Copied);
4647
changes.Add(change);
4748
continue;
4849
}
@@ -67,9 +68,6 @@ public QueryStagedChangesWithAmend(string repo, string parent)
6768
case "A":
6869
change.Set(Models.ChangeState.Added);
6970
break;
70-
case "C":
71-
change.Set(Models.ChangeState.Copied);
72-
break;
7371
case "D":
7472
change.Set(Models.ChangeState.Deleted);
7573
break;

src/Models/Change.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public void Set(ChangeState index, ChangeState workTree = ChangeState.None)
6060
Index = index;
6161
WorkTree = workTree;
6262

63-
if (index == ChangeState.Renamed || workTree == ChangeState.Renamed)
63+
if (index == ChangeState.Renamed || index == ChangeState.Copied || workTree == ChangeState.Renamed)
6464
{
6565
var parts = Path.Split('\t', 2);
6666
if (parts.Length < 2)

src/Models/DiffOption.cs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,10 +86,20 @@ public DiffOption(Commit commit, string file)
8686
/// <param name="ver"></param>
8787
public DiffOption(FileVersion ver)
8888
{
89-
_revisions.Add(ver.HasParent ? $"{ver.SHA}^" : Commit.EmptyTreeSHA1);
90-
_revisions.Add(ver.SHA);
91-
_path = ver.Path;
92-
_orgPath = ver.Change.OriginalPath;
89+
if (string.IsNullOrEmpty(ver.Change.OriginalPath))
90+
{
91+
_revisions.Add(ver.HasParent ? $"{ver.SHA}^" : Commit.EmptyTreeSHA1);
92+
_revisions.Add(ver.SHA);
93+
_path = ver.Path;
94+
}
95+
else
96+
{
97+
_revisions.Add($"{ver.SHA}^:{ver.Change.OriginalPath.Quoted()}");
98+
_revisions.Add($"{ver.SHA}:{ver.Path.Quoted()}");
99+
_path = ver.Path;
100+
_orgPath = ver.Change.OriginalPath;
101+
_ignorePaths = true;
102+
}
93103
}
94104

95105
/// <summary>

0 commit comments

Comments
 (0)