blob: 2b2c6b77afd53870423d4bad9191ed669bc55660 [file] [log] [blame]
Linus Torvalds8bc9a0c2005-04-07 15:16:10 -07001/*
2 * GIT - The information manager from hell
3 *
4 * Copyright (C) Linus Torvalds, 2005
5 */
Linus Torvaldse83c5162005-04-07 15:13:13 -07006#include "cache.h"
7
Bryan Larsen9c1fa702005-07-10 20:53:44 -07008static int missing_ok = 0;
9
Linus Torvaldse83c5162005-04-07 15:13:13 -070010static int check_valid_sha1(unsigned char *sha1)
11{
Linus Torvaldse83c5162005-04-07 15:13:13 -070012 int ret;
13
14 /* If we were anal, we'd check that the sha1 of the contents actually matches */
Jan Harkes7323aa12005-06-25 14:23:36 -040015 ret = has_sha1_file(sha1);
16 if (ret == 0)
17 perror(sha1_file_name(sha1));
18 return ret ? 0 : -1;
Linus Torvaldse83c5162005-04-07 15:13:13 -070019}
20
Linus Torvaldsd6d3f9d2005-04-09 17:09:34 -070021static int write_tree(struct cache_entry **cachep, int maxentries, const char *base, int baselen, unsigned char *returnsha1)
Linus Torvaldse83c5162005-04-07 15:13:13 -070022{
Linus Torvaldsd6d3f9d2005-04-09 17:09:34 -070023 unsigned char subdir_sha1[20];
Linus Torvalds19b28602005-04-08 09:59:28 -070024 unsigned long size, offset;
Linus Torvaldse83c5162005-04-07 15:13:13 -070025 char *buffer;
Linus Torvaldsa44c9a52005-04-25 10:19:53 -070026 int nr;
Linus Torvaldse83c5162005-04-07 15:13:13 -070027
Linus Torvaldsd6d3f9d2005-04-09 17:09:34 -070028 /* Guess at some random initial size */
29 size = 8192;
Christopher Li812666c2005-04-26 12:00:58 -070030 buffer = xmalloc(size);
Linus Torvaldsa44c9a52005-04-25 10:19:53 -070031 offset = 0;
Linus Torvaldse83c5162005-04-07 15:13:13 -070032
Linus Torvaldsd6d3f9d2005-04-09 17:09:34 -070033 nr = 0;
Petr Baudisc8993502005-05-08 16:15:59 +020034 while (nr < maxentries) {
Linus Torvaldsd6d3f9d2005-04-09 17:09:34 -070035 struct cache_entry *ce = cachep[nr];
36 const char *pathname = ce->name, *filename, *dirname;
Linus Torvaldsccc4feb2005-04-15 10:44:27 -070037 int pathlen = ce_namelen(ce), entrylen;
Linus Torvaldsd6d3f9d2005-04-09 17:09:34 -070038 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 10:44:27 -070046 mode = ntohl(ce->ce_mode);
Linus Torvaldsd6d3f9d2005-04-09 17:09:34 -070047
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-09 17:09:34 -070055 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-10 20:53:44 -070066 if (!missing_ok && check_valid_sha1(sha1) < 0)
Linus Torvaldse83c5162005-04-07 15:13:13 -070067 exit(1);
Linus Torvaldsd6d3f9d2005-04-09 17:09:34 -070068
69 entrylen = pathlen - baselen;
70 if (offset + entrylen + 100 > size) {
71 size = alloc_nr(offset + entrylen + 100);
Christopher Li812666c2005-04-26 12:00:58 -070072 buffer = xrealloc(buffer, size);
Linus Torvaldse83c5162005-04-07 15:13:13 -070073 }
Linus Torvaldsd6d3f9d2005-04-09 17:09:34 -070074 offset += sprintf(buffer + offset, "%o %.*s", mode, entrylen, filename);
Linus Torvaldse83c5162005-04-07 15:13:13 -070075 buffer[offset++] = 0;
Linus Torvaldsd6d3f9d2005-04-09 17:09:34 -070076 memcpy(buffer + offset, sha1, 20);
Linus Torvaldse83c5162005-04-07 15:13:13 -070077 offset += 20;
Linus Torvaldsd6d3f9d2005-04-09 17:09:34 -070078 nr++;
Petr Baudisc8993502005-05-08 16:15:59 +020079 }
Linus Torvaldse83c5162005-04-07 15:13:13 -070080
Linus Torvaldsa44c9a52005-04-25 10:19:53 -070081 write_sha1_file(buffer, offset, "tree", returnsha1);
Brad Roberts7223a882005-04-17 10:55:12 -070082 free(buffer);
Linus Torvaldsd6d3f9d2005-04-09 17:09:34 -070083 return nr;
84}
85
86int main(int argc, char **argv)
87{
Junio C Hamano8eef4d32005-05-07 12:22:02 -070088 int i, funny;
Linus Torvaldsd6d3f9d2005-04-09 17:09:34 -070089 int entries = read_cache();
90 unsigned char sha1[20];
Bryan Larsen9c1fa702005-07-10 20:53:44 -070091
Petr Baudis0b124bb2005-07-29 11:00:45 +020092 if (argc == 2) {
Bryan Larsen9c1fa702005-07-10 20:53:44 -070093 if (!strcmp(argv[1], "--missing-ok"))
94 missing_ok = 1;
95 else
96 die("unknown option %s", argv[1]);
97 }
98
Petr Baudis0b124bb2005-07-29 11:00:45 +020099 if (argc > 2)
Bryan Larsen9c1fa702005-07-10 20:53:44 -0700100 die("too many options");
Linus Torvaldsd6d3f9d2005-04-09 17:09:34 -0700101
Petr Baudisc8993502005-05-08 16:15:59 +0200102 if (entries < 0)
Alexey Nezhdanov667bb592005-05-19 15:17:16 +0400103 die("git-write-tree: error reading cache");
Linus Torvaldsc347ea52005-04-15 22:04:54 -0700104
105 /* Verify that the tree is merged */
Junio C Hamano8eef4d32005-05-07 12:22:02 -0700106 funny = 0;
Linus Torvaldsc347ea52005-04-15 22:04:54 -0700107 for (i = 0; i < entries; i++) {
108 struct cache_entry *ce = active_cache[i];
109 if (ntohs(ce->ce_flags) & ~CE_NAMEMASK) {
Junio C Hamano8eef4d32005-05-07 12:22:02 -0700110 if (10 < ++funny) {
Linus Torvaldsc347ea52005-04-15 22:04:54 -0700111 fprintf(stderr, "...\n");
112 break;
113 }
114 fprintf(stderr, "%s: unmerged (%s)\n", ce->name, sha1_to_hex(ce->sha1));
115 }
116 }
Junio C Hamano8eef4d32005-05-07 12:22:02 -0700117 if (funny)
Alexey Nezhdanov667bb592005-05-19 15:17:16 +0400118 die("git-write-tree: not able to write tree");
Junio C Hamano8eef4d32005-05-07 12:22:02 -0700119
120 /* Also verify that the cache does not have path and path/file
121 * at the same time. At this point we know the cache has only
122 * stage 0 entries.
123 */
124 funny = 0;
125 for (i = 0; i < entries - 1; i++) {
126 /* path/file always comes after path because of the way
127 * the cache is sorted. Also path can appear only once,
128 * which means conflicting one would immediately follow.
129 */
130 const char *this_name = active_cache[i]->name;
131 const char *next_name = active_cache[i+1]->name;
132 int this_len = strlen(this_name);
133 if (this_len < strlen(next_name) &&
134 strncmp(this_name, next_name, this_len) == 0 &&
135 next_name[this_len] == '/') {
136 if (10 < ++funny) {
137 fprintf(stderr, "...\n");
138 break;
139 }
140 fprintf(stderr, "You have both %s and %s\n",
141 this_name, next_name);
142 }
143 }
144 if (funny)
Alexey Nezhdanov667bb592005-05-19 15:17:16 +0400145 die("git-write-tree: not able to write tree");
Linus Torvaldsc347ea52005-04-15 22:04:54 -0700146
147 /* Ok, write it out */
Linus Torvaldsd6d3f9d2005-04-09 17:09:34 -0700148 if (write_tree(active_cache, entries, "", 0, sha1) != entries)
Alexey Nezhdanov667bb592005-05-19 15:17:16 +0400149 die("git-write-tree: internal error");
Linus Torvaldsd6d3f9d2005-04-09 17:09:34 -0700150 printf("%s\n", sha1_to_hex(sha1));
Linus Torvaldse83c5162005-04-07 15:13:13 -0700151 return 0;
152}