2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
5 * Copyright (C) Junio C Hamano, 2005
12 #include "object-store.h"
15 #include "parse-options.h"
20 * This is to create corrupt objects for debugging and as such it
21 * needs to bypass the data conversion performed by, and the type
22 * limitation imposed by, index_fd() and its callees.
24 static int hash_literally(struct object_id
*oid
, int fd
, const char *type
, unsigned flags
)
26 struct strbuf buf
= STRBUF_INIT
;
29 if (strbuf_read(&buf
, fd
, 4096) < 0)
32 ret
= write_object_file_literally(buf
.buf
, buf
.len
, type
, oid
,
39 static void hash_fd(int fd
, const char *type
, const char *path
, unsigned flags
,
45 if (fstat(fd
, &st
) < 0 ||
47 ? hash_literally(&oid
, fd
, type
, flags
)
48 : index_fd(the_repository
->index
, &oid
, fd
, &st
,
49 type_from_string(type
), path
, flags
)))
50 die((flags
& HASH_WRITE_OBJECT
)
51 ? "Unable to add %s to database"
52 : "Unable to hash %s", path
);
53 printf("%s\n", oid_to_hex(&oid
));
54 maybe_flush_or_die(stdout
, "hash to stdout");
57 static void hash_object(const char *path
, const char *type
, const char *vpath
,
58 unsigned flags
, int literally
)
61 fd
= xopen(path
, O_RDONLY
);
62 hash_fd(fd
, type
, vpath
, flags
, literally
);
65 static void hash_stdin_paths(const char *type
, int no_filters
, unsigned flags
,
68 struct strbuf buf
= STRBUF_INIT
;
69 struct strbuf unquoted
= STRBUF_INIT
;
71 while (strbuf_getline(&buf
, stdin
) != EOF
) {
72 if (buf
.buf
[0] == '"') {
73 strbuf_reset(&unquoted
);
74 if (unquote_c_style(&unquoted
, buf
.buf
, NULL
))
75 die("line is badly quoted");
76 strbuf_swap(&buf
, &unquoted
);
78 hash_object(buf
.buf
, type
, no_filters
? NULL
: buf
.buf
, flags
,
82 strbuf_release(&unquoted
);
85 int cmd_hash_object(int argc
, const char **argv
, const char *prefix
)
87 static const char * const hash_object_usage
[] = {
88 N_("git hash-object [-t <type>] [-w] [--path=<file> | --no-filters]\n"
89 " [--stdin [--literally]] [--] <file>..."),
90 N_("git hash-object [-t <type>] [-w] --stdin-paths [--no-filters]"),
93 const char *type
= blob_type
;
99 unsigned flags
= HASH_FORMAT_CHECK
;
100 const char *vpath
= NULL
;
101 char *vpath_free
= NULL
;
102 const struct option hash_object_options
[] = {
103 OPT_STRING('t', NULL
, &type
, N_("type"), N_("object type")),
104 OPT_BIT('w', NULL
, &flags
, N_("write the object into the object database"),
106 OPT_COUNTUP( 0 , "stdin", &hashstdin
, N_("read the object from stdin")),
107 OPT_BOOL( 0 , "stdin-paths", &stdin_paths
, N_("read file names from stdin")),
108 OPT_BOOL( 0 , "no-filters", &no_filters
, N_("store file as is without filters")),
109 OPT_BOOL( 0, "literally", &literally
, N_("just hash any random garbage to create corrupt objects for debugging Git")),
110 OPT_STRING( 0 , "path", &vpath
, N_("file"), N_("process file as it were from this path")),
114 const char *errstr
= NULL
;
116 argc
= parse_options(argc
, argv
, prefix
, hash_object_options
,
117 hash_object_usage
, 0);
119 if (flags
& HASH_WRITE_OBJECT
)
120 prefix
= setup_git_directory();
122 prefix
= setup_git_directory_gently(&nongit
);
124 if (vpath
&& prefix
) {
125 vpath_free
= prefix_filename(prefix
, vpath
);
129 git_config(git_default_config
, NULL
);
133 errstr
= "Can't use --stdin-paths with --stdin";
135 errstr
= "Can't specify files with --stdin-paths";
137 errstr
= "Can't use --stdin-paths with --path";
141 errstr
= "Multiple --stdin arguments are not supported";
142 if (vpath
&& no_filters
)
143 errstr
= "Can't use --path with --no-filters";
148 usage_with_options(hash_object_usage
, hash_object_options
);
152 hash_fd(0, type
, vpath
, flags
, literally
);
154 for (i
= 0 ; i
< argc
; i
++) {
155 const char *arg
= argv
[i
];
156 char *to_free
= NULL
;
159 arg
= to_free
= prefix_filename(prefix
, arg
);
160 hash_object(arg
, type
, no_filters
? NULL
: vpath
? vpath
: arg
,
166 hash_stdin_paths(type
, no_filters
, flags
, literally
);