]> git.ipfire.org Git - thirdparty/git.git/blame - hash-object.c
Make usage strings dash-less
[thirdparty/git.git] / hash-object.c
CommitLineData
7672db20
BL
1/*
2 * GIT - The information manager from hell
3 *
4 * Copyright (C) Linus Torvalds, 2005
8e440259 5 * Copyright (C) Junio C Hamano, 2005
7672db20
BL
6 */
7#include "cache.h"
8e440259 8#include "blob.h"
d8ee4832 9#include "quote.h"
7672db20 10
edaec3fb 11static void hash_object(const char *path, enum object_type type, int write_object)
7672db20
BL
12{
13 int fd;
14 struct stat st;
15 unsigned char sha1[20];
16 fd = open(path, O_RDONLY);
17 if (fd < 0 ||
18 fstat(fd, &st) < 0 ||
53bca91a 19 index_fd(sha1, fd, &st, write_object, type, path))
7672db20
BL
20 die(write_object
21 ? "Unable to add %s to database"
22 : "Unable to hash %s", path);
23 printf("%s\n", sha1_to_hex(sha1));
d8ee4832 24 maybe_flush_or_die(stdout, "hash to stdout");
7672db20
BL
25}
26
024510c8
DB
27static void hash_stdin(const char *type, int write_object)
28{
29 unsigned char sha1[20];
30 if (index_pipe(sha1, 0, type, write_object))
31 die("Unable to add stdin to database");
32 printf("%s\n", sha1_to_hex(sha1));
33}
34
d8ee4832
AR
35static void hash_stdin_paths(const char *type, int write_objects)
36{
37 struct strbuf buf, nbuf;
38
39 strbuf_init(&buf, 0);
40 strbuf_init(&nbuf, 0);
41 while (strbuf_getline(&buf, stdin, '\n') != EOF) {
42 if (buf.buf[0] == '"') {
43 strbuf_reset(&nbuf);
44 if (unquote_c_style(&nbuf, buf.buf, NULL))
45 die("line is badly quoted");
46 strbuf_swap(&buf, &nbuf);
47 }
48 hash_object(buf.buf, type_from_string(type), write_objects);
49 }
50 strbuf_release(&buf);
51 strbuf_release(&nbuf);
52}
53
4d1f1190 54static const char hash_object_usage[] =
1b1dd23f 55"git hash-object [ [-t <type>] [-w] [--stdin] <file>... | --stdin-paths < <list-of-paths> ]";
7672db20
BL
56
57int main(int argc, char **argv)
58{
59 int i;
8e440259 60 const char *type = blob_type;
7672db20 61 int write_object = 0;
99e01692 62 const char *prefix = NULL;
706fe6ae 63 int prefix_length = -1;
9c2e7c0c 64 int no_more_flags = 0;
8a2f5e5b 65 int hashstdin = 0;
d8ee4832 66 int stdin_paths = 0;
7672db20 67
ef90d6d4 68 git_config(git_default_config, NULL);
ff350ccf 69
7672db20 70 for (i = 1 ; i < argc; i++) {
9c2e7c0c
JH
71 if (!no_more_flags && argv[i][0] == '-') {
72 if (!strcmp(argv[i], "-t")) {
73 if (argc <= ++i)
8cdf3364 74 usage(hash_object_usage);
9c2e7c0c
JH
75 type = argv[i];
76 }
77 else if (!strcmp(argv[i], "-w")) {
78 if (prefix_length < 0) {
79 prefix = setup_git_directory();
80 prefix_length =
81 prefix ? strlen(prefix) : 0;
82 }
83 write_object = 1;
706fe6ae 84 }
9c2e7c0c
JH
85 else if (!strcmp(argv[i], "--")) {
86 no_more_flags = 1;
87 }
88 else if (!strcmp(argv[i], "--help"))
89 usage(hash_object_usage);
d8ee4832
AR
90 else if (!strcmp(argv[i], "--stdin-paths")) {
91 if (hashstdin) {
92 error("Can't use --stdin-paths with --stdin");
93 usage(hash_object_usage);
94 }
95 stdin_paths = 1;
96
97 }
024510c8 98 else if (!strcmp(argv[i], "--stdin")) {
d8ee4832
AR
99 if (stdin_paths) {
100 error("Can't use %s with --stdin-paths", argv[i]);
101 usage(hash_object_usage);
102 }
8a2f5e5b
GP
103 if (hashstdin)
104 die("Multiple --stdin arguments are not supported");
105 hashstdin = 1;
024510c8 106 }
9c2e7c0c 107 else
8cdf3364
RJ
108 usage(hash_object_usage);
109 }
706fe6ae 110 else {
99e01692 111 const char *arg = argv[i];
8a2f5e5b 112
d8ee4832
AR
113 if (stdin_paths) {
114 error("Can't specify files (such as \"%s\") with --stdin-paths", arg);
115 usage(hash_object_usage);
116 }
117
8a2f5e5b
GP
118 if (hashstdin) {
119 hash_stdin(type, write_object);
120 hashstdin = 0;
121 }
706fe6ae
JH
122 if (0 <= prefix_length)
123 arg = prefix_filename(prefix, prefix_length,
124 arg);
edaec3fb 125 hash_object(arg, type_from_string(type), write_object);
9c2e7c0c 126 no_more_flags = 1;
706fe6ae 127 }
7672db20 128 }
d8ee4832
AR
129
130 if (stdin_paths)
131 hash_stdin_paths(type, write_object);
132
8a2f5e5b
GP
133 if (hashstdin)
134 hash_stdin(type, write_object);
7672db20
BL
135 return 0;
136}