2 9f7d7167 2018-04-29 stsp * Copyright (c) 2018 Stefan Sperling <stsp@openbsd.org>
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.
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.
17 80ddbec8 2018-04-29 stsp #include <sys/queue.h>
18 ffd1d5e5 2018-06-23 stsp #include <sys/stat.h>
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>
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"
49 881b2d3e 2018-04-30 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
52 9f7d7167 2018-04-29 stsp #ifndef nitems
53 9f7d7167 2018-04-29 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
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;
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);
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 *[]);
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" },
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 ad80ab7b 2018-08-04 stsp TOG_VIEW_BLAME,
89 ad80ab7b 2018-08-04 stsp TOG_VIEW_TREE
92 ad80ab7b 2018-08-04 stsp struct tog_diff_view_state {
94 ad80ab7b 2018-08-04 stsp int first_displayed_line;
95 ad80ab7b 2018-08-04 stsp int last_displayed_line;
98 ba4f502b 2018-08-04 stsp struct commit_queue_entry {
99 ba4f502b 2018-08-04 stsp TAILQ_ENTRY(commit_queue_entry) entry;
100 ba4f502b 2018-08-04 stsp struct got_object_id *id;
101 ba4f502b 2018-08-04 stsp struct got_commit_object *commit;
103 ba4f502b 2018-08-04 stsp TAILQ_HEAD(commit_queue_head, commit_queue_entry);
104 ba4f502b 2018-08-04 stsp struct commit_queue {
105 ba4f502b 2018-08-04 stsp int ncommits;
106 ba4f502b 2018-08-04 stsp struct commit_queue_head head;
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;
120 e9424729 2018-08-04 stsp struct tog_blame_cb_args {
121 e9424729 2018-08-04 stsp pthread_mutex_t *mutex;
122 e9424729 2018-08-04 stsp struct tog_blame_line *lines; /* one per line */
123 e9424729 2018-08-04 stsp int nlines;
125 e9424729 2018-08-04 stsp struct tog_view *view;
126 e9424729 2018-08-04 stsp struct got_object_id *commit_id;
128 e9424729 2018-08-04 stsp const char *path;
129 e9424729 2018-08-04 stsp int *first_displayed_line;
130 e9424729 2018-08-04 stsp int *last_displayed_line;
131 e9424729 2018-08-04 stsp int *selected_line;
135 e9424729 2018-08-04 stsp struct tog_blame_thread_args {
136 e9424729 2018-08-04 stsp const char *path;
137 e9424729 2018-08-04 stsp struct got_repository *repo;
138 e9424729 2018-08-04 stsp struct tog_blame_cb_args *cb_args;
139 e9424729 2018-08-04 stsp int *complete;
142 e9424729 2018-08-04 stsp struct tog_blame {
144 e9424729 2018-08-04 stsp size_t filesize;
145 e9424729 2018-08-04 stsp struct tog_blame_line *lines;
146 e9424729 2018-08-04 stsp size_t nlines;
147 e9424729 2018-08-04 stsp pthread_t thread;
148 e9424729 2018-08-04 stsp struct tog_blame_thread_args thread_args;
149 e9424729 2018-08-04 stsp struct tog_blame_cb_args cb_args;
150 e9424729 2018-08-04 stsp const char *path;
153 7cbe629d 2018-08-04 stsp struct tog_blame_view_state {
154 7cbe629d 2018-08-04 stsp int first_displayed_line;
155 7cbe629d 2018-08-04 stsp int last_displayed_line;
156 7cbe629d 2018-08-04 stsp int selected_line;
157 7cbe629d 2018-08-04 stsp int blame_complete;
158 7cbe629d 2018-08-04 stsp pthread_mutex_t mutex;
159 7cbe629d 2018-08-04 stsp struct got_object_id_queue blamed_commits;
160 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
161 7cbe629d 2018-08-04 stsp const char *path;
162 7cbe629d 2018-08-04 stsp struct got_repository *repo;
163 ad80ab7b 2018-08-04 stsp struct got_object_id *commit_id;
164 e9424729 2018-08-04 stsp struct tog_blame blame;
167 ad80ab7b 2018-08-04 stsp struct tog_parent_tree {
168 ad80ab7b 2018-08-04 stsp TAILQ_ENTRY(tog_parent_tree) entry;
169 ad80ab7b 2018-08-04 stsp struct got_tree_object *tree;
170 ad80ab7b 2018-08-04 stsp struct got_tree_entry *first_displayed_entry;
171 ad80ab7b 2018-08-04 stsp struct got_tree_entry *selected_entry;
172 ad80ab7b 2018-08-04 stsp int selected;
175 ad80ab7b 2018-08-04 stsp TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
177 ad80ab7b 2018-08-04 stsp struct tog_tree_view_state {
178 ad80ab7b 2018-08-04 stsp char *tree_label;
179 ad80ab7b 2018-08-04 stsp struct got_tree_object *root;
180 ad80ab7b 2018-08-04 stsp struct got_tree_object *tree;
181 ad80ab7b 2018-08-04 stsp const struct got_tree_entries *entries;
182 ad80ab7b 2018-08-04 stsp struct got_tree_entry *first_displayed_entry;
183 ad80ab7b 2018-08-04 stsp struct got_tree_entry *last_displayed_entry;
184 ad80ab7b 2018-08-04 stsp struct got_tree_entry *selected_entry;
185 ad80ab7b 2018-08-04 stsp int nentries, ndisplayed, selected, show_ids;
186 ad80ab7b 2018-08-04 stsp struct tog_parent_trees parents;
187 7cbe629d 2018-08-04 stsp struct got_object_id *commit_id;
188 ad80ab7b 2018-08-04 stsp struct got_repository *repo;
191 cc3c9aac 2018-08-01 stsp struct tog_view {
192 26ed57b2 2018-05-19 stsp WINDOW *window;
193 26ed57b2 2018-05-19 stsp PANEL *panel;
194 97ddc146 2018-08-01 stsp int nlines, ncols, begin_y, begin_x;
195 f7d12f7e 2018-08-01 stsp int lines, cols; /* copies of LINES and COLS */
196 6d0fee91 2018-08-01 stsp struct tog_view *parent;
198 5dc9f4bc 2018-08-04 stsp /* type-specific state */
199 d6b05b5b 2018-08-04 stsp enum tog_view_type type;
201 b01e7d3b 2018-08-04 stsp struct tog_diff_view_state diff;
202 b01e7d3b 2018-08-04 stsp struct tog_log_view_state log;
203 7cbe629d 2018-08-04 stsp struct tog_blame_view_state blame;
204 ad80ab7b 2018-08-04 stsp struct tog_tree_view_state tree;
208 ba4f502b 2018-08-04 stsp static const struct got_error *open_diff_view(struct tog_view *,
209 ba4f502b 2018-08-04 stsp struct got_object *, struct got_object *, struct got_repository *);
210 5dc9f4bc 2018-08-04 stsp static const struct got_error *show_diff_view(struct tog_view *);
211 5dc9f4bc 2018-08-04 stsp static void close_diff_view(struct tog_view *);
212 ba4f502b 2018-08-04 stsp static const struct got_error *open_log_view(struct tog_view *,
213 ba4f502b 2018-08-04 stsp struct got_object_id *, struct got_repository *, const char *);
214 ba4f502b 2018-08-04 stsp static const struct got_error * show_log_view(struct tog_view *);
215 ba4f502b 2018-08-04 stsp static void close_log_view(struct tog_view *);
216 7cbe629d 2018-08-04 stsp static const struct got_error *open_blame_view(struct tog_view *, const char *,
217 5221c383 2018-08-01 stsp struct got_object_id *, struct got_repository *);
218 7cbe629d 2018-08-04 stsp static const struct got_error *show_blame_view(struct tog_view *);
219 7cbe629d 2018-08-04 stsp static void close_blame_view(struct tog_view *);
220 ad80ab7b 2018-08-04 stsp static const struct got_error *open_tree_view(struct tog_view *,
221 4fc679ca 2018-08-04 stsp struct got_tree_object *, struct got_object_id *, struct got_repository *);
222 ad80ab7b 2018-08-04 stsp static const struct got_error *show_tree_view(struct tog_view *);
223 ad80ab7b 2018-08-04 stsp static void close_tree_view(struct tog_view *);
225 ea5e7bb5 2018-08-01 stsp static void
226 96a765a8 2018-08-04 stsp view_close(struct tog_view *view)
228 ea5e7bb5 2018-08-01 stsp if (view->panel)
229 ea5e7bb5 2018-08-01 stsp del_panel(view->panel);
230 ea5e7bb5 2018-08-01 stsp if (view->window)
231 ea5e7bb5 2018-08-01 stsp delwin(view->window);
232 ea5e7bb5 2018-08-01 stsp free(view);
235 ea5e7bb5 2018-08-01 stsp static struct tog_view *
236 b3665f43 2018-08-04 stsp view_open(int nlines, int ncols, int begin_y, int begin_x,
237 d6b05b5b 2018-08-04 stsp struct tog_view *parent, enum tog_view_type type)
239 ad80ab7b 2018-08-04 stsp struct tog_view *view = calloc(1, sizeof(*view));
241 ea5e7bb5 2018-08-01 stsp if (view == NULL)
242 ea5e7bb5 2018-08-01 stsp return NULL;
244 6d0fee91 2018-08-01 stsp view->parent = parent;
245 d6b05b5b 2018-08-04 stsp view->type = type;
246 f7d12f7e 2018-08-01 stsp view->lines = LINES;
247 f7d12f7e 2018-08-01 stsp view->cols = COLS;
248 207b9029 2018-08-01 stsp view->nlines = nlines ? nlines : LINES - begin_y;
249 207b9029 2018-08-01 stsp view->ncols = ncols ? ncols : COLS - begin_x;
250 97ddc146 2018-08-01 stsp view->begin_y = begin_y;
251 97ddc146 2018-08-01 stsp view->begin_x = begin_x;
252 842167bf 2018-08-01 stsp view->window = newwin(nlines, ncols, begin_y, begin_x);
253 ea5e7bb5 2018-08-01 stsp if (view->window == NULL) {
254 96a765a8 2018-08-04 stsp view_close(view);
255 ea5e7bb5 2018-08-01 stsp return NULL;
257 ea5e7bb5 2018-08-01 stsp view->panel = new_panel(view->window);
258 ea5e7bb5 2018-08-01 stsp if (view->panel == NULL) {
259 96a765a8 2018-08-04 stsp view_close(view);
260 ea5e7bb5 2018-08-01 stsp return NULL;
263 ea5e7bb5 2018-08-01 stsp keypad(view->window, TRUE);
264 ea5e7bb5 2018-08-01 stsp return view;
268 6acd1bd1 2018-08-04 stsp view_show(struct tog_view *view)
270 cdf1ee82 2018-08-01 stsp show_panel(view->panel);
271 cdf1ee82 2018-08-01 stsp update_panels();
274 f7d12f7e 2018-08-01 stsp const struct got_error *
275 f7d12f7e 2018-08-01 stsp view_resize(struct tog_view *view)
277 f7d12f7e 2018-08-01 stsp int nlines, ncols;
279 6d0fee91 2018-08-01 stsp while (view) {
280 6d0fee91 2018-08-01 stsp if (view->lines > LINES)
281 6d0fee91 2018-08-01 stsp nlines = view->nlines - (view->lines - LINES);
283 6d0fee91 2018-08-01 stsp nlines = view->nlines + (LINES - view->lines);
285 6d0fee91 2018-08-01 stsp if (view->cols > COLS)
286 6d0fee91 2018-08-01 stsp ncols = view->ncols - (view->cols - COLS);
288 6d0fee91 2018-08-01 stsp ncols = view->ncols + (COLS - view->cols);
290 6d0fee91 2018-08-01 stsp if (wresize(view->window, nlines, ncols) == ERR)
291 6d0fee91 2018-08-01 stsp return got_error_from_errno();
292 77a567dd 2018-08-01 stsp replace_panel(view->panel, view->window);
294 6d0fee91 2018-08-01 stsp view->nlines = nlines;
295 6d0fee91 2018-08-01 stsp view->ncols = ncols;
296 6d0fee91 2018-08-01 stsp view->lines = LINES;
297 6d0fee91 2018-08-01 stsp view->cols = COLS;
299 6d0fee91 2018-08-01 stsp view = view->parent;
302 f7d12f7e 2018-08-01 stsp return NULL;
305 4ed7e80c 2018-05-20 stsp __dead static void
306 9f7d7167 2018-04-29 stsp usage_log(void)
309 c70c5802 2018-08-01 stsp fprintf(stderr,
310 c70c5802 2018-08-01 stsp "usage: %s log [-c commit] [-r repository-path] [path]\n",
311 9f7d7167 2018-04-29 stsp getprogname());
315 963b370f 2018-05-20 stsp /* Create newly allocated wide-character string equivalent to a byte string. */
316 80ddbec8 2018-04-29 stsp static const struct got_error *
317 963b370f 2018-05-20 stsp mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
319 00dfcb92 2018-06-11 stsp char *vis = NULL;
320 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
322 963b370f 2018-05-20 stsp *ws = NULL;
323 963b370f 2018-05-20 stsp *wlen = mbstowcs(NULL, s, 0);
324 00dfcb92 2018-06-11 stsp if (*wlen == (size_t)-1) {
325 00dfcb92 2018-06-11 stsp int vislen;
326 00dfcb92 2018-06-11 stsp if (errno != EILSEQ)
327 00dfcb92 2018-06-11 stsp return got_error_from_errno();
329 00dfcb92 2018-06-11 stsp /* byte string invalid in current encoding; try to "fix" it */
330 00dfcb92 2018-06-11 stsp err = got_mbsavis(&vis, &vislen, s);
332 00dfcb92 2018-06-11 stsp return err;
333 00dfcb92 2018-06-11 stsp *wlen = mbstowcs(NULL, vis, 0);
334 a7f50699 2018-06-11 stsp if (*wlen == (size_t)-1) {
335 a7f50699 2018-06-11 stsp err = got_error_from_errno(); /* give up */
340 963b370f 2018-05-20 stsp *ws = calloc(*wlen + 1, sizeof(*ws));
341 a7f50699 2018-06-11 stsp if (*ws == NULL) {
342 a7f50699 2018-06-11 stsp err = got_error_from_errno();
346 00dfcb92 2018-06-11 stsp if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
347 963b370f 2018-05-20 stsp err = got_error_from_errno();
352 963b370f 2018-05-20 stsp *ws = NULL;
355 963b370f 2018-05-20 stsp return err;
358 963b370f 2018-05-20 stsp /* Format a line for display, ensuring that it won't overflow a width limit. */
359 963b370f 2018-05-20 stsp static const struct got_error *
360 ffd1d5e5 2018-06-23 stsp format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit)
362 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
363 963b370f 2018-05-20 stsp int cols = 0;
364 963b370f 2018-05-20 stsp wchar_t *wline = NULL;
365 963b370f 2018-05-20 stsp size_t wlen;
368 963b370f 2018-05-20 stsp *wlinep = NULL;
369 b700b5d6 2018-07-10 stsp *widthp = 0;
371 963b370f 2018-05-20 stsp err = mbs2ws(&wline, &wlen, line);
373 963b370f 2018-05-20 stsp return err;
376 b700b5d6 2018-07-10 stsp while (i < wlen && cols < wlimit) {
377 963b370f 2018-05-20 stsp int width = wcwidth(wline[i]);
378 963b370f 2018-05-20 stsp switch (width) {
384 b700b5d6 2018-07-10 stsp if (cols + width <= wlimit) {
385 b700b5d6 2018-07-10 stsp cols += width;
390 963b370f 2018-05-20 stsp if (wline[i] == L'\t')
391 b700b5d6 2018-07-10 stsp cols += TABSIZE - ((cols + 1) % TABSIZE);
395 963b370f 2018-05-20 stsp err = got_error_from_errno();
399 963b370f 2018-05-20 stsp wline[i] = L'\0';
400 b700b5d6 2018-07-10 stsp if (widthp)
401 b700b5d6 2018-07-10 stsp *widthp = cols;
404 963b370f 2018-05-20 stsp free(wline);
406 963b370f 2018-05-20 stsp *wlinep = wline;
407 963b370f 2018-05-20 stsp return err;
410 963b370f 2018-05-20 stsp static const struct got_error *
411 2814baeb 2018-08-01 stsp draw_commit(struct tog_view *view, struct got_commit_object *commit,
412 2814baeb 2018-08-01 stsp struct got_object_id *id)
414 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
415 b39d25c7 2018-07-10 stsp char datebuf[10]; /* YY-MM-DD + SPACE + NUL */
416 80ddbec8 2018-04-29 stsp char *logmsg0 = NULL, *logmsg = NULL;
417 6d9fbc00 2018-04-29 stsp char *author0 = NULL, *author = NULL;
418 bb737323 2018-05-20 stsp wchar_t *wlogmsg = NULL, *wauthor = NULL;
419 bb737323 2018-05-20 stsp int author_width, logmsg_width;
420 6d9fbc00 2018-04-29 stsp char *newline, *smallerthan;
421 80ddbec8 2018-04-29 stsp char *line = NULL;
422 bb737323 2018-05-20 stsp int col, limit;
423 b39d25c7 2018-07-10 stsp static const size_t date_display_cols = 9;
424 bb737323 2018-05-20 stsp static const size_t author_display_cols = 16;
425 f7d12f7e 2018-08-01 stsp const int avail = view->ncols;
427 c70c5802 2018-08-01 stsp if (strftime(datebuf, sizeof(datebuf), "%g/%m/%d ",
428 c70c5802 2018-08-01 stsp &commit->tm_committer) >= sizeof(datebuf))
429 b39d25c7 2018-07-10 stsp return got_error(GOT_ERR_NO_SPACE);
431 b39d25c7 2018-07-10 stsp if (avail < date_display_cols)
432 b39d25c7 2018-07-10 stsp limit = MIN(sizeof(datebuf) - 1, avail);
434 b39d25c7 2018-07-10 stsp limit = MIN(date_display_cols, sizeof(datebuf) - 1);
435 2814baeb 2018-08-01 stsp waddnstr(view->window, datebuf, limit);
436 b39d25c7 2018-07-10 stsp col = limit + 1;
437 b39d25c7 2018-07-10 stsp if (col > avail)
440 6d9fbc00 2018-04-29 stsp author0 = strdup(commit->author);
441 6d9fbc00 2018-04-29 stsp if (author0 == NULL) {
442 80ddbec8 2018-04-29 stsp err = got_error_from_errno();
445 6d9fbc00 2018-04-29 stsp author = author0;
446 6d9fbc00 2018-04-29 stsp smallerthan = strchr(author, '<');
447 6d9fbc00 2018-04-29 stsp if (smallerthan)
448 6d9fbc00 2018-04-29 stsp *smallerthan = '\0';
450 6d9fbc00 2018-04-29 stsp char *at = strchr(author, '@');
452 6d9fbc00 2018-04-29 stsp *at = '\0';
454 b39d25c7 2018-07-10 stsp limit = avail - col;
455 bb737323 2018-05-20 stsp err = format_line(&wauthor, &author_width, author, limit);
458 2814baeb 2018-08-01 stsp waddwstr(view->window, wauthor);
459 bb737323 2018-05-20 stsp col += author_width;
460 9c2eaf34 2018-05-20 stsp while (col <= avail && author_width < author_display_cols + 1) {
461 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
463 bb737323 2018-05-20 stsp author_width++;
465 9c2eaf34 2018-05-20 stsp if (col > avail)
468 bb737323 2018-05-20 stsp logmsg0 = strdup(commit->logmsg);
469 bb737323 2018-05-20 stsp if (logmsg0 == NULL) {
470 6d9fbc00 2018-04-29 stsp err = got_error_from_errno();
473 bb737323 2018-05-20 stsp logmsg = logmsg0;
474 bb737323 2018-05-20 stsp while (*logmsg == '\n')
476 bb737323 2018-05-20 stsp newline = strchr(logmsg, '\n');
477 bb737323 2018-05-20 stsp if (newline)
478 bb737323 2018-05-20 stsp *newline = '\0';
479 bb737323 2018-05-20 stsp limit = avail - col;
480 bb737323 2018-05-20 stsp err = format_line(&wlogmsg, &logmsg_width, logmsg, limit);
483 2814baeb 2018-08-01 stsp waddwstr(view->window, wlogmsg);
484 bb737323 2018-05-20 stsp col += logmsg_width;
485 bb737323 2018-05-20 stsp while (col <= avail) {
486 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
490 80ddbec8 2018-04-29 stsp free(logmsg0);
491 bb737323 2018-05-20 stsp free(wlogmsg);
492 6d9fbc00 2018-04-29 stsp free(author0);
493 bb737323 2018-05-20 stsp free(wauthor);
494 80ddbec8 2018-04-29 stsp free(line);
495 80ddbec8 2018-04-29 stsp return err;
498 899d86c2 2018-05-10 stsp static struct commit_queue_entry *
499 899d86c2 2018-05-10 stsp alloc_commit_queue_entry(struct got_commit_object *commit,
500 899d86c2 2018-05-10 stsp struct got_object_id *id)
502 80ddbec8 2018-04-29 stsp struct commit_queue_entry *entry;
504 80ddbec8 2018-04-29 stsp entry = calloc(1, sizeof(*entry));
505 80ddbec8 2018-04-29 stsp if (entry == NULL)
506 899d86c2 2018-05-10 stsp return NULL;
508 899d86c2 2018-05-10 stsp entry->id = id;
509 99db9666 2018-05-07 stsp entry->commit = commit;
510 899d86c2 2018-05-10 stsp return entry;
513 99db9666 2018-05-07 stsp static void
514 99db9666 2018-05-07 stsp pop_commit(struct commit_queue *commits)
516 99db9666 2018-05-07 stsp struct commit_queue_entry *entry;
518 ecb28ae0 2018-07-16 stsp entry = TAILQ_FIRST(&commits->head);
519 ecb28ae0 2018-07-16 stsp TAILQ_REMOVE(&commits->head, entry, entry);
520 99db9666 2018-05-07 stsp got_object_commit_close(entry->commit);
521 ecb28ae0 2018-07-16 stsp commits->ncommits--;
522 9ba79e04 2018-06-11 stsp /* Don't free entry->id! It is owned by the commit graph. */
523 99db9666 2018-05-07 stsp free(entry);
526 99db9666 2018-05-07 stsp static void
527 99db9666 2018-05-07 stsp free_commits(struct commit_queue *commits)
529 ecb28ae0 2018-07-16 stsp while (!TAILQ_EMPTY(&commits->head))
530 99db9666 2018-05-07 stsp pop_commit(commits);
533 c4972b91 2018-05-07 stsp static const struct got_error *
534 9ba79e04 2018-06-11 stsp queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
535 ecb28ae0 2018-07-16 stsp struct got_object_id *start_id, int minqueue, int init,
536 ecb28ae0 2018-07-16 stsp struct got_repository *repo, const char *path)
538 899d86c2 2018-05-10 stsp const struct got_error *err = NULL;
539 899d86c2 2018-05-10 stsp struct got_object_id *id;
540 9ba79e04 2018-06-11 stsp struct commit_queue_entry *entry;
541 ecb28ae0 2018-07-16 stsp int nfetched, nqueued = 0, found_obj = 0;
542 c8f60bff 2018-07-23 stsp int is_root_path = strcmp(path, "/") == 0;
544 9ba79e04 2018-06-11 stsp err = got_commit_graph_iter_start(graph, start_id);
546 c4972b91 2018-05-07 stsp return err;
548 ecb28ae0 2018-07-16 stsp entry = TAILQ_LAST(&commits->head, commit_queue_head);
549 9ba79e04 2018-06-11 stsp if (entry && got_object_id_cmp(entry->id, start_id) == 0) {
550 9ba79e04 2018-06-11 stsp int nfetched;
552 9ba79e04 2018-06-11 stsp /* Start ID's commit is already on the queue; skip over it. */
553 9ba79e04 2018-06-11 stsp err = got_commit_graph_iter_next(&id, graph);
554 9ba79e04 2018-06-11 stsp if (err && err->code != GOT_ERR_ITER_NEED_MORE)
555 9ba79e04 2018-06-11 stsp return err;
557 9ba79e04 2018-06-11 stsp err = got_commit_graph_fetch_commits(&nfetched, graph, 1, repo);
559 9ba79e04 2018-06-11 stsp return err;
562 9ba79e04 2018-06-11 stsp while (1) {
563 9ba79e04 2018-06-11 stsp struct got_commit_object *commit;
565 9ba79e04 2018-06-11 stsp err = got_commit_graph_iter_next(&id, graph);
567 ecb28ae0 2018-07-16 stsp if (err->code != GOT_ERR_ITER_NEED_MORE)
569 ecb28ae0 2018-07-16 stsp if (nqueued >= minqueue) {
570 9ba79e04 2018-06-11 stsp err = NULL;
573 ecb28ae0 2018-07-16 stsp err = got_commit_graph_fetch_commits(&nfetched,
574 ecb28ae0 2018-07-16 stsp graph, 1, repo);
576 ecb28ae0 2018-07-16 stsp return err;
579 ecb28ae0 2018-07-16 stsp if (id == NULL)
582 9ba79e04 2018-06-11 stsp err = got_object_open_as_commit(&commit, repo, id);
586 c8f60bff 2018-07-23 stsp if (!is_root_path) {
587 ecb28ae0 2018-07-16 stsp struct got_object *obj;
588 ecb28ae0 2018-07-16 stsp struct got_object_qid *pid;
589 ecb28ae0 2018-07-16 stsp int changed = 0;
591 ecb28ae0 2018-07-16 stsp err = got_object_open_by_path(&obj, repo, id, path);
593 c8f60bff 2018-07-23 stsp got_object_commit_close(commit);
594 ecb28ae0 2018-07-16 stsp if (err->code == GOT_ERR_NO_OBJ &&
595 ecb28ae0 2018-07-16 stsp (found_obj || !init)) {
596 ecb28ae0 2018-07-16 stsp /* History stops here. */
597 ecb28ae0 2018-07-16 stsp err = got_error(GOT_ERR_ITER_COMPLETED);
601 ecb28ae0 2018-07-16 stsp found_obj = 1;
603 ecb28ae0 2018-07-16 stsp pid = SIMPLEQ_FIRST(&commit->parent_ids);
604 ecb28ae0 2018-07-16 stsp if (pid != NULL) {
605 ecb28ae0 2018-07-16 stsp struct got_object *pobj;
606 ecb28ae0 2018-07-16 stsp err = got_object_open_by_path(&pobj, repo,
607 ecb28ae0 2018-07-16 stsp pid->id, path);
609 ecb28ae0 2018-07-16 stsp if (err->code != GOT_ERR_NO_OBJ) {
610 ecb28ae0 2018-07-16 stsp got_object_close(obj);
611 c8f60bff 2018-07-23 stsp got_object_commit_close(commit);
614 ecb28ae0 2018-07-16 stsp err = NULL;
615 ecb28ae0 2018-07-16 stsp changed = 1;
617 c2f16475 2018-07-23 stsp struct got_object_id *id, *pid;
618 c2f16475 2018-07-23 stsp id = got_object_get_id(obj);
619 c2f16475 2018-07-23 stsp if (id == NULL) {
620 c2f16475 2018-07-23 stsp err = got_error_from_errno();
621 c8f60bff 2018-07-23 stsp got_object_close(obj);
622 c8f60bff 2018-07-23 stsp got_object_close(pobj);
625 c2f16475 2018-07-23 stsp pid = got_object_get_id(pobj);
626 c2f16475 2018-07-23 stsp if (pid == NULL) {
627 c2f16475 2018-07-23 stsp err = got_error_from_errno();
629 c8f60bff 2018-07-23 stsp got_object_close(obj);
630 c8f60bff 2018-07-23 stsp got_object_close(pobj);
634 c2f16475 2018-07-23 stsp (got_object_id_cmp(id, pid) != 0);
635 ecb28ae0 2018-07-16 stsp got_object_close(pobj);
640 c8f60bff 2018-07-23 stsp got_object_close(obj);
641 ecb28ae0 2018-07-16 stsp if (!changed) {
642 ecb28ae0 2018-07-16 stsp got_object_commit_close(commit);
647 9ba79e04 2018-06-11 stsp entry = alloc_commit_queue_entry(commit, id);
648 9ba79e04 2018-06-11 stsp if (entry == NULL) {
649 9ba79e04 2018-06-11 stsp err = got_error_from_errno();
652 ecb28ae0 2018-07-16 stsp TAILQ_INSERT_TAIL(&commits->head, entry, entry);
654 ecb28ae0 2018-07-16 stsp commits->ncommits++;
657 9ba79e04 2018-06-11 stsp return err;
660 99db9666 2018-05-07 stsp static const struct got_error *
661 9ba79e04 2018-06-11 stsp fetch_next_commit(struct commit_queue_entry **pentry,
662 9ba79e04 2018-06-11 stsp struct commit_queue_entry *entry, struct commit_queue *commits,
663 ecb28ae0 2018-07-16 stsp struct got_commit_graph *graph, struct got_repository *repo,
664 ecb28ae0 2018-07-16 stsp const char *path)
666 899d86c2 2018-05-10 stsp const struct got_error *err = NULL;
668 9ba79e04 2018-06-11 stsp *pentry = NULL;
670 ecb28ae0 2018-07-16 stsp err = queue_commits(graph, commits, entry->id, 1, 0, repo, path);
672 9ba79e04 2018-06-11 stsp return err;
674 9ba79e04 2018-06-11 stsp /* Next entry to display should now be available. */
675 9ba79e04 2018-06-11 stsp *pentry = TAILQ_NEXT(entry, entry);
676 9ba79e04 2018-06-11 stsp if (*pentry == NULL)
677 9ba79e04 2018-06-11 stsp return got_error(GOT_ERR_NO_OBJ);
679 9ba79e04 2018-06-11 stsp return NULL;
682 899d86c2 2018-05-10 stsp static const struct got_error *
683 9ba79e04 2018-06-11 stsp get_head_commit_id(struct got_object_id **head_id, struct got_repository *repo)
685 9ba79e04 2018-06-11 stsp const struct got_error *err = NULL;
686 9ba79e04 2018-06-11 stsp struct got_reference *head_ref;
688 9ba79e04 2018-06-11 stsp *head_id = NULL;
690 9ba79e04 2018-06-11 stsp err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
692 99db9666 2018-05-07 stsp return err;
694 9ba79e04 2018-06-11 stsp err = got_ref_resolve(head_id, repo, head_ref);
695 9ba79e04 2018-06-11 stsp got_ref_close(head_ref);
697 9ba79e04 2018-06-11 stsp *head_id = NULL;
698 99db9666 2018-05-07 stsp return err;
701 9ba79e04 2018-06-11 stsp return NULL;
704 0553a4e3 2018-04-30 stsp static const struct got_error *
705 2814baeb 2018-08-01 stsp draw_commits(struct tog_view *view, struct commit_queue_entry **last,
706 ecb28ae0 2018-07-16 stsp struct commit_queue_entry **selected, struct commit_queue_entry *first,
707 a7f40148 2018-07-18 stsp struct commit_queue *commits, int selected_idx, int limit,
708 a7f40148 2018-07-18 stsp struct got_commit_graph *graph, struct got_repository *repo,
709 a7f40148 2018-07-18 stsp const char *path)
711 0553a4e3 2018-04-30 stsp const struct got_error *err = NULL;
712 0553a4e3 2018-04-30 stsp struct commit_queue_entry *entry;
713 ecb28ae0 2018-07-16 stsp int ncommits, width;
714 867c6645 2018-07-10 stsp char *id_str, *header;
715 ecb28ae0 2018-07-16 stsp wchar_t *wline;
717 e0d42f60 2018-07-22 stsp entry = first;
718 e0d42f60 2018-07-22 stsp ncommits = 0;
719 e0d42f60 2018-07-22 stsp while (entry) {
720 e0d42f60 2018-07-22 stsp if (ncommits == selected_idx) {
721 e0d42f60 2018-07-22 stsp *selected = entry;
724 e0d42f60 2018-07-22 stsp entry = TAILQ_NEXT(entry, entry);
725 e0d42f60 2018-07-22 stsp ncommits++;
728 867c6645 2018-07-10 stsp err = got_object_id_str(&id_str, (*selected)->id);
730 867c6645 2018-07-10 stsp return err;
732 c3ba6f36 2018-08-01 stsp if (path && strcmp(path, "/") != 0) {
733 ecb28ae0 2018-07-16 stsp if (asprintf(&header, "commit: %s [%s]", id_str, path) == -1) {
734 ecb28ae0 2018-07-16 stsp err = got_error_from_errno();
735 ecb28ae0 2018-07-16 stsp free(id_str);
736 ecb28ae0 2018-07-16 stsp return err;
738 ecb28ae0 2018-07-16 stsp } else if (asprintf(&header, "commit: %s", id_str) == -1) {
739 867c6645 2018-07-10 stsp err = got_error_from_errno();
740 867c6645 2018-07-10 stsp free(id_str);
741 867c6645 2018-07-10 stsp return err;
743 ecb28ae0 2018-07-16 stsp free(id_str);
744 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, header, view->ncols);
746 ecb28ae0 2018-07-16 stsp free(header);
747 ecb28ae0 2018-07-16 stsp return err;
749 ecb28ae0 2018-07-16 stsp free(header);
751 2814baeb 2018-08-01 stsp werase(view->window);
753 2814baeb 2018-08-01 stsp waddwstr(view->window, wline);
754 f7d12f7e 2018-08-01 stsp if (width < view->ncols)
755 2814baeb 2018-08-01 stsp waddch(view->window, '\n');
756 ecb28ae0 2018-07-16 stsp free(wline);
757 ecb28ae0 2018-07-16 stsp if (limit <= 1)
758 ecb28ae0 2018-07-16 stsp return NULL;
760 899d86c2 2018-05-10 stsp entry = first;
761 899d86c2 2018-05-10 stsp *last = first;
762 867c6645 2018-07-10 stsp ncommits = 0;
763 899d86c2 2018-05-10 stsp while (entry) {
764 ecb28ae0 2018-07-16 stsp if (ncommits >= limit - 1)
766 e0d42f60 2018-07-22 stsp if (ncommits == selected_idx)
767 2814baeb 2018-08-01 stsp wstandout(view->window);
768 2814baeb 2018-08-01 stsp err = draw_commit(view, entry->commit, entry->id);
769 cd0acaa7 2018-05-20 stsp if (ncommits == selected_idx)
770 2814baeb 2018-08-01 stsp wstandend(view->window);
773 0553a4e3 2018-04-30 stsp ncommits++;
774 899d86c2 2018-05-10 stsp *last = entry;
775 a7f40148 2018-07-18 stsp if (entry == TAILQ_LAST(&commits->head, commit_queue_head)) {
776 a7f40148 2018-07-18 stsp err = queue_commits(graph, commits, entry->id, 1,
777 a7f40148 2018-07-18 stsp 0, repo, path);
779 a7f40148 2018-07-18 stsp if (err->code != GOT_ERR_ITER_COMPLETED)
780 a7f40148 2018-07-18 stsp return err;
781 a7f40148 2018-07-18 stsp err = NULL;
784 899d86c2 2018-05-10 stsp entry = TAILQ_NEXT(entry, entry);
787 80ddbec8 2018-04-29 stsp update_panels();
788 80ddbec8 2018-04-29 stsp doupdate();
790 80ddbec8 2018-04-29 stsp return err;
793 07b55e75 2018-05-10 stsp static void
794 16482c3b 2018-05-20 stsp scroll_up(struct commit_queue_entry **first_displayed_entry, int maxscroll,
795 07b55e75 2018-05-10 stsp struct commit_queue *commits)
797 07b55e75 2018-05-10 stsp struct commit_queue_entry *entry;
798 07b55e75 2018-05-10 stsp int nscrolled = 0;
800 ecb28ae0 2018-07-16 stsp entry = TAILQ_FIRST(&commits->head);
801 07b55e75 2018-05-10 stsp if (*first_displayed_entry == entry)
804 07b55e75 2018-05-10 stsp entry = *first_displayed_entry;
805 16482c3b 2018-05-20 stsp while (entry && nscrolled < maxscroll) {
806 ecb28ae0 2018-07-16 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
807 07b55e75 2018-05-10 stsp if (entry) {
808 07b55e75 2018-05-10 stsp *first_displayed_entry = entry;
809 07b55e75 2018-05-10 stsp nscrolled++;
814 aa075928 2018-05-10 stsp static const struct got_error *
815 16482c3b 2018-05-20 stsp scroll_down(struct commit_queue_entry **first_displayed_entry, int maxscroll,
816 aa075928 2018-05-10 stsp struct commit_queue_entry *last_displayed_entry,
817 9ba79e04 2018-06-11 stsp struct commit_queue *commits, struct got_commit_graph *graph,
818 ecb28ae0 2018-07-16 stsp struct got_repository *repo, const char *path)
820 aa075928 2018-05-10 stsp const struct got_error *err = NULL;
821 dd0a52c1 2018-05-20 stsp struct commit_queue_entry *pentry;
822 aa075928 2018-05-10 stsp int nscrolled = 0;
825 dd0a52c1 2018-05-20 stsp pentry = TAILQ_NEXT(last_displayed_entry, entry);
826 aa075928 2018-05-10 stsp if (pentry == NULL) {
827 9ba79e04 2018-06-11 stsp err = fetch_next_commit(&pentry, last_displayed_entry,
828 ecb28ae0 2018-07-16 stsp commits, graph, repo, path);
829 9a6bf2a5 2018-05-20 stsp if (err || pentry == NULL)
832 dd0a52c1 2018-05-20 stsp last_displayed_entry = pentry;
834 dd0a52c1 2018-05-20 stsp pentry = TAILQ_NEXT(*first_displayed_entry, entry);
835 dd0a52c1 2018-05-20 stsp if (pentry == NULL)
837 aa075928 2018-05-10 stsp *first_displayed_entry = pentry;
838 16482c3b 2018-05-20 stsp } while (++nscrolled < maxscroll);
840 dd0a52c1 2018-05-20 stsp return err;
843 cd0acaa7 2018-05-20 stsp static const struct got_error *
844 6d0fee91 2018-08-01 stsp show_commit(struct tog_view *parent_view, struct commit_queue_entry *entry,
845 6d0fee91 2018-08-01 stsp struct got_repository *repo)
847 cd0acaa7 2018-05-20 stsp const struct got_error *err;
848 cd0acaa7 2018-05-20 stsp struct got_object *obj1 = NULL, *obj2 = NULL;
849 9ba79e04 2018-06-11 stsp struct got_object_qid *parent_id;
850 ea5e7bb5 2018-08-01 stsp struct tog_view *view;
852 cd0acaa7 2018-05-20 stsp err = got_object_open(&obj2, repo, entry->id);
854 cd0acaa7 2018-05-20 stsp return err;
856 9ba79e04 2018-06-11 stsp parent_id = SIMPLEQ_FIRST(&entry->commit->parent_ids);
857 9ba79e04 2018-06-11 stsp if (parent_id) {
858 9ba79e04 2018-06-11 stsp err = got_object_open(&obj1, repo, parent_id->id);
863 b3665f43 2018-08-04 stsp view = view_open(0, 0, 0, 0, parent_view, TOG_VIEW_DIFF);
864 ea5e7bb5 2018-08-01 stsp if (view == NULL) {
865 ea5e7bb5 2018-08-01 stsp err = got_error_from_errno();
869 5dc9f4bc 2018-08-04 stsp err = open_diff_view(view, obj1, obj2, repo);
872 5dc9f4bc 2018-08-04 stsp err = show_diff_view(view);
873 5dc9f4bc 2018-08-04 stsp close_diff_view(view);
874 96a765a8 2018-08-04 stsp view_close(view);
875 6acd1bd1 2018-08-04 stsp view_show(parent_view);
878 cd0acaa7 2018-05-20 stsp got_object_close(obj1);
880 cd0acaa7 2018-05-20 stsp got_object_close(obj2);
881 cd0acaa7 2018-05-20 stsp return err;
884 80ddbec8 2018-04-29 stsp static const struct got_error *
885 6d0fee91 2018-08-01 stsp browse_commit(struct tog_view *parent_view, struct commit_queue_entry *entry,
886 6d0fee91 2018-08-01 stsp struct got_repository *repo)
888 9343a5fb 2018-06-23 stsp const struct got_error *err = NULL;
889 9343a5fb 2018-06-23 stsp struct got_tree_object *tree;
890 5221c383 2018-08-01 stsp struct tog_view *view;
892 9343a5fb 2018-06-23 stsp err = got_object_open_as_tree(&tree, repo, entry->commit->tree_id);
894 9343a5fb 2018-06-23 stsp return err;
896 b3665f43 2018-08-04 stsp view = view_open(0, 0, 0, 0, parent_view, TOG_VIEW_TREE);
897 5221c383 2018-08-01 stsp if (view == NULL) {
898 5221c383 2018-08-01 stsp err = got_error_from_errno();
901 ad80ab7b 2018-08-04 stsp err = open_tree_view(view, tree, entry->id, repo);
904 ad80ab7b 2018-08-04 stsp err = show_tree_view(view);
905 ad80ab7b 2018-08-04 stsp close_tree_view(view);
906 96a765a8 2018-08-04 stsp view_close(view);
907 6acd1bd1 2018-08-04 stsp view_show(parent_view);
909 9343a5fb 2018-06-23 stsp got_object_tree_close(tree);
910 9343a5fb 2018-06-23 stsp return err;
913 9343a5fb 2018-06-23 stsp static const struct got_error *
914 ba4f502b 2018-08-04 stsp open_log_view(struct tog_view *view, struct got_object_id *start_id,
915 04cc582a 2018-08-01 stsp struct got_repository *repo, const char *path)
917 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
918 9ba79e04 2018-06-11 stsp struct got_object_id *head_id = NULL;
919 ba4f502b 2018-08-04 stsp int nfetched;
920 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
922 fb2756b9 2018-08-04 stsp err = got_repo_map_path(&s->in_repo_path, repo, path);
923 ecb28ae0 2018-07-16 stsp if (err != NULL)
926 9ba79e04 2018-06-11 stsp err = get_head_commit_id(&head_id, repo);
928 9ba79e04 2018-06-11 stsp return err;
930 034e3b69 2018-07-22 stsp /* The graph contains all commits. */
931 fb2756b9 2018-08-04 stsp err = got_commit_graph_open(&s->graph, head_id, 0, repo);
934 034e3b69 2018-07-22 stsp /* The commit queue contains a subset of commits filtered by path. */
935 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->commits.head);
936 fb2756b9 2018-08-04 stsp s->commits.ncommits = 0;
938 9ba79e04 2018-06-11 stsp /* Populate commit graph with a sufficient number of commits. */
939 fb2756b9 2018-08-04 stsp err = got_commit_graph_fetch_commits_up_to(&nfetched, s->graph,
940 fb2756b9 2018-08-04 stsp start_id, repo);
945 9ba79e04 2018-06-11 stsp * Open the initial batch of commits, sorted in commit graph order.
946 9ba79e04 2018-06-11 stsp * We keep all commits open throughout the lifetime of the log view
947 9ba79e04 2018-06-11 stsp * in order to avoid having to re-fetch commits from disk while
948 9ba79e04 2018-06-11 stsp * updating the display.
950 fb2756b9 2018-08-04 stsp err = queue_commits(s->graph, &s->commits, start_id, view->nlines, 1,
951 fb2756b9 2018-08-04 stsp repo, s->in_repo_path);
953 55198a88 2018-07-16 stsp if (err->code != GOT_ERR_ITER_COMPLETED)
955 55198a88 2018-07-16 stsp err = NULL;
958 fb2756b9 2018-08-04 stsp s->repo = repo;
960 ba4f502b 2018-08-04 stsp free(head_id);
961 ba4f502b 2018-08-04 stsp return err;
964 ba4f502b 2018-08-04 stsp static void close_log_view(struct tog_view *view)
966 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
968 fb2756b9 2018-08-04 stsp if (s->graph)
969 fb2756b9 2018-08-04 stsp got_commit_graph_close(s->graph);
970 fb2756b9 2018-08-04 stsp free_commits(&s->commits);
971 fb2756b9 2018-08-04 stsp free(s->in_repo_path);
974 ba4f502b 2018-08-04 stsp static const struct got_error *
975 ba4f502b 2018-08-04 stsp show_log_view(struct tog_view *view)
977 ba4f502b 2018-08-04 stsp const struct got_error *err = NULL;
978 ba4f502b 2018-08-04 stsp int ch, done = 0;
979 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
981 6acd1bd1 2018-08-04 stsp view_show(view);
983 fb2756b9 2018-08-04 stsp s->first_displayed_entry =
984 fb2756b9 2018-08-04 stsp TAILQ_FIRST(&s->commits.head);
985 fb2756b9 2018-08-04 stsp s->selected_entry = s->first_displayed_entry;
986 899d86c2 2018-05-10 stsp while (!done) {
987 fb2756b9 2018-08-04 stsp err = draw_commits(view, &s->last_displayed_entry,
988 fb2756b9 2018-08-04 stsp &s->selected_entry, s->first_displayed_entry,
989 fb2756b9 2018-08-04 stsp &s->commits, s->selected, view->nlines, s->graph,
990 fb2756b9 2018-08-04 stsp s->repo, s->in_repo_path);
994 80ddbec8 2018-04-29 stsp nodelay(stdscr, FALSE);
995 2814baeb 2018-08-01 stsp ch = wgetch(view->window);
996 f7182337 2018-05-20 stsp nodelay(stdscr, TRUE);
997 80ddbec8 2018-04-29 stsp switch (ch) {
1004 80ddbec8 2018-04-29 stsp case KEY_UP:
1005 fb2756b9 2018-08-04 stsp if (s->selected > 0)
1006 fb2756b9 2018-08-04 stsp s->selected--;
1007 fb2756b9 2018-08-04 stsp if (s->selected > 0)
1009 fb2756b9 2018-08-04 stsp scroll_up(&s->first_displayed_entry, 1,
1010 fb2756b9 2018-08-04 stsp &s->commits);
1012 48531068 2018-05-10 stsp case KEY_PPAGE:
1013 fb2756b9 2018-08-04 stsp if (TAILQ_FIRST(&s->commits.head) ==
1014 fb2756b9 2018-08-04 stsp s->first_displayed_entry) {
1015 fb2756b9 2018-08-04 stsp s->selected = 0;
1018 fb2756b9 2018-08-04 stsp scroll_up(&s->first_displayed_entry,
1019 fb2756b9 2018-08-04 stsp view->nlines, &s->commits);
1022 80ddbec8 2018-04-29 stsp case KEY_DOWN:
1023 fb2756b9 2018-08-04 stsp if (s->selected < MIN(view->nlines - 2,
1024 fb2756b9 2018-08-04 stsp s->commits.ncommits - 1)) {
1025 fb2756b9 2018-08-04 stsp s->selected++;
1028 fb2756b9 2018-08-04 stsp err = scroll_down(&s->first_displayed_entry, 1,
1029 fb2756b9 2018-08-04 stsp s->last_displayed_entry, &s->commits,
1030 fb2756b9 2018-08-04 stsp s->graph, s->repo, s->in_repo_path);
1031 ecb28ae0 2018-07-16 stsp if (err) {
1032 ecb28ae0 2018-07-16 stsp if (err->code != GOT_ERR_ITER_COMPLETED)
1033 ecb28ae0 2018-07-16 stsp goto done;
1034 ecb28ae0 2018-07-16 stsp err = NULL;
1037 ecb28ae0 2018-07-16 stsp case KEY_NPAGE: {
1038 c70c5802 2018-08-01 stsp struct commit_queue_entry *first;
1039 fb2756b9 2018-08-04 stsp first = s->first_displayed_entry;
1040 fb2756b9 2018-08-04 stsp err = scroll_down(&s->first_displayed_entry,
1041 fb2756b9 2018-08-04 stsp view->nlines, s->last_displayed_entry,
1042 fb2756b9 2018-08-04 stsp &s->commits, s->graph, s->repo,
1043 fb2756b9 2018-08-04 stsp s->in_repo_path);
1044 4d7951a5 2018-08-04 stsp if (err == NULL)
1046 4d7951a5 2018-08-04 stsp if (err->code != GOT_ERR_ITER_COMPLETED)
1047 4d7951a5 2018-08-04 stsp goto done;
1048 fb2756b9 2018-08-04 stsp if (first == s->first_displayed_entry &&
1049 fb2756b9 2018-08-04 stsp s->selected < MIN(view->nlines - 2,
1050 fb2756b9 2018-08-04 stsp s->commits.ncommits - 1)) {
1051 4d7951a5 2018-08-04 stsp /* can't scroll further down */
1052 fb2756b9 2018-08-04 stsp s->selected = MIN(view->nlines - 2,
1053 fb2756b9 2018-08-04 stsp s->commits.ncommits - 1);
1055 4d7951a5 2018-08-04 stsp err = NULL;
1058 d6df9be4 2018-04-30 stsp case KEY_RESIZE:
1059 a41d2007 2018-08-01 stsp err = view_resize(view);
1061 a41d2007 2018-08-01 stsp goto done;
1062 fb2756b9 2018-08-04 stsp if (s->selected > view->nlines - 2)
1063 fb2756b9 2018-08-04 stsp s->selected = view->nlines - 2;
1064 fb2756b9 2018-08-04 stsp if (s->selected > s->commits.ncommits - 1)
1065 fb2756b9 2018-08-04 stsp s->selected = s->commits.ncommits - 1;
1067 cd0acaa7 2018-05-20 stsp case KEY_ENTER:
1068 cd0acaa7 2018-05-20 stsp case '\r':
1069 fb2756b9 2018-08-04 stsp err = show_commit(view, s->selected_entry,
1072 9343a5fb 2018-06-23 stsp goto done;
1073 6acd1bd1 2018-08-04 stsp view_show(view);
1076 fb2756b9 2018-08-04 stsp err = browse_commit(view, s->selected_entry,
1079 0d4100bb 2018-06-23 stsp goto done;
1080 6acd1bd1 2018-08-04 stsp view_show(view);
1087 80ddbec8 2018-04-29 stsp return err;
1090 4ed7e80c 2018-05-20 stsp static const struct got_error *
1091 9f7d7167 2018-04-29 stsp cmd_log(int argc, char *argv[])
1093 80ddbec8 2018-04-29 stsp const struct got_error *error;
1094 ecb28ae0 2018-07-16 stsp struct got_repository *repo = NULL;
1095 899d86c2 2018-05-10 stsp struct got_object_id *start_id = NULL;
1096 ecb28ae0 2018-07-16 stsp char *path = NULL, *repo_path = NULL, *cwd = NULL;
1097 80ddbec8 2018-04-29 stsp char *start_commit = NULL;
1099 04cc582a 2018-08-01 stsp struct tog_view *view;
1101 80ddbec8 2018-04-29 stsp #ifndef PROFILE
1102 80ddbec8 2018-04-29 stsp if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
1103 80ddbec8 2018-04-29 stsp err(1, "pledge");
1106 ecb28ae0 2018-07-16 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1107 80ddbec8 2018-04-29 stsp switch (ch) {
1109 80ddbec8 2018-04-29 stsp start_commit = optarg;
1112 ecb28ae0 2018-07-16 stsp repo_path = realpath(optarg, NULL);
1113 ecb28ae0 2018-07-16 stsp if (repo_path == NULL)
1114 ecb28ae0 2018-07-16 stsp err(1, "-r option");
1118 80ddbec8 2018-04-29 stsp /* NOTREACHED */
1122 80ddbec8 2018-04-29 stsp argc -= optind;
1123 80ddbec8 2018-04-29 stsp argv += optind;
1125 ecb28ae0 2018-07-16 stsp if (argc == 0)
1126 ecb28ae0 2018-07-16 stsp path = strdup("");
1127 ecb28ae0 2018-07-16 stsp else if (argc == 1)
1128 ecb28ae0 2018-07-16 stsp path = strdup(argv[0]);
1130 80ddbec8 2018-04-29 stsp usage_log();
1131 ecb28ae0 2018-07-16 stsp if (path == NULL)
1132 ecb28ae0 2018-07-16 stsp return got_error_from_errno();
1134 ecb28ae0 2018-07-16 stsp cwd = getcwd(NULL, 0);
1135 ecb28ae0 2018-07-16 stsp if (cwd == NULL) {
1136 ecb28ae0 2018-07-16 stsp error = got_error_from_errno();
1137 ecb28ae0 2018-07-16 stsp goto done;
1139 ecb28ae0 2018-07-16 stsp if (repo_path == NULL) {
1140 ecb28ae0 2018-07-16 stsp repo_path = strdup(cwd);
1141 ecb28ae0 2018-07-16 stsp if (repo_path == NULL) {
1142 ecb28ae0 2018-07-16 stsp error = got_error_from_errno();
1143 ecb28ae0 2018-07-16 stsp goto done;
1147 80ddbec8 2018-04-29 stsp error = got_repo_open(&repo, repo_path);
1148 80ddbec8 2018-04-29 stsp if (error != NULL)
1149 ecb28ae0 2018-07-16 stsp goto done;
1151 80ddbec8 2018-04-29 stsp if (start_commit == NULL) {
1152 899d86c2 2018-05-10 stsp error = get_head_commit_id(&start_id, repo);
1153 80ddbec8 2018-04-29 stsp if (error != NULL)
1154 ecb28ae0 2018-07-16 stsp goto done;
1156 80ddbec8 2018-04-29 stsp struct got_object *obj;
1157 80ddbec8 2018-04-29 stsp error = got_object_open_by_id_str(&obj, repo, start_commit);
1158 80ddbec8 2018-04-29 stsp if (error == NULL) {
1159 899d86c2 2018-05-10 stsp start_id = got_object_get_id(obj);
1160 899d86c2 2018-05-10 stsp if (start_id == NULL)
1161 80ddbec8 2018-04-29 stsp error = got_error_from_errno();
1162 ecb28ae0 2018-07-16 stsp goto done;
1165 80ddbec8 2018-04-29 stsp if (error != NULL)
1166 ecb28ae0 2018-07-16 stsp goto done;
1168 b3665f43 2018-08-04 stsp view = view_open(0, 0, 0, 0, NULL, TOG_VIEW_LOG);
1169 04cc582a 2018-08-01 stsp if (view == NULL) {
1170 04cc582a 2018-08-01 stsp error = got_error_from_errno();
1171 04cc582a 2018-08-01 stsp goto done;
1173 ba4f502b 2018-08-04 stsp error = open_log_view(view, start_id, repo, path);
1174 ba4f502b 2018-08-04 stsp if (error)
1175 ba4f502b 2018-08-04 stsp goto done;
1176 ba4f502b 2018-08-04 stsp error = show_log_view(view);
1177 ba4f502b 2018-08-04 stsp close_log_view(view);
1178 96a765a8 2018-08-04 stsp view_close(view);
1180 ecb28ae0 2018-07-16 stsp free(repo_path);
1181 ecb28ae0 2018-07-16 stsp free(cwd);
1182 ecb28ae0 2018-07-16 stsp free(path);
1183 899d86c2 2018-05-10 stsp free(start_id);
1185 ecb28ae0 2018-07-16 stsp got_repo_close(repo);
1186 80ddbec8 2018-04-29 stsp return error;
1189 4ed7e80c 2018-05-20 stsp __dead static void
1190 9f7d7167 2018-04-29 stsp usage_diff(void)
1193 9f7d7167 2018-04-29 stsp fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
1194 9f7d7167 2018-04-29 stsp getprogname());
1198 b304db33 2018-05-20 stsp static char *
1199 b304db33 2018-05-20 stsp parse_next_line(FILE *f, size_t *len)
1201 b304db33 2018-05-20 stsp char *line;
1202 b304db33 2018-05-20 stsp size_t linelen;
1203 b304db33 2018-05-20 stsp size_t lineno;
1204 b304db33 2018-05-20 stsp const char delim[3] = { '\0', '\0', '\0'};
1206 b304db33 2018-05-20 stsp line = fparseln(f, &linelen, &lineno, delim, 0);
1208 b304db33 2018-05-20 stsp *len = linelen;
1209 b304db33 2018-05-20 stsp return line;
1212 4ed7e80c 2018-05-20 stsp static const struct got_error *
1213 f7d12f7e 2018-08-01 stsp draw_file(struct tog_view *view, FILE *f, int *first_displayed_line,
1214 a70480e0 2018-06-23 stsp int *last_displayed_line, int *eof, int max_lines)
1216 61e69b96 2018-05-20 stsp const struct got_error *err;
1217 26ed57b2 2018-05-19 stsp int nlines = 0, nprinted = 0;
1218 b304db33 2018-05-20 stsp char *line;
1219 b304db33 2018-05-20 stsp size_t len;
1220 61e69b96 2018-05-20 stsp wchar_t *wline;
1221 e0b650dd 2018-05-20 stsp int width;
1223 26ed57b2 2018-05-19 stsp rewind(f);
1224 f7d12f7e 2018-08-01 stsp werase(view->window);
1227 26ed57b2 2018-05-19 stsp while (nprinted < max_lines) {
1228 b304db33 2018-05-20 stsp line = parse_next_line(f, &len);
1229 26ed57b2 2018-05-19 stsp if (line == NULL) {
1233 26ed57b2 2018-05-19 stsp if (++nlines < *first_displayed_line) {
1234 26ed57b2 2018-05-19 stsp free(line);
1238 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, line, view->ncols);
1239 61e69b96 2018-05-20 stsp if (err) {
1240 61e69b96 2018-05-20 stsp free(line);
1241 2550e4c3 2018-07-13 stsp free(wline);
1242 61e69b96 2018-05-20 stsp return err;
1244 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
1245 f7d12f7e 2018-08-01 stsp if (width < view->ncols)
1246 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
1247 26ed57b2 2018-05-19 stsp if (++nprinted == 1)
1248 26ed57b2 2018-05-19 stsp *first_displayed_line = nlines;
1249 26ed57b2 2018-05-19 stsp free(line);
1250 2550e4c3 2018-07-13 stsp free(wline);
1251 2550e4c3 2018-07-13 stsp wline = NULL;
1253 26ed57b2 2018-05-19 stsp *last_displayed_line = nlines;
1255 26ed57b2 2018-05-19 stsp update_panels();
1256 26ed57b2 2018-05-19 stsp doupdate();
1258 26ed57b2 2018-05-19 stsp return NULL;
1261 4ed7e80c 2018-05-20 stsp static const struct got_error *
1262 5dc9f4bc 2018-08-04 stsp open_diff_view(struct tog_view *view, struct got_object *obj1,
1263 ea5e7bb5 2018-08-01 stsp struct got_object *obj2, struct got_repository *repo)
1265 26ed57b2 2018-05-19 stsp const struct got_error *err;
1268 cd0acaa7 2018-05-20 stsp if (obj1 != NULL && obj2 != NULL &&
1269 cd0acaa7 2018-05-20 stsp got_object_get_type(obj1) != got_object_get_type(obj2))
1270 26ed57b2 2018-05-19 stsp return got_error(GOT_ERR_OBJ_TYPE);
1272 511a516b 2018-05-19 stsp f = got_opentemp();
1273 26ed57b2 2018-05-19 stsp if (f == NULL)
1274 26ed57b2 2018-05-19 stsp return got_error_from_errno();
1276 cd0acaa7 2018-05-20 stsp switch (got_object_get_type(obj1 ? obj1 : obj2)) {
1277 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_BLOB:
1278 11528a82 2018-05-19 stsp err = got_diff_objects_as_blobs(obj1, obj2, repo, f);
1280 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_TREE:
1281 11528a82 2018-05-19 stsp err = got_diff_objects_as_trees(obj1, obj2, repo, f);
1283 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_COMMIT:
1284 11528a82 2018-05-19 stsp err = got_diff_objects_as_commits(obj1, obj2, repo, f);
1287 26ed57b2 2018-05-19 stsp return got_error(GOT_ERR_OBJ_TYPE);
1290 26ed57b2 2018-05-19 stsp fflush(f);
1292 5dc9f4bc 2018-08-04 stsp view->state.diff.f = f;
1293 5dc9f4bc 2018-08-04 stsp view->state.diff.first_displayed_line = 1;
1294 5dc9f4bc 2018-08-04 stsp view->state.diff.last_displayed_line = view->nlines;
1296 5dc9f4bc 2018-08-04 stsp return NULL;
1299 5dc9f4bc 2018-08-04 stsp static void
1300 5dc9f4bc 2018-08-04 stsp close_diff_view(struct tog_view *view)
1302 5dc9f4bc 2018-08-04 stsp fclose(view->state.diff.f);
1305 5dc9f4bc 2018-08-04 stsp static const struct got_error *
1306 5dc9f4bc 2018-08-04 stsp show_diff_view(struct tog_view *view)
1308 5dc9f4bc 2018-08-04 stsp const struct got_error *err = NULL;
1309 5dc9f4bc 2018-08-04 stsp int ch, done = 0;
1310 5dc9f4bc 2018-08-04 stsp int eof, i;
1311 fb2756b9 2018-08-04 stsp struct tog_diff_view_state *s = &view->state.diff;
1313 6acd1bd1 2018-08-04 stsp view_show(view);
1315 26ed57b2 2018-05-19 stsp while (!done) {
1316 fb2756b9 2018-08-04 stsp err = draw_file(view, s->f, &s->first_displayed_line,
1317 fb2756b9 2018-08-04 stsp &s->last_displayed_line, &eof, view->nlines);
1320 26ed57b2 2018-05-19 stsp nodelay(stdscr, FALSE);
1321 ea5e7bb5 2018-08-01 stsp ch = wgetch(view->window);
1322 f7182337 2018-05-20 stsp nodelay(stdscr, TRUE);
1323 26ed57b2 2018-05-19 stsp switch (ch) {
1328 26ed57b2 2018-05-19 stsp case KEY_UP:
1329 fb2756b9 2018-08-04 stsp if (s->first_displayed_line > 1)
1330 fb2756b9 2018-08-04 stsp s->first_displayed_line--;
1332 b304db33 2018-05-20 stsp case KEY_PPAGE:
1333 c46b9352 2018-07-12 stsp case KEY_BACKSPACE:
1335 f7d12f7e 2018-08-01 stsp while (i++ < view->nlines - 1 &&
1336 fb2756b9 2018-08-04 stsp s->first_displayed_line > 1)
1337 fb2756b9 2018-08-04 stsp s->first_displayed_line--;
1340 26ed57b2 2018-05-19 stsp case KEY_DOWN:
1342 fb2756b9 2018-08-04 stsp s->first_displayed_line++;
1344 b304db33 2018-05-20 stsp case KEY_NPAGE:
1347 f7d12f7e 2018-08-01 stsp while (!eof && i++ < view->nlines - 1) {
1348 fb2756b9 2018-08-04 stsp char *line;
1349 fb2756b9 2018-08-04 stsp line = parse_next_line(s->f, NULL);
1350 fb2756b9 2018-08-04 stsp s->first_displayed_line++;
1351 b304db33 2018-05-20 stsp if (line == NULL)
1355 82357c0a 2018-08-01 stsp case KEY_RESIZE:
1356 a41d2007 2018-08-01 stsp err = view_resize(view);
1358 a41d2007 2018-08-01 stsp goto done;
1365 26ed57b2 2018-05-19 stsp return err;
1368 4ed7e80c 2018-05-20 stsp static const struct got_error *
1369 9f7d7167 2018-04-29 stsp cmd_diff(int argc, char *argv[])
1371 26ed57b2 2018-05-19 stsp const struct got_error *error = NULL;
1372 26ed57b2 2018-05-19 stsp struct got_repository *repo = NULL;
1373 26ed57b2 2018-05-19 stsp struct got_object *obj1 = NULL, *obj2 = NULL;
1374 26ed57b2 2018-05-19 stsp char *repo_path = NULL;
1375 26ed57b2 2018-05-19 stsp char *obj_id_str1 = NULL, *obj_id_str2 = NULL;
1377 ea5e7bb5 2018-08-01 stsp struct tog_view *view;
1379 26ed57b2 2018-05-19 stsp #ifndef PROFILE
1380 26ed57b2 2018-05-19 stsp if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
1381 26ed57b2 2018-05-19 stsp err(1, "pledge");
1384 26ed57b2 2018-05-19 stsp while ((ch = getopt(argc, argv, "")) != -1) {
1385 26ed57b2 2018-05-19 stsp switch (ch) {
1388 26ed57b2 2018-05-19 stsp /* NOTREACHED */
1392 26ed57b2 2018-05-19 stsp argc -= optind;
1393 26ed57b2 2018-05-19 stsp argv += optind;
1395 26ed57b2 2018-05-19 stsp if (argc == 0) {
1396 26ed57b2 2018-05-19 stsp usage_diff(); /* TODO show local worktree changes */
1397 26ed57b2 2018-05-19 stsp } else if (argc == 2) {
1398 26ed57b2 2018-05-19 stsp repo_path = getcwd(NULL, 0);
1399 26ed57b2 2018-05-19 stsp if (repo_path == NULL)
1400 26ed57b2 2018-05-19 stsp return got_error_from_errno();
1401 26ed57b2 2018-05-19 stsp obj_id_str1 = argv[0];
1402 26ed57b2 2018-05-19 stsp obj_id_str2 = argv[1];
1403 26ed57b2 2018-05-19 stsp } else if (argc == 3) {
1404 26ed57b2 2018-05-19 stsp repo_path = realpath(argv[0], NULL);
1405 26ed57b2 2018-05-19 stsp if (repo_path == NULL)
1406 26ed57b2 2018-05-19 stsp return got_error_from_errno();
1407 26ed57b2 2018-05-19 stsp obj_id_str1 = argv[1];
1408 26ed57b2 2018-05-19 stsp obj_id_str2 = argv[2];
1410 26ed57b2 2018-05-19 stsp usage_diff();
1412 26ed57b2 2018-05-19 stsp error = got_repo_open(&repo, repo_path);
1413 26ed57b2 2018-05-19 stsp free(repo_path);
1414 26ed57b2 2018-05-19 stsp if (error)
1415 26ed57b2 2018-05-19 stsp goto done;
1417 26ed57b2 2018-05-19 stsp error = got_object_open_by_id_str(&obj1, repo, obj_id_str1);
1418 26ed57b2 2018-05-19 stsp if (error)
1419 26ed57b2 2018-05-19 stsp goto done;
1421 26ed57b2 2018-05-19 stsp error = got_object_open_by_id_str(&obj2, repo, obj_id_str2);
1422 26ed57b2 2018-05-19 stsp if (error)
1423 26ed57b2 2018-05-19 stsp goto done;
1425 b3665f43 2018-08-04 stsp view = view_open(0, 0, 0, 0, NULL, TOG_VIEW_DIFF);
1426 ea5e7bb5 2018-08-01 stsp if (view == NULL) {
1427 ea5e7bb5 2018-08-01 stsp error = got_error_from_errno();
1428 ea5e7bb5 2018-08-01 stsp goto done;
1430 5dc9f4bc 2018-08-04 stsp error = open_diff_view(view, obj1, obj2, repo);
1431 5dc9f4bc 2018-08-04 stsp if (error)
1432 5dc9f4bc 2018-08-04 stsp goto done;
1433 5dc9f4bc 2018-08-04 stsp error = show_diff_view(view);
1434 5dc9f4bc 2018-08-04 stsp close_diff_view(view);
1435 96a765a8 2018-08-04 stsp view_close(view);
1437 26ed57b2 2018-05-19 stsp got_repo_close(repo);
1439 26ed57b2 2018-05-19 stsp got_object_close(obj1);
1441 26ed57b2 2018-05-19 stsp got_object_close(obj2);
1442 26ed57b2 2018-05-19 stsp return error;
1445 4ed7e80c 2018-05-20 stsp __dead static void
1446 9f7d7167 2018-04-29 stsp usage_blame(void)
1449 69069811 2018-08-02 stsp fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
1450 9f7d7167 2018-04-29 stsp getprogname());
1454 84451b3e 2018-07-10 stsp struct tog_blame_line {
1455 84451b3e 2018-07-10 stsp int annotated;
1456 84451b3e 2018-07-10 stsp struct got_object_id *id;
1459 4ed7e80c 2018-05-20 stsp static const struct got_error *
1460 f7d12f7e 2018-08-01 stsp draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
1461 f7d12f7e 2018-08-01 stsp const char *path, struct tog_blame_line *lines, int nlines,
1462 f7d12f7e 2018-08-01 stsp int blame_complete, int selected_line, int *first_displayed_line,
1463 f7d12f7e 2018-08-01 stsp int *last_displayed_line, int *eof, int max_lines)
1465 84451b3e 2018-07-10 stsp const struct got_error *err;
1466 84451b3e 2018-07-10 stsp int lineno = 0, nprinted = 0;
1467 84451b3e 2018-07-10 stsp char *line;
1468 84451b3e 2018-07-10 stsp size_t len;
1469 84451b3e 2018-07-10 stsp wchar_t *wline;
1470 b700b5d6 2018-07-10 stsp int width, wlimit;
1471 84451b3e 2018-07-10 stsp struct tog_blame_line *blame_line;
1472 ee41ec32 2018-07-10 stsp struct got_object_id *prev_id = NULL;
1473 ab089a2a 2018-07-12 stsp char *id_str;
1475 ab089a2a 2018-07-12 stsp err = got_object_id_str(&id_str, id);
1477 ab089a2a 2018-07-12 stsp return err;
1479 84451b3e 2018-07-10 stsp rewind(f);
1480 f7d12f7e 2018-08-01 stsp werase(view->window);
1482 ab089a2a 2018-07-12 stsp if (asprintf(&line, "commit: %s", id_str) == -1) {
1483 ab089a2a 2018-07-12 stsp err = got_error_from_errno();
1484 ab089a2a 2018-07-12 stsp free(id_str);
1485 ab089a2a 2018-07-12 stsp return err;
1488 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, line, view->ncols);
1489 ab089a2a 2018-07-12 stsp free(line);
1490 2550e4c3 2018-07-13 stsp line = NULL;
1491 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
1492 2550e4c3 2018-07-13 stsp free(wline);
1493 2550e4c3 2018-07-13 stsp wline = NULL;
1494 f7d12f7e 2018-08-01 stsp if (width < view->ncols)
1495 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
1497 084063cd 2018-07-12 stsp if (asprintf(&line, "[%d/%d] %s%s",
1498 084063cd 2018-07-12 stsp *first_displayed_line - 1 + selected_line, nlines,
1499 12a0b23b 2018-07-12 stsp blame_complete ? "" : "annotating ", path) == -1) {
1500 ab089a2a 2018-07-12 stsp free(id_str);
1501 3f60a8ef 2018-07-10 stsp return got_error_from_errno();
1503 ab089a2a 2018-07-12 stsp free(id_str);
1504 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, line, view->ncols);
1505 3f60a8ef 2018-07-10 stsp free(line);
1506 2550e4c3 2018-07-13 stsp line = NULL;
1508 3f60a8ef 2018-07-10 stsp return err;
1509 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
1510 2550e4c3 2018-07-13 stsp free(wline);
1511 2550e4c3 2018-07-13 stsp wline = NULL;
1512 f7d12f7e 2018-08-01 stsp if (width < view->ncols)
1513 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
1516 ab089a2a 2018-07-12 stsp while (nprinted < max_lines - 2) {
1517 84451b3e 2018-07-10 stsp line = parse_next_line(f, &len);
1518 84451b3e 2018-07-10 stsp if (line == NULL) {
1522 84451b3e 2018-07-10 stsp if (++lineno < *first_displayed_line) {
1523 84451b3e 2018-07-10 stsp free(line);
1527 f7d12f7e 2018-08-01 stsp wlimit = view->ncols < 9 ? 0 : view->ncols - 9;
1528 b700b5d6 2018-07-10 stsp err = format_line(&wline, &width, line, wlimit);
1529 84451b3e 2018-07-10 stsp if (err) {
1530 84451b3e 2018-07-10 stsp free(line);
1531 84451b3e 2018-07-10 stsp return err;
1534 b700b5d6 2018-07-10 stsp if (nprinted == selected_line - 1)
1535 f7d12f7e 2018-08-01 stsp wstandout(view->window);
1537 84451b3e 2018-07-10 stsp blame_line = &lines[lineno - 1];
1538 ee41ec32 2018-07-10 stsp if (blame_line->annotated && prev_id &&
1539 ee41ec32 2018-07-10 stsp got_object_id_cmp(prev_id, blame_line->id) == 0)
1540 f7d12f7e 2018-08-01 stsp waddstr(view->window, " ");
1541 ee41ec32 2018-07-10 stsp else if (blame_line->annotated) {
1542 84451b3e 2018-07-10 stsp char *id_str;
1543 84451b3e 2018-07-10 stsp err = got_object_id_str(&id_str, blame_line->id);
1544 84451b3e 2018-07-10 stsp if (err) {
1545 84451b3e 2018-07-10 stsp free(line);
1546 2550e4c3 2018-07-13 stsp free(wline);
1547 84451b3e 2018-07-10 stsp return err;
1549 f7d12f7e 2018-08-01 stsp wprintw(view->window, "%.8s ", id_str);
1550 84451b3e 2018-07-10 stsp free(id_str);
1551 ee41ec32 2018-07-10 stsp prev_id = blame_line->id;
1553 f7d12f7e 2018-08-01 stsp waddstr(view->window, "........ ");
1554 ee41ec32 2018-07-10 stsp prev_id = NULL;
1557 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
1558 b700b5d6 2018-07-10 stsp while (width < wlimit) {
1559 f7d12f7e 2018-08-01 stsp waddch(view->window, ' ');
1562 b700b5d6 2018-07-10 stsp if (nprinted == selected_line - 1)
1563 f7d12f7e 2018-08-01 stsp wstandend(view->window);
1564 84451b3e 2018-07-10 stsp if (++nprinted == 1)
1565 84451b3e 2018-07-10 stsp *first_displayed_line = lineno;
1566 84451b3e 2018-07-10 stsp free(line);
1567 2550e4c3 2018-07-13 stsp free(wline);
1568 2550e4c3 2018-07-13 stsp wline = NULL;
1570 84451b3e 2018-07-10 stsp *last_displayed_line = lineno;
1572 84451b3e 2018-07-10 stsp update_panels();
1573 84451b3e 2018-07-10 stsp doupdate();
1575 84451b3e 2018-07-10 stsp return NULL;
1578 84451b3e 2018-07-10 stsp static const struct got_error *
1579 84451b3e 2018-07-10 stsp blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
1581 84451b3e 2018-07-10 stsp const struct got_error *err = NULL;
1582 84451b3e 2018-07-10 stsp struct tog_blame_cb_args *a = arg;
1583 84451b3e 2018-07-10 stsp struct tog_blame_line *line;
1586 d68a0a7d 2018-07-10 stsp if (nlines != a->nlines ||
1587 d68a0a7d 2018-07-10 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
1588 84451b3e 2018-07-10 stsp return got_error(GOT_ERR_RANGE);
1590 84451b3e 2018-07-10 stsp if (pthread_mutex_lock(a->mutex) != 0)
1591 84451b3e 2018-07-10 stsp return got_error_from_errno();
1593 18430de3 2018-07-10 stsp if (*a->quit) { /* user has quit the blame view */
1594 d68a0a7d 2018-07-10 stsp err = got_error(GOT_ERR_ITER_COMPLETED);
1595 d68a0a7d 2018-07-10 stsp goto done;
1598 d68a0a7d 2018-07-10 stsp if (lineno == -1)
1599 d68a0a7d 2018-07-10 stsp goto done; /* no change in this commit */
1601 84451b3e 2018-07-10 stsp line = &a->lines[lineno - 1];
1602 d68a0a7d 2018-07-10 stsp if (line->annotated)
1603 d68a0a7d 2018-07-10 stsp goto done;
1605 84451b3e 2018-07-10 stsp line->id = got_object_id_dup(id);
1606 84451b3e 2018-07-10 stsp if (line->id == NULL) {
1607 84451b3e 2018-07-10 stsp err = got_error_from_errno();
1608 84451b3e 2018-07-10 stsp goto done;
1610 84451b3e 2018-07-10 stsp line->annotated = 1;
1612 f7d12f7e 2018-08-01 stsp err = draw_blame(a->view, a->commit_id, a->f, a->path,
1613 245d91c1 2018-07-12 stsp a->lines, a->nlines, 0, *a->selected_line, a->first_displayed_line,
1614 f7d12f7e 2018-08-01 stsp a->last_displayed_line, &eof, a->view->nlines);
1616 84451b3e 2018-07-10 stsp if (pthread_mutex_unlock(a->mutex) != 0)
1617 84451b3e 2018-07-10 stsp return got_error_from_errno();
1618 84451b3e 2018-07-10 stsp return err;
1621 84451b3e 2018-07-10 stsp static void *
1622 84451b3e 2018-07-10 stsp blame_thread(void *arg)
1624 18430de3 2018-07-10 stsp const struct got_error *err;
1625 18430de3 2018-07-10 stsp struct tog_blame_thread_args *ta = arg;
1626 245d91c1 2018-07-12 stsp struct tog_blame_cb_args *a = ta->cb_args;
1629 ab089a2a 2018-07-12 stsp err = got_blame_incremental(ta->path, a->commit_id, ta->repo,
1630 245d91c1 2018-07-12 stsp blame_cb, ta->cb_args);
1632 18430de3 2018-07-10 stsp if (pthread_mutex_lock(a->mutex) != 0)
1633 18430de3 2018-07-10 stsp return (void *)got_error_from_errno();
1635 c9beca56 2018-07-22 stsp got_repo_close(ta->repo);
1636 c9beca56 2018-07-22 stsp ta->repo = NULL;
1637 c9beca56 2018-07-22 stsp *ta->complete = 1;
1639 f7d12f7e 2018-08-01 stsp err = draw_blame(a->view, a->commit_id, a->f, a->path,
1640 f7d12f7e 2018-08-01 stsp a->lines, a->nlines, 1, *a->selected_line,
1641 c9beca56 2018-07-22 stsp a->first_displayed_line, a->last_displayed_line, &eof,
1642 f7d12f7e 2018-08-01 stsp a->view->nlines);
1644 18430de3 2018-07-10 stsp if (pthread_mutex_unlock(a->mutex) != 0 && err == NULL)
1645 18430de3 2018-07-10 stsp err = got_error_from_errno();
1647 18430de3 2018-07-10 stsp return (void *)err;
1650 245d91c1 2018-07-12 stsp static struct got_object_id *
1651 245d91c1 2018-07-12 stsp get_selected_commit_id(struct tog_blame_line *lines,
1652 245d91c1 2018-07-12 stsp int first_displayed_line, int selected_line)
1654 245d91c1 2018-07-12 stsp struct tog_blame_line *line;
1656 245d91c1 2018-07-12 stsp line = &lines[first_displayed_line - 1 + selected_line - 1];
1657 245d91c1 2018-07-12 stsp if (!line->annotated)
1658 245d91c1 2018-07-12 stsp return NULL;
1660 245d91c1 2018-07-12 stsp return line->id;
1663 84451b3e 2018-07-10 stsp static const struct got_error *
1664 245d91c1 2018-07-12 stsp open_selected_commit(struct got_object **pobj, struct got_object **obj,
1665 b880a918 2018-07-10 stsp struct tog_blame_line *lines, int first_displayed_line,
1666 b880a918 2018-07-10 stsp int selected_line, struct got_repository *repo)
1668 b880a918 2018-07-10 stsp const struct got_error *err = NULL;
1669 b880a918 2018-07-10 stsp struct got_commit_object *commit = NULL;
1670 245d91c1 2018-07-12 stsp struct got_object_id *selected_id;
1671 b880a918 2018-07-10 stsp struct got_object_qid *pid;
1673 b880a918 2018-07-10 stsp *pobj = NULL;
1674 b880a918 2018-07-10 stsp *obj = NULL;
1676 245d91c1 2018-07-12 stsp selected_id = get_selected_commit_id(lines,
1677 245d91c1 2018-07-12 stsp first_displayed_line, selected_line);
1678 245d91c1 2018-07-12 stsp if (selected_id == NULL)
1679 b880a918 2018-07-10 stsp return NULL;
1681 245d91c1 2018-07-12 stsp err = got_object_open(obj, repo, selected_id);
1683 b880a918 2018-07-10 stsp goto done;
1685 b880a918 2018-07-10 stsp err = got_object_commit_open(&commit, repo, *obj);
1687 b880a918 2018-07-10 stsp goto done;
1689 b880a918 2018-07-10 stsp pid = SIMPLEQ_FIRST(&commit->parent_ids);
1690 b880a918 2018-07-10 stsp if (pid) {
1691 b880a918 2018-07-10 stsp err = got_object_open(pobj, repo, pid->id);
1693 b880a918 2018-07-10 stsp goto done;
1696 b880a918 2018-07-10 stsp if (commit)
1697 b880a918 2018-07-10 stsp got_object_commit_close(commit);
1698 b880a918 2018-07-10 stsp return err;
1701 b880a918 2018-07-10 stsp static const struct got_error *
1702 245d91c1 2018-07-12 stsp stop_blame(struct tog_blame *blame)
1704 a70480e0 2018-06-23 stsp const struct got_error *err = NULL;
1707 245d91c1 2018-07-12 stsp if (blame->thread) {
1708 245d91c1 2018-07-12 stsp if (pthread_join(blame->thread, (void **)&err) != 0)
1709 245d91c1 2018-07-12 stsp err = got_error_from_errno();
1710 245d91c1 2018-07-12 stsp if (err && err->code == GOT_ERR_ITER_COMPLETED)
1711 245d91c1 2018-07-12 stsp err = NULL;
1712 245d91c1 2018-07-12 stsp blame->thread = NULL;
1714 245d91c1 2018-07-12 stsp if (blame->thread_args.repo) {
1715 245d91c1 2018-07-12 stsp got_repo_close(blame->thread_args.repo);
1716 245d91c1 2018-07-12 stsp blame->thread_args.repo = NULL;
1718 245d91c1 2018-07-12 stsp if (blame->f) {
1719 245d91c1 2018-07-12 stsp fclose(blame->f);
1720 245d91c1 2018-07-12 stsp blame->f = NULL;
1722 245d91c1 2018-07-12 stsp for (i = 0; i < blame->nlines; i++)
1723 245d91c1 2018-07-12 stsp free(blame->lines[i].id);
1724 245d91c1 2018-07-12 stsp free(blame->lines);
1725 245d91c1 2018-07-12 stsp blame->lines = NULL;
1726 75c32340 2018-07-23 stsp free(blame->cb_args.commit_id);
1727 75c32340 2018-07-23 stsp blame->cb_args.commit_id = NULL;
1729 245d91c1 2018-07-12 stsp return err;
1732 245d91c1 2018-07-12 stsp static const struct got_error *
1733 7cc84d77 2018-08-01 stsp run_blame(struct tog_blame *blame, pthread_mutex_t *mutex,
1734 7cc84d77 2018-08-01 stsp struct tog_view *view, int *blame_complete,
1735 245d91c1 2018-07-12 stsp int *first_displayed_line, int *last_displayed_line,
1736 245d91c1 2018-07-12 stsp int *selected_line, int *done, const char *path,
1737 245d91c1 2018-07-12 stsp struct got_object_id *commit_id,
1738 245d91c1 2018-07-12 stsp struct got_repository *repo)
1740 245d91c1 2018-07-12 stsp const struct got_error *err = NULL;
1741 84451b3e 2018-07-10 stsp struct got_blob_object *blob = NULL;
1742 245d91c1 2018-07-12 stsp struct got_repository *thread_repo = NULL;
1743 245d91c1 2018-07-12 stsp struct got_object *obj;
1745 84451b3e 2018-07-10 stsp err = got_object_open_by_path(&obj, repo, commit_id, path);
1747 84451b3e 2018-07-10 stsp goto done;
1748 84451b3e 2018-07-10 stsp if (got_object_get_type(obj) != GOT_OBJ_TYPE_BLOB) {
1749 84451b3e 2018-07-10 stsp err = got_error(GOT_ERR_OBJ_TYPE);
1750 84451b3e 2018-07-10 stsp goto done;
1753 84451b3e 2018-07-10 stsp err = got_object_blob_open(&blob, repo, obj, 8192);
1755 a70480e0 2018-06-23 stsp goto done;
1756 245d91c1 2018-07-12 stsp blame->f = got_opentemp();
1757 245d91c1 2018-07-12 stsp if (blame->f == NULL) {
1758 84451b3e 2018-07-10 stsp err = got_error_from_errno();
1759 84451b3e 2018-07-10 stsp goto done;
1761 245d91c1 2018-07-12 stsp err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
1762 245d91c1 2018-07-12 stsp blame->f, blob);
1764 84451b3e 2018-07-10 stsp goto done;
1766 245d91c1 2018-07-12 stsp blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
1767 245d91c1 2018-07-12 stsp if (blame->lines == NULL) {
1768 84451b3e 2018-07-10 stsp err = got_error_from_errno();
1769 84451b3e 2018-07-10 stsp goto done;
1772 245d91c1 2018-07-12 stsp err = got_repo_open(&thread_repo, got_repo_get_path(repo));
1774 bd24772e 2018-07-11 stsp goto done;
1776 7cc84d77 2018-08-01 stsp blame->cb_args.view = view;
1777 245d91c1 2018-07-12 stsp blame->cb_args.lines = blame->lines;
1778 245d91c1 2018-07-12 stsp blame->cb_args.nlines = blame->nlines;
1779 245d91c1 2018-07-12 stsp blame->cb_args.mutex = mutex;
1780 245d91c1 2018-07-12 stsp blame->cb_args.commit_id = got_object_id_dup(commit_id);
1781 245d91c1 2018-07-12 stsp if (blame->cb_args.commit_id == NULL) {
1782 245d91c1 2018-07-12 stsp err = got_error_from_errno();
1783 245d91c1 2018-07-12 stsp goto done;
1785 245d91c1 2018-07-12 stsp blame->cb_args.f = blame->f;
1786 245d91c1 2018-07-12 stsp blame->cb_args.path = path;
1787 245d91c1 2018-07-12 stsp blame->cb_args.first_displayed_line = first_displayed_line;
1788 245d91c1 2018-07-12 stsp blame->cb_args.selected_line = selected_line;
1789 245d91c1 2018-07-12 stsp blame->cb_args.last_displayed_line = last_displayed_line;
1790 245d91c1 2018-07-12 stsp blame->cb_args.quit = done;
1792 245d91c1 2018-07-12 stsp blame->thread_args.path = path;
1793 245d91c1 2018-07-12 stsp blame->thread_args.repo = thread_repo;
1794 245d91c1 2018-07-12 stsp blame->thread_args.cb_args = &blame->cb_args;
1795 245d91c1 2018-07-12 stsp blame->thread_args.complete = blame_complete;
1796 245d91c1 2018-07-12 stsp *blame_complete = 0;
1798 245d91c1 2018-07-12 stsp if (pthread_create(&blame->thread, NULL, blame_thread,
1799 245d91c1 2018-07-12 stsp &blame->thread_args) != 0) {
1800 245d91c1 2018-07-12 stsp err = got_error_from_errno();
1801 245d91c1 2018-07-12 stsp goto done;
1806 245d91c1 2018-07-12 stsp got_object_blob_close(blob);
1808 245d91c1 2018-07-12 stsp got_object_close(obj);
1810 245d91c1 2018-07-12 stsp stop_blame(blame);
1811 245d91c1 2018-07-12 stsp return err;
1814 245d91c1 2018-07-12 stsp static const struct got_error *
1815 7cbe629d 2018-08-04 stsp open_blame_view(struct tog_view *view, const char *path,
1816 e1cd8fed 2018-08-01 stsp struct got_object_id *commit_id, struct got_repository *repo)
1818 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL;
1819 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
1821 fb2756b9 2018-08-04 stsp SIMPLEQ_INIT(&s->blamed_commits);
1823 fb2756b9 2018-08-04 stsp if (pthread_mutex_init(&s->mutex, NULL) != 0)
1824 7cbe629d 2018-08-04 stsp return got_error_from_errno();
1826 fb2756b9 2018-08-04 stsp err = got_object_qid_alloc(&s->blamed_commit, commit_id);
1828 7cbe629d 2018-08-04 stsp return err;
1830 fb2756b9 2018-08-04 stsp SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
1831 fb2756b9 2018-08-04 stsp s->first_displayed_line = 1;
1832 fb2756b9 2018-08-04 stsp s->last_displayed_line = view->nlines;
1833 fb2756b9 2018-08-04 stsp s->selected_line = 1;
1834 fb2756b9 2018-08-04 stsp s->blame_complete = 0;
1835 fb2756b9 2018-08-04 stsp s->path = path;
1836 fb2756b9 2018-08-04 stsp s->repo = repo;
1837 fb2756b9 2018-08-04 stsp s->commit_id = commit_id;
1838 e9424729 2018-08-04 stsp memset(&s->blame, 0, sizeof(s->blame));
1840 7cbe629d 2018-08-04 stsp return NULL;
1843 7cbe629d 2018-08-04 stsp static void
1844 7cbe629d 2018-08-04 stsp close_blame_view(struct tog_view *view)
1846 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
1848 fb2756b9 2018-08-04 stsp while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
1849 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
1850 fb2756b9 2018-08-04 stsp blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
1851 fb2756b9 2018-08-04 stsp SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
1852 7cbe629d 2018-08-04 stsp got_object_qid_free(blamed_commit);
1856 7cbe629d 2018-08-04 stsp static const struct got_error *
1857 7cbe629d 2018-08-04 stsp show_blame_view(struct tog_view *view)
1859 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL, *thread_err = NULL;
1860 7cbe629d 2018-08-04 stsp int ch, done = 0, eof;
1861 7cbe629d 2018-08-04 stsp struct got_object *obj = NULL, *pobj = NULL;
1862 7cbe629d 2018-08-04 stsp struct tog_view *diff_view;
1863 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
1865 6acd1bd1 2018-08-04 stsp view_show(view);
1867 e9424729 2018-08-04 stsp err = run_blame(&s->blame, &s->mutex, view, &s->blame_complete,
1868 fb2756b9 2018-08-04 stsp &s->first_displayed_line, &s->last_displayed_line,
1869 fb2756b9 2018-08-04 stsp &s->selected_line, &done, s->path, s->blamed_commit->id, s->repo);
1871 245d91c1 2018-07-12 stsp return err;
1873 a70480e0 2018-06-23 stsp while (!done) {
1874 fb2756b9 2018-08-04 stsp if (pthread_mutex_lock(&s->mutex) != 0) {
1875 84451b3e 2018-07-10 stsp err = got_error_from_errno();
1876 84451b3e 2018-07-10 stsp goto done;
1878 e9424729 2018-08-04 stsp err = draw_blame(view, s->blamed_commit->id, s->blame.f,
1879 e9424729 2018-08-04 stsp s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
1880 fb2756b9 2018-08-04 stsp s->selected_line, &s->first_displayed_line,
1881 fb2756b9 2018-08-04 stsp &s->last_displayed_line, &eof, view->nlines);
1882 fb2756b9 2018-08-04 stsp if (pthread_mutex_unlock(&s->mutex) != 0) {
1883 84451b3e 2018-07-10 stsp err = got_error_from_errno();
1884 84451b3e 2018-07-10 stsp goto done;
1888 a70480e0 2018-06-23 stsp nodelay(stdscr, FALSE);
1889 7cc84d77 2018-08-01 stsp ch = wgetch(view->window);
1890 a70480e0 2018-06-23 stsp nodelay(stdscr, TRUE);
1891 fb2756b9 2018-08-04 stsp if (pthread_mutex_lock(&s->mutex) != 0) {
1892 84451b3e 2018-07-10 stsp err = got_error_from_errno();
1893 84451b3e 2018-07-10 stsp goto done;
1895 a70480e0 2018-06-23 stsp switch (ch) {
1900 a70480e0 2018-06-23 stsp case KEY_UP:
1901 fb2756b9 2018-08-04 stsp if (s->selected_line > 1)
1902 fb2756b9 2018-08-04 stsp s->selected_line--;
1903 fb2756b9 2018-08-04 stsp else if (s->selected_line == 1 &&
1904 fb2756b9 2018-08-04 stsp s->first_displayed_line > 1)
1905 fb2756b9 2018-08-04 stsp s->first_displayed_line--;
1907 a70480e0 2018-06-23 stsp case KEY_PPAGE:
1908 38f94530 2018-07-12 stsp case KEY_BACKSPACE:
1909 fb2756b9 2018-08-04 stsp if (s->first_displayed_line == 1) {
1910 fb2756b9 2018-08-04 stsp s->selected_line = 1;
1913 fb2756b9 2018-08-04 stsp if (s->first_displayed_line > view->nlines - 2)
1914 fb2756b9 2018-08-04 stsp s->first_displayed_line -=
1915 f7d12f7e 2018-08-01 stsp (view->nlines - 2);
1917 fb2756b9 2018-08-04 stsp s->first_displayed_line = 1;
1920 a70480e0 2018-06-23 stsp case KEY_DOWN:
1921 fb2756b9 2018-08-04 stsp if (s->selected_line < view->nlines - 2 &&
1922 fb2756b9 2018-08-04 stsp s->first_displayed_line +
1923 e9424729 2018-08-04 stsp s->selected_line <= s->blame.nlines)
1924 fb2756b9 2018-08-04 stsp s->selected_line++;
1925 e9424729 2018-08-04 stsp else if (s->last_displayed_line <
1926 e9424729 2018-08-04 stsp s->blame.nlines)
1927 fb2756b9 2018-08-04 stsp s->first_displayed_line++;
1930 7a2921f9 2018-07-12 stsp case 'p': {
1931 245d91c1 2018-07-12 stsp struct got_object_id *id;
1932 e9424729 2018-08-04 stsp id = get_selected_commit_id(s->blame.lines,
1933 fb2756b9 2018-08-04 stsp s->first_displayed_line, s->selected_line);
1934 245d91c1 2018-07-12 stsp if (id == NULL || got_object_id_cmp(id,
1935 fb2756b9 2018-08-04 stsp s->blamed_commit->id) == 0)
1937 245d91c1 2018-07-12 stsp err = open_selected_commit(&pobj, &obj,
1938 e9424729 2018-08-04 stsp s->blame.lines, s->first_displayed_line,
1939 fb2756b9 2018-08-04 stsp s->selected_line, s->repo);
1942 245d91c1 2018-07-12 stsp if (pobj == NULL && obj == NULL)
1944 7a2921f9 2018-07-12 stsp if (ch == 'p' && pobj == NULL)
1947 fb2756b9 2018-08-04 stsp if (pthread_mutex_unlock(&s->mutex) != 0) {
1948 245d91c1 2018-07-12 stsp err = got_error_from_errno();
1949 ad6a6c43 2018-07-10 stsp goto done;
1951 e9424729 2018-08-04 stsp thread_err = stop_blame(&s->blame);
1953 fb2756b9 2018-08-04 stsp if (pthread_mutex_lock(&s->mutex) != 0) {
1954 245d91c1 2018-07-12 stsp err = got_error_from_errno();
1955 245d91c1 2018-07-12 stsp goto done;
1957 245d91c1 2018-07-12 stsp if (thread_err)
1959 7a2921f9 2018-07-12 stsp id = got_object_get_id(ch == 'b' ? obj : pobj);
1960 1960a6f9 2018-07-12 stsp got_object_close(obj);
1961 1960a6f9 2018-07-12 stsp obj = NULL;
1962 1960a6f9 2018-07-12 stsp if (pobj) {
1963 1960a6f9 2018-07-12 stsp got_object_close(pobj);
1964 14437fb1 2018-07-13 stsp pobj = NULL;
1966 dbc6a6b6 2018-07-12 stsp if (id == NULL) {
1967 245d91c1 2018-07-12 stsp err = got_error_from_errno();
1970 7cbe629d 2018-08-04 stsp err = got_object_qid_alloc(
1971 fb2756b9 2018-08-04 stsp &s->blamed_commit, id);
1974 dbc6a6b6 2018-07-12 stsp goto done;
1975 fb2756b9 2018-08-04 stsp SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
1976 fb2756b9 2018-08-04 stsp s->blamed_commit, entry);
1977 e9424729 2018-08-04 stsp err = run_blame(&s->blame, &s->mutex, view,
1978 fb2756b9 2018-08-04 stsp &s->blame_complete,
1979 fb2756b9 2018-08-04 stsp &s->first_displayed_line,
1980 fb2756b9 2018-08-04 stsp &s->last_displayed_line,
1981 fb2756b9 2018-08-04 stsp &s->selected_line, &done, s->path,
1982 fb2756b9 2018-08-04 stsp s->blamed_commit->id, s->repo);
1987 dbc6a6b6 2018-07-12 stsp case 'B': {
1988 dbc6a6b6 2018-07-12 stsp struct got_object_qid *first;
1989 fb2756b9 2018-08-04 stsp first = SIMPLEQ_FIRST(&s->blamed_commits);
1990 fb2756b9 2018-08-04 stsp if (!got_object_id_cmp(first->id, s->commit_id))
1993 fb2756b9 2018-08-04 stsp if (pthread_mutex_unlock(&s->mutex) != 0) {
1994 dbc6a6b6 2018-07-12 stsp err = got_error_from_errno();
1995 dbc6a6b6 2018-07-12 stsp goto done;
1997 e9424729 2018-08-04 stsp thread_err = stop_blame(&s->blame);
1999 fb2756b9 2018-08-04 stsp if (pthread_mutex_lock(&s->mutex) != 0) {
2000 dbc6a6b6 2018-07-12 stsp err = got_error_from_errno();
2001 dbc6a6b6 2018-07-12 stsp goto done;
2003 dbc6a6b6 2018-07-12 stsp if (thread_err)
2005 fb2756b9 2018-08-04 stsp SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
2006 fb2756b9 2018-08-04 stsp got_object_qid_free(s->blamed_commit);
2007 fb2756b9 2018-08-04 stsp s->blamed_commit =
2008 fb2756b9 2018-08-04 stsp SIMPLEQ_FIRST(&s->blamed_commits);
2009 e9424729 2018-08-04 stsp err = run_blame(&s->blame, &s->mutex, view,
2010 fb2756b9 2018-08-04 stsp &s->blame_complete,
2011 fb2756b9 2018-08-04 stsp &s->first_displayed_line,
2012 fb2756b9 2018-08-04 stsp &s->last_displayed_line,
2013 fb2756b9 2018-08-04 stsp &s->selected_line, &done, s->path,
2014 fb2756b9 2018-08-04 stsp s->blamed_commit->id, s->repo);
2019 245d91c1 2018-07-12 stsp case KEY_ENTER:
2020 245d91c1 2018-07-12 stsp case '\r':
2021 245d91c1 2018-07-12 stsp err = open_selected_commit(&pobj, &obj,
2022 e9424729 2018-08-04 stsp s->blame.lines, s->first_displayed_line,
2023 fb2756b9 2018-08-04 stsp s->selected_line, s->repo);
2026 b880a918 2018-07-10 stsp if (pobj == NULL && obj == NULL)
2028 b3665f43 2018-08-04 stsp diff_view = view_open(0, 0, 0, 0, view,
2029 d6b05b5b 2018-08-04 stsp TOG_VIEW_DIFF);
2030 ea5e7bb5 2018-08-01 stsp if (diff_view == NULL) {
2031 ea5e7bb5 2018-08-01 stsp err = got_error_from_errno();
2034 7cbe629d 2018-08-04 stsp err = open_diff_view(diff_view, pobj, obj,
2038 5dc9f4bc 2018-08-04 stsp err = show_diff_view(diff_view);
2039 5dc9f4bc 2018-08-04 stsp close_diff_view(diff_view);
2040 96a765a8 2018-08-04 stsp view_close(diff_view);
2043 6acd1bd1 2018-08-04 stsp view_show(view);
2044 b880a918 2018-07-10 stsp if (pobj) {
2045 b880a918 2018-07-10 stsp got_object_close(pobj);
2046 b880a918 2018-07-10 stsp pobj = NULL;
2048 b880a918 2018-07-10 stsp got_object_close(obj);
2049 b880a918 2018-07-10 stsp obj = NULL;
2053 a70480e0 2018-06-23 stsp case KEY_NPAGE:
2055 e9424729 2018-08-04 stsp if (s->last_displayed_line >= s->blame.nlines &&
2056 fb2756b9 2018-08-04 stsp s->selected_line < view->nlines - 2) {
2057 e9424729 2018-08-04 stsp s->selected_line = MIN(s->blame.nlines,
2058 f7d12f7e 2018-08-01 stsp view->nlines - 2);
2061 fb2756b9 2018-08-04 stsp if (s->last_displayed_line + view->nlines - 2
2062 e9424729 2018-08-04 stsp <= s->blame.nlines)
2063 fb2756b9 2018-08-04 stsp s->first_displayed_line +=
2064 f7d12f7e 2018-08-01 stsp view->nlines - 2;
2066 fb2756b9 2018-08-04 stsp s->first_displayed_line =
2067 e9424729 2018-08-04 stsp s->blame.nlines -
2068 e9424729 2018-08-04 stsp (view->nlines - 3);
2070 7e5c1fe1 2018-08-01 stsp case KEY_RESIZE:
2071 a41d2007 2018-08-01 stsp err = view_resize(view);
2074 fb2756b9 2018-08-04 stsp if (s->selected_line > view->nlines - 2) {
2075 e9424729 2018-08-04 stsp s->selected_line = MIN(s->blame.nlines,
2076 7e5c1fe1 2018-08-01 stsp view->nlines - 2);
2082 fb2756b9 2018-08-04 stsp if (pthread_mutex_unlock(&s->mutex) != 0)
2083 84451b3e 2018-07-10 stsp err = got_error_from_errno();
2084 245d91c1 2018-07-12 stsp if (err || thread_err)
2089 b880a918 2018-07-10 stsp got_object_close(pobj);
2090 e9424729 2018-08-04 stsp if (s->blame.thread)
2091 e9424729 2018-08-04 stsp thread_err = stop_blame(&s->blame);
2092 245d91c1 2018-07-12 stsp return thread_err ? thread_err : err;
2095 a70480e0 2018-06-23 stsp static const struct got_error *
2096 9f7d7167 2018-04-29 stsp cmd_blame(int argc, char *argv[])
2098 a70480e0 2018-06-23 stsp const struct got_error *error;
2099 a70480e0 2018-06-23 stsp struct got_repository *repo = NULL;
2100 69069811 2018-08-02 stsp char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2101 a70480e0 2018-06-23 stsp struct got_object_id *commit_id = NULL;
2102 a70480e0 2018-06-23 stsp char *commit_id_str = NULL;
2104 e1cd8fed 2018-08-01 stsp struct tog_view *view;
2106 a70480e0 2018-06-23 stsp #ifndef PROFILE
2107 a70480e0 2018-06-23 stsp if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
2108 a70480e0 2018-06-23 stsp err(1, "pledge");
2111 69069811 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2112 a70480e0 2018-06-23 stsp switch (ch) {
2114 a70480e0 2018-06-23 stsp commit_id_str = optarg;
2117 69069811 2018-08-02 stsp repo_path = realpath(optarg, NULL);
2118 69069811 2018-08-02 stsp if (repo_path == NULL)
2119 69069811 2018-08-02 stsp err(1, "-r option");
2123 a70480e0 2018-06-23 stsp /* NOTREACHED */
2127 a70480e0 2018-06-23 stsp argc -= optind;
2128 a70480e0 2018-06-23 stsp argv += optind;
2130 69069811 2018-08-02 stsp if (argc == 1)
2131 69069811 2018-08-02 stsp path = argv[0];
2133 a70480e0 2018-06-23 stsp usage_blame();
2135 69069811 2018-08-02 stsp cwd = getcwd(NULL, 0);
2136 69069811 2018-08-02 stsp if (cwd == NULL) {
2137 69069811 2018-08-02 stsp error = got_error_from_errno();
2138 69069811 2018-08-02 stsp goto done;
2140 69069811 2018-08-02 stsp if (repo_path == NULL) {
2141 69069811 2018-08-02 stsp repo_path = strdup(cwd);
2142 69069811 2018-08-02 stsp if (repo_path == NULL) {
2143 69069811 2018-08-02 stsp error = got_error_from_errno();
2144 69069811 2018-08-02 stsp goto done;
2149 69069811 2018-08-02 stsp error = got_repo_open(&repo, repo_path);
2150 a70480e0 2018-06-23 stsp if (error != NULL)
2151 66b4983c 2018-06-23 stsp return error;
2153 69069811 2018-08-02 stsp error = got_repo_map_path(&in_repo_path, repo, path);
2154 69069811 2018-08-02 stsp if (error != NULL)
2155 69069811 2018-08-02 stsp goto done;
2157 a70480e0 2018-06-23 stsp if (commit_id_str == NULL) {
2158 a70480e0 2018-06-23 stsp struct got_reference *head_ref;
2159 a70480e0 2018-06-23 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
2160 a70480e0 2018-06-23 stsp if (error != NULL)
2161 66b4983c 2018-06-23 stsp goto done;
2162 a70480e0 2018-06-23 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
2163 a70480e0 2018-06-23 stsp got_ref_close(head_ref);
2165 a70480e0 2018-06-23 stsp struct got_object *obj;
2166 a70480e0 2018-06-23 stsp error = got_object_open_by_id_str(&obj, repo, commit_id_str);
2167 a70480e0 2018-06-23 stsp if (error != NULL)
2168 66b4983c 2018-06-23 stsp goto done;
2169 a70480e0 2018-06-23 stsp commit_id = got_object_get_id(obj);
2170 a19e88aa 2018-06-23 stsp if (commit_id == NULL)
2171 66b4983c 2018-06-23 stsp error = got_error_from_errno();
2172 a19e88aa 2018-06-23 stsp got_object_close(obj);
2174 a19e88aa 2018-06-23 stsp if (error != NULL)
2175 a19e88aa 2018-06-23 stsp goto done;
2177 b3665f43 2018-08-04 stsp view = view_open(0, 0, 0, 0, NULL, TOG_VIEW_BLAME);
2178 e1cd8fed 2018-08-01 stsp if (view == NULL) {
2179 e1cd8fed 2018-08-01 stsp error = got_error_from_errno();
2180 e1cd8fed 2018-08-01 stsp goto done;
2182 7cbe629d 2018-08-04 stsp error = open_blame_view(view, in_repo_path, commit_id, repo);
2183 7cbe629d 2018-08-04 stsp if (error)
2184 7cbe629d 2018-08-04 stsp goto done;
2185 7cbe629d 2018-08-04 stsp error = show_blame_view(view);
2186 7cbe629d 2018-08-04 stsp close_blame_view(view);
2187 96a765a8 2018-08-04 stsp view_close(view);
2189 69069811 2018-08-02 stsp free(in_repo_path);
2190 69069811 2018-08-02 stsp free(repo_path);
2191 69069811 2018-08-02 stsp free(cwd);
2192 a70480e0 2018-06-23 stsp free(commit_id);
2194 a70480e0 2018-06-23 stsp got_repo_close(repo);
2195 a70480e0 2018-06-23 stsp return error;
2198 ffd1d5e5 2018-06-23 stsp static const struct got_error *
2199 f7d12f7e 2018-08-01 stsp draw_tree_entries(struct tog_view *view,
2200 f7d12f7e 2018-08-01 stsp struct got_tree_entry **first_displayed_entry,
2201 ffd1d5e5 2018-06-23 stsp struct got_tree_entry **last_displayed_entry,
2202 ffd1d5e5 2018-06-23 stsp struct got_tree_entry **selected_entry, int *ndisplayed,
2203 f7d12f7e 2018-08-01 stsp const char *label, int show_ids, const char *parent_path,
2204 ce52c690 2018-06-23 stsp const struct got_tree_entries *entries, int selected, int limit, int isroot)
2206 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
2207 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *te;
2208 ffd1d5e5 2018-06-23 stsp wchar_t *wline;
2209 ffd1d5e5 2018-06-23 stsp int width, n;
2211 ffd1d5e5 2018-06-23 stsp *ndisplayed = 0;
2213 f7d12f7e 2018-08-01 stsp werase(view->window);
2215 ffd1d5e5 2018-06-23 stsp if (limit == 0)
2216 ffd1d5e5 2018-06-23 stsp return NULL;
2218 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, label, view->ncols);
2220 ffd1d5e5 2018-06-23 stsp return err;
2221 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
2222 2550e4c3 2018-07-13 stsp free(wline);
2223 2550e4c3 2018-07-13 stsp wline = NULL;
2224 f7d12f7e 2018-08-01 stsp if (width < view->ncols)
2225 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
2226 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
2227 ffd1d5e5 2018-06-23 stsp return NULL;
2228 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, parent_path, view->ncols);
2230 ce52c690 2018-06-23 stsp return err;
2231 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
2232 2550e4c3 2018-07-13 stsp free(wline);
2233 2550e4c3 2018-07-13 stsp wline = NULL;
2234 f7d12f7e 2018-08-01 stsp if (width < view->ncols)
2235 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
2236 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
2237 ffd1d5e5 2018-06-23 stsp return NULL;
2238 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
2239 a1eca9bb 2018-06-23 stsp if (--limit <= 0)
2240 a1eca9bb 2018-06-23 stsp return NULL;
2242 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_FIRST(&entries->head);
2243 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry == NULL) {
2244 ffd1d5e5 2018-06-23 stsp if (selected == 0) {
2245 f7d12f7e 2018-08-01 stsp wstandout(view->window);
2246 ffd1d5e5 2018-06-23 stsp *selected_entry = NULL;
2248 f7d12f7e 2018-08-01 stsp waddstr(view->window, " ..\n"); /* parent directory */
2249 ffd1d5e5 2018-06-23 stsp if (selected == 0)
2250 f7d12f7e 2018-08-01 stsp wstandend(view->window);
2251 ffd1d5e5 2018-06-23 stsp (*ndisplayed)++;
2252 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
2253 ffd1d5e5 2018-06-23 stsp return NULL;
2257 ffd1d5e5 2018-06-23 stsp while (te != *first_displayed_entry)
2258 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_NEXT(te, entry);
2261 ffd1d5e5 2018-06-23 stsp while (te) {
2262 1d13200f 2018-07-12 stsp char *line = NULL, *id_str = NULL;
2264 1d13200f 2018-07-12 stsp if (show_ids) {
2265 1d13200f 2018-07-12 stsp err = got_object_id_str(&id_str, te->id);
2267 1d13200f 2018-07-12 stsp return got_error_from_errno();
2269 1d13200f 2018-07-12 stsp if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
2270 1d13200f 2018-07-12 stsp te->name, S_ISDIR(te->mode) ? "/" : "") == -1) {
2271 1d13200f 2018-07-12 stsp free(id_str);
2272 ffd1d5e5 2018-06-23 stsp return got_error_from_errno();
2274 1d13200f 2018-07-12 stsp free(id_str);
2275 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, line, view->ncols);
2276 ffd1d5e5 2018-06-23 stsp if (err) {
2277 ffd1d5e5 2018-06-23 stsp free(line);
2280 ffd1d5e5 2018-06-23 stsp if (n == selected) {
2281 f7d12f7e 2018-08-01 stsp wstandout(view->window);
2282 ffd1d5e5 2018-06-23 stsp *selected_entry = te;
2284 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
2285 f7d12f7e 2018-08-01 stsp if (width < view->ncols)
2286 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
2287 ffd1d5e5 2018-06-23 stsp if (n == selected)
2288 f7d12f7e 2018-08-01 stsp wstandend(view->window);
2289 ffd1d5e5 2018-06-23 stsp free(line);
2290 2550e4c3 2018-07-13 stsp free(wline);
2291 2550e4c3 2018-07-13 stsp wline = NULL;
2293 ffd1d5e5 2018-06-23 stsp (*ndisplayed)++;
2294 ffd1d5e5 2018-06-23 stsp *last_displayed_entry = te;
2295 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
2297 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_NEXT(te, entry);
2300 ffd1d5e5 2018-06-23 stsp return err;
2303 ffd1d5e5 2018-06-23 stsp static void
2304 ffd1d5e5 2018-06-23 stsp tree_scroll_up(struct got_tree_entry **first_displayed_entry, int maxscroll,
2305 ffd1d5e5 2018-06-23 stsp const struct got_tree_entries *entries, int isroot)
2307 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *te, *prev;
2310 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry == NULL)
2313 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_FIRST(&entries->head);
2314 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry == te) {
2315 ffd1d5e5 2018-06-23 stsp if (!isroot)
2316 ffd1d5e5 2018-06-23 stsp *first_displayed_entry = NULL;
2320 ffd1d5e5 2018-06-23 stsp /* XXX this is stupid... switch to TAILQ? */
2321 ffd1d5e5 2018-06-23 stsp for (i = 0; i < maxscroll; i++) {
2322 ffd1d5e5 2018-06-23 stsp while (te != *first_displayed_entry) {
2323 ffd1d5e5 2018-06-23 stsp prev = te;
2324 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_NEXT(te, entry);
2326 ffd1d5e5 2018-06-23 stsp *first_displayed_entry = prev;
2327 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_FIRST(&entries->head);
2329 ffd1d5e5 2018-06-23 stsp if (!isroot && te == SIMPLEQ_FIRST(&entries->head) && i < maxscroll)
2330 ffd1d5e5 2018-06-23 stsp *first_displayed_entry = NULL;
2333 ffd1d5e5 2018-06-23 stsp static void
2334 ffd1d5e5 2018-06-23 stsp tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
2335 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *last_displayed_entry,
2336 ffd1d5e5 2018-06-23 stsp const struct got_tree_entries *entries)
2338 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *next;
2339 ffd1d5e5 2018-06-23 stsp int n = 0;
2341 ffd1d5e5 2018-06-23 stsp if (SIMPLEQ_NEXT(last_displayed_entry, entry) == NULL)
2344 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry)
2345 ffd1d5e5 2018-06-23 stsp next = SIMPLEQ_NEXT(*first_displayed_entry, entry);
2347 ffd1d5e5 2018-06-23 stsp next = SIMPLEQ_FIRST(&entries->head);
2348 ffd1d5e5 2018-06-23 stsp while (next) {
2349 ffd1d5e5 2018-06-23 stsp *first_displayed_entry = next;
2350 ffd1d5e5 2018-06-23 stsp if (++n >= maxscroll)
2352 ffd1d5e5 2018-06-23 stsp next = SIMPLEQ_NEXT(next, entry);
2356 ffd1d5e5 2018-06-23 stsp static const struct got_error *
2357 ce52c690 2018-06-23 stsp tree_entry_path(char **path, struct tog_parent_trees *parents,
2358 ce52c690 2018-06-23 stsp struct got_tree_entry *te)
2360 cb2ebc8a 2018-06-23 stsp const struct got_error *err = NULL;
2361 ffd1d5e5 2018-06-23 stsp struct tog_parent_tree *pt;
2362 ffd1d5e5 2018-06-23 stsp size_t len = 2; /* for leading slash and NUL */
2364 d9765a41 2018-06-23 stsp TAILQ_FOREACH(pt, parents, entry)
2365 ffd1d5e5 2018-06-23 stsp len += strlen(pt->selected_entry->name) + 1 /* slash */;
2367 ce52c690 2018-06-23 stsp len += strlen(te->name);
2369 ce52c690 2018-06-23 stsp *path = calloc(1, len);
2370 ffd1d5e5 2018-06-23 stsp if (path == NULL)
2371 ffd1d5e5 2018-06-23 stsp return got_error_from_errno();
2373 ce52c690 2018-06-23 stsp (*path)[0] = '/';
2374 d9765a41 2018-06-23 stsp pt = TAILQ_LAST(parents, tog_parent_trees);
2375 d9765a41 2018-06-23 stsp while (pt) {
2376 ce52c690 2018-06-23 stsp if (strlcat(*path, pt->selected_entry->name, len) >= len) {
2377 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
2378 cb2ebc8a 2018-06-23 stsp goto done;
2380 ce52c690 2018-06-23 stsp if (strlcat(*path, "/", len) >= len) {
2381 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
2382 cb2ebc8a 2018-06-23 stsp goto done;
2384 d9765a41 2018-06-23 stsp pt = TAILQ_PREV(pt, tog_parent_trees, entry);
2387 ce52c690 2018-06-23 stsp if (strlcat(*path, te->name, len) >= len) {
2388 ce52c690 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
2389 ce52c690 2018-06-23 stsp goto done;
2393 ce52c690 2018-06-23 stsp if (err) {
2394 ce52c690 2018-06-23 stsp free(*path);
2395 ce52c690 2018-06-23 stsp *path = NULL;
2397 ce52c690 2018-06-23 stsp return err;
2400 ce52c690 2018-06-23 stsp static const struct got_error *
2401 cdf1ee82 2018-08-01 stsp blame_tree_entry(struct tog_view *parent_view, struct got_tree_entry *te,
2402 e1cd8fed 2018-08-01 stsp struct tog_parent_trees *parents, struct got_object_id *commit_id,
2403 e1cd8fed 2018-08-01 stsp struct got_repository *repo)
2405 ce52c690 2018-06-23 stsp const struct got_error *err = NULL;
2406 ce52c690 2018-06-23 stsp char *path;
2407 cdf1ee82 2018-08-01 stsp struct tog_view *view;
2409 ce52c690 2018-06-23 stsp err = tree_entry_path(&path, parents, te);
2411 ce52c690 2018-06-23 stsp return err;
2413 b3665f43 2018-08-04 stsp view = view_open(0, 0, 0, 0, parent_view, TOG_VIEW_BLAME);
2414 cdf1ee82 2018-08-01 stsp if (view) {
2415 7cbe629d 2018-08-04 stsp err = open_blame_view(view, path, commit_id, repo);
2417 7cbe629d 2018-08-04 stsp goto done;
2418 7cbe629d 2018-08-04 stsp err = show_blame_view(view);
2419 7cbe629d 2018-08-04 stsp close_blame_view(view);
2420 96a765a8 2018-08-04 stsp view_close(view);
2422 cdf1ee82 2018-08-01 stsp err = got_error_from_errno();
2424 6acd1bd1 2018-08-04 stsp view_show(parent_view);
2426 69efd4c4 2018-07-18 stsp free(path);
2427 69efd4c4 2018-07-18 stsp return err;
2430 69efd4c4 2018-07-18 stsp static const struct got_error *
2431 04cc582a 2018-08-01 stsp log_tree_entry(struct tog_view *view, struct got_tree_entry *te,
2432 04cc582a 2018-08-01 stsp struct tog_parent_trees *parents, struct got_object_id *commit_id,
2433 04cc582a 2018-08-01 stsp struct got_repository *repo)
2435 69efd4c4 2018-07-18 stsp const struct got_error *err = NULL;
2436 69efd4c4 2018-07-18 stsp char *path;
2438 69efd4c4 2018-07-18 stsp err = tree_entry_path(&path, parents, te);
2440 69efd4c4 2018-07-18 stsp return err;
2442 ba4f502b 2018-08-04 stsp err = open_log_view(view, commit_id, repo, path);
2444 ba4f502b 2018-08-04 stsp goto done;
2445 ba4f502b 2018-08-04 stsp err = show_log_view(view);
2446 ba4f502b 2018-08-04 stsp close_log_view(view);
2448 cb2ebc8a 2018-06-23 stsp free(path);
2449 cb2ebc8a 2018-06-23 stsp return err;
2452 ffd1d5e5 2018-06-23 stsp static const struct got_error *
2453 ad80ab7b 2018-08-04 stsp open_tree_view(struct tog_view *view, struct got_tree_object *root,
2454 5221c383 2018-08-01 stsp struct got_object_id *commit_id, struct got_repository *repo)
2456 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
2457 ad80ab7b 2018-08-04 stsp char *commit_id_str = NULL;
2458 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
2460 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->parents);
2462 ffd1d5e5 2018-06-23 stsp err = got_object_id_str(&commit_id_str, commit_id);
2463 ffd1d5e5 2018-06-23 stsp if (err != NULL)
2464 ffd1d5e5 2018-06-23 stsp goto done;
2466 fb2756b9 2018-08-04 stsp if (asprintf(&s->tree_label, "commit: %s", commit_id_str) == -1) {
2467 ffd1d5e5 2018-06-23 stsp err = got_error_from_errno();
2468 ffd1d5e5 2018-06-23 stsp goto done;
2471 fb2756b9 2018-08-04 stsp s->root = s->tree = root;
2472 fb2756b9 2018-08-04 stsp s->entries = got_object_tree_get_entries(root);
2473 fb2756b9 2018-08-04 stsp s->first_displayed_entry = SIMPLEQ_FIRST(&s->entries->head);
2474 fb2756b9 2018-08-04 stsp s->commit_id = commit_id;
2475 fb2756b9 2018-08-04 stsp s->repo = repo;
2477 ad80ab7b 2018-08-04 stsp free(commit_id_str);
2479 fb2756b9 2018-08-04 stsp free(s->tree_label);
2480 ad80ab7b 2018-08-04 stsp return err;
2483 ad80ab7b 2018-08-04 stsp static void
2484 ad80ab7b 2018-08-04 stsp close_tree_view(struct tog_view *view)
2486 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
2488 fb2756b9 2018-08-04 stsp free(s->tree_label);
2489 fb2756b9 2018-08-04 stsp while (!TAILQ_EMPTY(&s->parents)) {
2490 ad80ab7b 2018-08-04 stsp struct tog_parent_tree *parent;
2491 fb2756b9 2018-08-04 stsp parent = TAILQ_FIRST(&s->parents);
2492 fb2756b9 2018-08-04 stsp TAILQ_REMOVE(&s->parents, parent, entry);
2493 ad80ab7b 2018-08-04 stsp free(parent);
2496 fb2756b9 2018-08-04 stsp if (s->tree != s->root)
2497 fb2756b9 2018-08-04 stsp got_object_tree_close(s->tree);
2500 ad80ab7b 2018-08-04 stsp static const struct got_error *
2501 ad80ab7b 2018-08-04 stsp show_tree_view(struct tog_view *view)
2503 ad80ab7b 2018-08-04 stsp const struct got_error *err = NULL;
2504 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
2505 ad80ab7b 2018-08-04 stsp int ch, done = 0;
2506 ad80ab7b 2018-08-04 stsp int nentries;
2508 6acd1bd1 2018-08-04 stsp view_show(view);
2510 ffd1d5e5 2018-06-23 stsp while (!done) {
2511 ce52c690 2018-06-23 stsp char *parent_path;
2512 fb2756b9 2018-08-04 stsp s->entries = got_object_tree_get_entries(s->tree);
2513 fb2756b9 2018-08-04 stsp nentries = s->entries->nentries;
2514 fb2756b9 2018-08-04 stsp if (s->tree != s->root)
2515 ffd1d5e5 2018-06-23 stsp nentries++; /* '..' directory */
2517 fb2756b9 2018-08-04 stsp err = tree_entry_path(&parent_path, &s->parents, NULL);
2519 ce52c690 2018-06-23 stsp goto done;
2521 fb2756b9 2018-08-04 stsp err = draw_tree_entries(view, &s->first_displayed_entry,
2522 fb2756b9 2018-08-04 stsp &s->last_displayed_entry, &s->selected_entry,
2523 fb2756b9 2018-08-04 stsp &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
2524 fb2756b9 2018-08-04 stsp s->entries, s->selected, view->nlines, s->tree == s->root);
2525 ce52c690 2018-06-23 stsp free(parent_path);
2529 ffd1d5e5 2018-06-23 stsp nodelay(stdscr, FALSE);
2530 cc3c9aac 2018-08-01 stsp ch = wgetch(view->window);
2531 ffd1d5e5 2018-06-23 stsp nodelay(stdscr, TRUE);
2532 ffd1d5e5 2018-06-23 stsp switch (ch) {
2537 fb2756b9 2018-08-04 stsp s->show_ids = !s->show_ids;
2540 fb2756b9 2018-08-04 stsp if (s->selected_entry) {
2541 04cc582a 2018-08-01 stsp struct tog_view *log_view;
2542 b3665f43 2018-08-04 stsp log_view = view_open(0, 0, 0, 0, view,
2543 d6b05b5b 2018-08-04 stsp TOG_VIEW_LOG);
2544 04cc582a 2018-08-01 stsp if (log_view == NULL) {
2545 04cc582a 2018-08-01 stsp err = got_error_from_errno();
2546 04cc582a 2018-08-01 stsp goto done;
2548 04cc582a 2018-08-01 stsp err = log_tree_entry(log_view,
2549 fb2756b9 2018-08-04 stsp s->selected_entry, &s->parents,
2550 fb2756b9 2018-08-04 stsp s->commit_id, s->repo);
2551 96a765a8 2018-08-04 stsp view_close(log_view);
2552 6acd1bd1 2018-08-04 stsp view_show(view);
2554 69efd4c4 2018-07-18 stsp goto done;
2558 ffd1d5e5 2018-06-23 stsp case KEY_UP:
2559 fb2756b9 2018-08-04 stsp if (s->selected > 0)
2560 fb2756b9 2018-08-04 stsp s->selected--;
2561 fb2756b9 2018-08-04 stsp if (s->selected > 0)
2563 fb2756b9 2018-08-04 stsp tree_scroll_up(&s->first_displayed_entry, 1,
2564 fb2756b9 2018-08-04 stsp s->entries, s->tree == s->root);
2566 ffd1d5e5 2018-06-23 stsp case KEY_PPAGE:
2567 fb2756b9 2018-08-04 stsp if (SIMPLEQ_FIRST(&s->entries->head) ==
2568 fb2756b9 2018-08-04 stsp s->first_displayed_entry) {
2569 fb2756b9 2018-08-04 stsp if (s->tree != s->root)
2570 fb2756b9 2018-08-04 stsp s->first_displayed_entry = NULL;
2571 fb2756b9 2018-08-04 stsp s->selected = 0;
2574 fb2756b9 2018-08-04 stsp tree_scroll_up(&s->first_displayed_entry,
2575 fb2756b9 2018-08-04 stsp view->nlines, s->entries,
2576 fb2756b9 2018-08-04 stsp s->tree == s->root);
2579 ffd1d5e5 2018-06-23 stsp case KEY_DOWN:
2580 fb2756b9 2018-08-04 stsp if (s->selected < s->ndisplayed - 1) {
2581 fb2756b9 2018-08-04 stsp s->selected++;
2584 fb2756b9 2018-08-04 stsp tree_scroll_down(&s->first_displayed_entry, 1,
2585 fb2756b9 2018-08-04 stsp s->last_displayed_entry, s->entries);
2587 ffd1d5e5 2018-06-23 stsp case KEY_NPAGE:
2588 fb2756b9 2018-08-04 stsp tree_scroll_down(&s->first_displayed_entry,
2589 fb2756b9 2018-08-04 stsp view->nlines, s->last_displayed_entry,
2590 fb2756b9 2018-08-04 stsp s->entries);
2591 fb2756b9 2018-08-04 stsp if (SIMPLEQ_NEXT(s->last_displayed_entry,
2594 ffd1d5e5 2018-06-23 stsp /* can't scroll any further; move cursor down */
2595 fb2756b9 2018-08-04 stsp if (s->selected < s->ndisplayed - 1)
2596 fb2756b9 2018-08-04 stsp s->selected = s->ndisplayed - 1;
2598 ffd1d5e5 2018-06-23 stsp case KEY_ENTER:
2599 ffd1d5e5 2018-06-23 stsp case '\r':
2600 fb2756b9 2018-08-04 stsp if (s->selected_entry == NULL) {
2601 ffd1d5e5 2018-06-23 stsp struct tog_parent_tree *parent;
2602 ffd1d5e5 2018-06-23 stsp case KEY_BACKSPACE:
2603 ffd1d5e5 2018-06-23 stsp /* user selected '..' */
2604 fb2756b9 2018-08-04 stsp if (s->tree == s->root)
2606 fb2756b9 2018-08-04 stsp parent = TAILQ_FIRST(&s->parents);
2607 fb2756b9 2018-08-04 stsp TAILQ_REMOVE(&s->parents, parent,
2609 fb2756b9 2018-08-04 stsp got_object_tree_close(s->tree);
2610 fb2756b9 2018-08-04 stsp s->tree = parent->tree;
2611 fb2756b9 2018-08-04 stsp s->first_displayed_entry =
2612 ffd1d5e5 2018-06-23 stsp parent->first_displayed_entry;
2613 fb2756b9 2018-08-04 stsp s->selected_entry =
2614 fb2756b9 2018-08-04 stsp parent->selected_entry;
2615 fb2756b9 2018-08-04 stsp s->selected = parent->selected;
2616 ffd1d5e5 2018-06-23 stsp free(parent);
2617 fb2756b9 2018-08-04 stsp } else if (S_ISDIR(s->selected_entry->mode)) {
2618 ffd1d5e5 2018-06-23 stsp struct tog_parent_tree *parent;
2619 ffd1d5e5 2018-06-23 stsp struct got_tree_object *child;
2620 fb2756b9 2018-08-04 stsp err = got_object_open_as_tree(&child,
2621 fb2756b9 2018-08-04 stsp s->repo, s->selected_entry->id);
2623 ffd1d5e5 2018-06-23 stsp goto done;
2624 ffd1d5e5 2018-06-23 stsp parent = calloc(1, sizeof(*parent));
2625 ffd1d5e5 2018-06-23 stsp if (parent == NULL) {
2626 ffd1d5e5 2018-06-23 stsp err = got_error_from_errno();
2627 ffd1d5e5 2018-06-23 stsp goto done;
2629 fb2756b9 2018-08-04 stsp parent->tree = s->tree;
2630 ffd1d5e5 2018-06-23 stsp parent->first_displayed_entry =
2631 fb2756b9 2018-08-04 stsp s->first_displayed_entry;
2632 fb2756b9 2018-08-04 stsp parent->selected_entry =
2633 fb2756b9 2018-08-04 stsp s->selected_entry;
2634 fb2756b9 2018-08-04 stsp parent->selected = s->selected;
2635 fb2756b9 2018-08-04 stsp TAILQ_INSERT_HEAD(&s->parents, parent,
2637 fb2756b9 2018-08-04 stsp s->tree = child;
2638 fb2756b9 2018-08-04 stsp s->selected = 0;
2639 fb2756b9 2018-08-04 stsp s->first_displayed_entry = NULL;
2640 fb2756b9 2018-08-04 stsp } else if (S_ISREG(s->selected_entry->mode)) {
2641 cdf1ee82 2018-08-01 stsp err = blame_tree_entry(view,
2642 fb2756b9 2018-08-04 stsp s->selected_entry, &s->parents,
2643 fb2756b9 2018-08-04 stsp s->commit_id, s->repo);
2645 ffd1d5e5 2018-06-23 stsp goto done;
2648 ffd1d5e5 2018-06-23 stsp case KEY_RESIZE:
2649 a41d2007 2018-08-01 stsp err = view_resize(view);
2651 a41d2007 2018-08-01 stsp goto done;
2652 fb2756b9 2018-08-04 stsp if (s->selected > view->nlines)
2653 fb2756b9 2018-08-04 stsp s->selected = s->ndisplayed - 1;
2660 ffd1d5e5 2018-06-23 stsp return err;
2663 ffd1d5e5 2018-06-23 stsp __dead static void
2664 ffd1d5e5 2018-06-23 stsp usage_tree(void)
2667 ffd1d5e5 2018-06-23 stsp fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
2668 ffd1d5e5 2018-06-23 stsp getprogname());
2672 ffd1d5e5 2018-06-23 stsp static const struct got_error *
2673 ffd1d5e5 2018-06-23 stsp cmd_tree(int argc, char *argv[])
2675 ffd1d5e5 2018-06-23 stsp const struct got_error *error;
2676 ffd1d5e5 2018-06-23 stsp struct got_repository *repo = NULL;
2677 ffd1d5e5 2018-06-23 stsp char *repo_path = NULL;
2678 ffd1d5e5 2018-06-23 stsp struct got_object_id *commit_id = NULL;
2679 ffd1d5e5 2018-06-23 stsp char *commit_id_arg = NULL;
2680 ffd1d5e5 2018-06-23 stsp struct got_commit_object *commit = NULL;
2681 ffd1d5e5 2018-06-23 stsp struct got_tree_object *tree = NULL;
2683 5221c383 2018-08-01 stsp struct tog_view *view;
2685 ffd1d5e5 2018-06-23 stsp #ifndef PROFILE
2686 ffd1d5e5 2018-06-23 stsp if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
2687 ffd1d5e5 2018-06-23 stsp err(1, "pledge");
2690 ffd1d5e5 2018-06-23 stsp while ((ch = getopt(argc, argv, "c:")) != -1) {
2691 ffd1d5e5 2018-06-23 stsp switch (ch) {
2693 ffd1d5e5 2018-06-23 stsp commit_id_arg = optarg;
2697 ffd1d5e5 2018-06-23 stsp /* NOTREACHED */
2701 ffd1d5e5 2018-06-23 stsp argc -= optind;
2702 ffd1d5e5 2018-06-23 stsp argv += optind;
2704 ffd1d5e5 2018-06-23 stsp if (argc == 0) {
2705 ffd1d5e5 2018-06-23 stsp repo_path = getcwd(NULL, 0);
2706 ffd1d5e5 2018-06-23 stsp if (repo_path == NULL)
2707 ffd1d5e5 2018-06-23 stsp return got_error_from_errno();
2708 ffd1d5e5 2018-06-23 stsp } else if (argc == 1) {
2709 ffd1d5e5 2018-06-23 stsp repo_path = realpath(argv[0], NULL);
2710 ffd1d5e5 2018-06-23 stsp if (repo_path == NULL)
2711 ffd1d5e5 2018-06-23 stsp return got_error_from_errno();
2713 ffd1d5e5 2018-06-23 stsp usage_log();
2715 ffd1d5e5 2018-06-23 stsp error = got_repo_open(&repo, repo_path);
2716 ffd1d5e5 2018-06-23 stsp free(repo_path);
2717 ffd1d5e5 2018-06-23 stsp if (error != NULL)
2718 ffd1d5e5 2018-06-23 stsp return error;
2720 ffd1d5e5 2018-06-23 stsp if (commit_id_arg == NULL) {
2721 ffd1d5e5 2018-06-23 stsp error = get_head_commit_id(&commit_id, repo);
2722 ffd1d5e5 2018-06-23 stsp if (error != NULL)
2723 ffd1d5e5 2018-06-23 stsp goto done;
2725 ffd1d5e5 2018-06-23 stsp struct got_object *obj;
2726 ffd1d5e5 2018-06-23 stsp error = got_object_open_by_id_str(&obj, repo, commit_id_arg);
2727 ffd1d5e5 2018-06-23 stsp if (error == NULL) {
2728 ffd1d5e5 2018-06-23 stsp commit_id = got_object_get_id(obj);
2729 ffd1d5e5 2018-06-23 stsp if (commit_id == NULL)
2730 ffd1d5e5 2018-06-23 stsp error = got_error_from_errno();
2733 ffd1d5e5 2018-06-23 stsp if (error != NULL)
2734 ffd1d5e5 2018-06-23 stsp goto done;
2736 ffd1d5e5 2018-06-23 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
2737 ffd1d5e5 2018-06-23 stsp if (error != NULL)
2738 ffd1d5e5 2018-06-23 stsp goto done;
2740 ffd1d5e5 2018-06-23 stsp error = got_object_open_as_tree(&tree, repo, commit->tree_id);
2741 ffd1d5e5 2018-06-23 stsp if (error != NULL)
2742 ffd1d5e5 2018-06-23 stsp goto done;
2744 b3665f43 2018-08-04 stsp view = view_open(0, 0, 0, 0, NULL, TOG_VIEW_TREE);
2745 5221c383 2018-08-01 stsp if (view == NULL) {
2746 5221c383 2018-08-01 stsp error = got_error_from_errno();
2747 5221c383 2018-08-01 stsp goto done;
2749 ad80ab7b 2018-08-04 stsp error = open_tree_view(view, tree, commit_id, repo);
2750 ad80ab7b 2018-08-04 stsp if (error)
2751 ad80ab7b 2018-08-04 stsp goto done;
2752 ad80ab7b 2018-08-04 stsp error = show_tree_view(view);
2753 ad80ab7b 2018-08-04 stsp close_tree_view(view);
2754 96a765a8 2018-08-04 stsp view_close(view);
2756 ffd1d5e5 2018-06-23 stsp free(commit_id);
2757 ffd1d5e5 2018-06-23 stsp if (commit)
2758 ffd1d5e5 2018-06-23 stsp got_object_commit_close(commit);
2760 ffd1d5e5 2018-06-23 stsp got_object_tree_close(tree);
2762 ffd1d5e5 2018-06-23 stsp got_repo_close(repo);
2763 ffd1d5e5 2018-06-23 stsp return error;
2765 5c5136c5 2018-05-20 stsp static void
2766 9f7d7167 2018-04-29 stsp init_curses(void)
2768 9f7d7167 2018-04-29 stsp initscr();
2772 9f7d7167 2018-04-29 stsp intrflush(stdscr, FALSE);
2773 9f7d7167 2018-04-29 stsp keypad(stdscr, TRUE);
2774 1f475ad8 2018-05-10 stsp curs_set(0);
2777 4ed7e80c 2018-05-20 stsp __dead static void
2778 9f7d7167 2018-04-29 stsp usage(void)
2782 c2301be8 2018-04-30 stsp fprintf(stderr, "usage: %s [-h] [command] [arg ...]\n\n"
2783 9f7d7167 2018-04-29 stsp "Available commands:\n", getprogname());
2784 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
2785 9f7d7167 2018-04-29 stsp struct tog_cmd *cmd = &tog_commands[i];
2786 c2301be8 2018-04-30 stsp fprintf(stderr, " %s: %s\n", cmd->name, cmd->descr);
2791 c2301be8 2018-04-30 stsp static char **
2792 c2301be8 2018-04-30 stsp make_argv(const char *arg0, const char *arg1)
2794 c2301be8 2018-04-30 stsp char **argv;
2795 c2301be8 2018-04-30 stsp int argc = (arg1 == NULL ? 1 : 2);
2797 c2301be8 2018-04-30 stsp argv = calloc(argc, sizeof(char *));
2798 c2301be8 2018-04-30 stsp if (argv == NULL)
2799 c2301be8 2018-04-30 stsp err(1, "calloc");
2800 c2301be8 2018-04-30 stsp argv[0] = strdup(arg0);
2801 c2301be8 2018-04-30 stsp if (argv[0] == NULL)
2802 c2301be8 2018-04-30 stsp err(1, "calloc");
2803 c2301be8 2018-04-30 stsp if (arg1) {
2804 c2301be8 2018-04-30 stsp argv[1] = strdup(arg1);
2805 c2301be8 2018-04-30 stsp if (argv[1] == NULL)
2806 c2301be8 2018-04-30 stsp err(1, "calloc");
2809 c2301be8 2018-04-30 stsp return argv;
2813 9f7d7167 2018-04-29 stsp main(int argc, char *argv[])
2815 9f7d7167 2018-04-29 stsp const struct got_error *error = NULL;
2816 9f7d7167 2018-04-29 stsp struct tog_cmd *cmd = NULL;
2817 9f7d7167 2018-04-29 stsp int ch, hflag = 0;
2818 c2301be8 2018-04-30 stsp char **cmd_argv = NULL;
2820 9f7d7167 2018-04-29 stsp setlocale(LC_ALL, "");
2822 9f7d7167 2018-04-29 stsp while ((ch = getopt(argc, argv, "h")) != -1) {
2823 9f7d7167 2018-04-29 stsp switch (ch) {
2825 9f7d7167 2018-04-29 stsp hflag = 1;
2829 9f7d7167 2018-04-29 stsp /* NOTREACHED */
2833 9f7d7167 2018-04-29 stsp argc -= optind;
2834 9f7d7167 2018-04-29 stsp argv += optind;
2835 9f7d7167 2018-04-29 stsp optind = 0;
2836 c2301be8 2018-04-30 stsp optreset = 1;
2838 c2301be8 2018-04-30 stsp if (argc == 0) {
2839 f29d3e89 2018-06-23 stsp if (hflag)
2841 c2301be8 2018-04-30 stsp /* Build an argument vector which runs a default command. */
2842 9f7d7167 2018-04-29 stsp cmd = &tog_commands[0];
2843 c2301be8 2018-04-30 stsp cmd_argv = make_argv(cmd->name, NULL);
2848 c2301be8 2018-04-30 stsp /* Did the user specific a command? */
2849 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
2850 c2301be8 2018-04-30 stsp if (strncmp(tog_commands[i].name, argv[0],
2851 9f7d7167 2018-04-29 stsp strlen(argv[0])) == 0) {
2852 9f7d7167 2018-04-29 stsp cmd = &tog_commands[i];
2853 9f7d7167 2018-04-29 stsp if (hflag)
2854 9f7d7167 2018-04-29 stsp tog_commands[i].cmd_usage();
2858 9f7d7167 2018-04-29 stsp if (cmd == NULL) {
2859 c2301be8 2018-04-30 stsp /* Did the user specify a repository? */
2860 c2301be8 2018-04-30 stsp char *repo_path = realpath(argv[0], NULL);
2861 c2301be8 2018-04-30 stsp if (repo_path) {
2862 c2301be8 2018-04-30 stsp struct got_repository *repo;
2863 c2301be8 2018-04-30 stsp error = got_repo_open(&repo, repo_path);
2864 c2301be8 2018-04-30 stsp if (error == NULL)
2865 c2301be8 2018-04-30 stsp got_repo_close(repo);
2867 ad7de8d9 2018-04-30 stsp error = got_error_from_errno();
2868 c2301be8 2018-04-30 stsp if (error) {
2869 f29d3e89 2018-06-23 stsp if (hflag) {
2870 f29d3e89 2018-06-23 stsp fprintf(stderr, "%s: '%s' is not a "
2871 f29d3e89 2018-06-23 stsp "known command\n", getprogname(),
2875 ad7de8d9 2018-04-30 stsp fprintf(stderr, "%s: '%s' is neither a known "
2876 ad7de8d9 2018-04-30 stsp "command nor a path to a repository\n",
2877 c2301be8 2018-04-30 stsp getprogname(), argv[0]);
2878 ad7de8d9 2018-04-30 stsp free(repo_path);
2881 c2301be8 2018-04-30 stsp cmd = &tog_commands[0];
2882 c2301be8 2018-04-30 stsp cmd_argv = make_argv(cmd->name, repo_path);
2884 c2301be8 2018-04-30 stsp free(repo_path);
2888 5c5136c5 2018-05-20 stsp init_curses();
2890 c2301be8 2018-04-30 stsp error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
2891 9f7d7167 2018-04-29 stsp if (error)
2892 9f7d7167 2018-04-29 stsp goto done;
2895 c2301be8 2018-04-30 stsp free(cmd_argv);
2896 9f7d7167 2018-04-29 stsp if (error)
2897 9f7d7167 2018-04-29 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);