Skip to content

Commit

Permalink
Find next merge directly
Browse files Browse the repository at this point in the history
Find merges by traversing lines directly. Instead of collecting
hte merges like matches, the next merge is determined starting
from the current line. Whether the search wraps around is
controlled by wrap-search.

As per review: #525 (comment)
  • Loading branch information
phi-gamma committed Oct 22, 2016
1 parent 62ee9a0 commit 4ce01c9
Showing 1 changed file with 39 additions and 37 deletions.
76 changes: 39 additions & 37 deletions src/search.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,36 +55,6 @@ find_matches(struct view *view)
return true;
}

static bool
find_merges(struct view *view)
{
size_t lineno;

for (lineno = 0; lineno < view->lines; lineno++) {
struct line *line = &view->line[lineno];
struct commit *commit = line->data;
struct graph_canvas *canvas = &commit->graph;
struct view_column_data column_data;

if (!view->ops->get_column_data(view, line, &column_data))
continue;

if (column_data.graph && !column_data.graph->is_merge(canvas))
continue;

if (!realloc_unsigned_ints(&view->matched_line, view->matched_lines, 1))
return false;

view->line[lineno].search_result = true;
view->matched_line[view->matched_lines++] = lineno;
}

/* Clear and show highlighted results. */
redraw_view_from(view, 0);

return true;
}

static enum status_code find_next_match(struct view *view, enum request request);

static enum status_code
Expand Down Expand Up @@ -191,6 +161,44 @@ find_next(struct view *view, enum request request)
report("%s", get_status_message(code));
}

static enum status_code
find_next_merge_line(struct view *view, int direction, bool wrapped)
{
enum status_code code = -1;
/*size_t lineno = view->pos.lineno + direction;*/
/*for (; lineno < view->lines; lineno = lineno + direction % view->lines)*/
ssize_t dist;
size_t max;
if (wrapped)
max = view->lines;
else
max = direction > 0 ? view->lines - view->pos.lineno
: view->pos.lineno;
for (dist = direction; abs(dist) < max; dist += direction) {
ssize_t lineno = view->pos.lineno + dist;
struct line *line = &view->line[lineno];
struct commit *commit = line->data;
struct graph_canvas *canvas = &commit->graph;
struct view_column_data column_data;

if (lineno < 0)
lineno += view->lines;
else if (lineno >= view->lines)
lineno -= view->lines;

if (!view->ops->get_column_data(view, line, &column_data))
continue;

if (column_data.graph && !column_data.graph->is_merge(canvas))
continue;

select_view_line(view, lineno);
code = SUCCESS;
break;
}
return code;
}

static enum status_code
find_next_merge(struct view *view, enum request request)
{
Expand All @@ -210,13 +218,7 @@ find_next_merge(struct view *view, enum request request)
return error("Invalid request searching for next merge");
}

if (!view->matched_lines && !find_merges(view))
return ERROR_OUT_OF_MEMORY;

code = find_next_match_line(view, direction, false);
if (code != SUCCESS && opt_wrap_search)
code = find_next_match_line(view, direction, true);

code = find_next_merge_line(view, direction, opt_wrap_search);

return code == SUCCESS ? code : success("No merge commit found");
}
Expand Down

0 comments on commit 4ce01c9

Please sign in to comment.