Git fork
1/*
2 * GIT - The information manager from hell
3 *
4 * Copyright (C) Linus Torvalds, 2005
5 * Copyright (C) Junio C Hamano, 2005
6 */
7#define USE_THE_REPOSITORY_VARIABLE
8#include "builtin.h"
9#include "abspath.h"
10#include "config.h"
11#include "environment.h"
12#include "gettext.h"
13#include "hex.h"
14#include "object-file.h"
15#include "odb.h"
16#include "blob.h"
17#include "quote.h"
18#include "parse-options.h"
19#include "setup.h"
20#include "strbuf.h"
21#include "write-or-die.h"
22
23static void hash_fd(int fd, const char *type, const char *path, unsigned flags)
24{
25 struct stat st;
26 struct object_id oid;
27
28 if (fstat(fd, &st) < 0 ||
29 index_fd(the_repository->index, &oid, fd, &st,
30 type_from_string(type), path, flags))
31 die((flags & INDEX_WRITE_OBJECT)
32 ? "Unable to add %s to database"
33 : "Unable to hash %s", path);
34 printf("%s\n", oid_to_hex(&oid));
35 maybe_flush_or_die(stdout, "hash to stdout");
36}
37
38static void hash_object(const char *path, const char *type, const char *vpath,
39 unsigned flags)
40{
41 int fd;
42 fd = xopen(path, O_RDONLY);
43 hash_fd(fd, type, vpath, flags);
44}
45
46static void hash_stdin_paths(const char *type, int no_filters, unsigned flags)
47{
48 struct strbuf buf = STRBUF_INIT;
49 struct strbuf unquoted = STRBUF_INIT;
50
51 while (strbuf_getline(&buf, stdin) != EOF) {
52 if (buf.buf[0] == '"') {
53 strbuf_reset(&unquoted);
54 if (unquote_c_style(&unquoted, buf.buf, NULL))
55 die("line is badly quoted");
56 strbuf_swap(&buf, &unquoted);
57 }
58 hash_object(buf.buf, type, no_filters ? NULL : buf.buf, flags);
59 }
60 strbuf_release(&buf);
61 strbuf_release(&unquoted);
62}
63
64int cmd_hash_object(int argc,
65 const char **argv,
66 const char *prefix,
67 struct repository *repo UNUSED)
68{
69 static const char * const hash_object_usage[] = {
70 N_("git hash-object [-t <type>] [-w] [--path=<file> | --no-filters]\n"
71 " [--stdin [--literally]] [--] <file>..."),
72 N_("git hash-object [-t <type>] [-w] --stdin-paths [--no-filters]"),
73 NULL
74 };
75 const char *type = blob_type;
76 int hashstdin = 0;
77 int stdin_paths = 0;
78 int no_filters = 0;
79 int nongit = 0;
80 unsigned flags = INDEX_FORMAT_CHECK;
81 const char *vpath = NULL;
82 char *vpath_free = NULL;
83 const struct option hash_object_options[] = {
84 OPT_STRING('t', NULL, &type, N_("type"), N_("object type")),
85 OPT_BIT('w', NULL, &flags, N_("write the object into the object database"),
86 INDEX_WRITE_OBJECT),
87 OPT_COUNTUP( 0 , "stdin", &hashstdin, N_("read the object from stdin")),
88 OPT_BOOL( 0 , "stdin-paths", &stdin_paths, N_("read file names from stdin")),
89 OPT_BOOL( 0 , "no-filters", &no_filters, N_("store file as is without filters")),
90 OPT_NEGBIT( 0, "literally", &flags,
91 N_("just hash any random garbage to create corrupt objects for debugging Git"),
92 INDEX_FORMAT_CHECK),
93 OPT_STRING( 0 , "path", &vpath, N_("file"), N_("process file as it were from this path")),
94 OPT_END()
95 };
96 int i;
97 const char *errstr = NULL;
98
99 argc = parse_options(argc, argv, prefix, hash_object_options,
100 hash_object_usage, 0);
101
102 if (flags & INDEX_WRITE_OBJECT)
103 prefix = setup_git_directory();
104 else
105 prefix = setup_git_directory_gently(&nongit);
106
107 if (nongit && !the_hash_algo)
108 repo_set_hash_algo(the_repository, GIT_HASH_DEFAULT);
109
110 if (vpath && prefix) {
111 vpath_free = prefix_filename(prefix, vpath);
112 vpath = vpath_free;
113 }
114
115 repo_config(the_repository, git_default_config, NULL);
116
117 if (stdin_paths) {
118 if (hashstdin)
119 errstr = "Can't use --stdin-paths with --stdin";
120 else if (argc)
121 errstr = "Can't specify files with --stdin-paths";
122 else if (vpath)
123 errstr = "Can't use --stdin-paths with --path";
124 }
125 else {
126 if (hashstdin > 1)
127 errstr = "Multiple --stdin arguments are not supported";
128 if (vpath && no_filters)
129 errstr = "Can't use --path with --no-filters";
130 }
131
132 if (errstr) {
133 error("%s", errstr);
134 usage_with_options(hash_object_usage, hash_object_options);
135 }
136
137 if (hashstdin)
138 hash_fd(0, type, vpath, flags);
139
140 for (i = 0 ; i < argc; i++) {
141 const char *arg = argv[i];
142 char *to_free = NULL;
143
144 if (prefix)
145 arg = to_free = prefix_filename(prefix, arg);
146 hash_object(arg, type, no_filters ? NULL : vpath ? vpath : arg,
147 flags);
148 free(to_free);
149 }
150
151 if (stdin_paths)
152 hash_stdin_paths(type, no_filters, flags);
153
154 free(vpath_free);
155
156 return 0;
157}