2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
5 * Copyright (C) Junio C Hamano, 2005
9 static void hash_object(const char *path, const char *type, int write_object)
13 unsigned char sha1[20];
14 fd = open(path, O_RDONLY);
17 index_fd(sha1, fd, &st, write_object, type))
19 ? "Unable to add %s to database"
20 : "Unable to hash %s", path);
21 printf("%s\n", sha1_to_hex(sha1));
24 static void hash_stdin(const char *type, int write_object)
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));
32 static const char hash_object_usage[] =
33 "git-hash-object [-t <type>] [-w] [--stdin] <file>...";
35 int main(int argc, char **argv)
38 const char *type = "blob";
40 const char *prefix = NULL;
41 int prefix_length = -1;
42 int no_more_flags = 0;
44 for (i = 1 ; i < argc; i++) {
45 if (!no_more_flags && argv[i][0] == '-') {
46 if (!strcmp(argv[i], "-t")) {
48 die(hash_object_usage);
51 else if (!strcmp(argv[i], "-w")) {
52 if (prefix_length < 0) {
53 prefix = setup_git_directory();
55 prefix ? strlen(prefix) : 0;
59 else if (!strcmp(argv[i], "--")) {
62 else if (!strcmp(argv[i], "--help"))
63 usage(hash_object_usage);
64 else if (!strcmp(argv[i], "--stdin")) {
65 hash_stdin(type, write_object);
68 die(hash_object_usage);
71 const char *arg = argv[i];
72 if (0 <= prefix_length)
73 arg = prefix_filename(prefix, prefix_length,
75 hash_object(arg, type, write_object);