Add a "checkout-cache" command which does what the name suggests.
[git/debian.git] / commit-tree.c
blob0d7852f7f295d9fb85b00a71bcb7cfbeb1acd026
1 /*
2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
5 */
6 #include "cache.h"
8 #include <pwd.h>
9 #include <time.h>
11 #define BLOCKING (1ul << 14)
12 #define ORIG_OFFSET (40)
15 * Leave space at the beginning to insert the tag
16 * once we know how big things are.
18 * FIXME! Share the code with "write-tree.c"
20 static void init_buffer(char **bufp, unsigned int *sizep)
22 char *buf = malloc(BLOCKING);
23 memset(buf, 0, ORIG_OFFSET);
24 *sizep = ORIG_OFFSET;
25 *bufp = buf;
28 static void add_buffer(char **bufp, unsigned int *sizep, const char *fmt, ...)
30 char one_line[2048];
31 va_list args;
32 int len;
33 unsigned long alloc, size, newsize;
34 char *buf;
36 va_start(args, fmt);
37 len = vsnprintf(one_line, sizeof(one_line), fmt, args);
38 va_end(args);
39 size = *sizep;
40 newsize = size + len;
41 alloc = (size + 32767) & ~32767;
42 buf = *bufp;
43 if (newsize > alloc) {
44 alloc = (newsize + 32767) & ~32767;
45 buf = realloc(buf, alloc);
46 *bufp = buf;
48 *sizep = newsize;
49 memcpy(buf + size, one_line, len);
52 static int prepend_integer(char *buffer, unsigned val, int i)
54 buffer[--i] = '\0';
55 do {
56 buffer[--i] = '0' + (val % 10);
57 val /= 10;
58 } while (val);
59 return i;
62 static void finish_buffer(char *tag, char **bufp, unsigned int *sizep)
64 int taglen;
65 int offset;
66 char *buf = *bufp;
67 unsigned int size = *sizep;
69 offset = prepend_integer(buf, size - ORIG_OFFSET, ORIG_OFFSET);
70 taglen = strlen(tag);
71 offset -= taglen;
72 buf += offset;
73 size -= offset;
74 memcpy(buf, tag, taglen);
76 *bufp = buf;
77 *sizep = size;
80 static void remove_special(char *p)
82 char c;
83 char *dst = p;
85 for (;;) {
86 c = *p;
87 p++;
88 switch(c) {
89 case '\n': case '<': case '>':
90 continue;
92 *dst++ = c;
93 if (!c)
94 break;
99 * Having more than two parents may be strange, but hey, there's
100 * no conceptual reason why the file format couldn't accept multi-way
101 * merges. It might be the "union" of several packages, for example.
103 * I don't really expect that to happen, but this is here to make
104 * it clear that _conceptually_ it's ok..
106 #define MAXPARENT (16)
108 int main(int argc, char **argv)
110 int i, len;
111 int parents = 0;
112 unsigned char tree_sha1[20];
113 unsigned char parent_sha1[MAXPARENT][20];
114 char *gecos, *realgecos;
115 char *email, realemail[1000];
116 char *date, *realdate;
117 char comment[1000];
118 struct passwd *pw;
119 time_t now;
120 char *buffer;
121 unsigned int size;
123 if (argc < 2 || get_sha1_hex(argv[1], tree_sha1) < 0)
124 usage("commit-tree <sha1> [-p <sha1>]* < changelog");
126 for (i = 2; i < argc; i += 2) {
127 char *a, *b;
128 a = argv[i]; b = argv[i+1];
129 if (!b || strcmp(a, "-p") || get_sha1_hex(b, parent_sha1[parents]))
130 usage("commit-tree <sha1> [-p <sha1>]* < changelog");
131 parents++;
133 if (!parents)
134 fprintf(stderr, "Committing initial tree %s\n", argv[1]);
135 pw = getpwuid(getuid());
136 if (!pw)
137 usage("You don't exist. Go away!");
138 realgecos = pw->pw_gecos;
139 len = strlen(pw->pw_name);
140 memcpy(realemail, pw->pw_name, len);
141 realemail[len] = '@';
142 gethostname(realemail+len+1, sizeof(realemail)-len-1);
143 time(&now);
144 realdate = ctime(&now);
146 gecos = getenv("COMMITTER_NAME") ? : realgecos;
147 email = getenv("COMMITTER_EMAIL") ? : realemail;
148 date = getenv("COMMITTER_DATE") ? : realdate;
150 remove_special(gecos); remove_special(realgecos);
151 remove_special(email); remove_special(realemail);
152 remove_special(date); remove_special(realdate);
154 init_buffer(&buffer, &size);
155 add_buffer(&buffer, &size, "tree %s\n", sha1_to_hex(tree_sha1));
158 * NOTE! This ordering means that the same exact tree merged with a
159 * different order of parents will be a _different_ changeset even
160 * if everything else stays the same.
162 for (i = 0; i < parents; i++)
163 add_buffer(&buffer, &size, "parent %s\n", sha1_to_hex(parent_sha1[i]));
165 /* Person/date information */
166 add_buffer(&buffer, &size, "author %s <%s> %s\n", gecos, email, date);
167 add_buffer(&buffer, &size, "committer %s <%s> %s\n\n", realgecos, realemail, realdate);
169 /* And add the comment */
170 while (fgets(comment, sizeof(comment), stdin) != NULL)
171 add_buffer(&buffer, &size, "%s", comment);
173 finish_buffer("commit ", &buffer, &size);
175 write_sha1_file(buffer, size);
176 return 0;