enhance: try to use friendly name instead of commit hash

This commit is contained in:
leo 2024-12-10 20:23:46 +08:00
parent 3b5d87391d
commit 2dd9cab695
No known key found for this signature in database
2 changed files with 41 additions and 27 deletions

View file

@ -62,6 +62,19 @@ namespace SourceGit.ViewModels
Args = $"{Cmd} --continue", Args = $"{Cmd} --continue",
}.Exec(); }.Exec();
} }
protected string GetFriendlyNameOfCommit(Models.Commit commit)
{
var branchDecorator = commit.Decorators.Find(x => x.Type == Models.DecoratorType.LocalBranchHead || x.Type == Models.DecoratorType.RemoteBranchHead);
if (branchDecorator != null)
return branchDecorator.Name;
var tagDecorator = commit.Decorators.Find(x => x.Type == Models.DecoratorType.Tag);
if (tagDecorator != null)
return tagDecorator.Name;
return commit.SHA.Substring(0, 10);
}
} }
public class CherryPickInProgress : InProgressContext public class CherryPickInProgress : InProgressContext
@ -72,6 +85,11 @@ namespace SourceGit.ViewModels
private set; private set;
} }
public string HeadName
{
get => GetFriendlyNameOfCommit(Head);
}
public CherryPickInProgress(Repository repo) : base(repo.FullPath, "cherry-pick", true) public CherryPickInProgress(Repository repo) : base(repo.FullPath, "cherry-pick", true)
{ {
var headSHA = File.ReadAllText(Path.Combine(repo.GitDir, "CHERRY_PICK_HEAD")).Trim(); var headSHA = File.ReadAllText(Path.Combine(repo.GitDir, "CHERRY_PICK_HEAD")).Trim();
@ -87,18 +105,32 @@ namespace SourceGit.ViewModels
private set; private set;
} }
public string BaseName
{
get => GetFriendlyNameOfCommit(Onto);
}
public Models.Commit StoppedAt public Models.Commit StoppedAt
{ {
get; get;
private set; private set;
} }
public Models.Commit Onto
{
get;
private set;
}
public RebaseInProgress(Repository repo) : base(repo.FullPath, "rebase", true) public RebaseInProgress(Repository repo) : base(repo.FullPath, "rebase", true)
{ {
_gitDir = repo.GitDir; _gitDir = repo.GitDir;
var stoppedSHA = File.ReadAllText(Path.Combine(repo.GitDir, "rebase-merge", "stopped-sha")).Trim(); var stoppedSHA = File.ReadAllText(Path.Combine(repo.GitDir, "rebase-merge", "stopped-sha")).Trim();
StoppedAt = new Commands.QuerySingleCommit(repo.FullPath, stoppedSHA).Result(); StoppedAt = new Commands.QuerySingleCommit(repo.FullPath, stoppedSHA).Result() ?? new Models.Commit() { SHA = stoppedSHA };
var ontoSHA = File.ReadAllText(Path.Combine(repo.GitDir, "rebase-merge", "onto")).Trim();
Onto = new Commands.QuerySingleCommit(repo.FullPath, ontoSHA).Result() ?? new Models.Commit() { SHA = ontoSHA };
HeadName = File.ReadAllText(Path.Combine(repo.GitDir, "rebase-merge", "head-name")).Trim(); HeadName = File.ReadAllText(Path.Combine(repo.GitDir, "rebase-merge", "head-name")).Trim();
if (HeadName.StartsWith("refs/heads/")) if (HeadName.StartsWith("refs/heads/"))
@ -150,16 +182,15 @@ namespace SourceGit.ViewModels
private set; private set;
} }
public string SourceName public Models.Commit Source
{ {
get; get;
private set; private set;
} }
public Models.Commit Source public string SourceName
{ {
get; get => GetFriendlyNameOfCommit(Source);
private set;
} }
public MergeInProgress(Repository repo) : base(repo.FullPath, "merge", false) public MergeInProgress(Repository repo) : base(repo.FullPath, "merge", false)
@ -167,24 +198,7 @@ namespace SourceGit.ViewModels
Current = Commands.Branch.ShowCurrent(repo.FullPath); Current = Commands.Branch.ShowCurrent(repo.FullPath);
var sourceSHA = File.ReadAllText(Path.Combine(repo.GitDir, "MERGE_HEAD")).Trim(); var sourceSHA = File.ReadAllText(Path.Combine(repo.GitDir, "MERGE_HEAD")).Trim();
Source = new Commands.QuerySingleCommit(repo.FullPath, sourceSHA).Result(); Source = new Commands.QuerySingleCommit(repo.FullPath, sourceSHA).Result() ?? new Models.Commit() { SHA = sourceSHA };
if (Source == null)
return;
var branchDecorator = Source.Decorators.Find(x => x.Type == Models.DecoratorType.LocalBranchHead || x.Type == Models.DecoratorType.RemoteBranchHead);
if (branchDecorator != null)
{
SourceName = branchDecorator.Name;
return;
}
var tagDecorator = Source.Decorators.Find(x => x.Type == Models.DecoratorType.Tag);
if (tagDecorator != null)
{
SourceName = tagDecorator.Name;
}
SourceName = Source.SHA.Substring(0, 10);
} }
} }
} }

View file

@ -572,11 +572,11 @@ namespace SourceGit.ViewModels
if (_inProgressContext is RebaseInProgress rebase) if (_inProgressContext is RebaseInProgress rebase)
{ {
useTheirs.Header = new Views.NameHighlightedTextBlock("FileCM.ResolveUsing", rebase.HeadName); useTheirs.Header = new Views.NameHighlightedTextBlock("FileCM.ResolveUsing", rebase.HeadName);
useMine.Header = new Views.NameHighlightedTextBlock("FileCM.ResolveUsing", rebase.StoppedAt.SHA.Substring(0, 10)); useMine.Header = new Views.NameHighlightedTextBlock("FileCM.ResolveUsing", rebase.BaseName);
} }
else if (_inProgressContext is CherryPickInProgress cherryPick) else if (_inProgressContext is CherryPickInProgress cherryPick)
{ {
useTheirs.Header = new Views.NameHighlightedTextBlock("FileCM.ResolveUsing", cherryPick.Head.SHA.Substring(0, 10)); useTheirs.Header = new Views.NameHighlightedTextBlock("FileCM.ResolveUsing", cherryPick.HeadName);
useMine.Header = new Views.NameHighlightedTextBlock("FileCM.ResolveUsing", _repo.CurrentBranch.Name); useMine.Header = new Views.NameHighlightedTextBlock("FileCM.ResolveUsing", _repo.CurrentBranch.Name);
} }
else if (_inProgressContext is MergeInProgress merge) else if (_inProgressContext is MergeInProgress merge)
@ -912,11 +912,11 @@ namespace SourceGit.ViewModels
if (_inProgressContext is RebaseInProgress rebase) if (_inProgressContext is RebaseInProgress rebase)
{ {
useTheirs.Header = new Views.NameHighlightedTextBlock("FileCM.ResolveUsing", rebase.HeadName); useTheirs.Header = new Views.NameHighlightedTextBlock("FileCM.ResolveUsing", rebase.HeadName);
useMine.Header = new Views.NameHighlightedTextBlock("FileCM.ResolveUsing", rebase.StoppedAt.SHA.Substring(0, 10)); useMine.Header = new Views.NameHighlightedTextBlock("FileCM.ResolveUsing", rebase.BaseName);
} }
else if (_inProgressContext is CherryPickInProgress cherryPick) else if (_inProgressContext is CherryPickInProgress cherryPick)
{ {
useTheirs.Header = new Views.NameHighlightedTextBlock("FileCM.ResolveUsing", cherryPick.Head.SHA.Substring(0, 10)); useTheirs.Header = new Views.NameHighlightedTextBlock("FileCM.ResolveUsing", cherryPick.HeadName);
useMine.Header = new Views.NameHighlightedTextBlock("FileCM.ResolveUsing", _repo.CurrentBranch.Name); useMine.Header = new Views.NameHighlightedTextBlock("FileCM.ResolveUsing", _repo.CurrentBranch.Name);
} }
else if (_inProgressContext is MergeInProgress merge) else if (_inProgressContext is MergeInProgress merge)