]> git.ipfire.org Git - thirdparty/git.git/blame - write-tree.c
Add cache-tree.
[thirdparty/git.git] / write-tree.c
CommitLineData
8bc9a0c7
LT
1/*
2 * GIT - The information manager from hell
3 *
4 * Copyright (C) Linus Torvalds, 2005
5 */
e83c5163 6#include "cache.h"
8e440259 7#include "tree.h"
e83c5163 8
9c1fa70a
BL
9static int missing_ok = 0;
10
e83c5163
LT
11static int check_valid_sha1(unsigned char *sha1)
12{
e83c5163
LT
13 int ret;
14
15 /* If we were anal, we'd check that the sha1 of the contents actually matches */
7323aa11
JH
16 ret = has_sha1_file(sha1);
17 if (ret == 0)
18 perror(sha1_file_name(sha1));
19 return ret ? 0 : -1;
e83c5163
LT
20}
21
d6d3f9d0 22static int write_tree(struct cache_entry **cachep, int maxentries, const char *base, int baselen, unsigned char *returnsha1)
e83c5163 23{
d6d3f9d0 24 unsigned char subdir_sha1[20];
19b2860c 25 unsigned long size, offset;
e83c5163 26 char *buffer;
a44c9a5e 27 int nr;
e83c5163 28
d6d3f9d0
LT
29 /* Guess at some random initial size */
30 size = 8192;
812666c8 31 buffer = xmalloc(size);
a44c9a5e 32 offset = 0;
e83c5163 33
d6d3f9d0 34 nr = 0;
c899350e 35 while (nr < maxentries) {
d6d3f9d0
LT
36 struct cache_entry *ce = cachep[nr];
37 const char *pathname = ce->name, *filename, *dirname;
ccc4feb5 38 int pathlen = ce_namelen(ce), entrylen;
d6d3f9d0
LT
39 unsigned char *sha1;
40 unsigned int mode;
41
42 /* Did we hit the end of the directory? Return how many we wrote */
43 if (baselen >= pathlen || memcmp(base, pathname, baselen))
44 break;
45
46 sha1 = ce->sha1;
ccc4feb5 47 mode = ntohl(ce->ce_mode);
d6d3f9d0
LT
48
49 /* Do we have _further_ subdirectories? */
50 filename = pathname + baselen;
51 dirname = strchr(filename, '/');
52 if (dirname) {
53 int subdir_written;
54
55 subdir_written = write_tree(cachep + nr, maxentries - nr, pathname, dirname-pathname+1, subdir_sha1);
d6d3f9d0
LT
56 nr += subdir_written;
57
58 /* Now we need to write out the directory entry into this tree.. */
59 mode = S_IFDIR;
60 pathlen = dirname - pathname;
61
62 /* ..but the directory entry doesn't count towards the total count */
63 nr--;
64 sha1 = subdir_sha1;
65 }
66
9c1fa70a 67 if (!missing_ok && check_valid_sha1(sha1) < 0)
e83c5163 68 exit(1);
d6d3f9d0
LT
69
70 entrylen = pathlen - baselen;
71 if (offset + entrylen + 100 > size) {
72 size = alloc_nr(offset + entrylen + 100);
812666c8 73 buffer = xrealloc(buffer, size);
e83c5163 74 }
d6d3f9d0 75 offset += sprintf(buffer + offset, "%o %.*s", mode, entrylen, filename);
e83c5163 76 buffer[offset++] = 0;
d6d3f9d0 77 memcpy(buffer + offset, sha1, 20);
e83c5163 78 offset += 20;
d6d3f9d0 79 nr++;
c899350e 80 }
e83c5163 81
8e440259 82 write_sha1_file(buffer, offset, tree_type, returnsha1);
7223a88c 83 free(buffer);
d6d3f9d0
LT
84 return nr;
85}
86
75a46f6b
JH
87static const char write_tree_usage[] = "git-write-tree [--missing-ok]";
88
d6d3f9d0
LT
89int main(int argc, char **argv)
90{
8eef4d3e 91 int i, funny;
53228a5f 92 int entries;
d6d3f9d0 93 unsigned char sha1[20];
9c1fa70a 94
53228a5f
JH
95 setup_git_directory();
96
97 entries = read_cache();
0b124bb4 98 if (argc == 2) {
9c1fa70a
BL
99 if (!strcmp(argv[1], "--missing-ok"))
100 missing_ok = 1;
101 else
75a46f6b 102 die(write_tree_usage);
9c1fa70a
BL
103 }
104
0b124bb4 105 if (argc > 2)
9c1fa70a 106 die("too many options");
d6d3f9d0 107
c899350e 108 if (entries < 0)
667bb59b 109 die("git-write-tree: error reading cache");
c347ea5d
LT
110
111 /* Verify that the tree is merged */
8eef4d3e 112 funny = 0;
c347ea5d
LT
113 for (i = 0; i < entries; i++) {
114 struct cache_entry *ce = active_cache[i];
5f73076c 115 if (ce_stage(ce)) {
8eef4d3e 116 if (10 < ++funny) {
c347ea5d
LT
117 fprintf(stderr, "...\n");
118 break;
119 }
120 fprintf(stderr, "%s: unmerged (%s)\n", ce->name, sha1_to_hex(ce->sha1));
121 }
122 }
8eef4d3e 123 if (funny)
667bb59b 124 die("git-write-tree: not able to write tree");
8eef4d3e
JH
125
126 /* Also verify that the cache does not have path and path/file
127 * at the same time. At this point we know the cache has only
128 * stage 0 entries.
129 */
130 funny = 0;
131 for (i = 0; i < entries - 1; i++) {
132 /* path/file always comes after path because of the way
133 * the cache is sorted. Also path can appear only once,
134 * which means conflicting one would immediately follow.
135 */
136 const char *this_name = active_cache[i]->name;
137 const char *next_name = active_cache[i+1]->name;
138 int this_len = strlen(this_name);
139 if (this_len < strlen(next_name) &&
140 strncmp(this_name, next_name, this_len) == 0 &&
141 next_name[this_len] == '/') {
142 if (10 < ++funny) {
143 fprintf(stderr, "...\n");
144 break;
145 }
146 fprintf(stderr, "You have both %s and %s\n",
147 this_name, next_name);
148 }
149 }
150 if (funny)
667bb59b 151 die("git-write-tree: not able to write tree");
c347ea5d
LT
152
153 /* Ok, write it out */
d6d3f9d0 154 if (write_tree(active_cache, entries, "", 0, sha1) != entries)
667bb59b 155 die("git-write-tree: internal error");
d6d3f9d0 156 printf("%s\n", sha1_to_hex(sha1));
e83c5163
LT
157 return 0;
158}