/
tig.c
7108 lines (5796 loc) · 160 KB
/
tig.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Copyright (c) 2006-2009 Jonas Fonseca <fonseca@diku.dk>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of
* the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#ifndef TIG_VERSION
#define TIG_VERSION "unknown-version"
#endif
#ifndef DEBUG
#define NDEBUG
#endif
#include <assert.h>
#include <errno.h>
#include <ctype.h>
#include <signal.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <sys/select.h>
#include <unistd.h>
#include <time.h>
#include <fcntl.h>
#include <regex.h>
#include <locale.h>
#include <langinfo.h>
#include <iconv.h>
/* ncurses(3): Must be defined to have extended wide-character functions. */
#define _XOPEN_SOURCE_EXTENDED
#ifdef HAVE_NCURSESW_NCURSES_H
#include <ncursesw/ncurses.h>
#else
#ifdef HAVE_NCURSES_NCURSES_H
#include <ncurses/ncurses.h>
#else
#include <ncurses.h>
#endif
#endif
#if __GNUC__ >= 3
#define __NORETURN __attribute__((__noreturn__))
#else
#define __NORETURN
#endif
static void __NORETURN die(const char *err, ...);
static void warn(const char *msg, ...);
static void report(const char *msg, ...);
static void set_nonblocking_input(bool loading);
static int load_refs(void);
static size_t utf8_length(const char **string, size_t col, int *width, size_t max_width, int *trimmed, bool reserve);
#define ABS(x) ((x) >= 0 ? (x) : -(x))
#define MIN(x, y) ((x) < (y) ? (x) : (y))
#define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0]))
#define STRING_SIZE(x) (sizeof(x) - 1)
#define SIZEOF_STR 1024 /* Default string size. */
#define SIZEOF_REF 256 /* Size of symbolic or SHA1 ID. */
#define SIZEOF_REV 41 /* Holds a SHA-1 and an ending NUL. */
#define SIZEOF_ARG 32 /* Default argument array size. */
/* Revision graph */
#define REVGRAPH_INIT 'I'
#define REVGRAPH_MERGE 'M'
#define REVGRAPH_BRANCH '+'
#define REVGRAPH_COMMIT '*'
#define REVGRAPH_BOUND '^'
#define SIZEOF_REVGRAPH 19 /* Size of revision ancestry graphics. */
/* This color name can be used to refer to the default term colors. */
#define COLOR_DEFAULT (-1)
#define ICONV_NONE ((iconv_t) -1)
#ifndef ICONV_CONST
#define ICONV_CONST /* nothing */
#endif
/* The format and size of the date column in the main view. */
#define DATE_FORMAT "%Y-%m-%d %H:%M"
#define DATE_COLS STRING_SIZE("2006-04-29 14:21 ")
#define AUTHOR_COLS 20
#define ID_COLS 8
/* The default interval between line numbers. */
#define NUMBER_INTERVAL 5
#define SCROLL_INTERVAL 1
#define TAB_SIZE 8
#define SCALE_SPLIT_VIEW(height) ((height) * 2 / 3)
#define NULL_ID "0000000000000000000000000000000000000000"
#define S_ISGITLINK(mode) (((mode) & S_IFMT) == 0160000)
#ifndef GIT_CONFIG
#define GIT_CONFIG "config"
#endif
/* Some ASCII-shorthands fitted into the ncurses namespace. */
#define KEY_TAB '\t'
#define KEY_RETURN '\r'
#define KEY_ESC 27
struct ref {
char *name; /* Ref name; tag or head names are shortened. */
char id[SIZEOF_REV]; /* Commit SHA1 ID */
unsigned int head:1; /* Is it the current HEAD? */
unsigned int tag:1; /* Is it a tag? */
unsigned int ltag:1; /* If so, is the tag local? */
unsigned int remote:1; /* Is it a remote ref? */
unsigned int tracked:1; /* Is it the remote for the current HEAD? */
unsigned int next:1; /* For ref lists: are there more refs? */
};
static struct ref **get_refs(const char *id);
enum format_flags {
FORMAT_ALL, /* Perform replacement in all arguments. */
FORMAT_DASH, /* Perform replacement up until "--". */
FORMAT_NONE /* No replacement should be performed. */
};
static bool format_argv(const char *dst[], const char *src[], enum format_flags flags);
enum input_status {
INPUT_OK,
INPUT_SKIP,
INPUT_STOP,
INPUT_CANCEL
};
typedef enum input_status (*input_handler)(void *data, char *buf, int c);
static char *prompt_input(const char *prompt, input_handler handler, void *data);
static bool prompt_yesno(const char *prompt);
/*
* String helpers
*/
static inline void
string_ncopy_do(char *dst, size_t dstlen, const char *src, size_t srclen)
{
if (srclen > dstlen - 1)
srclen = dstlen - 1;
strncpy(dst, src, srclen);
dst[srclen] = 0;
}
/* Shorthands for safely copying into a fixed buffer. */
#define string_copy(dst, src) \
string_ncopy_do(dst, sizeof(dst), src, sizeof(src))
#define string_ncopy(dst, src, srclen) \
string_ncopy_do(dst, sizeof(dst), src, srclen)
#define string_copy_rev(dst, src) \
string_ncopy_do(dst, SIZEOF_REV, src, SIZEOF_REV - 1)
#define string_add(dst, from, src) \
string_ncopy_do(dst + (from), sizeof(dst) - (from), src, sizeof(src))
static void
string_expand(char *dst, size_t dstlen, const char *src, int tabsize)
{
size_t size, pos;
for (size = pos = 0; size < dstlen - 1 && src[pos]; pos++) {
if (src[pos] == '\t') {
size_t expanded = tabsize - (size % tabsize);
if (expanded + size >= dstlen - 1)
expanded = dstlen - size - 1;
memcpy(dst + size, " ", expanded);
size += expanded;
} else {
dst[size++] = src[pos];
}
}
dst[size] = 0;
}
static char *
chomp_string(char *name)
{
int namelen;
while (isspace(*name))
name++;
namelen = strlen(name) - 1;
while (namelen > 0 && isspace(name[namelen]))
name[namelen--] = 0;
return name;
}
static bool
string_nformat(char *buf, size_t bufsize, size_t *bufpos, const char *fmt, ...)
{
va_list args;
size_t pos = bufpos ? *bufpos : 0;
va_start(args, fmt);
pos += vsnprintf(buf + pos, bufsize - pos, fmt, args);
va_end(args);
if (bufpos)
*bufpos = pos;
return pos >= bufsize ? FALSE : TRUE;
}
#define string_format(buf, fmt, args...) \
string_nformat(buf, sizeof(buf), NULL, fmt, args)
#define string_format_from(buf, from, fmt, args...) \
string_nformat(buf, sizeof(buf), from, fmt, args)
static int
string_enum_compare(const char *str1, const char *str2, int len)
{
size_t i;
#define string_enum_sep(x) ((x) == '-' || (x) == '_' || (x) == '.')
/* Diff-Header == DIFF_HEADER */
for (i = 0; i < len; i++) {
if (toupper(str1[i]) == toupper(str2[i]))
continue;
if (string_enum_sep(str1[i]) &&
string_enum_sep(str2[i]))
continue;
return str1[i] - str2[i];
}
return 0;
}
struct enum_map {
const char *name;
int namelen;
int value;
};
#define ENUM_MAP(name, value) { name, STRING_SIZE(name), value }
static bool
map_enum_do(const struct enum_map *map, size_t map_size, int *value, const char *name)
{
size_t namelen = strlen(name);
int i;
for (i = 0; i < map_size; i++)
if (namelen == map[i].namelen &&
!string_enum_compare(name, map[i].name, namelen)) {
*value = map[i].value;
return TRUE;
}
return FALSE;
}
#define map_enum(attr, map, name) \
map_enum_do(map, ARRAY_SIZE(map), attr, name)
#define prefixcmp(str1, str2) \
strncmp(str1, str2, STRING_SIZE(str2))
static inline int
suffixcmp(const char *str, int slen, const char *suffix)
{
size_t len = slen >= 0 ? slen : strlen(str);
size_t suffixlen = strlen(suffix);
return suffixlen < len ? strcmp(str + len - suffixlen, suffix) : -1;
}
static bool
argv_from_string(const char *argv[SIZEOF_ARG], int *argc, char *cmd)
{
int valuelen;
while (*cmd && *argc < SIZEOF_ARG && (valuelen = strcspn(cmd, " \t"))) {
bool advance = cmd[valuelen] != 0;
cmd[valuelen] = 0;
argv[(*argc)++] = chomp_string(cmd);
cmd = chomp_string(cmd + valuelen + advance);
}
if (*argc < SIZEOF_ARG)
argv[*argc] = NULL;
return *argc < SIZEOF_ARG;
}
static void
argv_from_env(const char **argv, const char *name)
{
char *env = argv ? getenv(name) : NULL;
int argc = 0;
if (env && *env)
env = strdup(env);
if (env && !argv_from_string(argv, &argc, env))
die("Too many arguments in the `%s` environment variable", name);
}
/*
* Executing external commands.
*/
enum io_type {
IO_FD, /* File descriptor based IO. */
IO_BG, /* Execute command in the background. */
IO_FG, /* Execute command with same std{in,out,err}. */
IO_RD, /* Read only fork+exec IO. */
IO_WR, /* Write only fork+exec IO. */
IO_AP, /* Append fork+exec output to file. */
};
struct io {
enum io_type type; /* The requested type of pipe. */
const char *dir; /* Directory from which to execute. */
pid_t pid; /* Pipe for reading or writing. */
int pipe; /* Pipe end for reading or writing. */
int error; /* Error status. */
const char *argv[SIZEOF_ARG]; /* Shell command arguments. */
char *buf; /* Read buffer. */
size_t bufalloc; /* Allocated buffer size. */
size_t bufsize; /* Buffer content size. */
char *bufpos; /* Current buffer position. */
unsigned int eof:1; /* Has end of file been reached. */
};
static void
reset_io(struct io *io)
{
io->pipe = -1;
io->pid = 0;
io->buf = io->bufpos = NULL;
io->bufalloc = io->bufsize = 0;
io->error = 0;
io->eof = 0;
}
static void
init_io(struct io *io, const char *dir, enum io_type type)
{
reset_io(io);
io->type = type;
io->dir = dir;
}
static bool
init_io_rd(struct io *io, const char *argv[], const char *dir,
enum format_flags flags)
{
init_io(io, dir, IO_RD);
return format_argv(io->argv, argv, flags);
}
static bool
io_open(struct io *io, const char *name)
{
init_io(io, NULL, IO_FD);
io->pipe = *name ? open(name, O_RDONLY) : STDIN_FILENO;
if (io->pipe == -1)
io->error = errno;
return io->pipe != -1;
}
static bool
kill_io(struct io *io)
{
return io->pid == 0 || kill(io->pid, SIGKILL) != -1;
}
static bool
done_io(struct io *io)
{
pid_t pid = io->pid;
if (io->pipe != -1)
close(io->pipe);
free(io->buf);
reset_io(io);
while (pid > 0) {
int status;
pid_t waiting = waitpid(pid, &status, 0);
if (waiting < 0) {
if (errno == EINTR)
continue;
report("waitpid failed (%s)", strerror(errno));
return FALSE;
}
return waiting == pid &&
!WIFSIGNALED(status) &&
WIFEXITED(status) &&
!WEXITSTATUS(status);
}
return TRUE;
}
static bool
start_io(struct io *io)
{
int pipefds[2] = { -1, -1 };
if (io->type == IO_FD)
return TRUE;
if ((io->type == IO_RD || io->type == IO_WR) &&
pipe(pipefds) < 0)
return FALSE;
else if (io->type == IO_AP)
pipefds[1] = io->pipe;
if ((io->pid = fork())) {
if (pipefds[!(io->type == IO_WR)] != -1)
close(pipefds[!(io->type == IO_WR)]);
if (io->pid != -1) {
io->pipe = pipefds[!!(io->type == IO_WR)];
return TRUE;
}
} else {
if (io->type != IO_FG) {
int devnull = open("/dev/null", O_RDWR);
int readfd = io->type == IO_WR ? pipefds[0] : devnull;
int writefd = (io->type == IO_RD || io->type == IO_AP)
? pipefds[1] : devnull;
dup2(readfd, STDIN_FILENO);
dup2(writefd, STDOUT_FILENO);
dup2(devnull, STDERR_FILENO);
close(devnull);
if (pipefds[0] != -1)
close(pipefds[0]);
if (pipefds[1] != -1)
close(pipefds[1]);
}
if (io->dir && *io->dir && chdir(io->dir) == -1)
die("Failed to change directory: %s", strerror(errno));
execvp(io->argv[0], (char *const*) io->argv);
die("Failed to execute program: %s", strerror(errno));
}
if (pipefds[!!(io->type == IO_WR)] != -1)
close(pipefds[!!(io->type == IO_WR)]);
return FALSE;
}
static bool
run_io(struct io *io, const char **argv, const char *dir, enum io_type type)
{
init_io(io, dir, type);
if (!format_argv(io->argv, argv, FORMAT_NONE))
return FALSE;
return start_io(io);
}
static int
run_io_do(struct io *io)
{
return start_io(io) && done_io(io);
}
static int
run_io_bg(const char **argv)
{
struct io io = {};
init_io(&io, NULL, IO_BG);
if (!format_argv(io.argv, argv, FORMAT_NONE))
return FALSE;
return run_io_do(&io);
}
static bool
run_io_fg(const char **argv, const char *dir)
{
struct io io = {};
init_io(&io, dir, IO_FG);
if (!format_argv(io.argv, argv, FORMAT_NONE))
return FALSE;
return run_io_do(&io);
}
static bool
run_io_append(const char **argv, enum format_flags flags, int fd)
{
struct io io = {};
init_io(&io, NULL, IO_AP);
io.pipe = fd;
if (format_argv(io.argv, argv, flags))
return run_io_do(&io);
close(fd);
return FALSE;
}
static bool
run_io_rd(struct io *io, const char **argv, enum format_flags flags)
{
return init_io_rd(io, argv, NULL, flags) && start_io(io);
}
static bool
io_eof(struct io *io)
{
return io->eof;
}
static int
io_error(struct io *io)
{
return io->error;
}
static char *
io_strerror(struct io *io)
{
return strerror(io->error);
}
static bool
io_can_read(struct io *io)
{
struct timeval tv = { 0, 500 };
fd_set fds;
FD_ZERO(&fds);
FD_SET(io->pipe, &fds);
return select(io->pipe + 1, &fds, NULL, NULL, &tv) > 0;
}
static ssize_t
io_read(struct io *io, void *buf, size_t bufsize)
{
do {
ssize_t readsize = read(io->pipe, buf, bufsize);
if (readsize < 0 && (errno == EAGAIN || errno == EINTR))
continue;
else if (readsize == -1)
io->error = errno;
else if (readsize == 0)
io->eof = 1;
return readsize;
} while (1);
}
static char *
io_get(struct io *io, int c, bool can_read)
{
char *eol;
ssize_t readsize;
if (!io->buf) {
io->buf = io->bufpos = malloc(BUFSIZ);
if (!io->buf)
return NULL;
io->bufalloc = BUFSIZ;
io->bufsize = 0;
}
while (TRUE) {
if (io->bufsize > 0) {
eol = memchr(io->bufpos, c, io->bufsize);
if (eol) {
char *line = io->bufpos;
*eol = 0;
io->bufpos = eol + 1;
io->bufsize -= io->bufpos - line;
return line;
}
}
if (io_eof(io)) {
if (io->bufsize) {
io->bufpos[io->bufsize] = 0;
io->bufsize = 0;
return io->bufpos;
}
return NULL;
}
if (!can_read)
return NULL;
if (io->bufsize > 0 && io->bufpos > io->buf)
memmove(io->buf, io->bufpos, io->bufsize);
io->bufpos = io->buf;
readsize = io_read(io, io->buf + io->bufsize, io->bufalloc - io->bufsize);
if (io_error(io))
return NULL;
io->bufsize += readsize;
}
}
static bool
io_write(struct io *io, const void *buf, size_t bufsize)
{
size_t written = 0;
while (!io_error(io) && written < bufsize) {
ssize_t size;
size = write(io->pipe, buf + written, bufsize - written);
if (size < 0 && (errno == EAGAIN || errno == EINTR))
continue;
else if (size == -1)
io->error = errno;
else
written += size;
}
return written == bufsize;
}
static bool
io_read_buf(struct io *io, char buf[], size_t bufsize)
{
bool error;
io->buf = io->bufpos = buf;
io->bufalloc = bufsize;
error = !io_get(io, '\n', TRUE) && io_error(io);
io->buf = NULL;
return done_io(io) || error;
}
static bool
run_io_buf(const char **argv, char buf[], size_t bufsize)
{
struct io io = {};
return run_io_rd(&io, argv, FORMAT_NONE) && io_read_buf(&io, buf, bufsize);
}
static int
io_load(struct io *io, const char *separators,
int (*read_property)(char *, size_t, char *, size_t))
{
char *name;
int state = OK;
if (!start_io(io))
return ERR;
while (state == OK && (name = io_get(io, '\n', TRUE))) {
char *value;
size_t namelen;
size_t valuelen;
name = chomp_string(name);
namelen = strcspn(name, separators);
if (name[namelen]) {
name[namelen] = 0;
value = chomp_string(name + namelen + 1);
valuelen = strlen(value);
} else {
value = "";
valuelen = 0;
}
state = read_property(name, namelen, value, valuelen);
}
if (state != ERR && io_error(io))
state = ERR;
done_io(io);
return state;
}
static int
run_io_load(const char **argv, const char *separators,
int (*read_property)(char *, size_t, char *, size_t))
{
struct io io = {};
return init_io_rd(&io, argv, NULL, FORMAT_NONE)
? io_load(&io, separators, read_property) : ERR;
}
/*
* User requests
*/
#define REQ_INFO \
/* XXX: Keep the view request first and in sync with views[]. */ \
REQ_GROUP("View switching") \
REQ_(VIEW_MAIN, "Show main view"), \
REQ_(VIEW_DIFF, "Show diff view"), \
REQ_(VIEW_LOG, "Show log view"), \
REQ_(VIEW_TREE, "Show tree view"), \
REQ_(VIEW_BLOB, "Show blob view"), \
REQ_(VIEW_BLAME, "Show blame view"), \
REQ_(VIEW_HELP, "Show help page"), \
REQ_(VIEW_PAGER, "Show pager view"), \
REQ_(VIEW_STATUS, "Show status view"), \
REQ_(VIEW_STAGE, "Show stage view"), \
\
REQ_GROUP("View manipulation") \
REQ_(ENTER, "Enter current line and scroll"), \
REQ_(NEXT, "Move to next"), \
REQ_(PREVIOUS, "Move to previous"), \
REQ_(PARENT, "Move to parent"), \
REQ_(VIEW_NEXT, "Move focus to next view"), \
REQ_(REFRESH, "Reload and refresh"), \
REQ_(MAXIMIZE, "Maximize the current view"), \
REQ_(VIEW_CLOSE, "Close the current view"), \
REQ_(QUIT, "Close all views and quit"), \
\
REQ_GROUP("View specific requests") \
REQ_(STATUS_UPDATE, "Update file status"), \
REQ_(STATUS_REVERT, "Revert file changes"), \
REQ_(STATUS_MERGE, "Merge file using external tool"), \
REQ_(STAGE_NEXT, "Find next chunk to stage"), \
\
REQ_GROUP("Cursor navigation") \
REQ_(MOVE_UP, "Move cursor one line up"), \
REQ_(MOVE_DOWN, "Move cursor one line down"), \
REQ_(MOVE_PAGE_DOWN, "Move cursor one page down"), \
REQ_(MOVE_PAGE_UP, "Move cursor one page up"), \
REQ_(MOVE_FIRST_LINE, "Move cursor to first line"), \
REQ_(MOVE_LAST_LINE, "Move cursor to last line"), \
\
REQ_GROUP("Scrolling") \
REQ_(SCROLL_LEFT, "Scroll two columns left"), \
REQ_(SCROLL_RIGHT, "Scroll two columns right"), \
REQ_(SCROLL_LINE_UP, "Scroll one line up"), \
REQ_(SCROLL_LINE_DOWN, "Scroll one line down"), \
REQ_(SCROLL_PAGE_UP, "Scroll one page up"), \
REQ_(SCROLL_PAGE_DOWN, "Scroll one page down"), \
\
REQ_GROUP("Searching") \
REQ_(SEARCH, "Search the view"), \
REQ_(SEARCH_BACK, "Search backwards in the view"), \
REQ_(FIND_NEXT, "Find next search match"), \
REQ_(FIND_PREV, "Find previous search match"), \
\
REQ_GROUP("Option manipulation") \
REQ_(TOGGLE_LINENO, "Toggle line numbers"), \
REQ_(TOGGLE_DATE, "Toggle date display"), \
REQ_(TOGGLE_AUTHOR, "Toggle author display"), \
REQ_(TOGGLE_REV_GRAPH, "Toggle revision graph visualization"), \
REQ_(TOGGLE_REFS, "Toggle reference display (tags/branches)"), \
\
REQ_GROUP("Misc") \
REQ_(PROMPT, "Bring up the prompt"), \
REQ_(SCREEN_REDRAW, "Redraw the screen"), \
REQ_(SHOW_VERSION, "Show version information"), \
REQ_(STOP_LOADING, "Stop all loading views"), \
REQ_(EDIT, "Open in editor"), \
REQ_(NONE, "Do nothing")
/* User action requests. */
enum request {
#define REQ_GROUP(help)
#define REQ_(req, help) REQ_##req
/* Offset all requests to avoid conflicts with ncurses getch values. */
REQ_OFFSET = KEY_MAX + 1,
REQ_INFO
#undef REQ_GROUP
#undef REQ_
};
struct request_info {
enum request request;
const char *name;
int namelen;
const char *help;
};
static const struct request_info req_info[] = {
#define REQ_GROUP(help) { 0, NULL, 0, (help) },
#define REQ_(req, help) { REQ_##req, (#req), STRING_SIZE(#req), (help) }
REQ_INFO
#undef REQ_GROUP
#undef REQ_
};
static enum request
get_request(const char *name)
{
int namelen = strlen(name);
int i;
for (i = 0; i < ARRAY_SIZE(req_info); i++)
if (req_info[i].namelen == namelen &&
!string_enum_compare(req_info[i].name, name, namelen))
return req_info[i].request;
return REQ_NONE;
}
/*
* Options
*/
/* Option and state variables. */
static bool opt_date = TRUE;
static bool opt_author = TRUE;
static bool opt_line_number = FALSE;
static bool opt_line_graphics = TRUE;
static bool opt_rev_graph = FALSE;
static bool opt_show_refs = TRUE;
static int opt_num_interval = NUMBER_INTERVAL;
static int opt_tab_size = TAB_SIZE;
static int opt_author_cols = AUTHOR_COLS-1;
static char opt_path[SIZEOF_STR] = "";
static char opt_file[SIZEOF_STR] = "";
static char opt_ref[SIZEOF_REF] = "";
static char opt_head[SIZEOF_REF] = "";
static char opt_head_rev[SIZEOF_REV] = "";
static char opt_remote[SIZEOF_REF] = "";
static char opt_encoding[20] = "UTF-8";
static bool opt_utf8 = TRUE;
static char opt_codeset[20] = "UTF-8";
static iconv_t opt_iconv = ICONV_NONE;
static char opt_search[SIZEOF_STR] = "";
static char opt_cdup[SIZEOF_STR] = "";
static char opt_prefix[SIZEOF_STR] = "";
static char opt_git_dir[SIZEOF_STR] = "";
static signed char opt_is_inside_work_tree = -1; /* set to TRUE or FALSE */
static char opt_editor[SIZEOF_STR] = "";
static FILE *opt_tty = NULL;
#define is_initial_commit() (!*opt_head_rev)
#define is_head_commit(rev) (!strcmp((rev), "HEAD") || !strcmp(opt_head_rev, (rev)))
/*
* Line-oriented content detection.
*/
#define LINE_INFO \
LINE(DIFF_HEADER, "diff --git ", COLOR_YELLOW, COLOR_DEFAULT, 0), \
LINE(DIFF_CHUNK, "@@", COLOR_MAGENTA, COLOR_DEFAULT, 0), \
LINE(DIFF_ADD, "+", COLOR_GREEN, COLOR_DEFAULT, 0), \
LINE(DIFF_DEL, "-", COLOR_RED, COLOR_DEFAULT, 0), \
LINE(DIFF_INDEX, "index ", COLOR_BLUE, COLOR_DEFAULT, 0), \
LINE(DIFF_OLDMODE, "old file mode ", COLOR_YELLOW, COLOR_DEFAULT, 0), \
LINE(DIFF_NEWMODE, "new file mode ", COLOR_YELLOW, COLOR_DEFAULT, 0), \
LINE(DIFF_COPY_FROM, "copy from", COLOR_YELLOW, COLOR_DEFAULT, 0), \
LINE(DIFF_COPY_TO, "copy to", COLOR_YELLOW, COLOR_DEFAULT, 0), \
LINE(DIFF_RENAME_FROM, "rename from", COLOR_YELLOW, COLOR_DEFAULT, 0), \
LINE(DIFF_RENAME_TO, "rename to", COLOR_YELLOW, COLOR_DEFAULT, 0), \
LINE(DIFF_SIMILARITY, "similarity ", COLOR_YELLOW, COLOR_DEFAULT, 0), \
LINE(DIFF_DISSIMILARITY,"dissimilarity ", COLOR_YELLOW, COLOR_DEFAULT, 0), \
LINE(DIFF_TREE, "diff-tree ", COLOR_BLUE, COLOR_DEFAULT, 0), \
LINE(PP_AUTHOR, "Author: ", COLOR_CYAN, COLOR_DEFAULT, 0), \
LINE(PP_COMMIT, "Commit: ", COLOR_MAGENTA, COLOR_DEFAULT, 0), \
LINE(PP_MERGE, "Merge: ", COLOR_BLUE, COLOR_DEFAULT, 0), \
LINE(PP_DATE, "Date: ", COLOR_YELLOW, COLOR_DEFAULT, 0), \
LINE(PP_ADATE, "AuthorDate: ", COLOR_YELLOW, COLOR_DEFAULT, 0), \
LINE(PP_CDATE, "CommitDate: ", COLOR_YELLOW, COLOR_DEFAULT, 0), \
LINE(PP_REFS, "Refs: ", COLOR_RED, COLOR_DEFAULT, 0), \
LINE(COMMIT, "commit ", COLOR_GREEN, COLOR_DEFAULT, 0), \
LINE(PARENT, "parent ", COLOR_BLUE, COLOR_DEFAULT, 0), \
LINE(TREE, "tree ", COLOR_BLUE, COLOR_DEFAULT, 0), \
LINE(AUTHOR, "author ", COLOR_GREEN, COLOR_DEFAULT, 0), \
LINE(COMMITTER, "committer ", COLOR_MAGENTA, COLOR_DEFAULT, 0), \
LINE(SIGNOFF, " Signed-off-by", COLOR_YELLOW, COLOR_DEFAULT, 0), \
LINE(ACKED, " Acked-by", COLOR_YELLOW, COLOR_DEFAULT, 0), \
LINE(DEFAULT, "", COLOR_DEFAULT, COLOR_DEFAULT, A_NORMAL), \
LINE(CURSOR, "", COLOR_WHITE, COLOR_GREEN, A_BOLD), \
LINE(STATUS, "", COLOR_GREEN, COLOR_DEFAULT, 0), \
LINE(DELIMITER, "", COLOR_MAGENTA, COLOR_DEFAULT, 0), \
LINE(DATE, "", COLOR_BLUE, COLOR_DEFAULT, 0), \
LINE(MODE, "", COLOR_CYAN, COLOR_DEFAULT, 0), \
LINE(LINE_NUMBER, "", COLOR_CYAN, COLOR_DEFAULT, 0), \
LINE(TITLE_BLUR, "", COLOR_WHITE, COLOR_BLUE, 0), \
LINE(TITLE_FOCUS, "", COLOR_WHITE, COLOR_BLUE, A_BOLD), \
LINE(MAIN_COMMIT, "", COLOR_DEFAULT, COLOR_DEFAULT, 0), \
LINE(MAIN_TAG, "", COLOR_MAGENTA, COLOR_DEFAULT, A_BOLD), \
LINE(MAIN_LOCAL_TAG,"", COLOR_MAGENTA, COLOR_DEFAULT, 0), \
LINE(MAIN_REMOTE, "", COLOR_YELLOW, COLOR_DEFAULT, 0), \
LINE(MAIN_TRACKED, "", COLOR_YELLOW, COLOR_DEFAULT, A_BOLD), \
LINE(MAIN_REF, "", COLOR_CYAN, COLOR_DEFAULT, 0), \
LINE(MAIN_HEAD, "", COLOR_CYAN, COLOR_DEFAULT, A_BOLD), \
LINE(MAIN_REVGRAPH,"", COLOR_MAGENTA, COLOR_DEFAULT, 0), \
LINE(TREE_HEAD, "", COLOR_DEFAULT, COLOR_DEFAULT, A_BOLD), \
LINE(TREE_DIR, "", COLOR_YELLOW, COLOR_DEFAULT, A_NORMAL), \
LINE(TREE_FILE, "", COLOR_DEFAULT, COLOR_DEFAULT, A_NORMAL), \
LINE(STAT_HEAD, "", COLOR_YELLOW, COLOR_DEFAULT, 0), \
LINE(STAT_SECTION, "", COLOR_CYAN, COLOR_DEFAULT, 0), \
LINE(STAT_NONE, "", COLOR_DEFAULT, COLOR_DEFAULT, 0), \
LINE(STAT_STAGED, "", COLOR_MAGENTA, COLOR_DEFAULT, 0), \
LINE(STAT_UNSTAGED,"", COLOR_MAGENTA, COLOR_DEFAULT, 0), \
LINE(STAT_UNTRACKED,"", COLOR_MAGENTA, COLOR_DEFAULT, 0), \
LINE(BLAME_ID, "", COLOR_MAGENTA, COLOR_DEFAULT, 0)
enum line_type {
#define LINE(type, line, fg, bg, attr) \
LINE_##type
LINE_INFO,
LINE_NONE
#undef LINE
};
struct line_info {
const char *name; /* Option name. */
int namelen; /* Size of option name. */
const char *line; /* The start of line to match. */
int linelen; /* Size of string to match. */
int fg, bg, attr; /* Color and text attributes for the lines. */
};
static struct line_info line_info[] = {
#define LINE(type, line, fg, bg, attr) \
{ #type, STRING_SIZE(#type), (line), STRING_SIZE(line), (fg), (bg), (attr) }
LINE_INFO
#undef LINE
};
static enum line_type
get_line_type(const char *line)
{
int linelen = strlen(line);
enum line_type type;
for (type = 0; type < ARRAY_SIZE(line_info); type++)
/* Case insensitive search matches Signed-off-by lines better. */
if (linelen >= line_info[type].linelen &&
!strncasecmp(line_info[type].line, line, line_info[type].linelen))
return type;
return LINE_DEFAULT;
}
static inline int
get_line_attr(enum line_type type)
{
assert(type < ARRAY_SIZE(line_info));
return COLOR_PAIR(type) | line_info[type].attr;
}
static struct line_info *
get_line_info(const char *name)
{
size_t namelen = strlen(name);
enum line_type type;