Skip to content

Commit e32cad8

Browse files
committed
Generalize and move parsing of reflog info into main_read
1 parent 547c402 commit e32cad8

File tree

1 file changed

+57
-41
lines changed

1 file changed

+57
-41
lines changed

tig.c

+57-41
Original file line numberDiff line numberDiff line change
@@ -2293,18 +2293,6 @@ draw_field(struct view *view, enum line_type type, const char *text, int width,
22932293
|| draw_space(view, LINE_DEFAULT, max - (view->col - col), max);
22942294
}
22952295

2296-
static bool PRINTF_LIKE(4, 5)
2297-
draw_formatted_field(struct view *view, enum line_type type, int width, const char *format, ...)
2298-
{
2299-
char text[SIZEOF_STR];
2300-
int retval;
2301-
2302-
FORMAT_BUFFER(text, sizeof(text), format, retval, TRUE);
2303-
if (retval < 0)
2304-
return VIEW_MAX_LEN(view) <= 0;
2305-
return draw_field(view, type, text, width, ALIGN_LEFT, FALSE);
2306-
}
2307-
23082296
static bool
23092297
draw_date(struct view *view, struct time *time)
23102298
{
@@ -7636,6 +7624,8 @@ static bool draw_graph(struct view *view, struct graph_canvas *canvas)
76367624
* Main view backend
76377625
*/
76387626

7627+
DEFINE_ALLOCATOR(realloc_reflogs, char *, 32)
7628+
76397629
struct commit {
76407630
char id[SIZEOF_REV]; /* SHA1 ID. */
76417631
const struct ident *author; /* Author of the commit. */
@@ -7647,7 +7637,10 @@ struct commit {
76477637
struct main_state {
76487638
struct graph graph;
76497639
struct commit current;
7650-
int id_width;
7640+
char **reflog;
7641+
size_t reflogs;
7642+
int reflog_width;
7643+
char reflogmsg[SIZEOF_STR / 2];
76517644
bool in_header;
76527645
bool added_changes_commits;
76537646
bool with_graph;
@@ -7685,6 +7678,7 @@ main_add_commit(struct view *view, enum line_type type, struct commit *template,
76857678
strncpy(commit->title, title, titlelen);
76867679
state->graph.canvas = &commit->graph;
76877680
memset(template, 0, sizeof(*template));
7681+
state->reflogmsg[0] = 0;
76887682
return commit;
76897683
}
76907684

@@ -7774,13 +7768,18 @@ main_open(struct view *view, enum open_flags flags)
77747768
static void
77757769
main_done(struct view *view)
77767770
{
7771+
struct main_state *state = view->private;
77777772
int i;
77787773

77797774
for (i = 0; i < view->lines; i++) {
77807775
struct commit *commit = view->line[i].data;
77817776

77827777
free(commit->graph.symbols);
77837778
}
7779+
7780+
for (i = 0; i < state->reflogs; i++)
7781+
free(state->reflog[i]);
7782+
free(state->reflog);
77847783
}
77857784

77867785
#define MAIN_NO_COMMIT_REFS 1
@@ -7812,9 +7811,10 @@ main_draw(struct view *view, struct line *line, unsigned int lineno)
78127811
return TRUE;
78137812

78147813
if (opt_show_id) {
7815-
if (state->id_width) {
7816-
if (draw_formatted_field(view, LINE_ID, state->id_width,
7817-
"stash@{%d}", line->lineno - 1))
7814+
if (state->reflogs) {
7815+
const char *id = state->reflog[line->lineno - 1];
7816+
7817+
if (draw_id_custom(view, LINE_ID, id, state->reflog_width))
78187818
return TRUE;
78197819
} else if (draw_id(view, commit->id)) {
78207820
return TRUE;
@@ -7838,6 +7838,31 @@ main_draw(struct view *view, struct line *line, unsigned int lineno)
78387838
return TRUE;
78397839
}
78407840

7841+
static bool
7842+
main_add_reflog(struct view *view, struct main_state *state, char *reflog)
7843+
{
7844+
char *end = strchr(reflog, ' ');
7845+
int id_width;
7846+
7847+
if (!end)
7848+
return FALSE;
7849+
*end = 0;
7850+
7851+
if (!realloc_reflogs(&state->reflog, state->reflogs, 1)
7852+
|| !(reflog = strdup(reflog)))
7853+
return FALSE;
7854+
7855+
state->reflog[state->reflogs++] = reflog;
7856+
id_width = strlen(reflog);
7857+
if (state->reflog_width < id_width) {
7858+
state->reflog_width = id_width;
7859+
if (opt_show_id)
7860+
view->force_redraw = TRUE;
7861+
}
7862+
7863+
return TRUE;
7864+
}
7865+
78417866
/* Reads git log --pretty=raw output and parses it into the commit struct. */
78427867
static bool
78437868
main_read(struct view *view, char *line)
@@ -7894,6 +7919,16 @@ main_read(struct view *view, char *line)
78947919
state->in_header = FALSE;
78957920

78967921
switch (type) {
7922+
case LINE_PP_REFLOG:
7923+
if (!main_add_reflog(view, state, line + STRING_SIZE("Reflog: ")))
7924+
return FALSE;
7925+
break;
7926+
7927+
case LINE_PP_REFLOGMSG:
7928+
line += STRING_SIZE("Reflog message: ");
7929+
string_ncopy(state->reflogmsg, line, strlen(line));
7930+
break;
7931+
78977932
case LINE_PARENT:
78987933
if (state->with_graph && !graph->has_parents)
78997934
graph_add_parent(graph, line + STRING_SIZE("parent "));
@@ -7926,6 +7961,8 @@ main_read(struct view *view, char *line)
79267961
line++;
79277962
if (*line == '\0')
79287963
break;
7964+
if (*state->reflogmsg)
7965+
line = state->reflogmsg;
79297966
main_add_commit(view, LINE_MAIN_COMMIT, commit, line, FALSE);
79307967
}
79317968

@@ -8068,32 +8105,11 @@ stash_open(struct view *view, enum open_flags flags)
80688105
{
80698106
static const char *stash_argv[] = { "git", "stash", "list",
80708107
opt_encoding_arg, "--no-color", "--pretty=raw", NULL };
8071-
8072-
return begin_update(view, NULL, stash_argv, flags | OPEN_RELOAD);
8073-
}
8074-
8075-
static bool
8076-
stash_read(struct view *view, char *line)
8077-
{
80788108
struct main_state *state = view->private;
8079-
struct commit *commit = &state->current;
8080-
8081-
if (!state->added_changes_commits) {
8082-
state->added_changes_commits = TRUE;
8083-
state->with_graph = FALSE;
8084-
}
8085-
8086-
if (commit && line && get_line_type(line) == LINE_PP_REFLOG) {
8087-
int id_width = STRING_SIZE("stash@{}") + count_digits(view->lines);
80888109

8089-
if (state->id_width < id_width) {
8090-
state->id_width = id_width;
8091-
if (opt_show_id)
8092-
view->force_redraw = TRUE;
8093-
}
8094-
}
8095-
8096-
return main_read(view, line);
8110+
state->added_changes_commits = TRUE;
8111+
state->with_graph = FALSE;
8112+
return begin_update(view, NULL, stash_argv, flags | OPEN_RELOAD);
80978113
}
80988114

80998115
static void
@@ -8110,7 +8126,7 @@ static struct view_ops stash_ops = {
81108126
VIEW_SEND_CHILD_ENTER,
81118127
sizeof(struct main_state),
81128128
stash_open,
8113-
stash_read,
8129+
main_read,
81148130
main_draw,
81158131
main_request,
81168132
main_grep,

0 commit comments

Comments
 (0)