Skip to content

Commit

Permalink
log: add support for --graph
Browse files Browse the repository at this point in the history
  • Loading branch information
jonas committed Apr 19, 2014
1 parent 4217c77 commit fa2fcc9
Show file tree
Hide file tree
Showing 13 changed files with 72 additions and 26 deletions.
1 change: 1 addition & 0 deletions NEWS.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ Improvements:
- More informative configuration error messages.
- Add completion and history support to the prompt via readline. (GH #185)
- Options can be toggled individually for each view. (GH #89)
- Add support for `--graph` and highlight diff stats in the log view.

Bug fixes:

Expand Down
2 changes: 1 addition & 1 deletion include/tig/diff.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ struct diff_state {
enum request diff_common_edit(struct view *view, enum request request, struct line *line);
bool diff_common_read(struct view *view, const char *data, struct diff_state *state);
enum request diff_common_enter(struct view *view, enum request request, struct line *line);
bool diff_common_add_diff_stat(struct view *view, const char *data);
struct line *diff_common_add_diff_stat(struct view *view, const char *text, size_t offset);

unsigned int diff_get_lineno(struct view *view, struct line *line);
const char *diff_get_pathname(struct view *view, struct line *line);
Expand Down
2 changes: 1 addition & 1 deletion include/tig/pager.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
bool pager_column_init(struct view *view);
bool pager_get_column_data(struct view *view, const struct line *line, struct view_column_data *column_data);
bool pager_read(struct view *view, char *data);
bool pager_common_read(struct view *view, const char *data, enum line_type type);
bool pager_common_read(struct view *view, const char *data, enum line_type type, struct line **line);
enum request pager_request(struct view *view, enum request request, struct line *line);
void pager_select(struct view *view, struct line *line);

Expand Down
1 change: 1 addition & 0 deletions include/tig/string.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

bool string_isnumber(const char *str);
bool iscommit(const char *str);
#define get_graph_indent(str) strspn(str, "*|\\/ ")

static inline int
ascii_toupper(int c)
Expand Down
1 change: 1 addition & 0 deletions include/tig/view.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ struct line {
unsigned int wrapped:1;
unsigned int commit_title:1;
unsigned int no_commit_refs:1;
unsigned int graph_indent:1;

void *data; /* User data */
};
Expand Down
1 change: 1 addition & 0 deletions src/argv.c
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,7 @@ argv_parse_rev_flag(const char *arg, struct rev_flags *rev_flags)
"--first-parent",
"--fixed-strings",
"--full-history",
"--graph",
"--glob=",
"--left-only",
"--max-parents=",
Expand Down
13 changes: 7 additions & 6 deletions src/diff.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,10 @@ diff_open(struct view *view, enum open_flags flags)
return begin_update(view, NULL, diff_argv, flags);
}

bool
diff_common_add_diff_stat(struct view *view, const char *data)
struct line *
diff_common_add_diff_stat(struct view *view, const char *text, size_t offset)
{
const char *data = text + offset;
size_t len = strlen(data);
char *pipe = strchr(data, '|');
bool has_histogram = data[len - 1] == '-' || data[len - 1] == '+';
Expand All @@ -48,8 +49,8 @@ diff_common_add_diff_stat(struct view *view, const char *data)
bool has_no_change = pipe && strstr(pipe, " 0");

if (pipe && (has_histogram || has_bin_diff || has_rename || has_no_change))
return add_line_text(view, data, LINE_DIFF_STAT) != NULL;
return FALSE;
return add_line_text(view, text, LINE_DIFF_STAT);
return NULL;
}

bool
Expand All @@ -64,7 +65,7 @@ diff_common_read(struct view *view, const char *data, struct diff_state *state)
state->reading_diff_stat = TRUE;

if (state->reading_diff_stat) {
if (diff_common_add_diff_stat(view, data))
if (diff_common_add_diff_stat(view, data, 0))
return TRUE;
state->reading_diff_stat = FALSE;

Expand Down Expand Up @@ -97,7 +98,7 @@ diff_common_read(struct view *view, const char *data, struct diff_state *state)
if (!state->combined_diff && (type == LINE_DIFF_ADD2 || type == LINE_DIFF_DEL2))
type = LINE_DEFAULT;

return pager_common_read(view, data, type);
return pager_common_read(view, data, type, NULL);
}

static bool
Expand Down
10 changes: 9 additions & 1 deletion src/draw.c
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,7 @@ draw_graph(struct view *view, const struct graph_canvas *canvas)
static bool
draw_diff_stat_part(struct view *view, enum line_type *type, const char **text, char c, enum line_type next_type)
{
const char *sep = strchr(*text, c);
const char *sep = c == '|' ? strrchr(*text, c) : strchr(*text, c);

if (sep != NULL) {
draw_text_expanded(view, *type, *text, sep - *text, FALSE);
Expand Down Expand Up @@ -507,6 +507,14 @@ view_column_draw(struct view *view, struct line *line, unsigned int lineno)

if (line->wrapped && draw_text(view, LINE_DELIMITER, "+"))
return TRUE;

if (line->graph_indent) {
size_t indent = get_graph_indent(text);

if (draw_text_expanded(view, LINE_DEFAULT, text, indent, FALSE))
return TRUE;
text += indent;
}
if (type == LINE_DIFF_STAT)
draw_diff_stat(view, &type, &text);
if (line->commit_title) {
Expand Down
41 changes: 32 additions & 9 deletions src/log.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,22 @@ struct log_state {
* commit, for example when the user scrolls up or uses the page
* up/down in the log view. */
int last_lineno;
size_t graph_indent;
enum line_type last_type;
bool commit_title_read;
bool after_commit_header;
bool reading_diff_stat;
};

static inline void
log_copy_rev(struct view *view, struct line *line)
{
const char *text = line->data;
size_t offset = get_graph_indent(text);

string_copy_rev_from_commit_line(view->ref, text + offset);
}

static void
log_select(struct view *view, struct line *line)
{
Expand All @@ -37,15 +47,14 @@ log_select(struct view *view, struct line *line)

if (!last_lineno || abs(last_lineno - line->lineno) > 1
|| (state->last_type == LINE_COMMIT && last_lineno > line->lineno)) {
const struct line *commit_line = find_prev_line_by_type(view, line, LINE_COMMIT);
struct line *commit_line = find_prev_line_by_type(view, line, LINE_COMMIT);

if (commit_line)
string_copy_rev_from_commit_line(view->ref, commit_line->data);
log_copy_rev(view, commit_line);
}

if (line->type == LINE_COMMIT && !view_has_flags(view, VIEW_NO_REF)) {
string_copy_rev_from_commit_line(view->ref, (char *)line->data);
}
if (line->type == LINE_COMMIT && !view_has_flags(view, VIEW_NO_REF))
log_copy_rev(view, line);
string_copy_rev(view->env->commit, view->ref);
state->last_lineno = line->lineno;
state->last_type = line->type;
Expand Down Expand Up @@ -87,15 +96,21 @@ log_request(struct view *view, enum request request, struct line *line)
static bool
log_read(struct view *view, char *data)
{
struct line *line = NULL;
enum line_type type;
struct log_state *state = view->private;
size_t len;
char *commit;

if (!data)
return TRUE;

type = get_line_type(data);
len = strlen(data);
commit = strstr(data, "commit ");
if (commit && get_graph_indent(data) == commit - data)
state->graph_indent = commit - data;

type = get_line_type(data + state->graph_indent);
len = strlen(data + state->graph_indent);

if (type == LINE_COMMIT)
state->commit_title_read = TRUE;
Expand All @@ -106,12 +121,20 @@ log_read(struct view *view, char *data)
state->after_commit_header = FALSE;
state->reading_diff_stat = TRUE;
} else if (state->reading_diff_stat) {
if (diff_common_add_diff_stat(view, data))
line = diff_common_add_diff_stat(view, data, state->graph_indent);
if (line) {
if (state->graph_indent)
line->graph_indent = 1;
return TRUE;
}
state->reading_diff_stat = FALSE;
}

return pager_common_read(view, data, type);
if (!pager_common_read(view, data, type, &line))
return FALSE;
if (line && state->graph_indent)
line->graph_indent = 1;
return TRUE;
}

static struct view_ops log_ops = {
Expand Down
12 changes: 12 additions & 0 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,18 @@ main_check_argv(struct view *view, const char *argv[])
const char *arg = argv[i];
struct rev_flags rev_flags = {};

if (!strcmp(arg, "--graph")) {
struct view_column *column = get_view_column(view, VIEW_COLUMN_COMMIT_TITLE);

if (column) {
column->opt.commit_title.graph = TRUE;
if (opt_commit_order != COMMIT_ORDER_REVERSE)
state->with_graph = TRUE;
}
argv[i] = "";
continue;
}

if (!argv_parse_rev_flag(arg, &rev_flags))
continue;

Expand Down
5 changes: 0 additions & 5 deletions src/options.c
Original file line number Diff line number Diff line change
Expand Up @@ -130,11 +130,6 @@ update_options_from_argv(const char *argv[])
continue;
}

if (!strcmp(flag, "--graph")) {
opt_show_rev_graph = TRUE;
continue;
}

argv[flags_pos++] = flag;
}

Expand Down
7 changes: 5 additions & 2 deletions src/pager.c
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ pager_wrap_line(struct view *view, const char *data, enum line_type type)
}

bool
pager_common_read(struct view *view, const char *data, enum line_type type)
pager_common_read(struct view *view, const char *data, enum line_type type, struct line **line_ptr)
{
struct line *line;

Expand All @@ -156,6 +156,9 @@ pager_common_read(struct view *view, const char *data, enum line_type type)
if (!line)
return FALSE;

if (line_ptr)
*line_ptr = line;

if (line->type == LINE_COMMIT && view_has_flags(view, VIEW_ADD_PAGER_REFS))
add_pager_refs(view, data + STRING_SIZE("commit "));

Expand All @@ -168,7 +171,7 @@ pager_read(struct view *view, char *data)
if (!data)
return TRUE;

return pager_common_read(view, data, get_line_type(data));
return pager_common_read(view, data, get_line_type(data), NULL);
}

enum request
Expand Down
2 changes: 1 addition & 1 deletion src/stage.c
Original file line number Diff line number Diff line change
Expand Up @@ -481,7 +481,7 @@ stage_read(struct view *view, char *data)
struct stage_state *state = view->private;

if (stage_line_type == LINE_STAT_UNTRACKED)
return pager_common_read(view, data, LINE_DEFAULT);
return pager_common_read(view, data, LINE_DEFAULT, NULL);

if (data && diff_common_read(view, data, &state->diff))
return TRUE;
Expand Down

0 comments on commit fa2fcc9

Please sign in to comment.