Linus Torvalds | 8bc9a0c | 2005-04-07 15:16:10 -0700 | [diff] [blame] | 1 | /* |
| 2 | * GIT - The information manager from hell |
| 3 | * |
| 4 | * Copyright (C) Linus Torvalds, 2005 |
| 5 | */ |
Linus Torvalds | e83c516 | 2005-04-07 15:13:13 -0700 | [diff] [blame] | 6 | #include "cache.h" |
| 7 | |
| 8 | #include <pwd.h> |
| 9 | #include <time.h> |
David Woodhouse | 27de946 | 2005-04-15 08:39:57 -0700 | [diff] [blame] | 10 | #include <ctype.h> |
Linus Torvalds | e83c516 | 2005-04-07 15:13:13 -0700 | [diff] [blame] | 11 | |
| 12 | #define BLOCKING (1ul << 14) |
Linus Torvalds | e83c516 | 2005-04-07 15:13:13 -0700 | [diff] [blame] | 13 | |
| 14 | /* |
Linus Torvalds | e83c516 | 2005-04-07 15:13:13 -0700 | [diff] [blame] | 15 | * FIXME! Share the code with "write-tree.c" |
| 16 | */ |
| 17 | static void init_buffer(char **bufp, unsigned int *sizep) |
| 18 | { |
Christopher Li | 812666c | 2005-04-26 12:00:58 -0700 | [diff] [blame] | 19 | char *buf = xmalloc(BLOCKING); |
Linus Torvalds | a44c9a5 | 2005-04-25 10:19:53 -0700 | [diff] [blame] | 20 | *sizep = 0; |
Linus Torvalds | e83c516 | 2005-04-07 15:13:13 -0700 | [diff] [blame] | 21 | *bufp = buf; |
| 22 | } |
| 23 | |
| 24 | static void add_buffer(char **bufp, unsigned int *sizep, const char *fmt, ...) |
| 25 | { |
| 26 | char one_line[2048]; |
| 27 | va_list args; |
| 28 | int len; |
| 29 | unsigned long alloc, size, newsize; |
| 30 | char *buf; |
| 31 | |
| 32 | va_start(args, fmt); |
| 33 | len = vsnprintf(one_line, sizeof(one_line), fmt, args); |
| 34 | va_end(args); |
| 35 | size = *sizep; |
| 36 | newsize = size + len; |
| 37 | alloc = (size + 32767) & ~32767; |
| 38 | buf = *bufp; |
| 39 | if (newsize > alloc) { |
Ingo Molnar | aebb267 | 2005-04-12 11:36:26 -0700 | [diff] [blame] | 40 | alloc = (newsize + 32767) & ~32767; |
Christopher Li | 812666c | 2005-04-26 12:00:58 -0700 | [diff] [blame] | 41 | buf = xrealloc(buf, alloc); |
Linus Torvalds | e83c516 | 2005-04-07 15:13:13 -0700 | [diff] [blame] | 42 | *bufp = buf; |
| 43 | } |
| 44 | *sizep = newsize; |
| 45 | memcpy(buf + size, one_line, len); |
| 46 | } |
| 47 | |
Linus Torvalds | e83c516 | 2005-04-07 15:13:13 -0700 | [diff] [blame] | 48 | static void remove_special(char *p) |
| 49 | { |
| 50 | char c; |
Brad Roberts | 74b2428 | 2005-04-17 19:12:14 -0700 | [diff] [blame] | 51 | char *dst = p, *src = p; |
Linus Torvalds | e83c516 | 2005-04-07 15:13:13 -0700 | [diff] [blame] | 52 | |
| 53 | for (;;) { |
Brad Roberts | 74b2428 | 2005-04-17 19:12:14 -0700 | [diff] [blame] | 54 | c = *src; |
| 55 | src++; |
Linus Torvalds | e83c516 | 2005-04-07 15:13:13 -0700 | [diff] [blame] | 56 | switch(c) { |
| 57 | case '\n': case '<': case '>': |
| 58 | continue; |
| 59 | } |
| 60 | *dst++ = c; |
| 61 | if (!c) |
| 62 | break; |
| 63 | } |
Linus Torvalds | 5e5128e | 2005-04-17 16:52:54 -0700 | [diff] [blame] | 64 | |
| 65 | /* |
| 66 | * Go back, and remove crud from the end: some people |
| 67 | * have commas etc in their gecos field |
| 68 | */ |
| 69 | dst--; |
| 70 | while (--dst >= p) { |
| 71 | unsigned char c = *dst; |
| 72 | switch (c) { |
| 73 | case ',': case ';': case '.': |
| 74 | *dst = 0; |
| 75 | continue; |
| 76 | } |
| 77 | break; |
| 78 | } |
Linus Torvalds | e83c516 | 2005-04-07 15:13:13 -0700 | [diff] [blame] | 79 | } |
| 80 | |
Linus Torvalds | d0d7cbe | 2005-04-17 15:26:13 -0700 | [diff] [blame] | 81 | static void check_valid(unsigned char *sha1, const char *expect) |
| 82 | { |
| 83 | void *buf; |
| 84 | char type[20]; |
| 85 | unsigned long size; |
| 86 | |
| 87 | buf = read_sha1_file(sha1, type, &size); |
| 88 | if (!buf || strcmp(type, expect)) |
| 89 | die("%s is not a valid '%s' object", sha1_to_hex(sha1), expect); |
| 90 | free(buf); |
| 91 | } |
| 92 | |
Linus Torvalds | e83c516 | 2005-04-07 15:13:13 -0700 | [diff] [blame] | 93 | /* |
Junio C Hamano | c5bac17 | 2005-04-20 19:49:16 -0700 | [diff] [blame] | 94 | * Having more than two parents is not strange at all, and this is |
| 95 | * how multi-way merges are represented. |
Linus Torvalds | e83c516 | 2005-04-07 15:13:13 -0700 | [diff] [blame] | 96 | */ |
| 97 | #define MAXPARENT (16) |
Linus Torvalds | b389237 | 2005-06-19 10:40:10 -0700 | [diff] [blame] | 98 | static unsigned char parent_sha1[MAXPARENT][20]; |
Linus Torvalds | e83c516 | 2005-04-07 15:13:13 -0700 | [diff] [blame] | 99 | |
Alexey Nezhdanov | 667bb59 | 2005-05-19 15:17:16 +0400 | [diff] [blame] | 100 | static char *commit_tree_usage = "git-commit-tree <sha1> [-p <sha1>]* < changelog"; |
Junio C Hamano | c5bac17 | 2005-04-20 19:49:16 -0700 | [diff] [blame] | 101 | |
Linus Torvalds | b389237 | 2005-06-19 10:40:10 -0700 | [diff] [blame] | 102 | static int new_parent(int idx) |
| 103 | { |
| 104 | int i; |
| 105 | unsigned char *sha1 = parent_sha1[idx]; |
| 106 | for (i = 0; i < idx; i++) { |
| 107 | if (!memcmp(parent_sha1[i], sha1, 20)) { |
| 108 | error("duplicate parent %s ignored", sha1_to_hex(sha1)); |
| 109 | return 0; |
| 110 | } |
| 111 | } |
| 112 | return 1; |
| 113 | } |
| 114 | |
Linus Torvalds | e83c516 | 2005-04-07 15:13:13 -0700 | [diff] [blame] | 115 | int main(int argc, char **argv) |
| 116 | { |
| 117 | int i, len; |
| 118 | int parents = 0; |
| 119 | unsigned char tree_sha1[20]; |
Linus Torvalds | d6d3f9d | 2005-04-09 17:09:34 -0700 | [diff] [blame] | 120 | unsigned char commit_sha1[20]; |
Greg KH | b70070f | 2005-04-18 17:37:32 -0700 | [diff] [blame] | 121 | char *gecos, *realgecos, *commitgecos; |
| 122 | char *email, *commitemail, realemail[1000]; |
Linus Torvalds | c4bd907 | 2005-05-24 10:21:13 -0700 | [diff] [blame] | 123 | char date[50], realdate[50]; |
| 124 | char *audate, *cmdate; |
Linus Torvalds | e83c516 | 2005-04-07 15:13:13 -0700 | [diff] [blame] | 125 | char comment[1000]; |
| 126 | struct passwd *pw; |
Linus Torvalds | e83c516 | 2005-04-07 15:13:13 -0700 | [diff] [blame] | 127 | char *buffer; |
| 128 | unsigned int size; |
| 129 | |
| 130 | if (argc < 2 || get_sha1_hex(argv[1], tree_sha1) < 0) |
Junio C Hamano | c5bac17 | 2005-04-20 19:49:16 -0700 | [diff] [blame] | 131 | usage(commit_tree_usage); |
Linus Torvalds | e83c516 | 2005-04-07 15:13:13 -0700 | [diff] [blame] | 132 | |
Linus Torvalds | d0d7cbe | 2005-04-17 15:26:13 -0700 | [diff] [blame] | 133 | check_valid(tree_sha1, "tree"); |
Linus Torvalds | e83c516 | 2005-04-07 15:13:13 -0700 | [diff] [blame] | 134 | for (i = 2; i < argc; i += 2) { |
| 135 | char *a, *b; |
| 136 | a = argv[i]; b = argv[i+1]; |
Linus Torvalds | 3c249c9 | 2005-05-01 16:36:56 -0700 | [diff] [blame] | 137 | if (!b || strcmp(a, "-p") || get_sha1(b, parent_sha1[parents])) |
Junio C Hamano | c5bac17 | 2005-04-20 19:49:16 -0700 | [diff] [blame] | 138 | usage(commit_tree_usage); |
Linus Torvalds | d0d7cbe | 2005-04-17 15:26:13 -0700 | [diff] [blame] | 139 | check_valid(parent_sha1[parents], "commit"); |
Linus Torvalds | b389237 | 2005-06-19 10:40:10 -0700 | [diff] [blame] | 140 | if (new_parent(parents)) |
| 141 | parents++; |
Linus Torvalds | e83c516 | 2005-04-07 15:13:13 -0700 | [diff] [blame] | 142 | } |
| 143 | if (!parents) |
| 144 | fprintf(stderr, "Committing initial tree %s\n", argv[1]); |
| 145 | pw = getpwuid(getuid()); |
| 146 | if (!pw) |
Petr Baudis | 2de381f | 2005-04-13 02:28:48 -0700 | [diff] [blame] | 147 | die("You don't exist. Go away!"); |
Linus Torvalds | e83c516 | 2005-04-07 15:13:13 -0700 | [diff] [blame] | 148 | realgecos = pw->pw_gecos; |
| 149 | len = strlen(pw->pw_name); |
| 150 | memcpy(realemail, pw->pw_name, len); |
| 151 | realemail[len] = '@'; |
| 152 | gethostname(realemail+len+1, sizeof(realemail)-len-1); |
Linus Torvalds | b96afa5 | 2005-04-19 15:46:06 -0700 | [diff] [blame] | 153 | if (!strchr(realemail+len+1, '.')) { |
| 154 | strcat(realemail, "."); |
| 155 | getdomainname(realemail+strlen(realemail), sizeof(realemail)-strlen(realemail)-1); |
| 156 | } |
David Woodhouse | 27de946 | 2005-04-15 08:39:57 -0700 | [diff] [blame] | 157 | |
Edgar Toernig | ecee9d9 | 2005-04-30 09:46:49 -0700 | [diff] [blame] | 158 | datestamp(realdate, sizeof(realdate)); |
David Woodhouse | 27de946 | 2005-04-15 08:39:57 -0700 | [diff] [blame] | 159 | strcpy(date, realdate); |
Linus Torvalds | e83c516 | 2005-04-07 15:13:13 -0700 | [diff] [blame] | 160 | |
Junio C Hamano | d19938a | 2005-05-09 17:57:56 -0700 | [diff] [blame] | 161 | commitgecos = gitenv("GIT_COMMITTER_NAME") ? : realgecos; |
| 162 | commitemail = gitenv("GIT_COMMITTER_EMAIL") ? : realemail; |
| 163 | gecos = gitenv("GIT_AUTHOR_NAME") ? : realgecos; |
| 164 | email = gitenv("GIT_AUTHOR_EMAIL") ? : realemail; |
| 165 | audate = gitenv("GIT_AUTHOR_DATE"); |
David Woodhouse | 27de946 | 2005-04-15 08:39:57 -0700 | [diff] [blame] | 166 | if (audate) |
Edgar Toernig | ecee9d9 | 2005-04-30 09:46:49 -0700 | [diff] [blame] | 167 | parse_date(audate, date, sizeof(date)); |
Linus Torvalds | c4bd907 | 2005-05-24 10:21:13 -0700 | [diff] [blame] | 168 | cmdate = gitenv("GIT_COMMITTER_DATE"); |
| 169 | if (cmdate) |
Rene Scharfe | c504aae | 2005-05-27 01:03:26 +0200 | [diff] [blame] | 170 | parse_date(cmdate, realdate, sizeof(realdate)); |
Linus Torvalds | e83c516 | 2005-04-07 15:13:13 -0700 | [diff] [blame] | 171 | |
Greg KH | b70070f | 2005-04-18 17:37:32 -0700 | [diff] [blame] | 172 | remove_special(gecos); remove_special(realgecos); remove_special(commitgecos); |
| 173 | remove_special(email); remove_special(realemail); remove_special(commitemail); |
Linus Torvalds | e83c516 | 2005-04-07 15:13:13 -0700 | [diff] [blame] | 174 | |
| 175 | init_buffer(&buffer, &size); |
| 176 | add_buffer(&buffer, &size, "tree %s\n", sha1_to_hex(tree_sha1)); |
| 177 | |
| 178 | /* |
| 179 | * NOTE! This ordering means that the same exact tree merged with a |
| 180 | * different order of parents will be a _different_ changeset even |
| 181 | * if everything else stays the same. |
| 182 | */ |
| 183 | for (i = 0; i < parents; i++) |
| 184 | add_buffer(&buffer, &size, "parent %s\n", sha1_to_hex(parent_sha1[i])); |
| 185 | |
| 186 | /* Person/date information */ |
| 187 | add_buffer(&buffer, &size, "author %s <%s> %s\n", gecos, email, date); |
Greg KH | b70070f | 2005-04-18 17:37:32 -0700 | [diff] [blame] | 188 | add_buffer(&buffer, &size, "committer %s <%s> %s\n\n", commitgecos, commitemail, realdate); |
Linus Torvalds | e83c516 | 2005-04-07 15:13:13 -0700 | [diff] [blame] | 189 | |
| 190 | /* And add the comment */ |
| 191 | while (fgets(comment, sizeof(comment), stdin) != NULL) |
| 192 | add_buffer(&buffer, &size, "%s", comment); |
| 193 | |
Linus Torvalds | a44c9a5 | 2005-04-25 10:19:53 -0700 | [diff] [blame] | 194 | write_sha1_file(buffer, size, "commit", commit_sha1); |
Linus Torvalds | d6d3f9d | 2005-04-09 17:09:34 -0700 | [diff] [blame] | 195 | printf("%s\n", sha1_to_hex(commit_sha1)); |
Linus Torvalds | e83c516 | 2005-04-07 15:13:13 -0700 | [diff] [blame] | 196 | return 0; |
| 197 | } |