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 {
93 a3404814 2018-09-02 stsp struct got_object_id *id1, *id2;
95 ad80ab7b 2018-08-04 stsp int first_displayed_line;
96 ad80ab7b 2018-08-04 stsp int last_displayed_line;
100 ba4f502b 2018-08-04 stsp struct commit_queue_entry {
101 ba4f502b 2018-08-04 stsp TAILQ_ENTRY(commit_queue_entry) entry;
102 ba4f502b 2018-08-04 stsp struct got_object_id *id;
103 ba4f502b 2018-08-04 stsp struct got_commit_object *commit;
105 ba4f502b 2018-08-04 stsp TAILQ_HEAD(commit_queue_head, commit_queue_entry);
106 ba4f502b 2018-08-04 stsp struct commit_queue {
107 ba4f502b 2018-08-04 stsp int ncommits;
108 ba4f502b 2018-08-04 stsp struct commit_queue_head head;
111 b01e7d3b 2018-08-04 stsp struct tog_log_view_state {
112 b01e7d3b 2018-08-04 stsp struct got_commit_graph *graph;
113 b01e7d3b 2018-08-04 stsp struct commit_queue commits;
114 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *first_displayed_entry;
115 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *last_displayed_entry;
116 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *selected_entry;
117 b01e7d3b 2018-08-04 stsp int selected;
118 b01e7d3b 2018-08-04 stsp char *in_repo_path;
119 b01e7d3b 2018-08-04 stsp struct got_repository *repo;
122 e9424729 2018-08-04 stsp struct tog_blame_cb_args {
123 e9424729 2018-08-04 stsp pthread_mutex_t *mutex;
124 e9424729 2018-08-04 stsp struct tog_blame_line *lines; /* one per line */
125 e9424729 2018-08-04 stsp int nlines;
127 e9424729 2018-08-04 stsp struct tog_view *view;
128 e9424729 2018-08-04 stsp struct got_object_id *commit_id;
130 e9424729 2018-08-04 stsp const char *path;
131 e9424729 2018-08-04 stsp int *first_displayed_line;
132 e9424729 2018-08-04 stsp int *last_displayed_line;
133 e9424729 2018-08-04 stsp int *selected_line;
138 e9424729 2018-08-04 stsp struct tog_blame_thread_args {
139 e9424729 2018-08-04 stsp const char *path;
140 e9424729 2018-08-04 stsp struct got_repository *repo;
141 e9424729 2018-08-04 stsp struct tog_blame_cb_args *cb_args;
142 e9424729 2018-08-04 stsp int *complete;
145 e9424729 2018-08-04 stsp struct tog_blame {
147 e9424729 2018-08-04 stsp size_t filesize;
148 e9424729 2018-08-04 stsp struct tog_blame_line *lines;
149 e9424729 2018-08-04 stsp size_t nlines;
150 e9424729 2018-08-04 stsp pthread_t thread;
151 e9424729 2018-08-04 stsp struct tog_blame_thread_args thread_args;
152 e9424729 2018-08-04 stsp struct tog_blame_cb_args cb_args;
153 e9424729 2018-08-04 stsp const char *path;
156 7cbe629d 2018-08-04 stsp struct tog_blame_view_state {
157 7cbe629d 2018-08-04 stsp int first_displayed_line;
158 7cbe629d 2018-08-04 stsp int last_displayed_line;
159 7cbe629d 2018-08-04 stsp int selected_line;
160 7cbe629d 2018-08-04 stsp int blame_complete;
163 7cbe629d 2018-08-04 stsp pthread_mutex_t mutex;
164 7cbe629d 2018-08-04 stsp struct got_object_id_queue blamed_commits;
165 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
166 e5a0f69f 2018-08-18 stsp char *path;
167 7cbe629d 2018-08-04 stsp struct got_repository *repo;
168 ad80ab7b 2018-08-04 stsp struct got_object_id *commit_id;
169 e9424729 2018-08-04 stsp struct tog_blame blame;
172 ad80ab7b 2018-08-04 stsp struct tog_parent_tree {
173 ad80ab7b 2018-08-04 stsp TAILQ_ENTRY(tog_parent_tree) entry;
174 ad80ab7b 2018-08-04 stsp struct got_tree_object *tree;
175 ad80ab7b 2018-08-04 stsp struct got_tree_entry *first_displayed_entry;
176 ad80ab7b 2018-08-04 stsp struct got_tree_entry *selected_entry;
177 ad80ab7b 2018-08-04 stsp int selected;
180 ad80ab7b 2018-08-04 stsp TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
182 ad80ab7b 2018-08-04 stsp struct tog_tree_view_state {
183 ad80ab7b 2018-08-04 stsp char *tree_label;
184 ad80ab7b 2018-08-04 stsp struct got_tree_object *root;
185 ad80ab7b 2018-08-04 stsp struct got_tree_object *tree;
186 ad80ab7b 2018-08-04 stsp const struct got_tree_entries *entries;
187 ad80ab7b 2018-08-04 stsp struct got_tree_entry *first_displayed_entry;
188 ad80ab7b 2018-08-04 stsp struct got_tree_entry *last_displayed_entry;
189 ad80ab7b 2018-08-04 stsp struct got_tree_entry *selected_entry;
190 ad80ab7b 2018-08-04 stsp int nentries, ndisplayed, selected, show_ids;
191 ad80ab7b 2018-08-04 stsp struct tog_parent_trees parents;
192 7cbe629d 2018-08-04 stsp struct got_object_id *commit_id;
193 ad80ab7b 2018-08-04 stsp struct got_repository *repo;
196 bcbd79e2 2018-08-19 stsp TAILQ_HEAD(tog_view_list_head, tog_view);
197 cc3c9aac 2018-08-01 stsp struct tog_view {
198 e5a0f69f 2018-08-18 stsp TAILQ_ENTRY(tog_view) entry;
199 26ed57b2 2018-05-19 stsp WINDOW *window;
200 26ed57b2 2018-05-19 stsp PANEL *panel;
201 97ddc146 2018-08-01 stsp int nlines, ncols, begin_y, begin_x;
202 f7d12f7e 2018-08-01 stsp int lines, cols; /* copies of LINES and COLS */
203 a3404814 2018-09-02 stsp int focussed;
204 6d0fee91 2018-08-01 stsp struct tog_view *parent;
205 bcbd79e2 2018-08-19 stsp struct tog_view *child;
207 5dc9f4bc 2018-08-04 stsp /* type-specific state */
208 d6b05b5b 2018-08-04 stsp enum tog_view_type type;
210 b01e7d3b 2018-08-04 stsp struct tog_diff_view_state diff;
211 b01e7d3b 2018-08-04 stsp struct tog_log_view_state log;
212 7cbe629d 2018-08-04 stsp struct tog_blame_view_state blame;
213 ad80ab7b 2018-08-04 stsp struct tog_tree_view_state tree;
216 e5a0f69f 2018-08-18 stsp const struct got_error *(*show)(struct tog_view *);
217 e5a0f69f 2018-08-18 stsp const struct got_error *(*input)(struct tog_view **,
218 e5a0f69f 2018-08-18 stsp struct tog_view **, struct tog_view *, int);
219 bcbd79e2 2018-08-19 stsp const struct got_error *(*set_child)(struct tog_view *,
220 bcbd79e2 2018-08-19 stsp struct tog_view *);
221 e5a0f69f 2018-08-18 stsp const struct got_error *(*close)(struct tog_view *);
224 ba4f502b 2018-08-04 stsp static const struct got_error *open_diff_view(struct tog_view *,
225 ba4f502b 2018-08-04 stsp struct got_object *, struct got_object *, struct got_repository *);
226 5dc9f4bc 2018-08-04 stsp static const struct got_error *show_diff_view(struct tog_view *);
227 e5a0f69f 2018-08-18 stsp static const struct got_error *input_diff_view(struct tog_view **,
228 e5a0f69f 2018-08-18 stsp struct tog_view **, struct tog_view *, int);
229 e5a0f69f 2018-08-18 stsp static const struct got_error* close_diff_view(struct tog_view *);
231 ba4f502b 2018-08-04 stsp static const struct got_error *open_log_view(struct tog_view *,
232 ba4f502b 2018-08-04 stsp struct got_object_id *, struct got_repository *, const char *);
233 ba4f502b 2018-08-04 stsp static const struct got_error * show_log_view(struct tog_view *);
234 e5a0f69f 2018-08-18 stsp static const struct got_error *input_log_view(struct tog_view **,
235 e5a0f69f 2018-08-18 stsp struct tog_view **, struct tog_view *, int);
236 e5a0f69f 2018-08-18 stsp static const struct got_error *close_log_view(struct tog_view *);
237 bcbd79e2 2018-08-19 stsp static const struct got_error* set_child_log_view(struct tog_view *,
238 bcbd79e2 2018-08-19 stsp struct tog_view *);
240 e5a0f69f 2018-08-18 stsp static const struct got_error *open_blame_view(struct tog_view *, char *,
241 5221c383 2018-08-01 stsp struct got_object_id *, struct got_repository *);
242 7cbe629d 2018-08-04 stsp static const struct got_error *show_blame_view(struct tog_view *);
243 e5a0f69f 2018-08-18 stsp static const struct got_error *input_blame_view(struct tog_view **,
244 e5a0f69f 2018-08-18 stsp struct tog_view **, struct tog_view *, int);
245 e5a0f69f 2018-08-18 stsp static const struct got_error *close_blame_view(struct tog_view *);
247 ad80ab7b 2018-08-04 stsp static const struct got_error *open_tree_view(struct tog_view *,
248 4fc679ca 2018-08-04 stsp struct got_tree_object *, struct got_object_id *, struct got_repository *);
249 ad80ab7b 2018-08-04 stsp static const struct got_error *show_tree_view(struct tog_view *);
250 e5a0f69f 2018-08-18 stsp static const struct got_error *input_tree_view(struct tog_view **,
251 e5a0f69f 2018-08-18 stsp struct tog_view **, struct tog_view *, int);
252 e5a0f69f 2018-08-18 stsp static const struct got_error *close_tree_view(struct tog_view *);
254 e5a0f69f 2018-08-18 stsp static const struct got_error *
255 96a765a8 2018-08-04 stsp view_close(struct tog_view *view)
257 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
259 bcbd79e2 2018-08-19 stsp if (view->child)
260 bcbd79e2 2018-08-19 stsp view->child->parent = NULL;
261 bcbd79e2 2018-08-19 stsp if (view->parent)
262 bcbd79e2 2018-08-19 stsp view->parent->child = NULL;
263 e5a0f69f 2018-08-18 stsp if (view->close)
264 e5a0f69f 2018-08-18 stsp err = view->close(view);
265 ea5e7bb5 2018-08-01 stsp if (view->panel)
266 ea5e7bb5 2018-08-01 stsp del_panel(view->panel);
267 ea5e7bb5 2018-08-01 stsp if (view->window)
268 ea5e7bb5 2018-08-01 stsp delwin(view->window);
269 ea5e7bb5 2018-08-01 stsp free(view);
270 e5a0f69f 2018-08-18 stsp return err;
273 ea5e7bb5 2018-08-01 stsp static struct tog_view *
274 b3665f43 2018-08-04 stsp view_open(int nlines, int ncols, int begin_y, int begin_x,
275 d6b05b5b 2018-08-04 stsp struct tog_view *parent, enum tog_view_type type)
277 ad80ab7b 2018-08-04 stsp struct tog_view *view = calloc(1, sizeof(*view));
279 ea5e7bb5 2018-08-01 stsp if (view == NULL)
280 ea5e7bb5 2018-08-01 stsp return NULL;
282 dd04dd3f 2018-08-19 stsp if (begin_x == 0 && parent && parent->ncols - 80 > 10)
283 dd04dd3f 2018-08-19 stsp begin_x = parent->ncols - 80;
285 6d0fee91 2018-08-01 stsp view->parent = parent;
286 bcbd79e2 2018-08-19 stsp if (parent)
287 bcbd79e2 2018-08-19 stsp parent->child = view;
288 d6b05b5b 2018-08-04 stsp view->type = type;
289 f7d12f7e 2018-08-01 stsp view->lines = LINES;
290 f7d12f7e 2018-08-01 stsp view->cols = COLS;
291 207b9029 2018-08-01 stsp view->nlines = nlines ? nlines : LINES - begin_y;
292 207b9029 2018-08-01 stsp view->ncols = ncols ? ncols : COLS - begin_x;
293 97ddc146 2018-08-01 stsp view->begin_y = begin_y;
294 97ddc146 2018-08-01 stsp view->begin_x = begin_x;
295 842167bf 2018-08-01 stsp view->window = newwin(nlines, ncols, begin_y, begin_x);
296 ea5e7bb5 2018-08-01 stsp if (view->window == NULL) {
297 96a765a8 2018-08-04 stsp view_close(view);
298 ea5e7bb5 2018-08-01 stsp return NULL;
300 ea5e7bb5 2018-08-01 stsp view->panel = new_panel(view->window);
301 ea5e7bb5 2018-08-01 stsp if (view->panel == NULL) {
302 96a765a8 2018-08-04 stsp view_close(view);
303 ea5e7bb5 2018-08-01 stsp return NULL;
306 ea5e7bb5 2018-08-01 stsp keypad(view->window, TRUE);
307 ea5e7bb5 2018-08-01 stsp return view;
310 4d8c2215 2018-08-19 stsp static const struct got_error *
311 6acd1bd1 2018-08-04 stsp view_show(struct tog_view *view)
313 e5a0f69f 2018-08-18 stsp const struct got_error *err;
315 bcbd79e2 2018-08-19 stsp if (view->parent) {
316 bcbd79e2 2018-08-19 stsp err = view->parent->show(view->parent);
318 bcbd79e2 2018-08-19 stsp return err;
319 bcbd79e2 2018-08-19 stsp show_panel(view->parent->panel);
322 e5a0f69f 2018-08-18 stsp err = view->show(view);
324 e5a0f69f 2018-08-18 stsp return err;
325 cdf1ee82 2018-08-01 stsp show_panel(view->panel);
327 a3404814 2018-09-02 stsp if (view->child && view->child->begin_x > view->begin_x) {
328 bcbd79e2 2018-08-19 stsp err = view->child->show(view->child);
330 bcbd79e2 2018-08-19 stsp return err;
331 bcbd79e2 2018-08-19 stsp show_panel(view->child->panel);
334 cdf1ee82 2018-08-01 stsp update_panels();
335 e5a0f69f 2018-08-18 stsp doupdate();
337 e5a0f69f 2018-08-18 stsp return err;
340 4d8c2215 2018-08-19 stsp static const struct got_error *
341 f7d12f7e 2018-08-01 stsp view_resize(struct tog_view *view)
343 f7d12f7e 2018-08-01 stsp int nlines, ncols;
345 6d0fee91 2018-08-01 stsp while (view) {
346 6d0fee91 2018-08-01 stsp if (view->lines > LINES)
347 6d0fee91 2018-08-01 stsp nlines = view->nlines - (view->lines - LINES);
349 6d0fee91 2018-08-01 stsp nlines = view->nlines + (LINES - view->lines);
351 6d0fee91 2018-08-01 stsp if (view->cols > COLS)
352 6d0fee91 2018-08-01 stsp ncols = view->ncols - (view->cols - COLS);
354 6d0fee91 2018-08-01 stsp ncols = view->ncols + (COLS - view->cols);
356 6d0fee91 2018-08-01 stsp if (wresize(view->window, nlines, ncols) == ERR)
357 6d0fee91 2018-08-01 stsp return got_error_from_errno();
358 77a567dd 2018-08-01 stsp replace_panel(view->panel, view->window);
360 6d0fee91 2018-08-01 stsp view->nlines = nlines;
361 6d0fee91 2018-08-01 stsp view->ncols = ncols;
362 6d0fee91 2018-08-01 stsp view->lines = LINES;
363 6d0fee91 2018-08-01 stsp view->cols = COLS;
365 6d0fee91 2018-08-01 stsp view = view->parent;
368 f7d12f7e 2018-08-01 stsp return NULL;
371 e5a0f69f 2018-08-18 stsp static const struct got_error *
372 48fcc512 2018-08-18 stsp view_input(struct tog_view **new, struct tog_view **dead,
373 e4197bf9 2018-08-18 stsp struct tog_view **focus, int *done, struct tog_view *view,
374 48fcc512 2018-08-18 stsp struct tog_view_list_head *views)
376 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
377 31607d6c 2018-08-18 stsp struct tog_view *next, *prev;
380 e5a0f69f 2018-08-18 stsp *new = NULL;
381 e5a0f69f 2018-08-18 stsp *dead = NULL;
383 e5a0f69f 2018-08-18 stsp nodelay(stdscr, FALSE);
384 e5a0f69f 2018-08-18 stsp ch = wgetch(view->window);
385 e5a0f69f 2018-08-18 stsp nodelay(stdscr, TRUE);
386 e5a0f69f 2018-08-18 stsp switch (ch) {
390 48fcc512 2018-08-18 stsp next = TAILQ_NEXT(view, entry);
392 48fcc512 2018-08-18 stsp *focus = next;
394 48fcc512 2018-08-18 stsp *focus = TAILQ_FIRST(views);
395 a3404814 2018-09-02 stsp view->focussed = 0;
396 a3404814 2018-09-02 stsp (*focus)->focussed = 1;
399 31607d6c 2018-08-18 stsp prev = TAILQ_PREV(view, tog_view_list_head, entry);
401 31607d6c 2018-08-18 stsp *focus = prev;
403 31607d6c 2018-08-18 stsp *focus = TAILQ_LAST(views, tog_view_list_head);
404 786292d9 2018-09-02 stsp view->focussed = 0;
405 786292d9 2018-09-02 stsp (*focus)->focussed = 1;
408 e5a0f69f 2018-08-18 stsp err = view->input(new, dead, view, ch);
409 e5a0f69f 2018-08-18 stsp *dead = view;
414 e5a0f69f 2018-08-18 stsp case KEY_RESIZE:
415 e5a0f69f 2018-08-18 stsp err = view_resize(view);
417 e5a0f69f 2018-08-18 stsp return err;
418 e5a0f69f 2018-08-18 stsp err = view->input(new, dead, view, ch);
421 e5a0f69f 2018-08-18 stsp err = view->input(new, dead, view, ch);
425 e5a0f69f 2018-08-18 stsp return err;
428 e5a0f69f 2018-08-18 stsp static const struct got_error *
429 bcbd79e2 2018-08-19 stsp view_set_child(struct tog_view *view, struct tog_view *child)
431 bcbd79e2 2018-08-19 stsp const struct got_error *err;
433 bcbd79e2 2018-08-19 stsp if (view->set_child) {
434 bcbd79e2 2018-08-19 stsp err = view->set_child(view, child);
436 bcbd79e2 2018-08-19 stsp return err;
439 bcbd79e2 2018-08-19 stsp view->child = child;
440 bcbd79e2 2018-08-19 stsp return NULL;
444 1a57306a 2018-09-02 stsp view_vborder(struct tog_view *view)
446 1a57306a 2018-09-02 stsp if (view->child == NULL)
449 1a57306a 2018-09-02 stsp mvwvline(view->window, view->begin_y, view->child->begin_x - 1,
450 1a57306a 2018-09-02 stsp got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
454 a3404814 2018-09-02 stsp view_needs_focus_indication(struct tog_view *view)
456 a3404814 2018-09-02 stsp if (!view->focussed)
459 a3404814 2018-09-02 stsp if (view->child && view->child->begin_x > view->begin_x)
462 a3404814 2018-09-02 stsp if (view->parent && view->begin_x > view->parent->begin_x)
468 bcbd79e2 2018-08-19 stsp static const struct got_error *
469 e5a0f69f 2018-08-18 stsp view_loop(struct tog_view *view)
471 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
472 e5a0f69f 2018-08-18 stsp struct tog_view_list_head views;
473 e5a0f69f 2018-08-18 stsp struct tog_view *new_view, *dead_view;
474 e4197bf9 2018-08-18 stsp int done = 0;
476 e5a0f69f 2018-08-18 stsp TAILQ_INIT(&views);
477 e5a0f69f 2018-08-18 stsp TAILQ_INSERT_HEAD(&views, view, entry);
479 a3404814 2018-09-02 stsp view->focussed = 1;
480 e4197bf9 2018-08-18 stsp while (!TAILQ_EMPTY(&views) && !done) {
481 e5a0f69f 2018-08-18 stsp err = view_show(view);
484 e4197bf9 2018-08-18 stsp err = view_input(&new_view, &dead_view, &view, &done,
485 e4197bf9 2018-08-18 stsp view, &views);
488 e5a0f69f 2018-08-18 stsp if (dead_view) {
489 89864818 2018-09-02 stsp struct tog_view *v, *t;
490 e5a0f69f 2018-08-18 stsp TAILQ_REMOVE(&views, dead_view, entry);
491 89864818 2018-09-02 stsp TAILQ_FOREACH_SAFE(v, &views, entry, t) {
492 89864818 2018-09-02 stsp if (v->parent == dead_view) {
493 89864818 2018-09-02 stsp TAILQ_REMOVE(&views, v, entry);
494 7e1e2c14 2018-09-02 stsp err = view_close(v);
499 e5a0f69f 2018-08-18 stsp if (dead_view->parent)
500 e5a0f69f 2018-08-18 stsp view = dead_view->parent;
502 e5a0f69f 2018-08-18 stsp view = TAILQ_LAST(&views, tog_view_list_head);
504 a3404814 2018-09-02 stsp view->focussed = 1;
505 e5a0f69f 2018-08-18 stsp err = view_close(dead_view);
509 bcbd79e2 2018-08-19 stsp if (new_view) {
510 ae73d513 2018-09-24 stsp struct tog_view *v, *t;
511 a3404814 2018-09-02 stsp view->focussed = 0;
512 ae73d513 2018-09-24 stsp /* Only allow one view per type. */
513 ae73d513 2018-09-24 stsp TAILQ_FOREACH_SAFE(v, &views, entry, t) {
514 ae73d513 2018-09-24 stsp if (v->type != new_view->type)
516 ae73d513 2018-09-24 stsp TAILQ_REMOVE(&views, v, entry);
517 ae73d513 2018-09-24 stsp err = view_close(v);
521 bcbd79e2 2018-08-19 stsp TAILQ_INSERT_TAIL(&views, new_view, entry);
522 bcbd79e2 2018-08-19 stsp if (new_view->parent) {
523 bcbd79e2 2018-08-19 stsp err = view_set_child(new_view->parent, new_view);
526 a3404814 2018-09-02 stsp new_view->parent->focussed = 0;
528 bcbd79e2 2018-08-19 stsp view = new_view;
529 a3404814 2018-09-02 stsp view->focussed = 1;
533 e5a0f69f 2018-08-18 stsp while (!TAILQ_EMPTY(&views)) {
534 e5a0f69f 2018-08-18 stsp view = TAILQ_FIRST(&views);
535 e5a0f69f 2018-08-18 stsp TAILQ_REMOVE(&views, view, entry);
536 e5a0f69f 2018-08-18 stsp view_close(view);
538 e5a0f69f 2018-08-18 stsp return err;
541 4ed7e80c 2018-05-20 stsp __dead static void
542 9f7d7167 2018-04-29 stsp usage_log(void)
545 c70c5802 2018-08-01 stsp fprintf(stderr,
546 c70c5802 2018-08-01 stsp "usage: %s log [-c commit] [-r repository-path] [path]\n",
547 9f7d7167 2018-04-29 stsp getprogname());
551 963b370f 2018-05-20 stsp /* Create newly allocated wide-character string equivalent to a byte string. */
552 80ddbec8 2018-04-29 stsp static const struct got_error *
553 963b370f 2018-05-20 stsp mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
555 00dfcb92 2018-06-11 stsp char *vis = NULL;
556 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
558 963b370f 2018-05-20 stsp *ws = NULL;
559 963b370f 2018-05-20 stsp *wlen = mbstowcs(NULL, s, 0);
560 00dfcb92 2018-06-11 stsp if (*wlen == (size_t)-1) {
561 00dfcb92 2018-06-11 stsp int vislen;
562 00dfcb92 2018-06-11 stsp if (errno != EILSEQ)
563 00dfcb92 2018-06-11 stsp return got_error_from_errno();
565 00dfcb92 2018-06-11 stsp /* byte string invalid in current encoding; try to "fix" it */
566 00dfcb92 2018-06-11 stsp err = got_mbsavis(&vis, &vislen, s);
568 00dfcb92 2018-06-11 stsp return err;
569 00dfcb92 2018-06-11 stsp *wlen = mbstowcs(NULL, vis, 0);
570 a7f50699 2018-06-11 stsp if (*wlen == (size_t)-1) {
571 a7f50699 2018-06-11 stsp err = got_error_from_errno(); /* give up */
576 963b370f 2018-05-20 stsp *ws = calloc(*wlen + 1, sizeof(*ws));
577 a7f50699 2018-06-11 stsp if (*ws == NULL) {
578 a7f50699 2018-06-11 stsp err = got_error_from_errno();
582 00dfcb92 2018-06-11 stsp if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
583 963b370f 2018-05-20 stsp err = got_error_from_errno();
588 963b370f 2018-05-20 stsp *ws = NULL;
591 963b370f 2018-05-20 stsp return err;
594 963b370f 2018-05-20 stsp /* Format a line for display, ensuring that it won't overflow a width limit. */
595 963b370f 2018-05-20 stsp static const struct got_error *
596 ffd1d5e5 2018-06-23 stsp format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit)
598 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
599 963b370f 2018-05-20 stsp int cols = 0;
600 963b370f 2018-05-20 stsp wchar_t *wline = NULL;
601 963b370f 2018-05-20 stsp size_t wlen;
604 963b370f 2018-05-20 stsp *wlinep = NULL;
605 b700b5d6 2018-07-10 stsp *widthp = 0;
607 963b370f 2018-05-20 stsp err = mbs2ws(&wline, &wlen, line);
609 963b370f 2018-05-20 stsp return err;
612 b700b5d6 2018-07-10 stsp while (i < wlen && cols < wlimit) {
613 963b370f 2018-05-20 stsp int width = wcwidth(wline[i]);
614 963b370f 2018-05-20 stsp switch (width) {
620 3c1f04f1 2018-09-13 stsp if (cols + width <= wlimit)
621 b700b5d6 2018-07-10 stsp cols += width;
625 963b370f 2018-05-20 stsp if (wline[i] == L'\t')
626 b700b5d6 2018-07-10 stsp cols += TABSIZE - ((cols + 1) % TABSIZE);
630 963b370f 2018-05-20 stsp err = got_error_from_errno();
634 963b370f 2018-05-20 stsp wline[i] = L'\0';
635 b700b5d6 2018-07-10 stsp if (widthp)
636 b700b5d6 2018-07-10 stsp *widthp = cols;
639 963b370f 2018-05-20 stsp free(wline);
641 963b370f 2018-05-20 stsp *wlinep = wline;
642 963b370f 2018-05-20 stsp return err;
645 963b370f 2018-05-20 stsp static const struct got_error *
646 2814baeb 2018-08-01 stsp draw_commit(struct tog_view *view, struct got_commit_object *commit,
647 2814baeb 2018-08-01 stsp struct got_object_id *id)
649 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
650 b39d25c7 2018-07-10 stsp char datebuf[10]; /* YY-MM-DD + SPACE + NUL */
651 80ddbec8 2018-04-29 stsp char *logmsg0 = NULL, *logmsg = NULL;
652 6d9fbc00 2018-04-29 stsp char *author0 = NULL, *author = NULL;
653 bb737323 2018-05-20 stsp wchar_t *wlogmsg = NULL, *wauthor = NULL;
654 bb737323 2018-05-20 stsp int author_width, logmsg_width;
655 6d9fbc00 2018-04-29 stsp char *newline, *smallerthan;
656 80ddbec8 2018-04-29 stsp char *line = NULL;
657 bb737323 2018-05-20 stsp int col, limit;
658 b39d25c7 2018-07-10 stsp static const size_t date_display_cols = 9;
659 bb737323 2018-05-20 stsp static const size_t author_display_cols = 16;
660 f7d12f7e 2018-08-01 stsp const int avail = view->ncols;
662 c70c5802 2018-08-01 stsp if (strftime(datebuf, sizeof(datebuf), "%g/%m/%d ",
663 c70c5802 2018-08-01 stsp &commit->tm_committer) >= sizeof(datebuf))
664 b39d25c7 2018-07-10 stsp return got_error(GOT_ERR_NO_SPACE);
666 b39d25c7 2018-07-10 stsp if (avail < date_display_cols)
667 b39d25c7 2018-07-10 stsp limit = MIN(sizeof(datebuf) - 1, avail);
669 b39d25c7 2018-07-10 stsp limit = MIN(date_display_cols, sizeof(datebuf) - 1);
670 2814baeb 2018-08-01 stsp waddnstr(view->window, datebuf, limit);
671 b39d25c7 2018-07-10 stsp col = limit + 1;
672 b39d25c7 2018-07-10 stsp if (col > avail)
675 6d9fbc00 2018-04-29 stsp author0 = strdup(commit->author);
676 6d9fbc00 2018-04-29 stsp if (author0 == NULL) {
677 80ddbec8 2018-04-29 stsp err = got_error_from_errno();
680 6d9fbc00 2018-04-29 stsp author = author0;
681 6d9fbc00 2018-04-29 stsp smallerthan = strchr(author, '<');
682 6d9fbc00 2018-04-29 stsp if (smallerthan)
683 6d9fbc00 2018-04-29 stsp *smallerthan = '\0';
685 6d9fbc00 2018-04-29 stsp char *at = strchr(author, '@');
687 6d9fbc00 2018-04-29 stsp *at = '\0';
689 b39d25c7 2018-07-10 stsp limit = avail - col;
690 bb737323 2018-05-20 stsp err = format_line(&wauthor, &author_width, author, limit);
693 2814baeb 2018-08-01 stsp waddwstr(view->window, wauthor);
694 bb737323 2018-05-20 stsp col += author_width;
695 9c2eaf34 2018-05-20 stsp while (col <= avail && author_width < author_display_cols + 1) {
696 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
698 bb737323 2018-05-20 stsp author_width++;
700 9c2eaf34 2018-05-20 stsp if (col > avail)
703 bb737323 2018-05-20 stsp logmsg0 = strdup(commit->logmsg);
704 bb737323 2018-05-20 stsp if (logmsg0 == NULL) {
705 6d9fbc00 2018-04-29 stsp err = got_error_from_errno();
708 bb737323 2018-05-20 stsp logmsg = logmsg0;
709 bb737323 2018-05-20 stsp while (*logmsg == '\n')
711 bb737323 2018-05-20 stsp newline = strchr(logmsg, '\n');
712 bb737323 2018-05-20 stsp if (newline)
713 bb737323 2018-05-20 stsp *newline = '\0';
714 bb737323 2018-05-20 stsp limit = avail - col;
715 bb737323 2018-05-20 stsp err = format_line(&wlogmsg, &logmsg_width, logmsg, limit);
718 2814baeb 2018-08-01 stsp waddwstr(view->window, wlogmsg);
719 bb737323 2018-05-20 stsp col += logmsg_width;
720 bb737323 2018-05-20 stsp while (col <= avail) {
721 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
725 80ddbec8 2018-04-29 stsp free(logmsg0);
726 bb737323 2018-05-20 stsp free(wlogmsg);
727 6d9fbc00 2018-04-29 stsp free(author0);
728 bb737323 2018-05-20 stsp free(wauthor);
729 80ddbec8 2018-04-29 stsp free(line);
730 80ddbec8 2018-04-29 stsp return err;
733 899d86c2 2018-05-10 stsp static struct commit_queue_entry *
734 899d86c2 2018-05-10 stsp alloc_commit_queue_entry(struct got_commit_object *commit,
735 899d86c2 2018-05-10 stsp struct got_object_id *id)
737 80ddbec8 2018-04-29 stsp struct commit_queue_entry *entry;
739 80ddbec8 2018-04-29 stsp entry = calloc(1, sizeof(*entry));
740 80ddbec8 2018-04-29 stsp if (entry == NULL)
741 899d86c2 2018-05-10 stsp return NULL;
743 899d86c2 2018-05-10 stsp entry->id = id;
744 99db9666 2018-05-07 stsp entry->commit = commit;
745 899d86c2 2018-05-10 stsp return entry;
748 99db9666 2018-05-07 stsp static void
749 99db9666 2018-05-07 stsp pop_commit(struct commit_queue *commits)
751 99db9666 2018-05-07 stsp struct commit_queue_entry *entry;
753 ecb28ae0 2018-07-16 stsp entry = TAILQ_FIRST(&commits->head);
754 ecb28ae0 2018-07-16 stsp TAILQ_REMOVE(&commits->head, entry, entry);
755 99db9666 2018-05-07 stsp got_object_commit_close(entry->commit);
756 ecb28ae0 2018-07-16 stsp commits->ncommits--;
757 9ba79e04 2018-06-11 stsp /* Don't free entry->id! It is owned by the commit graph. */
758 99db9666 2018-05-07 stsp free(entry);
761 99db9666 2018-05-07 stsp static void
762 99db9666 2018-05-07 stsp free_commits(struct commit_queue *commits)
764 ecb28ae0 2018-07-16 stsp while (!TAILQ_EMPTY(&commits->head))
765 99db9666 2018-05-07 stsp pop_commit(commits);
768 c4972b91 2018-05-07 stsp static const struct got_error *
769 9ba79e04 2018-06-11 stsp queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
770 93e45b7c 2018-09-24 stsp struct got_object_id *start_id, int minqueue,
771 ecb28ae0 2018-07-16 stsp struct got_repository *repo, const char *path)
773 899d86c2 2018-05-10 stsp const struct got_error *err = NULL;
774 93e45b7c 2018-09-24 stsp int nqueued = 0;
776 93e45b7c 2018-09-24 stsp if (start_id) {
777 93e45b7c 2018-09-24 stsp err = got_commit_graph_iter_start(graph, start_id, repo);
779 9ba79e04 2018-06-11 stsp return err;
782 93e45b7c 2018-09-24 stsp while (nqueued < minqueue) {
783 93e45b7c 2018-09-24 stsp struct got_object_id *id;
784 9ba79e04 2018-06-11 stsp struct got_commit_object *commit;
785 93e45b7c 2018-09-24 stsp struct commit_queue_entry *entry;
787 9ba79e04 2018-06-11 stsp err = got_commit_graph_iter_next(&id, graph);
789 93e45b7c 2018-09-24 stsp if (err->code == GOT_ERR_ITER_COMPLETED) {
790 9ba79e04 2018-06-11 stsp err = NULL;
793 93e45b7c 2018-09-24 stsp if (err->code != GOT_ERR_ITER_NEED_MORE)
795 93e45b7c 2018-09-24 stsp err = got_commit_graph_fetch_commits(graph,
796 93e45b7c 2018-09-24 stsp minqueue, repo);
798 ecb28ae0 2018-07-16 stsp return err;
802 ecb28ae0 2018-07-16 stsp if (id == NULL)
805 9ba79e04 2018-06-11 stsp err = got_object_open_as_commit(&commit, repo, id);
808 9ba79e04 2018-06-11 stsp entry = alloc_commit_queue_entry(commit, id);
809 9ba79e04 2018-06-11 stsp if (entry == NULL) {
810 9ba79e04 2018-06-11 stsp err = got_error_from_errno();
814 ecb28ae0 2018-07-16 stsp TAILQ_INSERT_TAIL(&commits->head, entry, entry);
816 ecb28ae0 2018-07-16 stsp commits->ncommits++;
819 9ba79e04 2018-06-11 stsp return err;
822 99db9666 2018-05-07 stsp static const struct got_error *
823 9ba79e04 2018-06-11 stsp fetch_next_commit(struct commit_queue_entry **pentry,
824 9ba79e04 2018-06-11 stsp struct commit_queue_entry *entry, struct commit_queue *commits,
825 ecb28ae0 2018-07-16 stsp struct got_commit_graph *graph, struct got_repository *repo,
826 ecb28ae0 2018-07-16 stsp const char *path)
828 899d86c2 2018-05-10 stsp const struct got_error *err = NULL;
830 9ba79e04 2018-06-11 stsp *pentry = NULL;
832 93e45b7c 2018-09-24 stsp err = queue_commits(graph, commits, NULL, 1, repo, path);
834 9ba79e04 2018-06-11 stsp return err;
836 9ba79e04 2018-06-11 stsp /* Next entry to display should now be available. */
837 9ba79e04 2018-06-11 stsp *pentry = TAILQ_NEXT(entry, entry);
838 9ba79e04 2018-06-11 stsp return NULL;
841 899d86c2 2018-05-10 stsp static const struct got_error *
842 9ba79e04 2018-06-11 stsp get_head_commit_id(struct got_object_id **head_id, struct got_repository *repo)
844 9ba79e04 2018-06-11 stsp const struct got_error *err = NULL;
845 9ba79e04 2018-06-11 stsp struct got_reference *head_ref;
847 9ba79e04 2018-06-11 stsp *head_id = NULL;
849 9ba79e04 2018-06-11 stsp err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
851 99db9666 2018-05-07 stsp return err;
853 9ba79e04 2018-06-11 stsp err = got_ref_resolve(head_id, repo, head_ref);
854 9ba79e04 2018-06-11 stsp got_ref_close(head_ref);
856 9ba79e04 2018-06-11 stsp *head_id = NULL;
857 99db9666 2018-05-07 stsp return err;
860 9ba79e04 2018-06-11 stsp return NULL;
863 0553a4e3 2018-04-30 stsp static const struct got_error *
864 2814baeb 2018-08-01 stsp draw_commits(struct tog_view *view, struct commit_queue_entry **last,
865 ecb28ae0 2018-07-16 stsp struct commit_queue_entry **selected, struct commit_queue_entry *first,
866 a7f40148 2018-07-18 stsp struct commit_queue *commits, int selected_idx, int limit,
867 a7f40148 2018-07-18 stsp struct got_commit_graph *graph, struct got_repository *repo,
868 a7f40148 2018-07-18 stsp const char *path)
870 0553a4e3 2018-04-30 stsp const struct got_error *err = NULL;
871 0553a4e3 2018-04-30 stsp struct commit_queue_entry *entry;
872 ecb28ae0 2018-07-16 stsp int ncommits, width;
873 867c6645 2018-07-10 stsp char *id_str, *header;
874 ecb28ae0 2018-07-16 stsp wchar_t *wline;
876 e0d42f60 2018-07-22 stsp entry = first;
877 e0d42f60 2018-07-22 stsp ncommits = 0;
878 e0d42f60 2018-07-22 stsp while (entry) {
879 e0d42f60 2018-07-22 stsp if (ncommits == selected_idx) {
880 e0d42f60 2018-07-22 stsp *selected = entry;
883 e0d42f60 2018-07-22 stsp entry = TAILQ_NEXT(entry, entry);
884 e0d42f60 2018-07-22 stsp ncommits++;
887 867c6645 2018-07-10 stsp err = got_object_id_str(&id_str, (*selected)->id);
889 867c6645 2018-07-10 stsp return err;
891 c3ba6f36 2018-08-01 stsp if (path && strcmp(path, "/") != 0) {
892 ecb28ae0 2018-07-16 stsp if (asprintf(&header, "commit: %s [%s]", id_str, path) == -1) {
893 ecb28ae0 2018-07-16 stsp err = got_error_from_errno();
894 ecb28ae0 2018-07-16 stsp free(id_str);
895 ecb28ae0 2018-07-16 stsp return err;
897 ecb28ae0 2018-07-16 stsp } else if (asprintf(&header, "commit: %s", id_str) == -1) {
898 867c6645 2018-07-10 stsp err = got_error_from_errno();
899 867c6645 2018-07-10 stsp free(id_str);
900 867c6645 2018-07-10 stsp return err;
902 ecb28ae0 2018-07-16 stsp free(id_str);
903 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, header, view->ncols);
905 ecb28ae0 2018-07-16 stsp free(header);
906 ecb28ae0 2018-07-16 stsp return err;
908 ecb28ae0 2018-07-16 stsp free(header);
910 2814baeb 2018-08-01 stsp werase(view->window);
912 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
913 a3404814 2018-09-02 stsp wstandout(view->window);
914 2814baeb 2018-08-01 stsp waddwstr(view->window, wline);
915 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
916 a3404814 2018-09-02 stsp wstandend(view->window);
917 f7d12f7e 2018-08-01 stsp if (width < view->ncols)
918 2814baeb 2018-08-01 stsp waddch(view->window, '\n');
919 ecb28ae0 2018-07-16 stsp free(wline);
920 ecb28ae0 2018-07-16 stsp if (limit <= 1)
921 ecb28ae0 2018-07-16 stsp return NULL;
923 899d86c2 2018-05-10 stsp entry = first;
924 899d86c2 2018-05-10 stsp *last = first;
925 867c6645 2018-07-10 stsp ncommits = 0;
926 899d86c2 2018-05-10 stsp while (entry) {
927 ecb28ae0 2018-07-16 stsp if (ncommits >= limit - 1)
929 e0d42f60 2018-07-22 stsp if (ncommits == selected_idx)
930 2814baeb 2018-08-01 stsp wstandout(view->window);
931 2814baeb 2018-08-01 stsp err = draw_commit(view, entry->commit, entry->id);
932 cd0acaa7 2018-05-20 stsp if (ncommits == selected_idx)
933 2814baeb 2018-08-01 stsp wstandend(view->window);
936 0553a4e3 2018-04-30 stsp ncommits++;
937 899d86c2 2018-05-10 stsp *last = entry;
938 a7f40148 2018-07-18 stsp if (entry == TAILQ_LAST(&commits->head, commit_queue_head)) {
939 93e45b7c 2018-09-24 stsp err = queue_commits(graph, commits, NULL, 1,
940 93e45b7c 2018-09-24 stsp repo, path);
942 a7f40148 2018-07-18 stsp if (err->code != GOT_ERR_ITER_COMPLETED)
943 a7f40148 2018-07-18 stsp return err;
944 a7f40148 2018-07-18 stsp err = NULL;
947 899d86c2 2018-05-10 stsp entry = TAILQ_NEXT(entry, entry);
950 1a57306a 2018-09-02 stsp view_vborder(view);
952 80ddbec8 2018-04-29 stsp return err;
955 07b55e75 2018-05-10 stsp static void
956 16482c3b 2018-05-20 stsp scroll_up(struct commit_queue_entry **first_displayed_entry, int maxscroll,
957 07b55e75 2018-05-10 stsp struct commit_queue *commits)
959 07b55e75 2018-05-10 stsp struct commit_queue_entry *entry;
960 07b55e75 2018-05-10 stsp int nscrolled = 0;
962 ecb28ae0 2018-07-16 stsp entry = TAILQ_FIRST(&commits->head);
963 07b55e75 2018-05-10 stsp if (*first_displayed_entry == entry)
966 07b55e75 2018-05-10 stsp entry = *first_displayed_entry;
967 16482c3b 2018-05-20 stsp while (entry && nscrolled < maxscroll) {
968 ecb28ae0 2018-07-16 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
969 07b55e75 2018-05-10 stsp if (entry) {
970 07b55e75 2018-05-10 stsp *first_displayed_entry = entry;
971 07b55e75 2018-05-10 stsp nscrolled++;
976 aa075928 2018-05-10 stsp static const struct got_error *
977 16482c3b 2018-05-20 stsp scroll_down(struct commit_queue_entry **first_displayed_entry, int maxscroll,
978 93e45b7c 2018-09-24 stsp struct commit_queue_entry **last_displayed_entry,
979 9ba79e04 2018-06-11 stsp struct commit_queue *commits, struct got_commit_graph *graph,
980 ecb28ae0 2018-07-16 stsp struct got_repository *repo, const char *path)
982 aa075928 2018-05-10 stsp const struct got_error *err = NULL;
983 dd0a52c1 2018-05-20 stsp struct commit_queue_entry *pentry;
984 aa075928 2018-05-10 stsp int nscrolled = 0;
987 93e45b7c 2018-09-24 stsp pentry = TAILQ_NEXT(*last_displayed_entry, entry);
988 aa075928 2018-05-10 stsp if (pentry == NULL) {
989 93e45b7c 2018-09-24 stsp err = fetch_next_commit(&pentry, *last_displayed_entry,
990 ecb28ae0 2018-07-16 stsp commits, graph, repo, path);
991 9a6bf2a5 2018-05-20 stsp if (err || pentry == NULL)
994 93e45b7c 2018-09-24 stsp *last_displayed_entry = pentry;
996 dd0a52c1 2018-05-20 stsp pentry = TAILQ_NEXT(*first_displayed_entry, entry);
997 dd0a52c1 2018-05-20 stsp if (pentry == NULL)
999 aa075928 2018-05-10 stsp *first_displayed_entry = pentry;
1000 16482c3b 2018-05-20 stsp } while (++nscrolled < maxscroll);
1002 dd0a52c1 2018-05-20 stsp return err;
1005 cd0acaa7 2018-05-20 stsp static const struct got_error *
1006 e5a0f69f 2018-08-18 stsp show_commit(struct tog_view **new_view, struct tog_view *parent_view,
1007 e5a0f69f 2018-08-18 stsp struct commit_queue_entry *entry, struct got_repository *repo)
1009 cd0acaa7 2018-05-20 stsp const struct got_error *err;
1010 cd0acaa7 2018-05-20 stsp struct got_object *obj1 = NULL, *obj2 = NULL;
1011 9ba79e04 2018-06-11 stsp struct got_object_qid *parent_id;
1012 e5a0f69f 2018-08-18 stsp struct tog_view *diff_view;
1014 cd0acaa7 2018-05-20 stsp err = got_object_open(&obj2, repo, entry->id);
1016 cd0acaa7 2018-05-20 stsp return err;
1018 9ba79e04 2018-06-11 stsp parent_id = SIMPLEQ_FIRST(&entry->commit->parent_ids);
1019 9ba79e04 2018-06-11 stsp if (parent_id) {
1020 9ba79e04 2018-06-11 stsp err = got_object_open(&obj1, repo, parent_id->id);
1022 cd0acaa7 2018-05-20 stsp goto done;
1025 e5a0f69f 2018-08-18 stsp diff_view = view_open(0, 0, 0, 0, parent_view, TOG_VIEW_DIFF);
1026 e5a0f69f 2018-08-18 stsp if (diff_view == NULL) {
1027 ea5e7bb5 2018-08-01 stsp err = got_error_from_errno();
1028 ea5e7bb5 2018-08-01 stsp goto done;
1031 e5a0f69f 2018-08-18 stsp err = open_diff_view(diff_view, obj1, obj2, repo);
1032 e5a0f69f 2018-08-18 stsp if (err == NULL)
1033 e5a0f69f 2018-08-18 stsp *new_view = diff_view;
1036 cd0acaa7 2018-05-20 stsp got_object_close(obj1);
1038 cd0acaa7 2018-05-20 stsp got_object_close(obj2);
1039 cd0acaa7 2018-05-20 stsp return err;
1042 80ddbec8 2018-04-29 stsp static const struct got_error *
1043 e5a0f69f 2018-08-18 stsp browse_commit(struct tog_view **new_view, struct tog_view *parent_view,
1044 e5a0f69f 2018-08-18 stsp struct commit_queue_entry *entry, struct got_repository *repo)
1046 9343a5fb 2018-06-23 stsp const struct got_error *err = NULL;
1047 9343a5fb 2018-06-23 stsp struct got_tree_object *tree;
1048 e5a0f69f 2018-08-18 stsp struct tog_view *tree_view;
1050 9343a5fb 2018-06-23 stsp err = got_object_open_as_tree(&tree, repo, entry->commit->tree_id);
1052 9343a5fb 2018-06-23 stsp return err;
1054 e5a0f69f 2018-08-18 stsp tree_view = view_open(0, 0, 0, 0, parent_view, TOG_VIEW_TREE);
1055 e5a0f69f 2018-08-18 stsp if (tree_view == NULL)
1056 e5a0f69f 2018-08-18 stsp return got_error_from_errno();
1058 e5a0f69f 2018-08-18 stsp err = open_tree_view(tree_view, tree, entry->id, repo);
1060 e5a0f69f 2018-08-18 stsp got_object_tree_close(tree);
1062 e5a0f69f 2018-08-18 stsp *new_view = tree_view;
1063 9343a5fb 2018-06-23 stsp return err;
1066 bcbd79e2 2018-08-19 stsp static const struct got_error *
1067 bcbd79e2 2018-08-19 stsp set_child_log_view(struct tog_view *view, struct tog_view *child)
1069 bcbd79e2 2018-08-19 stsp struct tog_log_view_state *s = &view->state.log;
1070 bcbd79e2 2018-08-19 stsp struct tog_diff_view_state *ds;
1071 bcbd79e2 2018-08-19 stsp struct commit_queue_entry *commit, *child_entry = NULL;
1072 bcbd79e2 2018-08-19 stsp int selected_idx = 0;
1074 bcbd79e2 2018-08-19 stsp if (child->type != TOG_VIEW_DIFF)
1075 bcbd79e2 2018-08-19 stsp return NULL;
1076 bcbd79e2 2018-08-19 stsp ds = &child->state.diff;
1078 bcbd79e2 2018-08-19 stsp TAILQ_FOREACH(commit, &s->commits.head, entry) {
1079 a3404814 2018-09-02 stsp if (got_object_id_cmp(commit->id, ds->id2) == 0) {
1080 bcbd79e2 2018-08-19 stsp child_entry = commit;
1084 bcbd79e2 2018-08-19 stsp if (child_entry == NULL)
1085 bcbd79e2 2018-08-19 stsp return NULL;
1087 bcbd79e2 2018-08-19 stsp commit = s->first_displayed_entry;
1088 bcbd79e2 2018-08-19 stsp while (commit) {
1089 bcbd79e2 2018-08-19 stsp if (got_object_id_cmp(commit->id, child_entry->id) == 0) {
1090 bcbd79e2 2018-08-19 stsp s->selected_entry = child_entry;
1091 bcbd79e2 2018-08-19 stsp s->selected = selected_idx;
1094 bcbd79e2 2018-08-19 stsp if (commit == s->last_displayed_entry)
1096 bcbd79e2 2018-08-19 stsp selected_idx++;
1097 bcbd79e2 2018-08-19 stsp commit = TAILQ_NEXT(commit, entry);
1100 bcbd79e2 2018-08-19 stsp return show_log_view(view);
1103 9343a5fb 2018-06-23 stsp static const struct got_error *
1104 ba4f502b 2018-08-04 stsp open_log_view(struct tog_view *view, struct got_object_id *start_id,
1105 04cc582a 2018-08-01 stsp struct got_repository *repo, const char *path)
1107 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
1108 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
1110 fb2756b9 2018-08-04 stsp err = got_repo_map_path(&s->in_repo_path, repo, path);
1111 ecb28ae0 2018-07-16 stsp if (err != NULL)
1112 ecb28ae0 2018-07-16 stsp goto done;
1114 93e45b7c 2018-09-24 stsp err = got_commit_graph_open(&s->graph, start_id, s->in_repo_path,
1117 9ba79e04 2018-06-11 stsp goto done;
1118 93e45b7c 2018-09-24 stsp /* The commit queue only contains commits being displayed. */
1119 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->commits.head);
1120 fb2756b9 2018-08-04 stsp s->commits.ncommits = 0;
1123 9ba79e04 2018-06-11 stsp * Open the initial batch of commits, sorted in commit graph order.
1124 9ba79e04 2018-06-11 stsp * We keep all commits open throughout the lifetime of the log view
1125 9ba79e04 2018-06-11 stsp * in order to avoid having to re-fetch commits from disk while
1126 9ba79e04 2018-06-11 stsp * updating the display.
1128 93e45b7c 2018-09-24 stsp err = queue_commits(s->graph, &s->commits, start_id, view->nlines,
1129 fb2756b9 2018-08-04 stsp repo, s->in_repo_path);
1130 55198a88 2018-07-16 stsp if (err) {
1131 55198a88 2018-07-16 stsp if (err->code != GOT_ERR_ITER_COMPLETED)
1132 55198a88 2018-07-16 stsp goto done;
1133 55198a88 2018-07-16 stsp err = NULL;
1136 93e45b7c 2018-09-24 stsp s->first_displayed_entry = TAILQ_FIRST(&s->commits.head);
1137 1bfa490b 2018-08-18 stsp s->selected_entry = s->first_displayed_entry;
1138 fb2756b9 2018-08-04 stsp s->repo = repo;
1140 e5a0f69f 2018-08-18 stsp view->show = show_log_view;
1141 e5a0f69f 2018-08-18 stsp view->input = input_log_view;
1142 e5a0f69f 2018-08-18 stsp view->close = close_log_view;
1143 bcbd79e2 2018-08-19 stsp view->set_child = set_child_log_view;
1145 ba4f502b 2018-08-04 stsp return err;
1148 e5a0f69f 2018-08-18 stsp static const struct got_error *
1149 e5a0f69f 2018-08-18 stsp close_log_view(struct tog_view *view)
1151 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
1153 fb2756b9 2018-08-04 stsp if (s->graph)
1154 fb2756b9 2018-08-04 stsp got_commit_graph_close(s->graph);
1155 fb2756b9 2018-08-04 stsp free_commits(&s->commits);
1156 fb2756b9 2018-08-04 stsp free(s->in_repo_path);
1157 e5a0f69f 2018-08-18 stsp return NULL;
1160 ba4f502b 2018-08-04 stsp static const struct got_error *
1161 bcbd79e2 2018-08-19 stsp update_diff_child_view(struct tog_view *parent,
1162 bcbd79e2 2018-08-19 stsp struct commit_queue_entry *selected_entry, struct got_repository *repo)
1164 bcbd79e2 2018-08-19 stsp const struct got_error *err = NULL;
1165 bcbd79e2 2018-08-19 stsp struct tog_diff_view_state *ds;
1166 bcbd79e2 2018-08-19 stsp struct got_object *obj1 = NULL, *obj2 = NULL;
1167 bcbd79e2 2018-08-19 stsp struct got_object_qid *parent_id;
1168 bcbd79e2 2018-08-19 stsp struct tog_view *child_view = parent->child;
1170 bcbd79e2 2018-08-19 stsp if (child_view == NULL)
1171 bcbd79e2 2018-08-19 stsp return NULL;
1172 bcbd79e2 2018-08-19 stsp if (child_view->type != TOG_VIEW_DIFF)
1173 bcbd79e2 2018-08-19 stsp return NULL;
1174 bcbd79e2 2018-08-19 stsp ds = &child_view->state.diff;
1175 a3404814 2018-09-02 stsp if (got_object_id_cmp(ds->id2, selected_entry->id) == 0)
1176 bcbd79e2 2018-08-19 stsp return NULL;
1178 bcbd79e2 2018-08-19 stsp err = got_object_open(&obj2, repo, selected_entry->id);
1180 bcbd79e2 2018-08-19 stsp return err;
1182 bcbd79e2 2018-08-19 stsp parent_id = SIMPLEQ_FIRST(&selected_entry->commit->parent_ids);
1183 bcbd79e2 2018-08-19 stsp if (parent_id) {
1184 bcbd79e2 2018-08-19 stsp err = got_object_open(&obj1, repo, parent_id->id);
1186 bcbd79e2 2018-08-19 stsp goto done;
1189 bcbd79e2 2018-08-19 stsp err = close_diff_view(child_view);
1191 bcbd79e2 2018-08-19 stsp goto done;
1193 bcbd79e2 2018-08-19 stsp err = open_diff_view(child_view, obj1, obj2, repo);
1195 bcbd79e2 2018-08-19 stsp goto done;
1198 bcbd79e2 2018-08-19 stsp got_object_close(obj1);
1200 bcbd79e2 2018-08-19 stsp got_object_close(obj2);
1201 bcbd79e2 2018-08-19 stsp return err;
1204 bcbd79e2 2018-08-19 stsp static const struct got_error *
1205 ba4f502b 2018-08-04 stsp show_log_view(struct tog_view *view)
1207 bcbd79e2 2018-08-19 stsp const struct got_error *err = NULL;
1208 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
1210 bcbd79e2 2018-08-19 stsp err = draw_commits(view, &s->last_displayed_entry,
1211 e5a0f69f 2018-08-18 stsp &s->selected_entry, s->first_displayed_entry,
1212 e5a0f69f 2018-08-18 stsp &s->commits, s->selected, view->nlines, s->graph,
1213 e5a0f69f 2018-08-18 stsp s->repo, s->in_repo_path);
1215 bcbd79e2 2018-08-19 stsp return err;
1217 bcbd79e2 2018-08-19 stsp return update_diff_child_view(view, s->selected_entry, s->repo);
1220 e5a0f69f 2018-08-18 stsp static const struct got_error *
1221 e5a0f69f 2018-08-18 stsp input_log_view(struct tog_view **new_view, struct tog_view **dead_view,
1222 e5a0f69f 2018-08-18 stsp struct tog_view *view, int ch)
1224 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
1225 e5a0f69f 2018-08-18 stsp struct tog_log_view_state *s = &view->state.log;
1227 e5a0f69f 2018-08-18 stsp switch (ch) {
1229 e5a0f69f 2018-08-18 stsp case KEY_UP:
1231 e5a0f69f 2018-08-18 stsp if (s->selected > 0)
1232 e5a0f69f 2018-08-18 stsp s->selected--;
1233 e5a0f69f 2018-08-18 stsp if (s->selected > 0)
1235 e5a0f69f 2018-08-18 stsp scroll_up(&s->first_displayed_entry, 1,
1236 e5a0f69f 2018-08-18 stsp &s->commits);
1238 e5a0f69f 2018-08-18 stsp case KEY_PPAGE:
1239 e5a0f69f 2018-08-18 stsp if (TAILQ_FIRST(&s->commits.head) ==
1240 e5a0f69f 2018-08-18 stsp s->first_displayed_entry) {
1241 e5a0f69f 2018-08-18 stsp s->selected = 0;
1244 e5a0f69f 2018-08-18 stsp scroll_up(&s->first_displayed_entry,
1245 e5a0f69f 2018-08-18 stsp view->nlines, &s->commits);
1248 e5a0f69f 2018-08-18 stsp case KEY_DOWN:
1250 e5a0f69f 2018-08-18 stsp if (s->selected < MIN(view->nlines - 2,
1251 e5a0f69f 2018-08-18 stsp s->commits.ncommits - 1)) {
1252 e5a0f69f 2018-08-18 stsp s->selected++;
1255 e5a0f69f 2018-08-18 stsp err = scroll_down(&s->first_displayed_entry, 1,
1256 93e45b7c 2018-09-24 stsp &s->last_displayed_entry, &s->commits,
1257 e5a0f69f 2018-08-18 stsp s->graph, s->repo, s->in_repo_path);
1258 e5a0f69f 2018-08-18 stsp if (err) {
1259 4d7951a5 2018-08-04 stsp if (err->code != GOT_ERR_ITER_COMPLETED)
1261 4d7951a5 2018-08-04 stsp err = NULL;
1264 e5a0f69f 2018-08-18 stsp case KEY_NPAGE: {
1265 e5a0f69f 2018-08-18 stsp struct commit_queue_entry *first;
1266 e5a0f69f 2018-08-18 stsp first = s->first_displayed_entry;
1267 e5a0f69f 2018-08-18 stsp err = scroll_down(&s->first_displayed_entry,
1268 93e45b7c 2018-09-24 stsp view->nlines, &s->last_displayed_entry,
1269 e5a0f69f 2018-08-18 stsp &s->commits, s->graph, s->repo,
1270 e5a0f69f 2018-08-18 stsp s->in_repo_path);
1271 93e45b7c 2018-09-24 stsp if (err && err->code != GOT_ERR_ITER_COMPLETED)
1273 e5a0f69f 2018-08-18 stsp if (first == s->first_displayed_entry &&
1274 e5a0f69f 2018-08-18 stsp s->selected < MIN(view->nlines - 2,
1275 e5a0f69f 2018-08-18 stsp s->commits.ncommits - 1)) {
1276 e5a0f69f 2018-08-18 stsp /* can't scroll further down */
1277 e5a0f69f 2018-08-18 stsp s->selected = MIN(view->nlines - 2,
1278 e5a0f69f 2018-08-18 stsp s->commits.ncommits - 1);
1280 e5a0f69f 2018-08-18 stsp err = NULL;
1283 e5a0f69f 2018-08-18 stsp case KEY_RESIZE:
1284 e5a0f69f 2018-08-18 stsp if (s->selected > view->nlines - 2)
1285 e5a0f69f 2018-08-18 stsp s->selected = view->nlines - 2;
1286 e5a0f69f 2018-08-18 stsp if (s->selected > s->commits.ncommits - 1)
1287 e5a0f69f 2018-08-18 stsp s->selected = s->commits.ncommits - 1;
1289 e5a0f69f 2018-08-18 stsp case KEY_ENTER:
1290 e5a0f69f 2018-08-18 stsp case '\r':
1291 e5a0f69f 2018-08-18 stsp err = show_commit(new_view, view, s->selected_entry,
1295 e5a0f69f 2018-08-18 stsp err = browse_commit(new_view, view, s->selected_entry,
1302 80ddbec8 2018-04-29 stsp return err;
1305 4ed7e80c 2018-05-20 stsp static const struct got_error *
1306 9f7d7167 2018-04-29 stsp cmd_log(int argc, char *argv[])
1308 80ddbec8 2018-04-29 stsp const struct got_error *error;
1309 ecb28ae0 2018-07-16 stsp struct got_repository *repo = NULL;
1310 899d86c2 2018-05-10 stsp struct got_object_id *start_id = NULL;
1311 ecb28ae0 2018-07-16 stsp char *path = NULL, *repo_path = NULL, *cwd = NULL;
1312 80ddbec8 2018-04-29 stsp char *start_commit = NULL;
1314 04cc582a 2018-08-01 stsp struct tog_view *view;
1316 80ddbec8 2018-04-29 stsp #ifndef PROFILE
1317 ad242220 2018-09-08 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd", NULL)
1319 80ddbec8 2018-04-29 stsp err(1, "pledge");
1322 ecb28ae0 2018-07-16 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1323 80ddbec8 2018-04-29 stsp switch (ch) {
1325 80ddbec8 2018-04-29 stsp start_commit = optarg;
1328 ecb28ae0 2018-07-16 stsp repo_path = realpath(optarg, NULL);
1329 ecb28ae0 2018-07-16 stsp if (repo_path == NULL)
1330 ecb28ae0 2018-07-16 stsp err(1, "-r option");
1334 80ddbec8 2018-04-29 stsp /* NOTREACHED */
1338 80ddbec8 2018-04-29 stsp argc -= optind;
1339 80ddbec8 2018-04-29 stsp argv += optind;
1341 ecb28ae0 2018-07-16 stsp if (argc == 0)
1342 ecb28ae0 2018-07-16 stsp path = strdup("");
1343 ecb28ae0 2018-07-16 stsp else if (argc == 1)
1344 ecb28ae0 2018-07-16 stsp path = strdup(argv[0]);
1346 80ddbec8 2018-04-29 stsp usage_log();
1347 ecb28ae0 2018-07-16 stsp if (path == NULL)
1348 ecb28ae0 2018-07-16 stsp return got_error_from_errno();
1350 ecb28ae0 2018-07-16 stsp cwd = getcwd(NULL, 0);
1351 ecb28ae0 2018-07-16 stsp if (cwd == NULL) {
1352 ecb28ae0 2018-07-16 stsp error = got_error_from_errno();
1353 ecb28ae0 2018-07-16 stsp goto done;
1355 ecb28ae0 2018-07-16 stsp if (repo_path == NULL) {
1356 ecb28ae0 2018-07-16 stsp repo_path = strdup(cwd);
1357 ecb28ae0 2018-07-16 stsp if (repo_path == NULL) {
1358 ecb28ae0 2018-07-16 stsp error = got_error_from_errno();
1359 ecb28ae0 2018-07-16 stsp goto done;
1363 80ddbec8 2018-04-29 stsp error = got_repo_open(&repo, repo_path);
1364 80ddbec8 2018-04-29 stsp if (error != NULL)
1365 ecb28ae0 2018-07-16 stsp goto done;
1367 80ddbec8 2018-04-29 stsp if (start_commit == NULL) {
1368 899d86c2 2018-05-10 stsp error = get_head_commit_id(&start_id, repo);
1369 80ddbec8 2018-04-29 stsp if (error != NULL)
1370 ecb28ae0 2018-07-16 stsp goto done;
1372 80ddbec8 2018-04-29 stsp struct got_object *obj;
1373 80ddbec8 2018-04-29 stsp error = got_object_open_by_id_str(&obj, repo, start_commit);
1374 80ddbec8 2018-04-29 stsp if (error == NULL) {
1375 6402fb3c 2018-09-15 stsp start_id = got_object_id_dup(got_object_get_id(obj));
1376 899d86c2 2018-05-10 stsp if (start_id == NULL)
1377 80ddbec8 2018-04-29 stsp error = got_error_from_errno();
1378 ecb28ae0 2018-07-16 stsp goto done;
1381 80ddbec8 2018-04-29 stsp if (error != NULL)
1382 ecb28ae0 2018-07-16 stsp goto done;
1384 b3665f43 2018-08-04 stsp view = view_open(0, 0, 0, 0, NULL, TOG_VIEW_LOG);
1385 04cc582a 2018-08-01 stsp if (view == NULL) {
1386 04cc582a 2018-08-01 stsp error = got_error_from_errno();
1387 04cc582a 2018-08-01 stsp goto done;
1389 ba4f502b 2018-08-04 stsp error = open_log_view(view, start_id, repo, path);
1390 ba4f502b 2018-08-04 stsp if (error)
1391 ba4f502b 2018-08-04 stsp goto done;
1392 e5a0f69f 2018-08-18 stsp error = view_loop(view);
1394 ecb28ae0 2018-07-16 stsp free(repo_path);
1395 ecb28ae0 2018-07-16 stsp free(cwd);
1396 ecb28ae0 2018-07-16 stsp free(path);
1397 899d86c2 2018-05-10 stsp free(start_id);
1399 ecb28ae0 2018-07-16 stsp got_repo_close(repo);
1400 80ddbec8 2018-04-29 stsp return error;
1403 4ed7e80c 2018-05-20 stsp __dead static void
1404 9f7d7167 2018-04-29 stsp usage_diff(void)
1407 9f7d7167 2018-04-29 stsp fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
1408 9f7d7167 2018-04-29 stsp getprogname());
1412 b304db33 2018-05-20 stsp static char *
1413 b304db33 2018-05-20 stsp parse_next_line(FILE *f, size_t *len)
1415 b304db33 2018-05-20 stsp char *line;
1416 b304db33 2018-05-20 stsp size_t linelen;
1417 b304db33 2018-05-20 stsp size_t lineno;
1418 b304db33 2018-05-20 stsp const char delim[3] = { '\0', '\0', '\0'};
1420 b304db33 2018-05-20 stsp line = fparseln(f, &linelen, &lineno, delim, 0);
1422 b304db33 2018-05-20 stsp *len = linelen;
1423 b304db33 2018-05-20 stsp return line;
1426 4ed7e80c 2018-05-20 stsp static const struct got_error *
1427 f7d12f7e 2018-08-01 stsp draw_file(struct tog_view *view, FILE *f, int *first_displayed_line,
1428 a3404814 2018-09-02 stsp int *last_displayed_line, int *eof, int max_lines,
1429 a3404814 2018-09-02 stsp char * header)
1431 61e69b96 2018-05-20 stsp const struct got_error *err;
1432 26ed57b2 2018-05-19 stsp int nlines = 0, nprinted = 0;
1433 b304db33 2018-05-20 stsp char *line;
1434 b304db33 2018-05-20 stsp size_t len;
1435 61e69b96 2018-05-20 stsp wchar_t *wline;
1436 e0b650dd 2018-05-20 stsp int width;
1438 26ed57b2 2018-05-19 stsp rewind(f);
1439 f7d12f7e 2018-08-01 stsp werase(view->window);
1441 a3404814 2018-09-02 stsp if (header) {
1442 a3404814 2018-09-02 stsp err = format_line(&wline, &width, header, view->ncols);
1443 a3404814 2018-09-02 stsp if (err) {
1444 a3404814 2018-09-02 stsp return err;
1447 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
1448 a3404814 2018-09-02 stsp wstandout(view->window);
1449 a3404814 2018-09-02 stsp waddwstr(view->window, wline);
1450 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
1451 a3404814 2018-09-02 stsp wstandend(view->window);
1452 a3404814 2018-09-02 stsp if (width < view->ncols)
1453 a3404814 2018-09-02 stsp waddch(view->window, '\n');
1455 a3404814 2018-09-02 stsp if (max_lines <= 1)
1456 a3404814 2018-09-02 stsp return NULL;
1457 a3404814 2018-09-02 stsp max_lines--;
1461 26ed57b2 2018-05-19 stsp while (nprinted < max_lines) {
1462 b304db33 2018-05-20 stsp line = parse_next_line(f, &len);
1463 26ed57b2 2018-05-19 stsp if (line == NULL) {
1467 26ed57b2 2018-05-19 stsp if (++nlines < *first_displayed_line) {
1468 26ed57b2 2018-05-19 stsp free(line);
1472 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, line, view->ncols);
1473 61e69b96 2018-05-20 stsp if (err) {
1474 61e69b96 2018-05-20 stsp free(line);
1475 61e69b96 2018-05-20 stsp return err;
1477 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
1478 f7d12f7e 2018-08-01 stsp if (width < view->ncols)
1479 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
1480 26ed57b2 2018-05-19 stsp if (++nprinted == 1)
1481 26ed57b2 2018-05-19 stsp *first_displayed_line = nlines;
1482 26ed57b2 2018-05-19 stsp free(line);
1483 2550e4c3 2018-07-13 stsp free(wline);
1484 2550e4c3 2018-07-13 stsp wline = NULL;
1486 26ed57b2 2018-05-19 stsp *last_displayed_line = nlines;
1488 1a57306a 2018-09-02 stsp view_vborder(view);
1490 26ed57b2 2018-05-19 stsp return NULL;
1493 4ed7e80c 2018-05-20 stsp static const struct got_error *
1494 5dc9f4bc 2018-08-04 stsp open_diff_view(struct tog_view *view, struct got_object *obj1,
1495 ea5e7bb5 2018-08-01 stsp struct got_object *obj2, struct got_repository *repo)
1497 26ed57b2 2018-05-19 stsp const struct got_error *err;
1500 cd0acaa7 2018-05-20 stsp if (obj1 != NULL && obj2 != NULL &&
1501 cd0acaa7 2018-05-20 stsp got_object_get_type(obj1) != got_object_get_type(obj2))
1502 26ed57b2 2018-05-19 stsp return got_error(GOT_ERR_OBJ_TYPE);
1504 511a516b 2018-05-19 stsp f = got_opentemp();
1505 26ed57b2 2018-05-19 stsp if (f == NULL)
1506 26ed57b2 2018-05-19 stsp return got_error_from_errno();
1508 cd0acaa7 2018-05-20 stsp switch (got_object_get_type(obj1 ? obj1 : obj2)) {
1509 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_BLOB:
1510 f6861a81 2018-09-13 stsp err = got_diff_objects_as_blobs(obj1, obj2, NULL, NULL,
1513 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_TREE:
1514 f6861a81 2018-09-13 stsp err = got_diff_objects_as_trees(obj1, obj2, "", "", repo, f);
1516 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_COMMIT:
1517 11528a82 2018-05-19 stsp err = got_diff_objects_as_commits(obj1, obj2, repo, f);
1520 26ed57b2 2018-05-19 stsp return got_error(GOT_ERR_OBJ_TYPE);
1523 26ed57b2 2018-05-19 stsp fflush(f);
1525 a3404814 2018-09-02 stsp view->state.diff.id1 = obj1 ? got_object_get_id(obj1) : NULL;
1526 a3404814 2018-09-02 stsp view->state.diff.id2 = got_object_get_id(obj2);
1527 5dc9f4bc 2018-08-04 stsp view->state.diff.f = f;
1528 5dc9f4bc 2018-08-04 stsp view->state.diff.first_displayed_line = 1;
1529 5dc9f4bc 2018-08-04 stsp view->state.diff.last_displayed_line = view->nlines;
1531 e5a0f69f 2018-08-18 stsp view->show = show_diff_view;
1532 e5a0f69f 2018-08-18 stsp view->input = input_diff_view;
1533 e5a0f69f 2018-08-18 stsp view->close = close_diff_view;
1535 5dc9f4bc 2018-08-04 stsp return NULL;
1538 e5a0f69f 2018-08-18 stsp static const struct got_error *
1539 5dc9f4bc 2018-08-04 stsp close_diff_view(struct tog_view *view)
1541 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
1543 e5a0f69f 2018-08-18 stsp if (view->state.diff.f && fclose(view->state.diff.f) == EOF)
1544 e5a0f69f 2018-08-18 stsp err = got_error_from_errno();
1545 e5a0f69f 2018-08-18 stsp return err;
1548 5dc9f4bc 2018-08-04 stsp static const struct got_error *
1549 5dc9f4bc 2018-08-04 stsp show_diff_view(struct tog_view *view)
1551 a3404814 2018-09-02 stsp const struct got_error *err;
1552 fb2756b9 2018-08-04 stsp struct tog_diff_view_state *s = &view->state.diff;
1553 a3404814 2018-09-02 stsp char *id_str1 = NULL, *id_str2, *header;
1555 a3404814 2018-09-02 stsp if (s->id1) {
1556 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str1, s->id1);
1558 a3404814 2018-09-02 stsp return err;
1560 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str2, s->id2);
1562 a3404814 2018-09-02 stsp return err;
1564 a3404814 2018-09-02 stsp if (asprintf(&header, "diff: %s %s",
1565 a3404814 2018-09-02 stsp id_str1 ? id_str1 : "/dev/null", id_str2) == -1) {
1566 a3404814 2018-09-02 stsp err = got_error_from_errno();
1567 a3404814 2018-09-02 stsp free(id_str1);
1568 a3404814 2018-09-02 stsp free(id_str2);
1569 a3404814 2018-09-02 stsp return err;
1571 a3404814 2018-09-02 stsp free(id_str1);
1572 a3404814 2018-09-02 stsp free(id_str2);
1574 e5a0f69f 2018-08-18 stsp return draw_file(view, s->f, &s->first_displayed_line,
1575 a3404814 2018-09-02 stsp &s->last_displayed_line, &s->eof, view->nlines,
1579 e5a0f69f 2018-08-18 stsp static const struct got_error *
1580 bcbd79e2 2018-08-19 stsp input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
1581 e5a0f69f 2018-08-18 stsp struct tog_view *view, int ch)
1583 bcbd79e2 2018-08-19 stsp const struct got_error *err = NULL;
1584 e5a0f69f 2018-08-18 stsp struct tog_diff_view_state *s = &view->state.diff;
1587 e5a0f69f 2018-08-18 stsp switch (ch) {
1589 e5a0f69f 2018-08-18 stsp case KEY_UP:
1590 e5a0f69f 2018-08-18 stsp if (s->first_displayed_line > 1)
1591 e5a0f69f 2018-08-18 stsp s->first_displayed_line--;
1593 e5a0f69f 2018-08-18 stsp case KEY_PPAGE:
1595 e5a0f69f 2018-08-18 stsp while (i++ < view->nlines - 1 &&
1596 e5a0f69f 2018-08-18 stsp s->first_displayed_line > 1)
1597 e5a0f69f 2018-08-18 stsp s->first_displayed_line--;
1600 e5a0f69f 2018-08-18 stsp case KEY_DOWN:
1601 e5a0f69f 2018-08-18 stsp if (!s->eof)
1602 e5a0f69f 2018-08-18 stsp s->first_displayed_line++;
1604 e5a0f69f 2018-08-18 stsp case KEY_NPAGE:
1607 e5a0f69f 2018-08-18 stsp while (!s->eof && i++ < view->nlines - 1) {
1608 e5a0f69f 2018-08-18 stsp char *line;
1609 e5a0f69f 2018-08-18 stsp line = parse_next_line(s->f, NULL);
1610 e5a0f69f 2018-08-18 stsp s->first_displayed_line++;
1611 e5a0f69f 2018-08-18 stsp if (line == NULL)
1616 bcbd79e2 2018-08-19 stsp case ']': {
1617 bcbd79e2 2018-08-19 stsp struct tog_log_view_state *ls;
1618 bcbd79e2 2018-08-19 stsp struct commit_queue_entry *entry;
1619 bcbd79e2 2018-08-19 stsp struct tog_view *diff_view;
1621 bcbd79e2 2018-08-19 stsp if (view->parent == NULL)
1623 bcbd79e2 2018-08-19 stsp if (view->parent->type != TOG_VIEW_LOG)
1625 bcbd79e2 2018-08-19 stsp ls = &view->parent->state.log;
1627 bcbd79e2 2018-08-19 stsp if (ch == '[') {
1628 bcbd79e2 2018-08-19 stsp entry = TAILQ_PREV(ls->selected_entry,
1629 bcbd79e2 2018-08-19 stsp commit_queue_head, entry);
1631 bcbd79e2 2018-08-19 stsp entry = TAILQ_NEXT(ls->selected_entry, entry);
1632 bcbd79e2 2018-08-19 stsp if (entry == NULL) {
1633 bcbd79e2 2018-08-19 stsp err = fetch_next_commit(&entry,
1634 bcbd79e2 2018-08-19 stsp ls->selected_entry,
1635 bcbd79e2 2018-08-19 stsp &ls->commits, ls->graph,
1636 bcbd79e2 2018-08-19 stsp ls->repo, ls->in_repo_path);
1641 bcbd79e2 2018-08-19 stsp if (entry == NULL)
1643 bcbd79e2 2018-08-19 stsp err = show_commit(&diff_view, view->parent,
1644 bcbd79e2 2018-08-19 stsp entry, ls->repo);
1647 bcbd79e2 2018-08-19 stsp *new_view = diff_view;
1648 bcbd79e2 2018-08-19 stsp *dead_view = view;
1655 bcbd79e2 2018-08-19 stsp return err;
1658 4ed7e80c 2018-05-20 stsp static const struct got_error *
1659 9f7d7167 2018-04-29 stsp cmd_diff(int argc, char *argv[])
1661 26ed57b2 2018-05-19 stsp const struct got_error *error = NULL;
1662 26ed57b2 2018-05-19 stsp struct got_repository *repo = NULL;
1663 26ed57b2 2018-05-19 stsp struct got_object *obj1 = NULL, *obj2 = NULL;
1664 26ed57b2 2018-05-19 stsp char *repo_path = NULL;
1665 26ed57b2 2018-05-19 stsp char *obj_id_str1 = NULL, *obj_id_str2 = NULL;
1667 ea5e7bb5 2018-08-01 stsp struct tog_view *view;
1669 26ed57b2 2018-05-19 stsp #ifndef PROFILE
1670 ad242220 2018-09-08 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd", NULL)
1672 26ed57b2 2018-05-19 stsp err(1, "pledge");
1675 26ed57b2 2018-05-19 stsp while ((ch = getopt(argc, argv, "")) != -1) {
1676 26ed57b2 2018-05-19 stsp switch (ch) {
1679 26ed57b2 2018-05-19 stsp /* NOTREACHED */
1683 26ed57b2 2018-05-19 stsp argc -= optind;
1684 26ed57b2 2018-05-19 stsp argv += optind;
1686 26ed57b2 2018-05-19 stsp if (argc == 0) {
1687 26ed57b2 2018-05-19 stsp usage_diff(); /* TODO show local worktree changes */
1688 26ed57b2 2018-05-19 stsp } else if (argc == 2) {
1689 26ed57b2 2018-05-19 stsp repo_path = getcwd(NULL, 0);
1690 26ed57b2 2018-05-19 stsp if (repo_path == NULL)
1691 26ed57b2 2018-05-19 stsp return got_error_from_errno();
1692 26ed57b2 2018-05-19 stsp obj_id_str1 = argv[0];
1693 26ed57b2 2018-05-19 stsp obj_id_str2 = argv[1];
1694 26ed57b2 2018-05-19 stsp } else if (argc == 3) {
1695 26ed57b2 2018-05-19 stsp repo_path = realpath(argv[0], NULL);
1696 26ed57b2 2018-05-19 stsp if (repo_path == NULL)
1697 26ed57b2 2018-05-19 stsp return got_error_from_errno();
1698 26ed57b2 2018-05-19 stsp obj_id_str1 = argv[1];
1699 26ed57b2 2018-05-19 stsp obj_id_str2 = argv[2];
1701 26ed57b2 2018-05-19 stsp usage_diff();
1703 26ed57b2 2018-05-19 stsp error = got_repo_open(&repo, repo_path);
1704 26ed57b2 2018-05-19 stsp free(repo_path);
1705 26ed57b2 2018-05-19 stsp if (error)
1706 26ed57b2 2018-05-19 stsp goto done;
1708 26ed57b2 2018-05-19 stsp error = got_object_open_by_id_str(&obj1, repo, obj_id_str1);
1709 26ed57b2 2018-05-19 stsp if (error)
1710 26ed57b2 2018-05-19 stsp goto done;
1712 26ed57b2 2018-05-19 stsp error = got_object_open_by_id_str(&obj2, repo, obj_id_str2);
1713 26ed57b2 2018-05-19 stsp if (error)
1714 26ed57b2 2018-05-19 stsp goto done;
1716 b3665f43 2018-08-04 stsp view = view_open(0, 0, 0, 0, NULL, TOG_VIEW_DIFF);
1717 ea5e7bb5 2018-08-01 stsp if (view == NULL) {
1718 ea5e7bb5 2018-08-01 stsp error = got_error_from_errno();
1719 ea5e7bb5 2018-08-01 stsp goto done;
1721 5dc9f4bc 2018-08-04 stsp error = open_diff_view(view, obj1, obj2, repo);
1722 5dc9f4bc 2018-08-04 stsp if (error)
1723 5dc9f4bc 2018-08-04 stsp goto done;
1724 e5a0f69f 2018-08-18 stsp error = view_loop(view);
1726 26ed57b2 2018-05-19 stsp got_repo_close(repo);
1728 26ed57b2 2018-05-19 stsp got_object_close(obj1);
1730 26ed57b2 2018-05-19 stsp got_object_close(obj2);
1731 26ed57b2 2018-05-19 stsp return error;
1734 4ed7e80c 2018-05-20 stsp __dead static void
1735 9f7d7167 2018-04-29 stsp usage_blame(void)
1738 69069811 2018-08-02 stsp fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
1739 9f7d7167 2018-04-29 stsp getprogname());
1743 84451b3e 2018-07-10 stsp struct tog_blame_line {
1744 84451b3e 2018-07-10 stsp int annotated;
1745 84451b3e 2018-07-10 stsp struct got_object_id *id;
1748 4ed7e80c 2018-05-20 stsp static const struct got_error *
1749 f7d12f7e 2018-08-01 stsp draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
1750 f7d12f7e 2018-08-01 stsp const char *path, struct tog_blame_line *lines, int nlines,
1751 f7d12f7e 2018-08-01 stsp int blame_complete, int selected_line, int *first_displayed_line,
1752 f7d12f7e 2018-08-01 stsp int *last_displayed_line, int *eof, int max_lines)
1754 84451b3e 2018-07-10 stsp const struct got_error *err;
1755 84451b3e 2018-07-10 stsp int lineno = 0, nprinted = 0;
1756 84451b3e 2018-07-10 stsp char *line;
1757 84451b3e 2018-07-10 stsp size_t len;
1758 84451b3e 2018-07-10 stsp wchar_t *wline;
1759 b700b5d6 2018-07-10 stsp int width, wlimit;
1760 84451b3e 2018-07-10 stsp struct tog_blame_line *blame_line;
1761 ee41ec32 2018-07-10 stsp struct got_object_id *prev_id = NULL;
1762 ab089a2a 2018-07-12 stsp char *id_str;
1764 ab089a2a 2018-07-12 stsp err = got_object_id_str(&id_str, id);
1766 ab089a2a 2018-07-12 stsp return err;
1768 84451b3e 2018-07-10 stsp rewind(f);
1769 f7d12f7e 2018-08-01 stsp werase(view->window);
1771 ab089a2a 2018-07-12 stsp if (asprintf(&line, "commit: %s", id_str) == -1) {
1772 ab089a2a 2018-07-12 stsp err = got_error_from_errno();
1773 ab089a2a 2018-07-12 stsp free(id_str);
1774 ab089a2a 2018-07-12 stsp return err;
1777 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, line, view->ncols);
1778 ab089a2a 2018-07-12 stsp free(line);
1779 2550e4c3 2018-07-13 stsp line = NULL;
1780 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
1781 a3404814 2018-09-02 stsp wstandout(view->window);
1782 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
1783 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
1784 a3404814 2018-09-02 stsp wstandend(view->window);
1785 2550e4c3 2018-07-13 stsp free(wline);
1786 2550e4c3 2018-07-13 stsp wline = NULL;
1787 f7d12f7e 2018-08-01 stsp if (width < view->ncols)
1788 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
1790 084063cd 2018-07-12 stsp if (asprintf(&line, "[%d/%d] %s%s",
1791 084063cd 2018-07-12 stsp *first_displayed_line - 1 + selected_line, nlines,
1792 12a0b23b 2018-07-12 stsp blame_complete ? "" : "annotating ", path) == -1) {
1793 ab089a2a 2018-07-12 stsp free(id_str);
1794 3f60a8ef 2018-07-10 stsp return got_error_from_errno();
1796 ab089a2a 2018-07-12 stsp free(id_str);
1797 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, line, view->ncols);
1798 3f60a8ef 2018-07-10 stsp free(line);
1799 2550e4c3 2018-07-13 stsp line = NULL;
1801 3f60a8ef 2018-07-10 stsp return err;
1802 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
1803 2550e4c3 2018-07-13 stsp free(wline);
1804 2550e4c3 2018-07-13 stsp wline = NULL;
1805 f7d12f7e 2018-08-01 stsp if (width < view->ncols)
1806 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
1809 ab089a2a 2018-07-12 stsp while (nprinted < max_lines - 2) {
1810 84451b3e 2018-07-10 stsp line = parse_next_line(f, &len);
1811 84451b3e 2018-07-10 stsp if (line == NULL) {
1815 84451b3e 2018-07-10 stsp if (++lineno < *first_displayed_line) {
1816 84451b3e 2018-07-10 stsp free(line);
1820 f7d12f7e 2018-08-01 stsp wlimit = view->ncols < 9 ? 0 : view->ncols - 9;
1821 b700b5d6 2018-07-10 stsp err = format_line(&wline, &width, line, wlimit);
1822 84451b3e 2018-07-10 stsp if (err) {
1823 84451b3e 2018-07-10 stsp free(line);
1824 84451b3e 2018-07-10 stsp return err;
1827 b700b5d6 2018-07-10 stsp if (nprinted == selected_line - 1)
1828 f7d12f7e 2018-08-01 stsp wstandout(view->window);
1830 84451b3e 2018-07-10 stsp blame_line = &lines[lineno - 1];
1831 ee41ec32 2018-07-10 stsp if (blame_line->annotated && prev_id &&
1832 ee41ec32 2018-07-10 stsp got_object_id_cmp(prev_id, blame_line->id) == 0)
1833 f7d12f7e 2018-08-01 stsp waddstr(view->window, " ");
1834 ee41ec32 2018-07-10 stsp else if (blame_line->annotated) {
1835 84451b3e 2018-07-10 stsp char *id_str;
1836 84451b3e 2018-07-10 stsp err = got_object_id_str(&id_str, blame_line->id);
1837 84451b3e 2018-07-10 stsp if (err) {
1838 84451b3e 2018-07-10 stsp free(line);
1839 2550e4c3 2018-07-13 stsp free(wline);
1840 84451b3e 2018-07-10 stsp return err;
1842 f7d12f7e 2018-08-01 stsp wprintw(view->window, "%.8s ", id_str);
1843 84451b3e 2018-07-10 stsp free(id_str);
1844 ee41ec32 2018-07-10 stsp prev_id = blame_line->id;
1846 f7d12f7e 2018-08-01 stsp waddstr(view->window, "........ ");
1847 ee41ec32 2018-07-10 stsp prev_id = NULL;
1850 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
1851 b700b5d6 2018-07-10 stsp while (width < wlimit) {
1852 f7d12f7e 2018-08-01 stsp waddch(view->window, ' ');
1855 b700b5d6 2018-07-10 stsp if (nprinted == selected_line - 1)
1856 f7d12f7e 2018-08-01 stsp wstandend(view->window);
1857 84451b3e 2018-07-10 stsp if (++nprinted == 1)
1858 84451b3e 2018-07-10 stsp *first_displayed_line = lineno;
1859 84451b3e 2018-07-10 stsp free(line);
1860 2550e4c3 2018-07-13 stsp free(wline);
1861 2550e4c3 2018-07-13 stsp wline = NULL;
1863 84451b3e 2018-07-10 stsp *last_displayed_line = lineno;
1865 1a57306a 2018-09-02 stsp view_vborder(view);
1867 84451b3e 2018-07-10 stsp return NULL;
1870 84451b3e 2018-07-10 stsp static const struct got_error *
1871 84451b3e 2018-07-10 stsp blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
1873 84451b3e 2018-07-10 stsp const struct got_error *err = NULL;
1874 84451b3e 2018-07-10 stsp struct tog_blame_cb_args *a = arg;
1875 84451b3e 2018-07-10 stsp struct tog_blame_line *line;
1877 d68a0a7d 2018-07-10 stsp if (nlines != a->nlines ||
1878 d68a0a7d 2018-07-10 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
1879 84451b3e 2018-07-10 stsp return got_error(GOT_ERR_RANGE);
1881 84451b3e 2018-07-10 stsp if (pthread_mutex_lock(a->mutex) != 0)
1882 84451b3e 2018-07-10 stsp return got_error_from_errno();
1884 18430de3 2018-07-10 stsp if (*a->quit) { /* user has quit the blame view */
1885 d68a0a7d 2018-07-10 stsp err = got_error(GOT_ERR_ITER_COMPLETED);
1886 d68a0a7d 2018-07-10 stsp goto done;
1889 d68a0a7d 2018-07-10 stsp if (lineno == -1)
1890 d68a0a7d 2018-07-10 stsp goto done; /* no change in this commit */
1892 84451b3e 2018-07-10 stsp line = &a->lines[lineno - 1];
1893 d68a0a7d 2018-07-10 stsp if (line->annotated)
1894 d68a0a7d 2018-07-10 stsp goto done;
1896 84451b3e 2018-07-10 stsp line->id = got_object_id_dup(id);
1897 84451b3e 2018-07-10 stsp if (line->id == NULL) {
1898 84451b3e 2018-07-10 stsp err = got_error_from_errno();
1899 84451b3e 2018-07-10 stsp goto done;
1901 84451b3e 2018-07-10 stsp line->annotated = 1;
1903 f7d12f7e 2018-08-01 stsp err = draw_blame(a->view, a->commit_id, a->f, a->path,
1904 245d91c1 2018-07-12 stsp a->lines, a->nlines, 0, *a->selected_line, a->first_displayed_line,
1905 e5a0f69f 2018-08-18 stsp a->last_displayed_line, a->eof, a->view->nlines);
1907 84451b3e 2018-07-10 stsp if (pthread_mutex_unlock(a->mutex) != 0)
1908 84451b3e 2018-07-10 stsp return got_error_from_errno();
1909 84451b3e 2018-07-10 stsp return err;
1912 84451b3e 2018-07-10 stsp static void *
1913 84451b3e 2018-07-10 stsp blame_thread(void *arg)
1915 18430de3 2018-07-10 stsp const struct got_error *err;
1916 18430de3 2018-07-10 stsp struct tog_blame_thread_args *ta = arg;
1917 245d91c1 2018-07-12 stsp struct tog_blame_cb_args *a = ta->cb_args;
1919 ab089a2a 2018-07-12 stsp err = got_blame_incremental(ta->path, a->commit_id, ta->repo,
1920 245d91c1 2018-07-12 stsp blame_cb, ta->cb_args);
1922 18430de3 2018-07-10 stsp if (pthread_mutex_lock(a->mutex) != 0)
1923 18430de3 2018-07-10 stsp return (void *)got_error_from_errno();
1925 c9beca56 2018-07-22 stsp got_repo_close(ta->repo);
1926 c9beca56 2018-07-22 stsp ta->repo = NULL;
1927 c9beca56 2018-07-22 stsp *ta->complete = 1;
1929 f7d12f7e 2018-08-01 stsp err = draw_blame(a->view, a->commit_id, a->f, a->path,
1930 f7d12f7e 2018-08-01 stsp a->lines, a->nlines, 1, *a->selected_line,
1931 e5a0f69f 2018-08-18 stsp a->first_displayed_line, a->last_displayed_line, a->eof,
1932 f7d12f7e 2018-08-01 stsp a->view->nlines);
1934 18430de3 2018-07-10 stsp if (pthread_mutex_unlock(a->mutex) != 0 && err == NULL)
1935 18430de3 2018-07-10 stsp err = got_error_from_errno();
1937 18430de3 2018-07-10 stsp return (void *)err;
1940 245d91c1 2018-07-12 stsp static struct got_object_id *
1941 245d91c1 2018-07-12 stsp get_selected_commit_id(struct tog_blame_line *lines,
1942 245d91c1 2018-07-12 stsp int first_displayed_line, int selected_line)
1944 245d91c1 2018-07-12 stsp struct tog_blame_line *line;
1946 245d91c1 2018-07-12 stsp line = &lines[first_displayed_line - 1 + selected_line - 1];
1947 245d91c1 2018-07-12 stsp if (!line->annotated)
1948 245d91c1 2018-07-12 stsp return NULL;
1950 245d91c1 2018-07-12 stsp return line->id;
1953 84451b3e 2018-07-10 stsp static const struct got_error *
1954 245d91c1 2018-07-12 stsp open_selected_commit(struct got_object **pobj, struct got_object **obj,
1955 b880a918 2018-07-10 stsp struct tog_blame_line *lines, int first_displayed_line,
1956 b880a918 2018-07-10 stsp int selected_line, struct got_repository *repo)
1958 b880a918 2018-07-10 stsp const struct got_error *err = NULL;
1959 b880a918 2018-07-10 stsp struct got_commit_object *commit = NULL;
1960 245d91c1 2018-07-12 stsp struct got_object_id *selected_id;
1961 b880a918 2018-07-10 stsp struct got_object_qid *pid;
1963 b880a918 2018-07-10 stsp *pobj = NULL;
1964 b880a918 2018-07-10 stsp *obj = NULL;
1966 245d91c1 2018-07-12 stsp selected_id = get_selected_commit_id(lines,
1967 245d91c1 2018-07-12 stsp first_displayed_line, selected_line);
1968 245d91c1 2018-07-12 stsp if (selected_id == NULL)
1969 b880a918 2018-07-10 stsp return NULL;
1971 245d91c1 2018-07-12 stsp err = got_object_open(obj, repo, selected_id);
1973 b880a918 2018-07-10 stsp goto done;
1975 b880a918 2018-07-10 stsp err = got_object_commit_open(&commit, repo, *obj);
1977 b880a918 2018-07-10 stsp goto done;
1979 b880a918 2018-07-10 stsp pid = SIMPLEQ_FIRST(&commit->parent_ids);
1980 b880a918 2018-07-10 stsp if (pid) {
1981 b880a918 2018-07-10 stsp err = got_object_open(pobj, repo, pid->id);
1983 b880a918 2018-07-10 stsp goto done;
1986 b880a918 2018-07-10 stsp if (commit)
1987 b880a918 2018-07-10 stsp got_object_commit_close(commit);
1988 b880a918 2018-07-10 stsp return err;
1991 b880a918 2018-07-10 stsp static const struct got_error *
1992 245d91c1 2018-07-12 stsp stop_blame(struct tog_blame *blame)
1994 a70480e0 2018-06-23 stsp const struct got_error *err = NULL;
1997 245d91c1 2018-07-12 stsp if (blame->thread) {
1998 245d91c1 2018-07-12 stsp if (pthread_join(blame->thread, (void **)&err) != 0)
1999 245d91c1 2018-07-12 stsp err = got_error_from_errno();
2000 245d91c1 2018-07-12 stsp if (err && err->code == GOT_ERR_ITER_COMPLETED)
2001 245d91c1 2018-07-12 stsp err = NULL;
2002 245d91c1 2018-07-12 stsp blame->thread = NULL;
2004 245d91c1 2018-07-12 stsp if (blame->thread_args.repo) {
2005 245d91c1 2018-07-12 stsp got_repo_close(blame->thread_args.repo);
2006 245d91c1 2018-07-12 stsp blame->thread_args.repo = NULL;
2008 245d91c1 2018-07-12 stsp if (blame->f) {
2009 245d91c1 2018-07-12 stsp fclose(blame->f);
2010 245d91c1 2018-07-12 stsp blame->f = NULL;
2012 245d91c1 2018-07-12 stsp for (i = 0; i < blame->nlines; i++)
2013 245d91c1 2018-07-12 stsp free(blame->lines[i].id);
2014 245d91c1 2018-07-12 stsp free(blame->lines);
2015 245d91c1 2018-07-12 stsp blame->lines = NULL;
2016 75c32340 2018-07-23 stsp free(blame->cb_args.commit_id);
2017 75c32340 2018-07-23 stsp blame->cb_args.commit_id = NULL;
2019 245d91c1 2018-07-12 stsp return err;
2022 245d91c1 2018-07-12 stsp static const struct got_error *
2023 7cc84d77 2018-08-01 stsp run_blame(struct tog_blame *blame, pthread_mutex_t *mutex,
2024 7cc84d77 2018-08-01 stsp struct tog_view *view, int *blame_complete,
2025 245d91c1 2018-07-12 stsp int *first_displayed_line, int *last_displayed_line,
2026 e5a0f69f 2018-08-18 stsp int *selected_line, int *done, int *eof, const char *path,
2027 245d91c1 2018-07-12 stsp struct got_object_id *commit_id,
2028 245d91c1 2018-07-12 stsp struct got_repository *repo)
2030 245d91c1 2018-07-12 stsp const struct got_error *err = NULL;
2031 84451b3e 2018-07-10 stsp struct got_blob_object *blob = NULL;
2032 245d91c1 2018-07-12 stsp struct got_repository *thread_repo = NULL;
2033 27d434c2 2018-09-15 stsp struct got_object_id *obj_id = NULL;
2034 27d434c2 2018-09-15 stsp struct got_object *obj = NULL;
2036 27d434c2 2018-09-15 stsp err = got_object_id_by_path(&obj_id, repo, commit_id, path);
2038 27d434c2 2018-09-15 stsp goto done;
2040 27d434c2 2018-09-15 stsp err = got_object_open(&obj, repo, obj_id);
2042 84451b3e 2018-07-10 stsp goto done;
2044 84451b3e 2018-07-10 stsp if (got_object_get_type(obj) != GOT_OBJ_TYPE_BLOB) {
2045 84451b3e 2018-07-10 stsp err = got_error(GOT_ERR_OBJ_TYPE);
2046 84451b3e 2018-07-10 stsp goto done;
2049 84451b3e 2018-07-10 stsp err = got_object_blob_open(&blob, repo, obj, 8192);
2051 a70480e0 2018-06-23 stsp goto done;
2052 245d91c1 2018-07-12 stsp blame->f = got_opentemp();
2053 245d91c1 2018-07-12 stsp if (blame->f == NULL) {
2054 84451b3e 2018-07-10 stsp err = got_error_from_errno();
2055 84451b3e 2018-07-10 stsp goto done;
2057 245d91c1 2018-07-12 stsp err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
2058 245d91c1 2018-07-12 stsp blame->f, blob);
2060 84451b3e 2018-07-10 stsp goto done;
2062 245d91c1 2018-07-12 stsp blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
2063 245d91c1 2018-07-12 stsp if (blame->lines == NULL) {
2064 84451b3e 2018-07-10 stsp err = got_error_from_errno();
2065 84451b3e 2018-07-10 stsp goto done;
2068 245d91c1 2018-07-12 stsp err = got_repo_open(&thread_repo, got_repo_get_path(repo));
2070 bd24772e 2018-07-11 stsp goto done;
2072 7cc84d77 2018-08-01 stsp blame->cb_args.view = view;
2073 245d91c1 2018-07-12 stsp blame->cb_args.lines = blame->lines;
2074 245d91c1 2018-07-12 stsp blame->cb_args.nlines = blame->nlines;
2075 245d91c1 2018-07-12 stsp blame->cb_args.mutex = mutex;
2076 245d91c1 2018-07-12 stsp blame->cb_args.commit_id = got_object_id_dup(commit_id);
2077 245d91c1 2018-07-12 stsp if (blame->cb_args.commit_id == NULL) {
2078 245d91c1 2018-07-12 stsp err = got_error_from_errno();
2079 245d91c1 2018-07-12 stsp goto done;
2081 245d91c1 2018-07-12 stsp blame->cb_args.f = blame->f;
2082 245d91c1 2018-07-12 stsp blame->cb_args.path = path;
2083 245d91c1 2018-07-12 stsp blame->cb_args.first_displayed_line = first_displayed_line;
2084 245d91c1 2018-07-12 stsp blame->cb_args.selected_line = selected_line;
2085 245d91c1 2018-07-12 stsp blame->cb_args.last_displayed_line = last_displayed_line;
2086 245d91c1 2018-07-12 stsp blame->cb_args.quit = done;
2087 e5a0f69f 2018-08-18 stsp blame->cb_args.eof = eof;
2089 245d91c1 2018-07-12 stsp blame->thread_args.path = path;
2090 245d91c1 2018-07-12 stsp blame->thread_args.repo = thread_repo;
2091 245d91c1 2018-07-12 stsp blame->thread_args.cb_args = &blame->cb_args;
2092 245d91c1 2018-07-12 stsp blame->thread_args.complete = blame_complete;
2093 245d91c1 2018-07-12 stsp *blame_complete = 0;
2095 245d91c1 2018-07-12 stsp if (pthread_create(&blame->thread, NULL, blame_thread,
2096 245d91c1 2018-07-12 stsp &blame->thread_args) != 0) {
2097 245d91c1 2018-07-12 stsp err = got_error_from_errno();
2098 245d91c1 2018-07-12 stsp goto done;
2103 245d91c1 2018-07-12 stsp got_object_blob_close(blob);
2104 27d434c2 2018-09-15 stsp free(obj_id);
2106 245d91c1 2018-07-12 stsp got_object_close(obj);
2108 245d91c1 2018-07-12 stsp stop_blame(blame);
2109 245d91c1 2018-07-12 stsp return err;
2112 245d91c1 2018-07-12 stsp static const struct got_error *
2113 e5a0f69f 2018-08-18 stsp open_blame_view(struct tog_view *view, char *path,
2114 e1cd8fed 2018-08-01 stsp struct got_object_id *commit_id, struct got_repository *repo)
2116 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL;
2117 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
2119 fb2756b9 2018-08-04 stsp SIMPLEQ_INIT(&s->blamed_commits);
2121 fb2756b9 2018-08-04 stsp if (pthread_mutex_init(&s->mutex, NULL) != 0)
2122 7cbe629d 2018-08-04 stsp return got_error_from_errno();
2124 fb2756b9 2018-08-04 stsp err = got_object_qid_alloc(&s->blamed_commit, commit_id);
2126 7cbe629d 2018-08-04 stsp return err;
2128 fb2756b9 2018-08-04 stsp SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
2129 fb2756b9 2018-08-04 stsp s->first_displayed_line = 1;
2130 fb2756b9 2018-08-04 stsp s->last_displayed_line = view->nlines;
2131 fb2756b9 2018-08-04 stsp s->selected_line = 1;
2132 fb2756b9 2018-08-04 stsp s->blame_complete = 0;
2133 fb2756b9 2018-08-04 stsp s->path = path;
2134 e5a0f69f 2018-08-18 stsp if (s->path == NULL)
2135 e5a0f69f 2018-08-18 stsp return got_error_from_errno();
2136 fb2756b9 2018-08-04 stsp s->repo = repo;
2137 fb2756b9 2018-08-04 stsp s->commit_id = commit_id;
2138 e9424729 2018-08-04 stsp memset(&s->blame, 0, sizeof(s->blame));
2140 e5a0f69f 2018-08-18 stsp view->show = show_blame_view;
2141 e5a0f69f 2018-08-18 stsp view->input = input_blame_view;
2142 e5a0f69f 2018-08-18 stsp view->close = close_blame_view;
2144 e5a0f69f 2018-08-18 stsp return run_blame(&s->blame, &s->mutex, view, &s->blame_complete,
2145 e5a0f69f 2018-08-18 stsp &s->first_displayed_line, &s->last_displayed_line,
2146 e5a0f69f 2018-08-18 stsp &s->selected_line, &s->done, &s->eof, s->path,
2147 e5a0f69f 2018-08-18 stsp s->blamed_commit->id, s->repo);
2150 e5a0f69f 2018-08-18 stsp static const struct got_error *
2151 7cbe629d 2018-08-04 stsp close_blame_view(struct tog_view *view)
2153 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
2154 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
2156 e5a0f69f 2018-08-18 stsp if (s->blame.thread)
2157 e5a0f69f 2018-08-18 stsp err = stop_blame(&s->blame);
2159 fb2756b9 2018-08-04 stsp while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
2160 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
2161 fb2756b9 2018-08-04 stsp blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
2162 fb2756b9 2018-08-04 stsp SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
2163 7cbe629d 2018-08-04 stsp got_object_qid_free(blamed_commit);
2166 e5a0f69f 2018-08-18 stsp free(s->path);
2168 e5a0f69f 2018-08-18 stsp return err;
2171 7cbe629d 2018-08-04 stsp static const struct got_error *
2172 7cbe629d 2018-08-04 stsp show_blame_view(struct tog_view *view)
2174 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
2175 e5a0f69f 2018-08-18 stsp struct tog_blame_view_state *s = &view->state.blame;
2177 e5a0f69f 2018-08-18 stsp if (pthread_mutex_lock(&s->mutex) != 0)
2178 e5a0f69f 2018-08-18 stsp return got_error_from_errno();
2180 e5a0f69f 2018-08-18 stsp err = draw_blame(view, s->blamed_commit->id, s->blame.f,
2181 e5a0f69f 2018-08-18 stsp s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
2182 e5a0f69f 2018-08-18 stsp s->selected_line, &s->first_displayed_line,
2183 e5a0f69f 2018-08-18 stsp &s->last_displayed_line, &s->eof, view->nlines);
2185 e5a0f69f 2018-08-18 stsp if (pthread_mutex_unlock(&s->mutex) != 0 && err == NULL)
2186 e5a0f69f 2018-08-18 stsp err = got_error_from_errno();
2188 e5a0f69f 2018-08-18 stsp return err;
2191 e5a0f69f 2018-08-18 stsp static const struct got_error *
2192 e5a0f69f 2018-08-18 stsp input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
2193 e5a0f69f 2018-08-18 stsp struct tog_view *view, int ch)
2195 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL, *thread_err = NULL;
2196 7cbe629d 2018-08-04 stsp struct got_object *obj = NULL, *pobj = NULL;
2197 7cbe629d 2018-08-04 stsp struct tog_view *diff_view;
2198 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
2200 e5a0f69f 2018-08-18 stsp if (pthread_mutex_lock(&s->mutex) != 0) {
2201 e5a0f69f 2018-08-18 stsp err = got_error_from_errno();
2202 e5a0f69f 2018-08-18 stsp goto done;
2205 e5a0f69f 2018-08-18 stsp switch (ch) {
2207 e5a0f69f 2018-08-18 stsp s->done = 1;
2208 e5a0f69f 2018-08-18 stsp if (pthread_mutex_unlock(&s->mutex) != 0) {
2209 e5a0f69f 2018-08-18 stsp err = got_error_from_errno();
2210 e5a0f69f 2018-08-18 stsp goto done;
2212 e5a0f69f 2018-08-18 stsp return stop_blame(&s->blame);
2214 e5a0f69f 2018-08-18 stsp case KEY_UP:
2215 e5a0f69f 2018-08-18 stsp if (s->selected_line > 1)
2216 e5a0f69f 2018-08-18 stsp s->selected_line--;
2217 e5a0f69f 2018-08-18 stsp else if (s->selected_line == 1 &&
2218 e5a0f69f 2018-08-18 stsp s->first_displayed_line > 1)
2219 e5a0f69f 2018-08-18 stsp s->first_displayed_line--;
2221 e5a0f69f 2018-08-18 stsp case KEY_PPAGE:
2222 e5a0f69f 2018-08-18 stsp if (s->first_displayed_line == 1) {
2223 e5a0f69f 2018-08-18 stsp s->selected_line = 1;
2226 e5a0f69f 2018-08-18 stsp if (s->first_displayed_line > view->nlines - 2)
2227 e5a0f69f 2018-08-18 stsp s->first_displayed_line -=
2228 e5a0f69f 2018-08-18 stsp (view->nlines - 2);
2230 e5a0f69f 2018-08-18 stsp s->first_displayed_line = 1;
2233 e5a0f69f 2018-08-18 stsp case KEY_DOWN:
2234 e5a0f69f 2018-08-18 stsp if (s->selected_line < view->nlines - 2 &&
2235 e5a0f69f 2018-08-18 stsp s->first_displayed_line +
2236 e5a0f69f 2018-08-18 stsp s->selected_line <= s->blame.nlines)
2237 e5a0f69f 2018-08-18 stsp s->selected_line++;
2238 e5a0f69f 2018-08-18 stsp else if (s->last_displayed_line <
2239 e5a0f69f 2018-08-18 stsp s->blame.nlines)
2240 e5a0f69f 2018-08-18 stsp s->first_displayed_line++;
2243 e5a0f69f 2018-08-18 stsp case 'p': {
2244 e5a0f69f 2018-08-18 stsp struct got_object_id *id;
2245 e5a0f69f 2018-08-18 stsp id = get_selected_commit_id(s->blame.lines,
2246 e5a0f69f 2018-08-18 stsp s->first_displayed_line, s->selected_line);
2247 e5a0f69f 2018-08-18 stsp if (id == NULL || got_object_id_cmp(id,
2248 e5a0f69f 2018-08-18 stsp s->blamed_commit->id) == 0)
2250 e5a0f69f 2018-08-18 stsp err = open_selected_commit(&pobj, &obj,
2251 e5a0f69f 2018-08-18 stsp s->blame.lines, s->first_displayed_line,
2252 e5a0f69f 2018-08-18 stsp s->selected_line, s->repo);
2255 e5a0f69f 2018-08-18 stsp if (pobj == NULL && obj == NULL)
2257 e5a0f69f 2018-08-18 stsp if (ch == 'p' && pobj == NULL)
2259 e5a0f69f 2018-08-18 stsp s->done = 1;
2260 e5a0f69f 2018-08-18 stsp if (pthread_mutex_unlock(&s->mutex) != 0) {
2261 e5a0f69f 2018-08-18 stsp err = got_error_from_errno();
2262 e5a0f69f 2018-08-18 stsp goto done;
2264 e5a0f69f 2018-08-18 stsp thread_err = stop_blame(&s->blame);
2265 e5a0f69f 2018-08-18 stsp s->done = 0;
2266 e5a0f69f 2018-08-18 stsp if (pthread_mutex_lock(&s->mutex) != 0) {
2267 e5a0f69f 2018-08-18 stsp err = got_error_from_errno();
2268 e5a0f69f 2018-08-18 stsp goto done;
2270 e5a0f69f 2018-08-18 stsp if (thread_err)
2272 e5a0f69f 2018-08-18 stsp id = got_object_get_id(ch == 'b' ? obj : pobj);
2273 e5a0f69f 2018-08-18 stsp got_object_close(obj);
2274 e5a0f69f 2018-08-18 stsp obj = NULL;
2275 e5a0f69f 2018-08-18 stsp if (pobj) {
2276 e5a0f69f 2018-08-18 stsp got_object_close(pobj);
2277 e5a0f69f 2018-08-18 stsp pobj = NULL;
2279 6402fb3c 2018-09-15 stsp err = got_object_qid_alloc(&s->blamed_commit, id);
2281 e5a0f69f 2018-08-18 stsp goto done;
2282 e5a0f69f 2018-08-18 stsp SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
2283 e5a0f69f 2018-08-18 stsp s->blamed_commit, entry);
2284 e5a0f69f 2018-08-18 stsp err = run_blame(&s->blame, &s->mutex, view,
2285 e5a0f69f 2018-08-18 stsp &s->blame_complete,
2286 e5a0f69f 2018-08-18 stsp &s->first_displayed_line,
2287 e5a0f69f 2018-08-18 stsp &s->last_displayed_line,
2288 e5a0f69f 2018-08-18 stsp &s->selected_line, &s->done, &s->eof,
2289 e5a0f69f 2018-08-18 stsp s->path, s->blamed_commit->id, s->repo);
2294 e5a0f69f 2018-08-18 stsp case 'B': {
2295 e5a0f69f 2018-08-18 stsp struct got_object_qid *first;
2296 e5a0f69f 2018-08-18 stsp first = SIMPLEQ_FIRST(&s->blamed_commits);
2297 e5a0f69f 2018-08-18 stsp if (!got_object_id_cmp(first->id, s->commit_id))
2299 e5a0f69f 2018-08-18 stsp s->done = 1;
2300 e5a0f69f 2018-08-18 stsp if (pthread_mutex_unlock(&s->mutex) != 0) {
2301 e5a0f69f 2018-08-18 stsp err = got_error_from_errno();
2302 e5a0f69f 2018-08-18 stsp goto done;
2304 e5a0f69f 2018-08-18 stsp thread_err = stop_blame(&s->blame);
2305 e5a0f69f 2018-08-18 stsp s->done = 0;
2306 e5a0f69f 2018-08-18 stsp if (pthread_mutex_lock(&s->mutex) != 0) {
2307 e5a0f69f 2018-08-18 stsp err = got_error_from_errno();
2308 e5a0f69f 2018-08-18 stsp goto done;
2310 e5a0f69f 2018-08-18 stsp if (thread_err)
2312 e5a0f69f 2018-08-18 stsp SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
2313 e5a0f69f 2018-08-18 stsp got_object_qid_free(s->blamed_commit);
2314 e5a0f69f 2018-08-18 stsp s->blamed_commit =
2315 e5a0f69f 2018-08-18 stsp SIMPLEQ_FIRST(&s->blamed_commits);
2316 e5a0f69f 2018-08-18 stsp err = run_blame(&s->blame, &s->mutex, view,
2317 e5a0f69f 2018-08-18 stsp &s->blame_complete,
2318 e5a0f69f 2018-08-18 stsp &s->first_displayed_line,
2319 e5a0f69f 2018-08-18 stsp &s->last_displayed_line,
2320 e5a0f69f 2018-08-18 stsp &s->selected_line, &s->done, &s->eof, s->path,
2321 e5a0f69f 2018-08-18 stsp s->blamed_commit->id, s->repo);
2326 e5a0f69f 2018-08-18 stsp case KEY_ENTER:
2327 e5a0f69f 2018-08-18 stsp case '\r':
2328 e5a0f69f 2018-08-18 stsp err = open_selected_commit(&pobj, &obj,
2329 e5a0f69f 2018-08-18 stsp s->blame.lines, s->first_displayed_line,
2330 e5a0f69f 2018-08-18 stsp s->selected_line, s->repo);
2333 e5a0f69f 2018-08-18 stsp if (pobj == NULL && obj == NULL)
2335 e5a0f69f 2018-08-18 stsp diff_view = view_open(0, 0, 0, 0, view,
2336 e5a0f69f 2018-08-18 stsp TOG_VIEW_DIFF);
2337 e5a0f69f 2018-08-18 stsp if (diff_view == NULL) {
2338 e5a0f69f 2018-08-18 stsp err = got_error_from_errno();
2341 e5a0f69f 2018-08-18 stsp err = open_diff_view(diff_view, pobj, obj,
2343 e5a0f69f 2018-08-18 stsp if (err) {
2344 e5a0f69f 2018-08-18 stsp view_close(diff_view);
2347 e5a0f69f 2018-08-18 stsp *new_view = diff_view;
2348 e5a0f69f 2018-08-18 stsp if (pobj) {
2349 e5a0f69f 2018-08-18 stsp got_object_close(pobj);
2350 e5a0f69f 2018-08-18 stsp pobj = NULL;
2352 e5a0f69f 2018-08-18 stsp got_object_close(obj);
2353 e5a0f69f 2018-08-18 stsp obj = NULL;
2357 e5a0f69f 2018-08-18 stsp case KEY_NPAGE:
2359 e5a0f69f 2018-08-18 stsp if (s->last_displayed_line >= s->blame.nlines &&
2360 e5a0f69f 2018-08-18 stsp s->selected_line < view->nlines - 2) {
2361 e5a0f69f 2018-08-18 stsp s->selected_line = MIN(s->blame.nlines,
2362 e5a0f69f 2018-08-18 stsp view->nlines - 2);
2365 e5a0f69f 2018-08-18 stsp if (s->last_displayed_line + view->nlines - 2
2366 e5a0f69f 2018-08-18 stsp <= s->blame.nlines)
2367 e5a0f69f 2018-08-18 stsp s->first_displayed_line +=
2368 e5a0f69f 2018-08-18 stsp view->nlines - 2;
2370 e5a0f69f 2018-08-18 stsp s->first_displayed_line =
2371 e5a0f69f 2018-08-18 stsp s->blame.nlines -
2372 e5a0f69f 2018-08-18 stsp (view->nlines - 3);
2374 e5a0f69f 2018-08-18 stsp case KEY_RESIZE:
2375 e5a0f69f 2018-08-18 stsp if (s->selected_line > view->nlines - 2) {
2376 e5a0f69f 2018-08-18 stsp s->selected_line = MIN(s->blame.nlines,
2377 e5a0f69f 2018-08-18 stsp view->nlines - 2);
2384 e5a0f69f 2018-08-18 stsp if (pthread_mutex_unlock(&s->mutex) != 0)
2385 e5a0f69f 2018-08-18 stsp err = got_error_from_errno();
2388 b880a918 2018-07-10 stsp got_object_close(pobj);
2389 245d91c1 2018-07-12 stsp return thread_err ? thread_err : err;
2392 a70480e0 2018-06-23 stsp static const struct got_error *
2393 9f7d7167 2018-04-29 stsp cmd_blame(int argc, char *argv[])
2395 a70480e0 2018-06-23 stsp const struct got_error *error;
2396 a70480e0 2018-06-23 stsp struct got_repository *repo = NULL;
2397 69069811 2018-08-02 stsp char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2398 a70480e0 2018-06-23 stsp struct got_object_id *commit_id = NULL;
2399 a70480e0 2018-06-23 stsp char *commit_id_str = NULL;
2401 e1cd8fed 2018-08-01 stsp struct tog_view *view;
2403 a70480e0 2018-06-23 stsp #ifndef PROFILE
2404 ad242220 2018-09-08 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd", NULL)
2406 a70480e0 2018-06-23 stsp err(1, "pledge");
2409 69069811 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2410 a70480e0 2018-06-23 stsp switch (ch) {
2412 a70480e0 2018-06-23 stsp commit_id_str = optarg;
2415 69069811 2018-08-02 stsp repo_path = realpath(optarg, NULL);
2416 69069811 2018-08-02 stsp if (repo_path == NULL)
2417 69069811 2018-08-02 stsp err(1, "-r option");
2421 a70480e0 2018-06-23 stsp /* NOTREACHED */
2425 a70480e0 2018-06-23 stsp argc -= optind;
2426 a70480e0 2018-06-23 stsp argv += optind;
2428 69069811 2018-08-02 stsp if (argc == 1)
2429 69069811 2018-08-02 stsp path = argv[0];
2431 a70480e0 2018-06-23 stsp usage_blame();
2433 69069811 2018-08-02 stsp cwd = getcwd(NULL, 0);
2434 69069811 2018-08-02 stsp if (cwd == NULL) {
2435 69069811 2018-08-02 stsp error = got_error_from_errno();
2436 69069811 2018-08-02 stsp goto done;
2438 69069811 2018-08-02 stsp if (repo_path == NULL) {
2439 69069811 2018-08-02 stsp repo_path = strdup(cwd);
2440 69069811 2018-08-02 stsp if (repo_path == NULL) {
2441 69069811 2018-08-02 stsp error = got_error_from_errno();
2442 69069811 2018-08-02 stsp goto done;
2447 69069811 2018-08-02 stsp error = got_repo_open(&repo, repo_path);
2448 a70480e0 2018-06-23 stsp if (error != NULL)
2449 66b4983c 2018-06-23 stsp return error;
2451 69069811 2018-08-02 stsp error = got_repo_map_path(&in_repo_path, repo, path);
2452 69069811 2018-08-02 stsp if (error != NULL)
2453 69069811 2018-08-02 stsp goto done;
2455 a70480e0 2018-06-23 stsp if (commit_id_str == NULL) {
2456 a70480e0 2018-06-23 stsp struct got_reference *head_ref;
2457 a70480e0 2018-06-23 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
2458 a70480e0 2018-06-23 stsp if (error != NULL)
2459 66b4983c 2018-06-23 stsp goto done;
2460 a70480e0 2018-06-23 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
2461 a70480e0 2018-06-23 stsp got_ref_close(head_ref);
2463 a70480e0 2018-06-23 stsp struct got_object *obj;
2464 a70480e0 2018-06-23 stsp error = got_object_open_by_id_str(&obj, repo, commit_id_str);
2465 a70480e0 2018-06-23 stsp if (error != NULL)
2466 66b4983c 2018-06-23 stsp goto done;
2467 6402fb3c 2018-09-15 stsp commit_id = got_object_id_dup(got_object_get_id(obj));
2468 a19e88aa 2018-06-23 stsp if (commit_id == NULL)
2469 66b4983c 2018-06-23 stsp error = got_error_from_errno();
2470 a19e88aa 2018-06-23 stsp got_object_close(obj);
2472 a19e88aa 2018-06-23 stsp if (error != NULL)
2473 a19e88aa 2018-06-23 stsp goto done;
2475 b3665f43 2018-08-04 stsp view = view_open(0, 0, 0, 0, NULL, TOG_VIEW_BLAME);
2476 e1cd8fed 2018-08-01 stsp if (view == NULL) {
2477 e1cd8fed 2018-08-01 stsp error = got_error_from_errno();
2478 e1cd8fed 2018-08-01 stsp goto done;
2480 7cbe629d 2018-08-04 stsp error = open_blame_view(view, in_repo_path, commit_id, repo);
2481 7cbe629d 2018-08-04 stsp if (error)
2482 7cbe629d 2018-08-04 stsp goto done;
2483 e5a0f69f 2018-08-18 stsp error = view_loop(view);
2485 69069811 2018-08-02 stsp free(repo_path);
2486 69069811 2018-08-02 stsp free(cwd);
2487 a70480e0 2018-06-23 stsp free(commit_id);
2489 a70480e0 2018-06-23 stsp got_repo_close(repo);
2490 a70480e0 2018-06-23 stsp return error;
2493 ffd1d5e5 2018-06-23 stsp static const struct got_error *
2494 f7d12f7e 2018-08-01 stsp draw_tree_entries(struct tog_view *view,
2495 f7d12f7e 2018-08-01 stsp struct got_tree_entry **first_displayed_entry,
2496 ffd1d5e5 2018-06-23 stsp struct got_tree_entry **last_displayed_entry,
2497 ffd1d5e5 2018-06-23 stsp struct got_tree_entry **selected_entry, int *ndisplayed,
2498 f7d12f7e 2018-08-01 stsp const char *label, int show_ids, const char *parent_path,
2499 ce52c690 2018-06-23 stsp const struct got_tree_entries *entries, int selected, int limit, int isroot)
2501 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
2502 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *te;
2503 ffd1d5e5 2018-06-23 stsp wchar_t *wline;
2504 ffd1d5e5 2018-06-23 stsp int width, n;
2506 ffd1d5e5 2018-06-23 stsp *ndisplayed = 0;
2508 f7d12f7e 2018-08-01 stsp werase(view->window);
2510 ffd1d5e5 2018-06-23 stsp if (limit == 0)
2511 ffd1d5e5 2018-06-23 stsp return NULL;
2513 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, label, view->ncols);
2515 ffd1d5e5 2018-06-23 stsp return err;
2516 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2517 a3404814 2018-09-02 stsp wstandout(view->window);
2518 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
2519 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2520 a3404814 2018-09-02 stsp wstandend(view->window);
2521 2550e4c3 2018-07-13 stsp free(wline);
2522 2550e4c3 2018-07-13 stsp wline = NULL;
2523 f7d12f7e 2018-08-01 stsp if (width < view->ncols)
2524 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
2525 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
2526 ffd1d5e5 2018-06-23 stsp return NULL;
2527 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, parent_path, view->ncols);
2529 ce52c690 2018-06-23 stsp return err;
2530 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
2531 2550e4c3 2018-07-13 stsp free(wline);
2532 2550e4c3 2018-07-13 stsp wline = NULL;
2533 f7d12f7e 2018-08-01 stsp if (width < view->ncols)
2534 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
2535 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
2536 ffd1d5e5 2018-06-23 stsp return NULL;
2537 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
2538 a1eca9bb 2018-06-23 stsp if (--limit <= 0)
2539 a1eca9bb 2018-06-23 stsp return NULL;
2541 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_FIRST(&entries->head);
2542 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry == NULL) {
2543 ffd1d5e5 2018-06-23 stsp if (selected == 0) {
2544 f7d12f7e 2018-08-01 stsp wstandout(view->window);
2545 ffd1d5e5 2018-06-23 stsp *selected_entry = NULL;
2547 f7d12f7e 2018-08-01 stsp waddstr(view->window, " ..\n"); /* parent directory */
2548 ffd1d5e5 2018-06-23 stsp if (selected == 0)
2549 f7d12f7e 2018-08-01 stsp wstandend(view->window);
2550 ffd1d5e5 2018-06-23 stsp (*ndisplayed)++;
2551 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
2552 ffd1d5e5 2018-06-23 stsp return NULL;
2556 ffd1d5e5 2018-06-23 stsp while (te != *first_displayed_entry)
2557 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_NEXT(te, entry);
2560 ffd1d5e5 2018-06-23 stsp while (te) {
2561 1d13200f 2018-07-12 stsp char *line = NULL, *id_str = NULL;
2563 1d13200f 2018-07-12 stsp if (show_ids) {
2564 1d13200f 2018-07-12 stsp err = got_object_id_str(&id_str, te->id);
2566 1d13200f 2018-07-12 stsp return got_error_from_errno();
2568 1d13200f 2018-07-12 stsp if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
2569 1d13200f 2018-07-12 stsp te->name, S_ISDIR(te->mode) ? "/" : "") == -1) {
2570 1d13200f 2018-07-12 stsp free(id_str);
2571 ffd1d5e5 2018-06-23 stsp return got_error_from_errno();
2573 1d13200f 2018-07-12 stsp free(id_str);
2574 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, line, view->ncols);
2575 ffd1d5e5 2018-06-23 stsp if (err) {
2576 ffd1d5e5 2018-06-23 stsp free(line);
2579 ffd1d5e5 2018-06-23 stsp if (n == selected) {
2580 f7d12f7e 2018-08-01 stsp wstandout(view->window);
2581 ffd1d5e5 2018-06-23 stsp *selected_entry = te;
2583 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
2584 f7d12f7e 2018-08-01 stsp if (width < view->ncols)
2585 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
2586 ffd1d5e5 2018-06-23 stsp if (n == selected)
2587 f7d12f7e 2018-08-01 stsp wstandend(view->window);
2588 ffd1d5e5 2018-06-23 stsp free(line);
2589 2550e4c3 2018-07-13 stsp free(wline);
2590 2550e4c3 2018-07-13 stsp wline = NULL;
2592 ffd1d5e5 2018-06-23 stsp (*ndisplayed)++;
2593 ffd1d5e5 2018-06-23 stsp *last_displayed_entry = te;
2594 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
2596 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_NEXT(te, entry);
2599 1a57306a 2018-09-02 stsp view_vborder(view);
2600 ffd1d5e5 2018-06-23 stsp return err;
2603 ffd1d5e5 2018-06-23 stsp static void
2604 ffd1d5e5 2018-06-23 stsp tree_scroll_up(struct got_tree_entry **first_displayed_entry, int maxscroll,
2605 ffd1d5e5 2018-06-23 stsp const struct got_tree_entries *entries, int isroot)
2607 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *te, *prev;
2610 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry == NULL)
2613 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_FIRST(&entries->head);
2614 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry == te) {
2615 ffd1d5e5 2018-06-23 stsp if (!isroot)
2616 ffd1d5e5 2018-06-23 stsp *first_displayed_entry = NULL;
2620 ffd1d5e5 2018-06-23 stsp /* XXX this is stupid... switch to TAILQ? */
2621 ffd1d5e5 2018-06-23 stsp for (i = 0; i < maxscroll; i++) {
2622 ffd1d5e5 2018-06-23 stsp while (te != *first_displayed_entry) {
2623 ffd1d5e5 2018-06-23 stsp prev = te;
2624 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_NEXT(te, entry);
2626 ffd1d5e5 2018-06-23 stsp *first_displayed_entry = prev;
2627 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_FIRST(&entries->head);
2629 ffd1d5e5 2018-06-23 stsp if (!isroot && te == SIMPLEQ_FIRST(&entries->head) && i < maxscroll)
2630 ffd1d5e5 2018-06-23 stsp *first_displayed_entry = NULL;
2633 ffd1d5e5 2018-06-23 stsp static void
2634 ffd1d5e5 2018-06-23 stsp tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
2635 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *last_displayed_entry,
2636 ffd1d5e5 2018-06-23 stsp const struct got_tree_entries *entries)
2638 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *next;
2639 ffd1d5e5 2018-06-23 stsp int n = 0;
2641 ffd1d5e5 2018-06-23 stsp if (SIMPLEQ_NEXT(last_displayed_entry, entry) == NULL)
2644 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry)
2645 ffd1d5e5 2018-06-23 stsp next = SIMPLEQ_NEXT(*first_displayed_entry, entry);
2647 ffd1d5e5 2018-06-23 stsp next = SIMPLEQ_FIRST(&entries->head);
2648 ffd1d5e5 2018-06-23 stsp while (next) {
2649 ffd1d5e5 2018-06-23 stsp *first_displayed_entry = next;
2650 ffd1d5e5 2018-06-23 stsp if (++n >= maxscroll)
2652 ffd1d5e5 2018-06-23 stsp next = SIMPLEQ_NEXT(next, entry);
2656 ffd1d5e5 2018-06-23 stsp static const struct got_error *
2657 ce52c690 2018-06-23 stsp tree_entry_path(char **path, struct tog_parent_trees *parents,
2658 ce52c690 2018-06-23 stsp struct got_tree_entry *te)
2660 cb2ebc8a 2018-06-23 stsp const struct got_error *err = NULL;
2661 ffd1d5e5 2018-06-23 stsp struct tog_parent_tree *pt;
2662 ffd1d5e5 2018-06-23 stsp size_t len = 2; /* for leading slash and NUL */
2664 d9765a41 2018-06-23 stsp TAILQ_FOREACH(pt, parents, entry)
2665 ffd1d5e5 2018-06-23 stsp len += strlen(pt->selected_entry->name) + 1 /* slash */;
2667 ce52c690 2018-06-23 stsp len += strlen(te->name);
2669 ce52c690 2018-06-23 stsp *path = calloc(1, len);
2670 ffd1d5e5 2018-06-23 stsp if (path == NULL)
2671 ffd1d5e5 2018-06-23 stsp return got_error_from_errno();
2673 ce52c690 2018-06-23 stsp (*path)[0] = '/';
2674 d9765a41 2018-06-23 stsp pt = TAILQ_LAST(parents, tog_parent_trees);
2675 d9765a41 2018-06-23 stsp while (pt) {
2676 ce52c690 2018-06-23 stsp if (strlcat(*path, pt->selected_entry->name, len) >= len) {
2677 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
2678 cb2ebc8a 2018-06-23 stsp goto done;
2680 ce52c690 2018-06-23 stsp if (strlcat(*path, "/", len) >= len) {
2681 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
2682 cb2ebc8a 2018-06-23 stsp goto done;
2684 d9765a41 2018-06-23 stsp pt = TAILQ_PREV(pt, tog_parent_trees, entry);
2687 ce52c690 2018-06-23 stsp if (strlcat(*path, te->name, len) >= len) {
2688 ce52c690 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
2689 ce52c690 2018-06-23 stsp goto done;
2693 ce52c690 2018-06-23 stsp if (err) {
2694 ce52c690 2018-06-23 stsp free(*path);
2695 ce52c690 2018-06-23 stsp *path = NULL;
2697 ce52c690 2018-06-23 stsp return err;
2700 ce52c690 2018-06-23 stsp static const struct got_error *
2701 e5a0f69f 2018-08-18 stsp blame_tree_entry(struct tog_view **new_view, struct tog_view *parent_view,
2702 e5a0f69f 2018-08-18 stsp struct got_tree_entry *te, struct tog_parent_trees *parents,
2703 e5a0f69f 2018-08-18 stsp struct got_object_id *commit_id, struct got_repository *repo)
2705 ce52c690 2018-06-23 stsp const struct got_error *err = NULL;
2706 ce52c690 2018-06-23 stsp char *path;
2707 e5a0f69f 2018-08-18 stsp struct tog_view *blame_view;
2709 ce52c690 2018-06-23 stsp err = tree_entry_path(&path, parents, te);
2711 ce52c690 2018-06-23 stsp return err;
2713 e5a0f69f 2018-08-18 stsp blame_view = view_open(0, 0, 0, 0, parent_view, TOG_VIEW_BLAME);
2714 e5a0f69f 2018-08-18 stsp if (blame_view == NULL)
2715 e5a0f69f 2018-08-18 stsp return got_error_from_errno();
2717 e5a0f69f 2018-08-18 stsp err = open_blame_view(blame_view, path, commit_id, repo);
2718 e5a0f69f 2018-08-18 stsp if (err) {
2719 e5a0f69f 2018-08-18 stsp view_close(blame_view);
2720 e5a0f69f 2018-08-18 stsp free(path);
2722 e5a0f69f 2018-08-18 stsp *new_view = blame_view;
2723 69efd4c4 2018-07-18 stsp return err;
2726 69efd4c4 2018-07-18 stsp static const struct got_error *
2727 e5a0f69f 2018-08-18 stsp log_tree_entry(struct tog_view **new_view, struct tog_view *parent_view,
2728 e5a0f69f 2018-08-18 stsp struct got_tree_entry *te, struct tog_parent_trees *parents,
2729 e5a0f69f 2018-08-18 stsp struct got_object_id *commit_id, struct got_repository *repo)
2731 e5a0f69f 2018-08-18 stsp struct tog_view *log_view;
2732 69efd4c4 2018-07-18 stsp const struct got_error *err = NULL;
2733 69efd4c4 2018-07-18 stsp char *path;
2735 e5a0f69f 2018-08-18 stsp log_view = view_open(0, 0, 0, 0, parent_view, TOG_VIEW_LOG);
2736 e5a0f69f 2018-08-18 stsp if (log_view == NULL)
2737 e5a0f69f 2018-08-18 stsp return got_error_from_errno();
2739 69efd4c4 2018-07-18 stsp err = tree_entry_path(&path, parents, te);
2741 69efd4c4 2018-07-18 stsp return err;
2743 e5a0f69f 2018-08-18 stsp err = open_log_view(log_view, commit_id, repo, path);
2745 e5a0f69f 2018-08-18 stsp view_close(log_view);
2747 e5a0f69f 2018-08-18 stsp *new_view = log_view;
2748 cb2ebc8a 2018-06-23 stsp free(path);
2749 cb2ebc8a 2018-06-23 stsp return err;
2752 ffd1d5e5 2018-06-23 stsp static const struct got_error *
2753 ad80ab7b 2018-08-04 stsp open_tree_view(struct tog_view *view, struct got_tree_object *root,
2754 5221c383 2018-08-01 stsp struct got_object_id *commit_id, struct got_repository *repo)
2756 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
2757 ad80ab7b 2018-08-04 stsp char *commit_id_str = NULL;
2758 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
2760 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->parents);
2762 ffd1d5e5 2018-06-23 stsp err = got_object_id_str(&commit_id_str, commit_id);
2763 ffd1d5e5 2018-06-23 stsp if (err != NULL)
2764 ffd1d5e5 2018-06-23 stsp goto done;
2766 fb2756b9 2018-08-04 stsp if (asprintf(&s->tree_label, "commit: %s", commit_id_str) == -1) {
2767 ffd1d5e5 2018-06-23 stsp err = got_error_from_errno();
2768 ffd1d5e5 2018-06-23 stsp goto done;
2771 fb2756b9 2018-08-04 stsp s->root = s->tree = root;
2772 fb2756b9 2018-08-04 stsp s->entries = got_object_tree_get_entries(root);
2773 fb2756b9 2018-08-04 stsp s->first_displayed_entry = SIMPLEQ_FIRST(&s->entries->head);
2774 fb2756b9 2018-08-04 stsp s->commit_id = commit_id;
2775 fb2756b9 2018-08-04 stsp s->repo = repo;
2777 e5a0f69f 2018-08-18 stsp view->show = show_tree_view;
2778 e5a0f69f 2018-08-18 stsp view->input = input_tree_view;
2779 e5a0f69f 2018-08-18 stsp view->close = close_tree_view;
2781 ad80ab7b 2018-08-04 stsp free(commit_id_str);
2783 fb2756b9 2018-08-04 stsp free(s->tree_label);
2784 ad80ab7b 2018-08-04 stsp return err;
2787 e5a0f69f 2018-08-18 stsp static const struct got_error *
2788 ad80ab7b 2018-08-04 stsp close_tree_view(struct tog_view *view)
2790 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
2792 fb2756b9 2018-08-04 stsp free(s->tree_label);
2793 fb2756b9 2018-08-04 stsp while (!TAILQ_EMPTY(&s->parents)) {
2794 ad80ab7b 2018-08-04 stsp struct tog_parent_tree *parent;
2795 fb2756b9 2018-08-04 stsp parent = TAILQ_FIRST(&s->parents);
2796 fb2756b9 2018-08-04 stsp TAILQ_REMOVE(&s->parents, parent, entry);
2797 ad80ab7b 2018-08-04 stsp free(parent);
2800 fb2756b9 2018-08-04 stsp if (s->tree != s->root)
2801 fb2756b9 2018-08-04 stsp got_object_tree_close(s->tree);
2802 e5a0f69f 2018-08-18 stsp got_object_tree_close(s->root);
2804 e5a0f69f 2018-08-18 stsp return NULL;
2807 ad80ab7b 2018-08-04 stsp static const struct got_error *
2808 ad80ab7b 2018-08-04 stsp show_tree_view(struct tog_view *view)
2810 ad80ab7b 2018-08-04 stsp const struct got_error *err = NULL;
2811 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
2812 e5a0f69f 2018-08-18 stsp char *parent_path;
2814 e5a0f69f 2018-08-18 stsp err = tree_entry_path(&parent_path, &s->parents, NULL);
2816 e5a0f69f 2018-08-18 stsp return err;
2818 e5a0f69f 2018-08-18 stsp err = draw_tree_entries(view, &s->first_displayed_entry,
2819 e5a0f69f 2018-08-18 stsp &s->last_displayed_entry, &s->selected_entry,
2820 e5a0f69f 2018-08-18 stsp &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
2821 e5a0f69f 2018-08-18 stsp s->entries, s->selected, view->nlines, s->tree == s->root);
2822 e5a0f69f 2018-08-18 stsp free(parent_path);
2823 e5a0f69f 2018-08-18 stsp return err;
2826 e5a0f69f 2018-08-18 stsp static const struct got_error *
2827 e5a0f69f 2018-08-18 stsp input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
2828 e5a0f69f 2018-08-18 stsp struct tog_view *view, int ch)
2830 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
2831 e5a0f69f 2018-08-18 stsp struct tog_tree_view_state *s = &view->state.tree;
2833 e5a0f69f 2018-08-18 stsp switch (ch) {
2835 e5a0f69f 2018-08-18 stsp s->show_ids = !s->show_ids;
2838 e5a0f69f 2018-08-18 stsp if (s->selected_entry) {
2839 e5a0f69f 2018-08-18 stsp err = log_tree_entry(new_view, view,
2840 e5a0f69f 2018-08-18 stsp s->selected_entry, &s->parents,
2841 e5a0f69f 2018-08-18 stsp s->commit_id, s->repo);
2845 e5a0f69f 2018-08-18 stsp case KEY_UP:
2846 e5a0f69f 2018-08-18 stsp if (s->selected > 0)
2847 e5a0f69f 2018-08-18 stsp s->selected--;
2848 e5a0f69f 2018-08-18 stsp if (s->selected > 0)
2850 e5a0f69f 2018-08-18 stsp tree_scroll_up(&s->first_displayed_entry, 1,
2851 e5a0f69f 2018-08-18 stsp s->entries, s->tree == s->root);
2853 e5a0f69f 2018-08-18 stsp case KEY_PPAGE:
2854 e5a0f69f 2018-08-18 stsp if (SIMPLEQ_FIRST(&s->entries->head) ==
2855 e5a0f69f 2018-08-18 stsp s->first_displayed_entry) {
2856 e5a0f69f 2018-08-18 stsp if (s->tree != s->root)
2857 e5a0f69f 2018-08-18 stsp s->first_displayed_entry = NULL;
2858 e5a0f69f 2018-08-18 stsp s->selected = 0;
2861 e5a0f69f 2018-08-18 stsp tree_scroll_up(&s->first_displayed_entry,
2862 e5a0f69f 2018-08-18 stsp view->nlines, s->entries,
2863 e5a0f69f 2018-08-18 stsp s->tree == s->root);
2866 e5a0f69f 2018-08-18 stsp case KEY_DOWN:
2867 e5a0f69f 2018-08-18 stsp if (s->selected < s->ndisplayed - 1) {
2868 e5a0f69f 2018-08-18 stsp s->selected++;
2871 e5a0f69f 2018-08-18 stsp tree_scroll_down(&s->first_displayed_entry, 1,
2872 e5a0f69f 2018-08-18 stsp s->last_displayed_entry, s->entries);
2874 e5a0f69f 2018-08-18 stsp case KEY_NPAGE:
2875 e5a0f69f 2018-08-18 stsp tree_scroll_down(&s->first_displayed_entry,
2876 e5a0f69f 2018-08-18 stsp view->nlines, s->last_displayed_entry,
2877 e5a0f69f 2018-08-18 stsp s->entries);
2878 e5a0f69f 2018-08-18 stsp if (SIMPLEQ_NEXT(s->last_displayed_entry,
2881 e5a0f69f 2018-08-18 stsp /* can't scroll any further; move cursor down */
2882 e5a0f69f 2018-08-18 stsp if (s->selected < s->ndisplayed - 1)
2883 e5a0f69f 2018-08-18 stsp s->selected = s->ndisplayed - 1;
2885 e5a0f69f 2018-08-18 stsp case KEY_ENTER:
2886 e5a0f69f 2018-08-18 stsp case '\r':
2887 e5a0f69f 2018-08-18 stsp if (s->selected_entry == NULL) {
2888 e5a0f69f 2018-08-18 stsp struct tog_parent_tree *parent;
2889 31607d6c 2018-08-18 stsp case KEY_LEFT:
2890 e5a0f69f 2018-08-18 stsp /* user selected '..' */
2891 e5a0f69f 2018-08-18 stsp if (s->tree == s->root)
2893 e5a0f69f 2018-08-18 stsp parent = TAILQ_FIRST(&s->parents);
2894 e5a0f69f 2018-08-18 stsp TAILQ_REMOVE(&s->parents, parent,
2896 e5a0f69f 2018-08-18 stsp got_object_tree_close(s->tree);
2897 e5a0f69f 2018-08-18 stsp s->tree = parent->tree;
2898 e5a0f69f 2018-08-18 stsp s->entries =
2899 e5a0f69f 2018-08-18 stsp got_object_tree_get_entries(s->tree);
2900 e5a0f69f 2018-08-18 stsp s->first_displayed_entry =
2901 e5a0f69f 2018-08-18 stsp parent->first_displayed_entry;
2902 e5a0f69f 2018-08-18 stsp s->selected_entry =
2903 e5a0f69f 2018-08-18 stsp parent->selected_entry;
2904 e5a0f69f 2018-08-18 stsp s->selected = parent->selected;
2905 e5a0f69f 2018-08-18 stsp free(parent);
2906 e5a0f69f 2018-08-18 stsp } else if (S_ISDIR(s->selected_entry->mode)) {
2907 e5a0f69f 2018-08-18 stsp struct tog_parent_tree *parent;
2908 e5a0f69f 2018-08-18 stsp struct got_tree_object *child;
2909 e5a0f69f 2018-08-18 stsp err = got_object_open_as_tree(&child,
2910 e5a0f69f 2018-08-18 stsp s->repo, s->selected_entry->id);
2913 e5a0f69f 2018-08-18 stsp parent = calloc(1, sizeof(*parent));
2914 e5a0f69f 2018-08-18 stsp if (parent == NULL) {
2915 e5a0f69f 2018-08-18 stsp err = got_error_from_errno();
2918 e5a0f69f 2018-08-18 stsp parent->tree = s->tree;
2919 e5a0f69f 2018-08-18 stsp parent->first_displayed_entry =
2920 e5a0f69f 2018-08-18 stsp s->first_displayed_entry;
2921 e5a0f69f 2018-08-18 stsp parent->selected_entry = s->selected_entry;
2922 e5a0f69f 2018-08-18 stsp parent->selected = s->selected;
2923 e5a0f69f 2018-08-18 stsp TAILQ_INSERT_HEAD(&s->parents, parent, entry);
2924 e5a0f69f 2018-08-18 stsp s->tree = child;
2925 e5a0f69f 2018-08-18 stsp s->entries =
2926 e5a0f69f 2018-08-18 stsp got_object_tree_get_entries(s->tree);
2927 e5a0f69f 2018-08-18 stsp s->selected = 0;
2928 e5a0f69f 2018-08-18 stsp s->first_displayed_entry = NULL;
2929 e5a0f69f 2018-08-18 stsp } else if (S_ISREG(s->selected_entry->mode)) {
2930 e5a0f69f 2018-08-18 stsp err = blame_tree_entry(new_view, view,
2931 e5a0f69f 2018-08-18 stsp s->selected_entry, &s->parents,
2932 e5a0f69f 2018-08-18 stsp s->commit_id, s->repo);
2937 e5a0f69f 2018-08-18 stsp case KEY_RESIZE:
2938 e5a0f69f 2018-08-18 stsp if (s->selected > view->nlines)
2939 e5a0f69f 2018-08-18 stsp s->selected = s->ndisplayed - 1;
2945 ffd1d5e5 2018-06-23 stsp return err;
2948 ffd1d5e5 2018-06-23 stsp __dead static void
2949 ffd1d5e5 2018-06-23 stsp usage_tree(void)
2952 ffd1d5e5 2018-06-23 stsp fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
2953 ffd1d5e5 2018-06-23 stsp getprogname());
2957 ffd1d5e5 2018-06-23 stsp static const struct got_error *
2958 ffd1d5e5 2018-06-23 stsp cmd_tree(int argc, char *argv[])
2960 ffd1d5e5 2018-06-23 stsp const struct got_error *error;
2961 ffd1d5e5 2018-06-23 stsp struct got_repository *repo = NULL;
2962 ffd1d5e5 2018-06-23 stsp char *repo_path = NULL;
2963 ffd1d5e5 2018-06-23 stsp struct got_object_id *commit_id = NULL;
2964 ffd1d5e5 2018-06-23 stsp char *commit_id_arg = NULL;
2965 ffd1d5e5 2018-06-23 stsp struct got_commit_object *commit = NULL;
2966 ffd1d5e5 2018-06-23 stsp struct got_tree_object *tree = NULL;
2968 5221c383 2018-08-01 stsp struct tog_view *view;
2970 ffd1d5e5 2018-06-23 stsp #ifndef PROFILE
2971 ad242220 2018-09-08 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd", NULL)
2973 ffd1d5e5 2018-06-23 stsp err(1, "pledge");
2976 ffd1d5e5 2018-06-23 stsp while ((ch = getopt(argc, argv, "c:")) != -1) {
2977 ffd1d5e5 2018-06-23 stsp switch (ch) {
2979 ffd1d5e5 2018-06-23 stsp commit_id_arg = optarg;
2983 ffd1d5e5 2018-06-23 stsp /* NOTREACHED */
2987 ffd1d5e5 2018-06-23 stsp argc -= optind;
2988 ffd1d5e5 2018-06-23 stsp argv += optind;
2990 ffd1d5e5 2018-06-23 stsp if (argc == 0) {
2991 ffd1d5e5 2018-06-23 stsp repo_path = getcwd(NULL, 0);
2992 ffd1d5e5 2018-06-23 stsp if (repo_path == NULL)
2993 ffd1d5e5 2018-06-23 stsp return got_error_from_errno();
2994 ffd1d5e5 2018-06-23 stsp } else if (argc == 1) {
2995 ffd1d5e5 2018-06-23 stsp repo_path = realpath(argv[0], NULL);
2996 ffd1d5e5 2018-06-23 stsp if (repo_path == NULL)
2997 ffd1d5e5 2018-06-23 stsp return got_error_from_errno();
2999 ffd1d5e5 2018-06-23 stsp usage_log();
3001 ffd1d5e5 2018-06-23 stsp error = got_repo_open(&repo, repo_path);
3002 ffd1d5e5 2018-06-23 stsp free(repo_path);
3003 ffd1d5e5 2018-06-23 stsp if (error != NULL)
3004 ffd1d5e5 2018-06-23 stsp return error;
3006 ffd1d5e5 2018-06-23 stsp if (commit_id_arg == NULL) {
3007 ffd1d5e5 2018-06-23 stsp error = get_head_commit_id(&commit_id, repo);
3008 ffd1d5e5 2018-06-23 stsp if (error != NULL)
3009 ffd1d5e5 2018-06-23 stsp goto done;
3011 ffd1d5e5 2018-06-23 stsp struct got_object *obj;
3012 ffd1d5e5 2018-06-23 stsp error = got_object_open_by_id_str(&obj, repo, commit_id_arg);
3013 ffd1d5e5 2018-06-23 stsp if (error == NULL) {
3014 6402fb3c 2018-09-15 stsp commit_id = got_object_id_dup(got_object_get_id(obj));
3015 ffd1d5e5 2018-06-23 stsp if (commit_id == NULL)
3016 ffd1d5e5 2018-06-23 stsp error = got_error_from_errno();
3019 ffd1d5e5 2018-06-23 stsp if (error != NULL)
3020 ffd1d5e5 2018-06-23 stsp goto done;
3022 ffd1d5e5 2018-06-23 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
3023 ffd1d5e5 2018-06-23 stsp if (error != NULL)
3024 ffd1d5e5 2018-06-23 stsp goto done;
3026 ffd1d5e5 2018-06-23 stsp error = got_object_open_as_tree(&tree, repo, commit->tree_id);
3027 ffd1d5e5 2018-06-23 stsp if (error != NULL)
3028 ffd1d5e5 2018-06-23 stsp goto done;
3030 b3665f43 2018-08-04 stsp view = view_open(0, 0, 0, 0, NULL, TOG_VIEW_TREE);
3031 5221c383 2018-08-01 stsp if (view == NULL) {
3032 5221c383 2018-08-01 stsp error = got_error_from_errno();
3033 5221c383 2018-08-01 stsp goto done;
3035 ad80ab7b 2018-08-04 stsp error = open_tree_view(view, tree, commit_id, repo);
3036 ad80ab7b 2018-08-04 stsp if (error)
3037 ad80ab7b 2018-08-04 stsp goto done;
3038 e5a0f69f 2018-08-18 stsp error = view_loop(view);
3040 ffd1d5e5 2018-06-23 stsp free(commit_id);
3041 ffd1d5e5 2018-06-23 stsp if (commit)
3042 ffd1d5e5 2018-06-23 stsp got_object_commit_close(commit);
3044 ffd1d5e5 2018-06-23 stsp got_object_tree_close(tree);
3046 ffd1d5e5 2018-06-23 stsp got_repo_close(repo);
3047 ffd1d5e5 2018-06-23 stsp return error;
3049 5c5136c5 2018-05-20 stsp static void
3050 9f7d7167 2018-04-29 stsp init_curses(void)
3052 9f7d7167 2018-04-29 stsp initscr();
3056 9f7d7167 2018-04-29 stsp intrflush(stdscr, FALSE);
3057 9f7d7167 2018-04-29 stsp keypad(stdscr, TRUE);
3058 1f475ad8 2018-05-10 stsp curs_set(0);
3061 4ed7e80c 2018-05-20 stsp __dead static void
3062 9f7d7167 2018-04-29 stsp usage(void)
3066 c2301be8 2018-04-30 stsp fprintf(stderr, "usage: %s [-h] [command] [arg ...]\n\n"
3067 9f7d7167 2018-04-29 stsp "Available commands:\n", getprogname());
3068 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
3069 9f7d7167 2018-04-29 stsp struct tog_cmd *cmd = &tog_commands[i];
3070 c2301be8 2018-04-30 stsp fprintf(stderr, " %s: %s\n", cmd->name, cmd->descr);
3075 c2301be8 2018-04-30 stsp static char **
3076 c2301be8 2018-04-30 stsp make_argv(const char *arg0, const char *arg1)
3078 c2301be8 2018-04-30 stsp char **argv;
3079 c2301be8 2018-04-30 stsp int argc = (arg1 == NULL ? 1 : 2);
3081 c2301be8 2018-04-30 stsp argv = calloc(argc, sizeof(char *));
3082 c2301be8 2018-04-30 stsp if (argv == NULL)
3083 c2301be8 2018-04-30 stsp err(1, "calloc");
3084 c2301be8 2018-04-30 stsp argv[0] = strdup(arg0);
3085 c2301be8 2018-04-30 stsp if (argv[0] == NULL)
3086 c2301be8 2018-04-30 stsp err(1, "calloc");
3087 c2301be8 2018-04-30 stsp if (arg1) {
3088 c2301be8 2018-04-30 stsp argv[1] = strdup(arg1);
3089 c2301be8 2018-04-30 stsp if (argv[1] == NULL)
3090 c2301be8 2018-04-30 stsp err(1, "calloc");
3093 c2301be8 2018-04-30 stsp return argv;
3097 9f7d7167 2018-04-29 stsp main(int argc, char *argv[])
3099 9f7d7167 2018-04-29 stsp const struct got_error *error = NULL;
3100 9f7d7167 2018-04-29 stsp struct tog_cmd *cmd = NULL;
3101 9f7d7167 2018-04-29 stsp int ch, hflag = 0;
3102 c2301be8 2018-04-30 stsp char **cmd_argv = NULL;
3104 9f7d7167 2018-04-29 stsp setlocale(LC_ALL, "");
3106 9f7d7167 2018-04-29 stsp while ((ch = getopt(argc, argv, "h")) != -1) {
3107 9f7d7167 2018-04-29 stsp switch (ch) {
3109 9f7d7167 2018-04-29 stsp hflag = 1;
3113 9f7d7167 2018-04-29 stsp /* NOTREACHED */
3117 9f7d7167 2018-04-29 stsp argc -= optind;
3118 9f7d7167 2018-04-29 stsp argv += optind;
3119 9f7d7167 2018-04-29 stsp optind = 0;
3120 c2301be8 2018-04-30 stsp optreset = 1;
3122 c2301be8 2018-04-30 stsp if (argc == 0) {
3123 f29d3e89 2018-06-23 stsp if (hflag)
3125 c2301be8 2018-04-30 stsp /* Build an argument vector which runs a default command. */
3126 9f7d7167 2018-04-29 stsp cmd = &tog_commands[0];
3127 c2301be8 2018-04-30 stsp cmd_argv = make_argv(cmd->name, NULL);
3132 c2301be8 2018-04-30 stsp /* Did the user specific a command? */
3133 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
3134 c2301be8 2018-04-30 stsp if (strncmp(tog_commands[i].name, argv[0],
3135 9f7d7167 2018-04-29 stsp strlen(argv[0])) == 0) {
3136 9f7d7167 2018-04-29 stsp cmd = &tog_commands[i];
3137 9f7d7167 2018-04-29 stsp if (hflag)
3138 9f7d7167 2018-04-29 stsp tog_commands[i].cmd_usage();
3142 9f7d7167 2018-04-29 stsp if (cmd == NULL) {
3143 c2301be8 2018-04-30 stsp /* Did the user specify a repository? */
3144 c2301be8 2018-04-30 stsp char *repo_path = realpath(argv[0], NULL);
3145 c2301be8 2018-04-30 stsp if (repo_path) {
3146 c2301be8 2018-04-30 stsp struct got_repository *repo;
3147 c2301be8 2018-04-30 stsp error = got_repo_open(&repo, repo_path);
3148 c2301be8 2018-04-30 stsp if (error == NULL)
3149 c2301be8 2018-04-30 stsp got_repo_close(repo);
3151 ad7de8d9 2018-04-30 stsp error = got_error_from_errno();
3152 c2301be8 2018-04-30 stsp if (error) {
3153 f29d3e89 2018-06-23 stsp if (hflag) {
3154 f29d3e89 2018-06-23 stsp fprintf(stderr, "%s: '%s' is not a "
3155 f29d3e89 2018-06-23 stsp "known command\n", getprogname(),
3159 ad7de8d9 2018-04-30 stsp fprintf(stderr, "%s: '%s' is neither a known "
3160 ad7de8d9 2018-04-30 stsp "command nor a path to a repository\n",
3161 c2301be8 2018-04-30 stsp getprogname(), argv[0]);
3162 ad7de8d9 2018-04-30 stsp free(repo_path);
3165 c2301be8 2018-04-30 stsp cmd = &tog_commands[0];
3166 c2301be8 2018-04-30 stsp cmd_argv = make_argv(cmd->name, repo_path);
3168 c2301be8 2018-04-30 stsp free(repo_path);
3172 5c5136c5 2018-05-20 stsp init_curses();
3174 c2301be8 2018-04-30 stsp error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
3175 9f7d7167 2018-04-29 stsp if (error)
3176 9f7d7167 2018-04-29 stsp goto done;
3179 c2301be8 2018-04-30 stsp free(cmd_argv);
3180 9f7d7167 2018-04-29 stsp if (error)
3181 9f7d7167 2018-04-29 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);