date parsing: be friendlier to our European friends.
[git] / write-tree.c
1 /*
2  * GIT - The information manager from hell
3  *
4  * Copyright (C) Linus Torvalds, 2005
5  */
6 #include "cache.h"
7 #include "tree.h"
8
9 static int missing_ok = 0;
10
11 static int check_valid_sha1(unsigned char *sha1)
12 {
13         int ret;
14
15         /* If we were anal, we'd check that the sha1 of the contents actually matches */
16         ret = has_sha1_file(sha1);
17         if (ret == 0)
18                 perror(sha1_file_name(sha1));
19         return ret ? 0 : -1;
20 }
21
22 static int write_tree(struct cache_entry **cachep, int maxentries, const char *base, int baselen, unsigned char *returnsha1)
23 {
24         unsigned char subdir_sha1[20];
25         unsigned long size, offset;
26         char *buffer;
27         int nr;
28
29         /* Guess at some random initial size */
30         size = 8192;
31         buffer = xmalloc(size);
32         offset = 0;
33
34         nr = 0;
35         while (nr < maxentries) {
36                 struct cache_entry *ce = cachep[nr];
37                 const char *pathname = ce->name, *filename, *dirname;
38                 int pathlen = ce_namelen(ce), entrylen;
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;
47                 mode = ntohl(ce->ce_mode);
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);
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
67                 if (!missing_ok && check_valid_sha1(sha1) < 0)
68                         exit(1);
69
70                 entrylen = pathlen - baselen;
71                 if (offset + entrylen + 100 > size) {
72                         size = alloc_nr(offset + entrylen + 100);
73                         buffer = xrealloc(buffer, size);
74                 }
75                 offset += sprintf(buffer + offset, "%o %.*s", mode, entrylen, filename);
76                 buffer[offset++] = 0;
77                 memcpy(buffer + offset, sha1, 20);
78                 offset += 20;
79                 nr++;
80         }
81
82         write_sha1_file(buffer, offset, tree_type, returnsha1);
83         free(buffer);
84         return nr;
85 }
86
87 static const char write_tree_usage[] = "git-write-tree [--missing-ok]";
88
89 int main(int argc, char **argv)
90 {
91         int i, funny;
92         int entries;
93         unsigned char sha1[20];
94         
95         setup_git_directory();
96
97         entries = read_cache();
98         if (argc == 2) {
99                 if (!strcmp(argv[1], "--missing-ok"))
100                         missing_ok = 1;
101                 else
102                         die(write_tree_usage);
103         }
104         
105         if (argc > 2)
106                 die("too many options");
107
108         if (entries < 0)
109                 die("git-write-tree: error reading cache");
110
111         /* Verify that the tree is merged */
112         funny = 0;
113         for (i = 0; i < entries; i++) {
114                 struct cache_entry *ce = active_cache[i];
115                 if (ce_stage(ce)) {
116                         if (10 < ++funny) {
117                                 fprintf(stderr, "...\n");
118                                 break;
119                         }
120                         fprintf(stderr, "%s: unmerged (%s)\n", ce->name, sha1_to_hex(ce->sha1));
121                 }
122         }
123         if (funny)
124                 die("git-write-tree: not able to write tree");
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)
151                 die("git-write-tree: not able to write tree");
152
153         /* Ok, write it out */
154         if (write_tree(active_cache, entries, "", 0, sha1) != entries)
155                 die("git-write-tree: internal error");
156         printf("%s\n", sha1_to_hex(sha1));
157         return 0;
158 }