Skip to content

Commit 5eaa71f

Browse files
committed
code_style: simplify code about conflicts
Signed-off-by: leo <longshuang@msn.cn>
1 parent 1652b05 commit 5eaa71f

2 files changed

Lines changed: 15 additions & 91 deletions

File tree

src/ViewModels/Conflict.cs

Lines changed: 14 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,8 @@
1-
using System;
1+
using System.IO;
22
using System.Threading.Tasks;
33

44
namespace SourceGit.ViewModels
55
{
6-
public class ConflictSourceBranch
7-
{
8-
public string Name { get; private set; }
9-
public string Head { get; private set; }
10-
public Models.Commit Revision { get; private set; }
11-
12-
public ConflictSourceBranch(string name, string head, Models.Commit revision)
13-
{
14-
Name = name;
15-
Head = head;
16-
Revision = revision;
17-
}
18-
19-
public ConflictSourceBranch(Repository repo, Models.Branch branch)
20-
{
21-
Name = branch.Name;
22-
Head = branch.Head;
23-
Revision = new Commands.QuerySingleCommit(repo.FullPath, branch.Head).GetResult() ?? new Models.Commit() { SHA = branch.Head };
24-
}
25-
}
26-
276
public class Conflict
287
{
298
public string Marker
@@ -60,52 +39,28 @@ public bool CanMerge
6039
private set;
6140
} = false;
6241

63-
public string FilePath
64-
{
65-
get => _change.Path;
66-
}
67-
6842
public Conflict(Repository repo, WorkingCopy wc, Models.Change change)
6943
{
7044
_repo = repo;
7145
_wc = wc;
7246
_change = change;
7347

74-
var isSubmodule = repo.Submodules.Find(x => x.Path.Equals(change.Path, StringComparison.Ordinal)) != null;
75-
if (!isSubmodule && (_change.ConflictReason is Models.ConflictReason.BothAdded or Models.ConflictReason.BothModified))
76-
{
77-
CanMerge = true;
48+
CanMerge = _change.ConflictReason is Models.ConflictReason.BothAdded or Models.ConflictReason.BothModified;
49+
if (CanMerge)
50+
CanMerge = !Directory.Exists(Path.Combine(repo.FullPath, change.Path)); // Cannot merge directories (submodules)
51+
52+
if (CanMerge)
7853
IsResolved = new Commands.IsConflictResolved(repo.FullPath, change).GetResult();
79-
}
8054

81-
switch (wc.InProgressContext)
55+
var head = new Commands.QuerySingleCommit(repo.FullPath, "HEAD").GetResult();
56+
(Mine, Theirs) = wc.InProgressContext switch
8257
{
83-
case CherryPickInProgress cherryPick:
84-
Theirs = cherryPick.Head;
85-
Mine = new ConflictSourceBranch(repo, repo.CurrentBranch);
86-
break;
87-
case RebaseInProgress rebase:
88-
var b = repo.Branches.Find(x => x.IsLocal && x.Name == rebase.HeadName);
89-
if (b != null)
90-
Theirs = new ConflictSourceBranch(b.Name, b.Head, rebase.StoppedAt);
91-
else
92-
Theirs = new ConflictSourceBranch(rebase.HeadName, rebase.StoppedAt?.SHA ?? "----------", rebase.StoppedAt);
93-
94-
Mine = rebase.Onto;
95-
break;
96-
case RevertInProgress revert:
97-
Theirs = revert.Head;
98-
Mine = new ConflictSourceBranch(repo, repo.CurrentBranch);
99-
break;
100-
case MergeInProgress merge:
101-
Theirs = merge.Source;
102-
Mine = new ConflictSourceBranch(repo, repo.CurrentBranch);
103-
break;
104-
default:
105-
Theirs = "Stash or Patch";
106-
Mine = new ConflictSourceBranch(repo, repo.CurrentBranch);
107-
break;
108-
}
58+
CherryPickInProgress cherryPick => (head, cherryPick.Head),
59+
RebaseInProgress rebase => (rebase.Onto, rebase.StoppedAt),
60+
RevertInProgress revert => (head, revert.Head),
61+
MergeInProgress merge => (head, merge.Source),
62+
_ => (head, (object)"Stash or Patch"),
63+
};
10964
}
11065

11166
public async Task UseTheirsAsync()

src/Views/Conflict.axaml

Lines changed: 1 addition & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -25,40 +25,9 @@
2525

2626
<Border Margin="16,0" Padding="8" CornerRadius="4" BorderThickness="1" BorderBrush="{DynamicResource Brush.Border2}">
2727
<Border.DataTemplates>
28-
<DataTemplate DataType="vm:ConflictSourceBranch">
29-
<StackPanel Orientation="Horizontal">
30-
<Path Width="12" Height="12" Data="{StaticResource Icons.Branch}"/>
31-
<TextBlock Margin="4,0,0,0" Text="{Binding Name}"/>
32-
<TextBlock Margin="4,0,0,0"
33-
Text="{Binding Head, Converter={x:Static c:StringConverters.ToShortSHA}}"
34-
Foreground="DarkOrange"
35-
TextDecorations="Underline"
36-
Cursor="Hand"
37-
PointerPressed="OnPressedSHA"
38-
ToolTip.Tip="{Binding Revision}"
39-
ToolTip.ShowDelay="0">
40-
<TextBlock.DataTemplates>
41-
<DataTemplate DataType="m:Commit">
42-
<StackPanel MinWidth="400" Orientation="Vertical">
43-
<Grid ColumnDefinitions="Auto,*,Auto">
44-
<v:Avatar Grid.Column="0" Width="16" Height="16" VerticalAlignment="Center" IsHitTestVisible="False" User="{Binding Author}"/>
45-
<TextBlock Grid.Column="1" Text="{Binding Author.Name}" Margin="8,0,0,0"/>
46-
<TextBlock Grid.Column="2" Text="{Binding CommitterTimeStr}" Foreground="{DynamicResource Brush.FG2}" Margin="8,0,0,0"/>
47-
</Grid>
48-
49-
<TextBlock Margin="0,8,0,0" Text="{Binding Subject}" TextWrapping="Wrap"/>
50-
</StackPanel>
51-
</DataTemplate>
52-
</TextBlock.DataTemplates>
53-
</TextBlock>
54-
</StackPanel>
55-
</DataTemplate>
56-
5728
<DataTemplate DataType="m:Commit">
5829
<StackPanel Orientation="Horizontal">
59-
<Path Width="12" Height="12" Data="{StaticResource Icons.Commit}"/>
60-
<v:CommitRefsPresenter Margin="8,0,0,0"
61-
Foreground="{DynamicResource Brush.FG1}"
30+
<v:CommitRefsPresenter Foreground="{DynamicResource Brush.FG1}"
6231
FontSize="12"
6332
VerticalAlignment="Center"
6433
UseGraphColor="False"/>

0 commit comments

Comments
 (0)