Add a "rev-tree" helper, which calculates the revision
[git/git.git] / read-tree.c
CommitLineData
8bc9a0c7
LT
1/*
2 * GIT - The information manager from hell
3 *
4 * Copyright (C) Linus Torvalds, 2005
5 */
e83c5163
LT
6#include "cache.h"
7
f768846e 8static int read_one_entry(unsigned char *sha1, const char *base, int baselen, const char *pathname, unsigned mode)
e497ea2a 9{
83adac3c 10 int len = strlen(pathname);
f768846e 11 unsigned int size = cache_entry_size(baselen + len);
83adac3c 12 struct cache_entry *ce = malloc(size);
e497ea2a 13
83adac3c 14 memset(ce, 0, size);
e497ea2a 15
83adac3c 16 ce->st_mode = mode;
f768846e
LT
17 ce->namelen = baselen + len;
18 memcpy(ce->name, base, baselen);
19 memcpy(ce->name + baselen, pathname, len+1);
83adac3c 20 memcpy(ce->sha1, sha1, 20);
121481ab 21 return add_cache_entry(ce, 1);
e497ea2a
LT
22}
23
f768846e 24static int read_tree(unsigned char *sha1, const char *base, int baselen)
e83c5163
LT
25{
26 void *buffer;
27 unsigned long size;
28 char type[20];
29
30 buffer = read_sha1_file(sha1, type, &size);
31 if (!buffer)
83adac3c 32 return -1;
e83c5163 33 if (strcmp(type, "tree"))
83adac3c 34 return -1;
e83c5163
LT
35 while (size) {
36 int len = strlen(buffer)+1;
37 unsigned char *sha1 = buffer + len;
38 char *path = strchr(buffer, ' ')+1;
39 unsigned int mode;
e497ea2a 40
e83c5163 41 if (size < len + 20 || sscanf(buffer, "%o", &mode) != 1)
83adac3c
LT
42 return -1;
43
e83c5163
LT
44 buffer = sha1 + 20;
45 size -= len + 20;
83adac3c 46
f768846e
LT
47 if (S_ISDIR(mode)) {
48 int retval;
49 int pathlen = strlen(path);
50 char *newbase = malloc(baselen + 1 + pathlen);
51 memcpy(newbase, base, baselen);
52 memcpy(newbase + baselen, path, pathlen);
53 newbase[baselen + pathlen] = '/';
54 retval = read_tree(sha1, newbase, baselen + pathlen + 1);
55 free(newbase);
56 if (retval)
57 return -1;
58 continue;
59 }
60 if (read_one_entry(sha1, base, baselen, path, mode) < 0)
83adac3c 61 return -1;
e83c5163
LT
62 }
63 return 0;
64}
65
66int main(int argc, char **argv)
67{
83adac3c 68 int i, newfd;
e83c5163
LT
69 unsigned char sha1[20];
70
83adac3c
LT
71 newfd = open(".dircache/index.lock", O_RDWR | O_CREAT | O_EXCL, 0600);
72 if (newfd < 0)
73 usage("unable to create new cachefile");
74
75 for (i = 1; i < argc; i++) {
76 const char *arg = argv[i];
77
78 /* "-m" stands for "merge" current directory cache */
79 if (!strcmp(arg, "-m")) {
80 if (active_cache) {
81 fprintf(stderr, "read-tree: cannot merge old cache on top of new\n");
82 goto out;
83 }
84 if (read_cache() < 0) {
85 fprintf(stderr, "read-tree: corrupt directory cache\n");
86 goto out;
87 }
88 continue;
89 }
90 if (get_sha1_hex(arg, sha1) < 0) {
91 fprintf(stderr, "read-tree [-m] <sha1>\n");
92 goto out;
93 }
f768846e 94 if (read_tree(sha1, "", 0) < 0) {
83adac3c
LT
95 fprintf(stderr, "failed to unpack tree object %s\n", arg);
96 goto out;
97 }
98 }
99 if (!write_cache(newfd, active_cache, active_nr) && !rename(".dircache/index.lock", ".dircache/index"))
100 return 0;
101
102out:
103 unlink(".dircache/index.lock");
104 exit(1);
e83c5163 105}