forked from qt-creator/qt-creator
Git: Refactor branch/HEAD access
Change-Id: I0c9955737033c0f839ac1f6ea053fecc20c24d48 Reviewed-by: Tobias Hunger <tobias.hunger@digia.com>
This commit is contained in:
committed by
Orgad Shaneh
parent
4eedda04d3
commit
a1e3a5de9e
@@ -1098,64 +1098,43 @@ static inline QString msgCannotDetermineBranch(const QString &workingDirectory,
|
||||
return GitClient::tr("Cannot retrieve branch of \"%1\": %2").arg(QDir::toNativeSeparators(workingDirectory), why);
|
||||
}
|
||||
|
||||
// Retrieve head revision/branch
|
||||
bool GitClient::synchronousTopRevision(const QString &workingDirectory, QString *revision,
|
||||
QString *branch, QString *errorMessageIn)
|
||||
// Retrieve head branch
|
||||
QString GitClient::synchronousBranch(const QString &workingDirectory)
|
||||
{
|
||||
QByteArray outputTextData;
|
||||
QStringList arguments;
|
||||
arguments << QLatin1String("symbolic-ref") << QLatin1String("--short") << QLatin1String("HEAD");
|
||||
// if HEAD is detached, the command is expected to fail.
|
||||
if (!fullySynchronousGit(workingDirectory, arguments, &outputTextData))
|
||||
return QString();
|
||||
QString branch = commandOutputFromLocal8Bit(outputTextData);
|
||||
branch.remove(QLatin1Char('\n'));
|
||||
return branch;
|
||||
}
|
||||
|
||||
// Retrieve head revision
|
||||
QString GitClient::synchronousTopRevision(const QString &workingDirectory, QString *errorMessageIn)
|
||||
{
|
||||
QByteArray outputTextData;
|
||||
QByteArray errorText;
|
||||
QStringList arguments;
|
||||
QString errorMessage;
|
||||
do {
|
||||
// get revision
|
||||
if (revision) {
|
||||
revision->clear();
|
||||
arguments << QLatin1String("log") << QLatin1String(noColorOption)
|
||||
<< QLatin1String("--max-count=1") << QLatin1String("--pretty=format:%H");
|
||||
arguments << QLatin1String("rev-parse") << QLatin1String("HEAD");
|
||||
if (!fullySynchronousGit(workingDirectory, arguments, &outputTextData, &errorText)) {
|
||||
errorMessage = tr("Cannot retrieve top revision of \"%1\": %2").arg(QDir::toNativeSeparators(workingDirectory), commandOutputFromLocal8Bit(errorText));
|
||||
break;
|
||||
errorMessage = tr("Cannot retrieve top revision of \"%1\": %2")
|
||||
.arg(QDir::toNativeSeparators(workingDirectory), commandOutputFromLocal8Bit(errorText));
|
||||
return QString();
|
||||
}
|
||||
*revision = commandOutputFromLocal8Bit(outputTextData);
|
||||
revision->remove(QLatin1Char('\n'));
|
||||
} // revision desired
|
||||
// get branch
|
||||
if (branch) {
|
||||
branch->clear();
|
||||
arguments.clear();
|
||||
arguments << QLatin1String("branch") << QLatin1String(noColorOption);
|
||||
if (!fullySynchronousGit(workingDirectory, arguments, &outputTextData, &errorText)) {
|
||||
errorMessage = msgCannotDetermineBranch(workingDirectory, commandOutputFromLocal8Bit(errorText));
|
||||
break;
|
||||
}
|
||||
/* parse output for current branch: \code
|
||||
* master
|
||||
branch2
|
||||
\endcode */
|
||||
const QString branchPrefix = QLatin1String("* ");
|
||||
foreach(const QString &line, commandOutputLinesFromLocal8Bit(outputTextData)) {
|
||||
if (line.startsWith(branchPrefix)) {
|
||||
*branch = line;
|
||||
branch->remove(0, branchPrefix.size());
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (branch->isEmpty()) {
|
||||
errorMessage = msgCannotDetermineBranch(workingDirectory,
|
||||
QString::fromLatin1("Internal error: Failed to parse output: %1").arg(commandOutputFromLocal8Bit(outputTextData)));
|
||||
break;
|
||||
}
|
||||
} // branch
|
||||
} while (false);
|
||||
const bool failed = (revision && revision->isEmpty()) || (branch && branch->isEmpty());
|
||||
if (failed && !errorMessage.isEmpty()) {
|
||||
if (errorMessageIn) {
|
||||
QString revision = commandOutputFromLocal8Bit(outputTextData);
|
||||
revision.remove(QLatin1Char('\n'));
|
||||
if (revision.isEmpty() && !errorMessage.isEmpty()) {
|
||||
if (errorMessageIn)
|
||||
*errorMessageIn = errorMessage;
|
||||
} else {
|
||||
else
|
||||
outputWindow()->appendError(errorMessage);
|
||||
}
|
||||
}
|
||||
return !failed;
|
||||
return revision;
|
||||
}
|
||||
|
||||
// Format an entry in a one-liner for selection list using git log.
|
||||
|
||||
@@ -160,8 +160,8 @@ public:
|
||||
QString synchronousShortDescription(const QString &workingDirectory, const QString &revision);
|
||||
QString synchronousShortDescription(const QString &workingDirectory, const QString &revision,
|
||||
const QString &format);
|
||||
bool synchronousTopRevision(const QString &workingDirectory, QString *revision = 0,
|
||||
QString *branch = 0, QString *errorMessage = 0);
|
||||
QString synchronousBranch(const QString &workingDirectory);
|
||||
QString synchronousTopRevision(const QString &workingDirectory, QString *errorMessage = 0);
|
||||
|
||||
bool cloneRepository(const QString &directory, const QByteArray &url);
|
||||
QString vcsGetRepositoryURL(const QString &directory);
|
||||
@@ -266,7 +266,7 @@ private:
|
||||
bool fullySynchronousGit(const QString &workingDirectory,
|
||||
const QStringList &arguments,
|
||||
QByteArray* outputText,
|
||||
QByteArray* errorText,
|
||||
QByteArray* errorText = 0,
|
||||
bool logCommandToWindow = true) const;
|
||||
|
||||
// Synchronous git execution using Utils::SynchronousProcess, with
|
||||
|
||||
@@ -161,10 +161,10 @@ QString GitVersionControl::vcsCreateSnapshot(const QString &topLevel)
|
||||
return stashMessage;
|
||||
if (repositoryUnchanged) {
|
||||
// For unchanged repository state: return identifier + top revision
|
||||
QString topRevision;
|
||||
QString branch;
|
||||
if (!m_client->synchronousTopRevision(topLevel, &topRevision, &branch))
|
||||
QString topRevision = m_client->synchronousTopRevision(topLevel);
|
||||
if (topRevision.isEmpty())
|
||||
return QString();
|
||||
QString branch = m_client->synchronousBranch(topLevel);
|
||||
const QChar colon = QLatin1Char(':');
|
||||
QString id = QLatin1String(stashRevisionIdC);
|
||||
id += colon;
|
||||
@@ -201,9 +201,13 @@ bool GitVersionControl::vcsRestoreSnapshot(const QString &topLevel, const QStrin
|
||||
break;
|
||||
const QString branch = tokens.at(1);
|
||||
const QString revision = tokens.at(2);
|
||||
success = m_client->synchronousReset(topLevel)
|
||||
&& m_client->synchronousCheckoutBranch(topLevel, branch)
|
||||
&& m_client->synchronousCheckoutFiles(topLevel, QStringList(), revision);
|
||||
success = m_client->synchronousReset(topLevel);
|
||||
if (success && !branch.isEmpty()) {
|
||||
success = m_client->synchronousCheckoutBranch(topLevel, branch) &&
|
||||
m_client->synchronousCheckoutFiles(topLevel, QStringList(), revision);
|
||||
} else {
|
||||
success = m_client->synchronousCheckoutBranch(topLevel, revision);
|
||||
}
|
||||
} else {
|
||||
// Restore stash if it can be resolved.
|
||||
QString stashName;
|
||||
|
||||
Reference in New Issue
Block a user