blob: 9455dd0709aeb81436ef2e3e36422578d66ce8e7 [file] [log] [blame]
Bryan Larsen7672db22005-07-08 16:51:55 -07001/*
2 * GIT - The information manager from hell
3 *
4 * Copyright (C) Linus Torvalds, 2005
Peter Eriksen8e440252006-04-02 14:44:09 +02005 * Copyright (C) Junio C Hamano, 2005
Bryan Larsen7672db22005-07-08 16:51:55 -07006 */
7#include "cache.h"
Peter Eriksen8e440252006-04-02 14:44:09 +02008#include "blob.h"
Adam Robend8ee4832008-05-23 16:19:38 +02009#include "quote.h"
Dmitry Potapov548601a2008-08-03 18:36:20 +040010#include "parse-options.h"
Steffen Prohaska2fb3f6d2009-01-18 13:00:12 +010011#include "exec_cmd.h"
Bryan Larsen7672db22005-07-08 16:51:55 -070012
Dmitry Potapov43df4f82008-08-03 08:39:16 +040013static void hash_fd(int fd, const char *type, int write_object, const char *path)
Bryan Larsen7672db22005-07-08 16:51:55 -070014{
Bryan Larsen7672db22005-07-08 16:51:55 -070015 struct stat st;
16 unsigned char sha1[20];
Dmitry Potapov43df4f82008-08-03 08:39:16 +040017 if (fstat(fd, &st) < 0 ||
18 index_fd(sha1, fd, &st, write_object, type_from_string(type), path))
Bryan Larsen7672db22005-07-08 16:51:55 -070019 die(write_object
20 ? "Unable to add %s to database"
21 : "Unable to hash %s", path);
22 printf("%s\n", sha1_to_hex(sha1));
Adam Robend8ee4832008-05-23 16:19:38 +020023 maybe_flush_or_die(stdout, "hash to stdout");
Bryan Larsen7672db22005-07-08 16:51:55 -070024}
25
Dmitry Potapov39702432008-08-03 18:36:21 +040026static void hash_object(const char *path, const char *type, int write_object,
27 const char *vpath)
Daniel Barkalow024510c2005-12-10 17:25:24 -050028{
Dmitry Potapov43df4f82008-08-03 08:39:16 +040029 int fd;
30 fd = open(path, O_RDONLY);
31 if (fd < 0)
Thomas Rast0721c312009-06-27 17:58:47 +020032 die_errno("Cannot open '%s'", path);
Dmitry Potapov39702432008-08-03 18:36:21 +040033 hash_fd(fd, type, write_object, vpath);
Daniel Barkalow024510c2005-12-10 17:25:24 -050034}
35
Adam Robend8ee4832008-05-23 16:19:38 +020036static void hash_stdin_paths(const char *type, int write_objects)
37{
Brandon Caseyf285a2d2008-10-09 14:12:12 -050038 struct strbuf buf = STRBUF_INIT, nbuf = STRBUF_INIT;
Adam Robend8ee4832008-05-23 16:19:38 +020039
Adam Robend8ee4832008-05-23 16:19:38 +020040 while (strbuf_getline(&buf, stdin, '\n') != EOF) {
41 if (buf.buf[0] == '"') {
42 strbuf_reset(&nbuf);
43 if (unquote_c_style(&nbuf, buf.buf, NULL))
44 die("line is badly quoted");
45 strbuf_swap(&buf, &nbuf);
46 }
Dmitry Potapov39702432008-08-03 18:36:21 +040047 hash_object(buf.buf, type, write_objects, buf.buf);
Adam Robend8ee4832008-05-23 16:19:38 +020048 }
49 strbuf_release(&buf);
50 strbuf_release(&nbuf);
51}
52
Dmitry Potapov548601a2008-08-03 18:36:20 +040053static const char * const hash_object_usage[] = {
Dmitry Potapov4a3d85d2008-08-03 18:36:22 +040054 "git hash-object [-t <type>] [-w] [--path=<file>|--no-filters] [--stdin] [--] <file>...",
Dmitry Potapov548601a2008-08-03 18:36:20 +040055 "git hash-object --stdin-paths < <list-of-paths>",
56 NULL
57};
Bryan Larsen7672db22005-07-08 16:51:55 -070058
Dmitry Potapov548601a2008-08-03 18:36:20 +040059static const char *type;
60static int write_object;
61static int hashstdin;
62static int stdin_paths;
Dmitry Potapov4a3d85d2008-08-03 18:36:22 +040063static int no_filters;
Dmitry Potapov39702432008-08-03 18:36:21 +040064static const char *vpath;
Dmitry Potapov548601a2008-08-03 18:36:20 +040065
66static const struct option hash_object_options[] = {
67 OPT_STRING('t', NULL, &type, "type", "object type"),
68 OPT_BOOLEAN('w', NULL, &write_object, "write the object into the object database"),
69 OPT_BOOLEAN( 0 , "stdin", &hashstdin, "read the object from stdin"),
70 OPT_BOOLEAN( 0 , "stdin-paths", &stdin_paths, "read file names from stdin"),
Dmitry Potapov4a3d85d2008-08-03 18:36:22 +040071 OPT_BOOLEAN( 0 , "no-filters", &no_filters, "store file as is without filters"),
Dmitry Potapov39702432008-08-03 18:36:21 +040072 OPT_STRING( 0 , "path", &vpath, "file", "process file as it were from this path"),
Dmitry Potapov548601a2008-08-03 18:36:20 +040073 OPT_END()
74};
75
76int main(int argc, const char **argv)
Bryan Larsen7672db22005-07-08 16:51:55 -070077{
78 int i;
Junio C Hamano99e01692005-11-28 03:19:03 -080079 const char *prefix = NULL;
Junio C Hamano706fe6a2005-11-26 00:30:07 -080080 int prefix_length = -1;
Dmitry Potapov548601a2008-08-03 18:36:20 +040081 const char *errstr = NULL;
82
83 type = blob_type;
Bryan Larsen7672db22005-07-08 16:51:55 -070084
Steffen Prohaska2fb3f6d2009-01-18 13:00:12 +010085 git_extract_argv0_path(argv[0]);
86
Stephen Boyd37782922009-05-23 11:53:12 -070087 argc = parse_options(argc, argv, NULL, hash_object_options,
88 hash_object_usage, 0);
Adam Robend8ee4832008-05-23 16:19:38 +020089
Dmitry Potapov548601a2008-08-03 18:36:20 +040090 if (write_object) {
91 prefix = setup_git_directory();
92 prefix_length = prefix ? strlen(prefix) : 0;
Dmitry Potapov39702432008-08-03 18:36:21 +040093 if (vpath && prefix)
94 vpath = prefix_filename(prefix, prefix_length, vpath);
Dmitry Potapov548601a2008-08-03 18:36:20 +040095 }
Gerrit Pape8a2f5e52008-02-21 10:06:47 +000096
Elijah Newren272459a2009-02-28 12:56:49 -070097 git_config(git_default_config, NULL);
98
Dmitry Potapov548601a2008-08-03 18:36:20 +040099 if (stdin_paths) {
100 if (hashstdin)
101 errstr = "Can't use --stdin-paths with --stdin";
102 else if (argc)
103 errstr = "Can't specify files with --stdin-paths";
Dmitry Potapov39702432008-08-03 18:36:21 +0400104 else if (vpath)
105 errstr = "Can't use --stdin-paths with --path";
Dmitry Potapov4a3d85d2008-08-03 18:36:22 +0400106 else if (no_filters)
107 errstr = "Can't use --stdin-paths with --no-filters";
Dmitry Potapov548601a2008-08-03 18:36:20 +0400108 }
Dmitry Potapov4a3d85d2008-08-03 18:36:22 +0400109 else {
110 if (hashstdin > 1)
111 errstr = "Multiple --stdin arguments are not supported";
112 if (vpath && no_filters)
113 errstr = "Can't use --path with --no-filters";
114 }
Adam Robend8ee4832008-05-23 16:19:38 +0200115
Dmitry Potapov548601a2008-08-03 18:36:20 +0400116 if (errstr) {
Daniel Lowe42fc1132008-11-10 16:07:52 -0500117 error("%s", errstr);
Dmitry Potapov548601a2008-08-03 18:36:20 +0400118 usage_with_options(hash_object_usage, hash_object_options);
119 }
120
121 if (hashstdin)
Dmitry Potapov39702432008-08-03 18:36:21 +0400122 hash_fd(0, type, write_object, vpath);
Dmitry Potapov548601a2008-08-03 18:36:20 +0400123
124 for (i = 0 ; i < argc; i++) {
125 const char *arg = argv[i];
126
127 if (0 <= prefix_length)
128 arg = prefix_filename(prefix, prefix_length, arg);
Dmitry Potapov4a3d85d2008-08-03 18:36:22 +0400129 hash_object(arg, type, write_object,
130 no_filters ? NULL : vpath ? vpath : arg);
Bryan Larsen7672db22005-07-08 16:51:55 -0700131 }
Adam Robend8ee4832008-05-23 16:19:38 +0200132
133 if (stdin_paths)
134 hash_stdin_paths(type, write_object);
135
Bryan Larsen7672db22005-07-08 16:51:55 -0700136 return 0;
137}