Fix git-log-script when HEAD is invalid.
[git/dkf.git] / hash-object.c
blobc8c9adb3aa789729e3c53a0d9ff1135367e93ee0
1 /*
2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
5 * Copyright (C) Junio C Hamano, 2005
6 */
7 #include "cache.h"
9 static void hash_object(const char *path, const char *type, int write_object)
11 int fd;
12 struct stat st;
13 unsigned char sha1[20];
14 fd = open(path, O_RDONLY);
15 if (fd < 0 ||
16 fstat(fd, &st) < 0 ||
17 index_fd(sha1, fd, &st, write_object, type))
18 die(write_object
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)
29 int i;
30 const char *type = "blob";
31 int write_object = 0;
33 for (i = 1 ; i < argc; i++) {
34 if (!strcmp(argv[i], "-t")) {
35 if (argc <= ++i)
36 die(hash_object_usage);
37 type = argv[i];
39 else if (!strcmp(argv[i], "-w"))
40 write_object = 1;
41 else
42 hash_object(argv[i], type, write_object);
44 return 0;