2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
5 * Copyright (C) Junio C Hamano, 2005
11 static void hash_fd(int fd, const char *type, int write_object, const char *path)
14 unsigned char sha1[20];
15 if (fstat(fd, &st) < 0 ||
16 index_fd(sha1, fd, &st, write_object, type_from_string(type), path))
18 ? "Unable to add %s to database"
19 : "Unable to hash %s", path);
20 printf("%s\n", sha1_to_hex(sha1));
21 maybe_flush_or_die(stdout, "hash to stdout");
24 static void hash_object(const char *path, const char *type, int write_object)
27 fd = open(path, O_RDONLY);
29 die("Cannot open %s", path);
30 hash_fd(fd, type, write_object, path);
33 static void hash_stdin_paths(const char *type, int write_objects)
35 struct strbuf buf, nbuf;
38 strbuf_init(&nbuf, 0);
39 while (strbuf_getline(&buf, stdin, '\n') != EOF) {
40 if (buf.buf[0] == '"') {
42 if (unquote_c_style(&nbuf, buf.buf, NULL))
43 die("line is badly quoted");
44 strbuf_swap(&buf, &nbuf);
46 hash_object(buf.buf, type, write_objects);
49 strbuf_release(&nbuf);
52 static const char hash_object_usage[] =
53 "git hash-object [-t <type>] [-w] [--stdin] [--] <file>...\n"
54 " or: git hash-object --stdin-paths < <list-of-paths>";
56 int main(int argc, char **argv)
59 const char *type = blob_type;
61 const char *prefix = NULL;
62 int prefix_length = -1;
63 int no_more_flags = 0;
67 git_config(git_default_config, NULL);
69 for (i = 1 ; i < argc; i++) {
70 if (!no_more_flags && argv[i][0] == '-') {
71 if (!strcmp(argv[i], "-t")) {
73 usage(hash_object_usage);
76 else if (!strcmp(argv[i], "-w")) {
77 if (prefix_length < 0) {
78 prefix = setup_git_directory();
80 prefix ? strlen(prefix) : 0;
84 else if (!strcmp(argv[i], "--")) {
87 else if (!strcmp(argv[i], "--help"))
88 usage(hash_object_usage);
89 else if (!strcmp(argv[i], "--stdin-paths")) {
91 error("Can't use --stdin-paths with --stdin");
92 usage(hash_object_usage);
97 else if (!strcmp(argv[i], "--stdin")) {
99 error("Can't use %s with --stdin-paths", argv[i]);
100 usage(hash_object_usage);
103 die("Multiple --stdin arguments are not supported");
107 usage(hash_object_usage);
110 const char *arg = argv[i];
113 error("Can't specify files (such as \"%s\") with --stdin-paths", arg);
114 usage(hash_object_usage);
118 hash_fd(0, type, write_object, NULL);
121 if (0 <= prefix_length)
122 arg = prefix_filename(prefix, prefix_length,
124 hash_object(arg, type, write_object);
130 hash_stdin_paths(type, write_object);
133 hash_fd(0, type, write_object, NULL);