blob: 43bd93bffb3f5e0b7b19a62865347a421d6a56a3 [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
10static void hash_object(const char *path, const char *type, int write_object)
11{
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 ||
18 index_fd(sha1, fd, &st, write_object, type))
19 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
45 for (i = 1 ; i < argc; i++) {
Junio C Hamano9c2e7c02005-12-05 22:29:05 -080046 if (!no_more_flags && argv[i][0] == '-') {
47 if (!strcmp(argv[i], "-t")) {
48 if (argc <= ++i)
49 die(hash_object_usage);
50 type = argv[i];
Junio C Hamano706fe6a2005-11-26 00:30:07 -080051 }
Junio C Hamano9c2e7c02005-12-05 22:29:05 -080052 else if (!strcmp(argv[i], "-w")) {
53 if (prefix_length < 0) {
54 prefix = setup_git_directory();
55 prefix_length =
56 prefix ? strlen(prefix) : 0;
57 }
58 write_object = 1;
59 }
60 else if (!strcmp(argv[i], "--")) {
61 no_more_flags = 1;
62 }
63 else if (!strcmp(argv[i], "--help"))
64 usage(hash_object_usage);
Daniel Barkalow024510c2005-12-10 17:25:24 -050065 else if (!strcmp(argv[i], "--stdin")) {
66 hash_stdin(type, write_object);
67 }
Junio C Hamano9c2e7c02005-12-05 22:29:05 -080068 else
69 die(hash_object_usage);
Daniel Barkalow024510c2005-12-10 17:25:24 -050070 }
Junio C Hamano706fe6a2005-11-26 00:30:07 -080071 else {
Junio C Hamano99e01692005-11-28 03:19:03 -080072 const char *arg = argv[i];
Junio C Hamano706fe6a2005-11-26 00:30:07 -080073 if (0 <= prefix_length)
74 arg = prefix_filename(prefix, prefix_length,
75 arg);
76 hash_object(arg, type, write_object);
Junio C Hamano9c2e7c02005-12-05 22:29:05 -080077 no_more_flags = 1;
Junio C Hamano706fe6a2005-11-26 00:30:07 -080078 }
Bryan Larsen7672db22005-07-08 16:51:55 -070079 }
80 return 0;
81}