1f1074cec66d367d217fffc611dc45981ea67013
   1/*
   2 * Builtin "git log" and related commands (show, whatchanged)
   3 *
   4 * (C) Copyright 2006 Linus Torvalds
   5 *               2006 Junio Hamano
   6 */
   7#include "cache.h"
   8#include "commit.h"
   9#include "diff.h"
  10#include "revision.h"
  11#include "log-tree.h"
  12#include "builtin.h"
  13
  14/* this is in builtin-diff.c */
  15void add_head(struct rev_info *revs);
  16
  17static void cmd_log_init(int argc, const char **argv, char **envp,
  18                      struct rev_info *rev)
  19{
  20        rev->abbrev = DEFAULT_ABBREV;
  21        rev->commit_format = CMIT_FMT_DEFAULT;
  22        rev->verbose_header = 1;
  23        argc = setup_revisions(argc, argv, rev, "HEAD");
  24        if (rev->diffopt.pickaxe || rev->diffopt.filter)
  25                rev->always_show_header = 0;
  26        if (argc > 1)
  27                die("unrecognized argument: %s", argv[1]);
  28}
  29
  30static int cmd_log_walk(struct rev_info *rev)
  31{
  32        struct commit *commit;
  33
  34        prepare_revision_walk(rev);
  35        setup_pager();
  36        while ((commit = get_revision(rev)) != NULL) {
  37                log_tree_commit(rev, commit);
  38                free(commit->buffer);
  39                commit->buffer = NULL;
  40                free_commit_list(commit->parents);
  41                commit->parents = NULL;
  42        }
  43        return 0;
  44}
  45
  46int cmd_whatchanged(int argc, const char **argv, char **envp)
  47{
  48        struct rev_info rev;
  49
  50        git_config(git_diff_ui_config);
  51        init_revisions(&rev);
  52        rev.diff = 1;
  53        rev.diffopt.recursive = 1;
  54        rev.simplify_history = 0;
  55        cmd_log_init(argc, argv, envp, &rev);
  56        if (!rev.diffopt.output_format)
  57                rev.diffopt.output_format = DIFF_FORMAT_RAW;
  58        return cmd_log_walk(&rev);
  59}
  60
  61int cmd_show(int argc, const char **argv, char **envp)
  62{
  63        struct rev_info rev;
  64
  65        git_config(git_diff_ui_config);
  66        init_revisions(&rev);
  67        rev.diff = 1;
  68        rev.diffopt.recursive = 1;
  69        rev.combine_merges = 1;
  70        rev.dense_combined_merges = 1;
  71        rev.always_show_header = 1;
  72        rev.ignore_merges = 0;
  73        rev.no_walk = 1;
  74        cmd_log_init(argc, argv, envp, &rev);
  75        return cmd_log_walk(&rev);
  76}
  77
  78int cmd_log(int argc, const char **argv, char **envp)
  79{
  80        struct rev_info rev;
  81
  82        git_config(git_diff_ui_config);
  83        init_revisions(&rev);
  84        rev.always_show_header = 1;
  85        cmd_log_init(argc, argv, envp, &rev);
  86        return cmd_log_walk(&rev);
  87}
  88
  89static int istitlechar(char c)
  90{
  91        return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') ||
  92                (c >= '0' && c <= '9') || c == '.' || c == '_';
  93}
  94
  95static char *extra_headers = NULL;
  96static int extra_headers_size = 0;
  97
  98static int git_format_config(const char *var, const char *value)
  99{
 100        if (!strcmp(var, "format.headers")) {
 101                int len = strlen(value);
 102                extra_headers_size += len + 1;
 103                extra_headers = realloc(extra_headers, extra_headers_size);
 104                extra_headers[extra_headers_size - len - 1] = 0;
 105                strcat(extra_headers, value);
 106                return 0;
 107        }
 108        if (!strcmp(var, "diff.color")) {
 109                return 0;
 110        }
 111        return git_diff_ui_config(var, value);
 112}
 113
 114
 115static FILE *realstdout = NULL;
 116static const char *output_directory = NULL;
 117
 118static void reopen_stdout(struct commit *commit, int nr, int keep_subject)
 119{
 120        char filename[1024];
 121        char *sol;
 122        int len = 0;
 123
 124        if (output_directory) {
 125                strlcpy(filename, output_directory, 1010);
 126                len = strlen(filename);
 127                if (filename[len - 1] != '/')
 128                        filename[len++] = '/';
 129        }
 130
 131        sprintf(filename + len, "%04d", nr);
 132        len = strlen(filename);
 133
 134        sol = strstr(commit->buffer, "\n\n");
 135        if (sol) {
 136                int j, space = 1;
 137
 138                sol += 2;
 139                /* strip [PATCH] or [PATCH blabla] */
 140                if (!keep_subject && !strncmp(sol, "[PATCH", 6)) {
 141                        char *eos = strchr(sol + 6, ']');
 142                        if (eos) {
 143                                while (isspace(*eos))
 144                                        eos++;
 145                                sol = eos;
 146                        }
 147                }
 148
 149                for (j = 0; len < 1024 - 6 && sol[j] && sol[j] != '\n'; j++) {
 150                        if (istitlechar(sol[j])) {
 151                                if (space) {
 152                                        filename[len++] = '-';
 153                                        space = 0;
 154                                }
 155                                filename[len++] = sol[j];
 156                                if (sol[j] == '.')
 157                                        while (sol[j + 1] == '.')
 158                                                j++;
 159                        } else
 160                                space = 1;
 161                }
 162                while (filename[len - 1] == '.' || filename[len - 1] == '-')
 163                        len--;
 164        }
 165        strcpy(filename + len, ".txt");
 166        fprintf(realstdout, "%s\n", filename);
 167        freopen(filename, "w", stdout);
 168}
 169
 170static int get_patch_id(struct commit *commit, struct diff_options *options,
 171                unsigned char *sha1)
 172{
 173        diff_tree_sha1(commit->parents->item->object.sha1, commit->object.sha1,
 174                        "", options);
 175        diffcore_std(options);
 176        return diff_flush_patch_id(options, sha1);
 177}
 178
 179static void get_patch_ids(struct rev_info *rev, struct diff_options *options)
 180{
 181        struct rev_info check_rev;
 182        struct commit *commit;
 183        struct object *o1, *o2;
 184        unsigned flags1, flags2;
 185        unsigned char sha1[20];
 186
 187        if (rev->pending.nr != 2)
 188                die("Need exactly one range.");
 189
 190        o1 = rev->pending.objects[0].item;
 191        flags1 = o1->flags;
 192        o2 = rev->pending.objects[1].item;
 193        flags2 = o2->flags;
 194
 195        if ((flags1 & UNINTERESTING) == (flags2 & UNINTERESTING))
 196                die("Not a range.");
 197
 198        diff_setup(options);
 199        options->recursive = 1;
 200        if (diff_setup_done(options) < 0)
 201                die("diff_setup_done failed");
 202
 203        /* given a range a..b get all patch ids for b..a */
 204        init_revisions(&check_rev);
 205        o1->flags ^= UNINTERESTING;
 206        o2->flags ^= UNINTERESTING;
 207        add_pending_object(&check_rev, o1, "o1");
 208        add_pending_object(&check_rev, o2, "o2");
 209        prepare_revision_walk(&check_rev);
 210
 211        while ((commit = get_revision(&check_rev)) != NULL) {
 212                /* ignore merges */
 213                if (commit->parents && commit->parents->next)
 214                        continue;
 215
 216                if (!get_patch_id(commit, options, sha1))
 217                        created_object(sha1, xcalloc(1, sizeof(struct object)));
 218        }
 219
 220        /* reset for next revision walk */
 221        clear_commit_marks((struct commit *)o1,
 222                        SEEN | UNINTERESTING | SHOWN | ADDED);
 223        clear_commit_marks((struct commit *)o2,
 224                        SEEN | UNINTERESTING | SHOWN | ADDED);
 225        o1->flags = flags1;
 226        o2->flags = flags2;
 227}
 228
 229static void gen_message_id(char *dest, unsigned int length, char *base)
 230{
 231        const char *committer = git_committer_info(1);
 232        const char *email_start = strrchr(committer, '<');
 233        const char *email_end = strrchr(committer, '>');
 234        if(!email_start || !email_end || email_start > email_end - 1)
 235                die("Could not extract email from committer identity.");
 236        snprintf(dest, length, "%s.%u.git.%.*s", base, time(NULL),
 237                 email_end - email_start - 1, email_start + 1);
 238}
 239
 240int cmd_format_patch(int argc, const char **argv, char **envp)
 241{
 242        struct commit *commit;
 243        struct commit **list = NULL;
 244        struct rev_info rev;
 245        int nr = 0, total, i, j;
 246        int use_stdout = 0;
 247        int numbered = 0;
 248        int start_number = -1;
 249        int keep_subject = 0;
 250        int ignore_if_in_upstream = 0;
 251        struct diff_options patch_id_opts;
 252        char *add_signoff = NULL;
 253        char message_id[1024];
 254        char ref_message_id[1024];
 255
 256        git_config(git_format_config);
 257        init_revisions(&rev);
 258        rev.commit_format = CMIT_FMT_EMAIL;
 259        rev.verbose_header = 1;
 260        rev.diff = 1;
 261        rev.combine_merges = 0;
 262        rev.ignore_merges = 1;
 263        rev.diffopt.msg_sep = "";
 264        rev.diffopt.recursive = 1;
 265
 266        rev.extra_headers = extra_headers;
 267
 268        /*
 269         * Parse the arguments before setup_revisions(), or something
 270         * like "git fmt-patch -o a123 HEAD^.." may fail; a123 is
 271         * possibly a valid SHA1.
 272         */
 273        for (i = 1, j = 1; i < argc; i++) {
 274                if (!strcmp(argv[i], "--stdout"))
 275                        use_stdout = 1;
 276                else if (!strcmp(argv[i], "-n") ||
 277                                !strcmp(argv[i], "--numbered"))
 278                        numbered = 1;
 279                else if (!strncmp(argv[i], "--start-number=", 15))
 280                        start_number = strtol(argv[i] + 15, NULL, 10);
 281                else if (!strcmp(argv[i], "--start-number")) {
 282                        i++;
 283                        if (i == argc)
 284                                die("Need a number for --start-number");
 285                        start_number = strtol(argv[i], NULL, 10);
 286                }
 287                else if (!strcmp(argv[i], "-k") ||
 288                                !strcmp(argv[i], "--keep-subject")) {
 289                        keep_subject = 1;
 290                        rev.total = -1;
 291                }
 292                else if (!strcmp(argv[i], "--output-directory") ||
 293                         !strcmp(argv[i], "-o")) {
 294                        i++;
 295                        if (argc <= i)
 296                                die("Which directory?");
 297                        if (output_directory)
 298                                die("Two output directories?");
 299                        output_directory = argv[i];
 300                }
 301                else if (!strcmp(argv[i], "--signoff") ||
 302                         !strcmp(argv[i], "-s")) {
 303                        const char *committer;
 304                        const char *endpos;
 305                        setup_ident();
 306                        committer = git_committer_info(1);
 307                        endpos = strchr(committer, '>');
 308                        if (!endpos)
 309                                die("bogos committer info %s\n", committer);
 310                        add_signoff = xmalloc(endpos - committer + 2);
 311                        memcpy(add_signoff, committer, endpos - committer + 1);
 312                        add_signoff[endpos - committer + 1] = 0;
 313                }
 314                else if (!strcmp(argv[i], "--attach"))
 315                        rev.mime_boundary = git_version_string;
 316                else if (!strncmp(argv[i], "--attach=", 9))
 317                        rev.mime_boundary = argv[i] + 9;
 318                else if (!strcmp(argv[i], "--ignore-if-in-upstream"))
 319                        ignore_if_in_upstream = 1;
 320                else
 321                        argv[j++] = argv[i];
 322        }
 323        argc = j;
 324
 325        if (start_number < 0)
 326                start_number = 1;
 327        if (numbered && keep_subject)
 328                die ("-n and -k are mutually exclusive.");
 329
 330        argc = setup_revisions(argc, argv, &rev, "HEAD");
 331        if (argc > 1)
 332                die ("unrecognized argument: %s", argv[1]);
 333
 334        if (!rev.diffopt.output_format)
 335                rev.diffopt.output_format = DIFF_FORMAT_DIFFSTAT | DIFF_FORMAT_PATCH;
 336
 337        if (output_directory) {
 338                if (use_stdout)
 339                        die("standard output, or directory, which one?");
 340                if (mkdir(output_directory, 0777) < 0 && errno != EEXIST)
 341                        die("Could not create directory %s",
 342                            output_directory);
 343        }
 344
 345        if (rev.pending.nr == 1) {
 346                rev.pending.objects[0].item->flags |= UNINTERESTING;
 347                add_head(&rev);
 348        }
 349
 350        if (ignore_if_in_upstream)
 351                get_patch_ids(&rev, &patch_id_opts);
 352
 353        if (!use_stdout)
 354                realstdout = fdopen(dup(1), "w");
 355
 356        prepare_revision_walk(&rev);
 357        while ((commit = get_revision(&rev)) != NULL) {
 358                unsigned char sha1[20];
 359
 360                /* ignore merges */
 361                if (commit->parents && commit->parents->next)
 362                        continue;
 363
 364                if (ignore_if_in_upstream &&
 365                                !get_patch_id(commit, &patch_id_opts, sha1) &&
 366                                lookup_object(sha1))
 367                        continue;
 368
 369                nr++;
 370                list = realloc(list, nr * sizeof(list[0]));
 371                list[nr - 1] = commit;
 372        }
 373        total = nr;
 374        if (numbered)
 375                rev.total = total + start_number - 1;
 376        rev.add_signoff = add_signoff;
 377        while (0 <= --nr) {
 378                int shown;
 379                commit = list[nr];
 380                rev.nr = total - nr + (start_number - 1);
 381                /* Make the second and subsequent mails replies to the first */
 382                if (nr == (total - 2)) {
 383                        strncpy(ref_message_id, message_id,
 384                                sizeof(ref_message_id));
 385                        ref_message_id[sizeof(ref_message_id)-1] = '\0';
 386                        rev.ref_message_id = ref_message_id;
 387                }
 388                gen_message_id(message_id, sizeof(message_id),
 389                               sha1_to_hex(commit->object.sha1));
 390                rev.message_id = message_id;
 391                if (!use_stdout)
 392                        reopen_stdout(commit, rev.nr, keep_subject);
 393                shown = log_tree_commit(&rev, commit);
 394                free(commit->buffer);
 395                commit->buffer = NULL;
 396
 397                /* We put one extra blank line between formatted
 398                 * patches and this flag is used by log-tree code
 399                 * to see if it needs to emit a LF before showing
 400                 * the log; when using one file per patch, we do
 401                 * not want the extra blank line.
 402                 */
 403                if (!use_stdout)
 404                        rev.shown_one = 0;
 405                if (shown) {
 406                        if (rev.mime_boundary)
 407                                printf("\n--%s%s--\n\n\n",
 408                                       mime_boundary_leader,
 409                                       rev.mime_boundary);
 410                        else
 411                                printf("-- \n%s\n\n", git_version_string);
 412                }
 413                if (!use_stdout)
 414                        fclose(stdout);
 415        }
 416        free(list);
 417        return 0;
 418}
 419