Skip to content

Commit a24953f

Browse files
davvidgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 8241ae6 commit a24953f

File tree

1 file changed

+29
-25
lines changed

1 file changed

+29
-25
lines changed

builtin/difftool.c

Lines changed: 29 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,8 @@ static int print_tool_help(void)
7272
return run_command(&cmd);
7373
}
7474

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,
7677
struct object_id *oid1, struct object_id *oid2,
7778
char *status)
7879
{
@@ -84,11 +85,11 @@ static int parse_index_info(char *p, int *mode1, int *mode2,
8485
*mode2 = (int)strtol(p + 1, &p, 8);
8586
if (*p != ' ')
8687
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))
8889
return error("expected object ID, got '%s'", p);
8990
if (*p != ' ')
9091
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))
9293
return error("expected object ID, got '%s'", p);
9394
if (*p != ' ')
9495
return error("expected ' ', got '%c'", *p);
@@ -115,7 +116,8 @@ static void add_path(struct strbuf *buf, size_t base_len, const char *path)
115116
/*
116117
* Determine whether we can simply reuse the file in the worktree.
117118
*/
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,
119121
struct object_id *oid)
120122
{
121123
struct strbuf buf = STRBUF_INIT;
@@ -130,7 +132,7 @@ static int use_wt_file(const char *workdir, const char *name,
130132
int fd = open(buf.buf, O_RDONLY);
131133

132134
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)) {
134136
if (is_null_oid(oid)) {
135137
oidcpy(oid, &wt_oid);
136138
use = 1;
@@ -221,13 +223,14 @@ static int path_entry_cmp(const void *cmp_data UNUSED,
221223
return strcmp(a->path, key ? key : b->path);
222224
}
223225

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,
225228
const char *workdir)
226229
{
227230
struct child_process update_index = CHILD_PROCESS_INIT;
228231
struct child_process diff_files = CHILD_PROCESS_INIT;
229232
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));
231234
FILE *fp;
232235

233236
strvec_pushl(&update_index.args,
@@ -300,7 +303,8 @@ static int ensure_leading_directories(char *path)
300303
* to compare the readlink(2) result as text, even on a filesystem that is
301304
* capable of doing a symbolic link.
302305
*/
303-
static char *get_symlink(struct difftool_options *dt_options,
306+
static char *get_symlink(struct repository *repo,
307+
struct difftool_options *dt_options,
304308
const struct object_id *oid, const char *path)
305309
{
306310
char *data;
@@ -317,8 +321,7 @@ static char *get_symlink(struct difftool_options *dt_options,
317321
} else {
318322
enum object_type type;
319323
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);
322325
if (!data)
323326
die(_("could not read object %s for symlink %s"),
324327
oid_to_hex(oid), path);
@@ -365,7 +368,8 @@ static void write_standin_files(struct pair_entry *entry,
365368
write_file_in_directory(rdir, rdir_len, entry->path, entry->right);
366369
}
367370

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,
369373
const char *extcmd, const char *prefix,
370374
struct child_process *child)
371375
{
@@ -386,15 +390,15 @@ static int run_dir_diff(struct difftool_options *dt_options,
386390
struct hashmap symlinks2 = HASHMAP_INIT(pair_cmp, NULL);
387391
struct hashmap_iter iter;
388392
struct pair_entry *entry;
389-
struct index_state wtindex = INDEX_STATE_INIT(the_repository);
393+
struct index_state wtindex = INDEX_STATE_INIT(repo);
390394
struct checkout lstate, rstate;
391395
int err = 0;
392396
struct child_process cmd = CHILD_PROCESS_INIT;
393397
struct hashmap wt_modified = HASHMAP_INIT(path_entry_cmp, NULL);
394398
struct hashmap tmp_modified = HASHMAP_INIT(path_entry_cmp, NULL);
395399
int indices_loaded = 0;
396400

397-
workdir = repo_get_work_tree(the_repository);
401+
workdir = repo_get_work_tree(repo);
398402

399403
/* Setup temp directories */
400404
tmp = getenv("TMPDIR");
@@ -449,8 +453,7 @@ static int run_dir_diff(struct difftool_options *dt_options,
449453
"not supported in\n"
450454
"directory diff mode ('-d' and '--dir-diff')."));
451455

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))
454457
break;
455458
if (strbuf_getline_nul(&lpath, fp))
456459
break;
@@ -480,13 +483,13 @@ static int run_dir_diff(struct difftool_options *dt_options,
480483
}
481484

482485
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);
484487
add_left_or_right(&symlinks2, src_path, content, 0);
485488
free(content);
486489
}
487490

488491
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);
490493
add_left_or_right(&symlinks2, dst_path, content, 1);
491494
free(content);
492495
}
@@ -511,7 +514,7 @@ static int run_dir_diff(struct difftool_options *dt_options,
511514
}
512515
hashmap_add(&working_tree_dups, &entry->entry);
513516

514-
if (!use_wt_file(workdir, dst_path, &roid)) {
517+
if (!use_wt_file(repo, workdir, dst_path, &roid)) {
515518
if (checkout_path(rmode, &roid, dst_path,
516519
&rstate)) {
517520
ret = error("could not write '%s'",
@@ -637,9 +640,9 @@ static int run_dir_diff(struct difftool_options *dt_options,
637640
ret = error("could not write %s", buf.buf);
638641
goto finish;
639642
}
640-
changed_files(&wt_modified, buf.buf, workdir);
643+
changed_files(repo, &wt_modified, buf.buf, workdir);
641644
strbuf_setlen(&rdir, rdir_len);
642-
changed_files(&tmp_modified, buf.buf, rdir.buf);
645+
changed_files(repo, &tmp_modified, buf.buf, rdir.buf);
643646
add_path(&rdir, rdir_len, name);
644647
indices_loaded = 1;
645648
}
@@ -713,7 +716,7 @@ static int run_file_diff(int prompt, const char *prefix,
713716
int cmd_difftool(int argc,
714717
const char **argv,
715718
const char *prefix,
716-
struct repository *repo UNUSED)
719+
struct repository *repo)
717720
{
718721
int use_gui_tool = -1, dir_diff = 0, prompt = -1, tool_help = 0, no_index = 0;
719722
static char *difftool_cmd = NULL, *extcmd = NULL;
@@ -749,7 +752,8 @@ int cmd_difftool(int argc,
749752
};
750753
struct child_process child = CHILD_PROCESS_INIT;
751754

752-
git_config(difftool_config, &dt_options);
755+
if (repo)
756+
repo_config(repo, difftool_config, &dt_options);
753757
dt_options.symlinks = dt_options.has_symlinks;
754758

755759
argc = parse_options(argc, argv, prefix, builtin_difftool_options,
@@ -764,8 +768,8 @@ int cmd_difftool(int argc,
764768

765769
if (!no_index){
766770
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);
769773
} else if (dir_diff)
770774
die(_("options '%s' and '%s' cannot be used together"), "--dir-diff", "--no-index");
771775

@@ -814,6 +818,6 @@ int cmd_difftool(int argc,
814818
strvec_pushv(&child.args, argv);
815819

816820
if (dir_diff)
817-
return run_dir_diff(&dt_options, extcmd, prefix, &child);
821+
return run_dir_diff(repo, &dt_options, extcmd, prefix, &child);
818822
return run_file_diff(prompt, prefix, &child);
819823
}

0 commit comments

Comments
 (0)