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>
37 5036bf37 2018-09-24 stsp #include <libgen.h>
39 9f7d7167 2018-04-29 stsp #include "got_error.h"
40 80ddbec8 2018-04-29 stsp #include "got_object.h"
41 80ddbec8 2018-04-29 stsp #include "got_reference.h"
42 80ddbec8 2018-04-29 stsp #include "got_repository.h"
43 80ddbec8 2018-04-29 stsp #include "got_diff.h"
44 511a516b 2018-05-19 stsp #include "got_opentemp.h"
45 9ba79e04 2018-06-11 stsp #include "got_commit_graph.h"
46 00dfcb92 2018-06-11 stsp #include "got_utf8.h"
47 a70480e0 2018-06-23 stsp #include "got_blame.h"
50 881b2d3e 2018-04-30 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
53 9f7d7167 2018-04-29 stsp #ifndef nitems
54 9f7d7167 2018-04-29 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
57 9f7d7167 2018-04-29 stsp struct tog_cmd {
58 c2301be8 2018-04-30 stsp const char *name;
59 9f7d7167 2018-04-29 stsp const struct got_error *(*cmd_main)(int, char *[]);
60 9f7d7167 2018-04-29 stsp void (*cmd_usage)(void);
61 c2301be8 2018-04-30 stsp const char *descr;
64 4ed7e80c 2018-05-20 stsp __dead static void usage(void);
65 4ed7e80c 2018-05-20 stsp __dead static void usage_log(void);
66 4ed7e80c 2018-05-20 stsp __dead static void usage_diff(void);
67 4ed7e80c 2018-05-20 stsp __dead static void usage_blame(void);
68 ffd1d5e5 2018-06-23 stsp __dead static void usage_tree(void);
70 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_log(int, char *[]);
71 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_diff(int, char *[]);
72 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_blame(int, char *[]);
73 ffd1d5e5 2018-06-23 stsp static const struct got_error* cmd_tree(int, char *[]);
75 4ed7e80c 2018-05-20 stsp static struct tog_cmd tog_commands[] = {
76 cbb6b58a 2018-05-20 stsp { "log", cmd_log, usage_log,
77 9f7d7167 2018-04-29 stsp "show repository history" },
78 cbb6b58a 2018-05-20 stsp { "diff", cmd_diff, usage_diff,
79 9f7d7167 2018-04-29 stsp "compare files and directories" },
80 cbb6b58a 2018-05-20 stsp { "blame", cmd_blame, usage_blame,
81 9f7d7167 2018-04-29 stsp "show line-by-line file history" },
82 ffd1d5e5 2018-06-23 stsp { "tree", cmd_tree, usage_tree,
83 ffd1d5e5 2018-06-23 stsp "browse trees in repository" },
86 d6b05b5b 2018-08-04 stsp enum tog_view_type {
87 d6b05b5b 2018-08-04 stsp TOG_VIEW_DIFF,
88 d6b05b5b 2018-08-04 stsp TOG_VIEW_LOG,
89 ad80ab7b 2018-08-04 stsp TOG_VIEW_BLAME,
90 ad80ab7b 2018-08-04 stsp TOG_VIEW_TREE
93 ad80ab7b 2018-08-04 stsp struct tog_diff_view_state {
94 a3404814 2018-09-02 stsp struct got_object_id *id1, *id2;
96 ad80ab7b 2018-08-04 stsp int first_displayed_line;
97 ad80ab7b 2018-08-04 stsp int last_displayed_line;
101 ba4f502b 2018-08-04 stsp struct commit_queue_entry {
102 ba4f502b 2018-08-04 stsp TAILQ_ENTRY(commit_queue_entry) entry;
103 ba4f502b 2018-08-04 stsp struct got_object_id *id;
104 ba4f502b 2018-08-04 stsp struct got_commit_object *commit;
106 ba4f502b 2018-08-04 stsp TAILQ_HEAD(commit_queue_head, commit_queue_entry);
107 ba4f502b 2018-08-04 stsp struct commit_queue {
108 ba4f502b 2018-08-04 stsp int ncommits;
109 ba4f502b 2018-08-04 stsp struct commit_queue_head head;
112 b01e7d3b 2018-08-04 stsp struct tog_log_view_state {
113 b01e7d3b 2018-08-04 stsp struct got_commit_graph *graph;
114 b01e7d3b 2018-08-04 stsp struct commit_queue commits;
115 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *first_displayed_entry;
116 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *last_displayed_entry;
117 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *selected_entry;
118 b01e7d3b 2018-08-04 stsp int selected;
119 b01e7d3b 2018-08-04 stsp char *in_repo_path;
120 b01e7d3b 2018-08-04 stsp struct got_repository *repo;
121 5036bf37 2018-09-24 stsp struct got_object_id *start_id;
124 e9424729 2018-08-04 stsp struct tog_blame_cb_args {
125 e9424729 2018-08-04 stsp pthread_mutex_t *mutex;
126 e9424729 2018-08-04 stsp struct tog_blame_line *lines; /* one per line */
127 e9424729 2018-08-04 stsp int nlines;
129 e9424729 2018-08-04 stsp struct tog_view *view;
130 e9424729 2018-08-04 stsp struct got_object_id *commit_id;
132 e9424729 2018-08-04 stsp const char *path;
133 e9424729 2018-08-04 stsp int *first_displayed_line;
134 e9424729 2018-08-04 stsp int *last_displayed_line;
135 e9424729 2018-08-04 stsp int *selected_line;
140 e9424729 2018-08-04 stsp struct tog_blame_thread_args {
141 e9424729 2018-08-04 stsp const char *path;
142 e9424729 2018-08-04 stsp struct got_repository *repo;
143 e9424729 2018-08-04 stsp struct tog_blame_cb_args *cb_args;
144 e9424729 2018-08-04 stsp int *complete;
147 e9424729 2018-08-04 stsp struct tog_blame {
149 e9424729 2018-08-04 stsp size_t filesize;
150 e9424729 2018-08-04 stsp struct tog_blame_line *lines;
151 e9424729 2018-08-04 stsp size_t nlines;
152 e9424729 2018-08-04 stsp pthread_t thread;
153 e9424729 2018-08-04 stsp struct tog_blame_thread_args thread_args;
154 e9424729 2018-08-04 stsp struct tog_blame_cb_args cb_args;
155 e9424729 2018-08-04 stsp const char *path;
158 7cbe629d 2018-08-04 stsp struct tog_blame_view_state {
159 7cbe629d 2018-08-04 stsp int first_displayed_line;
160 7cbe629d 2018-08-04 stsp int last_displayed_line;
161 7cbe629d 2018-08-04 stsp int selected_line;
162 7cbe629d 2018-08-04 stsp int blame_complete;
165 7cbe629d 2018-08-04 stsp pthread_mutex_t mutex;
166 7cbe629d 2018-08-04 stsp struct got_object_id_queue blamed_commits;
167 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
168 e5a0f69f 2018-08-18 stsp char *path;
169 7cbe629d 2018-08-04 stsp struct got_repository *repo;
170 ad80ab7b 2018-08-04 stsp struct got_object_id *commit_id;
171 e9424729 2018-08-04 stsp struct tog_blame blame;
174 ad80ab7b 2018-08-04 stsp struct tog_parent_tree {
175 ad80ab7b 2018-08-04 stsp TAILQ_ENTRY(tog_parent_tree) entry;
176 ad80ab7b 2018-08-04 stsp struct got_tree_object *tree;
177 ad80ab7b 2018-08-04 stsp struct got_tree_entry *first_displayed_entry;
178 ad80ab7b 2018-08-04 stsp struct got_tree_entry *selected_entry;
179 ad80ab7b 2018-08-04 stsp int selected;
182 ad80ab7b 2018-08-04 stsp TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
184 ad80ab7b 2018-08-04 stsp struct tog_tree_view_state {
185 ad80ab7b 2018-08-04 stsp char *tree_label;
186 ad80ab7b 2018-08-04 stsp struct got_tree_object *root;
187 ad80ab7b 2018-08-04 stsp struct got_tree_object *tree;
188 ad80ab7b 2018-08-04 stsp const struct got_tree_entries *entries;
189 ad80ab7b 2018-08-04 stsp struct got_tree_entry *first_displayed_entry;
190 ad80ab7b 2018-08-04 stsp struct got_tree_entry *last_displayed_entry;
191 ad80ab7b 2018-08-04 stsp struct got_tree_entry *selected_entry;
192 ad80ab7b 2018-08-04 stsp int nentries, ndisplayed, selected, show_ids;
193 ad80ab7b 2018-08-04 stsp struct tog_parent_trees parents;
194 7cbe629d 2018-08-04 stsp struct got_object_id *commit_id;
195 ad80ab7b 2018-08-04 stsp struct got_repository *repo;
198 bcbd79e2 2018-08-19 stsp TAILQ_HEAD(tog_view_list_head, tog_view);
199 cc3c9aac 2018-08-01 stsp struct tog_view {
200 e5a0f69f 2018-08-18 stsp TAILQ_ENTRY(tog_view) entry;
201 26ed57b2 2018-05-19 stsp WINDOW *window;
202 26ed57b2 2018-05-19 stsp PANEL *panel;
203 97ddc146 2018-08-01 stsp int nlines, ncols, begin_y, begin_x;
204 f7d12f7e 2018-08-01 stsp int lines, cols; /* copies of LINES and COLS */
205 a3404814 2018-09-02 stsp int focussed;
206 6d0fee91 2018-08-01 stsp struct tog_view *parent;
207 bcbd79e2 2018-08-19 stsp struct tog_view *child;
209 5dc9f4bc 2018-08-04 stsp /* type-specific state */
210 d6b05b5b 2018-08-04 stsp enum tog_view_type type;
212 b01e7d3b 2018-08-04 stsp struct tog_diff_view_state diff;
213 b01e7d3b 2018-08-04 stsp struct tog_log_view_state log;
214 7cbe629d 2018-08-04 stsp struct tog_blame_view_state blame;
215 ad80ab7b 2018-08-04 stsp struct tog_tree_view_state tree;
218 e5a0f69f 2018-08-18 stsp const struct got_error *(*show)(struct tog_view *);
219 e5a0f69f 2018-08-18 stsp const struct got_error *(*input)(struct tog_view **,
220 e5a0f69f 2018-08-18 stsp struct tog_view **, struct tog_view *, int);
221 bcbd79e2 2018-08-19 stsp const struct got_error *(*set_child)(struct tog_view *,
222 bcbd79e2 2018-08-19 stsp struct tog_view *);
223 e5a0f69f 2018-08-18 stsp const struct got_error *(*close)(struct tog_view *);
226 ba4f502b 2018-08-04 stsp static const struct got_error *open_diff_view(struct tog_view *,
227 ba4f502b 2018-08-04 stsp struct got_object *, struct got_object *, struct got_repository *);
228 5dc9f4bc 2018-08-04 stsp static const struct got_error *show_diff_view(struct tog_view *);
229 e5a0f69f 2018-08-18 stsp static const struct got_error *input_diff_view(struct tog_view **,
230 e5a0f69f 2018-08-18 stsp struct tog_view **, struct tog_view *, int);
231 e5a0f69f 2018-08-18 stsp static const struct got_error* close_diff_view(struct tog_view *);
233 ba4f502b 2018-08-04 stsp static const struct got_error *open_log_view(struct tog_view *,
234 ba4f502b 2018-08-04 stsp struct got_object_id *, struct got_repository *, const char *);
235 ba4f502b 2018-08-04 stsp static const struct got_error * show_log_view(struct tog_view *);
236 e5a0f69f 2018-08-18 stsp static const struct got_error *input_log_view(struct tog_view **,
237 e5a0f69f 2018-08-18 stsp struct tog_view **, struct tog_view *, int);
238 e5a0f69f 2018-08-18 stsp static const struct got_error *close_log_view(struct tog_view *);
239 bcbd79e2 2018-08-19 stsp static const struct got_error* set_child_log_view(struct tog_view *,
240 bcbd79e2 2018-08-19 stsp struct tog_view *);
242 e5a0f69f 2018-08-18 stsp static const struct got_error *open_blame_view(struct tog_view *, char *,
243 5221c383 2018-08-01 stsp struct got_object_id *, struct got_repository *);
244 7cbe629d 2018-08-04 stsp static const struct got_error *show_blame_view(struct tog_view *);
245 e5a0f69f 2018-08-18 stsp static const struct got_error *input_blame_view(struct tog_view **,
246 e5a0f69f 2018-08-18 stsp struct tog_view **, struct tog_view *, int);
247 e5a0f69f 2018-08-18 stsp static const struct got_error *close_blame_view(struct tog_view *);
249 ad80ab7b 2018-08-04 stsp static const struct got_error *open_tree_view(struct tog_view *,
250 4fc679ca 2018-08-04 stsp struct got_tree_object *, struct got_object_id *, struct got_repository *);
251 ad80ab7b 2018-08-04 stsp static const struct got_error *show_tree_view(struct tog_view *);
252 e5a0f69f 2018-08-18 stsp static const struct got_error *input_tree_view(struct tog_view **,
253 e5a0f69f 2018-08-18 stsp struct tog_view **, struct tog_view *, int);
254 e5a0f69f 2018-08-18 stsp static const struct got_error *close_tree_view(struct tog_view *);
256 e5a0f69f 2018-08-18 stsp static const struct got_error *
257 96a765a8 2018-08-04 stsp view_close(struct tog_view *view)
259 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
261 bcbd79e2 2018-08-19 stsp if (view->child)
262 bcbd79e2 2018-08-19 stsp view->child->parent = NULL;
263 bcbd79e2 2018-08-19 stsp if (view->parent)
264 bcbd79e2 2018-08-19 stsp view->parent->child = NULL;
265 e5a0f69f 2018-08-18 stsp if (view->close)
266 e5a0f69f 2018-08-18 stsp err = view->close(view);
267 ea5e7bb5 2018-08-01 stsp if (view->panel)
268 ea5e7bb5 2018-08-01 stsp del_panel(view->panel);
269 ea5e7bb5 2018-08-01 stsp if (view->window)
270 ea5e7bb5 2018-08-01 stsp delwin(view->window);
271 ea5e7bb5 2018-08-01 stsp free(view);
272 e5a0f69f 2018-08-18 stsp return err;
275 ea5e7bb5 2018-08-01 stsp static struct tog_view *
276 b3665f43 2018-08-04 stsp view_open(int nlines, int ncols, int begin_y, int begin_x,
277 d6b05b5b 2018-08-04 stsp struct tog_view *parent, enum tog_view_type type)
279 ad80ab7b 2018-08-04 stsp struct tog_view *view = calloc(1, sizeof(*view));
281 ea5e7bb5 2018-08-01 stsp if (view == NULL)
282 ea5e7bb5 2018-08-01 stsp return NULL;
284 dd04dd3f 2018-08-19 stsp if (begin_x == 0 && parent && parent->ncols - 80 > 10)
285 dd04dd3f 2018-08-19 stsp begin_x = parent->ncols - 80;
287 6d0fee91 2018-08-01 stsp view->parent = parent;
288 bcbd79e2 2018-08-19 stsp if (parent)
289 bcbd79e2 2018-08-19 stsp parent->child = view;
290 d6b05b5b 2018-08-04 stsp view->type = type;
291 f7d12f7e 2018-08-01 stsp view->lines = LINES;
292 f7d12f7e 2018-08-01 stsp view->cols = COLS;
293 207b9029 2018-08-01 stsp view->nlines = nlines ? nlines : LINES - begin_y;
294 207b9029 2018-08-01 stsp view->ncols = ncols ? ncols : COLS - begin_x;
295 97ddc146 2018-08-01 stsp view->begin_y = begin_y;
296 97ddc146 2018-08-01 stsp view->begin_x = begin_x;
297 842167bf 2018-08-01 stsp view->window = newwin(nlines, ncols, begin_y, begin_x);
298 ea5e7bb5 2018-08-01 stsp if (view->window == NULL) {
299 96a765a8 2018-08-04 stsp view_close(view);
300 ea5e7bb5 2018-08-01 stsp return NULL;
302 ea5e7bb5 2018-08-01 stsp view->panel = new_panel(view->window);
303 ea5e7bb5 2018-08-01 stsp if (view->panel == NULL) {
304 96a765a8 2018-08-04 stsp view_close(view);
305 ea5e7bb5 2018-08-01 stsp return NULL;
308 ea5e7bb5 2018-08-01 stsp keypad(view->window, TRUE);
309 ea5e7bb5 2018-08-01 stsp return view;
312 4d8c2215 2018-08-19 stsp static const struct got_error *
313 6acd1bd1 2018-08-04 stsp view_show(struct tog_view *view)
315 e5a0f69f 2018-08-18 stsp const struct got_error *err;
317 bcbd79e2 2018-08-19 stsp if (view->parent) {
318 bcbd79e2 2018-08-19 stsp err = view->parent->show(view->parent);
320 bcbd79e2 2018-08-19 stsp return err;
321 bcbd79e2 2018-08-19 stsp show_panel(view->parent->panel);
324 e5a0f69f 2018-08-18 stsp err = view->show(view);
326 e5a0f69f 2018-08-18 stsp return err;
327 cdf1ee82 2018-08-01 stsp show_panel(view->panel);
329 a3404814 2018-09-02 stsp if (view->child && view->child->begin_x > view->begin_x) {
330 bcbd79e2 2018-08-19 stsp err = view->child->show(view->child);
332 bcbd79e2 2018-08-19 stsp return err;
333 bcbd79e2 2018-08-19 stsp show_panel(view->child->panel);
336 cdf1ee82 2018-08-01 stsp update_panels();
337 e5a0f69f 2018-08-18 stsp doupdate();
339 e5a0f69f 2018-08-18 stsp return err;
342 4d8c2215 2018-08-19 stsp static const struct got_error *
343 f7d12f7e 2018-08-01 stsp view_resize(struct tog_view *view)
345 f7d12f7e 2018-08-01 stsp int nlines, ncols;
347 6d0fee91 2018-08-01 stsp while (view) {
348 6d0fee91 2018-08-01 stsp if (view->lines > LINES)
349 6d0fee91 2018-08-01 stsp nlines = view->nlines - (view->lines - LINES);
351 6d0fee91 2018-08-01 stsp nlines = view->nlines + (LINES - view->lines);
353 6d0fee91 2018-08-01 stsp if (view->cols > COLS)
354 6d0fee91 2018-08-01 stsp ncols = view->ncols - (view->cols - COLS);
356 6d0fee91 2018-08-01 stsp ncols = view->ncols + (COLS - view->cols);
358 6d0fee91 2018-08-01 stsp if (wresize(view->window, nlines, ncols) == ERR)
359 6d0fee91 2018-08-01 stsp return got_error_from_errno();
360 77a567dd 2018-08-01 stsp replace_panel(view->panel, view->window);
362 6d0fee91 2018-08-01 stsp view->nlines = nlines;
363 6d0fee91 2018-08-01 stsp view->ncols = ncols;
364 6d0fee91 2018-08-01 stsp view->lines = LINES;
365 6d0fee91 2018-08-01 stsp view->cols = COLS;
367 6d0fee91 2018-08-01 stsp view = view->parent;
370 f7d12f7e 2018-08-01 stsp return NULL;
373 e5a0f69f 2018-08-18 stsp static const struct got_error *
374 48fcc512 2018-08-18 stsp view_input(struct tog_view **new, struct tog_view **dead,
375 e4197bf9 2018-08-18 stsp struct tog_view **focus, int *done, struct tog_view *view,
376 48fcc512 2018-08-18 stsp struct tog_view_list_head *views)
378 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
379 31607d6c 2018-08-18 stsp struct tog_view *next, *prev;
382 e5a0f69f 2018-08-18 stsp *new = NULL;
383 e5a0f69f 2018-08-18 stsp *dead = NULL;
385 e5a0f69f 2018-08-18 stsp nodelay(stdscr, FALSE);
386 e5a0f69f 2018-08-18 stsp ch = wgetch(view->window);
387 e5a0f69f 2018-08-18 stsp nodelay(stdscr, TRUE);
388 e5a0f69f 2018-08-18 stsp switch (ch) {
392 48fcc512 2018-08-18 stsp next = TAILQ_NEXT(view, entry);
394 48fcc512 2018-08-18 stsp *focus = next;
396 48fcc512 2018-08-18 stsp *focus = TAILQ_FIRST(views);
397 a3404814 2018-09-02 stsp view->focussed = 0;
398 a3404814 2018-09-02 stsp (*focus)->focussed = 1;
401 31607d6c 2018-08-18 stsp prev = TAILQ_PREV(view, tog_view_list_head, entry);
403 31607d6c 2018-08-18 stsp *focus = prev;
405 31607d6c 2018-08-18 stsp *focus = TAILQ_LAST(views, tog_view_list_head);
406 786292d9 2018-09-02 stsp view->focussed = 0;
407 786292d9 2018-09-02 stsp (*focus)->focussed = 1;
410 e5a0f69f 2018-08-18 stsp err = view->input(new, dead, view, ch);
411 e5a0f69f 2018-08-18 stsp *dead = view;
416 e5a0f69f 2018-08-18 stsp case KEY_RESIZE:
417 e5a0f69f 2018-08-18 stsp err = view_resize(view);
419 e5a0f69f 2018-08-18 stsp return err;
420 e5a0f69f 2018-08-18 stsp err = view->input(new, dead, view, ch);
423 e5a0f69f 2018-08-18 stsp err = view->input(new, dead, view, ch);
427 e5a0f69f 2018-08-18 stsp return err;
430 e5a0f69f 2018-08-18 stsp static const struct got_error *
431 bcbd79e2 2018-08-19 stsp view_set_child(struct tog_view *view, struct tog_view *child)
433 bcbd79e2 2018-08-19 stsp const struct got_error *err;
435 bcbd79e2 2018-08-19 stsp if (view->set_child) {
436 bcbd79e2 2018-08-19 stsp err = view->set_child(view, child);
438 bcbd79e2 2018-08-19 stsp return err;
441 bcbd79e2 2018-08-19 stsp view->child = child;
442 bcbd79e2 2018-08-19 stsp return NULL;
446 1a57306a 2018-09-02 stsp view_vborder(struct tog_view *view)
448 1a57306a 2018-09-02 stsp if (view->child == NULL)
451 1a57306a 2018-09-02 stsp mvwvline(view->window, view->begin_y, view->child->begin_x - 1,
452 1a57306a 2018-09-02 stsp got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
456 a3404814 2018-09-02 stsp view_needs_focus_indication(struct tog_view *view)
458 a3404814 2018-09-02 stsp if (!view->focussed)
461 a3404814 2018-09-02 stsp if (view->child && view->child->begin_x > view->begin_x)
464 a3404814 2018-09-02 stsp if (view->parent && view->begin_x > view->parent->begin_x)
470 bcbd79e2 2018-08-19 stsp static const struct got_error *
471 e5a0f69f 2018-08-18 stsp view_loop(struct tog_view *view)
473 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
474 e5a0f69f 2018-08-18 stsp struct tog_view_list_head views;
475 e5a0f69f 2018-08-18 stsp struct tog_view *new_view, *dead_view;
476 e4197bf9 2018-08-18 stsp int done = 0;
478 e5a0f69f 2018-08-18 stsp TAILQ_INIT(&views);
479 e5a0f69f 2018-08-18 stsp TAILQ_INSERT_HEAD(&views, view, entry);
481 a3404814 2018-09-02 stsp view->focussed = 1;
482 e4197bf9 2018-08-18 stsp while (!TAILQ_EMPTY(&views) && !done) {
483 e5a0f69f 2018-08-18 stsp err = view_show(view);
486 e4197bf9 2018-08-18 stsp err = view_input(&new_view, &dead_view, &view, &done,
487 e4197bf9 2018-08-18 stsp view, &views);
490 e5a0f69f 2018-08-18 stsp if (dead_view) {
491 89864818 2018-09-02 stsp struct tog_view *v, *t;
492 e5a0f69f 2018-08-18 stsp TAILQ_REMOVE(&views, dead_view, entry);
493 89864818 2018-09-02 stsp TAILQ_FOREACH_SAFE(v, &views, entry, t) {
494 89864818 2018-09-02 stsp if (v->parent == dead_view) {
495 89864818 2018-09-02 stsp TAILQ_REMOVE(&views, v, entry);
496 7e1e2c14 2018-09-02 stsp err = view_close(v);
501 e5a0f69f 2018-08-18 stsp if (dead_view->parent)
502 e5a0f69f 2018-08-18 stsp view = dead_view->parent;
504 e5a0f69f 2018-08-18 stsp view = TAILQ_LAST(&views, tog_view_list_head);
506 a3404814 2018-09-02 stsp view->focussed = 1;
507 e5a0f69f 2018-08-18 stsp err = view_close(dead_view);
511 bcbd79e2 2018-08-19 stsp if (new_view) {
512 ae73d513 2018-09-24 stsp struct tog_view *v, *t;
513 a3404814 2018-09-02 stsp view->focussed = 0;
514 ae73d513 2018-09-24 stsp /* Only allow one view per type. */
515 ae73d513 2018-09-24 stsp TAILQ_FOREACH_SAFE(v, &views, entry, t) {
516 ae73d513 2018-09-24 stsp if (v->type != new_view->type)
518 ae73d513 2018-09-24 stsp TAILQ_REMOVE(&views, v, entry);
519 ae73d513 2018-09-24 stsp err = view_close(v);
523 bcbd79e2 2018-08-19 stsp TAILQ_INSERT_TAIL(&views, new_view, entry);
524 bcbd79e2 2018-08-19 stsp if (new_view->parent) {
525 bcbd79e2 2018-08-19 stsp err = view_set_child(new_view->parent, new_view);
528 a3404814 2018-09-02 stsp new_view->parent->focussed = 0;
530 bcbd79e2 2018-08-19 stsp view = new_view;
531 a3404814 2018-09-02 stsp view->focussed = 1;
535 e5a0f69f 2018-08-18 stsp while (!TAILQ_EMPTY(&views)) {
536 e5a0f69f 2018-08-18 stsp view = TAILQ_FIRST(&views);
537 e5a0f69f 2018-08-18 stsp TAILQ_REMOVE(&views, view, entry);
538 e5a0f69f 2018-08-18 stsp view_close(view);
540 e5a0f69f 2018-08-18 stsp return err;
543 4ed7e80c 2018-05-20 stsp __dead static void
544 9f7d7167 2018-04-29 stsp usage_log(void)
547 c70c5802 2018-08-01 stsp fprintf(stderr,
548 c70c5802 2018-08-01 stsp "usage: %s log [-c commit] [-r repository-path] [path]\n",
549 9f7d7167 2018-04-29 stsp getprogname());
553 963b370f 2018-05-20 stsp /* Create newly allocated wide-character string equivalent to a byte string. */
554 80ddbec8 2018-04-29 stsp static const struct got_error *
555 963b370f 2018-05-20 stsp mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
557 00dfcb92 2018-06-11 stsp char *vis = NULL;
558 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
560 963b370f 2018-05-20 stsp *ws = NULL;
561 963b370f 2018-05-20 stsp *wlen = mbstowcs(NULL, s, 0);
562 00dfcb92 2018-06-11 stsp if (*wlen == (size_t)-1) {
563 00dfcb92 2018-06-11 stsp int vislen;
564 00dfcb92 2018-06-11 stsp if (errno != EILSEQ)
565 00dfcb92 2018-06-11 stsp return got_error_from_errno();
567 00dfcb92 2018-06-11 stsp /* byte string invalid in current encoding; try to "fix" it */
568 00dfcb92 2018-06-11 stsp err = got_mbsavis(&vis, &vislen, s);
570 00dfcb92 2018-06-11 stsp return err;
571 00dfcb92 2018-06-11 stsp *wlen = mbstowcs(NULL, vis, 0);
572 a7f50699 2018-06-11 stsp if (*wlen == (size_t)-1) {
573 a7f50699 2018-06-11 stsp err = got_error_from_errno(); /* give up */
578 963b370f 2018-05-20 stsp *ws = calloc(*wlen + 1, sizeof(*ws));
579 a7f50699 2018-06-11 stsp if (*ws == NULL) {
580 a7f50699 2018-06-11 stsp err = got_error_from_errno();
584 00dfcb92 2018-06-11 stsp if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
585 963b370f 2018-05-20 stsp err = got_error_from_errno();
590 963b370f 2018-05-20 stsp *ws = NULL;
593 963b370f 2018-05-20 stsp return err;
596 963b370f 2018-05-20 stsp /* Format a line for display, ensuring that it won't overflow a width limit. */
597 963b370f 2018-05-20 stsp static const struct got_error *
598 ffd1d5e5 2018-06-23 stsp format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit)
600 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
601 963b370f 2018-05-20 stsp int cols = 0;
602 963b370f 2018-05-20 stsp wchar_t *wline = NULL;
603 963b370f 2018-05-20 stsp size_t wlen;
606 963b370f 2018-05-20 stsp *wlinep = NULL;
607 b700b5d6 2018-07-10 stsp *widthp = 0;
609 963b370f 2018-05-20 stsp err = mbs2ws(&wline, &wlen, line);
611 963b370f 2018-05-20 stsp return err;
614 b700b5d6 2018-07-10 stsp while (i < wlen && cols < wlimit) {
615 963b370f 2018-05-20 stsp int width = wcwidth(wline[i]);
616 963b370f 2018-05-20 stsp switch (width) {
622 3c1f04f1 2018-09-13 stsp if (cols + width <= wlimit)
623 b700b5d6 2018-07-10 stsp cols += width;
627 963b370f 2018-05-20 stsp if (wline[i] == L'\t')
628 b700b5d6 2018-07-10 stsp cols += TABSIZE - ((cols + 1) % TABSIZE);
632 963b370f 2018-05-20 stsp err = got_error_from_errno();
636 963b370f 2018-05-20 stsp wline[i] = L'\0';
637 b700b5d6 2018-07-10 stsp if (widthp)
638 b700b5d6 2018-07-10 stsp *widthp = cols;
641 963b370f 2018-05-20 stsp free(wline);
643 963b370f 2018-05-20 stsp *wlinep = wline;
644 963b370f 2018-05-20 stsp return err;
647 963b370f 2018-05-20 stsp static const struct got_error *
648 2814baeb 2018-08-01 stsp draw_commit(struct tog_view *view, struct got_commit_object *commit,
649 2814baeb 2018-08-01 stsp struct got_object_id *id)
651 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
652 b39d25c7 2018-07-10 stsp char datebuf[10]; /* YY-MM-DD + SPACE + NUL */
653 80ddbec8 2018-04-29 stsp char *logmsg0 = NULL, *logmsg = NULL;
654 6d9fbc00 2018-04-29 stsp char *author0 = NULL, *author = NULL;
655 bb737323 2018-05-20 stsp wchar_t *wlogmsg = NULL, *wauthor = NULL;
656 bb737323 2018-05-20 stsp int author_width, logmsg_width;
657 6d9fbc00 2018-04-29 stsp char *newline, *smallerthan;
658 80ddbec8 2018-04-29 stsp char *line = NULL;
659 bb737323 2018-05-20 stsp int col, limit;
660 b39d25c7 2018-07-10 stsp static const size_t date_display_cols = 9;
661 bb737323 2018-05-20 stsp static const size_t author_display_cols = 16;
662 f7d12f7e 2018-08-01 stsp const int avail = view->ncols;
664 c70c5802 2018-08-01 stsp if (strftime(datebuf, sizeof(datebuf), "%g/%m/%d ",
665 c70c5802 2018-08-01 stsp &commit->tm_committer) >= sizeof(datebuf))
666 b39d25c7 2018-07-10 stsp return got_error(GOT_ERR_NO_SPACE);
668 b39d25c7 2018-07-10 stsp if (avail < date_display_cols)
669 b39d25c7 2018-07-10 stsp limit = MIN(sizeof(datebuf) - 1, avail);
671 b39d25c7 2018-07-10 stsp limit = MIN(date_display_cols, sizeof(datebuf) - 1);
672 2814baeb 2018-08-01 stsp waddnstr(view->window, datebuf, limit);
673 b39d25c7 2018-07-10 stsp col = limit + 1;
674 b39d25c7 2018-07-10 stsp if (col > avail)
677 6d9fbc00 2018-04-29 stsp author0 = strdup(commit->author);
678 6d9fbc00 2018-04-29 stsp if (author0 == NULL) {
679 80ddbec8 2018-04-29 stsp err = got_error_from_errno();
682 6d9fbc00 2018-04-29 stsp author = author0;
683 6d9fbc00 2018-04-29 stsp smallerthan = strchr(author, '<');
684 6d9fbc00 2018-04-29 stsp if (smallerthan)
685 6d9fbc00 2018-04-29 stsp *smallerthan = '\0';
687 6d9fbc00 2018-04-29 stsp char *at = strchr(author, '@');
689 6d9fbc00 2018-04-29 stsp *at = '\0';
691 b39d25c7 2018-07-10 stsp limit = avail - col;
692 bb737323 2018-05-20 stsp err = format_line(&wauthor, &author_width, author, limit);
695 2814baeb 2018-08-01 stsp waddwstr(view->window, wauthor);
696 bb737323 2018-05-20 stsp col += author_width;
697 9c2eaf34 2018-05-20 stsp while (col <= avail && author_width < author_display_cols + 1) {
698 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
700 bb737323 2018-05-20 stsp author_width++;
702 9c2eaf34 2018-05-20 stsp if (col > avail)
705 bb737323 2018-05-20 stsp logmsg0 = strdup(commit->logmsg);
706 bb737323 2018-05-20 stsp if (logmsg0 == NULL) {
707 6d9fbc00 2018-04-29 stsp err = got_error_from_errno();
710 bb737323 2018-05-20 stsp logmsg = logmsg0;
711 bb737323 2018-05-20 stsp while (*logmsg == '\n')
713 bb737323 2018-05-20 stsp newline = strchr(logmsg, '\n');
714 bb737323 2018-05-20 stsp if (newline)
715 bb737323 2018-05-20 stsp *newline = '\0';
716 bb737323 2018-05-20 stsp limit = avail - col;
717 bb737323 2018-05-20 stsp err = format_line(&wlogmsg, &logmsg_width, logmsg, limit);
720 2814baeb 2018-08-01 stsp waddwstr(view->window, wlogmsg);
721 bb737323 2018-05-20 stsp col += logmsg_width;
722 bb737323 2018-05-20 stsp while (col <= avail) {
723 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
727 80ddbec8 2018-04-29 stsp free(logmsg0);
728 bb737323 2018-05-20 stsp free(wlogmsg);
729 6d9fbc00 2018-04-29 stsp free(author0);
730 bb737323 2018-05-20 stsp free(wauthor);
731 80ddbec8 2018-04-29 stsp free(line);
732 80ddbec8 2018-04-29 stsp return err;
735 899d86c2 2018-05-10 stsp static struct commit_queue_entry *
736 899d86c2 2018-05-10 stsp alloc_commit_queue_entry(struct got_commit_object *commit,
737 899d86c2 2018-05-10 stsp struct got_object_id *id)
739 80ddbec8 2018-04-29 stsp struct commit_queue_entry *entry;
741 80ddbec8 2018-04-29 stsp entry = calloc(1, sizeof(*entry));
742 80ddbec8 2018-04-29 stsp if (entry == NULL)
743 899d86c2 2018-05-10 stsp return NULL;
745 899d86c2 2018-05-10 stsp entry->id = id;
746 99db9666 2018-05-07 stsp entry->commit = commit;
747 899d86c2 2018-05-10 stsp return entry;
750 99db9666 2018-05-07 stsp static void
751 99db9666 2018-05-07 stsp pop_commit(struct commit_queue *commits)
753 99db9666 2018-05-07 stsp struct commit_queue_entry *entry;
755 ecb28ae0 2018-07-16 stsp entry = TAILQ_FIRST(&commits->head);
756 ecb28ae0 2018-07-16 stsp TAILQ_REMOVE(&commits->head, entry, entry);
757 99db9666 2018-05-07 stsp got_object_commit_close(entry->commit);
758 ecb28ae0 2018-07-16 stsp commits->ncommits--;
759 9ba79e04 2018-06-11 stsp /* Don't free entry->id! It is owned by the commit graph. */
760 99db9666 2018-05-07 stsp free(entry);
763 99db9666 2018-05-07 stsp static void
764 99db9666 2018-05-07 stsp free_commits(struct commit_queue *commits)
766 ecb28ae0 2018-07-16 stsp while (!TAILQ_EMPTY(&commits->head))
767 99db9666 2018-05-07 stsp pop_commit(commits);
770 c4972b91 2018-05-07 stsp static const struct got_error *
771 9ba79e04 2018-06-11 stsp queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
772 93e45b7c 2018-09-24 stsp struct got_object_id *start_id, int minqueue,
773 ecb28ae0 2018-07-16 stsp struct got_repository *repo, const char *path)
775 899d86c2 2018-05-10 stsp const struct got_error *err = NULL;
776 93e45b7c 2018-09-24 stsp int nqueued = 0;
778 93e45b7c 2018-09-24 stsp if (start_id) {
779 93e45b7c 2018-09-24 stsp err = got_commit_graph_iter_start(graph, start_id, repo);
781 9ba79e04 2018-06-11 stsp return err;
784 93e45b7c 2018-09-24 stsp while (nqueued < minqueue) {
785 93e45b7c 2018-09-24 stsp struct got_object_id *id;
786 9ba79e04 2018-06-11 stsp struct got_commit_object *commit;
787 93e45b7c 2018-09-24 stsp struct commit_queue_entry *entry;
789 9ba79e04 2018-06-11 stsp err = got_commit_graph_iter_next(&id, graph);
791 93e45b7c 2018-09-24 stsp if (err->code == GOT_ERR_ITER_COMPLETED) {
792 9ba79e04 2018-06-11 stsp err = NULL;
795 93e45b7c 2018-09-24 stsp if (err->code != GOT_ERR_ITER_NEED_MORE)
797 93e45b7c 2018-09-24 stsp err = got_commit_graph_fetch_commits(graph,
798 93e45b7c 2018-09-24 stsp minqueue, repo);
800 ecb28ae0 2018-07-16 stsp return err;
804 ecb28ae0 2018-07-16 stsp if (id == NULL)
807 9ba79e04 2018-06-11 stsp err = got_object_open_as_commit(&commit, repo, id);
810 9ba79e04 2018-06-11 stsp entry = alloc_commit_queue_entry(commit, id);
811 9ba79e04 2018-06-11 stsp if (entry == NULL) {
812 9ba79e04 2018-06-11 stsp err = got_error_from_errno();
816 ecb28ae0 2018-07-16 stsp TAILQ_INSERT_TAIL(&commits->head, entry, entry);
818 ecb28ae0 2018-07-16 stsp commits->ncommits++;
821 9ba79e04 2018-06-11 stsp return err;
824 99db9666 2018-05-07 stsp static const struct got_error *
825 9ba79e04 2018-06-11 stsp fetch_next_commit(struct commit_queue_entry **pentry,
826 9ba79e04 2018-06-11 stsp struct commit_queue_entry *entry, struct commit_queue *commits,
827 ecb28ae0 2018-07-16 stsp struct got_commit_graph *graph, struct got_repository *repo,
828 ecb28ae0 2018-07-16 stsp const char *path)
830 899d86c2 2018-05-10 stsp const struct got_error *err = NULL;
832 9ba79e04 2018-06-11 stsp *pentry = NULL;
834 93e45b7c 2018-09-24 stsp err = queue_commits(graph, commits, NULL, 1, repo, path);
836 9ba79e04 2018-06-11 stsp return err;
838 9ba79e04 2018-06-11 stsp /* Next entry to display should now be available. */
839 9ba79e04 2018-06-11 stsp *pentry = TAILQ_NEXT(entry, entry);
840 9ba79e04 2018-06-11 stsp return NULL;
843 899d86c2 2018-05-10 stsp static const struct got_error *
844 9ba79e04 2018-06-11 stsp get_head_commit_id(struct got_object_id **head_id, struct got_repository *repo)
846 9ba79e04 2018-06-11 stsp const struct got_error *err = NULL;
847 9ba79e04 2018-06-11 stsp struct got_reference *head_ref;
849 9ba79e04 2018-06-11 stsp *head_id = NULL;
851 9ba79e04 2018-06-11 stsp err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
853 99db9666 2018-05-07 stsp return err;
855 9ba79e04 2018-06-11 stsp err = got_ref_resolve(head_id, repo, head_ref);
856 9ba79e04 2018-06-11 stsp got_ref_close(head_ref);
858 9ba79e04 2018-06-11 stsp *head_id = NULL;
859 99db9666 2018-05-07 stsp return err;
862 9ba79e04 2018-06-11 stsp return NULL;
865 0553a4e3 2018-04-30 stsp static const struct got_error *
866 2814baeb 2018-08-01 stsp draw_commits(struct tog_view *view, struct commit_queue_entry **last,
867 ecb28ae0 2018-07-16 stsp struct commit_queue_entry **selected, struct commit_queue_entry *first,
868 a7f40148 2018-07-18 stsp struct commit_queue *commits, int selected_idx, int limit,
869 a7f40148 2018-07-18 stsp struct got_commit_graph *graph, struct got_repository *repo,
870 a7f40148 2018-07-18 stsp const char *path)
872 0553a4e3 2018-04-30 stsp const struct got_error *err = NULL;
873 0553a4e3 2018-04-30 stsp struct commit_queue_entry *entry;
874 ecb28ae0 2018-07-16 stsp int ncommits, width;
875 867c6645 2018-07-10 stsp char *id_str, *header;
876 ecb28ae0 2018-07-16 stsp wchar_t *wline;
878 e0d42f60 2018-07-22 stsp entry = first;
879 e0d42f60 2018-07-22 stsp ncommits = 0;
880 e0d42f60 2018-07-22 stsp while (entry) {
881 e0d42f60 2018-07-22 stsp if (ncommits == selected_idx) {
882 e0d42f60 2018-07-22 stsp *selected = entry;
885 e0d42f60 2018-07-22 stsp entry = TAILQ_NEXT(entry, entry);
886 e0d42f60 2018-07-22 stsp ncommits++;
889 867c6645 2018-07-10 stsp err = got_object_id_str(&id_str, (*selected)->id);
891 867c6645 2018-07-10 stsp return err;
893 c3ba6f36 2018-08-01 stsp if (path && strcmp(path, "/") != 0) {
894 ecb28ae0 2018-07-16 stsp if (asprintf(&header, "commit: %s [%s]", id_str, path) == -1) {
895 ecb28ae0 2018-07-16 stsp err = got_error_from_errno();
896 ecb28ae0 2018-07-16 stsp free(id_str);
897 ecb28ae0 2018-07-16 stsp return err;
899 ecb28ae0 2018-07-16 stsp } else if (asprintf(&header, "commit: %s", id_str) == -1) {
900 867c6645 2018-07-10 stsp err = got_error_from_errno();
901 867c6645 2018-07-10 stsp free(id_str);
902 867c6645 2018-07-10 stsp return err;
904 ecb28ae0 2018-07-16 stsp free(id_str);
905 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, header, view->ncols);
907 ecb28ae0 2018-07-16 stsp free(header);
908 ecb28ae0 2018-07-16 stsp return err;
910 ecb28ae0 2018-07-16 stsp free(header);
912 2814baeb 2018-08-01 stsp werase(view->window);
914 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
915 a3404814 2018-09-02 stsp wstandout(view->window);
916 2814baeb 2018-08-01 stsp waddwstr(view->window, wline);
917 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
918 a3404814 2018-09-02 stsp wstandend(view->window);
919 f7d12f7e 2018-08-01 stsp if (width < view->ncols)
920 2814baeb 2018-08-01 stsp waddch(view->window, '\n');
921 ecb28ae0 2018-07-16 stsp free(wline);
922 ecb28ae0 2018-07-16 stsp if (limit <= 1)
923 ecb28ae0 2018-07-16 stsp return NULL;
925 899d86c2 2018-05-10 stsp entry = first;
926 899d86c2 2018-05-10 stsp *last = first;
927 867c6645 2018-07-10 stsp ncommits = 0;
928 899d86c2 2018-05-10 stsp while (entry) {
929 ecb28ae0 2018-07-16 stsp if (ncommits >= limit - 1)
931 e0d42f60 2018-07-22 stsp if (ncommits == selected_idx)
932 2814baeb 2018-08-01 stsp wstandout(view->window);
933 2814baeb 2018-08-01 stsp err = draw_commit(view, entry->commit, entry->id);
934 cd0acaa7 2018-05-20 stsp if (ncommits == selected_idx)
935 2814baeb 2018-08-01 stsp wstandend(view->window);
938 0553a4e3 2018-04-30 stsp ncommits++;
939 899d86c2 2018-05-10 stsp *last = entry;
940 a7f40148 2018-07-18 stsp if (entry == TAILQ_LAST(&commits->head, commit_queue_head)) {
941 93e45b7c 2018-09-24 stsp err = queue_commits(graph, commits, NULL, 1,
942 93e45b7c 2018-09-24 stsp repo, path);
944 a7f40148 2018-07-18 stsp if (err->code != GOT_ERR_ITER_COMPLETED)
945 a7f40148 2018-07-18 stsp return err;
946 a7f40148 2018-07-18 stsp err = NULL;
949 899d86c2 2018-05-10 stsp entry = TAILQ_NEXT(entry, entry);
952 1a57306a 2018-09-02 stsp view_vborder(view);
954 80ddbec8 2018-04-29 stsp return err;
957 07b55e75 2018-05-10 stsp static void
958 16482c3b 2018-05-20 stsp scroll_up(struct commit_queue_entry **first_displayed_entry, int maxscroll,
959 07b55e75 2018-05-10 stsp struct commit_queue *commits)
961 07b55e75 2018-05-10 stsp struct commit_queue_entry *entry;
962 07b55e75 2018-05-10 stsp int nscrolled = 0;
964 ecb28ae0 2018-07-16 stsp entry = TAILQ_FIRST(&commits->head);
965 07b55e75 2018-05-10 stsp if (*first_displayed_entry == entry)
968 07b55e75 2018-05-10 stsp entry = *first_displayed_entry;
969 16482c3b 2018-05-20 stsp while (entry && nscrolled < maxscroll) {
970 ecb28ae0 2018-07-16 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
971 07b55e75 2018-05-10 stsp if (entry) {
972 07b55e75 2018-05-10 stsp *first_displayed_entry = entry;
973 07b55e75 2018-05-10 stsp nscrolled++;
978 aa075928 2018-05-10 stsp static const struct got_error *
979 16482c3b 2018-05-20 stsp scroll_down(struct commit_queue_entry **first_displayed_entry, int maxscroll,
980 93e45b7c 2018-09-24 stsp struct commit_queue_entry **last_displayed_entry,
981 9ba79e04 2018-06-11 stsp struct commit_queue *commits, struct got_commit_graph *graph,
982 ecb28ae0 2018-07-16 stsp struct got_repository *repo, const char *path)
984 aa075928 2018-05-10 stsp const struct got_error *err = NULL;
985 dd0a52c1 2018-05-20 stsp struct commit_queue_entry *pentry;
986 aa075928 2018-05-10 stsp int nscrolled = 0;
989 93e45b7c 2018-09-24 stsp pentry = TAILQ_NEXT(*last_displayed_entry, entry);
990 aa075928 2018-05-10 stsp if (pentry == NULL) {
991 93e45b7c 2018-09-24 stsp err = fetch_next_commit(&pentry, *last_displayed_entry,
992 ecb28ae0 2018-07-16 stsp commits, graph, repo, path);
993 9a6bf2a5 2018-05-20 stsp if (err || pentry == NULL)
996 93e45b7c 2018-09-24 stsp *last_displayed_entry = pentry;
998 dd0a52c1 2018-05-20 stsp pentry = TAILQ_NEXT(*first_displayed_entry, entry);
999 dd0a52c1 2018-05-20 stsp if (pentry == NULL)
1001 aa075928 2018-05-10 stsp *first_displayed_entry = pentry;
1002 16482c3b 2018-05-20 stsp } while (++nscrolled < maxscroll);
1004 dd0a52c1 2018-05-20 stsp return err;
1007 cd0acaa7 2018-05-20 stsp static const struct got_error *
1008 e5a0f69f 2018-08-18 stsp show_commit(struct tog_view **new_view, struct tog_view *parent_view,
1009 e5a0f69f 2018-08-18 stsp struct commit_queue_entry *entry, struct got_repository *repo)
1011 cd0acaa7 2018-05-20 stsp const struct got_error *err;
1012 cd0acaa7 2018-05-20 stsp struct got_object *obj1 = NULL, *obj2 = NULL;
1013 9ba79e04 2018-06-11 stsp struct got_object_qid *parent_id;
1014 e5a0f69f 2018-08-18 stsp struct tog_view *diff_view;
1016 cd0acaa7 2018-05-20 stsp err = got_object_open(&obj2, repo, entry->id);
1018 cd0acaa7 2018-05-20 stsp return err;
1020 9ba79e04 2018-06-11 stsp parent_id = SIMPLEQ_FIRST(&entry->commit->parent_ids);
1021 9ba79e04 2018-06-11 stsp if (parent_id) {
1022 9ba79e04 2018-06-11 stsp err = got_object_open(&obj1, repo, parent_id->id);
1024 cd0acaa7 2018-05-20 stsp goto done;
1027 e5a0f69f 2018-08-18 stsp diff_view = view_open(0, 0, 0, 0, parent_view, TOG_VIEW_DIFF);
1028 e5a0f69f 2018-08-18 stsp if (diff_view == NULL) {
1029 ea5e7bb5 2018-08-01 stsp err = got_error_from_errno();
1030 ea5e7bb5 2018-08-01 stsp goto done;
1033 e5a0f69f 2018-08-18 stsp err = open_diff_view(diff_view, obj1, obj2, repo);
1034 e5a0f69f 2018-08-18 stsp if (err == NULL)
1035 e5a0f69f 2018-08-18 stsp *new_view = diff_view;
1038 cd0acaa7 2018-05-20 stsp got_object_close(obj1);
1040 cd0acaa7 2018-05-20 stsp got_object_close(obj2);
1041 cd0acaa7 2018-05-20 stsp return err;
1044 80ddbec8 2018-04-29 stsp static const struct got_error *
1045 e5a0f69f 2018-08-18 stsp browse_commit(struct tog_view **new_view, struct tog_view *parent_view,
1046 e5a0f69f 2018-08-18 stsp struct commit_queue_entry *entry, struct got_repository *repo)
1048 9343a5fb 2018-06-23 stsp const struct got_error *err = NULL;
1049 9343a5fb 2018-06-23 stsp struct got_tree_object *tree;
1050 e5a0f69f 2018-08-18 stsp struct tog_view *tree_view;
1052 9343a5fb 2018-06-23 stsp err = got_object_open_as_tree(&tree, repo, entry->commit->tree_id);
1054 9343a5fb 2018-06-23 stsp return err;
1056 e5a0f69f 2018-08-18 stsp tree_view = view_open(0, 0, 0, 0, parent_view, TOG_VIEW_TREE);
1057 e5a0f69f 2018-08-18 stsp if (tree_view == NULL)
1058 e5a0f69f 2018-08-18 stsp return got_error_from_errno();
1060 e5a0f69f 2018-08-18 stsp err = open_tree_view(tree_view, tree, entry->id, repo);
1062 e5a0f69f 2018-08-18 stsp got_object_tree_close(tree);
1064 e5a0f69f 2018-08-18 stsp *new_view = tree_view;
1065 9343a5fb 2018-06-23 stsp return err;
1068 bcbd79e2 2018-08-19 stsp static const struct got_error *
1069 bcbd79e2 2018-08-19 stsp set_child_log_view(struct tog_view *view, struct tog_view *child)
1071 bcbd79e2 2018-08-19 stsp struct tog_log_view_state *s = &view->state.log;
1072 bcbd79e2 2018-08-19 stsp struct tog_diff_view_state *ds;
1073 bcbd79e2 2018-08-19 stsp struct commit_queue_entry *commit, *child_entry = NULL;
1074 bcbd79e2 2018-08-19 stsp int selected_idx = 0;
1076 bcbd79e2 2018-08-19 stsp if (child->type != TOG_VIEW_DIFF)
1077 bcbd79e2 2018-08-19 stsp return NULL;
1078 bcbd79e2 2018-08-19 stsp ds = &child->state.diff;
1080 bcbd79e2 2018-08-19 stsp TAILQ_FOREACH(commit, &s->commits.head, entry) {
1081 a3404814 2018-09-02 stsp if (got_object_id_cmp(commit->id, ds->id2) == 0) {
1082 bcbd79e2 2018-08-19 stsp child_entry = commit;
1086 bcbd79e2 2018-08-19 stsp if (child_entry == NULL)
1087 bcbd79e2 2018-08-19 stsp return NULL;
1089 bcbd79e2 2018-08-19 stsp commit = s->first_displayed_entry;
1090 bcbd79e2 2018-08-19 stsp while (commit) {
1091 bcbd79e2 2018-08-19 stsp if (got_object_id_cmp(commit->id, child_entry->id) == 0) {
1092 bcbd79e2 2018-08-19 stsp s->selected_entry = child_entry;
1093 bcbd79e2 2018-08-19 stsp s->selected = selected_idx;
1096 bcbd79e2 2018-08-19 stsp if (commit == s->last_displayed_entry)
1098 bcbd79e2 2018-08-19 stsp selected_idx++;
1099 bcbd79e2 2018-08-19 stsp commit = TAILQ_NEXT(commit, entry);
1102 bcbd79e2 2018-08-19 stsp return show_log_view(view);
1105 9343a5fb 2018-06-23 stsp static const struct got_error *
1106 ba4f502b 2018-08-04 stsp open_log_view(struct tog_view *view, struct got_object_id *start_id,
1107 04cc582a 2018-08-01 stsp struct got_repository *repo, const char *path)
1109 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
1110 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
1112 fb2756b9 2018-08-04 stsp err = got_repo_map_path(&s->in_repo_path, repo, path);
1113 ecb28ae0 2018-07-16 stsp if (err != NULL)
1114 ecb28ae0 2018-07-16 stsp goto done;
1116 93e45b7c 2018-09-24 stsp err = got_commit_graph_open(&s->graph, start_id, s->in_repo_path,
1119 9ba79e04 2018-06-11 stsp goto done;
1120 93e45b7c 2018-09-24 stsp /* The commit queue only contains commits being displayed. */
1121 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->commits.head);
1122 fb2756b9 2018-08-04 stsp s->commits.ncommits = 0;
1125 9ba79e04 2018-06-11 stsp * Open the initial batch of commits, sorted in commit graph order.
1126 9ba79e04 2018-06-11 stsp * We keep all commits open throughout the lifetime of the log view
1127 9ba79e04 2018-06-11 stsp * in order to avoid having to re-fetch commits from disk while
1128 9ba79e04 2018-06-11 stsp * updating the display.
1130 93e45b7c 2018-09-24 stsp err = queue_commits(s->graph, &s->commits, start_id, view->nlines,
1131 fb2756b9 2018-08-04 stsp repo, s->in_repo_path);
1132 55198a88 2018-07-16 stsp if (err) {
1133 55198a88 2018-07-16 stsp if (err->code != GOT_ERR_ITER_COMPLETED)
1134 55198a88 2018-07-16 stsp goto done;
1135 55198a88 2018-07-16 stsp err = NULL;
1138 93e45b7c 2018-09-24 stsp s->first_displayed_entry = TAILQ_FIRST(&s->commits.head);
1139 1bfa490b 2018-08-18 stsp s->selected_entry = s->first_displayed_entry;
1140 fb2756b9 2018-08-04 stsp s->repo = repo;
1141 5036bf37 2018-09-24 stsp s->start_id = got_object_id_dup(start_id);
1142 5036bf37 2018-09-24 stsp if (s->start_id == NULL) {
1143 5036bf37 2018-09-24 stsp err = got_error_from_errno();
1144 5036bf37 2018-09-24 stsp goto done;
1147 e5a0f69f 2018-08-18 stsp view->show = show_log_view;
1148 e5a0f69f 2018-08-18 stsp view->input = input_log_view;
1149 e5a0f69f 2018-08-18 stsp view->close = close_log_view;
1150 bcbd79e2 2018-08-19 stsp view->set_child = set_child_log_view;
1152 ba4f502b 2018-08-04 stsp return err;
1155 e5a0f69f 2018-08-18 stsp static const struct got_error *
1156 e5a0f69f 2018-08-18 stsp close_log_view(struct tog_view *view)
1158 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
1160 fb2756b9 2018-08-04 stsp if (s->graph)
1161 fb2756b9 2018-08-04 stsp got_commit_graph_close(s->graph);
1162 fb2756b9 2018-08-04 stsp free_commits(&s->commits);
1163 fb2756b9 2018-08-04 stsp free(s->in_repo_path);
1164 5036bf37 2018-09-24 stsp free(s->start_id);
1165 e5a0f69f 2018-08-18 stsp return NULL;
1168 ba4f502b 2018-08-04 stsp static const struct got_error *
1169 c1fea1f8 2018-09-29 stsp update_diff_view(struct tog_view *diff_view,
1170 c1fea1f8 2018-09-29 stsp struct got_object_id *commit_id, struct got_commit_object *commit,
1171 c1fea1f8 2018-09-29 stsp struct got_repository *repo)
1173 bcbd79e2 2018-08-19 stsp const struct got_error *err = NULL;
1174 bcbd79e2 2018-08-19 stsp struct tog_diff_view_state *ds;
1175 bcbd79e2 2018-08-19 stsp struct got_object *obj1 = NULL, *obj2 = NULL;
1176 bcbd79e2 2018-08-19 stsp struct got_object_qid *parent_id;
1178 c1fea1f8 2018-09-29 stsp ds = &diff_view->state.diff;
1179 c1fea1f8 2018-09-29 stsp if (got_object_id_cmp(ds->id2, commit_id) == 0)
1180 bcbd79e2 2018-08-19 stsp return NULL;
1182 c1fea1f8 2018-09-29 stsp err = got_object_open(&obj2, repo, commit_id);
1184 bcbd79e2 2018-08-19 stsp return err;
1186 c1fea1f8 2018-09-29 stsp parent_id = SIMPLEQ_FIRST(&commit->parent_ids);
1187 bcbd79e2 2018-08-19 stsp if (parent_id) {
1188 bcbd79e2 2018-08-19 stsp err = got_object_open(&obj1, repo, parent_id->id);
1190 bcbd79e2 2018-08-19 stsp goto done;
1193 c1fea1f8 2018-09-29 stsp err = close_diff_view(diff_view);
1195 bcbd79e2 2018-08-19 stsp goto done;
1197 c1fea1f8 2018-09-29 stsp err = open_diff_view(diff_view, obj1, obj2, repo);
1199 bcbd79e2 2018-08-19 stsp goto done;
1202 bcbd79e2 2018-08-19 stsp got_object_close(obj1);
1204 bcbd79e2 2018-08-19 stsp got_object_close(obj2);
1205 bcbd79e2 2018-08-19 stsp return err;
1208 bcbd79e2 2018-08-19 stsp static const struct got_error *
1209 ba4f502b 2018-08-04 stsp show_log_view(struct tog_view *view)
1211 bcbd79e2 2018-08-19 stsp const struct got_error *err = NULL;
1212 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
1214 bcbd79e2 2018-08-19 stsp err = draw_commits(view, &s->last_displayed_entry,
1215 e5a0f69f 2018-08-18 stsp &s->selected_entry, s->first_displayed_entry,
1216 e5a0f69f 2018-08-18 stsp &s->commits, s->selected, view->nlines, s->graph,
1217 e5a0f69f 2018-08-18 stsp s->repo, s->in_repo_path);
1219 bcbd79e2 2018-08-19 stsp return err;
1221 c1fea1f8 2018-09-29 stsp if (view->child && view->child->type == TOG_VIEW_DIFF)
1222 c1fea1f8 2018-09-29 stsp err = update_diff_view(view->child, s->selected_entry->id,
1223 c1fea1f8 2018-09-29 stsp s->selected_entry->commit, s->repo);
1225 c1fea1f8 2018-09-29 stsp return err;
1228 e5a0f69f 2018-08-18 stsp static const struct got_error *
1229 e5a0f69f 2018-08-18 stsp input_log_view(struct tog_view **new_view, struct tog_view **dead_view,
1230 e5a0f69f 2018-08-18 stsp struct tog_view *view, int ch)
1232 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
1233 e5a0f69f 2018-08-18 stsp struct tog_log_view_state *s = &view->state.log;
1234 5036bf37 2018-09-24 stsp char *parent_path;
1236 e5a0f69f 2018-08-18 stsp switch (ch) {
1238 e5a0f69f 2018-08-18 stsp case KEY_UP:
1240 e5a0f69f 2018-08-18 stsp if (s->selected > 0)
1241 e5a0f69f 2018-08-18 stsp s->selected--;
1242 e5a0f69f 2018-08-18 stsp if (s->selected > 0)
1244 e5a0f69f 2018-08-18 stsp scroll_up(&s->first_displayed_entry, 1,
1245 e5a0f69f 2018-08-18 stsp &s->commits);
1247 e5a0f69f 2018-08-18 stsp case KEY_PPAGE:
1248 e5a0f69f 2018-08-18 stsp if (TAILQ_FIRST(&s->commits.head) ==
1249 e5a0f69f 2018-08-18 stsp s->first_displayed_entry) {
1250 e5a0f69f 2018-08-18 stsp s->selected = 0;
1253 e5a0f69f 2018-08-18 stsp scroll_up(&s->first_displayed_entry,
1254 e5a0f69f 2018-08-18 stsp view->nlines, &s->commits);
1257 e5a0f69f 2018-08-18 stsp case KEY_DOWN:
1259 e5a0f69f 2018-08-18 stsp if (s->selected < MIN(view->nlines - 2,
1260 e5a0f69f 2018-08-18 stsp s->commits.ncommits - 1)) {
1261 e5a0f69f 2018-08-18 stsp s->selected++;
1264 e5a0f69f 2018-08-18 stsp err = scroll_down(&s->first_displayed_entry, 1,
1265 93e45b7c 2018-09-24 stsp &s->last_displayed_entry, &s->commits,
1266 e5a0f69f 2018-08-18 stsp s->graph, s->repo, s->in_repo_path);
1267 e5a0f69f 2018-08-18 stsp if (err) {
1268 4d7951a5 2018-08-04 stsp if (err->code != GOT_ERR_ITER_COMPLETED)
1270 4d7951a5 2018-08-04 stsp err = NULL;
1273 e5a0f69f 2018-08-18 stsp case KEY_NPAGE: {
1274 e5a0f69f 2018-08-18 stsp struct commit_queue_entry *first;
1275 e5a0f69f 2018-08-18 stsp first = s->first_displayed_entry;
1276 e5a0f69f 2018-08-18 stsp err = scroll_down(&s->first_displayed_entry,
1277 93e45b7c 2018-09-24 stsp view->nlines, &s->last_displayed_entry,
1278 e5a0f69f 2018-08-18 stsp &s->commits, s->graph, s->repo,
1279 e5a0f69f 2018-08-18 stsp s->in_repo_path);
1280 93e45b7c 2018-09-24 stsp if (err && err->code != GOT_ERR_ITER_COMPLETED)
1282 e5a0f69f 2018-08-18 stsp if (first == s->first_displayed_entry &&
1283 e5a0f69f 2018-08-18 stsp s->selected < MIN(view->nlines - 2,
1284 e5a0f69f 2018-08-18 stsp s->commits.ncommits - 1)) {
1285 e5a0f69f 2018-08-18 stsp /* can't scroll further down */
1286 e5a0f69f 2018-08-18 stsp s->selected = MIN(view->nlines - 2,
1287 e5a0f69f 2018-08-18 stsp s->commits.ncommits - 1);
1289 e5a0f69f 2018-08-18 stsp err = NULL;
1292 e5a0f69f 2018-08-18 stsp case KEY_RESIZE:
1293 e5a0f69f 2018-08-18 stsp if (s->selected > view->nlines - 2)
1294 e5a0f69f 2018-08-18 stsp s->selected = view->nlines - 2;
1295 e5a0f69f 2018-08-18 stsp if (s->selected > s->commits.ncommits - 1)
1296 e5a0f69f 2018-08-18 stsp s->selected = s->commits.ncommits - 1;
1298 e5a0f69f 2018-08-18 stsp case KEY_ENTER:
1299 e5a0f69f 2018-08-18 stsp case '\r':
1300 e5a0f69f 2018-08-18 stsp err = show_commit(new_view, view, s->selected_entry,
1304 e5a0f69f 2018-08-18 stsp err = browse_commit(new_view, view, s->selected_entry,
1307 5036bf37 2018-09-24 stsp case KEY_BACKSPACE:
1308 5036bf37 2018-09-24 stsp if (strcmp(s->in_repo_path, "/") == 0)
1310 5036bf37 2018-09-24 stsp parent_path = dirname(s->in_repo_path);
1311 5036bf37 2018-09-24 stsp if (parent_path && strcmp(parent_path, ".") != 0) {
1312 5036bf37 2018-09-24 stsp struct tog_view *lv;
1313 5036bf37 2018-09-24 stsp lv = view_open(0, 0, 0, 0, NULL, TOG_VIEW_LOG);
1314 5036bf37 2018-09-24 stsp if (lv == NULL)
1315 5036bf37 2018-09-24 stsp return got_error_from_errno();
1316 5036bf37 2018-09-24 stsp err = open_log_view(lv, s->start_id, s->repo,
1317 5036bf37 2018-09-24 stsp parent_path);
1320 5036bf37 2018-09-24 stsp *new_view = lv;
1327 80ddbec8 2018-04-29 stsp return err;
1330 4ed7e80c 2018-05-20 stsp static const struct got_error *
1331 9f7d7167 2018-04-29 stsp cmd_log(int argc, char *argv[])
1333 80ddbec8 2018-04-29 stsp const struct got_error *error;
1334 ecb28ae0 2018-07-16 stsp struct got_repository *repo = NULL;
1335 899d86c2 2018-05-10 stsp struct got_object_id *start_id = NULL;
1336 ecb28ae0 2018-07-16 stsp char *path = NULL, *repo_path = NULL, *cwd = NULL;
1337 80ddbec8 2018-04-29 stsp char *start_commit = NULL;
1339 04cc582a 2018-08-01 stsp struct tog_view *view;
1341 80ddbec8 2018-04-29 stsp #ifndef PROFILE
1342 ad242220 2018-09-08 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd", NULL)
1344 80ddbec8 2018-04-29 stsp err(1, "pledge");
1347 ecb28ae0 2018-07-16 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1348 80ddbec8 2018-04-29 stsp switch (ch) {
1350 80ddbec8 2018-04-29 stsp start_commit = optarg;
1353 ecb28ae0 2018-07-16 stsp repo_path = realpath(optarg, NULL);
1354 ecb28ae0 2018-07-16 stsp if (repo_path == NULL)
1355 ecb28ae0 2018-07-16 stsp err(1, "-r option");
1359 80ddbec8 2018-04-29 stsp /* NOTREACHED */
1363 80ddbec8 2018-04-29 stsp argc -= optind;
1364 80ddbec8 2018-04-29 stsp argv += optind;
1366 ecb28ae0 2018-07-16 stsp if (argc == 0)
1367 ecb28ae0 2018-07-16 stsp path = strdup("");
1368 ecb28ae0 2018-07-16 stsp else if (argc == 1)
1369 ecb28ae0 2018-07-16 stsp path = strdup(argv[0]);
1371 80ddbec8 2018-04-29 stsp usage_log();
1372 ecb28ae0 2018-07-16 stsp if (path == NULL)
1373 ecb28ae0 2018-07-16 stsp return got_error_from_errno();
1375 ecb28ae0 2018-07-16 stsp cwd = getcwd(NULL, 0);
1376 ecb28ae0 2018-07-16 stsp if (cwd == NULL) {
1377 ecb28ae0 2018-07-16 stsp error = got_error_from_errno();
1378 ecb28ae0 2018-07-16 stsp goto done;
1380 ecb28ae0 2018-07-16 stsp if (repo_path == NULL) {
1381 ecb28ae0 2018-07-16 stsp repo_path = strdup(cwd);
1382 ecb28ae0 2018-07-16 stsp if (repo_path == NULL) {
1383 ecb28ae0 2018-07-16 stsp error = got_error_from_errno();
1384 ecb28ae0 2018-07-16 stsp goto done;
1388 80ddbec8 2018-04-29 stsp error = got_repo_open(&repo, repo_path);
1389 80ddbec8 2018-04-29 stsp if (error != NULL)
1390 ecb28ae0 2018-07-16 stsp goto done;
1392 80ddbec8 2018-04-29 stsp if (start_commit == NULL) {
1393 899d86c2 2018-05-10 stsp error = get_head_commit_id(&start_id, repo);
1394 80ddbec8 2018-04-29 stsp if (error != NULL)
1395 ecb28ae0 2018-07-16 stsp goto done;
1397 80ddbec8 2018-04-29 stsp struct got_object *obj;
1398 80ddbec8 2018-04-29 stsp error = got_object_open_by_id_str(&obj, repo, start_commit);
1399 80ddbec8 2018-04-29 stsp if (error == NULL) {
1400 6402fb3c 2018-09-15 stsp start_id = got_object_id_dup(got_object_get_id(obj));
1401 899d86c2 2018-05-10 stsp if (start_id == NULL)
1402 80ddbec8 2018-04-29 stsp error = got_error_from_errno();
1403 ecb28ae0 2018-07-16 stsp goto done;
1406 80ddbec8 2018-04-29 stsp if (error != NULL)
1407 ecb28ae0 2018-07-16 stsp goto done;
1409 b3665f43 2018-08-04 stsp view = view_open(0, 0, 0, 0, NULL, TOG_VIEW_LOG);
1410 04cc582a 2018-08-01 stsp if (view == NULL) {
1411 04cc582a 2018-08-01 stsp error = got_error_from_errno();
1412 04cc582a 2018-08-01 stsp goto done;
1414 ba4f502b 2018-08-04 stsp error = open_log_view(view, start_id, repo, path);
1415 ba4f502b 2018-08-04 stsp if (error)
1416 ba4f502b 2018-08-04 stsp goto done;
1417 e5a0f69f 2018-08-18 stsp error = view_loop(view);
1419 ecb28ae0 2018-07-16 stsp free(repo_path);
1420 ecb28ae0 2018-07-16 stsp free(cwd);
1421 ecb28ae0 2018-07-16 stsp free(path);
1422 899d86c2 2018-05-10 stsp free(start_id);
1424 ecb28ae0 2018-07-16 stsp got_repo_close(repo);
1425 80ddbec8 2018-04-29 stsp return error;
1428 4ed7e80c 2018-05-20 stsp __dead static void
1429 9f7d7167 2018-04-29 stsp usage_diff(void)
1432 9f7d7167 2018-04-29 stsp fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
1433 9f7d7167 2018-04-29 stsp getprogname());
1437 b304db33 2018-05-20 stsp static char *
1438 b304db33 2018-05-20 stsp parse_next_line(FILE *f, size_t *len)
1440 b304db33 2018-05-20 stsp char *line;
1441 b304db33 2018-05-20 stsp size_t linelen;
1442 b304db33 2018-05-20 stsp size_t lineno;
1443 b304db33 2018-05-20 stsp const char delim[3] = { '\0', '\0', '\0'};
1445 b304db33 2018-05-20 stsp line = fparseln(f, &linelen, &lineno, delim, 0);
1447 b304db33 2018-05-20 stsp *len = linelen;
1448 b304db33 2018-05-20 stsp return line;
1451 4ed7e80c 2018-05-20 stsp static const struct got_error *
1452 f7d12f7e 2018-08-01 stsp draw_file(struct tog_view *view, FILE *f, int *first_displayed_line,
1453 a3404814 2018-09-02 stsp int *last_displayed_line, int *eof, int max_lines,
1454 a3404814 2018-09-02 stsp char * header)
1456 61e69b96 2018-05-20 stsp const struct got_error *err;
1457 26ed57b2 2018-05-19 stsp int nlines = 0, nprinted = 0;
1458 b304db33 2018-05-20 stsp char *line;
1459 b304db33 2018-05-20 stsp size_t len;
1460 61e69b96 2018-05-20 stsp wchar_t *wline;
1461 e0b650dd 2018-05-20 stsp int width;
1463 26ed57b2 2018-05-19 stsp rewind(f);
1464 f7d12f7e 2018-08-01 stsp werase(view->window);
1466 a3404814 2018-09-02 stsp if (header) {
1467 a3404814 2018-09-02 stsp err = format_line(&wline, &width, header, view->ncols);
1468 a3404814 2018-09-02 stsp if (err) {
1469 a3404814 2018-09-02 stsp return err;
1472 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
1473 a3404814 2018-09-02 stsp wstandout(view->window);
1474 a3404814 2018-09-02 stsp waddwstr(view->window, wline);
1475 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
1476 a3404814 2018-09-02 stsp wstandend(view->window);
1477 a3404814 2018-09-02 stsp if (width < view->ncols)
1478 a3404814 2018-09-02 stsp waddch(view->window, '\n');
1480 a3404814 2018-09-02 stsp if (max_lines <= 1)
1481 a3404814 2018-09-02 stsp return NULL;
1482 a3404814 2018-09-02 stsp max_lines--;
1486 26ed57b2 2018-05-19 stsp while (nprinted < max_lines) {
1487 b304db33 2018-05-20 stsp line = parse_next_line(f, &len);
1488 26ed57b2 2018-05-19 stsp if (line == NULL) {
1492 26ed57b2 2018-05-19 stsp if (++nlines < *first_displayed_line) {
1493 26ed57b2 2018-05-19 stsp free(line);
1497 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, line, view->ncols);
1498 61e69b96 2018-05-20 stsp if (err) {
1499 61e69b96 2018-05-20 stsp free(line);
1500 61e69b96 2018-05-20 stsp return err;
1502 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
1503 f7d12f7e 2018-08-01 stsp if (width < view->ncols)
1504 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
1505 26ed57b2 2018-05-19 stsp if (++nprinted == 1)
1506 26ed57b2 2018-05-19 stsp *first_displayed_line = nlines;
1507 26ed57b2 2018-05-19 stsp free(line);
1508 2550e4c3 2018-07-13 stsp free(wline);
1509 2550e4c3 2018-07-13 stsp wline = NULL;
1511 26ed57b2 2018-05-19 stsp *last_displayed_line = nlines;
1513 1a57306a 2018-09-02 stsp view_vborder(view);
1515 26ed57b2 2018-05-19 stsp return NULL;
1518 4ed7e80c 2018-05-20 stsp static const struct got_error *
1519 5dc9f4bc 2018-08-04 stsp open_diff_view(struct tog_view *view, struct got_object *obj1,
1520 ea5e7bb5 2018-08-01 stsp struct got_object *obj2, struct got_repository *repo)
1522 26ed57b2 2018-05-19 stsp const struct got_error *err;
1525 cd0acaa7 2018-05-20 stsp if (obj1 != NULL && obj2 != NULL &&
1526 cd0acaa7 2018-05-20 stsp got_object_get_type(obj1) != got_object_get_type(obj2))
1527 26ed57b2 2018-05-19 stsp return got_error(GOT_ERR_OBJ_TYPE);
1529 511a516b 2018-05-19 stsp f = got_opentemp();
1530 26ed57b2 2018-05-19 stsp if (f == NULL)
1531 26ed57b2 2018-05-19 stsp return got_error_from_errno();
1533 cd0acaa7 2018-05-20 stsp switch (got_object_get_type(obj1 ? obj1 : obj2)) {
1534 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_BLOB:
1535 f6861a81 2018-09-13 stsp err = got_diff_objects_as_blobs(obj1, obj2, NULL, NULL,
1538 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_TREE:
1539 f6861a81 2018-09-13 stsp err = got_diff_objects_as_trees(obj1, obj2, "", "", repo, f);
1541 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_COMMIT:
1542 11528a82 2018-05-19 stsp err = got_diff_objects_as_commits(obj1, obj2, repo, f);
1545 26ed57b2 2018-05-19 stsp return got_error(GOT_ERR_OBJ_TYPE);
1548 26ed57b2 2018-05-19 stsp fflush(f);
1550 a3404814 2018-09-02 stsp view->state.diff.id1 = obj1 ? got_object_get_id(obj1) : NULL;
1551 a3404814 2018-09-02 stsp view->state.diff.id2 = got_object_get_id(obj2);
1552 5dc9f4bc 2018-08-04 stsp view->state.diff.f = f;
1553 5dc9f4bc 2018-08-04 stsp view->state.diff.first_displayed_line = 1;
1554 5dc9f4bc 2018-08-04 stsp view->state.diff.last_displayed_line = view->nlines;
1556 e5a0f69f 2018-08-18 stsp view->show = show_diff_view;
1557 e5a0f69f 2018-08-18 stsp view->input = input_diff_view;
1558 e5a0f69f 2018-08-18 stsp view->close = close_diff_view;
1560 5dc9f4bc 2018-08-04 stsp return NULL;
1563 e5a0f69f 2018-08-18 stsp static const struct got_error *
1564 5dc9f4bc 2018-08-04 stsp close_diff_view(struct tog_view *view)
1566 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
1568 e5a0f69f 2018-08-18 stsp if (view->state.diff.f && fclose(view->state.diff.f) == EOF)
1569 e5a0f69f 2018-08-18 stsp err = got_error_from_errno();
1570 e5a0f69f 2018-08-18 stsp return err;
1573 5dc9f4bc 2018-08-04 stsp static const struct got_error *
1574 5dc9f4bc 2018-08-04 stsp show_diff_view(struct tog_view *view)
1576 a3404814 2018-09-02 stsp const struct got_error *err;
1577 fb2756b9 2018-08-04 stsp struct tog_diff_view_state *s = &view->state.diff;
1578 a3404814 2018-09-02 stsp char *id_str1 = NULL, *id_str2, *header;
1580 a3404814 2018-09-02 stsp if (s->id1) {
1581 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str1, s->id1);
1583 a3404814 2018-09-02 stsp return err;
1585 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str2, s->id2);
1587 a3404814 2018-09-02 stsp return err;
1589 a3404814 2018-09-02 stsp if (asprintf(&header, "diff: %s %s",
1590 a3404814 2018-09-02 stsp id_str1 ? id_str1 : "/dev/null", id_str2) == -1) {
1591 a3404814 2018-09-02 stsp err = got_error_from_errno();
1592 a3404814 2018-09-02 stsp free(id_str1);
1593 a3404814 2018-09-02 stsp free(id_str2);
1594 a3404814 2018-09-02 stsp return err;
1596 a3404814 2018-09-02 stsp free(id_str1);
1597 a3404814 2018-09-02 stsp free(id_str2);
1599 e5a0f69f 2018-08-18 stsp return draw_file(view, s->f, &s->first_displayed_line,
1600 a3404814 2018-09-02 stsp &s->last_displayed_line, &s->eof, view->nlines,
1604 e5a0f69f 2018-08-18 stsp static const struct got_error *
1605 bcbd79e2 2018-08-19 stsp input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
1606 e5a0f69f 2018-08-18 stsp struct tog_view *view, int ch)
1608 bcbd79e2 2018-08-19 stsp const struct got_error *err = NULL;
1609 e5a0f69f 2018-08-18 stsp struct tog_diff_view_state *s = &view->state.diff;
1612 e5a0f69f 2018-08-18 stsp switch (ch) {
1614 e5a0f69f 2018-08-18 stsp case KEY_UP:
1615 e5a0f69f 2018-08-18 stsp if (s->first_displayed_line > 1)
1616 e5a0f69f 2018-08-18 stsp s->first_displayed_line--;
1618 e5a0f69f 2018-08-18 stsp case KEY_PPAGE:
1620 e5a0f69f 2018-08-18 stsp while (i++ < view->nlines - 1 &&
1621 e5a0f69f 2018-08-18 stsp s->first_displayed_line > 1)
1622 e5a0f69f 2018-08-18 stsp s->first_displayed_line--;
1625 e5a0f69f 2018-08-18 stsp case KEY_DOWN:
1626 e5a0f69f 2018-08-18 stsp if (!s->eof)
1627 e5a0f69f 2018-08-18 stsp s->first_displayed_line++;
1629 e5a0f69f 2018-08-18 stsp case KEY_NPAGE:
1632 e5a0f69f 2018-08-18 stsp while (!s->eof && i++ < view->nlines - 1) {
1633 e5a0f69f 2018-08-18 stsp char *line;
1634 e5a0f69f 2018-08-18 stsp line = parse_next_line(s->f, NULL);
1635 e5a0f69f 2018-08-18 stsp s->first_displayed_line++;
1636 e5a0f69f 2018-08-18 stsp if (line == NULL)
1641 bcbd79e2 2018-08-19 stsp case ']': {
1642 bcbd79e2 2018-08-19 stsp struct tog_log_view_state *ls;
1643 bcbd79e2 2018-08-19 stsp struct commit_queue_entry *entry;
1644 bcbd79e2 2018-08-19 stsp struct tog_view *diff_view;
1646 bcbd79e2 2018-08-19 stsp if (view->parent == NULL)
1648 bcbd79e2 2018-08-19 stsp if (view->parent->type != TOG_VIEW_LOG)
1650 bcbd79e2 2018-08-19 stsp ls = &view->parent->state.log;
1652 bcbd79e2 2018-08-19 stsp if (ch == '[') {
1653 bcbd79e2 2018-08-19 stsp entry = TAILQ_PREV(ls->selected_entry,
1654 bcbd79e2 2018-08-19 stsp commit_queue_head, entry);
1656 bcbd79e2 2018-08-19 stsp entry = TAILQ_NEXT(ls->selected_entry, entry);
1657 bcbd79e2 2018-08-19 stsp if (entry == NULL) {
1658 bcbd79e2 2018-08-19 stsp err = fetch_next_commit(&entry,
1659 bcbd79e2 2018-08-19 stsp ls->selected_entry,
1660 bcbd79e2 2018-08-19 stsp &ls->commits, ls->graph,
1661 bcbd79e2 2018-08-19 stsp ls->repo, ls->in_repo_path);
1666 bcbd79e2 2018-08-19 stsp if (entry == NULL)
1668 bcbd79e2 2018-08-19 stsp err = show_commit(&diff_view, view->parent,
1669 bcbd79e2 2018-08-19 stsp entry, ls->repo);
1672 bcbd79e2 2018-08-19 stsp *new_view = diff_view;
1673 bcbd79e2 2018-08-19 stsp *dead_view = view;
1680 bcbd79e2 2018-08-19 stsp return err;
1683 4ed7e80c 2018-05-20 stsp static const struct got_error *
1684 9f7d7167 2018-04-29 stsp cmd_diff(int argc, char *argv[])
1686 26ed57b2 2018-05-19 stsp const struct got_error *error = NULL;
1687 26ed57b2 2018-05-19 stsp struct got_repository *repo = NULL;
1688 26ed57b2 2018-05-19 stsp struct got_object *obj1 = NULL, *obj2 = NULL;
1689 26ed57b2 2018-05-19 stsp char *repo_path = NULL;
1690 26ed57b2 2018-05-19 stsp char *obj_id_str1 = NULL, *obj_id_str2 = NULL;
1692 ea5e7bb5 2018-08-01 stsp struct tog_view *view;
1694 26ed57b2 2018-05-19 stsp #ifndef PROFILE
1695 ad242220 2018-09-08 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd", NULL)
1697 26ed57b2 2018-05-19 stsp err(1, "pledge");
1700 26ed57b2 2018-05-19 stsp while ((ch = getopt(argc, argv, "")) != -1) {
1701 26ed57b2 2018-05-19 stsp switch (ch) {
1704 26ed57b2 2018-05-19 stsp /* NOTREACHED */
1708 26ed57b2 2018-05-19 stsp argc -= optind;
1709 26ed57b2 2018-05-19 stsp argv += optind;
1711 26ed57b2 2018-05-19 stsp if (argc == 0) {
1712 26ed57b2 2018-05-19 stsp usage_diff(); /* TODO show local worktree changes */
1713 26ed57b2 2018-05-19 stsp } else if (argc == 2) {
1714 26ed57b2 2018-05-19 stsp repo_path = getcwd(NULL, 0);
1715 26ed57b2 2018-05-19 stsp if (repo_path == NULL)
1716 26ed57b2 2018-05-19 stsp return got_error_from_errno();
1717 26ed57b2 2018-05-19 stsp obj_id_str1 = argv[0];
1718 26ed57b2 2018-05-19 stsp obj_id_str2 = argv[1];
1719 26ed57b2 2018-05-19 stsp } else if (argc == 3) {
1720 26ed57b2 2018-05-19 stsp repo_path = realpath(argv[0], NULL);
1721 26ed57b2 2018-05-19 stsp if (repo_path == NULL)
1722 26ed57b2 2018-05-19 stsp return got_error_from_errno();
1723 26ed57b2 2018-05-19 stsp obj_id_str1 = argv[1];
1724 26ed57b2 2018-05-19 stsp obj_id_str2 = argv[2];
1726 26ed57b2 2018-05-19 stsp usage_diff();
1728 26ed57b2 2018-05-19 stsp error = got_repo_open(&repo, repo_path);
1729 26ed57b2 2018-05-19 stsp free(repo_path);
1730 26ed57b2 2018-05-19 stsp if (error)
1731 26ed57b2 2018-05-19 stsp goto done;
1733 26ed57b2 2018-05-19 stsp error = got_object_open_by_id_str(&obj1, repo, obj_id_str1);
1734 26ed57b2 2018-05-19 stsp if (error)
1735 26ed57b2 2018-05-19 stsp goto done;
1737 26ed57b2 2018-05-19 stsp error = got_object_open_by_id_str(&obj2, repo, obj_id_str2);
1738 26ed57b2 2018-05-19 stsp if (error)
1739 26ed57b2 2018-05-19 stsp goto done;
1741 b3665f43 2018-08-04 stsp view = view_open(0, 0, 0, 0, NULL, TOG_VIEW_DIFF);
1742 ea5e7bb5 2018-08-01 stsp if (view == NULL) {
1743 ea5e7bb5 2018-08-01 stsp error = got_error_from_errno();
1744 ea5e7bb5 2018-08-01 stsp goto done;
1746 5dc9f4bc 2018-08-04 stsp error = open_diff_view(view, obj1, obj2, repo);
1747 5dc9f4bc 2018-08-04 stsp if (error)
1748 5dc9f4bc 2018-08-04 stsp goto done;
1749 e5a0f69f 2018-08-18 stsp error = view_loop(view);
1751 26ed57b2 2018-05-19 stsp got_repo_close(repo);
1753 26ed57b2 2018-05-19 stsp got_object_close(obj1);
1755 26ed57b2 2018-05-19 stsp got_object_close(obj2);
1756 26ed57b2 2018-05-19 stsp return error;
1759 4ed7e80c 2018-05-20 stsp __dead static void
1760 9f7d7167 2018-04-29 stsp usage_blame(void)
1763 69069811 2018-08-02 stsp fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
1764 9f7d7167 2018-04-29 stsp getprogname());
1768 84451b3e 2018-07-10 stsp struct tog_blame_line {
1769 84451b3e 2018-07-10 stsp int annotated;
1770 84451b3e 2018-07-10 stsp struct got_object_id *id;
1773 4ed7e80c 2018-05-20 stsp static const struct got_error *
1774 f7d12f7e 2018-08-01 stsp draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
1775 f7d12f7e 2018-08-01 stsp const char *path, struct tog_blame_line *lines, int nlines,
1776 f7d12f7e 2018-08-01 stsp int blame_complete, int selected_line, int *first_displayed_line,
1777 f7d12f7e 2018-08-01 stsp int *last_displayed_line, int *eof, int max_lines)
1779 84451b3e 2018-07-10 stsp const struct got_error *err;
1780 84451b3e 2018-07-10 stsp int lineno = 0, nprinted = 0;
1781 84451b3e 2018-07-10 stsp char *line;
1782 84451b3e 2018-07-10 stsp size_t len;
1783 84451b3e 2018-07-10 stsp wchar_t *wline;
1784 b700b5d6 2018-07-10 stsp int width, wlimit;
1785 84451b3e 2018-07-10 stsp struct tog_blame_line *blame_line;
1786 ee41ec32 2018-07-10 stsp struct got_object_id *prev_id = NULL;
1787 ab089a2a 2018-07-12 stsp char *id_str;
1789 ab089a2a 2018-07-12 stsp err = got_object_id_str(&id_str, id);
1791 ab089a2a 2018-07-12 stsp return err;
1793 84451b3e 2018-07-10 stsp rewind(f);
1794 f7d12f7e 2018-08-01 stsp werase(view->window);
1796 ab089a2a 2018-07-12 stsp if (asprintf(&line, "commit: %s", id_str) == -1) {
1797 ab089a2a 2018-07-12 stsp err = got_error_from_errno();
1798 ab089a2a 2018-07-12 stsp free(id_str);
1799 ab089a2a 2018-07-12 stsp return err;
1802 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, line, view->ncols);
1803 ab089a2a 2018-07-12 stsp free(line);
1804 2550e4c3 2018-07-13 stsp line = NULL;
1805 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
1806 a3404814 2018-09-02 stsp wstandout(view->window);
1807 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
1808 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
1809 a3404814 2018-09-02 stsp wstandend(view->window);
1810 2550e4c3 2018-07-13 stsp free(wline);
1811 2550e4c3 2018-07-13 stsp wline = NULL;
1812 f7d12f7e 2018-08-01 stsp if (width < view->ncols)
1813 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
1815 084063cd 2018-07-12 stsp if (asprintf(&line, "[%d/%d] %s%s",
1816 084063cd 2018-07-12 stsp *first_displayed_line - 1 + selected_line, nlines,
1817 12a0b23b 2018-07-12 stsp blame_complete ? "" : "annotating ", path) == -1) {
1818 ab089a2a 2018-07-12 stsp free(id_str);
1819 3f60a8ef 2018-07-10 stsp return got_error_from_errno();
1821 ab089a2a 2018-07-12 stsp free(id_str);
1822 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, line, view->ncols);
1823 3f60a8ef 2018-07-10 stsp free(line);
1824 2550e4c3 2018-07-13 stsp line = NULL;
1826 3f60a8ef 2018-07-10 stsp return err;
1827 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
1828 2550e4c3 2018-07-13 stsp free(wline);
1829 2550e4c3 2018-07-13 stsp wline = NULL;
1830 f7d12f7e 2018-08-01 stsp if (width < view->ncols)
1831 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
1834 ab089a2a 2018-07-12 stsp while (nprinted < max_lines - 2) {
1835 84451b3e 2018-07-10 stsp line = parse_next_line(f, &len);
1836 84451b3e 2018-07-10 stsp if (line == NULL) {
1840 84451b3e 2018-07-10 stsp if (++lineno < *first_displayed_line) {
1841 84451b3e 2018-07-10 stsp free(line);
1845 f7d12f7e 2018-08-01 stsp wlimit = view->ncols < 9 ? 0 : view->ncols - 9;
1846 b700b5d6 2018-07-10 stsp err = format_line(&wline, &width, line, wlimit);
1847 84451b3e 2018-07-10 stsp if (err) {
1848 84451b3e 2018-07-10 stsp free(line);
1849 84451b3e 2018-07-10 stsp return err;
1852 b700b5d6 2018-07-10 stsp if (nprinted == selected_line - 1)
1853 f7d12f7e 2018-08-01 stsp wstandout(view->window);
1855 84451b3e 2018-07-10 stsp blame_line = &lines[lineno - 1];
1856 ee41ec32 2018-07-10 stsp if (blame_line->annotated && prev_id &&
1857 ee41ec32 2018-07-10 stsp got_object_id_cmp(prev_id, blame_line->id) == 0)
1858 f7d12f7e 2018-08-01 stsp waddstr(view->window, " ");
1859 ee41ec32 2018-07-10 stsp else if (blame_line->annotated) {
1860 84451b3e 2018-07-10 stsp char *id_str;
1861 84451b3e 2018-07-10 stsp err = got_object_id_str(&id_str, blame_line->id);
1862 84451b3e 2018-07-10 stsp if (err) {
1863 84451b3e 2018-07-10 stsp free(line);
1864 2550e4c3 2018-07-13 stsp free(wline);
1865 84451b3e 2018-07-10 stsp return err;
1867 f7d12f7e 2018-08-01 stsp wprintw(view->window, "%.8s ", id_str);
1868 84451b3e 2018-07-10 stsp free(id_str);
1869 ee41ec32 2018-07-10 stsp prev_id = blame_line->id;
1871 f7d12f7e 2018-08-01 stsp waddstr(view->window, "........ ");
1872 ee41ec32 2018-07-10 stsp prev_id = NULL;
1875 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
1876 b700b5d6 2018-07-10 stsp while (width < wlimit) {
1877 f7d12f7e 2018-08-01 stsp waddch(view->window, ' ');
1880 b700b5d6 2018-07-10 stsp if (nprinted == selected_line - 1)
1881 f7d12f7e 2018-08-01 stsp wstandend(view->window);
1882 84451b3e 2018-07-10 stsp if (++nprinted == 1)
1883 84451b3e 2018-07-10 stsp *first_displayed_line = lineno;
1884 84451b3e 2018-07-10 stsp free(line);
1885 2550e4c3 2018-07-13 stsp free(wline);
1886 2550e4c3 2018-07-13 stsp wline = NULL;
1888 84451b3e 2018-07-10 stsp *last_displayed_line = lineno;
1890 1a57306a 2018-09-02 stsp view_vborder(view);
1892 84451b3e 2018-07-10 stsp return NULL;
1895 84451b3e 2018-07-10 stsp static const struct got_error *
1896 84451b3e 2018-07-10 stsp blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
1898 84451b3e 2018-07-10 stsp const struct got_error *err = NULL;
1899 84451b3e 2018-07-10 stsp struct tog_blame_cb_args *a = arg;
1900 84451b3e 2018-07-10 stsp struct tog_blame_line *line;
1902 d68a0a7d 2018-07-10 stsp if (nlines != a->nlines ||
1903 d68a0a7d 2018-07-10 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
1904 84451b3e 2018-07-10 stsp return got_error(GOT_ERR_RANGE);
1906 84451b3e 2018-07-10 stsp if (pthread_mutex_lock(a->mutex) != 0)
1907 84451b3e 2018-07-10 stsp return got_error_from_errno();
1909 18430de3 2018-07-10 stsp if (*a->quit) { /* user has quit the blame view */
1910 d68a0a7d 2018-07-10 stsp err = got_error(GOT_ERR_ITER_COMPLETED);
1911 d68a0a7d 2018-07-10 stsp goto done;
1914 d68a0a7d 2018-07-10 stsp if (lineno == -1)
1915 d68a0a7d 2018-07-10 stsp goto done; /* no change in this commit */
1917 84451b3e 2018-07-10 stsp line = &a->lines[lineno - 1];
1918 d68a0a7d 2018-07-10 stsp if (line->annotated)
1919 d68a0a7d 2018-07-10 stsp goto done;
1921 84451b3e 2018-07-10 stsp line->id = got_object_id_dup(id);
1922 84451b3e 2018-07-10 stsp if (line->id == NULL) {
1923 84451b3e 2018-07-10 stsp err = got_error_from_errno();
1924 84451b3e 2018-07-10 stsp goto done;
1926 84451b3e 2018-07-10 stsp line->annotated = 1;
1928 f7d12f7e 2018-08-01 stsp err = draw_blame(a->view, a->commit_id, a->f, a->path,
1929 245d91c1 2018-07-12 stsp a->lines, a->nlines, 0, *a->selected_line, a->first_displayed_line,
1930 e5a0f69f 2018-08-18 stsp a->last_displayed_line, a->eof, a->view->nlines);
1932 84451b3e 2018-07-10 stsp if (pthread_mutex_unlock(a->mutex) != 0)
1933 84451b3e 2018-07-10 stsp return got_error_from_errno();
1934 84451b3e 2018-07-10 stsp return err;
1937 84451b3e 2018-07-10 stsp static void *
1938 84451b3e 2018-07-10 stsp blame_thread(void *arg)
1940 18430de3 2018-07-10 stsp const struct got_error *err;
1941 18430de3 2018-07-10 stsp struct tog_blame_thread_args *ta = arg;
1942 245d91c1 2018-07-12 stsp struct tog_blame_cb_args *a = ta->cb_args;
1944 ab089a2a 2018-07-12 stsp err = got_blame_incremental(ta->path, a->commit_id, ta->repo,
1945 245d91c1 2018-07-12 stsp blame_cb, ta->cb_args);
1947 18430de3 2018-07-10 stsp if (pthread_mutex_lock(a->mutex) != 0)
1948 18430de3 2018-07-10 stsp return (void *)got_error_from_errno();
1950 c9beca56 2018-07-22 stsp got_repo_close(ta->repo);
1951 c9beca56 2018-07-22 stsp ta->repo = NULL;
1952 c9beca56 2018-07-22 stsp *ta->complete = 1;
1954 f7d12f7e 2018-08-01 stsp err = draw_blame(a->view, a->commit_id, a->f, a->path,
1955 f7d12f7e 2018-08-01 stsp a->lines, a->nlines, 1, *a->selected_line,
1956 e5a0f69f 2018-08-18 stsp a->first_displayed_line, a->last_displayed_line, a->eof,
1957 f7d12f7e 2018-08-01 stsp a->view->nlines);
1959 18430de3 2018-07-10 stsp if (pthread_mutex_unlock(a->mutex) != 0 && err == NULL)
1960 18430de3 2018-07-10 stsp err = got_error_from_errno();
1962 18430de3 2018-07-10 stsp return (void *)err;
1965 245d91c1 2018-07-12 stsp static struct got_object_id *
1966 245d91c1 2018-07-12 stsp get_selected_commit_id(struct tog_blame_line *lines,
1967 245d91c1 2018-07-12 stsp int first_displayed_line, int selected_line)
1969 245d91c1 2018-07-12 stsp struct tog_blame_line *line;
1971 245d91c1 2018-07-12 stsp line = &lines[first_displayed_line - 1 + selected_line - 1];
1972 245d91c1 2018-07-12 stsp if (!line->annotated)
1973 245d91c1 2018-07-12 stsp return NULL;
1975 245d91c1 2018-07-12 stsp return line->id;
1978 84451b3e 2018-07-10 stsp static const struct got_error *
1979 245d91c1 2018-07-12 stsp open_selected_commit(struct got_object **pobj, struct got_object **obj,
1980 b880a918 2018-07-10 stsp struct tog_blame_line *lines, int first_displayed_line,
1981 b880a918 2018-07-10 stsp int selected_line, struct got_repository *repo)
1983 b880a918 2018-07-10 stsp const struct got_error *err = NULL;
1984 b880a918 2018-07-10 stsp struct got_commit_object *commit = NULL;
1985 245d91c1 2018-07-12 stsp struct got_object_id *selected_id;
1986 b880a918 2018-07-10 stsp struct got_object_qid *pid;
1988 b880a918 2018-07-10 stsp *pobj = NULL;
1989 b880a918 2018-07-10 stsp *obj = NULL;
1991 245d91c1 2018-07-12 stsp selected_id = get_selected_commit_id(lines,
1992 245d91c1 2018-07-12 stsp first_displayed_line, selected_line);
1993 245d91c1 2018-07-12 stsp if (selected_id == NULL)
1994 b880a918 2018-07-10 stsp return NULL;
1996 245d91c1 2018-07-12 stsp err = got_object_open(obj, repo, selected_id);
1998 b880a918 2018-07-10 stsp goto done;
2000 b880a918 2018-07-10 stsp err = got_object_commit_open(&commit, repo, *obj);
2002 b880a918 2018-07-10 stsp goto done;
2004 b880a918 2018-07-10 stsp pid = SIMPLEQ_FIRST(&commit->parent_ids);
2005 b880a918 2018-07-10 stsp if (pid) {
2006 b880a918 2018-07-10 stsp err = got_object_open(pobj, repo, pid->id);
2008 b880a918 2018-07-10 stsp goto done;
2011 b880a918 2018-07-10 stsp if (commit)
2012 b880a918 2018-07-10 stsp got_object_commit_close(commit);
2013 b880a918 2018-07-10 stsp return err;
2016 b880a918 2018-07-10 stsp static const struct got_error *
2017 245d91c1 2018-07-12 stsp stop_blame(struct tog_blame *blame)
2019 a70480e0 2018-06-23 stsp const struct got_error *err = NULL;
2022 245d91c1 2018-07-12 stsp if (blame->thread) {
2023 245d91c1 2018-07-12 stsp if (pthread_join(blame->thread, (void **)&err) != 0)
2024 245d91c1 2018-07-12 stsp err = got_error_from_errno();
2025 245d91c1 2018-07-12 stsp if (err && err->code == GOT_ERR_ITER_COMPLETED)
2026 245d91c1 2018-07-12 stsp err = NULL;
2027 245d91c1 2018-07-12 stsp blame->thread = NULL;
2029 245d91c1 2018-07-12 stsp if (blame->thread_args.repo) {
2030 245d91c1 2018-07-12 stsp got_repo_close(blame->thread_args.repo);
2031 245d91c1 2018-07-12 stsp blame->thread_args.repo = NULL;
2033 245d91c1 2018-07-12 stsp if (blame->f) {
2034 245d91c1 2018-07-12 stsp fclose(blame->f);
2035 245d91c1 2018-07-12 stsp blame->f = NULL;
2037 245d91c1 2018-07-12 stsp for (i = 0; i < blame->nlines; i++)
2038 245d91c1 2018-07-12 stsp free(blame->lines[i].id);
2039 245d91c1 2018-07-12 stsp free(blame->lines);
2040 245d91c1 2018-07-12 stsp blame->lines = NULL;
2041 75c32340 2018-07-23 stsp free(blame->cb_args.commit_id);
2042 75c32340 2018-07-23 stsp blame->cb_args.commit_id = NULL;
2044 245d91c1 2018-07-12 stsp return err;
2047 245d91c1 2018-07-12 stsp static const struct got_error *
2048 7cc84d77 2018-08-01 stsp run_blame(struct tog_blame *blame, pthread_mutex_t *mutex,
2049 7cc84d77 2018-08-01 stsp struct tog_view *view, int *blame_complete,
2050 245d91c1 2018-07-12 stsp int *first_displayed_line, int *last_displayed_line,
2051 e5a0f69f 2018-08-18 stsp int *selected_line, int *done, int *eof, const char *path,
2052 245d91c1 2018-07-12 stsp struct got_object_id *commit_id,
2053 245d91c1 2018-07-12 stsp struct got_repository *repo)
2055 245d91c1 2018-07-12 stsp const struct got_error *err = NULL;
2056 84451b3e 2018-07-10 stsp struct got_blob_object *blob = NULL;
2057 245d91c1 2018-07-12 stsp struct got_repository *thread_repo = NULL;
2058 27d434c2 2018-09-15 stsp struct got_object_id *obj_id = NULL;
2059 27d434c2 2018-09-15 stsp struct got_object *obj = NULL;
2061 27d434c2 2018-09-15 stsp err = got_object_id_by_path(&obj_id, repo, commit_id, path);
2063 27d434c2 2018-09-15 stsp goto done;
2065 27d434c2 2018-09-15 stsp err = got_object_open(&obj, repo, obj_id);
2067 84451b3e 2018-07-10 stsp goto done;
2069 84451b3e 2018-07-10 stsp if (got_object_get_type(obj) != GOT_OBJ_TYPE_BLOB) {
2070 84451b3e 2018-07-10 stsp err = got_error(GOT_ERR_OBJ_TYPE);
2071 84451b3e 2018-07-10 stsp goto done;
2074 84451b3e 2018-07-10 stsp err = got_object_blob_open(&blob, repo, obj, 8192);
2076 a70480e0 2018-06-23 stsp goto done;
2077 245d91c1 2018-07-12 stsp blame->f = got_opentemp();
2078 245d91c1 2018-07-12 stsp if (blame->f == NULL) {
2079 84451b3e 2018-07-10 stsp err = got_error_from_errno();
2080 84451b3e 2018-07-10 stsp goto done;
2082 245d91c1 2018-07-12 stsp err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
2083 245d91c1 2018-07-12 stsp blame->f, blob);
2085 84451b3e 2018-07-10 stsp goto done;
2087 245d91c1 2018-07-12 stsp blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
2088 245d91c1 2018-07-12 stsp if (blame->lines == NULL) {
2089 84451b3e 2018-07-10 stsp err = got_error_from_errno();
2090 84451b3e 2018-07-10 stsp goto done;
2093 245d91c1 2018-07-12 stsp err = got_repo_open(&thread_repo, got_repo_get_path(repo));
2095 bd24772e 2018-07-11 stsp goto done;
2097 7cc84d77 2018-08-01 stsp blame->cb_args.view = view;
2098 245d91c1 2018-07-12 stsp blame->cb_args.lines = blame->lines;
2099 245d91c1 2018-07-12 stsp blame->cb_args.nlines = blame->nlines;
2100 245d91c1 2018-07-12 stsp blame->cb_args.mutex = mutex;
2101 245d91c1 2018-07-12 stsp blame->cb_args.commit_id = got_object_id_dup(commit_id);
2102 245d91c1 2018-07-12 stsp if (blame->cb_args.commit_id == NULL) {
2103 245d91c1 2018-07-12 stsp err = got_error_from_errno();
2104 245d91c1 2018-07-12 stsp goto done;
2106 245d91c1 2018-07-12 stsp blame->cb_args.f = blame->f;
2107 245d91c1 2018-07-12 stsp blame->cb_args.path = path;
2108 245d91c1 2018-07-12 stsp blame->cb_args.first_displayed_line = first_displayed_line;
2109 245d91c1 2018-07-12 stsp blame->cb_args.selected_line = selected_line;
2110 245d91c1 2018-07-12 stsp blame->cb_args.last_displayed_line = last_displayed_line;
2111 245d91c1 2018-07-12 stsp blame->cb_args.quit = done;
2112 e5a0f69f 2018-08-18 stsp blame->cb_args.eof = eof;
2114 245d91c1 2018-07-12 stsp blame->thread_args.path = path;
2115 245d91c1 2018-07-12 stsp blame->thread_args.repo = thread_repo;
2116 245d91c1 2018-07-12 stsp blame->thread_args.cb_args = &blame->cb_args;
2117 245d91c1 2018-07-12 stsp blame->thread_args.complete = blame_complete;
2118 245d91c1 2018-07-12 stsp *blame_complete = 0;
2120 245d91c1 2018-07-12 stsp if (pthread_create(&blame->thread, NULL, blame_thread,
2121 245d91c1 2018-07-12 stsp &blame->thread_args) != 0) {
2122 245d91c1 2018-07-12 stsp err = got_error_from_errno();
2123 245d91c1 2018-07-12 stsp goto done;
2128 245d91c1 2018-07-12 stsp got_object_blob_close(blob);
2129 27d434c2 2018-09-15 stsp free(obj_id);
2131 245d91c1 2018-07-12 stsp got_object_close(obj);
2133 245d91c1 2018-07-12 stsp stop_blame(blame);
2134 245d91c1 2018-07-12 stsp return err;
2137 245d91c1 2018-07-12 stsp static const struct got_error *
2138 e5a0f69f 2018-08-18 stsp open_blame_view(struct tog_view *view, char *path,
2139 e1cd8fed 2018-08-01 stsp struct got_object_id *commit_id, struct got_repository *repo)
2141 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL;
2142 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
2144 fb2756b9 2018-08-04 stsp SIMPLEQ_INIT(&s->blamed_commits);
2146 fb2756b9 2018-08-04 stsp if (pthread_mutex_init(&s->mutex, NULL) != 0)
2147 7cbe629d 2018-08-04 stsp return got_error_from_errno();
2149 fb2756b9 2018-08-04 stsp err = got_object_qid_alloc(&s->blamed_commit, commit_id);
2151 7cbe629d 2018-08-04 stsp return err;
2153 fb2756b9 2018-08-04 stsp SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
2154 fb2756b9 2018-08-04 stsp s->first_displayed_line = 1;
2155 fb2756b9 2018-08-04 stsp s->last_displayed_line = view->nlines;
2156 fb2756b9 2018-08-04 stsp s->selected_line = 1;
2157 fb2756b9 2018-08-04 stsp s->blame_complete = 0;
2158 fb2756b9 2018-08-04 stsp s->path = path;
2159 e5a0f69f 2018-08-18 stsp if (s->path == NULL)
2160 e5a0f69f 2018-08-18 stsp return got_error_from_errno();
2161 fb2756b9 2018-08-04 stsp s->repo = repo;
2162 fb2756b9 2018-08-04 stsp s->commit_id = commit_id;
2163 e9424729 2018-08-04 stsp memset(&s->blame, 0, sizeof(s->blame));
2165 e5a0f69f 2018-08-18 stsp view->show = show_blame_view;
2166 e5a0f69f 2018-08-18 stsp view->input = input_blame_view;
2167 e5a0f69f 2018-08-18 stsp view->close = close_blame_view;
2169 e5a0f69f 2018-08-18 stsp return run_blame(&s->blame, &s->mutex, view, &s->blame_complete,
2170 e5a0f69f 2018-08-18 stsp &s->first_displayed_line, &s->last_displayed_line,
2171 e5a0f69f 2018-08-18 stsp &s->selected_line, &s->done, &s->eof, s->path,
2172 e5a0f69f 2018-08-18 stsp s->blamed_commit->id, s->repo);
2175 e5a0f69f 2018-08-18 stsp static const struct got_error *
2176 7cbe629d 2018-08-04 stsp close_blame_view(struct tog_view *view)
2178 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
2179 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
2181 e5a0f69f 2018-08-18 stsp if (s->blame.thread)
2182 e5a0f69f 2018-08-18 stsp err = stop_blame(&s->blame);
2184 fb2756b9 2018-08-04 stsp while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
2185 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
2186 fb2756b9 2018-08-04 stsp blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
2187 fb2756b9 2018-08-04 stsp SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
2188 7cbe629d 2018-08-04 stsp got_object_qid_free(blamed_commit);
2191 e5a0f69f 2018-08-18 stsp free(s->path);
2193 e5a0f69f 2018-08-18 stsp return err;
2196 7cbe629d 2018-08-04 stsp static const struct got_error *
2197 7cbe629d 2018-08-04 stsp show_blame_view(struct tog_view *view)
2199 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
2200 e5a0f69f 2018-08-18 stsp struct tog_blame_view_state *s = &view->state.blame;
2202 e5a0f69f 2018-08-18 stsp if (pthread_mutex_lock(&s->mutex) != 0)
2203 e5a0f69f 2018-08-18 stsp return got_error_from_errno();
2205 e5a0f69f 2018-08-18 stsp err = draw_blame(view, s->blamed_commit->id, s->blame.f,
2206 e5a0f69f 2018-08-18 stsp s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
2207 e5a0f69f 2018-08-18 stsp s->selected_line, &s->first_displayed_line,
2208 e5a0f69f 2018-08-18 stsp &s->last_displayed_line, &s->eof, view->nlines);
2210 e5a0f69f 2018-08-18 stsp if (pthread_mutex_unlock(&s->mutex) != 0 && err == NULL)
2211 e5a0f69f 2018-08-18 stsp err = got_error_from_errno();
2213 e5a0f69f 2018-08-18 stsp return err;
2216 e5a0f69f 2018-08-18 stsp static const struct got_error *
2217 e5a0f69f 2018-08-18 stsp input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
2218 e5a0f69f 2018-08-18 stsp struct tog_view *view, int ch)
2220 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL, *thread_err = NULL;
2221 7cbe629d 2018-08-04 stsp struct got_object *obj = NULL, *pobj = NULL;
2222 7cbe629d 2018-08-04 stsp struct tog_view *diff_view;
2223 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
2225 e5a0f69f 2018-08-18 stsp if (pthread_mutex_lock(&s->mutex) != 0) {
2226 e5a0f69f 2018-08-18 stsp err = got_error_from_errno();
2227 e5a0f69f 2018-08-18 stsp goto done;
2230 e5a0f69f 2018-08-18 stsp switch (ch) {
2232 e5a0f69f 2018-08-18 stsp s->done = 1;
2233 e5a0f69f 2018-08-18 stsp if (pthread_mutex_unlock(&s->mutex) != 0) {
2234 e5a0f69f 2018-08-18 stsp err = got_error_from_errno();
2235 e5a0f69f 2018-08-18 stsp goto done;
2237 e5a0f69f 2018-08-18 stsp return stop_blame(&s->blame);
2239 e5a0f69f 2018-08-18 stsp case KEY_UP:
2240 e5a0f69f 2018-08-18 stsp if (s->selected_line > 1)
2241 e5a0f69f 2018-08-18 stsp s->selected_line--;
2242 e5a0f69f 2018-08-18 stsp else if (s->selected_line == 1 &&
2243 e5a0f69f 2018-08-18 stsp s->first_displayed_line > 1)
2244 e5a0f69f 2018-08-18 stsp s->first_displayed_line--;
2246 e5a0f69f 2018-08-18 stsp case KEY_PPAGE:
2247 e5a0f69f 2018-08-18 stsp if (s->first_displayed_line == 1) {
2248 e5a0f69f 2018-08-18 stsp s->selected_line = 1;
2251 e5a0f69f 2018-08-18 stsp if (s->first_displayed_line > view->nlines - 2)
2252 e5a0f69f 2018-08-18 stsp s->first_displayed_line -=
2253 e5a0f69f 2018-08-18 stsp (view->nlines - 2);
2255 e5a0f69f 2018-08-18 stsp s->first_displayed_line = 1;
2258 e5a0f69f 2018-08-18 stsp case KEY_DOWN:
2259 e5a0f69f 2018-08-18 stsp if (s->selected_line < view->nlines - 2 &&
2260 e5a0f69f 2018-08-18 stsp s->first_displayed_line +
2261 e5a0f69f 2018-08-18 stsp s->selected_line <= s->blame.nlines)
2262 e5a0f69f 2018-08-18 stsp s->selected_line++;
2263 e5a0f69f 2018-08-18 stsp else if (s->last_displayed_line <
2264 e5a0f69f 2018-08-18 stsp s->blame.nlines)
2265 e5a0f69f 2018-08-18 stsp s->first_displayed_line++;
2268 e5a0f69f 2018-08-18 stsp case 'p': {
2269 e5a0f69f 2018-08-18 stsp struct got_object_id *id;
2270 e5a0f69f 2018-08-18 stsp id = get_selected_commit_id(s->blame.lines,
2271 e5a0f69f 2018-08-18 stsp s->first_displayed_line, s->selected_line);
2272 e5a0f69f 2018-08-18 stsp if (id == NULL || got_object_id_cmp(id,
2273 e5a0f69f 2018-08-18 stsp s->blamed_commit->id) == 0)
2275 e5a0f69f 2018-08-18 stsp err = open_selected_commit(&pobj, &obj,
2276 e5a0f69f 2018-08-18 stsp s->blame.lines, s->first_displayed_line,
2277 e5a0f69f 2018-08-18 stsp s->selected_line, s->repo);
2280 e5a0f69f 2018-08-18 stsp if (pobj == NULL && obj == NULL)
2282 e5a0f69f 2018-08-18 stsp if (ch == 'p' && pobj == NULL)
2284 e5a0f69f 2018-08-18 stsp s->done = 1;
2285 e5a0f69f 2018-08-18 stsp if (pthread_mutex_unlock(&s->mutex) != 0) {
2286 e5a0f69f 2018-08-18 stsp err = got_error_from_errno();
2287 e5a0f69f 2018-08-18 stsp goto done;
2289 e5a0f69f 2018-08-18 stsp thread_err = stop_blame(&s->blame);
2290 e5a0f69f 2018-08-18 stsp s->done = 0;
2291 e5a0f69f 2018-08-18 stsp if (pthread_mutex_lock(&s->mutex) != 0) {
2292 e5a0f69f 2018-08-18 stsp err = got_error_from_errno();
2293 e5a0f69f 2018-08-18 stsp goto done;
2295 e5a0f69f 2018-08-18 stsp if (thread_err)
2297 e5a0f69f 2018-08-18 stsp id = got_object_get_id(ch == 'b' ? obj : pobj);
2298 e5a0f69f 2018-08-18 stsp got_object_close(obj);
2299 e5a0f69f 2018-08-18 stsp obj = NULL;
2300 e5a0f69f 2018-08-18 stsp if (pobj) {
2301 e5a0f69f 2018-08-18 stsp got_object_close(pobj);
2302 e5a0f69f 2018-08-18 stsp pobj = NULL;
2304 6402fb3c 2018-09-15 stsp err = got_object_qid_alloc(&s->blamed_commit, id);
2306 e5a0f69f 2018-08-18 stsp goto done;
2307 e5a0f69f 2018-08-18 stsp SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
2308 e5a0f69f 2018-08-18 stsp s->blamed_commit, entry);
2309 e5a0f69f 2018-08-18 stsp err = run_blame(&s->blame, &s->mutex, view,
2310 e5a0f69f 2018-08-18 stsp &s->blame_complete,
2311 e5a0f69f 2018-08-18 stsp &s->first_displayed_line,
2312 e5a0f69f 2018-08-18 stsp &s->last_displayed_line,
2313 e5a0f69f 2018-08-18 stsp &s->selected_line, &s->done, &s->eof,
2314 e5a0f69f 2018-08-18 stsp s->path, s->blamed_commit->id, s->repo);
2319 e5a0f69f 2018-08-18 stsp case 'B': {
2320 e5a0f69f 2018-08-18 stsp struct got_object_qid *first;
2321 e5a0f69f 2018-08-18 stsp first = SIMPLEQ_FIRST(&s->blamed_commits);
2322 e5a0f69f 2018-08-18 stsp if (!got_object_id_cmp(first->id, s->commit_id))
2324 e5a0f69f 2018-08-18 stsp s->done = 1;
2325 e5a0f69f 2018-08-18 stsp if (pthread_mutex_unlock(&s->mutex) != 0) {
2326 e5a0f69f 2018-08-18 stsp err = got_error_from_errno();
2327 e5a0f69f 2018-08-18 stsp goto done;
2329 e5a0f69f 2018-08-18 stsp thread_err = stop_blame(&s->blame);
2330 e5a0f69f 2018-08-18 stsp s->done = 0;
2331 e5a0f69f 2018-08-18 stsp if (pthread_mutex_lock(&s->mutex) != 0) {
2332 e5a0f69f 2018-08-18 stsp err = got_error_from_errno();
2333 e5a0f69f 2018-08-18 stsp goto done;
2335 e5a0f69f 2018-08-18 stsp if (thread_err)
2337 e5a0f69f 2018-08-18 stsp SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
2338 e5a0f69f 2018-08-18 stsp got_object_qid_free(s->blamed_commit);
2339 e5a0f69f 2018-08-18 stsp s->blamed_commit =
2340 e5a0f69f 2018-08-18 stsp SIMPLEQ_FIRST(&s->blamed_commits);
2341 e5a0f69f 2018-08-18 stsp err = run_blame(&s->blame, &s->mutex, view,
2342 e5a0f69f 2018-08-18 stsp &s->blame_complete,
2343 e5a0f69f 2018-08-18 stsp &s->first_displayed_line,
2344 e5a0f69f 2018-08-18 stsp &s->last_displayed_line,
2345 e5a0f69f 2018-08-18 stsp &s->selected_line, &s->done, &s->eof, s->path,
2346 e5a0f69f 2018-08-18 stsp s->blamed_commit->id, s->repo);
2351 e5a0f69f 2018-08-18 stsp case KEY_ENTER:
2352 e5a0f69f 2018-08-18 stsp case '\r':
2353 e5a0f69f 2018-08-18 stsp err = open_selected_commit(&pobj, &obj,
2354 e5a0f69f 2018-08-18 stsp s->blame.lines, s->first_displayed_line,
2355 e5a0f69f 2018-08-18 stsp s->selected_line, s->repo);
2358 e5a0f69f 2018-08-18 stsp if (pobj == NULL && obj == NULL)
2360 e5a0f69f 2018-08-18 stsp diff_view = view_open(0, 0, 0, 0, view,
2361 e5a0f69f 2018-08-18 stsp TOG_VIEW_DIFF);
2362 e5a0f69f 2018-08-18 stsp if (diff_view == NULL) {
2363 e5a0f69f 2018-08-18 stsp err = got_error_from_errno();
2366 e5a0f69f 2018-08-18 stsp err = open_diff_view(diff_view, pobj, obj,
2368 e5a0f69f 2018-08-18 stsp if (err) {
2369 e5a0f69f 2018-08-18 stsp view_close(diff_view);
2372 e5a0f69f 2018-08-18 stsp *new_view = diff_view;
2373 e5a0f69f 2018-08-18 stsp if (pobj) {
2374 e5a0f69f 2018-08-18 stsp got_object_close(pobj);
2375 e5a0f69f 2018-08-18 stsp pobj = NULL;
2377 e5a0f69f 2018-08-18 stsp got_object_close(obj);
2378 e5a0f69f 2018-08-18 stsp obj = NULL;
2382 e5a0f69f 2018-08-18 stsp case KEY_NPAGE:
2384 e5a0f69f 2018-08-18 stsp if (s->last_displayed_line >= s->blame.nlines &&
2385 e5a0f69f 2018-08-18 stsp s->selected_line < view->nlines - 2) {
2386 e5a0f69f 2018-08-18 stsp s->selected_line = MIN(s->blame.nlines,
2387 e5a0f69f 2018-08-18 stsp view->nlines - 2);
2390 e5a0f69f 2018-08-18 stsp if (s->last_displayed_line + view->nlines - 2
2391 e5a0f69f 2018-08-18 stsp <= s->blame.nlines)
2392 e5a0f69f 2018-08-18 stsp s->first_displayed_line +=
2393 e5a0f69f 2018-08-18 stsp view->nlines - 2;
2395 e5a0f69f 2018-08-18 stsp s->first_displayed_line =
2396 e5a0f69f 2018-08-18 stsp s->blame.nlines -
2397 e5a0f69f 2018-08-18 stsp (view->nlines - 3);
2399 e5a0f69f 2018-08-18 stsp case KEY_RESIZE:
2400 e5a0f69f 2018-08-18 stsp if (s->selected_line > view->nlines - 2) {
2401 e5a0f69f 2018-08-18 stsp s->selected_line = MIN(s->blame.nlines,
2402 e5a0f69f 2018-08-18 stsp view->nlines - 2);
2409 e5a0f69f 2018-08-18 stsp if (pthread_mutex_unlock(&s->mutex) != 0)
2410 e5a0f69f 2018-08-18 stsp err = got_error_from_errno();
2413 b880a918 2018-07-10 stsp got_object_close(pobj);
2414 245d91c1 2018-07-12 stsp return thread_err ? thread_err : err;
2417 a70480e0 2018-06-23 stsp static const struct got_error *
2418 9f7d7167 2018-04-29 stsp cmd_blame(int argc, char *argv[])
2420 a70480e0 2018-06-23 stsp const struct got_error *error;
2421 a70480e0 2018-06-23 stsp struct got_repository *repo = NULL;
2422 69069811 2018-08-02 stsp char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2423 a70480e0 2018-06-23 stsp struct got_object_id *commit_id = NULL;
2424 a70480e0 2018-06-23 stsp char *commit_id_str = NULL;
2426 e1cd8fed 2018-08-01 stsp struct tog_view *view;
2428 a70480e0 2018-06-23 stsp #ifndef PROFILE
2429 ad242220 2018-09-08 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd", NULL)
2431 a70480e0 2018-06-23 stsp err(1, "pledge");
2434 69069811 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2435 a70480e0 2018-06-23 stsp switch (ch) {
2437 a70480e0 2018-06-23 stsp commit_id_str = optarg;
2440 69069811 2018-08-02 stsp repo_path = realpath(optarg, NULL);
2441 69069811 2018-08-02 stsp if (repo_path == NULL)
2442 69069811 2018-08-02 stsp err(1, "-r option");
2446 a70480e0 2018-06-23 stsp /* NOTREACHED */
2450 a70480e0 2018-06-23 stsp argc -= optind;
2451 a70480e0 2018-06-23 stsp argv += optind;
2453 69069811 2018-08-02 stsp if (argc == 1)
2454 69069811 2018-08-02 stsp path = argv[0];
2456 a70480e0 2018-06-23 stsp usage_blame();
2458 69069811 2018-08-02 stsp cwd = getcwd(NULL, 0);
2459 69069811 2018-08-02 stsp if (cwd == NULL) {
2460 69069811 2018-08-02 stsp error = got_error_from_errno();
2461 69069811 2018-08-02 stsp goto done;
2463 69069811 2018-08-02 stsp if (repo_path == NULL) {
2464 69069811 2018-08-02 stsp repo_path = strdup(cwd);
2465 69069811 2018-08-02 stsp if (repo_path == NULL) {
2466 69069811 2018-08-02 stsp error = got_error_from_errno();
2467 69069811 2018-08-02 stsp goto done;
2472 69069811 2018-08-02 stsp error = got_repo_open(&repo, repo_path);
2473 a70480e0 2018-06-23 stsp if (error != NULL)
2474 66b4983c 2018-06-23 stsp return error;
2476 69069811 2018-08-02 stsp error = got_repo_map_path(&in_repo_path, repo, path);
2477 69069811 2018-08-02 stsp if (error != NULL)
2478 69069811 2018-08-02 stsp goto done;
2480 a70480e0 2018-06-23 stsp if (commit_id_str == NULL) {
2481 a70480e0 2018-06-23 stsp struct got_reference *head_ref;
2482 a70480e0 2018-06-23 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
2483 a70480e0 2018-06-23 stsp if (error != NULL)
2484 66b4983c 2018-06-23 stsp goto done;
2485 a70480e0 2018-06-23 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
2486 a70480e0 2018-06-23 stsp got_ref_close(head_ref);
2488 a70480e0 2018-06-23 stsp struct got_object *obj;
2489 a70480e0 2018-06-23 stsp error = got_object_open_by_id_str(&obj, repo, commit_id_str);
2490 a70480e0 2018-06-23 stsp if (error != NULL)
2491 66b4983c 2018-06-23 stsp goto done;
2492 6402fb3c 2018-09-15 stsp commit_id = got_object_id_dup(got_object_get_id(obj));
2493 a19e88aa 2018-06-23 stsp if (commit_id == NULL)
2494 66b4983c 2018-06-23 stsp error = got_error_from_errno();
2495 a19e88aa 2018-06-23 stsp got_object_close(obj);
2497 a19e88aa 2018-06-23 stsp if (error != NULL)
2498 a19e88aa 2018-06-23 stsp goto done;
2500 b3665f43 2018-08-04 stsp view = view_open(0, 0, 0, 0, NULL, TOG_VIEW_BLAME);
2501 e1cd8fed 2018-08-01 stsp if (view == NULL) {
2502 e1cd8fed 2018-08-01 stsp error = got_error_from_errno();
2503 e1cd8fed 2018-08-01 stsp goto done;
2505 7cbe629d 2018-08-04 stsp error = open_blame_view(view, in_repo_path, commit_id, repo);
2506 7cbe629d 2018-08-04 stsp if (error)
2507 7cbe629d 2018-08-04 stsp goto done;
2508 e5a0f69f 2018-08-18 stsp error = view_loop(view);
2510 69069811 2018-08-02 stsp free(repo_path);
2511 69069811 2018-08-02 stsp free(cwd);
2512 a70480e0 2018-06-23 stsp free(commit_id);
2514 a70480e0 2018-06-23 stsp got_repo_close(repo);
2515 a70480e0 2018-06-23 stsp return error;
2518 ffd1d5e5 2018-06-23 stsp static const struct got_error *
2519 f7d12f7e 2018-08-01 stsp draw_tree_entries(struct tog_view *view,
2520 f7d12f7e 2018-08-01 stsp struct got_tree_entry **first_displayed_entry,
2521 ffd1d5e5 2018-06-23 stsp struct got_tree_entry **last_displayed_entry,
2522 ffd1d5e5 2018-06-23 stsp struct got_tree_entry **selected_entry, int *ndisplayed,
2523 f7d12f7e 2018-08-01 stsp const char *label, int show_ids, const char *parent_path,
2524 ce52c690 2018-06-23 stsp const struct got_tree_entries *entries, int selected, int limit, int isroot)
2526 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
2527 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *te;
2528 ffd1d5e5 2018-06-23 stsp wchar_t *wline;
2529 ffd1d5e5 2018-06-23 stsp int width, n;
2531 ffd1d5e5 2018-06-23 stsp *ndisplayed = 0;
2533 f7d12f7e 2018-08-01 stsp werase(view->window);
2535 ffd1d5e5 2018-06-23 stsp if (limit == 0)
2536 ffd1d5e5 2018-06-23 stsp return NULL;
2538 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, label, view->ncols);
2540 ffd1d5e5 2018-06-23 stsp return err;
2541 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2542 a3404814 2018-09-02 stsp wstandout(view->window);
2543 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
2544 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2545 a3404814 2018-09-02 stsp wstandend(view->window);
2546 2550e4c3 2018-07-13 stsp free(wline);
2547 2550e4c3 2018-07-13 stsp wline = NULL;
2548 f7d12f7e 2018-08-01 stsp if (width < view->ncols)
2549 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
2550 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
2551 ffd1d5e5 2018-06-23 stsp return NULL;
2552 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, parent_path, view->ncols);
2554 ce52c690 2018-06-23 stsp return err;
2555 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
2556 2550e4c3 2018-07-13 stsp free(wline);
2557 2550e4c3 2018-07-13 stsp wline = NULL;
2558 f7d12f7e 2018-08-01 stsp if (width < view->ncols)
2559 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
2560 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
2561 ffd1d5e5 2018-06-23 stsp return NULL;
2562 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
2563 a1eca9bb 2018-06-23 stsp if (--limit <= 0)
2564 a1eca9bb 2018-06-23 stsp return NULL;
2566 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_FIRST(&entries->head);
2567 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry == NULL) {
2568 ffd1d5e5 2018-06-23 stsp if (selected == 0) {
2569 f7d12f7e 2018-08-01 stsp wstandout(view->window);
2570 ffd1d5e5 2018-06-23 stsp *selected_entry = NULL;
2572 f7d12f7e 2018-08-01 stsp waddstr(view->window, " ..\n"); /* parent directory */
2573 ffd1d5e5 2018-06-23 stsp if (selected == 0)
2574 f7d12f7e 2018-08-01 stsp wstandend(view->window);
2575 ffd1d5e5 2018-06-23 stsp (*ndisplayed)++;
2576 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
2577 ffd1d5e5 2018-06-23 stsp return NULL;
2581 ffd1d5e5 2018-06-23 stsp while (te != *first_displayed_entry)
2582 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_NEXT(te, entry);
2585 ffd1d5e5 2018-06-23 stsp while (te) {
2586 1d13200f 2018-07-12 stsp char *line = NULL, *id_str = NULL;
2588 1d13200f 2018-07-12 stsp if (show_ids) {
2589 1d13200f 2018-07-12 stsp err = got_object_id_str(&id_str, te->id);
2591 1d13200f 2018-07-12 stsp return got_error_from_errno();
2593 1d13200f 2018-07-12 stsp if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
2594 1d13200f 2018-07-12 stsp te->name, S_ISDIR(te->mode) ? "/" : "") == -1) {
2595 1d13200f 2018-07-12 stsp free(id_str);
2596 ffd1d5e5 2018-06-23 stsp return got_error_from_errno();
2598 1d13200f 2018-07-12 stsp free(id_str);
2599 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, line, view->ncols);
2600 ffd1d5e5 2018-06-23 stsp if (err) {
2601 ffd1d5e5 2018-06-23 stsp free(line);
2604 ffd1d5e5 2018-06-23 stsp if (n == selected) {
2605 f7d12f7e 2018-08-01 stsp wstandout(view->window);
2606 ffd1d5e5 2018-06-23 stsp *selected_entry = te;
2608 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
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 (n == selected)
2612 f7d12f7e 2018-08-01 stsp wstandend(view->window);
2613 ffd1d5e5 2018-06-23 stsp free(line);
2614 2550e4c3 2018-07-13 stsp free(wline);
2615 2550e4c3 2018-07-13 stsp wline = NULL;
2617 ffd1d5e5 2018-06-23 stsp (*ndisplayed)++;
2618 ffd1d5e5 2018-06-23 stsp *last_displayed_entry = te;
2619 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
2621 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_NEXT(te, entry);
2624 1a57306a 2018-09-02 stsp view_vborder(view);
2625 ffd1d5e5 2018-06-23 stsp return err;
2628 ffd1d5e5 2018-06-23 stsp static void
2629 ffd1d5e5 2018-06-23 stsp tree_scroll_up(struct got_tree_entry **first_displayed_entry, int maxscroll,
2630 ffd1d5e5 2018-06-23 stsp const struct got_tree_entries *entries, int isroot)
2632 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *te, *prev;
2635 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry == NULL)
2638 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_FIRST(&entries->head);
2639 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry == te) {
2640 ffd1d5e5 2018-06-23 stsp if (!isroot)
2641 ffd1d5e5 2018-06-23 stsp *first_displayed_entry = NULL;
2645 ffd1d5e5 2018-06-23 stsp /* XXX this is stupid... switch to TAILQ? */
2646 ffd1d5e5 2018-06-23 stsp for (i = 0; i < maxscroll; i++) {
2647 ffd1d5e5 2018-06-23 stsp while (te != *first_displayed_entry) {
2648 ffd1d5e5 2018-06-23 stsp prev = te;
2649 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_NEXT(te, entry);
2651 ffd1d5e5 2018-06-23 stsp *first_displayed_entry = prev;
2652 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_FIRST(&entries->head);
2654 ffd1d5e5 2018-06-23 stsp if (!isroot && te == SIMPLEQ_FIRST(&entries->head) && i < maxscroll)
2655 ffd1d5e5 2018-06-23 stsp *first_displayed_entry = NULL;
2658 ffd1d5e5 2018-06-23 stsp static void
2659 ffd1d5e5 2018-06-23 stsp tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
2660 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *last_displayed_entry,
2661 ffd1d5e5 2018-06-23 stsp const struct got_tree_entries *entries)
2663 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *next;
2664 ffd1d5e5 2018-06-23 stsp int n = 0;
2666 ffd1d5e5 2018-06-23 stsp if (SIMPLEQ_NEXT(last_displayed_entry, entry) == NULL)
2669 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry)
2670 ffd1d5e5 2018-06-23 stsp next = SIMPLEQ_NEXT(*first_displayed_entry, entry);
2672 ffd1d5e5 2018-06-23 stsp next = SIMPLEQ_FIRST(&entries->head);
2673 ffd1d5e5 2018-06-23 stsp while (next) {
2674 ffd1d5e5 2018-06-23 stsp *first_displayed_entry = next;
2675 ffd1d5e5 2018-06-23 stsp if (++n >= maxscroll)
2677 ffd1d5e5 2018-06-23 stsp next = SIMPLEQ_NEXT(next, entry);
2681 ffd1d5e5 2018-06-23 stsp static const struct got_error *
2682 ce52c690 2018-06-23 stsp tree_entry_path(char **path, struct tog_parent_trees *parents,
2683 ce52c690 2018-06-23 stsp struct got_tree_entry *te)
2685 cb2ebc8a 2018-06-23 stsp const struct got_error *err = NULL;
2686 ffd1d5e5 2018-06-23 stsp struct tog_parent_tree *pt;
2687 ffd1d5e5 2018-06-23 stsp size_t len = 2; /* for leading slash and NUL */
2689 d9765a41 2018-06-23 stsp TAILQ_FOREACH(pt, parents, entry)
2690 ffd1d5e5 2018-06-23 stsp len += strlen(pt->selected_entry->name) + 1 /* slash */;
2692 ce52c690 2018-06-23 stsp len += strlen(te->name);
2694 ce52c690 2018-06-23 stsp *path = calloc(1, len);
2695 ffd1d5e5 2018-06-23 stsp if (path == NULL)
2696 ffd1d5e5 2018-06-23 stsp return got_error_from_errno();
2698 ce52c690 2018-06-23 stsp (*path)[0] = '/';
2699 d9765a41 2018-06-23 stsp pt = TAILQ_LAST(parents, tog_parent_trees);
2700 d9765a41 2018-06-23 stsp while (pt) {
2701 ce52c690 2018-06-23 stsp if (strlcat(*path, pt->selected_entry->name, len) >= len) {
2702 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
2703 cb2ebc8a 2018-06-23 stsp goto done;
2705 ce52c690 2018-06-23 stsp if (strlcat(*path, "/", len) >= len) {
2706 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
2707 cb2ebc8a 2018-06-23 stsp goto done;
2709 d9765a41 2018-06-23 stsp pt = TAILQ_PREV(pt, tog_parent_trees, entry);
2712 ce52c690 2018-06-23 stsp if (strlcat(*path, te->name, len) >= len) {
2713 ce52c690 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
2714 ce52c690 2018-06-23 stsp goto done;
2718 ce52c690 2018-06-23 stsp if (err) {
2719 ce52c690 2018-06-23 stsp free(*path);
2720 ce52c690 2018-06-23 stsp *path = NULL;
2722 ce52c690 2018-06-23 stsp return err;
2725 ce52c690 2018-06-23 stsp static const struct got_error *
2726 e5a0f69f 2018-08-18 stsp blame_tree_entry(struct tog_view **new_view, struct tog_view *parent_view,
2727 e5a0f69f 2018-08-18 stsp struct got_tree_entry *te, struct tog_parent_trees *parents,
2728 e5a0f69f 2018-08-18 stsp struct got_object_id *commit_id, struct got_repository *repo)
2730 ce52c690 2018-06-23 stsp const struct got_error *err = NULL;
2731 ce52c690 2018-06-23 stsp char *path;
2732 e5a0f69f 2018-08-18 stsp struct tog_view *blame_view;
2734 ce52c690 2018-06-23 stsp err = tree_entry_path(&path, parents, te);
2736 ce52c690 2018-06-23 stsp return err;
2738 e5a0f69f 2018-08-18 stsp blame_view = view_open(0, 0, 0, 0, parent_view, TOG_VIEW_BLAME);
2739 e5a0f69f 2018-08-18 stsp if (blame_view == NULL)
2740 e5a0f69f 2018-08-18 stsp return got_error_from_errno();
2742 e5a0f69f 2018-08-18 stsp err = open_blame_view(blame_view, path, commit_id, repo);
2743 e5a0f69f 2018-08-18 stsp if (err) {
2744 e5a0f69f 2018-08-18 stsp view_close(blame_view);
2745 e5a0f69f 2018-08-18 stsp free(path);
2747 e5a0f69f 2018-08-18 stsp *new_view = blame_view;
2748 69efd4c4 2018-07-18 stsp return err;
2751 69efd4c4 2018-07-18 stsp static const struct got_error *
2752 e5a0f69f 2018-08-18 stsp log_tree_entry(struct tog_view **new_view, struct tog_view *parent_view,
2753 e5a0f69f 2018-08-18 stsp struct got_tree_entry *te, struct tog_parent_trees *parents,
2754 e5a0f69f 2018-08-18 stsp struct got_object_id *commit_id, struct got_repository *repo)
2756 e5a0f69f 2018-08-18 stsp struct tog_view *log_view;
2757 69efd4c4 2018-07-18 stsp const struct got_error *err = NULL;
2758 69efd4c4 2018-07-18 stsp char *path;
2760 e5a0f69f 2018-08-18 stsp log_view = view_open(0, 0, 0, 0, parent_view, TOG_VIEW_LOG);
2761 e5a0f69f 2018-08-18 stsp if (log_view == NULL)
2762 e5a0f69f 2018-08-18 stsp return got_error_from_errno();
2764 69efd4c4 2018-07-18 stsp err = tree_entry_path(&path, parents, te);
2766 69efd4c4 2018-07-18 stsp return err;
2768 e5a0f69f 2018-08-18 stsp err = open_log_view(log_view, commit_id, repo, path);
2770 e5a0f69f 2018-08-18 stsp view_close(log_view);
2772 e5a0f69f 2018-08-18 stsp *new_view = log_view;
2773 cb2ebc8a 2018-06-23 stsp free(path);
2774 cb2ebc8a 2018-06-23 stsp return err;
2777 ffd1d5e5 2018-06-23 stsp static const struct got_error *
2778 ad80ab7b 2018-08-04 stsp open_tree_view(struct tog_view *view, struct got_tree_object *root,
2779 5221c383 2018-08-01 stsp struct got_object_id *commit_id, struct got_repository *repo)
2781 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
2782 ad80ab7b 2018-08-04 stsp char *commit_id_str = NULL;
2783 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
2785 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->parents);
2787 ffd1d5e5 2018-06-23 stsp err = got_object_id_str(&commit_id_str, commit_id);
2788 ffd1d5e5 2018-06-23 stsp if (err != NULL)
2789 ffd1d5e5 2018-06-23 stsp goto done;
2791 fb2756b9 2018-08-04 stsp if (asprintf(&s->tree_label, "commit: %s", commit_id_str) == -1) {
2792 ffd1d5e5 2018-06-23 stsp err = got_error_from_errno();
2793 ffd1d5e5 2018-06-23 stsp goto done;
2796 fb2756b9 2018-08-04 stsp s->root = s->tree = root;
2797 fb2756b9 2018-08-04 stsp s->entries = got_object_tree_get_entries(root);
2798 fb2756b9 2018-08-04 stsp s->first_displayed_entry = SIMPLEQ_FIRST(&s->entries->head);
2799 6484ec90 2018-09-29 stsp s->commit_id = got_object_id_dup(commit_id);
2800 6484ec90 2018-09-29 stsp if (s->commit_id == NULL) {
2801 6484ec90 2018-09-29 stsp err = got_error_from_errno();
2802 6484ec90 2018-09-29 stsp goto done;
2804 fb2756b9 2018-08-04 stsp s->repo = repo;
2806 e5a0f69f 2018-08-18 stsp view->show = show_tree_view;
2807 e5a0f69f 2018-08-18 stsp view->input = input_tree_view;
2808 e5a0f69f 2018-08-18 stsp view->close = close_tree_view;
2810 ad80ab7b 2018-08-04 stsp free(commit_id_str);
2811 6484ec90 2018-09-29 stsp if (err) {
2812 fb2756b9 2018-08-04 stsp free(s->tree_label);
2813 6484ec90 2018-09-29 stsp s->tree_label = NULL;
2815 ad80ab7b 2018-08-04 stsp return err;
2818 e5a0f69f 2018-08-18 stsp static const struct got_error *
2819 ad80ab7b 2018-08-04 stsp close_tree_view(struct tog_view *view)
2821 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
2823 fb2756b9 2018-08-04 stsp free(s->tree_label);
2824 6484ec90 2018-09-29 stsp s->tree_label = NULL;
2825 6484ec90 2018-09-29 stsp free(s->commit_id);
2826 6484ec90 2018-09-29 stsp s->commit_id = NULL;
2827 fb2756b9 2018-08-04 stsp while (!TAILQ_EMPTY(&s->parents)) {
2828 ad80ab7b 2018-08-04 stsp struct tog_parent_tree *parent;
2829 fb2756b9 2018-08-04 stsp parent = TAILQ_FIRST(&s->parents);
2830 fb2756b9 2018-08-04 stsp TAILQ_REMOVE(&s->parents, parent, entry);
2831 ad80ab7b 2018-08-04 stsp free(parent);
2834 fb2756b9 2018-08-04 stsp if (s->tree != s->root)
2835 fb2756b9 2018-08-04 stsp got_object_tree_close(s->tree);
2836 e5a0f69f 2018-08-18 stsp got_object_tree_close(s->root);
2838 e5a0f69f 2018-08-18 stsp return NULL;
2841 ad80ab7b 2018-08-04 stsp static const struct got_error *
2842 ad80ab7b 2018-08-04 stsp show_tree_view(struct tog_view *view)
2844 ad80ab7b 2018-08-04 stsp const struct got_error *err = NULL;
2845 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
2846 e5a0f69f 2018-08-18 stsp char *parent_path;
2848 e5a0f69f 2018-08-18 stsp err = tree_entry_path(&parent_path, &s->parents, NULL);
2850 e5a0f69f 2018-08-18 stsp return err;
2852 e5a0f69f 2018-08-18 stsp err = draw_tree_entries(view, &s->first_displayed_entry,
2853 e5a0f69f 2018-08-18 stsp &s->last_displayed_entry, &s->selected_entry,
2854 e5a0f69f 2018-08-18 stsp &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
2855 e5a0f69f 2018-08-18 stsp s->entries, s->selected, view->nlines, s->tree == s->root);
2856 e5a0f69f 2018-08-18 stsp free(parent_path);
2857 e5a0f69f 2018-08-18 stsp return err;
2860 e5a0f69f 2018-08-18 stsp static const struct got_error *
2861 e5a0f69f 2018-08-18 stsp input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
2862 e5a0f69f 2018-08-18 stsp struct tog_view *view, int ch)
2864 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
2865 e5a0f69f 2018-08-18 stsp struct tog_tree_view_state *s = &view->state.tree;
2867 e5a0f69f 2018-08-18 stsp switch (ch) {
2869 e5a0f69f 2018-08-18 stsp s->show_ids = !s->show_ids;
2872 e5a0f69f 2018-08-18 stsp if (s->selected_entry) {
2873 e5a0f69f 2018-08-18 stsp err = log_tree_entry(new_view, view,
2874 e5a0f69f 2018-08-18 stsp s->selected_entry, &s->parents,
2875 e5a0f69f 2018-08-18 stsp s->commit_id, s->repo);
2879 e5a0f69f 2018-08-18 stsp case KEY_UP:
2880 e5a0f69f 2018-08-18 stsp if (s->selected > 0)
2881 e5a0f69f 2018-08-18 stsp s->selected--;
2882 e5a0f69f 2018-08-18 stsp if (s->selected > 0)
2884 e5a0f69f 2018-08-18 stsp tree_scroll_up(&s->first_displayed_entry, 1,
2885 e5a0f69f 2018-08-18 stsp s->entries, s->tree == s->root);
2887 e5a0f69f 2018-08-18 stsp case KEY_PPAGE:
2888 e5a0f69f 2018-08-18 stsp if (SIMPLEQ_FIRST(&s->entries->head) ==
2889 e5a0f69f 2018-08-18 stsp s->first_displayed_entry) {
2890 e5a0f69f 2018-08-18 stsp if (s->tree != s->root)
2891 e5a0f69f 2018-08-18 stsp s->first_displayed_entry = NULL;
2892 e5a0f69f 2018-08-18 stsp s->selected = 0;
2895 e5a0f69f 2018-08-18 stsp tree_scroll_up(&s->first_displayed_entry,
2896 e5a0f69f 2018-08-18 stsp view->nlines, s->entries,
2897 e5a0f69f 2018-08-18 stsp s->tree == s->root);
2900 e5a0f69f 2018-08-18 stsp case KEY_DOWN:
2901 e5a0f69f 2018-08-18 stsp if (s->selected < s->ndisplayed - 1) {
2902 e5a0f69f 2018-08-18 stsp s->selected++;
2905 e5a0f69f 2018-08-18 stsp tree_scroll_down(&s->first_displayed_entry, 1,
2906 e5a0f69f 2018-08-18 stsp s->last_displayed_entry, s->entries);
2908 e5a0f69f 2018-08-18 stsp case KEY_NPAGE:
2909 e5a0f69f 2018-08-18 stsp tree_scroll_down(&s->first_displayed_entry,
2910 e5a0f69f 2018-08-18 stsp view->nlines, s->last_displayed_entry,
2911 e5a0f69f 2018-08-18 stsp s->entries);
2912 e5a0f69f 2018-08-18 stsp if (SIMPLEQ_NEXT(s->last_displayed_entry,
2915 e5a0f69f 2018-08-18 stsp /* can't scroll any further; move cursor down */
2916 e5a0f69f 2018-08-18 stsp if (s->selected < s->ndisplayed - 1)
2917 e5a0f69f 2018-08-18 stsp s->selected = s->ndisplayed - 1;
2919 e5a0f69f 2018-08-18 stsp case KEY_ENTER:
2920 e5a0f69f 2018-08-18 stsp case '\r':
2921 e5a0f69f 2018-08-18 stsp if (s->selected_entry == NULL) {
2922 e5a0f69f 2018-08-18 stsp struct tog_parent_tree *parent;
2923 7837eeac 2018-09-24 stsp case KEY_BACKSPACE:
2924 e5a0f69f 2018-08-18 stsp /* user selected '..' */
2925 e5a0f69f 2018-08-18 stsp if (s->tree == s->root)
2927 e5a0f69f 2018-08-18 stsp parent = TAILQ_FIRST(&s->parents);
2928 e5a0f69f 2018-08-18 stsp TAILQ_REMOVE(&s->parents, parent,
2930 e5a0f69f 2018-08-18 stsp got_object_tree_close(s->tree);
2931 e5a0f69f 2018-08-18 stsp s->tree = parent->tree;
2932 e5a0f69f 2018-08-18 stsp s->entries =
2933 e5a0f69f 2018-08-18 stsp got_object_tree_get_entries(s->tree);
2934 e5a0f69f 2018-08-18 stsp s->first_displayed_entry =
2935 e5a0f69f 2018-08-18 stsp parent->first_displayed_entry;
2936 e5a0f69f 2018-08-18 stsp s->selected_entry =
2937 e5a0f69f 2018-08-18 stsp parent->selected_entry;
2938 e5a0f69f 2018-08-18 stsp s->selected = parent->selected;
2939 e5a0f69f 2018-08-18 stsp free(parent);
2940 e5a0f69f 2018-08-18 stsp } else if (S_ISDIR(s->selected_entry->mode)) {
2941 e5a0f69f 2018-08-18 stsp struct tog_parent_tree *parent;
2942 e5a0f69f 2018-08-18 stsp struct got_tree_object *child;
2943 e5a0f69f 2018-08-18 stsp err = got_object_open_as_tree(&child,
2944 e5a0f69f 2018-08-18 stsp s->repo, s->selected_entry->id);
2947 e5a0f69f 2018-08-18 stsp parent = calloc(1, sizeof(*parent));
2948 e5a0f69f 2018-08-18 stsp if (parent == NULL) {
2949 e5a0f69f 2018-08-18 stsp err = got_error_from_errno();
2952 e5a0f69f 2018-08-18 stsp parent->tree = s->tree;
2953 e5a0f69f 2018-08-18 stsp parent->first_displayed_entry =
2954 e5a0f69f 2018-08-18 stsp s->first_displayed_entry;
2955 e5a0f69f 2018-08-18 stsp parent->selected_entry = s->selected_entry;
2956 e5a0f69f 2018-08-18 stsp parent->selected = s->selected;
2957 e5a0f69f 2018-08-18 stsp TAILQ_INSERT_HEAD(&s->parents, parent, entry);
2958 e5a0f69f 2018-08-18 stsp s->tree = child;
2959 e5a0f69f 2018-08-18 stsp s->entries =
2960 e5a0f69f 2018-08-18 stsp got_object_tree_get_entries(s->tree);
2961 e5a0f69f 2018-08-18 stsp s->selected = 0;
2962 e5a0f69f 2018-08-18 stsp s->first_displayed_entry = NULL;
2963 e5a0f69f 2018-08-18 stsp } else if (S_ISREG(s->selected_entry->mode)) {
2964 e5a0f69f 2018-08-18 stsp err = blame_tree_entry(new_view, view,
2965 e5a0f69f 2018-08-18 stsp s->selected_entry, &s->parents,
2966 e5a0f69f 2018-08-18 stsp s->commit_id, s->repo);
2971 e5a0f69f 2018-08-18 stsp case KEY_RESIZE:
2972 e5a0f69f 2018-08-18 stsp if (s->selected > view->nlines)
2973 e5a0f69f 2018-08-18 stsp s->selected = s->ndisplayed - 1;
2979 ffd1d5e5 2018-06-23 stsp return err;
2982 ffd1d5e5 2018-06-23 stsp __dead static void
2983 ffd1d5e5 2018-06-23 stsp usage_tree(void)
2986 ffd1d5e5 2018-06-23 stsp fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
2987 ffd1d5e5 2018-06-23 stsp getprogname());
2991 ffd1d5e5 2018-06-23 stsp static const struct got_error *
2992 ffd1d5e5 2018-06-23 stsp cmd_tree(int argc, char *argv[])
2994 ffd1d5e5 2018-06-23 stsp const struct got_error *error;
2995 ffd1d5e5 2018-06-23 stsp struct got_repository *repo = NULL;
2996 ffd1d5e5 2018-06-23 stsp char *repo_path = NULL;
2997 ffd1d5e5 2018-06-23 stsp struct got_object_id *commit_id = NULL;
2998 ffd1d5e5 2018-06-23 stsp char *commit_id_arg = NULL;
2999 ffd1d5e5 2018-06-23 stsp struct got_commit_object *commit = NULL;
3000 ffd1d5e5 2018-06-23 stsp struct got_tree_object *tree = NULL;
3002 5221c383 2018-08-01 stsp struct tog_view *view;
3004 ffd1d5e5 2018-06-23 stsp #ifndef PROFILE
3005 ad242220 2018-09-08 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd", NULL)
3007 ffd1d5e5 2018-06-23 stsp err(1, "pledge");
3010 ffd1d5e5 2018-06-23 stsp while ((ch = getopt(argc, argv, "c:")) != -1) {
3011 ffd1d5e5 2018-06-23 stsp switch (ch) {
3013 ffd1d5e5 2018-06-23 stsp commit_id_arg = optarg;
3017 ffd1d5e5 2018-06-23 stsp /* NOTREACHED */
3021 ffd1d5e5 2018-06-23 stsp argc -= optind;
3022 ffd1d5e5 2018-06-23 stsp argv += optind;
3024 ffd1d5e5 2018-06-23 stsp if (argc == 0) {
3025 ffd1d5e5 2018-06-23 stsp repo_path = getcwd(NULL, 0);
3026 ffd1d5e5 2018-06-23 stsp if (repo_path == NULL)
3027 ffd1d5e5 2018-06-23 stsp return got_error_from_errno();
3028 ffd1d5e5 2018-06-23 stsp } else if (argc == 1) {
3029 ffd1d5e5 2018-06-23 stsp repo_path = realpath(argv[0], NULL);
3030 ffd1d5e5 2018-06-23 stsp if (repo_path == NULL)
3031 ffd1d5e5 2018-06-23 stsp return got_error_from_errno();
3033 ffd1d5e5 2018-06-23 stsp usage_log();
3035 ffd1d5e5 2018-06-23 stsp error = got_repo_open(&repo, repo_path);
3036 ffd1d5e5 2018-06-23 stsp free(repo_path);
3037 ffd1d5e5 2018-06-23 stsp if (error != NULL)
3038 ffd1d5e5 2018-06-23 stsp return error;
3040 ffd1d5e5 2018-06-23 stsp if (commit_id_arg == NULL) {
3041 ffd1d5e5 2018-06-23 stsp error = get_head_commit_id(&commit_id, repo);
3042 ffd1d5e5 2018-06-23 stsp if (error != NULL)
3043 ffd1d5e5 2018-06-23 stsp goto done;
3045 ffd1d5e5 2018-06-23 stsp struct got_object *obj;
3046 ffd1d5e5 2018-06-23 stsp error = got_object_open_by_id_str(&obj, repo, commit_id_arg);
3047 ffd1d5e5 2018-06-23 stsp if (error == NULL) {
3048 6402fb3c 2018-09-15 stsp commit_id = got_object_id_dup(got_object_get_id(obj));
3049 ffd1d5e5 2018-06-23 stsp if (commit_id == NULL)
3050 ffd1d5e5 2018-06-23 stsp error = got_error_from_errno();
3053 ffd1d5e5 2018-06-23 stsp if (error != NULL)
3054 ffd1d5e5 2018-06-23 stsp goto done;
3056 ffd1d5e5 2018-06-23 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
3057 ffd1d5e5 2018-06-23 stsp if (error != NULL)
3058 ffd1d5e5 2018-06-23 stsp goto done;
3060 ffd1d5e5 2018-06-23 stsp error = got_object_open_as_tree(&tree, repo, commit->tree_id);
3061 ffd1d5e5 2018-06-23 stsp if (error != NULL)
3062 ffd1d5e5 2018-06-23 stsp goto done;
3064 b3665f43 2018-08-04 stsp view = view_open(0, 0, 0, 0, NULL, TOG_VIEW_TREE);
3065 5221c383 2018-08-01 stsp if (view == NULL) {
3066 5221c383 2018-08-01 stsp error = got_error_from_errno();
3067 5221c383 2018-08-01 stsp goto done;
3069 ad80ab7b 2018-08-04 stsp error = open_tree_view(view, tree, commit_id, repo);
3070 ad80ab7b 2018-08-04 stsp if (error)
3071 ad80ab7b 2018-08-04 stsp goto done;
3072 e5a0f69f 2018-08-18 stsp error = view_loop(view);
3074 ffd1d5e5 2018-06-23 stsp free(commit_id);
3075 ffd1d5e5 2018-06-23 stsp if (commit)
3076 ffd1d5e5 2018-06-23 stsp got_object_commit_close(commit);
3078 ffd1d5e5 2018-06-23 stsp got_object_tree_close(tree);
3080 ffd1d5e5 2018-06-23 stsp got_repo_close(repo);
3081 ffd1d5e5 2018-06-23 stsp return error;
3083 5c5136c5 2018-05-20 stsp static void
3084 9f7d7167 2018-04-29 stsp init_curses(void)
3086 9f7d7167 2018-04-29 stsp initscr();
3090 9f7d7167 2018-04-29 stsp intrflush(stdscr, FALSE);
3091 9f7d7167 2018-04-29 stsp keypad(stdscr, TRUE);
3092 1f475ad8 2018-05-10 stsp curs_set(0);
3095 4ed7e80c 2018-05-20 stsp __dead static void
3096 9f7d7167 2018-04-29 stsp usage(void)
3100 c2301be8 2018-04-30 stsp fprintf(stderr, "usage: %s [-h] [command] [arg ...]\n\n"
3101 9f7d7167 2018-04-29 stsp "Available commands:\n", getprogname());
3102 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
3103 9f7d7167 2018-04-29 stsp struct tog_cmd *cmd = &tog_commands[i];
3104 c2301be8 2018-04-30 stsp fprintf(stderr, " %s: %s\n", cmd->name, cmd->descr);
3109 c2301be8 2018-04-30 stsp static char **
3110 c2301be8 2018-04-30 stsp make_argv(const char *arg0, const char *arg1)
3112 c2301be8 2018-04-30 stsp char **argv;
3113 c2301be8 2018-04-30 stsp int argc = (arg1 == NULL ? 1 : 2);
3115 c2301be8 2018-04-30 stsp argv = calloc(argc, sizeof(char *));
3116 c2301be8 2018-04-30 stsp if (argv == NULL)
3117 c2301be8 2018-04-30 stsp err(1, "calloc");
3118 c2301be8 2018-04-30 stsp argv[0] = strdup(arg0);
3119 c2301be8 2018-04-30 stsp if (argv[0] == NULL)
3120 c2301be8 2018-04-30 stsp err(1, "calloc");
3121 c2301be8 2018-04-30 stsp if (arg1) {
3122 c2301be8 2018-04-30 stsp argv[1] = strdup(arg1);
3123 c2301be8 2018-04-30 stsp if (argv[1] == NULL)
3124 c2301be8 2018-04-30 stsp err(1, "calloc");
3127 c2301be8 2018-04-30 stsp return argv;
3131 9f7d7167 2018-04-29 stsp main(int argc, char *argv[])
3133 9f7d7167 2018-04-29 stsp const struct got_error *error = NULL;
3134 9f7d7167 2018-04-29 stsp struct tog_cmd *cmd = NULL;
3135 9f7d7167 2018-04-29 stsp int ch, hflag = 0;
3136 c2301be8 2018-04-30 stsp char **cmd_argv = NULL;
3138 9f7d7167 2018-04-29 stsp setlocale(LC_ALL, "");
3140 9f7d7167 2018-04-29 stsp while ((ch = getopt(argc, argv, "h")) != -1) {
3141 9f7d7167 2018-04-29 stsp switch (ch) {
3143 9f7d7167 2018-04-29 stsp hflag = 1;
3147 9f7d7167 2018-04-29 stsp /* NOTREACHED */
3151 9f7d7167 2018-04-29 stsp argc -= optind;
3152 9f7d7167 2018-04-29 stsp argv += optind;
3153 9f7d7167 2018-04-29 stsp optind = 0;
3154 c2301be8 2018-04-30 stsp optreset = 1;
3156 c2301be8 2018-04-30 stsp if (argc == 0) {
3157 f29d3e89 2018-06-23 stsp if (hflag)
3159 c2301be8 2018-04-30 stsp /* Build an argument vector which runs a default command. */
3160 9f7d7167 2018-04-29 stsp cmd = &tog_commands[0];
3161 c2301be8 2018-04-30 stsp cmd_argv = make_argv(cmd->name, NULL);
3166 c2301be8 2018-04-30 stsp /* Did the user specific a command? */
3167 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
3168 c2301be8 2018-04-30 stsp if (strncmp(tog_commands[i].name, argv[0],
3169 9f7d7167 2018-04-29 stsp strlen(argv[0])) == 0) {
3170 9f7d7167 2018-04-29 stsp cmd = &tog_commands[i];
3171 9f7d7167 2018-04-29 stsp if (hflag)
3172 9f7d7167 2018-04-29 stsp tog_commands[i].cmd_usage();
3176 9f7d7167 2018-04-29 stsp if (cmd == NULL) {
3177 c2301be8 2018-04-30 stsp /* Did the user specify a repository? */
3178 c2301be8 2018-04-30 stsp char *repo_path = realpath(argv[0], NULL);
3179 c2301be8 2018-04-30 stsp if (repo_path) {
3180 c2301be8 2018-04-30 stsp struct got_repository *repo;
3181 c2301be8 2018-04-30 stsp error = got_repo_open(&repo, repo_path);
3182 c2301be8 2018-04-30 stsp if (error == NULL)
3183 c2301be8 2018-04-30 stsp got_repo_close(repo);
3185 ad7de8d9 2018-04-30 stsp error = got_error_from_errno();
3186 c2301be8 2018-04-30 stsp if (error) {
3187 f29d3e89 2018-06-23 stsp if (hflag) {
3188 f29d3e89 2018-06-23 stsp fprintf(stderr, "%s: '%s' is not a "
3189 f29d3e89 2018-06-23 stsp "known command\n", getprogname(),
3193 ad7de8d9 2018-04-30 stsp fprintf(stderr, "%s: '%s' is neither a known "
3194 ad7de8d9 2018-04-30 stsp "command nor a path to a repository\n",
3195 c2301be8 2018-04-30 stsp getprogname(), argv[0]);
3196 ad7de8d9 2018-04-30 stsp free(repo_path);
3199 c2301be8 2018-04-30 stsp cmd = &tog_commands[0];
3200 c2301be8 2018-04-30 stsp cmd_argv = make_argv(cmd->name, repo_path);
3202 c2301be8 2018-04-30 stsp free(repo_path);
3206 5c5136c5 2018-05-20 stsp init_curses();
3208 c2301be8 2018-04-30 stsp error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
3209 9f7d7167 2018-04-29 stsp if (error)
3210 9f7d7167 2018-04-29 stsp goto done;
3213 c2301be8 2018-04-30 stsp free(cmd_argv);
3214 9f7d7167 2018-04-29 stsp if (error)
3215 9f7d7167 2018-04-29 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);