tone down the detached head warning
[gitweb.git] / builtin-fmt-merge-msg.c
index 65274824d3b29aeb0ff78fc360c93820214c1ef8..87d3d63ec71ec7f08127085330d28ed28e3f35c7 100644 (file)
@@ -1,3 +1,4 @@
+#include "builtin.h"
 #include "cache.h"
 #include "commit.h"
 #include "diff.h"
@@ -7,7 +8,7 @@
 static const char *fmt_merge_msg_usage =
        "git-fmt-merge-msg [--summary] [--no-summary] [--file <file>]";
 
-static int merge_summary = 0;
+static int merge_summary;
 
 static int fmt_merge_msg_config(const char *key, const char *value)
 {
@@ -26,8 +27,8 @@ static void append_to_list(struct list *list, char *value, void *payload)
 {
        if (list->nr == list->alloc) {
                list->alloc += 32;
-               list->list = realloc(list->list, sizeof(char *) * list->alloc);
-               list->payload = realloc(list->payload,
+               list->list = xrealloc(list->list, sizeof(char *) * list->alloc);
+               list->payload = xrealloc(list->payload,
                                sizeof(char *) * list->alloc);
        }
        list->payload[list->nr] = payload;
@@ -54,8 +55,7 @@ static void free_list(struct list *list)
 
        for (i = 0; i < list->nr; i++) {
                free(list->list[i]);
-               if (list->payload[i])
-                       free(list->payload[i]);
+               free(list->payload[i]);
        }
        free(list->list);
        free(list->payload);
@@ -76,6 +76,7 @@ static int handle_line(char *line)
        unsigned char *sha1;
        char *src, *origin;
        struct src_data *src_data;
+       int pulling_head = 0;
 
        if (len < 43 || line[40] != '\t')
                return 1;
@@ -101,49 +102,52 @@ static int handle_line(char *line)
        if (src) {
                *src = 0;
                src += 4;
-       } else
-               src = "HEAD";
+               pulling_head = 0;
+       } else {
+               src = line;
+               pulling_head = 1;
+       }
 
        i = find_in_list(&srcs, src);
        if (i < 0) {
                i = srcs.nr;
-               append_to_list(&srcs, strdup(src),
+               append_to_list(&srcs, xstrdup(src),
                                xcalloc(1, sizeof(struct src_data)));
        }
        src_data = srcs.payload[i];
 
-       if (!strncmp(line, "branch ", 7)) {
-               origin = strdup(line + 7);
+       if (pulling_head) {
+               origin = xstrdup(src);
+               src_data->head_status |= 1;
+       } else if (!strncmp(line, "branch ", 7)) {
+               origin = xstrdup(line + 7);
                append_to_list(&src_data->branch, origin, NULL);
                src_data->head_status |= 2;
        } else if (!strncmp(line, "tag ", 4)) {
                origin = line;
-               append_to_list(&src_data->tag, strdup(origin + 4), NULL);
+               append_to_list(&src_data->tag, xstrdup(origin + 4), NULL);
                src_data->head_status |= 2;
        } else if (!strncmp(line, "remote branch ", 14)) {
-               origin = strdup(line + 14);
+               origin = xstrdup(line + 14);
                append_to_list(&src_data->r_branch, origin, NULL);
                src_data->head_status |= 2;
-       } else if (!strcmp(line, "HEAD")) {
-               origin = strdup(src);
-               src_data->head_status |= 1;
        } else {
-               origin = strdup(src);
-               append_to_list(&src_data->generic, strdup(line), NULL);
+               origin = xstrdup(src);
+               append_to_list(&src_data->generic, xstrdup(line), NULL);
                src_data->head_status |= 2;
        }
 
        if (!strcmp(".", src) || !strcmp(src, origin)) {
                int len = strlen(origin);
                if (origin[0] == '\'' && origin[len - 1] == '\'') {
-                       char *new_origin = malloc(len - 1);
+                       char *new_origin = xmalloc(len - 1);
                        memcpy(new_origin, origin + 1, len - 2);
-                       new_origin[len - 1] = 0;
+                       new_origin[len - 2] = 0;
                        origin = new_origin;
                } else
-                       origin = strdup(origin);
+                       origin = xstrdup(origin);
        } else {
-               char *new_origin = malloc(strlen(origin) + strlen(src) + 5);
+               char *new_origin = xmalloc(strlen(origin) + strlen(src) + 5);
                sprintf(new_origin, "%s of %s", origin, src);
                origin = new_origin;
        }
@@ -177,7 +181,7 @@ static void shortlog(const char *name, unsigned char *sha1,
        int flags = UNINTERESTING | TREECHANGE | SEEN | SHOWN | ADDED;
 
        branch = deref_tag(parse_object(sha1), sha1_to_hex(sha1), 40);
-       if (!branch || branch->type != TYPE_COMMIT)
+       if (!branch || branch->type != OBJ_COMMIT)
                return;
 
        setup_revisions(0, NULL, rev, NULL);
@@ -199,7 +203,7 @@ static void shortlog(const char *name, unsigned char *sha1,
 
                bol = strstr(commit->buffer, "\n\n");
                if (!bol) {
-                       append_to_list(&subjects, strdup(sha1_to_hex(
+                       append_to_list(&subjects, xstrdup(sha1_to_hex(
                                                        commit->object.sha1)),
                                        NULL);
                        continue;
@@ -210,11 +214,11 @@ static void shortlog(const char *name, unsigned char *sha1,
 
                if (eol) {
                        int len = eol - bol;
-                       oneline = malloc(len + 1);
+                       oneline = xmalloc(len + 1);
                        memcpy(oneline, bol, len);
                        oneline[len] = 0;
                } else
-                       oneline = strdup(bol);
+                       oneline = xstrdup(bol);
                append_to_list(&subjects, oneline, NULL);
        }
 
@@ -238,14 +242,14 @@ static void shortlog(const char *name, unsigned char *sha1,
        free_list(&subjects);
 }
 
-int cmd_fmt_merge_msg(int argc, char **argv, char **envp)
+int cmd_fmt_merge_msg(int argc, const char **argv, const char *prefix)
 {
        int limit = 20, i = 0;
        char line[1024];
        FILE *in = stdin;
        const char *sep = "";
        unsigned char head_sha1[20];
-       const char *head, *current_branch;
+       const char *current_branch;
 
        git_config(fmt_merge_msg_config);
 
@@ -273,10 +277,9 @@ int cmd_fmt_merge_msg(int argc, char **argv, char **envp)
                usage(fmt_merge_msg_usage);
 
        /* get current branch */
-       head = strdup(git_path("HEAD"));
-       current_branch = resolve_ref(head, head_sha1, 1);
-       current_branch += strlen(head) - 4;
-       free((char *)head);
+       current_branch = resolve_ref("HEAD", head_sha1, 1, NULL);
+       if (!current_branch)
+               die("No current branch");
        if (!strncmp(current_branch, "refs/heads/", 11))
                current_branch += 11;
 
@@ -338,7 +341,7 @@ int cmd_fmt_merge_msg(int argc, char **argv, char **envp)
                struct rev_info rev;
 
                head = lookup_commit(head_sha1);
-               init_revisions(&rev);
+               init_revisions(&rev, prefix);
                rev.commit_format = CMIT_FMT_ONELINE;
                rev.ignore_merges = 1;
                rev.limited = 1;