[geany/geany-plugins] 187dfc: GeanyVC:(Git) Fallback to `git rev-parse` for getting the repo/worktree's top directory

nomadbyte git-noreply at geany.org
Tue Apr 26 02:38:49 UTC 2022


Branch:      refs/heads/master
Author:      nomadbyte <nomadbyte at users.noreply.github.com>
Committer:   nomadbyte <nomadbyte at users.noreply.github.com>
Date:        Tue, 26 Apr 2022 02:38:49 UTC
Commit:      187dfc2aa55de6e52688d88cf8a505f1d47173cc
             https://github.com/geany/geany-plugins/commit/187dfc2aa55de6e52688d88cf8a505f1d47173cc

Log Message:
-----------
GeanyVC:(Git) Fallback to `git rev-parse` for getting the repo/worktree's top directory

An example setup for worktree:

```
mkdir r1
cd r1

git init
git config user.name testuser
git config user.email testuser@
echo "f1" >> f1
git add f1
git commit -a -m "added f1"

git worktree add ../w1 -b work1
echo "f1:r1" >> f1
git status

cd ../w1
git status
echo "f2" >> f2
git add f2
git commit -a -m "added f2"
echo "f1:w1" >> f1
git status
```

All VC actions should be available for all files in each worktree.


Modified Paths:
--------------
    geanyvc/src/vc_git.c

Modified: geanyvc/src/vc_git.c
44 lines changed, 39 insertions(+), 5 deletions(-)
===================================================================
@@ -28,7 +28,37 @@ extern GeanyData *geany_data;
 static gchar *
 get_base_dir(const gchar * path)
 {
-	return find_subdir_path(path, ".git");
+	gchar *base_dir = NULL;
+	const gchar *argv[] = { "git", "rev-parse", "--show-toplevel", NULL };
+	gchar *dir = NULL;
+	gchar *filename = NULL;
+	gchar *std_out = NULL;
+	gchar *std_err = NULL;
+
+	base_dir = find_subdir_path(path, ".git");
+	if (base_dir) return base_dir;
+
+	if (g_file_test(path, G_FILE_TEST_IS_DIR))
+		dir = g_strdup(path);
+	else
+		dir = g_path_get_dirname(path);
+
+	execute_custom_command(dir, (const gchar **) argv, NULL, &std_out, &std_err,
+			       dir, NULL, NULL);
+	g_free(dir);
+	if (!std_out) return NULL;
+
+	/* trim the trailing newline */
+	sscanf(std_out, "%s\n", std_out);
+	dir = std_out;
+
+	filename = g_build_filename(dir, ".", NULL); /* in case of a trailing slash */
+	base_dir = g_path_get_dirname(filename);
+
+	g_free(filename);
+	g_free(dir);
+
+	return base_dir;
 }
 
 static gint
@@ -187,11 +217,15 @@ in_vc_git(const gchar * filename)
 	gboolean ret = FALSE;
 	gchar *std_output;
 
-	if (!find_dir(filename, ".git", TRUE))
-		return FALSE;
-
 	if (g_file_test(filename, G_FILE_TEST_IS_DIR))
+	{
+		gchar *base_dir = get_base_dir(filename);
+
+		if (!base_dir) return FALSE;
+
+		g_free(base_dir);
 		return TRUE;
+	}
 
 	dir = g_path_get_dirname(filename);
 	base_name = g_path_get_basename(filename);
@@ -254,7 +288,7 @@ get_commit_files_git(const gchar * file)
 	const gchar *argv[] = { "git", "status", NULL };
 	const gchar *env[] = { "PAGES=cat", NULL };
 	gchar *std_out = NULL;
-	gchar *base_dir = find_subdir_path(file, ".git");
+	gchar *base_dir = get_base_dir(file);
 	GSList *ret = NULL;
 
 	g_return_val_if_fail(base_dir, NULL);



--------------
This E-Mail was brought to you by github_commit_mail.py (Source: https://github.com/geany/infrastructure).


More information about the Plugins-Commits mailing list