]>
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 | ||
8 | #include <pwd.h> | |
9 | #include <time.h> | |
27de946d DW |
10 | #include <string.h> |
11 | #include <ctype.h> | |
12 | #include <time.h> | |
d167f147 | 13 | #include <curl/curl.h> |
e83c5163 LT |
14 | |
15 | #define BLOCKING (1ul << 14) | |
e83c5163 LT |
16 | |
17 | /* | |
e83c5163 LT |
18 | * FIXME! Share the code with "write-tree.c" |
19 | */ | |
20 | static void init_buffer(char **bufp, unsigned int *sizep) | |
21 | { | |
812666c8 | 22 | char *buf = xmalloc(BLOCKING); |
a44c9a5e | 23 | *sizep = 0; |
e83c5163 LT |
24 | *bufp = buf; |
25 | } | |
26 | ||
27 | static void add_buffer(char **bufp, unsigned int *sizep, const char *fmt, ...) | |
28 | { | |
29 | char one_line[2048]; | |
30 | va_list args; | |
31 | int len; | |
32 | unsigned long alloc, size, newsize; | |
33 | char *buf; | |
34 | ||
35 | va_start(args, fmt); | |
36 | len = vsnprintf(one_line, sizeof(one_line), fmt, args); | |
37 | va_end(args); | |
38 | size = *sizep; | |
39 | newsize = size + len; | |
40 | alloc = (size + 32767) & ~32767; | |
41 | buf = *bufp; | |
42 | if (newsize > alloc) { | |
aebb2679 | 43 | alloc = (newsize + 32767) & ~32767; |
812666c8 | 44 | buf = xrealloc(buf, alloc); |
e83c5163 LT |
45 | *bufp = buf; |
46 | } | |
47 | *sizep = newsize; | |
48 | memcpy(buf + size, one_line, len); | |
49 | } | |
50 | ||
e83c5163 LT |
51 | static void remove_special(char *p) |
52 | { | |
53 | char c; | |
74b2428f | 54 | char *dst = p, *src = p; |
e83c5163 LT |
55 | |
56 | for (;;) { | |
74b2428f BR |
57 | c = *src; |
58 | src++; | |
e83c5163 LT |
59 | switch(c) { |
60 | case '\n': case '<': case '>': | |
61 | continue; | |
62 | } | |
63 | *dst++ = c; | |
64 | if (!c) | |
65 | break; | |
66 | } | |
5e5128ed LT |
67 | |
68 | /* | |
69 | * Go back, and remove crud from the end: some people | |
70 | * have commas etc in their gecos field | |
71 | */ | |
72 | dst--; | |
73 | while (--dst >= p) { | |
74 | unsigned char c = *dst; | |
75 | switch (c) { | |
76 | case ',': case ';': case '.': | |
77 | *dst = 0; | |
78 | continue; | |
79 | } | |
80 | break; | |
81 | } | |
e83c5163 LT |
82 | } |
83 | ||
27de946d DW |
84 | /* Gr. strptime is crap for this; it doesn't have a way to require RFC2822 |
85 | (i.e. English) day/month names, and it doesn't work correctly with %z. */ | |
d167f147 | 86 | static void parse_date(char *date, time_t *now, char *result, int maxlen) |
27de946d | 87 | { |
27de946d | 88 | char *p; |
27de946d DW |
89 | time_t then; |
90 | ||
d167f147 | 91 | if ((then = curl_getdate(date, now)) == 0) |
27de946d DW |
92 | return; |
93 | ||
d167f147 TL |
94 | /* find the timezone at the end */ |
95 | p = date + strlen(date); | |
96 | while (p > date && isdigit(*--p)) | |
97 | ; | |
98 | if ((*p == '+' || *p == '-') && strlen(p) == 5) | |
99 | snprintf(result, maxlen, "%lu %5.5s", then, p); | |
27de946d DW |
100 | } |
101 | ||
d0d7cbe7 LT |
102 | static void check_valid(unsigned char *sha1, const char *expect) |
103 | { | |
104 | void *buf; | |
105 | char type[20]; | |
106 | unsigned long size; | |
107 | ||
108 | buf = read_sha1_file(sha1, type, &size); | |
109 | if (!buf || strcmp(type, expect)) | |
110 | die("%s is not a valid '%s' object", sha1_to_hex(sha1), expect); | |
111 | free(buf); | |
112 | } | |
113 | ||
e83c5163 | 114 | /* |
c5bac17a JH |
115 | * Having more than two parents is not strange at all, and this is |
116 | * how multi-way merges are represented. | |
e83c5163 LT |
117 | */ |
118 | #define MAXPARENT (16) | |
119 | ||
c5bac17a JH |
120 | static char *commit_tree_usage = "commit-tree <sha1> [-p <sha1>]* < changelog"; |
121 | ||
e83c5163 LT |
122 | int main(int argc, char **argv) |
123 | { | |
124 | int i, len; | |
125 | int parents = 0; | |
126 | unsigned char tree_sha1[20]; | |
127 | unsigned char parent_sha1[MAXPARENT][20]; | |
d6d3f9d0 | 128 | unsigned char commit_sha1[20]; |
b70070f0 GK |
129 | char *gecos, *realgecos, *commitgecos; |
130 | char *email, *commitemail, realemail[1000]; | |
27de946d DW |
131 | char date[20], realdate[20]; |
132 | char *audate; | |
e83c5163 LT |
133 | char comment[1000]; |
134 | struct passwd *pw; | |
135 | time_t now; | |
27de946d | 136 | struct tm *tm; |
e83c5163 LT |
137 | char *buffer; |
138 | unsigned int size; | |
139 | ||
140 | if (argc < 2 || get_sha1_hex(argv[1], tree_sha1) < 0) | |
c5bac17a | 141 | usage(commit_tree_usage); |
e83c5163 | 142 | |
d0d7cbe7 | 143 | check_valid(tree_sha1, "tree"); |
e83c5163 LT |
144 | for (i = 2; i < argc; i += 2) { |
145 | char *a, *b; | |
146 | a = argv[i]; b = argv[i+1]; | |
147 | if (!b || strcmp(a, "-p") || get_sha1_hex(b, parent_sha1[parents])) | |
c5bac17a | 148 | usage(commit_tree_usage); |
d0d7cbe7 | 149 | check_valid(parent_sha1[parents], "commit"); |
e83c5163 LT |
150 | parents++; |
151 | } | |
152 | if (!parents) | |
153 | fprintf(stderr, "Committing initial tree %s\n", argv[1]); | |
154 | pw = getpwuid(getuid()); | |
155 | if (!pw) | |
2de381f9 | 156 | die("You don't exist. Go away!"); |
e83c5163 LT |
157 | realgecos = pw->pw_gecos; |
158 | len = strlen(pw->pw_name); | |
159 | memcpy(realemail, pw->pw_name, len); | |
160 | realemail[len] = '@'; | |
161 | gethostname(realemail+len+1, sizeof(realemail)-len-1); | |
b96afa59 LT |
162 | if (!strchr(realemail+len+1, '.')) { |
163 | strcat(realemail, "."); | |
164 | getdomainname(realemail+strlen(realemail), sizeof(realemail)-strlen(realemail)-1); | |
165 | } | |
e83c5163 | 166 | time(&now); |
27de946d DW |
167 | tm = localtime(&now); |
168 | ||
169 | strftime(realdate, sizeof(realdate), "%s %z", tm); | |
170 | strcpy(date, realdate); | |
e83c5163 | 171 | |
b70070f0 GK |
172 | commitgecos = getenv("COMMIT_AUTHOR_NAME") ? : realgecos; |
173 | commitemail = getenv("COMMIT_AUTHOR_EMAIL") ? : realemail; | |
bf16c71e LT |
174 | gecos = getenv("AUTHOR_NAME") ? : realgecos; |
175 | email = getenv("AUTHOR_EMAIL") ? : realemail; | |
27de946d DW |
176 | audate = getenv("AUTHOR_DATE"); |
177 | if (audate) | |
d167f147 | 178 | parse_date(audate, &now, date, sizeof(date)); |
e83c5163 | 179 | |
b70070f0 GK |
180 | remove_special(gecos); remove_special(realgecos); remove_special(commitgecos); |
181 | remove_special(email); remove_special(realemail); remove_special(commitemail); | |
e83c5163 LT |
182 | |
183 | init_buffer(&buffer, &size); | |
184 | add_buffer(&buffer, &size, "tree %s\n", sha1_to_hex(tree_sha1)); | |
185 | ||
186 | /* | |
187 | * NOTE! This ordering means that the same exact tree merged with a | |
188 | * different order of parents will be a _different_ changeset even | |
189 | * if everything else stays the same. | |
190 | */ | |
191 | for (i = 0; i < parents; i++) | |
192 | add_buffer(&buffer, &size, "parent %s\n", sha1_to_hex(parent_sha1[i])); | |
193 | ||
194 | /* Person/date information */ | |
195 | add_buffer(&buffer, &size, "author %s <%s> %s\n", gecos, email, date); | |
b70070f0 | 196 | add_buffer(&buffer, &size, "committer %s <%s> %s\n\n", commitgecos, commitemail, realdate); |
e83c5163 LT |
197 | |
198 | /* And add the comment */ | |
199 | while (fgets(comment, sizeof(comment), stdin) != NULL) | |
200 | add_buffer(&buffer, &size, "%s", comment); | |
201 | ||
a44c9a5e | 202 | write_sha1_file(buffer, size, "commit", commit_sha1); |
d6d3f9d0 | 203 | printf("%s\n", sha1_to_hex(commit_sha1)); |
e83c5163 LT |
204 | return 0; |
205 | } |