Merge branch 'jc/maint-format-patch-o-relative' into maint
authorJunio C Hamano <gitster@pobox.com>
Thu, 29 Jan 2009 07:56:13 +0000 (23:56 -0800)
committerJunio C Hamano <gitster@pobox.com>
Thu, 29 Jan 2009 07:56:13 +0000 (23:56 -0800)
* jc/maint-format-patch-o-relative:
Teach format-patch to handle output directory relative to cwd

Conflicts:
t/t4014-format-patch.sh

1  2 
builtin-log.c
t/t4014-format-patch.sh
diff --combined builtin-log.c
index 4420b4fd4f871d1284b90e80fa58fbe529b61592,16a0f11c5d78e27cdb64846006402f7d52965a59..60f8dd86048fd3a4ec51d7296b85984a0cefd602
@@@ -14,6 -14,7 +14,6 @@@
  #include "tag.h"
  #include "reflog-walk.h"
  #include "patch-ids.h"
 -#include "refs.h"
  #include "run-command.h"
  #include "shortlog.h"
  
@@@ -24,10 -25,36 +24,10 @@@ static int default_show_root = 1
  static const char *fmt_patch_subject_prefix = "PATCH";
  static const char *fmt_pretty;
  
 -static void add_name_decoration(const char *prefix, const char *name, struct object *obj)
 -{
 -      int plen = strlen(prefix);
 -      int nlen = strlen(name);
 -      struct name_decoration *res = xmalloc(sizeof(struct name_decoration) + plen + nlen);
 -      memcpy(res->name, prefix, plen);
 -      memcpy(res->name + plen, name, nlen + 1);
 -      res->next = add_decoration(&name_decoration, obj, res);
 -}
 -
 -static int add_ref_decoration(const char *refname, const unsigned char *sha1, int flags, void *cb_data)
 -{
 -      struct object *obj = parse_object(sha1);
 -      if (!obj)
 -              return 0;
 -      add_name_decoration("", refname, obj);
 -      while (obj->type == OBJ_TAG) {
 -              obj = ((struct tag *)obj)->tagged;
 -              if (!obj)
 -                      break;
 -              add_name_decoration("tag: ", refname, obj);
 -      }
 -      return 0;
 -}
 -
  static void cmd_log_init(int argc, const char **argv, const char *prefix,
                      struct rev_info *rev)
  {
        int i;
 -      int decorate = 0;
  
        rev->abbrev = DEFAULT_ABBREV;
        rev->commit_format = CMIT_FMT_DEFAULT;
@@@ -37,7 -64,6 +37,7 @@@
        DIFF_OPT_SET(&rev->diffopt, RECURSIVE);
        rev->show_root_diff = default_show_root;
        rev->subject_prefix = fmt_patch_subject_prefix;
 +      DIFF_OPT_SET(&rev->diffopt, ALLOW_TEXTCONV);
  
        if (default_date_mode)
                rev->date_mode = parse_date_format(default_date_mode);
        for (i = 1; i < argc; i++) {
                const char *arg = argv[i];
                if (!strcmp(arg, "--decorate")) {
 -                      if (!decorate)
 -                              for_each_ref(add_ref_decoration, NULL);
 -                      decorate = 1;
 +                      load_ref_decorations();
 +                      rev->show_decorations = 1;
 +              } else if (!strcmp(arg, "--source")) {
 +                      rev->show_source = 1;
                } else
                        die("unrecognized argument: %s", arg);
        }
@@@ -192,11 -217,6 +192,11 @@@ static int cmd_log_walk(struct rev_inf
        if (rev->early_output)
                finish_early_output(rev);
  
 +      /*
 +       * For --check and --exit-code, the exit code is based on CHECK_FAILED
 +       * and HAS_CHANGES being accumulated in rev->diffopt, so be careful to
 +       * retain that state information if replacing rev->diffopt in this loop
 +       */
        while ((commit = get_revision(rev)) != NULL) {
                log_tree_commit(rev, commit);
                if (!rev->reflog_info) {
                free_commit_list(commit->parents);
                commit->parents = NULL;
        }
 -      return 0;
 +      if (rev->diffopt.output_format & DIFF_FORMAT_CHECKDIFF &&
 +          DIFF_OPT_TST(&rev->diffopt, CHECK_FAILED)) {
 +              return 02;
 +      }
 +      return diff_result_code(&rev->diffopt, 0);
  }
  
  static int git_log_config(const char *var, const char *value, void *cb)
@@@ -434,7 -450,7 +434,7 @@@ static int istitlechar(char c
  
  static const char *fmt_patch_suffix = ".patch";
  static int numbered = 0;
 -static int auto_number = 0;
 +static int auto_number = 1;
  
  static char **extra_hdr;
  static int extra_hdr_nr;
@@@ -493,7 -509,6 +493,7 @@@ static int git_format_config(const cha
                        return 0;
                }
                numbered = git_config_bool(var, value);
 +              auto_number = auto_number && numbered;
                return 0;
        }
  
@@@ -553,6 -568,7 +553,7 @@@ static const char *get_oneline_for_file
  
  static FILE *realstdout = NULL;
  static const char *output_directory = NULL;
+ static int outdir_offset;
  
  static int reopen_stdout(const char *oneline, int nr, int total)
  {
                strcpy(filename + len, fmt_patch_suffix);
        }
  
-       fprintf(realstdout, "%s\n", filename);
+       fprintf(realstdout, "%s\n", filename + outdir_offset);
        if (freopen(filename, "w", stdout) == NULL)
                return error("Cannot open patch file %s",filename);
  
@@@ -637,9 -653,10 +638,9 @@@ static void gen_message_id(struct rev_i
        const char *committer = git_committer_info(IDENT_WARN_ON_NO_NAME);
        const char *email_start = strrchr(committer, '<');
        const char *email_end = strrchr(committer, '>');
 -      struct strbuf buf;
 +      struct strbuf buf = STRBUF_INIT;
        if (!email_start || !email_end || email_start > email_end - 1)
                die("Could not extract email from committer identity.");
 -      strbuf_init(&buf, 0);
        strbuf_addf(&buf, "%s.%lu.git.%.*s", base,
                    (unsigned long) time(NULL),
                    (int)(email_end - email_start - 1), email_start + 1);
@@@ -658,7 -675,7 +659,7 @@@ static void make_cover_letter(struct re
        const char *msg;
        const char *extra_headers = rev->extra_headers;
        struct shortlog log;
 -      struct strbuf sb;
 +      struct strbuf sb = STRBUF_INIT;
        int i;
        const char *encoding = "utf-8";
        struct diff_options opts;
        committer = git_committer_info(0);
  
        msg = body;
 -      strbuf_init(&sb, 0);
        pp_user_info(NULL, CMIT_FMT_EMAIL, &sb, committer, DATE_RFC2822,
                     encoding);
        pp_title_line(CMIT_FMT_EMAIL, &msg, &sb, subject_start, extra_headers,
@@@ -740,6 -758,27 +741,27 @@@ static const char *clean_message_id(con
        return xmemdupz(a, z - a);
  }
  
+ static const char *set_outdir(const char *prefix, const char *output_directory)
+ {
+       if (output_directory && is_absolute_path(output_directory))
+               return output_directory;
+       if (!prefix || !*prefix) {
+               if (output_directory)
+                       return output_directory;
+               /* The user did not explicitly ask for "./" */
+               outdir_offset = 2;
+               return "./";
+       }
+       outdir_offset = strlen(prefix);
+       if (!output_directory)
+               return prefix;
+       return xstrdup(prefix_filename(prefix, outdir_offset,
+                                      output_directory));
+ }
  int cmd_format_patch(int argc, const char **argv, const char *prefix)
  {
        struct commit *commit;
        const char *in_reply_to = NULL;
        struct patch_ids ids;
        char *add_signoff = NULL;
 -      struct strbuf buf;
 +      struct strbuf buf = STRBUF_INIT;
  
        git_config(git_format_config, NULL);
        init_revisions(&rev, prefix);
        }
        argc = j;
  
 -      strbuf_init(&buf, 0);
 -
        for (i = 0; i < extra_hdr_nr; i++) {
                strbuf_addstr(&buf, extra_hdr[i]);
                strbuf_addch(&buf, '\n');
        if (argc > 1)
                die ("unrecognized argument: %s", argv[1]);
  
 -      if (!rev.diffopt.output_format)
 +      if (!rev.diffopt.output_format
 +              || rev.diffopt.output_format == DIFF_FORMAT_PATCH)
                rev.diffopt.output_format = DIFF_FORMAT_DIFFSTAT | DIFF_FORMAT_SUMMARY | DIFF_FORMAT_PATCH;
  
        if (!DIFF_OPT_TST(&rev.diffopt, TEXT) && !no_binary_diff)
                DIFF_OPT_SET(&rev.diffopt, BINARY);
  
-       if (!output_directory && !use_stdout)
-               output_directory = prefix;
+       if (!use_stdout)
+               output_directory = set_outdir(prefix, output_directory);
  
        if (output_directory) {
                if (use_stdout)
                 * get_revision() to do the usual traversal.
                 */
        }
 +
 +      /*
 +       * We cannot move this anywhere earlier because we do want to
 +       * know if --root was given explicitly from the comand line.
 +       */
 +      rev.show_root_diff = 1;
 +
        if (cover_letter) {
                /* remember the range */
                int i;
@@@ -1151,7 -1184,8 +1173,7 @@@ int cmd_cherry(int argc, const char **a
                        sign = '-';
  
                if (verbose) {
 -                      struct strbuf buf;
 -                      strbuf_init(&buf, 0);
 +                      struct strbuf buf = STRBUF_INIT;
                        pretty_print_commit(CMIT_FMT_ONELINE, commit,
                                            &buf, 0, NULL, NULL, 0, 0);
                        printf("%c %s %s\n", sign,
diff --combined t/t4014-format-patch.sh
index 9d99dc28879d4f7f35001e0785f97f319fe13b40,16de4364d74bfdb3069149af740d329351b05e2c..f045898fe3196b068d03a66fd9edeea6f32add30
@@@ -3,7 -3,7 +3,7 @@@
  # Copyright (c) 2006 Junio C Hamano
  #
  
- test_description='Format-patch skipping already incorporated patches'
+ test_description='various format-patch tests'
  
  . ./test-lib.sh
  
@@@ -230,29 -230,54 +230,79 @@@ test_expect_success 'shortlog of cover-
  
  '
  
 +cat > expect << EOF
 +---
 + file |   16 ++++++++++++++++
 + 1 files changed, 16 insertions(+), 0 deletions(-)
 +
 +diff --git a/file b/file
 +index 40f36c6..2dc5c23 100644
 +--- a/file
 ++++ b/file
 +@@ -13,4 +13,20 @@ C
 + 10
 + D
 + E
 + F
 ++5
 +EOF
 +
 +test_expect_success 'format-patch respects -U' '
 +
 +      git format-patch -U4 -2 &&
 +      sed -e "1,/^$/d" -e "/^+5/q" < 0001-This-is-an-excessively-long-subject-line-for-a-messa.patch > output &&
 +      test_cmp expect output
 +
 +'
 +
+ test_expect_success 'format-patch from a subdirectory (1)' '
+       filename=$(
+               rm -rf sub &&
+               mkdir -p sub/dir &&
+               cd sub/dir &&
+               git format-patch -1
+       ) &&
+       case "$filename" in
+       0*)
+               ;; # ok
+       *)
+               echo "Oops? $filename"
+               false
+               ;;
+       esac &&
+       test -f "$filename"
+ '
+ test_expect_success 'format-patch from a subdirectory (2)' '
+       filename=$(
+               rm -rf sub &&
+               mkdir -p sub/dir &&
+               cd sub/dir &&
+               git format-patch -1 -o ..
+       ) &&
+       case "$filename" in
+       ../0*)
+               ;; # ok
+       *)
+               echo "Oops? $filename"
+               false
+               ;;
+       esac &&
+       basename=$(expr "$filename" : ".*/\(.*\)") &&
+       test -f "sub/$basename"
+ '
+ test_expect_success 'format-patch from a subdirectory (3)' '
+       here="$TEST_DIRECTORY/$test" &&
+       rm -f 0* &&
+       filename=$(
+               rm -rf sub &&
+               mkdir -p sub/dir &&
+               cd sub/dir &&
+               git format-patch -1 -o "$here"
+       ) &&
+       basename=$(expr "$filename" : ".*/\(.*\)") &&
+       test -f "$basename"
+ '
  test_done