Documentation/git-tag.txt: Fix the order of sections (DESCRIPTION should come before...
[git] / hash-object.c
1 /*
2  * GIT - The information manager from hell
3  *
4  * Copyright (C) Linus Torvalds, 2005
5  * Copyright (C) Junio C Hamano, 2005 
6  */
7 #include "cache.h"
8
9 static void hash_object(const char *path, const char *type, int write_object)
10 {
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));
22 }
23
24 static const char hash_object_usage[] =
25 "git-hash-object [-t <type>] [-w] <file>...";
26
27 int main(int argc, char **argv)
28 {
29         int i;
30         const char *type = "blob";
31         int write_object = 0;
32         const char *prefix = NULL;
33         int prefix_length = -1;
34         int no_more_flags = 0;
35
36         for (i = 1 ; i < argc; i++) {
37                 if (!no_more_flags && argv[i][0] == '-') {
38                         if (!strcmp(argv[i], "-t")) {
39                                 if (argc <= ++i)
40                                         die(hash_object_usage);
41                                 type = argv[i];
42                         }
43                         else if (!strcmp(argv[i], "-w")) {
44                                 if (prefix_length < 0) {
45                                         prefix = setup_git_directory();
46                                         prefix_length =
47                                                 prefix ? strlen(prefix) : 0;
48                                 }
49                                 write_object = 1;
50                         }
51                         else if (!strcmp(argv[i], "--")) {
52                                 no_more_flags = 1;
53                         }
54                         else if (!strcmp(argv[i], "--help"))
55                                 usage(hash_object_usage);
56                         else
57                                 die(hash_object_usage);
58                 }
59                 else {
60                         const char *arg = argv[i];
61                         if (0 <= prefix_length)
62                                 arg = prefix_filename(prefix, prefix_length,
63                                                       arg);
64                         hash_object(arg, type, write_object);
65                         no_more_flags = 1;
66                 }
67         }
68         return 0;
69 }