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;
398 31607d6c 2018-08-18 stsp case KEY_BACKSPACE:
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 a3404814 2018-09-02 stsp view->focussed = 0;
511 bcbd79e2 2018-08-19 stsp /* TODO: de-duplicate! */
512 bcbd79e2 2018-08-19 stsp TAILQ_INSERT_TAIL(&views, new_view, entry);
513 bcbd79e2 2018-08-19 stsp if (new_view->parent) {
514 bcbd79e2 2018-08-19 stsp err = view_set_child(new_view->parent, new_view);
517 a3404814 2018-09-02 stsp new_view->parent->focussed = 0;
519 bcbd79e2 2018-08-19 stsp view = new_view;
520 a3404814 2018-09-02 stsp view->focussed = 1;
524 e5a0f69f 2018-08-18 stsp while (!TAILQ_EMPTY(&views)) {
525 e5a0f69f 2018-08-18 stsp view = TAILQ_FIRST(&views);
526 e5a0f69f 2018-08-18 stsp TAILQ_REMOVE(&views, view, entry);
527 e5a0f69f 2018-08-18 stsp view_close(view);
529 e5a0f69f 2018-08-18 stsp return err;
532 4ed7e80c 2018-05-20 stsp __dead static void
533 9f7d7167 2018-04-29 stsp usage_log(void)
536 c70c5802 2018-08-01 stsp fprintf(stderr,
537 c70c5802 2018-08-01 stsp "usage: %s log [-c commit] [-r repository-path] [path]\n",
538 9f7d7167 2018-04-29 stsp getprogname());
542 963b370f 2018-05-20 stsp /* Create newly allocated wide-character string equivalent to a byte string. */
543 80ddbec8 2018-04-29 stsp static const struct got_error *
544 963b370f 2018-05-20 stsp mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
546 00dfcb92 2018-06-11 stsp char *vis = NULL;
547 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
549 963b370f 2018-05-20 stsp *ws = NULL;
550 963b370f 2018-05-20 stsp *wlen = mbstowcs(NULL, s, 0);
551 00dfcb92 2018-06-11 stsp if (*wlen == (size_t)-1) {
552 00dfcb92 2018-06-11 stsp int vislen;
553 00dfcb92 2018-06-11 stsp if (errno != EILSEQ)
554 00dfcb92 2018-06-11 stsp return got_error_from_errno();
556 00dfcb92 2018-06-11 stsp /* byte string invalid in current encoding; try to "fix" it */
557 00dfcb92 2018-06-11 stsp err = got_mbsavis(&vis, &vislen, s);
559 00dfcb92 2018-06-11 stsp return err;
560 00dfcb92 2018-06-11 stsp *wlen = mbstowcs(NULL, vis, 0);
561 a7f50699 2018-06-11 stsp if (*wlen == (size_t)-1) {
562 a7f50699 2018-06-11 stsp err = got_error_from_errno(); /* give up */
567 963b370f 2018-05-20 stsp *ws = calloc(*wlen + 1, sizeof(*ws));
568 a7f50699 2018-06-11 stsp if (*ws == NULL) {
569 a7f50699 2018-06-11 stsp err = got_error_from_errno();
573 00dfcb92 2018-06-11 stsp if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
574 963b370f 2018-05-20 stsp err = got_error_from_errno();
579 963b370f 2018-05-20 stsp *ws = NULL;
582 963b370f 2018-05-20 stsp return err;
585 963b370f 2018-05-20 stsp /* Format a line for display, ensuring that it won't overflow a width limit. */
586 963b370f 2018-05-20 stsp static const struct got_error *
587 ffd1d5e5 2018-06-23 stsp format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit)
589 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
590 963b370f 2018-05-20 stsp int cols = 0;
591 963b370f 2018-05-20 stsp wchar_t *wline = NULL;
592 963b370f 2018-05-20 stsp size_t wlen;
595 963b370f 2018-05-20 stsp *wlinep = NULL;
596 b700b5d6 2018-07-10 stsp *widthp = 0;
598 963b370f 2018-05-20 stsp err = mbs2ws(&wline, &wlen, line);
600 963b370f 2018-05-20 stsp return err;
603 b700b5d6 2018-07-10 stsp while (i < wlen && cols < wlimit) {
604 963b370f 2018-05-20 stsp int width = wcwidth(wline[i]);
605 963b370f 2018-05-20 stsp switch (width) {
611 b700b5d6 2018-07-10 stsp if (cols + width <= wlimit) {
612 b700b5d6 2018-07-10 stsp cols += width;
617 963b370f 2018-05-20 stsp if (wline[i] == L'\t')
618 b700b5d6 2018-07-10 stsp cols += TABSIZE - ((cols + 1) % TABSIZE);
622 963b370f 2018-05-20 stsp err = got_error_from_errno();
626 963b370f 2018-05-20 stsp wline[i] = L'\0';
627 b700b5d6 2018-07-10 stsp if (widthp)
628 b700b5d6 2018-07-10 stsp *widthp = cols;
631 963b370f 2018-05-20 stsp free(wline);
633 963b370f 2018-05-20 stsp *wlinep = wline;
634 963b370f 2018-05-20 stsp return err;
637 963b370f 2018-05-20 stsp static const struct got_error *
638 2814baeb 2018-08-01 stsp draw_commit(struct tog_view *view, struct got_commit_object *commit,
639 2814baeb 2018-08-01 stsp struct got_object_id *id)
641 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
642 b39d25c7 2018-07-10 stsp char datebuf[10]; /* YY-MM-DD + SPACE + NUL */
643 80ddbec8 2018-04-29 stsp char *logmsg0 = NULL, *logmsg = NULL;
644 6d9fbc00 2018-04-29 stsp char *author0 = NULL, *author = NULL;
645 bb737323 2018-05-20 stsp wchar_t *wlogmsg = NULL, *wauthor = NULL;
646 bb737323 2018-05-20 stsp int author_width, logmsg_width;
647 6d9fbc00 2018-04-29 stsp char *newline, *smallerthan;
648 80ddbec8 2018-04-29 stsp char *line = NULL;
649 bb737323 2018-05-20 stsp int col, limit;
650 b39d25c7 2018-07-10 stsp static const size_t date_display_cols = 9;
651 bb737323 2018-05-20 stsp static const size_t author_display_cols = 16;
652 f7d12f7e 2018-08-01 stsp const int avail = view->ncols;
654 c70c5802 2018-08-01 stsp if (strftime(datebuf, sizeof(datebuf), "%g/%m/%d ",
655 c70c5802 2018-08-01 stsp &commit->tm_committer) >= sizeof(datebuf))
656 b39d25c7 2018-07-10 stsp return got_error(GOT_ERR_NO_SPACE);
658 b39d25c7 2018-07-10 stsp if (avail < date_display_cols)
659 b39d25c7 2018-07-10 stsp limit = MIN(sizeof(datebuf) - 1, avail);
661 b39d25c7 2018-07-10 stsp limit = MIN(date_display_cols, sizeof(datebuf) - 1);
662 2814baeb 2018-08-01 stsp waddnstr(view->window, datebuf, limit);
663 b39d25c7 2018-07-10 stsp col = limit + 1;
664 b39d25c7 2018-07-10 stsp if (col > avail)
667 6d9fbc00 2018-04-29 stsp author0 = strdup(commit->author);
668 6d9fbc00 2018-04-29 stsp if (author0 == NULL) {
669 80ddbec8 2018-04-29 stsp err = got_error_from_errno();
672 6d9fbc00 2018-04-29 stsp author = author0;
673 6d9fbc00 2018-04-29 stsp smallerthan = strchr(author, '<');
674 6d9fbc00 2018-04-29 stsp if (smallerthan)
675 6d9fbc00 2018-04-29 stsp *smallerthan = '\0';
677 6d9fbc00 2018-04-29 stsp char *at = strchr(author, '@');
679 6d9fbc00 2018-04-29 stsp *at = '\0';
681 b39d25c7 2018-07-10 stsp limit = avail - col;
682 bb737323 2018-05-20 stsp err = format_line(&wauthor, &author_width, author, limit);
685 2814baeb 2018-08-01 stsp waddwstr(view->window, wauthor);
686 bb737323 2018-05-20 stsp col += author_width;
687 9c2eaf34 2018-05-20 stsp while (col <= avail && author_width < author_display_cols + 1) {
688 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
690 bb737323 2018-05-20 stsp author_width++;
692 9c2eaf34 2018-05-20 stsp if (col > avail)
695 bb737323 2018-05-20 stsp logmsg0 = strdup(commit->logmsg);
696 bb737323 2018-05-20 stsp if (logmsg0 == NULL) {
697 6d9fbc00 2018-04-29 stsp err = got_error_from_errno();
700 bb737323 2018-05-20 stsp logmsg = logmsg0;
701 bb737323 2018-05-20 stsp while (*logmsg == '\n')
703 bb737323 2018-05-20 stsp newline = strchr(logmsg, '\n');
704 bb737323 2018-05-20 stsp if (newline)
705 bb737323 2018-05-20 stsp *newline = '\0';
706 bb737323 2018-05-20 stsp limit = avail - col;
707 bb737323 2018-05-20 stsp err = format_line(&wlogmsg, &logmsg_width, logmsg, limit);
710 2814baeb 2018-08-01 stsp waddwstr(view->window, wlogmsg);
711 bb737323 2018-05-20 stsp col += logmsg_width;
712 bb737323 2018-05-20 stsp while (col <= avail) {
713 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
717 80ddbec8 2018-04-29 stsp free(logmsg0);
718 bb737323 2018-05-20 stsp free(wlogmsg);
719 6d9fbc00 2018-04-29 stsp free(author0);
720 bb737323 2018-05-20 stsp free(wauthor);
721 80ddbec8 2018-04-29 stsp free(line);
722 80ddbec8 2018-04-29 stsp return err;
725 899d86c2 2018-05-10 stsp static struct commit_queue_entry *
726 899d86c2 2018-05-10 stsp alloc_commit_queue_entry(struct got_commit_object *commit,
727 899d86c2 2018-05-10 stsp struct got_object_id *id)
729 80ddbec8 2018-04-29 stsp struct commit_queue_entry *entry;
731 80ddbec8 2018-04-29 stsp entry = calloc(1, sizeof(*entry));
732 80ddbec8 2018-04-29 stsp if (entry == NULL)
733 899d86c2 2018-05-10 stsp return NULL;
735 899d86c2 2018-05-10 stsp entry->id = id;
736 99db9666 2018-05-07 stsp entry->commit = commit;
737 899d86c2 2018-05-10 stsp return entry;
740 99db9666 2018-05-07 stsp static void
741 99db9666 2018-05-07 stsp pop_commit(struct commit_queue *commits)
743 99db9666 2018-05-07 stsp struct commit_queue_entry *entry;
745 ecb28ae0 2018-07-16 stsp entry = TAILQ_FIRST(&commits->head);
746 ecb28ae0 2018-07-16 stsp TAILQ_REMOVE(&commits->head, entry, entry);
747 99db9666 2018-05-07 stsp got_object_commit_close(entry->commit);
748 ecb28ae0 2018-07-16 stsp commits->ncommits--;
749 9ba79e04 2018-06-11 stsp /* Don't free entry->id! It is owned by the commit graph. */
750 99db9666 2018-05-07 stsp free(entry);
753 99db9666 2018-05-07 stsp static void
754 99db9666 2018-05-07 stsp free_commits(struct commit_queue *commits)
756 ecb28ae0 2018-07-16 stsp while (!TAILQ_EMPTY(&commits->head))
757 99db9666 2018-05-07 stsp pop_commit(commits);
760 c4972b91 2018-05-07 stsp static const struct got_error *
761 9ba79e04 2018-06-11 stsp queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
762 ecb28ae0 2018-07-16 stsp struct got_object_id *start_id, int minqueue, int init,
763 ecb28ae0 2018-07-16 stsp struct got_repository *repo, const char *path)
765 899d86c2 2018-05-10 stsp const struct got_error *err = NULL;
766 899d86c2 2018-05-10 stsp struct got_object_id *id;
767 9ba79e04 2018-06-11 stsp struct commit_queue_entry *entry;
768 ecb28ae0 2018-07-16 stsp int nfetched, nqueued = 0, found_obj = 0;
769 c8f60bff 2018-07-23 stsp int is_root_path = strcmp(path, "/") == 0;
771 9ba79e04 2018-06-11 stsp err = got_commit_graph_iter_start(graph, start_id);
773 c4972b91 2018-05-07 stsp return err;
775 ecb28ae0 2018-07-16 stsp entry = TAILQ_LAST(&commits->head, commit_queue_head);
776 9ba79e04 2018-06-11 stsp if (entry && got_object_id_cmp(entry->id, start_id) == 0) {
777 9ba79e04 2018-06-11 stsp int nfetched;
779 9ba79e04 2018-06-11 stsp /* Start ID's commit is already on the queue; skip over it. */
780 9ba79e04 2018-06-11 stsp err = got_commit_graph_iter_next(&id, graph);
781 9ba79e04 2018-06-11 stsp if (err && err->code != GOT_ERR_ITER_NEED_MORE)
782 9ba79e04 2018-06-11 stsp return err;
784 9ba79e04 2018-06-11 stsp err = got_commit_graph_fetch_commits(&nfetched, graph, 1, repo);
786 9ba79e04 2018-06-11 stsp return err;
789 9ba79e04 2018-06-11 stsp while (1) {
790 9ba79e04 2018-06-11 stsp struct got_commit_object *commit;
792 9ba79e04 2018-06-11 stsp err = got_commit_graph_iter_next(&id, graph);
794 ecb28ae0 2018-07-16 stsp if (err->code != GOT_ERR_ITER_NEED_MORE)
796 ecb28ae0 2018-07-16 stsp if (nqueued >= minqueue) {
797 9ba79e04 2018-06-11 stsp err = NULL;
800 ecb28ae0 2018-07-16 stsp err = got_commit_graph_fetch_commits(&nfetched,
801 ecb28ae0 2018-07-16 stsp graph, 1, repo);
803 ecb28ae0 2018-07-16 stsp return err;
806 ecb28ae0 2018-07-16 stsp if (id == NULL)
809 9ba79e04 2018-06-11 stsp err = got_object_open_as_commit(&commit, repo, id);
813 c8f60bff 2018-07-23 stsp if (!is_root_path) {
814 ecb28ae0 2018-07-16 stsp struct got_object *obj;
815 ecb28ae0 2018-07-16 stsp struct got_object_qid *pid;
816 ecb28ae0 2018-07-16 stsp int changed = 0;
818 ecb28ae0 2018-07-16 stsp err = got_object_open_by_path(&obj, repo, id, path);
820 c8f60bff 2018-07-23 stsp got_object_commit_close(commit);
821 ecb28ae0 2018-07-16 stsp if (err->code == GOT_ERR_NO_OBJ &&
822 ecb28ae0 2018-07-16 stsp (found_obj || !init)) {
823 ecb28ae0 2018-07-16 stsp /* History stops here. */
824 ecb28ae0 2018-07-16 stsp err = got_error(GOT_ERR_ITER_COMPLETED);
828 ecb28ae0 2018-07-16 stsp found_obj = 1;
830 ecb28ae0 2018-07-16 stsp pid = SIMPLEQ_FIRST(&commit->parent_ids);
831 ecb28ae0 2018-07-16 stsp if (pid != NULL) {
832 ecb28ae0 2018-07-16 stsp struct got_object *pobj;
833 ecb28ae0 2018-07-16 stsp err = got_object_open_by_path(&pobj, repo,
834 ecb28ae0 2018-07-16 stsp pid->id, path);
836 ecb28ae0 2018-07-16 stsp if (err->code != GOT_ERR_NO_OBJ) {
837 ecb28ae0 2018-07-16 stsp got_object_close(obj);
838 c8f60bff 2018-07-23 stsp got_object_commit_close(commit);
841 ecb28ae0 2018-07-16 stsp err = NULL;
842 ecb28ae0 2018-07-16 stsp changed = 1;
844 c2f16475 2018-07-23 stsp struct got_object_id *id, *pid;
845 c2f16475 2018-07-23 stsp id = got_object_get_id(obj);
846 c2f16475 2018-07-23 stsp if (id == NULL) {
847 c2f16475 2018-07-23 stsp err = got_error_from_errno();
848 c8f60bff 2018-07-23 stsp got_object_close(obj);
849 c8f60bff 2018-07-23 stsp got_object_close(pobj);
852 c2f16475 2018-07-23 stsp pid = got_object_get_id(pobj);
853 c2f16475 2018-07-23 stsp if (pid == NULL) {
854 c2f16475 2018-07-23 stsp err = got_error_from_errno();
856 c8f60bff 2018-07-23 stsp got_object_close(obj);
857 c8f60bff 2018-07-23 stsp got_object_close(pobj);
861 c2f16475 2018-07-23 stsp (got_object_id_cmp(id, pid) != 0);
862 ecb28ae0 2018-07-16 stsp got_object_close(pobj);
867 c8f60bff 2018-07-23 stsp got_object_close(obj);
868 ecb28ae0 2018-07-16 stsp if (!changed) {
869 ecb28ae0 2018-07-16 stsp got_object_commit_close(commit);
874 9ba79e04 2018-06-11 stsp entry = alloc_commit_queue_entry(commit, id);
875 9ba79e04 2018-06-11 stsp if (entry == NULL) {
876 9ba79e04 2018-06-11 stsp err = got_error_from_errno();
879 ecb28ae0 2018-07-16 stsp TAILQ_INSERT_TAIL(&commits->head, entry, entry);
881 ecb28ae0 2018-07-16 stsp commits->ncommits++;
884 9ba79e04 2018-06-11 stsp return err;
887 99db9666 2018-05-07 stsp static const struct got_error *
888 9ba79e04 2018-06-11 stsp fetch_next_commit(struct commit_queue_entry **pentry,
889 9ba79e04 2018-06-11 stsp struct commit_queue_entry *entry, struct commit_queue *commits,
890 ecb28ae0 2018-07-16 stsp struct got_commit_graph *graph, struct got_repository *repo,
891 ecb28ae0 2018-07-16 stsp const char *path)
893 899d86c2 2018-05-10 stsp const struct got_error *err = NULL;
895 9ba79e04 2018-06-11 stsp *pentry = NULL;
897 ecb28ae0 2018-07-16 stsp err = queue_commits(graph, commits, entry->id, 1, 0, repo, path);
899 9ba79e04 2018-06-11 stsp return err;
901 9ba79e04 2018-06-11 stsp /* Next entry to display should now be available. */
902 9ba79e04 2018-06-11 stsp *pentry = TAILQ_NEXT(entry, entry);
903 9ba79e04 2018-06-11 stsp if (*pentry == NULL)
904 9ba79e04 2018-06-11 stsp return got_error(GOT_ERR_NO_OBJ);
906 9ba79e04 2018-06-11 stsp return NULL;
909 899d86c2 2018-05-10 stsp static const struct got_error *
910 9ba79e04 2018-06-11 stsp get_head_commit_id(struct got_object_id **head_id, struct got_repository *repo)
912 9ba79e04 2018-06-11 stsp const struct got_error *err = NULL;
913 9ba79e04 2018-06-11 stsp struct got_reference *head_ref;
915 9ba79e04 2018-06-11 stsp *head_id = NULL;
917 9ba79e04 2018-06-11 stsp err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
919 99db9666 2018-05-07 stsp return err;
921 9ba79e04 2018-06-11 stsp err = got_ref_resolve(head_id, repo, head_ref);
922 9ba79e04 2018-06-11 stsp got_ref_close(head_ref);
924 9ba79e04 2018-06-11 stsp *head_id = NULL;
925 99db9666 2018-05-07 stsp return err;
928 9ba79e04 2018-06-11 stsp return NULL;
931 0553a4e3 2018-04-30 stsp static const struct got_error *
932 2814baeb 2018-08-01 stsp draw_commits(struct tog_view *view, struct commit_queue_entry **last,
933 ecb28ae0 2018-07-16 stsp struct commit_queue_entry **selected, struct commit_queue_entry *first,
934 a7f40148 2018-07-18 stsp struct commit_queue *commits, int selected_idx, int limit,
935 a7f40148 2018-07-18 stsp struct got_commit_graph *graph, struct got_repository *repo,
936 a7f40148 2018-07-18 stsp const char *path)
938 0553a4e3 2018-04-30 stsp const struct got_error *err = NULL;
939 0553a4e3 2018-04-30 stsp struct commit_queue_entry *entry;
940 ecb28ae0 2018-07-16 stsp int ncommits, width;
941 867c6645 2018-07-10 stsp char *id_str, *header;
942 ecb28ae0 2018-07-16 stsp wchar_t *wline;
944 e0d42f60 2018-07-22 stsp entry = first;
945 e0d42f60 2018-07-22 stsp ncommits = 0;
946 e0d42f60 2018-07-22 stsp while (entry) {
947 e0d42f60 2018-07-22 stsp if (ncommits == selected_idx) {
948 e0d42f60 2018-07-22 stsp *selected = entry;
951 e0d42f60 2018-07-22 stsp entry = TAILQ_NEXT(entry, entry);
952 e0d42f60 2018-07-22 stsp ncommits++;
955 867c6645 2018-07-10 stsp err = got_object_id_str(&id_str, (*selected)->id);
957 867c6645 2018-07-10 stsp return err;
959 c3ba6f36 2018-08-01 stsp if (path && strcmp(path, "/") != 0) {
960 ecb28ae0 2018-07-16 stsp if (asprintf(&header, "commit: %s [%s]", id_str, path) == -1) {
961 ecb28ae0 2018-07-16 stsp err = got_error_from_errno();
962 ecb28ae0 2018-07-16 stsp free(id_str);
963 ecb28ae0 2018-07-16 stsp return err;
965 ecb28ae0 2018-07-16 stsp } else if (asprintf(&header, "commit: %s", id_str) == -1) {
966 867c6645 2018-07-10 stsp err = got_error_from_errno();
967 867c6645 2018-07-10 stsp free(id_str);
968 867c6645 2018-07-10 stsp return err;
970 ecb28ae0 2018-07-16 stsp free(id_str);
971 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, header, view->ncols);
973 ecb28ae0 2018-07-16 stsp free(header);
974 ecb28ae0 2018-07-16 stsp return err;
976 ecb28ae0 2018-07-16 stsp free(header);
978 2814baeb 2018-08-01 stsp werase(view->window);
980 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
981 a3404814 2018-09-02 stsp wstandout(view->window);
982 2814baeb 2018-08-01 stsp waddwstr(view->window, wline);
983 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
984 a3404814 2018-09-02 stsp wstandend(view->window);
985 f7d12f7e 2018-08-01 stsp if (width < view->ncols)
986 2814baeb 2018-08-01 stsp waddch(view->window, '\n');
987 ecb28ae0 2018-07-16 stsp free(wline);
988 ecb28ae0 2018-07-16 stsp if (limit <= 1)
989 ecb28ae0 2018-07-16 stsp return NULL;
991 899d86c2 2018-05-10 stsp entry = first;
992 899d86c2 2018-05-10 stsp *last = first;
993 867c6645 2018-07-10 stsp ncommits = 0;
994 899d86c2 2018-05-10 stsp while (entry) {
995 ecb28ae0 2018-07-16 stsp if (ncommits >= limit - 1)
997 e0d42f60 2018-07-22 stsp if (ncommits == selected_idx)
998 2814baeb 2018-08-01 stsp wstandout(view->window);
999 2814baeb 2018-08-01 stsp err = draw_commit(view, entry->commit, entry->id);
1000 cd0acaa7 2018-05-20 stsp if (ncommits == selected_idx)
1001 2814baeb 2018-08-01 stsp wstandend(view->window);
1004 0553a4e3 2018-04-30 stsp ncommits++;
1005 899d86c2 2018-05-10 stsp *last = entry;
1006 a7f40148 2018-07-18 stsp if (entry == TAILQ_LAST(&commits->head, commit_queue_head)) {
1007 a7f40148 2018-07-18 stsp err = queue_commits(graph, commits, entry->id, 1,
1008 a7f40148 2018-07-18 stsp 0, repo, path);
1009 a7f40148 2018-07-18 stsp if (err) {
1010 a7f40148 2018-07-18 stsp if (err->code != GOT_ERR_ITER_COMPLETED)
1011 a7f40148 2018-07-18 stsp return err;
1012 a7f40148 2018-07-18 stsp err = NULL;
1015 899d86c2 2018-05-10 stsp entry = TAILQ_NEXT(entry, entry);
1018 1a57306a 2018-09-02 stsp view_vborder(view);
1020 80ddbec8 2018-04-29 stsp return err;
1023 07b55e75 2018-05-10 stsp static void
1024 16482c3b 2018-05-20 stsp scroll_up(struct commit_queue_entry **first_displayed_entry, int maxscroll,
1025 07b55e75 2018-05-10 stsp struct commit_queue *commits)
1027 07b55e75 2018-05-10 stsp struct commit_queue_entry *entry;
1028 07b55e75 2018-05-10 stsp int nscrolled = 0;
1030 ecb28ae0 2018-07-16 stsp entry = TAILQ_FIRST(&commits->head);
1031 07b55e75 2018-05-10 stsp if (*first_displayed_entry == entry)
1034 07b55e75 2018-05-10 stsp entry = *first_displayed_entry;
1035 16482c3b 2018-05-20 stsp while (entry && nscrolled < maxscroll) {
1036 ecb28ae0 2018-07-16 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
1037 07b55e75 2018-05-10 stsp if (entry) {
1038 07b55e75 2018-05-10 stsp *first_displayed_entry = entry;
1039 07b55e75 2018-05-10 stsp nscrolled++;
1044 aa075928 2018-05-10 stsp static const struct got_error *
1045 16482c3b 2018-05-20 stsp scroll_down(struct commit_queue_entry **first_displayed_entry, int maxscroll,
1046 aa075928 2018-05-10 stsp struct commit_queue_entry *last_displayed_entry,
1047 9ba79e04 2018-06-11 stsp struct commit_queue *commits, struct got_commit_graph *graph,
1048 ecb28ae0 2018-07-16 stsp struct got_repository *repo, const char *path)
1050 aa075928 2018-05-10 stsp const struct got_error *err = NULL;
1051 dd0a52c1 2018-05-20 stsp struct commit_queue_entry *pentry;
1052 aa075928 2018-05-10 stsp int nscrolled = 0;
1055 dd0a52c1 2018-05-20 stsp pentry = TAILQ_NEXT(last_displayed_entry, entry);
1056 aa075928 2018-05-10 stsp if (pentry == NULL) {
1057 9ba79e04 2018-06-11 stsp err = fetch_next_commit(&pentry, last_displayed_entry,
1058 ecb28ae0 2018-07-16 stsp commits, graph, repo, path);
1059 9a6bf2a5 2018-05-20 stsp if (err || pentry == NULL)
1062 dd0a52c1 2018-05-20 stsp last_displayed_entry = pentry;
1064 dd0a52c1 2018-05-20 stsp pentry = TAILQ_NEXT(*first_displayed_entry, entry);
1065 dd0a52c1 2018-05-20 stsp if (pentry == NULL)
1067 aa075928 2018-05-10 stsp *first_displayed_entry = pentry;
1068 16482c3b 2018-05-20 stsp } while (++nscrolled < maxscroll);
1070 dd0a52c1 2018-05-20 stsp return err;
1073 cd0acaa7 2018-05-20 stsp static const struct got_error *
1074 e5a0f69f 2018-08-18 stsp show_commit(struct tog_view **new_view, struct tog_view *parent_view,
1075 e5a0f69f 2018-08-18 stsp struct commit_queue_entry *entry, struct got_repository *repo)
1077 cd0acaa7 2018-05-20 stsp const struct got_error *err;
1078 cd0acaa7 2018-05-20 stsp struct got_object *obj1 = NULL, *obj2 = NULL;
1079 9ba79e04 2018-06-11 stsp struct got_object_qid *parent_id;
1080 e5a0f69f 2018-08-18 stsp struct tog_view *diff_view;
1082 cd0acaa7 2018-05-20 stsp err = got_object_open(&obj2, repo, entry->id);
1084 cd0acaa7 2018-05-20 stsp return err;
1086 9ba79e04 2018-06-11 stsp parent_id = SIMPLEQ_FIRST(&entry->commit->parent_ids);
1087 9ba79e04 2018-06-11 stsp if (parent_id) {
1088 9ba79e04 2018-06-11 stsp err = got_object_open(&obj1, repo, parent_id->id);
1090 cd0acaa7 2018-05-20 stsp goto done;
1093 e5a0f69f 2018-08-18 stsp diff_view = view_open(0, 0, 0, 0, parent_view, TOG_VIEW_DIFF);
1094 e5a0f69f 2018-08-18 stsp if (diff_view == NULL) {
1095 ea5e7bb5 2018-08-01 stsp err = got_error_from_errno();
1096 ea5e7bb5 2018-08-01 stsp goto done;
1099 e5a0f69f 2018-08-18 stsp err = open_diff_view(diff_view, obj1, obj2, repo);
1100 e5a0f69f 2018-08-18 stsp if (err == NULL)
1101 e5a0f69f 2018-08-18 stsp *new_view = diff_view;
1104 cd0acaa7 2018-05-20 stsp got_object_close(obj1);
1106 cd0acaa7 2018-05-20 stsp got_object_close(obj2);
1107 cd0acaa7 2018-05-20 stsp return err;
1110 80ddbec8 2018-04-29 stsp static const struct got_error *
1111 e5a0f69f 2018-08-18 stsp browse_commit(struct tog_view **new_view, struct tog_view *parent_view,
1112 e5a0f69f 2018-08-18 stsp struct commit_queue_entry *entry, struct got_repository *repo)
1114 9343a5fb 2018-06-23 stsp const struct got_error *err = NULL;
1115 9343a5fb 2018-06-23 stsp struct got_tree_object *tree;
1116 e5a0f69f 2018-08-18 stsp struct tog_view *tree_view;
1118 9343a5fb 2018-06-23 stsp err = got_object_open_as_tree(&tree, repo, entry->commit->tree_id);
1120 9343a5fb 2018-06-23 stsp return err;
1122 e5a0f69f 2018-08-18 stsp tree_view = view_open(0, 0, 0, 0, parent_view, TOG_VIEW_TREE);
1123 e5a0f69f 2018-08-18 stsp if (tree_view == NULL)
1124 e5a0f69f 2018-08-18 stsp return got_error_from_errno();
1126 e5a0f69f 2018-08-18 stsp err = open_tree_view(tree_view, tree, entry->id, repo);
1128 e5a0f69f 2018-08-18 stsp got_object_tree_close(tree);
1130 e5a0f69f 2018-08-18 stsp *new_view = tree_view;
1131 9343a5fb 2018-06-23 stsp return err;
1134 bcbd79e2 2018-08-19 stsp static const struct got_error *
1135 bcbd79e2 2018-08-19 stsp set_child_log_view(struct tog_view *view, struct tog_view *child)
1137 bcbd79e2 2018-08-19 stsp struct tog_log_view_state *s = &view->state.log;
1138 bcbd79e2 2018-08-19 stsp struct tog_diff_view_state *ds;
1139 bcbd79e2 2018-08-19 stsp struct commit_queue_entry *commit, *child_entry = NULL;
1140 bcbd79e2 2018-08-19 stsp int selected_idx = 0;
1142 bcbd79e2 2018-08-19 stsp if (child->type != TOG_VIEW_DIFF)
1143 bcbd79e2 2018-08-19 stsp return NULL;
1144 bcbd79e2 2018-08-19 stsp ds = &child->state.diff;
1146 bcbd79e2 2018-08-19 stsp TAILQ_FOREACH(commit, &s->commits.head, entry) {
1147 a3404814 2018-09-02 stsp if (got_object_id_cmp(commit->id, ds->id2) == 0) {
1148 bcbd79e2 2018-08-19 stsp child_entry = commit;
1152 bcbd79e2 2018-08-19 stsp if (child_entry == NULL)
1153 bcbd79e2 2018-08-19 stsp return NULL;
1155 bcbd79e2 2018-08-19 stsp commit = s->first_displayed_entry;
1156 bcbd79e2 2018-08-19 stsp while (commit) {
1157 bcbd79e2 2018-08-19 stsp if (got_object_id_cmp(commit->id, child_entry->id) == 0) {
1158 bcbd79e2 2018-08-19 stsp s->selected_entry = child_entry;
1159 bcbd79e2 2018-08-19 stsp s->selected = selected_idx;
1162 bcbd79e2 2018-08-19 stsp if (commit == s->last_displayed_entry)
1164 bcbd79e2 2018-08-19 stsp selected_idx++;
1165 bcbd79e2 2018-08-19 stsp commit = TAILQ_NEXT(commit, entry);
1168 bcbd79e2 2018-08-19 stsp return show_log_view(view);
1171 9343a5fb 2018-06-23 stsp static const struct got_error *
1172 ba4f502b 2018-08-04 stsp open_log_view(struct tog_view *view, struct got_object_id *start_id,
1173 04cc582a 2018-08-01 stsp struct got_repository *repo, const char *path)
1175 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
1176 9ba79e04 2018-06-11 stsp struct got_object_id *head_id = NULL;
1177 ba4f502b 2018-08-04 stsp int nfetched;
1178 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
1180 fb2756b9 2018-08-04 stsp err = got_repo_map_path(&s->in_repo_path, repo, path);
1181 ecb28ae0 2018-07-16 stsp if (err != NULL)
1182 ecb28ae0 2018-07-16 stsp goto done;
1184 9ba79e04 2018-06-11 stsp err = get_head_commit_id(&head_id, repo);
1186 9ba79e04 2018-06-11 stsp return err;
1188 034e3b69 2018-07-22 stsp /* The graph contains all commits. */
1189 fb2756b9 2018-08-04 stsp err = got_commit_graph_open(&s->graph, head_id, 0, repo);
1191 9ba79e04 2018-06-11 stsp goto done;
1192 034e3b69 2018-07-22 stsp /* The commit queue contains a subset of commits filtered by path. */
1193 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->commits.head);
1194 fb2756b9 2018-08-04 stsp s->commits.ncommits = 0;
1196 9ba79e04 2018-06-11 stsp /* Populate commit graph with a sufficient number of commits. */
1197 fb2756b9 2018-08-04 stsp err = got_commit_graph_fetch_commits_up_to(&nfetched, s->graph,
1198 fb2756b9 2018-08-04 stsp start_id, repo);
1200 9ba79e04 2018-06-11 stsp goto done;
1203 9ba79e04 2018-06-11 stsp * Open the initial batch of commits, sorted in commit graph order.
1204 9ba79e04 2018-06-11 stsp * We keep all commits open throughout the lifetime of the log view
1205 9ba79e04 2018-06-11 stsp * in order to avoid having to re-fetch commits from disk while
1206 9ba79e04 2018-06-11 stsp * updating the display.
1208 fb2756b9 2018-08-04 stsp err = queue_commits(s->graph, &s->commits, start_id, view->nlines, 1,
1209 fb2756b9 2018-08-04 stsp repo, s->in_repo_path);
1210 55198a88 2018-07-16 stsp if (err) {
1211 55198a88 2018-07-16 stsp if (err->code != GOT_ERR_ITER_COMPLETED)
1212 55198a88 2018-07-16 stsp goto done;
1213 55198a88 2018-07-16 stsp err = NULL;
1216 1bfa490b 2018-08-18 stsp s->first_displayed_entry =
1217 1bfa490b 2018-08-18 stsp TAILQ_FIRST(&s->commits.head);
1218 1bfa490b 2018-08-18 stsp s->selected_entry = s->first_displayed_entry;
1219 fb2756b9 2018-08-04 stsp s->repo = repo;
1221 e5a0f69f 2018-08-18 stsp view->show = show_log_view;
1222 e5a0f69f 2018-08-18 stsp view->input = input_log_view;
1223 e5a0f69f 2018-08-18 stsp view->close = close_log_view;
1224 bcbd79e2 2018-08-19 stsp view->set_child = set_child_log_view;
1226 ba4f502b 2018-08-04 stsp free(head_id);
1227 ba4f502b 2018-08-04 stsp return err;
1230 e5a0f69f 2018-08-18 stsp static const struct got_error *
1231 e5a0f69f 2018-08-18 stsp close_log_view(struct tog_view *view)
1233 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
1235 fb2756b9 2018-08-04 stsp if (s->graph)
1236 fb2756b9 2018-08-04 stsp got_commit_graph_close(s->graph);
1237 fb2756b9 2018-08-04 stsp free_commits(&s->commits);
1238 fb2756b9 2018-08-04 stsp free(s->in_repo_path);
1239 e5a0f69f 2018-08-18 stsp return NULL;
1242 ba4f502b 2018-08-04 stsp static const struct got_error *
1243 bcbd79e2 2018-08-19 stsp update_diff_child_view(struct tog_view *parent,
1244 bcbd79e2 2018-08-19 stsp struct commit_queue_entry *selected_entry, struct got_repository *repo)
1246 bcbd79e2 2018-08-19 stsp const struct got_error *err = NULL;
1247 bcbd79e2 2018-08-19 stsp struct tog_diff_view_state *ds;
1248 bcbd79e2 2018-08-19 stsp struct got_object *obj1 = NULL, *obj2 = NULL;
1249 bcbd79e2 2018-08-19 stsp struct got_object_qid *parent_id;
1250 bcbd79e2 2018-08-19 stsp struct tog_view *child_view = parent->child;
1252 bcbd79e2 2018-08-19 stsp if (child_view == NULL)
1253 bcbd79e2 2018-08-19 stsp return NULL;
1254 bcbd79e2 2018-08-19 stsp if (child_view->type != TOG_VIEW_DIFF)
1255 bcbd79e2 2018-08-19 stsp return NULL;
1256 bcbd79e2 2018-08-19 stsp ds = &child_view->state.diff;
1257 a3404814 2018-09-02 stsp if (got_object_id_cmp(ds->id2, selected_entry->id) == 0)
1258 bcbd79e2 2018-08-19 stsp return NULL;
1260 bcbd79e2 2018-08-19 stsp err = got_object_open(&obj2, repo, selected_entry->id);
1262 bcbd79e2 2018-08-19 stsp return err;
1264 bcbd79e2 2018-08-19 stsp parent_id = SIMPLEQ_FIRST(&selected_entry->commit->parent_ids);
1265 bcbd79e2 2018-08-19 stsp if (parent_id) {
1266 bcbd79e2 2018-08-19 stsp err = got_object_open(&obj1, repo, parent_id->id);
1268 bcbd79e2 2018-08-19 stsp goto done;
1271 bcbd79e2 2018-08-19 stsp err = close_diff_view(child_view);
1273 bcbd79e2 2018-08-19 stsp goto done;
1275 bcbd79e2 2018-08-19 stsp err = open_diff_view(child_view, obj1, obj2, repo);
1277 bcbd79e2 2018-08-19 stsp goto done;
1280 bcbd79e2 2018-08-19 stsp got_object_close(obj1);
1282 bcbd79e2 2018-08-19 stsp got_object_close(obj2);
1283 bcbd79e2 2018-08-19 stsp return err;
1286 bcbd79e2 2018-08-19 stsp static const struct got_error *
1287 ba4f502b 2018-08-04 stsp show_log_view(struct tog_view *view)
1289 bcbd79e2 2018-08-19 stsp const struct got_error *err = NULL;
1290 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
1292 bcbd79e2 2018-08-19 stsp err = draw_commits(view, &s->last_displayed_entry,
1293 e5a0f69f 2018-08-18 stsp &s->selected_entry, s->first_displayed_entry,
1294 e5a0f69f 2018-08-18 stsp &s->commits, s->selected, view->nlines, s->graph,
1295 e5a0f69f 2018-08-18 stsp s->repo, s->in_repo_path);
1297 bcbd79e2 2018-08-19 stsp return err;
1299 bcbd79e2 2018-08-19 stsp return update_diff_child_view(view, s->selected_entry, s->repo);
1302 e5a0f69f 2018-08-18 stsp static const struct got_error *
1303 e5a0f69f 2018-08-18 stsp input_log_view(struct tog_view **new_view, struct tog_view **dead_view,
1304 e5a0f69f 2018-08-18 stsp struct tog_view *view, int ch)
1306 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
1307 e5a0f69f 2018-08-18 stsp struct tog_log_view_state *s = &view->state.log;
1309 e5a0f69f 2018-08-18 stsp switch (ch) {
1311 e5a0f69f 2018-08-18 stsp case KEY_UP:
1313 e5a0f69f 2018-08-18 stsp if (s->selected > 0)
1314 e5a0f69f 2018-08-18 stsp s->selected--;
1315 e5a0f69f 2018-08-18 stsp if (s->selected > 0)
1317 e5a0f69f 2018-08-18 stsp scroll_up(&s->first_displayed_entry, 1,
1318 e5a0f69f 2018-08-18 stsp &s->commits);
1320 e5a0f69f 2018-08-18 stsp case KEY_PPAGE:
1321 e5a0f69f 2018-08-18 stsp if (TAILQ_FIRST(&s->commits.head) ==
1322 e5a0f69f 2018-08-18 stsp s->first_displayed_entry) {
1323 e5a0f69f 2018-08-18 stsp s->selected = 0;
1326 e5a0f69f 2018-08-18 stsp scroll_up(&s->first_displayed_entry,
1327 e5a0f69f 2018-08-18 stsp view->nlines, &s->commits);
1330 e5a0f69f 2018-08-18 stsp case KEY_DOWN:
1332 e5a0f69f 2018-08-18 stsp if (s->selected < MIN(view->nlines - 2,
1333 e5a0f69f 2018-08-18 stsp s->commits.ncommits - 1)) {
1334 e5a0f69f 2018-08-18 stsp s->selected++;
1337 e5a0f69f 2018-08-18 stsp err = scroll_down(&s->first_displayed_entry, 1,
1338 e5a0f69f 2018-08-18 stsp s->last_displayed_entry, &s->commits,
1339 e5a0f69f 2018-08-18 stsp s->graph, s->repo, s->in_repo_path);
1340 e5a0f69f 2018-08-18 stsp if (err) {
1341 4d7951a5 2018-08-04 stsp if (err->code != GOT_ERR_ITER_COMPLETED)
1343 4d7951a5 2018-08-04 stsp err = NULL;
1346 e5a0f69f 2018-08-18 stsp case KEY_NPAGE: {
1347 e5a0f69f 2018-08-18 stsp struct commit_queue_entry *first;
1348 e5a0f69f 2018-08-18 stsp first = s->first_displayed_entry;
1349 e5a0f69f 2018-08-18 stsp err = scroll_down(&s->first_displayed_entry,
1350 e5a0f69f 2018-08-18 stsp view->nlines, s->last_displayed_entry,
1351 e5a0f69f 2018-08-18 stsp &s->commits, s->graph, s->repo,
1352 e5a0f69f 2018-08-18 stsp s->in_repo_path);
1353 e5a0f69f 2018-08-18 stsp if (err == NULL)
1355 e5a0f69f 2018-08-18 stsp if (err->code != GOT_ERR_ITER_COMPLETED)
1357 e5a0f69f 2018-08-18 stsp if (first == s->first_displayed_entry &&
1358 e5a0f69f 2018-08-18 stsp s->selected < MIN(view->nlines - 2,
1359 e5a0f69f 2018-08-18 stsp s->commits.ncommits - 1)) {
1360 e5a0f69f 2018-08-18 stsp /* can't scroll further down */
1361 e5a0f69f 2018-08-18 stsp s->selected = MIN(view->nlines - 2,
1362 e5a0f69f 2018-08-18 stsp s->commits.ncommits - 1);
1364 e5a0f69f 2018-08-18 stsp err = NULL;
1367 e5a0f69f 2018-08-18 stsp case KEY_RESIZE:
1368 e5a0f69f 2018-08-18 stsp if (s->selected > view->nlines - 2)
1369 e5a0f69f 2018-08-18 stsp s->selected = view->nlines - 2;
1370 e5a0f69f 2018-08-18 stsp if (s->selected > s->commits.ncommits - 1)
1371 e5a0f69f 2018-08-18 stsp s->selected = s->commits.ncommits - 1;
1373 e5a0f69f 2018-08-18 stsp case KEY_ENTER:
1374 e5a0f69f 2018-08-18 stsp case '\r':
1375 e5a0f69f 2018-08-18 stsp err = show_commit(new_view, view, s->selected_entry,
1379 e5a0f69f 2018-08-18 stsp err = browse_commit(new_view, view, s->selected_entry,
1386 80ddbec8 2018-04-29 stsp return err;
1389 4ed7e80c 2018-05-20 stsp static const struct got_error *
1390 9f7d7167 2018-04-29 stsp cmd_log(int argc, char *argv[])
1392 80ddbec8 2018-04-29 stsp const struct got_error *error;
1393 ecb28ae0 2018-07-16 stsp struct got_repository *repo = NULL;
1394 899d86c2 2018-05-10 stsp struct got_object_id *start_id = NULL;
1395 ecb28ae0 2018-07-16 stsp char *path = NULL, *repo_path = NULL, *cwd = NULL;
1396 80ddbec8 2018-04-29 stsp char *start_commit = NULL;
1398 04cc582a 2018-08-01 stsp struct tog_view *view;
1400 80ddbec8 2018-04-29 stsp #ifndef PROFILE
1401 ad242220 2018-09-08 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd", NULL)
1403 80ddbec8 2018-04-29 stsp err(1, "pledge");
1406 ecb28ae0 2018-07-16 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1407 80ddbec8 2018-04-29 stsp switch (ch) {
1409 80ddbec8 2018-04-29 stsp start_commit = optarg;
1412 ecb28ae0 2018-07-16 stsp repo_path = realpath(optarg, NULL);
1413 ecb28ae0 2018-07-16 stsp if (repo_path == NULL)
1414 ecb28ae0 2018-07-16 stsp err(1, "-r option");
1418 80ddbec8 2018-04-29 stsp /* NOTREACHED */
1422 80ddbec8 2018-04-29 stsp argc -= optind;
1423 80ddbec8 2018-04-29 stsp argv += optind;
1425 ecb28ae0 2018-07-16 stsp if (argc == 0)
1426 ecb28ae0 2018-07-16 stsp path = strdup("");
1427 ecb28ae0 2018-07-16 stsp else if (argc == 1)
1428 ecb28ae0 2018-07-16 stsp path = strdup(argv[0]);
1430 80ddbec8 2018-04-29 stsp usage_log();
1431 ecb28ae0 2018-07-16 stsp if (path == NULL)
1432 ecb28ae0 2018-07-16 stsp return got_error_from_errno();
1434 ecb28ae0 2018-07-16 stsp cwd = getcwd(NULL, 0);
1435 ecb28ae0 2018-07-16 stsp if (cwd == NULL) {
1436 ecb28ae0 2018-07-16 stsp error = got_error_from_errno();
1437 ecb28ae0 2018-07-16 stsp goto done;
1439 ecb28ae0 2018-07-16 stsp if (repo_path == NULL) {
1440 ecb28ae0 2018-07-16 stsp repo_path = strdup(cwd);
1441 ecb28ae0 2018-07-16 stsp if (repo_path == NULL) {
1442 ecb28ae0 2018-07-16 stsp error = got_error_from_errno();
1443 ecb28ae0 2018-07-16 stsp goto done;
1447 80ddbec8 2018-04-29 stsp error = got_repo_open(&repo, repo_path);
1448 80ddbec8 2018-04-29 stsp if (error != NULL)
1449 ecb28ae0 2018-07-16 stsp goto done;
1451 80ddbec8 2018-04-29 stsp if (start_commit == NULL) {
1452 899d86c2 2018-05-10 stsp error = get_head_commit_id(&start_id, repo);
1453 80ddbec8 2018-04-29 stsp if (error != NULL)
1454 ecb28ae0 2018-07-16 stsp goto done;
1456 80ddbec8 2018-04-29 stsp struct got_object *obj;
1457 80ddbec8 2018-04-29 stsp error = got_object_open_by_id_str(&obj, repo, start_commit);
1458 80ddbec8 2018-04-29 stsp if (error == NULL) {
1459 899d86c2 2018-05-10 stsp start_id = got_object_get_id(obj);
1460 899d86c2 2018-05-10 stsp if (start_id == NULL)
1461 80ddbec8 2018-04-29 stsp error = got_error_from_errno();
1462 ecb28ae0 2018-07-16 stsp goto done;
1465 80ddbec8 2018-04-29 stsp if (error != NULL)
1466 ecb28ae0 2018-07-16 stsp goto done;
1468 b3665f43 2018-08-04 stsp view = view_open(0, 0, 0, 0, NULL, TOG_VIEW_LOG);
1469 04cc582a 2018-08-01 stsp if (view == NULL) {
1470 04cc582a 2018-08-01 stsp error = got_error_from_errno();
1471 04cc582a 2018-08-01 stsp goto done;
1473 ba4f502b 2018-08-04 stsp error = open_log_view(view, start_id, repo, path);
1474 ba4f502b 2018-08-04 stsp if (error)
1475 ba4f502b 2018-08-04 stsp goto done;
1476 e5a0f69f 2018-08-18 stsp error = view_loop(view);
1478 ecb28ae0 2018-07-16 stsp free(repo_path);
1479 ecb28ae0 2018-07-16 stsp free(cwd);
1480 ecb28ae0 2018-07-16 stsp free(path);
1481 899d86c2 2018-05-10 stsp free(start_id);
1483 ecb28ae0 2018-07-16 stsp got_repo_close(repo);
1484 80ddbec8 2018-04-29 stsp return error;
1487 4ed7e80c 2018-05-20 stsp __dead static void
1488 9f7d7167 2018-04-29 stsp usage_diff(void)
1491 9f7d7167 2018-04-29 stsp fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
1492 9f7d7167 2018-04-29 stsp getprogname());
1496 b304db33 2018-05-20 stsp static char *
1497 b304db33 2018-05-20 stsp parse_next_line(FILE *f, size_t *len)
1499 b304db33 2018-05-20 stsp char *line;
1500 b304db33 2018-05-20 stsp size_t linelen;
1501 b304db33 2018-05-20 stsp size_t lineno;
1502 b304db33 2018-05-20 stsp const char delim[3] = { '\0', '\0', '\0'};
1504 b304db33 2018-05-20 stsp line = fparseln(f, &linelen, &lineno, delim, 0);
1506 b304db33 2018-05-20 stsp *len = linelen;
1507 b304db33 2018-05-20 stsp return line;
1510 4ed7e80c 2018-05-20 stsp static const struct got_error *
1511 f7d12f7e 2018-08-01 stsp draw_file(struct tog_view *view, FILE *f, int *first_displayed_line,
1512 a3404814 2018-09-02 stsp int *last_displayed_line, int *eof, int max_lines,
1513 a3404814 2018-09-02 stsp char * header)
1515 61e69b96 2018-05-20 stsp const struct got_error *err;
1516 26ed57b2 2018-05-19 stsp int nlines = 0, nprinted = 0;
1517 b304db33 2018-05-20 stsp char *line;
1518 b304db33 2018-05-20 stsp size_t len;
1519 61e69b96 2018-05-20 stsp wchar_t *wline;
1520 e0b650dd 2018-05-20 stsp int width;
1522 26ed57b2 2018-05-19 stsp rewind(f);
1523 f7d12f7e 2018-08-01 stsp werase(view->window);
1525 a3404814 2018-09-02 stsp if (header) {
1526 a3404814 2018-09-02 stsp err = format_line(&wline, &width, header, view->ncols);
1527 a3404814 2018-09-02 stsp if (err) {
1528 a3404814 2018-09-02 stsp return err;
1531 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
1532 a3404814 2018-09-02 stsp wstandout(view->window);
1533 a3404814 2018-09-02 stsp waddwstr(view->window, wline);
1534 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
1535 a3404814 2018-09-02 stsp wstandend(view->window);
1536 a3404814 2018-09-02 stsp if (width < view->ncols)
1537 a3404814 2018-09-02 stsp waddch(view->window, '\n');
1539 a3404814 2018-09-02 stsp if (max_lines <= 1)
1540 a3404814 2018-09-02 stsp return NULL;
1541 a3404814 2018-09-02 stsp max_lines--;
1545 26ed57b2 2018-05-19 stsp while (nprinted < max_lines) {
1546 b304db33 2018-05-20 stsp line = parse_next_line(f, &len);
1547 26ed57b2 2018-05-19 stsp if (line == NULL) {
1551 26ed57b2 2018-05-19 stsp if (++nlines < *first_displayed_line) {
1552 26ed57b2 2018-05-19 stsp free(line);
1556 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, line, view->ncols);
1557 61e69b96 2018-05-20 stsp if (err) {
1558 61e69b96 2018-05-20 stsp free(line);
1559 61e69b96 2018-05-20 stsp return err;
1561 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
1562 f7d12f7e 2018-08-01 stsp if (width < view->ncols)
1563 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
1564 26ed57b2 2018-05-19 stsp if (++nprinted == 1)
1565 26ed57b2 2018-05-19 stsp *first_displayed_line = nlines;
1566 26ed57b2 2018-05-19 stsp free(line);
1567 2550e4c3 2018-07-13 stsp free(wline);
1568 2550e4c3 2018-07-13 stsp wline = NULL;
1570 26ed57b2 2018-05-19 stsp *last_displayed_line = nlines;
1572 1a57306a 2018-09-02 stsp view_vborder(view);
1574 26ed57b2 2018-05-19 stsp return NULL;
1577 4ed7e80c 2018-05-20 stsp static const struct got_error *
1578 5dc9f4bc 2018-08-04 stsp open_diff_view(struct tog_view *view, struct got_object *obj1,
1579 ea5e7bb5 2018-08-01 stsp struct got_object *obj2, struct got_repository *repo)
1581 26ed57b2 2018-05-19 stsp const struct got_error *err;
1584 cd0acaa7 2018-05-20 stsp if (obj1 != NULL && obj2 != NULL &&
1585 cd0acaa7 2018-05-20 stsp got_object_get_type(obj1) != got_object_get_type(obj2))
1586 26ed57b2 2018-05-19 stsp return got_error(GOT_ERR_OBJ_TYPE);
1588 511a516b 2018-05-19 stsp f = got_opentemp();
1589 26ed57b2 2018-05-19 stsp if (f == NULL)
1590 26ed57b2 2018-05-19 stsp return got_error_from_errno();
1592 cd0acaa7 2018-05-20 stsp switch (got_object_get_type(obj1 ? obj1 : obj2)) {
1593 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_BLOB:
1594 11528a82 2018-05-19 stsp err = got_diff_objects_as_blobs(obj1, obj2, repo, f);
1596 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_TREE:
1597 11528a82 2018-05-19 stsp err = got_diff_objects_as_trees(obj1, obj2, repo, f);
1599 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_COMMIT:
1600 11528a82 2018-05-19 stsp err = got_diff_objects_as_commits(obj1, obj2, repo, f);
1603 26ed57b2 2018-05-19 stsp return got_error(GOT_ERR_OBJ_TYPE);
1606 26ed57b2 2018-05-19 stsp fflush(f);
1608 a3404814 2018-09-02 stsp view->state.diff.id1 = obj1 ? got_object_get_id(obj1) : NULL;
1609 a3404814 2018-09-02 stsp view->state.diff.id2 = got_object_get_id(obj2);
1610 a3404814 2018-09-02 stsp if (view->state.diff.id2 == NULL)
1611 bcbd79e2 2018-08-19 stsp return got_error_from_errno();
1612 5dc9f4bc 2018-08-04 stsp view->state.diff.f = f;
1613 5dc9f4bc 2018-08-04 stsp view->state.diff.first_displayed_line = 1;
1614 5dc9f4bc 2018-08-04 stsp view->state.diff.last_displayed_line = view->nlines;
1616 e5a0f69f 2018-08-18 stsp view->show = show_diff_view;
1617 e5a0f69f 2018-08-18 stsp view->input = input_diff_view;
1618 e5a0f69f 2018-08-18 stsp view->close = close_diff_view;
1620 5dc9f4bc 2018-08-04 stsp return NULL;
1623 e5a0f69f 2018-08-18 stsp static const struct got_error *
1624 5dc9f4bc 2018-08-04 stsp close_diff_view(struct tog_view *view)
1626 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
1628 e5a0f69f 2018-08-18 stsp if (view->state.diff.f && fclose(view->state.diff.f) == EOF)
1629 e5a0f69f 2018-08-18 stsp err = got_error_from_errno();
1630 a3404814 2018-09-02 stsp free(view->state.diff.id1);
1631 a3404814 2018-09-02 stsp free(view->state.diff.id2);
1632 e5a0f69f 2018-08-18 stsp return err;
1635 5dc9f4bc 2018-08-04 stsp static const struct got_error *
1636 5dc9f4bc 2018-08-04 stsp show_diff_view(struct tog_view *view)
1638 a3404814 2018-09-02 stsp const struct got_error *err;
1639 fb2756b9 2018-08-04 stsp struct tog_diff_view_state *s = &view->state.diff;
1640 a3404814 2018-09-02 stsp char *id_str1 = NULL, *id_str2, *header;
1642 a3404814 2018-09-02 stsp if (s->id1) {
1643 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str1, s->id1);
1645 a3404814 2018-09-02 stsp return err;
1647 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str2, s->id2);
1649 a3404814 2018-09-02 stsp return err;
1651 a3404814 2018-09-02 stsp if (asprintf(&header, "diff: %s %s",
1652 a3404814 2018-09-02 stsp id_str1 ? id_str1 : "/dev/null", id_str2) == -1) {
1653 a3404814 2018-09-02 stsp err = got_error_from_errno();
1654 a3404814 2018-09-02 stsp free(id_str1);
1655 a3404814 2018-09-02 stsp free(id_str2);
1656 a3404814 2018-09-02 stsp return err;
1658 a3404814 2018-09-02 stsp free(id_str1);
1659 a3404814 2018-09-02 stsp free(id_str2);
1661 e5a0f69f 2018-08-18 stsp return draw_file(view, s->f, &s->first_displayed_line,
1662 a3404814 2018-09-02 stsp &s->last_displayed_line, &s->eof, view->nlines,
1666 e5a0f69f 2018-08-18 stsp static const struct got_error *
1667 bcbd79e2 2018-08-19 stsp input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
1668 e5a0f69f 2018-08-18 stsp struct tog_view *view, int ch)
1670 bcbd79e2 2018-08-19 stsp const struct got_error *err = NULL;
1671 e5a0f69f 2018-08-18 stsp struct tog_diff_view_state *s = &view->state.diff;
1674 e5a0f69f 2018-08-18 stsp switch (ch) {
1676 e5a0f69f 2018-08-18 stsp case KEY_UP:
1677 e5a0f69f 2018-08-18 stsp if (s->first_displayed_line > 1)
1678 e5a0f69f 2018-08-18 stsp s->first_displayed_line--;
1680 e5a0f69f 2018-08-18 stsp case KEY_PPAGE:
1682 e5a0f69f 2018-08-18 stsp while (i++ < view->nlines - 1 &&
1683 e5a0f69f 2018-08-18 stsp s->first_displayed_line > 1)
1684 e5a0f69f 2018-08-18 stsp s->first_displayed_line--;
1687 e5a0f69f 2018-08-18 stsp case KEY_DOWN:
1688 e5a0f69f 2018-08-18 stsp if (!s->eof)
1689 e5a0f69f 2018-08-18 stsp s->first_displayed_line++;
1691 e5a0f69f 2018-08-18 stsp case KEY_NPAGE:
1694 e5a0f69f 2018-08-18 stsp while (!s->eof && i++ < view->nlines - 1) {
1695 e5a0f69f 2018-08-18 stsp char *line;
1696 e5a0f69f 2018-08-18 stsp line = parse_next_line(s->f, NULL);
1697 e5a0f69f 2018-08-18 stsp s->first_displayed_line++;
1698 e5a0f69f 2018-08-18 stsp if (line == NULL)
1703 bcbd79e2 2018-08-19 stsp case ']': {
1704 bcbd79e2 2018-08-19 stsp struct tog_log_view_state *ls;
1705 bcbd79e2 2018-08-19 stsp struct commit_queue_entry *entry;
1706 bcbd79e2 2018-08-19 stsp struct tog_view *diff_view;
1708 bcbd79e2 2018-08-19 stsp if (view->parent == NULL)
1710 bcbd79e2 2018-08-19 stsp if (view->parent->type != TOG_VIEW_LOG)
1712 bcbd79e2 2018-08-19 stsp ls = &view->parent->state.log;
1714 bcbd79e2 2018-08-19 stsp if (ch == '[') {
1715 bcbd79e2 2018-08-19 stsp entry = TAILQ_PREV(ls->selected_entry,
1716 bcbd79e2 2018-08-19 stsp commit_queue_head, entry);
1718 bcbd79e2 2018-08-19 stsp entry = TAILQ_NEXT(ls->selected_entry, entry);
1719 bcbd79e2 2018-08-19 stsp if (entry == NULL) {
1720 bcbd79e2 2018-08-19 stsp err = fetch_next_commit(&entry,
1721 bcbd79e2 2018-08-19 stsp ls->selected_entry,
1722 bcbd79e2 2018-08-19 stsp &ls->commits, ls->graph,
1723 bcbd79e2 2018-08-19 stsp ls->repo, ls->in_repo_path);
1728 bcbd79e2 2018-08-19 stsp if (entry == NULL)
1730 bcbd79e2 2018-08-19 stsp err = show_commit(&diff_view, view->parent,
1731 bcbd79e2 2018-08-19 stsp entry, ls->repo);
1734 bcbd79e2 2018-08-19 stsp *new_view = diff_view;
1735 bcbd79e2 2018-08-19 stsp *dead_view = view;
1742 bcbd79e2 2018-08-19 stsp return err;
1745 4ed7e80c 2018-05-20 stsp static const struct got_error *
1746 9f7d7167 2018-04-29 stsp cmd_diff(int argc, char *argv[])
1748 26ed57b2 2018-05-19 stsp const struct got_error *error = NULL;
1749 26ed57b2 2018-05-19 stsp struct got_repository *repo = NULL;
1750 26ed57b2 2018-05-19 stsp struct got_object *obj1 = NULL, *obj2 = NULL;
1751 26ed57b2 2018-05-19 stsp char *repo_path = NULL;
1752 26ed57b2 2018-05-19 stsp char *obj_id_str1 = NULL, *obj_id_str2 = NULL;
1754 ea5e7bb5 2018-08-01 stsp struct tog_view *view;
1756 26ed57b2 2018-05-19 stsp #ifndef PROFILE
1757 ad242220 2018-09-08 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd", NULL)
1759 26ed57b2 2018-05-19 stsp err(1, "pledge");
1762 26ed57b2 2018-05-19 stsp while ((ch = getopt(argc, argv, "")) != -1) {
1763 26ed57b2 2018-05-19 stsp switch (ch) {
1766 26ed57b2 2018-05-19 stsp /* NOTREACHED */
1770 26ed57b2 2018-05-19 stsp argc -= optind;
1771 26ed57b2 2018-05-19 stsp argv += optind;
1773 26ed57b2 2018-05-19 stsp if (argc == 0) {
1774 26ed57b2 2018-05-19 stsp usage_diff(); /* TODO show local worktree changes */
1775 26ed57b2 2018-05-19 stsp } else if (argc == 2) {
1776 26ed57b2 2018-05-19 stsp repo_path = getcwd(NULL, 0);
1777 26ed57b2 2018-05-19 stsp if (repo_path == NULL)
1778 26ed57b2 2018-05-19 stsp return got_error_from_errno();
1779 26ed57b2 2018-05-19 stsp obj_id_str1 = argv[0];
1780 26ed57b2 2018-05-19 stsp obj_id_str2 = argv[1];
1781 26ed57b2 2018-05-19 stsp } else if (argc == 3) {
1782 26ed57b2 2018-05-19 stsp repo_path = realpath(argv[0], NULL);
1783 26ed57b2 2018-05-19 stsp if (repo_path == NULL)
1784 26ed57b2 2018-05-19 stsp return got_error_from_errno();
1785 26ed57b2 2018-05-19 stsp obj_id_str1 = argv[1];
1786 26ed57b2 2018-05-19 stsp obj_id_str2 = argv[2];
1788 26ed57b2 2018-05-19 stsp usage_diff();
1790 26ed57b2 2018-05-19 stsp error = got_repo_open(&repo, repo_path);
1791 26ed57b2 2018-05-19 stsp free(repo_path);
1792 26ed57b2 2018-05-19 stsp if (error)
1793 26ed57b2 2018-05-19 stsp goto done;
1795 26ed57b2 2018-05-19 stsp error = got_object_open_by_id_str(&obj1, repo, obj_id_str1);
1796 26ed57b2 2018-05-19 stsp if (error)
1797 26ed57b2 2018-05-19 stsp goto done;
1799 26ed57b2 2018-05-19 stsp error = got_object_open_by_id_str(&obj2, repo, obj_id_str2);
1800 26ed57b2 2018-05-19 stsp if (error)
1801 26ed57b2 2018-05-19 stsp goto done;
1803 b3665f43 2018-08-04 stsp view = view_open(0, 0, 0, 0, NULL, TOG_VIEW_DIFF);
1804 ea5e7bb5 2018-08-01 stsp if (view == NULL) {
1805 ea5e7bb5 2018-08-01 stsp error = got_error_from_errno();
1806 ea5e7bb5 2018-08-01 stsp goto done;
1808 5dc9f4bc 2018-08-04 stsp error = open_diff_view(view, obj1, obj2, repo);
1809 5dc9f4bc 2018-08-04 stsp if (error)
1810 5dc9f4bc 2018-08-04 stsp goto done;
1811 e5a0f69f 2018-08-18 stsp error = view_loop(view);
1813 26ed57b2 2018-05-19 stsp got_repo_close(repo);
1815 26ed57b2 2018-05-19 stsp got_object_close(obj1);
1817 26ed57b2 2018-05-19 stsp got_object_close(obj2);
1818 26ed57b2 2018-05-19 stsp return error;
1821 4ed7e80c 2018-05-20 stsp __dead static void
1822 9f7d7167 2018-04-29 stsp usage_blame(void)
1825 69069811 2018-08-02 stsp fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
1826 9f7d7167 2018-04-29 stsp getprogname());
1830 84451b3e 2018-07-10 stsp struct tog_blame_line {
1831 84451b3e 2018-07-10 stsp int annotated;
1832 84451b3e 2018-07-10 stsp struct got_object_id *id;
1835 4ed7e80c 2018-05-20 stsp static const struct got_error *
1836 f7d12f7e 2018-08-01 stsp draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
1837 f7d12f7e 2018-08-01 stsp const char *path, struct tog_blame_line *lines, int nlines,
1838 f7d12f7e 2018-08-01 stsp int blame_complete, int selected_line, int *first_displayed_line,
1839 f7d12f7e 2018-08-01 stsp int *last_displayed_line, int *eof, int max_lines)
1841 84451b3e 2018-07-10 stsp const struct got_error *err;
1842 84451b3e 2018-07-10 stsp int lineno = 0, nprinted = 0;
1843 84451b3e 2018-07-10 stsp char *line;
1844 84451b3e 2018-07-10 stsp size_t len;
1845 84451b3e 2018-07-10 stsp wchar_t *wline;
1846 b700b5d6 2018-07-10 stsp int width, wlimit;
1847 84451b3e 2018-07-10 stsp struct tog_blame_line *blame_line;
1848 ee41ec32 2018-07-10 stsp struct got_object_id *prev_id = NULL;
1849 ab089a2a 2018-07-12 stsp char *id_str;
1851 ab089a2a 2018-07-12 stsp err = got_object_id_str(&id_str, id);
1853 ab089a2a 2018-07-12 stsp return err;
1855 84451b3e 2018-07-10 stsp rewind(f);
1856 f7d12f7e 2018-08-01 stsp werase(view->window);
1858 ab089a2a 2018-07-12 stsp if (asprintf(&line, "commit: %s", id_str) == -1) {
1859 ab089a2a 2018-07-12 stsp err = got_error_from_errno();
1860 ab089a2a 2018-07-12 stsp free(id_str);
1861 ab089a2a 2018-07-12 stsp return err;
1864 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, line, view->ncols);
1865 ab089a2a 2018-07-12 stsp free(line);
1866 2550e4c3 2018-07-13 stsp line = NULL;
1867 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
1868 a3404814 2018-09-02 stsp wstandout(view->window);
1869 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
1870 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
1871 a3404814 2018-09-02 stsp wstandend(view->window);
1872 2550e4c3 2018-07-13 stsp free(wline);
1873 2550e4c3 2018-07-13 stsp wline = NULL;
1874 f7d12f7e 2018-08-01 stsp if (width < view->ncols)
1875 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
1877 084063cd 2018-07-12 stsp if (asprintf(&line, "[%d/%d] %s%s",
1878 084063cd 2018-07-12 stsp *first_displayed_line - 1 + selected_line, nlines,
1879 12a0b23b 2018-07-12 stsp blame_complete ? "" : "annotating ", path) == -1) {
1880 ab089a2a 2018-07-12 stsp free(id_str);
1881 3f60a8ef 2018-07-10 stsp return got_error_from_errno();
1883 ab089a2a 2018-07-12 stsp free(id_str);
1884 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, line, view->ncols);
1885 3f60a8ef 2018-07-10 stsp free(line);
1886 2550e4c3 2018-07-13 stsp line = NULL;
1888 3f60a8ef 2018-07-10 stsp return err;
1889 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
1890 2550e4c3 2018-07-13 stsp free(wline);
1891 2550e4c3 2018-07-13 stsp wline = NULL;
1892 f7d12f7e 2018-08-01 stsp if (width < view->ncols)
1893 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
1896 ab089a2a 2018-07-12 stsp while (nprinted < max_lines - 2) {
1897 84451b3e 2018-07-10 stsp line = parse_next_line(f, &len);
1898 84451b3e 2018-07-10 stsp if (line == NULL) {
1902 84451b3e 2018-07-10 stsp if (++lineno < *first_displayed_line) {
1903 84451b3e 2018-07-10 stsp free(line);
1907 f7d12f7e 2018-08-01 stsp wlimit = view->ncols < 9 ? 0 : view->ncols - 9;
1908 b700b5d6 2018-07-10 stsp err = format_line(&wline, &width, line, wlimit);
1909 84451b3e 2018-07-10 stsp if (err) {
1910 84451b3e 2018-07-10 stsp free(line);
1911 84451b3e 2018-07-10 stsp return err;
1914 b700b5d6 2018-07-10 stsp if (nprinted == selected_line - 1)
1915 f7d12f7e 2018-08-01 stsp wstandout(view->window);
1917 84451b3e 2018-07-10 stsp blame_line = &lines[lineno - 1];
1918 ee41ec32 2018-07-10 stsp if (blame_line->annotated && prev_id &&
1919 ee41ec32 2018-07-10 stsp got_object_id_cmp(prev_id, blame_line->id) == 0)
1920 f7d12f7e 2018-08-01 stsp waddstr(view->window, " ");
1921 ee41ec32 2018-07-10 stsp else if (blame_line->annotated) {
1922 84451b3e 2018-07-10 stsp char *id_str;
1923 84451b3e 2018-07-10 stsp err = got_object_id_str(&id_str, blame_line->id);
1924 84451b3e 2018-07-10 stsp if (err) {
1925 84451b3e 2018-07-10 stsp free(line);
1926 2550e4c3 2018-07-13 stsp free(wline);
1927 84451b3e 2018-07-10 stsp return err;
1929 f7d12f7e 2018-08-01 stsp wprintw(view->window, "%.8s ", id_str);
1930 84451b3e 2018-07-10 stsp free(id_str);
1931 ee41ec32 2018-07-10 stsp prev_id = blame_line->id;
1933 f7d12f7e 2018-08-01 stsp waddstr(view->window, "........ ");
1934 ee41ec32 2018-07-10 stsp prev_id = NULL;
1937 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
1938 b700b5d6 2018-07-10 stsp while (width < wlimit) {
1939 f7d12f7e 2018-08-01 stsp waddch(view->window, ' ');
1942 b700b5d6 2018-07-10 stsp if (nprinted == selected_line - 1)
1943 f7d12f7e 2018-08-01 stsp wstandend(view->window);
1944 84451b3e 2018-07-10 stsp if (++nprinted == 1)
1945 84451b3e 2018-07-10 stsp *first_displayed_line = lineno;
1946 84451b3e 2018-07-10 stsp free(line);
1947 2550e4c3 2018-07-13 stsp free(wline);
1948 2550e4c3 2018-07-13 stsp wline = NULL;
1950 84451b3e 2018-07-10 stsp *last_displayed_line = lineno;
1952 1a57306a 2018-09-02 stsp view_vborder(view);
1954 84451b3e 2018-07-10 stsp return NULL;
1957 84451b3e 2018-07-10 stsp static const struct got_error *
1958 84451b3e 2018-07-10 stsp blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
1960 84451b3e 2018-07-10 stsp const struct got_error *err = NULL;
1961 84451b3e 2018-07-10 stsp struct tog_blame_cb_args *a = arg;
1962 84451b3e 2018-07-10 stsp struct tog_blame_line *line;
1964 d68a0a7d 2018-07-10 stsp if (nlines != a->nlines ||
1965 d68a0a7d 2018-07-10 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
1966 84451b3e 2018-07-10 stsp return got_error(GOT_ERR_RANGE);
1968 84451b3e 2018-07-10 stsp if (pthread_mutex_lock(a->mutex) != 0)
1969 84451b3e 2018-07-10 stsp return got_error_from_errno();
1971 18430de3 2018-07-10 stsp if (*a->quit) { /* user has quit the blame view */
1972 d68a0a7d 2018-07-10 stsp err = got_error(GOT_ERR_ITER_COMPLETED);
1973 d68a0a7d 2018-07-10 stsp goto done;
1976 d68a0a7d 2018-07-10 stsp if (lineno == -1)
1977 d68a0a7d 2018-07-10 stsp goto done; /* no change in this commit */
1979 84451b3e 2018-07-10 stsp line = &a->lines[lineno - 1];
1980 d68a0a7d 2018-07-10 stsp if (line->annotated)
1981 d68a0a7d 2018-07-10 stsp goto done;
1983 84451b3e 2018-07-10 stsp line->id = got_object_id_dup(id);
1984 84451b3e 2018-07-10 stsp if (line->id == NULL) {
1985 84451b3e 2018-07-10 stsp err = got_error_from_errno();
1986 84451b3e 2018-07-10 stsp goto done;
1988 84451b3e 2018-07-10 stsp line->annotated = 1;
1990 f7d12f7e 2018-08-01 stsp err = draw_blame(a->view, a->commit_id, a->f, a->path,
1991 245d91c1 2018-07-12 stsp a->lines, a->nlines, 0, *a->selected_line, a->first_displayed_line,
1992 e5a0f69f 2018-08-18 stsp a->last_displayed_line, a->eof, a->view->nlines);
1994 84451b3e 2018-07-10 stsp if (pthread_mutex_unlock(a->mutex) != 0)
1995 84451b3e 2018-07-10 stsp return got_error_from_errno();
1996 84451b3e 2018-07-10 stsp return err;
1999 84451b3e 2018-07-10 stsp static void *
2000 84451b3e 2018-07-10 stsp blame_thread(void *arg)
2002 18430de3 2018-07-10 stsp const struct got_error *err;
2003 18430de3 2018-07-10 stsp struct tog_blame_thread_args *ta = arg;
2004 245d91c1 2018-07-12 stsp struct tog_blame_cb_args *a = ta->cb_args;
2006 ab089a2a 2018-07-12 stsp err = got_blame_incremental(ta->path, a->commit_id, ta->repo,
2007 245d91c1 2018-07-12 stsp blame_cb, ta->cb_args);
2009 18430de3 2018-07-10 stsp if (pthread_mutex_lock(a->mutex) != 0)
2010 18430de3 2018-07-10 stsp return (void *)got_error_from_errno();
2012 c9beca56 2018-07-22 stsp got_repo_close(ta->repo);
2013 c9beca56 2018-07-22 stsp ta->repo = NULL;
2014 c9beca56 2018-07-22 stsp *ta->complete = 1;
2016 f7d12f7e 2018-08-01 stsp err = draw_blame(a->view, a->commit_id, a->f, a->path,
2017 f7d12f7e 2018-08-01 stsp a->lines, a->nlines, 1, *a->selected_line,
2018 e5a0f69f 2018-08-18 stsp a->first_displayed_line, a->last_displayed_line, a->eof,
2019 f7d12f7e 2018-08-01 stsp a->view->nlines);
2021 18430de3 2018-07-10 stsp if (pthread_mutex_unlock(a->mutex) != 0 && err == NULL)
2022 18430de3 2018-07-10 stsp err = got_error_from_errno();
2024 18430de3 2018-07-10 stsp return (void *)err;
2027 245d91c1 2018-07-12 stsp static struct got_object_id *
2028 245d91c1 2018-07-12 stsp get_selected_commit_id(struct tog_blame_line *lines,
2029 245d91c1 2018-07-12 stsp int first_displayed_line, int selected_line)
2031 245d91c1 2018-07-12 stsp struct tog_blame_line *line;
2033 245d91c1 2018-07-12 stsp line = &lines[first_displayed_line - 1 + selected_line - 1];
2034 245d91c1 2018-07-12 stsp if (!line->annotated)
2035 245d91c1 2018-07-12 stsp return NULL;
2037 245d91c1 2018-07-12 stsp return line->id;
2040 84451b3e 2018-07-10 stsp static const struct got_error *
2041 245d91c1 2018-07-12 stsp open_selected_commit(struct got_object **pobj, struct got_object **obj,
2042 b880a918 2018-07-10 stsp struct tog_blame_line *lines, int first_displayed_line,
2043 b880a918 2018-07-10 stsp int selected_line, struct got_repository *repo)
2045 b880a918 2018-07-10 stsp const struct got_error *err = NULL;
2046 b880a918 2018-07-10 stsp struct got_commit_object *commit = NULL;
2047 245d91c1 2018-07-12 stsp struct got_object_id *selected_id;
2048 b880a918 2018-07-10 stsp struct got_object_qid *pid;
2050 b880a918 2018-07-10 stsp *pobj = NULL;
2051 b880a918 2018-07-10 stsp *obj = NULL;
2053 245d91c1 2018-07-12 stsp selected_id = get_selected_commit_id(lines,
2054 245d91c1 2018-07-12 stsp first_displayed_line, selected_line);
2055 245d91c1 2018-07-12 stsp if (selected_id == NULL)
2056 b880a918 2018-07-10 stsp return NULL;
2058 245d91c1 2018-07-12 stsp err = got_object_open(obj, repo, selected_id);
2060 b880a918 2018-07-10 stsp goto done;
2062 b880a918 2018-07-10 stsp err = got_object_commit_open(&commit, repo, *obj);
2064 b880a918 2018-07-10 stsp goto done;
2066 b880a918 2018-07-10 stsp pid = SIMPLEQ_FIRST(&commit->parent_ids);
2067 b880a918 2018-07-10 stsp if (pid) {
2068 b880a918 2018-07-10 stsp err = got_object_open(pobj, repo, pid->id);
2070 b880a918 2018-07-10 stsp goto done;
2073 b880a918 2018-07-10 stsp if (commit)
2074 b880a918 2018-07-10 stsp got_object_commit_close(commit);
2075 b880a918 2018-07-10 stsp return err;
2078 b880a918 2018-07-10 stsp static const struct got_error *
2079 245d91c1 2018-07-12 stsp stop_blame(struct tog_blame *blame)
2081 a70480e0 2018-06-23 stsp const struct got_error *err = NULL;
2084 245d91c1 2018-07-12 stsp if (blame->thread) {
2085 245d91c1 2018-07-12 stsp if (pthread_join(blame->thread, (void **)&err) != 0)
2086 245d91c1 2018-07-12 stsp err = got_error_from_errno();
2087 245d91c1 2018-07-12 stsp if (err && err->code == GOT_ERR_ITER_COMPLETED)
2088 245d91c1 2018-07-12 stsp err = NULL;
2089 245d91c1 2018-07-12 stsp blame->thread = NULL;
2091 245d91c1 2018-07-12 stsp if (blame->thread_args.repo) {
2092 245d91c1 2018-07-12 stsp got_repo_close(blame->thread_args.repo);
2093 245d91c1 2018-07-12 stsp blame->thread_args.repo = NULL;
2095 245d91c1 2018-07-12 stsp if (blame->f) {
2096 245d91c1 2018-07-12 stsp fclose(blame->f);
2097 245d91c1 2018-07-12 stsp blame->f = NULL;
2099 245d91c1 2018-07-12 stsp for (i = 0; i < blame->nlines; i++)
2100 245d91c1 2018-07-12 stsp free(blame->lines[i].id);
2101 245d91c1 2018-07-12 stsp free(blame->lines);
2102 245d91c1 2018-07-12 stsp blame->lines = NULL;
2103 75c32340 2018-07-23 stsp free(blame->cb_args.commit_id);
2104 75c32340 2018-07-23 stsp blame->cb_args.commit_id = NULL;
2106 245d91c1 2018-07-12 stsp return err;
2109 245d91c1 2018-07-12 stsp static const struct got_error *
2110 7cc84d77 2018-08-01 stsp run_blame(struct tog_blame *blame, pthread_mutex_t *mutex,
2111 7cc84d77 2018-08-01 stsp struct tog_view *view, int *blame_complete,
2112 245d91c1 2018-07-12 stsp int *first_displayed_line, int *last_displayed_line,
2113 e5a0f69f 2018-08-18 stsp int *selected_line, int *done, int *eof, const char *path,
2114 245d91c1 2018-07-12 stsp struct got_object_id *commit_id,
2115 245d91c1 2018-07-12 stsp struct got_repository *repo)
2117 245d91c1 2018-07-12 stsp const struct got_error *err = NULL;
2118 84451b3e 2018-07-10 stsp struct got_blob_object *blob = NULL;
2119 245d91c1 2018-07-12 stsp struct got_repository *thread_repo = NULL;
2120 245d91c1 2018-07-12 stsp struct got_object *obj;
2122 84451b3e 2018-07-10 stsp err = got_object_open_by_path(&obj, repo, commit_id, path);
2124 84451b3e 2018-07-10 stsp goto done;
2125 84451b3e 2018-07-10 stsp if (got_object_get_type(obj) != GOT_OBJ_TYPE_BLOB) {
2126 84451b3e 2018-07-10 stsp err = got_error(GOT_ERR_OBJ_TYPE);
2127 84451b3e 2018-07-10 stsp goto done;
2130 84451b3e 2018-07-10 stsp err = got_object_blob_open(&blob, repo, obj, 8192);
2132 a70480e0 2018-06-23 stsp goto done;
2133 245d91c1 2018-07-12 stsp blame->f = got_opentemp();
2134 245d91c1 2018-07-12 stsp if (blame->f == NULL) {
2135 84451b3e 2018-07-10 stsp err = got_error_from_errno();
2136 84451b3e 2018-07-10 stsp goto done;
2138 245d91c1 2018-07-12 stsp err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
2139 245d91c1 2018-07-12 stsp blame->f, blob);
2141 84451b3e 2018-07-10 stsp goto done;
2143 245d91c1 2018-07-12 stsp blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
2144 245d91c1 2018-07-12 stsp if (blame->lines == NULL) {
2145 84451b3e 2018-07-10 stsp err = got_error_from_errno();
2146 84451b3e 2018-07-10 stsp goto done;
2149 245d91c1 2018-07-12 stsp err = got_repo_open(&thread_repo, got_repo_get_path(repo));
2151 bd24772e 2018-07-11 stsp goto done;
2153 7cc84d77 2018-08-01 stsp blame->cb_args.view = view;
2154 245d91c1 2018-07-12 stsp blame->cb_args.lines = blame->lines;
2155 245d91c1 2018-07-12 stsp blame->cb_args.nlines = blame->nlines;
2156 245d91c1 2018-07-12 stsp blame->cb_args.mutex = mutex;
2157 245d91c1 2018-07-12 stsp blame->cb_args.commit_id = got_object_id_dup(commit_id);
2158 245d91c1 2018-07-12 stsp if (blame->cb_args.commit_id == NULL) {
2159 245d91c1 2018-07-12 stsp err = got_error_from_errno();
2160 245d91c1 2018-07-12 stsp goto done;
2162 245d91c1 2018-07-12 stsp blame->cb_args.f = blame->f;
2163 245d91c1 2018-07-12 stsp blame->cb_args.path = path;
2164 245d91c1 2018-07-12 stsp blame->cb_args.first_displayed_line = first_displayed_line;
2165 245d91c1 2018-07-12 stsp blame->cb_args.selected_line = selected_line;
2166 245d91c1 2018-07-12 stsp blame->cb_args.last_displayed_line = last_displayed_line;
2167 245d91c1 2018-07-12 stsp blame->cb_args.quit = done;
2168 e5a0f69f 2018-08-18 stsp blame->cb_args.eof = eof;
2170 245d91c1 2018-07-12 stsp blame->thread_args.path = path;
2171 245d91c1 2018-07-12 stsp blame->thread_args.repo = thread_repo;
2172 245d91c1 2018-07-12 stsp blame->thread_args.cb_args = &blame->cb_args;
2173 245d91c1 2018-07-12 stsp blame->thread_args.complete = blame_complete;
2174 245d91c1 2018-07-12 stsp *blame_complete = 0;
2176 245d91c1 2018-07-12 stsp if (pthread_create(&blame->thread, NULL, blame_thread,
2177 245d91c1 2018-07-12 stsp &blame->thread_args) != 0) {
2178 245d91c1 2018-07-12 stsp err = got_error_from_errno();
2179 245d91c1 2018-07-12 stsp goto done;
2184 245d91c1 2018-07-12 stsp got_object_blob_close(blob);
2186 245d91c1 2018-07-12 stsp got_object_close(obj);
2188 245d91c1 2018-07-12 stsp stop_blame(blame);
2189 245d91c1 2018-07-12 stsp return err;
2192 245d91c1 2018-07-12 stsp static const struct got_error *
2193 e5a0f69f 2018-08-18 stsp open_blame_view(struct tog_view *view, char *path,
2194 e1cd8fed 2018-08-01 stsp struct got_object_id *commit_id, struct got_repository *repo)
2196 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL;
2197 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
2199 fb2756b9 2018-08-04 stsp SIMPLEQ_INIT(&s->blamed_commits);
2201 fb2756b9 2018-08-04 stsp if (pthread_mutex_init(&s->mutex, NULL) != 0)
2202 7cbe629d 2018-08-04 stsp return got_error_from_errno();
2204 fb2756b9 2018-08-04 stsp err = got_object_qid_alloc(&s->blamed_commit, commit_id);
2206 7cbe629d 2018-08-04 stsp return err;
2208 fb2756b9 2018-08-04 stsp SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
2209 fb2756b9 2018-08-04 stsp s->first_displayed_line = 1;
2210 fb2756b9 2018-08-04 stsp s->last_displayed_line = view->nlines;
2211 fb2756b9 2018-08-04 stsp s->selected_line = 1;
2212 fb2756b9 2018-08-04 stsp s->blame_complete = 0;
2213 fb2756b9 2018-08-04 stsp s->path = path;
2214 e5a0f69f 2018-08-18 stsp if (s->path == NULL)
2215 e5a0f69f 2018-08-18 stsp return got_error_from_errno();
2216 fb2756b9 2018-08-04 stsp s->repo = repo;
2217 fb2756b9 2018-08-04 stsp s->commit_id = commit_id;
2218 e9424729 2018-08-04 stsp memset(&s->blame, 0, sizeof(s->blame));
2220 e5a0f69f 2018-08-18 stsp view->show = show_blame_view;
2221 e5a0f69f 2018-08-18 stsp view->input = input_blame_view;
2222 e5a0f69f 2018-08-18 stsp view->close = close_blame_view;
2224 e5a0f69f 2018-08-18 stsp return run_blame(&s->blame, &s->mutex, view, &s->blame_complete,
2225 e5a0f69f 2018-08-18 stsp &s->first_displayed_line, &s->last_displayed_line,
2226 e5a0f69f 2018-08-18 stsp &s->selected_line, &s->done, &s->eof, s->path,
2227 e5a0f69f 2018-08-18 stsp s->blamed_commit->id, s->repo);
2230 e5a0f69f 2018-08-18 stsp static const struct got_error *
2231 7cbe629d 2018-08-04 stsp close_blame_view(struct tog_view *view)
2233 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
2234 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
2236 e5a0f69f 2018-08-18 stsp if (s->blame.thread)
2237 e5a0f69f 2018-08-18 stsp err = stop_blame(&s->blame);
2239 fb2756b9 2018-08-04 stsp while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
2240 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
2241 fb2756b9 2018-08-04 stsp blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
2242 fb2756b9 2018-08-04 stsp SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
2243 7cbe629d 2018-08-04 stsp got_object_qid_free(blamed_commit);
2246 e5a0f69f 2018-08-18 stsp free(s->path);
2248 e5a0f69f 2018-08-18 stsp return err;
2251 7cbe629d 2018-08-04 stsp static const struct got_error *
2252 7cbe629d 2018-08-04 stsp show_blame_view(struct tog_view *view)
2254 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
2255 e5a0f69f 2018-08-18 stsp struct tog_blame_view_state *s = &view->state.blame;
2257 e5a0f69f 2018-08-18 stsp if (pthread_mutex_lock(&s->mutex) != 0)
2258 e5a0f69f 2018-08-18 stsp return got_error_from_errno();
2260 e5a0f69f 2018-08-18 stsp err = draw_blame(view, s->blamed_commit->id, s->blame.f,
2261 e5a0f69f 2018-08-18 stsp s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
2262 e5a0f69f 2018-08-18 stsp s->selected_line, &s->first_displayed_line,
2263 e5a0f69f 2018-08-18 stsp &s->last_displayed_line, &s->eof, view->nlines);
2265 e5a0f69f 2018-08-18 stsp if (pthread_mutex_unlock(&s->mutex) != 0 && err == NULL)
2266 e5a0f69f 2018-08-18 stsp err = got_error_from_errno();
2268 e5a0f69f 2018-08-18 stsp return err;
2271 e5a0f69f 2018-08-18 stsp static const struct got_error *
2272 e5a0f69f 2018-08-18 stsp input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
2273 e5a0f69f 2018-08-18 stsp struct tog_view *view, int ch)
2275 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL, *thread_err = NULL;
2276 7cbe629d 2018-08-04 stsp struct got_object *obj = NULL, *pobj = NULL;
2277 7cbe629d 2018-08-04 stsp struct tog_view *diff_view;
2278 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
2280 e5a0f69f 2018-08-18 stsp if (pthread_mutex_lock(&s->mutex) != 0) {
2281 e5a0f69f 2018-08-18 stsp err = got_error_from_errno();
2282 e5a0f69f 2018-08-18 stsp goto done;
2285 e5a0f69f 2018-08-18 stsp switch (ch) {
2287 e5a0f69f 2018-08-18 stsp s->done = 1;
2288 e5a0f69f 2018-08-18 stsp if (pthread_mutex_unlock(&s->mutex) != 0) {
2289 e5a0f69f 2018-08-18 stsp err = got_error_from_errno();
2290 e5a0f69f 2018-08-18 stsp goto done;
2292 e5a0f69f 2018-08-18 stsp return stop_blame(&s->blame);
2294 e5a0f69f 2018-08-18 stsp case KEY_UP:
2295 e5a0f69f 2018-08-18 stsp if (s->selected_line > 1)
2296 e5a0f69f 2018-08-18 stsp s->selected_line--;
2297 e5a0f69f 2018-08-18 stsp else if (s->selected_line == 1 &&
2298 e5a0f69f 2018-08-18 stsp s->first_displayed_line > 1)
2299 e5a0f69f 2018-08-18 stsp s->first_displayed_line--;
2301 e5a0f69f 2018-08-18 stsp case KEY_PPAGE:
2302 e5a0f69f 2018-08-18 stsp if (s->first_displayed_line == 1) {
2303 e5a0f69f 2018-08-18 stsp s->selected_line = 1;
2306 e5a0f69f 2018-08-18 stsp if (s->first_displayed_line > view->nlines - 2)
2307 e5a0f69f 2018-08-18 stsp s->first_displayed_line -=
2308 e5a0f69f 2018-08-18 stsp (view->nlines - 2);
2310 e5a0f69f 2018-08-18 stsp s->first_displayed_line = 1;
2313 e5a0f69f 2018-08-18 stsp case KEY_DOWN:
2314 e5a0f69f 2018-08-18 stsp if (s->selected_line < view->nlines - 2 &&
2315 e5a0f69f 2018-08-18 stsp s->first_displayed_line +
2316 e5a0f69f 2018-08-18 stsp s->selected_line <= s->blame.nlines)
2317 e5a0f69f 2018-08-18 stsp s->selected_line++;
2318 e5a0f69f 2018-08-18 stsp else if (s->last_displayed_line <
2319 e5a0f69f 2018-08-18 stsp s->blame.nlines)
2320 e5a0f69f 2018-08-18 stsp s->first_displayed_line++;
2323 e5a0f69f 2018-08-18 stsp case 'p': {
2324 e5a0f69f 2018-08-18 stsp struct got_object_id *id;
2325 e5a0f69f 2018-08-18 stsp id = get_selected_commit_id(s->blame.lines,
2326 e5a0f69f 2018-08-18 stsp s->first_displayed_line, s->selected_line);
2327 e5a0f69f 2018-08-18 stsp if (id == NULL || got_object_id_cmp(id,
2328 e5a0f69f 2018-08-18 stsp s->blamed_commit->id) == 0)
2330 e5a0f69f 2018-08-18 stsp err = open_selected_commit(&pobj, &obj,
2331 e5a0f69f 2018-08-18 stsp s->blame.lines, s->first_displayed_line,
2332 e5a0f69f 2018-08-18 stsp s->selected_line, s->repo);
2335 e5a0f69f 2018-08-18 stsp if (pobj == NULL && obj == NULL)
2337 e5a0f69f 2018-08-18 stsp if (ch == 'p' && pobj == NULL)
2339 e5a0f69f 2018-08-18 stsp s->done = 1;
2340 e5a0f69f 2018-08-18 stsp if (pthread_mutex_unlock(&s->mutex) != 0) {
2341 e5a0f69f 2018-08-18 stsp err = got_error_from_errno();
2342 e5a0f69f 2018-08-18 stsp goto done;
2344 e5a0f69f 2018-08-18 stsp thread_err = stop_blame(&s->blame);
2345 e5a0f69f 2018-08-18 stsp s->done = 0;
2346 e5a0f69f 2018-08-18 stsp if (pthread_mutex_lock(&s->mutex) != 0) {
2347 e5a0f69f 2018-08-18 stsp err = got_error_from_errno();
2348 e5a0f69f 2018-08-18 stsp goto done;
2350 e5a0f69f 2018-08-18 stsp if (thread_err)
2352 e5a0f69f 2018-08-18 stsp id = got_object_get_id(ch == 'b' ? obj : pobj);
2353 e5a0f69f 2018-08-18 stsp got_object_close(obj);
2354 e5a0f69f 2018-08-18 stsp obj = NULL;
2355 e5a0f69f 2018-08-18 stsp if (pobj) {
2356 e5a0f69f 2018-08-18 stsp got_object_close(pobj);
2357 e5a0f69f 2018-08-18 stsp pobj = NULL;
2359 e5a0f69f 2018-08-18 stsp if (id == NULL) {
2360 e5a0f69f 2018-08-18 stsp err = got_error_from_errno();
2363 e5a0f69f 2018-08-18 stsp err = got_object_qid_alloc(
2364 e5a0f69f 2018-08-18 stsp &s->blamed_commit, id);
2367 e5a0f69f 2018-08-18 stsp goto done;
2368 e5a0f69f 2018-08-18 stsp SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
2369 e5a0f69f 2018-08-18 stsp s->blamed_commit, entry);
2370 e5a0f69f 2018-08-18 stsp err = run_blame(&s->blame, &s->mutex, view,
2371 e5a0f69f 2018-08-18 stsp &s->blame_complete,
2372 e5a0f69f 2018-08-18 stsp &s->first_displayed_line,
2373 e5a0f69f 2018-08-18 stsp &s->last_displayed_line,
2374 e5a0f69f 2018-08-18 stsp &s->selected_line, &s->done, &s->eof,
2375 e5a0f69f 2018-08-18 stsp s->path, s->blamed_commit->id, s->repo);
2380 e5a0f69f 2018-08-18 stsp case 'B': {
2381 e5a0f69f 2018-08-18 stsp struct got_object_qid *first;
2382 e5a0f69f 2018-08-18 stsp first = SIMPLEQ_FIRST(&s->blamed_commits);
2383 e5a0f69f 2018-08-18 stsp if (!got_object_id_cmp(first->id, s->commit_id))
2385 e5a0f69f 2018-08-18 stsp s->done = 1;
2386 e5a0f69f 2018-08-18 stsp if (pthread_mutex_unlock(&s->mutex) != 0) {
2387 e5a0f69f 2018-08-18 stsp err = got_error_from_errno();
2388 e5a0f69f 2018-08-18 stsp goto done;
2390 e5a0f69f 2018-08-18 stsp thread_err = stop_blame(&s->blame);
2391 e5a0f69f 2018-08-18 stsp s->done = 0;
2392 e5a0f69f 2018-08-18 stsp if (pthread_mutex_lock(&s->mutex) != 0) {
2393 e5a0f69f 2018-08-18 stsp err = got_error_from_errno();
2394 e5a0f69f 2018-08-18 stsp goto done;
2396 e5a0f69f 2018-08-18 stsp if (thread_err)
2398 e5a0f69f 2018-08-18 stsp SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
2399 e5a0f69f 2018-08-18 stsp got_object_qid_free(s->blamed_commit);
2400 e5a0f69f 2018-08-18 stsp s->blamed_commit =
2401 e5a0f69f 2018-08-18 stsp SIMPLEQ_FIRST(&s->blamed_commits);
2402 e5a0f69f 2018-08-18 stsp err = run_blame(&s->blame, &s->mutex, view,
2403 e5a0f69f 2018-08-18 stsp &s->blame_complete,
2404 e5a0f69f 2018-08-18 stsp &s->first_displayed_line,
2405 e5a0f69f 2018-08-18 stsp &s->last_displayed_line,
2406 e5a0f69f 2018-08-18 stsp &s->selected_line, &s->done, &s->eof, s->path,
2407 e5a0f69f 2018-08-18 stsp s->blamed_commit->id, s->repo);
2412 e5a0f69f 2018-08-18 stsp case KEY_ENTER:
2413 e5a0f69f 2018-08-18 stsp case '\r':
2414 e5a0f69f 2018-08-18 stsp err = open_selected_commit(&pobj, &obj,
2415 e5a0f69f 2018-08-18 stsp s->blame.lines, s->first_displayed_line,
2416 e5a0f69f 2018-08-18 stsp s->selected_line, s->repo);
2419 e5a0f69f 2018-08-18 stsp if (pobj == NULL && obj == NULL)
2421 e5a0f69f 2018-08-18 stsp diff_view = view_open(0, 0, 0, 0, view,
2422 e5a0f69f 2018-08-18 stsp TOG_VIEW_DIFF);
2423 e5a0f69f 2018-08-18 stsp if (diff_view == NULL) {
2424 e5a0f69f 2018-08-18 stsp err = got_error_from_errno();
2427 e5a0f69f 2018-08-18 stsp err = open_diff_view(diff_view, pobj, obj,
2429 e5a0f69f 2018-08-18 stsp if (err) {
2430 e5a0f69f 2018-08-18 stsp view_close(diff_view);
2433 e5a0f69f 2018-08-18 stsp *new_view = diff_view;
2434 e5a0f69f 2018-08-18 stsp if (pobj) {
2435 e5a0f69f 2018-08-18 stsp got_object_close(pobj);
2436 e5a0f69f 2018-08-18 stsp pobj = NULL;
2438 e5a0f69f 2018-08-18 stsp got_object_close(obj);
2439 e5a0f69f 2018-08-18 stsp obj = NULL;
2443 e5a0f69f 2018-08-18 stsp case KEY_NPAGE:
2445 e5a0f69f 2018-08-18 stsp if (s->last_displayed_line >= s->blame.nlines &&
2446 e5a0f69f 2018-08-18 stsp s->selected_line < view->nlines - 2) {
2447 e5a0f69f 2018-08-18 stsp s->selected_line = MIN(s->blame.nlines,
2448 e5a0f69f 2018-08-18 stsp view->nlines - 2);
2451 e5a0f69f 2018-08-18 stsp if (s->last_displayed_line + view->nlines - 2
2452 e5a0f69f 2018-08-18 stsp <= s->blame.nlines)
2453 e5a0f69f 2018-08-18 stsp s->first_displayed_line +=
2454 e5a0f69f 2018-08-18 stsp view->nlines - 2;
2456 e5a0f69f 2018-08-18 stsp s->first_displayed_line =
2457 e5a0f69f 2018-08-18 stsp s->blame.nlines -
2458 e5a0f69f 2018-08-18 stsp (view->nlines - 3);
2460 e5a0f69f 2018-08-18 stsp case KEY_RESIZE:
2461 e5a0f69f 2018-08-18 stsp if (s->selected_line > view->nlines - 2) {
2462 e5a0f69f 2018-08-18 stsp s->selected_line = MIN(s->blame.nlines,
2463 e5a0f69f 2018-08-18 stsp view->nlines - 2);
2470 e5a0f69f 2018-08-18 stsp if (pthread_mutex_unlock(&s->mutex) != 0)
2471 e5a0f69f 2018-08-18 stsp err = got_error_from_errno();
2474 b880a918 2018-07-10 stsp got_object_close(pobj);
2475 245d91c1 2018-07-12 stsp return thread_err ? thread_err : err;
2478 a70480e0 2018-06-23 stsp static const struct got_error *
2479 9f7d7167 2018-04-29 stsp cmd_blame(int argc, char *argv[])
2481 a70480e0 2018-06-23 stsp const struct got_error *error;
2482 a70480e0 2018-06-23 stsp struct got_repository *repo = NULL;
2483 69069811 2018-08-02 stsp char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2484 a70480e0 2018-06-23 stsp struct got_object_id *commit_id = NULL;
2485 a70480e0 2018-06-23 stsp char *commit_id_str = NULL;
2487 e1cd8fed 2018-08-01 stsp struct tog_view *view;
2489 a70480e0 2018-06-23 stsp #ifndef PROFILE
2490 ad242220 2018-09-08 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd", NULL)
2492 a70480e0 2018-06-23 stsp err(1, "pledge");
2495 69069811 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2496 a70480e0 2018-06-23 stsp switch (ch) {
2498 a70480e0 2018-06-23 stsp commit_id_str = optarg;
2501 69069811 2018-08-02 stsp repo_path = realpath(optarg, NULL);
2502 69069811 2018-08-02 stsp if (repo_path == NULL)
2503 69069811 2018-08-02 stsp err(1, "-r option");
2507 a70480e0 2018-06-23 stsp /* NOTREACHED */
2511 a70480e0 2018-06-23 stsp argc -= optind;
2512 a70480e0 2018-06-23 stsp argv += optind;
2514 69069811 2018-08-02 stsp if (argc == 1)
2515 69069811 2018-08-02 stsp path = argv[0];
2517 a70480e0 2018-06-23 stsp usage_blame();
2519 69069811 2018-08-02 stsp cwd = getcwd(NULL, 0);
2520 69069811 2018-08-02 stsp if (cwd == NULL) {
2521 69069811 2018-08-02 stsp error = got_error_from_errno();
2522 69069811 2018-08-02 stsp goto done;
2524 69069811 2018-08-02 stsp if (repo_path == NULL) {
2525 69069811 2018-08-02 stsp repo_path = strdup(cwd);
2526 69069811 2018-08-02 stsp if (repo_path == NULL) {
2527 69069811 2018-08-02 stsp error = got_error_from_errno();
2528 69069811 2018-08-02 stsp goto done;
2533 69069811 2018-08-02 stsp error = got_repo_open(&repo, repo_path);
2534 a70480e0 2018-06-23 stsp if (error != NULL)
2535 66b4983c 2018-06-23 stsp return error;
2537 69069811 2018-08-02 stsp error = got_repo_map_path(&in_repo_path, repo, path);
2538 69069811 2018-08-02 stsp if (error != NULL)
2539 69069811 2018-08-02 stsp goto done;
2541 a70480e0 2018-06-23 stsp if (commit_id_str == NULL) {
2542 a70480e0 2018-06-23 stsp struct got_reference *head_ref;
2543 a70480e0 2018-06-23 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
2544 a70480e0 2018-06-23 stsp if (error != NULL)
2545 66b4983c 2018-06-23 stsp goto done;
2546 a70480e0 2018-06-23 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
2547 a70480e0 2018-06-23 stsp got_ref_close(head_ref);
2549 a70480e0 2018-06-23 stsp struct got_object *obj;
2550 a70480e0 2018-06-23 stsp error = got_object_open_by_id_str(&obj, repo, commit_id_str);
2551 a70480e0 2018-06-23 stsp if (error != NULL)
2552 66b4983c 2018-06-23 stsp goto done;
2553 a70480e0 2018-06-23 stsp commit_id = got_object_get_id(obj);
2554 a19e88aa 2018-06-23 stsp if (commit_id == NULL)
2555 66b4983c 2018-06-23 stsp error = got_error_from_errno();
2556 a19e88aa 2018-06-23 stsp got_object_close(obj);
2558 a19e88aa 2018-06-23 stsp if (error != NULL)
2559 a19e88aa 2018-06-23 stsp goto done;
2561 b3665f43 2018-08-04 stsp view = view_open(0, 0, 0, 0, NULL, TOG_VIEW_BLAME);
2562 e1cd8fed 2018-08-01 stsp if (view == NULL) {
2563 e1cd8fed 2018-08-01 stsp error = got_error_from_errno();
2564 e1cd8fed 2018-08-01 stsp goto done;
2566 7cbe629d 2018-08-04 stsp error = open_blame_view(view, in_repo_path, commit_id, repo);
2567 7cbe629d 2018-08-04 stsp if (error)
2568 7cbe629d 2018-08-04 stsp goto done;
2569 e5a0f69f 2018-08-18 stsp error = view_loop(view);
2571 69069811 2018-08-02 stsp free(repo_path);
2572 69069811 2018-08-02 stsp free(cwd);
2573 a70480e0 2018-06-23 stsp free(commit_id);
2575 a70480e0 2018-06-23 stsp got_repo_close(repo);
2576 a70480e0 2018-06-23 stsp return error;
2579 ffd1d5e5 2018-06-23 stsp static const struct got_error *
2580 f7d12f7e 2018-08-01 stsp draw_tree_entries(struct tog_view *view,
2581 f7d12f7e 2018-08-01 stsp struct got_tree_entry **first_displayed_entry,
2582 ffd1d5e5 2018-06-23 stsp struct got_tree_entry **last_displayed_entry,
2583 ffd1d5e5 2018-06-23 stsp struct got_tree_entry **selected_entry, int *ndisplayed,
2584 f7d12f7e 2018-08-01 stsp const char *label, int show_ids, const char *parent_path,
2585 ce52c690 2018-06-23 stsp const struct got_tree_entries *entries, int selected, int limit, int isroot)
2587 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
2588 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *te;
2589 ffd1d5e5 2018-06-23 stsp wchar_t *wline;
2590 ffd1d5e5 2018-06-23 stsp int width, n;
2592 ffd1d5e5 2018-06-23 stsp *ndisplayed = 0;
2594 f7d12f7e 2018-08-01 stsp werase(view->window);
2596 ffd1d5e5 2018-06-23 stsp if (limit == 0)
2597 ffd1d5e5 2018-06-23 stsp return NULL;
2599 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, label, view->ncols);
2601 ffd1d5e5 2018-06-23 stsp return err;
2602 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2603 a3404814 2018-09-02 stsp wstandout(view->window);
2604 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
2605 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2606 a3404814 2018-09-02 stsp wstandend(view->window);
2607 2550e4c3 2018-07-13 stsp free(wline);
2608 2550e4c3 2018-07-13 stsp wline = NULL;
2609 f7d12f7e 2018-08-01 stsp if (width < view->ncols)
2610 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
2611 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
2612 ffd1d5e5 2018-06-23 stsp return NULL;
2613 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, parent_path, view->ncols);
2615 ce52c690 2018-06-23 stsp return err;
2616 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
2617 2550e4c3 2018-07-13 stsp free(wline);
2618 2550e4c3 2018-07-13 stsp wline = NULL;
2619 f7d12f7e 2018-08-01 stsp if (width < view->ncols)
2620 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
2621 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
2622 ffd1d5e5 2018-06-23 stsp return NULL;
2623 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
2624 a1eca9bb 2018-06-23 stsp if (--limit <= 0)
2625 a1eca9bb 2018-06-23 stsp return NULL;
2627 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_FIRST(&entries->head);
2628 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry == NULL) {
2629 ffd1d5e5 2018-06-23 stsp if (selected == 0) {
2630 f7d12f7e 2018-08-01 stsp wstandout(view->window);
2631 ffd1d5e5 2018-06-23 stsp *selected_entry = NULL;
2633 f7d12f7e 2018-08-01 stsp waddstr(view->window, " ..\n"); /* parent directory */
2634 ffd1d5e5 2018-06-23 stsp if (selected == 0)
2635 f7d12f7e 2018-08-01 stsp wstandend(view->window);
2636 ffd1d5e5 2018-06-23 stsp (*ndisplayed)++;
2637 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
2638 ffd1d5e5 2018-06-23 stsp return NULL;
2642 ffd1d5e5 2018-06-23 stsp while (te != *first_displayed_entry)
2643 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_NEXT(te, entry);
2646 ffd1d5e5 2018-06-23 stsp while (te) {
2647 1d13200f 2018-07-12 stsp char *line = NULL, *id_str = NULL;
2649 1d13200f 2018-07-12 stsp if (show_ids) {
2650 1d13200f 2018-07-12 stsp err = got_object_id_str(&id_str, te->id);
2652 1d13200f 2018-07-12 stsp return got_error_from_errno();
2654 1d13200f 2018-07-12 stsp if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
2655 1d13200f 2018-07-12 stsp te->name, S_ISDIR(te->mode) ? "/" : "") == -1) {
2656 1d13200f 2018-07-12 stsp free(id_str);
2657 ffd1d5e5 2018-06-23 stsp return got_error_from_errno();
2659 1d13200f 2018-07-12 stsp free(id_str);
2660 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, line, view->ncols);
2661 ffd1d5e5 2018-06-23 stsp if (err) {
2662 ffd1d5e5 2018-06-23 stsp free(line);
2665 ffd1d5e5 2018-06-23 stsp if (n == selected) {
2666 f7d12f7e 2018-08-01 stsp wstandout(view->window);
2667 ffd1d5e5 2018-06-23 stsp *selected_entry = te;
2669 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
2670 f7d12f7e 2018-08-01 stsp if (width < view->ncols)
2671 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
2672 ffd1d5e5 2018-06-23 stsp if (n == selected)
2673 f7d12f7e 2018-08-01 stsp wstandend(view->window);
2674 ffd1d5e5 2018-06-23 stsp free(line);
2675 2550e4c3 2018-07-13 stsp free(wline);
2676 2550e4c3 2018-07-13 stsp wline = NULL;
2678 ffd1d5e5 2018-06-23 stsp (*ndisplayed)++;
2679 ffd1d5e5 2018-06-23 stsp *last_displayed_entry = te;
2680 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
2682 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_NEXT(te, entry);
2685 1a57306a 2018-09-02 stsp view_vborder(view);
2686 ffd1d5e5 2018-06-23 stsp return err;
2689 ffd1d5e5 2018-06-23 stsp static void
2690 ffd1d5e5 2018-06-23 stsp tree_scroll_up(struct got_tree_entry **first_displayed_entry, int maxscroll,
2691 ffd1d5e5 2018-06-23 stsp const struct got_tree_entries *entries, int isroot)
2693 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *te, *prev;
2696 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry == NULL)
2699 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_FIRST(&entries->head);
2700 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry == te) {
2701 ffd1d5e5 2018-06-23 stsp if (!isroot)
2702 ffd1d5e5 2018-06-23 stsp *first_displayed_entry = NULL;
2706 ffd1d5e5 2018-06-23 stsp /* XXX this is stupid... switch to TAILQ? */
2707 ffd1d5e5 2018-06-23 stsp for (i = 0; i < maxscroll; i++) {
2708 ffd1d5e5 2018-06-23 stsp while (te != *first_displayed_entry) {
2709 ffd1d5e5 2018-06-23 stsp prev = te;
2710 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_NEXT(te, entry);
2712 ffd1d5e5 2018-06-23 stsp *first_displayed_entry = prev;
2713 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_FIRST(&entries->head);
2715 ffd1d5e5 2018-06-23 stsp if (!isroot && te == SIMPLEQ_FIRST(&entries->head) && i < maxscroll)
2716 ffd1d5e5 2018-06-23 stsp *first_displayed_entry = NULL;
2719 ffd1d5e5 2018-06-23 stsp static void
2720 ffd1d5e5 2018-06-23 stsp tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
2721 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *last_displayed_entry,
2722 ffd1d5e5 2018-06-23 stsp const struct got_tree_entries *entries)
2724 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *next;
2725 ffd1d5e5 2018-06-23 stsp int n = 0;
2727 ffd1d5e5 2018-06-23 stsp if (SIMPLEQ_NEXT(last_displayed_entry, entry) == NULL)
2730 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry)
2731 ffd1d5e5 2018-06-23 stsp next = SIMPLEQ_NEXT(*first_displayed_entry, entry);
2733 ffd1d5e5 2018-06-23 stsp next = SIMPLEQ_FIRST(&entries->head);
2734 ffd1d5e5 2018-06-23 stsp while (next) {
2735 ffd1d5e5 2018-06-23 stsp *first_displayed_entry = next;
2736 ffd1d5e5 2018-06-23 stsp if (++n >= maxscroll)
2738 ffd1d5e5 2018-06-23 stsp next = SIMPLEQ_NEXT(next, entry);
2742 ffd1d5e5 2018-06-23 stsp static const struct got_error *
2743 ce52c690 2018-06-23 stsp tree_entry_path(char **path, struct tog_parent_trees *parents,
2744 ce52c690 2018-06-23 stsp struct got_tree_entry *te)
2746 cb2ebc8a 2018-06-23 stsp const struct got_error *err = NULL;
2747 ffd1d5e5 2018-06-23 stsp struct tog_parent_tree *pt;
2748 ffd1d5e5 2018-06-23 stsp size_t len = 2; /* for leading slash and NUL */
2750 d9765a41 2018-06-23 stsp TAILQ_FOREACH(pt, parents, entry)
2751 ffd1d5e5 2018-06-23 stsp len += strlen(pt->selected_entry->name) + 1 /* slash */;
2753 ce52c690 2018-06-23 stsp len += strlen(te->name);
2755 ce52c690 2018-06-23 stsp *path = calloc(1, len);
2756 ffd1d5e5 2018-06-23 stsp if (path == NULL)
2757 ffd1d5e5 2018-06-23 stsp return got_error_from_errno();
2759 ce52c690 2018-06-23 stsp (*path)[0] = '/';
2760 d9765a41 2018-06-23 stsp pt = TAILQ_LAST(parents, tog_parent_trees);
2761 d9765a41 2018-06-23 stsp while (pt) {
2762 ce52c690 2018-06-23 stsp if (strlcat(*path, pt->selected_entry->name, len) >= len) {
2763 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
2764 cb2ebc8a 2018-06-23 stsp goto done;
2766 ce52c690 2018-06-23 stsp if (strlcat(*path, "/", len) >= len) {
2767 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
2768 cb2ebc8a 2018-06-23 stsp goto done;
2770 d9765a41 2018-06-23 stsp pt = TAILQ_PREV(pt, tog_parent_trees, entry);
2773 ce52c690 2018-06-23 stsp if (strlcat(*path, te->name, len) >= len) {
2774 ce52c690 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
2775 ce52c690 2018-06-23 stsp goto done;
2779 ce52c690 2018-06-23 stsp if (err) {
2780 ce52c690 2018-06-23 stsp free(*path);
2781 ce52c690 2018-06-23 stsp *path = NULL;
2783 ce52c690 2018-06-23 stsp return err;
2786 ce52c690 2018-06-23 stsp static const struct got_error *
2787 e5a0f69f 2018-08-18 stsp blame_tree_entry(struct tog_view **new_view, struct tog_view *parent_view,
2788 e5a0f69f 2018-08-18 stsp struct got_tree_entry *te, struct tog_parent_trees *parents,
2789 e5a0f69f 2018-08-18 stsp struct got_object_id *commit_id, struct got_repository *repo)
2791 ce52c690 2018-06-23 stsp const struct got_error *err = NULL;
2792 ce52c690 2018-06-23 stsp char *path;
2793 e5a0f69f 2018-08-18 stsp struct tog_view *blame_view;
2795 ce52c690 2018-06-23 stsp err = tree_entry_path(&path, parents, te);
2797 ce52c690 2018-06-23 stsp return err;
2799 e5a0f69f 2018-08-18 stsp blame_view = view_open(0, 0, 0, 0, parent_view, TOG_VIEW_BLAME);
2800 e5a0f69f 2018-08-18 stsp if (blame_view == NULL)
2801 e5a0f69f 2018-08-18 stsp return got_error_from_errno();
2803 e5a0f69f 2018-08-18 stsp err = open_blame_view(blame_view, path, commit_id, repo);
2804 e5a0f69f 2018-08-18 stsp if (err) {
2805 e5a0f69f 2018-08-18 stsp view_close(blame_view);
2806 e5a0f69f 2018-08-18 stsp free(path);
2808 e5a0f69f 2018-08-18 stsp *new_view = blame_view;
2809 69efd4c4 2018-07-18 stsp return err;
2812 69efd4c4 2018-07-18 stsp static const struct got_error *
2813 e5a0f69f 2018-08-18 stsp log_tree_entry(struct tog_view **new_view, struct tog_view *parent_view,
2814 e5a0f69f 2018-08-18 stsp struct got_tree_entry *te, struct tog_parent_trees *parents,
2815 e5a0f69f 2018-08-18 stsp struct got_object_id *commit_id, struct got_repository *repo)
2817 e5a0f69f 2018-08-18 stsp struct tog_view *log_view;
2818 69efd4c4 2018-07-18 stsp const struct got_error *err = NULL;
2819 69efd4c4 2018-07-18 stsp char *path;
2821 e5a0f69f 2018-08-18 stsp log_view = view_open(0, 0, 0, 0, parent_view, TOG_VIEW_LOG);
2822 e5a0f69f 2018-08-18 stsp if (log_view == NULL)
2823 e5a0f69f 2018-08-18 stsp return got_error_from_errno();
2825 69efd4c4 2018-07-18 stsp err = tree_entry_path(&path, parents, te);
2827 69efd4c4 2018-07-18 stsp return err;
2829 e5a0f69f 2018-08-18 stsp err = open_log_view(log_view, commit_id, repo, path);
2831 e5a0f69f 2018-08-18 stsp view_close(log_view);
2833 e5a0f69f 2018-08-18 stsp *new_view = log_view;
2834 cb2ebc8a 2018-06-23 stsp free(path);
2835 cb2ebc8a 2018-06-23 stsp return err;
2838 ffd1d5e5 2018-06-23 stsp static const struct got_error *
2839 ad80ab7b 2018-08-04 stsp open_tree_view(struct tog_view *view, struct got_tree_object *root,
2840 5221c383 2018-08-01 stsp struct got_object_id *commit_id, struct got_repository *repo)
2842 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
2843 ad80ab7b 2018-08-04 stsp char *commit_id_str = NULL;
2844 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
2846 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->parents);
2848 ffd1d5e5 2018-06-23 stsp err = got_object_id_str(&commit_id_str, commit_id);
2849 ffd1d5e5 2018-06-23 stsp if (err != NULL)
2850 ffd1d5e5 2018-06-23 stsp goto done;
2852 fb2756b9 2018-08-04 stsp if (asprintf(&s->tree_label, "commit: %s", commit_id_str) == -1) {
2853 ffd1d5e5 2018-06-23 stsp err = got_error_from_errno();
2854 ffd1d5e5 2018-06-23 stsp goto done;
2857 fb2756b9 2018-08-04 stsp s->root = s->tree = root;
2858 fb2756b9 2018-08-04 stsp s->entries = got_object_tree_get_entries(root);
2859 fb2756b9 2018-08-04 stsp s->first_displayed_entry = SIMPLEQ_FIRST(&s->entries->head);
2860 fb2756b9 2018-08-04 stsp s->commit_id = commit_id;
2861 fb2756b9 2018-08-04 stsp s->repo = repo;
2863 e5a0f69f 2018-08-18 stsp view->show = show_tree_view;
2864 e5a0f69f 2018-08-18 stsp view->input = input_tree_view;
2865 e5a0f69f 2018-08-18 stsp view->close = close_tree_view;
2867 ad80ab7b 2018-08-04 stsp free(commit_id_str);
2869 fb2756b9 2018-08-04 stsp free(s->tree_label);
2870 ad80ab7b 2018-08-04 stsp return err;
2873 e5a0f69f 2018-08-18 stsp static const struct got_error *
2874 ad80ab7b 2018-08-04 stsp close_tree_view(struct tog_view *view)
2876 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
2878 fb2756b9 2018-08-04 stsp free(s->tree_label);
2879 fb2756b9 2018-08-04 stsp while (!TAILQ_EMPTY(&s->parents)) {
2880 ad80ab7b 2018-08-04 stsp struct tog_parent_tree *parent;
2881 fb2756b9 2018-08-04 stsp parent = TAILQ_FIRST(&s->parents);
2882 fb2756b9 2018-08-04 stsp TAILQ_REMOVE(&s->parents, parent, entry);
2883 ad80ab7b 2018-08-04 stsp free(parent);
2886 fb2756b9 2018-08-04 stsp if (s->tree != s->root)
2887 fb2756b9 2018-08-04 stsp got_object_tree_close(s->tree);
2888 e5a0f69f 2018-08-18 stsp got_object_tree_close(s->root);
2890 e5a0f69f 2018-08-18 stsp return NULL;
2893 ad80ab7b 2018-08-04 stsp static const struct got_error *
2894 ad80ab7b 2018-08-04 stsp show_tree_view(struct tog_view *view)
2896 ad80ab7b 2018-08-04 stsp const struct got_error *err = NULL;
2897 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
2898 e5a0f69f 2018-08-18 stsp char *parent_path;
2900 e5a0f69f 2018-08-18 stsp err = tree_entry_path(&parent_path, &s->parents, NULL);
2902 e5a0f69f 2018-08-18 stsp return err;
2904 e5a0f69f 2018-08-18 stsp err = draw_tree_entries(view, &s->first_displayed_entry,
2905 e5a0f69f 2018-08-18 stsp &s->last_displayed_entry, &s->selected_entry,
2906 e5a0f69f 2018-08-18 stsp &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
2907 e5a0f69f 2018-08-18 stsp s->entries, s->selected, view->nlines, s->tree == s->root);
2908 e5a0f69f 2018-08-18 stsp free(parent_path);
2909 e5a0f69f 2018-08-18 stsp return err;
2912 e5a0f69f 2018-08-18 stsp static const struct got_error *
2913 e5a0f69f 2018-08-18 stsp input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
2914 e5a0f69f 2018-08-18 stsp struct tog_view *view, int ch)
2916 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
2917 e5a0f69f 2018-08-18 stsp struct tog_tree_view_state *s = &view->state.tree;
2919 e5a0f69f 2018-08-18 stsp switch (ch) {
2921 e5a0f69f 2018-08-18 stsp s->show_ids = !s->show_ids;
2924 e5a0f69f 2018-08-18 stsp if (s->selected_entry) {
2925 e5a0f69f 2018-08-18 stsp err = log_tree_entry(new_view, view,
2926 e5a0f69f 2018-08-18 stsp s->selected_entry, &s->parents,
2927 e5a0f69f 2018-08-18 stsp s->commit_id, s->repo);
2931 e5a0f69f 2018-08-18 stsp case KEY_UP:
2932 e5a0f69f 2018-08-18 stsp if (s->selected > 0)
2933 e5a0f69f 2018-08-18 stsp s->selected--;
2934 e5a0f69f 2018-08-18 stsp if (s->selected > 0)
2936 e5a0f69f 2018-08-18 stsp tree_scroll_up(&s->first_displayed_entry, 1,
2937 e5a0f69f 2018-08-18 stsp s->entries, s->tree == s->root);
2939 e5a0f69f 2018-08-18 stsp case KEY_PPAGE:
2940 e5a0f69f 2018-08-18 stsp if (SIMPLEQ_FIRST(&s->entries->head) ==
2941 e5a0f69f 2018-08-18 stsp s->first_displayed_entry) {
2942 e5a0f69f 2018-08-18 stsp if (s->tree != s->root)
2943 e5a0f69f 2018-08-18 stsp s->first_displayed_entry = NULL;
2944 e5a0f69f 2018-08-18 stsp s->selected = 0;
2947 e5a0f69f 2018-08-18 stsp tree_scroll_up(&s->first_displayed_entry,
2948 e5a0f69f 2018-08-18 stsp view->nlines, s->entries,
2949 e5a0f69f 2018-08-18 stsp s->tree == s->root);
2952 e5a0f69f 2018-08-18 stsp case KEY_DOWN:
2953 e5a0f69f 2018-08-18 stsp if (s->selected < s->ndisplayed - 1) {
2954 e5a0f69f 2018-08-18 stsp s->selected++;
2957 e5a0f69f 2018-08-18 stsp tree_scroll_down(&s->first_displayed_entry, 1,
2958 e5a0f69f 2018-08-18 stsp s->last_displayed_entry, s->entries);
2960 e5a0f69f 2018-08-18 stsp case KEY_NPAGE:
2961 e5a0f69f 2018-08-18 stsp tree_scroll_down(&s->first_displayed_entry,
2962 e5a0f69f 2018-08-18 stsp view->nlines, s->last_displayed_entry,
2963 e5a0f69f 2018-08-18 stsp s->entries);
2964 e5a0f69f 2018-08-18 stsp if (SIMPLEQ_NEXT(s->last_displayed_entry,
2967 e5a0f69f 2018-08-18 stsp /* can't scroll any further; move cursor down */
2968 e5a0f69f 2018-08-18 stsp if (s->selected < s->ndisplayed - 1)
2969 e5a0f69f 2018-08-18 stsp s->selected = s->ndisplayed - 1;
2971 e5a0f69f 2018-08-18 stsp case KEY_ENTER:
2972 e5a0f69f 2018-08-18 stsp case '\r':
2973 e5a0f69f 2018-08-18 stsp if (s->selected_entry == NULL) {
2974 e5a0f69f 2018-08-18 stsp struct tog_parent_tree *parent;
2975 31607d6c 2018-08-18 stsp case KEY_LEFT:
2976 e5a0f69f 2018-08-18 stsp /* user selected '..' */
2977 e5a0f69f 2018-08-18 stsp if (s->tree == s->root)
2979 e5a0f69f 2018-08-18 stsp parent = TAILQ_FIRST(&s->parents);
2980 e5a0f69f 2018-08-18 stsp TAILQ_REMOVE(&s->parents, parent,
2982 e5a0f69f 2018-08-18 stsp got_object_tree_close(s->tree);
2983 e5a0f69f 2018-08-18 stsp s->tree = parent->tree;
2984 e5a0f69f 2018-08-18 stsp s->entries =
2985 e5a0f69f 2018-08-18 stsp got_object_tree_get_entries(s->tree);
2986 e5a0f69f 2018-08-18 stsp s->first_displayed_entry =
2987 e5a0f69f 2018-08-18 stsp parent->first_displayed_entry;
2988 e5a0f69f 2018-08-18 stsp s->selected_entry =
2989 e5a0f69f 2018-08-18 stsp parent->selected_entry;
2990 e5a0f69f 2018-08-18 stsp s->selected = parent->selected;
2991 e5a0f69f 2018-08-18 stsp free(parent);
2992 e5a0f69f 2018-08-18 stsp } else if (S_ISDIR(s->selected_entry->mode)) {
2993 e5a0f69f 2018-08-18 stsp struct tog_parent_tree *parent;
2994 e5a0f69f 2018-08-18 stsp struct got_tree_object *child;
2995 e5a0f69f 2018-08-18 stsp err = got_object_open_as_tree(&child,
2996 e5a0f69f 2018-08-18 stsp s->repo, s->selected_entry->id);
2999 e5a0f69f 2018-08-18 stsp parent = calloc(1, sizeof(*parent));
3000 e5a0f69f 2018-08-18 stsp if (parent == NULL) {
3001 e5a0f69f 2018-08-18 stsp err = got_error_from_errno();
3004 e5a0f69f 2018-08-18 stsp parent->tree = s->tree;
3005 e5a0f69f 2018-08-18 stsp parent->first_displayed_entry =
3006 e5a0f69f 2018-08-18 stsp s->first_displayed_entry;
3007 e5a0f69f 2018-08-18 stsp parent->selected_entry = s->selected_entry;
3008 e5a0f69f 2018-08-18 stsp parent->selected = s->selected;
3009 e5a0f69f 2018-08-18 stsp TAILQ_INSERT_HEAD(&s->parents, parent, entry);
3010 e5a0f69f 2018-08-18 stsp s->tree = child;
3011 e5a0f69f 2018-08-18 stsp s->entries =
3012 e5a0f69f 2018-08-18 stsp got_object_tree_get_entries(s->tree);
3013 e5a0f69f 2018-08-18 stsp s->selected = 0;
3014 e5a0f69f 2018-08-18 stsp s->first_displayed_entry = NULL;
3015 e5a0f69f 2018-08-18 stsp } else if (S_ISREG(s->selected_entry->mode)) {
3016 e5a0f69f 2018-08-18 stsp err = blame_tree_entry(new_view, view,
3017 e5a0f69f 2018-08-18 stsp s->selected_entry, &s->parents,
3018 e5a0f69f 2018-08-18 stsp s->commit_id, s->repo);
3023 e5a0f69f 2018-08-18 stsp case KEY_RESIZE:
3024 e5a0f69f 2018-08-18 stsp if (s->selected > view->nlines)
3025 e5a0f69f 2018-08-18 stsp s->selected = s->ndisplayed - 1;
3031 ffd1d5e5 2018-06-23 stsp return err;
3034 ffd1d5e5 2018-06-23 stsp __dead static void
3035 ffd1d5e5 2018-06-23 stsp usage_tree(void)
3038 ffd1d5e5 2018-06-23 stsp fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
3039 ffd1d5e5 2018-06-23 stsp getprogname());
3043 ffd1d5e5 2018-06-23 stsp static const struct got_error *
3044 ffd1d5e5 2018-06-23 stsp cmd_tree(int argc, char *argv[])
3046 ffd1d5e5 2018-06-23 stsp const struct got_error *error;
3047 ffd1d5e5 2018-06-23 stsp struct got_repository *repo = NULL;
3048 ffd1d5e5 2018-06-23 stsp char *repo_path = NULL;
3049 ffd1d5e5 2018-06-23 stsp struct got_object_id *commit_id = NULL;
3050 ffd1d5e5 2018-06-23 stsp char *commit_id_arg = NULL;
3051 ffd1d5e5 2018-06-23 stsp struct got_commit_object *commit = NULL;
3052 ffd1d5e5 2018-06-23 stsp struct got_tree_object *tree = NULL;
3054 5221c383 2018-08-01 stsp struct tog_view *view;
3056 ffd1d5e5 2018-06-23 stsp #ifndef PROFILE
3057 ad242220 2018-09-08 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd", NULL)
3059 ffd1d5e5 2018-06-23 stsp err(1, "pledge");
3062 ffd1d5e5 2018-06-23 stsp while ((ch = getopt(argc, argv, "c:")) != -1) {
3063 ffd1d5e5 2018-06-23 stsp switch (ch) {
3065 ffd1d5e5 2018-06-23 stsp commit_id_arg = optarg;
3069 ffd1d5e5 2018-06-23 stsp /* NOTREACHED */
3073 ffd1d5e5 2018-06-23 stsp argc -= optind;
3074 ffd1d5e5 2018-06-23 stsp argv += optind;
3076 ffd1d5e5 2018-06-23 stsp if (argc == 0) {
3077 ffd1d5e5 2018-06-23 stsp repo_path = getcwd(NULL, 0);
3078 ffd1d5e5 2018-06-23 stsp if (repo_path == NULL)
3079 ffd1d5e5 2018-06-23 stsp return got_error_from_errno();
3080 ffd1d5e5 2018-06-23 stsp } else if (argc == 1) {
3081 ffd1d5e5 2018-06-23 stsp repo_path = realpath(argv[0], NULL);
3082 ffd1d5e5 2018-06-23 stsp if (repo_path == NULL)
3083 ffd1d5e5 2018-06-23 stsp return got_error_from_errno();
3085 ffd1d5e5 2018-06-23 stsp usage_log();
3087 ffd1d5e5 2018-06-23 stsp error = got_repo_open(&repo, repo_path);
3088 ffd1d5e5 2018-06-23 stsp free(repo_path);
3089 ffd1d5e5 2018-06-23 stsp if (error != NULL)
3090 ffd1d5e5 2018-06-23 stsp return error;
3092 ffd1d5e5 2018-06-23 stsp if (commit_id_arg == NULL) {
3093 ffd1d5e5 2018-06-23 stsp error = get_head_commit_id(&commit_id, repo);
3094 ffd1d5e5 2018-06-23 stsp if (error != NULL)
3095 ffd1d5e5 2018-06-23 stsp goto done;
3097 ffd1d5e5 2018-06-23 stsp struct got_object *obj;
3098 ffd1d5e5 2018-06-23 stsp error = got_object_open_by_id_str(&obj, repo, commit_id_arg);
3099 ffd1d5e5 2018-06-23 stsp if (error == NULL) {
3100 ffd1d5e5 2018-06-23 stsp commit_id = got_object_get_id(obj);
3101 ffd1d5e5 2018-06-23 stsp if (commit_id == NULL)
3102 ffd1d5e5 2018-06-23 stsp error = got_error_from_errno();
3105 ffd1d5e5 2018-06-23 stsp if (error != NULL)
3106 ffd1d5e5 2018-06-23 stsp goto done;
3108 ffd1d5e5 2018-06-23 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
3109 ffd1d5e5 2018-06-23 stsp if (error != NULL)
3110 ffd1d5e5 2018-06-23 stsp goto done;
3112 ffd1d5e5 2018-06-23 stsp error = got_object_open_as_tree(&tree, repo, commit->tree_id);
3113 ffd1d5e5 2018-06-23 stsp if (error != NULL)
3114 ffd1d5e5 2018-06-23 stsp goto done;
3116 b3665f43 2018-08-04 stsp view = view_open(0, 0, 0, 0, NULL, TOG_VIEW_TREE);
3117 5221c383 2018-08-01 stsp if (view == NULL) {
3118 5221c383 2018-08-01 stsp error = got_error_from_errno();
3119 5221c383 2018-08-01 stsp goto done;
3121 ad80ab7b 2018-08-04 stsp error = open_tree_view(view, tree, commit_id, repo);
3122 ad80ab7b 2018-08-04 stsp if (error)
3123 ad80ab7b 2018-08-04 stsp goto done;
3124 e5a0f69f 2018-08-18 stsp error = view_loop(view);
3126 ffd1d5e5 2018-06-23 stsp free(commit_id);
3127 ffd1d5e5 2018-06-23 stsp if (commit)
3128 ffd1d5e5 2018-06-23 stsp got_object_commit_close(commit);
3130 ffd1d5e5 2018-06-23 stsp got_object_tree_close(tree);
3132 ffd1d5e5 2018-06-23 stsp got_repo_close(repo);
3133 ffd1d5e5 2018-06-23 stsp return error;
3135 5c5136c5 2018-05-20 stsp static void
3136 9f7d7167 2018-04-29 stsp init_curses(void)
3138 9f7d7167 2018-04-29 stsp initscr();
3142 9f7d7167 2018-04-29 stsp intrflush(stdscr, FALSE);
3143 9f7d7167 2018-04-29 stsp keypad(stdscr, TRUE);
3144 1f475ad8 2018-05-10 stsp curs_set(0);
3147 4ed7e80c 2018-05-20 stsp __dead static void
3148 9f7d7167 2018-04-29 stsp usage(void)
3152 c2301be8 2018-04-30 stsp fprintf(stderr, "usage: %s [-h] [command] [arg ...]\n\n"
3153 9f7d7167 2018-04-29 stsp "Available commands:\n", getprogname());
3154 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
3155 9f7d7167 2018-04-29 stsp struct tog_cmd *cmd = &tog_commands[i];
3156 c2301be8 2018-04-30 stsp fprintf(stderr, " %s: %s\n", cmd->name, cmd->descr);
3161 c2301be8 2018-04-30 stsp static char **
3162 c2301be8 2018-04-30 stsp make_argv(const char *arg0, const char *arg1)
3164 c2301be8 2018-04-30 stsp char **argv;
3165 c2301be8 2018-04-30 stsp int argc = (arg1 == NULL ? 1 : 2);
3167 c2301be8 2018-04-30 stsp argv = calloc(argc, sizeof(char *));
3168 c2301be8 2018-04-30 stsp if (argv == NULL)
3169 c2301be8 2018-04-30 stsp err(1, "calloc");
3170 c2301be8 2018-04-30 stsp argv[0] = strdup(arg0);
3171 c2301be8 2018-04-30 stsp if (argv[0] == NULL)
3172 c2301be8 2018-04-30 stsp err(1, "calloc");
3173 c2301be8 2018-04-30 stsp if (arg1) {
3174 c2301be8 2018-04-30 stsp argv[1] = strdup(arg1);
3175 c2301be8 2018-04-30 stsp if (argv[1] == NULL)
3176 c2301be8 2018-04-30 stsp err(1, "calloc");
3179 c2301be8 2018-04-30 stsp return argv;
3183 9f7d7167 2018-04-29 stsp main(int argc, char *argv[])
3185 9f7d7167 2018-04-29 stsp const struct got_error *error = NULL;
3186 9f7d7167 2018-04-29 stsp struct tog_cmd *cmd = NULL;
3187 9f7d7167 2018-04-29 stsp int ch, hflag = 0;
3188 c2301be8 2018-04-30 stsp char **cmd_argv = NULL;
3190 9f7d7167 2018-04-29 stsp setlocale(LC_ALL, "");
3192 9f7d7167 2018-04-29 stsp while ((ch = getopt(argc, argv, "h")) != -1) {
3193 9f7d7167 2018-04-29 stsp switch (ch) {
3195 9f7d7167 2018-04-29 stsp hflag = 1;
3199 9f7d7167 2018-04-29 stsp /* NOTREACHED */
3203 9f7d7167 2018-04-29 stsp argc -= optind;
3204 9f7d7167 2018-04-29 stsp argv += optind;
3205 9f7d7167 2018-04-29 stsp optind = 0;
3206 c2301be8 2018-04-30 stsp optreset = 1;
3208 c2301be8 2018-04-30 stsp if (argc == 0) {
3209 f29d3e89 2018-06-23 stsp if (hflag)
3211 c2301be8 2018-04-30 stsp /* Build an argument vector which runs a default command. */
3212 9f7d7167 2018-04-29 stsp cmd = &tog_commands[0];
3213 c2301be8 2018-04-30 stsp cmd_argv = make_argv(cmd->name, NULL);
3218 c2301be8 2018-04-30 stsp /* Did the user specific a command? */
3219 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
3220 c2301be8 2018-04-30 stsp if (strncmp(tog_commands[i].name, argv[0],
3221 9f7d7167 2018-04-29 stsp strlen(argv[0])) == 0) {
3222 9f7d7167 2018-04-29 stsp cmd = &tog_commands[i];
3223 9f7d7167 2018-04-29 stsp if (hflag)
3224 9f7d7167 2018-04-29 stsp tog_commands[i].cmd_usage();
3228 9f7d7167 2018-04-29 stsp if (cmd == NULL) {
3229 c2301be8 2018-04-30 stsp /* Did the user specify a repository? */
3230 c2301be8 2018-04-30 stsp char *repo_path = realpath(argv[0], NULL);
3231 c2301be8 2018-04-30 stsp if (repo_path) {
3232 c2301be8 2018-04-30 stsp struct got_repository *repo;
3233 c2301be8 2018-04-30 stsp error = got_repo_open(&repo, repo_path);
3234 c2301be8 2018-04-30 stsp if (error == NULL)
3235 c2301be8 2018-04-30 stsp got_repo_close(repo);
3237 ad7de8d9 2018-04-30 stsp error = got_error_from_errno();
3238 c2301be8 2018-04-30 stsp if (error) {
3239 f29d3e89 2018-06-23 stsp if (hflag) {
3240 f29d3e89 2018-06-23 stsp fprintf(stderr, "%s: '%s' is not a "
3241 f29d3e89 2018-06-23 stsp "known command\n", getprogname(),
3245 ad7de8d9 2018-04-30 stsp fprintf(stderr, "%s: '%s' is neither a known "
3246 ad7de8d9 2018-04-30 stsp "command nor a path to a repository\n",
3247 c2301be8 2018-04-30 stsp getprogname(), argv[0]);
3248 ad7de8d9 2018-04-30 stsp free(repo_path);
3251 c2301be8 2018-04-30 stsp cmd = &tog_commands[0];
3252 c2301be8 2018-04-30 stsp cmd_argv = make_argv(cmd->name, repo_path);
3254 c2301be8 2018-04-30 stsp free(repo_path);
3258 5c5136c5 2018-05-20 stsp init_curses();
3260 c2301be8 2018-04-30 stsp error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
3261 9f7d7167 2018-04-29 stsp if (error)
3262 9f7d7167 2018-04-29 stsp goto done;
3265 c2301be8 2018-04-30 stsp free(cmd_argv);
3266 9f7d7167 2018-04-29 stsp if (error)
3267 9f7d7167 2018-04-29 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);