From f992f0c80f30ac5a0aacfdfad55083dafb33047e Mon Sep 17 00:00:00 2001 From: Michael Haggerty Date: Sun, 4 Nov 2012 08:07:07 +0100 Subject: Initialize sort_uniq_list using named constant Signed-off-by: Michael Haggerty Acked-by: Johan Herland Signed-off-by: Jeff King --- notes.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'notes.c') diff --git a/notes.c b/notes.c index bc454e1ea..8652f8fcf 100644 --- a/notes.c +++ b/notes.c @@ -901,7 +901,7 @@ static int string_list_join_lines_helper(struct string_list_item *item, int combine_notes_cat_sort_uniq(unsigned char *cur_sha1, const unsigned char *new_sha1) { - struct string_list sort_uniq_list = { NULL, 0, 0, 1 }; + struct string_list sort_uniq_list = STRING_LIST_INIT_DUP; struct strbuf buf = STRBUF_INIT; int ret = 1; -- cgit v1.2.1 From 131352433621e89b2e8c58d8327b1d55bf0bc8d0 Mon Sep 17 00:00:00 2001 From: Michael Haggerty Date: Sun, 4 Nov 2012 08:07:08 +0100 Subject: combine_notes_cat_sort_uniq(): sort and dedup lines all at once Instead of reading lines one by one and insertion-sorting them into a string_list, read all of the lines, sort them, then remove duplicates. Aside from being less code, this reduces the complexity from O(N^2) to O(N lg N) in the total number of lines. Signed-off-by: Michael Haggerty Acked-by: Johan Herland Signed-off-by: Jeff King --- notes.c | 38 ++++++++++++++++---------------------- 1 file changed, 16 insertions(+), 22 deletions(-) (limited to 'notes.c') diff --git a/notes.c b/notes.c index 8652f8fcf..62f8f6f75 100644 --- a/notes.c +++ b/notes.c @@ -848,15 +848,16 @@ int combine_notes_ignore(unsigned char *cur_sha1, return 0; } -static int string_list_add_note_lines(struct string_list *sort_uniq_list, +/* + * Add the lines from the named object to list, with trailing + * newlines removed. + */ +static int string_list_add_note_lines(struct string_list *list, const unsigned char *sha1) { char *data; unsigned long len; enum object_type t; - struct strbuf buf = STRBUF_INIT; - struct strbuf **lines = NULL; - int i, list_index; if (is_null_sha1(sha1)) return 0; @@ -868,24 +869,14 @@ static int string_list_add_note_lines(struct string_list *sort_uniq_list, return t != OBJ_BLOB || !data; } - strbuf_attach(&buf, data, len, len + 1); - lines = strbuf_split(&buf, '\n'); - - for (i = 0; lines[i]; i++) { - if (lines[i]->buf[lines[i]->len - 1] == '\n') - strbuf_setlen(lines[i], lines[i]->len - 1); - if (!lines[i]->len) - continue; /* skip empty lines */ - list_index = string_list_find_insert_index(sort_uniq_list, - lines[i]->buf, 0); - if (list_index < 0) - continue; /* skip duplicate lines */ - string_list_insert_at_index(sort_uniq_list, list_index, - lines[i]->buf); - } - - strbuf_list_free(lines); - strbuf_release(&buf); + /* + * If the last line of the file is EOL-terminated, this will + * add an empty string to the list. But it will be removed + * later, along with any empty strings that came from empty + * lines within the file. + */ + string_list_split(list, data, '\n', -1); + free(data); return 0; } @@ -910,6 +901,9 @@ int combine_notes_cat_sort_uniq(unsigned char *cur_sha1, goto out; if (string_list_add_note_lines(&sort_uniq_list, new_sha1)) goto out; + string_list_remove_empty_items(&sort_uniq_list, 0); + sort_string_list(&sort_uniq_list); + string_list_remove_duplicates(&sort_uniq_list, 0); /* create a new blob object from sort_uniq_list */ if (for_each_string_list(&sort_uniq_list, -- cgit v1.2.1 From 031954d443668f6ea771e389c4137628d242c2d8 Mon Sep 17 00:00:00 2001 From: Michael Haggerty Date: Sun, 4 Nov 2012 08:07:09 +0100 Subject: notes: fix handling of colon-separated values The substrings output by strbuf_split() include the ':' delimiters. When processing GIT_NOTES_DISPLAY_REF and GIT_NOTES_REWRITE_REF, strip off the delimiter character *before* checking whether the substring is empty rather than after, so that empty strings within the list are also skipped. Signed-off-by: Michael Haggerty Acked-by: Johan Herland Signed-off-by: Jeff King --- notes.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'notes.c') diff --git a/notes.c b/notes.c index 62f8f6f75..63b2a09d4 100644 --- a/notes.c +++ b/notes.c @@ -951,10 +951,10 @@ void string_list_add_refs_from_colon_sep(struct string_list *list, split = strbuf_split(&globbuf, ':'); for (i = 0; split[i]; i++) { + if (split[i]->len && split[i]->buf[split[i]->len-1] == ':') + strbuf_setlen(split[i], split[i]->len-1); if (!split[i]->len) continue; - if (split[i]->buf[split[i]->len-1] == ':') - strbuf_setlen(split[i], split[i]->len-1); string_list_add_refs_by_glob(list, split[i]->buf); } -- cgit v1.2.1 From 6fa23773d2fa257cf45fb20640d6286d13ded852 Mon Sep 17 00:00:00 2001 From: Michael Haggerty Date: Sun, 4 Nov 2012 08:07:10 +0100 Subject: string_list_add_refs_from_colon_sep(): use string_list_split() It makes for simpler code than strbuf_split(). Signed-off-by: Michael Haggerty Acked-by: Johan Herland Signed-off-by: Jeff King --- notes.c | 21 ++++++++------------- 1 file changed, 8 insertions(+), 13 deletions(-) (limited to 'notes.c') diff --git a/notes.c b/notes.c index 63b2a09d4..b823701d9 100644 --- a/notes.c +++ b/notes.c @@ -943,23 +943,18 @@ void string_list_add_refs_by_glob(struct string_list *list, const char *glob) void string_list_add_refs_from_colon_sep(struct string_list *list, const char *globs) { - struct strbuf globbuf = STRBUF_INIT; - struct strbuf **split; + struct string_list split = STRING_LIST_INIT_NODUP; + char *globs_copy = xstrdup(globs); int i; - strbuf_addstr(&globbuf, globs); - split = strbuf_split(&globbuf, ':'); + string_list_split_in_place(&split, globs_copy, ':', -1); + string_list_remove_empty_items(&split, 0); - for (i = 0; split[i]; i++) { - if (split[i]->len && split[i]->buf[split[i]->len-1] == ':') - strbuf_setlen(split[i], split[i]->len-1); - if (!split[i]->len) - continue; - string_list_add_refs_by_glob(list, split[i]->buf); - } + for (i = 0; i < split.nr; i++) + string_list_add_refs_by_glob(list, split.items[i].string); - strbuf_list_free(split); - strbuf_release(&globbuf); + string_list_clear(&split, 0); + free(globs_copy); } static int notes_display_config(const char *k, const char *v, void *cb) -- cgit v1.2.1