Skip to content

Commit 9bfe28d

Browse files
committed
feature: opening submodule next to its parent page
Signed-off-by: leo <longshuang@msn.cn>
1 parent 9ca439d commit 9bfe28d

2 files changed

Lines changed: 68 additions & 3 deletions

File tree

src/ViewModels/Launcher.cs

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,72 @@ public void OpenRepositoryInTab(RepositoryNode node, LauncherPage page)
373373
ActivePage = page;
374374
}
375375

376+
public void OpenSubRepository(LauncherPage ownerPage, string fullpath)
377+
{
378+
var normalizedPath = fullpath.Replace('\\', '/').TrimEnd('/');
379+
380+
// Check if the sub-repository is already open in any of the tabs
381+
foreach (var one in Pages)
382+
{
383+
if (one.Node.Id.Equals(normalizedPath, StringComparison.Ordinal))
384+
{
385+
ActivePage = one;
386+
return;
387+
}
388+
}
389+
390+
// Make sure the target directory exists
391+
if (!Directory.Exists(normalizedPath))
392+
{
393+
ownerPage.Notifications.Add(new Models.Notification
394+
{
395+
Group = ownerPage.Node.Id,
396+
Message = "Repository path does NOT exist. Please check the path.",
397+
IsError = true,
398+
});
399+
400+
return;
401+
}
402+
403+
// Check if the target directory is a valid git repository
404+
var gitDir = GetRepositoryGitDir(normalizedPath);
405+
if (string.IsNullOrEmpty(gitDir))
406+
{
407+
ownerPage.Notifications.Add(new Models.Notification
408+
{
409+
Group = ownerPage.Node.Id,
410+
Message = "Given path is not a valid git repository!",
411+
IsError = true,
412+
});
413+
return;
414+
}
415+
416+
// Get the owner repository's name and extract the pure owner name (without any prefix)
417+
var ownerName = ownerPage.Node.Name;
418+
var colonIdx = ownerName.LastIndexOf(':');
419+
var pureOwnerName = (colonIdx >= 0 && colonIdx < ownerName.Length - 1) ? ownerName.Substring(colonIdx + 1).Trim() : ownerName;
420+
421+
// Find the sub-repository node in the preferences or create a new one if it doesn't exist
422+
var node = Preferences.Instance.FindNode(normalizedPath) ?? new RepositoryNode
423+
{
424+
Id = normalizedPath,
425+
Name = $"{pureOwnerName}: {Path.GetFileName(normalizedPath)}",
426+
Bookmark = 0,
427+
IsRepository = true,
428+
IsUnmanaged = true
429+
};
430+
node.LoadMinimalInfo(gitDir);
431+
432+
var repo = new Repository(false, node.Id, gitDir);
433+
repo.Open();
434+
435+
var page = new LauncherPage(node, repo);
436+
var idxOfOwner = Pages.IndexOf(ownerPage);
437+
Pages.Insert(idxOfOwner + 1, page);
438+
_activeWorkspace.Repositories.Insert(idxOfOwner + 1, normalizedPath);
439+
ActivePage = page;
440+
}
441+
376442
private void DispatchNotification(Models.Notification notification)
377443
{
378444
if (!Dispatcher.UIThread.CheckAccess())

src/ViewModels/Repository.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1567,9 +1567,8 @@ public void OpenSubmodule(string submodule)
15671567
if (selfPage == null)
15681568
return;
15691569

1570-
var root = Path.GetFullPath(Path.Combine(FullPath, submodule));
1571-
var normalizedPath = root.Replace('\\', '/').TrimEnd('/');
1572-
App.GetLauncher().OpenRepositoryInTab(normalizedPath, null);
1570+
var fullpath = Path.GetFullPath(Path.Combine(FullPath, submodule));
1571+
App.GetLauncher().OpenSubRepository(selfPage, fullpath);
15731572
}
15741573

15751574
public void AddWorktree()

0 commit comments

Comments
 (0)