Blame


1 9f7d7167 2018-04-29 stsp /*
2 9f7d7167 2018-04-29 stsp * Copyright (c) 2018 Stefan Sperling <stsp@openbsd.org>
3 9f7d7167 2018-04-29 stsp *
4 9f7d7167 2018-04-29 stsp * Permission to use, copy, modify, and distribute this software for any
5 9f7d7167 2018-04-29 stsp * purpose with or without fee is hereby granted, provided that the above
6 9f7d7167 2018-04-29 stsp * copyright notice and this permission notice appear in all copies.
7 9f7d7167 2018-04-29 stsp *
8 9f7d7167 2018-04-29 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 9f7d7167 2018-04-29 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 9f7d7167 2018-04-29 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 9f7d7167 2018-04-29 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 9f7d7167 2018-04-29 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 9f7d7167 2018-04-29 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 9f7d7167 2018-04-29 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 9f7d7167 2018-04-29 stsp */
16 9f7d7167 2018-04-29 stsp
17 80ddbec8 2018-04-29 stsp #include <sys/queue.h>
18 80ddbec8 2018-04-29 stsp
19 31120ada 2018-04-30 stsp #include <errno.h>
20 61e69b96 2018-05-20 stsp #define _XOPEN_SOURCE_EXTENDED
21 9f7d7167 2018-04-29 stsp #include <curses.h>
22 61e69b96 2018-05-20 stsp #undef _XOPEN_SOURCE_EXTENDED
23 9f7d7167 2018-04-29 stsp #include <panel.h>
24 9f7d7167 2018-04-29 stsp #include <locale.h>
25 9f7d7167 2018-04-29 stsp #include <stdlib.h>
26 26ed57b2 2018-05-19 stsp #include <stdio.h>
27 9f7d7167 2018-04-29 stsp #include <getopt.h>
28 9f7d7167 2018-04-29 stsp #include <string.h>
29 9f7d7167 2018-04-29 stsp #include <err.h>
30 80ddbec8 2018-04-29 stsp #include <unistd.h>
31 26ed57b2 2018-05-19 stsp #include <util.h>
32 26ed57b2 2018-05-19 stsp #include <limits.h>
33 61e69b96 2018-05-20 stsp #include <wchar.h>
34 788c352e 2018-06-16 stsp #include <time.h>
35 9f7d7167 2018-04-29 stsp
36 9f7d7167 2018-04-29 stsp #include "got_error.h"
37 80ddbec8 2018-04-29 stsp #include "got_object.h"
38 80ddbec8 2018-04-29 stsp #include "got_reference.h"
39 80ddbec8 2018-04-29 stsp #include "got_repository.h"
40 80ddbec8 2018-04-29 stsp #include "got_diff.h"
41 511a516b 2018-05-19 stsp #include "got_opentemp.h"
42 9ba79e04 2018-06-11 stsp #include "got_commit_graph.h"
43 00dfcb92 2018-06-11 stsp #include "got_utf8.h"
44 a70480e0 2018-06-23 stsp #include "got_blame.h"
45 9f7d7167 2018-04-29 stsp
46 881b2d3e 2018-04-30 stsp #ifndef MIN
47 881b2d3e 2018-04-30 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
48 881b2d3e 2018-04-30 stsp #endif
49 881b2d3e 2018-04-30 stsp
50 9f7d7167 2018-04-29 stsp #ifndef nitems
51 9f7d7167 2018-04-29 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
52 9f7d7167 2018-04-29 stsp #endif
53 9f7d7167 2018-04-29 stsp
54 9f7d7167 2018-04-29 stsp struct tog_cmd {
55 c2301be8 2018-04-30 stsp const char *name;
56 9f7d7167 2018-04-29 stsp const struct got_error *(*cmd_main)(int, char *[]);
57 9f7d7167 2018-04-29 stsp void (*cmd_usage)(void);
58 c2301be8 2018-04-30 stsp const char *descr;
59 9f7d7167 2018-04-29 stsp };
60 9f7d7167 2018-04-29 stsp
61 4ed7e80c 2018-05-20 stsp __dead static void usage(void);
62 4ed7e80c 2018-05-20 stsp __dead static void usage_log(void);
63 4ed7e80c 2018-05-20 stsp __dead static void usage_diff(void);
64 4ed7e80c 2018-05-20 stsp __dead static void usage_blame(void);
65 9f7d7167 2018-04-29 stsp
66 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_log(int, char *[]);
67 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_diff(int, char *[]);
68 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_blame(int, char *[]);
69 9f7d7167 2018-04-29 stsp
70 4ed7e80c 2018-05-20 stsp static struct tog_cmd tog_commands[] = {
71 cbb6b58a 2018-05-20 stsp { "log", cmd_log, usage_log,
72 9f7d7167 2018-04-29 stsp "show repository history" },
73 cbb6b58a 2018-05-20 stsp { "diff", cmd_diff, usage_diff,
74 9f7d7167 2018-04-29 stsp "compare files and directories" },
75 cbb6b58a 2018-05-20 stsp { "blame", cmd_blame, usage_blame,
76 9f7d7167 2018-04-29 stsp "show line-by-line file history" },
77 9f7d7167 2018-04-29 stsp };
78 9f7d7167 2018-04-29 stsp
79 fed328cc 2018-05-20 stsp static struct tog_view {
80 26ed57b2 2018-05-19 stsp WINDOW *window;
81 26ed57b2 2018-05-19 stsp PANEL *panel;
82 a70480e0 2018-06-23 stsp } tog_log_view, tog_diff_view, tog_blame_view;
83 cd0acaa7 2018-05-20 stsp
84 cd0acaa7 2018-05-20 stsp static const struct got_error *
85 cd0acaa7 2018-05-20 stsp show_diff_view(struct got_object *, struct got_object *,
86 cd0acaa7 2018-05-20 stsp struct got_repository *);
87 cd0acaa7 2018-05-20 stsp static const struct got_error *
88 cd0acaa7 2018-05-20 stsp show_log_view(struct got_object_id *, struct got_repository *);
89 a70480e0 2018-06-23 stsp static const struct got_error *
90 a70480e0 2018-06-23 stsp show_blame_view(const char *, struct got_object_id *, struct got_repository *);
91 26ed57b2 2018-05-19 stsp
92 4ed7e80c 2018-05-20 stsp __dead static void
93 9f7d7167 2018-04-29 stsp usage_log(void)
94 9f7d7167 2018-04-29 stsp {
95 80ddbec8 2018-04-29 stsp endwin();
96 80ddbec8 2018-04-29 stsp fprintf(stderr, "usage: %s log [-c commit] [repository-path]\n",
97 9f7d7167 2018-04-29 stsp getprogname());
98 9f7d7167 2018-04-29 stsp exit(1);
99 80ddbec8 2018-04-29 stsp }
100 80ddbec8 2018-04-29 stsp
101 963b370f 2018-05-20 stsp /* Create newly allocated wide-character string equivalent to a byte string. */
102 80ddbec8 2018-04-29 stsp static const struct got_error *
103 963b370f 2018-05-20 stsp mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
104 963b370f 2018-05-20 stsp {
105 00dfcb92 2018-06-11 stsp char *vis = NULL;
106 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
107 963b370f 2018-05-20 stsp
108 963b370f 2018-05-20 stsp *ws = NULL;
109 963b370f 2018-05-20 stsp *wlen = mbstowcs(NULL, s, 0);
110 00dfcb92 2018-06-11 stsp if (*wlen == (size_t)-1) {
111 00dfcb92 2018-06-11 stsp int vislen;
112 00dfcb92 2018-06-11 stsp if (errno != EILSEQ)
113 00dfcb92 2018-06-11 stsp return got_error_from_errno();
114 00dfcb92 2018-06-11 stsp
115 00dfcb92 2018-06-11 stsp /* byte string invalid in current encoding; try to "fix" it */
116 00dfcb92 2018-06-11 stsp err = got_mbsavis(&vis, &vislen, s);
117 00dfcb92 2018-06-11 stsp if (err)
118 00dfcb92 2018-06-11 stsp return err;
119 00dfcb92 2018-06-11 stsp *wlen = mbstowcs(NULL, vis, 0);
120 a7f50699 2018-06-11 stsp if (*wlen == (size_t)-1) {
121 a7f50699 2018-06-11 stsp err = got_error_from_errno(); /* give up */
122 a7f50699 2018-06-11 stsp goto done;
123 a7f50699 2018-06-11 stsp }
124 00dfcb92 2018-06-11 stsp }
125 963b370f 2018-05-20 stsp
126 963b370f 2018-05-20 stsp *ws = calloc(*wlen + 1, sizeof(*ws));
127 a7f50699 2018-06-11 stsp if (*ws == NULL) {
128 a7f50699 2018-06-11 stsp err = got_error_from_errno();
129 a7f50699 2018-06-11 stsp goto done;
130 a7f50699 2018-06-11 stsp }
131 963b370f 2018-05-20 stsp
132 00dfcb92 2018-06-11 stsp if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
133 963b370f 2018-05-20 stsp err = got_error_from_errno();
134 a7f50699 2018-06-11 stsp done:
135 00dfcb92 2018-06-11 stsp free(vis);
136 963b370f 2018-05-20 stsp if (err) {
137 963b370f 2018-05-20 stsp free(*ws);
138 963b370f 2018-05-20 stsp *ws = NULL;
139 963b370f 2018-05-20 stsp *wlen = 0;
140 963b370f 2018-05-20 stsp }
141 963b370f 2018-05-20 stsp return err;
142 963b370f 2018-05-20 stsp }
143 963b370f 2018-05-20 stsp
144 963b370f 2018-05-20 stsp /* Format a line for display, ensuring that it won't overflow a width limit. */
145 963b370f 2018-05-20 stsp static const struct got_error *
146 963b370f 2018-05-20 stsp format_line(wchar_t **wlinep, int *widthp, char *line, int wlimit)
147 963b370f 2018-05-20 stsp {
148 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
149 963b370f 2018-05-20 stsp int cols = 0;
150 963b370f 2018-05-20 stsp wchar_t *wline = NULL;
151 963b370f 2018-05-20 stsp size_t wlen;
152 963b370f 2018-05-20 stsp int i;
153 963b370f 2018-05-20 stsp
154 963b370f 2018-05-20 stsp *wlinep = NULL;
155 963b370f 2018-05-20 stsp
156 963b370f 2018-05-20 stsp err = mbs2ws(&wline, &wlen, line);
157 963b370f 2018-05-20 stsp if (err)
158 963b370f 2018-05-20 stsp return err;
159 963b370f 2018-05-20 stsp
160 963b370f 2018-05-20 stsp i = 0;
161 963b370f 2018-05-20 stsp while (i < wlen && cols <= wlimit) {
162 963b370f 2018-05-20 stsp int width = wcwidth(wline[i]);
163 963b370f 2018-05-20 stsp switch (width) {
164 963b370f 2018-05-20 stsp case 0:
165 963b370f 2018-05-20 stsp break;
166 963b370f 2018-05-20 stsp case 1:
167 963b370f 2018-05-20 stsp case 2:
168 963b370f 2018-05-20 stsp cols += width;
169 963b370f 2018-05-20 stsp break;
170 963b370f 2018-05-20 stsp case -1:
171 963b370f 2018-05-20 stsp if (wline[i] == L'\t')
172 963b370f 2018-05-20 stsp cols += TABSIZE;
173 963b370f 2018-05-20 stsp break;
174 963b370f 2018-05-20 stsp default:
175 963b370f 2018-05-20 stsp err = got_error_from_errno();
176 963b370f 2018-05-20 stsp goto done;
177 963b370f 2018-05-20 stsp }
178 963b370f 2018-05-20 stsp if (cols <= COLS) {
179 963b370f 2018-05-20 stsp i++;
180 963b370f 2018-05-20 stsp if (widthp)
181 963b370f 2018-05-20 stsp *widthp = cols;
182 963b370f 2018-05-20 stsp }
183 963b370f 2018-05-20 stsp }
184 963b370f 2018-05-20 stsp wline[i] = L'\0';
185 963b370f 2018-05-20 stsp done:
186 963b370f 2018-05-20 stsp if (err)
187 963b370f 2018-05-20 stsp free(wline);
188 963b370f 2018-05-20 stsp else
189 963b370f 2018-05-20 stsp *wlinep = wline;
190 963b370f 2018-05-20 stsp return err;
191 963b370f 2018-05-20 stsp }
192 963b370f 2018-05-20 stsp
193 963b370f 2018-05-20 stsp static const struct got_error *
194 0553a4e3 2018-04-30 stsp draw_commit(struct got_commit_object *commit, struct got_object_id *id)
195 80ddbec8 2018-04-29 stsp {
196 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
197 80ddbec8 2018-04-29 stsp char *logmsg0 = NULL, *logmsg = NULL;
198 6d9fbc00 2018-04-29 stsp char *author0 = NULL, *author = NULL;
199 bb737323 2018-05-20 stsp wchar_t *wlogmsg = NULL, *wauthor = NULL;
200 bb737323 2018-05-20 stsp int author_width, logmsg_width;
201 6d9fbc00 2018-04-29 stsp char *newline, *smallerthan;
202 80ddbec8 2018-04-29 stsp char *line = NULL;
203 6d9fbc00 2018-04-29 stsp char *id_str = NULL;
204 bb737323 2018-05-20 stsp size_t id_len;
205 bb737323 2018-05-20 stsp int col, limit;
206 bb737323 2018-05-20 stsp static const size_t id_display_cols = 8;
207 bb737323 2018-05-20 stsp static const size_t author_display_cols = 16;
208 9c2eaf34 2018-05-20 stsp const int avail = COLS;
209 80ddbec8 2018-04-29 stsp
210 80ddbec8 2018-04-29 stsp err = got_object_id_str(&id_str, id);
211 80ddbec8 2018-04-29 stsp if (err)
212 80ddbec8 2018-04-29 stsp return err;
213 881b2d3e 2018-04-30 stsp id_len = strlen(id_str);
214 bb737323 2018-05-20 stsp if (avail < id_display_cols) {
215 bb737323 2018-05-20 stsp limit = MIN(id_len, avail);
216 bb737323 2018-05-20 stsp waddnstr(tog_log_view.window, id_str, limit);
217 bb737323 2018-05-20 stsp } else {
218 bb737323 2018-05-20 stsp limit = MIN(id_display_cols, id_len);
219 bb737323 2018-05-20 stsp waddnstr(tog_log_view.window, id_str, limit);
220 6d9fbc00 2018-04-29 stsp }
221 bb737323 2018-05-20 stsp col = limit + 1;
222 9c2eaf34 2018-05-20 stsp while (col <= avail && col < id_display_cols + 2) {
223 bb737323 2018-05-20 stsp waddch(tog_log_view.window, ' ');
224 bb737323 2018-05-20 stsp col++;
225 bb737323 2018-05-20 stsp }
226 9c2eaf34 2018-05-20 stsp if (col > avail)
227 9c2eaf34 2018-05-20 stsp goto done;
228 80ddbec8 2018-04-29 stsp
229 6d9fbc00 2018-04-29 stsp author0 = strdup(commit->author);
230 6d9fbc00 2018-04-29 stsp if (author0 == NULL) {
231 80ddbec8 2018-04-29 stsp err = got_error_from_errno();
232 80ddbec8 2018-04-29 stsp goto done;
233 80ddbec8 2018-04-29 stsp }
234 6d9fbc00 2018-04-29 stsp author = author0;
235 6d9fbc00 2018-04-29 stsp smallerthan = strchr(author, '<');
236 6d9fbc00 2018-04-29 stsp if (smallerthan)
237 6d9fbc00 2018-04-29 stsp *smallerthan = '\0';
238 6d9fbc00 2018-04-29 stsp else {
239 6d9fbc00 2018-04-29 stsp char *at = strchr(author, '@');
240 6d9fbc00 2018-04-29 stsp if (at)
241 6d9fbc00 2018-04-29 stsp *at = '\0';
242 6d9fbc00 2018-04-29 stsp }
243 bb737323 2018-05-20 stsp limit = MIN(avail, author_display_cols);
244 bb737323 2018-05-20 stsp err = format_line(&wauthor, &author_width, author, limit);
245 bb737323 2018-05-20 stsp if (err)
246 bb737323 2018-05-20 stsp goto done;
247 bb737323 2018-05-20 stsp waddwstr(tog_log_view.window, wauthor);
248 bb737323 2018-05-20 stsp col += author_width;
249 9c2eaf34 2018-05-20 stsp while (col <= avail && author_width < author_display_cols + 1) {
250 bb737323 2018-05-20 stsp waddch(tog_log_view.window, ' ');
251 bb737323 2018-05-20 stsp col++;
252 bb737323 2018-05-20 stsp author_width++;
253 bb737323 2018-05-20 stsp }
254 9c2eaf34 2018-05-20 stsp if (col > avail)
255 9c2eaf34 2018-05-20 stsp goto done;
256 80ddbec8 2018-04-29 stsp
257 bb737323 2018-05-20 stsp logmsg0 = strdup(commit->logmsg);
258 bb737323 2018-05-20 stsp if (logmsg0 == NULL) {
259 6d9fbc00 2018-04-29 stsp err = got_error_from_errno();
260 6d9fbc00 2018-04-29 stsp goto done;
261 6d9fbc00 2018-04-29 stsp }
262 bb737323 2018-05-20 stsp logmsg = logmsg0;
263 bb737323 2018-05-20 stsp while (*logmsg == '\n')
264 bb737323 2018-05-20 stsp logmsg++;
265 bb737323 2018-05-20 stsp newline = strchr(logmsg, '\n');
266 bb737323 2018-05-20 stsp if (newline)
267 bb737323 2018-05-20 stsp *newline = '\0';
268 bb737323 2018-05-20 stsp limit = avail - col;
269 bb737323 2018-05-20 stsp err = format_line(&wlogmsg, &logmsg_width, logmsg, limit);
270 bb737323 2018-05-20 stsp if (err)
271 bb737323 2018-05-20 stsp goto done;
272 bb737323 2018-05-20 stsp waddwstr(tog_log_view.window, wlogmsg);
273 bb737323 2018-05-20 stsp col += logmsg_width;
274 bb737323 2018-05-20 stsp while (col <= avail) {
275 bb737323 2018-05-20 stsp waddch(tog_log_view.window, ' ');
276 bb737323 2018-05-20 stsp col++;
277 881b2d3e 2018-04-30 stsp }
278 80ddbec8 2018-04-29 stsp done:
279 80ddbec8 2018-04-29 stsp free(logmsg0);
280 bb737323 2018-05-20 stsp free(wlogmsg);
281 6d9fbc00 2018-04-29 stsp free(author0);
282 bb737323 2018-05-20 stsp free(wauthor);
283 80ddbec8 2018-04-29 stsp free(line);
284 6d9fbc00 2018-04-29 stsp free(id_str);
285 80ddbec8 2018-04-29 stsp return err;
286 80ddbec8 2018-04-29 stsp }
287 26ed57b2 2018-05-19 stsp
288 80ddbec8 2018-04-29 stsp struct commit_queue_entry {
289 80ddbec8 2018-04-29 stsp TAILQ_ENTRY(commit_queue_entry) entry;
290 80ddbec8 2018-04-29 stsp struct got_object_id *id;
291 80ddbec8 2018-04-29 stsp struct got_commit_object *commit;
292 80ddbec8 2018-04-29 stsp };
293 0553a4e3 2018-04-30 stsp TAILQ_HEAD(commit_queue, commit_queue_entry);
294 80ddbec8 2018-04-29 stsp
295 899d86c2 2018-05-10 stsp static struct commit_queue_entry *
296 899d86c2 2018-05-10 stsp alloc_commit_queue_entry(struct got_commit_object *commit,
297 899d86c2 2018-05-10 stsp struct got_object_id *id)
298 80ddbec8 2018-04-29 stsp {
299 80ddbec8 2018-04-29 stsp struct commit_queue_entry *entry;
300 80ddbec8 2018-04-29 stsp
301 80ddbec8 2018-04-29 stsp entry = calloc(1, sizeof(*entry));
302 80ddbec8 2018-04-29 stsp if (entry == NULL)
303 899d86c2 2018-05-10 stsp return NULL;
304 99db9666 2018-05-07 stsp
305 899d86c2 2018-05-10 stsp entry->id = id;
306 99db9666 2018-05-07 stsp entry->commit = commit;
307 899d86c2 2018-05-10 stsp return entry;
308 99db9666 2018-05-07 stsp }
309 80ddbec8 2018-04-29 stsp
310 99db9666 2018-05-07 stsp static void
311 99db9666 2018-05-07 stsp pop_commit(struct commit_queue *commits)
312 99db9666 2018-05-07 stsp {
313 99db9666 2018-05-07 stsp struct commit_queue_entry *entry;
314 99db9666 2018-05-07 stsp
315 99db9666 2018-05-07 stsp entry = TAILQ_FIRST(commits);
316 99db9666 2018-05-07 stsp TAILQ_REMOVE(commits, entry, entry);
317 99db9666 2018-05-07 stsp got_object_commit_close(entry->commit);
318 9ba79e04 2018-06-11 stsp /* Don't free entry->id! It is owned by the commit graph. */
319 99db9666 2018-05-07 stsp free(entry);
320 99db9666 2018-05-07 stsp }
321 99db9666 2018-05-07 stsp
322 99db9666 2018-05-07 stsp static void
323 99db9666 2018-05-07 stsp free_commits(struct commit_queue *commits)
324 99db9666 2018-05-07 stsp {
325 99db9666 2018-05-07 stsp while (!TAILQ_EMPTY(commits))
326 99db9666 2018-05-07 stsp pop_commit(commits);
327 c4972b91 2018-05-07 stsp }
328 c4972b91 2018-05-07 stsp
329 c4972b91 2018-05-07 stsp static const struct got_error *
330 9ba79e04 2018-06-11 stsp queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
331 9ba79e04 2018-06-11 stsp struct got_object_id *start_id, struct got_repository *repo)
332 c4972b91 2018-05-07 stsp {
333 899d86c2 2018-05-10 stsp const struct got_error *err = NULL;
334 899d86c2 2018-05-10 stsp struct got_object_id *id;
335 9ba79e04 2018-06-11 stsp struct commit_queue_entry *entry;
336 c4972b91 2018-05-07 stsp
337 9ba79e04 2018-06-11 stsp err = got_commit_graph_iter_start(graph, start_id);
338 c4972b91 2018-05-07 stsp if (err)
339 c4972b91 2018-05-07 stsp return err;
340 c4972b91 2018-05-07 stsp
341 9ba79e04 2018-06-11 stsp entry = TAILQ_LAST(commits, commit_queue);
342 9ba79e04 2018-06-11 stsp if (entry && got_object_id_cmp(entry->id, start_id) == 0) {
343 9ba79e04 2018-06-11 stsp int nfetched;
344 899d86c2 2018-05-10 stsp
345 9ba79e04 2018-06-11 stsp /* Start ID's commit is already on the queue; skip over it. */
346 9ba79e04 2018-06-11 stsp err = got_commit_graph_iter_next(&id, graph);
347 9ba79e04 2018-06-11 stsp if (err && err->code != GOT_ERR_ITER_NEED_MORE)
348 9ba79e04 2018-06-11 stsp return err;
349 9ba79e04 2018-06-11 stsp
350 9ba79e04 2018-06-11 stsp err = got_commit_graph_fetch_commits(&nfetched, graph, 1, repo);
351 9ba79e04 2018-06-11 stsp if (err)
352 9ba79e04 2018-06-11 stsp return err;
353 c4972b91 2018-05-07 stsp }
354 c4972b91 2018-05-07 stsp
355 9ba79e04 2018-06-11 stsp while (1) {
356 9ba79e04 2018-06-11 stsp struct got_commit_object *commit;
357 899d86c2 2018-05-10 stsp
358 9ba79e04 2018-06-11 stsp err = got_commit_graph_iter_next(&id, graph);
359 9ba79e04 2018-06-11 stsp if (err) {
360 9ba79e04 2018-06-11 stsp if (err->code == GOT_ERR_ITER_NEED_MORE)
361 9ba79e04 2018-06-11 stsp err = NULL;
362 9ba79e04 2018-06-11 stsp break;
363 9ba79e04 2018-06-11 stsp }
364 899d86c2 2018-05-10 stsp
365 9ba79e04 2018-06-11 stsp err = got_object_open_as_commit(&commit, repo, id);
366 9ba79e04 2018-06-11 stsp if (err)
367 9ba79e04 2018-06-11 stsp break;
368 899d86c2 2018-05-10 stsp
369 9ba79e04 2018-06-11 stsp entry = alloc_commit_queue_entry(commit, id);
370 9ba79e04 2018-06-11 stsp if (entry == NULL) {
371 9ba79e04 2018-06-11 stsp err = got_error_from_errno();
372 9ba79e04 2018-06-11 stsp break;
373 9ba79e04 2018-06-11 stsp }
374 899d86c2 2018-05-10 stsp
375 9ba79e04 2018-06-11 stsp TAILQ_INSERT_TAIL(commits, entry, entry);
376 899d86c2 2018-05-10 stsp }
377 899d86c2 2018-05-10 stsp
378 9ba79e04 2018-06-11 stsp return err;
379 99db9666 2018-05-07 stsp }
380 99db9666 2018-05-07 stsp
381 99db9666 2018-05-07 stsp static const struct got_error *
382 9ba79e04 2018-06-11 stsp fetch_next_commit(struct commit_queue_entry **pentry,
383 9ba79e04 2018-06-11 stsp struct commit_queue_entry *entry, struct commit_queue *commits,
384 9ba79e04 2018-06-11 stsp struct got_commit_graph *graph, struct got_repository *repo)
385 899d86c2 2018-05-10 stsp {
386 899d86c2 2018-05-10 stsp const struct got_error *err = NULL;
387 9ba79e04 2018-06-11 stsp struct got_object_qid *qid;
388 899d86c2 2018-05-10 stsp
389 9ba79e04 2018-06-11 stsp *pentry = NULL;
390 899d86c2 2018-05-10 stsp
391 9ba79e04 2018-06-11 stsp /* Populate commit graph with entry's parent commits. */
392 9ba79e04 2018-06-11 stsp SIMPLEQ_FOREACH(qid, &entry->commit->parent_ids, entry) {
393 9ba79e04 2018-06-11 stsp int nfetched;
394 9ba79e04 2018-06-11 stsp err = got_commit_graph_fetch_commits_up_to(&nfetched,
395 9ba79e04 2018-06-11 stsp graph, qid->id, repo);
396 9ba79e04 2018-06-11 stsp if (err)
397 9ba79e04 2018-06-11 stsp return err;
398 899d86c2 2018-05-10 stsp }
399 899d86c2 2018-05-10 stsp
400 9ba79e04 2018-06-11 stsp /* Append outstanding commits to queue in graph sort order. */
401 9ba79e04 2018-06-11 stsp err = queue_commits(graph, commits, entry->id, repo);
402 9ba79e04 2018-06-11 stsp if (err) {
403 9ba79e04 2018-06-11 stsp if (err->code == GOT_ERR_ITER_COMPLETED)
404 9ba79e04 2018-06-11 stsp err = NULL;
405 9ba79e04 2018-06-11 stsp return err;
406 899d86c2 2018-05-10 stsp }
407 899d86c2 2018-05-10 stsp
408 9ba79e04 2018-06-11 stsp /* Next entry to display should now be available. */
409 9ba79e04 2018-06-11 stsp *pentry = TAILQ_NEXT(entry, entry);
410 9ba79e04 2018-06-11 stsp if (*pentry == NULL)
411 9ba79e04 2018-06-11 stsp return got_error(GOT_ERR_NO_OBJ);
412 899d86c2 2018-05-10 stsp
413 9ba79e04 2018-06-11 stsp return NULL;
414 899d86c2 2018-05-10 stsp }
415 899d86c2 2018-05-10 stsp
416 899d86c2 2018-05-10 stsp static const struct got_error *
417 9ba79e04 2018-06-11 stsp get_head_commit_id(struct got_object_id **head_id, struct got_repository *repo)
418 99db9666 2018-05-07 stsp {
419 9ba79e04 2018-06-11 stsp const struct got_error *err = NULL;
420 9ba79e04 2018-06-11 stsp struct got_reference *head_ref;
421 99db9666 2018-05-07 stsp
422 9ba79e04 2018-06-11 stsp *head_id = NULL;
423 899d86c2 2018-05-10 stsp
424 9ba79e04 2018-06-11 stsp err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
425 99db9666 2018-05-07 stsp if (err)
426 99db9666 2018-05-07 stsp return err;
427 99db9666 2018-05-07 stsp
428 9ba79e04 2018-06-11 stsp err = got_ref_resolve(head_id, repo, head_ref);
429 9ba79e04 2018-06-11 stsp got_ref_close(head_ref);
430 9ba79e04 2018-06-11 stsp if (err) {
431 9ba79e04 2018-06-11 stsp *head_id = NULL;
432 99db9666 2018-05-07 stsp return err;
433 0553a4e3 2018-04-30 stsp }
434 80ddbec8 2018-04-29 stsp
435 9ba79e04 2018-06-11 stsp return NULL;
436 0553a4e3 2018-04-30 stsp }
437 0553a4e3 2018-04-30 stsp
438 0553a4e3 2018-04-30 stsp static const struct got_error *
439 cd0acaa7 2018-05-20 stsp draw_commits(struct commit_queue_entry **last, struct commit_queue_entry **selected,
440 cd0acaa7 2018-05-20 stsp struct commit_queue_entry *first, int selected_idx, int limit)
441 0553a4e3 2018-04-30 stsp {
442 0553a4e3 2018-04-30 stsp const struct got_error *err = NULL;
443 0553a4e3 2018-04-30 stsp struct commit_queue_entry *entry;
444 0553a4e3 2018-04-30 stsp int ncommits = 0;
445 0553a4e3 2018-04-30 stsp
446 9c2de6ef 2018-05-20 stsp werase(tog_log_view.window);
447 0553a4e3 2018-04-30 stsp
448 899d86c2 2018-05-10 stsp entry = first;
449 899d86c2 2018-05-10 stsp *last = first;
450 899d86c2 2018-05-10 stsp while (entry) {
451 899d86c2 2018-05-10 stsp if (ncommits == limit)
452 899d86c2 2018-05-10 stsp break;
453 cd0acaa7 2018-05-20 stsp if (ncommits == selected_idx) {
454 0553a4e3 2018-04-30 stsp wstandout(tog_log_view.window);
455 cd0acaa7 2018-05-20 stsp *selected = entry;
456 cd0acaa7 2018-05-20 stsp }
457 0553a4e3 2018-04-30 stsp err = draw_commit(entry->commit, entry->id);
458 cd0acaa7 2018-05-20 stsp if (ncommits == selected_idx)
459 0553a4e3 2018-04-30 stsp wstandend(tog_log_view.window);
460 0553a4e3 2018-04-30 stsp if (err)
461 0553a4e3 2018-04-30 stsp break;
462 0553a4e3 2018-04-30 stsp ncommits++;
463 899d86c2 2018-05-10 stsp *last = entry;
464 899d86c2 2018-05-10 stsp entry = TAILQ_NEXT(entry, entry);
465 80ddbec8 2018-04-29 stsp }
466 80ddbec8 2018-04-29 stsp
467 80ddbec8 2018-04-29 stsp update_panels();
468 80ddbec8 2018-04-29 stsp doupdate();
469 0553a4e3 2018-04-30 stsp
470 80ddbec8 2018-04-29 stsp return err;
471 9f7d7167 2018-04-29 stsp }
472 07b55e75 2018-05-10 stsp
473 07b55e75 2018-05-10 stsp static void
474 16482c3b 2018-05-20 stsp scroll_up(struct commit_queue_entry **first_displayed_entry, int maxscroll,
475 07b55e75 2018-05-10 stsp struct commit_queue *commits)
476 07b55e75 2018-05-10 stsp {
477 07b55e75 2018-05-10 stsp struct commit_queue_entry *entry;
478 07b55e75 2018-05-10 stsp int nscrolled = 0;
479 07b55e75 2018-05-10 stsp
480 07b55e75 2018-05-10 stsp entry = TAILQ_FIRST(commits);
481 07b55e75 2018-05-10 stsp if (*first_displayed_entry == entry)
482 07b55e75 2018-05-10 stsp return;
483 9f7d7167 2018-04-29 stsp
484 07b55e75 2018-05-10 stsp entry = *first_displayed_entry;
485 16482c3b 2018-05-20 stsp while (entry && nscrolled < maxscroll) {
486 07b55e75 2018-05-10 stsp entry = TAILQ_PREV(entry, commit_queue, entry);
487 07b55e75 2018-05-10 stsp if (entry) {
488 07b55e75 2018-05-10 stsp *first_displayed_entry = entry;
489 07b55e75 2018-05-10 stsp nscrolled++;
490 07b55e75 2018-05-10 stsp }
491 07b55e75 2018-05-10 stsp }
492 aa075928 2018-05-10 stsp }
493 aa075928 2018-05-10 stsp
494 aa075928 2018-05-10 stsp static const struct got_error *
495 16482c3b 2018-05-20 stsp scroll_down(struct commit_queue_entry **first_displayed_entry, int maxscroll,
496 aa075928 2018-05-10 stsp struct commit_queue_entry *last_displayed_entry,
497 9ba79e04 2018-06-11 stsp struct commit_queue *commits, struct got_commit_graph *graph,
498 9ba79e04 2018-06-11 stsp struct got_repository *repo)
499 aa075928 2018-05-10 stsp {
500 aa075928 2018-05-10 stsp const struct got_error *err = NULL;
501 dd0a52c1 2018-05-20 stsp struct commit_queue_entry *pentry;
502 aa075928 2018-05-10 stsp int nscrolled = 0;
503 aa075928 2018-05-10 stsp
504 aa075928 2018-05-10 stsp do {
505 dd0a52c1 2018-05-20 stsp pentry = TAILQ_NEXT(last_displayed_entry, entry);
506 aa075928 2018-05-10 stsp if (pentry == NULL) {
507 9ba79e04 2018-06-11 stsp err = fetch_next_commit(&pentry, last_displayed_entry,
508 9ba79e04 2018-06-11 stsp commits, graph, repo);
509 9a6bf2a5 2018-05-20 stsp if (err || pentry == NULL)
510 aa075928 2018-05-10 stsp break;
511 aa075928 2018-05-10 stsp }
512 dd0a52c1 2018-05-20 stsp last_displayed_entry = pentry;
513 aa075928 2018-05-10 stsp
514 dd0a52c1 2018-05-20 stsp pentry = TAILQ_NEXT(*first_displayed_entry, entry);
515 dd0a52c1 2018-05-20 stsp if (pentry == NULL)
516 dd0a52c1 2018-05-20 stsp break;
517 aa075928 2018-05-10 stsp *first_displayed_entry = pentry;
518 16482c3b 2018-05-20 stsp } while (++nscrolled < maxscroll);
519 aa075928 2018-05-10 stsp
520 dd0a52c1 2018-05-20 stsp return err;
521 07b55e75 2018-05-10 stsp }
522 4a7f7875 2018-05-10 stsp
523 4a7f7875 2018-05-10 stsp static int
524 4a7f7875 2018-05-10 stsp num_parents(struct commit_queue_entry *entry)
525 4a7f7875 2018-05-10 stsp {
526 4a7f7875 2018-05-10 stsp int nparents = 0;
527 07b55e75 2018-05-10 stsp
528 4a7f7875 2018-05-10 stsp while (entry) {
529 4a7f7875 2018-05-10 stsp entry = TAILQ_NEXT(entry, entry);
530 4a7f7875 2018-05-10 stsp nparents++;
531 4a7f7875 2018-05-10 stsp }
532 4a7f7875 2018-05-10 stsp
533 4a7f7875 2018-05-10 stsp return nparents;
534 cd0acaa7 2018-05-20 stsp }
535 cd0acaa7 2018-05-20 stsp
536 cd0acaa7 2018-05-20 stsp static const struct got_error *
537 cd0acaa7 2018-05-20 stsp show_commit(struct commit_queue_entry *entry, struct got_repository *repo)
538 cd0acaa7 2018-05-20 stsp {
539 cd0acaa7 2018-05-20 stsp const struct got_error *err;
540 cd0acaa7 2018-05-20 stsp struct got_object *obj1 = NULL, *obj2 = NULL;
541 9ba79e04 2018-06-11 stsp struct got_object_qid *parent_id;
542 cd0acaa7 2018-05-20 stsp
543 cd0acaa7 2018-05-20 stsp err = got_object_open(&obj2, repo, entry->id);
544 cd0acaa7 2018-05-20 stsp if (err)
545 cd0acaa7 2018-05-20 stsp return err;
546 cd0acaa7 2018-05-20 stsp
547 9ba79e04 2018-06-11 stsp parent_id = SIMPLEQ_FIRST(&entry->commit->parent_ids);
548 9ba79e04 2018-06-11 stsp if (parent_id) {
549 9ba79e04 2018-06-11 stsp err = got_object_open(&obj1, repo, parent_id->id);
550 cd0acaa7 2018-05-20 stsp if (err)
551 cd0acaa7 2018-05-20 stsp goto done;
552 cd0acaa7 2018-05-20 stsp }
553 cd0acaa7 2018-05-20 stsp
554 cd0acaa7 2018-05-20 stsp err = show_diff_view(obj1, obj2, repo);
555 cd0acaa7 2018-05-20 stsp done:
556 cd0acaa7 2018-05-20 stsp if (obj1)
557 cd0acaa7 2018-05-20 stsp got_object_close(obj1);
558 cd0acaa7 2018-05-20 stsp if (obj2)
559 cd0acaa7 2018-05-20 stsp got_object_close(obj2);
560 cd0acaa7 2018-05-20 stsp return err;
561 4a7f7875 2018-05-10 stsp }
562 4a7f7875 2018-05-10 stsp
563 80ddbec8 2018-04-29 stsp static const struct got_error *
564 80ddbec8 2018-04-29 stsp show_log_view(struct got_object_id *start_id, struct got_repository *repo)
565 80ddbec8 2018-04-29 stsp {
566 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
567 9ba79e04 2018-06-11 stsp struct got_object_id *head_id = NULL;
568 9ba79e04 2018-06-11 stsp int ch, done = 0, selected = 0, nparents, nfetched;
569 9ba79e04 2018-06-11 stsp struct got_commit_graph *graph;
570 0553a4e3 2018-04-30 stsp struct commit_queue commits;
571 9ba79e04 2018-06-11 stsp struct commit_queue_entry *entry = NULL;
572 899d86c2 2018-05-10 stsp struct commit_queue_entry *first_displayed_entry = NULL;
573 899d86c2 2018-05-10 stsp struct commit_queue_entry *last_displayed_entry = NULL;
574 cd0acaa7 2018-05-20 stsp struct commit_queue_entry *selected_entry = NULL;
575 80ddbec8 2018-04-29 stsp
576 80ddbec8 2018-04-29 stsp if (tog_log_view.window == NULL) {
577 80ddbec8 2018-04-29 stsp tog_log_view.window = newwin(0, 0, 0, 0);
578 80ddbec8 2018-04-29 stsp if (tog_log_view.window == NULL)
579 80ddbec8 2018-04-29 stsp return got_error_from_errno();
580 00a838fa 2018-04-29 stsp keypad(tog_log_view.window, TRUE);
581 80ddbec8 2018-04-29 stsp }
582 80ddbec8 2018-04-29 stsp if (tog_log_view.panel == NULL) {
583 80ddbec8 2018-04-29 stsp tog_log_view.panel = new_panel(tog_log_view.window);
584 80ddbec8 2018-04-29 stsp if (tog_log_view.panel == NULL)
585 80ddbec8 2018-04-29 stsp return got_error_from_errno();
586 cd0acaa7 2018-05-20 stsp } else
587 cd0acaa7 2018-05-20 stsp show_panel(tog_log_view.panel);
588 80ddbec8 2018-04-29 stsp
589 9ba79e04 2018-06-11 stsp err = get_head_commit_id(&head_id, repo);
590 9ba79e04 2018-06-11 stsp if (err)
591 9ba79e04 2018-06-11 stsp return err;
592 9ba79e04 2018-06-11 stsp
593 899d86c2 2018-05-10 stsp TAILQ_INIT(&commits);
594 9ba79e04 2018-06-11 stsp
595 5489743f 2018-06-13 stsp err = got_commit_graph_open(&graph, head_id, 0, repo);
596 9ba79e04 2018-06-11 stsp if (err)
597 9ba79e04 2018-06-11 stsp goto done;
598 9ba79e04 2018-06-11 stsp
599 9ba79e04 2018-06-11 stsp /* Populate commit graph with a sufficient number of commits. */
600 9ba79e04 2018-06-11 stsp err = got_commit_graph_fetch_commits_up_to(&nfetched, graph, start_id,
601 9ba79e04 2018-06-11 stsp repo);
602 9ba79e04 2018-06-11 stsp if (err)
603 9ba79e04 2018-06-11 stsp goto done;
604 9ba79e04 2018-06-11 stsp err = got_commit_graph_fetch_commits(&nfetched, graph, LINES, repo);
605 80ddbec8 2018-04-29 stsp if (err)
606 9ba79e04 2018-06-11 stsp goto done;
607 9ba79e04 2018-06-11 stsp
608 9ba79e04 2018-06-11 stsp /*
609 9ba79e04 2018-06-11 stsp * Open the initial batch of commits, sorted in commit graph order.
610 9ba79e04 2018-06-11 stsp * We keep all commits open throughout the lifetime of the log view
611 9ba79e04 2018-06-11 stsp * in order to avoid having to re-fetch commits from disk while
612 9ba79e04 2018-06-11 stsp * updating the display.
613 9ba79e04 2018-06-11 stsp */
614 9ba79e04 2018-06-11 stsp err = queue_commits(graph, &commits, head_id, repo);
615 9ba79e04 2018-06-11 stsp if (err && err->code != GOT_ERR_ITER_COMPLETED)
616 9ba79e04 2018-06-11 stsp goto done;
617 9ba79e04 2018-06-11 stsp
618 9ba79e04 2018-06-11 stsp /* Find entry corresponding to the first commit to display. */
619 9ba79e04 2018-06-11 stsp TAILQ_FOREACH(entry, &commits, entry) {
620 9ba79e04 2018-06-11 stsp if (got_object_id_cmp(entry->id, start_id) == 0) {
621 9ba79e04 2018-06-11 stsp first_displayed_entry = entry;
622 9ba79e04 2018-06-11 stsp break;
623 9ba79e04 2018-06-11 stsp }
624 9ba79e04 2018-06-11 stsp }
625 9ba79e04 2018-06-11 stsp if (first_displayed_entry == NULL) {
626 9ba79e04 2018-06-11 stsp err = got_error(GOT_ERR_NO_OBJ);
627 80ddbec8 2018-04-29 stsp goto done;
628 9ba79e04 2018-06-11 stsp }
629 9ba79e04 2018-06-11 stsp
630 899d86c2 2018-05-10 stsp while (!done) {
631 cd0acaa7 2018-05-20 stsp err = draw_commits(&last_displayed_entry, &selected_entry,
632 cd0acaa7 2018-05-20 stsp first_displayed_entry, selected, LINES);
633 80ddbec8 2018-04-29 stsp if (err)
634 d0f709cb 2018-04-30 stsp goto done;
635 80ddbec8 2018-04-29 stsp
636 80ddbec8 2018-04-29 stsp nodelay(stdscr, FALSE);
637 80ddbec8 2018-04-29 stsp ch = wgetch(tog_log_view.window);
638 f7182337 2018-05-20 stsp nodelay(stdscr, TRUE);
639 80ddbec8 2018-04-29 stsp switch (ch) {
640 31120ada 2018-04-30 stsp case ERR:
641 31120ada 2018-04-30 stsp if (errno) {
642 31120ada 2018-04-30 stsp err = got_error_from_errno();
643 31120ada 2018-04-30 stsp goto done;
644 31120ada 2018-04-30 stsp }
645 31120ada 2018-04-30 stsp break;
646 80ddbec8 2018-04-29 stsp case 'q':
647 80ddbec8 2018-04-29 stsp done = 1;
648 80ddbec8 2018-04-29 stsp break;
649 80ddbec8 2018-04-29 stsp case 'k':
650 80ddbec8 2018-04-29 stsp case KEY_UP:
651 80ddbec8 2018-04-29 stsp if (selected > 0)
652 80ddbec8 2018-04-29 stsp selected--;
653 8ec9698d 2018-05-10 stsp if (selected > 0)
654 d91e25cb 2018-05-10 stsp break;
655 07b55e75 2018-05-10 stsp scroll_up(&first_displayed_entry, 1, &commits);
656 80ddbec8 2018-04-29 stsp break;
657 48531068 2018-05-10 stsp case KEY_PPAGE:
658 dfc1d240 2018-05-10 stsp if (TAILQ_FIRST(&commits) ==
659 dfc1d240 2018-05-10 stsp first_displayed_entry) {
660 dfc1d240 2018-05-10 stsp selected = 0;
661 dfc1d240 2018-05-10 stsp break;
662 dfc1d240 2018-05-10 stsp }
663 48531068 2018-05-10 stsp scroll_up(&first_displayed_entry, LINES,
664 48531068 2018-05-10 stsp &commits);
665 48531068 2018-05-10 stsp break;
666 80ddbec8 2018-04-29 stsp case 'j':
667 80ddbec8 2018-04-29 stsp case KEY_DOWN:
668 80ee4603 2018-05-10 stsp nparents = num_parents(first_displayed_entry);
669 06abe2cd 2018-05-10 stsp if (selected < LINES - 1 &&
670 15c91275 2018-05-20 stsp selected < nparents - 1) {
671 15c91275 2018-05-20 stsp selected++;
672 15c91275 2018-05-20 stsp break;
673 8df4052c 2018-05-20 stsp }
674 15c91275 2018-05-20 stsp err = scroll_down(&first_displayed_entry, 1,
675 9ba79e04 2018-06-11 stsp last_displayed_entry, &commits, graph,
676 9ba79e04 2018-06-11 stsp repo);
677 15c91275 2018-05-20 stsp if (err)
678 15c91275 2018-05-20 stsp goto done;
679 4a7f7875 2018-05-10 stsp break;
680 4a7f7875 2018-05-10 stsp case KEY_NPAGE:
681 e50ee4f1 2018-05-10 stsp err = scroll_down(&first_displayed_entry, LINES,
682 9ba79e04 2018-06-11 stsp last_displayed_entry, &commits, graph,
683 9ba79e04 2018-06-11 stsp repo);
684 899d86c2 2018-05-10 stsp if (err)
685 aa075928 2018-05-10 stsp goto done;
686 dd0a52c1 2018-05-20 stsp if (last_displayed_entry->commit->nparents > 0)
687 dd0a52c1 2018-05-20 stsp break;
688 dd0a52c1 2018-05-20 stsp /* can't scroll any further; move cursor down */
689 4a7f7875 2018-05-10 stsp nparents = num_parents(first_displayed_entry);
690 dd0a52c1 2018-05-20 stsp if (selected < LINES - 1 ||
691 dd0a52c1 2018-05-20 stsp selected < nparents - 1)
692 dd0a52c1 2018-05-20 stsp selected = MIN(LINES - 1, nparents - 1);
693 80ddbec8 2018-04-29 stsp break;
694 d6df9be4 2018-04-30 stsp case KEY_RESIZE:
695 31120ada 2018-04-30 stsp if (selected > LINES)
696 31120ada 2018-04-30 stsp selected = LINES - 1;
697 cd0acaa7 2018-05-20 stsp break;
698 cd0acaa7 2018-05-20 stsp case KEY_ENTER:
699 cd0acaa7 2018-05-20 stsp case '\r':
700 cd0acaa7 2018-05-20 stsp err = show_commit(selected_entry, repo);
701 cd0acaa7 2018-05-20 stsp if (err)
702 cd0acaa7 2018-05-20 stsp break;
703 cd0acaa7 2018-05-20 stsp show_panel(tog_log_view.panel);
704 31120ada 2018-04-30 stsp break;
705 80ddbec8 2018-04-29 stsp default:
706 80ddbec8 2018-04-29 stsp break;
707 80ddbec8 2018-04-29 stsp }
708 899d86c2 2018-05-10 stsp }
709 80ddbec8 2018-04-29 stsp done:
710 9ba79e04 2018-06-11 stsp free(head_id);
711 9ba79e04 2018-06-11 stsp if (graph)
712 9ba79e04 2018-06-11 stsp got_commit_graph_close(graph);
713 0553a4e3 2018-04-30 stsp free_commits(&commits);
714 80ddbec8 2018-04-29 stsp return err;
715 80ddbec8 2018-04-29 stsp }
716 80ddbec8 2018-04-29 stsp
717 4ed7e80c 2018-05-20 stsp static const struct got_error *
718 9f7d7167 2018-04-29 stsp cmd_log(int argc, char *argv[])
719 9f7d7167 2018-04-29 stsp {
720 80ddbec8 2018-04-29 stsp const struct got_error *error;
721 80ddbec8 2018-04-29 stsp struct got_repository *repo;
722 899d86c2 2018-05-10 stsp struct got_object_id *start_id = NULL;
723 80ddbec8 2018-04-29 stsp char *repo_path = NULL;
724 80ddbec8 2018-04-29 stsp char *start_commit = NULL;
725 80ddbec8 2018-04-29 stsp int ch;
726 80ddbec8 2018-04-29 stsp
727 80ddbec8 2018-04-29 stsp #ifndef PROFILE
728 80ddbec8 2018-04-29 stsp if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
729 80ddbec8 2018-04-29 stsp err(1, "pledge");
730 80ddbec8 2018-04-29 stsp #endif
731 80ddbec8 2018-04-29 stsp
732 80ddbec8 2018-04-29 stsp while ((ch = getopt(argc, argv, "c:")) != -1) {
733 80ddbec8 2018-04-29 stsp switch (ch) {
734 80ddbec8 2018-04-29 stsp case 'c':
735 80ddbec8 2018-04-29 stsp start_commit = optarg;
736 80ddbec8 2018-04-29 stsp break;
737 80ddbec8 2018-04-29 stsp default:
738 80ddbec8 2018-04-29 stsp usage();
739 80ddbec8 2018-04-29 stsp /* NOTREACHED */
740 80ddbec8 2018-04-29 stsp }
741 80ddbec8 2018-04-29 stsp }
742 80ddbec8 2018-04-29 stsp
743 80ddbec8 2018-04-29 stsp argc -= optind;
744 80ddbec8 2018-04-29 stsp argv += optind;
745 80ddbec8 2018-04-29 stsp
746 80ddbec8 2018-04-29 stsp if (argc == 0) {
747 80ddbec8 2018-04-29 stsp repo_path = getcwd(NULL, 0);
748 80ddbec8 2018-04-29 stsp if (repo_path == NULL)
749 80ddbec8 2018-04-29 stsp return got_error_from_errno();
750 80ddbec8 2018-04-29 stsp } else if (argc == 1) {
751 80ddbec8 2018-04-29 stsp repo_path = realpath(argv[0], NULL);
752 80ddbec8 2018-04-29 stsp if (repo_path == NULL)
753 80ddbec8 2018-04-29 stsp return got_error_from_errno();
754 80ddbec8 2018-04-29 stsp } else
755 80ddbec8 2018-04-29 stsp usage_log();
756 80ddbec8 2018-04-29 stsp
757 80ddbec8 2018-04-29 stsp error = got_repo_open(&repo, repo_path);
758 80ddbec8 2018-04-29 stsp free(repo_path);
759 80ddbec8 2018-04-29 stsp if (error != NULL)
760 80ddbec8 2018-04-29 stsp return error;
761 80ddbec8 2018-04-29 stsp
762 80ddbec8 2018-04-29 stsp if (start_commit == NULL) {
763 899d86c2 2018-05-10 stsp error = get_head_commit_id(&start_id, repo);
764 80ddbec8 2018-04-29 stsp if (error != NULL)
765 80ddbec8 2018-04-29 stsp return error;
766 80ddbec8 2018-04-29 stsp } else {
767 80ddbec8 2018-04-29 stsp struct got_object *obj;
768 80ddbec8 2018-04-29 stsp error = got_object_open_by_id_str(&obj, repo, start_commit);
769 80ddbec8 2018-04-29 stsp if (error == NULL) {
770 899d86c2 2018-05-10 stsp start_id = got_object_get_id(obj);
771 899d86c2 2018-05-10 stsp if (start_id == NULL)
772 80ddbec8 2018-04-29 stsp error = got_error_from_errno();
773 80ddbec8 2018-04-29 stsp }
774 80ddbec8 2018-04-29 stsp }
775 80ddbec8 2018-04-29 stsp if (error != NULL)
776 80ddbec8 2018-04-29 stsp return error;
777 899d86c2 2018-05-10 stsp error = show_log_view(start_id, repo);
778 899d86c2 2018-05-10 stsp free(start_id);
779 80ddbec8 2018-04-29 stsp got_repo_close(repo);
780 80ddbec8 2018-04-29 stsp return error;
781 9f7d7167 2018-04-29 stsp }
782 9f7d7167 2018-04-29 stsp
783 4ed7e80c 2018-05-20 stsp __dead static void
784 9f7d7167 2018-04-29 stsp usage_diff(void)
785 9f7d7167 2018-04-29 stsp {
786 80ddbec8 2018-04-29 stsp endwin();
787 9f7d7167 2018-04-29 stsp fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
788 9f7d7167 2018-04-29 stsp getprogname());
789 9f7d7167 2018-04-29 stsp exit(1);
790 b304db33 2018-05-20 stsp }
791 b304db33 2018-05-20 stsp
792 b304db33 2018-05-20 stsp static char *
793 b304db33 2018-05-20 stsp parse_next_line(FILE *f, size_t *len)
794 b304db33 2018-05-20 stsp {
795 b304db33 2018-05-20 stsp char *line;
796 b304db33 2018-05-20 stsp size_t linelen;
797 b304db33 2018-05-20 stsp size_t lineno;
798 b304db33 2018-05-20 stsp const char delim[3] = { '\0', '\0', '\0'};
799 b304db33 2018-05-20 stsp
800 b304db33 2018-05-20 stsp line = fparseln(f, &linelen, &lineno, delim, 0);
801 b304db33 2018-05-20 stsp if (len)
802 b304db33 2018-05-20 stsp *len = linelen;
803 b304db33 2018-05-20 stsp return line;
804 26ed57b2 2018-05-19 stsp }
805 26ed57b2 2018-05-19 stsp
806 4ed7e80c 2018-05-20 stsp static const struct got_error *
807 a70480e0 2018-06-23 stsp draw_file(WINDOW *window, FILE *f, int *first_displayed_line,
808 a70480e0 2018-06-23 stsp int *last_displayed_line, int *eof, int max_lines)
809 26ed57b2 2018-05-19 stsp {
810 61e69b96 2018-05-20 stsp const struct got_error *err;
811 26ed57b2 2018-05-19 stsp int nlines = 0, nprinted = 0;
812 b304db33 2018-05-20 stsp char *line;
813 b304db33 2018-05-20 stsp size_t len;
814 61e69b96 2018-05-20 stsp wchar_t *wline;
815 e0b650dd 2018-05-20 stsp int width;
816 26ed57b2 2018-05-19 stsp
817 26ed57b2 2018-05-19 stsp rewind(f);
818 a70480e0 2018-06-23 stsp werase(window);
819 26ed57b2 2018-05-19 stsp
820 26ed57b2 2018-05-19 stsp *eof = 0;
821 26ed57b2 2018-05-19 stsp while (nprinted < max_lines) {
822 b304db33 2018-05-20 stsp line = parse_next_line(f, &len);
823 26ed57b2 2018-05-19 stsp if (line == NULL) {
824 26ed57b2 2018-05-19 stsp *eof = 1;
825 26ed57b2 2018-05-19 stsp break;
826 26ed57b2 2018-05-19 stsp }
827 26ed57b2 2018-05-19 stsp if (++nlines < *first_displayed_line) {
828 26ed57b2 2018-05-19 stsp free(line);
829 26ed57b2 2018-05-19 stsp continue;
830 26ed57b2 2018-05-19 stsp }
831 26ed57b2 2018-05-19 stsp
832 e0b650dd 2018-05-20 stsp err = format_line(&wline, &width, line, COLS);
833 61e69b96 2018-05-20 stsp if (err) {
834 61e69b96 2018-05-20 stsp free(line);
835 61e69b96 2018-05-20 stsp return err;
836 61e69b96 2018-05-20 stsp }
837 a70480e0 2018-06-23 stsp waddwstr(window, wline);
838 e0b650dd 2018-05-20 stsp if (width < COLS)
839 a70480e0 2018-06-23 stsp waddch(window, '\n');
840 26ed57b2 2018-05-19 stsp if (++nprinted == 1)
841 26ed57b2 2018-05-19 stsp *first_displayed_line = nlines;
842 26ed57b2 2018-05-19 stsp free(line);
843 26ed57b2 2018-05-19 stsp }
844 26ed57b2 2018-05-19 stsp *last_displayed_line = nlines;
845 26ed57b2 2018-05-19 stsp
846 26ed57b2 2018-05-19 stsp update_panels();
847 26ed57b2 2018-05-19 stsp doupdate();
848 26ed57b2 2018-05-19 stsp
849 26ed57b2 2018-05-19 stsp return NULL;
850 9f7d7167 2018-04-29 stsp }
851 9f7d7167 2018-04-29 stsp
852 4ed7e80c 2018-05-20 stsp static const struct got_error *
853 26ed57b2 2018-05-19 stsp show_diff_view(struct got_object *obj1, struct got_object *obj2,
854 26ed57b2 2018-05-19 stsp struct got_repository *repo)
855 26ed57b2 2018-05-19 stsp {
856 26ed57b2 2018-05-19 stsp const struct got_error *err;
857 26ed57b2 2018-05-19 stsp FILE *f;
858 26ed57b2 2018-05-19 stsp int ch, done = 0, first_displayed_line = 1, last_displayed_line = LINES;
859 b304db33 2018-05-20 stsp int eof, i;
860 26ed57b2 2018-05-19 stsp
861 cd0acaa7 2018-05-20 stsp if (obj1 != NULL && obj2 != NULL &&
862 cd0acaa7 2018-05-20 stsp got_object_get_type(obj1) != got_object_get_type(obj2))
863 26ed57b2 2018-05-19 stsp return got_error(GOT_ERR_OBJ_TYPE);
864 26ed57b2 2018-05-19 stsp
865 511a516b 2018-05-19 stsp f = got_opentemp();
866 26ed57b2 2018-05-19 stsp if (f == NULL)
867 26ed57b2 2018-05-19 stsp return got_error_from_errno();
868 26ed57b2 2018-05-19 stsp
869 cd0acaa7 2018-05-20 stsp switch (got_object_get_type(obj1 ? obj1 : obj2)) {
870 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_BLOB:
871 11528a82 2018-05-19 stsp err = got_diff_objects_as_blobs(obj1, obj2, repo, f);
872 26ed57b2 2018-05-19 stsp break;
873 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_TREE:
874 11528a82 2018-05-19 stsp err = got_diff_objects_as_trees(obj1, obj2, repo, f);
875 26ed57b2 2018-05-19 stsp break;
876 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_COMMIT:
877 11528a82 2018-05-19 stsp err = got_diff_objects_as_commits(obj1, obj2, repo, f);
878 26ed57b2 2018-05-19 stsp break;
879 26ed57b2 2018-05-19 stsp default:
880 26ed57b2 2018-05-19 stsp return got_error(GOT_ERR_OBJ_TYPE);
881 26ed57b2 2018-05-19 stsp }
882 26ed57b2 2018-05-19 stsp
883 26ed57b2 2018-05-19 stsp fflush(f);
884 26ed57b2 2018-05-19 stsp
885 26ed57b2 2018-05-19 stsp if (tog_diff_view.window == NULL) {
886 26ed57b2 2018-05-19 stsp tog_diff_view.window = newwin(0, 0, 0, 0);
887 26ed57b2 2018-05-19 stsp if (tog_diff_view.window == NULL)
888 26ed57b2 2018-05-19 stsp return got_error_from_errno();
889 26ed57b2 2018-05-19 stsp keypad(tog_diff_view.window, TRUE);
890 26ed57b2 2018-05-19 stsp }
891 26ed57b2 2018-05-19 stsp if (tog_diff_view.panel == NULL) {
892 26ed57b2 2018-05-19 stsp tog_diff_view.panel = new_panel(tog_diff_view.window);
893 26ed57b2 2018-05-19 stsp if (tog_diff_view.panel == NULL)
894 26ed57b2 2018-05-19 stsp return got_error_from_errno();
895 26ed57b2 2018-05-19 stsp } else
896 26ed57b2 2018-05-19 stsp show_panel(tog_diff_view.panel);
897 26ed57b2 2018-05-19 stsp
898 26ed57b2 2018-05-19 stsp while (!done) {
899 a70480e0 2018-06-23 stsp err = draw_file(tog_diff_view.window, f, &first_displayed_line,
900 a70480e0 2018-06-23 stsp &last_displayed_line, &eof, LINES);
901 26ed57b2 2018-05-19 stsp if (err)
902 26ed57b2 2018-05-19 stsp break;
903 26ed57b2 2018-05-19 stsp nodelay(stdscr, FALSE);
904 26ed57b2 2018-05-19 stsp ch = wgetch(tog_diff_view.window);
905 f7182337 2018-05-20 stsp nodelay(stdscr, TRUE);
906 26ed57b2 2018-05-19 stsp switch (ch) {
907 26ed57b2 2018-05-19 stsp case 'q':
908 26ed57b2 2018-05-19 stsp done = 1;
909 26ed57b2 2018-05-19 stsp break;
910 26ed57b2 2018-05-19 stsp case 'k':
911 26ed57b2 2018-05-19 stsp case KEY_UP:
912 925e6f23 2018-05-20 stsp case KEY_BACKSPACE:
913 26ed57b2 2018-05-19 stsp if (first_displayed_line > 1)
914 b304db33 2018-05-20 stsp first_displayed_line--;
915 b304db33 2018-05-20 stsp break;
916 b304db33 2018-05-20 stsp case KEY_PPAGE:
917 b304db33 2018-05-20 stsp i = 0;
918 d56caa55 2018-05-20 stsp while (i++ < LINES - 1 &&
919 d56caa55 2018-05-20 stsp first_displayed_line > 1)
920 26ed57b2 2018-05-19 stsp first_displayed_line--;
921 26ed57b2 2018-05-19 stsp break;
922 26ed57b2 2018-05-19 stsp case 'j':
923 26ed57b2 2018-05-19 stsp case KEY_DOWN:
924 925e6f23 2018-05-20 stsp case KEY_ENTER:
925 925e6f23 2018-05-20 stsp case '\r':
926 26ed57b2 2018-05-19 stsp if (!eof)
927 26ed57b2 2018-05-19 stsp first_displayed_line++;
928 26ed57b2 2018-05-19 stsp break;
929 b304db33 2018-05-20 stsp case KEY_NPAGE:
930 75e48879 2018-05-20 stsp case ' ':
931 b304db33 2018-05-20 stsp i = 0;
932 b304db33 2018-05-20 stsp while (!eof && i++ < LINES - 1) {
933 b304db33 2018-05-20 stsp char *line = parse_next_line(f, NULL);
934 b304db33 2018-05-20 stsp first_displayed_line++;
935 b304db33 2018-05-20 stsp if (line == NULL)
936 b304db33 2018-05-20 stsp break;
937 b304db33 2018-05-20 stsp }
938 b304db33 2018-05-20 stsp break;
939 26ed57b2 2018-05-19 stsp default:
940 26ed57b2 2018-05-19 stsp break;
941 26ed57b2 2018-05-19 stsp }
942 26ed57b2 2018-05-19 stsp }
943 26ed57b2 2018-05-19 stsp fclose(f);
944 26ed57b2 2018-05-19 stsp return err;
945 26ed57b2 2018-05-19 stsp }
946 26ed57b2 2018-05-19 stsp
947 4ed7e80c 2018-05-20 stsp static const struct got_error *
948 9f7d7167 2018-04-29 stsp cmd_diff(int argc, char *argv[])
949 9f7d7167 2018-04-29 stsp {
950 26ed57b2 2018-05-19 stsp const struct got_error *error = NULL;
951 26ed57b2 2018-05-19 stsp struct got_repository *repo = NULL;
952 26ed57b2 2018-05-19 stsp struct got_object *obj1 = NULL, *obj2 = NULL;
953 26ed57b2 2018-05-19 stsp char *repo_path = NULL;
954 26ed57b2 2018-05-19 stsp char *obj_id_str1 = NULL, *obj_id_str2 = NULL;
955 26ed57b2 2018-05-19 stsp int ch;
956 26ed57b2 2018-05-19 stsp
957 26ed57b2 2018-05-19 stsp #ifndef PROFILE
958 26ed57b2 2018-05-19 stsp if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
959 26ed57b2 2018-05-19 stsp err(1, "pledge");
960 26ed57b2 2018-05-19 stsp #endif
961 26ed57b2 2018-05-19 stsp
962 26ed57b2 2018-05-19 stsp while ((ch = getopt(argc, argv, "")) != -1) {
963 26ed57b2 2018-05-19 stsp switch (ch) {
964 26ed57b2 2018-05-19 stsp default:
965 26ed57b2 2018-05-19 stsp usage();
966 26ed57b2 2018-05-19 stsp /* NOTREACHED */
967 26ed57b2 2018-05-19 stsp }
968 26ed57b2 2018-05-19 stsp }
969 26ed57b2 2018-05-19 stsp
970 26ed57b2 2018-05-19 stsp argc -= optind;
971 26ed57b2 2018-05-19 stsp argv += optind;
972 26ed57b2 2018-05-19 stsp
973 26ed57b2 2018-05-19 stsp if (argc == 0) {
974 26ed57b2 2018-05-19 stsp usage_diff(); /* TODO show local worktree changes */
975 26ed57b2 2018-05-19 stsp } else if (argc == 2) {
976 26ed57b2 2018-05-19 stsp repo_path = getcwd(NULL, 0);
977 26ed57b2 2018-05-19 stsp if (repo_path == NULL)
978 26ed57b2 2018-05-19 stsp return got_error_from_errno();
979 26ed57b2 2018-05-19 stsp obj_id_str1 = argv[0];
980 26ed57b2 2018-05-19 stsp obj_id_str2 = argv[1];
981 26ed57b2 2018-05-19 stsp } else if (argc == 3) {
982 26ed57b2 2018-05-19 stsp repo_path = realpath(argv[0], NULL);
983 26ed57b2 2018-05-19 stsp if (repo_path == NULL)
984 26ed57b2 2018-05-19 stsp return got_error_from_errno();
985 26ed57b2 2018-05-19 stsp obj_id_str1 = argv[1];
986 26ed57b2 2018-05-19 stsp obj_id_str2 = argv[2];
987 26ed57b2 2018-05-19 stsp } else
988 26ed57b2 2018-05-19 stsp usage_diff();
989 26ed57b2 2018-05-19 stsp
990 26ed57b2 2018-05-19 stsp error = got_repo_open(&repo, repo_path);
991 26ed57b2 2018-05-19 stsp free(repo_path);
992 26ed57b2 2018-05-19 stsp if (error)
993 26ed57b2 2018-05-19 stsp goto done;
994 26ed57b2 2018-05-19 stsp
995 26ed57b2 2018-05-19 stsp error = got_object_open_by_id_str(&obj1, repo, obj_id_str1);
996 26ed57b2 2018-05-19 stsp if (error)
997 26ed57b2 2018-05-19 stsp goto done;
998 26ed57b2 2018-05-19 stsp
999 26ed57b2 2018-05-19 stsp error = got_object_open_by_id_str(&obj2, repo, obj_id_str2);
1000 26ed57b2 2018-05-19 stsp if (error)
1001 26ed57b2 2018-05-19 stsp goto done;
1002 26ed57b2 2018-05-19 stsp
1003 26ed57b2 2018-05-19 stsp error = show_diff_view(obj1, obj2, repo);
1004 26ed57b2 2018-05-19 stsp done:
1005 26ed57b2 2018-05-19 stsp got_repo_close(repo);
1006 26ed57b2 2018-05-19 stsp if (obj1)
1007 26ed57b2 2018-05-19 stsp got_object_close(obj1);
1008 26ed57b2 2018-05-19 stsp if (obj2)
1009 26ed57b2 2018-05-19 stsp got_object_close(obj2);
1010 26ed57b2 2018-05-19 stsp return error;
1011 9f7d7167 2018-04-29 stsp }
1012 9f7d7167 2018-04-29 stsp
1013 4ed7e80c 2018-05-20 stsp __dead static void
1014 9f7d7167 2018-04-29 stsp usage_blame(void)
1015 9f7d7167 2018-04-29 stsp {
1016 80ddbec8 2018-04-29 stsp endwin();
1017 a70480e0 2018-06-23 stsp fprintf(stderr, "usage: %s blame [-c commit] [repository-path] path\n",
1018 9f7d7167 2018-04-29 stsp getprogname());
1019 9f7d7167 2018-04-29 stsp exit(1);
1020 9f7d7167 2018-04-29 stsp }
1021 9f7d7167 2018-04-29 stsp
1022 4ed7e80c 2018-05-20 stsp static const struct got_error *
1023 a70480e0 2018-06-23 stsp show_blame_view(const char *path, struct got_object_id *commit_id,
1024 a70480e0 2018-06-23 stsp struct got_repository *repo)
1025 a70480e0 2018-06-23 stsp {
1026 a70480e0 2018-06-23 stsp const struct got_error *err = NULL;
1027 a70480e0 2018-06-23 stsp FILE *f;
1028 a70480e0 2018-06-23 stsp int ch, done = 0, first_displayed_line = 1, last_displayed_line = LINES;
1029 a70480e0 2018-06-23 stsp int eof, i;
1030 a70480e0 2018-06-23 stsp
1031 a70480e0 2018-06-23 stsp f = got_opentemp();
1032 a70480e0 2018-06-23 stsp if (f == NULL)
1033 a70480e0 2018-06-23 stsp return got_error_from_errno();
1034 a70480e0 2018-06-23 stsp
1035 a70480e0 2018-06-23 stsp err = got_blame(path, commit_id, repo, f);
1036 a70480e0 2018-06-23 stsp if (err)
1037 a70480e0 2018-06-23 stsp goto done;
1038 a70480e0 2018-06-23 stsp
1039 a70480e0 2018-06-23 stsp fflush(f);
1040 a70480e0 2018-06-23 stsp
1041 a70480e0 2018-06-23 stsp if (tog_blame_view.window == NULL) {
1042 a70480e0 2018-06-23 stsp tog_blame_view.window = newwin(0, 0, 0, 0);
1043 a70480e0 2018-06-23 stsp if (tog_blame_view.window == NULL)
1044 a70480e0 2018-06-23 stsp return got_error_from_errno();
1045 a70480e0 2018-06-23 stsp keypad(tog_blame_view.window, TRUE);
1046 a70480e0 2018-06-23 stsp }
1047 a70480e0 2018-06-23 stsp if (tog_blame_view.panel == NULL) {
1048 a70480e0 2018-06-23 stsp tog_blame_view.panel = new_panel(tog_blame_view.window);
1049 a70480e0 2018-06-23 stsp if (tog_blame_view.panel == NULL)
1050 a70480e0 2018-06-23 stsp return got_error_from_errno();
1051 a70480e0 2018-06-23 stsp } else
1052 a70480e0 2018-06-23 stsp show_panel(tog_blame_view.panel);
1053 a70480e0 2018-06-23 stsp
1054 a70480e0 2018-06-23 stsp while (!done) {
1055 a70480e0 2018-06-23 stsp err = draw_file(tog_blame_view.window, f, &first_displayed_line,
1056 a70480e0 2018-06-23 stsp &last_displayed_line, &eof, LINES);
1057 a70480e0 2018-06-23 stsp if (err)
1058 a70480e0 2018-06-23 stsp break;
1059 a70480e0 2018-06-23 stsp nodelay(stdscr, FALSE);
1060 a70480e0 2018-06-23 stsp ch = wgetch(tog_blame_view.window);
1061 a70480e0 2018-06-23 stsp nodelay(stdscr, TRUE);
1062 a70480e0 2018-06-23 stsp switch (ch) {
1063 a70480e0 2018-06-23 stsp case 'q':
1064 a70480e0 2018-06-23 stsp done = 1;
1065 a70480e0 2018-06-23 stsp break;
1066 a70480e0 2018-06-23 stsp case 'k':
1067 a70480e0 2018-06-23 stsp case KEY_UP:
1068 a70480e0 2018-06-23 stsp case KEY_BACKSPACE:
1069 a70480e0 2018-06-23 stsp if (first_displayed_line > 1)
1070 a70480e0 2018-06-23 stsp first_displayed_line--;
1071 a70480e0 2018-06-23 stsp break;
1072 a70480e0 2018-06-23 stsp case KEY_PPAGE:
1073 a70480e0 2018-06-23 stsp i = 0;
1074 a70480e0 2018-06-23 stsp while (i++ < LINES - 1 &&
1075 a70480e0 2018-06-23 stsp first_displayed_line > 1)
1076 a70480e0 2018-06-23 stsp first_displayed_line--;
1077 a70480e0 2018-06-23 stsp break;
1078 a70480e0 2018-06-23 stsp case 'j':
1079 a70480e0 2018-06-23 stsp case KEY_DOWN:
1080 a70480e0 2018-06-23 stsp case KEY_ENTER:
1081 a70480e0 2018-06-23 stsp case '\r':
1082 a70480e0 2018-06-23 stsp if (!eof)
1083 a70480e0 2018-06-23 stsp first_displayed_line++;
1084 a70480e0 2018-06-23 stsp break;
1085 a70480e0 2018-06-23 stsp case KEY_NPAGE:
1086 a70480e0 2018-06-23 stsp case ' ':
1087 a70480e0 2018-06-23 stsp i = 0;
1088 a70480e0 2018-06-23 stsp while (!eof && i++ < LINES - 1) {
1089 a70480e0 2018-06-23 stsp char *line = parse_next_line(f, NULL);
1090 a70480e0 2018-06-23 stsp first_displayed_line++;
1091 a70480e0 2018-06-23 stsp if (line == NULL)
1092 a70480e0 2018-06-23 stsp break;
1093 a70480e0 2018-06-23 stsp }
1094 a70480e0 2018-06-23 stsp break;
1095 a70480e0 2018-06-23 stsp default:
1096 a70480e0 2018-06-23 stsp break;
1097 a70480e0 2018-06-23 stsp }
1098 a70480e0 2018-06-23 stsp }
1099 a70480e0 2018-06-23 stsp done:
1100 a70480e0 2018-06-23 stsp fclose(f);
1101 a70480e0 2018-06-23 stsp return err;
1102 a70480e0 2018-06-23 stsp }
1103 a70480e0 2018-06-23 stsp
1104 a70480e0 2018-06-23 stsp static const struct got_error *
1105 9f7d7167 2018-04-29 stsp cmd_blame(int argc, char *argv[])
1106 9f7d7167 2018-04-29 stsp {
1107 a70480e0 2018-06-23 stsp const struct got_error *error;
1108 a70480e0 2018-06-23 stsp struct got_repository *repo = NULL;
1109 a70480e0 2018-06-23 stsp char *repo_path = NULL;
1110 a70480e0 2018-06-23 stsp char *path = NULL;
1111 a70480e0 2018-06-23 stsp struct got_object_id *commit_id = NULL;
1112 a70480e0 2018-06-23 stsp char *commit_id_str = NULL;
1113 a70480e0 2018-06-23 stsp int ch;
1114 a70480e0 2018-06-23 stsp
1115 a70480e0 2018-06-23 stsp #ifndef PROFILE
1116 a70480e0 2018-06-23 stsp if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
1117 a70480e0 2018-06-23 stsp err(1, "pledge");
1118 a70480e0 2018-06-23 stsp #endif
1119 a70480e0 2018-06-23 stsp
1120 a70480e0 2018-06-23 stsp while ((ch = getopt(argc, argv, "c:")) != -1) {
1121 a70480e0 2018-06-23 stsp switch (ch) {
1122 a70480e0 2018-06-23 stsp case 'c':
1123 a70480e0 2018-06-23 stsp commit_id_str = optarg;
1124 a70480e0 2018-06-23 stsp break;
1125 a70480e0 2018-06-23 stsp default:
1126 a70480e0 2018-06-23 stsp usage();
1127 a70480e0 2018-06-23 stsp /* NOTREACHED */
1128 a70480e0 2018-06-23 stsp }
1129 a70480e0 2018-06-23 stsp }
1130 a70480e0 2018-06-23 stsp
1131 a70480e0 2018-06-23 stsp argc -= optind;
1132 a70480e0 2018-06-23 stsp argv += optind;
1133 a70480e0 2018-06-23 stsp
1134 a70480e0 2018-06-23 stsp if (argc == 0) {
1135 a70480e0 2018-06-23 stsp usage_blame();
1136 a70480e0 2018-06-23 stsp } else if (argc == 1) {
1137 a70480e0 2018-06-23 stsp repo_path = getcwd(NULL, 0);
1138 a70480e0 2018-06-23 stsp if (repo_path == NULL)
1139 a70480e0 2018-06-23 stsp return got_error_from_errno();
1140 a70480e0 2018-06-23 stsp path = argv[0];
1141 a70480e0 2018-06-23 stsp } else if (argc == 2) {
1142 a70480e0 2018-06-23 stsp repo_path = realpath(argv[0], NULL);
1143 a70480e0 2018-06-23 stsp if (repo_path == NULL)
1144 a70480e0 2018-06-23 stsp return got_error_from_errno();
1145 a70480e0 2018-06-23 stsp path = argv[1];
1146 a70480e0 2018-06-23 stsp } else
1147 a70480e0 2018-06-23 stsp usage_blame();
1148 a70480e0 2018-06-23 stsp
1149 a70480e0 2018-06-23 stsp error = got_repo_open(&repo, repo_path);
1150 a70480e0 2018-06-23 stsp free(repo_path);
1151 a70480e0 2018-06-23 stsp if (error != NULL)
1152 66b4983c 2018-06-23 stsp return error;
1153 a70480e0 2018-06-23 stsp
1154 a70480e0 2018-06-23 stsp if (commit_id_str == NULL) {
1155 a70480e0 2018-06-23 stsp struct got_reference *head_ref;
1156 a70480e0 2018-06-23 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
1157 a70480e0 2018-06-23 stsp if (error != NULL)
1158 66b4983c 2018-06-23 stsp goto done;
1159 a70480e0 2018-06-23 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
1160 a70480e0 2018-06-23 stsp got_ref_close(head_ref);
1161 a70480e0 2018-06-23 stsp } else {
1162 a70480e0 2018-06-23 stsp struct got_object *obj;
1163 a70480e0 2018-06-23 stsp error = got_object_open_by_id_str(&obj, repo, commit_id_str);
1164 a70480e0 2018-06-23 stsp if (error != NULL)
1165 66b4983c 2018-06-23 stsp goto done;
1166 a70480e0 2018-06-23 stsp commit_id = got_object_get_id(obj);
1167 a19e88aa 2018-06-23 stsp if (commit_id == NULL)
1168 66b4983c 2018-06-23 stsp error = got_error_from_errno();
1169 a19e88aa 2018-06-23 stsp got_object_close(obj);
1170 a70480e0 2018-06-23 stsp }
1171 a19e88aa 2018-06-23 stsp if (error != NULL)
1172 a19e88aa 2018-06-23 stsp goto done;
1173 a70480e0 2018-06-23 stsp
1174 a70480e0 2018-06-23 stsp error = show_blame_view(path, commit_id, repo);
1175 a70480e0 2018-06-23 stsp done:
1176 a70480e0 2018-06-23 stsp free(commit_id);
1177 a70480e0 2018-06-23 stsp if (repo)
1178 a70480e0 2018-06-23 stsp got_repo_close(repo);
1179 a70480e0 2018-06-23 stsp return error;
1180 9f7d7167 2018-04-29 stsp }
1181 9f7d7167 2018-04-29 stsp
1182 5c5136c5 2018-05-20 stsp static void
1183 9f7d7167 2018-04-29 stsp init_curses(void)
1184 9f7d7167 2018-04-29 stsp {
1185 9f7d7167 2018-04-29 stsp initscr();
1186 9f7d7167 2018-04-29 stsp cbreak();
1187 9f7d7167 2018-04-29 stsp noecho();
1188 9f7d7167 2018-04-29 stsp nonl();
1189 9f7d7167 2018-04-29 stsp intrflush(stdscr, FALSE);
1190 9f7d7167 2018-04-29 stsp keypad(stdscr, TRUE);
1191 1f475ad8 2018-05-10 stsp curs_set(0);
1192 9f7d7167 2018-04-29 stsp }
1193 9f7d7167 2018-04-29 stsp
1194 4ed7e80c 2018-05-20 stsp __dead static void
1195 9f7d7167 2018-04-29 stsp usage(void)
1196 9f7d7167 2018-04-29 stsp {
1197 9f7d7167 2018-04-29 stsp int i;
1198 9f7d7167 2018-04-29 stsp
1199 c2301be8 2018-04-30 stsp fprintf(stderr, "usage: %s [-h] [command] [arg ...]\n\n"
1200 9f7d7167 2018-04-29 stsp "Available commands:\n", getprogname());
1201 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
1202 9f7d7167 2018-04-29 stsp struct tog_cmd *cmd = &tog_commands[i];
1203 c2301be8 2018-04-30 stsp fprintf(stderr, " %s: %s\n", cmd->name, cmd->descr);
1204 9f7d7167 2018-04-29 stsp }
1205 9f7d7167 2018-04-29 stsp exit(1);
1206 9f7d7167 2018-04-29 stsp }
1207 9f7d7167 2018-04-29 stsp
1208 c2301be8 2018-04-30 stsp static char **
1209 c2301be8 2018-04-30 stsp make_argv(const char *arg0, const char *arg1)
1210 c2301be8 2018-04-30 stsp {
1211 c2301be8 2018-04-30 stsp char **argv;
1212 c2301be8 2018-04-30 stsp int argc = (arg1 == NULL ? 1 : 2);
1213 c2301be8 2018-04-30 stsp
1214 c2301be8 2018-04-30 stsp argv = calloc(argc, sizeof(char *));
1215 c2301be8 2018-04-30 stsp if (argv == NULL)
1216 c2301be8 2018-04-30 stsp err(1, "calloc");
1217 c2301be8 2018-04-30 stsp argv[0] = strdup(arg0);
1218 c2301be8 2018-04-30 stsp if (argv[0] == NULL)
1219 c2301be8 2018-04-30 stsp err(1, "calloc");
1220 c2301be8 2018-04-30 stsp if (arg1) {
1221 c2301be8 2018-04-30 stsp argv[1] = strdup(arg1);
1222 c2301be8 2018-04-30 stsp if (argv[1] == NULL)
1223 c2301be8 2018-04-30 stsp err(1, "calloc");
1224 c2301be8 2018-04-30 stsp }
1225 c2301be8 2018-04-30 stsp
1226 c2301be8 2018-04-30 stsp return argv;
1227 c2301be8 2018-04-30 stsp }
1228 c2301be8 2018-04-30 stsp
1229 9f7d7167 2018-04-29 stsp int
1230 9f7d7167 2018-04-29 stsp main(int argc, char *argv[])
1231 9f7d7167 2018-04-29 stsp {
1232 9f7d7167 2018-04-29 stsp const struct got_error *error = NULL;
1233 9f7d7167 2018-04-29 stsp struct tog_cmd *cmd = NULL;
1234 9f7d7167 2018-04-29 stsp int ch, hflag = 0;
1235 c2301be8 2018-04-30 stsp char **cmd_argv = NULL;
1236 9f7d7167 2018-04-29 stsp
1237 9f7d7167 2018-04-29 stsp setlocale(LC_ALL, "");
1238 9f7d7167 2018-04-29 stsp
1239 9f7d7167 2018-04-29 stsp while ((ch = getopt(argc, argv, "h")) != -1) {
1240 9f7d7167 2018-04-29 stsp switch (ch) {
1241 9f7d7167 2018-04-29 stsp case 'h':
1242 9f7d7167 2018-04-29 stsp hflag = 1;
1243 9f7d7167 2018-04-29 stsp break;
1244 9f7d7167 2018-04-29 stsp default:
1245 9f7d7167 2018-04-29 stsp usage();
1246 9f7d7167 2018-04-29 stsp /* NOTREACHED */
1247 9f7d7167 2018-04-29 stsp }
1248 9f7d7167 2018-04-29 stsp }
1249 9f7d7167 2018-04-29 stsp
1250 9f7d7167 2018-04-29 stsp argc -= optind;
1251 9f7d7167 2018-04-29 stsp argv += optind;
1252 9f7d7167 2018-04-29 stsp optind = 0;
1253 c2301be8 2018-04-30 stsp optreset = 1;
1254 9f7d7167 2018-04-29 stsp
1255 c2301be8 2018-04-30 stsp if (argc == 0) {
1256 f29d3e89 2018-06-23 stsp if (hflag)
1257 f29d3e89 2018-06-23 stsp usage();
1258 c2301be8 2018-04-30 stsp /* Build an argument vector which runs a default command. */
1259 9f7d7167 2018-04-29 stsp cmd = &tog_commands[0];
1260 c2301be8 2018-04-30 stsp cmd_argv = make_argv(cmd->name, NULL);
1261 c2301be8 2018-04-30 stsp argc = 1;
1262 c2301be8 2018-04-30 stsp } else {
1263 9f7d7167 2018-04-29 stsp int i;
1264 9f7d7167 2018-04-29 stsp
1265 c2301be8 2018-04-30 stsp /* Did the user specific a command? */
1266 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
1267 c2301be8 2018-04-30 stsp if (strncmp(tog_commands[i].name, argv[0],
1268 9f7d7167 2018-04-29 stsp strlen(argv[0])) == 0) {
1269 9f7d7167 2018-04-29 stsp cmd = &tog_commands[i];
1270 9f7d7167 2018-04-29 stsp if (hflag)
1271 9f7d7167 2018-04-29 stsp tog_commands[i].cmd_usage();
1272 9f7d7167 2018-04-29 stsp break;
1273 9f7d7167 2018-04-29 stsp }
1274 9f7d7167 2018-04-29 stsp }
1275 9f7d7167 2018-04-29 stsp if (cmd == NULL) {
1276 c2301be8 2018-04-30 stsp /* Did the user specify a repository? */
1277 c2301be8 2018-04-30 stsp char *repo_path = realpath(argv[0], NULL);
1278 c2301be8 2018-04-30 stsp if (repo_path) {
1279 c2301be8 2018-04-30 stsp struct got_repository *repo;
1280 c2301be8 2018-04-30 stsp error = got_repo_open(&repo, repo_path);
1281 c2301be8 2018-04-30 stsp if (error == NULL)
1282 c2301be8 2018-04-30 stsp got_repo_close(repo);
1283 c2301be8 2018-04-30 stsp } else
1284 ad7de8d9 2018-04-30 stsp error = got_error_from_errno();
1285 c2301be8 2018-04-30 stsp if (error) {
1286 f29d3e89 2018-06-23 stsp if (hflag) {
1287 f29d3e89 2018-06-23 stsp fprintf(stderr, "%s: '%s' is not a "
1288 f29d3e89 2018-06-23 stsp "known command\n", getprogname(),
1289 f29d3e89 2018-06-23 stsp argv[0]);
1290 f29d3e89 2018-06-23 stsp usage();
1291 f29d3e89 2018-06-23 stsp }
1292 ad7de8d9 2018-04-30 stsp fprintf(stderr, "%s: '%s' is neither a known "
1293 ad7de8d9 2018-04-30 stsp "command nor a path to a repository\n",
1294 c2301be8 2018-04-30 stsp getprogname(), argv[0]);
1295 ad7de8d9 2018-04-30 stsp free(repo_path);
1296 c2301be8 2018-04-30 stsp return 1;
1297 c2301be8 2018-04-30 stsp }
1298 c2301be8 2018-04-30 stsp cmd = &tog_commands[0];
1299 c2301be8 2018-04-30 stsp cmd_argv = make_argv(cmd->name, repo_path);
1300 c2301be8 2018-04-30 stsp argc = 2;
1301 c2301be8 2018-04-30 stsp free(repo_path);
1302 9f7d7167 2018-04-29 stsp }
1303 9f7d7167 2018-04-29 stsp }
1304 9f7d7167 2018-04-29 stsp
1305 5c5136c5 2018-05-20 stsp init_curses();
1306 9f7d7167 2018-04-29 stsp
1307 c2301be8 2018-04-30 stsp error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
1308 9f7d7167 2018-04-29 stsp if (error)
1309 9f7d7167 2018-04-29 stsp goto done;
1310 9f7d7167 2018-04-29 stsp done:
1311 9f7d7167 2018-04-29 stsp endwin();
1312 c2301be8 2018-04-30 stsp free(cmd_argv);
1313 9f7d7167 2018-04-29 stsp if (error)
1314 9f7d7167 2018-04-29 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
1315 9f7d7167 2018-04-29 stsp return 0;
1316 9f7d7167 2018-04-29 stsp }