Commit | Line | Data |
---|---|---|
4a2909a7 JF |
1 | /* Copyright (c) 2006 Jonas Fonseca <fonseca@diku.dk> |
2 | * See license info at the bottom. */ | |
b801d8b2 JF |
3 | /** |
4 | * TIG(1) | |
5 | * ====== | |
6 | * | |
7 | * NAME | |
8 | * ---- | |
9 | * tig - text-mode interface for git | |
10 | * | |
11 | * SYNOPSIS | |
12 | * -------- | |
13 | * [verse] | |
03a93dbb JF |
14 | * tig [options] |
15 | * tig [options] [--] [git log options] | |
b76c2afc JF |
16 | * tig [options] log [git log options] |
17 | * tig [options] diff [git diff options] | |
8855ada4 JF |
18 | * tig [options] show [git show options] |
19 | * tig [options] < [git command output] | |
b801d8b2 JF |
20 | * |
21 | * DESCRIPTION | |
22 | * ----------- | |
6706b2ba JF |
23 | * Browse changes in a git repository. Additionally, tig(1) can also act |
24 | * as a pager for output of various git commands. | |
25 | * | |
57bdf034 | 26 | * When browsing repositories, tig(1) uses the underlying git commands |
468876c9 JF |
27 | * to present the user with various views, such as summarized commit log |
28 | * and showing the commit with the log message, diffstat, and the diff. | |
6706b2ba | 29 | * |
468876c9 JF |
30 | * Using tig(1) as a pager, it will display input from stdin and try |
31 | * to colorize it. | |
b801d8b2 JF |
32 | **/ |
33 | ||
b76c2afc | 34 | #ifndef VERSION |
8c11094f | 35 | #define VERSION "tig-0.3" |
b76c2afc JF |
36 | #endif |
37 | ||
8855ada4 JF |
38 | #ifndef DEBUG |
39 | #define NDEBUG | |
40 | #endif | |
41 | ||
22f66b0a | 42 | #include <assert.h> |
4c6fabc2 | 43 | #include <errno.h> |
22f66b0a JF |
44 | #include <ctype.h> |
45 | #include <signal.h> | |
b801d8b2 | 46 | #include <stdarg.h> |
b801d8b2 | 47 | #include <stdio.h> |
22f66b0a | 48 | #include <stdlib.h> |
b801d8b2 | 49 | #include <string.h> |
6908bdbd | 50 | #include <unistd.h> |
b76c2afc | 51 | #include <time.h> |
b801d8b2 JF |
52 | |
53 | #include <curses.h> | |
b801d8b2 JF |
54 | |
55 | static void die(const char *err, ...); | |
56 | static void report(const char *msg, ...); | |
1ba2ae4b | 57 | static void set_nonblocking_input(bool loading); |
10e290ee | 58 | static size_t utf8_length(const char *string, size_t max_width, int *coloffset, int *trimmed); |
6b161b31 JF |
59 | |
60 | #define ABS(x) ((x) >= 0 ? (x) : -(x)) | |
61 | #define MIN(x, y) ((x) < (y) ? (x) : (y)) | |
62 | ||
63 | #define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0])) | |
64 | #define STRING_SIZE(x) (sizeof(x) - 1) | |
b76c2afc | 65 | |
2e8488b4 JF |
66 | #define SIZEOF_REF 256 /* Size of symbolic or SHA1 ID. */ |
67 | #define SIZEOF_CMD 1024 /* Size of command buffer. */ | |
b801d8b2 | 68 | |
82e78006 JF |
69 | /* This color name can be used to refer to the default term colors. */ |
70 | #define COLOR_DEFAULT (-1) | |
78c70acd | 71 | |
67e48ac5 | 72 | #define TIG_HELP "(d)iff, (l)og, (m)ain, (q)uit, (h)elp" |
468876c9 | 73 | |
82e78006 | 74 | /* The format and size of the date column in the main view. */ |
4c6fabc2 | 75 | #define DATE_FORMAT "%Y-%m-%d %H:%M" |
6b161b31 | 76 | #define DATE_COLS STRING_SIZE("2006-04-29 14:21 ") |
4c6fabc2 | 77 | |
10e290ee JF |
78 | #define AUTHOR_COLS 20 |
79 | ||
a28bcc22 | 80 | /* The default interval between line numbers. */ |
4a2909a7 | 81 | #define NUMBER_INTERVAL 1 |
82e78006 | 82 | |
6706b2ba JF |
83 | #define TABSIZE 8 |
84 | ||
a28bcc22 JF |
85 | #define SCALE_SPLIT_VIEW(height) ((height) * 2 / 3) |
86 | ||
8855ada4 | 87 | /* Some ascii-shorthands fitted into the ncurses namespace. */ |
a28bcc22 JF |
88 | #define KEY_TAB '\t' |
89 | #define KEY_RETURN '\r' | |
4a2909a7 JF |
90 | #define KEY_ESC 27 |
91 | ||
6706b2ba | 92 | |
a28bcc22 | 93 | /* User action requests. */ |
4a2909a7 | 94 | enum request { |
a28bcc22 JF |
95 | /* Offset all requests to avoid conflicts with ncurses getch values. */ |
96 | REQ_OFFSET = KEY_MAX + 1, | |
97 | ||
4a2909a7 | 98 | /* XXX: Keep the view request first and in sync with views[]. */ |
2e8488b4 | 99 | REQ_VIEW_MAIN, |
4a2909a7 JF |
100 | REQ_VIEW_DIFF, |
101 | REQ_VIEW_LOG, | |
2e8488b4 | 102 | REQ_VIEW_HELP, |
6908bdbd | 103 | REQ_VIEW_PAGER, |
4a2909a7 | 104 | |
6b161b31 | 105 | REQ_ENTER, |
03a93dbb JF |
106 | REQ_QUIT, |
107 | REQ_PROMPT, | |
4a2909a7 | 108 | REQ_SCREEN_REDRAW, |
fac7db6c | 109 | REQ_SCREEN_RESIZE, |
4a2909a7 | 110 | REQ_SCREEN_UPDATE, |
03a93dbb JF |
111 | REQ_SHOW_VERSION, |
112 | REQ_STOP_LOADING, | |
4a2909a7 | 113 | REQ_TOGGLE_LINE_NUMBERS, |
03a93dbb | 114 | REQ_VIEW_NEXT, |
4f9b667a | 115 | REQ_VIEW_CLOSE, |
4a2909a7 JF |
116 | |
117 | REQ_MOVE_UP, | |
6706b2ba | 118 | REQ_MOVE_UP_ENTER, |
4a2909a7 | 119 | REQ_MOVE_DOWN, |
6706b2ba | 120 | REQ_MOVE_DOWN_ENTER, |
4a2909a7 JF |
121 | REQ_MOVE_PAGE_UP, |
122 | REQ_MOVE_PAGE_DOWN, | |
123 | REQ_MOVE_FIRST_LINE, | |
124 | REQ_MOVE_LAST_LINE, | |
125 | ||
126 | REQ_SCROLL_LINE_UP, | |
127 | REQ_SCROLL_LINE_DOWN, | |
128 | REQ_SCROLL_PAGE_UP, | |
129 | REQ_SCROLL_PAGE_DOWN, | |
130 | }; | |
131 | ||
c34d9c9f | 132 | struct ref { |
468876c9 JF |
133 | char *name; /* Ref name; tag or head names are shortened. */ |
134 | char id[41]; /* Commit SHA1 ID */ | |
135 | unsigned int tag:1; /* Is it a tag? */ | |
136 | unsigned int next:1; /* For ref lists: are there more refs? */ | |
c34d9c9f JF |
137 | }; |
138 | ||
4c6fabc2 | 139 | struct commit { |
a28bcc22 | 140 | char id[41]; /* SHA1 ID. */ |
6b161b31 | 141 | char title[75]; /* The first line of the commit message. */ |
a28bcc22 JF |
142 | char author[75]; /* The author of the commit. */ |
143 | struct tm time; /* Date from the author ident. */ | |
c34d9c9f | 144 | struct ref **refs; /* Repository references; tags & branch heads. */ |
4c6fabc2 JF |
145 | }; |
146 | ||
6706b2ba | 147 | |
03a93dbb JF |
148 | /* |
149 | * String helpers | |
150 | */ | |
78c70acd | 151 | |
82e78006 | 152 | static inline void |
4685845e | 153 | string_ncopy(char *dst, const char *src, int dstlen) |
82e78006 JF |
154 | { |
155 | strncpy(dst, src, dstlen - 1); | |
156 | dst[dstlen - 1] = 0; | |
03a93dbb | 157 | |
82e78006 JF |
158 | } |
159 | ||
160 | /* Shorthand for safely copying into a fixed buffer. */ | |
161 | #define string_copy(dst, src) \ | |
162 | string_ncopy(dst, src, sizeof(dst)) | |
163 | ||
6706b2ba | 164 | |
03a93dbb JF |
165 | /* Shell quoting |
166 | * | |
167 | * NOTE: The following is a slightly modified copy of the git project's shell | |
168 | * quoting routines found in the quote.c file. | |
169 | * | |
170 | * Help to copy the thing properly quoted for the shell safety. any single | |
171 | * quote is replaced with '\'', any exclamation point is replaced with '\!', | |
172 | * and the whole thing is enclosed in a | |
173 | * | |
174 | * E.g. | |
175 | * original sq_quote result | |
176 | * name ==> name ==> 'name' | |
177 | * a b ==> a b ==> 'a b' | |
178 | * a'b ==> a'\''b ==> 'a'\''b' | |
179 | * a!b ==> a'\!'b ==> 'a'\!'b' | |
180 | */ | |
181 | ||
182 | static size_t | |
183 | sq_quote(char buf[SIZEOF_CMD], size_t bufsize, const char *src) | |
184 | { | |
185 | char c; | |
186 | ||
a9274b95 | 187 | #define BUFPUT(x) do { if (bufsize < SIZEOF_CMD) buf[bufsize++] = (x); } while (0) |
03a93dbb JF |
188 | |
189 | BUFPUT('\''); | |
190 | while ((c = *src++)) { | |
191 | if (c == '\'' || c == '!') { | |
192 | BUFPUT('\''); | |
193 | BUFPUT('\\'); | |
194 | BUFPUT(c); | |
195 | BUFPUT('\''); | |
196 | } else { | |
197 | BUFPUT(c); | |
198 | } | |
199 | } | |
200 | BUFPUT('\''); | |
201 | ||
202 | return bufsize; | |
203 | } | |
204 | ||
82e78006 | 205 | |
b76c2afc JF |
206 | /** |
207 | * OPTIONS | |
208 | * ------- | |
209 | **/ | |
210 | ||
6706b2ba JF |
211 | /* Option and state variables. */ |
212 | static bool opt_line_number = FALSE; | |
2e8488b4 | 213 | static int opt_num_interval = NUMBER_INTERVAL; |
6706b2ba | 214 | static int opt_tab_size = TABSIZE; |
6b161b31 | 215 | static enum request opt_request = REQ_VIEW_MAIN; |
03a93dbb | 216 | static char opt_cmd[SIZEOF_CMD] = ""; |
6908bdbd | 217 | static FILE *opt_pipe = NULL; |
b76c2afc | 218 | |
b76c2afc | 219 | /* Returns the index of log or diff command or -1 to exit. */ |
8855ada4 | 220 | static bool |
b76c2afc JF |
221 | parse_options(int argc, char *argv[]) |
222 | { | |
223 | int i; | |
224 | ||
225 | for (i = 1; i < argc; i++) { | |
226 | char *opt = argv[i]; | |
227 | ||
228 | /** | |
b76c2afc | 229 | * -l:: |
1ba2ae4b | 230 | * Start up in log view using the internal log command. |
b76c2afc | 231 | **/ |
6b161b31 | 232 | if (!strcmp(opt, "-l")) { |
4a2909a7 | 233 | opt_request = REQ_VIEW_LOG; |
6b161b31 JF |
234 | continue; |
235 | } | |
b76c2afc JF |
236 | |
237 | /** | |
238 | * -d:: | |
1ba2ae4b | 239 | * Start up in diff view using the internal diff command. |
b76c2afc | 240 | **/ |
6b161b31 | 241 | if (!strcmp(opt, "-d")) { |
4a2909a7 | 242 | opt_request = REQ_VIEW_DIFF; |
6b161b31 JF |
243 | continue; |
244 | } | |
b76c2afc JF |
245 | |
246 | /** | |
2e8488b4 | 247 | * -n[INTERVAL], --line-number[=INTERVAL]:: |
b76c2afc | 248 | * Prefix line numbers in log and diff view. |
2e8488b4 | 249 | * Optionally, with interval different than each line. |
b76c2afc | 250 | **/ |
6b161b31 | 251 | if (!strncmp(opt, "-n", 2) || |
8855ada4 | 252 | !strncmp(opt, "--line-number", 13)) { |
2e8488b4 JF |
253 | char *num = opt; |
254 | ||
255 | if (opt[1] == 'n') { | |
256 | num = opt + 2; | |
257 | ||
258 | } else if (opt[STRING_SIZE("--line-number")] == '=') { | |
259 | num = opt + STRING_SIZE("--line-number="); | |
260 | } | |
261 | ||
262 | if (isdigit(*num)) | |
263 | opt_num_interval = atoi(num); | |
264 | ||
6b161b31 JF |
265 | opt_line_number = TRUE; |
266 | continue; | |
267 | } | |
b76c2afc JF |
268 | |
269 | /** | |
6706b2ba JF |
270 | * -t[NSPACES], --tab-size[=NSPACES]:: |
271 | * Set the number of spaces tabs should be expanded to. | |
272 | **/ | |
273 | if (!strncmp(opt, "-t", 2) || | |
274 | !strncmp(opt, "--tab-size", 10)) { | |
275 | char *num = opt; | |
276 | ||
277 | if (opt[1] == 't') { | |
278 | num = opt + 2; | |
279 | ||
280 | } else if (opt[STRING_SIZE("--tab-size")] == '=') { | |
281 | num = opt + STRING_SIZE("--tab-size="); | |
282 | } | |
283 | ||
284 | if (isdigit(*num)) | |
285 | opt_tab_size = MIN(atoi(num), TABSIZE); | |
286 | continue; | |
287 | } | |
288 | ||
289 | /** | |
b76c2afc JF |
290 | * -v, --version:: |
291 | * Show version and exit. | |
292 | **/ | |
6b161b31 | 293 | if (!strcmp(opt, "-v") || |
8855ada4 | 294 | !strcmp(opt, "--version")) { |
b76c2afc | 295 | printf("tig version %s\n", VERSION); |
8855ada4 | 296 | return FALSE; |
6b161b31 | 297 | } |
b76c2afc JF |
298 | |
299 | /** | |
03a93dbb | 300 | * \--:: |
0721c53a | 301 | * End of tig(1) options. Useful when specifying command |
d44f34bf | 302 | * options for the main view. Example: |
03a93dbb | 303 | * |
889cb462 | 304 | * $ tig -- --since=1.month |
b76c2afc | 305 | **/ |
6908bdbd JF |
306 | if (!strcmp(opt, "--")) { |
307 | i++; | |
308 | break; | |
309 | } | |
03a93dbb | 310 | |
8855ada4 | 311 | /** |
d44f34bf | 312 | * log [git log options]:: |
8855ada4 JF |
313 | * Open log view using the given git log options. |
314 | * | |
d44f34bf | 315 | * diff [git diff options]:: |
8855ada4 JF |
316 | * Open diff view using the given git diff options. |
317 | * | |
d44f34bf | 318 | * show [git show options]:: |
8855ada4 JF |
319 | * Open diff view using the given git show options. |
320 | **/ | |
321 | if (!strcmp(opt, "log") || | |
322 | !strcmp(opt, "diff") || | |
323 | !strcmp(opt, "show")) { | |
324 | opt_request = opt[0] == 'l' | |
325 | ? REQ_VIEW_LOG : REQ_VIEW_DIFF; | |
326 | break; | |
327 | } | |
328 | ||
d44f34bf | 329 | /** |
57bdf034 | 330 | * [git log options]:: |
d44f34bf JF |
331 | * tig(1) will stop the option parsing when the first |
332 | * command line parameter not starting with "-" is | |
57bdf034 JF |
333 | * encountered. All options including this one will be |
334 | * passed to git log when loading the main view. | |
335 | * This makes it possible to say: | |
889cb462 | 336 | * |
03a93dbb | 337 | * $ tig tag-1.0..HEAD |
d44f34bf | 338 | **/ |
03a93dbb | 339 | if (opt[0] && opt[0] != '-') |
6908bdbd | 340 | break; |
6b161b31 JF |
341 | |
342 | die("unknown command '%s'", opt); | |
b76c2afc JF |
343 | } |
344 | ||
6908bdbd | 345 | if (!isatty(STDIN_FILENO)) { |
8855ada4 JF |
346 | /** |
347 | * Pager mode | |
348 | * ~~~~~~~~~~ | |
349 | * If stdin is a pipe, any log or diff options will be ignored and the | |
350 | * pager view will be opened loading data from stdin. The pager mode | |
351 | * can be used for colorizing output from various git commands. | |
352 | * | |
353 | * Example on how to colorize the output of git-show(1): | |
354 | * | |
355 | * $ git show | tig | |
356 | **/ | |
6908bdbd JF |
357 | opt_request = REQ_VIEW_PAGER; |
358 | opt_pipe = stdin; | |
359 | ||
360 | } else if (i < argc) { | |
361 | size_t buf_size; | |
362 | ||
8855ada4 JF |
363 | /** |
364 | * Git command options | |
365 | * ~~~~~~~~~~~~~~~~~~~ | |
8855ada4 JF |
366 | * All git command options specified on the command line will |
367 | * be passed to the given command and all will be shell quoted | |
468876c9 | 368 | * before they are passed to the shell. |
8855ada4 | 369 | * |
468876c9 JF |
370 | * NOTE: If you specify options for the main view, you should |
371 | * not use the `--pretty` option as this option will be set | |
372 | * automatically to the format expected by the main view. | |
8855ada4 JF |
373 | * |
374 | * Example on how to open the log view and show both author and | |
375 | * committer information: | |
376 | * | |
377 | * $ tig log --pretty=fuller | |
468876c9 | 378 | * |
0721c53a JF |
379 | * See the <<refspec, "Specifying revisions">> section below |
380 | * for an introduction to revision options supported by the git | |
381 | * commands. For details on specific git command options, refer | |
382 | * to the man page of the command in question. | |
8855ada4 JF |
383 | **/ |
384 | ||
6908bdbd | 385 | if (opt_request == REQ_VIEW_MAIN) |
8855ada4 JF |
386 | /* XXX: This is vulnerable to the user overriding |
387 | * options required for the main view parser. */ | |
6908bdbd JF |
388 | string_copy(opt_cmd, "git log --stat --pretty=raw"); |
389 | else | |
390 | string_copy(opt_cmd, "git"); | |
391 | buf_size = strlen(opt_cmd); | |
392 | ||
393 | while (buf_size < sizeof(opt_cmd) && i < argc) { | |
394 | opt_cmd[buf_size++] = ' '; | |
395 | buf_size = sq_quote(opt_cmd, buf_size, argv[i++]); | |
396 | } | |
397 | ||
398 | if (buf_size >= sizeof(opt_cmd)) | |
399 | die("command too long"); | |
400 | ||
401 | opt_cmd[buf_size] = 0; | |
402 | ||
403 | } | |
404 | ||
8855ada4 | 405 | return TRUE; |
b76c2afc JF |
406 | } |
407 | ||
408 | ||
409 | /* | |
410 | * Line-oriented content detection. | |
6b161b31 JF |
411 | */ |
412 | ||
2e8488b4 | 413 | #define LINE_INFO \ |
6b161b31 JF |
414 | /* Line type String to match Foreground Background Attributes |
415 | * --------- --------------- ---------- ---------- ---------- */ \ | |
a28bcc22 JF |
416 | /* Diff markup */ \ |
417 | LINE(DIFF, "diff --git ", COLOR_YELLOW, COLOR_DEFAULT, 0), \ | |
8855ada4 | 418 | LINE(DIFF_INDEX, "index ", COLOR_BLUE, COLOR_DEFAULT, 0), \ |
a28bcc22 JF |
419 | LINE(DIFF_CHUNK, "@@", COLOR_MAGENTA, COLOR_DEFAULT, 0), \ |
420 | LINE(DIFF_ADD, "+", COLOR_GREEN, COLOR_DEFAULT, 0), \ | |
421 | LINE(DIFF_DEL, "-", COLOR_RED, COLOR_DEFAULT, 0), \ | |
3af8774e JF |
422 | LINE(DIFF_OLDMODE, "old file mode ", COLOR_YELLOW, COLOR_DEFAULT, 0), \ |
423 | LINE(DIFF_NEWMODE, "new file mode ", COLOR_YELLOW, COLOR_DEFAULT, 0), \ | |
a28bcc22 JF |
424 | LINE(DIFF_COPY, "copy ", COLOR_YELLOW, COLOR_DEFAULT, 0), \ |
425 | LINE(DIFF_RENAME, "rename ", COLOR_YELLOW, COLOR_DEFAULT, 0), \ | |
426 | LINE(DIFF_SIM, "similarity ", COLOR_YELLOW, COLOR_DEFAULT, 0), \ | |
427 | LINE(DIFF_DISSIM, "dissimilarity ", COLOR_YELLOW, COLOR_DEFAULT, 0), \ | |
428 | /* Pretty print commit header */ \ | |
6908bdbd | 429 | LINE(PP_AUTHOR, "Author: ", COLOR_CYAN, COLOR_DEFAULT, 0), \ |
8855ada4 | 430 | LINE(PP_COMMIT, "Commit: ", COLOR_MAGENTA, COLOR_DEFAULT, 0), \ |
6908bdbd JF |
431 | LINE(PP_MERGE, "Merge: ", COLOR_BLUE, COLOR_DEFAULT, 0), \ |
432 | LINE(PP_DATE, "Date: ", COLOR_YELLOW, COLOR_DEFAULT, 0), \ | |
8855ada4 JF |
433 | LINE(PP_ADATE, "AuthorDate: ", COLOR_YELLOW, COLOR_DEFAULT, 0), \ |
434 | LINE(PP_CDATE, "CommitDate: ", COLOR_YELLOW, COLOR_DEFAULT, 0), \ | |
a28bcc22 JF |
435 | /* Raw commit header */ \ |
436 | LINE(COMMIT, "commit ", COLOR_GREEN, COLOR_DEFAULT, 0), \ | |
437 | LINE(PARENT, "parent ", COLOR_BLUE, COLOR_DEFAULT, 0), \ | |
438 | LINE(TREE, "tree ", COLOR_BLUE, COLOR_DEFAULT, 0), \ | |
8855ada4 | 439 | LINE(AUTHOR, "author ", COLOR_CYAN, COLOR_DEFAULT, 0), \ |
a28bcc22 JF |
440 | LINE(COMMITTER, "committer ", COLOR_MAGENTA, COLOR_DEFAULT, 0), \ |
441 | /* Misc */ \ | |
442 | LINE(DIFF_TREE, "diff-tree ", COLOR_BLUE, COLOR_DEFAULT, 0), \ | |
443 | LINE(SIGNOFF, " Signed-off-by", COLOR_YELLOW, COLOR_DEFAULT, 0), \ | |
444 | /* UI colors */ \ | |
445 | LINE(DEFAULT, "", COLOR_DEFAULT, COLOR_DEFAULT, A_NORMAL), \ | |
446 | LINE(CURSOR, "", COLOR_WHITE, COLOR_GREEN, A_BOLD), \ | |
447 | LINE(STATUS, "", COLOR_GREEN, COLOR_DEFAULT, 0), \ | |
6b161b31 JF |
448 | LINE(TITLE_BLUR, "", COLOR_WHITE, COLOR_BLUE, 0), \ |
449 | LINE(TITLE_FOCUS, "", COLOR_WHITE, COLOR_BLUE, A_BOLD), \ | |
a28bcc22 JF |
450 | LINE(MAIN_DATE, "", COLOR_BLUE, COLOR_DEFAULT, 0), \ |
451 | LINE(MAIN_AUTHOR, "", COLOR_GREEN, COLOR_DEFAULT, 0), \ | |
452 | LINE(MAIN_COMMIT, "", COLOR_DEFAULT, COLOR_DEFAULT, 0), \ | |
c34d9c9f JF |
453 | LINE(MAIN_DELIM, "", COLOR_MAGENTA, COLOR_DEFAULT, 0), \ |
454 | LINE(MAIN_TAG, "", COLOR_MAGENTA, COLOR_DEFAULT, A_BOLD), \ | |
455 | LINE(MAIN_REF, "", COLOR_CYAN, COLOR_DEFAULT, A_BOLD), | |
2e8488b4 | 456 | |
78c70acd | 457 | enum line_type { |
2e8488b4 JF |
458 | #define LINE(type, line, fg, bg, attr) \ |
459 | LINE_##type | |
460 | LINE_INFO | |
461 | #undef LINE | |
78c70acd JF |
462 | }; |
463 | ||
464 | struct line_info { | |
4685845e | 465 | const char *line; /* The start of line to match. */ |
2e8488b4 JF |
466 | int linelen; /* Size of string to match. */ |
467 | int fg, bg, attr; /* Color and text attributes for the lines. */ | |
78c70acd JF |
468 | }; |
469 | ||
2e8488b4 | 470 | static struct line_info line_info[] = { |
78c70acd | 471 | #define LINE(type, line, fg, bg, attr) \ |
a28bcc22 | 472 | { (line), STRING_SIZE(line), (fg), (bg), (attr) } |
2e8488b4 JF |
473 | LINE_INFO |
474 | #undef LINE | |
78c70acd JF |
475 | }; |
476 | ||
2e8488b4 JF |
477 | static enum line_type |
478 | get_line_type(char *line) | |
78c70acd JF |
479 | { |
480 | int linelen = strlen(line); | |
a28bcc22 | 481 | enum line_type type; |
78c70acd | 482 | |
a28bcc22 | 483 | for (type = 0; type < ARRAY_SIZE(line_info); type++) |
2e8488b4 | 484 | /* Case insensitive search matches Signed-off-by lines better. */ |
a28bcc22 JF |
485 | if (linelen >= line_info[type].linelen && |
486 | !strncasecmp(line_info[type].line, line, line_info[type].linelen)) | |
487 | return type; | |
78c70acd | 488 | |
2e8488b4 | 489 | return LINE_DEFAULT; |
78c70acd JF |
490 | } |
491 | ||
2e8488b4 | 492 | static inline int |
78c70acd JF |
493 | get_line_attr(enum line_type type) |
494 | { | |
2e8488b4 JF |
495 | assert(type < ARRAY_SIZE(line_info)); |
496 | return COLOR_PAIR(type) | line_info[type].attr; | |
78c70acd JF |
497 | } |
498 | ||
499 | static void | |
500 | init_colors(void) | |
501 | { | |
82e78006 JF |
502 | int default_bg = COLOR_BLACK; |
503 | int default_fg = COLOR_WHITE; | |
a28bcc22 | 504 | enum line_type type; |
78c70acd JF |
505 | |
506 | start_color(); | |
507 | ||
508 | if (use_default_colors() != ERR) { | |
82e78006 JF |
509 | default_bg = -1; |
510 | default_fg = -1; | |
78c70acd JF |
511 | } |
512 | ||
a28bcc22 JF |
513 | for (type = 0; type < ARRAY_SIZE(line_info); type++) { |
514 | struct line_info *info = &line_info[type]; | |
82e78006 JF |
515 | int bg = info->bg == COLOR_DEFAULT ? default_bg : info->bg; |
516 | int fg = info->fg == COLOR_DEFAULT ? default_fg : info->fg; | |
78c70acd | 517 | |
a28bcc22 | 518 | init_pair(type, fg, bg); |
78c70acd JF |
519 | } |
520 | } | |
521 | ||
522 | ||
1ba2ae4b JF |
523 | /** |
524 | * ENVIRONMENT VARIABLES | |
525 | * --------------------- | |
c34d9c9f JF |
526 | * Several options related to the interface with git can be configured |
527 | * via environment options. | |
528 | * | |
529 | * Repository references | |
530 | * ~~~~~~~~~~~~~~~~~~~~~ | |
531 | * Commits that are referenced by tags and branch heads will be marked | |
532 | * by the reference name surrounded by '[' and ']': | |
533 | * | |
468876c9 | 534 | * 2006-03-26 19:42 Petr Baudis | [cogito-0.17.1] Cogito 0.17.1 |
c34d9c9f JF |
535 | * |
536 | * If you want to filter out certain directories under `.git/refs/`, say | |
537 | * `tmp` you can do it by setting the following variable: | |
538 | * | |
6706b2ba | 539 | * $ TIG_LS_REMOTE="git ls-remote . | sed /\/tmp\//d" tig |
c34d9c9f JF |
540 | * |
541 | * Or set the variable permanently in your environment. | |
542 | * | |
543 | * TIG_LS_REMOTE:: | |
6706b2ba JF |
544 | * Set command for retrieving all repository references. The command |
545 | * should output data in the same format as git-ls-remote(1). | |
c34d9c9f JF |
546 | **/ |
547 | ||
548 | #define TIG_LS_REMOTE \ | |
1b4b0bd9 | 549 | "git ls-remote . 2>/dev/null" |
c34d9c9f JF |
550 | |
551 | /** | |
0721c53a | 552 | * [[view-commands]] |
c34d9c9f JF |
553 | * View commands |
554 | * ~~~~~~~~~~~~~ | |
1ba2ae4b | 555 | * It is possible to alter which commands are used for the different views. |
6706b2ba JF |
556 | * If for example you prefer commits in the main view to be sorted by date |
557 | * and only show 500 commits, use: | |
1ba2ae4b JF |
558 | * |
559 | * $ TIG_MAIN_CMD="git log --date-order -n500 --pretty=raw %s" tig | |
560 | * | |
561 | * Or set the variable permanently in your environment. | |
562 | * | |
563 | * Notice, how `%s` is used to specify the commit reference. There can | |
564 | * be a maximum of 5 `%s` ref specifications. | |
565 | * | |
566 | * TIG_DIFF_CMD:: | |
567 | * The command used for the diff view. By default, git show is used | |
568 | * as a backend. | |
569 | * | |
570 | * TIG_LOG_CMD:: | |
6706b2ba JF |
571 | * The command used for the log view. If you prefer to have both |
572 | * author and committer shown in the log view be sure to pass | |
573 | * `--pretty=fuller` to git log. | |
1ba2ae4b JF |
574 | * |
575 | * TIG_MAIN_CMD:: | |
576 | * The command used for the main view. Note, you must always specify | |
577 | * the option: `--pretty=raw` since the main view parser expects to | |
578 | * read that format. | |
579 | **/ | |
580 | ||
581 | #define TIG_DIFF_CMD \ | |
582 | "git show --patch-with-stat --find-copies-harder -B -C %s" | |
583 | ||
584 | #define TIG_LOG_CMD \ | |
585 | "git log --cc --stat -n100 %s" | |
586 | ||
587 | #define TIG_MAIN_CMD \ | |
588 | "git log --topo-order --stat --pretty=raw %s" | |
589 | ||
0721c53a | 590 | /* ... silently ignore that the following are also exported. */ |
1ba2ae4b JF |
591 | |
592 | #define TIG_HELP_CMD \ | |
1b4b0bd9 | 593 | "man tig 2>/dev/null" |
1ba2ae4b JF |
594 | |
595 | #define TIG_PAGER_CMD \ | |
596 | "" | |
597 | ||
c2124ccd | 598 | |
468876c9 JF |
599 | /** |
600 | * The viewer | |
601 | * ---------- | |
c2124ccd JF |
602 | * The display consists of a status window on the last line of the screen and |
603 | * one or more views. The default is to only show one view at the time but it | |
604 | * is possible to split both the main and log view to also show the commit | |
605 | * diff. | |
468876c9 | 606 | * |
c2124ccd JF |
607 | * If you are in the log view and press 'Enter' when the current line is a |
608 | * commit line, such as: | |
468876c9 | 609 | * |
c2124ccd | 610 | * commit 4d55caff4cc89335192f3e566004b4ceef572521 |
468876c9 | 611 | * |
c2124ccd JF |
612 | * You will split the view so that the log view is displayed in the top window |
613 | * and the diff view in the bottom window. You can switch between the two | |
614 | * views by pressing 'Tab'. To maximize the log view again, simply press 'l'. | |
615 | **/ | |
616 | ||
617 | struct view; | |
618 | ||
619 | /* The display array of active views and the index of the current view. */ | |
620 | static struct view *display[2]; | |
621 | static unsigned int current_view; | |
622 | ||
623 | #define foreach_view(view, i) \ | |
624 | for (i = 0; i < ARRAY_SIZE(display) && (view = display[i]); i++) | |
625 | ||
626 | ||
627 | /** | |
628 | * Current head and commit ID | |
629 | * ~~~~~~~~~~~~~~~~~~~~~~~~~~ | |
630 | * The viewer keeps track of both what head and commit ID you are currently | |
631 | * viewing. The commit ID will follow the cursor line and change everytime time | |
632 | * you highlight a different commit. Whenever you reopen the diff view it | |
633 | * will be reloaded, if the commit ID changed. | |
468876c9 | 634 | * |
c2124ccd JF |
635 | * The head ID is used when opening the main and log view to indicate from |
636 | * what revision to show history. | |
468876c9 | 637 | **/ |
b801d8b2 | 638 | |
c2124ccd JF |
639 | static char ref_commit[SIZEOF_REF] = "HEAD"; |
640 | static char ref_head[SIZEOF_REF] = "HEAD"; | |
641 | ||
642 | ||
b801d8b2 | 643 | struct view { |
03a93dbb | 644 | const char *name; /* View name */ |
4685845e TH |
645 | const char *cmd_fmt; /* Default command line format */ |
646 | const char *cmd_env; /* Command line set via environment */ | |
647 | const char *id; /* Points to either of ref_{head,commit} */ | |
03a93dbb | 648 | size_t objsize; /* Size of objects in the line index */ |
6b161b31 JF |
649 | |
650 | struct view_ops { | |
6734f6b9 JF |
651 | /* What type of content being displayed. Used in the |
652 | * title bar. */ | |
4685845e | 653 | const char *type; |
8855ada4 | 654 | /* Draw one line; @lineno must be < view->height. */ |
6b161b31 | 655 | bool (*draw)(struct view *view, unsigned int lineno); |
8855ada4 | 656 | /* Read one line; updates view->line. */ |
6b161b31 | 657 | bool (*read)(struct view *view, char *line); |
8855ada4 | 658 | /* Depending on view, change display based on current line. */ |
6b161b31 JF |
659 | bool (*enter)(struct view *view); |
660 | } *ops; | |
22f66b0a | 661 | |
03a93dbb | 662 | char cmd[SIZEOF_CMD]; /* Command buffer */ |
49f2b43f JF |
663 | char ref[SIZEOF_REF]; /* Hovered commit reference */ |
664 | char vid[SIZEOF_REF]; /* View ID. Set to id member when updating. */ | |
2e8488b4 | 665 | |
8855ada4 JF |
666 | int height, width; /* The width and height of the main window */ |
667 | WINDOW *win; /* The main window */ | |
668 | WINDOW *title; /* The title window living below the main window */ | |
b801d8b2 JF |
669 | |
670 | /* Navigation */ | |
671 | unsigned long offset; /* Offset of the window top */ | |
672 | unsigned long lineno; /* Current line number */ | |
673 | ||
674 | /* Buffering */ | |
675 | unsigned long lines; /* Total number of lines */ | |
8855ada4 JF |
676 | void **line; /* Line index; each line contains user data */ |
677 | unsigned int digits; /* Number of digits in the lines member. */ | |
b801d8b2 JF |
678 | |
679 | /* Loading */ | |
680 | FILE *pipe; | |
2e8488b4 | 681 | time_t start_time; |
b801d8b2 JF |
682 | }; |
683 | ||
6b161b31 JF |
684 | static struct view_ops pager_ops; |
685 | static struct view_ops main_ops; | |
a28bcc22 | 686 | |
1ba2ae4b JF |
687 | #define VIEW_STR(name, cmd, env, ref, objsize, ops) \ |
688 | { name, cmd, #env, ref, objsize, ops } | |
689 | ||
690 | #define VIEW_(id, name, ops, ref, objsize) \ | |
691 | VIEW_STR(name, TIG_##id##_CMD, TIG_##id##_CMD, ref, objsize, ops) | |
692 | ||
c2124ccd JF |
693 | /** |
694 | * Views | |
695 | * ~~~~~ | |
696 | * tig(1) presents various 'views' of a repository. Each view is based on output | |
697 | * from an external command, most often 'git log', 'git diff', or 'git show'. | |
698 | * | |
699 | * The main view:: | |
700 | * Is the default view, and it shows a one line summary of each commit | |
701 | * in the chosen list of revisions. The summary includes commit date, | |
702 | * author, and the first line of the log message. Additionally, any | |
703 | * repository references, such as tags, will be shown. | |
704 | * | |
705 | * The log view:: | |
706 | * Presents a more rich view of the revision log showing the whole log | |
707 | * message and the diffstat. | |
708 | * | |
709 | * The diff view:: | |
710 | * Shows either the diff of the current working tree, that is, what | |
711 | * has changed since the last commit, or the commit diff complete | |
712 | * with log message, diffstat and diff. | |
713 | * | |
714 | * The pager view:: | |
715 | * Is used for displaying both input from stdin and output from git | |
716 | * commands entered in the internal prompt. | |
717 | * | |
718 | * The help view:: | |
719 | * Displays the information from the tig(1) man page. For the help view | |
720 | * to work you need to have the tig(1) man page installed. | |
721 | **/ | |
722 | ||
b801d8b2 | 723 | static struct view views[] = { |
1ba2ae4b JF |
724 | VIEW_(MAIN, "main", &main_ops, ref_head, sizeof(struct commit)), |
725 | VIEW_(DIFF, "diff", &pager_ops, ref_commit, sizeof(char)), | |
726 | VIEW_(LOG, "log", &pager_ops, ref_head, sizeof(char)), | |
c2124ccd | 727 | VIEW_(HELP, "help", &pager_ops, "static", sizeof(char)), |
1ba2ae4b | 728 | VIEW_(PAGER, "pager", &pager_ops, "static", sizeof(char)), |
b801d8b2 JF |
729 | }; |
730 | ||
a28bcc22 JF |
731 | #define VIEW(req) (&views[(req) - REQ_OFFSET - 1]) |
732 | ||
4c6fabc2 | 733 | |
b801d8b2 | 734 | static void |
82e78006 | 735 | redraw_view_from(struct view *view, int lineno) |
b801d8b2 | 736 | { |
82e78006 | 737 | assert(0 <= lineno && lineno < view->height); |
b801d8b2 | 738 | |
82e78006 | 739 | for (; lineno < view->height; lineno++) { |
6b161b31 | 740 | if (!view->ops->draw(view, lineno)) |
fd85fef1 | 741 | break; |
b801d8b2 JF |
742 | } |
743 | ||
744 | redrawwin(view->win); | |
745 | wrefresh(view->win); | |
746 | } | |
747 | ||
b76c2afc | 748 | static void |
82e78006 JF |
749 | redraw_view(struct view *view) |
750 | { | |
751 | wclear(view->win); | |
752 | redraw_view_from(view, 0); | |
753 | } | |
754 | ||
c2124ccd JF |
755 | |
756 | /** | |
757 | * Title windows | |
758 | * ~~~~~~~~~~~~~ | |
759 | * Each view has a title window which shows the name of the view, current | |
760 | * commit ID if available, and where the view is positioned: | |
761 | * | |
762 | * [main] c622eefaa485995320bc743431bae0d497b1d875 - commit 1 of 61 (1%) | |
763 | * | |
764 | * By default, the title of the current view is highlighted using bold font. | |
765 | **/ | |
766 | ||
6b161b31 | 767 | static void |
81030ec8 JF |
768 | update_view_title(struct view *view) |
769 | { | |
770 | if (view == display[current_view]) | |
771 | wbkgdset(view->title, get_line_attr(LINE_TITLE_FOCUS)); | |
772 | else | |
773 | wbkgdset(view->title, get_line_attr(LINE_TITLE_BLUR)); | |
774 | ||
775 | werase(view->title); | |
776 | wmove(view->title, 0, 0); | |
777 | ||
81030ec8 JF |
778 | if (*view->ref) |
779 | wprintw(view->title, "[%s] %s", view->name, view->ref); | |
780 | else | |
781 | wprintw(view->title, "[%s]", view->name); | |
782 | ||
783 | if (view->lines) { | |
784 | wprintw(view->title, " - %s %d of %d (%d%%)", | |
785 | view->ops->type, | |
786 | view->lineno + 1, | |
787 | view->lines, | |
788 | (view->lineno + 1) * 100 / view->lines); | |
789 | } | |
790 | ||
791 | wrefresh(view->title); | |
792 | } | |
793 | ||
794 | static void | |
6b161b31 | 795 | resize_display(void) |
b76c2afc | 796 | { |
03a93dbb | 797 | int offset, i; |
6b161b31 JF |
798 | struct view *base = display[0]; |
799 | struct view *view = display[1] ? display[1] : display[0]; | |
b76c2afc | 800 | |
6b161b31 | 801 | /* Setup window dimensions */ |
b76c2afc | 802 | |
03a93dbb | 803 | getmaxyx(stdscr, base->height, base->width); |
b76c2afc | 804 | |
6b161b31 | 805 | /* Make room for the status window. */ |
03a93dbb | 806 | base->height -= 1; |
6b161b31 JF |
807 | |
808 | if (view != base) { | |
03a93dbb JF |
809 | /* Horizontal split. */ |
810 | view->width = base->width; | |
6b161b31 JF |
811 | view->height = SCALE_SPLIT_VIEW(base->height); |
812 | base->height -= view->height; | |
813 | ||
814 | /* Make room for the title bar. */ | |
815 | view->height -= 1; | |
816 | } | |
817 | ||
818 | /* Make room for the title bar. */ | |
819 | base->height -= 1; | |
820 | ||
821 | offset = 0; | |
822 | ||
823 | foreach_view (view, i) { | |
4d55caff JF |
824 | /* Keep the height of all view->win windows one larger than is |
825 | * required so that the cursor can wrap-around on the last line | |
826 | * without scrolling the window. */ | |
b76c2afc | 827 | if (!view->win) { |
6706b2ba | 828 | view->win = newwin(view->height + 1, 0, offset, 0); |
6b161b31 JF |
829 | if (!view->win) |
830 | die("Failed to create %s view", view->name); | |
831 | ||
832 | scrollok(view->win, TRUE); | |
833 | ||
834 | view->title = newwin(1, 0, offset + view->height, 0); | |
835 | if (!view->title) | |
836 | die("Failed to create title window"); | |
837 | ||
838 | } else { | |
6706b2ba | 839 | wresize(view->win, view->height + 1, view->width); |
6b161b31 JF |
840 | mvwin(view->win, offset, 0); |
841 | mvwin(view->title, offset + view->height, 0); | |
842 | wrefresh(view->win); | |
a28bcc22 | 843 | } |
a28bcc22 | 844 | |
6b161b31 | 845 | offset += view->height + 1; |
b76c2afc | 846 | } |
6b161b31 | 847 | } |
b76c2afc | 848 | |
6b161b31 | 849 | static void |
20bb5e18 JF |
850 | redraw_display(void) |
851 | { | |
852 | struct view *view; | |
853 | int i; | |
854 | ||
855 | foreach_view (view, i) { | |
856 | redraw_view(view); | |
857 | update_view_title(view); | |
858 | } | |
859 | } | |
860 | ||
861 | ||
2e8488b4 JF |
862 | /* |
863 | * Navigation | |
864 | */ | |
865 | ||
4a2909a7 | 866 | /* Scrolling backend */ |
b801d8b2 | 867 | static void |
4a2909a7 | 868 | do_scroll_view(struct view *view, int lines) |
b801d8b2 | 869 | { |
fd85fef1 JF |
870 | /* The rendering expects the new offset. */ |
871 | view->offset += lines; | |
872 | ||
873 | assert(0 <= view->offset && view->offset < view->lines); | |
874 | assert(lines); | |
b801d8b2 | 875 | |
82e78006 | 876 | /* Redraw the whole screen if scrolling is pointless. */ |
4c6fabc2 | 877 | if (view->height < ABS(lines)) { |
b76c2afc JF |
878 | redraw_view(view); |
879 | ||
880 | } else { | |
22f66b0a | 881 | int line = lines > 0 ? view->height - lines : 0; |
82e78006 | 882 | int end = line + ABS(lines); |
fd85fef1 JF |
883 | |
884 | wscrl(view->win, lines); | |
885 | ||
22f66b0a | 886 | for (; line < end; line++) { |
6b161b31 | 887 | if (!view->ops->draw(view, line)) |
fd85fef1 JF |
888 | break; |
889 | } | |
890 | } | |
891 | ||
892 | /* Move current line into the view. */ | |
893 | if (view->lineno < view->offset) { | |
894 | view->lineno = view->offset; | |
6b161b31 | 895 | view->ops->draw(view, 0); |
fd85fef1 JF |
896 | |
897 | } else if (view->lineno >= view->offset + view->height) { | |
6706b2ba JF |
898 | if (view->lineno == view->offset + view->height) { |
899 | /* Clear the hidden line so it doesn't show if the view | |
900 | * is scrolled up. */ | |
901 | wmove(view->win, view->height, 0); | |
902 | wclrtoeol(view->win); | |
903 | } | |
fd85fef1 | 904 | view->lineno = view->offset + view->height - 1; |
6b161b31 | 905 | view->ops->draw(view, view->lineno - view->offset); |
fd85fef1 JF |
906 | } |
907 | ||
4c6fabc2 | 908 | assert(view->offset <= view->lineno && view->lineno < view->lines); |
fd85fef1 JF |
909 | |
910 | redrawwin(view->win); | |
911 | wrefresh(view->win); | |
9d3f5834 | 912 | report(""); |
fd85fef1 | 913 | } |
78c70acd | 914 | |
4a2909a7 | 915 | /* Scroll frontend */ |
fd85fef1 | 916 | static void |
6b161b31 | 917 | scroll_view(struct view *view, enum request request) |
fd85fef1 JF |
918 | { |
919 | int lines = 1; | |
b801d8b2 JF |
920 | |
921 | switch (request) { | |
4a2909a7 | 922 | case REQ_SCROLL_PAGE_DOWN: |
fd85fef1 | 923 | lines = view->height; |
4a2909a7 | 924 | case REQ_SCROLL_LINE_DOWN: |
b801d8b2 | 925 | if (view->offset + lines > view->lines) |
bde3653a | 926 | lines = view->lines - view->offset; |
b801d8b2 | 927 | |
fd85fef1 | 928 | if (lines == 0 || view->offset + view->height >= view->lines) { |
eb98559e | 929 | report("Cannot scroll beyond the last line"); |
b801d8b2 JF |
930 | return; |
931 | } | |
932 | break; | |
933 | ||
4a2909a7 | 934 | case REQ_SCROLL_PAGE_UP: |
fd85fef1 | 935 | lines = view->height; |
4a2909a7 | 936 | case REQ_SCROLL_LINE_UP: |
b801d8b2 JF |
937 | if (lines > view->offset) |
938 | lines = view->offset; | |
939 | ||
940 | if (lines == 0) { | |
eb98559e | 941 | report("Cannot scroll beyond the first line"); |
b801d8b2 JF |
942 | return; |
943 | } | |
944 | ||
fd85fef1 | 945 | lines = -lines; |
b801d8b2 | 946 | break; |
03a93dbb | 947 | |
6b161b31 JF |
948 | default: |
949 | die("request %d not handled in switch", request); | |
b801d8b2 JF |
950 | } |
951 | ||
4a2909a7 | 952 | do_scroll_view(view, lines); |
fd85fef1 | 953 | } |
b801d8b2 | 954 | |
4a2909a7 | 955 | /* Cursor moving */ |
fd85fef1 | 956 | static void |
6b161b31 | 957 | move_view(struct view *view, enum request request) |
fd85fef1 JF |
958 | { |
959 | int steps; | |
b801d8b2 | 960 | |
fd85fef1 | 961 | switch (request) { |
4a2909a7 | 962 | case REQ_MOVE_FIRST_LINE: |
78c70acd JF |
963 | steps = -view->lineno; |
964 | break; | |
965 | ||
4a2909a7 | 966 | case REQ_MOVE_LAST_LINE: |
78c70acd JF |
967 | steps = view->lines - view->lineno - 1; |
968 | break; | |
969 | ||
4a2909a7 | 970 | case REQ_MOVE_PAGE_UP: |
78c70acd JF |
971 | steps = view->height > view->lineno |
972 | ? -view->lineno : -view->height; | |
973 | break; | |
974 | ||
4a2909a7 | 975 | case REQ_MOVE_PAGE_DOWN: |
78c70acd JF |
976 | steps = view->lineno + view->height >= view->lines |
977 | ? view->lines - view->lineno - 1 : view->height; | |
978 | break; | |
979 | ||
4a2909a7 | 980 | case REQ_MOVE_UP: |
6706b2ba | 981 | case REQ_MOVE_UP_ENTER: |
fd85fef1 JF |
982 | steps = -1; |
983 | break; | |
b801d8b2 | 984 | |
4a2909a7 | 985 | case REQ_MOVE_DOWN: |
6706b2ba | 986 | case REQ_MOVE_DOWN_ENTER: |
fd85fef1 JF |
987 | steps = 1; |
988 | break; | |
6b161b31 JF |
989 | |
990 | default: | |
991 | die("request %d not handled in switch", request); | |
78c70acd | 992 | } |
b801d8b2 | 993 | |
4c6fabc2 | 994 | if (steps <= 0 && view->lineno == 0) { |
eb98559e | 995 | report("Cannot move beyond the first line"); |
78c70acd | 996 | return; |
b801d8b2 | 997 | |
6908bdbd | 998 | } else if (steps >= 0 && view->lineno + 1 >= view->lines) { |
eb98559e | 999 | report("Cannot move beyond the last line"); |
78c70acd | 1000 | return; |
fd85fef1 JF |
1001 | } |
1002 | ||
4c6fabc2 | 1003 | /* Move the current line */ |
fd85fef1 | 1004 | view->lineno += steps; |
4c6fabc2 JF |
1005 | assert(0 <= view->lineno && view->lineno < view->lines); |
1006 | ||
1007 | /* Repaint the old "current" line if we be scrolling */ | |
2e8488b4 JF |
1008 | if (ABS(steps) < view->height) { |
1009 | int prev_lineno = view->lineno - steps - view->offset; | |
1010 | ||
1011 | wmove(view->win, prev_lineno, 0); | |
1012 | wclrtoeol(view->win); | |
03a93dbb | 1013 | view->ops->draw(view, prev_lineno); |
2e8488b4 | 1014 | } |
fd85fef1 | 1015 | |
4c6fabc2 | 1016 | /* Check whether the view needs to be scrolled */ |
fd85fef1 JF |
1017 | if (view->lineno < view->offset || |
1018 | view->lineno >= view->offset + view->height) { | |
1019 | if (steps < 0 && -steps > view->offset) { | |
1020 | steps = -view->offset; | |
b76c2afc JF |
1021 | |
1022 | } else if (steps > 0) { | |
1023 | if (view->lineno == view->lines - 1 && | |
1024 | view->lines > view->height) { | |
1025 | steps = view->lines - view->offset - 1; | |
1026 | if (steps >= view->height) | |
1027 | steps -= view->height - 1; | |
1028 | } | |
b801d8b2 | 1029 | } |
78c70acd | 1030 | |
4a2909a7 | 1031 | do_scroll_view(view, steps); |
fd85fef1 | 1032 | return; |
b801d8b2 JF |
1033 | } |
1034 | ||
4c6fabc2 | 1035 | /* Draw the current line */ |
6b161b31 | 1036 | view->ops->draw(view, view->lineno - view->offset); |
fd85fef1 | 1037 | |
b801d8b2 JF |
1038 | redrawwin(view->win); |
1039 | wrefresh(view->win); | |
9d3f5834 | 1040 | report(""); |
b801d8b2 JF |
1041 | } |
1042 | ||
b801d8b2 | 1043 | |
2e8488b4 JF |
1044 | /* |
1045 | * Incremental updating | |
1046 | */ | |
b801d8b2 | 1047 | |
03a93dbb | 1048 | static bool |
b801d8b2 JF |
1049 | begin_update(struct view *view) |
1050 | { | |
4685845e | 1051 | const char *id = view->id; |
fd85fef1 | 1052 | |
03a93dbb JF |
1053 | if (opt_cmd[0]) { |
1054 | string_copy(view->cmd, opt_cmd); | |
1055 | opt_cmd[0] = 0; | |
8855ada4 JF |
1056 | /* When running random commands, the view ref could have become |
1057 | * invalid so clear it. */ | |
1058 | view->ref[0] = 0; | |
03a93dbb | 1059 | } else { |
4685845e | 1060 | const char *format = view->cmd_env ? view->cmd_env : view->cmd_fmt; |
1ba2ae4b JF |
1061 | |
1062 | if (snprintf(view->cmd, sizeof(view->cmd), format, | |
1063 | id, id, id, id, id) >= sizeof(view->cmd)) | |
03a93dbb JF |
1064 | return FALSE; |
1065 | } | |
b801d8b2 | 1066 | |
6908bdbd JF |
1067 | /* Special case for the pager view. */ |
1068 | if (opt_pipe) { | |
1069 | view->pipe = opt_pipe; | |
1070 | opt_pipe = NULL; | |
1071 | } else { | |
1072 | view->pipe = popen(view->cmd, "r"); | |
1073 | } | |
1074 | ||
2e8488b4 JF |
1075 | if (!view->pipe) |
1076 | return FALSE; | |
b801d8b2 | 1077 | |
6b161b31 | 1078 | set_nonblocking_input(TRUE); |
b801d8b2 JF |
1079 | |
1080 | view->offset = 0; | |
1081 | view->lines = 0; | |
1082 | view->lineno = 0; | |
49f2b43f | 1083 | string_copy(view->vid, id); |
b801d8b2 | 1084 | |
2e8488b4 JF |
1085 | if (view->line) { |
1086 | int i; | |
1087 | ||
1088 | for (i = 0; i < view->lines; i++) | |
1089 | if (view->line[i]) | |
1090 | free(view->line[i]); | |
1091 | ||
1092 | free(view->line); | |
1093 | view->line = NULL; | |
1094 | } | |
1095 | ||
1096 | view->start_time = time(NULL); | |
1097 | ||
b801d8b2 JF |
1098 | return TRUE; |
1099 | } | |
1100 | ||
1101 | static void | |
1102 | end_update(struct view *view) | |
1103 | { | |
03a93dbb JF |
1104 | if (!view->pipe) |
1105 | return; | |
6b161b31 | 1106 | set_nonblocking_input(FALSE); |
80ce96ea JF |
1107 | if (view->pipe == stdin) |
1108 | fclose(view->pipe); | |
1109 | else | |
1110 | pclose(view->pipe); | |
2e8488b4 | 1111 | view->pipe = NULL; |
b801d8b2 JF |
1112 | } |
1113 | ||
03a93dbb | 1114 | static bool |
b801d8b2 JF |
1115 | update_view(struct view *view) |
1116 | { | |
1117 | char buffer[BUFSIZ]; | |
1118 | char *line; | |
22f66b0a | 1119 | void **tmp; |
82e78006 JF |
1120 | /* The number of lines to read. If too low it will cause too much |
1121 | * redrawing (and possible flickering), if too high responsiveness | |
1122 | * will suffer. */ | |
8855ada4 | 1123 | unsigned long lines = view->height; |
82e78006 | 1124 | int redraw_from = -1; |
b801d8b2 JF |
1125 | |
1126 | if (!view->pipe) | |
1127 | return TRUE; | |
1128 | ||
82e78006 JF |
1129 | /* Only redraw if lines are visible. */ |
1130 | if (view->offset + view->height >= view->lines) | |
1131 | redraw_from = view->lines - view->offset; | |
b801d8b2 JF |
1132 | |
1133 | tmp = realloc(view->line, sizeof(*view->line) * (view->lines + lines)); | |
1134 | if (!tmp) | |
1135 | goto alloc_error; | |
1136 | ||
1137 | view->line = tmp; | |
1138 | ||
1139 | while ((line = fgets(buffer, sizeof(buffer), view->pipe))) { | |
c34d9c9f | 1140 | int linelen = strlen(line); |
b801d8b2 | 1141 | |
b801d8b2 JF |
1142 | if (linelen) |
1143 | line[linelen - 1] = 0; | |
1144 | ||
6b161b31 | 1145 | if (!view->ops->read(view, line)) |
b801d8b2 | 1146 | goto alloc_error; |
fd85fef1 JF |
1147 | |
1148 | if (lines-- == 1) | |
1149 | break; | |
b801d8b2 JF |
1150 | } |
1151 | ||
8855ada4 JF |
1152 | { |
1153 | int digits; | |
1154 | ||
1155 | lines = view->lines; | |
1156 | for (digits = 0; lines; digits++) | |
1157 | lines /= 10; | |
1158 | ||
1159 | /* Keep the displayed view in sync with line number scaling. */ | |
1160 | if (digits != view->digits) { | |
1161 | view->digits = digits; | |
1162 | redraw_from = 0; | |
1163 | } | |
1164 | } | |
1165 | ||
82e78006 JF |
1166 | if (redraw_from >= 0) { |
1167 | /* If this is an incremental update, redraw the previous line | |
a28bcc22 JF |
1168 | * since for commits some members could have changed when |
1169 | * loading the main view. */ | |
82e78006 JF |
1170 | if (redraw_from > 0) |
1171 | redraw_from--; | |
1172 | ||
1173 | /* Incrementally draw avoids flickering. */ | |
1174 | redraw_view_from(view, redraw_from); | |
4c6fabc2 | 1175 | } |
b801d8b2 | 1176 | |
eb98559e JF |
1177 | /* Update the title _after_ the redraw so that if the redraw picks up a |
1178 | * commit reference in view->ref it'll be available here. */ | |
1179 | update_view_title(view); | |
1180 | ||
b801d8b2 | 1181 | if (ferror(view->pipe)) { |
03a93dbb | 1182 | report("Failed to read: %s", strerror(errno)); |
b801d8b2 JF |
1183 | goto end; |
1184 | ||
1185 | } else if (feof(view->pipe)) { | |
2e8488b4 JF |
1186 | time_t secs = time(NULL) - view->start_time; |
1187 | ||
a28bcc22 | 1188 | if (view == VIEW(REQ_VIEW_HELP)) { |
4685845e | 1189 | const char *msg = TIG_HELP; |
468876c9 JF |
1190 | |
1191 | if (view->lines == 0) { | |
1192 | /* Slightly ugly, but abusing view->ref keeps | |
1193 | * the error message. */ | |
1194 | string_copy(view->ref, "No help available"); | |
1195 | msg = "The tig(1) manpage is not installed"; | |
1196 | } | |
1197 | ||
1198 | report("%s", msg); | |
2e8488b4 JF |
1199 | goto end; |
1200 | } | |
1201 | ||
1202 | report("Loaded %d lines in %ld second%s", view->lines, secs, | |
1203 | secs == 1 ? "" : "s"); | |
b801d8b2 JF |
1204 | goto end; |
1205 | } | |
1206 | ||
1207 | return TRUE; | |
1208 | ||
1209 | alloc_error: | |
2e8488b4 | 1210 | report("Allocation failure"); |
b801d8b2 JF |
1211 | |
1212 | end: | |
1213 | end_update(view); | |
1214 | return FALSE; | |
1215 | } | |
1216 | ||
49f2b43f JF |
1217 | enum open_flags { |
1218 | OPEN_DEFAULT = 0, /* Use default view switching. */ | |
1219 | OPEN_SPLIT = 1, /* Split current view. */ | |
1220 | OPEN_BACKGROUNDED = 2, /* Backgrounded. */ | |
1221 | OPEN_RELOAD = 4, /* Reload view even if it is the current. */ | |
1222 | }; | |
1223 | ||
6b161b31 | 1224 | static void |
49f2b43f | 1225 | open_view(struct view *prev, enum request request, enum open_flags flags) |
b801d8b2 | 1226 | { |
49f2b43f JF |
1227 | bool backgrounded = !!(flags & OPEN_BACKGROUNDED); |
1228 | bool split = !!(flags & OPEN_SPLIT); | |
1229 | bool reload = !!(flags & OPEN_RELOAD); | |
a28bcc22 | 1230 | struct view *view = VIEW(request); |
b801d8b2 | 1231 | struct view *displayed; |
6b161b31 JF |
1232 | int nviews; |
1233 | ||
03a93dbb | 1234 | /* Cycle between displayed views and count the views. */ |
6b161b31 | 1235 | foreach_view (displayed, nviews) { |
03a93dbb JF |
1236 | if (prev != view && |
1237 | view == displayed && | |
8855ada4 | 1238 | !strcmp(view->vid, prev->vid)) { |
6b161b31 JF |
1239 | current_view = nviews; |
1240 | /* Blur out the title of the previous view. */ | |
1241 | update_view_title(prev); | |
6734f6b9 | 1242 | report(""); |
6b161b31 | 1243 | return; |
a28bcc22 | 1244 | } |
6b161b31 | 1245 | } |
b801d8b2 | 1246 | |
49f2b43f | 1247 | if (view == prev && nviews == 1 && !reload) { |
6b161b31 JF |
1248 | report("Already in %s view", view->name); |
1249 | return; | |
1250 | } | |
b801d8b2 | 1251 | |
8855ada4 | 1252 | if ((reload || strcmp(view->vid, view->id)) && |
03a93dbb | 1253 | !begin_update(view)) { |
6b161b31 JF |
1254 | report("Failed to load %s view", view->name); |
1255 | return; | |
1256 | } | |
a28bcc22 | 1257 | |
6b161b31 JF |
1258 | if (split) { |
1259 | display[current_view + 1] = view; | |
1260 | if (!backgrounded) | |
a28bcc22 | 1261 | current_view++; |
6b161b31 JF |
1262 | } else { |
1263 | /* Maximize the current view. */ | |
1264 | memset(display, 0, sizeof(display)); | |
1265 | current_view = 0; | |
1266 | display[current_view] = view; | |
a28bcc22 | 1267 | } |
b801d8b2 | 1268 | |
6b161b31 | 1269 | resize_display(); |
b801d8b2 | 1270 | |
a8891802 | 1271 | if (split && prev->lineno - prev->offset >= prev->height) { |
03a93dbb | 1272 | /* Take the title line into account. */ |
eb98559e | 1273 | int lines = prev->lineno - prev->offset - prev->height + 1; |
03a93dbb JF |
1274 | |
1275 | /* Scroll the view that was split if the current line is | |
1276 | * outside the new limited view. */ | |
1277 | do_scroll_view(prev, lines); | |
1278 | } | |
1279 | ||
6b161b31 JF |
1280 | if (prev && view != prev) { |
1281 | /* "Blur" the previous view. */ | |
6706b2ba JF |
1282 | if (!backgrounded) |
1283 | update_view_title(prev); | |
6b161b31 JF |
1284 | |
1285 | /* Continue loading split views in the background. */ | |
03a93dbb | 1286 | if (!split) |
6b161b31 | 1287 | end_update(prev); |
b801d8b2 JF |
1288 | } |
1289 | ||
03a93dbb JF |
1290 | if (view->pipe) { |
1291 | /* Clear the old view and let the incremental updating refill | |
1292 | * the screen. */ | |
1293 | wclear(view->win); | |
1294 | report("Loading..."); | |
1295 | } else { | |
1296 | redraw_view(view); | |
468876c9 JF |
1297 | if (view == VIEW(REQ_VIEW_HELP)) |
1298 | report("%s", TIG_HELP); | |
1299 | else | |
1300 | report(""); | |
03a93dbb | 1301 | } |
6706b2ba JF |
1302 | |
1303 | /* If the view is backgrounded the above calls to report() | |
1304 | * won't redraw the view title. */ | |
1305 | if (backgrounded) | |
1306 | update_view_title(view); | |
b801d8b2 JF |
1307 | } |
1308 | ||
1309 | ||
6b161b31 JF |
1310 | /* |
1311 | * User request switch noodle | |
1312 | */ | |
1313 | ||
b801d8b2 | 1314 | static int |
6b161b31 | 1315 | view_driver(struct view *view, enum request request) |
b801d8b2 | 1316 | { |
b801d8b2 JF |
1317 | int i; |
1318 | ||
1319 | switch (request) { | |
4a2909a7 JF |
1320 | case REQ_MOVE_UP: |
1321 | case REQ_MOVE_DOWN: | |
1322 | case REQ_MOVE_PAGE_UP: | |
1323 | case REQ_MOVE_PAGE_DOWN: | |
1324 | case REQ_MOVE_FIRST_LINE: | |
1325 | case REQ_MOVE_LAST_LINE: | |
a28bcc22 | 1326 | move_view(view, request); |
fd85fef1 JF |
1327 | break; |
1328 | ||
4a2909a7 JF |
1329 | case REQ_SCROLL_LINE_DOWN: |
1330 | case REQ_SCROLL_LINE_UP: | |
1331 | case REQ_SCROLL_PAGE_DOWN: | |
1332 | case REQ_SCROLL_PAGE_UP: | |
a28bcc22 | 1333 | scroll_view(view, request); |
b801d8b2 JF |
1334 | break; |
1335 | ||
4a2909a7 | 1336 | case REQ_VIEW_MAIN: |
4a2909a7 | 1337 | case REQ_VIEW_DIFF: |
2e8488b4 JF |
1338 | case REQ_VIEW_LOG: |
1339 | case REQ_VIEW_HELP: | |
6908bdbd | 1340 | case REQ_VIEW_PAGER: |
49f2b43f | 1341 | open_view(view, request, OPEN_DEFAULT); |
b801d8b2 JF |
1342 | break; |
1343 | ||
6706b2ba JF |
1344 | case REQ_MOVE_UP_ENTER: |
1345 | case REQ_MOVE_DOWN_ENTER: | |
1346 | move_view(view, request); | |
1347 | /* Fall-through */ | |
1348 | ||
6b161b31 | 1349 | case REQ_ENTER: |
6908bdbd JF |
1350 | if (!view->lines) { |
1351 | report("Nothing to enter"); | |
1352 | break; | |
1353 | } | |
6b161b31 JF |
1354 | return view->ops->enter(view); |
1355 | ||
03a93dbb JF |
1356 | case REQ_VIEW_NEXT: |
1357 | { | |
1358 | int nviews = display[1] ? 2 : 1; | |
1359 | int next_view = (current_view + 1) % nviews; | |
1360 | ||
1361 | if (next_view == current_view) { | |
1362 | report("Only one view is displayed"); | |
1363 | break; | |
1364 | } | |
1365 | ||
1366 | current_view = next_view; | |
1367 | /* Blur out the title of the previous view. */ | |
1368 | update_view_title(view); | |
6734f6b9 | 1369 | report(""); |
03a93dbb JF |
1370 | break; |
1371 | } | |
4a2909a7 | 1372 | case REQ_TOGGLE_LINE_NUMBERS: |
b76c2afc | 1373 | opt_line_number = !opt_line_number; |
20bb5e18 | 1374 | redraw_display(); |
b801d8b2 JF |
1375 | break; |
1376 | ||
03a93dbb | 1377 | case REQ_PROMPT: |
8855ada4 | 1378 | /* Always reload^Wrerun commands from the prompt. */ |
49f2b43f | 1379 | open_view(view, opt_request, OPEN_RELOAD); |
03a93dbb JF |
1380 | break; |
1381 | ||
4a2909a7 | 1382 | case REQ_STOP_LOADING: |
03a93dbb | 1383 | foreach_view (view, i) { |
2e8488b4 | 1384 | if (view->pipe) |
6706b2ba | 1385 | report("Stopped loaded the %s view", view->name), |
03a93dbb JF |
1386 | end_update(view); |
1387 | } | |
b801d8b2 JF |
1388 | break; |
1389 | ||
4a2909a7 | 1390 | case REQ_SHOW_VERSION: |
2e8488b4 | 1391 | report("Version: %s", VERSION); |
b801d8b2 JF |
1392 | return TRUE; |
1393 | ||
fac7db6c JF |
1394 | case REQ_SCREEN_RESIZE: |
1395 | resize_display(); | |
1396 | /* Fall-through */ | |
4a2909a7 | 1397 | case REQ_SCREEN_REDRAW: |
20bb5e18 | 1398 | redraw_display(); |
4a2909a7 JF |
1399 | break; |
1400 | ||
1401 | case REQ_SCREEN_UPDATE: | |
b801d8b2 JF |
1402 | doupdate(); |
1403 | return TRUE; | |
1404 | ||
4f9b667a JF |
1405 | case REQ_VIEW_CLOSE: |
1406 | if (display[1]) { | |
1407 | view = display[(current_view + 1) % ARRAY_SIZE(display)]; | |
1408 | memset(display, 0, sizeof(display)); | |
1409 | current_view = 0; | |
1410 | display[current_view] = view; | |
1411 | resize_display(); | |
1412 | redraw_display(); | |
1413 | break; | |
1414 | } | |
1415 | /* Fall-through */ | |
b801d8b2 JF |
1416 | case REQ_QUIT: |
1417 | return FALSE; | |
1418 | ||
1419 | default: | |
2e8488b4 | 1420 | /* An unknown key will show most commonly used commands. */ |
468876c9 | 1421 | report("Unknown key, press 'h' for help"); |
b801d8b2 JF |
1422 | return TRUE; |
1423 | } | |
1424 | ||
1425 | return TRUE; | |
1426 | } | |
1427 | ||
1428 | ||
1429 | /* | |
6b161b31 | 1430 | * View backend handlers |
b801d8b2 JF |
1431 | */ |
1432 | ||
6b161b31 | 1433 | static bool |
22f66b0a | 1434 | pager_draw(struct view *view, unsigned int lineno) |
b801d8b2 | 1435 | { |
78c70acd | 1436 | enum line_type type; |
b801d8b2 | 1437 | char *line; |
4c6fabc2 | 1438 | int linelen; |
78c70acd | 1439 | int attr; |
b801d8b2 | 1440 | |
fd85fef1 JF |
1441 | if (view->offset + lineno >= view->lines) |
1442 | return FALSE; | |
1443 | ||
b801d8b2 | 1444 | line = view->line[view->offset + lineno]; |
78c70acd | 1445 | type = get_line_type(line); |
b801d8b2 | 1446 | |
6706b2ba JF |
1447 | wmove(view->win, lineno, 0); |
1448 | ||
fd85fef1 | 1449 | if (view->offset + lineno == view->lineno) { |
8855ada4 | 1450 | if (type == LINE_COMMIT) { |
03a93dbb JF |
1451 | string_copy(view->ref, line + 7); |
1452 | string_copy(ref_commit, view->ref); | |
1453 | } | |
8855ada4 | 1454 | |
78c70acd | 1455 | type = LINE_CURSOR; |
6706b2ba | 1456 | wchgat(view->win, -1, 0, type, NULL); |
fd85fef1 JF |
1457 | } |
1458 | ||
78c70acd | 1459 | attr = get_line_attr(type); |
b801d8b2 | 1460 | wattrset(view->win, attr); |
b76c2afc | 1461 | |
4c6fabc2 | 1462 | linelen = strlen(line); |
4c6fabc2 | 1463 | |
6706b2ba JF |
1464 | if (opt_line_number || opt_tab_size < TABSIZE) { |
1465 | static char spaces[] = " "; | |
1466 | int col_offset = 0, col = 0; | |
1467 | ||
1468 | if (opt_line_number) { | |
1469 | unsigned long real_lineno = view->offset + lineno + 1; | |
82e78006 | 1470 | |
6706b2ba JF |
1471 | if (real_lineno == 1 || |
1472 | (real_lineno % opt_num_interval) == 0) { | |
1473 | wprintw(view->win, "%.*d", view->digits, real_lineno); | |
8855ada4 | 1474 | |
6706b2ba JF |
1475 | } else { |
1476 | waddnstr(view->win, spaces, | |
1477 | MIN(view->digits, STRING_SIZE(spaces))); | |
1478 | } | |
1479 | waddstr(view->win, ": "); | |
1480 | col_offset = view->digits + 2; | |
1481 | } | |
8855ada4 | 1482 | |
6706b2ba JF |
1483 | while (line && col_offset + col < view->width) { |
1484 | int cols_max = view->width - col_offset - col; | |
1485 | char *text = line; | |
1486 | int cols; | |
4c6fabc2 | 1487 | |
b76c2afc | 1488 | if (*line == '\t') { |
6706b2ba | 1489 | assert(sizeof(spaces) > TABSIZE); |
b76c2afc | 1490 | line++; |
6706b2ba JF |
1491 | text = spaces; |
1492 | cols = opt_tab_size - (col % opt_tab_size); | |
82e78006 | 1493 | |
b76c2afc | 1494 | } else { |
6706b2ba JF |
1495 | line = strchr(line, '\t'); |
1496 | cols = line ? line - text : strlen(text); | |
b76c2afc | 1497 | } |
6706b2ba JF |
1498 | |
1499 | waddnstr(view->win, text, MIN(cols, cols_max)); | |
1500 | col += cols; | |
b76c2afc | 1501 | } |
b76c2afc JF |
1502 | |
1503 | } else { | |
6706b2ba | 1504 | int col = 0, pos = 0; |
b801d8b2 | 1505 | |
6706b2ba JF |
1506 | for (; pos < linelen && col < view->width; pos++, col++) |
1507 | if (line[pos] == '\t') | |
1508 | col += TABSIZE - (col % TABSIZE) - 1; | |
1509 | ||
1510 | waddnstr(view->win, line, pos); | |
1511 | } | |
2e8488b4 | 1512 | |
b801d8b2 JF |
1513 | return TRUE; |
1514 | } | |
1515 | ||
6b161b31 | 1516 | static bool |
22f66b0a JF |
1517 | pager_read(struct view *view, char *line) |
1518 | { | |
6706b2ba JF |
1519 | /* Compress empty lines in the help view. */ |
1520 | if (view == VIEW(REQ_VIEW_HELP) && | |
1521 | !*line && | |
1522 | view->lines && | |
1523 | !*((char *) view->line[view->lines - 1])) | |
1524 | return TRUE; | |
1525 | ||
22f66b0a JF |
1526 | view->line[view->lines] = strdup(line); |
1527 | if (!view->line[view->lines]) | |
1528 | return FALSE; | |
1529 | ||
1530 | view->lines++; | |
1531 | return TRUE; | |
1532 | } | |
1533 | ||
6b161b31 JF |
1534 | static bool |
1535 | pager_enter(struct view *view) | |
1536 | { | |
1537 | char *line = view->line[view->lineno]; | |
1538 | ||
67e48ac5 JF |
1539 | if (view == VIEW(REQ_VIEW_DIFF)) { |
1540 | scroll_view(view, REQ_SCROLL_LINE_DOWN); | |
1541 | return TRUE; | |
1542 | } | |
1543 | ||
6b161b31 | 1544 | if (get_line_type(line) == LINE_COMMIT) { |
6706b2ba JF |
1545 | if (view == VIEW(REQ_VIEW_LOG)) |
1546 | open_view(view, REQ_VIEW_DIFF, OPEN_SPLIT | OPEN_BACKGROUNDED); | |
1547 | else | |
1548 | open_view(view, REQ_VIEW_DIFF, OPEN_DEFAULT); | |
6b161b31 JF |
1549 | } |
1550 | ||
1551 | return TRUE; | |
1552 | } | |
1553 | ||
6b161b31 | 1554 | static struct view_ops pager_ops = { |
6734f6b9 | 1555 | "line", |
6b161b31 JF |
1556 | pager_draw, |
1557 | pager_read, | |
1558 | pager_enter, | |
1559 | }; | |
1560 | ||
80ce96ea | 1561 | |
c34d9c9f JF |
1562 | static struct ref **get_refs(char *id); |
1563 | ||
6b161b31 | 1564 | static bool |
22f66b0a JF |
1565 | main_draw(struct view *view, unsigned int lineno) |
1566 | { | |
2e8488b4 | 1567 | char buf[DATE_COLS + 1]; |
22f66b0a | 1568 | struct commit *commit; |
78c70acd | 1569 | enum line_type type; |
6706b2ba | 1570 | int col = 0; |
b76c2afc | 1571 | size_t timelen; |
10e290ee JF |
1572 | size_t authorlen; |
1573 | int trimmed; | |
22f66b0a JF |
1574 | |
1575 | if (view->offset + lineno >= view->lines) | |
1576 | return FALSE; | |
1577 | ||
1578 | commit = view->line[view->offset + lineno]; | |
4c6fabc2 JF |
1579 | if (!*commit->author) |
1580 | return FALSE; | |
22f66b0a | 1581 | |
6706b2ba JF |
1582 | wmove(view->win, lineno, col); |
1583 | ||
22f66b0a | 1584 | if (view->offset + lineno == view->lineno) { |
03a93dbb | 1585 | string_copy(view->ref, commit->id); |
49f2b43f | 1586 | string_copy(ref_commit, view->ref); |
78c70acd | 1587 | type = LINE_CURSOR; |
6706b2ba JF |
1588 | wattrset(view->win, get_line_attr(type)); |
1589 | wchgat(view->win, -1, 0, type, NULL); | |
1590 | ||
78c70acd | 1591 | } else { |
b76c2afc | 1592 | type = LINE_MAIN_COMMIT; |
6706b2ba | 1593 | wattrset(view->win, get_line_attr(LINE_MAIN_DATE)); |
b76c2afc JF |
1594 | } |
1595 | ||
4c6fabc2 | 1596 | timelen = strftime(buf, sizeof(buf), DATE_FORMAT, &commit->time); |
b76c2afc | 1597 | waddnstr(view->win, buf, timelen); |
4c6fabc2 | 1598 | waddstr(view->win, " "); |
b76c2afc | 1599 | |
6706b2ba JF |
1600 | col += DATE_COLS; |
1601 | wmove(view->win, lineno, col); | |
1602 | if (type != LINE_CURSOR) | |
1603 | wattrset(view->win, get_line_attr(LINE_MAIN_AUTHOR)); | |
b76c2afc | 1604 | |
10e290ee JF |
1605 | /* FIXME: Make this optional, and add i18n.commitEncoding support. */ |
1606 | authorlen = utf8_length(commit->author, AUTHOR_COLS - 2, &col, &trimmed); | |
1607 | ||
1608 | if (trimmed) { | |
1609 | waddnstr(view->win, commit->author, authorlen); | |
6706b2ba JF |
1610 | if (type != LINE_CURSOR) |
1611 | wattrset(view->win, get_line_attr(LINE_MAIN_DELIM)); | |
b76c2afc JF |
1612 | waddch(view->win, '~'); |
1613 | } else { | |
1614 | waddstr(view->win, commit->author); | |
22f66b0a JF |
1615 | } |
1616 | ||
10e290ee | 1617 | col += AUTHOR_COLS; |
6706b2ba JF |
1618 | if (type != LINE_CURSOR) |
1619 | wattrset(view->win, A_NORMAL); | |
1620 | ||
1621 | mvwaddch(view->win, lineno, col, ACS_LTEE); | |
1622 | wmove(view->win, lineno, col + 2); | |
1623 | col += 2; | |
c34d9c9f JF |
1624 | |
1625 | if (commit->refs) { | |
1626 | size_t i = 0; | |
1627 | ||
1628 | do { | |
6706b2ba JF |
1629 | if (type == LINE_CURSOR) |
1630 | ; | |
1631 | else if (commit->refs[i]->tag) | |
c34d9c9f JF |
1632 | wattrset(view->win, get_line_attr(LINE_MAIN_TAG)); |
1633 | else | |
1634 | wattrset(view->win, get_line_attr(LINE_MAIN_REF)); | |
1635 | waddstr(view->win, "["); | |
1636 | waddstr(view->win, commit->refs[i]->name); | |
1637 | waddstr(view->win, "]"); | |
6706b2ba JF |
1638 | if (type != LINE_CURSOR) |
1639 | wattrset(view->win, A_NORMAL); | |
c34d9c9f | 1640 | waddstr(view->win, " "); |
6706b2ba | 1641 | col += strlen(commit->refs[i]->name) + STRING_SIZE("[] "); |
c34d9c9f JF |
1642 | } while (commit->refs[i++]->next); |
1643 | } | |
1644 | ||
6706b2ba JF |
1645 | if (type != LINE_CURSOR) |
1646 | wattrset(view->win, get_line_attr(type)); | |
1647 | ||
1648 | { | |
1649 | int titlelen = strlen(commit->title); | |
1650 | ||
1651 | if (col + titlelen > view->width) | |
1652 | titlelen = view->width - col; | |
1653 | ||
1654 | waddnstr(view->win, commit->title, titlelen); | |
1655 | } | |
22f66b0a JF |
1656 | |
1657 | return TRUE; | |
1658 | } | |
1659 | ||
4c6fabc2 | 1660 | /* Reads git log --pretty=raw output and parses it into the commit struct. */ |
6b161b31 | 1661 | static bool |
22f66b0a JF |
1662 | main_read(struct view *view, char *line) |
1663 | { | |
78c70acd JF |
1664 | enum line_type type = get_line_type(line); |
1665 | struct commit *commit; | |
22f66b0a | 1666 | |
78c70acd JF |
1667 | switch (type) { |
1668 | case LINE_COMMIT: | |
22f66b0a JF |
1669 | commit = calloc(1, sizeof(struct commit)); |
1670 | if (!commit) | |
1671 | return FALSE; | |
1672 | ||
4c6fabc2 | 1673 | line += STRING_SIZE("commit "); |
b76c2afc | 1674 | |
22f66b0a | 1675 | view->line[view->lines++] = commit; |
82e78006 | 1676 | string_copy(commit->id, line); |
c34d9c9f | 1677 | commit->refs = get_refs(commit->id); |
78c70acd | 1678 | break; |
22f66b0a | 1679 | |
8855ada4 | 1680 | case LINE_AUTHOR: |
b76c2afc | 1681 | { |
4c6fabc2 | 1682 | char *ident = line + STRING_SIZE("author "); |
b76c2afc JF |
1683 | char *end = strchr(ident, '<'); |
1684 | ||
1685 | if (end) { | |
1686 | for (; end > ident && isspace(end[-1]); end--) ; | |
1687 | *end = 0; | |
1688 | } | |
1689 | ||
1690 | commit = view->line[view->lines - 1]; | |
82e78006 | 1691 | string_copy(commit->author, ident); |
b76c2afc | 1692 | |
4c6fabc2 | 1693 | /* Parse epoch and timezone */ |
b76c2afc JF |
1694 | if (end) { |
1695 | char *secs = strchr(end + 1, '>'); | |
1696 | char *zone; | |
1697 | time_t time; | |
1698 | ||
1699 | if (!secs || secs[1] != ' ') | |
1700 | break; | |
1701 | ||
1702 | secs += 2; | |
1703 | time = (time_t) atol(secs); | |
1704 | zone = strchr(secs, ' '); | |
4c6fabc2 | 1705 | if (zone && strlen(zone) == STRING_SIZE(" +0700")) { |
b76c2afc JF |
1706 | long tz; |
1707 | ||
1708 | zone++; | |
1709 | tz = ('0' - zone[1]) * 60 * 60 * 10; | |
1710 | tz += ('0' - zone[2]) * 60 * 60; | |
1711 | tz += ('0' - zone[3]) * 60; | |
1712 | tz += ('0' - zone[4]) * 60; | |
1713 | ||
1714 | if (zone[0] == '-') | |
1715 | tz = -tz; | |
1716 | ||
1717 | time -= tz; | |
1718 | } | |
1719 | gmtime_r(&time, &commit->time); | |
1720 | } | |
1721 | break; | |
1722 | } | |
78c70acd | 1723 | default: |
2e8488b4 JF |
1724 | /* We should only ever end up here if there has already been a |
1725 | * commit line, however, be safe. */ | |
1726 | if (view->lines == 0) | |
1727 | break; | |
1728 | ||
1729 | /* Fill in the commit title if it has not already been set. */ | |
78c70acd | 1730 | commit = view->line[view->lines - 1]; |
2e8488b4 JF |
1731 | if (commit->title[0]) |
1732 | break; | |
1733 | ||
1734 | /* Require titles to start with a non-space character at the | |
1735 | * offset used by git log. */ | |
eb98559e JF |
1736 | /* FIXME: More gracefull handling of titles; append "..." to |
1737 | * shortened titles, etc. */ | |
2e8488b4 | 1738 | if (strncmp(line, " ", 4) || |
eb98559e | 1739 | isspace(line[4])) |
82e78006 JF |
1740 | break; |
1741 | ||
1742 | string_copy(commit->title, line + 4); | |
22f66b0a JF |
1743 | } |
1744 | ||
1745 | return TRUE; | |
1746 | } | |
1747 | ||
6b161b31 JF |
1748 | static bool |
1749 | main_enter(struct view *view) | |
b801d8b2 | 1750 | { |
89e4aafd | 1751 | open_view(view, REQ_VIEW_DIFF, OPEN_SPLIT); |
6b161b31 | 1752 | return TRUE; |
b801d8b2 JF |
1753 | } |
1754 | ||
6b161b31 | 1755 | static struct view_ops main_ops = { |
6734f6b9 | 1756 | "commit", |
6b161b31 JF |
1757 | main_draw, |
1758 | main_read, | |
1759 | main_enter, | |
1760 | }; | |
2e8488b4 | 1761 | |
c34d9c9f | 1762 | |
468876c9 JF |
1763 | /** |
1764 | * KEYS | |
1765 | * ---- | |
1766 | * Below the default key bindings are shown. | |
1767 | **/ | |
1768 | ||
1769 | struct keymap { | |
1770 | int alias; | |
1771 | int request; | |
1772 | }; | |
1773 | ||
3a91b75e | 1774 | static struct keymap keymap[] = { |
468876c9 JF |
1775 | /** |
1776 | * View switching | |
1777 | * ~~~~~~~~~~~~~~ | |
1778 | * m:: | |
1779 | * Switch to main view. | |
1780 | * d:: | |
1781 | * Switch to diff view. | |
1782 | * l:: | |
1783 | * Switch to log view. | |
1784 | * p:: | |
1785 | * Switch to pager view. | |
1786 | * h:: | |
1787 | * Show man page. | |
4f9b667a JF |
1788 | * q:: |
1789 | * Close view if multiple views are open, else quit. | |
67e48ac5 JF |
1790 | * Enter:: |
1791 | * This key is "context sensitive" depending on what view you are | |
1792 | * currently in. When in log view on a commit line or in the main | |
1793 | * view, split the view and show the commit diff. In the diff view | |
1794 | * pressing Enter will simply scroll the view one line down. | |
468876c9 JF |
1795 | * Tab:: |
1796 | * Switch to next view. | |
1797 | **/ | |
1798 | { 'm', REQ_VIEW_MAIN }, | |
1799 | { 'd', REQ_VIEW_DIFF }, | |
1800 | { 'l', REQ_VIEW_LOG }, | |
1801 | { 'p', REQ_VIEW_PAGER }, | |
1802 | { 'h', REQ_VIEW_HELP }, | |
1803 | ||
4f9b667a | 1804 | { 'q', REQ_VIEW_CLOSE }, |
468876c9 JF |
1805 | { KEY_TAB, REQ_VIEW_NEXT }, |
1806 | { KEY_RETURN, REQ_ENTER }, | |
1807 | ||
1808 | /** | |
1809 | * Cursor navigation | |
1810 | * ~~~~~~~~~~~~~~~~~ | |
1811 | * Up:: | |
57bdf034 | 1812 | * Move cursor one line up. |
468876c9 JF |
1813 | * Down:: |
1814 | * Move cursor one line down. | |
1815 | * k:: | |
57bdf034 | 1816 | * Move cursor one line up and enter. When used in the main view |
468876c9 JF |
1817 | * this will always show the diff of the current commit in the |
1818 | * split diff view. | |
1819 | * j:: | |
1820 | * Move cursor one line down and enter. | |
1821 | * PgUp:: | |
c622eefa | 1822 | * b:: |
57bdf034 | 1823 | * Move cursor one page up. |
468876c9 | 1824 | * PgDown:: |
c622eefa | 1825 | * Space:: |
468876c9 JF |
1826 | * Move cursor one page down. |
1827 | * Home:: | |
1828 | * Jump to first line. | |
1829 | * End:: | |
1830 | * Jump to last line. | |
1831 | **/ | |
1832 | { KEY_UP, REQ_MOVE_UP }, | |
1833 | { KEY_DOWN, REQ_MOVE_DOWN }, | |
1834 | { 'k', REQ_MOVE_UP_ENTER }, | |
1835 | { 'j', REQ_MOVE_DOWN_ENTER }, | |
1836 | { KEY_HOME, REQ_MOVE_FIRST_LINE }, | |
1837 | { KEY_END, REQ_MOVE_LAST_LINE }, | |
1838 | { KEY_NPAGE, REQ_MOVE_PAGE_DOWN }, | |
c622eefa | 1839 | { ' ', REQ_MOVE_PAGE_DOWN }, |
468876c9 | 1840 | { KEY_PPAGE, REQ_MOVE_PAGE_UP }, |
c622eefa | 1841 | { 'b', REQ_MOVE_PAGE_UP }, |
468876c9 JF |
1842 | |
1843 | /** | |
1844 | * Scrolling | |
1845 | * ~~~~~~~~~ | |
1846 | * Insert:: | |
1847 | * Scroll view one line up. | |
1848 | * Delete:: | |
1849 | * Scroll view one line down. | |
1850 | * w:: | |
1851 | * Scroll view one page up. | |
1852 | * s:: | |
1853 | * Scroll view one page down. | |
1854 | **/ | |
1855 | { KEY_IC, REQ_SCROLL_LINE_UP }, | |
1856 | { KEY_DC, REQ_SCROLL_LINE_DOWN }, | |
1857 | { 'w', REQ_SCROLL_PAGE_UP }, | |
1858 | { 's', REQ_SCROLL_PAGE_DOWN }, | |
1859 | ||
1860 | /** | |
1861 | * Misc | |
1862 | * ~~~~ | |
d9d1c722 JF |
1863 | * Q:: |
1864 | * Quit. | |
468876c9 JF |
1865 | * r:: |
1866 | * Redraw screen. | |
1867 | * z:: | |
1868 | * Stop all background loading. This can be useful if you use | |
1869 | * tig(1) in a repository with a long history without limiting | |
0721c53a | 1870 | * the revision log. |
468876c9 JF |
1871 | * v:: |
1872 | * Show version. | |
1873 | * n:: | |
1874 | * Toggle line numbers on/off. | |
1875 | * ':':: | |
1876 | * Open prompt. This allows you to specify what git command | |
1877 | * to run. Example: | |
1878 | * | |
1879 | * :log -p | |
1880 | **/ | |
d9d1c722 | 1881 | { 'Q', REQ_QUIT }, |
468876c9 JF |
1882 | { 'z', REQ_STOP_LOADING }, |
1883 | { 'v', REQ_SHOW_VERSION }, | |
1884 | { 'r', REQ_SCREEN_REDRAW }, | |
1885 | { 'n', REQ_TOGGLE_LINE_NUMBERS }, | |
1886 | { ':', REQ_PROMPT }, | |
1887 | ||
1888 | /* wgetch() with nodelay() enabled returns ERR when there's no input. */ | |
1889 | { ERR, REQ_SCREEN_UPDATE }, | |
1890 | ||
1891 | /* Use the ncurses SIGWINCH handler. */ | |
1892 | { KEY_RESIZE, REQ_SCREEN_RESIZE }, | |
1893 | }; | |
1894 | ||
1895 | static enum request | |
1896 | get_request(int key) | |
1897 | { | |
1898 | int i; | |
1899 | ||
1900 | for (i = 0; i < ARRAY_SIZE(keymap); i++) | |
1901 | if (keymap[i].alias == key) | |
1902 | return keymap[i].request; | |
1903 | ||
1904 | return (enum request) key; | |
1905 | } | |
1906 | ||
1907 | ||
6b161b31 | 1908 | /* |
10e290ee JF |
1909 | * Unicode / UTF-8 handling |
1910 | * | |
1911 | * NOTE: Much of the following code for dealing with unicode is derived from | |
1912 | * ELinks' UTF-8 code developed by Scrool <scroolik@gmail.com>. Origin file is | |
1913 | * src/intl/charset.c from the utf8 branch commit elinks-0.11.0-g31f2c28. | |
1914 | */ | |
1915 | ||
1916 | /* I've (over)annotated a lot of code snippets because I am not entirely | |
1917 | * confident that the approach taken by this small UTF-8 interface is correct. | |
1918 | * --jonas */ | |
1919 | ||
1920 | static inline int | |
1921 | unicode_width(unsigned long c) | |
1922 | { | |
1923 | if (c >= 0x1100 && | |
1924 | (c <= 0x115f /* Hangul Jamo */ | |
1925 | || c == 0x2329 | |
1926 | || c == 0x232a | |
1927 | || (c >= 0x2e80 && c <= 0xa4cf && c != 0x303f) | |
1928 | /* CJK ... Yi */ | |
1929 | || (c >= 0xac00 && c <= 0xd7a3) /* Hangul Syllables */ | |
1930 | || (c >= 0xf900 && c <= 0xfaff) /* CJK Compatibility Ideographs */ | |
1931 | || (c >= 0xfe30 && c <= 0xfe6f) /* CJK Compatibility Forms */ | |
1932 | || (c >= 0xff00 && c <= 0xff60) /* Fullwidth Forms */ | |
1933 | || (c >= 0xffe0 && c <= 0xffe6) | |
1934 | || (c >= 0x20000 && c <= 0x2fffd) | |
1935 | || (c >= 0x30000 && c <= 0x3fffd))) | |
1936 | return 2; | |
1937 | ||
1938 | return 1; | |
1939 | } | |
1940 | ||
1941 | /* Number of bytes used for encoding a UTF-8 character indexed by first byte. | |
1942 | * Illegal bytes are set one. */ | |
1943 | static const unsigned char utf8_bytes[256] = { | |
1944 | 1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1, | |
1945 | 1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1, | |
1946 | 1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1, | |
1947 | 1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1, | |
1948 | 1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1, | |
1949 | 1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1, | |
1950 | 2,2,2,2,2,2,2,2, 2,2,2,2,2,2,2,2, 2,2,2,2,2,2,2,2, 2,2,2,2,2,2,2,2, | |
1951 | 3,3,3,3,3,3,3,3, 3,3,3,3,3,3,3,3, 4,4,4,4,4,4,4,4, 5,5,5,5,6,6,1,1, | |
1952 | }; | |
1953 | ||
1954 | /* Decode UTF-8 multi-byte representation into a unicode character. */ | |
1955 | static inline unsigned long | |
1956 | utf8_to_unicode(const char *string, size_t length) | |
1957 | { | |
1958 | unsigned long unicode; | |
1959 | ||
1960 | switch (length) { | |
1961 | case 1: | |
1962 | unicode = string[0]; | |
1963 | break; | |
1964 | case 2: | |
1965 | unicode = (string[0] & 0x1f) << 6; | |
1966 | unicode += (string[1] & 0x3f); | |
1967 | break; | |
1968 | case 3: | |
1969 | unicode = (string[0] & 0x0f) << 12; | |
1970 | unicode += ((string[1] & 0x3f) << 6); | |
1971 | unicode += (string[2] & 0x3f); | |
1972 | break; | |
1973 | case 4: | |
1974 | unicode = (string[0] & 0x0f) << 18; | |
1975 | unicode += ((string[1] & 0x3f) << 12); | |
1976 | unicode += ((string[2] & 0x3f) << 6); | |
1977 | unicode += (string[3] & 0x3f); | |
1978 | break; | |
1979 | case 5: | |
1980 | unicode = (string[0] & 0x0f) << 24; | |
1981 | unicode += ((string[1] & 0x3f) << 18); | |
1982 | unicode += ((string[2] & 0x3f) << 12); | |
1983 | unicode += ((string[3] & 0x3f) << 6); | |
1984 | unicode += (string[4] & 0x3f); | |
1985 | break; | |
1986 | case 6: | |
1987 | unicode = (string[0] & 0x01) << 30; | |
1988 | unicode += ((string[1] & 0x3f) << 24); | |
1989 | unicode += ((string[2] & 0x3f) << 18); | |
1990 | unicode += ((string[3] & 0x3f) << 12); | |
1991 | unicode += ((string[4] & 0x3f) << 6); | |
1992 | unicode += (string[5] & 0x3f); | |
1993 | break; | |
1994 | default: | |
1995 | die("Invalid unicode length"); | |
1996 | } | |
1997 | ||
1998 | /* Invalid characters could return the special 0xfffd value but NUL | |
1999 | * should be just as good. */ | |
2000 | return unicode > 0xffff ? 0 : unicode; | |
2001 | } | |
2002 | ||
2003 | /* Calculates how much of string can be shown within the given maximum width | |
2004 | * and sets trimmed parameter to non-zero value if all of string could not be | |
2005 | * shown. | |
2006 | * | |
2007 | * Additionally, adds to coloffset how many many columns to move to align with | |
2008 | * the expected position. Takes into account how multi-byte and double-width | |
2009 | * characters will effect the cursor position. | |
2010 | * | |
2011 | * Returns the number of bytes to output from string to satisfy max_width. */ | |
2012 | static size_t | |
2013 | utf8_length(const char *string, size_t max_width, int *coloffset, int *trimmed) | |
2014 | { | |
2015 | const char *start = string; | |
2016 | const char *end = strchr(string, '\0'); | |
2017 | size_t mbwidth = 0; | |
2018 | size_t width = 0; | |
2019 | ||
2020 | *trimmed = 0; | |
2021 | ||
2022 | while (string < end) { | |
2023 | int c = *(unsigned char *) string; | |
2024 | unsigned char bytes = utf8_bytes[c]; | |
2025 | size_t ucwidth; | |
2026 | unsigned long unicode; | |
2027 | ||
2028 | if (string + bytes > end) | |
2029 | break; | |
2030 | ||
2031 | /* Change representation to figure out whether | |
2032 | * it is a single- or double-width character. */ | |
2033 | ||
2034 | unicode = utf8_to_unicode(string, bytes); | |
2035 | /* FIXME: Graceful handling of invalid unicode character. */ | |
2036 | if (!unicode) | |
2037 | break; | |
2038 | ||
2039 | ucwidth = unicode_width(unicode); | |
2040 | width += ucwidth; | |
2041 | if (width > max_width) { | |
2042 | *trimmed = 1; | |
2043 | break; | |
2044 | } | |
2045 | ||
2046 | /* The column offset collects the differences between the | |
2047 | * number of bytes encoding a character and the number of | |
2048 | * columns will be used for rendering said character. | |
2049 | * | |
2050 | * So if some character A is encoded in 2 bytes, but will be | |
2051 | * represented on the screen using only 1 byte this will and up | |
2052 | * adding 1 to the multi-byte column offset. | |
2053 | * | |
2054 | * Assumes that no double-width character can be encoding in | |
2055 | * less than two bytes. */ | |
2056 | if (bytes > ucwidth) | |
2057 | mbwidth += bytes - ucwidth; | |
2058 | ||
2059 | string += bytes; | |
2060 | } | |
2061 | ||
2062 | *coloffset += mbwidth; | |
2063 | ||
2064 | return string - start; | |
2065 | } | |
2066 | ||
2067 | ||
2068 | /* | |
6b161b31 JF |
2069 | * Status management |
2070 | */ | |
2e8488b4 | 2071 | |
8855ada4 JF |
2072 | /* Whether or not the curses interface has been initialized. */ |
2073 | bool cursed = FALSE; | |
2074 | ||
6b161b31 JF |
2075 | /* The status window is used for polling keystrokes. */ |
2076 | static WINDOW *status_win; | |
4a2909a7 | 2077 | |
2e8488b4 | 2078 | /* Update status and title window. */ |
4a2909a7 JF |
2079 | static void |
2080 | report(const char *msg, ...) | |
2081 | { | |
6706b2ba JF |
2082 | static bool empty = TRUE; |
2083 | struct view *view = display[current_view]; | |
b76c2afc | 2084 | |
6706b2ba JF |
2085 | if (!empty || *msg) { |
2086 | va_list args; | |
4a2909a7 | 2087 | |
6706b2ba | 2088 | va_start(args, msg); |
4b76734f | 2089 | |
6706b2ba JF |
2090 | werase(status_win); |
2091 | wmove(status_win, 0, 0); | |
2092 | if (*msg) { | |
2093 | vwprintw(status_win, msg, args); | |
2094 | empty = FALSE; | |
2095 | } else { | |
2096 | empty = TRUE; | |
2097 | } | |
2098 | wrefresh(status_win); | |
b801d8b2 | 2099 | |
6706b2ba JF |
2100 | va_end(args); |
2101 | } | |
2102 | ||
2103 | update_view_title(view); | |
2104 | ||
2105 | /* Move the cursor to the right-most column of the cursor line. | |
2106 | * | |
2107 | * XXX: This could turn out to be a bit expensive, but it ensures that | |
2108 | * the cursor does not jump around. */ | |
2109 | if (view->lines) { | |
2110 | wmove(view->win, view->lineno - view->offset, view->width - 1); | |
2111 | wrefresh(view->win); | |
2112 | } | |
b801d8b2 JF |
2113 | } |
2114 | ||
6b161b31 JF |
2115 | /* Controls when nodelay should be in effect when polling user input. */ |
2116 | static void | |
1ba2ae4b | 2117 | set_nonblocking_input(bool loading) |
b801d8b2 | 2118 | { |
6706b2ba | 2119 | static unsigned int loading_views; |
b801d8b2 | 2120 | |
6706b2ba JF |
2121 | if ((loading == FALSE && loading_views-- == 1) || |
2122 | (loading == TRUE && loading_views++ == 0)) | |
1ba2ae4b | 2123 | nodelay(status_win, loading); |
6b161b31 JF |
2124 | } |
2125 | ||
2126 | static void | |
2127 | init_display(void) | |
2128 | { | |
2129 | int x, y; | |
b76c2afc | 2130 | |
6908bdbd JF |
2131 | /* Initialize the curses library */ |
2132 | if (isatty(STDIN_FILENO)) { | |
8855ada4 | 2133 | cursed = !!initscr(); |
6908bdbd JF |
2134 | } else { |
2135 | /* Leave stdin and stdout alone when acting as a pager. */ | |
2136 | FILE *io = fopen("/dev/tty", "r+"); | |
2137 | ||
8855ada4 | 2138 | cursed = !!newterm(NULL, io, io); |
6908bdbd JF |
2139 | } |
2140 | ||
8855ada4 JF |
2141 | if (!cursed) |
2142 | die("Failed to initialize curses"); | |
2143 | ||
2e8488b4 JF |
2144 | nonl(); /* Tell curses not to do NL->CR/NL on output */ |
2145 | cbreak(); /* Take input chars one at a time, no wait for \n */ | |
2146 | noecho(); /* Don't echo input */ | |
b801d8b2 | 2147 | leaveok(stdscr, TRUE); |
b801d8b2 JF |
2148 | |
2149 | if (has_colors()) | |
2150 | init_colors(); | |
2151 | ||
2152 | getmaxyx(stdscr, y, x); | |
2153 | status_win = newwin(1, 0, y - 1, 0); | |
2154 | if (!status_win) | |
2155 | die("Failed to create status window"); | |
2156 | ||
2157 | /* Enable keyboard mapping */ | |
2158 | keypad(status_win, TRUE); | |
78c70acd | 2159 | wbkgdset(status_win, get_line_attr(LINE_STATUS)); |
6b161b31 JF |
2160 | } |
2161 | ||
c34d9c9f JF |
2162 | |
2163 | /* | |
2164 | * Repository references | |
2165 | */ | |
2166 | ||
2167 | static struct ref *refs; | |
3a91b75e | 2168 | static size_t refs_size; |
c34d9c9f JF |
2169 | |
2170 | static struct ref ** | |
2171 | get_refs(char *id) | |
2172 | { | |
2173 | struct ref **id_refs = NULL; | |
2174 | size_t id_refs_size = 0; | |
2175 | size_t i; | |
2176 | ||
2177 | for (i = 0; i < refs_size; i++) { | |
2178 | struct ref **tmp; | |
2179 | ||
2180 | if (strcmp(id, refs[i].id)) | |
2181 | continue; | |
2182 | ||
2183 | tmp = realloc(id_refs, (id_refs_size + 1) * sizeof(*id_refs)); | |
2184 | if (!tmp) { | |
2185 | if (id_refs) | |
2186 | free(id_refs); | |
2187 | return NULL; | |
2188 | } | |
2189 | ||
2190 | id_refs = tmp; | |
3af8774e | 2191 | if (id_refs_size > 0) |
c34d9c9f | 2192 | id_refs[id_refs_size - 1]->next = 1; |
3af8774e JF |
2193 | id_refs[id_refs_size] = &refs[i]; |
2194 | ||
2195 | /* XXX: The properties of the commit chains ensures that we can | |
2196 | * safely modify the shared ref. The repo references will | |
2197 | * always be similar for the same id. */ | |
2198 | id_refs[id_refs_size]->next = 0; | |
2199 | id_refs_size++; | |
c34d9c9f JF |
2200 | } |
2201 | ||
2202 | return id_refs; | |
2203 | } | |
2204 | ||
2205 | static int | |
2206 | load_refs(void) | |
2207 | { | |
4685845e TH |
2208 | const char *cmd_env = getenv("TIG_LS_REMOTE"); |
2209 | const char *cmd = cmd_env && *cmd_env ? cmd_env : TIG_LS_REMOTE; | |
c34d9c9f JF |
2210 | FILE *pipe = popen(cmd, "r"); |
2211 | char buffer[BUFSIZ]; | |
2212 | char *line; | |
2213 | ||
2214 | if (!pipe) | |
2215 | return ERR; | |
2216 | ||
2217 | while ((line = fgets(buffer, sizeof(buffer), pipe))) { | |
2218 | char *name = strchr(line, '\t'); | |
2219 | struct ref *ref; | |
2220 | int namelen; | |
2221 | bool tag = FALSE; | |
6734f6b9 | 2222 | bool tag_commit = FALSE; |
c34d9c9f JF |
2223 | |
2224 | if (!name) | |
2225 | continue; | |
2226 | ||
2227 | *name++ = 0; | |
2228 | namelen = strlen(name) - 1; | |
6706b2ba JF |
2229 | |
2230 | /* Commits referenced by tags has "^{}" appended. */ | |
c34d9c9f JF |
2231 | if (name[namelen - 1] == '}') { |
2232 | while (namelen > 0 && name[namelen] != '^') | |
2233 | namelen--; | |
6734f6b9 JF |
2234 | if (namelen > 0) |
2235 | tag_commit = TRUE; | |
c34d9c9f JF |
2236 | } |
2237 | name[namelen] = 0; | |
2238 | ||
2239 | if (!strncmp(name, "refs/tags/", STRING_SIZE("refs/tags/"))) { | |
6734f6b9 JF |
2240 | if (!tag_commit) |
2241 | continue; | |
c34d9c9f JF |
2242 | name += STRING_SIZE("refs/tags/"); |
2243 | tag = TRUE; | |
3af8774e JF |
2244 | |
2245 | } else if (!strncmp(name, "refs/heads/", STRING_SIZE("refs/heads/"))) { | |
2246 | name += STRING_SIZE("refs/heads/"); | |
2247 | ||
2248 | } else if (!strcmp(name, "HEAD")) { | |
2249 | continue; | |
c34d9c9f JF |
2250 | } |
2251 | ||
2252 | refs = realloc(refs, sizeof(*refs) * (refs_size + 1)); | |
2253 | if (!refs) | |
2254 | return ERR; | |
2255 | ||
2256 | ref = &refs[refs_size++]; | |
2257 | ref->tag = tag; | |
2258 | ref->name = strdup(name); | |
2259 | if (!ref->name) | |
2260 | return ERR; | |
2261 | ||
2262 | string_copy(ref->id, line); | |
2263 | } | |
2264 | ||
2265 | if (ferror(pipe)) | |
2266 | return ERR; | |
2267 | ||
2268 | pclose(pipe); | |
2269 | ||
1b4b0bd9 JF |
2270 | if (refs_size == 0) |
2271 | die("Not a git repository"); | |
2272 | ||
c34d9c9f JF |
2273 | return OK; |
2274 | } | |
2275 | ||
6b161b31 JF |
2276 | /* |
2277 | * Main | |
2278 | */ | |
2279 | ||
b5c9e67f TH |
2280 | #if __GNUC__ >= 3 |
2281 | #define __NORETURN __attribute__((__noreturn__)) | |
2282 | #else | |
2283 | #define __NORETURN | |
2284 | #endif | |
2285 | ||
2286 | static void __NORETURN | |
6b161b31 JF |
2287 | quit(int sig) |
2288 | { | |
8855ada4 JF |
2289 | /* XXX: Restore tty modes and let the OS cleanup the rest! */ |
2290 | if (cursed) | |
2291 | endwin(); | |
6b161b31 JF |
2292 | exit(0); |
2293 | } | |
2294 | ||
c6704a4e JF |
2295 | static void __NORETURN |
2296 | die(const char *err, ...) | |
6b161b31 JF |
2297 | { |
2298 | va_list args; | |
2299 | ||
2300 | endwin(); | |
2301 | ||
2302 | va_start(args, err); | |
2303 | fputs("tig: ", stderr); | |
2304 | vfprintf(stderr, err, args); | |
2305 | fputs("\n", stderr); | |
2306 | va_end(args); | |
2307 | ||
2308 | exit(1); | |
2309 | } | |
2310 | ||
2311 | int | |
2312 | main(int argc, char *argv[]) | |
2313 | { | |
1ba2ae4b | 2314 | struct view *view; |
6b161b31 | 2315 | enum request request; |
1ba2ae4b | 2316 | size_t i; |
6b161b31 JF |
2317 | |
2318 | signal(SIGINT, quit); | |
2319 | ||
8855ada4 | 2320 | if (!parse_options(argc, argv)) |
6b161b31 JF |
2321 | return 0; |
2322 | ||
c34d9c9f JF |
2323 | if (load_refs() == ERR) |
2324 | die("Failed to load refs."); | |
2325 | ||
1ba2ae4b JF |
2326 | for (i = 0; i < ARRAY_SIZE(views) && (view = &views[i]); i++) |
2327 | view->cmd_env = getenv(view->cmd_env); | |
2328 | ||
6b161b31 JF |
2329 | request = opt_request; |
2330 | ||
2331 | init_display(); | |
b801d8b2 JF |
2332 | |
2333 | while (view_driver(display[current_view], request)) { | |
6b161b31 | 2334 | int key; |
b801d8b2 JF |
2335 | int i; |
2336 | ||
6b161b31 JF |
2337 | foreach_view (view, i) |
2338 | update_view(view); | |
b801d8b2 JF |
2339 | |
2340 | /* Refresh, accept single keystroke of input */ | |
6b161b31 JF |
2341 | key = wgetch(status_win); |
2342 | request = get_request(key); | |
03a93dbb | 2343 | |
6706b2ba | 2344 | /* Some low-level request handling. This keeps access to |
fac7db6c JF |
2345 | * status_win restricted. */ |
2346 | switch (request) { | |
2347 | case REQ_PROMPT: | |
6908bdbd JF |
2348 | report(":"); |
2349 | /* Temporarily switch to line-oriented and echoed | |
2350 | * input. */ | |
03a93dbb JF |
2351 | nocbreak(); |
2352 | echo(); | |
49f2b43f JF |
2353 | |
2354 | if (wgetnstr(status_win, opt_cmd + 4, sizeof(opt_cmd) - 4) == OK) { | |
2355 | memcpy(opt_cmd, "git ", 4); | |
2356 | opt_request = REQ_VIEW_PAGER; | |
2357 | } else { | |
2358 | request = ERR; | |
2359 | } | |
2360 | ||
6908bdbd JF |
2361 | noecho(); |
2362 | cbreak(); | |
fac7db6c JF |
2363 | break; |
2364 | ||
2365 | case REQ_SCREEN_RESIZE: | |
2366 | { | |
2367 | int height, width; | |
2368 | ||
2369 | getmaxyx(stdscr, height, width); | |
2370 | ||
2371 | /* Resize the status view and let the view driver take | |
2372 | * care of resizing the displayed views. */ | |
2373 | wresize(status_win, 1, width); | |
2374 | mvwin(status_win, height - 1, 0); | |
2375 | wrefresh(status_win); | |
2376 | break; | |
2377 | } | |
2378 | default: | |
2379 | break; | |
03a93dbb | 2380 | } |
b801d8b2 JF |
2381 | } |
2382 | ||
2383 | quit(0); | |
2384 | ||
2385 | return 0; | |
2386 | } | |
2387 | ||
2388 | /** | |
0721c53a | 2389 | * [[refspec]] |
3a11b38f JF |
2390 | * Revision specification |
2391 | * ---------------------- | |
0721c53a | 2392 | * This section describes various ways to specify what revisions to display |
3a11b38f JF |
2393 | * or otherwise limit the view to. tig(1) does not itself parse the described |
2394 | * revision options so refer to the relevant git man pages for futher | |
2395 | * information. Relevant man pages besides git-log(1) are git-diff(1) and | |
2396 | * git-rev-list(1). | |
468876c9 | 2397 | * |
3a11b38f JF |
2398 | * You can tune the interaction with git by making use of the options |
2399 | * explained in this section. For example, by configuring the environment | |
2400 | * variables described in the <<view-commands, "View commands">> section. | |
2401 | * | |
2402 | * Limit by path name | |
2403 | * ~~~~~~~~~~~~~~~~~~ | |
ab46037d JF |
2404 | * If you are interested only in those revisions that made changes to a |
2405 | * specific file (or even several files) list the files like this: | |
2406 | * | |
c760e6ba | 2407 | * $ tig log Makefile README |
ab46037d JF |
2408 | * |
2409 | * To avoid ambiguity with repository references such as tag name, be sure | |
2410 | * to separate file names from other git options using "\--". So if you | |
2411 | * have a file named 'master' it will clash with the reference named | |
2412 | * 'master', and thus you will have to use: | |
2413 | * | |
1b4b0bd9 | 2414 | * $ tig log -- master |
ab46037d JF |
2415 | * |
2416 | * NOTE: For the main view, avoiding ambiguity will in some cases require | |
2417 | * you to specify two "\--" options. The first will make tig(1) stop | |
2418 | * option processing and the latter will be passed to git log. | |
2419 | * | |
468876c9 JF |
2420 | * Limit by date or number |
2421 | * ~~~~~~~~~~~~~~~~~~~~~~~ | |
2422 | * To speed up interaction with git, you can limit the amount of commits | |
2423 | * to show both for the log and main view. Either limit by date using | |
2424 | * e.g. `--since=1.month` or limit by the number of commits using `-n400`. | |
2425 | * | |
3a11b38f JF |
2426 | * If you are only interested in changed that happened between two dates |
2427 | * you can use: | |
2428 | * | |
c760e6ba | 2429 | * $ tig -- --after="May 5th" --before="2006-05-16 15:44" |
468876c9 | 2430 | * |
c760e6ba JF |
2431 | * NOTE: If you want to avoid having to quote dates containing spaces you |
2432 | * can use "." instead, e.g. `--after=May.5th`. | |
3a11b38f JF |
2433 | * |
2434 | * Limiting by commit ranges | |
2435 | * ~~~~~~~~~~~~~~~~~~~~~~~~~ | |
468876c9 JF |
2436 | * Alternatively, commits can be limited to a specific range, such as |
2437 | * "all commits between 'tag-1.0' and 'tag-2.0'". For example: | |
2438 | * | |
2439 | * $ tig log tag-1.0..tag-2.0 | |
2440 | * | |
2441 | * This way of commit limiting makes it trivial to only browse the commits | |
2442 | * which haven't been pushed to a remote branch. Assuming 'origin' is your | |
2443 | * upstream remote branch, using: | |
2444 | * | |
2445 | * $ tig log origin..HEAD | |
2446 | * | |
2447 | * will list what will be pushed to the remote branch. Optionally, the ending | |
2448 | * 'HEAD' can be left out since it is implied. | |
2449 | * | |
2450 | * Limiting by reachability | |
2451 | * ~~~~~~~~~~~~~~~~~~~~~~~~ | |
2452 | * Git interprets the range specifier "tag-1.0..tag-2.0" as | |
2453 | * "all commits reachable from 'tag-2.0' but not from 'tag-1.0'". | |
3a11b38f JF |
2454 | * Where reachability refers to what commits are ancestors (or part of the |
2455 | * history) of the branch or tagged revision in question. | |
2456 | * | |
468876c9 JF |
2457 | * If you prefer to specify which commit to preview in this way use the |
2458 | * following: | |
2459 | * | |
2460 | * $ tig log tag-2.0 ^tag-1.0 | |
2461 | * | |
57bdf034 JF |
2462 | * You can think of '^' as a negation operator. Using this alternate syntax, |
2463 | * it is possible to further prune commits by specifying multiple branch | |
2464 | * cut offs. | |
468876c9 | 2465 | * |
3a11b38f JF |
2466 | * Combining revisions specification |
2467 | * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | |
2468 | * Revisions options can to some degree be combined, which makes it possible | |
2469 | * to say "show at most 20 commits from within the last month that changed | |
2470 | * files under the Documentation/ directory." | |
2471 | * | |
2472 | * $ tig -- --since=1.month -n20 -- Documentation/ | |
2473 | * | |
2474 | * Examining all repository references | |
2475 | * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | |
2476 | * In some cases, it can be useful to query changes across all references | |
2477 | * in a repository. An example is to ask "did any line of development in | |
2478 | * this repository change a particular file within the last week". This | |
2479 | * can be accomplished using: | |
2480 | * | |
2481 | * $ tig -- --all --since=1.week -- Makefile | |
2482 | * | |
6706b2ba JF |
2483 | * BUGS |
2484 | * ---- | |
2485 | * Known bugs and problems: | |
2486 | * | |
10e290ee JF |
2487 | * - In it's current state tig is pretty much UTF-8 only. |
2488 | * | |
6706b2ba | 2489 | * - If the screen width is very small the main view can draw |
468876c9 JF |
2490 | * outside the current view causing bad wrapping. Same goes |
2491 | * for title and status windows. | |
6706b2ba | 2492 | * |
4c6fabc2 JF |
2493 | * TODO |
2494 | * ---- | |
2495 | * Features that should be explored. | |
2496 | * | |
fac7db6c | 2497 | * - Searching. |
4c6fabc2 JF |
2498 | * |
2499 | * - Locale support. | |
2500 | * | |
b801d8b2 JF |
2501 | * COPYRIGHT |
2502 | * --------- | |
4a2909a7 | 2503 | * Copyright (c) Jonas Fonseca <fonseca@diku.dk>, 2006 |
b801d8b2 JF |
2504 | * |
2505 | * This program is free software; you can redistribute it and/or modify | |
2506 | * it under the terms of the GNU General Public License as published by | |
2507 | * the Free Software Foundation; either version 2 of the License, or | |
2508 | * (at your option) any later version. | |
2509 | * | |
2510 | * SEE ALSO | |
2511 | * -------- | |
4c6fabc2 | 2512 | * [verse] |
b801d8b2 JF |
2513 | * link:http://www.kernel.org/pub/software/scm/git/docs/[git(7)], |
2514 | * link:http://www.kernel.org/pub/software/scm/cogito/docs/[cogito(7)] | |
4c6fabc2 | 2515 | * gitk(1): git repository browser written using tcl/tk, |
c6704a4e | 2516 | * qgit(1): git repository browser written using c++/Qt, |
4c6fabc2 | 2517 | * gitview(1): git repository browser written using python/gtk. |
b801d8b2 | 2518 | **/ |