Bug 375849 - Show annotate on current document doesn't work in a git worktree
Summary: Show annotate on current document doesn't work in a git worktree
Status: RESOLVED FIXED
Alias: None
Product: kdevelop
Classification: Applications
Component: VCS: Git (show other bugs)
Version: 5.0.3
Platform: Fedora RPMs Linux
: NOR normal
Target Milestone: ---
Assignee: kdevelop-bugs-null
URL:
Keywords: junior-jobs
Depends on:
Blocks:
 
Reported: 2017-02-01 16:28 UTC by Thibault North
Modified: 2017-03-13 14:27 UTC (History)
1 user (show)

See Also:
Latest Commit:
Version Fixed In: 5.2.0


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Thibault North 2017-02-01 16:28:44 UTC
The feature "show annotate" that displays the equivalent of the git blame function works in main git repositories (where the .git/ folder lies), but not in the git worktrees (see https://git-scm.com/docs/git-worktree ).
In worktrees, a .git file exists (no .git folder), pointing to the main git folder.
It contains "gitdir: /path/to/the/main/repo".

Kdevelop is not able to follow this link to show annotations.
When calling for annotations, it fails with the message: "Could not annotate the document because it is not part of a version-controlled project."
Comment 1 Kevin Funk 2017-02-14 09:11:45 UTC
Interesting issue. 

Maybe you want to have a look at it yourself and fix the issue in KDevelop? The Git plugin is pretty much self-contained and easy to work with.

To get started:
  https://www.kdevelop.org/contribute-kdevelop
Comment 2 Sven Brauch 2017-02-14 20:50:54 UTC
The place where this needs to be fixed I think is plugins/git/gitplugin.cpp:QUrl GitPlugin::repositoryRoot(const QUrl& path).
Comment 3 Thibault North 2017-02-15 09:11:44 UTC
Thanks for all the pointers.
Since it's not trivial to prepare a dev environment for kdevelop *and* work at the same time ("You have to remove all KDevelop packages provided by your distribution"), I wasn't able to build/check a fix yet, but I came up with the following that might work (or not?):

diff --git a/plugins/git/gitplugin.cpp b/plugins/git/gitplugin.cpp
index b56d53d..b78e6bd 100644
--- a/plugins/git/gitplugin.cpp
+++ b/plugins/git/gitplugin.cpp
@@ -88,7 +88,18 @@ QDir dotGitDirectory(const QUrl& dirPath)
     if (dir.isRoot()) {
         qCWarning(PLUGIN_GIT) << "couldn't find the git root for" << dirPath;
     }
-
+    // is this a worktree ?
+    QFile dotGitPotentialFile(dir.filePath(".git"));
+    if (!dotGitPotentialFile.exists()) {
+        if (dotGitPotentialFile.open(QFile::ReadOnly)) {
+            QTextStream content(&dotGitPotentialFile);
+            QString gitWorktreeFileContent;
+            content >> gitWorktreeFileContent;
+            const auto items = gitWorktreeFileContent.splitRef(' ', QString::SkipEmptyParts);
+            if (items.size() == 2 && items.at(0) == "gitdir:")
+                return QDir(items.at(1));
+        }
+    }
     return dir;
 }

(Again: I don't even know if this compiles - and maybe I'm totally off :))
Comments/feedback appreciated.
Comment 4 Sven Brauch 2017-02-15 20:04:23 UTC
Seems a bit strange, first you check whether the file does not exist, then you open it? ;)
But otherwise looks good, it'd be great if you could test it and put it on phabricator.kde.org.

Having a development setup is actually quite simple, you can either just use the KDevelop you work on for working as well (that works fine with --pick-session) or use the AppImage we provide on kdevelop.org/download.
Comment 5 Thibault North 2017-02-17 15:33:45 UTC
Hu, right, that what happens when one edits the diff... :)

Ok. I'll be off for two weeks, I'll come back to this bug ASAP.
Comment 6 Thibault North 2017-03-11 13:55:21 UTC
Turns out only the function isValidDirectory() needs fixing.

A request is now available on Phabricator here:
https://phabricator.kde.org/D5014
Comment 7 Sven Brauch 2017-03-11 14:52:17 UTC
Git commit a200280a9b5b1d2ab5e9a98863db7eeaa9373174 by Sven Brauch, on behalf of Thibault North.
Committed on 11/03/2017 at 14:49.
Pushed by brauch into branch 'master'.

Add support for git worktrees

This patch adds support for the git worktree feature
(see https://git-scm.com/docs/git-worktree).
In a worktree, there is no .git/ folder, rather a .git
file which contains a path to the corresponding worktree
within the .git folder of the repository.

Differential Revision: https://phabricator.kde.org/D5014

M  +18   -1    plugins/git/gitplugin.cpp

https://commits.kde.org/kdevplatform/a200280a9b5b1d2ab5e9a98863db7eeaa9373174
Comment 8 Kevin Funk 2017-03-12 20:46:53 UTC
Thanks Thibault!
Comment 9 Thibault North 2017-03-13 14:27:37 UTC
Thanks for the guidance and fast integration!