Skip to content

Commit c4ce46f

Browse files
committed
index_fd(): turn write_object and format_check arguments into one flag
The "format_check" parameter tucked after the existing parameters is too ugly an afterthought to live in any reasonable API. Combine it with the other boolean parameter "write_object" into a single "flags" parameter. Signed-off-by: Junio C Hamano <[email protected]>
1 parent 9cedd16 commit c4ce46f

File tree

6 files changed

+27
-23
lines changed

6 files changed

+27
-23
lines changed

builtin/hash-object.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,11 @@ static void hash_fd(int fd, const char *type, int write_object, const char *path
1414
{
1515
struct stat st;
1616
unsigned char sha1[20];
17+
unsigned flags = (HASH_FORMAT_CHECK |
18+
(write_object ? HASH_WRITE_OBJECT : 0));
19+
1720
if (fstat(fd, &st) < 0 ||
18-
index_fd(sha1, fd, &st, write_object, type_from_string(type), path, 1))
21+
index_fd(sha1, fd, &st, type_from_string(type), path, flags))
1922
die(write_object
2023
? "Unable to add %s to database"
2124
: "Unable to hash %s", path);

builtin/update-index.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,8 @@ static int add_one_path(struct cache_entry *old, const char *path, int len, stru
9999
fill_stat_cache_info(ce, st);
100100
ce->ce_mode = ce_mode_from_stat(old, st->st_mode);
101101

102-
if (index_path(ce->sha1, path, st, !info_only))
102+
if (index_path(ce->sha1, path, st,
103+
info_only ? 0 : HASH_WRITE_OBJECT))
103104
return -1;
104105
option = allow_add ? ADD_CACHE_OK_TO_ADD : 0;
105106
option |= allow_replace ? ADD_CACHE_OK_TO_REPLACE : 0;

cache.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -518,8 +518,11 @@ struct pathspec {
518518
extern int init_pathspec(struct pathspec *, const char **);
519519
extern void free_pathspec(struct pathspec *);
520520
extern int ce_path_match(const struct cache_entry *ce, const struct pathspec *pathspec);
521-
extern int index_fd(unsigned char *sha1, int fd, struct stat *st, int write_object, enum object_type type, const char *path, int format_check);
522-
extern int index_path(unsigned char *sha1, const char *path, struct stat *st, int write_object);
521+
522+
#define HASH_WRITE_OBJECT 1
523+
#define HASH_FORMAT_CHECK 2
524+
extern int index_fd(unsigned char *sha1, int fd, struct stat *st, enum object_type type, const char *path, unsigned flags);
525+
extern int index_path(unsigned char *sha1, const char *path, struct stat *st, unsigned flags);
523526
extern void fill_stat_cache_info(struct cache_entry *ce, struct stat *st);
524527

525528
#define REFRESH_REALLY 0x0001 /* ignore_valid */

notes-merge.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -707,7 +707,7 @@ int notes_merge_commit(struct notes_merge_options *o,
707707
/* write file as blob, and add to partial_tree */
708708
if (stat(ent->name, &st))
709709
die_errno("Failed to stat '%s'", ent->name);
710-
if (index_path(blob_sha1, ent->name, &st, 1))
710+
if (index_path(blob_sha1, ent->name, &st, HASH_WRITE_OBJECT))
711711
die("Failed to write blob object from '%s'", ent->name);
712712
if (add_note(partial_tree, obj_sha1, blob_sha1, NULL))
713713
die("Failed to add resolved note '%s' to notes tree",

read-cache.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ static int ce_compare_data(struct cache_entry *ce, struct stat *st)
9292

9393
if (fd >= 0) {
9494
unsigned char sha1[20];
95-
if (!index_fd(sha1, fd, st, 0, OBJ_BLOB, ce->name, 0))
95+
if (!index_fd(sha1, fd, st, OBJ_BLOB, ce->name, 0))
9696
match = hashcmp(sha1, ce->sha1);
9797
/* index_fd() closed the file descriptor already */
9898
}
@@ -641,7 +641,7 @@ int add_to_index(struct index_state *istate, const char *path, struct stat *st,
641641
return 0;
642642
}
643643
if (!intent_only) {
644-
if (index_path(ce->sha1, path, st, 1))
644+
if (index_path(ce->sha1, path, st, HASH_WRITE_OBJECT))
645645
return error("unable to index file %s", path);
646646
} else
647647
record_intent_to_add(ce);

sha1_file.c

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2581,10 +2581,11 @@ static void check_tag(const void *buf, size_t size)
25812581
}
25822582

25832583
static int index_mem(unsigned char *sha1, void *buf, size_t size,
2584-
int write_object, enum object_type type,
2585-
const char *path, int format_check)
2584+
enum object_type type,
2585+
const char *path, unsigned flags)
25862586
{
25872587
int ret, re_allocated = 0;
2588+
int write_object = flags & HASH_WRITE_OBJECT;
25882589

25892590
if (!type)
25902591
type = OBJ_BLOB;
@@ -2600,7 +2601,7 @@ static int index_mem(unsigned char *sha1, void *buf, size_t size,
26002601
re_allocated = 1;
26012602
}
26022603
}
2603-
if (format_check) {
2604+
if (flags & HASH_FORMAT_CHECK) {
26042605
if (type == OBJ_TREE)
26052606
check_tree(buf, size);
26062607
if (type == OBJ_COMMIT)
@@ -2620,42 +2621,38 @@ static int index_mem(unsigned char *sha1, void *buf, size_t size,
26202621

26212622
#define SMALL_FILE_SIZE (32*1024)
26222623

2623-
int index_fd(unsigned char *sha1, int fd, struct stat *st, int write_object,
2624-
enum object_type type, const char *path, int format_check)
2624+
int index_fd(unsigned char *sha1, int fd, struct stat *st,
2625+
enum object_type type, const char *path, unsigned flags)
26252626
{
26262627
int ret;
26272628
size_t size = xsize_t(st->st_size);
26282629

26292630
if (!S_ISREG(st->st_mode)) {
26302631
struct strbuf sbuf = STRBUF_INIT;
26312632
if (strbuf_read(&sbuf, fd, 4096) >= 0)
2632-
ret = index_mem(sha1, sbuf.buf, sbuf.len, write_object,
2633-
type, path, format_check);
2633+
ret = index_mem(sha1, sbuf.buf, sbuf.len, type, path, flags);
26342634
else
26352635
ret = -1;
26362636
strbuf_release(&sbuf);
26372637
} else if (!size) {
2638-
ret = index_mem(sha1, NULL, size, write_object, type, path,
2639-
format_check);
2638+
ret = index_mem(sha1, NULL, size, type, path, flags);
26402639
} else if (size <= SMALL_FILE_SIZE) {
26412640
char *buf = xmalloc(size);
26422641
if (size == read_in_full(fd, buf, size))
2643-
ret = index_mem(sha1, buf, size, write_object, type,
2644-
path, format_check);
2642+
ret = index_mem(sha1, buf, size, type, path, flags);
26452643
else
26462644
ret = error("short read %s", strerror(errno));
26472645
free(buf);
26482646
} else {
26492647
void *buf = xmmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0);
2650-
ret = index_mem(sha1, buf, size, write_object, type, path,
2651-
format_check);
2648+
ret = index_mem(sha1, buf, size, type, path, flags);
26522649
munmap(buf, size);
26532650
}
26542651
close(fd);
26552652
return ret;
26562653
}
26572654

2658-
int index_path(unsigned char *sha1, const char *path, struct stat *st, int write_object)
2655+
int index_path(unsigned char *sha1, const char *path, struct stat *st, unsigned flags)
26592656
{
26602657
int fd;
26612658
struct strbuf sb = STRBUF_INIT;
@@ -2666,7 +2663,7 @@ int index_path(unsigned char *sha1, const char *path, struct stat *st, int write
26662663
if (fd < 0)
26672664
return error("open(\"%s\"): %s", path,
26682665
strerror(errno));
2669-
if (index_fd(sha1, fd, st, write_object, OBJ_BLOB, path, 0) < 0)
2666+
if (index_fd(sha1, fd, st, OBJ_BLOB, path, flags) < 0)
26702667
return error("%s: failed to insert into database",
26712668
path);
26722669
break;
@@ -2676,7 +2673,7 @@ int index_path(unsigned char *sha1, const char *path, struct stat *st, int write
26762673
return error("readlink(\"%s\"): %s", path,
26772674
errstr);
26782675
}
2679-
if (!write_object)
2676+
if (!(flags & HASH_WRITE_OBJECT))
26802677
hash_sha1_file(sb.buf, sb.len, blob_type, sha1);
26812678
else if (write_sha1_file(sb.buf, sb.len, blob_type, sha1))
26822679
return error("%s: failed to insert into database",

0 commit comments

Comments
 (0)