2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
5 * Copyright (C) Junio C Hamano, 2005
12 #include "object-file.h"
13 #include "object-store-ll.h"
16 #include "parse-options.h"
19 #include "write-or-die.h"
22 * This is to create corrupt objects for debugging and as such it
23 * needs to bypass the data conversion performed by, and the type
24 * limitation imposed by, index_fd() and its callees.
26 static int hash_literally(struct object_id
*oid
, int fd
, const char *type
, unsigned flags
)
28 struct strbuf buf
= STRBUF_INIT
;
31 if (strbuf_read(&buf
, fd
, 4096) < 0)
34 ret
= write_object_file_literally(buf
.buf
, buf
.len
, type
, oid
,
41 static void hash_fd(int fd
, const char *type
, const char *path
, unsigned flags
,
47 if (fstat(fd
, &st
) < 0 ||
49 ? hash_literally(&oid
, fd
, type
, flags
)
50 : index_fd(the_repository
->index
, &oid
, fd
, &st
,
51 type_from_string(type
), path
, flags
)))
52 die((flags
& HASH_WRITE_OBJECT
)
53 ? "Unable to add %s to database"
54 : "Unable to hash %s", path
);
55 printf("%s\n", oid_to_hex(&oid
));
56 maybe_flush_or_die(stdout
, "hash to stdout");
59 static void hash_object(const char *path
, const char *type
, const char *vpath
,
60 unsigned flags
, int literally
)
63 fd
= xopen(path
, O_RDONLY
);
64 hash_fd(fd
, type
, vpath
, flags
, literally
);
67 static void hash_stdin_paths(const char *type
, int no_filters
, unsigned flags
,
70 struct strbuf buf
= STRBUF_INIT
;
71 struct strbuf unquoted
= STRBUF_INIT
;
73 while (strbuf_getline(&buf
, stdin
) != EOF
) {
74 if (buf
.buf
[0] == '"') {
75 strbuf_reset(&unquoted
);
76 if (unquote_c_style(&unquoted
, buf
.buf
, NULL
))
77 die("line is badly quoted");
78 strbuf_swap(&buf
, &unquoted
);
80 hash_object(buf
.buf
, type
, no_filters
? NULL
: buf
.buf
, flags
,
84 strbuf_release(&unquoted
);
87 int cmd_hash_object(int argc
, const char **argv
, const char *prefix
)
89 static const char * const hash_object_usage
[] = {
90 N_("git hash-object [-t <type>] [-w] [--path=<file> | --no-filters]\n"
91 " [--stdin [--literally]] [--] <file>..."),
92 N_("git hash-object [-t <type>] [-w] --stdin-paths [--no-filters]"),
95 const char *type
= blob_type
;
101 unsigned flags
= HASH_FORMAT_CHECK
;
102 const char *vpath
= NULL
;
103 char *vpath_free
= NULL
;
104 const struct option hash_object_options
[] = {
105 OPT_STRING('t', NULL
, &type
, N_("type"), N_("object type")),
106 OPT_BIT('w', NULL
, &flags
, N_("write the object into the object database"),
108 OPT_COUNTUP( 0 , "stdin", &hashstdin
, N_("read the object from stdin")),
109 OPT_BOOL( 0 , "stdin-paths", &stdin_paths
, N_("read file names from stdin")),
110 OPT_BOOL( 0 , "no-filters", &no_filters
, N_("store file as is without filters")),
111 OPT_BOOL( 0, "literally", &literally
, N_("just hash any random garbage to create corrupt objects for debugging Git")),
112 OPT_STRING( 0 , "path", &vpath
, N_("file"), N_("process file as it were from this path")),
116 const char *errstr
= NULL
;
118 argc
= parse_options(argc
, argv
, prefix
, hash_object_options
,
119 hash_object_usage
, 0);
121 if (flags
& HASH_WRITE_OBJECT
)
122 prefix
= setup_git_directory();
124 prefix
= setup_git_directory_gently(&nongit
);
126 if (nongit
&& !the_hash_algo
)
127 repo_set_hash_algo(the_repository
, GIT_HASH_SHA1
);
129 if (vpath
&& prefix
) {
130 vpath_free
= prefix_filename(prefix
, vpath
);
134 git_config(git_default_config
, NULL
);
138 errstr
= "Can't use --stdin-paths with --stdin";
140 errstr
= "Can't specify files with --stdin-paths";
142 errstr
= "Can't use --stdin-paths with --path";
146 errstr
= "Multiple --stdin arguments are not supported";
147 if (vpath
&& no_filters
)
148 errstr
= "Can't use --path with --no-filters";
153 usage_with_options(hash_object_usage
, hash_object_options
);
157 hash_fd(0, type
, vpath
, flags
, literally
);
159 for (i
= 0 ; i
< argc
; i
++) {
160 const char *arg
= argv
[i
];
161 char *to_free
= NULL
;
164 arg
= to_free
= prefix_filename(prefix
, arg
);
165 hash_object(arg
, type
, no_filters
? NULL
: vpath
? vpath
: arg
,
171 hash_stdin_paths(type
, no_filters
, flags
, literally
);