]>
Commit | Line | Data |
---|---|---|
7672db20 BL |
1 | /* |
2 | * GIT - The information manager from hell | |
3 | * | |
4 | * Copyright (C) Linus Torvalds, 2005 | |
8e440259 | 5 | * Copyright (C) Junio C Hamano, 2005 |
7672db20 BL |
6 | */ |
7 | #include "cache.h" | |
8e440259 | 8 | #include "blob.h" |
7672db20 | 9 | |
edaec3fb | 10 | static void hash_object(const char *path, enum object_type type, int write_object) |
7672db20 BL |
11 | { |
12 | int fd; | |
13 | struct stat st; | |
14 | unsigned char sha1[20]; | |
15 | fd = open(path, O_RDONLY); | |
16 | if (fd < 0 || | |
17 | fstat(fd, &st) < 0 || | |
53bca91a | 18 | index_fd(sha1, fd, &st, write_object, type, path)) |
7672db20 BL |
19 | die(write_object |
20 | ? "Unable to add %s to database" | |
21 | : "Unable to hash %s", path); | |
22 | printf("%s\n", sha1_to_hex(sha1)); | |
23 | } | |
24 | ||
024510c8 DB |
25 | static void hash_stdin(const char *type, int write_object) |
26 | { | |
27 | unsigned char sha1[20]; | |
28 | if (index_pipe(sha1, 0, type, write_object)) | |
29 | die("Unable to add stdin to database"); | |
30 | printf("%s\n", sha1_to_hex(sha1)); | |
31 | } | |
32 | ||
4d1f1190 | 33 | static const char hash_object_usage[] = |
024510c8 | 34 | "git-hash-object [-t <type>] [-w] [--stdin] <file>..."; |
7672db20 BL |
35 | |
36 | int main(int argc, char **argv) | |
37 | { | |
38 | int i; | |
8e440259 | 39 | const char *type = blob_type; |
7672db20 | 40 | int write_object = 0; |
99e01692 | 41 | const char *prefix = NULL; |
706fe6ae | 42 | int prefix_length = -1; |
9c2e7c0c | 43 | int no_more_flags = 0; |
7672db20 BL |
44 | |
45 | for (i = 1 ; i < argc; i++) { | |
9c2e7c0c JH |
46 | if (!no_more_flags && argv[i][0] == '-') { |
47 | if (!strcmp(argv[i], "-t")) { | |
48 | if (argc <= ++i) | |
8cdf3364 | 49 | usage(hash_object_usage); |
9c2e7c0c JH |
50 | type = argv[i]; |
51 | } | |
52 | else if (!strcmp(argv[i], "-w")) { | |
53 | if (prefix_length < 0) { | |
54 | prefix = setup_git_directory(); | |
55 | prefix_length = | |
56 | prefix ? strlen(prefix) : 0; | |
57 | } | |
58 | write_object = 1; | |
706fe6ae | 59 | } |
9c2e7c0c JH |
60 | else if (!strcmp(argv[i], "--")) { |
61 | no_more_flags = 1; | |
62 | } | |
63 | else if (!strcmp(argv[i], "--help")) | |
64 | usage(hash_object_usage); | |
024510c8 DB |
65 | else if (!strcmp(argv[i], "--stdin")) { |
66 | hash_stdin(type, write_object); | |
67 | } | |
9c2e7c0c | 68 | else |
8cdf3364 RJ |
69 | usage(hash_object_usage); |
70 | } | |
706fe6ae | 71 | else { |
99e01692 | 72 | const char *arg = argv[i]; |
706fe6ae JH |
73 | if (0 <= prefix_length) |
74 | arg = prefix_filename(prefix, prefix_length, | |
75 | arg); | |
edaec3fb | 76 | hash_object(arg, type_from_string(type), write_object); |
9c2e7c0c | 77 | no_more_flags = 1; |
706fe6ae | 78 | } |
7672db20 BL |
79 | } |
80 | return 0; | |
81 | } |