sha1-file.c: remove implicit dependency on the_index

Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Nguyễn Thái Ngọc Duy committed Sep 21, 2018 at 17:57 UTC 58bf2a4cc7a929d2fe0ff30eda86fcec54aef0f8
9 files changed +61 -48
builtin/difftool.c
+1 -1
@@ -112,7 +112,7 @@ static int use_wt_file(const char *workdir, const char *name,
112 int fd = open(buf.buf, O_RDONLY);
113
114 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)) {
116 if (is_null_oid(oid)) {
117 oidcpy(oid, &wt_oid);
118 use = 1;
builtin/hash-object.c
+1 -1
@@ -40,7 +40,7 @@ static void hash_fd(int fd, const char *type, const char *path, unsigned flags,
40 if (fstat(fd, &st) < 0 ||
41 (literally
42 ? 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)))
44 die((flags & HASH_WRITE_OBJECT)
45 ? "Unable to add %s to database"
46 : "Unable to hash %s", path);
builtin/replace.c
+1 -1
@@ -295,7 +295,7 @@ static int import_object(struct object_id *oid, enum object_type type,
295 close(fd);
296 return -1;
297 }
298 - if (index_fd(oid, fd, &st, type, NULL, flags) < 0)
298 + if (index_fd(&the_index, oid, fd, &st, type, NULL, flags) < 0)
299 return error(_("unable to write object to database"));
300 /* index_fd close()s fd for us */
301 }
builtin/update-index.c
+1 -1
@@ -282,7 +282,7 @@ static int add_one_path(const struct cache_entry *old, const char *path, int len
282 fill_stat_cache_info(ce, st);
283 ce->ce_mode = ce_mode_from_stat(old, st->st_mode);
284
285 - if (index_path(&ce->oid, path, st,
285 + if (index_path(&the_index, &ce->oid, path, st,
286 info_only ? 0 : HASH_WRITE_OBJECT)) {
287 discard_cache_entry(ce);
288 return -1;
cache.h
+2 -2
@@ -787,8 +787,8 @@ extern int ie_modified(struct index_state *, const struct cache_entry *, struct
787 #define HASH_WRITE_OBJECT 1
788 #define HASH_FORMAT_CHECK 2
789 #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);
792
793 /*
794 * Record to sd the data from st that we use to check whether a file
diff.c
+10 -10
@@ -4252,7 +4252,7 @@ static void run_diff_cmd(const char *pgm,
4252 fprintf(o->file, "* Unmerged path %s\n", name);
4253 }
4254
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)
4256 {
4257 if (DIFF_FILE_VALID(one)) {
4258 if (!one->oid_valid) {
@@ -4263,7 +4263,7 @@ static void diff_fill_oid_info(struct diff_filespec *one)
4263 }
4264 if (lstat(one->path, &st) < 0)
4265 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))
4267 die("cannot hash %s", one->path);
4268 }
4269 }
@@ -4311,8 +4311,8 @@ static void run_diff(struct diff_filepair *p, struct diff_options *o)
4311 return;
4312 }
4313
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);
4316
4317 if (!pgm &&
4318 DIFF_FILE_VALID(one) && DIFF_FILE_VALID(two) &&
@@ -4359,8 +4359,8 @@ static void run_diffstat(struct diff_filepair *p, struct diff_options *o,
4359 if (o->prefix_length)
4360 strip_prefix(o->prefix_length, &name, &other);
4361
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);
4364
4365 builtin_diffstat(name, other, p->one, p->two,
4366 diffstat, o, p);
@@ -4384,8 +4384,8 @@ static void run_checkdiff(struct diff_filepair *p, struct diff_options *o)
4384 if (o->prefix_length)
4385 strip_prefix(o->prefix_length, &name, &other);
4386
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);
4389
4390 builtin_checkdiff(name, other, attr_path, p->one, p->two, o);
4391 }
@@ -5685,8 +5685,8 @@ static int diff_get_patch_id(struct diff_options *options, struct object_id *oid
5685 if (DIFF_PAIR_UNMERGED(p))
5686 continue;
5687
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);
5690
5691 len1 = remove_space(p->one->path, strlen(p->one->path));
5692 len2 = remove_space(p->two->path, strlen(p->two->path));
notes-merge.c
+1 -1
@@ -710,7 +710,7 @@ int notes_merge_commit(struct notes_merge_options *o,
710 /* write file as blob, and add to partial_tree */
711 if (stat(path.buf, &st))
712 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))
714 die("Failed to write blob object from '%s'", path.buf);
715 if (add_note(partial_tree, &obj_oid, &blob_oid, NULL))
716 die("Failed to add resolved note '%s' to notes tree",
read-cache.c
+15 -10
@@ -205,14 +205,16 @@ void fill_stat_cache_info(struct cache_entry *ce, struct stat *st)
205 }
206 }
207
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)
211 {
212 int match = -1;
213 int fd = git_open_cloexec(ce->name, O_RDONLY);
214
215 if (fd >= 0) {
216 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))
218 match = oidcmp(&oid, &ce->oid);
219 /* index_fd() closed the file descriptor already */
220 }
@@ -257,11 +259,13 @@ static int ce_compare_gitlink(const struct cache_entry *ce)
259 return oidcmp(&oid, &ce->oid);
260 }
261
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)
265 {
266 switch (st->st_mode & S_IFMT) {
267 case S_IFREG:
264 - if (ce_compare_data(ce, st))
268 + if (ce_compare_data(istate, ce, st))
269 return DATA_CHANGED;
270 break;
271 case S_IFLNK:
@@ -407,7 +411,7 @@ int ie_match_stat(struct index_state *istate,
411 if (assume_racy_is_modified)
412 changed |= DATA_CHANGED;
413 else
410 - changed |= ce_modified_check_fs(ce, st);
414 + changed |= ce_modified_check_fs(istate, ce, st);
415 }
416
417 return changed;
@@ -447,7 +451,7 @@ int ie_modified(struct index_state *istate,
451 (S_ISGITLINK(ce->ce_mode) || ce->ce_stat_data.sd_size != 0))
452 return changed;
453
450 - changed_fs = ce_modified_check_fs(ce, st);
454 + changed_fs = ce_modified_check_fs(istate, ce, st);
455 if (changed_fs)
456 return changed | changed_fs;
457 return 0;
@@ -753,7 +757,7 @@ int add_to_index(struct index_state *istate, const char *path, struct stat *st,
757 }
758 }
759 if (!intent_only) {
756 - if (index_path(&ce->oid, path, st, newflags)) {
760 + if (index_path(istate, &ce->oid, path, st, newflags)) {
761 discard_cache_entry(ce);
762 return error("unable to index file %s", path);
763 }
@@ -2230,7 +2234,8 @@ static int ce_flush(git_hash_ctx *context, int fd, unsigned char *hash)
2234 return (write_in_full(fd, write_buffer, left) < 0) ? -1 : 0;
2235 }
2236
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)
2239 {
2240 /*
2241 * 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)
2254 return;
2255 if (ce_match_stat_basic(ce, &st))
2256 return;
2252 - if (ce_modified_check_fs(ce, &st)) {
2257 + if (ce_modified_check_fs(istate, ce, &st)) {
2258 /* This is "racily clean"; smudge it. Note that this
2259 * is a tricky code. At first glance, it may appear
2260 * that it can break with this sequence:
@@ -2494,7 +2499,7 @@ static int do_write_index(struct index_state *istate, struct tempfile *tempfile,
2499 if (ce->ce_flags & CE_REMOVE)
2500 continue;
2501 if (!ce_uptodate(ce) && is_racy_timestamp(istate, ce))
2497 - ce_smudge_racily_clean_entry(ce);
2502 + ce_smudge_racily_clean_entry(istate, ce);
2503 if (is_null_oid(&ce->oid)) {
2504 static const char msg[] = "cache entry has null sha1: %s";
2505 static int allow = -1;
sha1-file.c
+29 -21
@@ -1813,7 +1813,8 @@ static void check_tag(const void *buf, size_t size)
1813 die(_("corrupt tag"));
1814 }
1815
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,
1818 enum object_type type,
1819 const char *path, unsigned flags)
1820 {
@@ -1828,7 +1829,7 @@ static int index_mem(struct object_id *oid, void *buf, size_t size,
1829 */
1830 if ((type == OBJ_BLOB) && path) {
1831 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,
1833 get_conv_flags(flags))) {
1834 buf = strbuf_detach(&nbuf, &size);
1835 re_allocated = 1;
@@ -1852,17 +1853,20 @@ static int index_mem(struct object_id *oid, void *buf, size_t size,
1853 return ret;
1854 }
1855
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)
1861 {
1862 int ret;
1863 const int write_object = flags & HASH_WRITE_OBJECT;
1864 struct strbuf sbuf = STRBUF_INIT;
1865
1866 assert(path);
1863 - assert(would_convert_to_git_filter_fd(&the_index, path));
1867 + assert(would_convert_to_git_filter_fd(istate, path));
1868
1865 - convert_to_git_filter_fd(&the_index, path, fd, &sbuf,
1869 + convert_to_git_filter_fd(istate, path, fd, &sbuf,
1870 get_conv_flags(flags));
1871
1872 if (write_object)
@@ -1875,14 +1879,15 @@ static int index_stream_convert_blob(struct object_id *oid, int fd,
1879 return ret;
1880 }
1881
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,
1884 const char *path, unsigned flags)
1885 {
1886 struct strbuf sbuf = STRBUF_INIT;
1887 int ret;
1888
1889 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);
1891 else
1892 ret = -1;
1893 strbuf_release(&sbuf);
@@ -1891,14 +1896,15 @@ static int index_pipe(struct object_id *oid, int fd, enum object_type type,
1896
1897 #define SMALL_FILE_SIZE (32*1024)
1898
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,
1901 enum object_type type, const char *path,
1902 unsigned flags)
1903 {
1904 int ret;
1905
1906 if (!size) {
1901 - ret = index_mem(oid, "", size, type, path, flags);
1907 + ret = index_mem(istate, oid, "", size, type, path, flags);
1908 } else if (size <= SMALL_FILE_SIZE) {
1909 char *buf = xmalloc(size);
1910 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,
1915 ret = error(_("short read while indexing %s"),
1916 path ? path : "<unknown>");
1917 else
1912 - ret = index_mem(oid, buf, size, type, path, flags);
1918 + ret = index_mem(istate, oid, buf, size, type, path, flags);
1919 free(buf);
1920 } else {
1921 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);
1923 munmap(buf, size);
1924 }
1925 return ret;
@@ -1941,7 +1947,8 @@ static int index_stream(struct object_id *oid, int fd, size_t size,
1947 return index_bulk_checkin(oid, fd, size, type, path, flags);
1948 }
1949
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,
1952 enum object_type type, const char *path, unsigned flags)
1953 {
1954 int ret;
@@ -1950,14 +1957,14 @@ int index_fd(struct object_id *oid, int fd, struct stat *st,
1957 * Call xsize_t() only when needed to avoid potentially unnecessary
1958 * die() for large files.
1959 */
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);
1962 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);
1964 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);
1968 else
1969 ret = index_stream(oid, fd, xsize_t(st->st_size), type, path,
1970 flags);
@@ -1965,7 +1972,8 @@ int index_fd(struct object_id *oid, int fd, struct stat *st,
1972 return ret;
1973 }
1974
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)
1977 {
1978 int fd;
1979 struct strbuf sb = STRBUF_INIT;
@@ -1976,7 +1984,7 @@ int index_path(struct object_id *oid, const char *path, struct stat *st, unsigne
1984 fd = open(path, O_RDONLY);
1985 if (fd < 0)
1986 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)
1988 return error(_("%s: failed to insert into database"),
1989 path);
1990 break;