🌐 AI搜索 & 代理 主页
blob: f866059f24bacd314fa4a979334a9893dbfc19ba [file] [log] [blame]
Linus Torvalds8bc9a0c2005-04-07 22:16:101/*
2 * GIT - The information manager from hell
3 *
4 * Copyright (C) Linus Torvalds, 2005
5 */
Linus Torvaldse83c5162005-04-07 22:13:136#include "cache.h"
7
Bryan Larsen9c1fa702005-07-11 03:53:448static int missing_ok = 0;
9
Linus Torvaldse83c5162005-04-07 22:13:1310static int check_valid_sha1(unsigned char *sha1)
11{
Linus Torvaldse83c5162005-04-07 22:13:1312 int ret;
13
14 /* If we were anal, we'd check that the sha1 of the contents actually matches */
Jan Harkes7323aa12005-06-25 18:23:3615 ret = has_sha1_file(sha1);
16 if (ret == 0)
17 perror(sha1_file_name(sha1));
18 return ret ? 0 : -1;
Linus Torvaldse83c5162005-04-07 22:13:1319}
20
Linus Torvaldsd6d3f9d2005-04-10 00:09:3421static int write_tree(struct cache_entry **cachep, int maxentries, const char *base, int baselen, unsigned char *returnsha1)
Linus Torvaldse83c5162005-04-07 22:13:1322{
Linus Torvaldsd6d3f9d2005-04-10 00:09:3423 unsigned char subdir_sha1[20];
Linus Torvalds19b28602005-04-08 16:59:2824 unsigned long size, offset;
Linus Torvaldse83c5162005-04-07 22:13:1325 char *buffer;
Linus Torvaldsa44c9a52005-04-25 17:19:5326 int nr;
Linus Torvaldse83c5162005-04-07 22:13:1327
Linus Torvaldsd6d3f9d2005-04-10 00:09:3428 /* Guess at some random initial size */
29 size = 8192;
Christopher Li812666c2005-04-26 19:00:5830 buffer = xmalloc(size);
Linus Torvaldsa44c9a52005-04-25 17:19:5331 offset = 0;
Linus Torvaldse83c5162005-04-07 22:13:1332
Linus Torvaldsd6d3f9d2005-04-10 00:09:3433 nr = 0;
Petr Baudisc8993502005-05-08 14:15:5934 while (nr < maxentries) {
Linus Torvaldsd6d3f9d2005-04-10 00:09:3435 struct cache_entry *ce = cachep[nr];
36 const char *pathname = ce->name, *filename, *dirname;
Linus Torvaldsccc4feb2005-04-15 17:44:2737 int pathlen = ce_namelen(ce), entrylen;
Linus Torvaldsd6d3f9d2005-04-10 00:09:3438 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;
Linus Torvaldsccc4feb2005-04-15 17:44:2746 mode = ntohl(ce->ce_mode);
Linus Torvaldsd6d3f9d2005-04-10 00:09:3447
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);
Linus Torvaldsd6d3f9d2005-04-10 00:09:3455 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
Bryan Larsen9c1fa702005-07-11 03:53:4466 if (!missing_ok && check_valid_sha1(sha1) < 0)
Linus Torvaldse83c5162005-04-07 22:13:1367 exit(1);
Linus Torvaldsd6d3f9d2005-04-10 00:09:3468
69 entrylen = pathlen - baselen;
70 if (offset + entrylen + 100 > size) {
71 size = alloc_nr(offset + entrylen + 100);
Christopher Li812666c2005-04-26 19:00:5872 buffer = xrealloc(buffer, size);
Linus Torvaldse83c5162005-04-07 22:13:1373 }
Linus Torvaldsd6d3f9d2005-04-10 00:09:3474 offset += sprintf(buffer + offset, "%o %.*s", mode, entrylen, filename);
Linus Torvaldse83c5162005-04-07 22:13:1375 buffer[offset++] = 0;
Linus Torvaldsd6d3f9d2005-04-10 00:09:3476 memcpy(buffer + offset, sha1, 20);
Linus Torvaldse83c5162005-04-07 22:13:1377 offset += 20;
Linus Torvaldsd6d3f9d2005-04-10 00:09:3478 nr++;
Petr Baudisc8993502005-05-08 14:15:5979 }
Linus Torvaldse83c5162005-04-07 22:13:1380
Linus Torvaldsa44c9a52005-04-25 17:19:5381 write_sha1_file(buffer, offset, "tree", returnsha1);
Brad Roberts7223a882005-04-17 17:55:1282 free(buffer);
Linus Torvaldsd6d3f9d2005-04-10 00:09:3483 return nr;
84}
85
Junio C Hamano75a46f62005-12-06 06:30:0786static const char write_tree_usage[] = "git-write-tree [--missing-ok]";
87
Linus Torvaldsd6d3f9d2005-04-10 00:09:3488int main(int argc, char **argv)
89{
Junio C Hamano8eef4d32005-05-07 19:22:0290 int i, funny;
Junio C Hamano53228a52005-11-26 08:50:0291 int entries;
Linus Torvaldsd6d3f9d2005-04-10 00:09:3492 unsigned char sha1[20];
Bryan Larsen9c1fa702005-07-11 03:53:4493
Junio C Hamano53228a52005-11-26 08:50:0294 setup_git_directory();
95
96 entries = read_cache();
Petr Baudis0b124bb2005-07-29 09:00:4597 if (argc == 2) {
Bryan Larsen9c1fa702005-07-11 03:53:4498 if (!strcmp(argv[1], "--missing-ok"))
99 missing_ok = 1;
100 else
Junio C Hamano75a46f62005-12-06 06:30:07101 die(write_tree_usage);
Bryan Larsen9c1fa702005-07-11 03:53:44102 }
103
Petr Baudis0b124bb2005-07-29 09:00:45104 if (argc > 2)
Bryan Larsen9c1fa702005-07-11 03:53:44105 die("too many options");
Linus Torvaldsd6d3f9d2005-04-10 00:09:34106
Petr Baudisc8993502005-05-08 14:15:59107 if (entries < 0)
Alexey Nezhdanov667bb592005-05-19 11:17:16108 die("git-write-tree: error reading cache");
Linus Torvaldsc347ea52005-04-16 05:04:54109
110 /* Verify that the tree is merged */
Junio C Hamano8eef4d32005-05-07 19:22:02111 funny = 0;
Linus Torvaldsc347ea52005-04-16 05:04:54112 for (i = 0; i < entries; i++) {
113 struct cache_entry *ce = active_cache[i];
114 if (ntohs(ce->ce_flags) & ~CE_NAMEMASK) {
Junio C Hamano8eef4d32005-05-07 19:22:02115 if (10 < ++funny) {
Linus Torvaldsc347ea52005-04-16 05:04:54116 fprintf(stderr, "...\n");
117 break;
118 }
119 fprintf(stderr, "%s: unmerged (%s)\n", ce->name, sha1_to_hex(ce->sha1));
120 }
121 }
Junio C Hamano8eef4d32005-05-07 19:22:02122 if (funny)
Alexey Nezhdanov667bb592005-05-19 11:17:16123 die("git-write-tree: not able to write tree");
Junio C Hamano8eef4d32005-05-07 19:22:02124
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)
Alexey Nezhdanov667bb592005-05-19 11:17:16150 die("git-write-tree: not able to write tree");
Linus Torvaldsc347ea52005-04-16 05:04:54151
152 /* Ok, write it out */
Linus Torvaldsd6d3f9d2005-04-10 00:09:34153 if (write_tree(active_cache, entries, "", 0, sha1) != entries)
Alexey Nezhdanov667bb592005-05-19 11:17:16154 die("git-write-tree: internal error");
Linus Torvaldsd6d3f9d2005-04-10 00:09:34155 printf("%s\n", sha1_to_hex(sha1));
Linus Torvaldse83c5162005-04-07 22:13:13156 return 0;
157}