Skip to content

Commit e00bc4e

Browse files
authored
Merge pull request #147 from filipeRmlh/feature/allowing_to_checkout_commit
Feature/allowing to checkout commit
2 parents dc63e39 + db9ca5b commit e00bc4e

15 files changed

+225
-9
lines changed

src/Commands/Checkout.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,14 @@ public bool FileWithRevision(string file, string revision)
6262
return Exec();
6363
}
6464

65+
public bool Commit(string commitId, Action<string> onProgress)
66+
{
67+
Args = $"checkout --detach --progress {commitId}";
68+
TraitErrorAsOutput = true;
69+
_outputHandler = onProgress;
70+
return Exec();
71+
}
72+
6573
public bool Files(List<string> files)
6674
{
6775
StringBuilder builder = new StringBuilder();

src/Commands/QueryBranches.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,12 @@ protected override void OnReadline(string line)
5151
var refName = parts[0];
5252
if (refName.EndsWith("/HEAD", StringComparison.Ordinal))
5353
return;
54-
54+
55+
if (refName.StartsWith("(HEAD detached at"))
56+
{
57+
branch.isHead = true;
58+
}
59+
5560
if (refName.StartsWith(PREFIX_LOCAL, StringComparison.Ordinal))
5661
{
5762
branch.Name = refName.Substring(PREFIX_LOCAL.Length);

src/Commands/QueryCommits.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,15 @@ private bool ParseDecorators(List<Models.Decorator> decorators, string data)
146146
Name = d.Substring(19).Trim(),
147147
});
148148
}
149+
else if (d.Equals("HEAD"))
150+
{
151+
isHeadOfCurrent = true;
152+
decorators.Add(new Models.Decorator()
153+
{
154+
Type = Models.DecoratorType.CurrentCommitHead,
155+
Name = d.Trim(),
156+
});
157+
}
149158
else if (d.StartsWith("refs/heads/", StringComparison.Ordinal))
150159
{
151160
decorators.Add(new Models.Decorator()

src/Converters/DecoratorTypeConverters.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ public static class DecoratorTypeConverters
3838
});
3939

4040
public static readonly FuncValueConverter<Models.DecoratorType, FontWeight> ToFontWeight =
41-
new FuncValueConverter<Models.DecoratorType, FontWeight>(v => v == Models.DecoratorType.CurrentBranchHead ? FontWeight.Bold : FontWeight.Regular);
41+
new FuncValueConverter<Models.DecoratorType, FontWeight>(v =>
42+
v is Models.DecoratorType.CurrentBranchHead or Models.DecoratorType.CurrentCommitHead
43+
? FontWeight.Bold : FontWeight.Regular
44+
);
4245
}
4346
}

src/Models/Branch.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,6 @@ public class Branch
1010
public string Upstream { get; set; }
1111
public string UpstreamTrackStatus { get; set; }
1212
public string Remote { get; set; }
13+
public bool isHead { get; set; }
1314
}
1415
}

src/Models/Commit.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public bool IsCommitterVisible
3232

3333
public bool IsCurrentHead
3434
{
35-
get => Decorators.Find(x => x.Type == DecoratorType.CurrentBranchHead) != null;
35+
get => Decorators.Find(x => x.Type is DecoratorType.CurrentBranchHead or DecoratorType.CurrentCommitHead) != null;
3636
}
3737

3838
public string FullMessage

src/Models/Decorator.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ public enum DecoratorType
77
None,
88
CurrentBranchHead,
99
LocalBranchHead,
10+
CurrentCommitHead,
1011
RemoteBranchHead,
1112
Tag,
1213
}

src/Resources/Locales/en_US.axaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,11 @@
5454
<x:String x:Key="Text.ChangeDisplayMode.List" xml:space="preserve">Show as List</x:String>
5555
<x:String x:Key="Text.ChangeDisplayMode.Tree" xml:space="preserve">Show as Tree</x:String>
5656
<x:String x:Key="Text.Checkout" xml:space="preserve">Checkout Branch</x:String>
57+
<x:String x:Key="Text.Checkout.Commit" xml:space="preserve">Checkout Commit</x:String>
58+
<x:String x:Key="Text.Checkout.Commit.Warning" xml:space="preserve">Warning: By doing a commit checkout, your Head will be detached</x:String>
5759
<x:String x:Key="Text.Checkout.Target" xml:space="preserve">Branch :</x:String>
60+
<x:String x:Key="Text.Checkout.CommitTarget.Sha" xml:space="preserve">Commit SHA :</x:String>
61+
<x:String x:Key="Text.Checkout.CommitTarget.ShortSha" xml:space="preserve">Commit Short SHA :</x:String>
5862
<x:String x:Key="Text.Checkout.LocalChanges" xml:space="preserve">Local Changes :</x:String>
5963
<x:String x:Key="Text.Checkout.LocalChanges.StashAndReply" xml:space="preserve">Stash &amp; Reapply</x:String>
6064
<x:String x:Key="Text.Checkout.LocalChanges.Discard" xml:space="preserve">Discard</x:String>
@@ -74,6 +78,7 @@
7478
<x:String x:Key="Text.Close" xml:space="preserve">CLOSE</x:String>
7579
<x:String x:Key="Text.CommitCM.CherryPick" xml:space="preserve">Cherry-Pick This Commit</x:String>
7680
<x:String x:Key="Text.CommitCM.CopySHA" xml:space="preserve">Copy SHA</x:String>
81+
<x:String x:Key="Text.CommitCM.Checkout" xml:space="preserve">Checkout commit${0}</x:String>
7782
<x:String x:Key="Text.CommitCM.Rebase" xml:space="preserve">Rebase${0}$to Here</x:String>
7883
<x:String x:Key="Text.CommitCM.Reset" xml:space="preserve">Reset${0}$to Here</x:String>
7984
<x:String x:Key="Text.CommitCM.Revert" xml:space="preserve">Revert Commit</x:String>

src/SourceGit.csproj

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,11 @@
4444
<TrimmerRootAssembly Include="SourceGit" />
4545
<TrimmerRootAssembly Include="Avalonia.Themes.Fluent" />
4646
</ItemGroup>
47+
48+
<ItemGroup>
49+
<Compile Update="Views\CheckoutCommit.axaml.cs">
50+
<DependentUpon>CheckoutCommit.axaml</DependentUpon>
51+
<SubType>Code</SubType>
52+
</Compile>
53+
</ItemGroup>
4754
</Project>

src/ViewModels/BranchTreeNode.cs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ namespace SourceGit.ViewModels
1111
{
1212
public enum BranchTreeNodeType
1313
{
14+
DetachedHead,
1415
Remote,
1516
Folder,
1617
Branch,
@@ -51,6 +52,11 @@ public bool IsBranch
5152
{
5253
get => Type == BranchTreeNodeType.Branch;
5354
}
55+
56+
public bool IsDetachedHead
57+
{
58+
get => Type == BranchTreeNodeType.DetachedHead;
59+
}
5460

5561
public bool IsCurrent
5662
{
@@ -213,11 +219,11 @@ private void MakeBranchNode(Models.Branch branch, List<BranchTreeNode> roots, Di
213219
start = sepIdx + 1;
214220
sepIdx = branch.Name.IndexOf('/', start);
215221
}
216-
222+
217223
lastFolder.Children.Add(new BranchTreeNode()
218224
{
219225
Name = Path.GetFileName(branch.Name),
220-
Type = BranchTreeNodeType.Branch,
226+
Type = branch.isHead ? BranchTreeNodeType.DetachedHead : BranchTreeNodeType.Branch,
221227
Backend = branch,
222228
IsExpanded = false,
223229
IsFiltered = isFiltered,
@@ -228,14 +234,16 @@ private void SortNodes(List<BranchTreeNode> nodes)
228234
{
229235
nodes.Sort((l, r) =>
230236
{
231-
if (l.Type == r.Type)
237+
if (l.Type == BranchTreeNodeType.DetachedHead)
232238
{
233-
return l.Name.CompareTo(r.Name);
239+
return -1;
234240
}
235-
else
241+
if (l.Type == r.Type)
236242
{
237-
return (int)l.Type - (int)r.Type;
243+
return l.Name.CompareTo(r.Name);
238244
}
245+
246+
return (int)l.Type - (int)r.Type;
239247
});
240248

241249
foreach (var node in nodes)

0 commit comments

Comments
 (0)