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