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 const char hash_object_usage
[] =
25 "git-hash-object [-t <type>] [-w] <file>...";
27 int main(int argc
, char **argv
)
30 const char *type
= "blob";
32 const char *prefix
= NULL
;
33 int prefix_length
= -1;
35 for (i
= 1 ; i
< argc
; i
++) {
36 if (!strcmp(argv
[i
], "-t")) {
38 die(hash_object_usage
);
41 else if (!strcmp(argv
[i
], "-w")) {
42 if (prefix_length
< 0) {
43 prefix
= setup_git_directory();
44 prefix_length
= prefix
? strlen(prefix
) : 0;
49 const char *arg
= argv
[i
];
50 if (0 <= prefix_length
)
51 arg
= prefix_filename(prefix
, prefix_length
,
53 hash_object(arg
, type
, write_object
);