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>... | --stdin-paths < <list-of-paths> ]";
55 int main(int argc
, char **argv
)
58 const char *type
= blob_type
;
60 const char *prefix
= NULL
;
61 int prefix_length
= -1;
62 int no_more_flags
= 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")) {
72 usage(hash_object_usage
);
75 else if (!strcmp(argv
[i
], "-w")) {
76 if (prefix_length
< 0) {
77 prefix
= setup_git_directory();
79 prefix
? strlen(prefix
) : 0;
83 else if (!strcmp(argv
[i
], "--")) {
86 else if (!strcmp(argv
[i
], "--help"))
87 usage(hash_object_usage
);
88 else if (!strcmp(argv
[i
], "--stdin-paths")) {
90 error("Can't use --stdin-paths with --stdin");
91 usage(hash_object_usage
);
96 else if (!strcmp(argv
[i
], "--stdin")) {
98 error("Can't use %s with --stdin-paths", argv
[i
]);
99 usage(hash_object_usage
);
102 die("Multiple --stdin arguments are not supported");
106 usage(hash_object_usage
);
109 const char *arg
= argv
[i
];
112 error("Can't specify files (such as \"%s\") with --stdin-paths", arg
);
113 usage(hash_object_usage
);
117 hash_fd(0, type
, write_object
, NULL
);
120 if (0 <= prefix_length
)
121 arg
= prefix_filename(prefix
, prefix_length
,
123 hash_object(arg
, type
, write_object
);
129 hash_stdin_paths(type
, write_object
);
132 hash_fd(0, type
, write_object
, NULL
);