2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
5 * Copyright (C) Junio C Hamano, 2005
11 static void hash_object(const char *path
, enum object_type type
, int write_object
)
15 unsigned char sha1
[20];
16 fd
= open(path
, O_RDONLY
);
19 index_fd(sha1
, fd
, &st
, write_object
, type
, path
))
21 ? "Unable to add %s to database"
22 : "Unable to hash %s", path
);
23 printf("%s\n", sha1_to_hex(sha1
));
24 maybe_flush_or_die(stdout
, "hash to stdout");
27 static void hash_stdin(const char *type
, int write_object
)
29 unsigned char sha1
[20];
30 if (index_pipe(sha1
, 0, type
, write_object
))
31 die("Unable to add stdin to database");
32 printf("%s\n", sha1_to_hex(sha1
));
35 static void hash_stdin_paths(const char *type
, int write_objects
)
37 struct strbuf buf
, nbuf
;
40 strbuf_init(&nbuf
, 0);
41 while (strbuf_getline(&buf
, stdin
, '\n') != EOF
) {
42 if (buf
.buf
[0] == '"') {
44 if (unquote_c_style(&nbuf
, buf
.buf
, NULL
))
45 die("line is badly quoted");
46 strbuf_swap(&buf
, &nbuf
);
48 hash_object(buf
.buf
, type_from_string(type
), write_objects
);
51 strbuf_release(&nbuf
);
54 static const char hash_object_usage
[] =
55 "git hash-object [ [-t <type>] [-w] [--stdin] <file>... | --stdin-paths < <list-of-paths> ]";
57 int main(int argc
, char **argv
)
60 const char *type
= blob_type
;
62 const char *prefix
= NULL
;
63 int prefix_length
= -1;
64 int no_more_flags
= 0;
68 git_config(git_default_config
, NULL
);
70 for (i
= 1 ; i
< argc
; i
++) {
71 if (!no_more_flags
&& argv
[i
][0] == '-') {
72 if (!strcmp(argv
[i
], "-t")) {
74 usage(hash_object_usage
);
77 else if (!strcmp(argv
[i
], "-w")) {
78 if (prefix_length
< 0) {
79 prefix
= setup_git_directory();
81 prefix
? strlen(prefix
) : 0;
85 else if (!strcmp(argv
[i
], "--")) {
88 else if (!strcmp(argv
[i
], "--help"))
89 usage(hash_object_usage
);
90 else if (!strcmp(argv
[i
], "--stdin-paths")) {
92 error("Can't use --stdin-paths with --stdin");
93 usage(hash_object_usage
);
98 else if (!strcmp(argv
[i
], "--stdin")) {
100 error("Can't use %s with --stdin-paths", argv
[i
]);
101 usage(hash_object_usage
);
104 die("Multiple --stdin arguments are not supported");
108 usage(hash_object_usage
);
111 const char *arg
= argv
[i
];
114 error("Can't specify files (such as \"%s\") with --stdin-paths", arg
);
115 usage(hash_object_usage
);
119 hash_stdin(type
, write_object
);
122 if (0 <= prefix_length
)
123 arg
= prefix_filename(prefix
, prefix_length
,
125 hash_object(arg
, type_from_string(type
), write_object
);
131 hash_stdin_paths(type
, write_object
);
134 hash_stdin(type
, write_object
);