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