Code

sha1_file.c (write_sha1_from_fd): Detect close failure.
[git.git] / builtin-log.c
index 982d871887a6cd5f3e751a8c650ef95e8332b186..71df957eaa0b85bd841fe3758b6efbfd51243881 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;
 
@@ -31,10 +32,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 = "";
                }
@@ -88,8 +89,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)
@@ -181,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;
@@ -192,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') ||
@@ -232,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++] = '/';
@@ -255,7 +293,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))
@@ -265,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])) {
@@ -282,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,
@@ -403,7 +448,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++;
@@ -437,15 +482,27 @@ 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++;
@@ -453,7 +510,7 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
                                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], "--suffix="))
                        fmt_patch_suffix = argv[i] + 9;
                else
                        argv[j++] = argv[i];
@@ -541,7 +598,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;