Skip to content

Commit 1a3a366

Browse files
committed
feature: supports git SHA-256 object hash
Signed-off-by: leo <longshuang@msn.cn>
1 parent 130e4f7 commit 1a3a366

File tree

13 files changed

+45
-30
lines changed

13 files changed

+45
-30
lines changed

src/App.axaml.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -806,7 +806,7 @@ private string FixFontFamilyName(string input)
806806
return trimmed.Count > 0 ? string.Join(',', trimmed) : string.Empty;
807807
}
808808

809-
[GeneratedRegex(@"^[a-z]+\s+([a-fA-F0-9]{4,40})(\s+.*)?$")]
809+
[GeneratedRegex(@"^[a-z]+\s+([a-fA-F0-9]{4,64})(\s+.*)?$")]
810810
private static partial Regex REG_REBASE_TODO();
811811

812812
private Models.IpcChannel _ipcChannel = null;

src/Commands/Diff.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public partial class Diff : Command
1212
[GeneratedRegex(@"^@@ \-(\d+),?\d* \+(\d+),?\d* @@")]
1313
private static partial Regex REG_INDICATOR();
1414

15-
[GeneratedRegex(@"^index\s([0-9a-f]{6,40})\.\.([0-9a-f]{6,40})(\s[1-9]{6})?")]
15+
[GeneratedRegex(@"^index\s([0-9a-f]{6,64})\.\.([0-9a-f]{6,64})(\s[1-9]{6})?")]
1616
private static partial Regex REG_HASH_CHANGE();
1717

1818
private const string PREFIX_LFS_NEW = "+version https://git-lfs.github.com/spec/";

src/Commands/IsBinary.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ public partial class IsBinary : Command
88
[GeneratedRegex(@"^\-\s+\-\s+.*$")]
99
private static partial Regex REG_TEST();
1010

11-
public IsBinary(string repo, string commit, string path)
11+
public IsBinary(string repo, string revision, string path)
1212
{
1313
WorkingDirectory = repo;
1414
Context = repo;
15-
Args = $"diff --no-color --no-ext-diff --numstat {Models.Commit.EmptyTreeSHA1} {commit} -- {path.Quoted()}";
15+
Args = $"diff --no-color --no-ext-diff --numstat {Models.EmptyTreeHash.Guess(revision)} {revision} -- {path.Quoted()}";
1616
RaiseError = false;
1717
}
1818

src/Commands/QueryCommitChildren.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public async Task<List<string>> GetResultAsync()
2424
foreach (var line in lines)
2525
{
2626
if (line.Contains(_commit))
27-
outs.Add(line.Substring(0, 40));
27+
outs.Add(line.Substring(0, _commit.Length));
2828
}
2929
}
3030

src/Commands/QueryStagedChangesWithAmend.cs

Lines changed: 2 additions & 2 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} ([ADMT])\d{0,6}\t(.*)$")]
9+
[GeneratedRegex(@"^:[\d]{6} ([\d]{6}) ([0-9a-f]{4,64}) [0-9a-f]{4,64} ([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} ([RC])\d{0,6}\t(.*\t.*)$")]
11+
[GeneratedRegex(@"^:[\d]{6} ([\d]{6}) ([0-9a-f]{4,64}) [0-9a-f]{4,64} ([RC])\d{0,6}\t(.*\t.*)$")]
1212
private static partial Regex REG_FORMAT2();
1313

1414
public QueryStagedChangesWithAmend(string repo, string parent)

src/Models/Commit.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@ public enum CommitSearchMethod
1515

1616
public class Commit
1717
{
18-
public const string EmptyTreeSHA1 = "4b825dc642cb6eb9a060e54bf8d69288fbee4904";
19-
2018
public string SHA { get; set; } = string.Empty;
2119
public User Author { get; set; } = User.Invalid;
2220
public ulong AuthorTime { get; set; } = 0;
@@ -33,6 +31,7 @@ public class Commit
3331
public bool IsCommitterVisible => !Author.Equals(Committer) || AuthorTime != CommitterTime;
3432
public bool IsCurrentHead => Decorators.Find(x => x.Type is DecoratorType.CurrentBranchHead or DecoratorType.CurrentCommitHead) != null;
3533
public bool HasDecorators => Decorators.Count > 0;
34+
public string FirstParentToCompare => Parents.Count > 0 ? $"{SHA}^" : EmptyTreeHash.Guess(SHA);
3635

3736
public string GetFriendlyName()
3837
{

src/Models/DiffOption.cs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,7 @@ public DiffOption(Change change, bool isUnstaged)
6060
/// <param name="change"></param>
6161
public DiffOption(Commit commit, Change change)
6262
{
63-
var baseRevision = commit.Parents.Count == 0 ? Commit.EmptyTreeSHA1 : $"{commit.SHA}^";
64-
_revisions.Add(baseRevision);
63+
_revisions.Add(commit.FirstParentToCompare);
6564
_revisions.Add(commit.SHA);
6665
_path = change.Path;
6766
_orgPath = change.OriginalPath;
@@ -74,8 +73,7 @@ public DiffOption(Commit commit, Change change)
7473
/// <param name="file"></param>
7574
public DiffOption(Commit commit, string file)
7675
{
77-
var baseRevision = commit.Parents.Count == 0 ? Commit.EmptyTreeSHA1 : $"{commit.SHA}^";
78-
_revisions.Add(baseRevision);
76+
_revisions.Add(commit.FirstParentToCompare);
7977
_revisions.Add(commit.SHA);
8078
_path = file;
8179
}
@@ -88,7 +86,7 @@ public DiffOption(FileVersion ver)
8886
{
8987
if (string.IsNullOrEmpty(ver.OriginalPath))
9088
{
91-
_revisions.Add(ver.HasParent ? $"{ver.SHA}^" : Commit.EmptyTreeSHA1);
89+
_revisions.Add(ver.HasParent ? $"{ver.SHA}^" : EmptyTreeHash.Guess(ver.SHA));
9290
_revisions.Add(ver.SHA);
9391
_path = ver.Path;
9492
}
@@ -111,14 +109,14 @@ public DiffOption(FileVersion start, FileVersion end)
111109
{
112110
if (start.Change.Index == ChangeState.Deleted)
113111
{
114-
_revisions.Add(Commit.EmptyTreeSHA1);
112+
_revisions.Add(EmptyTreeHash.Guess(end.SHA));
115113
_revisions.Add(end.SHA);
116114
_path = end.Path;
117115
}
118116
else if (end.Change.Index == ChangeState.Deleted)
119117
{
120118
_revisions.Add(start.SHA);
121-
_revisions.Add(Commit.EmptyTreeSHA1);
119+
_revisions.Add(EmptyTreeHash.Guess(start.SHA));
122120
_path = start.Path;
123121
}
124122
else if (!end.Path.Equals(start.Path, StringComparison.Ordinal))

src/Models/EmptyTreeHash.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
namespace SourceGit.Models
2+
{
3+
public static class EmptyTreeHash
4+
{
5+
public static string Guess(string revision)
6+
{
7+
return revision.Length == 40 ? SHA1 : SHA256;
8+
}
9+
10+
private const string SHA1 = "4b825dc642cb6eb9a060e54bf8d69288fbee4904";
11+
private const string SHA256 = "6ef19b41225c5369f1c104d45d8d85efa9b057b53b14b4b9b939dd74decc5321";
12+
}
13+
}

src/Models/Stash.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,6 @@ public class Stash
1010
public ulong Time { get; set; } = 0;
1111
public string Message { get; set; } = "";
1212
public string Subject => Message.Split('\n', 2)[0].Trim();
13+
public string UntrackedParent => EmptyTreeHash.Guess(SHA);
1314
}
1415
}

src/ViewModels/CommitDetail.cs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -240,8 +240,13 @@ public async Task SaveChangesAsPatchAsync(List<Models.Change> changes, string sa
240240
if (_commit == null)
241241
return;
242242

243-
var baseRevision = _commit.Parents.Count == 0 ? Models.Commit.EmptyTreeSHA1 : _commit.Parents[0];
244-
var succ = await Commands.SaveChangesAsPatch.ProcessRevisionCompareChangesAsync(_repo.FullPath, changes, baseRevision, _commit.SHA, saveTo);
243+
var succ = await Commands.SaveChangesAsPatch.ProcessRevisionCompareChangesAsync(
244+
_repo.FullPath,
245+
changes,
246+
_commit.FirstParentToCompare,
247+
_commit.SHA,
248+
saveTo);
249+
245250
if (succ)
246251
App.SendNotification(_repo.FullPath, App.Text("SaveAsPatchSuccess"));
247252
}
@@ -535,8 +540,7 @@ private void Refresh()
535540

536541
Task.Run(async () =>
537542
{
538-
var parent = _commit.Parents.Count == 0 ? Models.Commit.EmptyTreeSHA1 : $"{_commit.SHA}^";
539-
var cmd = new Commands.CompareRevisions(_repo.FullPath, parent, _commit.SHA) { CancellationToken = token };
543+
var cmd = new Commands.CompareRevisions(_repo.FullPath, _commit.FirstParentToCompare, _commit.SHA) { CancellationToken = token };
540544
var changes = await cmd.ReadAsync().ConfigureAwait(false);
541545
var visible = changes;
542546
if (!string.IsNullOrWhiteSpace(_searchChangeFilter))
@@ -757,7 +761,7 @@ private async Task SetViewingCommitAsync(Models.Object file)
757761
[GeneratedRegex(@"\b(https?://|ftp://)[\w\d\._/\-~%@()+:?&=#!]*[\w\d/]")]
758762
private static partial Regex REG_URL_FORMAT();
759763

760-
[GeneratedRegex(@"\b([0-9a-fA-F]{6,40})\b")]
764+
[GeneratedRegex(@"\b([0-9a-fA-F]{6,64})\b")]
761765
private static partial Regex REG_SHA_FORMAT();
762766

763767
[GeneratedRegex(@"`.*?`")]

0 commit comments

Comments
 (0)