Git: Show remote branch in topic

No extra terminal commands are executed.

Change-Id: Ia875b5dbce2784962b9fc2b7c9c1b0a696a02ec8
Reviewed-by: Tobias Hunger <tobias.hunger@digia.com>
Reviewed-by: Orgad Shaneh <orgads@gmail.com>
Reviewed-by: Petar Perisin <petar.perisin@gmail.com>
This commit is contained in:
Petar Perisin
2013-03-28 13:57:36 +02:00
parent eebee3311d
commit 31ed90b47b
2 changed files with 54 additions and 7 deletions

View File

@@ -1181,6 +1181,42 @@ static inline QString msgCannotDetermineBranch(const QString &workingDirectory,
return GitClient::tr("Cannot retrieve branch of \"%1\": %2").arg(QDir::toNativeSeparators(workingDirectory), why);
}
bool GitClient::synchronousHeadRefs(const QString &workingDirectory, QStringList *output,
QString *errorMessage)
{
QStringList args;
args << QLatin1String("show-ref") << QLatin1String("--head") << QLatin1String("--abbrev=10");
QByteArray outputText;
QByteArray errorText;
const bool rc = fullySynchronousGit(workingDirectory, args, &outputText, &errorText);
if (!rc) {
QString message = tr("Cannot run \"git show-ref --head\" in \"%1\": %2").
arg(QDir::toNativeSeparators(workingDirectory), commandOutputFromLocal8Bit(errorText));
if (errorMessage)
*errorMessage = message;
else
outputWindow()->append(message);
return false;
}
QByteArray headSha = outputText.left(10);
QByteArray newLine("\n");
int currentIndex = 15;
while (true) {
currentIndex = outputText.indexOf(headSha, currentIndex);
if (currentIndex < 0)
break;
currentIndex += 11;
output->append(QString::fromLocal8Bit(outputText.mid(currentIndex,
outputText.indexOf(newLine, currentIndex) - currentIndex)));
}
return true;
}
struct TopicData
{
QDateTime timeStamp;
@@ -1216,15 +1252,24 @@ QString GitClient::synchronousTopic(const QString &workingDirectory)
return data.topic = branch;
}
// Detached HEAD, try a tag
arguments.clear();
arguments << QLatin1String("describe") << QLatin1String("--tags")
<< QLatin1String("--exact-match") << QLatin1String("HEAD");
if (fullySynchronousGit(workingDirectory, arguments, &outputTextData, 0, false))
return data.topic = commandOutputFromLocal8Bit(outputTextData.trimmed());
// Detached HEAD, try a tag or remote branch
QStringList references;
if (!synchronousHeadRefs(workingDirectory, &references))
return QString();
QString tagStart = QLatin1String("refs/tags/");
QString remoteStart = QLatin1String("refs/remotes/");
QString remoteBranch;
foreach (const QString &ref, references) {
if (ref.startsWith(tagStart))
return data.topic = ref.mid(10);
if (ref.startsWith(remoteStart))
remoteBranch = ref.mid(13);
}
// No tag
return data.topic = tr("Detached HEAD");
return data.topic = remoteBranch.isEmpty() ? tr("Detached HEAD") : remoteBranch;
}
// Retrieve head revision