forked from jonas/tig
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlog.c
158 lines (136 loc) · 4.05 KB
/
log.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/* Copyright (c) 2006-2015 Jonas Fonseca <jonas.fonseca@gmail.com>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of
* the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
#include "tig/refdb.h"
#include "tig/display.h"
#include "tig/draw.h"
#include "tig/log.h"
#include "tig/diff.h"
#include "tig/pager.h"
struct log_state {
/* Used for tracking when we need to recalculate the previous
* 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 = box_text(line);
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)
{
struct log_state *state = view->private;
int last_lineno = state->last_lineno;
if (!last_lineno || abs(last_lineno - line->lineno) > 1
|| (state->last_type == LINE_COMMIT && last_lineno > line->lineno)) {
struct line *commit_line = find_prev_line_by_type(view, line, LINE_COMMIT);
if (commit_line)
log_copy_rev(view, commit_line);
}
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;
}
static bool
log_open(struct view *view, enum open_flags flags)
{
const char *log_argv[] = {
"git", "log", encoding_arg, commit_order_arg(), "--cc",
"--stat", "%(logargs)", "%(cmdlineargs)", "%(revargs)",
"--no-color", "--", "%(fileargs)", NULL
};
return begin_update(view, NULL, log_argv, flags);
}
static enum request
log_request(struct view *view, enum request request, struct line *line)
{
switch (request) {
case REQ_REFRESH:
load_refs(true);
refresh_view(view);
return REQ_NONE;
case REQ_ENTER:
if (!display[1] || strcmp(display[1]->vid, view->ref))
open_diff_view(view, OPEN_SPLIT);
return REQ_NONE;
default:
return request;
}
}
static bool
log_read(struct view *view, struct buffer *buf)
{
struct line *line = NULL;
enum line_type type;
struct log_state *state = view->private;
size_t len;
char *commit;
char *data;
if (!buf)
return true;
data = buf->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;
else if (state->commit_title_read && len < 1) {
state->commit_title_read = false;
state->after_commit_header = true;
} else if (state->after_commit_header && len < 1) {
state->after_commit_header = false;
state->reading_diff_stat = true;
} else if (state->reading_diff_stat) {
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;
}
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 = {
"line",
argv_env.head,
VIEW_ADD_PAGER_REFS | VIEW_OPEN_DIFF | VIEW_SEND_CHILD_ENTER | VIEW_LOG_LIKE | VIEW_REFRESH | VIEW_FLEX_WIDTH,
sizeof(struct log_state),
log_open,
log_read,
view_column_draw,
log_request,
view_column_grep,
log_select,
NULL,
view_column_bit(LINE_NUMBER) | view_column_bit(TEXT),
pager_get_column_data,
};
DEFINE_VIEW(log);
/* vim: set ts=8 sw=8 noexpandtab: */