-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtig.c
7362 lines (6014 loc) · 167 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-2010 Jonas Fonseca <[email protected]>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of
* the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
#include "tig.h"
#include "io.h"
#include "graph.h"
static void __NORETURN die(const char *err, ...);
static void warn(const char *msg, ...);
static void report(const char *msg, ...);
struct ref {
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? */
char name[1]; /* Ref name; tag or head names are shortened. */
};
struct ref_list {
char id[SIZEOF_REV]; /* Commit SHA1 ID */
size_t size; /* Number of refs. */
struct ref **refs; /* References for this ID. */
};
static struct ref *get_ref_head();
static struct ref_list *get_ref_list(const char *id);
static void foreach_ref(bool (*visitor)(void *data, const struct ref *ref), void *data);
static int load_refs(void);
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);
struct menu_item {
int hotkey;
const char *text;
void *data;
};
static bool prompt_menu(const char *prompt, const struct menu_item *items, int *selected);
#define GRAPHIC_ENUM(_) \
_(GRAPHIC, ASCII), \
_(GRAPHIC, DEFAULT), \
_(GRAPHIC, UTF_8)
DEFINE_ENUM(graphic, GRAPHIC_ENUM);
#define DATE_ENUM(_) \
_(DATE, NO), \
_(DATE, DEFAULT), \
_(DATE, LOCAL), \
_(DATE, RELATIVE), \
_(DATE, SHORT)
DEFINE_ENUM(date, DATE_ENUM);
struct time {
time_t sec;
int tz;
};
static inline int timecmp(const struct time *t1, const struct time *t2)
{
return t1->sec - t2->sec;
}
static const char *
mkdate(const struct time *time, enum date date)
{
static char buf[DATE_COLS + 1];
static const struct enum_map reldate[] = {
{ "second", 1, 60 * 2 },
{ "minute", 60, 60 * 60 * 2 },
{ "hour", 60 * 60, 60 * 60 * 24 * 2 },
{ "day", 60 * 60 * 24, 60 * 60 * 24 * 7 * 2 },
{ "week", 60 * 60 * 24 * 7, 60 * 60 * 24 * 7 * 5 },
{ "month", 60 * 60 * 24 * 30, 60 * 60 * 24 * 30 * 12 },
};
struct tm tm;
if (!date || !time || !time->sec)
return "";
if (date == DATE_RELATIVE) {
struct timeval now;
time_t date = time->sec + time->tz;
time_t seconds;
int i;
gettimeofday(&now, NULL);
seconds = now.tv_sec < date ? date - now.tv_sec : now.tv_sec - date;
for (i = 0; i < ARRAY_SIZE(reldate); i++) {
if (seconds >= reldate[i].value)
continue;
seconds /= reldate[i].namelen;
if (!string_format(buf, "%ld %s%s %s",
seconds, reldate[i].name,
seconds > 1 ? "s" : "",
now.tv_sec >= date ? "ago" : "ahead"))
break;
return buf;
}
}
if (date == DATE_LOCAL) {
time_t date = time->sec + time->tz;
localtime_r(&date, &tm);
}
else {
gmtime_r(&time->sec, &tm);
}
return strftime(buf, sizeof(buf), DATE_FORMAT, &tm) ? buf : NULL;
}
#define AUTHOR_ENUM(_) \
_(AUTHOR, NO), \
_(AUTHOR, FULL), \
_(AUTHOR, ABBREVIATED)
DEFINE_ENUM(author, AUTHOR_ENUM);
static const char *
get_author_initials(const char *author)
{
static char initials[AUTHOR_COLS * 6 + 1];
size_t pos = 0;
const char *end = strchr(author, '\0');
#define is_initial_sep(c) (isspace(c) || ispunct(c) || (c) == '@' || (c) == '-')
memset(initials, 0, sizeof(initials));
while (author < end) {
unsigned char bytes;
size_t i;
while (author < end && is_initial_sep(*author))
author++;
bytes = utf8_char_length(author, end);
if (bytes >= sizeof(initials) - 1 - pos)
break;
while (bytes--) {
initials[pos++] = *author++;
}
i = pos;
while (author < end && !is_initial_sep(*author)) {
bytes = utf8_char_length(author, end);
if (bytes >= sizeof(initials) - 1 - i) {
while (author < end && !is_initial_sep(*author))
author++;
break;
}
while (bytes--) {
initials[i++] = *author++;
}
}
initials[i++] = 0;
}
return initials;
}
#define author_trim(cols) (cols == 0 || cols > 5)
static const char *
mkauthor(const char *text, int cols, enum author author)
{
bool trim = author_trim(cols);
bool abbreviate = author == AUTHOR_ABBREVIATED || !trim;
if (author == AUTHOR_NO)
return "";
if (abbreviate && text)
return get_author_initials(text);
return text;
}
static const char *
mkmode(mode_t mode)
{
if (S_ISDIR(mode))
return "drwxr-xr-x";
else if (S_ISLNK(mode))
return "lrwxrwxrwx";
else if (S_ISGITLINK(mode))
return "m---------";
else if (S_ISREG(mode) && mode & S_IXUSR)
return "-rwxr-xr-x";
else if (S_ISREG(mode))
return "-rw-r--r--";
else
return "----------";
}
/*
* 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_BRANCH, "Show branch 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_(DIFF_CONTEXT_DOWN, "Decrease the diff context"), \
REQ_(DIFF_CONTEXT_UP, "Increase the diff context"), \
\
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_FIRST_COL, "Scroll to the first line columns"), \
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_(OPTIONS, "Open option menu"), \
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_GRAPHIC, "Toggle (line) graphics mode"), \
REQ_(TOGGLE_REFS, "Toggle reference display (tags/branches)"), \
REQ_(TOGGLE_SORT_ORDER, "Toggle ascending/descending sort order"), \
REQ_(TOGGLE_SORT_FIELD, "Toggle field to sort by"), \
\
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_UNKNOWN = KEY_MAX + 1,
REQ_OFFSET,
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 (enum_equals(req_info[i], name, namelen))
return req_info[i].request;
return REQ_UNKNOWN;
}
/*
* Options
*/
/* Option and state variables. */
static enum graphic opt_line_graphics = GRAPHIC_DEFAULT;
static enum date opt_date = DATE_DEFAULT;
static enum author opt_author = AUTHOR_FULL;
static bool opt_rev_graph = TRUE;
static bool opt_line_number = FALSE;
static bool opt_show_refs = TRUE;
static bool opt_untracked_dirs_content = TRUE;
static int opt_diff_context = 3;
static char opt_diff_context_arg[9] = "";
static int opt_num_interval = 5;
static double opt_hscroll = 0.50;
static double opt_scale_split_view = 2.0 / 3.0;
static int opt_tab_size = 8;
static int opt_author_cols = AUTHOR_COLS;
static char opt_path[SIZEOF_STR] = "";
static char opt_file[SIZEOF_STR] = "";
static char opt_ref[SIZEOF_REF] = "";
static unsigned long opt_goto_line = 0;
static char opt_head[SIZEOF_REF] = "";
static char opt_remote[SIZEOF_REF] = "";
static char opt_encoding[20] = "UTF-8";
static iconv_t opt_iconv_in = ICONV_NONE;
static iconv_t opt_iconv_out = 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;
static const char **opt_diff_argv = NULL;
static const char **opt_rev_argv = NULL;
static const char **opt_file_argv = NULL;
static const char **opt_blame_argv = NULL;
#define is_initial_commit() (!get_ref_head())
#define is_head_commit(rev) (!strcmp((rev), "HEAD") || (get_ref_head() && !strcmp(rev, get_ref_head()->id)))
static inline void
update_diff_context_arg(int diff_context)
{
if (!string_format(opt_diff_context_arg, "-U%u", diff_context))
string_ncopy(opt_diff_context_arg, "-U3", 3);
}
/*
* 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_ADD2, " +", COLOR_GREEN, COLOR_DEFAULT, 0), \
LINE(DIFF_DEL, "-", COLOR_RED, COLOR_DEFAULT, 0), \
LINE(DIFF_DEL2, " -", 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(TESTED, " Tested-by", COLOR_YELLOW, COLOR_DEFAULT, 0), \
LINE(REVIEWED, " Reviewed-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(HELP_KEYMAP, "", COLOR_CYAN, COLOR_DEFAULT, 0), \
LINE(HELP_GROUP, "", COLOR_BLUE, COLOR_DEFAULT, 0), \
LINE(BLAME_ID, "", COLOR_MAGENTA, COLOR_DEFAULT, 0), \
LINE(DIFF_STAT, "", COLOR_BLUE, COLOR_DEFAULT, 0), \
LINE(PALETTE_0, "", COLOR_MAGENTA, COLOR_DEFAULT, 0), \
LINE(PALETTE_1, "", COLOR_YELLOW, COLOR_DEFAULT, 0), \
LINE(PALETTE_2, "", COLOR_CYAN, COLOR_DEFAULT, 0), \
LINE(PALETTE_3, "", COLOR_GREEN, COLOR_DEFAULT, 0), \
LINE(PALETTE_4, "", COLOR_DEFAULT, COLOR_DEFAULT, 0), \
LINE(PALETTE_5, "", COLOR_WHITE, COLOR_DEFAULT, 0), \
LINE(PALETTE_6, "", COLOR_RED, COLOR_DEFAULT, 0), \
LINE(GRAPH_COMMIT, "", COLOR_BLUE, 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 enum line_type
get_line_type_from_ref(const struct ref *ref)
{
if (ref->head)
return LINE_MAIN_HEAD;
else if (ref->ltag)
return LINE_MAIN_LOCAL_TAG;
else if (ref->tag)
return LINE_MAIN_TAG;
else if (ref->tracked)
return LINE_MAIN_TRACKED;
else if (ref->remote)
return LINE_MAIN_REMOTE;
return LINE_MAIN_REF;
}
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;
for (type = 0; type < ARRAY_SIZE(line_info); type++)
if (enum_equals(line_info[type], name, namelen))
return &line_info[type];
return NULL;
}
static void
init_colors(void)
{
int default_bg = line_info[LINE_DEFAULT].bg;
int default_fg = line_info[LINE_DEFAULT].fg;
enum line_type type;
start_color();
if (assume_default_colors(default_fg, default_bg) == ERR) {
default_bg = COLOR_BLACK;
default_fg = COLOR_WHITE;
}
for (type = 0; type < ARRAY_SIZE(line_info); type++) {
struct line_info *info = &line_info[type];
int bg = info->bg == COLOR_DEFAULT ? default_bg : info->bg;
int fg = info->fg == COLOR_DEFAULT ? default_fg : info->fg;
init_pair(type, fg, bg);
}
}
struct line {
enum line_type type;
/* State flags */
unsigned int selected:1;
unsigned int dirty:1;
unsigned int cleareol:1;
unsigned int other:16;
void *data; /* User data */
};
/*
* Keys
*/
struct keybinding {
int alias;
enum request request;
};
static struct keybinding default_keybindings[] = {
/* View switching */
{ 'm', REQ_VIEW_MAIN },
{ 'd', REQ_VIEW_DIFF },
{ 'l', REQ_VIEW_LOG },
{ 't', REQ_VIEW_TREE },
{ 'f', REQ_VIEW_BLOB },
{ 'B', REQ_VIEW_BLAME },
{ 'H', REQ_VIEW_BRANCH },
{ 'p', REQ_VIEW_PAGER },
{ 'h', REQ_VIEW_HELP },
{ 'S', REQ_VIEW_STATUS },
{ 'c', REQ_VIEW_STAGE },
/* View manipulation */
{ 'q', REQ_VIEW_CLOSE },
{ KEY_TAB, REQ_VIEW_NEXT },
{ KEY_RETURN, REQ_ENTER },
{ KEY_UP, REQ_PREVIOUS },
{ KEY_CTL('P'), REQ_PREVIOUS },
{ KEY_DOWN, REQ_NEXT },
{ KEY_CTL('N'), REQ_NEXT },
{ 'R', REQ_REFRESH },
{ KEY_F(5), REQ_REFRESH },
{ 'O', REQ_MAXIMIZE },
{ ',', REQ_PARENT },
/* View specific */
{ 'u', REQ_STATUS_UPDATE },
{ '!', REQ_STATUS_REVERT },
{ 'M', REQ_STATUS_MERGE },
{ '@', REQ_STAGE_NEXT },
{ '[', REQ_DIFF_CONTEXT_DOWN },
{ ']', REQ_DIFF_CONTEXT_UP },
/* Cursor navigation */
{ 'k', REQ_MOVE_UP },
{ 'j', REQ_MOVE_DOWN },
{ KEY_HOME, REQ_MOVE_FIRST_LINE },
{ KEY_END, REQ_MOVE_LAST_LINE },
{ KEY_NPAGE, REQ_MOVE_PAGE_DOWN },
{ KEY_CTL('D'), REQ_MOVE_PAGE_DOWN },
{ ' ', REQ_MOVE_PAGE_DOWN },
{ KEY_PPAGE, REQ_MOVE_PAGE_UP },
{ KEY_CTL('U'), REQ_MOVE_PAGE_UP },
{ 'b', REQ_MOVE_PAGE_UP },
{ '-', REQ_MOVE_PAGE_UP },
/* Scrolling */
{ '|', REQ_SCROLL_FIRST_COL },
{ KEY_LEFT, REQ_SCROLL_LEFT },
{ KEY_RIGHT, REQ_SCROLL_RIGHT },
{ KEY_IC, REQ_SCROLL_LINE_UP },
{ KEY_CTL('Y'), REQ_SCROLL_LINE_UP },
{ KEY_DC, REQ_SCROLL_LINE_DOWN },
{ KEY_CTL('E'), REQ_SCROLL_LINE_DOWN },
{ 'w', REQ_SCROLL_PAGE_UP },
{ 's', REQ_SCROLL_PAGE_DOWN },
/* Searching */
{ '/', REQ_SEARCH },
{ '?', REQ_SEARCH_BACK },
{ 'n', REQ_FIND_NEXT },
{ 'N', REQ_FIND_PREV },
/* Misc */
{ 'Q', REQ_QUIT },
{ 'z', REQ_STOP_LOADING },
{ 'v', REQ_SHOW_VERSION },
{ 'r', REQ_SCREEN_REDRAW },
{ KEY_CTL('L'), REQ_SCREEN_REDRAW },
{ 'o', REQ_OPTIONS },
{ '.', REQ_TOGGLE_LINENO },
{ 'D', REQ_TOGGLE_DATE },
{ 'A', REQ_TOGGLE_AUTHOR },
{ 'g', REQ_TOGGLE_REV_GRAPH },
{ '~', REQ_TOGGLE_GRAPHIC },
{ 'F', REQ_TOGGLE_REFS },
{ 'I', REQ_TOGGLE_SORT_ORDER },
{ 'i', REQ_TOGGLE_SORT_FIELD },
{ ':', REQ_PROMPT },
{ 'e', REQ_EDIT },
};
#define KEYMAP_ENUM(_) \
_(KEYMAP, GENERIC), \
_(KEYMAP, MAIN), \
_(KEYMAP, DIFF), \
_(KEYMAP, LOG), \
_(KEYMAP, TREE), \
_(KEYMAP, BLOB), \
_(KEYMAP, BLAME), \
_(KEYMAP, BRANCH), \
_(KEYMAP, PAGER), \
_(KEYMAP, HELP), \
_(KEYMAP, STATUS), \
_(KEYMAP, STAGE)
DEFINE_ENUM(keymap, KEYMAP_ENUM);
#define set_keymap(map, name) map_enum(map, keymap_map, name)
struct keybinding_table {
struct keybinding *data;
size_t size;
};
static struct keybinding_table keybindings[ARRAY_SIZE(keymap_map)];
static void
add_keybinding(enum keymap keymap, enum request request, int key)
{
struct keybinding_table *table = &keybindings[keymap];
size_t i;
for (i = 0; i < keybindings[keymap].size; i++) {
if (keybindings[keymap].data[i].alias == key) {
keybindings[keymap].data[i].request = request;
return;
}
}
table->data = realloc(table->data, (table->size + 1) * sizeof(*table->data));
if (!table->data)
die("Failed to allocate keybinding");
table->data[table->size].alias = key;
table->data[table->size++].request = request;
if (request == REQ_NONE && keymap == KEYMAP_GENERIC) {
int i;
for (i = 0; i < ARRAY_SIZE(default_keybindings); i++)
if (default_keybindings[i].alias == key)
default_keybindings[i].request = REQ_NONE;
}
}
/* Looks for a key binding first in the given map, then in the generic map, and
* lastly in the default keybindings. */
static enum request
get_keybinding(enum keymap keymap, int key)
{
size_t i;
for (i = 0; i < keybindings[keymap].size; i++)
if (keybindings[keymap].data[i].alias == key)
return keybindings[keymap].data[i].request;
for (i = 0; i < keybindings[KEYMAP_GENERIC].size; i++)
if (keybindings[KEYMAP_GENERIC].data[i].alias == key)
return keybindings[KEYMAP_GENERIC].data[i].request;
for (i = 0; i < ARRAY_SIZE(default_keybindings); i++)
if (default_keybindings[i].alias == key)
return default_keybindings[i].request;
return (enum request) key;
}
struct key {
const char *name;
int value;
};
static const struct key key_table[] = {
{ "Enter", KEY_RETURN },
{ "Space", ' ' },
{ "Backspace", KEY_BACKSPACE },
{ "Tab", KEY_TAB },
{ "Escape", KEY_ESC },
{ "Left", KEY_LEFT },
{ "Right", KEY_RIGHT },
{ "Up", KEY_UP },
{ "Down", KEY_DOWN },
{ "Insert", KEY_IC },
{ "Delete", KEY_DC },
{ "Hash", '#' },
{ "Home", KEY_HOME },
{ "End", KEY_END },
{ "PageUp", KEY_PPAGE },
{ "PageDown", KEY_NPAGE },
{ "F1", KEY_F(1) },
{ "F2", KEY_F(2) },
{ "F3", KEY_F(3) },
{ "F4", KEY_F(4) },
{ "F5", KEY_F(5) },
{ "F6", KEY_F(6) },
{ "F7", KEY_F(7) },
{ "F8", KEY_F(8) },
{ "F9", KEY_F(9) },
{ "F10", KEY_F(10) },
{ "F11", KEY_F(11) },
{ "F12", KEY_F(12) },
};
static int
get_key_value(const char *name)
{
int i;
for (i = 0; i < ARRAY_SIZE(key_table); i++)
if (!strcasecmp(key_table[i].name, name))
return key_table[i].value;
if (strlen(name) == 2 && name[0] == '^' && isprint(*name))
return (int)name[1] & 0x1f;
if (strlen(name) == 1 && isprint(*name))
return (int) *name;
return ERR;
}
static const char *
get_key_name(int key_value)
{
static char key_char[] = "'X'\0";
const char *seq = NULL;
int key;
for (key = 0; key < ARRAY_SIZE(key_table); key++)
if (key_table[key].value == key_value)
seq = key_table[key].name;
if (seq == NULL && key_value < 0x7f) {
char *s = key_char + 1;
if (key_value >= 0x20) {
*s++ = key_value;
} else {
*s++ = '^';
*s++ = 0x40 | (key_value & 0x1f);
}
*s++ = '\'';
*s++ = '\0';
seq = key_char;
}
return seq ? seq : "(no key)";
}
static bool
append_key(char *buf, size_t *pos, const struct keybinding *keybinding)
{
const char *sep = *pos > 0 ? ", " : "";
const char *keyname = get_key_name(keybinding->alias);
return string_nformat(buf, BUFSIZ, pos, "%s%s", sep, keyname);
}
static bool
append_keymap_request_keys(char *buf, size_t *pos, enum request request,
enum keymap keymap, bool all)
{
int i;
for (i = 0; i < keybindings[keymap].size; i++) {
if (keybindings[keymap].data[i].request == request) {
if (!append_key(buf, pos, &keybindings[keymap].data[i]))
return FALSE;
if (!all)
break;
}
}
return TRUE;
}
#define get_key(keymap, request) get_keys(keymap, request, FALSE)
static const char *
get_keys(enum keymap keymap, enum request request, bool all)
{
static char buf[BUFSIZ];
size_t pos = 0;
int i;
buf[pos] = 0;
if (!append_keymap_request_keys(buf, &pos, request, keymap, all))
return "Too many keybindings!";
if (pos > 0 && !all)
return buf;
if (keymap != KEYMAP_GENERIC) {
/* Only the generic keymap includes the default keybindings when
* listing all keys. */
if (all)
return buf;
if (!append_keymap_request_keys(buf, &pos, request, KEYMAP_GENERIC, all))
return "Too many keybindings!";
if (pos)
return buf;
}
for (i = 0; i < ARRAY_SIZE(default_keybindings); i++) {
if (default_keybindings[i].request == request) {
if (!append_key(buf, &pos, &default_keybindings[i]))
return "Too many keybindings!";
if (!all)
return buf;
}
}
return buf;
}
struct run_request {
enum keymap keymap;
int key;
const char **argv;
};
static struct run_request *run_request;
static size_t run_requests;
DEFINE_ALLOCATOR(realloc_run_requests, struct run_request, 8)
static enum request
add_run_request(enum keymap keymap, int key, const char **argv)
{
struct run_request *req;
if (!realloc_run_requests(&run_request, run_requests, 1))
return REQ_NONE;
req = &run_request[run_requests];
req->keymap = keymap;
req->key = key;
req->argv = NULL;
if (!argv_copy(&req->argv, argv))
return REQ_NONE;
return REQ_NONE + ++run_requests;
}
static struct run_request *
get_run_request(enum request request)
{
if (request <= REQ_NONE)
return NULL;
return &run_request[request - REQ_NONE - 1];
}
static void
add_builtin_run_requests(void)
{
const char *cherry_pick[] = { "git", "cherry-pick", "%(commit)", NULL };
const char *checkout[] = { "git", "checkout", "%(branch)", NULL };
const char *commit[] = { "git", "commit", NULL };
const char *gc[] = { "git", "gc", NULL };
struct run_request reqs[] = {
{ KEYMAP_MAIN, 'C', cherry_pick },
{ KEYMAP_STATUS, 'C', commit },
{ KEYMAP_BRANCH, 'C', checkout },
{ KEYMAP_GENERIC, 'G', gc },
};
int i;
for (i = 0; i < ARRAY_SIZE(reqs); i++) {
enum request req = get_keybinding(reqs[i].keymap, reqs[i].key);
if (req != reqs[i].key)
continue;
req = add_run_request(reqs[i].keymap, reqs[i].key, reqs[i].argv);
if (req != REQ_NONE)
add_keybinding(reqs[i].keymap, req, reqs[i].key);
}
}
/*
* User config file handling.
*/
#define OPT_ERR_INFO \
OPT_ERR_(INTEGER_VALUE_OUT_OF_BOUND, "Integer value out of bound"), \
OPT_ERR_(INVALID_STEP_VALUE, "Invalid step value"), \
OPT_ERR_(NO_OPTION_VALUE, "No option value"), \
OPT_ERR_(NO_VALUE_ASSIGNED, "No value assigned"), \
OPT_ERR_(OBSOLETE_REQUEST_NAME, "Obsolete request name"), \
OPT_ERR_(OUT_OF_MEMORY, "Out of memory"), \
OPT_ERR_(TOO_MANY_OPTION_ARGUMENTS, "Too many option arguments"), \
OPT_ERR_(UNKNOWN_ATTRIBUTE, "Unknown attribute"), \
OPT_ERR_(UNKNOWN_COLOR, "Unknown color"), \
OPT_ERR_(UNKNOWN_COLOR_NAME, "Unknown color name"), \
OPT_ERR_(UNKNOWN_KEY, "Unknown key"), \
OPT_ERR_(UNKNOWN_KEY_MAP, "Unknown key map"), \
OPT_ERR_(UNKNOWN_OPTION_COMMAND, "Unknown option command"), \
OPT_ERR_(UNKNOWN_REQUEST_NAME, "Unknown request name"), \
OPT_ERR_(UNKNOWN_VARIABLE_NAME, "Unknown variable name"), \
OPT_ERR_(UNMATCHED_QUOTATION, "Unmatched quotation"), \
OPT_ERR_(WRONG_NUMBER_OF_ARGUMENTS, "Wrong number of arguments"),
enum option_code {
#define OPT_ERR_(name, msg) OPT_ERR_ ## name
OPT_ERR_INFO
#undef OPT_ERR_
OPT_OK
};
static const char *option_errors[] = {
#define OPT_ERR_(name, msg) msg
OPT_ERR_INFO
#undef OPT_ERR_
};
static const struct enum_map color_map[] = {