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