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);
|
return GitClient::tr("Cannot retrieve branch of \"%1\": %2").arg(QDir::toNativeSeparators(workingDirectory), why);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Retrieve head revision/branch
|
// Retrieve head branch
|
||||||
bool GitClient::synchronousTopRevision(const QString &workingDirectory, QString *revision,
|
QString GitClient::synchronousBranch(const QString &workingDirectory)
|
||||||
QString *branch, QString *errorMessageIn)
|
{
|
||||||
|
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 outputTextData;
|
||||||
QByteArray errorText;
|
QByteArray errorText;
|
||||||
QStringList arguments;
|
QStringList arguments;
|
||||||
QString errorMessage;
|
QString errorMessage;
|
||||||
do {
|
// get revision
|
||||||
// get revision
|
arguments << QLatin1String("rev-parse") << QLatin1String("HEAD");
|
||||||
if (revision) {
|
if (!fullySynchronousGit(workingDirectory, arguments, &outputTextData, &errorText)) {
|
||||||
revision->clear();
|
errorMessage = tr("Cannot retrieve top revision of \"%1\": %2")
|
||||||
arguments << QLatin1String("log") << QLatin1String(noColorOption)
|
.arg(QDir::toNativeSeparators(workingDirectory), commandOutputFromLocal8Bit(errorText));
|
||||||
<< QLatin1String("--max-count=1") << QLatin1String("--pretty=format:%H");
|
return QString();
|
||||||
if (!fullySynchronousGit(workingDirectory, arguments, &outputTextData, &errorText)) {
|
|
||||||
errorMessage = tr("Cannot retrieve top revision of \"%1\": %2").arg(QDir::toNativeSeparators(workingDirectory), commandOutputFromLocal8Bit(errorText));
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
*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) {
|
|
||||||
*errorMessageIn = errorMessage;
|
|
||||||
} else {
|
|
||||||
outputWindow()->appendError(errorMessage);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return !failed;
|
QString revision = commandOutputFromLocal8Bit(outputTextData);
|
||||||
|
revision.remove(QLatin1Char('\n'));
|
||||||
|
if (revision.isEmpty() && !errorMessage.isEmpty()) {
|
||||||
|
if (errorMessageIn)
|
||||||
|
*errorMessageIn = errorMessage;
|
||||||
|
else
|
||||||
|
outputWindow()->appendError(errorMessage);
|
||||||
|
}
|
||||||
|
return revision;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Format an entry in a one-liner for selection list using git log.
|
// 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);
|
||||||
QString synchronousShortDescription(const QString &workingDirectory, const QString &revision,
|
QString synchronousShortDescription(const QString &workingDirectory, const QString &revision,
|
||||||
const QString &format);
|
const QString &format);
|
||||||
bool synchronousTopRevision(const QString &workingDirectory, QString *revision = 0,
|
QString synchronousBranch(const QString &workingDirectory);
|
||||||
QString *branch = 0, QString *errorMessage = 0);
|
QString synchronousTopRevision(const QString &workingDirectory, QString *errorMessage = 0);
|
||||||
|
|
||||||
bool cloneRepository(const QString &directory, const QByteArray &url);
|
bool cloneRepository(const QString &directory, const QByteArray &url);
|
||||||
QString vcsGetRepositoryURL(const QString &directory);
|
QString vcsGetRepositoryURL(const QString &directory);
|
||||||
@@ -266,7 +266,7 @@ private:
|
|||||||
bool fullySynchronousGit(const QString &workingDirectory,
|
bool fullySynchronousGit(const QString &workingDirectory,
|
||||||
const QStringList &arguments,
|
const QStringList &arguments,
|
||||||
QByteArray* outputText,
|
QByteArray* outputText,
|
||||||
QByteArray* errorText,
|
QByteArray* errorText = 0,
|
||||||
bool logCommandToWindow = true) const;
|
bool logCommandToWindow = true) const;
|
||||||
|
|
||||||
// Synchronous git execution using Utils::SynchronousProcess, with
|
// Synchronous git execution using Utils::SynchronousProcess, with
|
||||||
|
|||||||
@@ -161,10 +161,10 @@ QString GitVersionControl::vcsCreateSnapshot(const QString &topLevel)
|
|||||||
return stashMessage;
|
return stashMessage;
|
||||||
if (repositoryUnchanged) {
|
if (repositoryUnchanged) {
|
||||||
// For unchanged repository state: return identifier + top revision
|
// For unchanged repository state: return identifier + top revision
|
||||||
QString topRevision;
|
QString topRevision = m_client->synchronousTopRevision(topLevel);
|
||||||
QString branch;
|
if (topRevision.isEmpty())
|
||||||
if (!m_client->synchronousTopRevision(topLevel, &topRevision, &branch))
|
|
||||||
return QString();
|
return QString();
|
||||||
|
QString branch = m_client->synchronousBranch(topLevel);
|
||||||
const QChar colon = QLatin1Char(':');
|
const QChar colon = QLatin1Char(':');
|
||||||
QString id = QLatin1String(stashRevisionIdC);
|
QString id = QLatin1String(stashRevisionIdC);
|
||||||
id += colon;
|
id += colon;
|
||||||
@@ -201,9 +201,13 @@ bool GitVersionControl::vcsRestoreSnapshot(const QString &topLevel, const QStrin
|
|||||||
break;
|
break;
|
||||||
const QString branch = tokens.at(1);
|
const QString branch = tokens.at(1);
|
||||||
const QString revision = tokens.at(2);
|
const QString revision = tokens.at(2);
|
||||||
success = m_client->synchronousReset(topLevel)
|
success = m_client->synchronousReset(topLevel);
|
||||||
&& m_client->synchronousCheckoutBranch(topLevel, branch)
|
if (success && !branch.isEmpty()) {
|
||||||
&& m_client->synchronousCheckoutFiles(topLevel, QStringList(), revision);
|
success = m_client->synchronousCheckoutBranch(topLevel, branch) &&
|
||||||
|
m_client->synchronousCheckoutFiles(topLevel, QStringList(), revision);
|
||||||
|
} else {
|
||||||
|
success = m_client->synchronousCheckoutBranch(topLevel, revision);
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
// Restore stash if it can be resolved.
|
// Restore stash if it can be resolved.
|
||||||
QString stashName;
|
QString stashName;
|
||||||
|
|||||||
Reference in New Issue
Block a user