aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/git/gitclient.cpp
diff options
context:
space:
mode:
authorOrgad Shaneh <[email protected]>2020-09-25 17:24:51 +0300
committerOrgad Shaneh <[email protected]>2020-09-29 07:09:18 +0000
commiteb6a2f2b895c2a946815755eddf543a6d3755126 (patch)
tree9852e7e73c992c24432bd3bce7e6d0d8d54feb82 /src/plugins/git/gitclient.cpp
parent91d3800ec39cd1c22417fa32673a800367e64422 (diff)
Git: Enable Show for file in a specified revision
Fixes: QTCREATORBUG-24689 Change-Id: Ic4ceb1c59f39009c28be7f34ee62f65fd41506c1 Reviewed-by: AndrĂ© Hartmann <[email protected]>
Diffstat (limited to 'src/plugins/git/gitclient.cpp')
-rw-r--r--src/plugins/git/gitclient.cpp49
1 files changed, 48 insertions, 1 deletions
diff --git a/src/plugins/git/gitclient.cpp b/src/plugins/git/gitclient.cpp
index 5014f9afe6e..2352c81f126 100644
--- a/src/plugins/git/gitclient.cpp
+++ b/src/plugins/git/gitclient.cpp
@@ -3714,9 +3714,10 @@ QString GitClient::suggestedLocalBranchName(
return suggestedName;
}
-void GitClient::addChangeActions(QMenu *menu, const QString &workingDir, const QString &change)
+void GitClient::addChangeActions(QMenu *menu, const QString &source, const QString &change)
{
QTC_ASSERT(!change.isEmpty(), return);
+ const QString &workingDir = fileWorkingDirectory(source);
menu->addAction(tr("Cherr&y-Pick Change %1").arg(change), [workingDir, change] {
m_instance->synchronousCherryPick(workingDir, change);
});
@@ -3733,6 +3734,13 @@ void GitClient::addChangeActions(QMenu *menu, const QString &workingDir, const Q
QAction *logAction = menu->addAction(tr("&Log for Change %1").arg(change), [workingDir, change] {
m_instance->log(workingDir, QString(), false, {change});
});
+ const FilePath filePath = FilePath::fromString(source);
+ if (!filePath.isDir()) {
+ menu->addAction(tr("Sh&ow file \"%1\" on revision %2").arg(filePath.fileName()).arg(change),
+ [workingDir, change, source] {
+ m_instance->openShowEditor(workingDir, change, source);
+ });
+ }
if (change.contains(".."))
menu->setDefaultAction(logAction);
menu->addAction(tr("Add &Tag for Change %1...").arg(change), [workingDir, change] {
@@ -3781,6 +3789,45 @@ void GitClient::addChangeActions(QMenu *menu, const QString &workingDir, const Q
});
}
+QString GitClient::fileWorkingDirectory(const QString &file)
+{
+ Utils::FilePath path = Utils::FilePath::fromString(file);
+ if (!path.isEmpty() && !path.isDir())
+ path = path.parentDir();
+ while (!path.isEmpty() && !path.exists())
+ path = path.parentDir();
+ return path.toString();
+}
+
+IEditor *GitClient::openShowEditor(const QString &workingDirectory, const QString &ref,
+ const QString &path, ShowEditor showSetting)
+{
+ QString topLevel;
+ VcsManager::findVersionControlForDirectory(workingDirectory, &topLevel);
+ const QString relativePath = QDir(topLevel).relativeFilePath(path);
+ const QByteArray content = synchronousShow(topLevel, ref + ":" + relativePath);
+ if (showSetting == ShowEditor::OnlyIfDifferent) {
+ if (content.isEmpty())
+ return nullptr;
+ QByteArray fileContent;
+ if (TextFileFormat::readFileUTF8(path, nullptr, &fileContent, nullptr)
+ == TextFileFormat::ReadSuccess) {
+ if (fileContent == content)
+ return nullptr; // open the file for read/write
+ }
+ }
+
+ const QString documentId = QLatin1String(Git::Constants::GIT_PLUGIN)
+ + QLatin1String(".GitShow.") + topLevel
+ + QLatin1String(".") + relativePath;
+ QString title = tr("Git Show %1:%2").arg(ref).arg(relativePath);
+ IEditor *editor = EditorManager::openEditorWithContents(Id(), &title, content, documentId,
+ EditorManager::DoNotSwitchToDesignMode);
+ editor->document()->setTemporary(true);
+ VcsBase::setSource(editor->document(), path);
+ return editor;
+}
+
} // namespace Internal
} // namespace Git