提交 6784fab0 编写于 作者: J Junio C Hamano

Merge branch 'dk/blame-janitorial'

Code clean-up.

* dk/blame-janitorial:
  builtin/blame.c::find_copy_in_blob: no need to scan for region end
  blame.c: prepare_lines should not call xrealloc for every line
  builtin/blame.c::prepare_lines: fix allocation size of sb->lineno
  builtin/blame.c: eliminate same_suspect()
  builtin/blame.c: struct blame_entry does not need a prev link
...@@ -197,7 +197,6 @@ static void drop_origin_blob(struct origin *o) ...@@ -197,7 +197,6 @@ static void drop_origin_blob(struct origin *o)
* scoreboard structure, sorted by the target line number. * scoreboard structure, sorted by the target line number.
*/ */
struct blame_entry { struct blame_entry {
struct blame_entry *prev;
struct blame_entry *next; struct blame_entry *next;
/* the first line of this group in the final image; /* the first line of this group in the final image;
...@@ -256,15 +255,6 @@ struct scoreboard { ...@@ -256,15 +255,6 @@ struct scoreboard {
int *lineno; int *lineno;
}; };
static inline int same_suspect(struct origin *a, struct origin *b)
{
if (a == b)
return 1;
if (a->commit != b->commit)
return 0;
return !strcmp(a->path, b->path);
}
static void sanity_check_refcnt(struct scoreboard *); static void sanity_check_refcnt(struct scoreboard *);
/* /*
...@@ -277,13 +267,11 @@ static void coalesce(struct scoreboard *sb) ...@@ -277,13 +267,11 @@ static void coalesce(struct scoreboard *sb)
struct blame_entry *ent, *next; struct blame_entry *ent, *next;
for (ent = sb->ent; ent && (next = ent->next); ent = next) { for (ent = sb->ent; ent && (next = ent->next); ent = next) {
if (same_suspect(ent->suspect, next->suspect) && if (ent->suspect == next->suspect &&
ent->guilty == next->guilty && ent->guilty == next->guilty &&
ent->s_lno + ent->num_lines == next->s_lno) { ent->s_lno + ent->num_lines == next->s_lno) {
ent->num_lines += next->num_lines; ent->num_lines += next->num_lines;
ent->next = next->next; ent->next = next->next;
if (ent->next)
ent->next->prev = ent;
origin_decref(next->suspect); origin_decref(next->suspect);
free(next); free(next);
ent->score = 0; ent->score = 0;
...@@ -534,7 +522,7 @@ static void add_blame_entry(struct scoreboard *sb, struct blame_entry *e) ...@@ -534,7 +522,7 @@ static void add_blame_entry(struct scoreboard *sb, struct blame_entry *e)
prev = ent; prev = ent;
/* prev, if not NULL, is the last one that is below e */ /* prev, if not NULL, is the last one that is below e */
e->prev = prev;
if (prev) { if (prev) {
e->next = prev->next; e->next = prev->next;
prev->next = e; prev->next = e;
...@@ -543,8 +531,6 @@ static void add_blame_entry(struct scoreboard *sb, struct blame_entry *e) ...@@ -543,8 +531,6 @@ static void add_blame_entry(struct scoreboard *sb, struct blame_entry *e)
e->next = sb->ent; e->next = sb->ent;
sb->ent = e; sb->ent = e;
} }
if (e->next)
e->next->prev = e;
} }
/* /*
...@@ -555,14 +541,12 @@ static void add_blame_entry(struct scoreboard *sb, struct blame_entry *e) ...@@ -555,14 +541,12 @@ static void add_blame_entry(struct scoreboard *sb, struct blame_entry *e)
*/ */
static void dup_entry(struct blame_entry *dst, struct blame_entry *src) static void dup_entry(struct blame_entry *dst, struct blame_entry *src)
{ {
struct blame_entry *p, *n; struct blame_entry *n;
p = dst->prev;
n = dst->next; n = dst->next;
origin_incref(src->suspect); origin_incref(src->suspect);
origin_decref(dst->suspect); origin_decref(dst->suspect);
memcpy(dst, src, sizeof(*src)); memcpy(dst, src, sizeof(*src));
dst->prev = p;
dst->next = n; dst->next = n;
dst->score = 0; dst->score = 0;
} }
...@@ -742,7 +726,7 @@ static int find_last_in_target(struct scoreboard *sb, struct origin *target) ...@@ -742,7 +726,7 @@ static int find_last_in_target(struct scoreboard *sb, struct origin *target)
int last_in_target = -1; int last_in_target = -1;
for (e = sb->ent; e; e = e->next) { for (e = sb->ent; e; e = e->next) {
if (e->guilty || !same_suspect(e->suspect, target)) if (e->guilty || e->suspect != target)
continue; continue;
if (last_in_target < e->s_lno + e->num_lines) if (last_in_target < e->s_lno + e->num_lines)
last_in_target = e->s_lno + e->num_lines; last_in_target = e->s_lno + e->num_lines;
...@@ -762,7 +746,7 @@ static void blame_chunk(struct scoreboard *sb, ...@@ -762,7 +746,7 @@ static void blame_chunk(struct scoreboard *sb,
struct blame_entry *e; struct blame_entry *e;
for (e = sb->ent; e; e = e->next) { for (e = sb->ent; e; e = e->next) {
if (e->guilty || !same_suspect(e->suspect, target)) if (e->guilty || e->suspect != target)
continue; continue;
if (same <= e->s_lno) if (same <= e->s_lno)
continue; continue;
...@@ -939,7 +923,6 @@ static void find_copy_in_blob(struct scoreboard *sb, ...@@ -939,7 +923,6 @@ static void find_copy_in_blob(struct scoreboard *sb,
mmfile_t *file_p) mmfile_t *file_p)
{ {
const char *cp; const char *cp;
int cnt;
mmfile_t file_o; mmfile_t file_o;
struct handle_split_cb_data d; struct handle_split_cb_data d;
...@@ -950,13 +933,7 @@ static void find_copy_in_blob(struct scoreboard *sb, ...@@ -950,13 +933,7 @@ static void find_copy_in_blob(struct scoreboard *sb,
*/ */
cp = nth_line(sb, ent->lno); cp = nth_line(sb, ent->lno);
file_o.ptr = (char *) cp; file_o.ptr = (char *) cp;
cnt = ent->num_lines; file_o.size = nth_line(sb, ent->lno + ent->num_lines) - cp;
while (cnt && cp < sb->final_buf + sb->final_buf_size) {
if (*cp++ == '\n')
cnt--;
}
file_o.size = cp - file_o.ptr;
/* /*
* file_o is a part of final image we are annotating. * file_o is a part of final image we are annotating.
...@@ -992,7 +969,7 @@ static int find_move_in_parent(struct scoreboard *sb, ...@@ -992,7 +969,7 @@ static int find_move_in_parent(struct scoreboard *sb,
while (made_progress) { while (made_progress) {
made_progress = 0; made_progress = 0;
for (e = sb->ent; e; e = e->next) { for (e = sb->ent; e; e = e->next) {
if (e->guilty || !same_suspect(e->suspect, target) || if (e->guilty || e->suspect != target ||
ent_score(sb, e) < blame_move_score) ent_score(sb, e) < blame_move_score)
continue; continue;
find_copy_in_blob(sb, e, parent, split, &file_p); find_copy_in_blob(sb, e, parent, split, &file_p);
...@@ -1027,14 +1004,14 @@ static struct blame_list *setup_blame_list(struct scoreboard *sb, ...@@ -1027,14 +1004,14 @@ static struct blame_list *setup_blame_list(struct scoreboard *sb,
for (e = sb->ent, num_ents = 0; e; e = e->next) for (e = sb->ent, num_ents = 0; e; e = e->next)
if (!e->scanned && !e->guilty && if (!e->scanned && !e->guilty &&
same_suspect(e->suspect, target) && e->suspect == target &&
min_score < ent_score(sb, e)) min_score < ent_score(sb, e))
num_ents++; num_ents++;
if (num_ents) { if (num_ents) {
blame_list = xcalloc(num_ents, sizeof(struct blame_list)); blame_list = xcalloc(num_ents, sizeof(struct blame_list));
for (e = sb->ent, i = 0; e; e = e->next) for (e = sb->ent, i = 0; e; e = e->next)
if (!e->scanned && !e->guilty && if (!e->scanned && !e->guilty &&
same_suspect(e->suspect, target) && e->suspect == target &&
min_score < ent_score(sb, e)) min_score < ent_score(sb, e))
blame_list[i++].ent = e; blame_list[i++].ent = e;
} }
...@@ -1178,7 +1155,7 @@ static void pass_whole_blame(struct scoreboard *sb, ...@@ -1178,7 +1155,7 @@ static void pass_whole_blame(struct scoreboard *sb,
origin->file.ptr = NULL; origin->file.ptr = NULL;
} }
for (e = sb->ent; e; e = e->next) { for (e = sb->ent; e; e = e->next) {
if (!same_suspect(e->suspect, origin)) if (e->suspect != origin)
continue; continue;
origin_incref(porigin); origin_incref(porigin);
origin_decref(e->suspect); origin_decref(e->suspect);
...@@ -1567,7 +1544,7 @@ static void assign_blame(struct scoreboard *sb, int opt) ...@@ -1567,7 +1544,7 @@ static void assign_blame(struct scoreboard *sb, int opt)
/* Take responsibility for the remaining entries */ /* Take responsibility for the remaining entries */
for (ent = sb->ent; ent; ent = ent->next) for (ent = sb->ent; ent; ent = ent->next)
if (same_suspect(ent->suspect, suspect)) if (ent->suspect == suspect)
found_guilty_entry(ent); found_guilty_entry(ent);
origin_decref(suspect); origin_decref(suspect);
...@@ -1772,25 +1749,41 @@ static int prepare_lines(struct scoreboard *sb) ...@@ -1772,25 +1749,41 @@ static int prepare_lines(struct scoreboard *sb)
{ {
const char *buf = sb->final_buf; const char *buf = sb->final_buf;
unsigned long len = sb->final_buf_size; unsigned long len = sb->final_buf_size;
int num = 0, incomplete = 0, bol = 1; const char *end = buf + len;
const char *p;
int *lineno;
int num = 0, incomplete = 0;
if (len && buf[len-1] != '\n') for (p = buf;;) {
incomplete++; /* incomplete line at the end */ p = memchr(p, '\n', end - p);
while (len--) { if (p) {
if (bol) { p++;
sb->lineno = xrealloc(sb->lineno,
sizeof(int *) * (num + 1));
sb->lineno[num] = buf - sb->final_buf;
bol = 0;
}
if (*buf++ == '\n') {
num++; num++;
bol = 1; continue;
} }
break;
} }
sb->lineno = xrealloc(sb->lineno,
sizeof(int *) * (num + incomplete + 1)); if (len && end[-1] != '\n')
sb->lineno[num + incomplete] = buf - sb->final_buf; incomplete++; /* incomplete line at the end */
sb->lineno = xmalloc(sizeof(*sb->lineno) * (num + incomplete + 1));
lineno = sb->lineno;
*lineno++ = 0;
for (p = buf;;) {
p = memchr(p, '\n', end - p);
if (p) {
p++;
*lineno++ = p - buf;
continue;
}
break;
}
if (incomplete)
*lineno++ = len;
sb->num_lines = num + incomplete; sb->num_lines = num + incomplete;
return sb->num_lines; return sb->num_lines;
} }
...@@ -2502,8 +2495,6 @@ int cmd_blame(int argc, const char **argv, const char *prefix) ...@@ -2502,8 +2495,6 @@ int cmd_blame(int argc, const char **argv, const char *prefix)
ent->suspect = o; ent->suspect = o;
ent->s_lno = bottom; ent->s_lno = bottom;
ent->next = next; ent->next = next;
if (next)
next->prev = ent;
origin_incref(o); origin_incref(o);
} }
origin_decref(o); origin_decref(o);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册