Documentation: support manual section (5) - file formats.
[gitweb.git] / builtin-log.c
index f3cff13edc80afad27b9f7930a0a736ba5aa6d16..469949457f61eee95f6ba801c4f81328a06cffda 100644 (file)
@@ -11,6 +11,8 @@
 #include "log-tree.h"
 #include "builtin.h"
 #include "tag.h"
+#include "reflog-walk.h"
+#include "patch-ids.h"
 
 static int default_show_root = 1;
 
@@ -31,10 +33,10 @@ static void cmd_log_init(int argc, const char **argv, const char *prefix,
                rev->always_show_header = 0;
        for (i = 1; i < argc; i++) {
                const char *arg = argv[i];
-               if (!strncmp(arg, "--encoding=", 11)) {
+               if (!prefixcmp(arg, "--encoding=")) {
                        arg += 11;
                        if (strcmp(arg, "none"))
-                               git_log_output_encoding = strdup(arg);
+                               git_log_output_encoding = xstrdup(arg);
                        else
                                git_log_output_encoding = "";
                }
@@ -50,8 +52,11 @@ static int cmd_log_walk(struct rev_info *rev)
        prepare_revision_walk(rev);
        while ((commit = get_revision(rev)) != NULL) {
                log_tree_commit(rev, commit);
-               free(commit->buffer);
-               commit->buffer = NULL;
+               if (!rev->reflog_info) {
+                       /* we allow cycles in reflog ancestry */
+                       free(commit->buffer);
+                       commit->buffer = NULL;
+               }
                free_commit_list(commit->parents);
                commit->parents = NULL;
        }
@@ -85,8 +90,8 @@ int cmd_whatchanged(int argc, const char **argv, const char *prefix)
 static int show_object(const unsigned char *sha1, int suppress_header)
 {
        unsigned long size;
-       char type[20];
-       char *buf = read_sha1_file(sha1, type, &size);
+       enum object_type type;
+       char *buf = read_sha1_file(sha1, &type, &size);
        int offset = 0;
 
        if (!buf)
@@ -178,6 +183,37 @@ int cmd_show(int argc, const char **argv, const char *prefix)
        return ret;
 }
 
+/*
+ * This is equivalent to "git log -g --abbrev-commit --pretty=oneline"
+ */
+int cmd_log_reflog(int argc, const char **argv, const char *prefix)
+{
+       struct rev_info rev;
+
+       git_config(git_log_config);
+       init_revisions(&rev, prefix);
+       init_reflog_walk(&rev.reflog_info);
+       rev.abbrev_commit = 1;
+       rev.verbose_header = 1;
+       cmd_log_init(argc, argv, prefix, &rev);
+
+       /*
+        * This means that we override whatever commit format the user gave
+        * on the cmd line.  Sad, but cmd_log_init() currently doesn't
+        * allow us to set a different default.
+        */
+       rev.commit_format = CMIT_FMT_ONELINE;
+       rev.always_show_header = 1;
+
+       /*
+        * We get called through "git reflog", so unlike the other log
+        * routines, we need to set up our pager manually..
+        */
+       setup_pager();
+
+       return cmd_log_walk(&rev);
+}
+
 int cmd_log(int argc, const char **argv, const char *prefix)
 {
        struct rev_info rev;
@@ -189,6 +225,9 @@ int cmd_log(int argc, const char **argv, const char *prefix)
        return cmd_log_walk(&rev);
 }
 
+/* format-patch */
+#define FORMAT_PATCH_NAME_MAX 64
+
 static int istitlechar(char c)
 {
        return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') ||
@@ -229,15 +268,18 @@ static int git_format_config(const char *var, const char *value)
 static FILE *realstdout = NULL;
 static const char *output_directory = NULL;
 
-static void reopen_stdout(struct commit *commit, int nr, int keep_subject)
+static int reopen_stdout(struct commit *commit, int nr, int keep_subject)
 {
-       char filename[1024];
+       char filename[PATH_MAX];
        char *sol;
        int len = 0;
-       int suffix_len = strlen(fmt_patch_suffix) + 10; /* ., NUL and slop */
+       int suffix_len = strlen(fmt_patch_suffix) + 1;
 
        if (output_directory) {
-               strlcpy(filename, output_directory, 1000);
+               if (strlen(output_directory) >=
+                   sizeof(filename) - FORMAT_PATCH_NAME_MAX - suffix_len)
+                       return error("name of output directory is too long");
+               strlcpy(filename, output_directory, sizeof(filename) - suffix_len);
                len = strlen(filename);
                if (filename[len - 1] != '/')
                        filename[len++] = '/';
@@ -252,7 +294,7 @@ static void reopen_stdout(struct commit *commit, int nr, int keep_subject)
 
                sol += 2;
                /* strip [PATCH] or [PATCH blabla] */
-               if (!keep_subject && !strncmp(sol, "[PATCH", 6)) {
+               if (!keep_subject && !prefixcmp(sol, "[PATCH")) {
                        char *eos = strchr(sol + 6, ']');
                        if (eos) {
                                while (isspace(*eos))
@@ -262,7 +304,8 @@ static void reopen_stdout(struct commit *commit, int nr, int keep_subject)
                }
 
                for (j = 0;
-                    len < sizeof(filename) - suffix_len &&
+                    j < FORMAT_PATCH_NAME_MAX - suffix_len - 5 &&
+                            len < sizeof(filename) - suffix_len &&
                             sol[j] && sol[j] != '\n';
                     j++) {
                        if (istitlechar(sol[j])) {
@@ -279,31 +322,24 @@ static void reopen_stdout(struct commit *commit, int nr, int keep_subject)
                }
                while (filename[len - 1] == '.' || filename[len - 1] == '-')
                        len--;
+               filename[len] = 0;
        }
+       if (len + suffix_len >= sizeof(filename))
+               return error("Patch pathname too long");
        strcpy(filename + len, fmt_patch_suffix);
        fprintf(realstdout, "%s\n", filename);
-       freopen(filename, "w", stdout);
-}
+       if (freopen(filename, "w", stdout) == NULL)
+               return error("Cannot open patch file %s",filename);
+       return 0;
 
-static int get_patch_id(struct commit *commit, struct diff_options *options,
-               unsigned char *sha1)
-{
-       if (commit->parents)
-               diff_tree_sha1(commit->parents->item->object.sha1,
-                              commit->object.sha1, "", options);
-       else
-               diff_root_tree_sha1(commit->object.sha1, "", options);
-       diffcore_std(options);
-       return diff_flush_patch_id(options, sha1);
 }
 
-static void get_patch_ids(struct rev_info *rev, struct diff_options *options, const char *prefix)
+static void get_patch_ids(struct rev_info *rev, struct patch_ids *ids, const char *prefix)
 {
        struct rev_info check_rev;
        struct commit *commit;
        struct object *o1, *o2;
        unsigned flags1, flags2;
-       unsigned char sha1[20];
 
        if (rev->pending.nr != 2)
                die("Need exactly one range.");
@@ -316,10 +352,7 @@ static void get_patch_ids(struct rev_info *rev, struct diff_options *options, co
        if ((flags1 & UNINTERESTING) == (flags2 & UNINTERESTING))
                die("Not a range.");
 
-       diff_setup(options);
-       options->recursive = 1;
-       if (diff_setup_done(options) < 0)
-               die("diff_setup_done failed");
+       init_patch_ids(ids);
 
        /* given a range a..b get all patch ids for b..a */
        init_revisions(&check_rev, prefix);
@@ -334,8 +367,7 @@ static void get_patch_ids(struct rev_info *rev, struct diff_options *options, co
                if (commit->parents && commit->parents->next)
                        continue;
 
-               if (!get_patch_id(commit, options, sha1))
-                       created_object(sha1, xcalloc(1, sizeof(struct object)));
+               add_commit_patch_id(commit, ids);
        }
 
        /* reset for next revision walk */
@@ -349,7 +381,7 @@ static void get_patch_ids(struct rev_info *rev, struct diff_options *options, co
 
 static void gen_message_id(char *dest, unsigned int length, char *base)
 {
-       const char *committer = git_committer_info(1);
+       const char *committer = git_committer_info(-1);
        const char *email_start = strrchr(committer, '<');
        const char *email_end = strrchr(committer, '>');
        if(!email_start || !email_end || email_start > email_end - 1)
@@ -369,15 +401,15 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
        int numbered = 0;
        int start_number = -1;
        int keep_subject = 0;
+       int subject_prefix = 0;
        int ignore_if_in_upstream = 0;
        int thread = 0;
        const char *in_reply_to = NULL;
-       struct diff_options patch_id_opts;
+       struct patch_ids ids;
        char *add_signoff = NULL;
        char message_id[1024];
        char ref_message_id[1024];
 
-       setup_ident();
        git_config(git_format_config);
        init_revisions(&rev, prefix);
        rev.commit_format = CMIT_FMT_EMAIL;
@@ -401,7 +433,7 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
                else if (!strcmp(argv[i], "-n") ||
                                !strcmp(argv[i], "--numbered"))
                        numbered = 1;
-               else if (!strncmp(argv[i], "--start-number=", 15))
+               else if (!prefixcmp(argv[i], "--start-number="))
                        start_number = strtol(argv[i] + 15, NULL, 10);
                else if (!strcmp(argv[i], "--start-number")) {
                        i++;
@@ -435,23 +467,37 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
                        memcpy(add_signoff, committer, endpos - committer + 1);
                        add_signoff[endpos - committer + 1] = 0;
                }
-               else if (!strcmp(argv[i], "--attach"))
+               else if (!strcmp(argv[i], "--attach")) {
                        rev.mime_boundary = git_version_string;
-               else if (!strncmp(argv[i], "--attach=", 9))
+                       rev.no_inline = 1;
+               }
+               else if (!prefixcmp(argv[i], "--attach=")) {
+                       rev.mime_boundary = argv[i] + 9;
+                       rev.no_inline = 1;
+               }
+               else if (!strcmp(argv[i], "--inline")) {
+                       rev.mime_boundary = git_version_string;
+                       rev.no_inline = 0;
+               }
+               else if (!prefixcmp(argv[i], "--inline=")) {
                        rev.mime_boundary = argv[i] + 9;
+                       rev.no_inline = 0;
+               }
                else if (!strcmp(argv[i], "--ignore-if-in-upstream"))
                        ignore_if_in_upstream = 1;
                else if (!strcmp(argv[i], "--thread"))
                        thread = 1;
-               else if (!strncmp(argv[i], "--in-reply-to=", 14))
+               else if (!prefixcmp(argv[i], "--in-reply-to="))
                        in_reply_to = argv[i] + 14;
                else if (!strcmp(argv[i], "--in-reply-to")) {
                        i++;
                        if (i == argc)
                                die("Need a Message-Id for --in-reply-to");
                        in_reply_to = argv[i];
-               }
-               else if (!strncmp(argv[i], "--suffix=", 9))
+               } else if (!prefixcmp(argv[i], "--subject-prefix=")) {
+                       subject_prefix = 1;
+                       rev.subject_prefix = argv[i] + 17;
+               } else if (!prefixcmp(argv[i], "--suffix="))
                        fmt_patch_suffix = argv[i] + 9;
                else
                        argv[j++] = argv[i];
@@ -462,6 +508,8 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
                start_number = 1;
        if (numbered && keep_subject)
                die ("-n and -k are mutually exclusive.");
+       if (keep_subject && subject_prefix)
+               die ("--subject-prefix and -k are mutually exclusive.");
 
        argc = setup_revisions(argc, argv, &rev, "HEAD");
        if (argc > 1)
@@ -473,7 +521,7 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
        if (!rev.diffopt.text)
                rev.diffopt.binary = 1;
 
-       if (!output_directory)
+       if (!output_directory && !use_stdout)
                output_directory = prefix;
 
        if (output_directory) {
@@ -495,22 +543,19 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
        }
 
        if (ignore_if_in_upstream)
-               get_patch_ids(&rev, &patch_id_opts, prefix);
+               get_patch_ids(&rev, &ids, prefix);
 
        if (!use_stdout)
                realstdout = fdopen(dup(1), "w");
 
        prepare_revision_walk(&rev);
        while ((commit = get_revision(&rev)) != NULL) {
-               unsigned char sha1[20];
-
                /* ignore merges */
                if (commit->parents && commit->parents->next)
                        continue;
 
                if (ignore_if_in_upstream &&
-                               !get_patch_id(commit, &patch_id_opts, sha1) &&
-                               lookup_object(sha1))
+                               has_commit_patch_id(commit, &ids))
                        continue;
 
                nr++;
@@ -539,7 +584,8 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
                        rev.message_id = message_id;
                }
                if (!use_stdout)
-                       reopen_stdout(commit, rev.nr, keep_subject);
+                       if (reopen_stdout(commit, rev.nr, keep_subject))
+                               die("Failed to create output files");
                shown = log_tree_commit(&rev, commit);
                free(commit->buffer);
                commit->buffer = NULL;
@@ -564,6 +610,8 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
                        fclose(stdout);
        }
        free(list);
+       if (ignore_if_in_upstream)
+               free_patch_ids(&ids);
        return 0;
 }
 
@@ -586,7 +634,7 @@ static const char cherry_usage[] =
 int cmd_cherry(int argc, const char **argv, const char *prefix)
 {
        struct rev_info revs;
-       struct diff_options patch_id_opts;
+       struct patch_ids ids;
        struct commit *commit;
        struct commit_list *list = NULL;
        const char *upstream;
@@ -632,7 +680,7 @@ int cmd_cherry(int argc, const char **argv, const char *prefix)
                        return 0;
        }
 
-       get_patch_ids(&revs, &patch_id_opts, prefix);
+       get_patch_ids(&revs, &ids, prefix);
 
        if (limit && add_pending_commit(limit, &revs, UNINTERESTING))
                die("Unknown commit %s", limit);
@@ -648,12 +696,10 @@ int cmd_cherry(int argc, const char **argv, const char *prefix)
        }
 
        while (list) {
-               unsigned char sha1[20];
                char sign = '+';
 
                commit = list->item;
-               if (!get_patch_id(commit, &patch_id_opts, sha1) &&
-                   lookup_object(sha1))
+               if (has_commit_patch_id(commit, &ids))
                        sign = '-';
 
                if (verbose) {
@@ -671,5 +717,6 @@ int cmd_cherry(int argc, const char **argv, const char *prefix)
                list = list->next;
        }
 
+       free_patch_ids(&ids);
        return 0;
 }