From 6d97c0ecc1d731dd1a54aa21c34659bb9b91c6ef Mon Sep 17 00:00:00 2001
From: Orgad Shaneh <orgads@gmail.com>
Date: Sun, 19 Feb 2012 21:23:41 +0200
Subject: [PATCH] Git: Enable 'Show' for files, not only commits

Change-Id: I0f49d3a26c801af84bb578478bd4778356507cf6
Reviewed-by: Tobias Hunger <tobias.hunger@nokia.com>
---
 src/plugins/git/changeselectiondialog.cpp | 40 ++++++++++-------------
 src/plugins/git/changeselectiondialog.h   |  4 +--
 src/plugins/git/changeselectiondialog.ui  |  6 ++--
 src/plugins/git/gitplugin.cpp             | 10 +++---
 4 files changed, 28 insertions(+), 32 deletions(-)

diff --git a/src/plugins/git/changeselectiondialog.cpp b/src/plugins/git/changeselectiondialog.cpp
index 62c4c35a102..e137f99b2ff 100644
--- a/src/plugins/git/changeselectiondialog.cpp
+++ b/src/plugins/git/changeselectiondialog.cpp
@@ -31,6 +31,8 @@
 **************************************************************************/
 
 #include "changeselectiondialog.h"
+#include "gitplugin.h"
+#include "gitclient.h"
 
 #include <QFileDialog>
 #include <QMessageBox>
@@ -43,7 +45,7 @@ ChangeSelectionDialog::ChangeSelectionDialog(QWidget *parent)
 {
     m_ui.setupUi(this);
     setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint);
-    connect(m_ui.repositoryButton, SIGNAL(clicked()), this, SLOT(selectWorkingDirectory()));
+    connect(m_ui.workingDirectoryButton, SIGNAL(clicked()), this, SLOT(selectWorkingDirectory()));
     setWindowTitle(tr("Select a Git Commit"));
 }
 
@@ -52,14 +54,14 @@ QString ChangeSelectionDialog::change() const
     return m_ui.changeNumberEdit->text();
 }
 
-QString ChangeSelectionDialog::repository() const
+QString ChangeSelectionDialog::workingDirectory() const
 {
-    return m_ui.repositoryEdit->text();
+    return m_ui.workingDirectoryEdit->text();
 }
 
-void ChangeSelectionDialog::setRepository(const QString &s)
+void ChangeSelectionDialog::setWorkingDirectory(const QString &s)
 {
-    m_ui.repositoryEdit->setText(QDir::toNativeSeparators(s));
+    m_ui.workingDirectoryEdit->setText(QDir::toNativeSeparators(s));
     m_ui.changeNumberEdit->setFocus(Qt::ActiveWindowFocusReason);
     m_ui.changeNumberEdit->setText(QLatin1String("HEAD"));
     m_ui.changeNumberEdit->selectAll();
@@ -67,28 +69,20 @@ void ChangeSelectionDialog::setRepository(const QString &s)
 
 void ChangeSelectionDialog::selectWorkingDirectory()
 {
-    static QString location;
-    location = QFileDialog::getExistingDirectory(this,
-                                      tr("Select Git Repository"),
-                                      location);
+    QString location = QFileDialog::getExistingDirectory(this, tr("Select Working Directory"),
+                                                         m_ui.workingDirectoryEdit->text());
     if (location.isEmpty())
         return;
 
     // Verify that the location is a repository
-    // We are polite, we also allow to specify a directory, which is not
-    // the head directory of the repository.
-    QDir repository(location);
-    do {
-        if (repository.entryList(QDir::AllDirs|QDir::Hidden).contains(QLatin1String(".git"))) {
-            m_ui.repositoryEdit->setText(repository.absolutePath());
-            return;
-        }
-    } while (repository.cdUp());
-
-    // Did not find a repo
-    QMessageBox::critical(this, tr("Error"),
-                          tr("Selected directory is not a Git repository"));
-
+    // We allow specifying a directory, which is not the head directory of the repository.
+    // This is useful for git show commit:./file
+    QString topLevel = GitPlugin::instance()->gitClient()->findRepositoryForDirectory(location);
+    if (!topLevel.isEmpty())
+        m_ui.workingDirectoryEdit->setText(location);
+    else // Did not find a repo
+        QMessageBox::critical(this, tr("Error"),
+                              tr("Selected directory is not a Git repository"));
 }
 
 }
diff --git a/src/plugins/git/changeselectiondialog.h b/src/plugins/git/changeselectiondialog.h
index 0020cdc7248..f77274edf74 100644
--- a/src/plugins/git/changeselectiondialog.h
+++ b/src/plugins/git/changeselectiondialog.h
@@ -48,8 +48,8 @@ public:
 
     QString change() const;
 
-    QString repository() const;
-    void setRepository(const QString &s);
+    QString workingDirectory() const;
+    void setWorkingDirectory(const QString &s);
 
 public slots:
     void selectWorkingDirectory();
diff --git a/src/plugins/git/changeselectiondialog.ui b/src/plugins/git/changeselectiondialog.ui
index 1b6180cad29..7cc88ec38b6 100644
--- a/src/plugins/git/changeselectiondialog.ui
+++ b/src/plugins/git/changeselectiondialog.ui
@@ -17,15 +17,15 @@
    <item row="0" column="0">
     <widget class="QLabel" name="label">
      <property name="text">
-      <string>Repository location:</string>
+      <string>Working directory:</string>
      </property>
     </widget>
    </item>
    <item row="0" column="1">
-    <widget class="QLineEdit" name="repositoryEdit"/>
+    <widget class="QLineEdit" name="workingDirectoryEdit"/>
    </item>
    <item row="0" column="2">
-    <widget class="QPushButton" name="repositoryButton">
+    <widget class="QPushButton" name="workingDirectoryButton">
      <property name="text">
       <string>Select</string>
      </property>
diff --git a/src/plugins/git/gitplugin.cpp b/src/plugins/git/gitplugin.cpp
index bd11886cabd..6c8c557e9ba 100644
--- a/src/plugins/git/gitplugin.cpp
+++ b/src/plugins/git/gitplugin.cpp
@@ -438,7 +438,7 @@ bool GitPlugin::initialize(const QStringList &arguments, QString *errorMessage)
                            tr("Remotes..."), Core::Id("Git.RemoteList"),
                            globalcontext, false, SLOT(remoteList()));
 
-    m_showAction = new QAction(tr("Show Commit..."), this);
+    m_showAction = new QAction(tr("Show..."), this);
     Core::Command *showCommitCommand = actionManager->registerAction(m_showAction, "Git.ShowCommit", globalcontext);
     connect(m_showAction, SIGNAL(triggered()), this, SLOT(showCommit()));
     gitContainer->addAction(showCommitCommand);
@@ -1038,8 +1038,10 @@ void GitPlugin::showCommit()
     if (!m_changeSelectionDialog)
         m_changeSelectionDialog = new ChangeSelectionDialog();
 
-    if (state.hasTopLevel())
-        m_changeSelectionDialog->setRepository(state.topLevel());
+    if (state.hasFile())
+        m_changeSelectionDialog->setWorkingDirectory(state.currentFileDirectory());
+    else if (state.hasTopLevel())
+        m_changeSelectionDialog->setWorkingDirectory(state.topLevel());
 
     if (m_changeSelectionDialog->exec() != QDialog::Accepted)
         return;
@@ -1047,7 +1049,7 @@ void GitPlugin::showCommit()
     if (change.isEmpty())
         return;
 
-    m_gitClient->show(m_changeSelectionDialog->repository(), change);
+    m_gitClient->show(m_changeSelectionDialog->workingDirectory(), change);
 }
 
 const GitSettings &GitPlugin::settings() const
-- 
GitLab