Skip to content

Commit 4b33e60

Browse files
dreamergitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 829e5c3 commit 4b33e60

File tree

4 files changed

+58
-64
lines changed

4 files changed

+58
-64
lines changed

cache.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1047,8 +1047,6 @@ extern const struct object_id empty_tree_oid;
10471047
"\xe6\x9d\xe2\x9b\xb2\xd1\xd6\x43\x4b\x8b" \
10481048
"\x29\xae\x77\x5a\xd8\xc2\xe4\x8c\x53\x91"
10491049
extern const struct object_id empty_blob_oid;
1050-
#define EMPTY_BLOB_SHA1_BIN (empty_blob_oid.hash)
1051-
10521050

10531051
static inline int is_empty_blob_sha1(const unsigned char *sha1)
10541052
{

dir.c

Lines changed: 50 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -231,12 +231,10 @@ int within_depth(const char *name, int namelen,
231231
* 1 along with { data, size } of the (possibly augmented) buffer
232232
* when successful.
233233
*
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).
235235
*/
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)
240238
{
241239
enum object_type type;
242240
unsigned long sz;
@@ -251,9 +249,9 @@ static int do_read_blob(const struct object_id *oid,
251249
return -1;
252250
}
253251

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);
257255
}
258256

259257
if (sz == 0) {
@@ -654,9 +652,8 @@ void add_exclude(const char *string, const char *base,
654652

655653
static int read_skip_worktree_file_from_index(const struct index_state *istate,
656654
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)
660657
{
661658
int pos, len;
662659

@@ -667,7 +664,7 @@ static int read_skip_worktree_file_from_index(const struct index_state *istate,
667664
if (!ce_skip_worktree(istate->cache[pos]))
668665
return -1;
669666

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);
671668
}
672669

673670
/*
@@ -795,9 +792,8 @@ static int add_excludes_from_buffer(char *buf, size_t size,
795792
* ss_valid is non-zero, "ss" must contain good value as input.
796793
*/
797794
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)
801797
{
802798
struct stat st;
803799
int r;
@@ -815,16 +811,16 @@ static int add_excludes(const char *fname, const char *base, int baselen,
815811
return -1;
816812
r = read_skip_worktree_file_from_index(istate, fname,
817813
&size, &buf,
818-
sha1_stat);
814+
oid_stat);
819815
if (r != 1)
820816
return r;
821817
} else {
822818
size = xsize_t(st.st_size);
823819
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;
828824
}
829825
close(fd);
830826
return 0;
@@ -837,22 +833,23 @@ static int add_excludes(const char *fname, const char *base, int baselen,
837833
}
838834
buf[size++] = '\n';
839835
close(fd);
840-
if (sha1_stat) {
836+
if (oid_stat) {
841837
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))
844840
; /* no content change, ss->sha1 still good */
845841
else if (istate &&
846842
(pos = index_name_pos(istate, fname, strlen(fname))) >= 0 &&
847843
!ce_stage(istate->cache[pos]) &&
848844
ce_uptodate(istate->cache[pos]) &&
849845
!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);
852848
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;
856853
}
857854
}
858855

@@ -930,7 +927,7 @@ struct exclude_list *add_exclude_list(struct dir_struct *dir,
930927
* Used to set up core.excludesfile and .git/info/exclude lists.
931928
*/
932929
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)
934931
{
935932
struct exclude_list *el;
936933
/*
@@ -941,7 +938,7 @@ static void add_excludes_from_file_1(struct dir_struct *dir, const char *fname,
941938
if (!dir->untracked)
942939
dir->unmanaged_exclude_files++;
943940
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)
945942
die("cannot use %s as an exclude file", fname);
946943
}
947944

@@ -1180,7 +1177,7 @@ static void prep_exclude(struct dir_struct *dir,
11801177

11811178
while (current < baselen) {
11821179
const char *cp;
1183-
struct sha1_stat sha1_stat;
1180+
struct oid_stat oid_stat;
11841181

11851182
stk = xcalloc(1, sizeof(*stk));
11861183
if (current < 0) {
@@ -1223,8 +1220,8 @@ static void prep_exclude(struct dir_struct *dir,
12231220
}
12241221

12251222
/* 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;
12281225
if (dir->exclude_per_dir &&
12291226
/*
12301227
* If we know that no files have been added in
@@ -1252,7 +1249,7 @@ static void prep_exclude(struct dir_struct *dir,
12521249
strbuf_addstr(&sb, dir->exclude_per_dir);
12531250
el->src = strbuf_detach(&sb, NULL);
12541251
add_excludes(el->src, el->src, stk->baselen, el, istate,
1255-
untracked ? &sha1_stat : NULL);
1252+
untracked ? &oid_stat : NULL);
12561253
}
12571254
/*
12581255
* NEEDSWORK: when untracked cache is enabled, prep_exclude()
@@ -1269,9 +1266,9 @@ static void prep_exclude(struct dir_struct *dir,
12691266
* order, though, if you do that.
12701267
*/
12711268
if (untracked &&
1272-
hashcmp(sha1_stat.sha1, untracked->exclude_sha1)) {
1269+
hashcmp(oid_stat.oid.hash, untracked->exclude_sha1)) {
12731270
invalidate_gitignore(dir->untracked, untracked);
1274-
hashcpy(untracked->exclude_sha1, sha1_stat.sha1);
1271+
hashcpy(untracked->exclude_sha1, oid_stat.oid.hash);
12751272
}
12761273
dir->exclude_stack = stk;
12771274
current = stk->baselen;
@@ -2228,13 +2225,13 @@ static struct untracked_cache_dir *validate_untracked_cache(struct dir_struct *d
22282225

22292226
/* Validate $GIT_DIR/info/exclude and core.excludesfile */
22302227
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)) {
22332230
invalidate_gitignore(dir->untracked, root);
22342231
dir->untracked->ss_info_exclude = dir->ss_info_exclude;
22352232
}
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)) {
22382235
invalidate_gitignore(dir->untracked, root);
22392236
dir->untracked->ss_excludes_file = dir->ss_excludes_file;
22402237
}
@@ -2638,8 +2635,8 @@ void write_untracked_extension(struct strbuf *out, struct untracked_cache *untra
26382635
FLEX_ALLOC_MEM(ouc, exclude_per_dir, untracked->exclude_per_dir, len);
26392636
stat_data_to_disk(&ouc->info_exclude_stat, &untracked->ss_info_exclude.stat);
26402637
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);
26432640
ouc->dir_flags = htonl(untracked->dir_flags);
26442641

26452642
varint_len = encode_varint(untracked->ident.len, varbuf);
@@ -2816,13 +2813,12 @@ static void read_sha1(size_t pos, void *cb)
28162813
rd->data += 20;
28172814
}
28182815

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)
28222818
{
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;
28262822
}
28272823

28282824
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
28502846
uc = xcalloc(1, sizeof(*uc));
28512847
strbuf_init(&uc->ident, ident_len);
28522848
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));
28592855
uc->dir_flags = get_be32(next + ouc_offset(dir_flags));
28602856
exclude_per_dir = (const char *)next + ouc_offset(exclude_per_dir);
28612857
uc->exclude_per_dir = xstrdup(exclude_per_dir);

dir.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,9 @@ struct exclude_list_group {
7474
struct exclude_list *el;
7575
};
7676

77-
struct sha1_stat {
77+
struct oid_stat {
7878
struct stat_data stat;
79-
unsigned char sha1[20];
79+
struct object_id oid;
8080
int valid;
8181
};
8282

@@ -124,8 +124,8 @@ struct untracked_cache_dir {
124124
};
125125

126126
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;
129129
const char *exclude_per_dir;
130130
struct strbuf ident;
131131
/*
@@ -195,8 +195,8 @@ struct dir_struct {
195195

196196
/* Enable untracked file cache if set */
197197
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;
200200
unsigned unmanaged_exclude_files;
201201
};
202202

t/helper/test-dump-untracked-cache.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@ int cmd_main(int ac, const char **av)
5454
printf("no untracked cache\n");
5555
return 0;
5656
}
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));
5959
printf("exclude_per_dir %s\n", uc->exclude_per_dir);
6060
printf("flags %08x\n", uc->dir_flags);
6161
if (uc->root)

0 commit comments

Comments
 (0)