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