commit-tree.con commit Make default merge messages denser. (5b1ea09)
   1/*
   2 * GIT - The information manager from hell
   3 *
   4 * Copyright (C) Linus Torvalds, 2005
   5 */
   6#include "cache.h"
   7
   8#include <pwd.h>
   9#include <time.h>
  10#include <ctype.h>
  11
  12#define BLOCKING (1ul << 14)
  13
  14/*
  15 * FIXME! Share the code with "write-tree.c"
  16 */
  17static void init_buffer(char **bufp, unsigned int *sizep)
  18{
  19        char *buf = xmalloc(BLOCKING);
  20        *sizep = 0;
  21        *bufp = buf;
  22}
  23
  24static 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) {
  40                alloc = (newsize + 32767) & ~32767;
  41                buf = xrealloc(buf, alloc);
  42                *bufp = buf;
  43        }
  44        *sizep = newsize;
  45        memcpy(buf + size, one_line, len);
  46}
  47
  48static void remove_special(char *p)
  49{
  50        char c;
  51        char *dst = p, *src = p;
  52
  53        for (;;) {
  54                c = *src;
  55                src++;
  56                switch(c) {
  57                case '\n': case '<': case '>':
  58                        continue;
  59                }
  60                *dst++ = c;
  61                if (!c)
  62                        break;
  63        }
  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        }
  79}
  80
  81static 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
  93/*
  94 * Having more than two parents is not strange at all, and this is
  95 * how multi-way merges are represented.
  96 */
  97#define MAXPARENT (16)
  98
  99static char *commit_tree_usage = "git-commit-tree <sha1> [-p <sha1>]* < changelog";
 100
 101int main(int argc, char **argv)
 102{
 103        int i, len;
 104        int parents = 0;
 105        unsigned char tree_sha1[20];
 106        unsigned char parent_sha1[MAXPARENT][20];
 107        unsigned char commit_sha1[20];
 108        char *gecos, *realgecos, *commitgecos;
 109        char *email, *commitemail, realemail[1000];
 110        char date[50], realdate[50];
 111        char *audate, *cmdate;
 112        char comment[1000];
 113        struct passwd *pw;
 114        char *buffer;
 115        unsigned int size;
 116
 117        if (argc < 2 || get_sha1_hex(argv[1], tree_sha1) < 0)
 118                usage(commit_tree_usage);
 119
 120        check_valid(tree_sha1, "tree");
 121        for (i = 2; i < argc; i += 2) {
 122                char *a, *b;
 123                a = argv[i]; b = argv[i+1];
 124                if (!b || strcmp(a, "-p") || get_sha1(b, parent_sha1[parents]))
 125                        usage(commit_tree_usage);
 126                check_valid(parent_sha1[parents], "commit");
 127                parents++;
 128        }
 129        if (!parents)
 130                fprintf(stderr, "Committing initial tree %s\n", argv[1]);
 131        pw = getpwuid(getuid());
 132        if (!pw)
 133                die("You don't exist. Go away!");
 134        realgecos = pw->pw_gecos;
 135        len = strlen(pw->pw_name);
 136        memcpy(realemail, pw->pw_name, len);
 137        realemail[len] = '@';
 138        gethostname(realemail+len+1, sizeof(realemail)-len-1);
 139        if (!strchr(realemail+len+1, '.')) {
 140                strcat(realemail, ".");
 141                getdomainname(realemail+strlen(realemail), sizeof(realemail)-strlen(realemail)-1);
 142        }
 143
 144        datestamp(realdate, sizeof(realdate));
 145        strcpy(date, realdate);
 146
 147        commitgecos = gitenv("GIT_COMMITTER_NAME") ? : realgecos;
 148        commitemail = gitenv("GIT_COMMITTER_EMAIL") ? : realemail;
 149        gecos = gitenv("GIT_AUTHOR_NAME") ? : realgecos;
 150        email = gitenv("GIT_AUTHOR_EMAIL") ? : realemail;
 151        audate = gitenv("GIT_AUTHOR_DATE");
 152        if (audate)
 153                parse_date(audate, date, sizeof(date));
 154        cmdate = gitenv("GIT_COMMITTER_DATE");
 155        if (cmdate)
 156                parse_date(cmdate, realdate, sizeof(realdate));
 157
 158        remove_special(gecos); remove_special(realgecos); remove_special(commitgecos);
 159        remove_special(email); remove_special(realemail); remove_special(commitemail);
 160
 161        init_buffer(&buffer, &size);
 162        add_buffer(&buffer, &size, "tree %s\n", sha1_to_hex(tree_sha1));
 163
 164        /*
 165         * NOTE! This ordering means that the same exact tree merged with a
 166         * different order of parents will be a _different_ changeset even
 167         * if everything else stays the same.
 168         */
 169        for (i = 0; i < parents; i++)
 170                add_buffer(&buffer, &size, "parent %s\n", sha1_to_hex(parent_sha1[i]));
 171
 172        /* Person/date information */
 173        add_buffer(&buffer, &size, "author %s <%s> %s\n", gecos, email, date);
 174        add_buffer(&buffer, &size, "committer %s <%s> %s\n\n", commitgecos, commitemail, realdate);
 175
 176        /* And add the comment */
 177        while (fgets(comment, sizeof(comment), stdin) != NULL)
 178                add_buffer(&buffer, &size, "%s", comment);
 179
 180        write_sha1_file(buffer, size, "commit", commit_sha1);
 181        printf("%s\n", sha1_to_hex(commit_sha1));
 182        return 0;
 183}