-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathglg.cc
2500 lines (1960 loc) · 49.5 KB
/
glg.cc
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <termios.h>
#include <sys/ioctl.h>
#include <signal.h>
#include <errno.h>
#include <sys/wait.h>
#include <limits.h>
#include <sys/signalfd.h>
#include <poll.h>
#include <regex.h>
#include <ncurses.h>
#include <string>
#include <regex>
using namespace std;
#include <getopt.h>
#include "util.hh"
#include "git.hh"
#include "commit.hh"
static char *debug_file_path;
extern int errno;
static bool running = true;
char dying_msg[1024];
struct commit *current;
static void *xalloc(size_t size)
{
void *ret;
ret = calloc(sizeof(char), size);
if (!ret)
die("memory allocation failed");
return ret;
}
static void *xrealloc(void *ptr, size_t size)
{
void *ret;
assert(size);
ret = realloc(ptr, size);
if (!ret)
die("memory allocation failed");
return ret;
}
static int ret_nl_index(char *s)
{
int i;
for (i = 0; s[i] != '\n'; i++)
;
return i;
}
static int stdin_fd = 0, tty_fd;
static unsigned int row, col;
#define LINES_INIT_SIZE 128
enum class main_loop_state {
DEFAULT,
INPUT_SEARCH_QUERY,
SEARCHING_QUERY,
INPUT_SEARCH_FILTER,
INPUT_SEARCH_FILTER2,
INPUT_SEARCH_DIRECTION,
LAUNCH_GIT_COMMAND,
READ_BRANCHNAME_FOR_CHECKOUT,
SHOW_CHANGED_FILES,
HELP,
};
static main_loop_state state = main_loop_state::DEFAULT;
enum class long_run {
DEFAULT,
RUNNING,
STOPPED,
};
static long_run state_long_run = long_run::DEFAULT;
/*
* long_run_command(): called in the main loop when state_long_run == true
* return 1 when the iteration ends. return 0 for continuing.
*/
static int (*long_run_command)(void);
/*
* long_run_command_compl(): called after completion of long running command
*/
static void (*long_run_command_compl)(bool stopped);
enum class search_type {
REGEX,
FTS,
};
static search_type current_search_type;
#define BOTTOM_MESSAGE_INIT_SIZE 32
static char *bottom_message;
static unsigned int bottom_message_size = BOTTOM_MESSAGE_INIT_SIZE;
#define MATCH_ARRAY_INIT_SIZE 32
static regmatch_t *match_array;
static unsigned int match_array_size = MATCH_ARRAY_INIT_SIZE;
#define bmprintf(fmt, arg...) \
do { \
snprintf(bottom_message, bottom_message_size, \
fmt, ##arg); \
} while (0)
static FILE* debug_file;
#define debug_printf(fmt, arg...) \
do { \
fprintf(debug_file, fmt, ##arg); \
} while (0)
static void update_row_col(void)
{
struct winsize size;
bzero(&size, sizeof(struct winsize));
ioctl(tty_fd, TIOCGWINSZ, (void *)&size);
row = size.ws_row - 1;
col = size.ws_col;
if (bottom_message_size - 1 < col) {
bottom_message_size = col + 1;
bottom_message = static_cast<char *>(xrealloc(bottom_message, bottom_message_size));
}
if (match_array_size < col) {
match_array_size = col;
match_array = static_cast<regmatch_t *>(xrealloc(match_array,
match_array_size * sizeof(regmatch_t)));
}
resizeterm(size.ws_row, size.ws_col);
}
/* static struct termios attr; */
#define COLORING_PLUS 1
#define COLORING_MINUS 2
#define COLORING_ATMARK 3
#define COLORING_COMMIT 4
static void init_tty(void)
{
tty_fd = open("/dev/tty", O_RDONLY);
if (tty_fd < 0)
die("open()ing /dev/tty");
initscr();
cbreak();
noecho();
nonl();
/* intrflush(stdscr, FALSE); */
/* keypad(stdscr, TRUE); */
start_color();
init_pair(COLORING_PLUS, COLOR_GREEN, COLOR_BLACK);
init_pair(COLORING_MINUS, COLOR_RED, COLOR_BLACK);
init_pair(COLORING_ATMARK, COLOR_CYAN, COLOR_BLACK);
init_pair(COLORING_COMMIT, COLOR_YELLOW, COLOR_BLACK);
update_row_col();
}
#define raw_get_cached(c) (&c->cached) /* simply get pointer */
int contain_visible_char(char *buf)
{
int len = strlen(buf);
for (int i = 0; i < len; i++)
if (buf[i] != ' ') return i;
return -1;
}
static void init_commit_lines(struct commit *c)
{
struct commit_cached *cached = raw_get_cached(c);
char *text = cached->text;
int text_size = cached->text_size;
cached->lines_size = LINES_INIT_SIZE;
cached->lines = static_cast<char **>(xalloc(cached->lines_size * sizeof(char *)));
char *line_head = text;
cached->nr_lines = 0;
for (int i = 0; i < text_size; i++) {
if (text[i] != '\n')
continue;
cached->lines[cached->nr_lines++] = line_head;
if (line_head[0] == 'c') {
c->commit_id = static_cast<char *>(xalloc(41));
memcpy(c->commit_id, line_head + 7 /* strlen("commit ") */, 40);
}
line_head = &text[i + 1];
if (cached->lines_size == cached->nr_lines) {
cached->lines_size <<= 1;
cached->lines = static_cast<char **>(xrealloc(cached->lines,
cached->lines_size * sizeof(char *)));
}
}
if (c->summary)
return;
for (int i = 0; i < cached->nr_lines; i++) {
int j, len, nli;
char *line;
line = cached->lines[i];
if ((j = contain_visible_char(line)) < 1)
continue;
nli = ret_nl_index(&line[j]);
line[j + nli] = '\0';
len = strlen(&line[j]);
c->summary = static_cast<char *>(xalloc(len + 1));
strcpy(c->summary, &line[j]);
line[j + nli] = '\n';
break;
}
const char *hdr = "+++ b/";
int hdr_len = strlen(hdr);
for (int i = 0; i < cached->nr_lines; i++) {
char *l = cached->lines[i];
if (ret_nl_index(l) < 7 /* +++ b/ */)
continue;
if (memcmp(l, hdr, hdr_len))
continue;
int nl = ret_nl_index(l);
l[nl] = '\0';
char *copied = strdup(l + hdr_len);
if (!copied)
die("strdup() failed\n");
l[nl] = '\n';
if (c->nr_file_list == c->file_list_size) {
if (!c->file_list_size) {
assert(!c->nr_file_list);
c->file_list_size = 8;
} else
c->file_list_size <<= 1;
c->file_list = static_cast<char **>(xrealloc(c->file_list,
c->file_list_size * sizeof(char *)));
}
c->file_list[c->nr_file_list++] = copied;
}
int i = 0;
int commit_log_begin_idx = -1, commit_log_end_idx = -1;
for (int state = 0; i < cached->nr_lines; i++) {
char *l = cached->lines[i];
switch (state) {
case 0:
if (!strlen(l))
continue;
if (l[0] == ' ') {
state = 1; /* beginning of commit log */
commit_log_begin_idx = i;
}
break;
case 1:
if (l[0] == '\n') {
commit_log_end_idx = i;
break;
}
break;
default:
die("unknown state of commit log parser: %d\n", state);
break;
}
}
if (commit_log_end_idx == -1)
commit_log_end_idx = i - 1;
assert(commit_log_begin_idx != -1 && commit_log_end_idx != -1);
c->commit_log_lines = commit_log_end_idx - commit_log_begin_idx;
c->commit_log = static_cast<char **>(xalloc(c->commit_log_lines * sizeof(char *)));
for (int i = commit_log_begin_idx, j = 0; i < commit_log_end_idx; i++, j++) {
char *copied, *l;
l = cached->lines[i];
int nl = ret_nl_index(l);
l[nl] = '\0';
copied= strdup(l);
l[nl] = '\n';
if (!copied)
die("strdup() failed\n");
c->commit_log[j] = copied;
}
}
static struct commit *size_order_head;
#define ALLOC_LIM (1 << 30)
static size_t total_alloced;
static void free_commits(size_t size)
{
size_t freed = 0;
for (struct commit *p = size_order_head; p; p = p->size_next) {
struct commit_cached *pc = raw_get_cached(p);
if (pc->state == commit_cached_state::PURGED)
continue;
free(pc->text);
pc->text = NULL;
free(pc->lines);
pc->lines = NULL;
pc->state = commit_cached_state::PURGED;
freed += pc->text_size;
if (size < freed)
break;
}
if (freed < size)
die("memory allocation failed\n");
total_alloced -= freed;
}
static void text_alloc(struct commit *c)
/* FIXME: clearly, I need a smart algorithm... */
{
struct commit_cached *cached = raw_get_cached(c);
size_t size = cached->text_size;
if (ALLOC_LIM < total_alloced + size)
free_commits(size);
total_alloced += size;
cached->text = static_cast<char *>(calloc(sizeof(char), size));
if (!cached->text)
die("memory allocation failed");
if (c->size_order_initialized)
return;
if (!size_order_head) {
size_order_head = c;
c->size_order_initialized = true;
return;
}
struct commit *size_prev = nullptr;
for (struct commit *p = size_order_head; p; p = p->size_next) {
struct commit_cached *pc = raw_get_cached(p);
if (!(pc->text_size < size)) {
size_prev = p;
continue;
}
if (!size_prev) {
/* FIXME: this case should be handled before this loop */
assert(p == size_order_head);
c->size_next = size_order_head;
size_order_head = c;
goto end;
}
c->size_next = p;
size_prev->size_next = c;
goto end;
}
size_prev->size_next = c;
end:
c->size_order_initialized = true;
}
char *read_from_fd(int fd, unsigned int *len)
{
static char *buf;
static int buf_size;
if (!buf) {
buf_size = 1024;
buf = static_cast<char *>(xalloc(buf_size));
}
int rbytes = 0, ret;
while ((ret = read(fd, buf + rbytes, buf_size - rbytes))) {
if (ret == -1) {
if (errno == EINTR)
continue;
die("read() failed\n");
}
rbytes += ret;
if (rbytes == buf_size) {
/* expand only */
buf_size <<= 1;
buf = static_cast<char *>(xrealloc(buf, buf_size));
}
}
*len = rbytes;
return buf;
}
static void read_commit_with_git_show(struct commit *c)
{
int pipefds[2];
if (pipe(pipefds))
die("pipe() failed\n");
char *ret = nullptr;
pid_t pid = fork();
switch (pid) {
case 0:
close(1);
dup(pipefds[1]);
close(pipefds[0]);
if (execlp("git", "git", "show", c->commit_id, NULL))
die("execlp() failed\n");
break;
case -1:
die("fork() failed\n");
break;
default:
close(pipefds[1]);
ret = read_from_fd(pipefds[0], &c->cached.text_size);
waitpid(pid, NULL, 0);
close(pipefds[0]);
break;
}
text_alloc(c);
memcpy(c->cached.text, ret, c->cached.text_size);
}
static struct commit_cached *get_cached(struct commit *c)
{
if (c->cached.state == commit_cached_state::FILLED)
return &c->cached;
assert(!c->cached.text);
read_commit_with_git_show(c);
init_commit_lines(c);
c->cached.state = commit_cached_state::FILLED;
return &c->cached;
}
/* head: HEAD, root: root of the commit tree */
static struct commit *head, *root;
/* current: current displaying commit, tail: tail of the read commits */
static struct commit *tail;
static struct commit *range_begin, *range_end;
static regex_t *re_compiled;
static char **tokenized_query;
static int nr_tokenized_query, tokenized_query_size;
static void coloring(char ch, int on)
{
int color = 0;
switch (ch) {
case '+':
color = COLOR_PAIR(COLORING_PLUS);
break;
case '-':
color = COLOR_PAIR(COLORING_MINUS);
break;
case '@':
color = COLOR_PAIR(COLORING_ATMARK);
break;
case 'c':
color = COLOR_PAIR(COLORING_COMMIT);
break;
default:
return;
break;
}
if (on)
attron(color);
else
attroff(color);
}
static void update_terminal_default(void)
{
move(0, 0);
clear();
struct commit_cached *cached = get_cached(current);
int bm_len = strlen(bottom_message);
int i;
for (i = current->head_line;
i < current->head_line + row - !!bm_len && i < cached->nr_lines;
i++) {
int j;
char first_char;
char *line;
line = cached->lines[i];
first_char = line[0];
coloring(first_char, 1);
if (state == main_loop_state::SEARCHING_QUERY) {
if (current_search_type == search_type::REGEX) {
int ret, mi, nli = ret_nl_index(line);
int rev = 0;
line[nli] = '\0';
ret = regexec(re_compiled, line,
match_array_size, match_array, 0);
line[nli] = '\n';
if (ret)
goto normal_print;
for (mi = j = 0; j < col && line[j] != '\n'; j++) {
if (j == match_array[mi].rm_so) {
attron(A_REVERSE);
rev = 1;
} else if (j == match_array[mi].rm_eo) {
attroff(A_REVERSE);
rev = 0;
mi++;
}
if (match_array[mi].rm_so
== match_array[mi].rm_eo) {
attroff(A_REVERSE);
rev = 0;
mi++;
}
addch(line[j]);
}
if (rev)
attroff(A_REVERSE);
} else {
/* FIXME: disaster... */
assert(current_search_type == search_type::FTS);
int nli = ret_nl_index(line);
for (int i = 0; i < nli; i++) {
skip_token_print:
for (int j = 0; j < nr_tokenized_query; j++) {
char *q = tokenized_query[j];
int q_len = strlen(q);
if (nli - i < q_len)
continue;
if (strncmp(q, line + i, q_len))
continue;
attron(A_REVERSE);
for (int k = 0; k < q_len; k++)
addch(q[k]);
attroff(A_REVERSE);
i += q_len;
goto skip_token_print;
}
addch(line[i]);
}
}
} else {
normal_print:
for (j = 0; j < col && line[j] != '\n'; j++)
addch(line[j]);
}
addch('\n');
coloring(first_char, 0);
}
while (i++ < current->head_line + row - !!bm_len)
addch('\n');
move(row - !!bm_len, 0);
attron(A_REVERSE);
char bm_buf[row + 1]; /* we are using C99 */
char *p = bm_buf;
if (cached->nr_lines <= current->head_line + row)
snprintf(p, row, "100%%");
else
snprintf(p, row, "% .0f%%",
(float)(current->head_line + row)
/ cached->nr_lines * 100.0);
p += strlen(p);
if (current->head_line + row < cached->nr_lines)
snprintf(p, row - strlen(p), " (%d/%d)",
current->head_line + row, cached->nr_lines);
else
snprintf(p, row - strlen(p), " (%d/%d)",
cached->nr_lines, cached->nr_lines);
p += strlen(p);
snprintf(p, row - strlen(p), " ");
p += strlen(p);
for (i = 0; i < 8; i++) {
snprintf(p, row - strlen(p), "%c",
current->commit_id[i]);
p += strlen(p);
}
snprintf(p, row - strlen(p), ": ");
p += strlen(p);
char summary[81];
snprintf(summary, 80, "%s", current->summary);
snprintf(p, row - strlen(p), "%s", summary);
p += strlen(p);
printw("%s", bm_buf);
if (bm_len) {
move(row, 0);
printw("%s", bottom_message);
}
attroff(A_REVERSE);
refresh();
}
struct help_str {
char cmd;
char *desc;
};
struct help_str help_str_array[] = {
#define cmd(cmd, func, desc) { cmd, static_cast<char *>(const_cast<char *>(desc)) },
#include "default_cmd.def"
#undef cmd
{ '\0', NULL }
};
static void update_terminal_help(void)
{
int i;
move(0, 0);
printw("keystorkes supported in default state\n\n");
for (i = 0; help_str_array[i].cmd != '\0' ; i++) {
printw("%c: %s\n", help_str_array[i].cmd,
help_str_array[i].desc);
}
while (i++ < row)
addch('\n');
refresh();
}
static void update_terminal_show_changed_files(void)
{
move(0, 0);
printw("files changed in this commit:\n");
int i;
for (i = 0; i < current->nr_file_list; i++) {
printw(" %s\n", current->file_list[i]);
}
while (i++ < row - 1)
addch('\n');
printw("type 'q' to quit this mode\n");
refresh();
}
static void update_terminal(void)
{
switch (state) {
case main_loop_state::DEFAULT:
case main_loop_state::INPUT_SEARCH_QUERY:
case main_loop_state::SEARCHING_QUERY:
case main_loop_state::INPUT_SEARCH_FILTER:
case main_loop_state::INPUT_SEARCH_FILTER2:
case main_loop_state::INPUT_SEARCH_DIRECTION:
case main_loop_state::LAUNCH_GIT_COMMAND:
case main_loop_state::READ_BRANCHNAME_FOR_CHECKOUT:
update_terminal_default();
break;
case main_loop_state::SHOW_CHANGED_FILES:
update_terminal_show_changed_files();
break;
case main_loop_state::HELP:
update_terminal_help();
break;
default:
die("unknown state: %d\n", state);
break;
};
}
static void signal_handler(int signum)
{
switch (signum) {
case SIGWINCH:
update_row_col();
update_terminal();
break;
case SIGINT:
if (state_long_run == long_run::RUNNING)
/* FIXME: use signalfd */
state_long_run = long_run::STOPPED;
break;
default:
die("unknown signal: %d", signum);
break;
}
}
static int init_signalfd(void)
{
sigset_t mask;
int sigfd;
sigemptyset(&mask);
sigaddset(&mask, SIGWINCH);
sigaddset(&mask, SIGINT);
sigprocmask(SIG_BLOCK, &mask, NULL);
sigfd = signalfd(-1, &mask, SFD_NONBLOCK);
if (sigfd < 0) {
fprintf(stderr, "signalfd() failed: %s\n",
strerror(errno));
exit(1);
}
return sigfd;
}
static void read_commit(void)
{
static int read_end;
if (read_end)
return;
char commit_id[41]; /* with '\0' */
int ret, rbytes = 0;
while ((ret = read(stdin_fd, commit_id + rbytes, 41 - rbytes))) {
if (ret == -1) {
if (errno == EINTR)
continue;
die("read() failed\n");
}
rbytes += ret;
if (rbytes == 41)
break;
}
if (!ret)
read_end = 1;
else
assert(commit_id[40] == '\n');
commit_id[40] = '\0';
struct commit *new_commit = static_cast<struct commit *>(xalloc(sizeof(*new_commit)));
if (tail) {
assert(!tail->prev);
tail->prev = new_commit;
new_commit->next = tail;
tail = new_commit;
} else {
/* very unlikely */
assert(!head && !tail);
current = head = tail = new_commit;
}
new_commit->commit_id = static_cast<char *>(xalloc(41));
memcpy(new_commit->commit_id, commit_id, 41);
new_commit->cached.state = commit_cached_state::PURGED;
return;
}
static int show_prev_commit(char cmd)
{
if (current == range_begin) {
bmprintf("begin of range...");
return 0;
}
if (!current->prev) {
read_commit();
if (!current->prev)
return 0;
}
current = current->prev;
current->head_line = 0;
return 1;
}
static int show_next_commit(char cmd)
{
if (current == range_end) {
bmprintf("end of range...");
return 1;
}
if (!current->next) {
assert(current == head);
return 0;
}
current = current->next;
current->head_line = 0;
return 1;
}
static int forward_line(char cmd)
{
struct commit_cached *cached = get_cached(current);
if (current->head_line + row < cached->nr_lines) {
current->head_line++;
return 1;
}
return 0;
}
static int backward_line(char cmd)
{
if (0 < current->head_line) {
current->head_line--;
return 1;
}
return 0;
}
static int goto_top(char cmd)
{
if (!current->head_line)
return 0;
current->head_line = 0;
return 1;
}
static int goto_bottom(char cmd)
{
struct commit_cached *cached = get_cached(current);
if (cached->nr_lines < row)
return 0;
current->head_line = cached->nr_lines - row;
return 1;
}
static int forward_page(char cmd)
{
struct commit_cached *cached = get_cached(current);
if (cached->nr_lines < current->head_line + row)
return 0;
current->head_line += row;
if (cached->nr_lines < current->head_line + row)
current->head_line = cached->nr_lines - row;
return 1;
}
static int backward_page(char cmd)
{
if (!current->head_line)
return 0;
current->head_line -= row;
if (current->head_line < 0)
current->head_line = 0;
return 1;
}
static struct commit *orig_before_visit_root;
static int long_run_command_visit_root(void)
{
if (!current->prev)
read_commit();
if (!current->prev) {
long_run_command = NULL;
return 1;
}
current = current->prev;
return 0;
}
static void long_run_command_compl_visit_root(bool stopped)
{
if (!stopped)
goto end;
current = orig_before_visit_root;
bmprintf("stop visiting root commit");