Include remote branches in workspace create auto-completion

The destination completer now lists both local and remote branches
(with remote prefix stripped for convenience). Fan-out logic checks
for remote tracking branches: if a branch exists on a remote but not
locally, creates a local tracking branch from it.
This commit is contained in:
Levi Neely 2026-07-22 16:59:24 +02:00
parent 9b1de56e97
commit 60d86ab690
1 changed files with 25 additions and 3 deletions

View File

@ -2501,11 +2501,19 @@ void GitPlusPlusView::createWorkspace()
QString repoPath = src.absoluteFilePath(entry); QString repoPath = src.absoluteFilePath(entry);
if (QFileInfo::exists(repoPath + QStringLiteral("/.git"))) { if (QFileInfo::exists(repoPath + QStringLiteral("/.git"))) {
QByteArray out = GitProcess::run(repoPath, QByteArray out = GitProcess::run(repoPath,
{QStringLiteral("for-each-ref"), QStringLiteral("--format=%(refname:short)"), QStringLiteral("refs/heads/")}); {QStringLiteral("for-each-ref"), QStringLiteral("--format=%(refname:short)"), QStringLiteral("refs/heads/"), QStringLiteral("refs/remotes/")});
const auto lines = QString::fromUtf8(out).split(QLatin1Char('\n'), Qt::SkipEmptyParts); const auto lines = QString::fromUtf8(out).split(QLatin1Char('\n'), Qt::SkipEmptyParts);
for (const auto &line : lines) { for (const auto &line : lines) {
QString b = line.trimmed(); QString b = line.trimmed();
if (!b.isEmpty() && !branches.contains(b)) if (b.isEmpty()) continue;
// Strip remote prefix (e.g. "origin/feature" -> "feature")
if (b.contains(QLatin1Char('/'))) {
QString stripped = b.section(QLatin1Char('/'), 1);
if (!stripped.isEmpty() && stripped != QStringLiteral("HEAD")
&& !branches.contains(stripped))
branches.append(stripped);
}
if (!branches.contains(b))
branches.append(b); branches.append(b);
} }
} }
@ -2614,16 +2622,30 @@ void GitPlusPlusView::createWorkspace()
QString sourceRepoPath = source.absoluteFilePath(repoName); QString sourceRepoPath = source.absoluteFilePath(repoName);
QString targetRepoPath = newWsPath + QLatin1Char('/') + repoName; QString targetRepoPath = newWsPath + QLatin1Char('/') + repoName;
// Check if branch exists in this repo // Check if branch exists locally
QByteArray branchCheck = GitProcess::run(sourceRepoPath, QByteArray branchCheck = GitProcess::run(sourceRepoPath,
{QStringLiteral("rev-parse"), QStringLiteral("--verify"), QStringLiteral("refs/heads/") + name}); {QStringLiteral("rev-parse"), QStringLiteral("--verify"), QStringLiteral("refs/heads/") + name});
bool branchExists = !branchCheck.isEmpty(); bool branchExists = !branchCheck.isEmpty();
// Check if branch exists on a remote (for tracking)
QString remoteRef;
if (!branchExists) {
QByteArray remoteCheck = GitProcess::run(sourceRepoPath,
{QStringLiteral("for-each-ref"), QStringLiteral("--format=%(refname:short)"),
QStringLiteral("refs/remotes/*/") + name});
remoteRef = QString::fromUtf8(remoteCheck).trimmed().section(QLatin1Char('\n'), 0, 0);
}
QStringList args; QStringList args;
args << QStringLiteral("worktree") << QStringLiteral("add"); args << QStringLiteral("worktree") << QStringLiteral("add");
if (branchExists) { if (branchExists) {
// Local branch exists: just check it out
args << targetRepoPath << name; args << targetRepoPath << name;
} else if (!remoteRef.isEmpty()) {
// Remote branch exists: create local tracking branch
args << QStringLiteral("-b") << name << targetRepoPath << remoteRef;
} else { } else {
// No branch anywhere: create new
args << QStringLiteral("-b") << name << targetRepoPath; args << QStringLiteral("-b") << name << targetRepoPath;
} }