Git: Return Result<QString> from GitClient::synchronizedLog()

Plan is to simplify the overall logic with less "shared"
out-parameters.

Complication in this change here is the magic in msgCannotRun() to
show or not show the error immediately, depending on whether
errorMessage is nullptr or not.

Since we now always pass a non-null errorMessage argument, some
VcsOutputWindow::appendError(...) calls have to be moved to the
caller side.

Change-Id: Ibd20d7ba0811ec34f86dc96914d4fbe7ae446ada
Reviewed-by: David Schulz <david.schulz@qt.io>
This commit is contained in:
hjk
2025-04-24 11:57:09 +02:00
parent f0ad0b3b40
commit 344a4e97a6
4 changed files with 44 additions and 33 deletions

View File

@@ -707,7 +707,6 @@ QModelIndex BranchModel::addBranch(const QString &name, bool track, const QModel
const QString trackedBranch = fullName(startPoint); const QString trackedBranch = fullName(startPoint);
const QString fullTrackedBranch = fullName(startPoint, true); const QString fullTrackedBranch = fullName(startPoint, true);
QString startSha; QString startSha;
QString output;
QString errorMessage; QString errorMessage;
QDateTime branchDateTime; QDateTime branchDateTime;
@@ -717,15 +716,20 @@ QModelIndex BranchModel::addBranch(const QString &name, bool track, const QModel
startSha = sha(startPoint); startSha = sha(startPoint);
branchDateTime = dateTime(startPoint); branchDateTime = dateTime(startPoint);
} else { } else {
const QStringList arguments({"-n1", "--format=%H %ct"}); const Result<QString> res = gitClient().synchronousLog(d->workingDirectory,
if (gitClient().synchronousLog(d->workingDirectory, arguments, &output, &errorMessage, {"-n1", "--format=%H %ct"},
RunFlags::SuppressCommandLogging)) { RunFlags::SuppressCommandLogging);
const QStringList values = output.split(' '); if (res) {
const QStringList values = res.value().split(' ');
startSha = values[0]; startSha = values[0];
branchDateTime = QDateTime::fromSecsSinceEpoch(values[1].toLongLong()); branchDateTime = QDateTime::fromSecsSinceEpoch(values[1].toLongLong());
} else {
errorMessage = res.error();
} }
} }
QString output;
if (!gitClient().synchronousBranchCmd(d->workingDirectory, args, &output, &errorMessage)) { if (!gitClient().synchronousBranchCmd(d->workingDirectory, args, &output, &errorMessage)) {
VcsOutputWindow::appendError(errorMessage); VcsOutputWindow::appendError(errorMessage);
return {}; return {};
@@ -998,13 +1002,9 @@ void BranchModel::Private::updateAllUpstreamStatus(BranchNode *node)
QString BranchModel::toolTip(const QString &sha) const QString BranchModel::toolTip(const QString &sha) const
{ {
// Show the sha description excluding diff as toolTip // Show the sha description excluding diff as toolTip
QString output; const Result<QString> res = gitClient().synchronousLog(d->workingDirectory, {"-n1", sha},
QString errorMessage; RunFlags::SuppressCommandLogging);
if (!gitClient().synchronousLog(d->workingDirectory, {"-n1", sha}, &output, &errorMessage, return res ? res.value() : res.error();
RunFlags::SuppressCommandLogging)) {
return errorMessage;
}
return output;
} }
} // Git::Internal } // Git::Internal

View File

@@ -1481,8 +1481,9 @@ void GitClient::addFile(const FilePath &workingDirectory, const QString &fileNam
vcsExec(workingDirectory, {"add", fileName}); vcsExec(workingDirectory, {"add", fileName});
} }
bool GitClient::synchronousLog(const FilePath &workingDirectory, const QStringList &arguments, Result<QString> GitClient::synchronousLog(const FilePath &workingDirectory,
QString *output, QString *errorMessageIn, RunFlags flags) const QStringList &arguments,
RunFlags flags)
{ {
QStringList allArguments = {"log", noColorOption}; QStringList allArguments = {"log", noColorOption};
@@ -1490,13 +1491,13 @@ bool GitClient::synchronousLog(const FilePath &workingDirectory, const QStringLi
const CommandResult result = vcsSynchronousExec(workingDirectory, allArguments, flags, const CommandResult result = vcsSynchronousExec(workingDirectory, allArguments, flags,
vcsTimeoutS(), encoding(EncodingLogOutput, workingDirectory)); vcsTimeoutS(), encoding(EncodingLogOutput, workingDirectory));
if (result.result() == ProcessResult::FinishedWithSuccess) { if (result.result() == ProcessResult::FinishedWithSuccess)
*output = result.cleanedStdOut(); return result.cleanedStdOut();
return true;
} QString errorMessage;
msgCannotRun(Tr::tr("Cannot obtain log of \"%1\": %2") msgCannotRun(Tr::tr("Cannot obtain log of \"%1\": %2")
.arg(workingDirectory.toUserOutput(), result.cleanedStdErr()), errorMessageIn); .arg(workingDirectory.toUserOutput(), result.cleanedStdErr()), &errorMessage);
return false; return ResultError(errorMessage);
} }
bool GitClient::synchronousAdd(const FilePath &workingDirectory, bool GitClient::synchronousAdd(const FilePath &workingDirectory,
@@ -2757,8 +2758,12 @@ Result<CommitData> GitClient::getCommitData(CommitType commitType, const FilePat
QString errorMessage; QString errorMessage;
QString output; QString output;
if (commitData.commitType == FixupCommit) { if (commitData.commitType == FixupCommit) {
synchronousLog(repoDirectory, {HEAD, "--not", "--remotes", "-n1"}, &output, &errorMessage, const Result<QString> res = synchronousLog(repoDirectory, {HEAD, "--not", "--remotes", "-n1"},
RunFlags::SuppressCommandLogging); RunFlags::SuppressCommandLogging);
if (res)
output = res.value();
else
errorMessage = res.error();
if (output.isEmpty()) if (output.isEmpty())
return ResultError(msgNoCommits(false)); return ResultError(msgNoCommits(false));
} else { } else {
@@ -3710,10 +3715,14 @@ QString GitClient::suggestedLocalBranchName(
if (targetType == BranchTargetType::Remote) { if (targetType == BranchTargetType::Remote) {
initialName = target.mid(target.lastIndexOf('/') + 1); initialName = target.mid(target.lastIndexOf('/') + 1);
} else { } else {
QString subject; const Result<QString> res =
gitClient().synchronousLog(workingDirectory, {"-n", "1", "--format=%s", target}, gitClient().synchronousLog(workingDirectory,
&subject, nullptr, RunFlags::NoOutput); {"-n", "1", "--format=%s", target},
initialName = subject.trimmed(); RunFlags::NoOutput);
if (res)
initialName = res.value().trimmed();
else
VcsOutputWindow::appendError(res.error());
} }
QString suggestedName = initialName; QString suggestedName = initialName;
int i = 2; int i = 2;

View File

@@ -177,9 +177,9 @@ public:
void removeStaleRemoteBranches(const Utils::FilePath &workingDirectory, const QString &remote); void removeStaleRemoteBranches(const Utils::FilePath &workingDirectory, const QString &remote);
void recoverDeletedFiles(const Utils::FilePath &workingDirectory); void recoverDeletedFiles(const Utils::FilePath &workingDirectory);
void addFile(const Utils::FilePath &workingDirectory, const QString &fileName); void addFile(const Utils::FilePath &workingDirectory, const QString &fileName);
bool synchronousLog(const Utils::FilePath &workingDirectory, const QStringList &arguments, Utils::Result<QString> synchronousLog(const Utils::FilePath &workingDirectory,
QString *output, QString *errorMessage = nullptr, const QStringList &arguments,
VcsBase::RunFlags flags = VcsBase::RunFlags::None); VcsBase::RunFlags flags = VcsBase::RunFlags::None);
bool synchronousAdd(const Utils::FilePath &workingDirectory, const QStringList &files, bool synchronousAdd(const Utils::FilePath &workingDirectory, const QStringList &files,
const QStringList &extraOptions = {}); const QStringList &extraOptions = {});
bool synchronousDelete(const Utils::FilePath &workingDirectory, bool synchronousDelete(const Utils::FilePath &workingDirectory,

View File

@@ -167,12 +167,14 @@ bool LogChangeWidget::populateLog(const FilePath &repository, const QString &com
arguments << "--not" << remotesFlag; arguments << "--not" << remotesFlag;
} }
arguments << "--"; arguments << "--";
QString output;
if (!gitClient().synchronousLog( const Result<QString> res = gitClient().synchronousLog(repository, arguments, RunFlags::NoOutput);
repository, arguments, &output, nullptr, RunFlags::NoOutput)) { if (!res) {
VcsOutputWindow::appendError(res.error());
return false; return false;
} }
const QStringList lines = output.split('\n');
const QStringList lines = res.value().split('\n');
for (const QString &line : lines) { for (const QString &line : lines) {
const int colonPos = line.indexOf(':'); const int colonPos = line.indexOf(':');
if (colonPos != -1) { if (colonPos != -1) {