Merge branch 'jc/plug-fmt-merge-msg-leak'
authorJunio C Hamano <gitster@pobox.com>
Mon, 11 May 2015 21:23:45 +0000 (14:23 -0700)
committerJunio C Hamano <gitster@pobox.com>
Mon, 11 May 2015 21:23:46 +0000 (14:23 -0700)
* jc/plug-fmt-merge-msg-leak:
fmt-merge-msg: plug small leak of commit buffer

1  2 
builtin/fmt-merge-msg.c
diff --combined builtin/fmt-merge-msg.c
index 1d962dc569eaafc8429c4cec815922f382c576b9,9953007ef9de7c555f84486f0bdb3f45fe9dccbd..05f4c263112ae94978e4961988e9a4805fde2427
@@@ -10,7 -10,7 +10,7 @@@
  #include "gpg-interface.h"
  
  static const char * const fmt_merge_msg_usage[] = {
 -      N_("git fmt-merge-msg [-m <message>] [--log[=<n>]|--no-log] [--file <file>]"),
 +      N_("git fmt-merge-msg [-m <message>] [--log[=<n>] | --no-log] [--file <file>]"),
        NULL
  };
  
@@@ -100,8 -100,7 +100,8 @@@ static int handle_line(char *line, stru
  {
        int i, len = strlen(line);
        struct origin_data *origin_data;
 -      char *src, *origin;
 +      char *src;
 +      const char *origin;
        struct src_data *src_data;
        struct string_list_item *item;
        int pulling_head = 0;
                origin = line;
                string_list_append(&src_data->tag, origin + 4);
                src_data->head_status |= 2;
 -      } else if (starts_with(line, "remote-tracking branch ")) {
 -              origin = line + strlen("remote-tracking branch ");
 +      } else if (skip_prefix(line, "remote-tracking branch ", &origin)) {
                string_list_append(&src_data->r_branch, origin);
                src_data->head_status |= 2;
        } else {
                int len = strlen(origin);
                if (origin[0] == '\'' && origin[len - 1] == '\'')
                        origin = xmemdupz(origin + 1, len - 2);
 -      } else {
 -              char *new_origin = xmalloc(strlen(origin) + strlen(src) + 5);
 -              sprintf(new_origin, "%s of %s", origin, src);
 -              origin = new_origin;
 -      }
 +      } else
 +              origin = xstrfmt("%s of %s", origin, src);
        if (strcmp(".", src))
                origin_data->is_local_branch = 0;
        string_list_append(&origins, origin)->util = origin_data;
@@@ -216,23 -219,22 +216,21 @@@ static void add_branch_desc(struct strb
                        strbuf_addf(out, "  : %.*s", (int)(ep - bp), bp);
                        bp = ep;
                }
 -              if (out->buf[out->len - 1] != '\n')
 -                      strbuf_addch(out, '\n');
 +              strbuf_complete_line(out);
        }
        strbuf_release(&desc);
  }
  
  #define util_as_integral(elem) ((intptr_t)((elem)->util))
  
- static void record_person(int which, struct string_list *people,
-                         struct commit *commit)
+ static void record_person_from_buf(int which, struct string_list *people,
+                                  const char *buffer)
  {
-       const char *buffer;
        char *name_buf, *name, *name_end;
        struct string_list_item *elem;
        const char *field;
  
        field = (which == 'a') ? "\nauthor " : "\ncommitter ";
-       buffer = get_commit_buffer(commit, NULL);
        name = strstr(buffer, field);
        if (!name)
                return;
        if (name_end < name)
                return;
        name_buf = xmemdupz(name, name_end - name + 1);
-       unuse_commit_buffer(commit, buffer);
  
        elem = string_list_lookup(people, name_buf);
        if (!elem) {
        free(name_buf);
  }
  
 -      const char *buffer = get_commit_buffer(commit);
+ static void record_person(int which, struct string_list *people,
+                         struct commit *commit)
+ {
++      const char *buffer = get_commit_buffer(commit, NULL);
+       record_person_from_buf(which, people, buffer);
+       unuse_commit_buffer(commit, buffer);
+ }
  static int cmp_string_list_util_as_integral(const void *a_, const void *b_)
  {
        const struct string_list_item *a = a_, *b = b_;
@@@ -296,8 -306,8 +302,8 @@@ static void credit_people(struct strbu
        if (!them->nr ||
            (them->nr == 1 &&
             me &&
 -           (me = skip_prefix(me, them->items->string)) != NULL &&
 -           skip_prefix(me, " <")))
 +           skip_prefix(me, them->items->string, &me) &&
 +           starts_with(me, " <")))
                return;
        strbuf_addf(out, "\n%c %s ", comment_line_char, label);
        add_people_count(out, them);
@@@ -601,7 -611,7 +607,7 @@@ int fmt_merge_msg(struct strbuf *in, st
  
        /* get current branch */
        current_branch = current_branch_to_free =
 -              resolve_refdup("HEAD", head_sha1, 1, NULL);
 +              resolve_refdup("HEAD", RESOLVE_REF_READING, head_sha1, NULL);
        if (!current_branch)
                die("No current branch");
        if (starts_with(current_branch, "refs/heads/"))