Skip to contentSkip to conversation

difftool: eliminate use of global variables

PATCH In review David Aguilar 4 participants 13 messages Feb 6, 2025 – Feb 7, 2025

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/3 difftool: eliminate use of global variables

Move difftool's global variables into a difftools_option struct in preparation for removal of USE_THE_REPOSITORY_VARIABLE.

---
 builtin/difftool.c | 51 ++++++++++++++++++++++++++++++----------------
 1 file changed, 33 insertions(+), 18 deletions(-)
builtin/difftool.c +33 -18
index 03a8bb92a9..0b6b92aee0 100644
@@ -36,18 +36,27 @@
 #include "entry.h"
 #include "setup.h"
 
-static int trust_exit_code;
-
 static const char *const builtin_difftool_usage[] = {
 	N_("git difftool [<options>] [<commit> [<commit>]] [--] [<path>...]"),
 	NULL
 };
 
+struct difftool_options {
+	int has_symlinks;
+	int symlinks;
+	int trust_exit_code;
+};
+
 static int difftool_config(const char *var, const char *value,
 			   const struct config_context *ctx, void *cb)
 {
+	struct difftool_options *dt_options = (struct difftool_options *)cb;
 	if (!strcmp(var, "difftool.trustexitcode")) {
-		trust_exit_code = git_config_bool(var, value);
+		dt_options->trust_exit_code = git_config_bool(var, value);
+		return 0;
+	}
+	if (!strcmp(var, "core.symlinks")) {
+		dt_options->has_symlinks = git_config_bool(var, value);
 		return 0;
 	}
 
@@ -291,13 +300,14 @@ static int ensure_leading_directories(char *path)
  * to compare the readlink(2) result as text, even on a filesystem that is
  * capable of doing a symbolic link.
  */
-static char *get_symlink(const struct object_id *oid, const char *path)
+static char *get_symlink(struct difftool_options *dt_options,
+			 const struct object_id *oid, const char *path)
 {
 	char *data;
 	if (is_null_oid(oid)) {
 		/* The symlink is unknown to Git so read from the filesystem */
 		struct strbuf link = STRBUF_INIT;
-		if (has_symlinks) {
+		if (dt_options->has_symlinks) {
 			if (strbuf_readlink(&link, path, strlen(path)))
 				die(_("could not read symlink %s"), path);
 		} else if (strbuf_read_file(&link, path, 128))
@@ -355,7 +365,8 @@ static void write_standin_files(struct pair_entry *entry,
 		write_file_in_directory(rdir, rdir_len, entry->path, entry->right);
 }
 
-static int run_dir_diff(const char *extcmd, int symlinks, const char *prefix,
+static int run_dir_diff(struct difftool_options *dt_options,
+			const char *extcmd, const char *prefix,
 			struct child_process *child)
 {
 	struct strbuf info = STRBUF_INIT, lpath = STRBUF_INIT;
@@ -469,13 +480,13 @@ static int run_dir_diff(const char *extcmd, int symlinks, const char *prefix,
 		}
 
 		if (S_ISLNK(lmode)) {
-			char *content = get_symlink(&loid, src_path);
+			char *content = get_symlink(dt_options, &loid, src_path);
 			add_left_or_right(&symlinks2, src_path, content, 0);
 			free(content);
 		}
 
 		if (S_ISLNK(rmode)) {
-			char *content = get_symlink(&roid, dst_path);
+			char *content = get_symlink(dt_options, &roid, dst_path);
 			add_left_or_right(&symlinks2, dst_path, content, 1);
 			free(content);
 		}
@@ -528,7 +539,7 @@ static int run_dir_diff(const char *extcmd, int symlinks, const char *prefix,
 					goto finish;
 				}
 				add_path(&wtdir, wtdir_len, dst_path);
-				if (symlinks) {
+				if (dt_options->symlinks) {
 					if (symlink(wtdir.buf, rdir.buf)) {
 						ret = error_errno("could not symlink '%s' to '%s'", wtdir.buf, rdir.buf);
 						goto finish;
@@ -614,7 +625,7 @@ static int run_dir_diff(const char *extcmd, int symlinks, const char *prefix,
 		if (lstat(rdir.buf, &st))
 			continue;
 
-		if ((symlinks && S_ISLNK(st.st_mode)) || !S_ISREG(st.st_mode))
+		if ((dt_options->symlinks && S_ISLNK(st.st_mode)) || !S_ISREG(st.st_mode))
 			continue;
 
 		if (!indices_loaded) {
@@ -704,9 +715,13 @@ int cmd_difftool(int argc,
 		 const char *prefix,
 		 struct repository *repo UNUSED)
 {
-	int use_gui_tool = -1, dir_diff = 0, prompt = -1, symlinks = 0,
-	    tool_help = 0, no_index = 0;
+	int use_gui_tool = -1, dir_diff = 0, prompt = -1, tool_help = 0, no_index = 0;
 	static char *difftool_cmd = NULL, *extcmd = NULL;
+	struct difftool_options dt_options = {
+		.has_symlinks = 1,
+		.symlinks = 1,
+		.trust_exit_code = 0
+	};
 	struct option builtin_difftool_options[] = {
 		OPT_BOOL('g', "gui", &use_gui_tool,
 			 N_("use `diff.guitool` instead of `diff.tool`")),
@@ -717,14 +732,14 @@ int cmd_difftool(int argc,
 			0, PARSE_OPT_NONEG),
 		OPT_SET_INT_F(0, "prompt", &prompt, NULL,
 			1, PARSE_OPT_NONEG | PARSE_OPT_HIDDEN),
-		OPT_BOOL(0, "symlinks", &symlinks,
+		OPT_BOOL(0, "symlinks", &dt_options.symlinks,
 			 N_("use symlinks in dir-diff mode")),
 		OPT_STRING('t', "tool", &difftool_cmd, N_("tool"),
 			   N_("use the specified diff tool")),
 		OPT_BOOL(0, "tool-help", &tool_help,
 			 N_("print a list of diff tools that may be used with "
 			    "`--tool`")),
-		OPT_BOOL(0, "trust-exit-code", &trust_exit_code,
+		OPT_BOOL(0, "trust-exit-code", &dt_options.trust_exit_code,
 			 N_("make 'git-difftool' exit when an invoked diff "
 			    "tool returns a non-zero exit code")),
 		OPT_STRING('x', "extcmd", &extcmd, N_("command"),
@@ -734,8 +749,8 @@ int cmd_difftool(int argc,
 	};
 	struct child_process child = CHILD_PROCESS_INIT;
 
-	git_config(difftool_config, NULL);
-	symlinks = has_symlinks;
+	git_config(difftool_config, &dt_options);
+	dt_options.symlinks = dt_options.has_symlinks;
 
 	argc = parse_options(argc, argv, prefix, builtin_difftool_options,
 			     builtin_difftool_usage, PARSE_OPT_KEEP_UNKNOWN_OPT |
@@ -783,7 +798,7 @@ int cmd_difftool(int argc,
 	}
 
 	setenv("GIT_DIFFTOOL_TRUST_EXIT_CODE",
-	       trust_exit_code ? "true" : "false", 1);
+	       dt_options.trust_exit_code ? "true" : "false", 1);
 
 	/*
 	 * In directory diff mode, 'git-difftool--helper' is called once
@@ -799,6 +814,6 @@ int cmd_difftool(int argc,
 	strvec_pushv(&child.args, argv);
 
 	if (dir_diff)
-		return run_dir_diff(extcmd, symlinks, prefix, &child);
+		return run_dir_diff(&dt_options, extcmd, prefix, &child);
 	return run_file_diff(prompt, prefix, &child);
 }
Signature · 1 line
2.48.1.461.g612e419e04
#1 lore raw

PATCH v2 2/3 difftool: eliminate use of the_repository

Make callers pass a repository struct into each function instead of relying on the global the_repository variable.

---
 builtin/difftool.c | 54 +++++++++++++++++++++++++---------------------
 1 file changed, 29 insertions(+), 25 deletions(-)
builtin/difftool.c +29 -25
index 0b6b92aee0..81d733dfdf 100644
@@ -72,7 +72,8 @@ static int print_tool_help(void)
 	return run_command(&cmd);
 }
 
-static int parse_index_info(char *p, int *mode1, int *mode2,
+static int parse_index_info(struct repository *repo,
+			    char *p, int *mode1, int *mode2,
 			    struct object_id *oid1, struct object_id *oid2,
 			    char *status)
 {
@@ -84,11 +85,11 @@ static int parse_index_info(char *p, int *mode1, int *mode2,
 	*mode2 = (int)strtol(p + 1, &p, 8);
 	if (*p != ' ')
 		return error("expected ' ', got '%c'", *p);
-	if (parse_oid_hex(++p, oid1, (const char **)&p))
+	if (parse_oid_hex_algop(++p, oid1, (const char **)&p, repo->hash_algo))
 		return error("expected object ID, got '%s'", p);
 	if (*p != ' ')
 		return error("expected ' ', got '%c'", *p);
-	if (parse_oid_hex(++p, oid2, (const char **)&p))
+	if (parse_oid_hex_algop(++p, oid2, (const char **)&p, repo->hash_algo))
 		return error("expected object ID, got '%s'", p);
 	if (*p != ' ')
 		return error("expected ' ', got '%c'", *p);
@@ -115,7 +116,8 @@ static void add_path(struct strbuf *buf, size_t base_len, const char *path)
 /*
  * Determine whether we can simply reuse the file in the worktree.
  */
-static int use_wt_file(const char *workdir, const char *name,
+static int use_wt_file(struct repository *repo,
+		       const char *workdir, const char *name,
 		       struct object_id *oid)
 {
 	struct strbuf buf = STRBUF_INIT;
@@ -130,7 +132,7 @@ static int use_wt_file(const char *workdir, const char *name,
 		int fd = open(buf.buf, O_RDONLY);
 
 		if (fd >= 0 &&
-		    !index_fd(the_repository->index, &wt_oid, fd, &st, OBJ_BLOB, name, 0)) {
+		    !index_fd(repo->index, &wt_oid, fd, &st, OBJ_BLOB, name, 0)) {
 			if (is_null_oid(oid)) {
 				oidcpy(oid, &wt_oid);
 				use = 1;
@@ -221,13 +223,14 @@ static int path_entry_cmp(const void *cmp_data UNUSED,
 	return strcmp(a->path, key ? key : b->path);
 }
 
-static void changed_files(struct hashmap *result, const char *index_path,
+static void changed_files(struct repository *repo,
+			  struct hashmap *result, const char *index_path,
 			  const char *workdir)
 {
 	struct child_process update_index = CHILD_PROCESS_INIT;
 	struct child_process diff_files = CHILD_PROCESS_INIT;
 	struct strbuf buf = STRBUF_INIT;
-	const char *git_dir = absolute_path(repo_get_git_dir(the_repository));
+	const char *git_dir = absolute_path(repo_get_git_dir(repo));
 	FILE *fp;
 
 	strvec_pushl(&update_index.args,
@@ -300,7 +303,8 @@ static int ensure_leading_directories(char *path)
  * to compare the readlink(2) result as text, even on a filesystem that is
  * capable of doing a symbolic link.
  */
-static char *get_symlink(struct difftool_options *dt_options,
+static char *get_symlink(struct repository *repo,
+			 struct difftool_options *dt_options,
 			 const struct object_id *oid, const char *path)
 {
 	char *data;
@@ -317,8 +321,7 @@ static char *get_symlink(struct difftool_options *dt_options,
 	} else {
 		enum object_type type;
 		unsigned long size;
-		data = repo_read_object_file(the_repository, oid, &type,
-					     &size);
+		data = repo_read_object_file(repo, oid, &type, &size);
 		if (!data)
 			die(_("could not read object %s for symlink %s"),
 				oid_to_hex(oid), path);
@@ -365,7 +368,8 @@ static void write_standin_files(struct pair_entry *entry,
 		write_file_in_directory(rdir, rdir_len, entry->path, entry->right);
 }
 
-static int run_dir_diff(struct difftool_options *dt_options,
+static int run_dir_diff(struct repository *repo,
+			struct difftool_options *dt_options,
 			const char *extcmd, const char *prefix,
 			struct child_process *child)
 {
@@ -386,7 +390,7 @@ static int run_dir_diff(struct difftool_options *dt_options,
 	struct hashmap symlinks2 = HASHMAP_INIT(pair_cmp, NULL);
 	struct hashmap_iter iter;
 	struct pair_entry *entry;
-	struct index_state wtindex = INDEX_STATE_INIT(the_repository);
+	struct index_state wtindex = INDEX_STATE_INIT(repo);
 	struct checkout lstate, rstate;
 	int err = 0;
 	struct child_process cmd = CHILD_PROCESS_INIT;
@@ -394,7 +398,7 @@ static int run_dir_diff(struct difftool_options *dt_options,
 	struct hashmap tmp_modified = HASHMAP_INIT(path_entry_cmp, NULL);
 	int indices_loaded = 0;
 
-	workdir = repo_get_work_tree(the_repository);
+	workdir = repo_get_work_tree(repo);
 
 	/* Setup temp directories */
 	tmp = getenv("TMPDIR");
@@ -449,8 +453,7 @@ static int run_dir_diff(struct difftool_options *dt_options,
 			       "not supported in\n"
 			       "directory diff mode ('-d' and '--dir-diff')."));
 
-		if (parse_index_info(info.buf, &lmode, &rmode, &loid, &roid,
-				     &status))
+		if (parse_index_info(repo, info.buf, &lmode, &rmode, &loid, &roid, &status))
 			break;
 		if (strbuf_getline_nul(&lpath, fp))
 			break;
@@ -480,13 +483,13 @@ static int run_dir_diff(struct difftool_options *dt_options,
 		}
 
 		if (S_ISLNK(lmode)) {
-			char *content = get_symlink(dt_options, &loid, src_path);
+			char *content = get_symlink(repo, dt_options, &loid, src_path);
 			add_left_or_right(&symlinks2, src_path, content, 0);
 			free(content);
 		}
 
 		if (S_ISLNK(rmode)) {
-			char *content = get_symlink(dt_options, &roid, dst_path);
+			char *content = get_symlink(repo, dt_options, &roid, dst_path);
 			add_left_or_right(&symlinks2, dst_path, content, 1);
 			free(content);
 		}
@@ -511,7 +514,7 @@ static int run_dir_diff(struct difftool_options *dt_options,
 			}
 			hashmap_add(&working_tree_dups, &entry->entry);
 
-			if (!use_wt_file(workdir, dst_path, &roid)) {
+			if (!use_wt_file(repo, workdir, dst_path, &roid)) {
 				if (checkout_path(rmode, &roid, dst_path,
 						  &rstate)) {
 					ret = error("could not write '%s'",
@@ -637,9 +640,9 @@ static int run_dir_diff(struct difftool_options *dt_options,
 				ret = error("could not write %s", buf.buf);
 				goto finish;
 			}
-			changed_files(&wt_modified, buf.buf, workdir);
+			changed_files(repo, &wt_modified, buf.buf, workdir);
 			strbuf_setlen(&rdir, rdir_len);
-			changed_files(&tmp_modified, buf.buf, rdir.buf);
+			changed_files(repo, &tmp_modified, buf.buf, rdir.buf);
 			add_path(&rdir, rdir_len, name);
 			indices_loaded = 1;
 		}
@@ -713,7 +716,7 @@ static int run_file_diff(int prompt, const char *prefix,
 int cmd_difftool(int argc,
 		 const char **argv,
 		 const char *prefix,
-		 struct repository *repo UNUSED)
+		 struct repository *repo)
 {
 	int use_gui_tool = -1, dir_diff = 0, prompt = -1, tool_help = 0, no_index = 0;
 	static char *difftool_cmd = NULL, *extcmd = NULL;
@@ -749,7 +752,8 @@ int cmd_difftool(int argc,
 	};
 	struct child_process child = CHILD_PROCESS_INIT;
 
-	git_config(difftool_config, &dt_options);
+	if (repo)
+		repo_config(repo, difftool_config, &dt_options);
 	dt_options.symlinks = dt_options.has_symlinks;
 
 	argc = parse_options(argc, argv, prefix, builtin_difftool_options,
@@ -764,8 +768,8 @@ int cmd_difftool(int argc,
 
 	if (!no_index){
 		setup_work_tree();
-		setenv(GIT_DIR_ENVIRONMENT, absolute_path(repo_get_git_dir(the_repository)), 1);
-		setenv(GIT_WORK_TREE_ENVIRONMENT, absolute_path(repo_get_work_tree(the_repository)), 1);
+		setenv(GIT_DIR_ENVIRONMENT, absolute_path(repo_get_git_dir(repo)), 1);
+		setenv(GIT_WORK_TREE_ENVIRONMENT, absolute_path(repo_get_work_tree(repo)), 1);
 	} else if (dir_diff)
 		die(_("options '%s' and '%s' cannot be used together"), "--dir-diff", "--no-index");
 
@@ -814,6 +818,6 @@ int cmd_difftool(int argc,
 	strvec_pushv(&child.args, argv);
 
 	if (dir_diff)
-		return run_dir_diff(&dt_options, extcmd, prefix, &child);
+		return run_dir_diff(repo, &dt_options, extcmd, prefix, &child);
 	return run_file_diff(prompt, prefix, &child);
 }
Signature · 1 line
2.48.1.461.g612e419e04
#2 lore raw

Re: [PATCH v2 2/3] difftool: eliminate use of the_repository

Quoted 207 lines · David Aguilar

Make callers pass a repository struct into each function instead of relying on the global the_repository variable.

---
 builtin/difftool.c | 54 +++++++++++++++++++++++++---------------------
 1 file changed, 29 insertions(+), 25 deletions(-)
builtin/difftool.c +29 -26
index 0b6b92aee0..81d733dfdf 100644
@@ -72,7 +72,8 @@ static int print_tool_help(void)
        return run_command(&cmd);
 }
 
-static int parse_index_info(char *p, int *mode1, int *mode2,
+static int parse_index_info(struct repository *repo,
+                           char *p, int *mode1, int *mode2,
                            struct object_id *oid1, struct object_id *oid2,
                            char *status)
 {
@@ -84,11 +85,11 @@ static int parse_index_info(char *p, int *mode1, int *mode2,
        *mode2 = (int)strtol(p + 1, &p, 8);
        if (*p != ' ')
                return error("expected ' ', got '%c'", *p);
-       if (parse_oid_hex(++p, oid1, (const char **)&p))
+       if (parse_oid_hex_algop(++p, oid1, (const char **)&p, repo->hash_algo))
                return error("expected object ID, got '%s'", p);
        if (*p != ' ')
                return error("expected ' ', got '%c'", *p);
-       if (parse_oid_hex(++p, oid2, (const char **)&p))
+       if (parse_oid_hex_algop(++p, oid2, (const char **)&p, repo->hash_algo))
                return error("expected object ID, got '%s'", p);
        if (*p != ' ')
                return error("expected ' ', got '%c'", *p);
@@ -115,7 +116,8 @@ static void add_path(struct strbuf *buf, size_t base_len, const char *path)
 /*
  * Determine whether we can simply reuse the file in the worktree.
  */
-static int use_wt_file(const char *workdir, const char *name,
+static int use_wt_file(struct repository *repo,
+                      const char *workdir, const char *name,
                       struct object_id *oid)
 {
        struct strbuf buf = STRBUF_INIT;
@@ -130,7 +132,7 @@ static int use_wt_file(const char *workdir, const char *name,
                int fd = open(buf.buf, O_RDONLY);
 
                if (fd >= 0 &&
-                   !index_fd(the_repository->index, &wt_oid, fd, &st, OBJ_BLOB, name, 0)) {
+                   !index_fd(repo->index, &wt_oid, fd, &st, OBJ_BLOB, name, 0)) {
                        if (is_null_oid(oid)) {
                                oidcpy(oid, &wt_oid);
                                use = 1;
@@ -221,13 +223,14 @@ static int path_entry_cmp(const void *cmp_data UNUSED,
        return strcmp(a->path, key ? key : b->path);
 }
 
-static void changed_files(struct hashmap *result, const char *index_path,
+static void changed_files(struct repository *repo,
+                         struct hashmap *result, const char *index_path,
                          const char *workdir)
 {
        struct child_process update_index = CHILD_PROCESS_INIT;
        struct child_process diff_files = CHILD_PROCESS_INIT;
        struct strbuf buf = STRBUF_INIT;
-       const char *git_dir = absolute_path(repo_get_git_dir(the_repository));
+       const char *git_dir = absolute_path(repo_get_git_dir(repo));
        FILE *fp;
 
        strvec_pushl(&update_index.args,
@@ -300,7 +303,8 @@ static int ensure_leading_directories(char *path)
  * to compare the readlink(2) result as text, even on a filesystem that is
  * capable of doing a symbolic link.
  */
-static char *get_symlink(struct difftool_options *dt_options,
+static char *get_symlink(struct repository *repo,
+                        struct difftool_options *dt_options,
                         const struct object_id *oid, const char *path)
 {
        char *data;
@@ -317,8 +321,7 @@ static char *get_symlink(struct difftool_options *dt_options,
        } else {
                enum object_type type;
                unsigned long size;
-               data = repo_read_object_file(the_repository, oid, &type,
-                                            &size);
+               data = repo_read_object_file(repo, oid, &type, &size);
                if (!data)
                        die(_("could not read object %s for symlink %s"),
                                oid_to_hex(oid), path);
@@ -365,7 +368,8 @@ static void write_standin_files(struct pair_entry *entry,
                write_file_in_directory(rdir, rdir_len, entry->path, entry->right);
 }
 
-static int run_dir_diff(struct difftool_options *dt_options,
+static int run_dir_diff(struct repository *repo,
+                       struct difftool_options *dt_options,
                        const char *extcmd, const char *prefix,
                        struct child_process *child)
 {
@@ -386,7 +390,7 @@ static int run_dir_diff(struct difftool_options *dt_options,
        struct hashmap symlinks2 = HASHMAP_INIT(pair_cmp, NULL);
        struct hashmap_iter iter;
        struct pair_entry *entry;
-       struct index_state wtindex = INDEX_STATE_INIT(the_repository);
+       struct index_state wtindex = INDEX_STATE_INIT(repo);
        struct checkout lstate, rstate;
        int err = 0;
        struct child_process cmd = CHILD_PROCESS_INIT;
@@ -394,7 +398,7 @@ static int run_dir_diff(struct difftool_options *dt_options,
        struct hashmap tmp_modified = HASHMAP_INIT(path_entry_cmp, NULL);
        int indices_loaded = 0;
 
-       workdir = repo_get_work_tree(the_repository);
+       workdir = repo_get_work_tree(repo);
 
        /* Setup temp directories */
        tmp = getenv("TMPDIR");
@@ -449,8 +453,7 @@ static int run_dir_diff(struct difftool_options *dt_options,
                               "not supported in\n"
                               "directory diff mode ('-d' and '--dir-diff')."));
 
-               if (parse_index_info(info.buf, &lmode, &rmode, &loid, &roid,
-                                    &status))
+               if (parse_index_info(repo, info.buf, &lmode, &rmode, &loid, &roid, &status))
                        break;
                if (strbuf_getline_nul(&lpath, fp))
                        break;
@@ -480,13 +483,13 @@ static int run_dir_diff(struct difftool_options *dt_options,
                }
 
                if (S_ISLNK(lmode)) {
-                       char *content = get_symlink(dt_options, &loid, src_path);
+                       char *content = get_symlink(repo, dt_options, &loid, src_path);
                        add_left_or_right(&symlinks2, src_path, content, 0);
                        free(content);
                }
 
                if (S_ISLNK(rmode)) {
-                       char *content = get_symlink(dt_options, &roid, dst_path);
+                       char *content = get_symlink(repo, dt_options, &roid, dst_path);
                        add_left_or_right(&symlinks2, dst_path, content, 1);
                        free(content);
                }
@@ -511,7 +514,7 @@ static int run_dir_diff(struct difftool_options *dt_options,
                        }
                        hashmap_add(&working_tree_dups, &entry->entry);
 
-                       if (!use_wt_file(workdir, dst_path, &roid)) {
+                       if (!use_wt_file(repo, workdir, dst_path, &roid)) {
                                if (checkout_path(rmode, &roid, dst_path,
                                                  &rstate)) {
                                        ret = error("could not write '%s'",
@@ -637,9 +640,9 @@ static int run_dir_diff(struct difftool_options *dt_options,
                                ret = error("could not write %s", buf.buf);
                                goto finish;
                        }
-                       changed_files(&wt_modified, buf.buf, workdir);
+                       changed_files(repo, &wt_modified, buf.buf, workdir);
                        strbuf_setlen(&rdir, rdir_len);
-                       changed_files(&tmp_modified, buf.buf, rdir.buf);
+                       changed_files(repo, &tmp_modified, buf.buf, rdir.buf);
                        add_path(&rdir, rdir_len, name);
                        indices_loaded = 1;
                }
@@ -713,7 +716,7 @@ static int run_file_diff(int prompt, const char *prefix,
 int cmd_difftool(int argc,
                 const char **argv,
                 const char *prefix,
-                struct repository *repo UNUSED)
+                struct repository *repo)
 {
        int use_gui_tool = -1, dir_diff = 0, prompt = -1, tool_help = 0, no_index = 0;
        static char *difftool_cmd = NULL, *extcmd = NULL;
@@ -749,7 +752,8 @@ int cmd_difftool(int argc,
        };
        struct child_process child = CHILD_PROCESS_INIT;
 
-       git_config(difftool_config, &dt_options);
+       if (repo)
+               repo_config(repo, difftool_config, &dt_options);
        dt_options.symlinks = dt_options.has_symlinks;
 
        argc = parse_options(argc, argv, prefix, builtin_difftool_options,
@@ -764,8 +768,8 @@ int cmd_difftool(int argc,
 
        if (!no_index){
                setup_work_tree();
-               setenv(GIT_DIR_ENVIRONMENT, absolute_path(repo_get_git_dir(the_repository)), 1);
-               setenv(GIT_WORK_TREE_ENVIRONMENT, absolute_path(repo_get_work_tree(the_repository)), 1);
+               setenv(GIT_DIR_ENVIRONMENT, absolute_path(repo_get_git_dir(repo)), 1);
+               setenv(GIT_WORK_TREE_ENVIRONMENT, absolute_path(repo_get_work_tree(repo)), 1);
        } else if (dir_diff)
                die(_("options '%s' and '%s' cannot be used together"), "--dir-diff", "--no-index");
 
@@ -814,6 +818,6 @@ int cmd_difftool(int argc,
        strvec_pushv(&child.args, argv);
 
        if (dir_diff)
-               return run_dir_diff(&dt_options, extcmd, prefix, &child);
+               return run_dir_diff(repo, &dt_options, extcmd, prefix, &child);
        return run_file_diff(prompt, prefix, &child);
 }
--
Quoted 1 line

2.48.1.461.g612e419e04

This is so much easier to read with the separate option struct being split out into a separate patch. Looks good to me.

#3 lore raw

PATCH v2 3/3 difftool: eliminate use of USE_THE_REPOSITORY_VARIABLE

In reply to David Aguilar

Remove the USE_THE_REPOSITORY_VARIABLE #define now that all state is passed to each function from callers.

---
 builtin/difftool.c | 2 --
 1 file changed, 2 deletions(-)
builtin/difftool.c +0 -2
index 81d733dfdf..41cd00066c 100644
@@ -12,8 +12,6 @@
  * Copyright (C) 2016 Johannes Schindelin
  */
 
-#define USE_THE_REPOSITORY_VARIABLE
-
 #include "builtin.h"
 
 #include "abspath.h"
Signature · 1 line
2.48.1.461.g612e419e04
#4 lore raw

Re: [PATCH v2 3/3] difftool: eliminate use of USE_THE_REPOSITORY_VARIABLE

Quoted 23 lines · David Aguilar

Remove the USE_THE_REPOSITORY_VARIABLE #define now that all state is passed to each function from callers.

---
 builtin/difftool.c | 2 --
 1 file changed, 2 deletions(-)
builtin/difftool.c +0 -3
index 81d733dfdf..41cd00066c 100644
@@ -12,8 +12,6 @@
  * Copyright (C) 2016 Johannes Schindelin
  */
 
-#define USE_THE_REPOSITORY_VARIABLE
-
 #include "builtin.h"
 
 #include "abspath.h"
--
Quoted 1 line

2.48.1.461.g612e419e04

Nice; thanks for working on this.

#5 lore raw

Re: [PATCH v2 3/3] difftool: eliminate use of USE_THE_REPOSITORY_VARIABLE

Quoted 22 lines · Elijah Newren
Quoted 21 lines · David Aguilar

Remove the USE_THE_REPOSITORY_VARIABLE #define now that all state is passed to each function from callers.

---
 builtin/difftool.c | 2 --
 1 file changed, 2 deletions(-)
builtin/difftool.c +0 -2
index 81d733dfdf..41cd00066c 100644
@@ -12,8 +12,6 @@
  * Copyright (C) 2016 Johannes Schindelin
  */
 
-#define USE_THE_REPOSITORY_VARIABLE
-
 #include "builtin.h"
 
Quoted 6 lines
Quoted 3 lines
builtin/difftool.c +0 -1
 #include "abspath.h"
--

2.48.1.461.g612e419e04

Nice; thanks for working on this.

Yup, thanks for rerolling. This version is much easier to read indeed and looks good to me.

Patrick

#6 lore raw

Re: [PATCH v2 3/3] difftool: eliminate use of USE_THE_REPOSITORY_VARIABLE

Quoted 8 lines · Patrick Steinhardt
Quoted 4 lines · Elijah Newren

On Wed, Feb 5, 2025 at 8:20 PM David Aguilar <[email protected]> wrote:
...

Nice; thanks for working on this.

Yup, thanks for rerolling. This version is much easier to read indeed and looks good to me.

Thanks, all, for working on this and reviewing the patches. Looking very good.

Let me mark the topic for 'next'.

#7 lore raw
In reply to David Aguilar
Quoted 3 lines · David Aguilar

Move difftool's global variables into a difftools_option struct in preparation for removal of USE_THE_REPOSITORY_VARIABLE.

Thanks for splitting these out.

Quoted 27 lines
---
 builtin/difftool.c | 51 ++++++++++++++++++++++++++++++----------------
 1 file changed, 33 insertions(+), 18 deletions(-)
builtin/difftool.c +6 -2
index 03a8bb92a9..0b6b92aee0 100644
@@ -36,18 +36,27 @@
 #include "entry.h"
 #include "setup.h"
 
-static int trust_exit_code;
-
 static const char *const builtin_difftool_usage[] = {
        N_("git difftool [<options>] [<commit> [<commit>]] [--] [<path>...]"),
        NULL
 };
 
+struct difftool_options {
+       int has_symlinks;
+       int symlinks;
+       int trust_exit_code;
+};
+
 static int difftool_config(const char *var, const char *value,

builtin/difftool.c line 59

Quoted 10 lines
builtin/difftool.c +6 -1
                           const struct config_context *ctx, void *cb)
 {
+       struct difftool_options *dt_options = (struct difftool_options *)cb;
        if (!strcmp(var, "difftool.trustexitcode")) {
-               trust_exit_code = git_config_bool(var, value);
+               dt_options->trust_exit_code = git_config_bool(var, value);
+               return 0;
+       }
+       if (!strcmp(var, "core.symlinks")) {
+               dt_options->has_symlinks = git_config_bool(var, value);

It appears that the only use for has_symlinks....

Quoted 7 lines
builtin/difftool.c +0 -0
                return 0;
        }
 
@@ -291,13 +300,14 @@ static int ensure_leading_directories(char *path)
  * to compare the readlink(2) result as text, even on a filesystem that is
  * capable of doing a symbolic link.
  */

builtin/difftool.c static int ensure_leading_directories(char *path) · line 310

Quoted 10 lines
builtin/difftool.c +3 -2
-static char *get_symlink(const struct object_id *oid, const char *path)
+static char *get_symlink(struct difftool_options *dt_options,
+                        const struct object_id *oid, const char *path)
 {
        char *data;
        if (is_null_oid(oid)) {
                /* The symlink is unknown to Git so read from the filesystem */
                struct strbuf link = STRBUF_INIT;
-               if (has_symlinks) {
+               if (dt_options->has_symlinks) {

Why is this based on dt_options->has_symlinks rather than dt_options->symlinks?

(I guess this question is equivalent to asking why the preimage code was using has_symlinks, instead of the symlinks parameter set from the command line option. As far as I can see, has_symlinks is supposed to merely function as a default value for symlinks in the case no command line parameter is passed...but this is the one counter-example. But was it an intentional counter-example, or an accident?)

That said, fixing this, if fixing is needed, doesn't belong in this patch; it'd probably be better as a preparatory patch. But, it trips up reviewers (looks like Patrick was wondering about the same thing on v1 of your series), so it at least would probably be helpful to mention in the commit message if no other cleanup is needed with these.

Quoted 81 lines
builtin/difftool.c +14 -9
                        if (strbuf_readlink(&link, path, strlen(path)))
                                die(_("could not read symlink %s"), path);
                } else if (strbuf_read_file(&link, path, 128))
@@ -355,7 +365,8 @@ static void write_standin_files(struct pair_entry *entry,
                write_file_in_directory(rdir, rdir_len, entry->path, entry->right);
 }
 
-static int run_dir_diff(const char *extcmd, int symlinks, const char *prefix,
+static int run_dir_diff(struct difftool_options *dt_options,
+                       const char *extcmd, const char *prefix,
                        struct child_process *child)
 {
        struct strbuf info = STRBUF_INIT, lpath = STRBUF_INIT;
@@ -469,13 +480,13 @@ static int run_dir_diff(const char *extcmd, int symlinks, const char *prefix,
                }
 
                if (S_ISLNK(lmode)) {
-                       char *content = get_symlink(&loid, src_path);
+                       char *content = get_symlink(dt_options, &loid, src_path);
                        add_left_or_right(&symlinks2, src_path, content, 0);
                        free(content);
                }
 
                if (S_ISLNK(rmode)) {
-                       char *content = get_symlink(&roid, dst_path);
+                       char *content = get_symlink(dt_options, &roid, dst_path);
                        add_left_or_right(&symlinks2, dst_path, content, 1);
                        free(content);
                }
@@ -528,7 +539,7 @@ static int run_dir_diff(const char *extcmd, int symlinks, const char *prefix,
                                        goto finish;
                                }
                                add_path(&wtdir, wtdir_len, dst_path);
-                               if (symlinks) {
+                               if (dt_options->symlinks) {
                                        if (symlink(wtdir.buf, rdir.buf)) {
                                                ret = error_errno("could not symlink '%s' to '%s'", wtdir.buf, rdir.buf);
                                                goto finish;
@@ -614,7 +625,7 @@ static int run_dir_diff(const char *extcmd, int symlinks, const char *prefix,
                if (lstat(rdir.buf, &st))
                        continue;
 
-               if ((symlinks && S_ISLNK(st.st_mode)) || !S_ISREG(st.st_mode))
+               if ((dt_options->symlinks && S_ISLNK(st.st_mode)) || !S_ISREG(st.st_mode))
                        continue;
 
                if (!indices_loaded) {
@@ -704,9 +715,13 @@ int cmd_difftool(int argc,
                 const char *prefix,
                 struct repository *repo UNUSED)
 {
-       int use_gui_tool = -1, dir_diff = 0, prompt = -1, symlinks = 0,
-           tool_help = 0, no_index = 0;
+       int use_gui_tool = -1, dir_diff = 0, prompt = -1, tool_help = 0, no_index = 0;
        static char *difftool_cmd = NULL, *extcmd = NULL;
+       struct difftool_options dt_options = {
+               .has_symlinks = 1,
+               .symlinks = 1,
+               .trust_exit_code = 0
+       };
        struct option builtin_difftool_options[] = {
                OPT_BOOL('g', "gui", &use_gui_tool,
                         N_("use `diff.guitool` instead of `diff.tool`")),
@@ -717,14 +732,14 @@ int cmd_difftool(int argc,
                        0, PARSE_OPT_NONEG),
                OPT_SET_INT_F(0, "prompt", &prompt, NULL,
                        1, PARSE_OPT_NONEG | PARSE_OPT_HIDDEN),
-               OPT_BOOL(0, "symlinks", &symlinks,
+               OPT_BOOL(0, "symlinks", &dt_options.symlinks,
                         N_("use symlinks in dir-diff mode")),
                OPT_STRING('t', "tool", &difftool_cmd, N_("tool"),
                           N_("use the specified diff tool")),
                OPT_BOOL(0, "tool-help", &tool_help,
                         N_("print a list of diff tools that may be used with "
                            "`--tool`")),
-               OPT_BOOL(0, "trust-exit-code", &trust_exit_code,
+               OPT_BOOL(0, "trust-exit-code", &dt_options.trust_exit_code,
                         N_("make 'git-difftool' exit when an invoked diff "
                            "tool returns a non-zero exit code")),
                OPT_STRING('x', "extcmd", &extcmd, N_("command"),

builtin/difftool.c int cmd_difftool(int argc, · line 753

Quoted 7 lines
builtin/difftool.c +2 -2
@@ -734,8 +749,8 @@ int cmd_difftool(int argc,
        };
        struct child_process child = CHILD_PROCESS_INIT;
 
-       git_config(difftool_config, NULL);
-       symlinks = has_symlinks;
+       git_config(difftool_config, &dt_options);
+       dt_options.symlinks = dt_options.has_symlinks;

If the get_symlink() function should have been using dt_options.symlinks instead of dt_options.has_symlinks, then dt_options.has_symlinks is merely functioning as a default, but would actually be superfluous. A follow-up patch could remove that extra field.

Quoted 21 lines
builtin/difftool.c +2 -3
        argc = parse_options(argc, argv, prefix, builtin_difftool_options,
                             builtin_difftool_usage, PARSE_OPT_KEEP_UNKNOWN_OPT |
@@ -783,7 +798,7 @@ int cmd_difftool(int argc,
        }
 
        setenv("GIT_DIFFTOOL_TRUST_EXIT_CODE",
-              trust_exit_code ? "true" : "false", 1);
+              dt_options.trust_exit_code ? "true" : "false", 1);
 
        /*
         * In directory diff mode, 'git-difftool--helper' is called once
@@ -799,6 +814,6 @@ int cmd_difftool(int argc,
        strvec_pushv(&child.args, argv);
 
        if (dir_diff)
-               return run_dir_diff(extcmd, symlinks, prefix, &child);
+               return run_dir_diff(&dt_options, extcmd, prefix, &child);
        return run_file_diff(prompt, prefix, &child);
 }
--
#8 lore raw
Quoted 63 lines · Elijah Newren
Quoted 22 lines · David Aguilar
builtin/difftool.c +6 -2
index 03a8bb92a9..0b6b92aee0 100644
@@ -36,18 +36,27 @@
 #include "entry.h"
 #include "setup.h"
 
-static int trust_exit_code;
-
 static const char *const builtin_difftool_usage[] = {
        N_("git difftool [<options>] [<commit> [<commit>]] [--] [<path>...]"),
        NULL
 };
 
+struct difftool_options {
+       int has_symlinks;
+       int symlinks;
+       int trust_exit_code;
+};
+
 static int difftool_config(const char *var, const char *value,

builtin/difftool.c line 59

Quoted 10 lines
builtin/difftool.c +6 -1
                           const struct config_context *ctx, void *cb)
 {
+       struct difftool_options *dt_options = (struct difftool_options *)cb;
        if (!strcmp(var, "difftool.trustexitcode")) {
-               trust_exit_code = git_config_bool(var, value);
+               dt_options->trust_exit_code = git_config_bool(var, value);
+               return 0;
+       }
+       if (!strcmp(var, "core.symlinks")) {
+               dt_options->has_symlinks = git_config_bool(var, value);

It appears that the only use for has_symlinks....

Quoted 7 lines
builtin/difftool.c +0 -0
                return 0;
        }
 
@@ -291,13 +300,14 @@ static int ensure_leading_directories(char *path)
  * to compare the readlink(2) result as text, even on a filesystem that is
  * capable of doing a symbolic link.
  */

builtin/difftool.c static int ensure_leading_directories(char *path) · line 310

Quoted 10 lines
builtin/difftool.c +3 -2
-static char *get_symlink(const struct object_id *oid, const char *path)
+static char *get_symlink(struct difftool_options *dt_options,
+                        const struct object_id *oid, const char *path)
 {
        char *data;
        if (is_null_oid(oid)) {
                /* The symlink is unknown to Git so read from the filesystem */
                struct strbuf link = STRBUF_INIT;
-               if (has_symlinks) {
+               if (dt_options->has_symlinks) {

Why is this based on dt_options->has_symlinks rather than dt_options->symlinks?

(I guess this question is equivalent to asking why the preimage code was using has_symlinks, instead of the symlinks parameter set from the command line option. As far as I can see, has_symlinks is supposed to merely function as a default value for symlinks in the case no command line parameter is passed...but this is the one counter-example. But was it an intentional counter-example, or an accident?)

Quoted 6 lines

That said, fixing this, if fixing is needed, doesn't belong in this patch; it'd probably be better as a preparatory patch. But, it trips up reviewers (looks like Patrick was wondering about the same thing on v1 of your series), so it at least would probably be helpful to mention in the commit message if no other cleanup is needed with these.

Agreed. If we fix this it should be done in a separate patch and we can explain why they were separate variables as part of that commit message. I don't necessarily agree that it belongs in this patch.

Combining these two fields leads to test errors which is why it wasn't touched in this round.

Quoted 4 lines
Quoted 4 lines
builtin/difftool.c +0 -0
@@ -734,8 +749,8 @@ int cmd_difftool(int argc,
        };
        struct child_process child = CHILD_PROCESS_INIT;
 
Quoted 10 lines

builtin/difftool.c int cmd_difftool(int argc, · line 753

Quoted 4 lines
builtin/difftool.c +2 -2
-       git_config(difftool_config, NULL);
-       symlinks = has_symlinks;
+       git_config(difftool_config, &dt_options);
+       dt_options.symlinks = dt_options.has_symlinks;

If the get_symlink() function should have been using dt_options.symlinks instead of dt_options.has_symlinks, then dt_options.has_symlinks is merely functioning as a default, but would actually be superfluous. A follow-up patch could remove that extra field.

has_symlinks is currently providing both a default value and controlling the behavior of the dir-diff mode, so it's not quite merely functioning as a default.

My eyes gloss over comments because I completely missed the following explanation in the comment above get_symlink().
This comment explain why we have a separate have_symlinks field:

/*
* Unconditional writing of a plain regular file is what
* "git difftool --dir-diff" wants to do for symlinks. We are preparing two
* temporary directories to be fed to a Git-unaware tool that knows how to
* show a diff of two directories (e.g. "diff -r A B").
*
* Because the tool is Git-unaware, if a symbolic link appears in either of
* these temporary directories, it will try to dereference and show the
* difference of the target of the symbolic link, which is not what we want,
* as the goal of the dir-diff mode is to produce an output that is logically
* equivalent to what "git diff" produces.
*
* Most importantly, we want to get textual comparison of the result of the
* readlink(2). get_symlink() provides that---it returns the contents of
* the symlink that gets written to a regular file to force the external tool
* to compare the readlink(2) result as text, even on a filesystem that is
* capable of doing a symbolic link.
*/

In other words, we intetionally take the extra step to readlink(2) symlinks in the dir-diff mode irrespective of the command-line option on systems that support symlinks. That's why has_symlinks has to be tracked separately.

In light of this, I suspect that we won't be combining these fields because this behavior is intentional and necessary.

git blame claims that I wrote this comment 8 years ago, but that's news to me!

Thanks for the thorough review. I'm not planning a re-roll since it seems like this is fine as-is, but let me know if y'all feel otherwise.

One thing I would maybe change would be to rename dt_options to options, but I also appreciate the verbosity of the dt_ prefix.

Interestingly, the struct difftool_state and dt_state names in the original patch were chosen because the struct contained more than just options. Specifically, it contains the has_symlinks field. I'm not really sure it's worth splitting hairs over that detail, but I'm all ears. struct difftool_options doesn't really bother me.

cheers,

Signature · 1 line
David
#9 lore raw
In reply to David Aguilar
Quoted 2 lines · David Aguilar

Move difftool's global variables into a difftools_option struct in preparation for removal of USE_THE_REPOSITORY_VARIABLE.

Both may be good things, but I am puzzled by the "in preparation for" part of the above description. Would it require we lose these three global variables if we wanted to pass through a repository instance through the callchain instead of relying on implicit use of the_repository?

Aren't these pretty much independent and orthogonal?

Quoted 163 lines
---
 builtin/difftool.c | 51 ++++++++++++++++++++++++++++++----------------
 1 file changed, 33 insertions(+), 18 deletions(-)
builtin/difftool.c +33 -18
index 03a8bb92a9..0b6b92aee0 100644
@@ -36,18 +36,27 @@
 #include "entry.h"
 #include "setup.h"
 
-static int trust_exit_code;
-
 static const char *const builtin_difftool_usage[] = {
 	N_("git difftool [<options>] [<commit> [<commit>]] [--] [<path>...]"),
 	NULL
 };
 
+struct difftool_options {
+	int has_symlinks;
+	int symlinks;
+	int trust_exit_code;
+};
+
 static int difftool_config(const char *var, const char *value,
 			   const struct config_context *ctx, void *cb)
 {
+	struct difftool_options *dt_options = (struct difftool_options *)cb;
 	if (!strcmp(var, "difftool.trustexitcode")) {
-		trust_exit_code = git_config_bool(var, value);
+		dt_options->trust_exit_code = git_config_bool(var, value);
+		return 0;
+	}
+	if (!strcmp(var, "core.symlinks")) {
+		dt_options->has_symlinks = git_config_bool(var, value);
 		return 0;
 	}
 
@@ -291,13 +300,14 @@ static int ensure_leading_directories(char *path)
  * to compare the readlink(2) result as text, even on a filesystem that is
  * capable of doing a symbolic link.
  */
-static char *get_symlink(const struct object_id *oid, const char *path)
+static char *get_symlink(struct difftool_options *dt_options,
+			 const struct object_id *oid, const char *path)
 {
 	char *data;
 	if (is_null_oid(oid)) {
 		/* The symlink is unknown to Git so read from the filesystem */
 		struct strbuf link = STRBUF_INIT;
-		if (has_symlinks) {
+		if (dt_options->has_symlinks) {
 			if (strbuf_readlink(&link, path, strlen(path)))
 				die(_("could not read symlink %s"), path);
 		} else if (strbuf_read_file(&link, path, 128))
@@ -355,7 +365,8 @@ static void write_standin_files(struct pair_entry *entry,
 		write_file_in_directory(rdir, rdir_len, entry->path, entry->right);
 }
 
-static int run_dir_diff(const char *extcmd, int symlinks, const char *prefix,
+static int run_dir_diff(struct difftool_options *dt_options,
+			const char *extcmd, const char *prefix,
 			struct child_process *child)
 {
 	struct strbuf info = STRBUF_INIT, lpath = STRBUF_INIT;
@@ -469,13 +480,13 @@ static int run_dir_diff(const char *extcmd, int symlinks, const char *prefix,
 		}
 
 		if (S_ISLNK(lmode)) {
-			char *content = get_symlink(&loid, src_path);
+			char *content = get_symlink(dt_options, &loid, src_path);
 			add_left_or_right(&symlinks2, src_path, content, 0);
 			free(content);
 		}
 
 		if (S_ISLNK(rmode)) {
-			char *content = get_symlink(&roid, dst_path);
+			char *content = get_symlink(dt_options, &roid, dst_path);
 			add_left_or_right(&symlinks2, dst_path, content, 1);
 			free(content);
 		}
@@ -528,7 +539,7 @@ static int run_dir_diff(const char *extcmd, int symlinks, const char *prefix,
 					goto finish;
 				}
 				add_path(&wtdir, wtdir_len, dst_path);
-				if (symlinks) {
+				if (dt_options->symlinks) {
 					if (symlink(wtdir.buf, rdir.buf)) {
 						ret = error_errno("could not symlink '%s' to '%s'", wtdir.buf, rdir.buf);
 						goto finish;
@@ -614,7 +625,7 @@ static int run_dir_diff(const char *extcmd, int symlinks, const char *prefix,
 		if (lstat(rdir.buf, &st))
 			continue;
 
-		if ((symlinks && S_ISLNK(st.st_mode)) || !S_ISREG(st.st_mode))
+		if ((dt_options->symlinks && S_ISLNK(st.st_mode)) || !S_ISREG(st.st_mode))
 			continue;
 
 		if (!indices_loaded) {
@@ -704,9 +715,13 @@ int cmd_difftool(int argc,
 		 const char *prefix,
 		 struct repository *repo UNUSED)
 {
-	int use_gui_tool = -1, dir_diff = 0, prompt = -1, symlinks = 0,
-	    tool_help = 0, no_index = 0;
+	int use_gui_tool = -1, dir_diff = 0, prompt = -1, tool_help = 0, no_index = 0;
 	static char *difftool_cmd = NULL, *extcmd = NULL;
+	struct difftool_options dt_options = {
+		.has_symlinks = 1,
+		.symlinks = 1,
+		.trust_exit_code = 0
+	};
 	struct option builtin_difftool_options[] = {
 		OPT_BOOL('g', "gui", &use_gui_tool,
 			 N_("use `diff.guitool` instead of `diff.tool`")),
@@ -717,14 +732,14 @@ int cmd_difftool(int argc,
 			0, PARSE_OPT_NONEG),
 		OPT_SET_INT_F(0, "prompt", &prompt, NULL,
 			1, PARSE_OPT_NONEG | PARSE_OPT_HIDDEN),
-		OPT_BOOL(0, "symlinks", &symlinks,
+		OPT_BOOL(0, "symlinks", &dt_options.symlinks,
 			 N_("use symlinks in dir-diff mode")),
 		OPT_STRING('t', "tool", &difftool_cmd, N_("tool"),
 			   N_("use the specified diff tool")),
 		OPT_BOOL(0, "tool-help", &tool_help,
 			 N_("print a list of diff tools that may be used with "
 			    "`--tool`")),
-		OPT_BOOL(0, "trust-exit-code", &trust_exit_code,
+		OPT_BOOL(0, "trust-exit-code", &dt_options.trust_exit_code,
 			 N_("make 'git-difftool' exit when an invoked diff "
 			    "tool returns a non-zero exit code")),
 		OPT_STRING('x', "extcmd", &extcmd, N_("command"),
@@ -734,8 +749,8 @@ int cmd_difftool(int argc,
 	};
 	struct child_process child = CHILD_PROCESS_INIT;
 
-	git_config(difftool_config, NULL);
-	symlinks = has_symlinks;
+	git_config(difftool_config, &dt_options);
+	dt_options.symlinks = dt_options.has_symlinks;
 
 	argc = parse_options(argc, argv, prefix, builtin_difftool_options,
 			     builtin_difftool_usage, PARSE_OPT_KEEP_UNKNOWN_OPT |
@@ -783,7 +798,7 @@ int cmd_difftool(int argc,
 	}
 
 	setenv("GIT_DIFFTOOL_TRUST_EXIT_CODE",
-	       trust_exit_code ? "true" : "false", 1);
+	       dt_options.trust_exit_code ? "true" : "false", 1);
 
 	/*
 	 * In directory diff mode, 'git-difftool--helper' is called once
@@ -799,6 +814,6 @@ int cmd_difftool(int argc,
 	strvec_pushv(&child.args, argv);
 
 	if (dir_diff)
-		return run_dir_diff(extcmd, symlinks, prefix, &child);
+		return run_dir_diff(&dt_options, extcmd, prefix, &child);
 	return run_file_diff(prompt, prefix, &child);
 }
#10 lore raw
Quoted 6 lines · Junio C Hamano
Quoted 2 lines · David Aguilar

Move difftool's global variables into a difftools_option struct in preparation for removal of USE_THE_REPOSITORY_VARIABLE.

Quoted 7 lines

Both may be good things, but I am puzzled by the "in preparation for" part of the above description. Would it require we lose these three global variables if we wanted to pass through a repository instance through the callchain instead of relying on implicit use of the_repository?

Aren't these pretty much independent and orthogonal?

The declaration of 'extern int has_symlinks;' in environment.h is guarded by an #ifdef USE_THE_REPOSITORY_VARIABLE, so if you want to stop declaring that, you need to both pass a repository through and stop using that global variable. (The change to trust_exit_code and symlinks vars do seem to be independent, but kind of make sense to handle at the same time you are changing how has_symlinks is treated.)

#11 lore raw
Quoted 3 lines · Elijah Newren

stop using that global variable. (The change to trust_exit_code and symlinks vars do seem to be independent, but kind of make sense to handle at the same time you are changing how has_symlinks is treated.)

Fair enough. Thanks.

#12 lore raw
In reply to Elijah Newren
Quoted 13 lines · Elijah Newren
Quoted 12 lines · Junio C Hamano
Quoted 2 lines · David Aguilar

Move difftool's global variables into a difftools_option struct in preparation for removal of USE_THE_REPOSITORY_VARIABLE.

Both may be good things, but I am puzzled by the "in preparation for" part of the above description. Would it require we lose these three global variables if we wanted to pass through a repository instance through the callchain instead of relying on implicit use of the_repository?

Quoted 8 lines
Quoted 1 line

Aren't these pretty much independent and orthogonal?

The declaration of 'extern int has_symlinks;' in environment.h is guarded by an #ifdef USE_THE_REPOSITORY_VARIABLE, so if you want to stop declaring that, you need to both pass a repository through and stop using that global variable. (The change to trust_exit_code and symlinks vars do seem to be independent, but kind of make sense to handle at the same time you are changing how has_symlinks is treated.)

Ah, that makes sense. It raises the question whether the refactoring thus breaks something because we don't use that global variable anymore, e.g. if we were munging it in the preimage. But as far as I can see we don't modify it at all, so this should be fine.

Patrick