diff --git a/src/ViewModels/CreateBranch.cs b/src/ViewModels/CreateBranch.cs index 64806a49..b67a453a 100644 --- a/src/ViewModels/CreateBranch.cs +++ b/src/ViewModels/CreateBranch.cs @@ -31,6 +31,11 @@ namespace SourceGit.ViewModels set => _repo.Settings.CheckoutBranchOnCreateBranch = value; } + public bool IsBareRepository + { + get => _repo.IsBare; + } + public CreateBranch(Repository repo, Models.Branch branch) { _repo = repo; @@ -84,7 +89,7 @@ namespace SourceGit.ViewModels return Task.Run(() => { var succ = false; - if (CheckoutAfterCreated) + if (CheckoutAfterCreated && !_repo.IsBare) { var changes = new Commands.CountLocalChangesWithoutUntracked(_repo.FullPath).Result(); var needPopStash = false; diff --git a/src/ViewModels/Launcher.cs b/src/ViewModels/Launcher.cs index 57a1c404..6761eccf 100644 --- a/src/ViewModels/Launcher.cs +++ b/src/ViewModels/Launcher.cs @@ -280,19 +280,20 @@ namespace SourceGit.ViewModels return; } - var gitDir = new Commands.QueryGitDir(node.Id).Result(); - if (string.IsNullOrEmpty(gitDir)) + var isBare = new Commands.IsBareRepository(node.Id).Result(); + var gitDir = node.Id; + if (!isBare) { - var ctx = page == null ? ActivePage.Node.Id : page.Node.Id; - App.RaiseException(ctx, "Given path is not a valid git repository!"); - return; + gitDir = new Commands.QueryGitDir(node.Id).Result(); + if (string.IsNullOrEmpty(gitDir)) + { + var ctx = page == null ? ActivePage.Node.Id : page.Node.Id; + App.RaiseException(ctx, "Given path is not a valid git repository!"); + return; + } } - var repo = new Repository() - { - FullPath = node.Id, - GitDir = gitDir, - }; + var repo = new Repository(isBare, node.Id, gitDir); repo.Open(); if (page == null) diff --git a/src/ViewModels/Repository.cs b/src/ViewModels/Repository.cs index b747556c..6dfa67ca 100644 --- a/src/ViewModels/Repository.cs +++ b/src/ViewModels/Repository.cs @@ -18,6 +18,11 @@ namespace SourceGit.ViewModels { public class Repository : ObservableObject, Models.IRepository { + public bool IsBare + { + get; + } + public string FullPath { get => _fullpath; @@ -448,6 +453,13 @@ namespace SourceGit.ViewModels private set => SetProperty(ref _isAutoFetching, value); } + public Repository(bool isBare, string path, string gitDir) + { + IsBare = isBare; + FullPath = path; + GitDir = gitDir; + } + public void Open() { var settingsFile = Path.Combine(_gitDir, "sourcegit.settings"); @@ -995,6 +1007,9 @@ namespace SourceGit.ViewModels public void RefreshWorkingCopyChanges() { + if (IsBare) + return; + var changes = new Commands.QueryLocalChanges(_fullpath, _settings.IncludeUntrackedInLocalChanges).Result(); if (_workingCopy == null) return; @@ -1010,6 +1025,9 @@ namespace SourceGit.ViewModels public void RefreshStashes() { + if (IsBare) + return; + var stashes = new Commands.QueryStashes(_fullpath).Result(); Dispatcher.UIThread.Invoke(() => { diff --git a/src/ViewModels/ScanRepositories.cs b/src/ViewModels/ScanRepositories.cs index 5c023587..8d14e36e 100644 --- a/src/ViewModels/ScanRepositories.cs +++ b/src/ViewModels/ScanRepositories.cs @@ -34,7 +34,7 @@ namespace SourceGit.ViewModels watch.Start(); var rootDir = new DirectoryInfo(RootDir); - var founded = new List(); + var founded = new List(); GetUnmanagedRepositories(rootDir, founded, new EnumerationOptions() { AttributesToSkip = FileAttributes.Hidden | FileAttributes.System, @@ -47,16 +47,16 @@ namespace SourceGit.ViewModels foreach (var f in founded) { - var parent = new DirectoryInfo(f).Parent!.FullName.Replace("\\", "/"); + var parent = new DirectoryInfo(f.Path).Parent!.FullName.Replace("\\", "/"); if (parent.Equals(normalizedRoot, StringComparison.Ordinal)) { - Preferences.Instance.FindOrAddNodeByRepositoryPath(f, null, false); + Preferences.Instance.FindOrAddNodeByRepositoryPath(f.Path, null, false); } else if (parent.StartsWith(normalizedRoot, StringComparison.Ordinal)) { var relative = parent.Substring(normalizedRoot.Length).TrimStart('/'); var group = FindOrCreateGroupRecursive(Preferences.Instance.RepositoryNodes, relative); - Preferences.Instance.FindOrAddNodeByRepositoryPath(f, group, false); + Preferences.Instance.FindOrAddNodeByRepositoryPath(f.Path, group, false); } } @@ -85,7 +85,7 @@ namespace SourceGit.ViewModels } } - private void GetUnmanagedRepositories(DirectoryInfo dir, List outs, EnumerationOptions opts, int depth = 0) + private void GetUnmanagedRepositories(DirectoryInfo dir, List outs, EnumerationOptions opts, int depth = 0) { var subdirs = dir.GetDirectories("*", opts); foreach (var subdir in subdirs) @@ -111,12 +111,19 @@ namespace SourceGit.ViewModels { var normalized = test.StdOut.Trim().Replace("\\", "/"); if (!_managed.Contains(normalized)) - outs.Add(normalized); + outs.Add(new FoundRepository(normalized, false)); } continue; } + var isBare = new Commands.IsBareRepository(subdir.FullName).Result(); + if (isBare) + { + outs.Add(new FoundRepository(normalizedSelf, true)); + continue; + } + if (depth < 5) GetUnmanagedRepositories(subdir, outs, opts, depth + 1); } @@ -161,6 +168,12 @@ namespace SourceGit.ViewModels return added; } + private record FoundRepository(string path, bool isBare) + { + public string Path { get; set; } = path; + public bool IsBare { get; set; } = isBare; + } + private HashSet _managed = new HashSet(); } } diff --git a/src/ViewModels/Welcome.cs b/src/ViewModels/Welcome.cs index 5c9c9380..95f7f010 100644 --- a/src/ViewModels/Welcome.cs +++ b/src/ViewModels/Welcome.cs @@ -94,20 +94,20 @@ namespace SourceGit.ViewModels } var isBare = new Commands.IsBareRepository(path).Result(); - if (isBare) + var repoRoot = path; + if (!isBare) { - App.RaiseException(string.Empty, $"'{path}' is a bare repository, which is not supported by SourceGit!"); - return; + var test = new Commands.QueryRepositoryRootPath(path).ReadToEnd(); + if (!test.IsSuccess || string.IsNullOrEmpty(test.StdOut)) + { + InitRepository(path, parent, test.StdErr); + return; + } + + repoRoot = test.StdOut.Trim(); } - var test = new Commands.QueryRepositoryRootPath(path).ReadToEnd(); - if (!test.IsSuccess || string.IsNullOrEmpty(test.StdOut)) - { - InitRepository(path, parent, test.StdErr); - return; - } - - var node = Preferences.Instance.FindOrAddNodeByRepositoryPath(test.StdOut.Trim(), parent, bMoveExistedNode); + var node = Preferences.Instance.FindOrAddNodeByRepositoryPath(repoRoot, parent, bMoveExistedNode); Refresh(); var launcher = App.GetLauncer(); diff --git a/src/Views/CreateBranch.axaml b/src/Views/CreateBranch.axaml index 43f4e50c..d8b89eb4 100644 --- a/src/Views/CreateBranch.axaml +++ b/src/Views/CreateBranch.axaml @@ -18,8 +18,8 @@ - - + + - - - - - + Text="{DynamicResource Text.CreateBranch.LocalChanges}" + IsVisible="{Binding !IsBareRepository}"/> + + + + + + + + IsChecked="{Binding CheckoutAfterCreated, Mode=TwoWay}" + IsVisible="{Binding !IsBareRepository}"/>