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 | * ----------- | |
23 | * Browse changes in a git repository. | |
b801d8b2 JF |
24 | **/ |
25 | ||
b76c2afc JF |
26 | #ifndef VERSION |
27 | #define VERSION "tig-0.1" | |
28 | #endif | |
29 | ||
8855ada4 JF |
30 | #ifndef DEBUG |
31 | #define NDEBUG | |
32 | #endif | |
33 | ||
22f66b0a | 34 | #include <assert.h> |
4c6fabc2 | 35 | #include <errno.h> |
22f66b0a JF |
36 | #include <ctype.h> |
37 | #include <signal.h> | |
b801d8b2 | 38 | #include <stdarg.h> |
b801d8b2 | 39 | #include <stdio.h> |
22f66b0a | 40 | #include <stdlib.h> |
b801d8b2 | 41 | #include <string.h> |
6908bdbd | 42 | #include <unistd.h> |
b76c2afc | 43 | #include <time.h> |
b801d8b2 JF |
44 | |
45 | #include <curses.h> | |
b801d8b2 JF |
46 | |
47 | static void die(const char *err, ...); | |
48 | static void report(const char *msg, ...); | |
1ba2ae4b | 49 | static void set_nonblocking_input(bool loading); |
6b161b31 JF |
50 | |
51 | #define ABS(x) ((x) >= 0 ? (x) : -(x)) | |
52 | #define MIN(x, y) ((x) < (y) ? (x) : (y)) | |
53 | ||
54 | #define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0])) | |
55 | #define STRING_SIZE(x) (sizeof(x) - 1) | |
b76c2afc | 56 | |
2e8488b4 JF |
57 | #define SIZEOF_REF 256 /* Size of symbolic or SHA1 ID. */ |
58 | #define SIZEOF_CMD 1024 /* Size of command buffer. */ | |
b801d8b2 | 59 | |
82e78006 JF |
60 | /* This color name can be used to refer to the default term colors. */ |
61 | #define COLOR_DEFAULT (-1) | |
78c70acd | 62 | |
82e78006 | 63 | /* The format and size of the date column in the main view. */ |
4c6fabc2 | 64 | #define DATE_FORMAT "%Y-%m-%d %H:%M" |
6b161b31 | 65 | #define DATE_COLS STRING_SIZE("2006-04-29 14:21 ") |
4c6fabc2 | 66 | |
a28bcc22 | 67 | /* The default interval between line numbers. */ |
4a2909a7 | 68 | #define NUMBER_INTERVAL 1 |
82e78006 | 69 | |
a28bcc22 JF |
70 | #define SCALE_SPLIT_VIEW(height) ((height) * 2 / 3) |
71 | ||
8855ada4 | 72 | /* Some ascii-shorthands fitted into the ncurses namespace. */ |
a28bcc22 JF |
73 | #define KEY_TAB '\t' |
74 | #define KEY_RETURN '\r' | |
4a2909a7 JF |
75 | #define KEY_ESC 27 |
76 | ||
a28bcc22 | 77 | /* User action requests. */ |
4a2909a7 | 78 | enum request { |
a28bcc22 JF |
79 | /* Offset all requests to avoid conflicts with ncurses getch values. */ |
80 | REQ_OFFSET = KEY_MAX + 1, | |
81 | ||
4a2909a7 | 82 | /* XXX: Keep the view request first and in sync with views[]. */ |
2e8488b4 | 83 | REQ_VIEW_MAIN, |
4a2909a7 JF |
84 | REQ_VIEW_DIFF, |
85 | REQ_VIEW_LOG, | |
2e8488b4 | 86 | REQ_VIEW_HELP, |
6908bdbd | 87 | REQ_VIEW_PAGER, |
4a2909a7 | 88 | |
6b161b31 | 89 | REQ_ENTER, |
03a93dbb JF |
90 | REQ_QUIT, |
91 | REQ_PROMPT, | |
4a2909a7 | 92 | REQ_SCREEN_REDRAW, |
fac7db6c | 93 | REQ_SCREEN_RESIZE, |
4a2909a7 | 94 | REQ_SCREEN_UPDATE, |
03a93dbb JF |
95 | REQ_SHOW_VERSION, |
96 | REQ_STOP_LOADING, | |
4a2909a7 | 97 | REQ_TOGGLE_LINE_NUMBERS, |
03a93dbb | 98 | REQ_VIEW_NEXT, |
4a2909a7 JF |
99 | |
100 | REQ_MOVE_UP, | |
101 | REQ_MOVE_DOWN, | |
102 | REQ_MOVE_PAGE_UP, | |
103 | REQ_MOVE_PAGE_DOWN, | |
104 | REQ_MOVE_FIRST_LINE, | |
105 | REQ_MOVE_LAST_LINE, | |
106 | ||
107 | REQ_SCROLL_LINE_UP, | |
108 | REQ_SCROLL_LINE_DOWN, | |
109 | REQ_SCROLL_PAGE_UP, | |
110 | REQ_SCROLL_PAGE_DOWN, | |
111 | }; | |
112 | ||
c34d9c9f JF |
113 | struct ref { |
114 | char *name; | |
115 | char id[41]; | |
116 | unsigned int tag:1; | |
117 | unsigned int next:1; | |
118 | }; | |
119 | ||
4c6fabc2 | 120 | struct commit { |
a28bcc22 | 121 | char id[41]; /* SHA1 ID. */ |
6b161b31 | 122 | char title[75]; /* The first line of the commit message. */ |
a28bcc22 JF |
123 | char author[75]; /* The author of the commit. */ |
124 | struct tm time; /* Date from the author ident. */ | |
c34d9c9f | 125 | struct ref **refs; /* Repository references; tags & branch heads. */ |
4c6fabc2 JF |
126 | }; |
127 | ||
03a93dbb JF |
128 | /* |
129 | * String helpers | |
130 | */ | |
78c70acd | 131 | |
82e78006 JF |
132 | static inline void |
133 | string_ncopy(char *dst, char *src, int dstlen) | |
134 | { | |
135 | strncpy(dst, src, dstlen - 1); | |
136 | dst[dstlen - 1] = 0; | |
03a93dbb | 137 | |
82e78006 JF |
138 | } |
139 | ||
140 | /* Shorthand for safely copying into a fixed buffer. */ | |
141 | #define string_copy(dst, src) \ | |
142 | string_ncopy(dst, src, sizeof(dst)) | |
143 | ||
03a93dbb JF |
144 | /* Shell quoting |
145 | * | |
146 | * NOTE: The following is a slightly modified copy of the git project's shell | |
147 | * quoting routines found in the quote.c file. | |
148 | * | |
149 | * Help to copy the thing properly quoted for the shell safety. any single | |
150 | * quote is replaced with '\'', any exclamation point is replaced with '\!', | |
151 | * and the whole thing is enclosed in a | |
152 | * | |
153 | * E.g. | |
154 | * original sq_quote result | |
155 | * name ==> name ==> 'name' | |
156 | * a b ==> a b ==> 'a b' | |
157 | * a'b ==> a'\''b ==> 'a'\''b' | |
158 | * a!b ==> a'\!'b ==> 'a'\!'b' | |
159 | */ | |
160 | ||
161 | static size_t | |
162 | sq_quote(char buf[SIZEOF_CMD], size_t bufsize, const char *src) | |
163 | { | |
164 | char c; | |
165 | ||
166 | #define BUFPUT(x) ( (bufsize < SIZEOF_CMD) && (buf[bufsize++] = (x)) ) | |
167 | ||
168 | BUFPUT('\''); | |
169 | while ((c = *src++)) { | |
170 | if (c == '\'' || c == '!') { | |
171 | BUFPUT('\''); | |
172 | BUFPUT('\\'); | |
173 | BUFPUT(c); | |
174 | BUFPUT('\''); | |
175 | } else { | |
176 | BUFPUT(c); | |
177 | } | |
178 | } | |
179 | BUFPUT('\''); | |
180 | ||
181 | return bufsize; | |
182 | } | |
183 | ||
82e78006 | 184 | |
b76c2afc JF |
185 | /** |
186 | * OPTIONS | |
187 | * ------- | |
188 | **/ | |
189 | ||
2e8488b4 JF |
190 | static int opt_line_number = FALSE; |
191 | static int opt_num_interval = NUMBER_INTERVAL; | |
6b161b31 | 192 | static enum request opt_request = REQ_VIEW_MAIN; |
03a93dbb | 193 | static char opt_cmd[SIZEOF_CMD] = ""; |
6908bdbd | 194 | static FILE *opt_pipe = NULL; |
b76c2afc | 195 | |
b76c2afc | 196 | /* Returns the index of log or diff command or -1 to exit. */ |
8855ada4 | 197 | static bool |
b76c2afc JF |
198 | parse_options(int argc, char *argv[]) |
199 | { | |
200 | int i; | |
201 | ||
202 | for (i = 1; i < argc; i++) { | |
203 | char *opt = argv[i]; | |
204 | ||
205 | /** | |
b76c2afc | 206 | * -l:: |
1ba2ae4b | 207 | * Start up in log view using the internal log command. |
b76c2afc | 208 | **/ |
6b161b31 | 209 | if (!strcmp(opt, "-l")) { |
4a2909a7 | 210 | opt_request = REQ_VIEW_LOG; |
6b161b31 JF |
211 | continue; |
212 | } | |
b76c2afc JF |
213 | |
214 | /** | |
215 | * -d:: | |
1ba2ae4b | 216 | * Start up in diff view using the internal diff command. |
b76c2afc | 217 | **/ |
6b161b31 | 218 | if (!strcmp(opt, "-d")) { |
4a2909a7 | 219 | opt_request = REQ_VIEW_DIFF; |
6b161b31 JF |
220 | continue; |
221 | } | |
b76c2afc JF |
222 | |
223 | /** | |
2e8488b4 | 224 | * -n[INTERVAL], --line-number[=INTERVAL]:: |
b76c2afc | 225 | * Prefix line numbers in log and diff view. |
2e8488b4 | 226 | * Optionally, with interval different than each line. |
b76c2afc | 227 | **/ |
6b161b31 | 228 | if (!strncmp(opt, "-n", 2) || |
8855ada4 | 229 | !strncmp(opt, "--line-number", 13)) { |
2e8488b4 JF |
230 | char *num = opt; |
231 | ||
232 | if (opt[1] == 'n') { | |
233 | num = opt + 2; | |
234 | ||
235 | } else if (opt[STRING_SIZE("--line-number")] == '=') { | |
236 | num = opt + STRING_SIZE("--line-number="); | |
237 | } | |
238 | ||
239 | if (isdigit(*num)) | |
240 | opt_num_interval = atoi(num); | |
241 | ||
6b161b31 JF |
242 | opt_line_number = TRUE; |
243 | continue; | |
244 | } | |
b76c2afc JF |
245 | |
246 | /** | |
247 | * -v, --version:: | |
248 | * Show version and exit. | |
249 | **/ | |
6b161b31 | 250 | if (!strcmp(opt, "-v") || |
8855ada4 | 251 | !strcmp(opt, "--version")) { |
b76c2afc | 252 | printf("tig version %s\n", VERSION); |
8855ada4 | 253 | return FALSE; |
6b161b31 | 254 | } |
b76c2afc JF |
255 | |
256 | /** | |
03a93dbb | 257 | * \--:: |
889cb462 | 258 | * End of tig(1) options. Useful when specifying commands |
03a93dbb JF |
259 | * for the main view. Example: |
260 | * | |
889cb462 | 261 | * $ tig -- --since=1.month |
b76c2afc | 262 | **/ |
6908bdbd JF |
263 | if (!strcmp(opt, "--")) { |
264 | i++; | |
265 | break; | |
266 | } | |
03a93dbb | 267 | |
8855ada4 JF |
268 | /** |
269 | * log [options]:: | |
270 | * Open log view using the given git log options. | |
271 | * | |
272 | * diff [options]:: | |
273 | * Open diff view using the given git diff options. | |
274 | * | |
275 | * show [options]:: | |
276 | * Open diff view using the given git show options. | |
277 | **/ | |
278 | if (!strcmp(opt, "log") || | |
279 | !strcmp(opt, "diff") || | |
280 | !strcmp(opt, "show")) { | |
281 | opt_request = opt[0] == 'l' | |
282 | ? REQ_VIEW_LOG : REQ_VIEW_DIFF; | |
283 | break; | |
284 | } | |
285 | ||
286 | /* Make stuff like: | |
889cb462 | 287 | * |
03a93dbb JF |
288 | * $ tig tag-1.0..HEAD |
289 | * | |
8855ada4 | 290 | * work. */ |
03a93dbb | 291 | if (opt[0] && opt[0] != '-') |
6908bdbd | 292 | break; |
6b161b31 JF |
293 | |
294 | die("unknown command '%s'", opt); | |
b76c2afc JF |
295 | } |
296 | ||
6908bdbd | 297 | if (!isatty(STDIN_FILENO)) { |
8855ada4 JF |
298 | /** |
299 | * Pager mode | |
300 | * ~~~~~~~~~~ | |
301 | * If stdin is a pipe, any log or diff options will be ignored and the | |
302 | * pager view will be opened loading data from stdin. The pager mode | |
303 | * can be used for colorizing output from various git commands. | |
304 | * | |
305 | * Example on how to colorize the output of git-show(1): | |
306 | * | |
307 | * $ git show | tig | |
308 | **/ | |
6908bdbd JF |
309 | opt_request = REQ_VIEW_PAGER; |
310 | opt_pipe = stdin; | |
311 | ||
312 | } else if (i < argc) { | |
313 | size_t buf_size; | |
314 | ||
8855ada4 JF |
315 | /** |
316 | * Git command options | |
317 | * ~~~~~~~~~~~~~~~~~~~ | |
8855ada4 JF |
318 | * All git command options specified on the command line will |
319 | * be passed to the given command and all will be shell quoted | |
320 | * before used. | |
321 | * | |
322 | * NOTE: It is possible to specify options even for the main | |
eb98559e | 323 | * view. If doing this you should not touch the `--pretty` |
8855ada4 JF |
324 | * option. |
325 | * | |
326 | * Example on how to open the log view and show both author and | |
327 | * committer information: | |
328 | * | |
329 | * $ tig log --pretty=fuller | |
330 | **/ | |
331 | ||
6908bdbd | 332 | if (opt_request == REQ_VIEW_MAIN) |
8855ada4 JF |
333 | /* XXX: This is vulnerable to the user overriding |
334 | * options required for the main view parser. */ | |
6908bdbd JF |
335 | string_copy(opt_cmd, "git log --stat --pretty=raw"); |
336 | else | |
337 | string_copy(opt_cmd, "git"); | |
338 | buf_size = strlen(opt_cmd); | |
339 | ||
340 | while (buf_size < sizeof(opt_cmd) && i < argc) { | |
341 | opt_cmd[buf_size++] = ' '; | |
342 | buf_size = sq_quote(opt_cmd, buf_size, argv[i++]); | |
343 | } | |
344 | ||
345 | if (buf_size >= sizeof(opt_cmd)) | |
346 | die("command too long"); | |
347 | ||
348 | opt_cmd[buf_size] = 0; | |
349 | ||
350 | } | |
351 | ||
8855ada4 | 352 | return TRUE; |
b76c2afc JF |
353 | } |
354 | ||
355 | ||
4a2909a7 JF |
356 | /** |
357 | * KEYS | |
358 | * ---- | |
4a2909a7 JF |
359 | **/ |
360 | ||
361 | #define HELP "(d)iff, (l)og, (m)ain, (q)uit, (v)ersion, (h)elp" | |
362 | ||
363 | struct keymap { | |
364 | int alias; | |
365 | int request; | |
366 | }; | |
367 | ||
368 | struct keymap keymap[] = { | |
6b161b31 JF |
369 | /** |
370 | * View switching | |
371 | * ~~~~~~~~~~~~~~ | |
372 | * d:: | |
373 | * Switch to diff view. | |
374 | * l:: | |
375 | * Switch to log view. | |
376 | * m:: | |
377 | * Switch to main view. | |
8855ada4 JF |
378 | * p:: |
379 | * Switch to pager view. | |
6b161b31 JF |
380 | * h:: |
381 | * Show man page. | |
382 | * Return:: | |
383 | * If in main view split the view | |
384 | * and show the diff in the bottom view. | |
03a93dbb JF |
385 | * Tab:: |
386 | * Switch to next view. | |
6b161b31 JF |
387 | **/ |
388 | { 'm', REQ_VIEW_MAIN }, | |
389 | { 'd', REQ_VIEW_DIFF }, | |
390 | { 'l', REQ_VIEW_LOG }, | |
8855ada4 | 391 | { 'p', REQ_VIEW_PAGER }, |
6b161b31 JF |
392 | { 'h', REQ_VIEW_HELP }, |
393 | ||
03a93dbb | 394 | { KEY_TAB, REQ_VIEW_NEXT }, |
6b161b31 JF |
395 | { KEY_RETURN, REQ_ENTER }, |
396 | ||
397 | /** | |
398 | * Cursor navigation | |
399 | * ~~~~~~~~~~~~~~~~~ | |
400 | * Up, k:: | |
401 | * Move curser one line up. | |
402 | * Down, j:: | |
403 | * Move cursor one line down. | |
404 | * Page Up:: | |
405 | * Move curser one page up. | |
406 | * Page Down:: | |
407 | * Move cursor one page down. | |
408 | * Home:: | |
409 | * Jump to first line. | |
410 | * End:: | |
411 | * Jump to last line. | |
412 | **/ | |
4a2909a7 JF |
413 | { KEY_UP, REQ_MOVE_UP }, |
414 | { 'k', REQ_MOVE_UP }, | |
415 | { KEY_DOWN, REQ_MOVE_DOWN }, | |
416 | { 'j', REQ_MOVE_DOWN }, | |
417 | { KEY_HOME, REQ_MOVE_FIRST_LINE }, | |
418 | { KEY_END, REQ_MOVE_LAST_LINE }, | |
419 | { KEY_NPAGE, REQ_MOVE_PAGE_DOWN }, | |
420 | { KEY_PPAGE, REQ_MOVE_PAGE_UP }, | |
421 | ||
6b161b31 JF |
422 | /** |
423 | * Scrolling | |
424 | * ~~~~~~~~~ | |
425 | * Insert:: | |
426 | * Scroll view one line up. | |
427 | * Delete:: | |
428 | * Scroll view one line down. | |
429 | * w:: | |
430 | * Scroll view one page up. | |
431 | * s:: | |
432 | * Scroll view one page down. | |
433 | **/ | |
a28bcc22 JF |
434 | { KEY_IC, REQ_SCROLL_LINE_UP }, |
435 | { KEY_DC, REQ_SCROLL_LINE_DOWN }, | |
436 | { 'w', REQ_SCROLL_PAGE_UP }, | |
437 | { 's', REQ_SCROLL_PAGE_DOWN }, | |
438 | ||
6b161b31 JF |
439 | /** |
440 | * Misc | |
441 | * ~~~~ | |
442 | * q, Escape:: | |
443 | * Quit | |
444 | * r:: | |
445 | * Redraw screen. | |
446 | * z:: | |
447 | * Stop all background loading. | |
448 | * v:: | |
449 | * Show version. | |
450 | * n:: | |
451 | * Toggle line numbers on/off. | |
03a93dbb | 452 | * ':':: |
8855ada4 JF |
453 | * Open prompt. This allows you to specify what git command to run. |
454 | * Example: | |
455 | * | |
456 | * :log -p | |
457 | * | |
6b161b31 | 458 | **/ |
4a2909a7 JF |
459 | { KEY_ESC, REQ_QUIT }, |
460 | { 'q', REQ_QUIT }, | |
461 | { 'z', REQ_STOP_LOADING }, | |
462 | { 'v', REQ_SHOW_VERSION }, | |
463 | { 'r', REQ_SCREEN_REDRAW }, | |
464 | { 'n', REQ_TOGGLE_LINE_NUMBERS }, | |
03a93dbb | 465 | { ':', REQ_PROMPT }, |
4a2909a7 JF |
466 | |
467 | /* wgetch() with nodelay() enabled returns ERR when there's no input. */ | |
468 | { ERR, REQ_SCREEN_UPDATE }, | |
4b76734f | 469 | |
fac7db6c JF |
470 | /* Use the ncurses SIGWINCH handler. */ |
471 | { KEY_RESIZE, REQ_SCREEN_RESIZE }, | |
4a2909a7 JF |
472 | }; |
473 | ||
6b161b31 JF |
474 | static enum request |
475 | get_request(int key) | |
4a2909a7 JF |
476 | { |
477 | int i; | |
478 | ||
479 | for (i = 0; i < ARRAY_SIZE(keymap); i++) | |
6b161b31 | 480 | if (keymap[i].alias == key) |
4a2909a7 JF |
481 | return keymap[i].request; |
482 | ||
6b161b31 | 483 | return (enum request) key; |
4a2909a7 JF |
484 | } |
485 | ||
486 | ||
b76c2afc JF |
487 | /* |
488 | * Line-oriented content detection. | |
6b161b31 JF |
489 | */ |
490 | ||
2e8488b4 | 491 | #define LINE_INFO \ |
6b161b31 JF |
492 | /* Line type String to match Foreground Background Attributes |
493 | * --------- --------------- ---------- ---------- ---------- */ \ | |
a28bcc22 JF |
494 | /* Diff markup */ \ |
495 | LINE(DIFF, "diff --git ", COLOR_YELLOW, COLOR_DEFAULT, 0), \ | |
8855ada4 | 496 | LINE(DIFF_INDEX, "index ", COLOR_BLUE, COLOR_DEFAULT, 0), \ |
a28bcc22 JF |
497 | LINE(DIFF_CHUNK, "@@", COLOR_MAGENTA, COLOR_DEFAULT, 0), \ |
498 | LINE(DIFF_ADD, "+", COLOR_GREEN, COLOR_DEFAULT, 0), \ | |
499 | LINE(DIFF_DEL, "-", COLOR_RED, COLOR_DEFAULT, 0), \ | |
3af8774e JF |
500 | LINE(DIFF_OLDMODE, "old file mode ", COLOR_YELLOW, COLOR_DEFAULT, 0), \ |
501 | LINE(DIFF_NEWMODE, "new file mode ", COLOR_YELLOW, COLOR_DEFAULT, 0), \ | |
a28bcc22 JF |
502 | LINE(DIFF_COPY, "copy ", COLOR_YELLOW, COLOR_DEFAULT, 0), \ |
503 | LINE(DIFF_RENAME, "rename ", COLOR_YELLOW, COLOR_DEFAULT, 0), \ | |
504 | LINE(DIFF_SIM, "similarity ", COLOR_YELLOW, COLOR_DEFAULT, 0), \ | |
505 | LINE(DIFF_DISSIM, "dissimilarity ", COLOR_YELLOW, COLOR_DEFAULT, 0), \ | |
506 | /* Pretty print commit header */ \ | |
6908bdbd | 507 | LINE(PP_AUTHOR, "Author: ", COLOR_CYAN, COLOR_DEFAULT, 0), \ |
8855ada4 | 508 | LINE(PP_COMMIT, "Commit: ", COLOR_MAGENTA, COLOR_DEFAULT, 0), \ |
6908bdbd JF |
509 | LINE(PP_MERGE, "Merge: ", COLOR_BLUE, COLOR_DEFAULT, 0), \ |
510 | LINE(PP_DATE, "Date: ", COLOR_YELLOW, COLOR_DEFAULT, 0), \ | |
8855ada4 JF |
511 | LINE(PP_ADATE, "AuthorDate: ", COLOR_YELLOW, COLOR_DEFAULT, 0), \ |
512 | LINE(PP_CDATE, "CommitDate: ", COLOR_YELLOW, COLOR_DEFAULT, 0), \ | |
a28bcc22 JF |
513 | /* Raw commit header */ \ |
514 | LINE(COMMIT, "commit ", COLOR_GREEN, COLOR_DEFAULT, 0), \ | |
515 | LINE(PARENT, "parent ", COLOR_BLUE, COLOR_DEFAULT, 0), \ | |
516 | LINE(TREE, "tree ", COLOR_BLUE, COLOR_DEFAULT, 0), \ | |
8855ada4 | 517 | LINE(AUTHOR, "author ", COLOR_CYAN, COLOR_DEFAULT, 0), \ |
a28bcc22 JF |
518 | LINE(COMMITTER, "committer ", COLOR_MAGENTA, COLOR_DEFAULT, 0), \ |
519 | /* Misc */ \ | |
520 | LINE(DIFF_TREE, "diff-tree ", COLOR_BLUE, COLOR_DEFAULT, 0), \ | |
521 | LINE(SIGNOFF, " Signed-off-by", COLOR_YELLOW, COLOR_DEFAULT, 0), \ | |
522 | /* UI colors */ \ | |
523 | LINE(DEFAULT, "", COLOR_DEFAULT, COLOR_DEFAULT, A_NORMAL), \ | |
524 | LINE(CURSOR, "", COLOR_WHITE, COLOR_GREEN, A_BOLD), \ | |
525 | LINE(STATUS, "", COLOR_GREEN, COLOR_DEFAULT, 0), \ | |
6b161b31 JF |
526 | LINE(TITLE_BLUR, "", COLOR_WHITE, COLOR_BLUE, 0), \ |
527 | LINE(TITLE_FOCUS, "", COLOR_WHITE, COLOR_BLUE, A_BOLD), \ | |
a28bcc22 JF |
528 | LINE(MAIN_DATE, "", COLOR_BLUE, COLOR_DEFAULT, 0), \ |
529 | LINE(MAIN_AUTHOR, "", COLOR_GREEN, COLOR_DEFAULT, 0), \ | |
530 | LINE(MAIN_COMMIT, "", COLOR_DEFAULT, COLOR_DEFAULT, 0), \ | |
c34d9c9f JF |
531 | LINE(MAIN_DELIM, "", COLOR_MAGENTA, COLOR_DEFAULT, 0), \ |
532 | LINE(MAIN_TAG, "", COLOR_MAGENTA, COLOR_DEFAULT, A_BOLD), \ | |
533 | LINE(MAIN_REF, "", COLOR_CYAN, COLOR_DEFAULT, A_BOLD), | |
2e8488b4 | 534 | |
78c70acd | 535 | enum line_type { |
2e8488b4 JF |
536 | #define LINE(type, line, fg, bg, attr) \ |
537 | LINE_##type | |
538 | LINE_INFO | |
539 | #undef LINE | |
78c70acd JF |
540 | }; |
541 | ||
542 | struct line_info { | |
2e8488b4 JF |
543 | char *line; /* The start of line to match. */ |
544 | int linelen; /* Size of string to match. */ | |
545 | int fg, bg, attr; /* Color and text attributes for the lines. */ | |
78c70acd JF |
546 | }; |
547 | ||
2e8488b4 | 548 | static struct line_info line_info[] = { |
78c70acd | 549 | #define LINE(type, line, fg, bg, attr) \ |
a28bcc22 | 550 | { (line), STRING_SIZE(line), (fg), (bg), (attr) } |
2e8488b4 JF |
551 | LINE_INFO |
552 | #undef LINE | |
78c70acd JF |
553 | }; |
554 | ||
2e8488b4 JF |
555 | static enum line_type |
556 | get_line_type(char *line) | |
78c70acd JF |
557 | { |
558 | int linelen = strlen(line); | |
a28bcc22 | 559 | enum line_type type; |
78c70acd | 560 | |
a28bcc22 | 561 | for (type = 0; type < ARRAY_SIZE(line_info); type++) |
2e8488b4 | 562 | /* Case insensitive search matches Signed-off-by lines better. */ |
a28bcc22 JF |
563 | if (linelen >= line_info[type].linelen && |
564 | !strncasecmp(line_info[type].line, line, line_info[type].linelen)) | |
565 | return type; | |
78c70acd | 566 | |
2e8488b4 | 567 | return LINE_DEFAULT; |
78c70acd JF |
568 | } |
569 | ||
2e8488b4 | 570 | static inline int |
78c70acd JF |
571 | get_line_attr(enum line_type type) |
572 | { | |
2e8488b4 JF |
573 | assert(type < ARRAY_SIZE(line_info)); |
574 | return COLOR_PAIR(type) | line_info[type].attr; | |
78c70acd JF |
575 | } |
576 | ||
577 | static void | |
578 | init_colors(void) | |
579 | { | |
82e78006 JF |
580 | int default_bg = COLOR_BLACK; |
581 | int default_fg = COLOR_WHITE; | |
a28bcc22 | 582 | enum line_type type; |
78c70acd JF |
583 | |
584 | start_color(); | |
585 | ||
586 | if (use_default_colors() != ERR) { | |
82e78006 JF |
587 | default_bg = -1; |
588 | default_fg = -1; | |
78c70acd JF |
589 | } |
590 | ||
a28bcc22 JF |
591 | for (type = 0; type < ARRAY_SIZE(line_info); type++) { |
592 | struct line_info *info = &line_info[type]; | |
82e78006 JF |
593 | int bg = info->bg == COLOR_DEFAULT ? default_bg : info->bg; |
594 | int fg = info->fg == COLOR_DEFAULT ? default_fg : info->fg; | |
78c70acd | 595 | |
a28bcc22 | 596 | init_pair(type, fg, bg); |
78c70acd JF |
597 | } |
598 | } | |
599 | ||
600 | ||
1ba2ae4b JF |
601 | /** |
602 | * ENVIRONMENT VARIABLES | |
603 | * --------------------- | |
c34d9c9f JF |
604 | * Several options related to the interface with git can be configured |
605 | * via environment options. | |
606 | * | |
607 | * Repository references | |
608 | * ~~~~~~~~~~~~~~~~~~~~~ | |
609 | * Commits that are referenced by tags and branch heads will be marked | |
610 | * by the reference name surrounded by '[' and ']': | |
611 | * | |
612 | * 2006-04-18 23:12 Jonas Fonseca | [tig-0.2] tig version 0.2 | |
613 | * | |
614 | * If you want to filter out certain directories under `.git/refs/`, say | |
615 | * `tmp` you can do it by setting the following variable: | |
616 | * | |
617 | * $ TIG_LS_REMOTE="git ls-remote | sed '/\/tmp\//d'" | |
618 | * | |
619 | * Or set the variable permanently in your environment. | |
620 | * | |
621 | * TIG_LS_REMOTE:: | |
622 | * Set command for retrieving all repository references.private | |
623 | **/ | |
624 | ||
625 | #define TIG_LS_REMOTE \ | |
626 | "git ls-remote ." | |
627 | ||
628 | /** | |
629 | * View commands | |
630 | * ~~~~~~~~~~~~~ | |
1ba2ae4b JF |
631 | * It is possible to alter which commands are used for the different views. |
632 | * If for example you prefer commits in the main to be sorted by date and | |
633 | * only show 500 commits, use: | |
634 | * | |
635 | * $ TIG_MAIN_CMD="git log --date-order -n500 --pretty=raw %s" tig | |
636 | * | |
637 | * Or set the variable permanently in your environment. | |
638 | * | |
639 | * Notice, how `%s` is used to specify the commit reference. There can | |
640 | * be a maximum of 5 `%s` ref specifications. | |
641 | * | |
642 | * TIG_DIFF_CMD:: | |
643 | * The command used for the diff view. By default, git show is used | |
644 | * as a backend. | |
645 | * | |
646 | * TIG_LOG_CMD:: | |
647 | * The command used for the log view. | |
648 | * | |
649 | * TIG_MAIN_CMD:: | |
650 | * The command used for the main view. Note, you must always specify | |
651 | * the option: `--pretty=raw` since the main view parser expects to | |
652 | * read that format. | |
653 | **/ | |
654 | ||
655 | #define TIG_DIFF_CMD \ | |
656 | "git show --patch-with-stat --find-copies-harder -B -C %s" | |
657 | ||
658 | #define TIG_LOG_CMD \ | |
659 | "git log --cc --stat -n100 %s" | |
660 | ||
661 | #define TIG_MAIN_CMD \ | |
662 | "git log --topo-order --stat --pretty=raw %s" | |
663 | ||
664 | /* We silently ignore that the following are also exported. */ | |
665 | ||
666 | #define TIG_HELP_CMD \ | |
667 | "man tig 2> /dev/null" | |
668 | ||
669 | #define TIG_PAGER_CMD \ | |
670 | "" | |
671 | ||
672 | ||
b801d8b2 JF |
673 | /* |
674 | * Viewer | |
675 | */ | |
676 | ||
677 | struct view { | |
03a93dbb | 678 | const char *name; /* View name */ |
1ba2ae4b JF |
679 | char *cmd_fmt; /* Default command line format */ |
680 | char *cmd_env; /* Command line set via environment */ | |
03a93dbb JF |
681 | char *id; /* Points to either of ref_{head,commit} */ |
682 | size_t objsize; /* Size of objects in the line index */ | |
6b161b31 JF |
683 | |
684 | struct view_ops { | |
6734f6b9 JF |
685 | /* What type of content being displayed. Used in the |
686 | * title bar. */ | |
687 | char *type; | |
8855ada4 | 688 | /* Draw one line; @lineno must be < view->height. */ |
6b161b31 | 689 | bool (*draw)(struct view *view, unsigned int lineno); |
8855ada4 | 690 | /* Read one line; updates view->line. */ |
6b161b31 | 691 | bool (*read)(struct view *view, char *line); |
8855ada4 | 692 | /* Depending on view, change display based on current line. */ |
6b161b31 JF |
693 | bool (*enter)(struct view *view); |
694 | } *ops; | |
22f66b0a | 695 | |
03a93dbb | 696 | char cmd[SIZEOF_CMD]; /* Command buffer */ |
49f2b43f JF |
697 | char ref[SIZEOF_REF]; /* Hovered commit reference */ |
698 | char vid[SIZEOF_REF]; /* View ID. Set to id member when updating. */ | |
2e8488b4 | 699 | |
8855ada4 JF |
700 | int height, width; /* The width and height of the main window */ |
701 | WINDOW *win; /* The main window */ | |
702 | WINDOW *title; /* The title window living below the main window */ | |
b801d8b2 JF |
703 | |
704 | /* Navigation */ | |
705 | unsigned long offset; /* Offset of the window top */ | |
706 | unsigned long lineno; /* Current line number */ | |
707 | ||
708 | /* Buffering */ | |
709 | unsigned long lines; /* Total number of lines */ | |
8855ada4 JF |
710 | void **line; /* Line index; each line contains user data */ |
711 | unsigned int digits; /* Number of digits in the lines member. */ | |
b801d8b2 JF |
712 | |
713 | /* Loading */ | |
714 | FILE *pipe; | |
2e8488b4 | 715 | time_t start_time; |
b801d8b2 JF |
716 | }; |
717 | ||
6b161b31 JF |
718 | static struct view_ops pager_ops; |
719 | static struct view_ops main_ops; | |
a28bcc22 | 720 | |
49f2b43f JF |
721 | char ref_head[SIZEOF_REF] = "HEAD"; |
722 | char ref_commit[SIZEOF_REF] = "HEAD"; | |
723 | ||
1ba2ae4b JF |
724 | #define VIEW_STR(name, cmd, env, ref, objsize, ops) \ |
725 | { name, cmd, #env, ref, objsize, ops } | |
726 | ||
727 | #define VIEW_(id, name, ops, ref, objsize) \ | |
728 | VIEW_STR(name, TIG_##id##_CMD, TIG_##id##_CMD, ref, objsize, ops) | |
729 | ||
b801d8b2 | 730 | static struct view views[] = { |
1ba2ae4b JF |
731 | VIEW_(MAIN, "main", &main_ops, ref_head, sizeof(struct commit)), |
732 | VIEW_(DIFF, "diff", &pager_ops, ref_commit, sizeof(char)), | |
733 | VIEW_(LOG, "log", &pager_ops, ref_head, sizeof(char)), | |
734 | VIEW_(HELP, "help", &pager_ops, ref_head, sizeof(char)), | |
735 | VIEW_(PAGER, "pager", &pager_ops, "static", sizeof(char)), | |
b801d8b2 JF |
736 | }; |
737 | ||
a28bcc22 JF |
738 | #define VIEW(req) (&views[(req) - REQ_OFFSET - 1]) |
739 | ||
4c6fabc2 | 740 | /* The display array of active views and the index of the current view. */ |
6b161b31 | 741 | static struct view *display[2]; |
4c6fabc2 | 742 | static unsigned int current_view; |
22f66b0a | 743 | |
b801d8b2 | 744 | #define foreach_view(view, i) \ |
6b161b31 | 745 | for (i = 0; i < ARRAY_SIZE(display) && (view = display[i]); i++) |
b801d8b2 | 746 | |
4c6fabc2 | 747 | |
b801d8b2 | 748 | static void |
82e78006 | 749 | redraw_view_from(struct view *view, int lineno) |
b801d8b2 | 750 | { |
82e78006 | 751 | assert(0 <= lineno && lineno < view->height); |
b801d8b2 | 752 | |
82e78006 | 753 | for (; lineno < view->height; lineno++) { |
6b161b31 | 754 | if (!view->ops->draw(view, lineno)) |
fd85fef1 | 755 | break; |
b801d8b2 JF |
756 | } |
757 | ||
758 | redrawwin(view->win); | |
759 | wrefresh(view->win); | |
760 | } | |
761 | ||
b76c2afc | 762 | static void |
82e78006 JF |
763 | redraw_view(struct view *view) |
764 | { | |
765 | wclear(view->win); | |
766 | redraw_view_from(view, 0); | |
767 | } | |
768 | ||
6b161b31 JF |
769 | static void |
770 | resize_display(void) | |
b76c2afc | 771 | { |
03a93dbb | 772 | int offset, i; |
6b161b31 JF |
773 | struct view *base = display[0]; |
774 | struct view *view = display[1] ? display[1] : display[0]; | |
b76c2afc | 775 | |
6b161b31 | 776 | /* Setup window dimensions */ |
b76c2afc | 777 | |
03a93dbb | 778 | getmaxyx(stdscr, base->height, base->width); |
b76c2afc | 779 | |
6b161b31 | 780 | /* Make room for the status window. */ |
03a93dbb | 781 | base->height -= 1; |
6b161b31 JF |
782 | |
783 | if (view != base) { | |
03a93dbb JF |
784 | /* Horizontal split. */ |
785 | view->width = base->width; | |
6b161b31 JF |
786 | view->height = SCALE_SPLIT_VIEW(base->height); |
787 | base->height -= view->height; | |
788 | ||
789 | /* Make room for the title bar. */ | |
790 | view->height -= 1; | |
791 | } | |
792 | ||
793 | /* Make room for the title bar. */ | |
794 | base->height -= 1; | |
795 | ||
796 | offset = 0; | |
797 | ||
798 | foreach_view (view, i) { | |
b76c2afc | 799 | if (!view->win) { |
6b161b31 JF |
800 | view->win = newwin(view->height, 0, offset, 0); |
801 | if (!view->win) | |
802 | die("Failed to create %s view", view->name); | |
803 | ||
804 | scrollok(view->win, TRUE); | |
805 | ||
806 | view->title = newwin(1, 0, offset + view->height, 0); | |
807 | if (!view->title) | |
808 | die("Failed to create title window"); | |
809 | ||
810 | } else { | |
811 | wresize(view->win, view->height, view->width); | |
812 | mvwin(view->win, offset, 0); | |
813 | mvwin(view->title, offset + view->height, 0); | |
814 | wrefresh(view->win); | |
a28bcc22 | 815 | } |
a28bcc22 | 816 | |
6b161b31 | 817 | offset += view->height + 1; |
b76c2afc | 818 | } |
6b161b31 | 819 | } |
b76c2afc | 820 | |
6b161b31 JF |
821 | static void |
822 | update_view_title(struct view *view) | |
823 | { | |
824 | if (view == display[current_view]) | |
825 | wbkgdset(view->title, get_line_attr(LINE_TITLE_FOCUS)); | |
826 | else | |
827 | wbkgdset(view->title, get_line_attr(LINE_TITLE_BLUR)); | |
a28bcc22 | 828 | |
6b161b31 JF |
829 | werase(view->title); |
830 | wmove(view->title, 0, 0); | |
b76c2afc | 831 | |
6b161b31 | 832 | /* [main] ref: 334b506... - commit 6 of 4383 (0%) */ |
6908bdbd JF |
833 | |
834 | if (*view->ref) | |
835 | wprintw(view->title, "[%s] ref: %s", view->name, view->ref); | |
836 | else | |
837 | wprintw(view->title, "[%s]", view->name); | |
838 | ||
6b161b31 | 839 | if (view->lines) { |
6b161b31 | 840 | wprintw(view->title, " - %s %d of %d (%d%%)", |
6734f6b9 | 841 | view->ops->type, |
6b161b31 JF |
842 | view->lineno + 1, |
843 | view->lines, | |
844 | (view->lineno + 1) * 100 / view->lines); | |
845 | } | |
846 | ||
847 | wrefresh(view->title); | |
848 | } | |
78c70acd | 849 | |
2e8488b4 JF |
850 | /* |
851 | * Navigation | |
852 | */ | |
853 | ||
4a2909a7 | 854 | /* Scrolling backend */ |
b801d8b2 | 855 | static void |
4a2909a7 | 856 | do_scroll_view(struct view *view, int lines) |
b801d8b2 | 857 | { |
fd85fef1 JF |
858 | /* The rendering expects the new offset. */ |
859 | view->offset += lines; | |
860 | ||
861 | assert(0 <= view->offset && view->offset < view->lines); | |
862 | assert(lines); | |
b801d8b2 | 863 | |
82e78006 | 864 | /* Redraw the whole screen if scrolling is pointless. */ |
4c6fabc2 | 865 | if (view->height < ABS(lines)) { |
b76c2afc JF |
866 | redraw_view(view); |
867 | ||
868 | } else { | |
22f66b0a | 869 | int line = lines > 0 ? view->height - lines : 0; |
82e78006 | 870 | int end = line + ABS(lines); |
fd85fef1 JF |
871 | |
872 | wscrl(view->win, lines); | |
873 | ||
22f66b0a | 874 | for (; line < end; line++) { |
6b161b31 | 875 | if (!view->ops->draw(view, line)) |
fd85fef1 JF |
876 | break; |
877 | } | |
878 | } | |
879 | ||
880 | /* Move current line into the view. */ | |
881 | if (view->lineno < view->offset) { | |
882 | view->lineno = view->offset; | |
6b161b31 | 883 | view->ops->draw(view, 0); |
fd85fef1 JF |
884 | |
885 | } else if (view->lineno >= view->offset + view->height) { | |
886 | view->lineno = view->offset + view->height - 1; | |
6b161b31 | 887 | view->ops->draw(view, view->lineno - view->offset); |
fd85fef1 JF |
888 | } |
889 | ||
4c6fabc2 | 890 | assert(view->offset <= view->lineno && view->lineno < view->lines); |
fd85fef1 JF |
891 | |
892 | redrawwin(view->win); | |
893 | wrefresh(view->win); | |
9d3f5834 | 894 | report(""); |
fd85fef1 | 895 | } |
78c70acd | 896 | |
4a2909a7 | 897 | /* Scroll frontend */ |
fd85fef1 | 898 | static void |
6b161b31 | 899 | scroll_view(struct view *view, enum request request) |
fd85fef1 JF |
900 | { |
901 | int lines = 1; | |
b801d8b2 JF |
902 | |
903 | switch (request) { | |
4a2909a7 | 904 | case REQ_SCROLL_PAGE_DOWN: |
fd85fef1 | 905 | lines = view->height; |
4a2909a7 | 906 | case REQ_SCROLL_LINE_DOWN: |
b801d8b2 | 907 | if (view->offset + lines > view->lines) |
bde3653a | 908 | lines = view->lines - view->offset; |
b801d8b2 | 909 | |
fd85fef1 | 910 | if (lines == 0 || view->offset + view->height >= view->lines) { |
eb98559e | 911 | report("Cannot scroll beyond the last line"); |
b801d8b2 JF |
912 | return; |
913 | } | |
914 | break; | |
915 | ||
4a2909a7 | 916 | case REQ_SCROLL_PAGE_UP: |
fd85fef1 | 917 | lines = view->height; |
4a2909a7 | 918 | case REQ_SCROLL_LINE_UP: |
b801d8b2 JF |
919 | if (lines > view->offset) |
920 | lines = view->offset; | |
921 | ||
922 | if (lines == 0) { | |
eb98559e | 923 | report("Cannot scroll beyond the first line"); |
b801d8b2 JF |
924 | return; |
925 | } | |
926 | ||
fd85fef1 | 927 | lines = -lines; |
b801d8b2 | 928 | break; |
03a93dbb | 929 | |
6b161b31 JF |
930 | default: |
931 | die("request %d not handled in switch", request); | |
b801d8b2 JF |
932 | } |
933 | ||
4a2909a7 | 934 | do_scroll_view(view, lines); |
fd85fef1 | 935 | } |
b801d8b2 | 936 | |
4a2909a7 | 937 | /* Cursor moving */ |
fd85fef1 | 938 | static void |
6b161b31 | 939 | move_view(struct view *view, enum request request) |
fd85fef1 JF |
940 | { |
941 | int steps; | |
b801d8b2 | 942 | |
fd85fef1 | 943 | switch (request) { |
4a2909a7 | 944 | case REQ_MOVE_FIRST_LINE: |
78c70acd JF |
945 | steps = -view->lineno; |
946 | break; | |
947 | ||
4a2909a7 | 948 | case REQ_MOVE_LAST_LINE: |
78c70acd JF |
949 | steps = view->lines - view->lineno - 1; |
950 | break; | |
951 | ||
4a2909a7 | 952 | case REQ_MOVE_PAGE_UP: |
78c70acd JF |
953 | steps = view->height > view->lineno |
954 | ? -view->lineno : -view->height; | |
955 | break; | |
956 | ||
4a2909a7 | 957 | case REQ_MOVE_PAGE_DOWN: |
78c70acd JF |
958 | steps = view->lineno + view->height >= view->lines |
959 | ? view->lines - view->lineno - 1 : view->height; | |
960 | break; | |
961 | ||
4a2909a7 | 962 | case REQ_MOVE_UP: |
fd85fef1 JF |
963 | steps = -1; |
964 | break; | |
b801d8b2 | 965 | |
4a2909a7 | 966 | case REQ_MOVE_DOWN: |
fd85fef1 JF |
967 | steps = 1; |
968 | break; | |
6b161b31 JF |
969 | |
970 | default: | |
971 | die("request %d not handled in switch", request); | |
78c70acd | 972 | } |
b801d8b2 | 973 | |
4c6fabc2 | 974 | if (steps <= 0 && view->lineno == 0) { |
eb98559e | 975 | report("Cannot move beyond the first line"); |
78c70acd | 976 | return; |
b801d8b2 | 977 | |
6908bdbd | 978 | } else if (steps >= 0 && view->lineno + 1 >= view->lines) { |
eb98559e | 979 | report("Cannot move beyond the last line"); |
78c70acd | 980 | return; |
fd85fef1 JF |
981 | } |
982 | ||
4c6fabc2 | 983 | /* Move the current line */ |
fd85fef1 | 984 | view->lineno += steps; |
4c6fabc2 JF |
985 | assert(0 <= view->lineno && view->lineno < view->lines); |
986 | ||
987 | /* Repaint the old "current" line if we be scrolling */ | |
2e8488b4 JF |
988 | if (ABS(steps) < view->height) { |
989 | int prev_lineno = view->lineno - steps - view->offset; | |
990 | ||
991 | wmove(view->win, prev_lineno, 0); | |
992 | wclrtoeol(view->win); | |
03a93dbb | 993 | view->ops->draw(view, prev_lineno); |
2e8488b4 | 994 | } |
fd85fef1 | 995 | |
4c6fabc2 | 996 | /* Check whether the view needs to be scrolled */ |
fd85fef1 JF |
997 | if (view->lineno < view->offset || |
998 | view->lineno >= view->offset + view->height) { | |
999 | if (steps < 0 && -steps > view->offset) { | |
1000 | steps = -view->offset; | |
b76c2afc JF |
1001 | |
1002 | } else if (steps > 0) { | |
1003 | if (view->lineno == view->lines - 1 && | |
1004 | view->lines > view->height) { | |
1005 | steps = view->lines - view->offset - 1; | |
1006 | if (steps >= view->height) | |
1007 | steps -= view->height - 1; | |
1008 | } | |
b801d8b2 | 1009 | } |
78c70acd | 1010 | |
4a2909a7 | 1011 | do_scroll_view(view, steps); |
fd85fef1 | 1012 | return; |
b801d8b2 JF |
1013 | } |
1014 | ||
4c6fabc2 | 1015 | /* Draw the current line */ |
6b161b31 | 1016 | view->ops->draw(view, view->lineno - view->offset); |
fd85fef1 | 1017 | |
b801d8b2 JF |
1018 | redrawwin(view->win); |
1019 | wrefresh(view->win); | |
9d3f5834 | 1020 | report(""); |
b801d8b2 JF |
1021 | } |
1022 | ||
b801d8b2 | 1023 | |
2e8488b4 JF |
1024 | /* |
1025 | * Incremental updating | |
1026 | */ | |
b801d8b2 | 1027 | |
03a93dbb | 1028 | static bool |
b801d8b2 JF |
1029 | begin_update(struct view *view) |
1030 | { | |
2e8488b4 | 1031 | char *id = view->id; |
fd85fef1 | 1032 | |
03a93dbb JF |
1033 | if (opt_cmd[0]) { |
1034 | string_copy(view->cmd, opt_cmd); | |
1035 | opt_cmd[0] = 0; | |
8855ada4 JF |
1036 | /* When running random commands, the view ref could have become |
1037 | * invalid so clear it. */ | |
1038 | view->ref[0] = 0; | |
03a93dbb | 1039 | } else { |
1ba2ae4b JF |
1040 | char *format = view->cmd_env ? view->cmd_env : view->cmd_fmt; |
1041 | ||
1042 | if (snprintf(view->cmd, sizeof(view->cmd), format, | |
1043 | id, id, id, id, id) >= sizeof(view->cmd)) | |
03a93dbb JF |
1044 | return FALSE; |
1045 | } | |
b801d8b2 | 1046 | |
6908bdbd JF |
1047 | /* Special case for the pager view. */ |
1048 | if (opt_pipe) { | |
1049 | view->pipe = opt_pipe; | |
1050 | opt_pipe = NULL; | |
1051 | } else { | |
1052 | view->pipe = popen(view->cmd, "r"); | |
1053 | } | |
1054 | ||
2e8488b4 JF |
1055 | if (!view->pipe) |
1056 | return FALSE; | |
b801d8b2 | 1057 | |
6b161b31 | 1058 | set_nonblocking_input(TRUE); |
b801d8b2 JF |
1059 | |
1060 | view->offset = 0; | |
1061 | view->lines = 0; | |
1062 | view->lineno = 0; | |
49f2b43f | 1063 | string_copy(view->vid, id); |
b801d8b2 | 1064 | |
2e8488b4 JF |
1065 | if (view->line) { |
1066 | int i; | |
1067 | ||
1068 | for (i = 0; i < view->lines; i++) | |
1069 | if (view->line[i]) | |
1070 | free(view->line[i]); | |
1071 | ||
1072 | free(view->line); | |
1073 | view->line = NULL; | |
1074 | } | |
1075 | ||
1076 | view->start_time = time(NULL); | |
1077 | ||
b801d8b2 JF |
1078 | return TRUE; |
1079 | } | |
1080 | ||
1081 | static void | |
1082 | end_update(struct view *view) | |
1083 | { | |
03a93dbb JF |
1084 | if (!view->pipe) |
1085 | return; | |
6b161b31 | 1086 | set_nonblocking_input(FALSE); |
80ce96ea JF |
1087 | if (view->pipe == stdin) |
1088 | fclose(view->pipe); | |
1089 | else | |
1090 | pclose(view->pipe); | |
2e8488b4 | 1091 | view->pipe = NULL; |
b801d8b2 JF |
1092 | } |
1093 | ||
03a93dbb | 1094 | static bool |
b801d8b2 JF |
1095 | update_view(struct view *view) |
1096 | { | |
1097 | char buffer[BUFSIZ]; | |
1098 | char *line; | |
22f66b0a | 1099 | void **tmp; |
82e78006 JF |
1100 | /* The number of lines to read. If too low it will cause too much |
1101 | * redrawing (and possible flickering), if too high responsiveness | |
1102 | * will suffer. */ | |
8855ada4 | 1103 | unsigned long lines = view->height; |
82e78006 | 1104 | int redraw_from = -1; |
b801d8b2 JF |
1105 | |
1106 | if (!view->pipe) | |
1107 | return TRUE; | |
1108 | ||
82e78006 JF |
1109 | /* Only redraw if lines are visible. */ |
1110 | if (view->offset + view->height >= view->lines) | |
1111 | redraw_from = view->lines - view->offset; | |
b801d8b2 JF |
1112 | |
1113 | tmp = realloc(view->line, sizeof(*view->line) * (view->lines + lines)); | |
1114 | if (!tmp) | |
1115 | goto alloc_error; | |
1116 | ||
1117 | view->line = tmp; | |
1118 | ||
1119 | while ((line = fgets(buffer, sizeof(buffer), view->pipe))) { | |
c34d9c9f | 1120 | int linelen = strlen(line); |
b801d8b2 | 1121 | |
b801d8b2 JF |
1122 | if (linelen) |
1123 | line[linelen - 1] = 0; | |
1124 | ||
6b161b31 | 1125 | if (!view->ops->read(view, line)) |
b801d8b2 | 1126 | goto alloc_error; |
fd85fef1 JF |
1127 | |
1128 | if (lines-- == 1) | |
1129 | break; | |
b801d8b2 JF |
1130 | } |
1131 | ||
8855ada4 JF |
1132 | { |
1133 | int digits; | |
1134 | ||
1135 | lines = view->lines; | |
1136 | for (digits = 0; lines; digits++) | |
1137 | lines /= 10; | |
1138 | ||
1139 | /* Keep the displayed view in sync with line number scaling. */ | |
1140 | if (digits != view->digits) { | |
1141 | view->digits = digits; | |
1142 | redraw_from = 0; | |
1143 | } | |
1144 | } | |
1145 | ||
82e78006 JF |
1146 | if (redraw_from >= 0) { |
1147 | /* If this is an incremental update, redraw the previous line | |
a28bcc22 JF |
1148 | * since for commits some members could have changed when |
1149 | * loading the main view. */ | |
82e78006 JF |
1150 | if (redraw_from > 0) |
1151 | redraw_from--; | |
1152 | ||
1153 | /* Incrementally draw avoids flickering. */ | |
1154 | redraw_view_from(view, redraw_from); | |
4c6fabc2 | 1155 | } |
b801d8b2 | 1156 | |
eb98559e JF |
1157 | /* Update the title _after_ the redraw so that if the redraw picks up a |
1158 | * commit reference in view->ref it'll be available here. */ | |
1159 | update_view_title(view); | |
1160 | ||
b801d8b2 | 1161 | if (ferror(view->pipe)) { |
03a93dbb | 1162 | report("Failed to read: %s", strerror(errno)); |
b801d8b2 JF |
1163 | goto end; |
1164 | ||
1165 | } else if (feof(view->pipe)) { | |
2e8488b4 JF |
1166 | time_t secs = time(NULL) - view->start_time; |
1167 | ||
a28bcc22 JF |
1168 | if (view == VIEW(REQ_VIEW_HELP)) { |
1169 | report("%s", HELP); | |
2e8488b4 JF |
1170 | goto end; |
1171 | } | |
1172 | ||
1173 | report("Loaded %d lines in %ld second%s", view->lines, secs, | |
1174 | secs == 1 ? "" : "s"); | |
b801d8b2 JF |
1175 | goto end; |
1176 | } | |
1177 | ||
1178 | return TRUE; | |
1179 | ||
1180 | alloc_error: | |
2e8488b4 | 1181 | report("Allocation failure"); |
b801d8b2 JF |
1182 | |
1183 | end: | |
1184 | end_update(view); | |
1185 | return FALSE; | |
1186 | } | |
1187 | ||
49f2b43f JF |
1188 | enum open_flags { |
1189 | OPEN_DEFAULT = 0, /* Use default view switching. */ | |
1190 | OPEN_SPLIT = 1, /* Split current view. */ | |
1191 | OPEN_BACKGROUNDED = 2, /* Backgrounded. */ | |
1192 | OPEN_RELOAD = 4, /* Reload view even if it is the current. */ | |
1193 | }; | |
1194 | ||
6b161b31 | 1195 | static void |
49f2b43f | 1196 | open_view(struct view *prev, enum request request, enum open_flags flags) |
b801d8b2 | 1197 | { |
49f2b43f JF |
1198 | bool backgrounded = !!(flags & OPEN_BACKGROUNDED); |
1199 | bool split = !!(flags & OPEN_SPLIT); | |
1200 | bool reload = !!(flags & OPEN_RELOAD); | |
a28bcc22 | 1201 | struct view *view = VIEW(request); |
b801d8b2 | 1202 | struct view *displayed; |
6b161b31 JF |
1203 | int nviews; |
1204 | ||
03a93dbb | 1205 | /* Cycle between displayed views and count the views. */ |
6b161b31 | 1206 | foreach_view (displayed, nviews) { |
03a93dbb JF |
1207 | if (prev != view && |
1208 | view == displayed && | |
8855ada4 | 1209 | !strcmp(view->vid, prev->vid)) { |
6b161b31 JF |
1210 | current_view = nviews; |
1211 | /* Blur out the title of the previous view. */ | |
1212 | update_view_title(prev); | |
6734f6b9 | 1213 | report(""); |
6b161b31 | 1214 | return; |
a28bcc22 | 1215 | } |
6b161b31 | 1216 | } |
b801d8b2 | 1217 | |
49f2b43f | 1218 | if (view == prev && nviews == 1 && !reload) { |
6b161b31 JF |
1219 | report("Already in %s view", view->name); |
1220 | return; | |
1221 | } | |
b801d8b2 | 1222 | |
8855ada4 | 1223 | if ((reload || strcmp(view->vid, view->id)) && |
03a93dbb | 1224 | !begin_update(view)) { |
6b161b31 JF |
1225 | report("Failed to load %s view", view->name); |
1226 | return; | |
1227 | } | |
a28bcc22 | 1228 | |
6b161b31 JF |
1229 | if (split) { |
1230 | display[current_view + 1] = view; | |
1231 | if (!backgrounded) | |
a28bcc22 | 1232 | current_view++; |
6b161b31 JF |
1233 | } else { |
1234 | /* Maximize the current view. */ | |
1235 | memset(display, 0, sizeof(display)); | |
1236 | current_view = 0; | |
1237 | display[current_view] = view; | |
a28bcc22 | 1238 | } |
b801d8b2 | 1239 | |
6b161b31 | 1240 | resize_display(); |
b801d8b2 | 1241 | |
a8891802 | 1242 | if (split && prev->lineno - prev->offset >= prev->height) { |
03a93dbb | 1243 | /* Take the title line into account. */ |
eb98559e | 1244 | int lines = prev->lineno - prev->offset - prev->height + 1; |
03a93dbb JF |
1245 | |
1246 | /* Scroll the view that was split if the current line is | |
1247 | * outside the new limited view. */ | |
1248 | do_scroll_view(prev, lines); | |
1249 | } | |
1250 | ||
6b161b31 JF |
1251 | if (prev && view != prev) { |
1252 | /* "Blur" the previous view. */ | |
1253 | update_view_title(prev); | |
1254 | ||
1255 | /* Continue loading split views in the background. */ | |
03a93dbb | 1256 | if (!split) |
6b161b31 | 1257 | end_update(prev); |
b801d8b2 JF |
1258 | } |
1259 | ||
03a93dbb JF |
1260 | if (view->pipe) { |
1261 | /* Clear the old view and let the incremental updating refill | |
1262 | * the screen. */ | |
1263 | wclear(view->win); | |
1264 | report("Loading..."); | |
1265 | } else { | |
1266 | redraw_view(view); | |
1267 | report(""); | |
1268 | } | |
b801d8b2 JF |
1269 | } |
1270 | ||
1271 | ||
6b161b31 JF |
1272 | /* |
1273 | * User request switch noodle | |
1274 | */ | |
1275 | ||
b801d8b2 | 1276 | static int |
6b161b31 | 1277 | view_driver(struct view *view, enum request request) |
b801d8b2 | 1278 | { |
b801d8b2 JF |
1279 | int i; |
1280 | ||
1281 | switch (request) { | |
4a2909a7 JF |
1282 | case REQ_MOVE_UP: |
1283 | case REQ_MOVE_DOWN: | |
1284 | case REQ_MOVE_PAGE_UP: | |
1285 | case REQ_MOVE_PAGE_DOWN: | |
1286 | case REQ_MOVE_FIRST_LINE: | |
1287 | case REQ_MOVE_LAST_LINE: | |
a28bcc22 | 1288 | move_view(view, request); |
fd85fef1 JF |
1289 | break; |
1290 | ||
4a2909a7 JF |
1291 | case REQ_SCROLL_LINE_DOWN: |
1292 | case REQ_SCROLL_LINE_UP: | |
1293 | case REQ_SCROLL_PAGE_DOWN: | |
1294 | case REQ_SCROLL_PAGE_UP: | |
a28bcc22 | 1295 | scroll_view(view, request); |
b801d8b2 JF |
1296 | break; |
1297 | ||
4a2909a7 | 1298 | case REQ_VIEW_MAIN: |
4a2909a7 | 1299 | case REQ_VIEW_DIFF: |
2e8488b4 JF |
1300 | case REQ_VIEW_LOG: |
1301 | case REQ_VIEW_HELP: | |
6908bdbd | 1302 | case REQ_VIEW_PAGER: |
49f2b43f | 1303 | open_view(view, request, OPEN_DEFAULT); |
b801d8b2 JF |
1304 | break; |
1305 | ||
6b161b31 | 1306 | case REQ_ENTER: |
6908bdbd JF |
1307 | if (!view->lines) { |
1308 | report("Nothing to enter"); | |
1309 | break; | |
1310 | } | |
6b161b31 JF |
1311 | return view->ops->enter(view); |
1312 | ||
03a93dbb JF |
1313 | case REQ_VIEW_NEXT: |
1314 | { | |
1315 | int nviews = display[1] ? 2 : 1; | |
1316 | int next_view = (current_view + 1) % nviews; | |
1317 | ||
1318 | if (next_view == current_view) { | |
1319 | report("Only one view is displayed"); | |
1320 | break; | |
1321 | } | |
1322 | ||
1323 | current_view = next_view; | |
1324 | /* Blur out the title of the previous view. */ | |
1325 | update_view_title(view); | |
6734f6b9 | 1326 | report(""); |
03a93dbb JF |
1327 | break; |
1328 | } | |
4a2909a7 | 1329 | case REQ_TOGGLE_LINE_NUMBERS: |
b76c2afc | 1330 | opt_line_number = !opt_line_number; |
a28bcc22 | 1331 | redraw_view(view); |
b801d8b2 JF |
1332 | break; |
1333 | ||
03a93dbb | 1334 | case REQ_PROMPT: |
8855ada4 | 1335 | /* Always reload^Wrerun commands from the prompt. */ |
49f2b43f | 1336 | open_view(view, opt_request, OPEN_RELOAD); |
03a93dbb JF |
1337 | break; |
1338 | ||
4a2909a7 | 1339 | case REQ_STOP_LOADING: |
03a93dbb | 1340 | foreach_view (view, i) { |
2e8488b4 | 1341 | if (view->pipe) |
03a93dbb JF |
1342 | report("Stopped loaded of %s view", view->name), |
1343 | end_update(view); | |
1344 | } | |
b801d8b2 JF |
1345 | break; |
1346 | ||
4a2909a7 | 1347 | case REQ_SHOW_VERSION: |
2e8488b4 | 1348 | report("Version: %s", VERSION); |
b801d8b2 JF |
1349 | return TRUE; |
1350 | ||
fac7db6c JF |
1351 | case REQ_SCREEN_RESIZE: |
1352 | resize_display(); | |
1353 | /* Fall-through */ | |
4a2909a7 | 1354 | case REQ_SCREEN_REDRAW: |
eb98559e JF |
1355 | foreach_view (view, i) { |
1356 | redraw_view(view); | |
1357 | update_view_title(view); | |
1358 | } | |
4a2909a7 JF |
1359 | break; |
1360 | ||
1361 | case REQ_SCREEN_UPDATE: | |
b801d8b2 JF |
1362 | doupdate(); |
1363 | return TRUE; | |
1364 | ||
1365 | case REQ_QUIT: | |
1366 | return FALSE; | |
1367 | ||
1368 | default: | |
2e8488b4 | 1369 | /* An unknown key will show most commonly used commands. */ |
a28bcc22 | 1370 | report("%s", HELP); |
b801d8b2 JF |
1371 | return TRUE; |
1372 | } | |
1373 | ||
1374 | return TRUE; | |
1375 | } | |
1376 | ||
1377 | ||
1378 | /* | |
6b161b31 | 1379 | * View backend handlers |
b801d8b2 JF |
1380 | */ |
1381 | ||
6b161b31 | 1382 | static bool |
22f66b0a | 1383 | pager_draw(struct view *view, unsigned int lineno) |
b801d8b2 | 1384 | { |
78c70acd | 1385 | enum line_type type; |
b801d8b2 | 1386 | char *line; |
4c6fabc2 | 1387 | int linelen; |
78c70acd | 1388 | int attr; |
b801d8b2 | 1389 | |
fd85fef1 JF |
1390 | if (view->offset + lineno >= view->lines) |
1391 | return FALSE; | |
1392 | ||
b801d8b2 | 1393 | line = view->line[view->offset + lineno]; |
78c70acd | 1394 | type = get_line_type(line); |
b801d8b2 | 1395 | |
fd85fef1 | 1396 | if (view->offset + lineno == view->lineno) { |
8855ada4 | 1397 | if (type == LINE_COMMIT) { |
03a93dbb JF |
1398 | string_copy(view->ref, line + 7); |
1399 | string_copy(ref_commit, view->ref); | |
1400 | } | |
8855ada4 | 1401 | |
78c70acd | 1402 | type = LINE_CURSOR; |
fd85fef1 JF |
1403 | } |
1404 | ||
78c70acd | 1405 | attr = get_line_attr(type); |
b801d8b2 | 1406 | wattrset(view->win, attr); |
b76c2afc | 1407 | |
4c6fabc2 JF |
1408 | linelen = strlen(line); |
1409 | linelen = MIN(linelen, view->width); | |
1410 | ||
b76c2afc | 1411 | if (opt_line_number) { |
8855ada4 JF |
1412 | static char indent[] = " "; |
1413 | unsigned long real_lineno = view->offset + lineno + 1; | |
a28bcc22 | 1414 | int col = 0; |
82e78006 | 1415 | |
2e8488b4 | 1416 | if (real_lineno == 1 || (real_lineno % opt_num_interval) == 0) |
8855ada4 JF |
1417 | mvwprintw(view->win, lineno, 0, "%.*d", view->digits, real_lineno); |
1418 | ||
1419 | else if (view->digits < sizeof(indent)) | |
1420 | mvwaddnstr(view->win, lineno, 0, indent, view->digits); | |
1421 | ||
1422 | waddstr(view->win, ": "); | |
4c6fabc2 | 1423 | |
b76c2afc JF |
1424 | while (line) { |
1425 | if (*line == '\t') { | |
82e78006 JF |
1426 | waddnstr(view->win, " ", 8 - (col % 8)); |
1427 | col += 8 - (col % 8); | |
b76c2afc | 1428 | line++; |
82e78006 | 1429 | |
b76c2afc JF |
1430 | } else { |
1431 | char *tab = strchr(line, '\t'); | |
1432 | ||
1433 | if (tab) | |
1434 | waddnstr(view->win, line, tab - line); | |
1435 | else | |
1436 | waddstr(view->win, line); | |
82e78006 | 1437 | col += tab - line; |
b76c2afc JF |
1438 | line = tab; |
1439 | } | |
1440 | } | |
1441 | waddstr(view->win, line); | |
1442 | ||
1443 | } else { | |
2e8488b4 JF |
1444 | #if 0 |
1445 | /* NOTE: Code for only highlighting the text on the cursor line. | |
1446 | * Kept since I've not yet decided whether to highlight the | |
1447 | * entire line or not. --fonseca */ | |
b76c2afc JF |
1448 | /* No empty lines makes cursor drawing and clearing implicit. */ |
1449 | if (!*line) | |
4c6fabc2 | 1450 | line = " ", linelen = 1; |
2e8488b4 | 1451 | #endif |
4c6fabc2 | 1452 | mvwaddnstr(view->win, lineno, 0, line, linelen); |
b76c2afc | 1453 | } |
b801d8b2 | 1454 | |
2e8488b4 JF |
1455 | /* Paint the rest of the line if it's the cursor line. */ |
1456 | if (type == LINE_CURSOR) | |
1457 | wchgat(view->win, -1, 0, type, NULL); | |
1458 | ||
b801d8b2 JF |
1459 | return TRUE; |
1460 | } | |
1461 | ||
6b161b31 | 1462 | static bool |
22f66b0a JF |
1463 | pager_read(struct view *view, char *line) |
1464 | { | |
1465 | view->line[view->lines] = strdup(line); | |
1466 | if (!view->line[view->lines]) | |
1467 | return FALSE; | |
1468 | ||
1469 | view->lines++; | |
1470 | return TRUE; | |
1471 | } | |
1472 | ||
6b161b31 JF |
1473 | static bool |
1474 | pager_enter(struct view *view) | |
1475 | { | |
1476 | char *line = view->line[view->lineno]; | |
1477 | ||
1478 | if (get_line_type(line) == LINE_COMMIT) { | |
49f2b43f | 1479 | open_view(view, REQ_VIEW_DIFF, OPEN_DEFAULT); |
6b161b31 JF |
1480 | } |
1481 | ||
1482 | return TRUE; | |
1483 | } | |
1484 | ||
6b161b31 | 1485 | static struct view_ops pager_ops = { |
6734f6b9 | 1486 | "line", |
6b161b31 JF |
1487 | pager_draw, |
1488 | pager_read, | |
1489 | pager_enter, | |
1490 | }; | |
1491 | ||
80ce96ea | 1492 | |
c34d9c9f JF |
1493 | static struct ref **get_refs(char *id); |
1494 | ||
6b161b31 | 1495 | static bool |
22f66b0a JF |
1496 | main_draw(struct view *view, unsigned int lineno) |
1497 | { | |
2e8488b4 | 1498 | char buf[DATE_COLS + 1]; |
22f66b0a | 1499 | struct commit *commit; |
78c70acd | 1500 | enum line_type type; |
b76c2afc JF |
1501 | int cols = 0; |
1502 | size_t timelen; | |
22f66b0a JF |
1503 | |
1504 | if (view->offset + lineno >= view->lines) | |
1505 | return FALSE; | |
1506 | ||
1507 | commit = view->line[view->offset + lineno]; | |
4c6fabc2 JF |
1508 | if (!*commit->author) |
1509 | return FALSE; | |
22f66b0a | 1510 | |
22f66b0a | 1511 | if (view->offset + lineno == view->lineno) { |
03a93dbb | 1512 | string_copy(view->ref, commit->id); |
49f2b43f | 1513 | string_copy(ref_commit, view->ref); |
78c70acd JF |
1514 | type = LINE_CURSOR; |
1515 | } else { | |
b76c2afc JF |
1516 | type = LINE_MAIN_COMMIT; |
1517 | } | |
1518 | ||
1519 | wmove(view->win, lineno, cols); | |
1520 | wattrset(view->win, get_line_attr(LINE_MAIN_DATE)); | |
1521 | ||
4c6fabc2 | 1522 | timelen = strftime(buf, sizeof(buf), DATE_FORMAT, &commit->time); |
b76c2afc | 1523 | waddnstr(view->win, buf, timelen); |
4c6fabc2 | 1524 | waddstr(view->win, " "); |
b76c2afc | 1525 | |
4c6fabc2 | 1526 | cols += DATE_COLS; |
b76c2afc JF |
1527 | wmove(view->win, lineno, cols); |
1528 | wattrset(view->win, get_line_attr(LINE_MAIN_AUTHOR)); | |
1529 | ||
1530 | if (strlen(commit->author) > 19) { | |
1531 | waddnstr(view->win, commit->author, 18); | |
1532 | wattrset(view->win, get_line_attr(LINE_MAIN_DELIM)); | |
1533 | waddch(view->win, '~'); | |
1534 | } else { | |
1535 | waddstr(view->win, commit->author); | |
22f66b0a JF |
1536 | } |
1537 | ||
b76c2afc JF |
1538 | cols += 20; |
1539 | wattrset(view->win, A_NORMAL); | |
1540 | mvwaddch(view->win, lineno, cols, ACS_LTEE); | |
c34d9c9f JF |
1541 | wmove(view->win, lineno, cols + 2); |
1542 | ||
1543 | if (commit->refs) { | |
1544 | size_t i = 0; | |
1545 | ||
1546 | do { | |
1547 | if (commit->refs[i]->tag) | |
1548 | wattrset(view->win, get_line_attr(LINE_MAIN_TAG)); | |
1549 | else | |
1550 | wattrset(view->win, get_line_attr(LINE_MAIN_REF)); | |
1551 | waddstr(view->win, "["); | |
1552 | waddstr(view->win, commit->refs[i]->name); | |
1553 | waddstr(view->win, "]"); | |
1554 | wattrset(view->win, A_NORMAL); | |
1555 | waddstr(view->win, " "); | |
1556 | } while (commit->refs[i++]->next); | |
1557 | } | |
1558 | ||
78c70acd | 1559 | wattrset(view->win, get_line_attr(type)); |
c34d9c9f | 1560 | waddstr(view->win, commit->title); |
22f66b0a JF |
1561 | wattrset(view->win, A_NORMAL); |
1562 | ||
1563 | return TRUE; | |
1564 | } | |
1565 | ||
4c6fabc2 | 1566 | /* Reads git log --pretty=raw output and parses it into the commit struct. */ |
6b161b31 | 1567 | static bool |
22f66b0a JF |
1568 | main_read(struct view *view, char *line) |
1569 | { | |
78c70acd JF |
1570 | enum line_type type = get_line_type(line); |
1571 | struct commit *commit; | |
22f66b0a | 1572 | |
78c70acd JF |
1573 | switch (type) { |
1574 | case LINE_COMMIT: | |
22f66b0a JF |
1575 | commit = calloc(1, sizeof(struct commit)); |
1576 | if (!commit) | |
1577 | return FALSE; | |
1578 | ||
4c6fabc2 | 1579 | line += STRING_SIZE("commit "); |
b76c2afc | 1580 | |
22f66b0a | 1581 | view->line[view->lines++] = commit; |
82e78006 | 1582 | string_copy(commit->id, line); |
c34d9c9f | 1583 | commit->refs = get_refs(commit->id); |
78c70acd | 1584 | break; |
22f66b0a | 1585 | |
8855ada4 | 1586 | case LINE_AUTHOR: |
b76c2afc | 1587 | { |
4c6fabc2 | 1588 | char *ident = line + STRING_SIZE("author "); |
b76c2afc JF |
1589 | char *end = strchr(ident, '<'); |
1590 | ||
1591 | if (end) { | |
1592 | for (; end > ident && isspace(end[-1]); end--) ; | |
1593 | *end = 0; | |
1594 | } | |
1595 | ||
1596 | commit = view->line[view->lines - 1]; | |
82e78006 | 1597 | string_copy(commit->author, ident); |
b76c2afc | 1598 | |
4c6fabc2 | 1599 | /* Parse epoch and timezone */ |
b76c2afc JF |
1600 | if (end) { |
1601 | char *secs = strchr(end + 1, '>'); | |
1602 | char *zone; | |
1603 | time_t time; | |
1604 | ||
1605 | if (!secs || secs[1] != ' ') | |
1606 | break; | |
1607 | ||
1608 | secs += 2; | |
1609 | time = (time_t) atol(secs); | |
1610 | zone = strchr(secs, ' '); | |
4c6fabc2 | 1611 | if (zone && strlen(zone) == STRING_SIZE(" +0700")) { |
b76c2afc JF |
1612 | long tz; |
1613 | ||
1614 | zone++; | |
1615 | tz = ('0' - zone[1]) * 60 * 60 * 10; | |
1616 | tz += ('0' - zone[2]) * 60 * 60; | |
1617 | tz += ('0' - zone[3]) * 60; | |
1618 | tz += ('0' - zone[4]) * 60; | |
1619 | ||
1620 | if (zone[0] == '-') | |
1621 | tz = -tz; | |
1622 | ||
1623 | time -= tz; | |
1624 | } | |
1625 | gmtime_r(&time, &commit->time); | |
1626 | } | |
1627 | break; | |
1628 | } | |
78c70acd | 1629 | default: |
2e8488b4 JF |
1630 | /* We should only ever end up here if there has already been a |
1631 | * commit line, however, be safe. */ | |
1632 | if (view->lines == 0) | |
1633 | break; | |
1634 | ||
1635 | /* Fill in the commit title if it has not already been set. */ | |
78c70acd | 1636 | commit = view->line[view->lines - 1]; |
2e8488b4 JF |
1637 | if (commit->title[0]) |
1638 | break; | |
1639 | ||
1640 | /* Require titles to start with a non-space character at the | |
1641 | * offset used by git log. */ | |
eb98559e JF |
1642 | /* FIXME: More gracefull handling of titles; append "..." to |
1643 | * shortened titles, etc. */ | |
2e8488b4 | 1644 | if (strncmp(line, " ", 4) || |
eb98559e | 1645 | isspace(line[4])) |
82e78006 JF |
1646 | break; |
1647 | ||
1648 | string_copy(commit->title, line + 4); | |
22f66b0a JF |
1649 | } |
1650 | ||
1651 | return TRUE; | |
1652 | } | |
1653 | ||
6b161b31 JF |
1654 | static bool |
1655 | main_enter(struct view *view) | |
b801d8b2 | 1656 | { |
49f2b43f | 1657 | open_view(view, REQ_VIEW_DIFF, OPEN_SPLIT | OPEN_BACKGROUNDED); |
6b161b31 | 1658 | return TRUE; |
b801d8b2 JF |
1659 | } |
1660 | ||
6b161b31 | 1661 | static struct view_ops main_ops = { |
6734f6b9 | 1662 | "commit", |
6b161b31 JF |
1663 | main_draw, |
1664 | main_read, | |
1665 | main_enter, | |
1666 | }; | |
2e8488b4 | 1667 | |
c34d9c9f | 1668 | |
6b161b31 JF |
1669 | /* |
1670 | * Status management | |
1671 | */ | |
2e8488b4 | 1672 | |
8855ada4 JF |
1673 | /* Whether or not the curses interface has been initialized. */ |
1674 | bool cursed = FALSE; | |
1675 | ||
6b161b31 JF |
1676 | /* The status window is used for polling keystrokes. */ |
1677 | static WINDOW *status_win; | |
4a2909a7 | 1678 | |
2e8488b4 | 1679 | /* Update status and title window. */ |
4a2909a7 JF |
1680 | static void |
1681 | report(const char *msg, ...) | |
1682 | { | |
1683 | va_list args; | |
b76c2afc | 1684 | |
2e8488b4 JF |
1685 | /* Update the title window first, so the cursor ends up in the status |
1686 | * window. */ | |
6b161b31 | 1687 | update_view_title(display[current_view]); |
4a2909a7 | 1688 | |
4b76734f JF |
1689 | va_start(args, msg); |
1690 | ||
b801d8b2 JF |
1691 | werase(status_win); |
1692 | wmove(status_win, 0, 0); | |
4b76734f JF |
1693 | if (*msg) |
1694 | vwprintw(status_win, msg, args); | |
b801d8b2 JF |
1695 | wrefresh(status_win); |
1696 | ||
1697 | va_end(args); | |
b801d8b2 JF |
1698 | } |
1699 | ||
6b161b31 JF |
1700 | /* Controls when nodelay should be in effect when polling user input. */ |
1701 | static void | |
1ba2ae4b | 1702 | set_nonblocking_input(bool loading) |
b801d8b2 | 1703 | { |
6b161b31 JF |
1704 | /* The number of loading views. */ |
1705 | static unsigned int nloading; | |
b801d8b2 | 1706 | |
1ba2ae4b JF |
1707 | if ((loading == FALSE && nloading-- == 1) || |
1708 | (loading == TRUE && nloading++ == 0)) | |
1709 | nodelay(status_win, loading); | |
6b161b31 JF |
1710 | } |
1711 | ||
1712 | static void | |
1713 | init_display(void) | |
1714 | { | |
1715 | int x, y; | |
b76c2afc | 1716 | |
6908bdbd JF |
1717 | /* Initialize the curses library */ |
1718 | if (isatty(STDIN_FILENO)) { | |
8855ada4 | 1719 | cursed = !!initscr(); |
6908bdbd JF |
1720 | } else { |
1721 | /* Leave stdin and stdout alone when acting as a pager. */ | |
1722 | FILE *io = fopen("/dev/tty", "r+"); | |
1723 | ||
8855ada4 | 1724 | cursed = !!newterm(NULL, io, io); |
6908bdbd JF |
1725 | } |
1726 | ||
8855ada4 JF |
1727 | if (!cursed) |
1728 | die("Failed to initialize curses"); | |
1729 | ||
2e8488b4 JF |
1730 | nonl(); /* Tell curses not to do NL->CR/NL on output */ |
1731 | cbreak(); /* Take input chars one at a time, no wait for \n */ | |
1732 | noecho(); /* Don't echo input */ | |
b801d8b2 | 1733 | leaveok(stdscr, TRUE); |
b801d8b2 JF |
1734 | |
1735 | if (has_colors()) | |
1736 | init_colors(); | |
1737 | ||
1738 | getmaxyx(stdscr, y, x); | |
1739 | status_win = newwin(1, 0, y - 1, 0); | |
1740 | if (!status_win) | |
1741 | die("Failed to create status window"); | |
1742 | ||
1743 | /* Enable keyboard mapping */ | |
1744 | keypad(status_win, TRUE); | |
78c70acd | 1745 | wbkgdset(status_win, get_line_attr(LINE_STATUS)); |
6b161b31 JF |
1746 | } |
1747 | ||
c34d9c9f JF |
1748 | |
1749 | /* | |
1750 | * Repository references | |
1751 | */ | |
1752 | ||
1753 | static struct ref *refs; | |
1754 | size_t refs_size; | |
1755 | ||
1756 | static struct ref ** | |
1757 | get_refs(char *id) | |
1758 | { | |
1759 | struct ref **id_refs = NULL; | |
1760 | size_t id_refs_size = 0; | |
1761 | size_t i; | |
1762 | ||
1763 | for (i = 0; i < refs_size; i++) { | |
1764 | struct ref **tmp; | |
1765 | ||
1766 | if (strcmp(id, refs[i].id)) | |
1767 | continue; | |
1768 | ||
1769 | tmp = realloc(id_refs, (id_refs_size + 1) * sizeof(*id_refs)); | |
1770 | if (!tmp) { | |
1771 | if (id_refs) | |
1772 | free(id_refs); | |
1773 | return NULL; | |
1774 | } | |
1775 | ||
1776 | id_refs = tmp; | |
3af8774e | 1777 | if (id_refs_size > 0) |
c34d9c9f | 1778 | id_refs[id_refs_size - 1]->next = 1; |
3af8774e JF |
1779 | id_refs[id_refs_size] = &refs[i]; |
1780 | ||
1781 | /* XXX: The properties of the commit chains ensures that we can | |
1782 | * safely modify the shared ref. The repo references will | |
1783 | * always be similar for the same id. */ | |
1784 | id_refs[id_refs_size]->next = 0; | |
1785 | id_refs_size++; | |
c34d9c9f JF |
1786 | } |
1787 | ||
1788 | return id_refs; | |
1789 | } | |
1790 | ||
1791 | static int | |
1792 | load_refs(void) | |
1793 | { | |
1794 | char *cmd_env = getenv("TIG_LS_REMOTE"); | |
1795 | char *cmd = cmd_env ? cmd_env : TIG_LS_REMOTE; | |
1796 | FILE *pipe = popen(cmd, "r"); | |
1797 | char buffer[BUFSIZ]; | |
1798 | char *line; | |
1799 | ||
1800 | if (!pipe) | |
1801 | return ERR; | |
1802 | ||
1803 | while ((line = fgets(buffer, sizeof(buffer), pipe))) { | |
1804 | char *name = strchr(line, '\t'); | |
1805 | struct ref *ref; | |
1806 | int namelen; | |
1807 | bool tag = FALSE; | |
6734f6b9 | 1808 | bool tag_commit = FALSE; |
c34d9c9f JF |
1809 | |
1810 | if (!name) | |
1811 | continue; | |
1812 | ||
1813 | *name++ = 0; | |
1814 | namelen = strlen(name) - 1; | |
1815 | if (name[namelen - 1] == '}') { | |
1816 | while (namelen > 0 && name[namelen] != '^') | |
1817 | namelen--; | |
6734f6b9 JF |
1818 | if (namelen > 0) |
1819 | tag_commit = TRUE; | |
c34d9c9f JF |
1820 | } |
1821 | name[namelen] = 0; | |
1822 | ||
1823 | if (!strncmp(name, "refs/tags/", STRING_SIZE("refs/tags/"))) { | |
6734f6b9 JF |
1824 | if (!tag_commit) |
1825 | continue; | |
c34d9c9f JF |
1826 | name += STRING_SIZE("refs/tags/"); |
1827 | tag = TRUE; | |
3af8774e JF |
1828 | |
1829 | } else if (!strncmp(name, "refs/heads/", STRING_SIZE("refs/heads/"))) { | |
1830 | name += STRING_SIZE("refs/heads/"); | |
1831 | ||
1832 | } else if (!strcmp(name, "HEAD")) { | |
1833 | continue; | |
c34d9c9f JF |
1834 | } |
1835 | ||
1836 | refs = realloc(refs, sizeof(*refs) * (refs_size + 1)); | |
1837 | if (!refs) | |
1838 | return ERR; | |
1839 | ||
1840 | ref = &refs[refs_size++]; | |
1841 | ref->tag = tag; | |
1842 | ref->name = strdup(name); | |
1843 | if (!ref->name) | |
1844 | return ERR; | |
1845 | ||
1846 | string_copy(ref->id, line); | |
1847 | } | |
1848 | ||
1849 | if (ferror(pipe)) | |
1850 | return ERR; | |
1851 | ||
1852 | pclose(pipe); | |
1853 | ||
1854 | return OK; | |
1855 | } | |
1856 | ||
6b161b31 JF |
1857 | /* |
1858 | * Main | |
1859 | */ | |
1860 | ||
1861 | static void | |
1862 | quit(int sig) | |
1863 | { | |
8855ada4 JF |
1864 | /* XXX: Restore tty modes and let the OS cleanup the rest! */ |
1865 | if (cursed) | |
1866 | endwin(); | |
6b161b31 JF |
1867 | exit(0); |
1868 | } | |
1869 | ||
1870 | static void die(const char *err, ...) | |
1871 | { | |
1872 | va_list args; | |
1873 | ||
1874 | endwin(); | |
1875 | ||
1876 | va_start(args, err); | |
1877 | fputs("tig: ", stderr); | |
1878 | vfprintf(stderr, err, args); | |
1879 | fputs("\n", stderr); | |
1880 | va_end(args); | |
1881 | ||
1882 | exit(1); | |
1883 | } | |
1884 | ||
1885 | int | |
1886 | main(int argc, char *argv[]) | |
1887 | { | |
1ba2ae4b | 1888 | struct view *view; |
6b161b31 | 1889 | enum request request; |
1ba2ae4b | 1890 | size_t i; |
6b161b31 JF |
1891 | |
1892 | signal(SIGINT, quit); | |
1893 | ||
8855ada4 | 1894 | if (!parse_options(argc, argv)) |
6b161b31 JF |
1895 | return 0; |
1896 | ||
c34d9c9f JF |
1897 | if (load_refs() == ERR) |
1898 | die("Failed to load refs."); | |
1899 | ||
1ba2ae4b JF |
1900 | for (i = 0; i < ARRAY_SIZE(views) && (view = &views[i]); i++) |
1901 | view->cmd_env = getenv(view->cmd_env); | |
1902 | ||
6b161b31 JF |
1903 | request = opt_request; |
1904 | ||
1905 | init_display(); | |
b801d8b2 JF |
1906 | |
1907 | while (view_driver(display[current_view], request)) { | |
6b161b31 | 1908 | int key; |
b801d8b2 JF |
1909 | int i; |
1910 | ||
6b161b31 JF |
1911 | foreach_view (view, i) |
1912 | update_view(view); | |
b801d8b2 JF |
1913 | |
1914 | /* Refresh, accept single keystroke of input */ | |
6b161b31 JF |
1915 | key = wgetch(status_win); |
1916 | request = get_request(key); | |
03a93dbb | 1917 | |
fac7db6c JF |
1918 | /* Some low-level request handling. This keeps handling of |
1919 | * status_win restricted. */ | |
1920 | switch (request) { | |
1921 | case REQ_PROMPT: | |
6908bdbd JF |
1922 | report(":"); |
1923 | /* Temporarily switch to line-oriented and echoed | |
1924 | * input. */ | |
03a93dbb JF |
1925 | nocbreak(); |
1926 | echo(); | |
49f2b43f JF |
1927 | |
1928 | if (wgetnstr(status_win, opt_cmd + 4, sizeof(opt_cmd) - 4) == OK) { | |
1929 | memcpy(opt_cmd, "git ", 4); | |
1930 | opt_request = REQ_VIEW_PAGER; | |
1931 | } else { | |
1932 | request = ERR; | |
1933 | } | |
1934 | ||
6908bdbd JF |
1935 | noecho(); |
1936 | cbreak(); | |
fac7db6c JF |
1937 | break; |
1938 | ||
1939 | case REQ_SCREEN_RESIZE: | |
1940 | { | |
1941 | int height, width; | |
1942 | ||
1943 | getmaxyx(stdscr, height, width); | |
1944 | ||
1945 | /* Resize the status view and let the view driver take | |
1946 | * care of resizing the displayed views. */ | |
1947 | wresize(status_win, 1, width); | |
1948 | mvwin(status_win, height - 1, 0); | |
1949 | wrefresh(status_win); | |
1950 | break; | |
1951 | } | |
1952 | default: | |
1953 | break; | |
03a93dbb | 1954 | } |
b801d8b2 JF |
1955 | } |
1956 | ||
1957 | quit(0); | |
1958 | ||
1959 | return 0; | |
1960 | } | |
1961 | ||
1962 | /** | |
4c6fabc2 JF |
1963 | * TODO |
1964 | * ---- | |
1965 | * Features that should be explored. | |
1966 | * | |
fac7db6c | 1967 | * - Searching. |
4c6fabc2 JF |
1968 | * |
1969 | * - Locale support. | |
1970 | * | |
b801d8b2 JF |
1971 | * COPYRIGHT |
1972 | * --------- | |
4a2909a7 | 1973 | * Copyright (c) Jonas Fonseca <fonseca@diku.dk>, 2006 |
b801d8b2 JF |
1974 | * |
1975 | * This program is free software; you can redistribute it and/or modify | |
1976 | * it under the terms of the GNU General Public License as published by | |
1977 | * the Free Software Foundation; either version 2 of the License, or | |
1978 | * (at your option) any later version. | |
1979 | * | |
1980 | * SEE ALSO | |
1981 | * -------- | |
4c6fabc2 | 1982 | * [verse] |
b801d8b2 JF |
1983 | * link:http://www.kernel.org/pub/software/scm/git/docs/[git(7)], |
1984 | * link:http://www.kernel.org/pub/software/scm/cogito/docs/[cogito(7)] | |
4c6fabc2 JF |
1985 | * gitk(1): git repository browser written using tcl/tk, |
1986 | * gitview(1): git repository browser written using python/gtk. | |
b801d8b2 | 1987 | **/ |