2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
5 * Copyright (C) Junio C Hamano, 2005
10 static void hash_object(const char *path
, enum object_type type
, int write_object
)
14 unsigned char sha1
[20];
15 fd
= open(path
, O_RDONLY
);
18 index_fd(sha1
, fd
, &st
, write_object
, type
, path
))
20 ? "Unable to add %s to database"
21 : "Unable to hash %s", path
);
22 printf("%s\n", sha1_to_hex(sha1
));
25 static void hash_stdin(const char *type
, int write_object
)
27 unsigned char sha1
[20];
28 if (index_pipe(sha1
, 0, type
, write_object
))
29 die("Unable to add stdin to database");
30 printf("%s\n", sha1_to_hex(sha1
));
33 static const char hash_object_usage
[] =
34 "git-hash-object [-t <type>] [-w] [--stdin] <file>...";
36 int main(int argc
, char **argv
)
39 const char *type
= blob_type
;
41 const char *prefix
= NULL
;
42 int prefix_length
= -1;
43 int no_more_flags
= 0;
46 git_config(git_default_config
);
48 for (i
= 1 ; i
< argc
; i
++) {
49 if (!no_more_flags
&& argv
[i
][0] == '-') {
50 if (!strcmp(argv
[i
], "-t")) {
52 usage(hash_object_usage
);
55 else if (!strcmp(argv
[i
], "-w")) {
56 if (prefix_length
< 0) {
57 prefix
= setup_git_directory();
59 prefix
? strlen(prefix
) : 0;
63 else if (!strcmp(argv
[i
], "--")) {
66 else if (!strcmp(argv
[i
], "--help"))
67 usage(hash_object_usage
);
68 else if (!strcmp(argv
[i
], "--stdin")) {
70 die("Multiple --stdin arguments are not supported");
74 usage(hash_object_usage
);
77 const char *arg
= argv
[i
];
80 hash_stdin(type
, write_object
);
83 if (0 <= prefix_length
)
84 arg
= prefix_filename(prefix
, prefix_length
,
86 hash_object(arg
, type_from_string(type
), write_object
);
91 hash_stdin(type
, write_object
);