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