Add revspec documentation for ':path', ':[0-3]:path' and git-describe
[git/jrn.git] / hash-object.c
blob5f89e64c13d6f13fd832309041a6a9fa9d0bca5c
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"
8 #include "blob.h"
10 static void hash_object(const char *path, const char *type, int write_object)
12 int fd;
13 struct stat st;
14 unsigned char sha1[20];
15 fd = open(path, O_RDONLY);
16 if (fd < 0 ||
17 fstat(fd, &st) < 0 ||
18 index_fd(sha1, fd, &st, write_object, type))
19 die(write_object
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)
38 int i;
39 const char *type = blob_type;
40 int write_object = 0;
41 const char *prefix = NULL;
42 int prefix_length = -1;
43 int no_more_flags = 0;
45 for (i = 1 ; i < argc; i++) {
46 if (!no_more_flags && argv[i][0] == '-') {
47 if (!strcmp(argv[i], "-t")) {
48 if (argc <= ++i)
49 usage(hash_object_usage);
50 type = argv[i];
52 else if (!strcmp(argv[i], "-w")) {
53 if (prefix_length < 0) {
54 prefix = setup_git_directory();
55 prefix_length =
56 prefix ? strlen(prefix) : 0;
58 write_object = 1;
60 else if (!strcmp(argv[i], "--")) {
61 no_more_flags = 1;
63 else if (!strcmp(argv[i], "--help"))
64 usage(hash_object_usage);
65 else if (!strcmp(argv[i], "--stdin")) {
66 hash_stdin(type, write_object);
68 else
69 usage(hash_object_usage);
71 else {
72 const char *arg = argv[i];
73 if (0 <= prefix_length)
74 arg = prefix_filename(prefix, prefix_length,
75 arg);
76 hash_object(arg, type, write_object);
77 no_more_flags = 1;
80 return 0;