Commit c49fe835 authored by Petar Perisin's avatar Petar Perisin
Browse files

Git: Add a function for getting current local branch



Change-Id: Ibda70cb896633cc7afa3a845b99aac523246c558
Reviewed-by: Orgad Shaneh's avatarOrgad Shaneh <orgads@gmail.com>
Reviewed-by: default avatarTobias Hunger <tobias.hunger@digia.com>
parent 30f24189
...@@ -432,15 +432,7 @@ QString GerritPlugin::gitBinary() ...@@ -432,15 +432,7 @@ QString GerritPlugin::gitBinary()
QString GerritPlugin::branch(const QString &repository) QString GerritPlugin::branch(const QString &repository)
{ {
Git::Internal::GitClient *client = Git::Internal::GitPlugin::instance()->gitClient(); Git::Internal::GitClient *client = Git::Internal::GitPlugin::instance()->gitClient();
QString errorMessage; return client->synchronousCurrentLocalBranch(repository);
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();
} }
void GerritPlugin::fetchDisplay(const QSharedPointer<Gerrit::Internal::GerritChange> &change) void GerritPlugin::fetchDisplay(const QSharedPointer<Gerrit::Internal::GerritChange> &change)
......
...@@ -1190,6 +1190,22 @@ QString GitClient::synchronousShortDescription(const QString &workingDirectory, ...@@ -1190,6 +1190,22 @@ QString GitClient::synchronousShortDescription(const QString &workingDirectory,
return output; 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) static inline QString msgCannotDetermineBranch(const QString &workingDirectory, const QString &why)
{ {
return GitClient::tr("Cannot retrieve branch of \"%1\": %2").arg(QDir::toNativeSeparators(workingDirectory), 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) ...@@ -1250,22 +1266,10 @@ QString GitClient::synchronousTopic(const QString &workingDirectory)
if (lastModified == data.timeStamp) if (lastModified == data.timeStamp)
return data.topic; return data.topic;
data.timeStamp = lastModified; data.timeStamp = lastModified;
QByteArray outputTextData;
QStringList arguments;
arguments << QLatin1String("symbolic-ref") << QLatin1String("HEAD");
// First try to find branch // First try to find branch
if (fullySynchronousGit(workingDirectory, arguments, &outputTextData, 0, false)) { QString branch = synchronousCurrentLocalBranch(workingDirectory);
QString branch = commandOutputFromLocal8Bit(outputTextData.trimmed()); if (!branch.isEmpty())
// 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());
return data.topic = branch; return data.topic = branch;
}
// Detached HEAD, try a tag or remote branch // Detached HEAD, try a tag or remote branch
QStringList references; QStringList references;
...@@ -1939,10 +1943,8 @@ void GitClient::continuePreviousGitCommand(const QString &workingDirectory, ...@@ -1939,10 +1943,8 @@ void GitClient::continuePreviousGitCommand(const QString &workingDirectory,
// Quietly retrieve branch list of remote repository URL // Quietly retrieve branch list of remote repository URL
// //
// The branch HEAD is pointing to is always returned first. // 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")); QStringList arguments(QLatin1String("ls-remote"));
arguments << repositoryURL << QLatin1String("HEAD") << QLatin1String("refs/heads/*"); arguments << repositoryURL << QLatin1String("HEAD") << QLatin1String("refs/heads/*");
const unsigned flags = const unsigned flags =
...@@ -1969,8 +1971,6 @@ QStringList GitClient::synchronousRepositoryBranches(const QString &repositoryUR ...@@ -1969,8 +1971,6 @@ QStringList GitClient::synchronousRepositoryBranches(const QString &repositoryUR
if (!headFound && line.startsWith(headSha)) { if (!headFound && line.startsWith(headSha)) {
branches[0] = branchName; branches[0] = branchName;
headFound = true; headFound = true;
if (isDetached)
*isDetached = false;
} else { } else {
branches.push_back(branchName); branches.push_back(branchName);
} }
......
...@@ -201,6 +201,9 @@ public: ...@@ -201,6 +201,9 @@ public:
QString synchronousShortDescription(const QString &workingDirectory, const QString &revision); QString synchronousShortDescription(const QString &workingDirectory, const QString &revision);
QString synchronousShortDescription(const QString &workingDirectory, const QString &revision, QString synchronousShortDescription(const QString &workingDirectory, const QString &revision,
const QString &format); const QString &format);
QString synchronousCurrentLocalBranch(const QString &workingDirectory);
bool synchronousHeadRefs(const QString &workingDirectory, QStringList *output, bool synchronousHeadRefs(const QString &workingDirectory, QStringList *output,
QString *errorMessage = 0); QString *errorMessage = 0);
QString synchronousTopic(const QString &workingDirectory); QString synchronousTopic(const QString &workingDirectory);
...@@ -273,7 +276,7 @@ public: ...@@ -273,7 +276,7 @@ public:
void launchRepositoryBrowser(const QString &workingDirectory); void launchRepositoryBrowser(const QString &workingDirectory);
QStringList synchronousRepositoryBranches(const QString &repositoryURL, bool *isDetached = 0); QStringList synchronousRepositoryBranches(const QString &repositoryURL);
GitSettings *settings() const; GitSettings *settings() const;
......
...@@ -1001,12 +1001,11 @@ void GitPlugin::pull() ...@@ -1001,12 +1001,11 @@ void GitPlugin::pull()
bool rebase = m_gitClient->settings()->boolValue(GitSettings::pullRebaseKey); bool rebase = m_gitClient->settings()->boolValue(GitSettings::pullRebaseKey);
if (!rebase) { if (!rebase) {
bool isDetached; QString currentBranch = m_gitClient->synchronousCurrentLocalBranch(topLevel);
QString branchRebaseConfig = m_gitClient->synchronousRepositoryBranches(topLevel, &isDetached).at(0); if (!currentBranch.isEmpty()) {
if (!isDetached) { currentBranch.prepend(QLatin1String("branch."));
branchRebaseConfig.prepend(QLatin1String("branch.")); currentBranch.append(QLatin1String(".rebase"));
branchRebaseConfig.append(QLatin1String(".rebase")); rebase = (m_gitClient->readConfigValue(topLevel, currentBranch) == QLatin1String("true"));
rebase = (m_gitClient->readConfigValue(topLevel, branchRebaseConfig) == QLatin1String("true"));
} }
} }
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment