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