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 <gitster@pobox.com>
Junio C Hamano committed
May 8, 2011 at 01:47 UTC
c4ce46fc7ac1b59372aa935e641ca15b12359f5b
6 files changed
+27
-23
builtin/hash-object.c
+4
-1
@@ -14,8 +14,11 @@ static void hash_fd(int fd, const char *type, int write_object, const char *path
14
{
15
struct stat st;
16
unsigned char sha1[20];
17
+ unsigned flags = (HASH_FORMAT_CHECK |
18
+ (write_object ? HASH_WRITE_OBJECT : 0));
19
+
20
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))
22
die(write_object
23
? "Unable to add %s to database"
24
: "Unable to hash %s", path);
builtin/update-index.c
+2
-1
@@ -99,7 +99,8 @@ static int add_one_path(struct cache_entry *old, const char *path, int len, stru
99
fill_stat_cache_info(ce, st);
100
ce->ce_mode = ce_mode_from_stat(old, st->st_mode);
101
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))
104
return -1;
105
option = allow_add ? ADD_CACHE_OK_TO_ADD : 0;
106
option |= allow_replace ? ADD_CACHE_OK_TO_REPLACE : 0;
cache.h
+5
-2
@@ -518,8 +518,11 @@ struct pathspec {
518
extern int init_pathspec(struct pathspec *, const char **);
519
extern void free_pathspec(struct pathspec *);
520
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);
526
extern void fill_stat_cache_info(struct cache_entry *ce, struct stat *st);
527
528
#define REFRESH_REALLY 0x0001 /* ignore_valid */
notes-merge.c
+1
-1
@@ -707,7 +707,7 @@ int notes_merge_commit(struct notes_merge_options *o,
707
/* write file as blob, and add to partial_tree */
708
if (stat(ent->name, &st))
709
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))
711
die("Failed to write blob object from '%s'", ent->name);
712
if (add_note(partial_tree, obj_sha1, blob_sha1, NULL))
713
die("Failed to add resolved note '%s' to notes tree",
read-cache.c
+2
-2
@@ -92,7 +92,7 @@ static int ce_compare_data(struct cache_entry *ce, struct stat *st)
92
93
if (fd >= 0) {
94
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))
96
match = hashcmp(sha1, ce->sha1);
97
/* index_fd() closed the file descriptor already */
98
}
@@ -641,7 +641,7 @@ int add_to_index(struct index_state *istate, const char *path, struct stat *st,
641
return 0;
642
}
643
if (!intent_only) {
644
- if (index_path(ce->sha1, path, st, 1))
644
+ if (index_path(ce->sha1, path, st, HASH_WRITE_OBJECT))
645
return error("unable to index file %s", path);
646
} else
647
record_intent_to_add(ce);
sha1_file.c
+13
-16
@@ -2581,10 +2581,11 @@ static void check_tag(const void *buf, size_t size)
2581
}
2582
2583
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)
2586
{
2587
int ret, re_allocated = 0;
2588
+ int write_object = flags & HASH_WRITE_OBJECT;
2589
2590
if (!type)
2591
type = OBJ_BLOB;
@@ -2600,7 +2601,7 @@ static int index_mem(unsigned char *sha1, void *buf, size_t size,
2601
re_allocated = 1;
2602
}
2603
}
2603
- if (format_check) {
2604
+ if (flags & HASH_FORMAT_CHECK) {
2605
if (type == OBJ_TREE)
2606
check_tree(buf, size);
2607
if (type == OBJ_COMMIT)
@@ -2620,8 +2621,8 @@ static int index_mem(unsigned char *sha1, void *buf, size_t size,
2621
2622
#define SMALL_FILE_SIZE (32*1024)
2623
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)
2626
{
2627
int ret;
2628
size_t size = xsize_t(st->st_size);
@@ -2629,33 +2630,29 @@ int index_fd(unsigned char *sha1, int fd, struct stat *st, int write_object,
2630
if (!S_ISREG(st->st_mode)) {
2631
struct strbuf sbuf = STRBUF_INIT;
2632
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);
2634
else
2635
ret = -1;
2636
strbuf_release(&sbuf);
2637
} 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);
2639
} else if (size <= SMALL_FILE_SIZE) {
2640
char *buf = xmalloc(size);
2641
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);
2643
else
2644
ret = error("short read %s", strerror(errno));
2645
free(buf);
2646
} else {
2647
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);
2649
munmap(buf, size);
2650
}
2651
close(fd);
2652
return ret;
2653
}
2654
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)
2656
{
2657
int fd;
2658
struct strbuf sb = STRBUF_INIT;
@@ -2666,7 +2663,7 @@ int index_path(unsigned char *sha1, const char *path, struct stat *st, int write
2663
if (fd < 0)
2664
return error("open(\"%s\"): %s", path,
2665
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)
2667
return error("%s: failed to insert into database",
2668
path);
2669
break;
@@ -2676,7 +2673,7 @@ int index_path(unsigned char *sha1, const char *path, struct stat *st, int write
2673
return error("readlink(\"%s\"): %s", path,
2674
errstr);
2675
}
2679
- if (!write_object)
2676
+ if (!(flags & HASH_WRITE_OBJECT))
2677
hash_sha1_file(sb.buf, sb.len, blob_type, sha1);
2678
else if (write_sha1_file(sb.buf, sb.len, blob_type, sha1))
2679
return error("%s: failed to insert into database",