Skip to content

Commit b9b5220

Browse files
committed
feature: bare repository support
1 parent cc5f3eb commit b9b5220

File tree

6 files changed

+88
-47
lines changed

6 files changed

+88
-47
lines changed

src/ViewModels/CreateBranch.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@ public bool CheckoutAfterCreated
3131
set => _repo.Settings.CheckoutBranchOnCreateBranch = value;
3232
}
3333

34+
public bool IsBareRepository
35+
{
36+
get => _repo.IsBare;
37+
}
38+
3439
public CreateBranch(Repository repo, Models.Branch branch)
3540
{
3641
_repo = repo;
@@ -84,7 +89,7 @@ public override Task<bool> Sure()
8489
return Task.Run(() =>
8590
{
8691
var succ = false;
87-
if (CheckoutAfterCreated)
92+
if (CheckoutAfterCreated && !_repo.IsBare)
8893
{
8994
var changes = new Commands.CountLocalChangesWithoutUntracked(_repo.FullPath).Result();
9095
var needPopStash = false;

src/ViewModels/Launcher.cs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -280,19 +280,20 @@ public void OpenRepositoryInTab(RepositoryNode node, LauncherPage page)
280280
return;
281281
}
282282

283-
var gitDir = new Commands.QueryGitDir(node.Id).Result();
284-
if (string.IsNullOrEmpty(gitDir))
283+
var isBare = new Commands.IsBareRepository(node.Id).Result();
284+
var gitDir = node.Id;
285+
if (!isBare)
285286
{
286-
var ctx = page == null ? ActivePage.Node.Id : page.Node.Id;
287-
App.RaiseException(ctx, "Given path is not a valid git repository!");
288-
return;
287+
gitDir = new Commands.QueryGitDir(node.Id).Result();
288+
if (string.IsNullOrEmpty(gitDir))
289+
{
290+
var ctx = page == null ? ActivePage.Node.Id : page.Node.Id;
291+
App.RaiseException(ctx, "Given path is not a valid git repository!");
292+
return;
293+
}
289294
}
290295

291-
var repo = new Repository()
292-
{
293-
FullPath = node.Id,
294-
GitDir = gitDir,
295-
};
296+
var repo = new Repository(isBare, node.Id, gitDir);
296297
repo.Open();
297298

298299
if (page == null)

src/ViewModels/Repository.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@ namespace SourceGit.ViewModels
1818
{
1919
public class Repository : ObservableObject, Models.IRepository
2020
{
21+
public bool IsBare
22+
{
23+
get;
24+
}
25+
2126
public string FullPath
2227
{
2328
get => _fullpath;
@@ -448,6 +453,13 @@ public bool IsAutoFetching
448453
private set => SetProperty(ref _isAutoFetching, value);
449454
}
450455

456+
public Repository(bool isBare, string path, string gitDir)
457+
{
458+
IsBare = isBare;
459+
FullPath = path;
460+
GitDir = gitDir;
461+
}
462+
451463
public void Open()
452464
{
453465
var settingsFile = Path.Combine(_gitDir, "sourcegit.settings");
@@ -995,6 +1007,9 @@ public void RefreshSubmodules()
9951007

9961008
public void RefreshWorkingCopyChanges()
9971009
{
1010+
if (IsBare)
1011+
return;
1012+
9981013
var changes = new Commands.QueryLocalChanges(_fullpath, _settings.IncludeUntrackedInLocalChanges).Result();
9991014
if (_workingCopy == null)
10001015
return;
@@ -1010,6 +1025,9 @@ public void RefreshWorkingCopyChanges()
10101025

10111026
public void RefreshStashes()
10121027
{
1028+
if (IsBare)
1029+
return;
1030+
10131031
var stashes = new Commands.QueryStashes(_fullpath).Result();
10141032
Dispatcher.UIThread.Invoke(() =>
10151033
{

src/ViewModels/ScanRepositories.cs

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public override Task<bool> Sure()
3434
watch.Start();
3535

3636
var rootDir = new DirectoryInfo(RootDir);
37-
var founded = new List<string>();
37+
var founded = new List<FoundRepository>();
3838
GetUnmanagedRepositories(rootDir, founded, new EnumerationOptions()
3939
{
4040
AttributesToSkip = FileAttributes.Hidden | FileAttributes.System,
@@ -47,16 +47,16 @@ public override Task<bool> Sure()
4747

4848
foreach (var f in founded)
4949
{
50-
var parent = new DirectoryInfo(f).Parent!.FullName.Replace("\\", "/");
50+
var parent = new DirectoryInfo(f.Path).Parent!.FullName.Replace("\\", "/");
5151
if (parent.Equals(normalizedRoot, StringComparison.Ordinal))
5252
{
53-
Preferences.Instance.FindOrAddNodeByRepositoryPath(f, null, false);
53+
Preferences.Instance.FindOrAddNodeByRepositoryPath(f.Path, null, false);
5454
}
5555
else if (parent.StartsWith(normalizedRoot, StringComparison.Ordinal))
5656
{
5757
var relative = parent.Substring(normalizedRoot.Length).TrimStart('/');
5858
var group = FindOrCreateGroupRecursive(Preferences.Instance.RepositoryNodes, relative);
59-
Preferences.Instance.FindOrAddNodeByRepositoryPath(f, group, false);
59+
Preferences.Instance.FindOrAddNodeByRepositoryPath(f.Path, group, false);
6060
}
6161
}
6262

@@ -85,7 +85,7 @@ private void GetManagedRepositories(List<RepositoryNode> group, HashSet<string>
8585
}
8686
}
8787

88-
private void GetUnmanagedRepositories(DirectoryInfo dir, List<string> outs, EnumerationOptions opts, int depth = 0)
88+
private void GetUnmanagedRepositories(DirectoryInfo dir, List<FoundRepository> outs, EnumerationOptions opts, int depth = 0)
8989
{
9090
var subdirs = dir.GetDirectories("*", opts);
9191
foreach (var subdir in subdirs)
@@ -111,12 +111,19 @@ private void GetUnmanagedRepositories(DirectoryInfo dir, List<string> outs, Enum
111111
{
112112
var normalized = test.StdOut.Trim().Replace("\\", "/");
113113
if (!_managed.Contains(normalized))
114-
outs.Add(normalized);
114+
outs.Add(new FoundRepository(normalized, false));
115115
}
116116

117117
continue;
118118
}
119119

120+
var isBare = new Commands.IsBareRepository(subdir.FullName).Result();
121+
if (isBare)
122+
{
123+
outs.Add(new FoundRepository(normalizedSelf, true));
124+
continue;
125+
}
126+
120127
if (depth < 5)
121128
GetUnmanagedRepositories(subdir, outs, opts, depth + 1);
122129
}
@@ -161,6 +168,12 @@ private RepositoryNode FindOrCreateGroup(List<RepositoryNode> collection, string
161168
return added;
162169
}
163170

171+
private record FoundRepository(string path, bool isBare)
172+
{
173+
public string Path { get; set; } = path;
174+
public bool IsBare { get; set; } = isBare;
175+
}
176+
164177
private HashSet<string> _managed = new HashSet<string>();
165178
}
166179
}

src/ViewModels/Welcome.cs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -94,20 +94,20 @@ public void OpenOrInitRepository(string path, RepositoryNode parent, bool bMoveE
9494
}
9595

9696
var isBare = new Commands.IsBareRepository(path).Result();
97-
if (isBare)
97+
var repoRoot = path;
98+
if (!isBare)
9899
{
99-
App.RaiseException(string.Empty, $"'{path}' is a bare repository, which is not supported by SourceGit!");
100-
return;
101-
}
100+
var test = new Commands.QueryRepositoryRootPath(path).ReadToEnd();
101+
if (!test.IsSuccess || string.IsNullOrEmpty(test.StdOut))
102+
{
103+
InitRepository(path, parent, test.StdErr);
104+
return;
105+
}
102106

103-
var test = new Commands.QueryRepositoryRootPath(path).ReadToEnd();
104-
if (!test.IsSuccess || string.IsNullOrEmpty(test.StdOut))
105-
{
106-
InitRepository(path, parent, test.StdErr);
107-
return;
107+
repoRoot = test.StdOut.Trim();
108108
}
109109

110-
var node = Preferences.Instance.FindOrAddNodeByRepositoryPath(test.StdOut.Trim(), parent, bMoveExistedNode);
110+
var node = Preferences.Instance.FindOrAddNodeByRepositoryPath(repoRoot, parent, bMoveExistedNode);
111111
Refresh();
112112

113113
var launcher = App.GetLauncer();

src/Views/CreateBranch.axaml

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818
<Grid.RowDefinitions>
1919
<RowDefinition Height="32"/>
2020
<RowDefinition Height="32"/>
21-
<RowDefinition Height="Auto" MinHeight="32"/>
22-
<RowDefinition Height="32"/>
21+
<RowDefinition Height="Auto"/>
22+
<RowDefinition Height="Auto"/>
2323
</Grid.RowDefinitions>
2424

2525
<TextBlock Grid.Row="0" Grid.Column="0"
@@ -67,27 +67,31 @@
6767
<TextBlock Grid.Row="2" Grid.Column="0"
6868
HorizontalAlignment="Right" VerticalAlignment="Center"
6969
Margin="0,0,8,0"
70-
Text="{DynamicResource Text.CreateBranch.LocalChanges}"/>
71-
<WrapPanel Grid.Row="2" Grid.Column="1" Orientation="Horizontal" VerticalAlignment="Center">
72-
<RadioButton Content="{DynamicResource Text.CreateBranch.LocalChanges.DoNothing}"
73-
x:Name="RadioDoNothing"
74-
GroupName="LocalChanges"
75-
Margin="0,0,8,0"
76-
IsCheckedChanged="OnLocalChangeActionIsCheckedChanged"/>
77-
<RadioButton Content="{DynamicResource Text.CreateBranch.LocalChanges.StashAndReply}"
78-
x:Name="RadioStashAndReply"
79-
GroupName="LocalChanges"
80-
Margin="0,0,8,0"
81-
IsCheckedChanged="OnLocalChangeActionIsCheckedChanged"/>
82-
<RadioButton Content="{DynamicResource Text.CreateBranch.LocalChanges.Discard}"
83-
x:Name="RadioDiscard"
84-
GroupName="LocalChanges"
85-
IsCheckedChanged="OnLocalChangeActionIsCheckedChanged"/>
86-
</WrapPanel>
70+
Text="{DynamicResource Text.CreateBranch.LocalChanges}"
71+
IsVisible="{Binding !IsBareRepository}"/>
72+
<Border Grid.Row="2" Grid.Column="1" MinHeight="32" IsVisible="{Binding !IsBareRepository}">
73+
<WrapPanel Grid.Row="2" Grid.Column="1" Orientation="Horizontal" VerticalAlignment="Center">
74+
<RadioButton Content="{DynamicResource Text.CreateBranch.LocalChanges.DoNothing}"
75+
x:Name="RadioDoNothing"
76+
GroupName="LocalChanges"
77+
Margin="0,0,8,0"
78+
IsCheckedChanged="OnLocalChangeActionIsCheckedChanged"/>
79+
<RadioButton Content="{DynamicResource Text.CreateBranch.LocalChanges.StashAndReply}"
80+
x:Name="RadioStashAndReply"
81+
GroupName="LocalChanges"
82+
Margin="0,0,8,0"
83+
IsCheckedChanged="OnLocalChangeActionIsCheckedChanged"/>
84+
<RadioButton Content="{DynamicResource Text.CreateBranch.LocalChanges.Discard}"
85+
x:Name="RadioDiscard"
86+
GroupName="LocalChanges"
87+
IsCheckedChanged="OnLocalChangeActionIsCheckedChanged"/>
88+
</WrapPanel>
89+
</Border>
8790

8891
<CheckBox Grid.Row="3" Grid.Column="1"
8992
Content="{DynamicResource Text.CreateBranch.Checkout}"
90-
IsChecked="{Binding CheckoutAfterCreated, Mode=TwoWay}"/>
93+
IsChecked="{Binding CheckoutAfterCreated, Mode=TwoWay}"
94+
IsVisible="{Binding !IsBareRepository}"/>
9195
</Grid>
9296
</StackPanel>
9397
</UserControl>

0 commit comments

Comments
 (0)