difftool: eliminate use of the_repository

Make callers pass a repository struct into each function instead of relying on the global the_repository variable. Signed-off-by: David Aguilar <davvid@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

David Aguilar committed Feb 5, 2025 at 20:20 UTC a24953f3df4546cad670892c652978cce161ec79
1 file changed +29 -25
builtin/difftool.c
+29 -25
@@ -72,7 +72,8 @@ static int print_tool_help(void)
72 return run_command(&cmd);
73 }
74
75 -static int parse_index_info(char *p, int *mode1, int *mode2,
75 +static int parse_index_info(struct repository *repo,
76 + char *p, int *mode1, int *mode2,
77 struct object_id *oid1, struct object_id *oid2,
78 char *status)
79 {
@@ -84,11 +85,11 @@ static int parse_index_info(char *p, int *mode1, int *mode2,
85 *mode2 = (int)strtol(p + 1, &p, 8);
86 if (*p != ' ')
87 return error("expected ' ', got '%c'", *p);
87 - if (parse_oid_hex(++p, oid1, (const char **)&p))
88 + if (parse_oid_hex_algop(++p, oid1, (const char **)&p, repo->hash_algo))
89 return error("expected object ID, got '%s'", p);
90 if (*p != ' ')
91 return error("expected ' ', got '%c'", *p);
91 - if (parse_oid_hex(++p, oid2, (const char **)&p))
92 + if (parse_oid_hex_algop(++p, oid2, (const char **)&p, repo->hash_algo))
93 return error("expected object ID, got '%s'", p);
94 if (*p != ' ')
95 return error("expected ' ', got '%c'", *p);
@@ -115,7 +116,8 @@ static void add_path(struct strbuf *buf, size_t base_len, const char *path)
116 /*
117 * Determine whether we can simply reuse the file in the worktree.
118 */
118 -static int use_wt_file(const char *workdir, const char *name,
119 +static int use_wt_file(struct repository *repo,
120 + const char *workdir, const char *name,
121 struct object_id *oid)
122 {
123 struct strbuf buf = STRBUF_INIT;
@@ -130,7 +132,7 @@ static int use_wt_file(const char *workdir, const char *name,
132 int fd = open(buf.buf, O_RDONLY);
133
134 if (fd >= 0 &&
133 - !index_fd(the_repository->index, &wt_oid, fd, &st, OBJ_BLOB, name, 0)) {
135 + !index_fd(repo->index, &wt_oid, fd, &st, OBJ_BLOB, name, 0)) {
136 if (is_null_oid(oid)) {
137 oidcpy(oid, &wt_oid);
138 use = 1;
@@ -221,13 +223,14 @@ static int path_entry_cmp(const void *cmp_data UNUSED,
223 return strcmp(a->path, key ? key : b->path);
224 }
225
224 -static void changed_files(struct hashmap *result, const char *index_path,
226 +static void changed_files(struct repository *repo,
227 + struct hashmap *result, const char *index_path,
228 const char *workdir)
229 {
230 struct child_process update_index = CHILD_PROCESS_INIT;
231 struct child_process diff_files = CHILD_PROCESS_INIT;
232 struct strbuf buf = STRBUF_INIT;
230 - const char *git_dir = absolute_path(repo_get_git_dir(the_repository));
233 + const char *git_dir = absolute_path(repo_get_git_dir(repo));
234 FILE *fp;
235
236 strvec_pushl(&update_index.args,
@@ -300,7 +303,8 @@ static int ensure_leading_directories(char *path)
303 * to compare the readlink(2) result as text, even on a filesystem that is
304 * capable of doing a symbolic link.
305 */
303 -static char *get_symlink(struct difftool_options *dt_options,
306 +static char *get_symlink(struct repository *repo,
307 + struct difftool_options *dt_options,
308 const struct object_id *oid, const char *path)
309 {
310 char *data;
@@ -317,8 +321,7 @@ static char *get_symlink(struct difftool_options *dt_options,
321 } else {
322 enum object_type type;
323 unsigned long size;
320 - data = repo_read_object_file(the_repository, oid, &type,
321 - &size);
324 + data = repo_read_object_file(repo, oid, &type, &size);
325 if (!data)
326 die(_("could not read object %s for symlink %s"),
327 oid_to_hex(oid), path);
@@ -365,7 +368,8 @@ static void write_standin_files(struct pair_entry *entry,
368 write_file_in_directory(rdir, rdir_len, entry->path, entry->right);
369 }
370
368 -static int run_dir_diff(struct difftool_options *dt_options,
371 +static int run_dir_diff(struct repository *repo,
372 + struct difftool_options *dt_options,
373 const char *extcmd, const char *prefix,
374 struct child_process *child)
375 {
@@ -386,7 +390,7 @@ static int run_dir_diff(struct difftool_options *dt_options,
390 struct hashmap symlinks2 = HASHMAP_INIT(pair_cmp, NULL);
391 struct hashmap_iter iter;
392 struct pair_entry *entry;
389 - struct index_state wtindex = INDEX_STATE_INIT(the_repository);
393 + struct index_state wtindex = INDEX_STATE_INIT(repo);
394 struct checkout lstate, rstate;
395 int err = 0;
396 struct child_process cmd = CHILD_PROCESS_INIT;
@@ -394,7 +398,7 @@ static int run_dir_diff(struct difftool_options *dt_options,
398 struct hashmap tmp_modified = HASHMAP_INIT(path_entry_cmp, NULL);
399 int indices_loaded = 0;
400
397 - workdir = repo_get_work_tree(the_repository);
401 + workdir = repo_get_work_tree(repo);
402
403 /* Setup temp directories */
404 tmp = getenv("TMPDIR");
@@ -449,8 +453,7 @@ static int run_dir_diff(struct difftool_options *dt_options,
453 "not supported in\n"
454 "directory diff mode ('-d' and '--dir-diff')."));
455
452 - if (parse_index_info(info.buf, &lmode, &rmode, &loid, &roid,
453 - &status))
456 + if (parse_index_info(repo, info.buf, &lmode, &rmode, &loid, &roid, &status))
457 break;
458 if (strbuf_getline_nul(&lpath, fp))
459 break;
@@ -480,13 +483,13 @@ static int run_dir_diff(struct difftool_options *dt_options,
483 }
484
485 if (S_ISLNK(lmode)) {
483 - char *content = get_symlink(dt_options, &loid, src_path);
486 + char *content = get_symlink(repo, dt_options, &loid, src_path);
487 add_left_or_right(&symlinks2, src_path, content, 0);
488 free(content);
489 }
490
491 if (S_ISLNK(rmode)) {
489 - char *content = get_symlink(dt_options, &roid, dst_path);
492 + char *content = get_symlink(repo, dt_options, &roid, dst_path);
493 add_left_or_right(&symlinks2, dst_path, content, 1);
494 free(content);
495 }
@@ -511,7 +514,7 @@ static int run_dir_diff(struct difftool_options *dt_options,
514 }
515 hashmap_add(&working_tree_dups, &entry->entry);
516
514 - if (!use_wt_file(workdir, dst_path, &roid)) {
517 + if (!use_wt_file(repo, workdir, dst_path, &roid)) {
518 if (checkout_path(rmode, &roid, dst_path,
519 &rstate)) {
520 ret = error("could not write '%s'",
@@ -637,9 +640,9 @@ static int run_dir_diff(struct difftool_options *dt_options,
640 ret = error("could not write %s", buf.buf);
641 goto finish;
642 }
640 - changed_files(&wt_modified, buf.buf, workdir);
643 + changed_files(repo, &wt_modified, buf.buf, workdir);
644 strbuf_setlen(&rdir, rdir_len);
642 - changed_files(&tmp_modified, buf.buf, rdir.buf);
645 + changed_files(repo, &tmp_modified, buf.buf, rdir.buf);
646 add_path(&rdir, rdir_len, name);
647 indices_loaded = 1;
648 }
@@ -713,7 +716,7 @@ static int run_file_diff(int prompt, const char *prefix,
716 int cmd_difftool(int argc,
717 const char **argv,
718 const char *prefix,
716 - struct repository *repo UNUSED)
719 + struct repository *repo)
720 {
721 int use_gui_tool = -1, dir_diff = 0, prompt = -1, tool_help = 0, no_index = 0;
722 static char *difftool_cmd = NULL, *extcmd = NULL;
@@ -749,7 +752,8 @@ int cmd_difftool(int argc,
752 };
753 struct child_process child = CHILD_PROCESS_INIT;
754
752 - git_config(difftool_config, &dt_options);
755 + if (repo)
756 + repo_config(repo, difftool_config, &dt_options);
757 dt_options.symlinks = dt_options.has_symlinks;
758
759 argc = parse_options(argc, argv, prefix, builtin_difftool_options,
@@ -764,8 +768,8 @@ int cmd_difftool(int argc,
768
769 if (!no_index){
770 setup_work_tree();
767 - setenv(GIT_DIR_ENVIRONMENT, absolute_path(repo_get_git_dir(the_repository)), 1);
768 - setenv(GIT_WORK_TREE_ENVIRONMENT, absolute_path(repo_get_work_tree(the_repository)), 1);
771 + setenv(GIT_DIR_ENVIRONMENT, absolute_path(repo_get_git_dir(repo)), 1);
772 + setenv(GIT_WORK_TREE_ENVIRONMENT, absolute_path(repo_get_work_tree(repo)), 1);
773 } else if (dir_diff)
774 die(_("options '%s' and '%s' cannot be used together"), "--dir-diff", "--no-index");
775
@@ -814,6 +818,6 @@ int cmd_difftool(int argc,
818 strvec_pushv(&child.args, argv);
819
820 if (dir_diff)
817 - return run_dir_diff(&dt_options, extcmd, prefix, &child);
821 + return run_dir_diff(repo, &dt_options, extcmd, prefix, &child);
822 return run_file_diff(prompt, prefix, &child);
823 }