fmt-merge-msg: remove custom string_list implementation

This command uses a custom version of string list when it could
just as easily use the string_list API. Convert it to use string_list
and reduce the code size a bit.

Signed-off-by: Stephen Boyd <bebarino@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
Stephen Boyd 2010-03-24 00:16:03 -07:00 committed by Junio C Hamano
parent e242148012
commit fcb243f7db

View file

@ -4,6 +4,7 @@
#include "diff.h" #include "diff.h"
#include "revision.h" #include "revision.h"
#include "tag.h" #include "tag.h"
#include "string-list.h"
static const char * const fmt_merge_msg_usage[] = { static const char * const fmt_merge_msg_usage[] = {
"git fmt-merge-msg [--log|--no-log] [--file <file>]", "git fmt-merge-msg [--log|--no-log] [--file <file>]",
@ -24,58 +25,21 @@ static int fmt_merge_msg_config(const char *key, const char *value, void *cb)
return 0; return 0;
} }
struct list {
char **list;
void **payload;
unsigned nr, alloc;
};
static void append_to_list(struct list *list, char *value, void *payload)
{
if (list->nr == list->alloc) {
list->alloc += 32;
list->list = xrealloc(list->list, sizeof(char *) * list->alloc);
list->payload = xrealloc(list->payload,
sizeof(char *) * list->alloc);
}
list->payload[list->nr] = payload;
list->list[list->nr++] = value;
}
static int find_in_list(struct list *list, char *value)
{
int i;
for (i = 0; i < list->nr; i++)
if (!strcmp(list->list[i], value))
return i;
return -1;
}
static void free_list(struct list *list)
{
int i;
if (list->alloc == 0)
return;
for (i = 0; i < list->nr; i++) {
free(list->list[i]);
free(list->payload[i]);
}
free(list->list);
free(list->payload);
list->nr = list->alloc = 0;
}
struct src_data { struct src_data {
struct list branch, tag, r_branch, generic; struct string_list branch, tag, r_branch, generic;
int head_status; int head_status;
}; };
static struct list srcs = { NULL, NULL, 0, 0}; void init_src_data(struct src_data *data)
static struct list origins = { NULL, NULL, 0, 0}; {
data->branch.strdup_strings = 1;
data->tag.strdup_strings = 1;
data->r_branch.strdup_strings = 1;
data->generic.strdup_strings = 1;
}
static struct string_list srcs = { NULL, 0, 0, 1 };
static struct string_list origins = { NULL, 0, 0, 1 };
static int handle_line(char *line) static int handle_line(char *line)
{ {
@ -83,6 +47,7 @@ static int handle_line(char *line)
unsigned char *sha1; unsigned char *sha1;
char *src, *origin; char *src, *origin;
struct src_data *src_data; struct src_data *src_data;
struct string_list_item *item;
int pulling_head = 0; int pulling_head = 0;
if (len < 43 || line[40] != '\t') if (len < 43 || line[40] != '\t')
@ -115,64 +80,62 @@ static int handle_line(char *line)
pulling_head = 1; pulling_head = 1;
} }
i = find_in_list(&srcs, src); item = unsorted_string_list_lookup(&srcs, src);
if (i < 0) { if (!item) {
i = srcs.nr; item = string_list_append(src, &srcs);
append_to_list(&srcs, xstrdup(src), item->util = xcalloc(1, sizeof(struct src_data));
xcalloc(1, sizeof(struct src_data))); init_src_data(item->util);
} }
src_data = srcs.payload[i]; src_data = item->util;
if (pulling_head) { if (pulling_head) {
origin = xstrdup(src); origin = src;
src_data->head_status |= 1; src_data->head_status |= 1;
} else if (!prefixcmp(line, "branch ")) { } else if (!prefixcmp(line, "branch ")) {
origin = xstrdup(line + 7); origin = line + 7;
append_to_list(&src_data->branch, origin, NULL); string_list_append(origin, &src_data->branch);
src_data->head_status |= 2; src_data->head_status |= 2;
} else if (!prefixcmp(line, "tag ")) { } else if (!prefixcmp(line, "tag ")) {
origin = line; origin = line;
append_to_list(&src_data->tag, xstrdup(origin + 4), NULL); string_list_append(origin + 4, &src_data->tag);
src_data->head_status |= 2; src_data->head_status |= 2;
} else if (!prefixcmp(line, "remote branch ")) { } else if (!prefixcmp(line, "remote branch ")) {
origin = xstrdup(line + 14); origin = line + 14;
append_to_list(&src_data->r_branch, origin, NULL); string_list_append(origin, &src_data->r_branch);
src_data->head_status |= 2; src_data->head_status |= 2;
} else { } else {
origin = xstrdup(src); origin = src;
append_to_list(&src_data->generic, xstrdup(line), NULL); string_list_append(line, &src_data->generic);
src_data->head_status |= 2; src_data->head_status |= 2;
} }
if (!strcmp(".", src) || !strcmp(src, origin)) { if (!strcmp(".", src) || !strcmp(src, origin)) {
int len = strlen(origin); int len = strlen(origin);
if (origin[0] == '\'' && origin[len - 1] == '\'') { if (origin[0] == '\'' && origin[len - 1] == '\'')
origin = xmemdupz(origin + 1, len - 2); origin = xmemdupz(origin + 1, len - 2);
} else {
origin = xstrdup(origin);
}
} else { } else {
char *new_origin = xmalloc(strlen(origin) + strlen(src) + 5); char *new_origin = xmalloc(strlen(origin) + strlen(src) + 5);
sprintf(new_origin, "%s of %s", origin, src); sprintf(new_origin, "%s of %s", origin, src);
origin = new_origin; origin = new_origin;
} }
append_to_list(&origins, origin, sha1); string_list_append(origin, &origins)->util = sha1;
return 0; return 0;
} }
static void print_joined(const char *singular, const char *plural, static void print_joined(const char *singular, const char *plural,
struct list *list, struct strbuf *out) struct string_list *list, struct strbuf *out)
{ {
if (list->nr == 0) if (list->nr == 0)
return; return;
if (list->nr == 1) { if (list->nr == 1) {
strbuf_addf(out, "%s%s", singular, list->list[0]); strbuf_addf(out, "%s%s", singular, list->items[0].string);
} else { } else {
int i; int i;
strbuf_addstr(out, plural); strbuf_addstr(out, plural);
for (i = 0; i < list->nr - 1; i++) for (i = 0; i < list->nr - 1; i++)
strbuf_addf(out, "%s%s", i > 0 ? ", " : "", list->list[i]); strbuf_addf(out, "%s%s", i > 0 ? ", " : "",
strbuf_addf(out, " and %s", list->list[list->nr - 1]); list->items[i].string);
strbuf_addf(out, " and %s", list->items[list->nr - 1].string);
} }
} }
@ -183,7 +146,7 @@ static void shortlog(const char *name, unsigned char *sha1,
int i, count = 0; int i, count = 0;
struct commit *commit; struct commit *commit;
struct object *branch; struct object *branch;
struct list subjects = { NULL, NULL, 0, 0 }; struct string_list subjects = { NULL, 0, 0, 1 };
int flags = UNINTERESTING | TREESAME | SEEN | SHOWN | ADDED; int flags = UNINTERESTING | TREESAME | SEEN | SHOWN | ADDED;
struct strbuf sb = STRBUF_INIT; struct strbuf sb = STRBUF_INIT;
@ -213,12 +176,10 @@ static void shortlog(const char *name, unsigned char *sha1,
strbuf_ltrim(&sb); strbuf_ltrim(&sb);
if (!sb.len) if (!sb.len)
append_to_list(&subjects, xstrdup(sha1_to_hex( string_list_append(sha1_to_hex(commit->object.sha1),
commit->object.sha1)), &subjects);
NULL);
else else
append_to_list(&subjects, strbuf_detach(&sb, NULL), string_list_append(strbuf_detach(&sb, NULL), &subjects);
NULL);
} }
if (count > limit) if (count > limit)
@ -230,7 +191,7 @@ static void shortlog(const char *name, unsigned char *sha1,
if (i >= limit) if (i >= limit)
strbuf_addf(out, " ...\n"); strbuf_addf(out, " ...\n");
else else
strbuf_addf(out, " %s\n", subjects.list[i]); strbuf_addf(out, " %s\n", subjects.items[i].string);
clear_commit_marks((struct commit *)branch, flags); clear_commit_marks((struct commit *)branch, flags);
clear_commit_marks(head, flags); clear_commit_marks(head, flags);
@ -238,7 +199,7 @@ static void shortlog(const char *name, unsigned char *sha1,
rev->commits = NULL; rev->commits = NULL;
rev->pending.nr = 0; rev->pending.nr = 0;
free_list(&subjects); string_list_clear(&subjects, 0);
} }
int fmt_merge_msg(int merge_summary, struct strbuf *in, struct strbuf *out) { int fmt_merge_msg(int merge_summary, struct strbuf *in, struct strbuf *out) {
@ -273,14 +234,14 @@ int fmt_merge_msg(int merge_summary, struct strbuf *in, struct strbuf *out) {
strbuf_addstr(out, "Merge "); strbuf_addstr(out, "Merge ");
for (i = 0; i < srcs.nr; i++) { for (i = 0; i < srcs.nr; i++) {
struct src_data *src_data = srcs.payload[i]; struct src_data *src_data = srcs.items[i].util;
const char *subsep = ""; const char *subsep = "";
strbuf_addstr(out, sep); strbuf_addstr(out, sep);
sep = "; "; sep = "; ";
if (src_data->head_status == 1) { if (src_data->head_status == 1) {
strbuf_addstr(out, srcs.list[i]); strbuf_addstr(out, srcs.items[i].string);
continue; continue;
} }
if (src_data->head_status == 3) { if (src_data->head_status == 3) {
@ -309,8 +270,8 @@ int fmt_merge_msg(int merge_summary, struct strbuf *in, struct strbuf *out) {
print_joined("commit ", "commits ", &src_data->generic, print_joined("commit ", "commits ", &src_data->generic,
out); out);
} }
if (strcmp(".", srcs.list[i])) if (strcmp(".", srcs.items[i].string))
strbuf_addf(out, " of %s", srcs.list[i]); strbuf_addf(out, " of %s", srcs.items[i].string);
} }
if (!strcmp("master", current_branch)) if (!strcmp("master", current_branch))
@ -329,7 +290,7 @@ int fmt_merge_msg(int merge_summary, struct strbuf *in, struct strbuf *out) {
rev.limited = 1; rev.limited = 1;
for (i = 0; i < origins.nr; i++) for (i = 0; i < origins.nr; i++)
shortlog(origins.list[i], origins.payload[i], shortlog(origins.items[i].string, origins.items[i].util,
head, &rev, limit, out); head, &rev, limit, out);
} }
return 0; return 0;