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 ffd1d5e5 2018-06-23 stsp #include <sys/stat.h>
19 80ddbec8 2018-04-29 stsp
20 31120ada 2018-04-30 stsp #include <errno.h>
21 61e69b96 2018-05-20 stsp #define _XOPEN_SOURCE_EXTENDED
22 9f7d7167 2018-04-29 stsp #include <curses.h>
23 61e69b96 2018-05-20 stsp #undef _XOPEN_SOURCE_EXTENDED
24 9f7d7167 2018-04-29 stsp #include <panel.h>
25 9f7d7167 2018-04-29 stsp #include <locale.h>
26 9f7d7167 2018-04-29 stsp #include <stdlib.h>
27 26ed57b2 2018-05-19 stsp #include <stdio.h>
28 9f7d7167 2018-04-29 stsp #include <getopt.h>
29 9f7d7167 2018-04-29 stsp #include <string.h>
30 9f7d7167 2018-04-29 stsp #include <err.h>
31 80ddbec8 2018-04-29 stsp #include <unistd.h>
32 26ed57b2 2018-05-19 stsp #include <util.h>
33 26ed57b2 2018-05-19 stsp #include <limits.h>
34 61e69b96 2018-05-20 stsp #include <wchar.h>
35 788c352e 2018-06-16 stsp #include <time.h>
36 84451b3e 2018-07-10 stsp #include <pthread.h>
37 9f7d7167 2018-04-29 stsp
38 9f7d7167 2018-04-29 stsp #include "got_error.h"
39 80ddbec8 2018-04-29 stsp #include "got_object.h"
40 80ddbec8 2018-04-29 stsp #include "got_reference.h"
41 80ddbec8 2018-04-29 stsp #include "got_repository.h"
42 80ddbec8 2018-04-29 stsp #include "got_diff.h"
43 511a516b 2018-05-19 stsp #include "got_opentemp.h"
44 9ba79e04 2018-06-11 stsp #include "got_commit_graph.h"
45 00dfcb92 2018-06-11 stsp #include "got_utf8.h"
46 a70480e0 2018-06-23 stsp #include "got_blame.h"
47 9f7d7167 2018-04-29 stsp
48 881b2d3e 2018-04-30 stsp #ifndef MIN
49 881b2d3e 2018-04-30 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
50 881b2d3e 2018-04-30 stsp #endif
51 881b2d3e 2018-04-30 stsp
52 9f7d7167 2018-04-29 stsp #ifndef nitems
53 9f7d7167 2018-04-29 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
54 9f7d7167 2018-04-29 stsp #endif
55 9f7d7167 2018-04-29 stsp
56 9f7d7167 2018-04-29 stsp struct tog_cmd {
57 c2301be8 2018-04-30 stsp const char *name;
58 9f7d7167 2018-04-29 stsp const struct got_error *(*cmd_main)(int, char *[]);
59 9f7d7167 2018-04-29 stsp void (*cmd_usage)(void);
60 c2301be8 2018-04-30 stsp const char *descr;
61 9f7d7167 2018-04-29 stsp };
62 9f7d7167 2018-04-29 stsp
63 4ed7e80c 2018-05-20 stsp __dead static void usage(void);
64 4ed7e80c 2018-05-20 stsp __dead static void usage_log(void);
65 4ed7e80c 2018-05-20 stsp __dead static void usage_diff(void);
66 4ed7e80c 2018-05-20 stsp __dead static void usage_blame(void);
67 ffd1d5e5 2018-06-23 stsp __dead static void usage_tree(void);
68 9f7d7167 2018-04-29 stsp
69 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_log(int, char *[]);
70 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_diff(int, char *[]);
71 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_blame(int, char *[]);
72 ffd1d5e5 2018-06-23 stsp static const struct got_error* cmd_tree(int, char *[]);
73 9f7d7167 2018-04-29 stsp
74 4ed7e80c 2018-05-20 stsp static struct tog_cmd tog_commands[] = {
75 cbb6b58a 2018-05-20 stsp { "log", cmd_log, usage_log,
76 9f7d7167 2018-04-29 stsp "show repository history" },
77 cbb6b58a 2018-05-20 stsp { "diff", cmd_diff, usage_diff,
78 9f7d7167 2018-04-29 stsp "compare files and directories" },
79 cbb6b58a 2018-05-20 stsp { "blame", cmd_blame, usage_blame,
80 9f7d7167 2018-04-29 stsp "show line-by-line file history" },
81 ffd1d5e5 2018-06-23 stsp { "tree", cmd_tree, usage_tree,
82 ffd1d5e5 2018-06-23 stsp "browse trees in repository" },
83 9f7d7167 2018-04-29 stsp };
84 9f7d7167 2018-04-29 stsp
85 d6b05b5b 2018-08-04 stsp enum tog_view_type {
86 d6b05b5b 2018-08-04 stsp TOG_VIEW_DIFF,
87 d6b05b5b 2018-08-04 stsp TOG_VIEW_LOG,
88 d6b05b5b 2018-08-04 stsp TOG_VIEW_TREE,
89 d6b05b5b 2018-08-04 stsp TOG_VIEW_BLAME
90 d6b05b5b 2018-08-04 stsp };
91 d6b05b5b 2018-08-04 stsp
92 ba4f502b 2018-08-04 stsp struct commit_queue_entry {
93 ba4f502b 2018-08-04 stsp TAILQ_ENTRY(commit_queue_entry) entry;
94 ba4f502b 2018-08-04 stsp struct got_object_id *id;
95 ba4f502b 2018-08-04 stsp struct got_commit_object *commit;
96 ba4f502b 2018-08-04 stsp };
97 ba4f502b 2018-08-04 stsp TAILQ_HEAD(commit_queue_head, commit_queue_entry);
98 ba4f502b 2018-08-04 stsp struct commit_queue {
99 ba4f502b 2018-08-04 stsp int ncommits;
100 ba4f502b 2018-08-04 stsp struct commit_queue_head head;
101 b01e7d3b 2018-08-04 stsp };
102 b01e7d3b 2018-08-04 stsp
103 b01e7d3b 2018-08-04 stsp struct tog_diff_view_state {
104 b01e7d3b 2018-08-04 stsp FILE *f;
105 b01e7d3b 2018-08-04 stsp int first_displayed_line;
106 b01e7d3b 2018-08-04 stsp int last_displayed_line;
107 b01e7d3b 2018-08-04 stsp };
108 b01e7d3b 2018-08-04 stsp
109 b01e7d3b 2018-08-04 stsp struct tog_log_view_state {
110 b01e7d3b 2018-08-04 stsp struct got_commit_graph *graph;
111 b01e7d3b 2018-08-04 stsp struct commit_queue commits;
112 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *first_displayed_entry;
113 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *last_displayed_entry;
114 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *selected_entry;
115 b01e7d3b 2018-08-04 stsp int selected;
116 b01e7d3b 2018-08-04 stsp char *in_repo_path;
117 b01e7d3b 2018-08-04 stsp struct got_repository *repo;
118 ba4f502b 2018-08-04 stsp };
119 ba4f502b 2018-08-04 stsp
120 cc3c9aac 2018-08-01 stsp struct tog_view {
121 26ed57b2 2018-05-19 stsp WINDOW *window;
122 26ed57b2 2018-05-19 stsp PANEL *panel;
123 97ddc146 2018-08-01 stsp int nlines, ncols, begin_y, begin_x;
124 f7d12f7e 2018-08-01 stsp int lines, cols; /* copies of LINES and COLS */
125 6d0fee91 2018-08-01 stsp struct tog_view *parent;
126 5dc9f4bc 2018-08-04 stsp
127 5dc9f4bc 2018-08-04 stsp /* type-specific state */
128 d6b05b5b 2018-08-04 stsp enum tog_view_type type;
129 5dc9f4bc 2018-08-04 stsp union {
130 b01e7d3b 2018-08-04 stsp struct tog_diff_view_state diff;
131 b01e7d3b 2018-08-04 stsp struct tog_log_view_state log;
132 5dc9f4bc 2018-08-04 stsp } state;
133 cc3c9aac 2018-08-01 stsp };
134 cd0acaa7 2018-05-20 stsp
135 ba4f502b 2018-08-04 stsp static const struct got_error *open_diff_view(struct tog_view *,
136 ba4f502b 2018-08-04 stsp struct got_object *, struct got_object *, struct got_repository *);
137 5dc9f4bc 2018-08-04 stsp static const struct got_error *show_diff_view(struct tog_view *);
138 5dc9f4bc 2018-08-04 stsp static void close_diff_view(struct tog_view *);
139 ba4f502b 2018-08-04 stsp static const struct got_error *open_log_view(struct tog_view *,
140 ba4f502b 2018-08-04 stsp struct got_object_id *, struct got_repository *, const char *);
141 ba4f502b 2018-08-04 stsp static const struct got_error * show_log_view(struct tog_view *);
142 ba4f502b 2018-08-04 stsp static void close_log_view(struct tog_view *);
143 4fc679ca 2018-08-04 stsp static const struct got_error *show_blame_view(struct tog_view *, const char *,
144 5221c383 2018-08-01 stsp struct got_object_id *, struct got_repository *);
145 4fc679ca 2018-08-04 stsp static const struct got_error *show_tree_view(struct tog_view *,
146 4fc679ca 2018-08-04 stsp struct got_tree_object *, struct got_object_id *, struct got_repository *);
147 26ed57b2 2018-05-19 stsp
148 ea5e7bb5 2018-08-01 stsp static void
149 96a765a8 2018-08-04 stsp view_close(struct tog_view *view)
150 ea5e7bb5 2018-08-01 stsp {
151 ea5e7bb5 2018-08-01 stsp if (view->panel)
152 ea5e7bb5 2018-08-01 stsp del_panel(view->panel);
153 ea5e7bb5 2018-08-01 stsp if (view->window)
154 ea5e7bb5 2018-08-01 stsp delwin(view->window);
155 ea5e7bb5 2018-08-01 stsp free(view);
156 ea5e7bb5 2018-08-01 stsp }
157 ea5e7bb5 2018-08-01 stsp
158 ea5e7bb5 2018-08-01 stsp static struct tog_view *
159 b3665f43 2018-08-04 stsp view_open(int nlines, int ncols, int begin_y, int begin_x,
160 d6b05b5b 2018-08-04 stsp struct tog_view *parent, enum tog_view_type type)
161 ea5e7bb5 2018-08-01 stsp {
162 ea5e7bb5 2018-08-01 stsp struct tog_view *view = malloc(sizeof(*view));
163 ea5e7bb5 2018-08-01 stsp
164 ea5e7bb5 2018-08-01 stsp if (view == NULL)
165 ea5e7bb5 2018-08-01 stsp return NULL;
166 ea5e7bb5 2018-08-01 stsp
167 6d0fee91 2018-08-01 stsp view->parent = parent;
168 d6b05b5b 2018-08-04 stsp view->type = type;
169 f7d12f7e 2018-08-01 stsp view->lines = LINES;
170 f7d12f7e 2018-08-01 stsp view->cols = COLS;
171 207b9029 2018-08-01 stsp view->nlines = nlines ? nlines : LINES - begin_y;
172 207b9029 2018-08-01 stsp view->ncols = ncols ? ncols : COLS - begin_x;
173 97ddc146 2018-08-01 stsp view->begin_y = begin_y;
174 97ddc146 2018-08-01 stsp view->begin_x = begin_x;
175 842167bf 2018-08-01 stsp view->window = newwin(nlines, ncols, begin_y, begin_x);
176 ea5e7bb5 2018-08-01 stsp if (view->window == NULL) {
177 96a765a8 2018-08-04 stsp view_close(view);
178 ea5e7bb5 2018-08-01 stsp return NULL;
179 ea5e7bb5 2018-08-01 stsp }
180 ea5e7bb5 2018-08-01 stsp view->panel = new_panel(view->window);
181 ea5e7bb5 2018-08-01 stsp if (view->panel == NULL) {
182 96a765a8 2018-08-04 stsp view_close(view);
183 ea5e7bb5 2018-08-01 stsp return NULL;
184 ea5e7bb5 2018-08-01 stsp }
185 ea5e7bb5 2018-08-01 stsp
186 ea5e7bb5 2018-08-01 stsp keypad(view->window, TRUE);
187 ea5e7bb5 2018-08-01 stsp return view;
188 f7d12f7e 2018-08-01 stsp }
189 f7d12f7e 2018-08-01 stsp
190 cdf1ee82 2018-08-01 stsp void
191 6acd1bd1 2018-08-04 stsp view_show(struct tog_view *view)
192 cdf1ee82 2018-08-01 stsp {
193 cdf1ee82 2018-08-01 stsp show_panel(view->panel);
194 cdf1ee82 2018-08-01 stsp update_panels();
195 cdf1ee82 2018-08-01 stsp }
196 cdf1ee82 2018-08-01 stsp
197 f7d12f7e 2018-08-01 stsp const struct got_error *
198 f7d12f7e 2018-08-01 stsp view_resize(struct tog_view *view)
199 f7d12f7e 2018-08-01 stsp {
200 f7d12f7e 2018-08-01 stsp int nlines, ncols;
201 f7d12f7e 2018-08-01 stsp
202 6d0fee91 2018-08-01 stsp while (view) {
203 6d0fee91 2018-08-01 stsp if (view->lines > LINES)
204 6d0fee91 2018-08-01 stsp nlines = view->nlines - (view->lines - LINES);
205 6d0fee91 2018-08-01 stsp else
206 6d0fee91 2018-08-01 stsp nlines = view->nlines + (LINES - view->lines);
207 f7d12f7e 2018-08-01 stsp
208 6d0fee91 2018-08-01 stsp if (view->cols > COLS)
209 6d0fee91 2018-08-01 stsp ncols = view->ncols - (view->cols - COLS);
210 6d0fee91 2018-08-01 stsp else
211 6d0fee91 2018-08-01 stsp ncols = view->ncols + (COLS - view->cols);
212 f7d12f7e 2018-08-01 stsp
213 6d0fee91 2018-08-01 stsp if (wresize(view->window, nlines, ncols) == ERR)
214 6d0fee91 2018-08-01 stsp return got_error_from_errno();
215 77a567dd 2018-08-01 stsp replace_panel(view->panel, view->window);
216 f7d12f7e 2018-08-01 stsp
217 6d0fee91 2018-08-01 stsp view->nlines = nlines;
218 6d0fee91 2018-08-01 stsp view->ncols = ncols;
219 6d0fee91 2018-08-01 stsp view->lines = LINES;
220 6d0fee91 2018-08-01 stsp view->cols = COLS;
221 6d0fee91 2018-08-01 stsp
222 6d0fee91 2018-08-01 stsp view = view->parent;
223 6d0fee91 2018-08-01 stsp }
224 6d0fee91 2018-08-01 stsp
225 f7d12f7e 2018-08-01 stsp return NULL;
226 ea5e7bb5 2018-08-01 stsp }
227 ea5e7bb5 2018-08-01 stsp
228 4ed7e80c 2018-05-20 stsp __dead static void
229 9f7d7167 2018-04-29 stsp usage_log(void)
230 9f7d7167 2018-04-29 stsp {
231 80ddbec8 2018-04-29 stsp endwin();
232 c70c5802 2018-08-01 stsp fprintf(stderr,
233 c70c5802 2018-08-01 stsp "usage: %s log [-c commit] [-r repository-path] [path]\n",
234 9f7d7167 2018-04-29 stsp getprogname());
235 9f7d7167 2018-04-29 stsp exit(1);
236 80ddbec8 2018-04-29 stsp }
237 80ddbec8 2018-04-29 stsp
238 963b370f 2018-05-20 stsp /* Create newly allocated wide-character string equivalent to a byte string. */
239 80ddbec8 2018-04-29 stsp static const struct got_error *
240 963b370f 2018-05-20 stsp mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
241 963b370f 2018-05-20 stsp {
242 00dfcb92 2018-06-11 stsp char *vis = NULL;
243 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
244 963b370f 2018-05-20 stsp
245 963b370f 2018-05-20 stsp *ws = NULL;
246 963b370f 2018-05-20 stsp *wlen = mbstowcs(NULL, s, 0);
247 00dfcb92 2018-06-11 stsp if (*wlen == (size_t)-1) {
248 00dfcb92 2018-06-11 stsp int vislen;
249 00dfcb92 2018-06-11 stsp if (errno != EILSEQ)
250 00dfcb92 2018-06-11 stsp return got_error_from_errno();
251 00dfcb92 2018-06-11 stsp
252 00dfcb92 2018-06-11 stsp /* byte string invalid in current encoding; try to "fix" it */
253 00dfcb92 2018-06-11 stsp err = got_mbsavis(&vis, &vislen, s);
254 00dfcb92 2018-06-11 stsp if (err)
255 00dfcb92 2018-06-11 stsp return err;
256 00dfcb92 2018-06-11 stsp *wlen = mbstowcs(NULL, vis, 0);
257 a7f50699 2018-06-11 stsp if (*wlen == (size_t)-1) {
258 a7f50699 2018-06-11 stsp err = got_error_from_errno(); /* give up */
259 a7f50699 2018-06-11 stsp goto done;
260 a7f50699 2018-06-11 stsp }
261 00dfcb92 2018-06-11 stsp }
262 963b370f 2018-05-20 stsp
263 963b370f 2018-05-20 stsp *ws = calloc(*wlen + 1, sizeof(*ws));
264 a7f50699 2018-06-11 stsp if (*ws == NULL) {
265 a7f50699 2018-06-11 stsp err = got_error_from_errno();
266 a7f50699 2018-06-11 stsp goto done;
267 a7f50699 2018-06-11 stsp }
268 963b370f 2018-05-20 stsp
269 00dfcb92 2018-06-11 stsp if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
270 963b370f 2018-05-20 stsp err = got_error_from_errno();
271 a7f50699 2018-06-11 stsp done:
272 00dfcb92 2018-06-11 stsp free(vis);
273 963b370f 2018-05-20 stsp if (err) {
274 963b370f 2018-05-20 stsp free(*ws);
275 963b370f 2018-05-20 stsp *ws = NULL;
276 963b370f 2018-05-20 stsp *wlen = 0;
277 963b370f 2018-05-20 stsp }
278 963b370f 2018-05-20 stsp return err;
279 963b370f 2018-05-20 stsp }
280 963b370f 2018-05-20 stsp
281 963b370f 2018-05-20 stsp /* Format a line for display, ensuring that it won't overflow a width limit. */
282 963b370f 2018-05-20 stsp static const struct got_error *
283 ffd1d5e5 2018-06-23 stsp format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit)
284 963b370f 2018-05-20 stsp {
285 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
286 963b370f 2018-05-20 stsp int cols = 0;
287 963b370f 2018-05-20 stsp wchar_t *wline = NULL;
288 963b370f 2018-05-20 stsp size_t wlen;
289 963b370f 2018-05-20 stsp int i;
290 963b370f 2018-05-20 stsp
291 963b370f 2018-05-20 stsp *wlinep = NULL;
292 b700b5d6 2018-07-10 stsp *widthp = 0;
293 963b370f 2018-05-20 stsp
294 963b370f 2018-05-20 stsp err = mbs2ws(&wline, &wlen, line);
295 963b370f 2018-05-20 stsp if (err)
296 963b370f 2018-05-20 stsp return err;
297 963b370f 2018-05-20 stsp
298 963b370f 2018-05-20 stsp i = 0;
299 b700b5d6 2018-07-10 stsp while (i < wlen && cols < wlimit) {
300 963b370f 2018-05-20 stsp int width = wcwidth(wline[i]);
301 963b370f 2018-05-20 stsp switch (width) {
302 963b370f 2018-05-20 stsp case 0:
303 b700b5d6 2018-07-10 stsp i++;
304 963b370f 2018-05-20 stsp break;
305 963b370f 2018-05-20 stsp case 1:
306 963b370f 2018-05-20 stsp case 2:
307 b700b5d6 2018-07-10 stsp if (cols + width <= wlimit) {
308 b700b5d6 2018-07-10 stsp cols += width;
309 b700b5d6 2018-07-10 stsp i++;
310 b700b5d6 2018-07-10 stsp }
311 963b370f 2018-05-20 stsp break;
312 963b370f 2018-05-20 stsp case -1:
313 963b370f 2018-05-20 stsp if (wline[i] == L'\t')
314 b700b5d6 2018-07-10 stsp cols += TABSIZE - ((cols + 1) % TABSIZE);
315 b700b5d6 2018-07-10 stsp i++;
316 963b370f 2018-05-20 stsp break;
317 963b370f 2018-05-20 stsp default:
318 963b370f 2018-05-20 stsp err = got_error_from_errno();
319 963b370f 2018-05-20 stsp goto done;
320 963b370f 2018-05-20 stsp }
321 963b370f 2018-05-20 stsp }
322 963b370f 2018-05-20 stsp wline[i] = L'\0';
323 b700b5d6 2018-07-10 stsp if (widthp)
324 b700b5d6 2018-07-10 stsp *widthp = cols;
325 963b370f 2018-05-20 stsp done:
326 963b370f 2018-05-20 stsp if (err)
327 963b370f 2018-05-20 stsp free(wline);
328 963b370f 2018-05-20 stsp else
329 963b370f 2018-05-20 stsp *wlinep = wline;
330 963b370f 2018-05-20 stsp return err;
331 963b370f 2018-05-20 stsp }
332 963b370f 2018-05-20 stsp
333 963b370f 2018-05-20 stsp static const struct got_error *
334 2814baeb 2018-08-01 stsp draw_commit(struct tog_view *view, struct got_commit_object *commit,
335 2814baeb 2018-08-01 stsp struct got_object_id *id)
336 80ddbec8 2018-04-29 stsp {
337 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
338 b39d25c7 2018-07-10 stsp char datebuf[10]; /* YY-MM-DD + SPACE + NUL */
339 80ddbec8 2018-04-29 stsp char *logmsg0 = NULL, *logmsg = NULL;
340 6d9fbc00 2018-04-29 stsp char *author0 = NULL, *author = NULL;
341 bb737323 2018-05-20 stsp wchar_t *wlogmsg = NULL, *wauthor = NULL;
342 bb737323 2018-05-20 stsp int author_width, logmsg_width;
343 6d9fbc00 2018-04-29 stsp char *newline, *smallerthan;
344 80ddbec8 2018-04-29 stsp char *line = NULL;
345 bb737323 2018-05-20 stsp int col, limit;
346 b39d25c7 2018-07-10 stsp static const size_t date_display_cols = 9;
347 bb737323 2018-05-20 stsp static const size_t author_display_cols = 16;
348 f7d12f7e 2018-08-01 stsp const int avail = view->ncols;
349 80ddbec8 2018-04-29 stsp
350 c70c5802 2018-08-01 stsp if (strftime(datebuf, sizeof(datebuf), "%g/%m/%d ",
351 c70c5802 2018-08-01 stsp &commit->tm_committer) >= sizeof(datebuf))
352 b39d25c7 2018-07-10 stsp return got_error(GOT_ERR_NO_SPACE);
353 b39d25c7 2018-07-10 stsp
354 b39d25c7 2018-07-10 stsp if (avail < date_display_cols)
355 b39d25c7 2018-07-10 stsp limit = MIN(sizeof(datebuf) - 1, avail);
356 b39d25c7 2018-07-10 stsp else
357 b39d25c7 2018-07-10 stsp limit = MIN(date_display_cols, sizeof(datebuf) - 1);
358 2814baeb 2018-08-01 stsp waddnstr(view->window, datebuf, limit);
359 b39d25c7 2018-07-10 stsp col = limit + 1;
360 b39d25c7 2018-07-10 stsp if (col > avail)
361 b39d25c7 2018-07-10 stsp goto done;
362 b39d25c7 2018-07-10 stsp
363 6d9fbc00 2018-04-29 stsp author0 = strdup(commit->author);
364 6d9fbc00 2018-04-29 stsp if (author0 == NULL) {
365 80ddbec8 2018-04-29 stsp err = got_error_from_errno();
366 80ddbec8 2018-04-29 stsp goto done;
367 80ddbec8 2018-04-29 stsp }
368 6d9fbc00 2018-04-29 stsp author = author0;
369 6d9fbc00 2018-04-29 stsp smallerthan = strchr(author, '<');
370 6d9fbc00 2018-04-29 stsp if (smallerthan)
371 6d9fbc00 2018-04-29 stsp *smallerthan = '\0';
372 6d9fbc00 2018-04-29 stsp else {
373 6d9fbc00 2018-04-29 stsp char *at = strchr(author, '@');
374 6d9fbc00 2018-04-29 stsp if (at)
375 6d9fbc00 2018-04-29 stsp *at = '\0';
376 6d9fbc00 2018-04-29 stsp }
377 b39d25c7 2018-07-10 stsp limit = avail - col;
378 bb737323 2018-05-20 stsp err = format_line(&wauthor, &author_width, author, limit);
379 bb737323 2018-05-20 stsp if (err)
380 bb737323 2018-05-20 stsp goto done;
381 2814baeb 2018-08-01 stsp waddwstr(view->window, wauthor);
382 bb737323 2018-05-20 stsp col += author_width;
383 9c2eaf34 2018-05-20 stsp while (col <= avail && author_width < author_display_cols + 1) {
384 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
385 bb737323 2018-05-20 stsp col++;
386 bb737323 2018-05-20 stsp author_width++;
387 bb737323 2018-05-20 stsp }
388 9c2eaf34 2018-05-20 stsp if (col > avail)
389 9c2eaf34 2018-05-20 stsp goto done;
390 80ddbec8 2018-04-29 stsp
391 bb737323 2018-05-20 stsp logmsg0 = strdup(commit->logmsg);
392 bb737323 2018-05-20 stsp if (logmsg0 == NULL) {
393 6d9fbc00 2018-04-29 stsp err = got_error_from_errno();
394 6d9fbc00 2018-04-29 stsp goto done;
395 6d9fbc00 2018-04-29 stsp }
396 bb737323 2018-05-20 stsp logmsg = logmsg0;
397 bb737323 2018-05-20 stsp while (*logmsg == '\n')
398 bb737323 2018-05-20 stsp logmsg++;
399 bb737323 2018-05-20 stsp newline = strchr(logmsg, '\n');
400 bb737323 2018-05-20 stsp if (newline)
401 bb737323 2018-05-20 stsp *newline = '\0';
402 bb737323 2018-05-20 stsp limit = avail - col;
403 bb737323 2018-05-20 stsp err = format_line(&wlogmsg, &logmsg_width, logmsg, limit);
404 bb737323 2018-05-20 stsp if (err)
405 bb737323 2018-05-20 stsp goto done;
406 2814baeb 2018-08-01 stsp waddwstr(view->window, wlogmsg);
407 bb737323 2018-05-20 stsp col += logmsg_width;
408 bb737323 2018-05-20 stsp while (col <= avail) {
409 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
410 bb737323 2018-05-20 stsp col++;
411 881b2d3e 2018-04-30 stsp }
412 80ddbec8 2018-04-29 stsp done:
413 80ddbec8 2018-04-29 stsp free(logmsg0);
414 bb737323 2018-05-20 stsp free(wlogmsg);
415 6d9fbc00 2018-04-29 stsp free(author0);
416 bb737323 2018-05-20 stsp free(wauthor);
417 80ddbec8 2018-04-29 stsp free(line);
418 80ddbec8 2018-04-29 stsp return err;
419 80ddbec8 2018-04-29 stsp }
420 26ed57b2 2018-05-19 stsp
421 899d86c2 2018-05-10 stsp static struct commit_queue_entry *
422 899d86c2 2018-05-10 stsp alloc_commit_queue_entry(struct got_commit_object *commit,
423 899d86c2 2018-05-10 stsp struct got_object_id *id)
424 80ddbec8 2018-04-29 stsp {
425 80ddbec8 2018-04-29 stsp struct commit_queue_entry *entry;
426 80ddbec8 2018-04-29 stsp
427 80ddbec8 2018-04-29 stsp entry = calloc(1, sizeof(*entry));
428 80ddbec8 2018-04-29 stsp if (entry == NULL)
429 899d86c2 2018-05-10 stsp return NULL;
430 99db9666 2018-05-07 stsp
431 899d86c2 2018-05-10 stsp entry->id = id;
432 99db9666 2018-05-07 stsp entry->commit = commit;
433 899d86c2 2018-05-10 stsp return entry;
434 99db9666 2018-05-07 stsp }
435 80ddbec8 2018-04-29 stsp
436 99db9666 2018-05-07 stsp static void
437 99db9666 2018-05-07 stsp pop_commit(struct commit_queue *commits)
438 99db9666 2018-05-07 stsp {
439 99db9666 2018-05-07 stsp struct commit_queue_entry *entry;
440 99db9666 2018-05-07 stsp
441 ecb28ae0 2018-07-16 stsp entry = TAILQ_FIRST(&commits->head);
442 ecb28ae0 2018-07-16 stsp TAILQ_REMOVE(&commits->head, entry, entry);
443 99db9666 2018-05-07 stsp got_object_commit_close(entry->commit);
444 ecb28ae0 2018-07-16 stsp commits->ncommits--;
445 9ba79e04 2018-06-11 stsp /* Don't free entry->id! It is owned by the commit graph. */
446 99db9666 2018-05-07 stsp free(entry);
447 99db9666 2018-05-07 stsp }
448 99db9666 2018-05-07 stsp
449 99db9666 2018-05-07 stsp static void
450 99db9666 2018-05-07 stsp free_commits(struct commit_queue *commits)
451 99db9666 2018-05-07 stsp {
452 ecb28ae0 2018-07-16 stsp while (!TAILQ_EMPTY(&commits->head))
453 99db9666 2018-05-07 stsp pop_commit(commits);
454 c4972b91 2018-05-07 stsp }
455 c4972b91 2018-05-07 stsp
456 c4972b91 2018-05-07 stsp static const struct got_error *
457 9ba79e04 2018-06-11 stsp queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
458 ecb28ae0 2018-07-16 stsp struct got_object_id *start_id, int minqueue, int init,
459 ecb28ae0 2018-07-16 stsp struct got_repository *repo, const char *path)
460 c4972b91 2018-05-07 stsp {
461 899d86c2 2018-05-10 stsp const struct got_error *err = NULL;
462 899d86c2 2018-05-10 stsp struct got_object_id *id;
463 9ba79e04 2018-06-11 stsp struct commit_queue_entry *entry;
464 ecb28ae0 2018-07-16 stsp int nfetched, nqueued = 0, found_obj = 0;
465 c8f60bff 2018-07-23 stsp int is_root_path = strcmp(path, "/") == 0;
466 c4972b91 2018-05-07 stsp
467 9ba79e04 2018-06-11 stsp err = got_commit_graph_iter_start(graph, start_id);
468 c4972b91 2018-05-07 stsp if (err)
469 c4972b91 2018-05-07 stsp return err;
470 c4972b91 2018-05-07 stsp
471 ecb28ae0 2018-07-16 stsp entry = TAILQ_LAST(&commits->head, commit_queue_head);
472 9ba79e04 2018-06-11 stsp if (entry && got_object_id_cmp(entry->id, start_id) == 0) {
473 9ba79e04 2018-06-11 stsp int nfetched;
474 899d86c2 2018-05-10 stsp
475 9ba79e04 2018-06-11 stsp /* Start ID's commit is already on the queue; skip over it. */
476 9ba79e04 2018-06-11 stsp err = got_commit_graph_iter_next(&id, graph);
477 9ba79e04 2018-06-11 stsp if (err && err->code != GOT_ERR_ITER_NEED_MORE)
478 9ba79e04 2018-06-11 stsp return err;
479 9ba79e04 2018-06-11 stsp
480 9ba79e04 2018-06-11 stsp err = got_commit_graph_fetch_commits(&nfetched, graph, 1, repo);
481 9ba79e04 2018-06-11 stsp if (err)
482 9ba79e04 2018-06-11 stsp return err;
483 c4972b91 2018-05-07 stsp }
484 c4972b91 2018-05-07 stsp
485 9ba79e04 2018-06-11 stsp while (1) {
486 9ba79e04 2018-06-11 stsp struct got_commit_object *commit;
487 899d86c2 2018-05-10 stsp
488 9ba79e04 2018-06-11 stsp err = got_commit_graph_iter_next(&id, graph);
489 9ba79e04 2018-06-11 stsp if (err) {
490 ecb28ae0 2018-07-16 stsp if (err->code != GOT_ERR_ITER_NEED_MORE)
491 ecb28ae0 2018-07-16 stsp break;
492 ecb28ae0 2018-07-16 stsp if (nqueued >= minqueue) {
493 9ba79e04 2018-06-11 stsp err = NULL;
494 ecb28ae0 2018-07-16 stsp break;
495 ecb28ae0 2018-07-16 stsp }
496 ecb28ae0 2018-07-16 stsp err = got_commit_graph_fetch_commits(&nfetched,
497 ecb28ae0 2018-07-16 stsp graph, 1, repo);
498 ecb28ae0 2018-07-16 stsp if (err)
499 ecb28ae0 2018-07-16 stsp return err;
500 ecb28ae0 2018-07-16 stsp continue;
501 9ba79e04 2018-06-11 stsp }
502 ecb28ae0 2018-07-16 stsp if (id == NULL)
503 ecb28ae0 2018-07-16 stsp break;
504 899d86c2 2018-05-10 stsp
505 9ba79e04 2018-06-11 stsp err = got_object_open_as_commit(&commit, repo, id);
506 9ba79e04 2018-06-11 stsp if (err)
507 9ba79e04 2018-06-11 stsp break;
508 899d86c2 2018-05-10 stsp
509 c8f60bff 2018-07-23 stsp if (!is_root_path) {
510 ecb28ae0 2018-07-16 stsp struct got_object *obj;
511 ecb28ae0 2018-07-16 stsp struct got_object_qid *pid;
512 ecb28ae0 2018-07-16 stsp int changed = 0;
513 ecb28ae0 2018-07-16 stsp
514 ecb28ae0 2018-07-16 stsp err = got_object_open_by_path(&obj, repo, id, path);
515 ecb28ae0 2018-07-16 stsp if (err) {
516 c8f60bff 2018-07-23 stsp got_object_commit_close(commit);
517 ecb28ae0 2018-07-16 stsp if (err->code == GOT_ERR_NO_OBJ &&
518 ecb28ae0 2018-07-16 stsp (found_obj || !init)) {
519 ecb28ae0 2018-07-16 stsp /* History stops here. */
520 ecb28ae0 2018-07-16 stsp err = got_error(GOT_ERR_ITER_COMPLETED);
521 ecb28ae0 2018-07-16 stsp }
522 ecb28ae0 2018-07-16 stsp break;
523 ecb28ae0 2018-07-16 stsp }
524 ecb28ae0 2018-07-16 stsp found_obj = 1;
525 ecb28ae0 2018-07-16 stsp
526 ecb28ae0 2018-07-16 stsp pid = SIMPLEQ_FIRST(&commit->parent_ids);
527 ecb28ae0 2018-07-16 stsp if (pid != NULL) {
528 ecb28ae0 2018-07-16 stsp struct got_object *pobj;
529 ecb28ae0 2018-07-16 stsp err = got_object_open_by_path(&pobj, repo,
530 ecb28ae0 2018-07-16 stsp pid->id, path);
531 ecb28ae0 2018-07-16 stsp if (err) {
532 ecb28ae0 2018-07-16 stsp if (err->code != GOT_ERR_NO_OBJ) {
533 ecb28ae0 2018-07-16 stsp got_object_close(obj);
534 c8f60bff 2018-07-23 stsp got_object_commit_close(commit);
535 ecb28ae0 2018-07-16 stsp break;
536 ecb28ae0 2018-07-16 stsp }
537 ecb28ae0 2018-07-16 stsp err = NULL;
538 ecb28ae0 2018-07-16 stsp changed = 1;
539 ecb28ae0 2018-07-16 stsp } else {
540 c2f16475 2018-07-23 stsp struct got_object_id *id, *pid;
541 c2f16475 2018-07-23 stsp id = got_object_get_id(obj);
542 c2f16475 2018-07-23 stsp if (id == NULL) {
543 c2f16475 2018-07-23 stsp err = got_error_from_errno();
544 c8f60bff 2018-07-23 stsp got_object_close(obj);
545 c8f60bff 2018-07-23 stsp got_object_close(pobj);
546 c2f16475 2018-07-23 stsp break;
547 c2f16475 2018-07-23 stsp }
548 c2f16475 2018-07-23 stsp pid = got_object_get_id(pobj);
549 c2f16475 2018-07-23 stsp if (pid == NULL) {
550 c2f16475 2018-07-23 stsp err = got_error_from_errno();
551 c2f16475 2018-07-23 stsp free(id);
552 c8f60bff 2018-07-23 stsp got_object_close(obj);
553 c8f60bff 2018-07-23 stsp got_object_close(pobj);
554 c2f16475 2018-07-23 stsp break;
555 c2f16475 2018-07-23 stsp }
556 c2f16475 2018-07-23 stsp changed =
557 c2f16475 2018-07-23 stsp (got_object_id_cmp(id, pid) != 0);
558 ecb28ae0 2018-07-16 stsp got_object_close(pobj);
559 c2f16475 2018-07-23 stsp free(id);
560 c2f16475 2018-07-23 stsp free(pid);
561 ecb28ae0 2018-07-16 stsp }
562 ecb28ae0 2018-07-16 stsp }
563 c8f60bff 2018-07-23 stsp got_object_close(obj);
564 ecb28ae0 2018-07-16 stsp if (!changed) {
565 ecb28ae0 2018-07-16 stsp got_object_commit_close(commit);
566 ecb28ae0 2018-07-16 stsp continue;
567 ecb28ae0 2018-07-16 stsp }
568 ecb28ae0 2018-07-16 stsp }
569 ecb28ae0 2018-07-16 stsp
570 9ba79e04 2018-06-11 stsp entry = alloc_commit_queue_entry(commit, id);
571 9ba79e04 2018-06-11 stsp if (entry == NULL) {
572 9ba79e04 2018-06-11 stsp err = got_error_from_errno();
573 9ba79e04 2018-06-11 stsp break;
574 9ba79e04 2018-06-11 stsp }
575 ecb28ae0 2018-07-16 stsp TAILQ_INSERT_TAIL(&commits->head, entry, entry);
576 ecb28ae0 2018-07-16 stsp nqueued++;
577 ecb28ae0 2018-07-16 stsp commits->ncommits++;
578 899d86c2 2018-05-10 stsp }
579 899d86c2 2018-05-10 stsp
580 9ba79e04 2018-06-11 stsp return err;
581 99db9666 2018-05-07 stsp }
582 99db9666 2018-05-07 stsp
583 99db9666 2018-05-07 stsp static const struct got_error *
584 9ba79e04 2018-06-11 stsp fetch_next_commit(struct commit_queue_entry **pentry,
585 9ba79e04 2018-06-11 stsp struct commit_queue_entry *entry, struct commit_queue *commits,
586 ecb28ae0 2018-07-16 stsp struct got_commit_graph *graph, struct got_repository *repo,
587 ecb28ae0 2018-07-16 stsp const char *path)
588 899d86c2 2018-05-10 stsp {
589 899d86c2 2018-05-10 stsp const struct got_error *err = NULL;
590 899d86c2 2018-05-10 stsp
591 9ba79e04 2018-06-11 stsp *pentry = NULL;
592 899d86c2 2018-05-10 stsp
593 ecb28ae0 2018-07-16 stsp err = queue_commits(graph, commits, entry->id, 1, 0, repo, path);
594 ecb28ae0 2018-07-16 stsp if (err)
595 9ba79e04 2018-06-11 stsp return err;
596 899d86c2 2018-05-10 stsp
597 9ba79e04 2018-06-11 stsp /* Next entry to display should now be available. */
598 9ba79e04 2018-06-11 stsp *pentry = TAILQ_NEXT(entry, entry);
599 9ba79e04 2018-06-11 stsp if (*pentry == NULL)
600 9ba79e04 2018-06-11 stsp return got_error(GOT_ERR_NO_OBJ);
601 899d86c2 2018-05-10 stsp
602 9ba79e04 2018-06-11 stsp return NULL;
603 899d86c2 2018-05-10 stsp }
604 899d86c2 2018-05-10 stsp
605 899d86c2 2018-05-10 stsp static const struct got_error *
606 9ba79e04 2018-06-11 stsp get_head_commit_id(struct got_object_id **head_id, struct got_repository *repo)
607 99db9666 2018-05-07 stsp {
608 9ba79e04 2018-06-11 stsp const struct got_error *err = NULL;
609 9ba79e04 2018-06-11 stsp struct got_reference *head_ref;
610 99db9666 2018-05-07 stsp
611 9ba79e04 2018-06-11 stsp *head_id = NULL;
612 899d86c2 2018-05-10 stsp
613 9ba79e04 2018-06-11 stsp err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
614 99db9666 2018-05-07 stsp if (err)
615 99db9666 2018-05-07 stsp return err;
616 99db9666 2018-05-07 stsp
617 9ba79e04 2018-06-11 stsp err = got_ref_resolve(head_id, repo, head_ref);
618 9ba79e04 2018-06-11 stsp got_ref_close(head_ref);
619 9ba79e04 2018-06-11 stsp if (err) {
620 9ba79e04 2018-06-11 stsp *head_id = NULL;
621 99db9666 2018-05-07 stsp return err;
622 0553a4e3 2018-04-30 stsp }
623 80ddbec8 2018-04-29 stsp
624 9ba79e04 2018-06-11 stsp return NULL;
625 0553a4e3 2018-04-30 stsp }
626 0553a4e3 2018-04-30 stsp
627 0553a4e3 2018-04-30 stsp static const struct got_error *
628 2814baeb 2018-08-01 stsp draw_commits(struct tog_view *view, struct commit_queue_entry **last,
629 ecb28ae0 2018-07-16 stsp struct commit_queue_entry **selected, struct commit_queue_entry *first,
630 a7f40148 2018-07-18 stsp struct commit_queue *commits, int selected_idx, int limit,
631 a7f40148 2018-07-18 stsp struct got_commit_graph *graph, struct got_repository *repo,
632 a7f40148 2018-07-18 stsp const char *path)
633 0553a4e3 2018-04-30 stsp {
634 0553a4e3 2018-04-30 stsp const struct got_error *err = NULL;
635 0553a4e3 2018-04-30 stsp struct commit_queue_entry *entry;
636 ecb28ae0 2018-07-16 stsp int ncommits, width;
637 867c6645 2018-07-10 stsp char *id_str, *header;
638 ecb28ae0 2018-07-16 stsp wchar_t *wline;
639 0553a4e3 2018-04-30 stsp
640 e0d42f60 2018-07-22 stsp entry = first;
641 e0d42f60 2018-07-22 stsp ncommits = 0;
642 e0d42f60 2018-07-22 stsp while (entry) {
643 e0d42f60 2018-07-22 stsp if (ncommits == selected_idx) {
644 e0d42f60 2018-07-22 stsp *selected = entry;
645 e0d42f60 2018-07-22 stsp break;
646 e0d42f60 2018-07-22 stsp }
647 e0d42f60 2018-07-22 stsp entry = TAILQ_NEXT(entry, entry);
648 e0d42f60 2018-07-22 stsp ncommits++;
649 e0d42f60 2018-07-22 stsp }
650 e0d42f60 2018-07-22 stsp
651 867c6645 2018-07-10 stsp err = got_object_id_str(&id_str, (*selected)->id);
652 867c6645 2018-07-10 stsp if (err)
653 867c6645 2018-07-10 stsp return err;
654 867c6645 2018-07-10 stsp
655 c3ba6f36 2018-08-01 stsp if (path && strcmp(path, "/") != 0) {
656 ecb28ae0 2018-07-16 stsp if (asprintf(&header, "commit: %s [%s]", id_str, path) == -1) {
657 ecb28ae0 2018-07-16 stsp err = got_error_from_errno();
658 ecb28ae0 2018-07-16 stsp free(id_str);
659 ecb28ae0 2018-07-16 stsp return err;
660 ecb28ae0 2018-07-16 stsp }
661 ecb28ae0 2018-07-16 stsp } else if (asprintf(&header, "commit: %s", id_str) == -1) {
662 867c6645 2018-07-10 stsp err = got_error_from_errno();
663 867c6645 2018-07-10 stsp free(id_str);
664 867c6645 2018-07-10 stsp return err;
665 867c6645 2018-07-10 stsp }
666 ecb28ae0 2018-07-16 stsp free(id_str);
667 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, header, view->ncols);
668 ecb28ae0 2018-07-16 stsp if (err) {
669 ecb28ae0 2018-07-16 stsp free(header);
670 ecb28ae0 2018-07-16 stsp return err;
671 ecb28ae0 2018-07-16 stsp }
672 ecb28ae0 2018-07-16 stsp free(header);
673 867c6645 2018-07-10 stsp
674 2814baeb 2018-08-01 stsp werase(view->window);
675 867c6645 2018-07-10 stsp
676 2814baeb 2018-08-01 stsp waddwstr(view->window, wline);
677 f7d12f7e 2018-08-01 stsp if (width < view->ncols)
678 2814baeb 2018-08-01 stsp waddch(view->window, '\n');
679 ecb28ae0 2018-07-16 stsp free(wline);
680 ecb28ae0 2018-07-16 stsp if (limit <= 1)
681 ecb28ae0 2018-07-16 stsp return NULL;
682 0553a4e3 2018-04-30 stsp
683 899d86c2 2018-05-10 stsp entry = first;
684 899d86c2 2018-05-10 stsp *last = first;
685 867c6645 2018-07-10 stsp ncommits = 0;
686 899d86c2 2018-05-10 stsp while (entry) {
687 ecb28ae0 2018-07-16 stsp if (ncommits >= limit - 1)
688 899d86c2 2018-05-10 stsp break;
689 e0d42f60 2018-07-22 stsp if (ncommits == selected_idx)
690 2814baeb 2018-08-01 stsp wstandout(view->window);
691 2814baeb 2018-08-01 stsp err = draw_commit(view, entry->commit, entry->id);
692 cd0acaa7 2018-05-20 stsp if (ncommits == selected_idx)
693 2814baeb 2018-08-01 stsp wstandend(view->window);
694 0553a4e3 2018-04-30 stsp if (err)
695 0553a4e3 2018-04-30 stsp break;
696 0553a4e3 2018-04-30 stsp ncommits++;
697 899d86c2 2018-05-10 stsp *last = entry;
698 a7f40148 2018-07-18 stsp if (entry == TAILQ_LAST(&commits->head, commit_queue_head)) {
699 a7f40148 2018-07-18 stsp err = queue_commits(graph, commits, entry->id, 1,
700 a7f40148 2018-07-18 stsp 0, repo, path);
701 a7f40148 2018-07-18 stsp if (err) {
702 a7f40148 2018-07-18 stsp if (err->code != GOT_ERR_ITER_COMPLETED)
703 a7f40148 2018-07-18 stsp return err;
704 a7f40148 2018-07-18 stsp err = NULL;
705 a7f40148 2018-07-18 stsp }
706 a7f40148 2018-07-18 stsp }
707 899d86c2 2018-05-10 stsp entry = TAILQ_NEXT(entry, entry);
708 80ddbec8 2018-04-29 stsp }
709 80ddbec8 2018-04-29 stsp
710 80ddbec8 2018-04-29 stsp update_panels();
711 80ddbec8 2018-04-29 stsp doupdate();
712 0553a4e3 2018-04-30 stsp
713 80ddbec8 2018-04-29 stsp return err;
714 9f7d7167 2018-04-29 stsp }
715 07b55e75 2018-05-10 stsp
716 07b55e75 2018-05-10 stsp static void
717 16482c3b 2018-05-20 stsp scroll_up(struct commit_queue_entry **first_displayed_entry, int maxscroll,
718 07b55e75 2018-05-10 stsp struct commit_queue *commits)
719 07b55e75 2018-05-10 stsp {
720 07b55e75 2018-05-10 stsp struct commit_queue_entry *entry;
721 07b55e75 2018-05-10 stsp int nscrolled = 0;
722 07b55e75 2018-05-10 stsp
723 ecb28ae0 2018-07-16 stsp entry = TAILQ_FIRST(&commits->head);
724 07b55e75 2018-05-10 stsp if (*first_displayed_entry == entry)
725 07b55e75 2018-05-10 stsp return;
726 9f7d7167 2018-04-29 stsp
727 07b55e75 2018-05-10 stsp entry = *first_displayed_entry;
728 16482c3b 2018-05-20 stsp while (entry && nscrolled < maxscroll) {
729 ecb28ae0 2018-07-16 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
730 07b55e75 2018-05-10 stsp if (entry) {
731 07b55e75 2018-05-10 stsp *first_displayed_entry = entry;
732 07b55e75 2018-05-10 stsp nscrolled++;
733 07b55e75 2018-05-10 stsp }
734 07b55e75 2018-05-10 stsp }
735 aa075928 2018-05-10 stsp }
736 aa075928 2018-05-10 stsp
737 aa075928 2018-05-10 stsp static const struct got_error *
738 16482c3b 2018-05-20 stsp scroll_down(struct commit_queue_entry **first_displayed_entry, int maxscroll,
739 aa075928 2018-05-10 stsp struct commit_queue_entry *last_displayed_entry,
740 9ba79e04 2018-06-11 stsp struct commit_queue *commits, struct got_commit_graph *graph,
741 ecb28ae0 2018-07-16 stsp struct got_repository *repo, const char *path)
742 aa075928 2018-05-10 stsp {
743 aa075928 2018-05-10 stsp const struct got_error *err = NULL;
744 dd0a52c1 2018-05-20 stsp struct commit_queue_entry *pentry;
745 aa075928 2018-05-10 stsp int nscrolled = 0;
746 aa075928 2018-05-10 stsp
747 aa075928 2018-05-10 stsp do {
748 dd0a52c1 2018-05-20 stsp pentry = TAILQ_NEXT(last_displayed_entry, entry);
749 aa075928 2018-05-10 stsp if (pentry == NULL) {
750 9ba79e04 2018-06-11 stsp err = fetch_next_commit(&pentry, last_displayed_entry,
751 ecb28ae0 2018-07-16 stsp commits, graph, repo, path);
752 9a6bf2a5 2018-05-20 stsp if (err || pentry == NULL)
753 aa075928 2018-05-10 stsp break;
754 aa075928 2018-05-10 stsp }
755 dd0a52c1 2018-05-20 stsp last_displayed_entry = pentry;
756 aa075928 2018-05-10 stsp
757 dd0a52c1 2018-05-20 stsp pentry = TAILQ_NEXT(*first_displayed_entry, entry);
758 dd0a52c1 2018-05-20 stsp if (pentry == NULL)
759 dd0a52c1 2018-05-20 stsp break;
760 aa075928 2018-05-10 stsp *first_displayed_entry = pentry;
761 16482c3b 2018-05-20 stsp } while (++nscrolled < maxscroll);
762 aa075928 2018-05-10 stsp
763 dd0a52c1 2018-05-20 stsp return err;
764 07b55e75 2018-05-10 stsp }
765 4a7f7875 2018-05-10 stsp
766 cd0acaa7 2018-05-20 stsp static const struct got_error *
767 6d0fee91 2018-08-01 stsp show_commit(struct tog_view *parent_view, struct commit_queue_entry *entry,
768 6d0fee91 2018-08-01 stsp struct got_repository *repo)
769 cd0acaa7 2018-05-20 stsp {
770 cd0acaa7 2018-05-20 stsp const struct got_error *err;
771 cd0acaa7 2018-05-20 stsp struct got_object *obj1 = NULL, *obj2 = NULL;
772 9ba79e04 2018-06-11 stsp struct got_object_qid *parent_id;
773 ea5e7bb5 2018-08-01 stsp struct tog_view *view;
774 cd0acaa7 2018-05-20 stsp
775 cd0acaa7 2018-05-20 stsp err = got_object_open(&obj2, repo, entry->id);
776 cd0acaa7 2018-05-20 stsp if (err)
777 cd0acaa7 2018-05-20 stsp return err;
778 cd0acaa7 2018-05-20 stsp
779 9ba79e04 2018-06-11 stsp parent_id = SIMPLEQ_FIRST(&entry->commit->parent_ids);
780 9ba79e04 2018-06-11 stsp if (parent_id) {
781 9ba79e04 2018-06-11 stsp err = got_object_open(&obj1, repo, parent_id->id);
782 cd0acaa7 2018-05-20 stsp if (err)
783 cd0acaa7 2018-05-20 stsp goto done;
784 cd0acaa7 2018-05-20 stsp }
785 cd0acaa7 2018-05-20 stsp
786 b3665f43 2018-08-04 stsp view = view_open(0, 0, 0, 0, parent_view, TOG_VIEW_DIFF);
787 ea5e7bb5 2018-08-01 stsp if (view == NULL) {
788 ea5e7bb5 2018-08-01 stsp err = got_error_from_errno();
789 ea5e7bb5 2018-08-01 stsp goto done;
790 ea5e7bb5 2018-08-01 stsp }
791 ea5e7bb5 2018-08-01 stsp
792 5dc9f4bc 2018-08-04 stsp err = open_diff_view(view, obj1, obj2, repo);
793 5dc9f4bc 2018-08-04 stsp if (err)
794 5dc9f4bc 2018-08-04 stsp goto done;
795 5dc9f4bc 2018-08-04 stsp err = show_diff_view(view);
796 5dc9f4bc 2018-08-04 stsp close_diff_view(view);
797 96a765a8 2018-08-04 stsp view_close(view);
798 6acd1bd1 2018-08-04 stsp view_show(parent_view);
799 cd0acaa7 2018-05-20 stsp done:
800 cd0acaa7 2018-05-20 stsp if (obj1)
801 cd0acaa7 2018-05-20 stsp got_object_close(obj1);
802 cd0acaa7 2018-05-20 stsp if (obj2)
803 cd0acaa7 2018-05-20 stsp got_object_close(obj2);
804 cd0acaa7 2018-05-20 stsp return err;
805 4a7f7875 2018-05-10 stsp }
806 4a7f7875 2018-05-10 stsp
807 80ddbec8 2018-04-29 stsp static const struct got_error *
808 6d0fee91 2018-08-01 stsp browse_commit(struct tog_view *parent_view, struct commit_queue_entry *entry,
809 6d0fee91 2018-08-01 stsp struct got_repository *repo)
810 9343a5fb 2018-06-23 stsp {
811 9343a5fb 2018-06-23 stsp const struct got_error *err = NULL;
812 9343a5fb 2018-06-23 stsp struct got_tree_object *tree;
813 5221c383 2018-08-01 stsp struct tog_view *view;
814 9343a5fb 2018-06-23 stsp
815 9343a5fb 2018-06-23 stsp err = got_object_open_as_tree(&tree, repo, entry->commit->tree_id);
816 9343a5fb 2018-06-23 stsp if (err)
817 9343a5fb 2018-06-23 stsp return err;
818 9343a5fb 2018-06-23 stsp
819 b3665f43 2018-08-04 stsp view = view_open(0, 0, 0, 0, parent_view, TOG_VIEW_TREE);
820 5221c383 2018-08-01 stsp if (view == NULL) {
821 5221c383 2018-08-01 stsp err = got_error_from_errno();
822 5221c383 2018-08-01 stsp goto done;
823 5221c383 2018-08-01 stsp }
824 5221c383 2018-08-01 stsp err = show_tree_view(view, tree, entry->id, repo);
825 96a765a8 2018-08-04 stsp view_close(view);
826 6acd1bd1 2018-08-04 stsp view_show(parent_view);
827 5221c383 2018-08-01 stsp done:
828 9343a5fb 2018-06-23 stsp got_object_tree_close(tree);
829 9343a5fb 2018-06-23 stsp return err;
830 9343a5fb 2018-06-23 stsp }
831 9343a5fb 2018-06-23 stsp
832 9343a5fb 2018-06-23 stsp static const struct got_error *
833 ba4f502b 2018-08-04 stsp open_log_view(struct tog_view *view, struct got_object_id *start_id,
834 04cc582a 2018-08-01 stsp struct got_repository *repo, const char *path)
835 80ddbec8 2018-04-29 stsp {
836 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
837 9ba79e04 2018-06-11 stsp struct got_object_id *head_id = NULL;
838 ba4f502b 2018-08-04 stsp int nfetched;
839 4d7951a5 2018-08-04 stsp struct tog_log_view_state *state = &view->state.log;
840 80ddbec8 2018-04-29 stsp
841 4d7951a5 2018-08-04 stsp err = got_repo_map_path(&state->in_repo_path, repo, path);
842 ecb28ae0 2018-07-16 stsp if (err != NULL)
843 ecb28ae0 2018-07-16 stsp goto done;
844 ecb28ae0 2018-07-16 stsp
845 9ba79e04 2018-06-11 stsp err = get_head_commit_id(&head_id, repo);
846 9ba79e04 2018-06-11 stsp if (err)
847 9ba79e04 2018-06-11 stsp return err;
848 9ba79e04 2018-06-11 stsp
849 034e3b69 2018-07-22 stsp /* The graph contains all commits. */
850 4d7951a5 2018-08-04 stsp err = got_commit_graph_open(&state->graph, head_id, 0, repo);
851 9ba79e04 2018-06-11 stsp if (err)
852 9ba79e04 2018-06-11 stsp goto done;
853 034e3b69 2018-07-22 stsp /* The commit queue contains a subset of commits filtered by path. */
854 4d7951a5 2018-08-04 stsp TAILQ_INIT(&state->commits.head);
855 4d7951a5 2018-08-04 stsp state->commits.ncommits = 0;
856 9ba79e04 2018-06-11 stsp
857 9ba79e04 2018-06-11 stsp /* Populate commit graph with a sufficient number of commits. */
858 ba4f502b 2018-08-04 stsp err = got_commit_graph_fetch_commits_up_to(&nfetched,
859 4d7951a5 2018-08-04 stsp state->graph, start_id, repo);
860 9ba79e04 2018-06-11 stsp if (err)
861 9ba79e04 2018-06-11 stsp goto done;
862 9ba79e04 2018-06-11 stsp
863 9ba79e04 2018-06-11 stsp /*
864 9ba79e04 2018-06-11 stsp * Open the initial batch of commits, sorted in commit graph order.
865 9ba79e04 2018-06-11 stsp * We keep all commits open throughout the lifetime of the log view
866 9ba79e04 2018-06-11 stsp * in order to avoid having to re-fetch commits from disk while
867 9ba79e04 2018-06-11 stsp * updating the display.
868 9ba79e04 2018-06-11 stsp */
869 4d7951a5 2018-08-04 stsp err = queue_commits(state->graph, &state->commits, start_id,
870 4d7951a5 2018-08-04 stsp view->nlines, 1, repo, state->in_repo_path);
871 55198a88 2018-07-16 stsp if (err) {
872 55198a88 2018-07-16 stsp if (err->code != GOT_ERR_ITER_COMPLETED)
873 55198a88 2018-07-16 stsp goto done;
874 55198a88 2018-07-16 stsp err = NULL;
875 2814baeb 2018-08-01 stsp }
876 2814baeb 2018-08-01 stsp
877 4d7951a5 2018-08-04 stsp state->repo = repo;
878 ba4f502b 2018-08-04 stsp done:
879 ba4f502b 2018-08-04 stsp free(head_id);
880 ba4f502b 2018-08-04 stsp return err;
881 ba4f502b 2018-08-04 stsp }
882 ba4f502b 2018-08-04 stsp
883 ba4f502b 2018-08-04 stsp static void close_log_view(struct tog_view *view)
884 ba4f502b 2018-08-04 stsp {
885 4d7951a5 2018-08-04 stsp struct tog_log_view_state *state = &view->state.log;
886 4d7951a5 2018-08-04 stsp
887 4d7951a5 2018-08-04 stsp if (state->graph)
888 4d7951a5 2018-08-04 stsp got_commit_graph_close(state->graph);
889 4d7951a5 2018-08-04 stsp free_commits(&state->commits);
890 4d7951a5 2018-08-04 stsp free(state->in_repo_path);
891 ba4f502b 2018-08-04 stsp }
892 ba4f502b 2018-08-04 stsp
893 ba4f502b 2018-08-04 stsp static const struct got_error *
894 ba4f502b 2018-08-04 stsp show_log_view(struct tog_view *view)
895 ba4f502b 2018-08-04 stsp {
896 ba4f502b 2018-08-04 stsp const struct got_error *err = NULL;
897 ba4f502b 2018-08-04 stsp int ch, done = 0;
898 4d7951a5 2018-08-04 stsp struct tog_log_view_state *state = &view->state.log;
899 ba4f502b 2018-08-04 stsp
900 6acd1bd1 2018-08-04 stsp view_show(view);
901 04cc582a 2018-08-01 stsp
902 4d7951a5 2018-08-04 stsp state->first_displayed_entry =
903 4d7951a5 2018-08-04 stsp TAILQ_FIRST(&state->commits.head);
904 4d7951a5 2018-08-04 stsp state->selected_entry = state->first_displayed_entry;
905 899d86c2 2018-05-10 stsp while (!done) {
906 4d7951a5 2018-08-04 stsp err = draw_commits(view, &state->last_displayed_entry,
907 4d7951a5 2018-08-04 stsp &state->selected_entry, state->first_displayed_entry,
908 4d7951a5 2018-08-04 stsp &state->commits, state->selected, view->nlines,
909 4d7951a5 2018-08-04 stsp state->graph, state->repo, state->in_repo_path);
910 80ddbec8 2018-04-29 stsp if (err)
911 d0f709cb 2018-04-30 stsp goto done;
912 80ddbec8 2018-04-29 stsp
913 80ddbec8 2018-04-29 stsp nodelay(stdscr, FALSE);
914 2814baeb 2018-08-01 stsp ch = wgetch(view->window);
915 f7182337 2018-05-20 stsp nodelay(stdscr, TRUE);
916 80ddbec8 2018-04-29 stsp switch (ch) {
917 31120ada 2018-04-30 stsp case ERR:
918 31120ada 2018-04-30 stsp break;
919 80ddbec8 2018-04-29 stsp case 'q':
920 80ddbec8 2018-04-29 stsp done = 1;
921 80ddbec8 2018-04-29 stsp break;
922 80ddbec8 2018-04-29 stsp case 'k':
923 80ddbec8 2018-04-29 stsp case KEY_UP:
924 4d7951a5 2018-08-04 stsp if (state->selected > 0)
925 4d7951a5 2018-08-04 stsp state->selected--;
926 4d7951a5 2018-08-04 stsp if (state->selected > 0)
927 d91e25cb 2018-05-10 stsp break;
928 4d7951a5 2018-08-04 stsp scroll_up(&state->first_displayed_entry, 1,
929 4d7951a5 2018-08-04 stsp &state->commits);
930 80ddbec8 2018-04-29 stsp break;
931 48531068 2018-05-10 stsp case KEY_PPAGE:
932 4d7951a5 2018-08-04 stsp if (TAILQ_FIRST(&state->commits.head) ==
933 4d7951a5 2018-08-04 stsp state->first_displayed_entry) {
934 4d7951a5 2018-08-04 stsp state->selected = 0;
935 dfc1d240 2018-05-10 stsp break;
936 dfc1d240 2018-05-10 stsp }
937 4d7951a5 2018-08-04 stsp scroll_up(&state->first_displayed_entry,
938 4d7951a5 2018-08-04 stsp view->nlines, &state->commits);
939 48531068 2018-05-10 stsp break;
940 80ddbec8 2018-04-29 stsp case 'j':
941 80ddbec8 2018-04-29 stsp case KEY_DOWN:
942 4d7951a5 2018-08-04 stsp if (state->selected < MIN(view->nlines - 2,
943 4d7951a5 2018-08-04 stsp state->commits.ncommits - 1)) {
944 4d7951a5 2018-08-04 stsp state->selected++;
945 15c91275 2018-05-20 stsp break;
946 8df4052c 2018-05-20 stsp }
947 4d7951a5 2018-08-04 stsp err = scroll_down(&state->first_displayed_entry,
948 4d7951a5 2018-08-04 stsp 1, state->last_displayed_entry,
949 4d7951a5 2018-08-04 stsp &state->commits, state->graph, state->repo,
950 4d7951a5 2018-08-04 stsp state->in_repo_path);
951 ecb28ae0 2018-07-16 stsp if (err) {
952 ecb28ae0 2018-07-16 stsp if (err->code != GOT_ERR_ITER_COMPLETED)
953 ecb28ae0 2018-07-16 stsp goto done;
954 ecb28ae0 2018-07-16 stsp err = NULL;
955 ecb28ae0 2018-07-16 stsp }
956 4a7f7875 2018-05-10 stsp break;
957 ecb28ae0 2018-07-16 stsp case KEY_NPAGE: {
958 c70c5802 2018-08-01 stsp struct commit_queue_entry *first;
959 4d7951a5 2018-08-04 stsp first = state->first_displayed_entry;
960 4d7951a5 2018-08-04 stsp err = scroll_down(&state->first_displayed_entry,
961 4d7951a5 2018-08-04 stsp view->nlines, state->last_displayed_entry,
962 4d7951a5 2018-08-04 stsp &state->commits, state->graph, state->repo,
963 4d7951a5 2018-08-04 stsp state->in_repo_path);
964 4d7951a5 2018-08-04 stsp if (err == NULL)
965 4d7951a5 2018-08-04 stsp break;
966 4d7951a5 2018-08-04 stsp if (err->code != GOT_ERR_ITER_COMPLETED)
967 4d7951a5 2018-08-04 stsp goto done;
968 4d7951a5 2018-08-04 stsp if (first == state->first_displayed_entry &&
969 4d7951a5 2018-08-04 stsp state->selected < MIN(view->nlines - 2,
970 4d7951a5 2018-08-04 stsp state->commits.ncommits - 1)) {
971 4d7951a5 2018-08-04 stsp /* can't scroll further down */
972 4d7951a5 2018-08-04 stsp state->selected = MIN(view->nlines - 2,
973 4d7951a5 2018-08-04 stsp state->commits.ncommits - 1);
974 ecb28ae0 2018-07-16 stsp }
975 4d7951a5 2018-08-04 stsp err = NULL;
976 80ddbec8 2018-04-29 stsp break;
977 ecb28ae0 2018-07-16 stsp }
978 d6df9be4 2018-04-30 stsp case KEY_RESIZE:
979 a41d2007 2018-08-01 stsp err = view_resize(view);
980 a41d2007 2018-08-01 stsp if (err)
981 a41d2007 2018-08-01 stsp goto done;
982 4d7951a5 2018-08-04 stsp if (state->selected > view->nlines - 2)
983 4d7951a5 2018-08-04 stsp state->selected = view->nlines - 2;
984 4d7951a5 2018-08-04 stsp if (state->selected >
985 4d7951a5 2018-08-04 stsp state->commits.ncommits - 1)
986 4d7951a5 2018-08-04 stsp state->selected =
987 4d7951a5 2018-08-04 stsp state->commits.ncommits - 1;
988 cd0acaa7 2018-05-20 stsp break;
989 cd0acaa7 2018-05-20 stsp case KEY_ENTER:
990 cd0acaa7 2018-05-20 stsp case '\r':
991 4d7951a5 2018-08-04 stsp err = show_commit(view, state->selected_entry,
992 4d7951a5 2018-08-04 stsp state->repo);
993 9343a5fb 2018-06-23 stsp if (err)
994 9343a5fb 2018-06-23 stsp goto done;
995 6acd1bd1 2018-08-04 stsp view_show(view);
996 9343a5fb 2018-06-23 stsp break;
997 9343a5fb 2018-06-23 stsp case 't':
998 4d7951a5 2018-08-04 stsp err = browse_commit(view, state->selected_entry,
999 4d7951a5 2018-08-04 stsp state->repo);
1000 cd0acaa7 2018-05-20 stsp if (err)
1001 0d4100bb 2018-06-23 stsp goto done;
1002 6acd1bd1 2018-08-04 stsp view_show(view);
1003 31120ada 2018-04-30 stsp break;
1004 80ddbec8 2018-04-29 stsp default:
1005 80ddbec8 2018-04-29 stsp break;
1006 80ddbec8 2018-04-29 stsp }
1007 899d86c2 2018-05-10 stsp }
1008 80ddbec8 2018-04-29 stsp done:
1009 80ddbec8 2018-04-29 stsp return err;
1010 80ddbec8 2018-04-29 stsp }
1011 80ddbec8 2018-04-29 stsp
1012 4ed7e80c 2018-05-20 stsp static const struct got_error *
1013 9f7d7167 2018-04-29 stsp cmd_log(int argc, char *argv[])
1014 9f7d7167 2018-04-29 stsp {
1015 80ddbec8 2018-04-29 stsp const struct got_error *error;
1016 ecb28ae0 2018-07-16 stsp struct got_repository *repo = NULL;
1017 899d86c2 2018-05-10 stsp struct got_object_id *start_id = NULL;
1018 ecb28ae0 2018-07-16 stsp char *path = NULL, *repo_path = NULL, *cwd = NULL;
1019 80ddbec8 2018-04-29 stsp char *start_commit = NULL;
1020 80ddbec8 2018-04-29 stsp int ch;
1021 04cc582a 2018-08-01 stsp struct tog_view *view;
1022 80ddbec8 2018-04-29 stsp
1023 80ddbec8 2018-04-29 stsp #ifndef PROFILE
1024 80ddbec8 2018-04-29 stsp if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
1025 80ddbec8 2018-04-29 stsp err(1, "pledge");
1026 80ddbec8 2018-04-29 stsp #endif
1027 80ddbec8 2018-04-29 stsp
1028 ecb28ae0 2018-07-16 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1029 80ddbec8 2018-04-29 stsp switch (ch) {
1030 80ddbec8 2018-04-29 stsp case 'c':
1031 80ddbec8 2018-04-29 stsp start_commit = optarg;
1032 80ddbec8 2018-04-29 stsp break;
1033 ecb28ae0 2018-07-16 stsp case 'r':
1034 ecb28ae0 2018-07-16 stsp repo_path = realpath(optarg, NULL);
1035 ecb28ae0 2018-07-16 stsp if (repo_path == NULL)
1036 ecb28ae0 2018-07-16 stsp err(1, "-r option");
1037 ecb28ae0 2018-07-16 stsp break;
1038 80ddbec8 2018-04-29 stsp default:
1039 80ddbec8 2018-04-29 stsp usage();
1040 80ddbec8 2018-04-29 stsp /* NOTREACHED */
1041 80ddbec8 2018-04-29 stsp }
1042 80ddbec8 2018-04-29 stsp }
1043 80ddbec8 2018-04-29 stsp
1044 80ddbec8 2018-04-29 stsp argc -= optind;
1045 80ddbec8 2018-04-29 stsp argv += optind;
1046 80ddbec8 2018-04-29 stsp
1047 ecb28ae0 2018-07-16 stsp if (argc == 0)
1048 ecb28ae0 2018-07-16 stsp path = strdup("");
1049 ecb28ae0 2018-07-16 stsp else if (argc == 1)
1050 ecb28ae0 2018-07-16 stsp path = strdup(argv[0]);
1051 ecb28ae0 2018-07-16 stsp else
1052 80ddbec8 2018-04-29 stsp usage_log();
1053 ecb28ae0 2018-07-16 stsp if (path == NULL)
1054 ecb28ae0 2018-07-16 stsp return got_error_from_errno();
1055 80ddbec8 2018-04-29 stsp
1056 ecb28ae0 2018-07-16 stsp cwd = getcwd(NULL, 0);
1057 ecb28ae0 2018-07-16 stsp if (cwd == NULL) {
1058 ecb28ae0 2018-07-16 stsp error = got_error_from_errno();
1059 ecb28ae0 2018-07-16 stsp goto done;
1060 ecb28ae0 2018-07-16 stsp }
1061 ecb28ae0 2018-07-16 stsp if (repo_path == NULL) {
1062 ecb28ae0 2018-07-16 stsp repo_path = strdup(cwd);
1063 ecb28ae0 2018-07-16 stsp if (repo_path == NULL) {
1064 ecb28ae0 2018-07-16 stsp error = got_error_from_errno();
1065 ecb28ae0 2018-07-16 stsp goto done;
1066 ecb28ae0 2018-07-16 stsp }
1067 ecb28ae0 2018-07-16 stsp }
1068 ecb28ae0 2018-07-16 stsp
1069 80ddbec8 2018-04-29 stsp error = got_repo_open(&repo, repo_path);
1070 80ddbec8 2018-04-29 stsp if (error != NULL)
1071 ecb28ae0 2018-07-16 stsp goto done;
1072 80ddbec8 2018-04-29 stsp
1073 80ddbec8 2018-04-29 stsp if (start_commit == NULL) {
1074 899d86c2 2018-05-10 stsp error = get_head_commit_id(&start_id, repo);
1075 80ddbec8 2018-04-29 stsp if (error != NULL)
1076 ecb28ae0 2018-07-16 stsp goto done;
1077 80ddbec8 2018-04-29 stsp } else {
1078 80ddbec8 2018-04-29 stsp struct got_object *obj;
1079 80ddbec8 2018-04-29 stsp error = got_object_open_by_id_str(&obj, repo, start_commit);
1080 80ddbec8 2018-04-29 stsp if (error == NULL) {
1081 899d86c2 2018-05-10 stsp start_id = got_object_get_id(obj);
1082 899d86c2 2018-05-10 stsp if (start_id == NULL)
1083 80ddbec8 2018-04-29 stsp error = got_error_from_errno();
1084 ecb28ae0 2018-07-16 stsp goto done;
1085 80ddbec8 2018-04-29 stsp }
1086 80ddbec8 2018-04-29 stsp }
1087 80ddbec8 2018-04-29 stsp if (error != NULL)
1088 ecb28ae0 2018-07-16 stsp goto done;
1089 ecb28ae0 2018-07-16 stsp
1090 b3665f43 2018-08-04 stsp view = view_open(0, 0, 0, 0, NULL, TOG_VIEW_LOG);
1091 04cc582a 2018-08-01 stsp if (view == NULL) {
1092 04cc582a 2018-08-01 stsp error = got_error_from_errno();
1093 04cc582a 2018-08-01 stsp goto done;
1094 04cc582a 2018-08-01 stsp }
1095 ba4f502b 2018-08-04 stsp error = open_log_view(view, start_id, repo, path);
1096 ba4f502b 2018-08-04 stsp if (error)
1097 ba4f502b 2018-08-04 stsp goto done;
1098 ba4f502b 2018-08-04 stsp error = show_log_view(view);
1099 ba4f502b 2018-08-04 stsp close_log_view(view);
1100 96a765a8 2018-08-04 stsp view_close(view);
1101 ecb28ae0 2018-07-16 stsp done:
1102 ecb28ae0 2018-07-16 stsp free(repo_path);
1103 ecb28ae0 2018-07-16 stsp free(cwd);
1104 ecb28ae0 2018-07-16 stsp free(path);
1105 899d86c2 2018-05-10 stsp free(start_id);
1106 ecb28ae0 2018-07-16 stsp if (repo)
1107 ecb28ae0 2018-07-16 stsp got_repo_close(repo);
1108 80ddbec8 2018-04-29 stsp return error;
1109 9f7d7167 2018-04-29 stsp }
1110 9f7d7167 2018-04-29 stsp
1111 4ed7e80c 2018-05-20 stsp __dead static void
1112 9f7d7167 2018-04-29 stsp usage_diff(void)
1113 9f7d7167 2018-04-29 stsp {
1114 80ddbec8 2018-04-29 stsp endwin();
1115 9f7d7167 2018-04-29 stsp fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
1116 9f7d7167 2018-04-29 stsp getprogname());
1117 9f7d7167 2018-04-29 stsp exit(1);
1118 b304db33 2018-05-20 stsp }
1119 b304db33 2018-05-20 stsp
1120 b304db33 2018-05-20 stsp static char *
1121 b304db33 2018-05-20 stsp parse_next_line(FILE *f, size_t *len)
1122 b304db33 2018-05-20 stsp {
1123 b304db33 2018-05-20 stsp char *line;
1124 b304db33 2018-05-20 stsp size_t linelen;
1125 b304db33 2018-05-20 stsp size_t lineno;
1126 b304db33 2018-05-20 stsp const char delim[3] = { '\0', '\0', '\0'};
1127 b304db33 2018-05-20 stsp
1128 b304db33 2018-05-20 stsp line = fparseln(f, &linelen, &lineno, delim, 0);
1129 b304db33 2018-05-20 stsp if (len)
1130 b304db33 2018-05-20 stsp *len = linelen;
1131 b304db33 2018-05-20 stsp return line;
1132 26ed57b2 2018-05-19 stsp }
1133 26ed57b2 2018-05-19 stsp
1134 4ed7e80c 2018-05-20 stsp static const struct got_error *
1135 f7d12f7e 2018-08-01 stsp draw_file(struct tog_view *view, FILE *f, int *first_displayed_line,
1136 a70480e0 2018-06-23 stsp int *last_displayed_line, int *eof, int max_lines)
1137 26ed57b2 2018-05-19 stsp {
1138 61e69b96 2018-05-20 stsp const struct got_error *err;
1139 26ed57b2 2018-05-19 stsp int nlines = 0, nprinted = 0;
1140 b304db33 2018-05-20 stsp char *line;
1141 b304db33 2018-05-20 stsp size_t len;
1142 61e69b96 2018-05-20 stsp wchar_t *wline;
1143 e0b650dd 2018-05-20 stsp int width;
1144 26ed57b2 2018-05-19 stsp
1145 26ed57b2 2018-05-19 stsp rewind(f);
1146 f7d12f7e 2018-08-01 stsp werase(view->window);
1147 26ed57b2 2018-05-19 stsp
1148 26ed57b2 2018-05-19 stsp *eof = 0;
1149 26ed57b2 2018-05-19 stsp while (nprinted < max_lines) {
1150 b304db33 2018-05-20 stsp line = parse_next_line(f, &len);
1151 26ed57b2 2018-05-19 stsp if (line == NULL) {
1152 26ed57b2 2018-05-19 stsp *eof = 1;
1153 26ed57b2 2018-05-19 stsp break;
1154 26ed57b2 2018-05-19 stsp }
1155 26ed57b2 2018-05-19 stsp if (++nlines < *first_displayed_line) {
1156 26ed57b2 2018-05-19 stsp free(line);
1157 26ed57b2 2018-05-19 stsp continue;
1158 26ed57b2 2018-05-19 stsp }
1159 26ed57b2 2018-05-19 stsp
1160 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, line, view->ncols);
1161 61e69b96 2018-05-20 stsp if (err) {
1162 61e69b96 2018-05-20 stsp free(line);
1163 2550e4c3 2018-07-13 stsp free(wline);
1164 61e69b96 2018-05-20 stsp return err;
1165 61e69b96 2018-05-20 stsp }
1166 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
1167 f7d12f7e 2018-08-01 stsp if (width < view->ncols)
1168 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
1169 26ed57b2 2018-05-19 stsp if (++nprinted == 1)
1170 26ed57b2 2018-05-19 stsp *first_displayed_line = nlines;
1171 26ed57b2 2018-05-19 stsp free(line);
1172 2550e4c3 2018-07-13 stsp free(wline);
1173 2550e4c3 2018-07-13 stsp wline = NULL;
1174 26ed57b2 2018-05-19 stsp }
1175 26ed57b2 2018-05-19 stsp *last_displayed_line = nlines;
1176 26ed57b2 2018-05-19 stsp
1177 26ed57b2 2018-05-19 stsp update_panels();
1178 26ed57b2 2018-05-19 stsp doupdate();
1179 26ed57b2 2018-05-19 stsp
1180 26ed57b2 2018-05-19 stsp return NULL;
1181 9f7d7167 2018-04-29 stsp }
1182 9f7d7167 2018-04-29 stsp
1183 4ed7e80c 2018-05-20 stsp static const struct got_error *
1184 5dc9f4bc 2018-08-04 stsp open_diff_view(struct tog_view *view, struct got_object *obj1,
1185 ea5e7bb5 2018-08-01 stsp struct got_object *obj2, struct got_repository *repo)
1186 26ed57b2 2018-05-19 stsp {
1187 26ed57b2 2018-05-19 stsp const struct got_error *err;
1188 26ed57b2 2018-05-19 stsp FILE *f;
1189 26ed57b2 2018-05-19 stsp
1190 cd0acaa7 2018-05-20 stsp if (obj1 != NULL && obj2 != NULL &&
1191 cd0acaa7 2018-05-20 stsp got_object_get_type(obj1) != got_object_get_type(obj2))
1192 26ed57b2 2018-05-19 stsp return got_error(GOT_ERR_OBJ_TYPE);
1193 26ed57b2 2018-05-19 stsp
1194 511a516b 2018-05-19 stsp f = got_opentemp();
1195 26ed57b2 2018-05-19 stsp if (f == NULL)
1196 26ed57b2 2018-05-19 stsp return got_error_from_errno();
1197 26ed57b2 2018-05-19 stsp
1198 cd0acaa7 2018-05-20 stsp switch (got_object_get_type(obj1 ? obj1 : obj2)) {
1199 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_BLOB:
1200 11528a82 2018-05-19 stsp err = got_diff_objects_as_blobs(obj1, obj2, repo, f);
1201 26ed57b2 2018-05-19 stsp break;
1202 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_TREE:
1203 11528a82 2018-05-19 stsp err = got_diff_objects_as_trees(obj1, obj2, repo, f);
1204 26ed57b2 2018-05-19 stsp break;
1205 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_COMMIT:
1206 11528a82 2018-05-19 stsp err = got_diff_objects_as_commits(obj1, obj2, repo, f);
1207 26ed57b2 2018-05-19 stsp break;
1208 26ed57b2 2018-05-19 stsp default:
1209 26ed57b2 2018-05-19 stsp return got_error(GOT_ERR_OBJ_TYPE);
1210 26ed57b2 2018-05-19 stsp }
1211 26ed57b2 2018-05-19 stsp
1212 26ed57b2 2018-05-19 stsp fflush(f);
1213 5dc9f4bc 2018-08-04 stsp
1214 5dc9f4bc 2018-08-04 stsp view->state.diff.f = f;
1215 5dc9f4bc 2018-08-04 stsp view->state.diff.first_displayed_line = 1;
1216 5dc9f4bc 2018-08-04 stsp view->state.diff.last_displayed_line = view->nlines;
1217 5dc9f4bc 2018-08-04 stsp
1218 5dc9f4bc 2018-08-04 stsp return NULL;
1219 5dc9f4bc 2018-08-04 stsp }
1220 5dc9f4bc 2018-08-04 stsp
1221 5dc9f4bc 2018-08-04 stsp static void
1222 5dc9f4bc 2018-08-04 stsp close_diff_view(struct tog_view *view)
1223 5dc9f4bc 2018-08-04 stsp {
1224 5dc9f4bc 2018-08-04 stsp fclose(view->state.diff.f);
1225 5dc9f4bc 2018-08-04 stsp }
1226 5dc9f4bc 2018-08-04 stsp
1227 5dc9f4bc 2018-08-04 stsp static const struct got_error *
1228 5dc9f4bc 2018-08-04 stsp show_diff_view(struct tog_view *view)
1229 5dc9f4bc 2018-08-04 stsp {
1230 5dc9f4bc 2018-08-04 stsp const struct got_error *err = NULL;
1231 5dc9f4bc 2018-08-04 stsp int ch, done = 0;
1232 5dc9f4bc 2018-08-04 stsp int eof, i;
1233 c90732a0 2018-08-04 stsp struct tog_diff_view_state *state = &view->state.diff;
1234 26ed57b2 2018-05-19 stsp
1235 6acd1bd1 2018-08-04 stsp view_show(view);
1236 26ed57b2 2018-05-19 stsp
1237 26ed57b2 2018-05-19 stsp while (!done) {
1238 c90732a0 2018-08-04 stsp err = draw_file(view, state->f, &state->first_displayed_line,
1239 c90732a0 2018-08-04 stsp &state->last_displayed_line, &eof, view->nlines);
1240 26ed57b2 2018-05-19 stsp if (err)
1241 26ed57b2 2018-05-19 stsp break;
1242 26ed57b2 2018-05-19 stsp nodelay(stdscr, FALSE);
1243 ea5e7bb5 2018-08-01 stsp ch = wgetch(view->window);
1244 f7182337 2018-05-20 stsp nodelay(stdscr, TRUE);
1245 26ed57b2 2018-05-19 stsp switch (ch) {
1246 26ed57b2 2018-05-19 stsp case 'q':
1247 26ed57b2 2018-05-19 stsp done = 1;
1248 26ed57b2 2018-05-19 stsp break;
1249 26ed57b2 2018-05-19 stsp case 'k':
1250 26ed57b2 2018-05-19 stsp case KEY_UP:
1251 c90732a0 2018-08-04 stsp if (state->first_displayed_line > 1)
1252 c90732a0 2018-08-04 stsp state->first_displayed_line--;
1253 b304db33 2018-05-20 stsp break;
1254 b304db33 2018-05-20 stsp case KEY_PPAGE:
1255 c46b9352 2018-07-12 stsp case KEY_BACKSPACE:
1256 b304db33 2018-05-20 stsp i = 0;
1257 f7d12f7e 2018-08-01 stsp while (i++ < view->nlines - 1 &&
1258 c90732a0 2018-08-04 stsp state->first_displayed_line > 1)
1259 c90732a0 2018-08-04 stsp state->first_displayed_line--;
1260 26ed57b2 2018-05-19 stsp break;
1261 26ed57b2 2018-05-19 stsp case 'j':
1262 26ed57b2 2018-05-19 stsp case KEY_DOWN:
1263 26ed57b2 2018-05-19 stsp if (!eof)
1264 c90732a0 2018-08-04 stsp state->first_displayed_line++;
1265 26ed57b2 2018-05-19 stsp break;
1266 b304db33 2018-05-20 stsp case KEY_NPAGE:
1267 75e48879 2018-05-20 stsp case ' ':
1268 b304db33 2018-05-20 stsp i = 0;
1269 f7d12f7e 2018-08-01 stsp while (!eof && i++ < view->nlines - 1) {
1270 5dc9f4bc 2018-08-04 stsp char *line = parse_next_line(
1271 c90732a0 2018-08-04 stsp state->f, NULL);
1272 c90732a0 2018-08-04 stsp state->first_displayed_line++;
1273 b304db33 2018-05-20 stsp if (line == NULL)
1274 b304db33 2018-05-20 stsp break;
1275 b304db33 2018-05-20 stsp }
1276 b304db33 2018-05-20 stsp break;
1277 82357c0a 2018-08-01 stsp case KEY_RESIZE:
1278 a41d2007 2018-08-01 stsp err = view_resize(view);
1279 a41d2007 2018-08-01 stsp if (err)
1280 a41d2007 2018-08-01 stsp goto done;
1281 82357c0a 2018-08-01 stsp break;
1282 26ed57b2 2018-05-19 stsp default:
1283 26ed57b2 2018-05-19 stsp break;
1284 26ed57b2 2018-05-19 stsp }
1285 26ed57b2 2018-05-19 stsp }
1286 a41d2007 2018-08-01 stsp done:
1287 26ed57b2 2018-05-19 stsp return err;
1288 26ed57b2 2018-05-19 stsp }
1289 26ed57b2 2018-05-19 stsp
1290 4ed7e80c 2018-05-20 stsp static const struct got_error *
1291 9f7d7167 2018-04-29 stsp cmd_diff(int argc, char *argv[])
1292 9f7d7167 2018-04-29 stsp {
1293 26ed57b2 2018-05-19 stsp const struct got_error *error = NULL;
1294 26ed57b2 2018-05-19 stsp struct got_repository *repo = NULL;
1295 26ed57b2 2018-05-19 stsp struct got_object *obj1 = NULL, *obj2 = NULL;
1296 26ed57b2 2018-05-19 stsp char *repo_path = NULL;
1297 26ed57b2 2018-05-19 stsp char *obj_id_str1 = NULL, *obj_id_str2 = NULL;
1298 26ed57b2 2018-05-19 stsp int ch;
1299 ea5e7bb5 2018-08-01 stsp struct tog_view *view;
1300 26ed57b2 2018-05-19 stsp
1301 26ed57b2 2018-05-19 stsp #ifndef PROFILE
1302 26ed57b2 2018-05-19 stsp if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
1303 26ed57b2 2018-05-19 stsp err(1, "pledge");
1304 26ed57b2 2018-05-19 stsp #endif
1305 26ed57b2 2018-05-19 stsp
1306 26ed57b2 2018-05-19 stsp while ((ch = getopt(argc, argv, "")) != -1) {
1307 26ed57b2 2018-05-19 stsp switch (ch) {
1308 26ed57b2 2018-05-19 stsp default:
1309 26ed57b2 2018-05-19 stsp usage();
1310 26ed57b2 2018-05-19 stsp /* NOTREACHED */
1311 26ed57b2 2018-05-19 stsp }
1312 26ed57b2 2018-05-19 stsp }
1313 26ed57b2 2018-05-19 stsp
1314 26ed57b2 2018-05-19 stsp argc -= optind;
1315 26ed57b2 2018-05-19 stsp argv += optind;
1316 26ed57b2 2018-05-19 stsp
1317 26ed57b2 2018-05-19 stsp if (argc == 0) {
1318 26ed57b2 2018-05-19 stsp usage_diff(); /* TODO show local worktree changes */
1319 26ed57b2 2018-05-19 stsp } else if (argc == 2) {
1320 26ed57b2 2018-05-19 stsp repo_path = getcwd(NULL, 0);
1321 26ed57b2 2018-05-19 stsp if (repo_path == NULL)
1322 26ed57b2 2018-05-19 stsp return got_error_from_errno();
1323 26ed57b2 2018-05-19 stsp obj_id_str1 = argv[0];
1324 26ed57b2 2018-05-19 stsp obj_id_str2 = argv[1];
1325 26ed57b2 2018-05-19 stsp } else if (argc == 3) {
1326 26ed57b2 2018-05-19 stsp repo_path = realpath(argv[0], NULL);
1327 26ed57b2 2018-05-19 stsp if (repo_path == NULL)
1328 26ed57b2 2018-05-19 stsp return got_error_from_errno();
1329 26ed57b2 2018-05-19 stsp obj_id_str1 = argv[1];
1330 26ed57b2 2018-05-19 stsp obj_id_str2 = argv[2];
1331 26ed57b2 2018-05-19 stsp } else
1332 26ed57b2 2018-05-19 stsp usage_diff();
1333 26ed57b2 2018-05-19 stsp
1334 26ed57b2 2018-05-19 stsp error = got_repo_open(&repo, repo_path);
1335 26ed57b2 2018-05-19 stsp free(repo_path);
1336 26ed57b2 2018-05-19 stsp if (error)
1337 26ed57b2 2018-05-19 stsp goto done;
1338 26ed57b2 2018-05-19 stsp
1339 26ed57b2 2018-05-19 stsp error = got_object_open_by_id_str(&obj1, repo, obj_id_str1);
1340 26ed57b2 2018-05-19 stsp if (error)
1341 26ed57b2 2018-05-19 stsp goto done;
1342 26ed57b2 2018-05-19 stsp
1343 26ed57b2 2018-05-19 stsp error = got_object_open_by_id_str(&obj2, repo, obj_id_str2);
1344 26ed57b2 2018-05-19 stsp if (error)
1345 26ed57b2 2018-05-19 stsp goto done;
1346 26ed57b2 2018-05-19 stsp
1347 b3665f43 2018-08-04 stsp view = view_open(0, 0, 0, 0, NULL, TOG_VIEW_DIFF);
1348 ea5e7bb5 2018-08-01 stsp if (view == NULL) {
1349 ea5e7bb5 2018-08-01 stsp error = got_error_from_errno();
1350 ea5e7bb5 2018-08-01 stsp goto done;
1351 ea5e7bb5 2018-08-01 stsp }
1352 5dc9f4bc 2018-08-04 stsp error = open_diff_view(view, obj1, obj2, repo);
1353 5dc9f4bc 2018-08-04 stsp if (error)
1354 5dc9f4bc 2018-08-04 stsp goto done;
1355 5dc9f4bc 2018-08-04 stsp error = show_diff_view(view);
1356 5dc9f4bc 2018-08-04 stsp close_diff_view(view);
1357 96a765a8 2018-08-04 stsp view_close(view);
1358 26ed57b2 2018-05-19 stsp done:
1359 26ed57b2 2018-05-19 stsp got_repo_close(repo);
1360 26ed57b2 2018-05-19 stsp if (obj1)
1361 26ed57b2 2018-05-19 stsp got_object_close(obj1);
1362 26ed57b2 2018-05-19 stsp if (obj2)
1363 26ed57b2 2018-05-19 stsp got_object_close(obj2);
1364 26ed57b2 2018-05-19 stsp return error;
1365 9f7d7167 2018-04-29 stsp }
1366 9f7d7167 2018-04-29 stsp
1367 4ed7e80c 2018-05-20 stsp __dead static void
1368 9f7d7167 2018-04-29 stsp usage_blame(void)
1369 9f7d7167 2018-04-29 stsp {
1370 80ddbec8 2018-04-29 stsp endwin();
1371 69069811 2018-08-02 stsp fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
1372 9f7d7167 2018-04-29 stsp getprogname());
1373 9f7d7167 2018-04-29 stsp exit(1);
1374 9f7d7167 2018-04-29 stsp }
1375 84451b3e 2018-07-10 stsp
1376 84451b3e 2018-07-10 stsp struct tog_blame_line {
1377 84451b3e 2018-07-10 stsp int annotated;
1378 84451b3e 2018-07-10 stsp struct got_object_id *id;
1379 84451b3e 2018-07-10 stsp };
1380 9f7d7167 2018-04-29 stsp
1381 4ed7e80c 2018-05-20 stsp static const struct got_error *
1382 f7d12f7e 2018-08-01 stsp draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
1383 f7d12f7e 2018-08-01 stsp const char *path, struct tog_blame_line *lines, int nlines,
1384 f7d12f7e 2018-08-01 stsp int blame_complete, int selected_line, int *first_displayed_line,
1385 f7d12f7e 2018-08-01 stsp int *last_displayed_line, int *eof, int max_lines)
1386 84451b3e 2018-07-10 stsp {
1387 84451b3e 2018-07-10 stsp const struct got_error *err;
1388 84451b3e 2018-07-10 stsp int lineno = 0, nprinted = 0;
1389 84451b3e 2018-07-10 stsp char *line;
1390 84451b3e 2018-07-10 stsp size_t len;
1391 84451b3e 2018-07-10 stsp wchar_t *wline;
1392 b700b5d6 2018-07-10 stsp int width, wlimit;
1393 84451b3e 2018-07-10 stsp struct tog_blame_line *blame_line;
1394 ee41ec32 2018-07-10 stsp struct got_object_id *prev_id = NULL;
1395 ab089a2a 2018-07-12 stsp char *id_str;
1396 ab089a2a 2018-07-12 stsp
1397 ab089a2a 2018-07-12 stsp err = got_object_id_str(&id_str, id);
1398 ab089a2a 2018-07-12 stsp if (err)
1399 ab089a2a 2018-07-12 stsp return err;
1400 84451b3e 2018-07-10 stsp
1401 84451b3e 2018-07-10 stsp rewind(f);
1402 f7d12f7e 2018-08-01 stsp werase(view->window);
1403 84451b3e 2018-07-10 stsp
1404 ab089a2a 2018-07-12 stsp if (asprintf(&line, "commit: %s", id_str) == -1) {
1405 ab089a2a 2018-07-12 stsp err = got_error_from_errno();
1406 ab089a2a 2018-07-12 stsp free(id_str);
1407 ab089a2a 2018-07-12 stsp return err;
1408 ab089a2a 2018-07-12 stsp }
1409 ab089a2a 2018-07-12 stsp
1410 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, line, view->ncols);
1411 ab089a2a 2018-07-12 stsp free(line);
1412 2550e4c3 2018-07-13 stsp line = NULL;
1413 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
1414 2550e4c3 2018-07-13 stsp free(wline);
1415 2550e4c3 2018-07-13 stsp wline = NULL;
1416 f7d12f7e 2018-08-01 stsp if (width < view->ncols)
1417 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
1418 ab089a2a 2018-07-12 stsp
1419 084063cd 2018-07-12 stsp if (asprintf(&line, "[%d/%d] %s%s",
1420 084063cd 2018-07-12 stsp *first_displayed_line - 1 + selected_line, nlines,
1421 12a0b23b 2018-07-12 stsp blame_complete ? "" : "annotating ", path) == -1) {
1422 ab089a2a 2018-07-12 stsp free(id_str);
1423 3f60a8ef 2018-07-10 stsp return got_error_from_errno();
1424 ab089a2a 2018-07-12 stsp }
1425 ab089a2a 2018-07-12 stsp free(id_str);
1426 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, line, view->ncols);
1427 3f60a8ef 2018-07-10 stsp free(line);
1428 2550e4c3 2018-07-13 stsp line = NULL;
1429 3f60a8ef 2018-07-10 stsp if (err)
1430 3f60a8ef 2018-07-10 stsp return err;
1431 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
1432 2550e4c3 2018-07-13 stsp free(wline);
1433 2550e4c3 2018-07-13 stsp wline = NULL;
1434 f7d12f7e 2018-08-01 stsp if (width < view->ncols)
1435 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
1436 3f60a8ef 2018-07-10 stsp
1437 84451b3e 2018-07-10 stsp *eof = 0;
1438 ab089a2a 2018-07-12 stsp while (nprinted < max_lines - 2) {
1439 84451b3e 2018-07-10 stsp line = parse_next_line(f, &len);
1440 84451b3e 2018-07-10 stsp if (line == NULL) {
1441 84451b3e 2018-07-10 stsp *eof = 1;
1442 84451b3e 2018-07-10 stsp break;
1443 84451b3e 2018-07-10 stsp }
1444 84451b3e 2018-07-10 stsp if (++lineno < *first_displayed_line) {
1445 84451b3e 2018-07-10 stsp free(line);
1446 84451b3e 2018-07-10 stsp continue;
1447 84451b3e 2018-07-10 stsp }
1448 84451b3e 2018-07-10 stsp
1449 f7d12f7e 2018-08-01 stsp wlimit = view->ncols < 9 ? 0 : view->ncols - 9;
1450 b700b5d6 2018-07-10 stsp err = format_line(&wline, &width, line, wlimit);
1451 84451b3e 2018-07-10 stsp if (err) {
1452 84451b3e 2018-07-10 stsp free(line);
1453 84451b3e 2018-07-10 stsp return err;
1454 84451b3e 2018-07-10 stsp }
1455 84451b3e 2018-07-10 stsp
1456 b700b5d6 2018-07-10 stsp if (nprinted == selected_line - 1)
1457 f7d12f7e 2018-08-01 stsp wstandout(view->window);
1458 b700b5d6 2018-07-10 stsp
1459 84451b3e 2018-07-10 stsp blame_line = &lines[lineno - 1];
1460 ee41ec32 2018-07-10 stsp if (blame_line->annotated && prev_id &&
1461 ee41ec32 2018-07-10 stsp got_object_id_cmp(prev_id, blame_line->id) == 0)
1462 f7d12f7e 2018-08-01 stsp waddstr(view->window, " ");
1463 ee41ec32 2018-07-10 stsp else if (blame_line->annotated) {
1464 84451b3e 2018-07-10 stsp char *id_str;
1465 84451b3e 2018-07-10 stsp err = got_object_id_str(&id_str, blame_line->id);
1466 84451b3e 2018-07-10 stsp if (err) {
1467 84451b3e 2018-07-10 stsp free(line);
1468 2550e4c3 2018-07-13 stsp free(wline);
1469 84451b3e 2018-07-10 stsp return err;
1470 84451b3e 2018-07-10 stsp }
1471 f7d12f7e 2018-08-01 stsp wprintw(view->window, "%.8s ", id_str);
1472 84451b3e 2018-07-10 stsp free(id_str);
1473 ee41ec32 2018-07-10 stsp prev_id = blame_line->id;
1474 ee41ec32 2018-07-10 stsp } else {
1475 f7d12f7e 2018-08-01 stsp waddstr(view->window, "........ ");
1476 ee41ec32 2018-07-10 stsp prev_id = NULL;
1477 ee41ec32 2018-07-10 stsp }
1478 84451b3e 2018-07-10 stsp
1479 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
1480 b700b5d6 2018-07-10 stsp while (width < wlimit) {
1481 f7d12f7e 2018-08-01 stsp waddch(view->window, ' ');
1482 b700b5d6 2018-07-10 stsp width++;
1483 b700b5d6 2018-07-10 stsp }
1484 b700b5d6 2018-07-10 stsp if (nprinted == selected_line - 1)
1485 f7d12f7e 2018-08-01 stsp wstandend(view->window);
1486 84451b3e 2018-07-10 stsp if (++nprinted == 1)
1487 84451b3e 2018-07-10 stsp *first_displayed_line = lineno;
1488 84451b3e 2018-07-10 stsp free(line);
1489 2550e4c3 2018-07-13 stsp free(wline);
1490 2550e4c3 2018-07-13 stsp wline = NULL;
1491 84451b3e 2018-07-10 stsp }
1492 84451b3e 2018-07-10 stsp *last_displayed_line = lineno;
1493 84451b3e 2018-07-10 stsp
1494 84451b3e 2018-07-10 stsp update_panels();
1495 84451b3e 2018-07-10 stsp doupdate();
1496 84451b3e 2018-07-10 stsp
1497 84451b3e 2018-07-10 stsp return NULL;
1498 84451b3e 2018-07-10 stsp }
1499 84451b3e 2018-07-10 stsp
1500 84451b3e 2018-07-10 stsp struct tog_blame_cb_args {
1501 84451b3e 2018-07-10 stsp pthread_mutex_t *mutex;
1502 84451b3e 2018-07-10 stsp struct tog_blame_line *lines; /* one per line */
1503 84451b3e 2018-07-10 stsp int nlines;
1504 84451b3e 2018-07-10 stsp
1505 7cc84d77 2018-08-01 stsp struct tog_view *view;
1506 ab089a2a 2018-07-12 stsp struct got_object_id *commit_id;
1507 84451b3e 2018-07-10 stsp FILE *f;
1508 3f60a8ef 2018-07-10 stsp const char *path;
1509 84451b3e 2018-07-10 stsp int *first_displayed_line;
1510 84451b3e 2018-07-10 stsp int *last_displayed_line;
1511 b700b5d6 2018-07-10 stsp int *selected_line;
1512 18430de3 2018-07-10 stsp int *quit;
1513 84451b3e 2018-07-10 stsp };
1514 84451b3e 2018-07-10 stsp
1515 84451b3e 2018-07-10 stsp static const struct got_error *
1516 84451b3e 2018-07-10 stsp blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
1517 84451b3e 2018-07-10 stsp {
1518 84451b3e 2018-07-10 stsp const struct got_error *err = NULL;
1519 84451b3e 2018-07-10 stsp struct tog_blame_cb_args *a = arg;
1520 84451b3e 2018-07-10 stsp struct tog_blame_line *line;
1521 84451b3e 2018-07-10 stsp int eof;
1522 84451b3e 2018-07-10 stsp
1523 d68a0a7d 2018-07-10 stsp if (nlines != a->nlines ||
1524 d68a0a7d 2018-07-10 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
1525 84451b3e 2018-07-10 stsp return got_error(GOT_ERR_RANGE);
1526 84451b3e 2018-07-10 stsp
1527 84451b3e 2018-07-10 stsp if (pthread_mutex_lock(a->mutex) != 0)
1528 84451b3e 2018-07-10 stsp return got_error_from_errno();
1529 84451b3e 2018-07-10 stsp
1530 18430de3 2018-07-10 stsp if (*a->quit) { /* user has quit the blame view */
1531 d68a0a7d 2018-07-10 stsp err = got_error(GOT_ERR_ITER_COMPLETED);
1532 d68a0a7d 2018-07-10 stsp goto done;
1533 d68a0a7d 2018-07-10 stsp }
1534 d68a0a7d 2018-07-10 stsp
1535 d68a0a7d 2018-07-10 stsp if (lineno == -1)
1536 d68a0a7d 2018-07-10 stsp goto done; /* no change in this commit */
1537 d68a0a7d 2018-07-10 stsp
1538 84451b3e 2018-07-10 stsp line = &a->lines[lineno - 1];
1539 d68a0a7d 2018-07-10 stsp if (line->annotated)
1540 d68a0a7d 2018-07-10 stsp goto done;
1541 d68a0a7d 2018-07-10 stsp
1542 84451b3e 2018-07-10 stsp line->id = got_object_id_dup(id);
1543 84451b3e 2018-07-10 stsp if (line->id == NULL) {
1544 84451b3e 2018-07-10 stsp err = got_error_from_errno();
1545 84451b3e 2018-07-10 stsp goto done;
1546 84451b3e 2018-07-10 stsp }
1547 84451b3e 2018-07-10 stsp line->annotated = 1;
1548 84451b3e 2018-07-10 stsp
1549 f7d12f7e 2018-08-01 stsp err = draw_blame(a->view, a->commit_id, a->f, a->path,
1550 245d91c1 2018-07-12 stsp a->lines, a->nlines, 0, *a->selected_line, a->first_displayed_line,
1551 f7d12f7e 2018-08-01 stsp a->last_displayed_line, &eof, a->view->nlines);
1552 84451b3e 2018-07-10 stsp done:
1553 84451b3e 2018-07-10 stsp if (pthread_mutex_unlock(a->mutex) != 0)
1554 84451b3e 2018-07-10 stsp return got_error_from_errno();
1555 84451b3e 2018-07-10 stsp return err;
1556 84451b3e 2018-07-10 stsp }
1557 84451b3e 2018-07-10 stsp
1558 84451b3e 2018-07-10 stsp struct tog_blame_thread_args {
1559 84451b3e 2018-07-10 stsp const char *path;
1560 84451b3e 2018-07-10 stsp struct got_repository *repo;
1561 245d91c1 2018-07-12 stsp struct tog_blame_cb_args *cb_args;
1562 18430de3 2018-07-10 stsp int *complete;
1563 84451b3e 2018-07-10 stsp };
1564 84451b3e 2018-07-10 stsp
1565 84451b3e 2018-07-10 stsp static void *
1566 84451b3e 2018-07-10 stsp blame_thread(void *arg)
1567 84451b3e 2018-07-10 stsp {
1568 18430de3 2018-07-10 stsp const struct got_error *err;
1569 18430de3 2018-07-10 stsp struct tog_blame_thread_args *ta = arg;
1570 245d91c1 2018-07-12 stsp struct tog_blame_cb_args *a = ta->cb_args;
1571 18430de3 2018-07-10 stsp int eof;
1572 18430de3 2018-07-10 stsp
1573 ab089a2a 2018-07-12 stsp err = got_blame_incremental(ta->path, a->commit_id, ta->repo,
1574 245d91c1 2018-07-12 stsp blame_cb, ta->cb_args);
1575 18430de3 2018-07-10 stsp
1576 18430de3 2018-07-10 stsp if (pthread_mutex_lock(a->mutex) != 0)
1577 18430de3 2018-07-10 stsp return (void *)got_error_from_errno();
1578 18430de3 2018-07-10 stsp
1579 c9beca56 2018-07-22 stsp got_repo_close(ta->repo);
1580 c9beca56 2018-07-22 stsp ta->repo = NULL;
1581 c9beca56 2018-07-22 stsp *ta->complete = 1;
1582 c9beca56 2018-07-22 stsp if (!err)
1583 f7d12f7e 2018-08-01 stsp err = draw_blame(a->view, a->commit_id, a->f, a->path,
1584 f7d12f7e 2018-08-01 stsp a->lines, a->nlines, 1, *a->selected_line,
1585 c9beca56 2018-07-22 stsp a->first_displayed_line, a->last_displayed_line, &eof,
1586 f7d12f7e 2018-08-01 stsp a->view->nlines);
1587 18430de3 2018-07-10 stsp
1588 18430de3 2018-07-10 stsp if (pthread_mutex_unlock(a->mutex) != 0 && err == NULL)
1589 18430de3 2018-07-10 stsp err = got_error_from_errno();
1590 18430de3 2018-07-10 stsp
1591 18430de3 2018-07-10 stsp return (void *)err;
1592 84451b3e 2018-07-10 stsp }
1593 84451b3e 2018-07-10 stsp
1594 245d91c1 2018-07-12 stsp static struct got_object_id *
1595 245d91c1 2018-07-12 stsp get_selected_commit_id(struct tog_blame_line *lines,
1596 245d91c1 2018-07-12 stsp int first_displayed_line, int selected_line)
1597 245d91c1 2018-07-12 stsp {
1598 245d91c1 2018-07-12 stsp struct tog_blame_line *line;
1599 b880a918 2018-07-10 stsp
1600 245d91c1 2018-07-12 stsp line = &lines[first_displayed_line - 1 + selected_line - 1];
1601 245d91c1 2018-07-12 stsp if (!line->annotated)
1602 245d91c1 2018-07-12 stsp return NULL;
1603 245d91c1 2018-07-12 stsp
1604 245d91c1 2018-07-12 stsp return line->id;
1605 245d91c1 2018-07-12 stsp }
1606 245d91c1 2018-07-12 stsp
1607 84451b3e 2018-07-10 stsp static const struct got_error *
1608 245d91c1 2018-07-12 stsp open_selected_commit(struct got_object **pobj, struct got_object **obj,
1609 b880a918 2018-07-10 stsp struct tog_blame_line *lines, int first_displayed_line,
1610 b880a918 2018-07-10 stsp int selected_line, struct got_repository *repo)
1611 b880a918 2018-07-10 stsp {
1612 b880a918 2018-07-10 stsp const struct got_error *err = NULL;
1613 b880a918 2018-07-10 stsp struct got_commit_object *commit = NULL;
1614 245d91c1 2018-07-12 stsp struct got_object_id *selected_id;
1615 b880a918 2018-07-10 stsp struct got_object_qid *pid;
1616 b880a918 2018-07-10 stsp
1617 b880a918 2018-07-10 stsp *pobj = NULL;
1618 b880a918 2018-07-10 stsp *obj = NULL;
1619 b880a918 2018-07-10 stsp
1620 245d91c1 2018-07-12 stsp selected_id = get_selected_commit_id(lines,
1621 245d91c1 2018-07-12 stsp first_displayed_line, selected_line);
1622 245d91c1 2018-07-12 stsp if (selected_id == NULL)
1623 b880a918 2018-07-10 stsp return NULL;
1624 b880a918 2018-07-10 stsp
1625 245d91c1 2018-07-12 stsp err = got_object_open(obj, repo, selected_id);
1626 b880a918 2018-07-10 stsp if (err)
1627 b880a918 2018-07-10 stsp goto done;
1628 b880a918 2018-07-10 stsp
1629 b880a918 2018-07-10 stsp err = got_object_commit_open(&commit, repo, *obj);
1630 b880a918 2018-07-10 stsp if (err)
1631 b880a918 2018-07-10 stsp goto done;
1632 b880a918 2018-07-10 stsp
1633 b880a918 2018-07-10 stsp pid = SIMPLEQ_FIRST(&commit->parent_ids);
1634 b880a918 2018-07-10 stsp if (pid) {
1635 b880a918 2018-07-10 stsp err = got_object_open(pobj, repo, pid->id);
1636 b880a918 2018-07-10 stsp if (err)
1637 b880a918 2018-07-10 stsp goto done;
1638 b880a918 2018-07-10 stsp }
1639 b880a918 2018-07-10 stsp done:
1640 b880a918 2018-07-10 stsp if (commit)
1641 b880a918 2018-07-10 stsp got_object_commit_close(commit);
1642 b880a918 2018-07-10 stsp return err;
1643 b880a918 2018-07-10 stsp }
1644 b880a918 2018-07-10 stsp
1645 245d91c1 2018-07-12 stsp struct tog_blame {
1646 245d91c1 2018-07-12 stsp FILE *f;
1647 245d91c1 2018-07-12 stsp size_t filesize;
1648 245d91c1 2018-07-12 stsp struct tog_blame_line *lines;
1649 245d91c1 2018-07-12 stsp size_t nlines;
1650 245d91c1 2018-07-12 stsp pthread_t thread;
1651 245d91c1 2018-07-12 stsp struct tog_blame_thread_args thread_args;
1652 245d91c1 2018-07-12 stsp struct tog_blame_cb_args cb_args;
1653 245d91c1 2018-07-12 stsp const char *path;
1654 245d91c1 2018-07-12 stsp };
1655 245d91c1 2018-07-12 stsp
1656 b880a918 2018-07-10 stsp static const struct got_error *
1657 245d91c1 2018-07-12 stsp stop_blame(struct tog_blame *blame)
1658 a70480e0 2018-06-23 stsp {
1659 a70480e0 2018-06-23 stsp const struct got_error *err = NULL;
1660 245d91c1 2018-07-12 stsp int i;
1661 245d91c1 2018-07-12 stsp
1662 245d91c1 2018-07-12 stsp if (blame->thread) {
1663 245d91c1 2018-07-12 stsp if (pthread_join(blame->thread, (void **)&err) != 0)
1664 245d91c1 2018-07-12 stsp err = got_error_from_errno();
1665 245d91c1 2018-07-12 stsp if (err && err->code == GOT_ERR_ITER_COMPLETED)
1666 245d91c1 2018-07-12 stsp err = NULL;
1667 245d91c1 2018-07-12 stsp blame->thread = NULL;
1668 245d91c1 2018-07-12 stsp }
1669 245d91c1 2018-07-12 stsp if (blame->thread_args.repo) {
1670 245d91c1 2018-07-12 stsp got_repo_close(blame->thread_args.repo);
1671 245d91c1 2018-07-12 stsp blame->thread_args.repo = NULL;
1672 245d91c1 2018-07-12 stsp }
1673 245d91c1 2018-07-12 stsp if (blame->f) {
1674 245d91c1 2018-07-12 stsp fclose(blame->f);
1675 245d91c1 2018-07-12 stsp blame->f = NULL;
1676 245d91c1 2018-07-12 stsp }
1677 245d91c1 2018-07-12 stsp for (i = 0; i < blame->nlines; i++)
1678 245d91c1 2018-07-12 stsp free(blame->lines[i].id);
1679 245d91c1 2018-07-12 stsp free(blame->lines);
1680 245d91c1 2018-07-12 stsp blame->lines = NULL;
1681 75c32340 2018-07-23 stsp free(blame->cb_args.commit_id);
1682 75c32340 2018-07-23 stsp blame->cb_args.commit_id = NULL;
1683 245d91c1 2018-07-12 stsp
1684 245d91c1 2018-07-12 stsp return err;
1685 245d91c1 2018-07-12 stsp }
1686 245d91c1 2018-07-12 stsp
1687 245d91c1 2018-07-12 stsp static const struct got_error *
1688 7cc84d77 2018-08-01 stsp run_blame(struct tog_blame *blame, pthread_mutex_t *mutex,
1689 7cc84d77 2018-08-01 stsp struct tog_view *view, int *blame_complete,
1690 245d91c1 2018-07-12 stsp int *first_displayed_line, int *last_displayed_line,
1691 245d91c1 2018-07-12 stsp int *selected_line, int *done, const char *path,
1692 245d91c1 2018-07-12 stsp struct got_object_id *commit_id,
1693 245d91c1 2018-07-12 stsp struct got_repository *repo)
1694 245d91c1 2018-07-12 stsp {
1695 245d91c1 2018-07-12 stsp const struct got_error *err = NULL;
1696 84451b3e 2018-07-10 stsp struct got_blob_object *blob = NULL;
1697 245d91c1 2018-07-12 stsp struct got_repository *thread_repo = NULL;
1698 245d91c1 2018-07-12 stsp struct got_object *obj;
1699 a70480e0 2018-06-23 stsp
1700 84451b3e 2018-07-10 stsp err = got_object_open_by_path(&obj, repo, commit_id, path);
1701 84451b3e 2018-07-10 stsp if (err)
1702 84451b3e 2018-07-10 stsp goto done;
1703 84451b3e 2018-07-10 stsp if (got_object_get_type(obj) != GOT_OBJ_TYPE_BLOB) {
1704 84451b3e 2018-07-10 stsp err = got_error(GOT_ERR_OBJ_TYPE);
1705 84451b3e 2018-07-10 stsp goto done;
1706 84451b3e 2018-07-10 stsp }
1707 a70480e0 2018-06-23 stsp
1708 84451b3e 2018-07-10 stsp err = got_object_blob_open(&blob, repo, obj, 8192);
1709 a70480e0 2018-06-23 stsp if (err)
1710 a70480e0 2018-06-23 stsp goto done;
1711 245d91c1 2018-07-12 stsp blame->f = got_opentemp();
1712 245d91c1 2018-07-12 stsp if (blame->f == NULL) {
1713 84451b3e 2018-07-10 stsp err = got_error_from_errno();
1714 84451b3e 2018-07-10 stsp goto done;
1715 84451b3e 2018-07-10 stsp }
1716 245d91c1 2018-07-12 stsp err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
1717 245d91c1 2018-07-12 stsp blame->f, blob);
1718 84451b3e 2018-07-10 stsp if (err)
1719 84451b3e 2018-07-10 stsp goto done;
1720 a70480e0 2018-06-23 stsp
1721 245d91c1 2018-07-12 stsp blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
1722 245d91c1 2018-07-12 stsp if (blame->lines == NULL) {
1723 84451b3e 2018-07-10 stsp err = got_error_from_errno();
1724 84451b3e 2018-07-10 stsp goto done;
1725 84451b3e 2018-07-10 stsp }
1726 a70480e0 2018-06-23 stsp
1727 245d91c1 2018-07-12 stsp err = got_repo_open(&thread_repo, got_repo_get_path(repo));
1728 bd24772e 2018-07-11 stsp if (err)
1729 bd24772e 2018-07-11 stsp goto done;
1730 bd24772e 2018-07-11 stsp
1731 7cc84d77 2018-08-01 stsp blame->cb_args.view = view;
1732 245d91c1 2018-07-12 stsp blame->cb_args.lines = blame->lines;
1733 245d91c1 2018-07-12 stsp blame->cb_args.nlines = blame->nlines;
1734 245d91c1 2018-07-12 stsp blame->cb_args.mutex = mutex;
1735 245d91c1 2018-07-12 stsp blame->cb_args.commit_id = got_object_id_dup(commit_id);
1736 245d91c1 2018-07-12 stsp if (blame->cb_args.commit_id == NULL) {
1737 245d91c1 2018-07-12 stsp err = got_error_from_errno();
1738 245d91c1 2018-07-12 stsp goto done;
1739 245d91c1 2018-07-12 stsp }
1740 245d91c1 2018-07-12 stsp blame->cb_args.f = blame->f;
1741 245d91c1 2018-07-12 stsp blame->cb_args.path = path;
1742 245d91c1 2018-07-12 stsp blame->cb_args.first_displayed_line = first_displayed_line;
1743 245d91c1 2018-07-12 stsp blame->cb_args.selected_line = selected_line;
1744 245d91c1 2018-07-12 stsp blame->cb_args.last_displayed_line = last_displayed_line;
1745 245d91c1 2018-07-12 stsp blame->cb_args.quit = done;
1746 245d91c1 2018-07-12 stsp
1747 245d91c1 2018-07-12 stsp blame->thread_args.path = path;
1748 245d91c1 2018-07-12 stsp blame->thread_args.repo = thread_repo;
1749 245d91c1 2018-07-12 stsp blame->thread_args.cb_args = &blame->cb_args;
1750 245d91c1 2018-07-12 stsp blame->thread_args.complete = blame_complete;
1751 245d91c1 2018-07-12 stsp *blame_complete = 0;
1752 245d91c1 2018-07-12 stsp
1753 245d91c1 2018-07-12 stsp if (pthread_create(&blame->thread, NULL, blame_thread,
1754 245d91c1 2018-07-12 stsp &blame->thread_args) != 0) {
1755 245d91c1 2018-07-12 stsp err = got_error_from_errno();
1756 245d91c1 2018-07-12 stsp goto done;
1757 245d91c1 2018-07-12 stsp }
1758 245d91c1 2018-07-12 stsp
1759 245d91c1 2018-07-12 stsp done:
1760 245d91c1 2018-07-12 stsp if (blob)
1761 245d91c1 2018-07-12 stsp got_object_blob_close(blob);
1762 245d91c1 2018-07-12 stsp if (obj)
1763 245d91c1 2018-07-12 stsp got_object_close(obj);
1764 245d91c1 2018-07-12 stsp if (err)
1765 245d91c1 2018-07-12 stsp stop_blame(blame);
1766 245d91c1 2018-07-12 stsp return err;
1767 245d91c1 2018-07-12 stsp }
1768 245d91c1 2018-07-12 stsp
1769 245d91c1 2018-07-12 stsp static const struct got_error *
1770 e1cd8fed 2018-08-01 stsp show_blame_view(struct tog_view *view, const char *path,
1771 e1cd8fed 2018-08-01 stsp struct got_object_id *commit_id, struct got_repository *repo)
1772 245d91c1 2018-07-12 stsp {
1773 245d91c1 2018-07-12 stsp const struct got_error *err = NULL, *thread_err = NULL;
1774 f7d12f7e 2018-08-01 stsp int ch, done = 0, first_displayed_line = 1, last_displayed_line;
1775 245d91c1 2018-07-12 stsp int selected_line = first_displayed_line;
1776 245d91c1 2018-07-12 stsp int eof, blame_complete = 0;
1777 245d91c1 2018-07-12 stsp struct got_object *obj = NULL, *pobj = NULL;
1778 245d91c1 2018-07-12 stsp pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
1779 245d91c1 2018-07-12 stsp struct tog_blame blame;
1780 dbc6a6b6 2018-07-12 stsp struct got_object_id_queue blamed_commits;
1781 dbc6a6b6 2018-07-12 stsp struct got_object_qid *blamed_commit = NULL;
1782 e1cd8fed 2018-08-01 stsp struct tog_view *diff_view;
1783 dbc6a6b6 2018-07-12 stsp
1784 dbc6a6b6 2018-07-12 stsp SIMPLEQ_INIT(&blamed_commits);
1785 245d91c1 2018-07-12 stsp
1786 245d91c1 2018-07-12 stsp if (pthread_mutex_init(&mutex, NULL) != 0) {
1787 245d91c1 2018-07-12 stsp err = got_error_from_errno();
1788 245d91c1 2018-07-12 stsp goto done;
1789 245d91c1 2018-07-12 stsp }
1790 245d91c1 2018-07-12 stsp
1791 dbc6a6b6 2018-07-12 stsp err = got_object_qid_alloc(&blamed_commit, commit_id);
1792 dbc6a6b6 2018-07-12 stsp if (err)
1793 245d91c1 2018-07-12 stsp goto done;
1794 dbc6a6b6 2018-07-12 stsp SIMPLEQ_INSERT_HEAD(&blamed_commits, blamed_commit, entry);
1795 245d91c1 2018-07-12 stsp
1796 6acd1bd1 2018-08-04 stsp view_show(view);
1797 f7d12f7e 2018-08-01 stsp last_displayed_line = view->nlines;
1798 a70480e0 2018-06-23 stsp
1799 245d91c1 2018-07-12 stsp memset(&blame, 0, sizeof(blame));
1800 7cc84d77 2018-08-01 stsp err = run_blame(&blame, &mutex, view, &blame_complete,
1801 245d91c1 2018-07-12 stsp &first_displayed_line, &last_displayed_line,
1802 dbc6a6b6 2018-07-12 stsp &selected_line, &done, path, blamed_commit->id, repo);
1803 245d91c1 2018-07-12 stsp if (err)
1804 245d91c1 2018-07-12 stsp return err;
1805 84451b3e 2018-07-10 stsp
1806 a70480e0 2018-06-23 stsp while (!done) {
1807 84451b3e 2018-07-10 stsp if (pthread_mutex_lock(&mutex) != 0) {
1808 84451b3e 2018-07-10 stsp err = got_error_from_errno();
1809 84451b3e 2018-07-10 stsp goto done;
1810 84451b3e 2018-07-10 stsp }
1811 f7d12f7e 2018-08-01 stsp err = draw_blame(view, blamed_commit->id, blame.f, path,
1812 f7d12f7e 2018-08-01 stsp blame.lines, blame.nlines, blame_complete, selected_line,
1813 f7d12f7e 2018-08-01 stsp &first_displayed_line, &last_displayed_line, &eof,
1814 f7d12f7e 2018-08-01 stsp view->nlines);
1815 84451b3e 2018-07-10 stsp if (pthread_mutex_unlock(&mutex) != 0) {
1816 84451b3e 2018-07-10 stsp err = got_error_from_errno();
1817 84451b3e 2018-07-10 stsp goto done;
1818 84451b3e 2018-07-10 stsp }
1819 a70480e0 2018-06-23 stsp if (err)
1820 a70480e0 2018-06-23 stsp break;
1821 a70480e0 2018-06-23 stsp nodelay(stdscr, FALSE);
1822 7cc84d77 2018-08-01 stsp ch = wgetch(view->window);
1823 a70480e0 2018-06-23 stsp nodelay(stdscr, TRUE);
1824 84451b3e 2018-07-10 stsp if (pthread_mutex_lock(&mutex) != 0) {
1825 84451b3e 2018-07-10 stsp err = got_error_from_errno();
1826 84451b3e 2018-07-10 stsp goto done;
1827 84451b3e 2018-07-10 stsp }
1828 a70480e0 2018-06-23 stsp switch (ch) {
1829 a70480e0 2018-06-23 stsp case 'q':
1830 a70480e0 2018-06-23 stsp done = 1;
1831 a70480e0 2018-06-23 stsp break;
1832 a70480e0 2018-06-23 stsp case 'k':
1833 a70480e0 2018-06-23 stsp case KEY_UP:
1834 b700b5d6 2018-07-10 stsp if (selected_line > 1)
1835 b700b5d6 2018-07-10 stsp selected_line--;
1836 b700b5d6 2018-07-10 stsp else if (selected_line == 1 &&
1837 b700b5d6 2018-07-10 stsp first_displayed_line > 1)
1838 a70480e0 2018-06-23 stsp first_displayed_line--;
1839 a70480e0 2018-06-23 stsp break;
1840 a70480e0 2018-06-23 stsp case KEY_PPAGE:
1841 38f94530 2018-07-12 stsp case KEY_BACKSPACE:
1842 b700b5d6 2018-07-10 stsp if (first_displayed_line == 1) {
1843 b700b5d6 2018-07-10 stsp selected_line = 1;
1844 b700b5d6 2018-07-10 stsp break;
1845 b700b5d6 2018-07-10 stsp }
1846 f7d12f7e 2018-08-01 stsp if (first_displayed_line > view->nlines - 2)
1847 f7d12f7e 2018-08-01 stsp first_displayed_line -=
1848 f7d12f7e 2018-08-01 stsp (view->nlines - 2);
1849 b700b5d6 2018-07-10 stsp else
1850 b700b5d6 2018-07-10 stsp first_displayed_line = 1;
1851 a70480e0 2018-06-23 stsp break;
1852 a70480e0 2018-06-23 stsp case 'j':
1853 a70480e0 2018-06-23 stsp case KEY_DOWN:
1854 f7d12f7e 2018-08-01 stsp if (selected_line < view->nlines - 2 &&
1855 a026b947 2018-07-12 stsp first_displayed_line + selected_line <=
1856 a026b947 2018-07-12 stsp blame.nlines)
1857 b700b5d6 2018-07-10 stsp selected_line++;
1858 245d91c1 2018-07-12 stsp else if (last_displayed_line < blame.nlines)
1859 b700b5d6 2018-07-10 stsp first_displayed_line++;
1860 b700b5d6 2018-07-10 stsp break;
1861 7a2921f9 2018-07-12 stsp case 'b':
1862 7a2921f9 2018-07-12 stsp case 'p': {
1863 245d91c1 2018-07-12 stsp struct got_object_id *id;
1864 245d91c1 2018-07-12 stsp id = get_selected_commit_id(blame.lines,
1865 245d91c1 2018-07-12 stsp first_displayed_line, selected_line);
1866 245d91c1 2018-07-12 stsp if (id == NULL || got_object_id_cmp(id,
1867 dbc6a6b6 2018-07-12 stsp blamed_commit->id) == 0)
1868 245d91c1 2018-07-12 stsp break;
1869 245d91c1 2018-07-12 stsp err = open_selected_commit(&pobj, &obj,
1870 245d91c1 2018-07-12 stsp blame.lines, first_displayed_line,
1871 245d91c1 2018-07-12 stsp selected_line, repo);
1872 ad6a6c43 2018-07-10 stsp if (err)
1873 245d91c1 2018-07-12 stsp break;
1874 245d91c1 2018-07-12 stsp if (pobj == NULL && obj == NULL)
1875 245d91c1 2018-07-12 stsp break;
1876 7a2921f9 2018-07-12 stsp if (ch == 'p' && pobj == NULL)
1877 7a2921f9 2018-07-12 stsp break;
1878 245d91c1 2018-07-12 stsp done = 1;
1879 245d91c1 2018-07-12 stsp if (pthread_mutex_unlock(&mutex) != 0) {
1880 245d91c1 2018-07-12 stsp err = got_error_from_errno();
1881 ad6a6c43 2018-07-10 stsp goto done;
1882 245d91c1 2018-07-12 stsp }
1883 245d91c1 2018-07-12 stsp thread_err = stop_blame(&blame);
1884 245d91c1 2018-07-12 stsp done = 0;
1885 245d91c1 2018-07-12 stsp if (pthread_mutex_lock(&mutex) != 0) {
1886 245d91c1 2018-07-12 stsp err = got_error_from_errno();
1887 245d91c1 2018-07-12 stsp goto done;
1888 245d91c1 2018-07-12 stsp }
1889 245d91c1 2018-07-12 stsp if (thread_err)
1890 245d91c1 2018-07-12 stsp break;
1891 7a2921f9 2018-07-12 stsp id = got_object_get_id(ch == 'b' ? obj : pobj);
1892 1960a6f9 2018-07-12 stsp got_object_close(obj);
1893 1960a6f9 2018-07-12 stsp obj = NULL;
1894 1960a6f9 2018-07-12 stsp if (pobj) {
1895 1960a6f9 2018-07-12 stsp got_object_close(pobj);
1896 14437fb1 2018-07-13 stsp pobj = NULL;
1897 1960a6f9 2018-07-12 stsp }
1898 dbc6a6b6 2018-07-12 stsp if (id == NULL) {
1899 245d91c1 2018-07-12 stsp err = got_error_from_errno();
1900 245d91c1 2018-07-12 stsp break;
1901 245d91c1 2018-07-12 stsp }
1902 dbc6a6b6 2018-07-12 stsp err = got_object_qid_alloc(&blamed_commit, id);
1903 dbc6a6b6 2018-07-12 stsp free(id);
1904 dbc6a6b6 2018-07-12 stsp if (err)
1905 dbc6a6b6 2018-07-12 stsp goto done;
1906 dbc6a6b6 2018-07-12 stsp SIMPLEQ_INSERT_HEAD(&blamed_commits,
1907 dbc6a6b6 2018-07-12 stsp blamed_commit, entry);
1908 7cc84d77 2018-08-01 stsp err = run_blame(&blame, &mutex, view,
1909 245d91c1 2018-07-12 stsp &blame_complete, &first_displayed_line,
1910 245d91c1 2018-07-12 stsp &last_displayed_line, &selected_line,
1911 dbc6a6b6 2018-07-12 stsp &done, path, blamed_commit->id, repo);
1912 dbc6a6b6 2018-07-12 stsp if (err)
1913 dbc6a6b6 2018-07-12 stsp break;
1914 dbc6a6b6 2018-07-12 stsp break;
1915 dbc6a6b6 2018-07-12 stsp }
1916 dbc6a6b6 2018-07-12 stsp case 'B': {
1917 dbc6a6b6 2018-07-12 stsp struct got_object_qid *first;
1918 dbc6a6b6 2018-07-12 stsp first = SIMPLEQ_FIRST(&blamed_commits);
1919 dbc6a6b6 2018-07-12 stsp if (!got_object_id_cmp(first->id, commit_id))
1920 dbc6a6b6 2018-07-12 stsp break;
1921 dbc6a6b6 2018-07-12 stsp done = 1;
1922 dbc6a6b6 2018-07-12 stsp if (pthread_mutex_unlock(&mutex) != 0) {
1923 dbc6a6b6 2018-07-12 stsp err = got_error_from_errno();
1924 dbc6a6b6 2018-07-12 stsp goto done;
1925 dbc6a6b6 2018-07-12 stsp }
1926 dbc6a6b6 2018-07-12 stsp thread_err = stop_blame(&blame);
1927 dbc6a6b6 2018-07-12 stsp done = 0;
1928 dbc6a6b6 2018-07-12 stsp if (pthread_mutex_lock(&mutex) != 0) {
1929 dbc6a6b6 2018-07-12 stsp err = got_error_from_errno();
1930 dbc6a6b6 2018-07-12 stsp goto done;
1931 dbc6a6b6 2018-07-12 stsp }
1932 dbc6a6b6 2018-07-12 stsp if (thread_err)
1933 dbc6a6b6 2018-07-12 stsp break;
1934 dbc6a6b6 2018-07-12 stsp SIMPLEQ_REMOVE_HEAD(&blamed_commits, entry);
1935 dbc6a6b6 2018-07-12 stsp got_object_qid_free(blamed_commit);
1936 dbc6a6b6 2018-07-12 stsp blamed_commit = SIMPLEQ_FIRST(&blamed_commits);
1937 7cc84d77 2018-08-01 stsp err = run_blame(&blame, &mutex, view,
1938 dbc6a6b6 2018-07-12 stsp &blame_complete, &first_displayed_line,
1939 dbc6a6b6 2018-07-12 stsp &last_displayed_line, &selected_line,
1940 dbc6a6b6 2018-07-12 stsp &done, path, blamed_commit->id, repo);
1941 245d91c1 2018-07-12 stsp if (err)
1942 245d91c1 2018-07-12 stsp break;
1943 245d91c1 2018-07-12 stsp break;
1944 245d91c1 2018-07-12 stsp }
1945 245d91c1 2018-07-12 stsp case KEY_ENTER:
1946 245d91c1 2018-07-12 stsp case '\r':
1947 245d91c1 2018-07-12 stsp err = open_selected_commit(&pobj, &obj,
1948 245d91c1 2018-07-12 stsp blame.lines, first_displayed_line,
1949 245d91c1 2018-07-12 stsp selected_line, repo);
1950 245d91c1 2018-07-12 stsp if (err)
1951 245d91c1 2018-07-12 stsp break;
1952 b880a918 2018-07-10 stsp if (pobj == NULL && obj == NULL)
1953 b880a918 2018-07-10 stsp break;
1954 b3665f43 2018-08-04 stsp diff_view = view_open(0, 0, 0, 0, view,
1955 d6b05b5b 2018-08-04 stsp TOG_VIEW_DIFF);
1956 ea5e7bb5 2018-08-01 stsp if (diff_view == NULL) {
1957 ea5e7bb5 2018-08-01 stsp err = got_error_from_errno();
1958 ea5e7bb5 2018-08-01 stsp break;
1959 ea5e7bb5 2018-08-01 stsp }
1960 5dc9f4bc 2018-08-04 stsp err = open_diff_view(diff_view, pobj, obj, repo);
1961 5dc9f4bc 2018-08-04 stsp if (err)
1962 5dc9f4bc 2018-08-04 stsp break;
1963 5dc9f4bc 2018-08-04 stsp err = show_diff_view(diff_view);
1964 5dc9f4bc 2018-08-04 stsp close_diff_view(diff_view);
1965 96a765a8 2018-08-04 stsp view_close(diff_view);
1966 5dc9f4bc 2018-08-04 stsp if (err)
1967 5dc9f4bc 2018-08-04 stsp break;
1968 6acd1bd1 2018-08-04 stsp view_show(view);
1969 b880a918 2018-07-10 stsp if (pobj) {
1970 b880a918 2018-07-10 stsp got_object_close(pobj);
1971 b880a918 2018-07-10 stsp pobj = NULL;
1972 fb311a66 2018-07-10 stsp }
1973 b880a918 2018-07-10 stsp got_object_close(obj);
1974 b880a918 2018-07-10 stsp obj = NULL;
1975 ad6a6c43 2018-07-10 stsp if (err)
1976 245d91c1 2018-07-12 stsp break;
1977 a70480e0 2018-06-23 stsp break;
1978 a70480e0 2018-06-23 stsp case KEY_NPAGE:
1979 a70480e0 2018-06-23 stsp case ' ':
1980 245d91c1 2018-07-12 stsp if (last_displayed_line >= blame.nlines &&
1981 f7d12f7e 2018-08-01 stsp selected_line < view->nlines - 2) {
1982 d2dfcfbf 2018-07-12 stsp selected_line = MIN(blame.nlines,
1983 f7d12f7e 2018-08-01 stsp view->nlines - 2);
1984 b700b5d6 2018-07-10 stsp break;
1985 a70480e0 2018-06-23 stsp }
1986 f7d12f7e 2018-08-01 stsp if (last_displayed_line + view->nlines - 2 <=
1987 245d91c1 2018-07-12 stsp blame.nlines)
1988 f7d12f7e 2018-08-01 stsp first_displayed_line +=
1989 f7d12f7e 2018-08-01 stsp view->nlines - 2;
1990 b700b5d6 2018-07-10 stsp else
1991 b700b5d6 2018-07-10 stsp first_displayed_line =
1992 f7d12f7e 2018-08-01 stsp blame.nlines - (view->nlines - 3);
1993 a70480e0 2018-06-23 stsp break;
1994 7e5c1fe1 2018-08-01 stsp case KEY_RESIZE:
1995 a41d2007 2018-08-01 stsp err = view_resize(view);
1996 a41d2007 2018-08-01 stsp if (err)
1997 a41d2007 2018-08-01 stsp break;
1998 7e5c1fe1 2018-08-01 stsp if (selected_line > view->nlines - 2) {
1999 7e5c1fe1 2018-08-01 stsp selected_line = MIN(blame.nlines,
2000 7e5c1fe1 2018-08-01 stsp view->nlines - 2);
2001 7e5c1fe1 2018-08-01 stsp }
2002 7e5c1fe1 2018-08-01 stsp break;
2003 a70480e0 2018-06-23 stsp default:
2004 a70480e0 2018-06-23 stsp break;
2005 84451b3e 2018-07-10 stsp }
2006 245d91c1 2018-07-12 stsp if (pthread_mutex_unlock(&mutex) != 0)
2007 84451b3e 2018-07-10 stsp err = got_error_from_errno();
2008 245d91c1 2018-07-12 stsp if (err || thread_err)
2009 245d91c1 2018-07-12 stsp break;
2010 a70480e0 2018-06-23 stsp }
2011 a70480e0 2018-06-23 stsp done:
2012 b880a918 2018-07-10 stsp if (pobj)
2013 b880a918 2018-07-10 stsp got_object_close(pobj);
2014 c9beca56 2018-07-22 stsp if (blame.thread)
2015 245d91c1 2018-07-12 stsp thread_err = stop_blame(&blame);
2016 dbc6a6b6 2018-07-12 stsp while (!SIMPLEQ_EMPTY(&blamed_commits)) {
2017 dbc6a6b6 2018-07-12 stsp blamed_commit = SIMPLEQ_FIRST(&blamed_commits);
2018 dbc6a6b6 2018-07-12 stsp SIMPLEQ_REMOVE_HEAD(&blamed_commits, entry);
2019 dbc6a6b6 2018-07-12 stsp got_object_qid_free(blamed_commit);
2020 dbc6a6b6 2018-07-12 stsp }
2021 245d91c1 2018-07-12 stsp return thread_err ? thread_err : err;
2022 a70480e0 2018-06-23 stsp }
2023 a70480e0 2018-06-23 stsp
2024 a70480e0 2018-06-23 stsp static const struct got_error *
2025 9f7d7167 2018-04-29 stsp cmd_blame(int argc, char *argv[])
2026 9f7d7167 2018-04-29 stsp {
2027 a70480e0 2018-06-23 stsp const struct got_error *error;
2028 a70480e0 2018-06-23 stsp struct got_repository *repo = NULL;
2029 69069811 2018-08-02 stsp char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2030 a70480e0 2018-06-23 stsp struct got_object_id *commit_id = NULL;
2031 a70480e0 2018-06-23 stsp char *commit_id_str = NULL;
2032 a70480e0 2018-06-23 stsp int ch;
2033 e1cd8fed 2018-08-01 stsp struct tog_view *view;
2034 a70480e0 2018-06-23 stsp
2035 a70480e0 2018-06-23 stsp #ifndef PROFILE
2036 a70480e0 2018-06-23 stsp if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
2037 a70480e0 2018-06-23 stsp err(1, "pledge");
2038 a70480e0 2018-06-23 stsp #endif
2039 a70480e0 2018-06-23 stsp
2040 69069811 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2041 a70480e0 2018-06-23 stsp switch (ch) {
2042 a70480e0 2018-06-23 stsp case 'c':
2043 a70480e0 2018-06-23 stsp commit_id_str = optarg;
2044 a70480e0 2018-06-23 stsp break;
2045 69069811 2018-08-02 stsp case 'r':
2046 69069811 2018-08-02 stsp repo_path = realpath(optarg, NULL);
2047 69069811 2018-08-02 stsp if (repo_path == NULL)
2048 69069811 2018-08-02 stsp err(1, "-r option");
2049 69069811 2018-08-02 stsp break;
2050 a70480e0 2018-06-23 stsp default:
2051 a70480e0 2018-06-23 stsp usage();
2052 a70480e0 2018-06-23 stsp /* NOTREACHED */
2053 a70480e0 2018-06-23 stsp }
2054 a70480e0 2018-06-23 stsp }
2055 a70480e0 2018-06-23 stsp
2056 a70480e0 2018-06-23 stsp argc -= optind;
2057 a70480e0 2018-06-23 stsp argv += optind;
2058 a70480e0 2018-06-23 stsp
2059 69069811 2018-08-02 stsp if (argc == 1)
2060 69069811 2018-08-02 stsp path = argv[0];
2061 69069811 2018-08-02 stsp else
2062 a70480e0 2018-06-23 stsp usage_blame();
2063 69069811 2018-08-02 stsp
2064 69069811 2018-08-02 stsp cwd = getcwd(NULL, 0);
2065 69069811 2018-08-02 stsp if (cwd == NULL) {
2066 69069811 2018-08-02 stsp error = got_error_from_errno();
2067 69069811 2018-08-02 stsp goto done;
2068 69069811 2018-08-02 stsp }
2069 69069811 2018-08-02 stsp if (repo_path == NULL) {
2070 69069811 2018-08-02 stsp repo_path = strdup(cwd);
2071 69069811 2018-08-02 stsp if (repo_path == NULL) {
2072 69069811 2018-08-02 stsp error = got_error_from_errno();
2073 69069811 2018-08-02 stsp goto done;
2074 69069811 2018-08-02 stsp }
2075 69069811 2018-08-02 stsp }
2076 69069811 2018-08-02 stsp
2077 69069811 2018-08-02 stsp
2078 69069811 2018-08-02 stsp error = got_repo_open(&repo, repo_path);
2079 a70480e0 2018-06-23 stsp if (error != NULL)
2080 66b4983c 2018-06-23 stsp return error;
2081 69069811 2018-08-02 stsp
2082 69069811 2018-08-02 stsp error = got_repo_map_path(&in_repo_path, repo, path);
2083 69069811 2018-08-02 stsp if (error != NULL)
2084 69069811 2018-08-02 stsp goto done;
2085 a70480e0 2018-06-23 stsp
2086 a70480e0 2018-06-23 stsp if (commit_id_str == NULL) {
2087 a70480e0 2018-06-23 stsp struct got_reference *head_ref;
2088 a70480e0 2018-06-23 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
2089 a70480e0 2018-06-23 stsp if (error != NULL)
2090 66b4983c 2018-06-23 stsp goto done;
2091 a70480e0 2018-06-23 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
2092 a70480e0 2018-06-23 stsp got_ref_close(head_ref);
2093 a70480e0 2018-06-23 stsp } else {
2094 a70480e0 2018-06-23 stsp struct got_object *obj;
2095 a70480e0 2018-06-23 stsp error = got_object_open_by_id_str(&obj, repo, commit_id_str);
2096 a70480e0 2018-06-23 stsp if (error != NULL)
2097 66b4983c 2018-06-23 stsp goto done;
2098 a70480e0 2018-06-23 stsp commit_id = got_object_get_id(obj);
2099 a19e88aa 2018-06-23 stsp if (commit_id == NULL)
2100 66b4983c 2018-06-23 stsp error = got_error_from_errno();
2101 a19e88aa 2018-06-23 stsp got_object_close(obj);
2102 a70480e0 2018-06-23 stsp }
2103 a19e88aa 2018-06-23 stsp if (error != NULL)
2104 a19e88aa 2018-06-23 stsp goto done;
2105 a70480e0 2018-06-23 stsp
2106 b3665f43 2018-08-04 stsp view = view_open(0, 0, 0, 0, NULL, TOG_VIEW_BLAME);
2107 e1cd8fed 2018-08-01 stsp if (view == NULL) {
2108 e1cd8fed 2018-08-01 stsp error = got_error_from_errno();
2109 e1cd8fed 2018-08-01 stsp goto done;
2110 e1cd8fed 2018-08-01 stsp }
2111 69069811 2018-08-02 stsp error = show_blame_view(view, in_repo_path, commit_id, repo);
2112 96a765a8 2018-08-04 stsp view_close(view);
2113 a70480e0 2018-06-23 stsp done:
2114 69069811 2018-08-02 stsp free(in_repo_path);
2115 69069811 2018-08-02 stsp free(repo_path);
2116 69069811 2018-08-02 stsp free(cwd);
2117 a70480e0 2018-06-23 stsp free(commit_id);
2118 a70480e0 2018-06-23 stsp if (repo)
2119 a70480e0 2018-06-23 stsp got_repo_close(repo);
2120 a70480e0 2018-06-23 stsp return error;
2121 ffd1d5e5 2018-06-23 stsp }
2122 ffd1d5e5 2018-06-23 stsp
2123 ffd1d5e5 2018-06-23 stsp static const struct got_error *
2124 f7d12f7e 2018-08-01 stsp draw_tree_entries(struct tog_view *view,
2125 f7d12f7e 2018-08-01 stsp struct got_tree_entry **first_displayed_entry,
2126 ffd1d5e5 2018-06-23 stsp struct got_tree_entry **last_displayed_entry,
2127 ffd1d5e5 2018-06-23 stsp struct got_tree_entry **selected_entry, int *ndisplayed,
2128 f7d12f7e 2018-08-01 stsp const char *label, int show_ids, const char *parent_path,
2129 ce52c690 2018-06-23 stsp const struct got_tree_entries *entries, int selected, int limit, int isroot)
2130 ffd1d5e5 2018-06-23 stsp {
2131 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
2132 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *te;
2133 ffd1d5e5 2018-06-23 stsp wchar_t *wline;
2134 ffd1d5e5 2018-06-23 stsp int width, n;
2135 ffd1d5e5 2018-06-23 stsp
2136 ffd1d5e5 2018-06-23 stsp *ndisplayed = 0;
2137 ffd1d5e5 2018-06-23 stsp
2138 f7d12f7e 2018-08-01 stsp werase(view->window);
2139 ffd1d5e5 2018-06-23 stsp
2140 ffd1d5e5 2018-06-23 stsp if (limit == 0)
2141 ffd1d5e5 2018-06-23 stsp return NULL;
2142 ffd1d5e5 2018-06-23 stsp
2143 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, label, view->ncols);
2144 ffd1d5e5 2018-06-23 stsp if (err)
2145 ffd1d5e5 2018-06-23 stsp return err;
2146 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
2147 2550e4c3 2018-07-13 stsp free(wline);
2148 2550e4c3 2018-07-13 stsp wline = NULL;
2149 f7d12f7e 2018-08-01 stsp if (width < view->ncols)
2150 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
2151 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
2152 ffd1d5e5 2018-06-23 stsp return NULL;
2153 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, parent_path, view->ncols);
2154 ce52c690 2018-06-23 stsp if (err)
2155 ce52c690 2018-06-23 stsp return err;
2156 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
2157 2550e4c3 2018-07-13 stsp free(wline);
2158 2550e4c3 2018-07-13 stsp wline = NULL;
2159 f7d12f7e 2018-08-01 stsp if (width < view->ncols)
2160 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
2161 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
2162 ffd1d5e5 2018-06-23 stsp return NULL;
2163 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
2164 a1eca9bb 2018-06-23 stsp if (--limit <= 0)
2165 a1eca9bb 2018-06-23 stsp return NULL;
2166 ffd1d5e5 2018-06-23 stsp
2167 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_FIRST(&entries->head);
2168 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry == NULL) {
2169 ffd1d5e5 2018-06-23 stsp if (selected == 0) {
2170 f7d12f7e 2018-08-01 stsp wstandout(view->window);
2171 ffd1d5e5 2018-06-23 stsp *selected_entry = NULL;
2172 ffd1d5e5 2018-06-23 stsp }
2173 f7d12f7e 2018-08-01 stsp waddstr(view->window, " ..\n"); /* parent directory */
2174 ffd1d5e5 2018-06-23 stsp if (selected == 0)
2175 f7d12f7e 2018-08-01 stsp wstandend(view->window);
2176 ffd1d5e5 2018-06-23 stsp (*ndisplayed)++;
2177 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
2178 ffd1d5e5 2018-06-23 stsp return NULL;
2179 ffd1d5e5 2018-06-23 stsp n = 1;
2180 ffd1d5e5 2018-06-23 stsp } else {
2181 ffd1d5e5 2018-06-23 stsp n = 0;
2182 ffd1d5e5 2018-06-23 stsp while (te != *first_displayed_entry)
2183 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_NEXT(te, entry);
2184 ffd1d5e5 2018-06-23 stsp }
2185 ffd1d5e5 2018-06-23 stsp
2186 ffd1d5e5 2018-06-23 stsp while (te) {
2187 1d13200f 2018-07-12 stsp char *line = NULL, *id_str = NULL;
2188 1d13200f 2018-07-12 stsp
2189 1d13200f 2018-07-12 stsp if (show_ids) {
2190 1d13200f 2018-07-12 stsp err = got_object_id_str(&id_str, te->id);
2191 1d13200f 2018-07-12 stsp if (err)
2192 1d13200f 2018-07-12 stsp return got_error_from_errno();
2193 1d13200f 2018-07-12 stsp }
2194 1d13200f 2018-07-12 stsp if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
2195 1d13200f 2018-07-12 stsp te->name, S_ISDIR(te->mode) ? "/" : "") == -1) {
2196 1d13200f 2018-07-12 stsp free(id_str);
2197 ffd1d5e5 2018-06-23 stsp return got_error_from_errno();
2198 1d13200f 2018-07-12 stsp }
2199 1d13200f 2018-07-12 stsp free(id_str);
2200 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, line, view->ncols);
2201 ffd1d5e5 2018-06-23 stsp if (err) {
2202 ffd1d5e5 2018-06-23 stsp free(line);
2203 ffd1d5e5 2018-06-23 stsp break;
2204 ffd1d5e5 2018-06-23 stsp }
2205 ffd1d5e5 2018-06-23 stsp if (n == selected) {
2206 f7d12f7e 2018-08-01 stsp wstandout(view->window);
2207 ffd1d5e5 2018-06-23 stsp *selected_entry = te;
2208 ffd1d5e5 2018-06-23 stsp }
2209 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
2210 f7d12f7e 2018-08-01 stsp if (width < view->ncols)
2211 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
2212 ffd1d5e5 2018-06-23 stsp if (n == selected)
2213 f7d12f7e 2018-08-01 stsp wstandend(view->window);
2214 ffd1d5e5 2018-06-23 stsp free(line);
2215 2550e4c3 2018-07-13 stsp free(wline);
2216 2550e4c3 2018-07-13 stsp wline = NULL;
2217 ffd1d5e5 2018-06-23 stsp n++;
2218 ffd1d5e5 2018-06-23 stsp (*ndisplayed)++;
2219 ffd1d5e5 2018-06-23 stsp *last_displayed_entry = te;
2220 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
2221 ffd1d5e5 2018-06-23 stsp break;
2222 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_NEXT(te, entry);
2223 ffd1d5e5 2018-06-23 stsp }
2224 ffd1d5e5 2018-06-23 stsp
2225 ffd1d5e5 2018-06-23 stsp return err;
2226 ffd1d5e5 2018-06-23 stsp }
2227 ffd1d5e5 2018-06-23 stsp
2228 ffd1d5e5 2018-06-23 stsp static void
2229 ffd1d5e5 2018-06-23 stsp tree_scroll_up(struct got_tree_entry **first_displayed_entry, int maxscroll,
2230 ffd1d5e5 2018-06-23 stsp const struct got_tree_entries *entries, int isroot)
2231 ffd1d5e5 2018-06-23 stsp {
2232 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *te, *prev;
2233 ffd1d5e5 2018-06-23 stsp int i;
2234 ffd1d5e5 2018-06-23 stsp
2235 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry == NULL)
2236 ffd1d5e5 2018-06-23 stsp return;
2237 ffd1d5e5 2018-06-23 stsp
2238 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_FIRST(&entries->head);
2239 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry == te) {
2240 ffd1d5e5 2018-06-23 stsp if (!isroot)
2241 ffd1d5e5 2018-06-23 stsp *first_displayed_entry = NULL;
2242 ffd1d5e5 2018-06-23 stsp return;
2243 ffd1d5e5 2018-06-23 stsp }
2244 ffd1d5e5 2018-06-23 stsp
2245 ffd1d5e5 2018-06-23 stsp /* XXX this is stupid... switch to TAILQ? */
2246 ffd1d5e5 2018-06-23 stsp for (i = 0; i < maxscroll; i++) {
2247 ffd1d5e5 2018-06-23 stsp while (te != *first_displayed_entry) {
2248 ffd1d5e5 2018-06-23 stsp prev = te;
2249 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_NEXT(te, entry);
2250 ffd1d5e5 2018-06-23 stsp }
2251 ffd1d5e5 2018-06-23 stsp *first_displayed_entry = prev;
2252 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_FIRST(&entries->head);
2253 ffd1d5e5 2018-06-23 stsp }
2254 ffd1d5e5 2018-06-23 stsp if (!isroot && te == SIMPLEQ_FIRST(&entries->head) && i < maxscroll)
2255 ffd1d5e5 2018-06-23 stsp *first_displayed_entry = NULL;
2256 ffd1d5e5 2018-06-23 stsp }
2257 ffd1d5e5 2018-06-23 stsp
2258 ffd1d5e5 2018-06-23 stsp static void
2259 ffd1d5e5 2018-06-23 stsp tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
2260 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *last_displayed_entry,
2261 ffd1d5e5 2018-06-23 stsp const struct got_tree_entries *entries)
2262 ffd1d5e5 2018-06-23 stsp {
2263 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *next;
2264 ffd1d5e5 2018-06-23 stsp int n = 0;
2265 ffd1d5e5 2018-06-23 stsp
2266 ffd1d5e5 2018-06-23 stsp if (SIMPLEQ_NEXT(last_displayed_entry, entry) == NULL)
2267 ffd1d5e5 2018-06-23 stsp return;
2268 ffd1d5e5 2018-06-23 stsp
2269 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry)
2270 ffd1d5e5 2018-06-23 stsp next = SIMPLEQ_NEXT(*first_displayed_entry, entry);
2271 ffd1d5e5 2018-06-23 stsp else
2272 ffd1d5e5 2018-06-23 stsp next = SIMPLEQ_FIRST(&entries->head);
2273 ffd1d5e5 2018-06-23 stsp while (next) {
2274 ffd1d5e5 2018-06-23 stsp *first_displayed_entry = next;
2275 ffd1d5e5 2018-06-23 stsp if (++n >= maxscroll)
2276 ffd1d5e5 2018-06-23 stsp break;
2277 ffd1d5e5 2018-06-23 stsp next = SIMPLEQ_NEXT(next, entry);
2278 ffd1d5e5 2018-06-23 stsp }
2279 ffd1d5e5 2018-06-23 stsp }
2280 ffd1d5e5 2018-06-23 stsp
2281 ffd1d5e5 2018-06-23 stsp struct tog_parent_tree {
2282 d9765a41 2018-06-23 stsp TAILQ_ENTRY(tog_parent_tree) entry;
2283 ffd1d5e5 2018-06-23 stsp struct got_tree_object *tree;
2284 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *first_displayed_entry;
2285 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *selected_entry;
2286 ffd1d5e5 2018-06-23 stsp int selected;
2287 ffd1d5e5 2018-06-23 stsp };
2288 ffd1d5e5 2018-06-23 stsp
2289 d9765a41 2018-06-23 stsp TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
2290 ffd1d5e5 2018-06-23 stsp
2291 ffd1d5e5 2018-06-23 stsp static const struct got_error *
2292 ce52c690 2018-06-23 stsp tree_entry_path(char **path, struct tog_parent_trees *parents,
2293 ce52c690 2018-06-23 stsp struct got_tree_entry *te)
2294 ffd1d5e5 2018-06-23 stsp {
2295 cb2ebc8a 2018-06-23 stsp const struct got_error *err = NULL;
2296 ffd1d5e5 2018-06-23 stsp struct tog_parent_tree *pt;
2297 ffd1d5e5 2018-06-23 stsp size_t len = 2; /* for leading slash and NUL */
2298 ffd1d5e5 2018-06-23 stsp
2299 d9765a41 2018-06-23 stsp TAILQ_FOREACH(pt, parents, entry)
2300 ffd1d5e5 2018-06-23 stsp len += strlen(pt->selected_entry->name) + 1 /* slash */;
2301 ce52c690 2018-06-23 stsp if (te)
2302 ce52c690 2018-06-23 stsp len += strlen(te->name);
2303 ce52c690 2018-06-23 stsp
2304 ce52c690 2018-06-23 stsp *path = calloc(1, len);
2305 ffd1d5e5 2018-06-23 stsp if (path == NULL)
2306 ffd1d5e5 2018-06-23 stsp return got_error_from_errno();
2307 ffd1d5e5 2018-06-23 stsp
2308 ce52c690 2018-06-23 stsp (*path)[0] = '/';
2309 d9765a41 2018-06-23 stsp pt = TAILQ_LAST(parents, tog_parent_trees);
2310 d9765a41 2018-06-23 stsp while (pt) {
2311 ce52c690 2018-06-23 stsp if (strlcat(*path, pt->selected_entry->name, len) >= len) {
2312 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
2313 cb2ebc8a 2018-06-23 stsp goto done;
2314 cb2ebc8a 2018-06-23 stsp }
2315 ce52c690 2018-06-23 stsp if (strlcat(*path, "/", len) >= len) {
2316 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
2317 cb2ebc8a 2018-06-23 stsp goto done;
2318 cb2ebc8a 2018-06-23 stsp }
2319 d9765a41 2018-06-23 stsp pt = TAILQ_PREV(pt, tog_parent_trees, entry);
2320 ffd1d5e5 2018-06-23 stsp }
2321 ce52c690 2018-06-23 stsp if (te) {
2322 ce52c690 2018-06-23 stsp if (strlcat(*path, te->name, len) >= len) {
2323 ce52c690 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
2324 ce52c690 2018-06-23 stsp goto done;
2325 ce52c690 2018-06-23 stsp }
2326 cb2ebc8a 2018-06-23 stsp }
2327 ce52c690 2018-06-23 stsp done:
2328 ce52c690 2018-06-23 stsp if (err) {
2329 ce52c690 2018-06-23 stsp free(*path);
2330 ce52c690 2018-06-23 stsp *path = NULL;
2331 ce52c690 2018-06-23 stsp }
2332 ce52c690 2018-06-23 stsp return err;
2333 ce52c690 2018-06-23 stsp }
2334 ce52c690 2018-06-23 stsp
2335 ce52c690 2018-06-23 stsp static const struct got_error *
2336 cdf1ee82 2018-08-01 stsp blame_tree_entry(struct tog_view *parent_view, struct got_tree_entry *te,
2337 e1cd8fed 2018-08-01 stsp struct tog_parent_trees *parents, struct got_object_id *commit_id,
2338 e1cd8fed 2018-08-01 stsp struct got_repository *repo)
2339 ce52c690 2018-06-23 stsp {
2340 ce52c690 2018-06-23 stsp const struct got_error *err = NULL;
2341 ce52c690 2018-06-23 stsp char *path;
2342 cdf1ee82 2018-08-01 stsp struct tog_view *view;
2343 69efd4c4 2018-07-18 stsp
2344 ce52c690 2018-06-23 stsp err = tree_entry_path(&path, parents, te);
2345 ce52c690 2018-06-23 stsp if (err)
2346 ce52c690 2018-06-23 stsp return err;
2347 ffd1d5e5 2018-06-23 stsp
2348 b3665f43 2018-08-04 stsp view = view_open(0, 0, 0, 0, parent_view, TOG_VIEW_BLAME);
2349 cdf1ee82 2018-08-01 stsp if (view) {
2350 cdf1ee82 2018-08-01 stsp err = show_blame_view(view, path, commit_id, repo);
2351 96a765a8 2018-08-04 stsp view_close(view);
2352 cdf1ee82 2018-08-01 stsp } else
2353 cdf1ee82 2018-08-01 stsp err = got_error_from_errno();
2354 cdf1ee82 2018-08-01 stsp
2355 6acd1bd1 2018-08-04 stsp view_show(parent_view);
2356 69efd4c4 2018-07-18 stsp free(path);
2357 69efd4c4 2018-07-18 stsp return err;
2358 69efd4c4 2018-07-18 stsp }
2359 69efd4c4 2018-07-18 stsp
2360 69efd4c4 2018-07-18 stsp static const struct got_error *
2361 04cc582a 2018-08-01 stsp log_tree_entry(struct tog_view *view, struct got_tree_entry *te,
2362 04cc582a 2018-08-01 stsp struct tog_parent_trees *parents, struct got_object_id *commit_id,
2363 04cc582a 2018-08-01 stsp struct got_repository *repo)
2364 69efd4c4 2018-07-18 stsp {
2365 69efd4c4 2018-07-18 stsp const struct got_error *err = NULL;
2366 69efd4c4 2018-07-18 stsp char *path;
2367 69efd4c4 2018-07-18 stsp
2368 69efd4c4 2018-07-18 stsp err = tree_entry_path(&path, parents, te);
2369 69efd4c4 2018-07-18 stsp if (err)
2370 69efd4c4 2018-07-18 stsp return err;
2371 69efd4c4 2018-07-18 stsp
2372 ba4f502b 2018-08-04 stsp err = open_log_view(view, commit_id, repo, path);
2373 ba4f502b 2018-08-04 stsp if (err)
2374 ba4f502b 2018-08-04 stsp goto done;
2375 ba4f502b 2018-08-04 stsp err = show_log_view(view);
2376 ba4f502b 2018-08-04 stsp close_log_view(view);
2377 ba4f502b 2018-08-04 stsp done:
2378 cb2ebc8a 2018-06-23 stsp free(path);
2379 cb2ebc8a 2018-06-23 stsp return err;
2380 ffd1d5e5 2018-06-23 stsp }
2381 ffd1d5e5 2018-06-23 stsp
2382 ffd1d5e5 2018-06-23 stsp static const struct got_error *
2383 5221c383 2018-08-01 stsp show_tree_view(struct tog_view *view, struct got_tree_object *root,
2384 5221c383 2018-08-01 stsp struct got_object_id *commit_id, struct got_repository *repo)
2385 ffd1d5e5 2018-06-23 stsp {
2386 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
2387 1d13200f 2018-07-12 stsp int ch, done = 0, selected = 0, show_ids = 0;
2388 ffd1d5e5 2018-06-23 stsp struct got_tree_object *tree = root;
2389 ffd1d5e5 2018-06-23 stsp const struct got_tree_entries *entries;
2390 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *first_displayed_entry = NULL;
2391 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *last_displayed_entry = NULL;
2392 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *selected_entry = NULL;
2393 ffd1d5e5 2018-06-23 stsp char *commit_id_str = NULL, *tree_label = NULL;
2394 ffd1d5e5 2018-06-23 stsp int nentries, ndisplayed;
2395 ffd1d5e5 2018-06-23 stsp struct tog_parent_trees parents;
2396 ffd1d5e5 2018-06-23 stsp
2397 d9765a41 2018-06-23 stsp TAILQ_INIT(&parents);
2398 ffd1d5e5 2018-06-23 stsp
2399 ffd1d5e5 2018-06-23 stsp err = got_object_id_str(&commit_id_str, commit_id);
2400 ffd1d5e5 2018-06-23 stsp if (err != NULL)
2401 ffd1d5e5 2018-06-23 stsp goto done;
2402 ffd1d5e5 2018-06-23 stsp
2403 decd3bd1 2018-07-12 stsp if (asprintf(&tree_label, "commit: %s", commit_id_str) == -1) {
2404 ffd1d5e5 2018-06-23 stsp err = got_error_from_errno();
2405 ffd1d5e5 2018-06-23 stsp goto done;
2406 ffd1d5e5 2018-06-23 stsp }
2407 ffd1d5e5 2018-06-23 stsp
2408 6acd1bd1 2018-08-04 stsp view_show(view);
2409 ffd1d5e5 2018-06-23 stsp
2410 ffd1d5e5 2018-06-23 stsp entries = got_object_tree_get_entries(root);
2411 ffd1d5e5 2018-06-23 stsp first_displayed_entry = SIMPLEQ_FIRST(&entries->head);
2412 ffd1d5e5 2018-06-23 stsp while (!done) {
2413 ce52c690 2018-06-23 stsp char *parent_path;
2414 ffd1d5e5 2018-06-23 stsp entries = got_object_tree_get_entries(tree);
2415 ffd1d5e5 2018-06-23 stsp nentries = entries->nentries;
2416 ffd1d5e5 2018-06-23 stsp if (tree != root)
2417 ffd1d5e5 2018-06-23 stsp nentries++; /* '..' directory */
2418 ce52c690 2018-06-23 stsp
2419 ce52c690 2018-06-23 stsp err = tree_entry_path(&parent_path, &parents, NULL);
2420 ce52c690 2018-06-23 stsp if (err)
2421 ce52c690 2018-06-23 stsp goto done;
2422 ffd1d5e5 2018-06-23 stsp
2423 f7d12f7e 2018-08-01 stsp err = draw_tree_entries(view, &first_displayed_entry,
2424 ffd1d5e5 2018-06-23 stsp &last_displayed_entry, &selected_entry, &ndisplayed,
2425 f7d12f7e 2018-08-01 stsp tree_label, show_ids, parent_path, entries, selected,
2426 f7d12f7e 2018-08-01 stsp view->nlines, tree == root);
2427 ce52c690 2018-06-23 stsp free(parent_path);
2428 ffd1d5e5 2018-06-23 stsp if (err)
2429 ffd1d5e5 2018-06-23 stsp break;
2430 ffd1d5e5 2018-06-23 stsp
2431 ffd1d5e5 2018-06-23 stsp nodelay(stdscr, FALSE);
2432 cc3c9aac 2018-08-01 stsp ch = wgetch(view->window);
2433 ffd1d5e5 2018-06-23 stsp nodelay(stdscr, TRUE);
2434 ffd1d5e5 2018-06-23 stsp switch (ch) {
2435 ffd1d5e5 2018-06-23 stsp case 'q':
2436 ffd1d5e5 2018-06-23 stsp done = 1;
2437 ffd1d5e5 2018-06-23 stsp break;
2438 1d13200f 2018-07-12 stsp case 'i':
2439 1d13200f 2018-07-12 stsp show_ids = !show_ids;
2440 69efd4c4 2018-07-18 stsp break;
2441 69efd4c4 2018-07-18 stsp case 'l':
2442 69efd4c4 2018-07-18 stsp if (selected_entry) {
2443 04cc582a 2018-08-01 stsp struct tog_view *log_view;
2444 b3665f43 2018-08-04 stsp log_view = view_open(0, 0, 0, 0, view,
2445 d6b05b5b 2018-08-04 stsp TOG_VIEW_LOG);
2446 04cc582a 2018-08-01 stsp if (log_view == NULL) {
2447 04cc582a 2018-08-01 stsp err = got_error_from_errno();
2448 04cc582a 2018-08-01 stsp goto done;
2449 04cc582a 2018-08-01 stsp }
2450 04cc582a 2018-08-01 stsp err = log_tree_entry(log_view,
2451 04cc582a 2018-08-01 stsp selected_entry, &parents,
2452 04cc582a 2018-08-01 stsp commit_id, repo);
2453 96a765a8 2018-08-04 stsp view_close(log_view);
2454 6acd1bd1 2018-08-04 stsp view_show(view);
2455 69efd4c4 2018-07-18 stsp if (err)
2456 69efd4c4 2018-07-18 stsp goto done;
2457 69efd4c4 2018-07-18 stsp }
2458 1d13200f 2018-07-12 stsp break;
2459 ffd1d5e5 2018-06-23 stsp case 'k':
2460 ffd1d5e5 2018-06-23 stsp case KEY_UP:
2461 ffd1d5e5 2018-06-23 stsp if (selected > 0)
2462 ffd1d5e5 2018-06-23 stsp selected--;
2463 ffd1d5e5 2018-06-23 stsp if (selected > 0)
2464 ffd1d5e5 2018-06-23 stsp break;
2465 ffd1d5e5 2018-06-23 stsp tree_scroll_up(&first_displayed_entry, 1,
2466 ffd1d5e5 2018-06-23 stsp entries, tree == root);
2467 ffd1d5e5 2018-06-23 stsp break;
2468 ffd1d5e5 2018-06-23 stsp case KEY_PPAGE:
2469 ffd1d5e5 2018-06-23 stsp if (SIMPLEQ_FIRST(&entries->head) ==
2470 ffd1d5e5 2018-06-23 stsp first_displayed_entry) {
2471 cf8f1261 2018-06-23 stsp if (tree != root)
2472 cf8f1261 2018-06-23 stsp first_displayed_entry = NULL;
2473 ffd1d5e5 2018-06-23 stsp selected = 0;
2474 ffd1d5e5 2018-06-23 stsp break;
2475 ffd1d5e5 2018-06-23 stsp }
2476 f7d12f7e 2018-08-01 stsp tree_scroll_up(&first_displayed_entry,
2477 f7d12f7e 2018-08-01 stsp view->nlines, entries, tree == root);
2478 ffd1d5e5 2018-06-23 stsp break;
2479 ffd1d5e5 2018-06-23 stsp case 'j':
2480 ffd1d5e5 2018-06-23 stsp case KEY_DOWN:
2481 ffd1d5e5 2018-06-23 stsp if (selected < ndisplayed - 1) {
2482 ffd1d5e5 2018-06-23 stsp selected++;
2483 ffd1d5e5 2018-06-23 stsp break;
2484 ffd1d5e5 2018-06-23 stsp }
2485 ffd1d5e5 2018-06-23 stsp tree_scroll_down(&first_displayed_entry, 1,
2486 ffd1d5e5 2018-06-23 stsp last_displayed_entry, entries);
2487 ffd1d5e5 2018-06-23 stsp break;
2488 ffd1d5e5 2018-06-23 stsp case KEY_NPAGE:
2489 f7d12f7e 2018-08-01 stsp tree_scroll_down(&first_displayed_entry,
2490 f7d12f7e 2018-08-01 stsp view->nlines, last_displayed_entry,
2491 f7d12f7e 2018-08-01 stsp entries);
2492 ffd1d5e5 2018-06-23 stsp if (SIMPLEQ_NEXT(last_displayed_entry, entry))
2493 ffd1d5e5 2018-06-23 stsp break;
2494 ffd1d5e5 2018-06-23 stsp /* can't scroll any further; move cursor down */
2495 ffd1d5e5 2018-06-23 stsp if (selected < ndisplayed - 1)
2496 ffd1d5e5 2018-06-23 stsp selected = ndisplayed - 1;
2497 ffd1d5e5 2018-06-23 stsp break;
2498 ffd1d5e5 2018-06-23 stsp case KEY_ENTER:
2499 ffd1d5e5 2018-06-23 stsp case '\r':
2500 ffd1d5e5 2018-06-23 stsp if (selected_entry == NULL) {
2501 ffd1d5e5 2018-06-23 stsp struct tog_parent_tree *parent;
2502 ffd1d5e5 2018-06-23 stsp case KEY_BACKSPACE:
2503 ffd1d5e5 2018-06-23 stsp /* user selected '..' */
2504 ffd1d5e5 2018-06-23 stsp if (tree == root)
2505 ffd1d5e5 2018-06-23 stsp break;
2506 d9765a41 2018-06-23 stsp parent = TAILQ_FIRST(&parents);
2507 d9765a41 2018-06-23 stsp TAILQ_REMOVE(&parents, parent, entry);
2508 ffd1d5e5 2018-06-23 stsp got_object_tree_close(tree);
2509 ffd1d5e5 2018-06-23 stsp tree = parent->tree;
2510 ffd1d5e5 2018-06-23 stsp first_displayed_entry =
2511 ffd1d5e5 2018-06-23 stsp parent->first_displayed_entry;
2512 ffd1d5e5 2018-06-23 stsp selected_entry = parent->selected_entry;
2513 ffd1d5e5 2018-06-23 stsp selected = parent->selected;
2514 ffd1d5e5 2018-06-23 stsp free(parent);
2515 ffd1d5e5 2018-06-23 stsp } else if (S_ISDIR(selected_entry->mode)) {
2516 ffd1d5e5 2018-06-23 stsp struct tog_parent_tree *parent;
2517 ffd1d5e5 2018-06-23 stsp struct got_tree_object *child;
2518 ffd1d5e5 2018-06-23 stsp err = got_object_open_as_tree(
2519 ffd1d5e5 2018-06-23 stsp &child, repo, selected_entry->id);
2520 ffd1d5e5 2018-06-23 stsp if (err)
2521 ffd1d5e5 2018-06-23 stsp goto done;
2522 ffd1d5e5 2018-06-23 stsp parent = calloc(1, sizeof(*parent));
2523 ffd1d5e5 2018-06-23 stsp if (parent == NULL) {
2524 ffd1d5e5 2018-06-23 stsp err = got_error_from_errno();
2525 ffd1d5e5 2018-06-23 stsp goto done;
2526 ffd1d5e5 2018-06-23 stsp }
2527 ffd1d5e5 2018-06-23 stsp parent->tree = tree;
2528 ffd1d5e5 2018-06-23 stsp parent->first_displayed_entry =
2529 ffd1d5e5 2018-06-23 stsp first_displayed_entry;
2530 ffd1d5e5 2018-06-23 stsp parent->selected_entry = selected_entry;
2531 ffd1d5e5 2018-06-23 stsp parent->selected = selected;
2532 d9765a41 2018-06-23 stsp TAILQ_INSERT_HEAD(&parents, parent,
2533 ffd1d5e5 2018-06-23 stsp entry);
2534 ffd1d5e5 2018-06-23 stsp tree = child;
2535 ffd1d5e5 2018-06-23 stsp selected = 0;
2536 ffd1d5e5 2018-06-23 stsp first_displayed_entry = NULL;
2537 ffd1d5e5 2018-06-23 stsp } else if (S_ISREG(selected_entry->mode)) {
2538 cdf1ee82 2018-08-01 stsp err = blame_tree_entry(view,
2539 e1cd8fed 2018-08-01 stsp selected_entry, &parents,
2540 e1cd8fed 2018-08-01 stsp commit_id, repo);
2541 ffd1d5e5 2018-06-23 stsp if (err)
2542 ffd1d5e5 2018-06-23 stsp goto done;
2543 ffd1d5e5 2018-06-23 stsp }
2544 ffd1d5e5 2018-06-23 stsp break;
2545 ffd1d5e5 2018-06-23 stsp case KEY_RESIZE:
2546 a41d2007 2018-08-01 stsp err = view_resize(view);
2547 a41d2007 2018-08-01 stsp if (err)
2548 a41d2007 2018-08-01 stsp goto done;
2549 f7d12f7e 2018-08-01 stsp if (selected > view->nlines)
2550 ffd1d5e5 2018-06-23 stsp selected = ndisplayed - 1;
2551 ffd1d5e5 2018-06-23 stsp break;
2552 ffd1d5e5 2018-06-23 stsp default:
2553 ffd1d5e5 2018-06-23 stsp break;
2554 ffd1d5e5 2018-06-23 stsp }
2555 ffd1d5e5 2018-06-23 stsp }
2556 ffd1d5e5 2018-06-23 stsp done:
2557 ffd1d5e5 2018-06-23 stsp free(tree_label);
2558 ffd1d5e5 2018-06-23 stsp free(commit_id_str);
2559 d9765a41 2018-06-23 stsp while (!TAILQ_EMPTY(&parents)) {
2560 ffd1d5e5 2018-06-23 stsp struct tog_parent_tree *parent;
2561 d9765a41 2018-06-23 stsp parent = TAILQ_FIRST(&parents);
2562 d9765a41 2018-06-23 stsp TAILQ_REMOVE(&parents, parent, entry);
2563 ffd1d5e5 2018-06-23 stsp free(parent);
2564 ffd1d5e5 2018-06-23 stsp
2565 ffd1d5e5 2018-06-23 stsp }
2566 ffd1d5e5 2018-06-23 stsp if (tree != root)
2567 ffd1d5e5 2018-06-23 stsp got_object_tree_close(tree);
2568 ffd1d5e5 2018-06-23 stsp return err;
2569 9f7d7167 2018-04-29 stsp }
2570 9f7d7167 2018-04-29 stsp
2571 ffd1d5e5 2018-06-23 stsp __dead static void
2572 ffd1d5e5 2018-06-23 stsp usage_tree(void)
2573 ffd1d5e5 2018-06-23 stsp {
2574 ffd1d5e5 2018-06-23 stsp endwin();
2575 ffd1d5e5 2018-06-23 stsp fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
2576 ffd1d5e5 2018-06-23 stsp getprogname());
2577 ffd1d5e5 2018-06-23 stsp exit(1);
2578 ffd1d5e5 2018-06-23 stsp }
2579 ffd1d5e5 2018-06-23 stsp
2580 ffd1d5e5 2018-06-23 stsp static const struct got_error *
2581 ffd1d5e5 2018-06-23 stsp cmd_tree(int argc, char *argv[])
2582 ffd1d5e5 2018-06-23 stsp {
2583 ffd1d5e5 2018-06-23 stsp const struct got_error *error;
2584 ffd1d5e5 2018-06-23 stsp struct got_repository *repo = NULL;
2585 ffd1d5e5 2018-06-23 stsp char *repo_path = NULL;
2586 ffd1d5e5 2018-06-23 stsp struct got_object_id *commit_id = NULL;
2587 ffd1d5e5 2018-06-23 stsp char *commit_id_arg = NULL;
2588 ffd1d5e5 2018-06-23 stsp struct got_commit_object *commit = NULL;
2589 ffd1d5e5 2018-06-23 stsp struct got_tree_object *tree = NULL;
2590 ffd1d5e5 2018-06-23 stsp int ch;
2591 5221c383 2018-08-01 stsp struct tog_view *view;
2592 ffd1d5e5 2018-06-23 stsp
2593 ffd1d5e5 2018-06-23 stsp #ifndef PROFILE
2594 ffd1d5e5 2018-06-23 stsp if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
2595 ffd1d5e5 2018-06-23 stsp err(1, "pledge");
2596 ffd1d5e5 2018-06-23 stsp #endif
2597 ffd1d5e5 2018-06-23 stsp
2598 ffd1d5e5 2018-06-23 stsp while ((ch = getopt(argc, argv, "c:")) != -1) {
2599 ffd1d5e5 2018-06-23 stsp switch (ch) {
2600 ffd1d5e5 2018-06-23 stsp case 'c':
2601 ffd1d5e5 2018-06-23 stsp commit_id_arg = optarg;
2602 ffd1d5e5 2018-06-23 stsp break;
2603 ffd1d5e5 2018-06-23 stsp default:
2604 ffd1d5e5 2018-06-23 stsp usage();
2605 ffd1d5e5 2018-06-23 stsp /* NOTREACHED */
2606 ffd1d5e5 2018-06-23 stsp }
2607 ffd1d5e5 2018-06-23 stsp }
2608 ffd1d5e5 2018-06-23 stsp
2609 ffd1d5e5 2018-06-23 stsp argc -= optind;
2610 ffd1d5e5 2018-06-23 stsp argv += optind;
2611 ffd1d5e5 2018-06-23 stsp
2612 ffd1d5e5 2018-06-23 stsp if (argc == 0) {
2613 ffd1d5e5 2018-06-23 stsp repo_path = getcwd(NULL, 0);
2614 ffd1d5e5 2018-06-23 stsp if (repo_path == NULL)
2615 ffd1d5e5 2018-06-23 stsp return got_error_from_errno();
2616 ffd1d5e5 2018-06-23 stsp } else if (argc == 1) {
2617 ffd1d5e5 2018-06-23 stsp repo_path = realpath(argv[0], NULL);
2618 ffd1d5e5 2018-06-23 stsp if (repo_path == NULL)
2619 ffd1d5e5 2018-06-23 stsp return got_error_from_errno();
2620 ffd1d5e5 2018-06-23 stsp } else
2621 ffd1d5e5 2018-06-23 stsp usage_log();
2622 ffd1d5e5 2018-06-23 stsp
2623 ffd1d5e5 2018-06-23 stsp error = got_repo_open(&repo, repo_path);
2624 ffd1d5e5 2018-06-23 stsp free(repo_path);
2625 ffd1d5e5 2018-06-23 stsp if (error != NULL)
2626 ffd1d5e5 2018-06-23 stsp return error;
2627 ffd1d5e5 2018-06-23 stsp
2628 ffd1d5e5 2018-06-23 stsp if (commit_id_arg == NULL) {
2629 ffd1d5e5 2018-06-23 stsp error = get_head_commit_id(&commit_id, repo);
2630 ffd1d5e5 2018-06-23 stsp if (error != NULL)
2631 ffd1d5e5 2018-06-23 stsp goto done;
2632 ffd1d5e5 2018-06-23 stsp } else {
2633 ffd1d5e5 2018-06-23 stsp struct got_object *obj;
2634 ffd1d5e5 2018-06-23 stsp error = got_object_open_by_id_str(&obj, repo, commit_id_arg);
2635 ffd1d5e5 2018-06-23 stsp if (error == NULL) {
2636 ffd1d5e5 2018-06-23 stsp commit_id = got_object_get_id(obj);
2637 ffd1d5e5 2018-06-23 stsp if (commit_id == NULL)
2638 ffd1d5e5 2018-06-23 stsp error = got_error_from_errno();
2639 ffd1d5e5 2018-06-23 stsp }
2640 ffd1d5e5 2018-06-23 stsp }
2641 ffd1d5e5 2018-06-23 stsp if (error != NULL)
2642 ffd1d5e5 2018-06-23 stsp goto done;
2643 ffd1d5e5 2018-06-23 stsp
2644 ffd1d5e5 2018-06-23 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
2645 ffd1d5e5 2018-06-23 stsp if (error != NULL)
2646 ffd1d5e5 2018-06-23 stsp goto done;
2647 ffd1d5e5 2018-06-23 stsp
2648 ffd1d5e5 2018-06-23 stsp error = got_object_open_as_tree(&tree, repo, commit->tree_id);
2649 ffd1d5e5 2018-06-23 stsp if (error != NULL)
2650 ffd1d5e5 2018-06-23 stsp goto done;
2651 ffd1d5e5 2018-06-23 stsp
2652 b3665f43 2018-08-04 stsp view = view_open(0, 0, 0, 0, NULL, TOG_VIEW_TREE);
2653 5221c383 2018-08-01 stsp if (view == NULL) {
2654 5221c383 2018-08-01 stsp error = got_error_from_errno();
2655 5221c383 2018-08-01 stsp goto done;
2656 5221c383 2018-08-01 stsp }
2657 5221c383 2018-08-01 stsp error = show_tree_view(view, tree, commit_id, repo);
2658 96a765a8 2018-08-04 stsp view_close(view);
2659 ffd1d5e5 2018-06-23 stsp done:
2660 ffd1d5e5 2018-06-23 stsp free(commit_id);
2661 ffd1d5e5 2018-06-23 stsp if (commit)
2662 ffd1d5e5 2018-06-23 stsp got_object_commit_close(commit);
2663 ffd1d5e5 2018-06-23 stsp if (tree)
2664 ffd1d5e5 2018-06-23 stsp got_object_tree_close(tree);
2665 ffd1d5e5 2018-06-23 stsp if (repo)
2666 ffd1d5e5 2018-06-23 stsp got_repo_close(repo);
2667 ffd1d5e5 2018-06-23 stsp return error;
2668 ffd1d5e5 2018-06-23 stsp }
2669 5c5136c5 2018-05-20 stsp static void
2670 9f7d7167 2018-04-29 stsp init_curses(void)
2671 9f7d7167 2018-04-29 stsp {
2672 9f7d7167 2018-04-29 stsp initscr();
2673 9f7d7167 2018-04-29 stsp cbreak();
2674 9f7d7167 2018-04-29 stsp noecho();
2675 9f7d7167 2018-04-29 stsp nonl();
2676 9f7d7167 2018-04-29 stsp intrflush(stdscr, FALSE);
2677 9f7d7167 2018-04-29 stsp keypad(stdscr, TRUE);
2678 1f475ad8 2018-05-10 stsp curs_set(0);
2679 9f7d7167 2018-04-29 stsp }
2680 9f7d7167 2018-04-29 stsp
2681 4ed7e80c 2018-05-20 stsp __dead static void
2682 9f7d7167 2018-04-29 stsp usage(void)
2683 9f7d7167 2018-04-29 stsp {
2684 9f7d7167 2018-04-29 stsp int i;
2685 9f7d7167 2018-04-29 stsp
2686 c2301be8 2018-04-30 stsp fprintf(stderr, "usage: %s [-h] [command] [arg ...]\n\n"
2687 9f7d7167 2018-04-29 stsp "Available commands:\n", getprogname());
2688 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
2689 9f7d7167 2018-04-29 stsp struct tog_cmd *cmd = &tog_commands[i];
2690 c2301be8 2018-04-30 stsp fprintf(stderr, " %s: %s\n", cmd->name, cmd->descr);
2691 9f7d7167 2018-04-29 stsp }
2692 9f7d7167 2018-04-29 stsp exit(1);
2693 9f7d7167 2018-04-29 stsp }
2694 9f7d7167 2018-04-29 stsp
2695 c2301be8 2018-04-30 stsp static char **
2696 c2301be8 2018-04-30 stsp make_argv(const char *arg0, const char *arg1)
2697 c2301be8 2018-04-30 stsp {
2698 c2301be8 2018-04-30 stsp char **argv;
2699 c2301be8 2018-04-30 stsp int argc = (arg1 == NULL ? 1 : 2);
2700 c2301be8 2018-04-30 stsp
2701 c2301be8 2018-04-30 stsp argv = calloc(argc, sizeof(char *));
2702 c2301be8 2018-04-30 stsp if (argv == NULL)
2703 c2301be8 2018-04-30 stsp err(1, "calloc");
2704 c2301be8 2018-04-30 stsp argv[0] = strdup(arg0);
2705 c2301be8 2018-04-30 stsp if (argv[0] == NULL)
2706 c2301be8 2018-04-30 stsp err(1, "calloc");
2707 c2301be8 2018-04-30 stsp if (arg1) {
2708 c2301be8 2018-04-30 stsp argv[1] = strdup(arg1);
2709 c2301be8 2018-04-30 stsp if (argv[1] == NULL)
2710 c2301be8 2018-04-30 stsp err(1, "calloc");
2711 c2301be8 2018-04-30 stsp }
2712 c2301be8 2018-04-30 stsp
2713 c2301be8 2018-04-30 stsp return argv;
2714 c2301be8 2018-04-30 stsp }
2715 c2301be8 2018-04-30 stsp
2716 9f7d7167 2018-04-29 stsp int
2717 9f7d7167 2018-04-29 stsp main(int argc, char *argv[])
2718 9f7d7167 2018-04-29 stsp {
2719 9f7d7167 2018-04-29 stsp const struct got_error *error = NULL;
2720 9f7d7167 2018-04-29 stsp struct tog_cmd *cmd = NULL;
2721 9f7d7167 2018-04-29 stsp int ch, hflag = 0;
2722 c2301be8 2018-04-30 stsp char **cmd_argv = NULL;
2723 9f7d7167 2018-04-29 stsp
2724 9f7d7167 2018-04-29 stsp setlocale(LC_ALL, "");
2725 9f7d7167 2018-04-29 stsp
2726 9f7d7167 2018-04-29 stsp while ((ch = getopt(argc, argv, "h")) != -1) {
2727 9f7d7167 2018-04-29 stsp switch (ch) {
2728 9f7d7167 2018-04-29 stsp case 'h':
2729 9f7d7167 2018-04-29 stsp hflag = 1;
2730 9f7d7167 2018-04-29 stsp break;
2731 9f7d7167 2018-04-29 stsp default:
2732 9f7d7167 2018-04-29 stsp usage();
2733 9f7d7167 2018-04-29 stsp /* NOTREACHED */
2734 9f7d7167 2018-04-29 stsp }
2735 9f7d7167 2018-04-29 stsp }
2736 9f7d7167 2018-04-29 stsp
2737 9f7d7167 2018-04-29 stsp argc -= optind;
2738 9f7d7167 2018-04-29 stsp argv += optind;
2739 9f7d7167 2018-04-29 stsp optind = 0;
2740 c2301be8 2018-04-30 stsp optreset = 1;
2741 9f7d7167 2018-04-29 stsp
2742 c2301be8 2018-04-30 stsp if (argc == 0) {
2743 f29d3e89 2018-06-23 stsp if (hflag)
2744 f29d3e89 2018-06-23 stsp usage();
2745 c2301be8 2018-04-30 stsp /* Build an argument vector which runs a default command. */
2746 9f7d7167 2018-04-29 stsp cmd = &tog_commands[0];
2747 c2301be8 2018-04-30 stsp cmd_argv = make_argv(cmd->name, NULL);
2748 c2301be8 2018-04-30 stsp argc = 1;
2749 c2301be8 2018-04-30 stsp } else {
2750 9f7d7167 2018-04-29 stsp int i;
2751 9f7d7167 2018-04-29 stsp
2752 c2301be8 2018-04-30 stsp /* Did the user specific a command? */
2753 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
2754 c2301be8 2018-04-30 stsp if (strncmp(tog_commands[i].name, argv[0],
2755 9f7d7167 2018-04-29 stsp strlen(argv[0])) == 0) {
2756 9f7d7167 2018-04-29 stsp cmd = &tog_commands[i];
2757 9f7d7167 2018-04-29 stsp if (hflag)
2758 9f7d7167 2018-04-29 stsp tog_commands[i].cmd_usage();
2759 9f7d7167 2018-04-29 stsp break;
2760 9f7d7167 2018-04-29 stsp }
2761 9f7d7167 2018-04-29 stsp }
2762 9f7d7167 2018-04-29 stsp if (cmd == NULL) {
2763 c2301be8 2018-04-30 stsp /* Did the user specify a repository? */
2764 c2301be8 2018-04-30 stsp char *repo_path = realpath(argv[0], NULL);
2765 c2301be8 2018-04-30 stsp if (repo_path) {
2766 c2301be8 2018-04-30 stsp struct got_repository *repo;
2767 c2301be8 2018-04-30 stsp error = got_repo_open(&repo, repo_path);
2768 c2301be8 2018-04-30 stsp if (error == NULL)
2769 c2301be8 2018-04-30 stsp got_repo_close(repo);
2770 c2301be8 2018-04-30 stsp } else
2771 ad7de8d9 2018-04-30 stsp error = got_error_from_errno();
2772 c2301be8 2018-04-30 stsp if (error) {
2773 f29d3e89 2018-06-23 stsp if (hflag) {
2774 f29d3e89 2018-06-23 stsp fprintf(stderr, "%s: '%s' is not a "
2775 f29d3e89 2018-06-23 stsp "known command\n", getprogname(),
2776 f29d3e89 2018-06-23 stsp argv[0]);
2777 f29d3e89 2018-06-23 stsp usage();
2778 f29d3e89 2018-06-23 stsp }
2779 ad7de8d9 2018-04-30 stsp fprintf(stderr, "%s: '%s' is neither a known "
2780 ad7de8d9 2018-04-30 stsp "command nor a path to a repository\n",
2781 c2301be8 2018-04-30 stsp getprogname(), argv[0]);
2782 ad7de8d9 2018-04-30 stsp free(repo_path);
2783 c2301be8 2018-04-30 stsp return 1;
2784 c2301be8 2018-04-30 stsp }
2785 c2301be8 2018-04-30 stsp cmd = &tog_commands[0];
2786 c2301be8 2018-04-30 stsp cmd_argv = make_argv(cmd->name, repo_path);
2787 c2301be8 2018-04-30 stsp argc = 2;
2788 c2301be8 2018-04-30 stsp free(repo_path);
2789 9f7d7167 2018-04-29 stsp }
2790 9f7d7167 2018-04-29 stsp }
2791 9f7d7167 2018-04-29 stsp
2792 5c5136c5 2018-05-20 stsp init_curses();
2793 9f7d7167 2018-04-29 stsp
2794 c2301be8 2018-04-30 stsp error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
2795 9f7d7167 2018-04-29 stsp if (error)
2796 9f7d7167 2018-04-29 stsp goto done;
2797 9f7d7167 2018-04-29 stsp done:
2798 9f7d7167 2018-04-29 stsp endwin();
2799 c2301be8 2018-04-30 stsp free(cmd_argv);
2800 9f7d7167 2018-04-29 stsp if (error)
2801 9f7d7167 2018-04-29 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
2802 9f7d7167 2018-04-29 stsp return 0;
2803 9f7d7167 2018-04-29 stsp }