git-merge: warn when -m provided on a fast forward
[gitweb.git] / builtin-log.c
index f3cff13edc80afad27b9f7930a0a736ba5aa6d16..a5e4b625f8cdb29f3f4cf09d90038df5f7201a6d 100644 (file)
@@ -11,6 +11,7 @@
 #include "log-tree.h"
 #include "builtin.h"
 #include "tag.h"
+#include "reflog-walk.h"
 
 static int default_show_root = 1;
 
@@ -50,8 +51,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;
        }
@@ -178,6 +182,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 +224,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 +267,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++] = '/';
@@ -262,7 +303,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,10 +321,16 @@ 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,
@@ -349,7 +397,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)
@@ -377,7 +425,6 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
        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;
@@ -473,7 +520,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) {
@@ -539,7 +586,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;