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>maint
							parent
							
								
									9cedd16c62
								
							
						
					
					
						commit
						c4ce46fc7a
					
				|  | @ -14,8 +14,11 @@ static void hash_fd(int fd, const char *type, int write_object, const char *path | ||||||
| { | { | ||||||
| 	struct stat st; | 	struct stat st; | ||||||
| 	unsigned char sha1[20]; | 	unsigned char sha1[20]; | ||||||
|  | 	unsigned flags = (HASH_FORMAT_CHECK | | ||||||
|  | 			  (write_object ? HASH_WRITE_OBJECT : 0)); | ||||||
|  |  | ||||||
| 	if (fstat(fd, &st) < 0 || | 	if (fstat(fd, &st) < 0 || | ||||||
| 	    index_fd(sha1, fd, &st, write_object, type_from_string(type), path, 1)) | 	    index_fd(sha1, fd, &st, type_from_string(type), path, flags)) | ||||||
| 		die(write_object | 		die(write_object | ||||||
| 		    ? "Unable to add %s to database" | 		    ? "Unable to add %s to database" | ||||||
| 		    : "Unable to hash %s", path); | 		    : "Unable to hash %s", path); | ||||||
|  |  | ||||||
|  | @ -99,7 +99,8 @@ static int add_one_path(struct cache_entry *old, const char *path, int len, stru | ||||||
| 	fill_stat_cache_info(ce, st); | 	fill_stat_cache_info(ce, st); | ||||||
| 	ce->ce_mode = ce_mode_from_stat(old, st->st_mode); | 	ce->ce_mode = ce_mode_from_stat(old, st->st_mode); | ||||||
|  |  | ||||||
| 	if (index_path(ce->sha1, path, st, !info_only)) | 	if (index_path(ce->sha1, path, st, | ||||||
|  | 		       info_only ? 0 : HASH_WRITE_OBJECT)) | ||||||
| 		return -1; | 		return -1; | ||||||
| 	option = allow_add ? ADD_CACHE_OK_TO_ADD : 0; | 	option = allow_add ? ADD_CACHE_OK_TO_ADD : 0; | ||||||
| 	option |= allow_replace ? ADD_CACHE_OK_TO_REPLACE : 0; | 	option |= allow_replace ? ADD_CACHE_OK_TO_REPLACE : 0; | ||||||
|  |  | ||||||
							
								
								
									
										7
									
								
								cache.h
								
								
								
								
							
							
						
						
									
										7
									
								
								cache.h
								
								
								
								
							|  | @ -518,8 +518,11 @@ struct pathspec { | ||||||
| extern int init_pathspec(struct pathspec *, const char **); | extern int init_pathspec(struct pathspec *, const char **); | ||||||
| extern void free_pathspec(struct pathspec *); | extern void free_pathspec(struct pathspec *); | ||||||
| extern int ce_path_match(const struct cache_entry *ce, const struct pathspec *pathspec); | extern int ce_path_match(const struct cache_entry *ce, const struct pathspec *pathspec); | ||||||
| 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); |  | ||||||
| extern int index_path(unsigned char *sha1, const char *path, struct stat *st, int write_object); | #define HASH_WRITE_OBJECT 1 | ||||||
|  | #define HASH_FORMAT_CHECK 2 | ||||||
|  | extern int index_fd(unsigned char *sha1, int fd, struct stat *st, enum object_type type, const char *path, unsigned flags); | ||||||
|  | extern int index_path(unsigned char *sha1, const char *path, struct stat *st, unsigned flags); | ||||||
| extern void fill_stat_cache_info(struct cache_entry *ce, struct stat *st); | extern void fill_stat_cache_info(struct cache_entry *ce, struct stat *st); | ||||||
|  |  | ||||||
| #define REFRESH_REALLY		0x0001	/* ignore_valid */ | #define REFRESH_REALLY		0x0001	/* ignore_valid */ | ||||||
|  |  | ||||||
|  | @ -707,7 +707,7 @@ int notes_merge_commit(struct notes_merge_options *o, | ||||||
| 		/* write file as blob, and add to partial_tree */ | 		/* write file as blob, and add to partial_tree */ | ||||||
| 		if (stat(ent->name, &st)) | 		if (stat(ent->name, &st)) | ||||||
| 			die_errno("Failed to stat '%s'", ent->name); | 			die_errno("Failed to stat '%s'", ent->name); | ||||||
| 		if (index_path(blob_sha1, ent->name, &st, 1)) | 		if (index_path(blob_sha1, ent->name, &st, HASH_WRITE_OBJECT)) | ||||||
| 			die("Failed to write blob object from '%s'", ent->name); | 			die("Failed to write blob object from '%s'", ent->name); | ||||||
| 		if (add_note(partial_tree, obj_sha1, blob_sha1, NULL)) | 		if (add_note(partial_tree, obj_sha1, blob_sha1, NULL)) | ||||||
| 			die("Failed to add resolved note '%s' to notes tree", | 			die("Failed to add resolved note '%s' to notes tree", | ||||||
|  |  | ||||||
|  | @ -92,7 +92,7 @@ static int ce_compare_data(struct cache_entry *ce, struct stat *st) | ||||||
|  |  | ||||||
| 	if (fd >= 0) { | 	if (fd >= 0) { | ||||||
| 		unsigned char sha1[20]; | 		unsigned char sha1[20]; | ||||||
| 		if (!index_fd(sha1, fd, st, 0, OBJ_BLOB, ce->name, 0)) | 		if (!index_fd(sha1, fd, st, OBJ_BLOB, ce->name, 0)) | ||||||
| 			match = hashcmp(sha1, ce->sha1); | 			match = hashcmp(sha1, ce->sha1); | ||||||
| 		/* index_fd() closed the file descriptor already */ | 		/* index_fd() closed the file descriptor already */ | ||||||
| 	} | 	} | ||||||
|  | @ -641,7 +641,7 @@ int add_to_index(struct index_state *istate, const char *path, struct stat *st, | ||||||
| 		return 0; | 		return 0; | ||||||
| 	} | 	} | ||||||
| 	if (!intent_only) { | 	if (!intent_only) { | ||||||
| 		if (index_path(ce->sha1, path, st, 1)) | 		if (index_path(ce->sha1, path, st, HASH_WRITE_OBJECT)) | ||||||
| 			return error("unable to index file %s", path); | 			return error("unable to index file %s", path); | ||||||
| 	} else | 	} else | ||||||
| 		record_intent_to_add(ce); | 		record_intent_to_add(ce); | ||||||
|  |  | ||||||
							
								
								
									
										29
									
								
								sha1_file.c
								
								
								
								
							
							
						
						
									
										29
									
								
								sha1_file.c
								
								
								
								
							|  | @ -2581,10 +2581,11 @@ static void check_tag(const void *buf, size_t size) | ||||||
| } | } | ||||||
|  |  | ||||||
| static int index_mem(unsigned char *sha1, void *buf, size_t size, | static int index_mem(unsigned char *sha1, void *buf, size_t size, | ||||||
| 		     int write_object, enum object_type type, | 		     enum object_type type, | ||||||
| 		     const char *path, int format_check) | 		     const char *path, unsigned flags) | ||||||
| { | { | ||||||
| 	int ret, re_allocated = 0; | 	int ret, re_allocated = 0; | ||||||
|  | 	int write_object = flags & HASH_WRITE_OBJECT; | ||||||
|  |  | ||||||
| 	if (!type) | 	if (!type) | ||||||
| 		type = OBJ_BLOB; | 		type = OBJ_BLOB; | ||||||
|  | @ -2600,7 +2601,7 @@ static int index_mem(unsigned char *sha1, void *buf, size_t size, | ||||||
| 			re_allocated = 1; | 			re_allocated = 1; | ||||||
| 		} | 		} | ||||||
| 	} | 	} | ||||||
| 	if (format_check) { | 	if (flags & HASH_FORMAT_CHECK) { | ||||||
| 		if (type == OBJ_TREE) | 		if (type == OBJ_TREE) | ||||||
| 			check_tree(buf, size); | 			check_tree(buf, size); | ||||||
| 		if (type == OBJ_COMMIT) | 		if (type == OBJ_COMMIT) | ||||||
|  | @ -2620,8 +2621,8 @@ static int index_mem(unsigned char *sha1, void *buf, size_t size, | ||||||
|  |  | ||||||
| #define SMALL_FILE_SIZE (32*1024) | #define SMALL_FILE_SIZE (32*1024) | ||||||
|  |  | ||||||
| int index_fd(unsigned char *sha1, int fd, struct stat *st, int write_object, | int index_fd(unsigned char *sha1, int fd, struct stat *st, | ||||||
| 	     enum object_type type, const char *path, int format_check) | 	     enum object_type type, const char *path, unsigned flags) | ||||||
| { | { | ||||||
| 	int ret; | 	int ret; | ||||||
| 	size_t size = xsize_t(st->st_size); | 	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, | ||||||
| 	if (!S_ISREG(st->st_mode)) { | 	if (!S_ISREG(st->st_mode)) { | ||||||
| 		struct strbuf sbuf = STRBUF_INIT; | 		struct strbuf sbuf = STRBUF_INIT; | ||||||
| 		if (strbuf_read(&sbuf, fd, 4096) >= 0) | 		if (strbuf_read(&sbuf, fd, 4096) >= 0) | ||||||
| 			ret = index_mem(sha1, sbuf.buf, sbuf.len, write_object, | 			ret = index_mem(sha1, sbuf.buf, sbuf.len, type,	path, flags); | ||||||
| 					type, path, format_check); |  | ||||||
| 		else | 		else | ||||||
| 			ret = -1; | 			ret = -1; | ||||||
| 		strbuf_release(&sbuf); | 		strbuf_release(&sbuf); | ||||||
| 	} else if (!size) { | 	} else if (!size) { | ||||||
| 		ret = index_mem(sha1, NULL, size, write_object, type, path, | 		ret = index_mem(sha1, NULL, size, type, path, flags); | ||||||
| 				format_check); |  | ||||||
| 	} else if (size <= SMALL_FILE_SIZE) { | 	} else if (size <= SMALL_FILE_SIZE) { | ||||||
| 		char *buf = xmalloc(size); | 		char *buf = xmalloc(size); | ||||||
| 		if (size == read_in_full(fd, buf, size)) | 		if (size == read_in_full(fd, buf, size)) | ||||||
| 			ret = index_mem(sha1, buf, size, write_object, type, | 			ret = index_mem(sha1, buf, size, type, path, flags); | ||||||
| 					path, format_check); |  | ||||||
| 		else | 		else | ||||||
| 			ret = error("short read %s", strerror(errno)); | 			ret = error("short read %s", strerror(errno)); | ||||||
| 		free(buf); | 		free(buf); | ||||||
| 	} else { | 	} else { | ||||||
| 		void *buf = xmmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0); | 		void *buf = xmmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0); | ||||||
| 		ret = index_mem(sha1, buf, size, write_object, type, path, | 		ret = index_mem(sha1, buf, size, type, path, flags); | ||||||
| 				format_check); |  | ||||||
| 		munmap(buf, size); | 		munmap(buf, size); | ||||||
| 	} | 	} | ||||||
| 	close(fd); | 	close(fd); | ||||||
| 	return ret; | 	return ret; | ||||||
| } | } | ||||||
|  |  | ||||||
| int index_path(unsigned char *sha1, const char *path, struct stat *st, int write_object) | int index_path(unsigned char *sha1, const char *path, struct stat *st, unsigned flags) | ||||||
| { | { | ||||||
| 	int fd; | 	int fd; | ||||||
| 	struct strbuf sb = STRBUF_INIT; | 	struct strbuf sb = STRBUF_INIT; | ||||||
|  | @ -2666,7 +2663,7 @@ int index_path(unsigned char *sha1, const char *path, struct stat *st, int write | ||||||
| 		if (fd < 0) | 		if (fd < 0) | ||||||
| 			return error("open(\"%s\"): %s", path, | 			return error("open(\"%s\"): %s", path, | ||||||
| 				     strerror(errno)); | 				     strerror(errno)); | ||||||
| 		if (index_fd(sha1, fd, st, write_object, OBJ_BLOB, path, 0) < 0) | 		if (index_fd(sha1, fd, st, OBJ_BLOB, path, flags) < 0) | ||||||
| 			return error("%s: failed to insert into database", | 			return error("%s: failed to insert into database", | ||||||
| 				     path); | 				     path); | ||||||
| 		break; | 		break; | ||||||
|  | @ -2676,7 +2673,7 @@ int index_path(unsigned char *sha1, const char *path, struct stat *st, int write | ||||||
| 			return error("readlink(\"%s\"): %s", path, | 			return error("readlink(\"%s\"): %s", path, | ||||||
| 			             errstr); | 			             errstr); | ||||||
| 		} | 		} | ||||||
| 		if (!write_object) | 		if (!(flags & HASH_WRITE_OBJECT)) | ||||||
| 			hash_sha1_file(sb.buf, sb.len, blob_type, sha1); | 			hash_sha1_file(sb.buf, sb.len, blob_type, sha1); | ||||||
| 		else if (write_sha1_file(sb.buf, sb.len, blob_type, sha1)) | 		else if (write_sha1_file(sb.buf, sb.len, blob_type, sha1)) | ||||||
| 			return error("%s: failed to insert into database", | 			return error("%s: failed to insert into database", | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue
	
	 Junio C Hamano
						Junio C Hamano