forked from qt-creator/qt-creator
Git: Add a function for getting current local branch
Change-Id: Ibda70cb896633cc7afa3a845b99aac523246c558 Reviewed-by: Orgad Shaneh <orgads@gmail.com> Reviewed-by: Tobias Hunger <tobias.hunger@digia.com>
This commit is contained in:
@@ -432,15 +432,7 @@ QString GerritPlugin::gitBinary()
|
||||
QString GerritPlugin::branch(const QString &repository)
|
||||
{
|
||||
Git::Internal::GitClient *client = Git::Internal::GitPlugin::instance()->gitClient();
|
||||
QString errorMessage;
|
||||
QString output;
|
||||
if (client->synchronousBranchCmd(repository, QStringList(), &output, &errorMessage)) {
|
||||
output.remove(QLatin1Char('\r'));
|
||||
foreach (const QString &line, output.split(QLatin1Char('\n')))
|
||||
if (line.startsWith(QLatin1String("* ")))
|
||||
return line.right(line.size() - 2);
|
||||
}
|
||||
return QString();
|
||||
return client->synchronousCurrentLocalBranch(repository);
|
||||
}
|
||||
|
||||
void GerritPlugin::fetchDisplay(const QSharedPointer<Gerrit::Internal::GerritChange> &change)
|
||||
|
||||
@@ -1190,6 +1190,22 @@ QString GitClient::synchronousShortDescription(const QString &workingDirectory,
|
||||
return output;
|
||||
}
|
||||
|
||||
QString GitClient::synchronousCurrentLocalBranch(const QString &workingDirectory)
|
||||
{
|
||||
QByteArray outputTextData;
|
||||
QStringList arguments;
|
||||
arguments << QLatin1String("symbolic-ref") << QLatin1String("HEAD");
|
||||
if (fullySynchronousGit(workingDirectory, arguments, &outputTextData, 0, false)) {
|
||||
QString branch = commandOutputFromLocal8Bit(outputTextData.trimmed());
|
||||
const QString refsHeadsPrefix = QLatin1String("refs/heads/");
|
||||
if (branch.startsWith(refsHeadsPrefix)) {
|
||||
branch.remove(0, refsHeadsPrefix.count());
|
||||
return branch;
|
||||
}
|
||||
}
|
||||
return QString();
|
||||
}
|
||||
|
||||
static inline QString msgCannotDetermineBranch(const QString &workingDirectory, const QString &why)
|
||||
{
|
||||
return GitClient::tr("Cannot retrieve branch of \"%1\": %2").arg(QDir::toNativeSeparators(workingDirectory), why);
|
||||
@@ -1250,22 +1266,10 @@ QString GitClient::synchronousTopic(const QString &workingDirectory)
|
||||
if (lastModified == data.timeStamp)
|
||||
return data.topic;
|
||||
data.timeStamp = lastModified;
|
||||
QByteArray outputTextData;
|
||||
QStringList arguments;
|
||||
arguments << QLatin1String("symbolic-ref") << QLatin1String("HEAD");
|
||||
// First try to find branch
|
||||
if (fullySynchronousGit(workingDirectory, arguments, &outputTextData, 0, false)) {
|
||||
QString branch = commandOutputFromLocal8Bit(outputTextData.trimmed());
|
||||
|
||||
// Must strip the "refs/heads/" prefix manually since the --short switch
|
||||
// of git symbolic-ref only got introduced with git 1.7.10, which is not
|
||||
// available for all popular Linux distributions yet.
|
||||
const QString refsHeadsPrefix = QLatin1String("refs/heads/");
|
||||
if (branch.startsWith(refsHeadsPrefix))
|
||||
branch.remove(0, refsHeadsPrefix.count());
|
||||
|
||||
QString branch = synchronousCurrentLocalBranch(workingDirectory);
|
||||
if (!branch.isEmpty())
|
||||
return data.topic = branch;
|
||||
}
|
||||
|
||||
// Detached HEAD, try a tag or remote branch
|
||||
QStringList references;
|
||||
@@ -1939,10 +1943,8 @@ void GitClient::continuePreviousGitCommand(const QString &workingDirectory,
|
||||
// Quietly retrieve branch list of remote repository URL
|
||||
//
|
||||
// The branch HEAD is pointing to is always returned first.
|
||||
QStringList GitClient::synchronousRepositoryBranches(const QString &repositoryURL, bool *isDetached)
|
||||
QStringList GitClient::synchronousRepositoryBranches(const QString &repositoryURL)
|
||||
{
|
||||
if (isDetached)
|
||||
*isDetached = true;
|
||||
QStringList arguments(QLatin1String("ls-remote"));
|
||||
arguments << repositoryURL << QLatin1String("HEAD") << QLatin1String("refs/heads/*");
|
||||
const unsigned flags =
|
||||
@@ -1969,8 +1971,6 @@ QStringList GitClient::synchronousRepositoryBranches(const QString &repositoryUR
|
||||
if (!headFound && line.startsWith(headSha)) {
|
||||
branches[0] = branchName;
|
||||
headFound = true;
|
||||
if (isDetached)
|
||||
*isDetached = false;
|
||||
} else {
|
||||
branches.push_back(branchName);
|
||||
}
|
||||
|
||||
@@ -201,6 +201,9 @@ public:
|
||||
QString synchronousShortDescription(const QString &workingDirectory, const QString &revision);
|
||||
QString synchronousShortDescription(const QString &workingDirectory, const QString &revision,
|
||||
const QString &format);
|
||||
|
||||
QString synchronousCurrentLocalBranch(const QString &workingDirectory);
|
||||
|
||||
bool synchronousHeadRefs(const QString &workingDirectory, QStringList *output,
|
||||
QString *errorMessage = 0);
|
||||
QString synchronousTopic(const QString &workingDirectory);
|
||||
@@ -273,7 +276,7 @@ public:
|
||||
|
||||
void launchRepositoryBrowser(const QString &workingDirectory);
|
||||
|
||||
QStringList synchronousRepositoryBranches(const QString &repositoryURL, bool *isDetached = 0);
|
||||
QStringList synchronousRepositoryBranches(const QString &repositoryURL);
|
||||
|
||||
GitSettings *settings() const;
|
||||
|
||||
|
||||
@@ -1001,12 +1001,11 @@ void GitPlugin::pull()
|
||||
bool rebase = m_gitClient->settings()->boolValue(GitSettings::pullRebaseKey);
|
||||
|
||||
if (!rebase) {
|
||||
bool isDetached;
|
||||
QString branchRebaseConfig = m_gitClient->synchronousRepositoryBranches(topLevel, &isDetached).at(0);
|
||||
if (!isDetached) {
|
||||
branchRebaseConfig.prepend(QLatin1String("branch."));
|
||||
branchRebaseConfig.append(QLatin1String(".rebase"));
|
||||
rebase = (m_gitClient->readConfigValue(topLevel, branchRebaseConfig) == QLatin1String("true"));
|
||||
QString currentBranch = m_gitClient->synchronousCurrentLocalBranch(topLevel);
|
||||
if (!currentBranch.isEmpty()) {
|
||||
currentBranch.prepend(QLatin1String("branch."));
|
||||
currentBranch.append(QLatin1String(".rebase"));
|
||||
rebase = (m_gitClient->readConfigValue(topLevel, currentBranch) == QLatin1String("true"));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user