Skip to content

Commit 58bf2a4

Browse files
pcloudsgitster
authored andcommitted
sha1-file.c: remove implicit dependency on the_index
Signed-off-by: Nguyễn Thái Ngọc Duy <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent a7edadd commit 58bf2a4

File tree

9 files changed

+61
-48
lines changed

9 files changed

+61
-48
lines changed

builtin/difftool.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ static int use_wt_file(const char *workdir, const char *name,
112112
int fd = open(buf.buf, O_RDONLY);
113113

114114
if (fd >= 0 &&
115-
!index_fd(&wt_oid, fd, &st, OBJ_BLOB, name, 0)) {
115+
!index_fd(&the_index, &wt_oid, fd, &st, OBJ_BLOB, name, 0)) {
116116
if (is_null_oid(oid)) {
117117
oidcpy(oid, &wt_oid);
118118
use = 1;

builtin/hash-object.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ static void hash_fd(int fd, const char *type, const char *path, unsigned flags,
4040
if (fstat(fd, &st) < 0 ||
4141
(literally
4242
? hash_literally(&oid, fd, type, flags)
43-
: index_fd(&oid, fd, &st, type_from_string(type), path, flags)))
43+
: index_fd(&the_index, &oid, fd, &st, type_from_string(type), path, flags)))
4444
die((flags & HASH_WRITE_OBJECT)
4545
? "Unable to add %s to database"
4646
: "Unable to hash %s", path);

builtin/replace.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,7 @@ static int import_object(struct object_id *oid, enum object_type type,
295295
close(fd);
296296
return -1;
297297
}
298-
if (index_fd(oid, fd, &st, type, NULL, flags) < 0)
298+
if (index_fd(&the_index, oid, fd, &st, type, NULL, flags) < 0)
299299
return error(_("unable to write object to database"));
300300
/* index_fd close()s fd for us */
301301
}

builtin/update-index.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ static int add_one_path(const struct cache_entry *old, const char *path, int len
282282
fill_stat_cache_info(ce, st);
283283
ce->ce_mode = ce_mode_from_stat(old, st->st_mode);
284284

285-
if (index_path(&ce->oid, path, st,
285+
if (index_path(&the_index, &ce->oid, path, st,
286286
info_only ? 0 : HASH_WRITE_OBJECT)) {
287287
discard_cache_entry(ce);
288288
return -1;

cache.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -787,8 +787,8 @@ extern int ie_modified(struct index_state *, const struct cache_entry *, struct
787787
#define HASH_WRITE_OBJECT 1
788788
#define HASH_FORMAT_CHECK 2
789789
#define HASH_RENORMALIZE 4
790-
extern int index_fd(struct object_id *oid, int fd, struct stat *st, enum object_type type, const char *path, unsigned flags);
791-
extern int index_path(struct object_id *oid, const char *path, struct stat *st, unsigned flags);
790+
extern int index_fd(struct index_state *istate, struct object_id *oid, int fd, struct stat *st, enum object_type type, const char *path, unsigned flags);
791+
extern int index_path(struct index_state *istate, struct object_id *oid, const char *path, struct stat *st, unsigned flags);
792792

793793
/*
794794
* Record to sd the data from st that we use to check whether a file

diff.c

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4252,7 +4252,7 @@ static void run_diff_cmd(const char *pgm,
42524252
fprintf(o->file, "* Unmerged path %s\n", name);
42534253
}
42544254

4255-
static void diff_fill_oid_info(struct diff_filespec *one)
4255+
static void diff_fill_oid_info(struct diff_filespec *one, struct index_state *istate)
42564256
{
42574257
if (DIFF_FILE_VALID(one)) {
42584258
if (!one->oid_valid) {
@@ -4263,7 +4263,7 @@ static void diff_fill_oid_info(struct diff_filespec *one)
42634263
}
42644264
if (lstat(one->path, &st) < 0)
42654265
die_errno("stat '%s'", one->path);
4266-
if (index_path(&one->oid, one->path, &st, 0))
4266+
if (index_path(istate, &one->oid, one->path, &st, 0))
42674267
die("cannot hash %s", one->path);
42684268
}
42694269
}
@@ -4311,8 +4311,8 @@ static void run_diff(struct diff_filepair *p, struct diff_options *o)
43114311
return;
43124312
}
43134313

4314-
diff_fill_oid_info(one);
4315-
diff_fill_oid_info(two);
4314+
diff_fill_oid_info(one, o->repo->index);
4315+
diff_fill_oid_info(two, o->repo->index);
43164316

43174317
if (!pgm &&
43184318
DIFF_FILE_VALID(one) && DIFF_FILE_VALID(two) &&
@@ -4359,8 +4359,8 @@ static void run_diffstat(struct diff_filepair *p, struct diff_options *o,
43594359
if (o->prefix_length)
43604360
strip_prefix(o->prefix_length, &name, &other);
43614361

4362-
diff_fill_oid_info(p->one);
4363-
diff_fill_oid_info(p->two);
4362+
diff_fill_oid_info(p->one, o->repo->index);
4363+
diff_fill_oid_info(p->two, o->repo->index);
43644364

43654365
builtin_diffstat(name, other, p->one, p->two,
43664366
diffstat, o, p);
@@ -4384,8 +4384,8 @@ static void run_checkdiff(struct diff_filepair *p, struct diff_options *o)
43844384
if (o->prefix_length)
43854385
strip_prefix(o->prefix_length, &name, &other);
43864386

4387-
diff_fill_oid_info(p->one);
4388-
diff_fill_oid_info(p->two);
4387+
diff_fill_oid_info(p->one, o->repo->index);
4388+
diff_fill_oid_info(p->two, o->repo->index);
43894389

43904390
builtin_checkdiff(name, other, attr_path, p->one, p->two, o);
43914391
}
@@ -5685,8 +5685,8 @@ static int diff_get_patch_id(struct diff_options *options, struct object_id *oid
56855685
if (DIFF_PAIR_UNMERGED(p))
56865686
continue;
56875687

5688-
diff_fill_oid_info(p->one);
5689-
diff_fill_oid_info(p->two);
5688+
diff_fill_oid_info(p->one, options->repo->index);
5689+
diff_fill_oid_info(p->two, options->repo->index);
56905690

56915691
len1 = remove_space(p->one->path, strlen(p->one->path));
56925692
len2 = remove_space(p->two->path, strlen(p->two->path));

notes-merge.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -710,7 +710,7 @@ int notes_merge_commit(struct notes_merge_options *o,
710710
/* write file as blob, and add to partial_tree */
711711
if (stat(path.buf, &st))
712712
die_errno("Failed to stat '%s'", path.buf);
713-
if (index_path(&blob_oid, path.buf, &st, HASH_WRITE_OBJECT))
713+
if (index_path(&the_index, &blob_oid, path.buf, &st, HASH_WRITE_OBJECT))
714714
die("Failed to write blob object from '%s'", path.buf);
715715
if (add_note(partial_tree, &obj_oid, &blob_oid, NULL))
716716
die("Failed to add resolved note '%s' to notes tree",

read-cache.c

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -205,14 +205,16 @@ void fill_stat_cache_info(struct cache_entry *ce, struct stat *st)
205205
}
206206
}
207207

208-
static int ce_compare_data(const struct cache_entry *ce, struct stat *st)
208+
static int ce_compare_data(struct index_state *istate,
209+
const struct cache_entry *ce,
210+
struct stat *st)
209211
{
210212
int match = -1;
211213
int fd = git_open_cloexec(ce->name, O_RDONLY);
212214

213215
if (fd >= 0) {
214216
struct object_id oid;
215-
if (!index_fd(&oid, fd, st, OBJ_BLOB, ce->name, 0))
217+
if (!index_fd(istate, &oid, fd, st, OBJ_BLOB, ce->name, 0))
216218
match = oidcmp(&oid, &ce->oid);
217219
/* index_fd() closed the file descriptor already */
218220
}
@@ -257,11 +259,13 @@ static int ce_compare_gitlink(const struct cache_entry *ce)
257259
return oidcmp(&oid, &ce->oid);
258260
}
259261

260-
static int ce_modified_check_fs(const struct cache_entry *ce, struct stat *st)
262+
static int ce_modified_check_fs(struct index_state *istate,
263+
const struct cache_entry *ce,
264+
struct stat *st)
261265
{
262266
switch (st->st_mode & S_IFMT) {
263267
case S_IFREG:
264-
if (ce_compare_data(ce, st))
268+
if (ce_compare_data(istate, ce, st))
265269
return DATA_CHANGED;
266270
break;
267271
case S_IFLNK:
@@ -407,7 +411,7 @@ int ie_match_stat(struct index_state *istate,
407411
if (assume_racy_is_modified)
408412
changed |= DATA_CHANGED;
409413
else
410-
changed |= ce_modified_check_fs(ce, st);
414+
changed |= ce_modified_check_fs(istate, ce, st);
411415
}
412416

413417
return changed;
@@ -447,7 +451,7 @@ int ie_modified(struct index_state *istate,
447451
(S_ISGITLINK(ce->ce_mode) || ce->ce_stat_data.sd_size != 0))
448452
return changed;
449453

450-
changed_fs = ce_modified_check_fs(ce, st);
454+
changed_fs = ce_modified_check_fs(istate, ce, st);
451455
if (changed_fs)
452456
return changed | changed_fs;
453457
return 0;
@@ -753,7 +757,7 @@ int add_to_index(struct index_state *istate, const char *path, struct stat *st,
753757
}
754758
}
755759
if (!intent_only) {
756-
if (index_path(&ce->oid, path, st, newflags)) {
760+
if (index_path(istate, &ce->oid, path, st, newflags)) {
757761
discard_cache_entry(ce);
758762
return error("unable to index file %s", path);
759763
}
@@ -2230,7 +2234,8 @@ static int ce_flush(git_hash_ctx *context, int fd, unsigned char *hash)
22302234
return (write_in_full(fd, write_buffer, left) < 0) ? -1 : 0;
22312235
}
22322236

2233-
static void ce_smudge_racily_clean_entry(struct cache_entry *ce)
2237+
static void ce_smudge_racily_clean_entry(struct index_state *istate,
2238+
struct cache_entry *ce)
22342239
{
22352240
/*
22362241
* The only thing we care about in this function is to smudge the
@@ -2249,7 +2254,7 @@ static void ce_smudge_racily_clean_entry(struct cache_entry *ce)
22492254
return;
22502255
if (ce_match_stat_basic(ce, &st))
22512256
return;
2252-
if (ce_modified_check_fs(ce, &st)) {
2257+
if (ce_modified_check_fs(istate, ce, &st)) {
22532258
/* This is "racily clean"; smudge it. Note that this
22542259
* is a tricky code. At first glance, it may appear
22552260
* that it can break with this sequence:
@@ -2494,7 +2499,7 @@ static int do_write_index(struct index_state *istate, struct tempfile *tempfile,
24942499
if (ce->ce_flags & CE_REMOVE)
24952500
continue;
24962501
if (!ce_uptodate(ce) && is_racy_timestamp(istate, ce))
2497-
ce_smudge_racily_clean_entry(ce);
2502+
ce_smudge_racily_clean_entry(istate, ce);
24982503
if (is_null_oid(&ce->oid)) {
24992504
static const char msg[] = "cache entry has null sha1: %s";
25002505
static int allow = -1;

sha1-file.c

Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1813,7 +1813,8 @@ static void check_tag(const void *buf, size_t size)
18131813
die(_("corrupt tag"));
18141814
}
18151815

1816-
static int index_mem(struct object_id *oid, void *buf, size_t size,
1816+
static int index_mem(struct index_state *istate,
1817+
struct object_id *oid, void *buf, size_t size,
18171818
enum object_type type,
18181819
const char *path, unsigned flags)
18191820
{
@@ -1828,7 +1829,7 @@ static int index_mem(struct object_id *oid, void *buf, size_t size,
18281829
*/
18291830
if ((type == OBJ_BLOB) && path) {
18301831
struct strbuf nbuf = STRBUF_INIT;
1831-
if (convert_to_git(&the_index, path, buf, size, &nbuf,
1832+
if (convert_to_git(istate, path, buf, size, &nbuf,
18321833
get_conv_flags(flags))) {
18331834
buf = strbuf_detach(&nbuf, &size);
18341835
re_allocated = 1;
@@ -1852,17 +1853,20 @@ static int index_mem(struct object_id *oid, void *buf, size_t size,
18521853
return ret;
18531854
}
18541855

1855-
static int index_stream_convert_blob(struct object_id *oid, int fd,
1856-
const char *path, unsigned flags)
1856+
static int index_stream_convert_blob(struct index_state *istate,
1857+
struct object_id *oid,
1858+
int fd,
1859+
const char *path,
1860+
unsigned flags)
18571861
{
18581862
int ret;
18591863
const int write_object = flags & HASH_WRITE_OBJECT;
18601864
struct strbuf sbuf = STRBUF_INIT;
18611865

18621866
assert(path);
1863-
assert(would_convert_to_git_filter_fd(&the_index, path));
1867+
assert(would_convert_to_git_filter_fd(istate, path));
18641868

1865-
convert_to_git_filter_fd(&the_index, path, fd, &sbuf,
1869+
convert_to_git_filter_fd(istate, path, fd, &sbuf,
18661870
get_conv_flags(flags));
18671871

18681872
if (write_object)
@@ -1875,14 +1879,15 @@ static int index_stream_convert_blob(struct object_id *oid, int fd,
18751879
return ret;
18761880
}
18771881

1878-
static int index_pipe(struct object_id *oid, int fd, enum object_type type,
1882+
static int index_pipe(struct index_state *istate, struct object_id *oid,
1883+
int fd, enum object_type type,
18791884
const char *path, unsigned flags)
18801885
{
18811886
struct strbuf sbuf = STRBUF_INIT;
18821887
int ret;
18831888

18841889
if (strbuf_read(&sbuf, fd, 4096) >= 0)
1885-
ret = index_mem(oid, sbuf.buf, sbuf.len, type, path, flags);
1890+
ret = index_mem(istate, oid, sbuf.buf, sbuf.len, type, path, flags);
18861891
else
18871892
ret = -1;
18881893
strbuf_release(&sbuf);
@@ -1891,14 +1896,15 @@ static int index_pipe(struct object_id *oid, int fd, enum object_type type,
18911896

18921897
#define SMALL_FILE_SIZE (32*1024)
18931898

1894-
static int index_core(struct object_id *oid, int fd, size_t size,
1899+
static int index_core(struct index_state *istate,
1900+
struct object_id *oid, int fd, size_t size,
18951901
enum object_type type, const char *path,
18961902
unsigned flags)
18971903
{
18981904
int ret;
18991905

19001906
if (!size) {
1901-
ret = index_mem(oid, "", size, type, path, flags);
1907+
ret = index_mem(istate, oid, "", size, type, path, flags);
19021908
} else if (size <= SMALL_FILE_SIZE) {
19031909
char *buf = xmalloc(size);
19041910
ssize_t read_result = read_in_full(fd, buf, size);
@@ -1909,11 +1915,11 @@ static int index_core(struct object_id *oid, int fd, size_t size,
19091915
ret = error(_("short read while indexing %s"),
19101916
path ? path : "<unknown>");
19111917
else
1912-
ret = index_mem(oid, buf, size, type, path, flags);
1918+
ret = index_mem(istate, oid, buf, size, type, path, flags);
19131919
free(buf);
19141920
} else {
19151921
void *buf = xmmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0);
1916-
ret = index_mem(oid, buf, size, type, path, flags);
1922+
ret = index_mem(istate, oid, buf, size, type, path, flags);
19171923
munmap(buf, size);
19181924
}
19191925
return ret;
@@ -1941,7 +1947,8 @@ static int index_stream(struct object_id *oid, int fd, size_t size,
19411947
return index_bulk_checkin(oid, fd, size, type, path, flags);
19421948
}
19431949

1944-
int index_fd(struct object_id *oid, int fd, struct stat *st,
1950+
int index_fd(struct index_state *istate, struct object_id *oid,
1951+
int fd, struct stat *st,
19451952
enum object_type type, const char *path, unsigned flags)
19461953
{
19471954
int ret;
@@ -1950,22 +1957,23 @@ int index_fd(struct object_id *oid, int fd, struct stat *st,
19501957
* Call xsize_t() only when needed to avoid potentially unnecessary
19511958
* die() for large files.
19521959
*/
1953-
if (type == OBJ_BLOB && path && would_convert_to_git_filter_fd(&the_index, path))
1954-
ret = index_stream_convert_blob(oid, fd, path, flags);
1960+
if (type == OBJ_BLOB && path && would_convert_to_git_filter_fd(istate, path))
1961+
ret = index_stream_convert_blob(istate, oid, fd, path, flags);
19551962
else if (!S_ISREG(st->st_mode))
1956-
ret = index_pipe(oid, fd, type, path, flags);
1963+
ret = index_pipe(istate, oid, fd, type, path, flags);
19571964
else if (st->st_size <= big_file_threshold || type != OBJ_BLOB ||
1958-
(path && would_convert_to_git(&the_index, path)))
1959-
ret = index_core(oid, fd, xsize_t(st->st_size), type, path,
1960-
flags);
1965+
(path && would_convert_to_git(istate, path)))
1966+
ret = index_core(istate, oid, fd, xsize_t(st->st_size),
1967+
type, path, flags);
19611968
else
19621969
ret = index_stream(oid, fd, xsize_t(st->st_size), type, path,
19631970
flags);
19641971
close(fd);
19651972
return ret;
19661973
}
19671974

1968-
int index_path(struct object_id *oid, const char *path, struct stat *st, unsigned flags)
1975+
int index_path(struct index_state *istate, struct object_id *oid,
1976+
const char *path, struct stat *st, unsigned flags)
19691977
{
19701978
int fd;
19711979
struct strbuf sb = STRBUF_INIT;
@@ -1976,7 +1984,7 @@ int index_path(struct object_id *oid, const char *path, struct stat *st, unsigne
19761984
fd = open(path, O_RDONLY);
19771985
if (fd < 0)
19781986
return error_errno("open(\"%s\")", path);
1979-
if (index_fd(oid, fd, st, OBJ_BLOB, path, flags) < 0)
1987+
if (index_fd(istate, oid, fd, st, OBJ_BLOB, path, flags) < 0)
19801988
return error(_("%s: failed to insert into database"),
19811989
path);
19821990
break;

0 commit comments

Comments
 (0)