blob: 0a58f3f1267dcb4dbd67c89fc165367c6840f1da [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"
Bryan Larsen7672db22005-07-08 16:51:55 -07009
Junio C Hamanoedaec3f2007-02-28 11:45:56 -080010static void hash_object(const char *path, enum object_type type, int write_object)
Bryan Larsen7672db22005-07-08 16:51:55 -070011{
12 int fd;
13 struct stat st;
14 unsigned char sha1[20];
15 fd = open(path, O_RDONLY);
16 if (fd < 0 ||
17 fstat(fd, &st) < 0 ||
Junio C Hamano53bca912007-02-28 11:52:04 -080018 index_fd(sha1, fd, &st, write_object, 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));
23}
24
Daniel Barkalow024510c2005-12-10 17:25:24 -050025static void hash_stdin(const char *type, int write_object)
26{
27 unsigned char sha1[20];
28 if (index_pipe(sha1, 0, type, write_object))
29 die("Unable to add stdin to database");
30 printf("%s\n", sha1_to_hex(sha1));
31}
32
Petr Baudis4d1f1192005-07-29 11:01:26 +020033static const char hash_object_usage[] =
Daniel Barkalow024510c2005-12-10 17:25:24 -050034"git-hash-object [-t <type>] [-w] [--stdin] <file>...";
Bryan Larsen7672db22005-07-08 16:51:55 -070035
36int main(int argc, char **argv)
37{
38 int i;
Peter Eriksen8e440252006-04-02 14:44:09 +020039 const char *type = blob_type;
Bryan Larsen7672db22005-07-08 16:51:55 -070040 int write_object = 0;
Junio C Hamano99e01692005-11-28 03:19:03 -080041 const char *prefix = NULL;
Junio C Hamano706fe6a2005-11-26 00:30:07 -080042 int prefix_length = -1;
Junio C Hamano9c2e7c02005-12-05 22:29:05 -080043 int no_more_flags = 0;
Bryan Larsen7672db22005-07-08 16:51:55 -070044
Nicolas Pitreff350cc2007-11-10 15:00:33 -050045 git_config(git_default_config);
46
Bryan Larsen7672db22005-07-08 16:51:55 -070047 for (i = 1 ; i < argc; i++) {
Junio C Hamano9c2e7c02005-12-05 22:29:05 -080048 if (!no_more_flags && argv[i][0] == '-') {
49 if (!strcmp(argv[i], "-t")) {
50 if (argc <= ++i)
Ramsay Allan Jones8cdf3362006-08-03 16:48:41 +010051 usage(hash_object_usage);
Junio C Hamano9c2e7c02005-12-05 22:29:05 -080052 type = argv[i];
Junio C Hamano706fe6a2005-11-26 00:30:07 -080053 }
Junio C Hamano9c2e7c02005-12-05 22:29:05 -080054 else if (!strcmp(argv[i], "-w")) {
55 if (prefix_length < 0) {
56 prefix = setup_git_directory();
57 prefix_length =
58 prefix ? strlen(prefix) : 0;
59 }
60 write_object = 1;
61 }
62 else if (!strcmp(argv[i], "--")) {
63 no_more_flags = 1;
64 }
65 else if (!strcmp(argv[i], "--help"))
66 usage(hash_object_usage);
Daniel Barkalow024510c2005-12-10 17:25:24 -050067 else if (!strcmp(argv[i], "--stdin")) {
68 hash_stdin(type, write_object);
69 }
Junio C Hamano9c2e7c02005-12-05 22:29:05 -080070 else
Ramsay Allan Jones8cdf3362006-08-03 16:48:41 +010071 usage(hash_object_usage);
72 }
Junio C Hamano706fe6a2005-11-26 00:30:07 -080073 else {
Junio C Hamano99e01692005-11-28 03:19:03 -080074 const char *arg = argv[i];
Junio C Hamano706fe6a2005-11-26 00:30:07 -080075 if (0 <= prefix_length)
76 arg = prefix_filename(prefix, prefix_length,
77 arg);
Junio C Hamanoedaec3f2007-02-28 11:45:56 -080078 hash_object(arg, type_from_string(type), write_object);
Junio C Hamano9c2e7c02005-12-05 22:29:05 -080079 no_more_flags = 1;
Junio C Hamano706fe6a2005-11-26 00:30:07 -080080 }
Bryan Larsen7672db22005-07-08 16:51:55 -070081 }
82 return 0;
83}