Add "get_sha1()" helper function.
This allows the programs to use various simplified versions of the SHA1 names, eg just say "HEAD" for the SHA1 pointed to by the .git/HEAD file etc. For example, this commit has been done with git-commit-tree $(git-write-tree) -p HEAD instead of the traditional "$(cat .git/HEAD)" syntax.maint
							parent
							
								
									68849b5442
								
							
						
					
					
						commit
						3c249c9506
					
				
							
								
								
									
										1
									
								
								cache.h
								
								
								
								
							
							
						
						
									
										1
									
								
								cache.h
								
								
								
								
							|  | @ -146,6 +146,7 @@ extern int write_sha1_from_fd(const unsigned char *sha1, int fd); | ||||||
| extern int has_sha1_file(const unsigned char *sha1); | extern int has_sha1_file(const unsigned char *sha1); | ||||||
|  |  | ||||||
| /* Convert to/from hex/sha1 representation */ | /* Convert to/from hex/sha1 representation */ | ||||||
|  | extern int get_sha1(const char *str, unsigned char *sha1); | ||||||
| extern int get_sha1_hex(const char *hex, unsigned char *sha1); | extern int get_sha1_hex(const char *hex, unsigned char *sha1); | ||||||
| extern char *sha1_to_hex(const unsigned char *sha1);	/* static buffer result! */ | extern char *sha1_to_hex(const unsigned char *sha1);	/* static buffer result! */ | ||||||
|  |  | ||||||
|  |  | ||||||
|  | @ -12,7 +12,7 @@ int main(int argc, char **argv) | ||||||
| 	void *buf; | 	void *buf; | ||||||
| 	unsigned long size; | 	unsigned long size; | ||||||
|  |  | ||||||
| 	if (argc != 3 || get_sha1_hex(argv[2], sha1)) | 	if (argc != 3 || get_sha1(argv[2], sha1)) | ||||||
| 		usage("cat-file [-t | tagname] <sha1>"); | 		usage("cat-file [-t | tagname] <sha1>"); | ||||||
| 	buf = read_sha1_file(sha1, type, &size); | 	buf = read_sha1_file(sha1, type, &size); | ||||||
| 	if (!buf) | 	if (!buf) | ||||||
|  |  | ||||||
|  | @ -123,7 +123,7 @@ int main(int argc, char **argv) | ||||||
| 	for (i = 2; i < argc; i += 2) { | 	for (i = 2; i < argc; i += 2) { | ||||||
| 		char *a, *b; | 		char *a, *b; | ||||||
| 		a = argv[i]; b = argv[i+1]; | 		a = argv[i]; b = argv[i+1]; | ||||||
| 		if (!b || strcmp(a, "-p") || get_sha1_hex(b, parent_sha1[parents])) | 		if (!b || strcmp(a, "-p") || get_sha1(b, parent_sha1[parents])) | ||||||
| 			usage(commit_tree_usage); | 			usage(commit_tree_usage); | ||||||
| 		check_valid(parent_sha1[parents], "commit"); | 		check_valid(parent_sha1[parents], "commit"); | ||||||
| 		parents++; | 		parents++; | ||||||
|  |  | ||||||
|  | @ -303,7 +303,7 @@ int main(int argc, char **argv) | ||||||
| 	unsigned char sha1[20]; | 	unsigned char sha1[20]; | ||||||
| 	struct entry *entry; | 	struct entry *entry; | ||||||
|  |  | ||||||
| 	if (argc != 2 || get_sha1_hex(argv[1], sha1)) | 	if (argc != 2 || get_sha1(argv[1], sha1)) | ||||||
| 		usage("convert-cache <sha1>"); | 		usage("convert-cache <sha1>"); | ||||||
|  |  | ||||||
| 	entry = convert_entry(sha1); | 	entry = convert_entry(sha1); | ||||||
|  |  | ||||||
|  | @ -175,7 +175,7 @@ int main(int argc, char **argv) | ||||||
| 		usage(diff_cache_usage); | 		usage(diff_cache_usage); | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	if (argc != 2 || get_sha1_hex(argv[1], tree_sha1)) | 	if (argc != 2 || get_sha1(argv[1], tree_sha1)) | ||||||
| 		usage(diff_cache_usage); | 		usage(diff_cache_usage); | ||||||
|  |  | ||||||
| 	mark_merge_entries(); | 	mark_merge_entries(); | ||||||
|  |  | ||||||
|  | @ -279,7 +279,7 @@ int main(int argc, char **argv) | ||||||
| 		usage(diff_tree_usage); | 		usage(diff_tree_usage); | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	if (argc < 3 || get_sha1_hex(argv[1], old) || get_sha1_hex(argv[2], new)) | 	if (argc < 3 || get_sha1(argv[1], old) || get_sha1(argv[2], new)) | ||||||
| 		usage(diff_tree_usage); | 		usage(diff_tree_usage); | ||||||
|  |  | ||||||
| 	if (argc > 3) { | 	if (argc > 3) { | ||||||
|  |  | ||||||
							
								
								
									
										4
									
								
								export.c
								
								
								
								
							
							
						
						
									
										4
									
								
								export.c
								
								
								
								
							|  | @ -73,8 +73,8 @@ int main(int argc, char **argv) | ||||||
| 	unsigned char top_sha1[20]; | 	unsigned char top_sha1[20]; | ||||||
|  |  | ||||||
| 	if (argc < 2 || argc > 4 || | 	if (argc < 2 || argc > 4 || | ||||||
| 	    get_sha1_hex(argv[1], top_sha1) || | 	    get_sha1(argv[1], top_sha1) || | ||||||
| 	    (argc == 3 && get_sha1_hex(argv[2], base_sha1))) | 	    (argc == 3 && get_sha1(argv[2], base_sha1))) | ||||||
| 		usage("git-export top [base]"); | 		usage("git-export top [base]"); | ||||||
| 	export(get_commit(top_sha1), argc==3 ? get_commit(base_sha1) : NULL); | 	export(get_commit(top_sha1), argc==3 ? get_commit(base_sha1) : NULL); | ||||||
| 	return 0; | 	return 0; | ||||||
|  |  | ||||||
|  | @ -178,7 +178,7 @@ int main(int argc, char **argv) | ||||||
| 		if (*arg == '-') | 		if (*arg == '-') | ||||||
| 			continue; | 			continue; | ||||||
|  |  | ||||||
| 		if (!get_sha1_hex(arg, head_sha1)) { | 		if (!get_sha1(arg, head_sha1)) { | ||||||
| 			struct commit *commit = lookup_commit(head_sha1); | 			struct commit *commit = lookup_commit(head_sha1); | ||||||
| 			struct object *obj; | 			struct object *obj; | ||||||
|  |  | ||||||
|  |  | ||||||
|  | @ -102,7 +102,7 @@ int main(int argc, char **argv) | ||||||
|  |  | ||||||
| 	if (argc != 2) | 	if (argc != 2) | ||||||
| 		usage(ls_tree_usage); | 		usage(ls_tree_usage); | ||||||
| 	if (get_sha1_hex(argv[1], sha1) < 0) | 	if (get_sha1(argv[1], sha1) < 0) | ||||||
| 		usage(ls_tree_usage); | 		usage(ls_tree_usage); | ||||||
| 	sha1_file_directory = getenv(DB_ENVIRONMENT); | 	sha1_file_directory = getenv(DB_ENVIRONMENT); | ||||||
| 	if (!sha1_file_directory) | 	if (!sha1_file_directory) | ||||||
|  |  | ||||||
|  | @ -58,8 +58,8 @@ int main(int argc, char **argv) | ||||||
| 	unsigned char rev1key[20], rev2key[20]; | 	unsigned char rev1key[20], rev2key[20]; | ||||||
|  |  | ||||||
| 	if (argc != 3 || | 	if (argc != 3 || | ||||||
| 	    get_sha1_hex(argv[1], rev1key) || | 	    get_sha1(argv[1], rev1key) || | ||||||
| 	    get_sha1_hex(argv[2], rev2key)) { | 	    get_sha1(argv[2], rev2key)) { | ||||||
| 		usage("merge-base <commit-id> <commit-id>"); | 		usage("merge-base <commit-id> <commit-id>"); | ||||||
| 	} | 	} | ||||||
| 	rev1 = lookup_commit(rev1key); | 	rev1 = lookup_commit(rev1key); | ||||||
|  |  | ||||||
|  | @ -191,7 +191,7 @@ int main(int argc, char **argv) | ||||||
| 			merge = 1; | 			merge = 1; | ||||||
| 			continue; | 			continue; | ||||||
| 		} | 		} | ||||||
| 		if (get_sha1_hex(arg, sha1) < 0) | 		if (get_sha1(arg, sha1) < 0) | ||||||
| 			usage(read_tree_usage); | 			usage(read_tree_usage); | ||||||
| 		if (stage > 3) | 		if (stage > 3) | ||||||
| 			usage(read_tree_usage); | 			usage(read_tree_usage); | ||||||
|  |  | ||||||
|  | @ -7,7 +7,7 @@ int main(int argc, char **argv) | ||||||
| 	struct commit_list *list = NULL; | 	struct commit_list *list = NULL; | ||||||
| 	struct commit *commit; | 	struct commit *commit; | ||||||
|  |  | ||||||
| 	if (argc != 2 || get_sha1_hex(argv[1], sha1)) | 	if (argc != 2 || get_sha1(argv[1], sha1)) | ||||||
| 		usage("rev-list <commit-id>"); | 		usage("rev-list <commit-id>"); | ||||||
|  |  | ||||||
| 	commit = lookup_commit(sha1); | 	commit = lookup_commit(sha1); | ||||||
|  |  | ||||||
|  | @ -97,7 +97,7 @@ int main(int argc, char **argv) | ||||||
| 			arg++; | 			arg++; | ||||||
| 			basemask |= 1<<nr; | 			basemask |= 1<<nr; | ||||||
| 		} | 		} | ||||||
| 		if (nr >= MAX_COMMITS || get_sha1_hex(arg, sha1[nr])) | 		if (nr >= MAX_COMMITS || get_sha1(arg, sha1[nr])) | ||||||
| 			usage("rev-tree [--edges] [--cache <cache-file>] <commit-id> [<commit-id>]"); | 			usage("rev-tree [--edges] [--cache <cache-file>] <commit-id> [<commit-id>]"); | ||||||
| 		process_commit(sha1[nr]); | 		process_commit(sha1[nr]); | ||||||
| 		nr++; | 		nr++; | ||||||
|  |  | ||||||
							
								
								
									
										29
									
								
								sha1_file.c
								
								
								
								
							
							
						
						
									
										29
									
								
								sha1_file.c
								
								
								
								
							|  | @ -45,6 +45,35 @@ int get_sha1_hex(const char *hex, unsigned char *sha1) | ||||||
| 	return 0; | 	return 0; | ||||||
| } | } | ||||||
|  |  | ||||||
|  | int get_sha1_file(const char *path, unsigned char *result) | ||||||
|  | { | ||||||
|  | 	char buffer[60]; | ||||||
|  | 	int fd = open(path, O_RDONLY); | ||||||
|  | 	int len; | ||||||
|  |  | ||||||
|  | 	if (fd < 0) | ||||||
|  | 		return -1; | ||||||
|  | 	len = read(fd, buffer, sizeof(buffer)); | ||||||
|  | 	close(fd); | ||||||
|  | 	if (len < 40) | ||||||
|  | 		return -1; | ||||||
|  | 	return get_sha1_hex(buffer, result); | ||||||
|  | } | ||||||
|  |  | ||||||
|  | int get_sha1(const char *str, unsigned char *sha1) | ||||||
|  | { | ||||||
|  | 	static char pathname[PATH_MAX]; | ||||||
|  |  | ||||||
|  | 	if (!get_sha1_hex(str, sha1)) | ||||||
|  | 		return 0; | ||||||
|  | 	if (!get_sha1_file(str, sha1)) | ||||||
|  | 		return 0; | ||||||
|  | 	snprintf(pathname, sizeof(pathname), ".git/%s", str); | ||||||
|  | 	if (!get_sha1_file(pathname, sha1)) | ||||||
|  | 		return 0; | ||||||
|  | 	return -1; | ||||||
|  | } | ||||||
|  |  | ||||||
| char * sha1_to_hex(const unsigned char *sha1) | char * sha1_to_hex(const unsigned char *sha1) | ||||||
| { | { | ||||||
| 	static char buffer[50]; | 	static char buffer[50]; | ||||||
|  |  | ||||||
|  | @ -338,7 +338,7 @@ int main(int argc, char **argv) | ||||||
| 		basedir = argv[2]; | 		basedir = argv[2]; | ||||||
| 		/* FALLTHROUGH */ | 		/* FALLTHROUGH */ | ||||||
| 	case 2: | 	case 2: | ||||||
| 		if (get_sha1_hex(argv[1], sha1) < 0) | 		if (get_sha1(argv[1], sha1) < 0) | ||||||
| 			usage(tar_tree_usage); | 			usage(tar_tree_usage); | ||||||
| 		break; | 		break; | ||||||
| 	default: | 	default: | ||||||
|  |  | ||||||
|  | @ -26,7 +26,7 @@ int main(int argc, char **argv) | ||||||
| { | { | ||||||
| 	unsigned char sha1[20]; | 	unsigned char sha1[20]; | ||||||
|  |  | ||||||
| 	if (argc != 2 || get_sha1_hex(argv[1], sha1)) | 	if (argc != 2 || get_sha1(argv[1], sha1)) | ||||||
| 		usage("unpack-file.c <sha1>"); | 		usage("unpack-file.c <sha1>"); | ||||||
|  |  | ||||||
| 	puts(create_temp_file(sha1)); | 	puts(create_temp_file(sha1)); | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue
	
	 Linus Torvalds
						Linus Torvalds