dir: convert struct sha1_stat to use object_id
Convert the declaration of struct sha1_stat. Adjust all usages of this struct and replace hash{clr,cmp,cpy} with oid{clr,cmp,cpy} wherever possible. Rename it to struct oid_stat. Rename static function load_sha1_stat to load_oid_stat. Remove macro EMPTY_BLOB_SHA1_BIN, as it's no longer used. Signed-off-by: Patryk Obara <patryk.obara@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Patryk Obara committed
Jan 28, 2018 at 01:13 UTC
4b33e60201c12f3a68441807444e3726853a3960
4 files changed
+58
-64
cache.h
-2
@@ -1047,8 +1047,6 @@ extern const struct object_id empty_tree_oid;
1047
"\xe6\x9d\xe2\x9b\xb2\xd1\xd6\x43\x4b\x8b" \
1048
"\x29\xae\x77\x5a\xd8\xc2\xe4\x8c\x53\x91"
1049
extern const struct object_id empty_blob_oid;
1050
-#define EMPTY_BLOB_SHA1_BIN (empty_blob_oid.hash)
1051
-
1050
1051
static inline int is_empty_blob_sha1(const unsigned char *sha1)
1052
{
dir.c
+50
-54
@@ -231,12 +231,10 @@ int within_depth(const char *name, int namelen,
231
* 1 along with { data, size } of the (possibly augmented) buffer
232
* when successful.
233
*
234
- * Optionally updates the given sha1_stat with the given OID (when valid).
234
+ * Optionally updates the given oid_stat with the given OID (when valid).
235
*/
236
-static int do_read_blob(const struct object_id *oid,
237
- struct sha1_stat *sha1_stat,
238
- size_t *size_out,
239
- char **data_out)
236
+static int do_read_blob(const struct object_id *oid, struct oid_stat *oid_stat,
237
+ size_t *size_out, char **data_out)
238
{
239
enum object_type type;
240
unsigned long sz;
@@ -251,9 +249,9 @@ static int do_read_blob(const struct object_id *oid,
249
return -1;
250
}
251
254
- if (sha1_stat) {
255
- memset(&sha1_stat->stat, 0, sizeof(sha1_stat->stat));
256
- hashcpy(sha1_stat->sha1, oid->hash);
252
+ if (oid_stat) {
253
+ memset(&oid_stat->stat, 0, sizeof(oid_stat->stat));
254
+ oidcpy(&oid_stat->oid, oid);
255
}
256
257
if (sz == 0) {
@@ -654,9 +652,8 @@ void add_exclude(const char *string, const char *base,
652
653
static int read_skip_worktree_file_from_index(const struct index_state *istate,
654
const char *path,
657
- size_t *size_out,
658
- char **data_out,
659
- struct sha1_stat *sha1_stat)
655
+ size_t *size_out, char **data_out,
656
+ struct oid_stat *oid_stat)
657
{
658
int pos, len;
659
@@ -667,7 +664,7 @@ static int read_skip_worktree_file_from_index(const struct index_state *istate,
664
if (!ce_skip_worktree(istate->cache[pos]))
665
return -1;
666
670
- return do_read_blob(&istate->cache[pos]->oid, sha1_stat, size_out, data_out);
667
+ return do_read_blob(&istate->cache[pos]->oid, oid_stat, size_out, data_out);
668
}
669
670
/*
@@ -795,9 +792,8 @@ static int add_excludes_from_buffer(char *buf, size_t size,
792
* ss_valid is non-zero, "ss" must contain good value as input.
793
*/
794
static int add_excludes(const char *fname, const char *base, int baselen,
798
- struct exclude_list *el,
799
- struct index_state *istate,
800
- struct sha1_stat *sha1_stat)
795
+ struct exclude_list *el, struct index_state *istate,
796
+ struct oid_stat *oid_stat)
797
{
798
struct stat st;
799
int r;
@@ -815,16 +811,16 @@ static int add_excludes(const char *fname, const char *base, int baselen,
811
return -1;
812
r = read_skip_worktree_file_from_index(istate, fname,
813
&size, &buf,
818
- sha1_stat);
814
+ oid_stat);
815
if (r != 1)
816
return r;
817
} else {
818
size = xsize_t(st.st_size);
819
if (size == 0) {
824
- if (sha1_stat) {
825
- fill_stat_data(&sha1_stat->stat, &st);
826
- hashcpy(sha1_stat->sha1, EMPTY_BLOB_SHA1_BIN);
827
- sha1_stat->valid = 1;
820
+ if (oid_stat) {
821
+ fill_stat_data(&oid_stat->stat, &st);
822
+ oidcpy(&oid_stat->oid, &empty_blob_oid);
823
+ oid_stat->valid = 1;
824
}
825
close(fd);
826
return 0;
@@ -837,22 +833,23 @@ static int add_excludes(const char *fname, const char *base, int baselen,
833
}
834
buf[size++] = '\n';
835
close(fd);
840
- if (sha1_stat) {
836
+ if (oid_stat) {
837
int pos;
842
- if (sha1_stat->valid &&
843
- !match_stat_data_racy(istate, &sha1_stat->stat, &st))
838
+ if (oid_stat->valid &&
839
+ !match_stat_data_racy(istate, &oid_stat->stat, &st))
840
; /* no content change, ss->sha1 still good */
841
else if (istate &&
842
(pos = index_name_pos(istate, fname, strlen(fname))) >= 0 &&
843
!ce_stage(istate->cache[pos]) &&
844
ce_uptodate(istate->cache[pos]) &&
845
!would_convert_to_git(istate, fname))
850
- hashcpy(sha1_stat->sha1,
851
- istate->cache[pos]->oid.hash);
846
+ oidcpy(&oid_stat->oid,
847
+ &istate->cache[pos]->oid);
848
else
853
- hash_sha1_file(buf, size, "blob", sha1_stat->sha1);
854
- fill_stat_data(&sha1_stat->stat, &st);
855
- sha1_stat->valid = 1;
849
+ hash_sha1_file(buf, size, "blob",
850
+ oid_stat->oid.hash);
851
+ fill_stat_data(&oid_stat->stat, &st);
852
+ oid_stat->valid = 1;
853
}
854
}
855
@@ -930,7 +927,7 @@ struct exclude_list *add_exclude_list(struct dir_struct *dir,
927
* Used to set up core.excludesfile and .git/info/exclude lists.
928
*/
929
static void add_excludes_from_file_1(struct dir_struct *dir, const char *fname,
933
- struct sha1_stat *sha1_stat)
930
+ struct oid_stat *oid_stat)
931
{
932
struct exclude_list *el;
933
/*
@@ -941,7 +938,7 @@ static void add_excludes_from_file_1(struct dir_struct *dir, const char *fname,
938
if (!dir->untracked)
939
dir->unmanaged_exclude_files++;
940
el = add_exclude_list(dir, EXC_FILE, fname);
944
- if (add_excludes(fname, "", 0, el, NULL, sha1_stat) < 0)
941
+ if (add_excludes(fname, "", 0, el, NULL, oid_stat) < 0)
942
die("cannot use %s as an exclude file", fname);
943
}
944
@@ -1180,7 +1177,7 @@ static void prep_exclude(struct dir_struct *dir,
1177
1178
while (current < baselen) {
1179
const char *cp;
1183
- struct sha1_stat sha1_stat;
1180
+ struct oid_stat oid_stat;
1181
1182
stk = xcalloc(1, sizeof(*stk));
1183
if (current < 0) {
@@ -1223,8 +1220,8 @@ static void prep_exclude(struct dir_struct *dir,
1220
}
1221
1222
/* Try to read per-directory file */
1226
- hashclr(sha1_stat.sha1);
1227
- sha1_stat.valid = 0;
1223
+ oidclr(&oid_stat.oid);
1224
+ oid_stat.valid = 0;
1225
if (dir->exclude_per_dir &&
1226
/*
1227
* If we know that no files have been added in
@@ -1252,7 +1249,7 @@ static void prep_exclude(struct dir_struct *dir,
1249
strbuf_addstr(&sb, dir->exclude_per_dir);
1250
el->src = strbuf_detach(&sb, NULL);
1251
add_excludes(el->src, el->src, stk->baselen, el, istate,
1255
- untracked ? &sha1_stat : NULL);
1252
+ untracked ? &oid_stat : NULL);
1253
}
1254
/*
1255
* NEEDSWORK: when untracked cache is enabled, prep_exclude()
@@ -1269,9 +1266,9 @@ static void prep_exclude(struct dir_struct *dir,
1266
* order, though, if you do that.
1267
*/
1268
if (untracked &&
1272
- hashcmp(sha1_stat.sha1, untracked->exclude_sha1)) {
1269
+ hashcmp(oid_stat.oid.hash, untracked->exclude_sha1)) {
1270
invalidate_gitignore(dir->untracked, untracked);
1274
- hashcpy(untracked->exclude_sha1, sha1_stat.sha1);
1271
+ hashcpy(untracked->exclude_sha1, oid_stat.oid.hash);
1272
}
1273
dir->exclude_stack = stk;
1274
current = stk->baselen;
@@ -2228,13 +2225,13 @@ static struct untracked_cache_dir *validate_untracked_cache(struct dir_struct *d
2225
2226
/* Validate $GIT_DIR/info/exclude and core.excludesfile */
2227
root = dir->untracked->root;
2231
- if (hashcmp(dir->ss_info_exclude.sha1,
2232
- dir->untracked->ss_info_exclude.sha1)) {
2228
+ if (oidcmp(&dir->ss_info_exclude.oid,
2229
+ &dir->untracked->ss_info_exclude.oid)) {
2230
invalidate_gitignore(dir->untracked, root);
2231
dir->untracked->ss_info_exclude = dir->ss_info_exclude;
2232
}
2236
- if (hashcmp(dir->ss_excludes_file.sha1,
2237
- dir->untracked->ss_excludes_file.sha1)) {
2233
+ if (oidcmp(&dir->ss_excludes_file.oid,
2234
+ &dir->untracked->ss_excludes_file.oid)) {
2235
invalidate_gitignore(dir->untracked, root);
2236
dir->untracked->ss_excludes_file = dir->ss_excludes_file;
2237
}
@@ -2638,8 +2635,8 @@ void write_untracked_extension(struct strbuf *out, struct untracked_cache *untra
2635
FLEX_ALLOC_MEM(ouc, exclude_per_dir, untracked->exclude_per_dir, len);
2636
stat_data_to_disk(&ouc->info_exclude_stat, &untracked->ss_info_exclude.stat);
2637
stat_data_to_disk(&ouc->excludes_file_stat, &untracked->ss_excludes_file.stat);
2641
- hashcpy(ouc->info_exclude_sha1, untracked->ss_info_exclude.sha1);
2642
- hashcpy(ouc->excludes_file_sha1, untracked->ss_excludes_file.sha1);
2638
+ hashcpy(ouc->info_exclude_sha1, untracked->ss_info_exclude.oid.hash);
2639
+ hashcpy(ouc->excludes_file_sha1, untracked->ss_excludes_file.oid.hash);
2640
ouc->dir_flags = htonl(untracked->dir_flags);
2641
2642
varint_len = encode_varint(untracked->ident.len, varbuf);
@@ -2816,13 +2813,12 @@ static void read_sha1(size_t pos, void *cb)
2813
rd->data += 20;
2814
}
2815
2819
-static void load_sha1_stat(struct sha1_stat *sha1_stat,
2820
- const unsigned char *data,
2821
- const unsigned char *sha1)
2816
+static void load_oid_stat(struct oid_stat *oid_stat, const unsigned char *data,
2817
+ const unsigned char *sha1)
2818
{
2823
- stat_data_from_disk(&sha1_stat->stat, data);
2824
- hashcpy(sha1_stat->sha1, sha1);
2825
- sha1_stat->valid = 1;
2819
+ stat_data_from_disk(&oid_stat->stat, data);
2820
+ hashcpy(oid_stat->oid.hash, sha1);
2821
+ oid_stat->valid = 1;
2822
}
2823
2824
struct untracked_cache *read_untracked_extension(const void *data, unsigned long sz)
@@ -2850,12 +2846,12 @@ struct untracked_cache *read_untracked_extension(const void *data, unsigned long
2846
uc = xcalloc(1, sizeof(*uc));
2847
strbuf_init(&uc->ident, ident_len);
2848
strbuf_add(&uc->ident, ident, ident_len);
2853
- load_sha1_stat(&uc->ss_info_exclude,
2854
- next + ouc_offset(info_exclude_stat),
2855
- next + ouc_offset(info_exclude_sha1));
2856
- load_sha1_stat(&uc->ss_excludes_file,
2857
- next + ouc_offset(excludes_file_stat),
2858
- next + ouc_offset(excludes_file_sha1));
2849
+ load_oid_stat(&uc->ss_info_exclude,
2850
+ next + ouc_offset(info_exclude_stat),
2851
+ next + ouc_offset(info_exclude_sha1));
2852
+ load_oid_stat(&uc->ss_excludes_file,
2853
+ next + ouc_offset(excludes_file_stat),
2854
+ next + ouc_offset(excludes_file_sha1));
2855
uc->dir_flags = get_be32(next + ouc_offset(dir_flags));
2856
exclude_per_dir = (const char *)next + ouc_offset(exclude_per_dir);
2857
uc->exclude_per_dir = xstrdup(exclude_per_dir);
dir.h
+6
-6
@@ -74,9 +74,9 @@ struct exclude_list_group {
74
struct exclude_list *el;
75
};
76
77
-struct sha1_stat {
77
+struct oid_stat {
78
struct stat_data stat;
79
- unsigned char sha1[20];
79
+ struct object_id oid;
80
int valid;
81
};
82
@@ -124,8 +124,8 @@ struct untracked_cache_dir {
124
};
125
126
struct untracked_cache {
127
- struct sha1_stat ss_info_exclude;
128
- struct sha1_stat ss_excludes_file;
127
+ struct oid_stat ss_info_exclude;
128
+ struct oid_stat ss_excludes_file;
129
const char *exclude_per_dir;
130
struct strbuf ident;
131
/*
@@ -195,8 +195,8 @@ struct dir_struct {
195
196
/* Enable untracked file cache if set */
197
struct untracked_cache *untracked;
198
- struct sha1_stat ss_info_exclude;
199
- struct sha1_stat ss_excludes_file;
198
+ struct oid_stat ss_info_exclude;
199
+ struct oid_stat ss_excludes_file;
200
unsigned unmanaged_exclude_files;
201
};
202
t/helper/test-dump-untracked-cache.c
+2
-2
@@ -54,8 +54,8 @@ int cmd_main(int ac, const char **av)
54
printf("no untracked cache\n");
55
return 0;
56
}
57
- printf("info/exclude %s\n", sha1_to_hex(uc->ss_info_exclude.sha1));
58
- printf("core.excludesfile %s\n", sha1_to_hex(uc->ss_excludes_file.sha1));
57
+ printf("info/exclude %s\n", oid_to_hex(&uc->ss_info_exclude.oid));
58
+ printf("core.excludesfile %s\n", oid_to_hex(&uc->ss_excludes_file.oid));
59
printf("exclude_per_dir %s\n", uc->exclude_per_dir);
60
printf("flags %08x\n", uc->dir_flags);
61
if (uc->root)