refs: make pack_refs() virtual
[git.git] / builtin / hash-object.c
blobf7d3567dd0ce2d75778d6cf011961c0f203432b4
1 /*
2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
5 * Copyright (C) Junio C Hamano, 2005
6 */
7 #include "builtin.h"
8 #include "blob.h"
9 #include "quote.h"
10 #include "parse-options.h"
11 #include "exec_cmd.h"
14 * This is to create corrupt objects for debugging and as such it
15 * needs to bypass the data conversion performed by, and the type
16 * limitation imposed by, index_fd() and its callees.
18 static int hash_literally(unsigned char *sha1, int fd, const char *type, unsigned flags)
20 struct strbuf buf = STRBUF_INIT;
21 int ret;
23 if (strbuf_read(&buf, fd, 4096) < 0)
24 ret = -1;
25 else
26 ret = hash_sha1_file_literally(buf.buf, buf.len, type, sha1, flags);
27 strbuf_release(&buf);
28 return ret;
31 static void hash_fd(int fd, const char *type, const char *path, unsigned flags,
32 int literally)
34 struct stat st;
35 unsigned char sha1[20];
37 if (fstat(fd, &st) < 0 ||
38 (literally
39 ? hash_literally(sha1, fd, type, flags)
40 : index_fd(sha1, fd, &st, type_from_string(type), path, flags)))
41 die((flags & HASH_WRITE_OBJECT)
42 ? "Unable to add %s to database"
43 : "Unable to hash %s", path);
44 printf("%s\n", sha1_to_hex(sha1));
45 maybe_flush_or_die(stdout, "hash to stdout");
48 static void hash_object(const char *path, const char *type, const char *vpath,
49 unsigned flags, int literally)
51 int fd;
52 fd = open(path, O_RDONLY);
53 if (fd < 0)
54 die_errno("Cannot open '%s'", path);
55 hash_fd(fd, type, vpath, flags, literally);
58 static void hash_stdin_paths(const char *type, int no_filters, unsigned flags,
59 int literally)
61 struct strbuf buf = STRBUF_INIT;
62 struct strbuf unquoted = STRBUF_INIT;
64 while (strbuf_getline(&buf, stdin) != EOF) {
65 if (buf.buf[0] == '"') {
66 strbuf_reset(&unquoted);
67 if (unquote_c_style(&unquoted, buf.buf, NULL))
68 die("line is badly quoted");
69 strbuf_swap(&buf, &unquoted);
71 hash_object(buf.buf, type, no_filters ? NULL : buf.buf, flags,
72 literally);
74 strbuf_release(&buf);
75 strbuf_release(&unquoted);
78 int cmd_hash_object(int argc, const char **argv, const char *prefix)
80 static const char * const hash_object_usage[] = {
81 N_("git hash-object [-t <type>] [-w] [--path=<file> | --no-filters] [--stdin] [--] <file>..."),
82 N_("git hash-object --stdin-paths"),
83 NULL
85 const char *type = blob_type;
86 int hashstdin = 0;
87 int stdin_paths = 0;
88 int no_filters = 0;
89 int literally = 0;
90 unsigned flags = HASH_FORMAT_CHECK;
91 const char *vpath = NULL;
92 const struct option hash_object_options[] = {
93 OPT_STRING('t', NULL, &type, N_("type"), N_("object type")),
94 OPT_BIT('w', NULL, &flags, N_("write the object into the object database"),
95 HASH_WRITE_OBJECT),
96 OPT_COUNTUP( 0 , "stdin", &hashstdin, N_("read the object from stdin")),
97 OPT_BOOL( 0 , "stdin-paths", &stdin_paths, N_("read file names from stdin")),
98 OPT_BOOL( 0 , "no-filters", &no_filters, N_("store file as is without filters")),
99 OPT_BOOL( 0, "literally", &literally, N_("just hash any random garbage to create corrupt objects for debugging Git")),
100 OPT_STRING( 0 , "path", &vpath, N_("file"), N_("process file as it were from this path")),
101 OPT_END()
103 int i;
104 int prefix_length = -1;
105 const char *errstr = NULL;
107 argc = parse_options(argc, argv, NULL, hash_object_options,
108 hash_object_usage, 0);
110 if (flags & HASH_WRITE_OBJECT) {
111 prefix = setup_git_directory();
112 prefix_length = prefix ? strlen(prefix) : 0;
113 if (vpath && prefix)
114 vpath = prefix_filename(prefix, prefix_length, vpath);
117 git_config(git_default_config, NULL);
119 if (stdin_paths) {
120 if (hashstdin)
121 errstr = "Can't use --stdin-paths with --stdin";
122 else if (argc)
123 errstr = "Can't specify files with --stdin-paths";
124 else if (vpath)
125 errstr = "Can't use --stdin-paths with --path";
127 else {
128 if (hashstdin > 1)
129 errstr = "Multiple --stdin arguments are not supported";
130 if (vpath && no_filters)
131 errstr = "Can't use --path with --no-filters";
134 if (errstr) {
135 error("%s", errstr);
136 usage_with_options(hash_object_usage, hash_object_options);
139 if (hashstdin)
140 hash_fd(0, type, vpath, flags, literally);
142 for (i = 0 ; i < argc; i++) {
143 const char *arg = argv[i];
145 if (0 <= prefix_length)
146 arg = prefix_filename(prefix, prefix_length, arg);
147 hash_object(arg, type, no_filters ? NULL : vpath ? vpath : arg,
148 flags, literally);
151 if (stdin_paths)
152 hash_stdin_paths(type, no_filters, flags, literally);
154 return 0;