teach index_fd to work with pipes
[git/dscho.git] / hash-object.c
blob7acfae15d9e946e0002f1f0850718b64bfe55768
1 /*
2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
5 * Copyright (C) Junio C Hamano, 2005
6 */
7 #include "cache.h"
8 #include "blob.h"
9 #include "quote.h"
11 static void hash_fd(int fd, const char *type, int write_object, const char *path)
13 struct stat st;
14 unsigned char sha1[20];
15 if (fstat(fd, &st) < 0 ||
16 index_fd(sha1, fd, &st, write_object, type_from_string(type), path))
17 die(write_object
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)
26 int fd;
27 fd = open(path, O_RDONLY);
28 if (fd < 0)
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;
37 strbuf_init(&buf, 0);
38 strbuf_init(&nbuf, 0);
39 while (strbuf_getline(&buf, stdin, '\n') != EOF) {
40 if (buf.buf[0] == '"') {
41 strbuf_reset(&nbuf);
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);
48 strbuf_release(&buf);
49 strbuf_release(&nbuf);
52 static const char hash_object_usage[] =
53 "git hash-object [ [-t <type>] [-w] [--stdin] <file>... | --stdin-paths < <list-of-paths> ]";
55 int main(int argc, char **argv)
57 int i;
58 const char *type = blob_type;
59 int write_object = 0;
60 const char *prefix = NULL;
61 int prefix_length = -1;
62 int no_more_flags = 0;
63 int hashstdin = 0;
64 int stdin_paths = 0;
66 git_config(git_default_config, NULL);
68 for (i = 1 ; i < argc; i++) {
69 if (!no_more_flags && argv[i][0] == '-') {
70 if (!strcmp(argv[i], "-t")) {
71 if (argc <= ++i)
72 usage(hash_object_usage);
73 type = argv[i];
75 else if (!strcmp(argv[i], "-w")) {
76 if (prefix_length < 0) {
77 prefix = setup_git_directory();
78 prefix_length =
79 prefix ? strlen(prefix) : 0;
81 write_object = 1;
83 else if (!strcmp(argv[i], "--")) {
84 no_more_flags = 1;
86 else if (!strcmp(argv[i], "--help"))
87 usage(hash_object_usage);
88 else if (!strcmp(argv[i], "--stdin-paths")) {
89 if (hashstdin) {
90 error("Can't use --stdin-paths with --stdin");
91 usage(hash_object_usage);
93 stdin_paths = 1;
96 else if (!strcmp(argv[i], "--stdin")) {
97 if (stdin_paths) {
98 error("Can't use %s with --stdin-paths", argv[i]);
99 usage(hash_object_usage);
101 if (hashstdin)
102 die("Multiple --stdin arguments are not supported");
103 hashstdin = 1;
105 else
106 usage(hash_object_usage);
108 else {
109 const char *arg = argv[i];
111 if (stdin_paths) {
112 error("Can't specify files (such as \"%s\") with --stdin-paths", arg);
113 usage(hash_object_usage);
116 if (hashstdin) {
117 hash_fd(0, type, write_object, NULL);
118 hashstdin = 0;
120 if (0 <= prefix_length)
121 arg = prefix_filename(prefix, prefix_length,
122 arg);
123 hash_object(arg, type, write_object);
124 no_more_flags = 1;
128 if (stdin_paths)
129 hash_stdin_paths(type, write_object);
131 if (hashstdin)
132 hash_fd(0, type, write_object, NULL);
133 return 0;