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 1004088d 2018-09-29 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 1004088d 2018-09-29 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 1004088d 2018-09-29 stsp view->focussed = 0;
398 1004088d 2018-09-29 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 1004088d 2018-09-29 stsp view->focussed = 0;
407 1004088d 2018-09-29 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 1004088d 2018-09-29 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 1004088d 2018-09-29 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 1004088d 2018-09-29 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 1004088d 2018-09-29 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 1004088d 2018-09-29 stsp new_view->parent->focussed = 0;
530 bcbd79e2 2018-08-19 stsp view = new_view;
531 1004088d 2018-09-29 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 38fe7374 2018-09-29 stsp open_diff_view_for_commit(struct tog_view **new_view,
1009 38fe7374 2018-09-29 stsp struct tog_view *parent_view, struct got_object_id *commit_id,
1010 38fe7374 2018-09-29 stsp struct got_commit_object *commit,
1011 2a4718d3 2018-09-29 stsp struct got_repository *repo)
1013 cd0acaa7 2018-05-20 stsp const struct got_error *err;
1014 cd0acaa7 2018-05-20 stsp struct got_object *obj1 = NULL, *obj2 = NULL;
1015 9ba79e04 2018-06-11 stsp struct got_object_qid *parent_id;
1016 e5a0f69f 2018-08-18 stsp struct tog_view *diff_view;
1018 2a4718d3 2018-09-29 stsp err = got_object_open(&obj2, repo, commit_id);
1020 cd0acaa7 2018-05-20 stsp return err;
1022 2a4718d3 2018-09-29 stsp parent_id = SIMPLEQ_FIRST(&commit->parent_ids);
1023 9ba79e04 2018-06-11 stsp if (parent_id) {
1024 9ba79e04 2018-06-11 stsp err = got_object_open(&obj1, repo, parent_id->id);
1026 cd0acaa7 2018-05-20 stsp goto done;
1029 e5a0f69f 2018-08-18 stsp diff_view = view_open(0, 0, 0, 0, parent_view, TOG_VIEW_DIFF);
1030 e5a0f69f 2018-08-18 stsp if (diff_view == NULL) {
1031 ea5e7bb5 2018-08-01 stsp err = got_error_from_errno();
1032 ea5e7bb5 2018-08-01 stsp goto done;
1035 e5a0f69f 2018-08-18 stsp err = open_diff_view(diff_view, obj1, obj2, repo);
1036 e5a0f69f 2018-08-18 stsp if (err == NULL)
1037 e5a0f69f 2018-08-18 stsp *new_view = diff_view;
1040 cd0acaa7 2018-05-20 stsp got_object_close(obj1);
1042 cd0acaa7 2018-05-20 stsp got_object_close(obj2);
1043 cd0acaa7 2018-05-20 stsp return err;
1046 80ddbec8 2018-04-29 stsp static const struct got_error *
1047 e5a0f69f 2018-08-18 stsp browse_commit(struct tog_view **new_view, struct tog_view *parent_view,
1048 e5a0f69f 2018-08-18 stsp struct commit_queue_entry *entry, struct got_repository *repo)
1050 9343a5fb 2018-06-23 stsp const struct got_error *err = NULL;
1051 9343a5fb 2018-06-23 stsp struct got_tree_object *tree;
1052 e5a0f69f 2018-08-18 stsp struct tog_view *tree_view;
1054 9343a5fb 2018-06-23 stsp err = got_object_open_as_tree(&tree, repo, entry->commit->tree_id);
1056 9343a5fb 2018-06-23 stsp return err;
1058 e5a0f69f 2018-08-18 stsp tree_view = view_open(0, 0, 0, 0, parent_view, TOG_VIEW_TREE);
1059 e5a0f69f 2018-08-18 stsp if (tree_view == NULL)
1060 e5a0f69f 2018-08-18 stsp return got_error_from_errno();
1062 e5a0f69f 2018-08-18 stsp err = open_tree_view(tree_view, tree, entry->id, repo);
1064 e5a0f69f 2018-08-18 stsp got_object_tree_close(tree);
1066 e5a0f69f 2018-08-18 stsp *new_view = tree_view;
1067 9343a5fb 2018-06-23 stsp return err;
1070 bcbd79e2 2018-08-19 stsp static const struct got_error *
1071 bcbd79e2 2018-08-19 stsp set_child_log_view(struct tog_view *view, struct tog_view *child)
1073 bcbd79e2 2018-08-19 stsp struct tog_log_view_state *s = &view->state.log;
1074 bcbd79e2 2018-08-19 stsp struct tog_diff_view_state *ds;
1075 bcbd79e2 2018-08-19 stsp struct commit_queue_entry *commit, *child_entry = NULL;
1076 bcbd79e2 2018-08-19 stsp int selected_idx = 0;
1078 bcbd79e2 2018-08-19 stsp if (child->type != TOG_VIEW_DIFF)
1079 bcbd79e2 2018-08-19 stsp return NULL;
1080 bcbd79e2 2018-08-19 stsp ds = &child->state.diff;
1082 bcbd79e2 2018-08-19 stsp TAILQ_FOREACH(commit, &s->commits.head, entry) {
1083 a3404814 2018-09-02 stsp if (got_object_id_cmp(commit->id, ds->id2) == 0) {
1084 bcbd79e2 2018-08-19 stsp child_entry = commit;
1088 bcbd79e2 2018-08-19 stsp if (child_entry == NULL)
1089 bcbd79e2 2018-08-19 stsp return NULL;
1091 bcbd79e2 2018-08-19 stsp commit = s->first_displayed_entry;
1092 bcbd79e2 2018-08-19 stsp while (commit) {
1093 bcbd79e2 2018-08-19 stsp if (got_object_id_cmp(commit->id, child_entry->id) == 0) {
1094 bcbd79e2 2018-08-19 stsp s->selected_entry = child_entry;
1095 bcbd79e2 2018-08-19 stsp s->selected = selected_idx;
1098 bcbd79e2 2018-08-19 stsp if (commit == s->last_displayed_entry)
1100 bcbd79e2 2018-08-19 stsp selected_idx++;
1101 bcbd79e2 2018-08-19 stsp commit = TAILQ_NEXT(commit, entry);
1104 bcbd79e2 2018-08-19 stsp return show_log_view(view);
1107 9343a5fb 2018-06-23 stsp static const struct got_error *
1108 ba4f502b 2018-08-04 stsp open_log_view(struct tog_view *view, struct got_object_id *start_id,
1109 04cc582a 2018-08-01 stsp struct got_repository *repo, const char *path)
1111 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
1112 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
1114 fb2756b9 2018-08-04 stsp err = got_repo_map_path(&s->in_repo_path, repo, path);
1115 ecb28ae0 2018-07-16 stsp if (err != NULL)
1116 ecb28ae0 2018-07-16 stsp goto done;
1118 93e45b7c 2018-09-24 stsp err = got_commit_graph_open(&s->graph, start_id, s->in_repo_path,
1121 9ba79e04 2018-06-11 stsp goto done;
1122 93e45b7c 2018-09-24 stsp /* The commit queue only contains commits being displayed. */
1123 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->commits.head);
1124 fb2756b9 2018-08-04 stsp s->commits.ncommits = 0;
1127 9ba79e04 2018-06-11 stsp * Open the initial batch of commits, sorted in commit graph order.
1128 9ba79e04 2018-06-11 stsp * We keep all commits open throughout the lifetime of the log view
1129 9ba79e04 2018-06-11 stsp * in order to avoid having to re-fetch commits from disk while
1130 9ba79e04 2018-06-11 stsp * updating the display.
1132 93e45b7c 2018-09-24 stsp err = queue_commits(s->graph, &s->commits, start_id, view->nlines,
1133 fb2756b9 2018-08-04 stsp repo, s->in_repo_path);
1134 55198a88 2018-07-16 stsp if (err) {
1135 55198a88 2018-07-16 stsp if (err->code != GOT_ERR_ITER_COMPLETED)
1136 55198a88 2018-07-16 stsp goto done;
1137 55198a88 2018-07-16 stsp err = NULL;
1140 93e45b7c 2018-09-24 stsp s->first_displayed_entry = TAILQ_FIRST(&s->commits.head);
1141 1bfa490b 2018-08-18 stsp s->selected_entry = s->first_displayed_entry;
1142 fb2756b9 2018-08-04 stsp s->repo = repo;
1143 5036bf37 2018-09-24 stsp s->start_id = got_object_id_dup(start_id);
1144 5036bf37 2018-09-24 stsp if (s->start_id == NULL) {
1145 5036bf37 2018-09-24 stsp err = got_error_from_errno();
1146 5036bf37 2018-09-24 stsp goto done;
1149 e5a0f69f 2018-08-18 stsp view->show = show_log_view;
1150 e5a0f69f 2018-08-18 stsp view->input = input_log_view;
1151 e5a0f69f 2018-08-18 stsp view->close = close_log_view;
1152 bcbd79e2 2018-08-19 stsp view->set_child = set_child_log_view;
1154 ba4f502b 2018-08-04 stsp return err;
1157 e5a0f69f 2018-08-18 stsp static const struct got_error *
1158 e5a0f69f 2018-08-18 stsp close_log_view(struct tog_view *view)
1160 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
1162 fb2756b9 2018-08-04 stsp if (s->graph)
1163 fb2756b9 2018-08-04 stsp got_commit_graph_close(s->graph);
1164 fb2756b9 2018-08-04 stsp free_commits(&s->commits);
1165 fb2756b9 2018-08-04 stsp free(s->in_repo_path);
1166 5036bf37 2018-09-24 stsp free(s->start_id);
1167 e5a0f69f 2018-08-18 stsp return NULL;
1170 ba4f502b 2018-08-04 stsp static const struct got_error *
1171 c1fea1f8 2018-09-29 stsp update_diff_view(struct tog_view *diff_view,
1172 c1fea1f8 2018-09-29 stsp struct got_object_id *commit_id, struct got_commit_object *commit,
1173 c1fea1f8 2018-09-29 stsp struct got_repository *repo)
1175 bcbd79e2 2018-08-19 stsp const struct got_error *err = NULL;
1176 bcbd79e2 2018-08-19 stsp struct tog_diff_view_state *ds;
1177 bcbd79e2 2018-08-19 stsp struct got_object *obj1 = NULL, *obj2 = NULL;
1178 bcbd79e2 2018-08-19 stsp struct got_object_qid *parent_id;
1180 c1fea1f8 2018-09-29 stsp ds = &diff_view->state.diff;
1181 c1fea1f8 2018-09-29 stsp if (got_object_id_cmp(ds->id2, commit_id) == 0)
1182 bcbd79e2 2018-08-19 stsp return NULL;
1184 c1fea1f8 2018-09-29 stsp err = got_object_open(&obj2, repo, commit_id);
1186 bcbd79e2 2018-08-19 stsp return err;
1188 c1fea1f8 2018-09-29 stsp parent_id = SIMPLEQ_FIRST(&commit->parent_ids);
1189 bcbd79e2 2018-08-19 stsp if (parent_id) {
1190 bcbd79e2 2018-08-19 stsp err = got_object_open(&obj1, repo, parent_id->id);
1192 bcbd79e2 2018-08-19 stsp goto done;
1195 c1fea1f8 2018-09-29 stsp err = close_diff_view(diff_view);
1197 bcbd79e2 2018-08-19 stsp goto done;
1199 c1fea1f8 2018-09-29 stsp err = open_diff_view(diff_view, obj1, obj2, repo);
1201 bcbd79e2 2018-08-19 stsp goto done;
1204 bcbd79e2 2018-08-19 stsp got_object_close(obj1);
1206 bcbd79e2 2018-08-19 stsp got_object_close(obj2);
1207 bcbd79e2 2018-08-19 stsp return err;
1210 bcbd79e2 2018-08-19 stsp static const struct got_error *
1211 ba4f502b 2018-08-04 stsp show_log_view(struct tog_view *view)
1213 bcbd79e2 2018-08-19 stsp const struct got_error *err = NULL;
1214 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
1216 bcbd79e2 2018-08-19 stsp err = draw_commits(view, &s->last_displayed_entry,
1217 e5a0f69f 2018-08-18 stsp &s->selected_entry, s->first_displayed_entry,
1218 e5a0f69f 2018-08-18 stsp &s->commits, s->selected, view->nlines, s->graph,
1219 e5a0f69f 2018-08-18 stsp s->repo, s->in_repo_path);
1221 bcbd79e2 2018-08-19 stsp return err;
1223 c1fea1f8 2018-09-29 stsp if (view->child && view->child->type == TOG_VIEW_DIFF)
1224 c1fea1f8 2018-09-29 stsp err = update_diff_view(view->child, s->selected_entry->id,
1225 c1fea1f8 2018-09-29 stsp s->selected_entry->commit, s->repo);
1227 c1fea1f8 2018-09-29 stsp return err;
1230 e5a0f69f 2018-08-18 stsp static const struct got_error *
1231 e5a0f69f 2018-08-18 stsp input_log_view(struct tog_view **new_view, struct tog_view **dead_view,
1232 e5a0f69f 2018-08-18 stsp struct tog_view *view, int ch)
1234 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
1235 e5a0f69f 2018-08-18 stsp struct tog_log_view_state *s = &view->state.log;
1236 5036bf37 2018-09-24 stsp char *parent_path;
1238 e5a0f69f 2018-08-18 stsp switch (ch) {
1240 e5a0f69f 2018-08-18 stsp case KEY_UP:
1242 e5a0f69f 2018-08-18 stsp if (s->selected > 0)
1243 e5a0f69f 2018-08-18 stsp s->selected--;
1244 e5a0f69f 2018-08-18 stsp if (s->selected > 0)
1246 e5a0f69f 2018-08-18 stsp scroll_up(&s->first_displayed_entry, 1,
1247 e5a0f69f 2018-08-18 stsp &s->commits);
1249 e5a0f69f 2018-08-18 stsp case KEY_PPAGE:
1250 e5a0f69f 2018-08-18 stsp if (TAILQ_FIRST(&s->commits.head) ==
1251 e5a0f69f 2018-08-18 stsp s->first_displayed_entry) {
1252 e5a0f69f 2018-08-18 stsp s->selected = 0;
1255 e5a0f69f 2018-08-18 stsp scroll_up(&s->first_displayed_entry,
1256 e5a0f69f 2018-08-18 stsp view->nlines, &s->commits);
1259 e5a0f69f 2018-08-18 stsp case KEY_DOWN:
1261 e5a0f69f 2018-08-18 stsp if (s->selected < MIN(view->nlines - 2,
1262 e5a0f69f 2018-08-18 stsp s->commits.ncommits - 1)) {
1263 e5a0f69f 2018-08-18 stsp s->selected++;
1266 e5a0f69f 2018-08-18 stsp err = scroll_down(&s->first_displayed_entry, 1,
1267 93e45b7c 2018-09-24 stsp &s->last_displayed_entry, &s->commits,
1268 e5a0f69f 2018-08-18 stsp s->graph, s->repo, s->in_repo_path);
1269 e5a0f69f 2018-08-18 stsp if (err) {
1270 4d7951a5 2018-08-04 stsp if (err->code != GOT_ERR_ITER_COMPLETED)
1272 4d7951a5 2018-08-04 stsp err = NULL;
1275 e5a0f69f 2018-08-18 stsp case KEY_NPAGE: {
1276 e5a0f69f 2018-08-18 stsp struct commit_queue_entry *first;
1277 e5a0f69f 2018-08-18 stsp first = s->first_displayed_entry;
1278 e5a0f69f 2018-08-18 stsp err = scroll_down(&s->first_displayed_entry,
1279 93e45b7c 2018-09-24 stsp view->nlines, &s->last_displayed_entry,
1280 e5a0f69f 2018-08-18 stsp &s->commits, s->graph, s->repo,
1281 e5a0f69f 2018-08-18 stsp s->in_repo_path);
1282 93e45b7c 2018-09-24 stsp if (err && err->code != GOT_ERR_ITER_COMPLETED)
1284 e5a0f69f 2018-08-18 stsp if (first == s->first_displayed_entry &&
1285 e5a0f69f 2018-08-18 stsp s->selected < MIN(view->nlines - 2,
1286 e5a0f69f 2018-08-18 stsp s->commits.ncommits - 1)) {
1287 e5a0f69f 2018-08-18 stsp /* can't scroll further down */
1288 e5a0f69f 2018-08-18 stsp s->selected = MIN(view->nlines - 2,
1289 e5a0f69f 2018-08-18 stsp s->commits.ncommits - 1);
1291 e5a0f69f 2018-08-18 stsp err = NULL;
1294 e5a0f69f 2018-08-18 stsp case KEY_RESIZE:
1295 e5a0f69f 2018-08-18 stsp if (s->selected > view->nlines - 2)
1296 e5a0f69f 2018-08-18 stsp s->selected = view->nlines - 2;
1297 e5a0f69f 2018-08-18 stsp if (s->selected > s->commits.ncommits - 1)
1298 e5a0f69f 2018-08-18 stsp s->selected = s->commits.ncommits - 1;
1300 e5a0f69f 2018-08-18 stsp case KEY_ENTER:
1301 e5a0f69f 2018-08-18 stsp case '\r':
1302 38fe7374 2018-09-29 stsp err = open_diff_view_for_commit(new_view, view,
1303 2a4718d3 2018-09-29 stsp s->selected_entry->id, s->selected_entry->commit,
1307 e5a0f69f 2018-08-18 stsp err = browse_commit(new_view, view, s->selected_entry,
1310 5036bf37 2018-09-24 stsp case KEY_BACKSPACE:
1311 5036bf37 2018-09-24 stsp if (strcmp(s->in_repo_path, "/") == 0)
1313 5036bf37 2018-09-24 stsp parent_path = dirname(s->in_repo_path);
1314 5036bf37 2018-09-24 stsp if (parent_path && strcmp(parent_path, ".") != 0) {
1315 5036bf37 2018-09-24 stsp struct tog_view *lv;
1316 5036bf37 2018-09-24 stsp lv = view_open(0, 0, 0, 0, NULL, TOG_VIEW_LOG);
1317 5036bf37 2018-09-24 stsp if (lv == NULL)
1318 5036bf37 2018-09-24 stsp return got_error_from_errno();
1319 5036bf37 2018-09-24 stsp err = open_log_view(lv, s->start_id, s->repo,
1320 5036bf37 2018-09-24 stsp parent_path);
1323 5036bf37 2018-09-24 stsp *new_view = lv;
1330 80ddbec8 2018-04-29 stsp return err;
1333 4ed7e80c 2018-05-20 stsp static const struct got_error *
1334 9f7d7167 2018-04-29 stsp cmd_log(int argc, char *argv[])
1336 80ddbec8 2018-04-29 stsp const struct got_error *error;
1337 ecb28ae0 2018-07-16 stsp struct got_repository *repo = NULL;
1338 899d86c2 2018-05-10 stsp struct got_object_id *start_id = NULL;
1339 ecb28ae0 2018-07-16 stsp char *path = NULL, *repo_path = NULL, *cwd = NULL;
1340 80ddbec8 2018-04-29 stsp char *start_commit = NULL;
1342 04cc582a 2018-08-01 stsp struct tog_view *view;
1344 80ddbec8 2018-04-29 stsp #ifndef PROFILE
1345 ad242220 2018-09-08 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd", NULL)
1347 80ddbec8 2018-04-29 stsp err(1, "pledge");
1350 ecb28ae0 2018-07-16 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1351 80ddbec8 2018-04-29 stsp switch (ch) {
1353 80ddbec8 2018-04-29 stsp start_commit = optarg;
1356 ecb28ae0 2018-07-16 stsp repo_path = realpath(optarg, NULL);
1357 ecb28ae0 2018-07-16 stsp if (repo_path == NULL)
1358 ecb28ae0 2018-07-16 stsp err(1, "-r option");
1362 80ddbec8 2018-04-29 stsp /* NOTREACHED */
1366 80ddbec8 2018-04-29 stsp argc -= optind;
1367 80ddbec8 2018-04-29 stsp argv += optind;
1369 ecb28ae0 2018-07-16 stsp if (argc == 0)
1370 ecb28ae0 2018-07-16 stsp path = strdup("");
1371 ecb28ae0 2018-07-16 stsp else if (argc == 1)
1372 ecb28ae0 2018-07-16 stsp path = strdup(argv[0]);
1374 80ddbec8 2018-04-29 stsp usage_log();
1375 ecb28ae0 2018-07-16 stsp if (path == NULL)
1376 ecb28ae0 2018-07-16 stsp return got_error_from_errno();
1378 ecb28ae0 2018-07-16 stsp cwd = getcwd(NULL, 0);
1379 ecb28ae0 2018-07-16 stsp if (cwd == NULL) {
1380 ecb28ae0 2018-07-16 stsp error = got_error_from_errno();
1381 ecb28ae0 2018-07-16 stsp goto done;
1383 ecb28ae0 2018-07-16 stsp if (repo_path == NULL) {
1384 ecb28ae0 2018-07-16 stsp repo_path = strdup(cwd);
1385 ecb28ae0 2018-07-16 stsp if (repo_path == NULL) {
1386 ecb28ae0 2018-07-16 stsp error = got_error_from_errno();
1387 ecb28ae0 2018-07-16 stsp goto done;
1391 80ddbec8 2018-04-29 stsp error = got_repo_open(&repo, repo_path);
1392 80ddbec8 2018-04-29 stsp if (error != NULL)
1393 ecb28ae0 2018-07-16 stsp goto done;
1395 80ddbec8 2018-04-29 stsp if (start_commit == NULL) {
1396 899d86c2 2018-05-10 stsp error = get_head_commit_id(&start_id, repo);
1397 80ddbec8 2018-04-29 stsp if (error != NULL)
1398 ecb28ae0 2018-07-16 stsp goto done;
1400 80ddbec8 2018-04-29 stsp struct got_object *obj;
1401 80ddbec8 2018-04-29 stsp error = got_object_open_by_id_str(&obj, repo, start_commit);
1402 80ddbec8 2018-04-29 stsp if (error == NULL) {
1403 6402fb3c 2018-09-15 stsp start_id = got_object_id_dup(got_object_get_id(obj));
1404 899d86c2 2018-05-10 stsp if (start_id == NULL)
1405 80ddbec8 2018-04-29 stsp error = got_error_from_errno();
1406 ecb28ae0 2018-07-16 stsp goto done;
1409 80ddbec8 2018-04-29 stsp if (error != NULL)
1410 ecb28ae0 2018-07-16 stsp goto done;
1412 b3665f43 2018-08-04 stsp view = view_open(0, 0, 0, 0, NULL, TOG_VIEW_LOG);
1413 04cc582a 2018-08-01 stsp if (view == NULL) {
1414 04cc582a 2018-08-01 stsp error = got_error_from_errno();
1415 04cc582a 2018-08-01 stsp goto done;
1417 ba4f502b 2018-08-04 stsp error = open_log_view(view, start_id, repo, path);
1418 ba4f502b 2018-08-04 stsp if (error)
1419 ba4f502b 2018-08-04 stsp goto done;
1420 e5a0f69f 2018-08-18 stsp error = view_loop(view);
1422 ecb28ae0 2018-07-16 stsp free(repo_path);
1423 ecb28ae0 2018-07-16 stsp free(cwd);
1424 ecb28ae0 2018-07-16 stsp free(path);
1425 899d86c2 2018-05-10 stsp free(start_id);
1427 ecb28ae0 2018-07-16 stsp got_repo_close(repo);
1428 80ddbec8 2018-04-29 stsp return error;
1431 4ed7e80c 2018-05-20 stsp __dead static void
1432 9f7d7167 2018-04-29 stsp usage_diff(void)
1435 9f7d7167 2018-04-29 stsp fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
1436 9f7d7167 2018-04-29 stsp getprogname());
1440 b304db33 2018-05-20 stsp static char *
1441 b304db33 2018-05-20 stsp parse_next_line(FILE *f, size_t *len)
1443 b304db33 2018-05-20 stsp char *line;
1444 b304db33 2018-05-20 stsp size_t linelen;
1445 b304db33 2018-05-20 stsp size_t lineno;
1446 b304db33 2018-05-20 stsp const char delim[3] = { '\0', '\0', '\0'};
1448 b304db33 2018-05-20 stsp line = fparseln(f, &linelen, &lineno, delim, 0);
1450 b304db33 2018-05-20 stsp *len = linelen;
1451 b304db33 2018-05-20 stsp return line;
1454 4ed7e80c 2018-05-20 stsp static const struct got_error *
1455 f7d12f7e 2018-08-01 stsp draw_file(struct tog_view *view, FILE *f, int *first_displayed_line,
1456 a3404814 2018-09-02 stsp int *last_displayed_line, int *eof, int max_lines,
1457 a3404814 2018-09-02 stsp char * header)
1459 61e69b96 2018-05-20 stsp const struct got_error *err;
1460 26ed57b2 2018-05-19 stsp int nlines = 0, nprinted = 0;
1461 b304db33 2018-05-20 stsp char *line;
1462 b304db33 2018-05-20 stsp size_t len;
1463 61e69b96 2018-05-20 stsp wchar_t *wline;
1464 e0b650dd 2018-05-20 stsp int width;
1466 26ed57b2 2018-05-19 stsp rewind(f);
1467 f7d12f7e 2018-08-01 stsp werase(view->window);
1469 a3404814 2018-09-02 stsp if (header) {
1470 a3404814 2018-09-02 stsp err = format_line(&wline, &width, header, view->ncols);
1471 a3404814 2018-09-02 stsp if (err) {
1472 a3404814 2018-09-02 stsp return err;
1475 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
1476 a3404814 2018-09-02 stsp wstandout(view->window);
1477 a3404814 2018-09-02 stsp waddwstr(view->window, wline);
1478 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
1479 a3404814 2018-09-02 stsp wstandend(view->window);
1480 a3404814 2018-09-02 stsp if (width < view->ncols)
1481 a3404814 2018-09-02 stsp waddch(view->window, '\n');
1483 a3404814 2018-09-02 stsp if (max_lines <= 1)
1484 a3404814 2018-09-02 stsp return NULL;
1485 a3404814 2018-09-02 stsp max_lines--;
1489 26ed57b2 2018-05-19 stsp while (nprinted < max_lines) {
1490 b304db33 2018-05-20 stsp line = parse_next_line(f, &len);
1491 26ed57b2 2018-05-19 stsp if (line == NULL) {
1495 26ed57b2 2018-05-19 stsp if (++nlines < *first_displayed_line) {
1496 26ed57b2 2018-05-19 stsp free(line);
1500 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, line, view->ncols);
1501 61e69b96 2018-05-20 stsp if (err) {
1502 61e69b96 2018-05-20 stsp free(line);
1503 61e69b96 2018-05-20 stsp return err;
1505 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
1506 f7d12f7e 2018-08-01 stsp if (width < view->ncols)
1507 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
1508 26ed57b2 2018-05-19 stsp if (++nprinted == 1)
1509 26ed57b2 2018-05-19 stsp *first_displayed_line = nlines;
1510 26ed57b2 2018-05-19 stsp free(line);
1511 2550e4c3 2018-07-13 stsp free(wline);
1512 2550e4c3 2018-07-13 stsp wline = NULL;
1514 26ed57b2 2018-05-19 stsp *last_displayed_line = nlines;
1516 1a57306a 2018-09-02 stsp view_vborder(view);
1518 26ed57b2 2018-05-19 stsp return NULL;
1521 4ed7e80c 2018-05-20 stsp static const struct got_error *
1522 5dc9f4bc 2018-08-04 stsp open_diff_view(struct tog_view *view, struct got_object *obj1,
1523 ea5e7bb5 2018-08-01 stsp struct got_object *obj2, struct got_repository *repo)
1525 26ed57b2 2018-05-19 stsp const struct got_error *err;
1528 cd0acaa7 2018-05-20 stsp if (obj1 != NULL && obj2 != NULL &&
1529 cd0acaa7 2018-05-20 stsp got_object_get_type(obj1) != got_object_get_type(obj2))
1530 26ed57b2 2018-05-19 stsp return got_error(GOT_ERR_OBJ_TYPE);
1532 511a516b 2018-05-19 stsp f = got_opentemp();
1533 26ed57b2 2018-05-19 stsp if (f == NULL)
1534 26ed57b2 2018-05-19 stsp return got_error_from_errno();
1536 cd0acaa7 2018-05-20 stsp switch (got_object_get_type(obj1 ? obj1 : obj2)) {
1537 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_BLOB:
1538 f6861a81 2018-09-13 stsp err = got_diff_objects_as_blobs(obj1, obj2, NULL, NULL,
1541 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_TREE:
1542 f6861a81 2018-09-13 stsp err = got_diff_objects_as_trees(obj1, obj2, "", "", repo, f);
1544 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_COMMIT:
1545 11528a82 2018-05-19 stsp err = got_diff_objects_as_commits(obj1, obj2, repo, f);
1548 26ed57b2 2018-05-19 stsp return got_error(GOT_ERR_OBJ_TYPE);
1551 26ed57b2 2018-05-19 stsp fflush(f);
1553 a3404814 2018-09-02 stsp view->state.diff.id1 = obj1 ? got_object_get_id(obj1) : NULL;
1554 a3404814 2018-09-02 stsp view->state.diff.id2 = got_object_get_id(obj2);
1555 5dc9f4bc 2018-08-04 stsp view->state.diff.f = f;
1556 5dc9f4bc 2018-08-04 stsp view->state.diff.first_displayed_line = 1;
1557 5dc9f4bc 2018-08-04 stsp view->state.diff.last_displayed_line = view->nlines;
1559 e5a0f69f 2018-08-18 stsp view->show = show_diff_view;
1560 e5a0f69f 2018-08-18 stsp view->input = input_diff_view;
1561 e5a0f69f 2018-08-18 stsp view->close = close_diff_view;
1563 5dc9f4bc 2018-08-04 stsp return NULL;
1566 e5a0f69f 2018-08-18 stsp static const struct got_error *
1567 5dc9f4bc 2018-08-04 stsp close_diff_view(struct tog_view *view)
1569 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
1571 e5a0f69f 2018-08-18 stsp if (view->state.diff.f && fclose(view->state.diff.f) == EOF)
1572 e5a0f69f 2018-08-18 stsp err = got_error_from_errno();
1573 e5a0f69f 2018-08-18 stsp return err;
1576 5dc9f4bc 2018-08-04 stsp static const struct got_error *
1577 5dc9f4bc 2018-08-04 stsp show_diff_view(struct tog_view *view)
1579 a3404814 2018-09-02 stsp const struct got_error *err;
1580 fb2756b9 2018-08-04 stsp struct tog_diff_view_state *s = &view->state.diff;
1581 a3404814 2018-09-02 stsp char *id_str1 = NULL, *id_str2, *header;
1583 a3404814 2018-09-02 stsp if (s->id1) {
1584 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str1, s->id1);
1586 a3404814 2018-09-02 stsp return err;
1588 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str2, s->id2);
1590 a3404814 2018-09-02 stsp return err;
1592 a3404814 2018-09-02 stsp if (asprintf(&header, "diff: %s %s",
1593 a3404814 2018-09-02 stsp id_str1 ? id_str1 : "/dev/null", id_str2) == -1) {
1594 a3404814 2018-09-02 stsp err = got_error_from_errno();
1595 a3404814 2018-09-02 stsp free(id_str1);
1596 a3404814 2018-09-02 stsp free(id_str2);
1597 a3404814 2018-09-02 stsp return err;
1599 a3404814 2018-09-02 stsp free(id_str1);
1600 a3404814 2018-09-02 stsp free(id_str2);
1602 e5a0f69f 2018-08-18 stsp return draw_file(view, s->f, &s->first_displayed_line,
1603 a3404814 2018-09-02 stsp &s->last_displayed_line, &s->eof, view->nlines,
1607 e5a0f69f 2018-08-18 stsp static const struct got_error *
1608 bcbd79e2 2018-08-19 stsp input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
1609 e5a0f69f 2018-08-18 stsp struct tog_view *view, int ch)
1611 bcbd79e2 2018-08-19 stsp const struct got_error *err = NULL;
1612 e5a0f69f 2018-08-18 stsp struct tog_diff_view_state *s = &view->state.diff;
1615 e5a0f69f 2018-08-18 stsp switch (ch) {
1617 e5a0f69f 2018-08-18 stsp case KEY_UP:
1618 e5a0f69f 2018-08-18 stsp if (s->first_displayed_line > 1)
1619 e5a0f69f 2018-08-18 stsp s->first_displayed_line--;
1621 e5a0f69f 2018-08-18 stsp case KEY_PPAGE:
1623 e5a0f69f 2018-08-18 stsp while (i++ < view->nlines - 1 &&
1624 e5a0f69f 2018-08-18 stsp s->first_displayed_line > 1)
1625 e5a0f69f 2018-08-18 stsp s->first_displayed_line--;
1628 e5a0f69f 2018-08-18 stsp case KEY_DOWN:
1629 e5a0f69f 2018-08-18 stsp if (!s->eof)
1630 e5a0f69f 2018-08-18 stsp s->first_displayed_line++;
1632 e5a0f69f 2018-08-18 stsp case KEY_NPAGE:
1635 e5a0f69f 2018-08-18 stsp while (!s->eof && i++ < view->nlines - 1) {
1636 e5a0f69f 2018-08-18 stsp char *line;
1637 e5a0f69f 2018-08-18 stsp line = parse_next_line(s->f, NULL);
1638 e5a0f69f 2018-08-18 stsp s->first_displayed_line++;
1639 e5a0f69f 2018-08-18 stsp if (line == NULL)
1644 bcbd79e2 2018-08-19 stsp case ']': {
1645 bcbd79e2 2018-08-19 stsp struct tog_log_view_state *ls;
1646 bcbd79e2 2018-08-19 stsp struct commit_queue_entry *entry;
1647 bcbd79e2 2018-08-19 stsp struct tog_view *diff_view;
1649 bcbd79e2 2018-08-19 stsp if (view->parent == NULL)
1651 bcbd79e2 2018-08-19 stsp if (view->parent->type != TOG_VIEW_LOG)
1653 bcbd79e2 2018-08-19 stsp ls = &view->parent->state.log;
1655 bcbd79e2 2018-08-19 stsp if (ch == '[') {
1656 bcbd79e2 2018-08-19 stsp entry = TAILQ_PREV(ls->selected_entry,
1657 bcbd79e2 2018-08-19 stsp commit_queue_head, entry);
1659 bcbd79e2 2018-08-19 stsp entry = TAILQ_NEXT(ls->selected_entry, entry);
1660 bcbd79e2 2018-08-19 stsp if (entry == NULL) {
1661 bcbd79e2 2018-08-19 stsp err = fetch_next_commit(&entry,
1662 bcbd79e2 2018-08-19 stsp ls->selected_entry,
1663 bcbd79e2 2018-08-19 stsp &ls->commits, ls->graph,
1664 bcbd79e2 2018-08-19 stsp ls->repo, ls->in_repo_path);
1669 bcbd79e2 2018-08-19 stsp if (entry == NULL)
1671 38fe7374 2018-09-29 stsp err = open_diff_view_for_commit(&diff_view,
1672 38fe7374 2018-09-29 stsp view->parent, entry->id, entry->commit,
1673 38fe7374 2018-09-29 stsp ls->repo);
1676 bcbd79e2 2018-08-19 stsp *new_view = diff_view;
1677 bcbd79e2 2018-08-19 stsp *dead_view = view;
1684 bcbd79e2 2018-08-19 stsp return err;
1687 4ed7e80c 2018-05-20 stsp static const struct got_error *
1688 9f7d7167 2018-04-29 stsp cmd_diff(int argc, char *argv[])
1690 26ed57b2 2018-05-19 stsp const struct got_error *error = NULL;
1691 26ed57b2 2018-05-19 stsp struct got_repository *repo = NULL;
1692 26ed57b2 2018-05-19 stsp struct got_object *obj1 = NULL, *obj2 = NULL;
1693 26ed57b2 2018-05-19 stsp char *repo_path = NULL;
1694 26ed57b2 2018-05-19 stsp char *obj_id_str1 = NULL, *obj_id_str2 = NULL;
1696 ea5e7bb5 2018-08-01 stsp struct tog_view *view;
1698 26ed57b2 2018-05-19 stsp #ifndef PROFILE
1699 ad242220 2018-09-08 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd", NULL)
1701 26ed57b2 2018-05-19 stsp err(1, "pledge");
1704 26ed57b2 2018-05-19 stsp while ((ch = getopt(argc, argv, "")) != -1) {
1705 26ed57b2 2018-05-19 stsp switch (ch) {
1708 26ed57b2 2018-05-19 stsp /* NOTREACHED */
1712 26ed57b2 2018-05-19 stsp argc -= optind;
1713 26ed57b2 2018-05-19 stsp argv += optind;
1715 26ed57b2 2018-05-19 stsp if (argc == 0) {
1716 26ed57b2 2018-05-19 stsp usage_diff(); /* TODO show local worktree changes */
1717 26ed57b2 2018-05-19 stsp } else if (argc == 2) {
1718 26ed57b2 2018-05-19 stsp repo_path = getcwd(NULL, 0);
1719 26ed57b2 2018-05-19 stsp if (repo_path == NULL)
1720 26ed57b2 2018-05-19 stsp return got_error_from_errno();
1721 26ed57b2 2018-05-19 stsp obj_id_str1 = argv[0];
1722 26ed57b2 2018-05-19 stsp obj_id_str2 = argv[1];
1723 26ed57b2 2018-05-19 stsp } else if (argc == 3) {
1724 26ed57b2 2018-05-19 stsp repo_path = realpath(argv[0], NULL);
1725 26ed57b2 2018-05-19 stsp if (repo_path == NULL)
1726 26ed57b2 2018-05-19 stsp return got_error_from_errno();
1727 26ed57b2 2018-05-19 stsp obj_id_str1 = argv[1];
1728 26ed57b2 2018-05-19 stsp obj_id_str2 = argv[2];
1730 26ed57b2 2018-05-19 stsp usage_diff();
1732 26ed57b2 2018-05-19 stsp error = got_repo_open(&repo, repo_path);
1733 26ed57b2 2018-05-19 stsp free(repo_path);
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(&obj1, repo, obj_id_str1);
1738 26ed57b2 2018-05-19 stsp if (error)
1739 26ed57b2 2018-05-19 stsp goto done;
1741 26ed57b2 2018-05-19 stsp error = got_object_open_by_id_str(&obj2, repo, obj_id_str2);
1742 26ed57b2 2018-05-19 stsp if (error)
1743 26ed57b2 2018-05-19 stsp goto done;
1745 b3665f43 2018-08-04 stsp view = view_open(0, 0, 0, 0, NULL, TOG_VIEW_DIFF);
1746 ea5e7bb5 2018-08-01 stsp if (view == NULL) {
1747 ea5e7bb5 2018-08-01 stsp error = got_error_from_errno();
1748 ea5e7bb5 2018-08-01 stsp goto done;
1750 5dc9f4bc 2018-08-04 stsp error = open_diff_view(view, obj1, obj2, repo);
1751 5dc9f4bc 2018-08-04 stsp if (error)
1752 5dc9f4bc 2018-08-04 stsp goto done;
1753 e5a0f69f 2018-08-18 stsp error = view_loop(view);
1755 26ed57b2 2018-05-19 stsp got_repo_close(repo);
1757 26ed57b2 2018-05-19 stsp got_object_close(obj1);
1759 26ed57b2 2018-05-19 stsp got_object_close(obj2);
1760 26ed57b2 2018-05-19 stsp return error;
1763 4ed7e80c 2018-05-20 stsp __dead static void
1764 9f7d7167 2018-04-29 stsp usage_blame(void)
1767 69069811 2018-08-02 stsp fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
1768 9f7d7167 2018-04-29 stsp getprogname());
1772 84451b3e 2018-07-10 stsp struct tog_blame_line {
1773 84451b3e 2018-07-10 stsp int annotated;
1774 84451b3e 2018-07-10 stsp struct got_object_id *id;
1777 4ed7e80c 2018-05-20 stsp static const struct got_error *
1778 f7d12f7e 2018-08-01 stsp draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
1779 f7d12f7e 2018-08-01 stsp const char *path, struct tog_blame_line *lines, int nlines,
1780 f7d12f7e 2018-08-01 stsp int blame_complete, int selected_line, int *first_displayed_line,
1781 f7d12f7e 2018-08-01 stsp int *last_displayed_line, int *eof, int max_lines)
1783 84451b3e 2018-07-10 stsp const struct got_error *err;
1784 84451b3e 2018-07-10 stsp int lineno = 0, nprinted = 0;
1785 84451b3e 2018-07-10 stsp char *line;
1786 84451b3e 2018-07-10 stsp size_t len;
1787 84451b3e 2018-07-10 stsp wchar_t *wline;
1788 b700b5d6 2018-07-10 stsp int width, wlimit;
1789 84451b3e 2018-07-10 stsp struct tog_blame_line *blame_line;
1790 ee41ec32 2018-07-10 stsp struct got_object_id *prev_id = NULL;
1791 ab089a2a 2018-07-12 stsp char *id_str;
1793 ab089a2a 2018-07-12 stsp err = got_object_id_str(&id_str, id);
1795 ab089a2a 2018-07-12 stsp return err;
1797 84451b3e 2018-07-10 stsp rewind(f);
1798 f7d12f7e 2018-08-01 stsp werase(view->window);
1800 ab089a2a 2018-07-12 stsp if (asprintf(&line, "commit: %s", id_str) == -1) {
1801 ab089a2a 2018-07-12 stsp err = got_error_from_errno();
1802 ab089a2a 2018-07-12 stsp free(id_str);
1803 ab089a2a 2018-07-12 stsp return err;
1806 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, line, view->ncols);
1807 ab089a2a 2018-07-12 stsp free(line);
1808 2550e4c3 2018-07-13 stsp line = NULL;
1809 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
1810 a3404814 2018-09-02 stsp wstandout(view->window);
1811 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
1812 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
1813 a3404814 2018-09-02 stsp wstandend(view->window);
1814 2550e4c3 2018-07-13 stsp free(wline);
1815 2550e4c3 2018-07-13 stsp wline = NULL;
1816 f7d12f7e 2018-08-01 stsp if (width < view->ncols)
1817 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
1819 084063cd 2018-07-12 stsp if (asprintf(&line, "[%d/%d] %s%s",
1820 084063cd 2018-07-12 stsp *first_displayed_line - 1 + selected_line, nlines,
1821 12a0b23b 2018-07-12 stsp blame_complete ? "" : "annotating ", path) == -1) {
1822 ab089a2a 2018-07-12 stsp free(id_str);
1823 3f60a8ef 2018-07-10 stsp return got_error_from_errno();
1825 ab089a2a 2018-07-12 stsp free(id_str);
1826 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, line, view->ncols);
1827 3f60a8ef 2018-07-10 stsp free(line);
1828 2550e4c3 2018-07-13 stsp line = NULL;
1830 3f60a8ef 2018-07-10 stsp return err;
1831 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
1832 2550e4c3 2018-07-13 stsp free(wline);
1833 2550e4c3 2018-07-13 stsp wline = NULL;
1834 f7d12f7e 2018-08-01 stsp if (width < view->ncols)
1835 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
1838 ab089a2a 2018-07-12 stsp while (nprinted < max_lines - 2) {
1839 84451b3e 2018-07-10 stsp line = parse_next_line(f, &len);
1840 84451b3e 2018-07-10 stsp if (line == NULL) {
1844 84451b3e 2018-07-10 stsp if (++lineno < *first_displayed_line) {
1845 84451b3e 2018-07-10 stsp free(line);
1849 f7d12f7e 2018-08-01 stsp wlimit = view->ncols < 9 ? 0 : view->ncols - 9;
1850 b700b5d6 2018-07-10 stsp err = format_line(&wline, &width, line, wlimit);
1851 84451b3e 2018-07-10 stsp if (err) {
1852 84451b3e 2018-07-10 stsp free(line);
1853 84451b3e 2018-07-10 stsp return err;
1856 b700b5d6 2018-07-10 stsp if (nprinted == selected_line - 1)
1857 f7d12f7e 2018-08-01 stsp wstandout(view->window);
1859 84451b3e 2018-07-10 stsp blame_line = &lines[lineno - 1];
1860 ee41ec32 2018-07-10 stsp if (blame_line->annotated && prev_id &&
1861 ee41ec32 2018-07-10 stsp got_object_id_cmp(prev_id, blame_line->id) == 0)
1862 f7d12f7e 2018-08-01 stsp waddstr(view->window, " ");
1863 ee41ec32 2018-07-10 stsp else if (blame_line->annotated) {
1864 84451b3e 2018-07-10 stsp char *id_str;
1865 84451b3e 2018-07-10 stsp err = got_object_id_str(&id_str, blame_line->id);
1866 84451b3e 2018-07-10 stsp if (err) {
1867 84451b3e 2018-07-10 stsp free(line);
1868 2550e4c3 2018-07-13 stsp free(wline);
1869 84451b3e 2018-07-10 stsp return err;
1871 f7d12f7e 2018-08-01 stsp wprintw(view->window, "%.8s ", id_str);
1872 84451b3e 2018-07-10 stsp free(id_str);
1873 ee41ec32 2018-07-10 stsp prev_id = blame_line->id;
1875 f7d12f7e 2018-08-01 stsp waddstr(view->window, "........ ");
1876 ee41ec32 2018-07-10 stsp prev_id = NULL;
1879 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
1880 b700b5d6 2018-07-10 stsp while (width < wlimit) {
1881 f7d12f7e 2018-08-01 stsp waddch(view->window, ' ');
1884 b700b5d6 2018-07-10 stsp if (nprinted == selected_line - 1)
1885 f7d12f7e 2018-08-01 stsp wstandend(view->window);
1886 84451b3e 2018-07-10 stsp if (++nprinted == 1)
1887 84451b3e 2018-07-10 stsp *first_displayed_line = lineno;
1888 84451b3e 2018-07-10 stsp free(line);
1889 2550e4c3 2018-07-13 stsp free(wline);
1890 2550e4c3 2018-07-13 stsp wline = NULL;
1892 84451b3e 2018-07-10 stsp *last_displayed_line = lineno;
1894 1a57306a 2018-09-02 stsp view_vborder(view);
1896 84451b3e 2018-07-10 stsp return NULL;
1899 84451b3e 2018-07-10 stsp static const struct got_error *
1900 84451b3e 2018-07-10 stsp blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
1902 84451b3e 2018-07-10 stsp const struct got_error *err = NULL;
1903 84451b3e 2018-07-10 stsp struct tog_blame_cb_args *a = arg;
1904 84451b3e 2018-07-10 stsp struct tog_blame_line *line;
1906 d68a0a7d 2018-07-10 stsp if (nlines != a->nlines ||
1907 d68a0a7d 2018-07-10 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
1908 84451b3e 2018-07-10 stsp return got_error(GOT_ERR_RANGE);
1910 84451b3e 2018-07-10 stsp if (pthread_mutex_lock(a->mutex) != 0)
1911 84451b3e 2018-07-10 stsp return got_error_from_errno();
1913 18430de3 2018-07-10 stsp if (*a->quit) { /* user has quit the blame view */
1914 d68a0a7d 2018-07-10 stsp err = got_error(GOT_ERR_ITER_COMPLETED);
1915 d68a0a7d 2018-07-10 stsp goto done;
1918 d68a0a7d 2018-07-10 stsp if (lineno == -1)
1919 d68a0a7d 2018-07-10 stsp goto done; /* no change in this commit */
1921 84451b3e 2018-07-10 stsp line = &a->lines[lineno - 1];
1922 d68a0a7d 2018-07-10 stsp if (line->annotated)
1923 d68a0a7d 2018-07-10 stsp goto done;
1925 84451b3e 2018-07-10 stsp line->id = got_object_id_dup(id);
1926 84451b3e 2018-07-10 stsp if (line->id == NULL) {
1927 84451b3e 2018-07-10 stsp err = got_error_from_errno();
1928 84451b3e 2018-07-10 stsp goto done;
1930 84451b3e 2018-07-10 stsp line->annotated = 1;
1932 f7d12f7e 2018-08-01 stsp err = draw_blame(a->view, a->commit_id, a->f, a->path,
1933 245d91c1 2018-07-12 stsp a->lines, a->nlines, 0, *a->selected_line, a->first_displayed_line,
1934 e5a0f69f 2018-08-18 stsp a->last_displayed_line, a->eof, a->view->nlines);
1936 84451b3e 2018-07-10 stsp if (pthread_mutex_unlock(a->mutex) != 0)
1937 84451b3e 2018-07-10 stsp return got_error_from_errno();
1938 84451b3e 2018-07-10 stsp return err;
1941 84451b3e 2018-07-10 stsp static void *
1942 84451b3e 2018-07-10 stsp blame_thread(void *arg)
1944 18430de3 2018-07-10 stsp const struct got_error *err;
1945 18430de3 2018-07-10 stsp struct tog_blame_thread_args *ta = arg;
1946 245d91c1 2018-07-12 stsp struct tog_blame_cb_args *a = ta->cb_args;
1948 ab089a2a 2018-07-12 stsp err = got_blame_incremental(ta->path, a->commit_id, ta->repo,
1949 245d91c1 2018-07-12 stsp blame_cb, ta->cb_args);
1951 18430de3 2018-07-10 stsp if (pthread_mutex_lock(a->mutex) != 0)
1952 18430de3 2018-07-10 stsp return (void *)got_error_from_errno();
1954 c9beca56 2018-07-22 stsp got_repo_close(ta->repo);
1955 c9beca56 2018-07-22 stsp ta->repo = NULL;
1956 c9beca56 2018-07-22 stsp *ta->complete = 1;
1958 f7d12f7e 2018-08-01 stsp err = draw_blame(a->view, a->commit_id, a->f, a->path,
1959 f7d12f7e 2018-08-01 stsp a->lines, a->nlines, 1, *a->selected_line,
1960 e5a0f69f 2018-08-18 stsp a->first_displayed_line, a->last_displayed_line, a->eof,
1961 f7d12f7e 2018-08-01 stsp a->view->nlines);
1963 18430de3 2018-07-10 stsp if (pthread_mutex_unlock(a->mutex) != 0 && err == NULL)
1964 18430de3 2018-07-10 stsp err = got_error_from_errno();
1966 18430de3 2018-07-10 stsp return (void *)err;
1969 245d91c1 2018-07-12 stsp static struct got_object_id *
1970 245d91c1 2018-07-12 stsp get_selected_commit_id(struct tog_blame_line *lines,
1971 245d91c1 2018-07-12 stsp int first_displayed_line, int selected_line)
1973 245d91c1 2018-07-12 stsp struct tog_blame_line *line;
1975 245d91c1 2018-07-12 stsp line = &lines[first_displayed_line - 1 + selected_line - 1];
1976 245d91c1 2018-07-12 stsp if (!line->annotated)
1977 245d91c1 2018-07-12 stsp return NULL;
1979 245d91c1 2018-07-12 stsp return line->id;
1982 84451b3e 2018-07-10 stsp static const struct got_error *
1983 245d91c1 2018-07-12 stsp open_selected_commit(struct got_object **pobj, struct got_object **obj,
1984 b880a918 2018-07-10 stsp struct tog_blame_line *lines, int first_displayed_line,
1985 b880a918 2018-07-10 stsp int selected_line, struct got_repository *repo)
1987 b880a918 2018-07-10 stsp const struct got_error *err = NULL;
1988 b880a918 2018-07-10 stsp struct got_commit_object *commit = NULL;
1989 245d91c1 2018-07-12 stsp struct got_object_id *selected_id;
1990 b880a918 2018-07-10 stsp struct got_object_qid *pid;
1992 b880a918 2018-07-10 stsp *pobj = NULL;
1993 b880a918 2018-07-10 stsp *obj = NULL;
1995 245d91c1 2018-07-12 stsp selected_id = get_selected_commit_id(lines,
1996 245d91c1 2018-07-12 stsp first_displayed_line, selected_line);
1997 245d91c1 2018-07-12 stsp if (selected_id == NULL)
1998 b880a918 2018-07-10 stsp return NULL;
2000 245d91c1 2018-07-12 stsp err = got_object_open(obj, repo, selected_id);
2002 b880a918 2018-07-10 stsp goto done;
2004 b880a918 2018-07-10 stsp err = got_object_commit_open(&commit, repo, *obj);
2006 b880a918 2018-07-10 stsp goto done;
2008 b880a918 2018-07-10 stsp pid = SIMPLEQ_FIRST(&commit->parent_ids);
2009 b880a918 2018-07-10 stsp if (pid) {
2010 b880a918 2018-07-10 stsp err = got_object_open(pobj, repo, pid->id);
2012 b880a918 2018-07-10 stsp goto done;
2015 b880a918 2018-07-10 stsp if (commit)
2016 b880a918 2018-07-10 stsp got_object_commit_close(commit);
2017 b880a918 2018-07-10 stsp return err;
2020 b880a918 2018-07-10 stsp static const struct got_error *
2021 245d91c1 2018-07-12 stsp stop_blame(struct tog_blame *blame)
2023 a70480e0 2018-06-23 stsp const struct got_error *err = NULL;
2026 245d91c1 2018-07-12 stsp if (blame->thread) {
2027 245d91c1 2018-07-12 stsp if (pthread_join(blame->thread, (void **)&err) != 0)
2028 245d91c1 2018-07-12 stsp err = got_error_from_errno();
2029 245d91c1 2018-07-12 stsp if (err && err->code == GOT_ERR_ITER_COMPLETED)
2030 245d91c1 2018-07-12 stsp err = NULL;
2031 245d91c1 2018-07-12 stsp blame->thread = NULL;
2033 245d91c1 2018-07-12 stsp if (blame->thread_args.repo) {
2034 245d91c1 2018-07-12 stsp got_repo_close(blame->thread_args.repo);
2035 245d91c1 2018-07-12 stsp blame->thread_args.repo = NULL;
2037 245d91c1 2018-07-12 stsp if (blame->f) {
2038 245d91c1 2018-07-12 stsp fclose(blame->f);
2039 245d91c1 2018-07-12 stsp blame->f = NULL;
2041 245d91c1 2018-07-12 stsp for (i = 0; i < blame->nlines; i++)
2042 245d91c1 2018-07-12 stsp free(blame->lines[i].id);
2043 245d91c1 2018-07-12 stsp free(blame->lines);
2044 245d91c1 2018-07-12 stsp blame->lines = NULL;
2045 75c32340 2018-07-23 stsp free(blame->cb_args.commit_id);
2046 75c32340 2018-07-23 stsp blame->cb_args.commit_id = NULL;
2048 245d91c1 2018-07-12 stsp return err;
2051 245d91c1 2018-07-12 stsp static const struct got_error *
2052 7cc84d77 2018-08-01 stsp run_blame(struct tog_blame *blame, pthread_mutex_t *mutex,
2053 7cc84d77 2018-08-01 stsp struct tog_view *view, int *blame_complete,
2054 245d91c1 2018-07-12 stsp int *first_displayed_line, int *last_displayed_line,
2055 e5a0f69f 2018-08-18 stsp int *selected_line, int *done, int *eof, const char *path,
2056 245d91c1 2018-07-12 stsp struct got_object_id *commit_id,
2057 245d91c1 2018-07-12 stsp struct got_repository *repo)
2059 245d91c1 2018-07-12 stsp const struct got_error *err = NULL;
2060 84451b3e 2018-07-10 stsp struct got_blob_object *blob = NULL;
2061 245d91c1 2018-07-12 stsp struct got_repository *thread_repo = NULL;
2062 27d434c2 2018-09-15 stsp struct got_object_id *obj_id = NULL;
2063 27d434c2 2018-09-15 stsp struct got_object *obj = NULL;
2065 27d434c2 2018-09-15 stsp err = got_object_id_by_path(&obj_id, repo, commit_id, path);
2067 27d434c2 2018-09-15 stsp goto done;
2069 27d434c2 2018-09-15 stsp err = got_object_open(&obj, repo, obj_id);
2071 84451b3e 2018-07-10 stsp goto done;
2073 84451b3e 2018-07-10 stsp if (got_object_get_type(obj) != GOT_OBJ_TYPE_BLOB) {
2074 84451b3e 2018-07-10 stsp err = got_error(GOT_ERR_OBJ_TYPE);
2075 84451b3e 2018-07-10 stsp goto done;
2078 84451b3e 2018-07-10 stsp err = got_object_blob_open(&blob, repo, obj, 8192);
2080 a70480e0 2018-06-23 stsp goto done;
2081 245d91c1 2018-07-12 stsp blame->f = got_opentemp();
2082 245d91c1 2018-07-12 stsp if (blame->f == NULL) {
2083 84451b3e 2018-07-10 stsp err = got_error_from_errno();
2084 84451b3e 2018-07-10 stsp goto done;
2086 245d91c1 2018-07-12 stsp err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
2087 245d91c1 2018-07-12 stsp blame->f, blob);
2089 84451b3e 2018-07-10 stsp goto done;
2091 245d91c1 2018-07-12 stsp blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
2092 245d91c1 2018-07-12 stsp if (blame->lines == NULL) {
2093 84451b3e 2018-07-10 stsp err = got_error_from_errno();
2094 84451b3e 2018-07-10 stsp goto done;
2097 245d91c1 2018-07-12 stsp err = got_repo_open(&thread_repo, got_repo_get_path(repo));
2099 bd24772e 2018-07-11 stsp goto done;
2101 7cc84d77 2018-08-01 stsp blame->cb_args.view = view;
2102 245d91c1 2018-07-12 stsp blame->cb_args.lines = blame->lines;
2103 245d91c1 2018-07-12 stsp blame->cb_args.nlines = blame->nlines;
2104 245d91c1 2018-07-12 stsp blame->cb_args.mutex = mutex;
2105 245d91c1 2018-07-12 stsp blame->cb_args.commit_id = got_object_id_dup(commit_id);
2106 245d91c1 2018-07-12 stsp if (blame->cb_args.commit_id == NULL) {
2107 245d91c1 2018-07-12 stsp err = got_error_from_errno();
2108 245d91c1 2018-07-12 stsp goto done;
2110 245d91c1 2018-07-12 stsp blame->cb_args.f = blame->f;
2111 245d91c1 2018-07-12 stsp blame->cb_args.path = path;
2112 245d91c1 2018-07-12 stsp blame->cb_args.first_displayed_line = first_displayed_line;
2113 245d91c1 2018-07-12 stsp blame->cb_args.selected_line = selected_line;
2114 245d91c1 2018-07-12 stsp blame->cb_args.last_displayed_line = last_displayed_line;
2115 245d91c1 2018-07-12 stsp blame->cb_args.quit = done;
2116 e5a0f69f 2018-08-18 stsp blame->cb_args.eof = eof;
2118 245d91c1 2018-07-12 stsp blame->thread_args.path = path;
2119 245d91c1 2018-07-12 stsp blame->thread_args.repo = thread_repo;
2120 245d91c1 2018-07-12 stsp blame->thread_args.cb_args = &blame->cb_args;
2121 245d91c1 2018-07-12 stsp blame->thread_args.complete = blame_complete;
2122 245d91c1 2018-07-12 stsp *blame_complete = 0;
2124 245d91c1 2018-07-12 stsp if (pthread_create(&blame->thread, NULL, blame_thread,
2125 245d91c1 2018-07-12 stsp &blame->thread_args) != 0) {
2126 245d91c1 2018-07-12 stsp err = got_error_from_errno();
2127 245d91c1 2018-07-12 stsp goto done;
2132 245d91c1 2018-07-12 stsp got_object_blob_close(blob);
2133 27d434c2 2018-09-15 stsp free(obj_id);
2135 245d91c1 2018-07-12 stsp got_object_close(obj);
2137 245d91c1 2018-07-12 stsp stop_blame(blame);
2138 245d91c1 2018-07-12 stsp return err;
2141 245d91c1 2018-07-12 stsp static const struct got_error *
2142 e5a0f69f 2018-08-18 stsp open_blame_view(struct tog_view *view, char *path,
2143 e1cd8fed 2018-08-01 stsp struct got_object_id *commit_id, struct got_repository *repo)
2145 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL;
2146 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
2148 fb2756b9 2018-08-04 stsp SIMPLEQ_INIT(&s->blamed_commits);
2150 fb2756b9 2018-08-04 stsp if (pthread_mutex_init(&s->mutex, NULL) != 0)
2151 7cbe629d 2018-08-04 stsp return got_error_from_errno();
2153 fb2756b9 2018-08-04 stsp err = got_object_qid_alloc(&s->blamed_commit, commit_id);
2155 7cbe629d 2018-08-04 stsp return err;
2157 fb2756b9 2018-08-04 stsp SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
2158 fb2756b9 2018-08-04 stsp s->first_displayed_line = 1;
2159 fb2756b9 2018-08-04 stsp s->last_displayed_line = view->nlines;
2160 fb2756b9 2018-08-04 stsp s->selected_line = 1;
2161 fb2756b9 2018-08-04 stsp s->blame_complete = 0;
2162 fb2756b9 2018-08-04 stsp s->path = path;
2163 e5a0f69f 2018-08-18 stsp if (s->path == NULL)
2164 e5a0f69f 2018-08-18 stsp return got_error_from_errno();
2165 fb2756b9 2018-08-04 stsp s->repo = repo;
2166 fb2756b9 2018-08-04 stsp s->commit_id = commit_id;
2167 e9424729 2018-08-04 stsp memset(&s->blame, 0, sizeof(s->blame));
2169 e5a0f69f 2018-08-18 stsp view->show = show_blame_view;
2170 e5a0f69f 2018-08-18 stsp view->input = input_blame_view;
2171 e5a0f69f 2018-08-18 stsp view->close = close_blame_view;
2173 e5a0f69f 2018-08-18 stsp return run_blame(&s->blame, &s->mutex, view, &s->blame_complete,
2174 e5a0f69f 2018-08-18 stsp &s->first_displayed_line, &s->last_displayed_line,
2175 e5a0f69f 2018-08-18 stsp &s->selected_line, &s->done, &s->eof, s->path,
2176 e5a0f69f 2018-08-18 stsp s->blamed_commit->id, s->repo);
2179 e5a0f69f 2018-08-18 stsp static const struct got_error *
2180 7cbe629d 2018-08-04 stsp close_blame_view(struct tog_view *view)
2182 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
2183 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
2185 e5a0f69f 2018-08-18 stsp if (s->blame.thread)
2186 e5a0f69f 2018-08-18 stsp err = stop_blame(&s->blame);
2188 fb2756b9 2018-08-04 stsp while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
2189 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
2190 fb2756b9 2018-08-04 stsp blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
2191 fb2756b9 2018-08-04 stsp SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
2192 7cbe629d 2018-08-04 stsp got_object_qid_free(blamed_commit);
2195 e5a0f69f 2018-08-18 stsp free(s->path);
2197 e5a0f69f 2018-08-18 stsp return err;
2200 7cbe629d 2018-08-04 stsp static const struct got_error *
2201 7cbe629d 2018-08-04 stsp show_blame_view(struct tog_view *view)
2203 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
2204 e5a0f69f 2018-08-18 stsp struct tog_blame_view_state *s = &view->state.blame;
2206 e5a0f69f 2018-08-18 stsp if (pthread_mutex_lock(&s->mutex) != 0)
2207 e5a0f69f 2018-08-18 stsp return got_error_from_errno();
2209 e5a0f69f 2018-08-18 stsp err = draw_blame(view, s->blamed_commit->id, s->blame.f,
2210 e5a0f69f 2018-08-18 stsp s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
2211 e5a0f69f 2018-08-18 stsp s->selected_line, &s->first_displayed_line,
2212 e5a0f69f 2018-08-18 stsp &s->last_displayed_line, &s->eof, view->nlines);
2214 e5a0f69f 2018-08-18 stsp if (pthread_mutex_unlock(&s->mutex) != 0 && err == NULL)
2215 e5a0f69f 2018-08-18 stsp err = got_error_from_errno();
2217 e5a0f69f 2018-08-18 stsp return err;
2220 e5a0f69f 2018-08-18 stsp static const struct got_error *
2221 e5a0f69f 2018-08-18 stsp input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
2222 e5a0f69f 2018-08-18 stsp struct tog_view *view, int ch)
2224 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL, *thread_err = NULL;
2225 7cbe629d 2018-08-04 stsp struct got_object *obj = NULL, *pobj = NULL;
2226 7cbe629d 2018-08-04 stsp struct tog_view *diff_view;
2227 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
2229 e5a0f69f 2018-08-18 stsp if (pthread_mutex_lock(&s->mutex) != 0) {
2230 e5a0f69f 2018-08-18 stsp err = got_error_from_errno();
2231 e5a0f69f 2018-08-18 stsp goto done;
2234 e5a0f69f 2018-08-18 stsp switch (ch) {
2236 e5a0f69f 2018-08-18 stsp s->done = 1;
2237 e5a0f69f 2018-08-18 stsp if (pthread_mutex_unlock(&s->mutex) != 0) {
2238 e5a0f69f 2018-08-18 stsp err = got_error_from_errno();
2239 e5a0f69f 2018-08-18 stsp goto done;
2241 e5a0f69f 2018-08-18 stsp return stop_blame(&s->blame);
2243 e5a0f69f 2018-08-18 stsp case KEY_UP:
2244 e5a0f69f 2018-08-18 stsp if (s->selected_line > 1)
2245 e5a0f69f 2018-08-18 stsp s->selected_line--;
2246 e5a0f69f 2018-08-18 stsp else if (s->selected_line == 1 &&
2247 e5a0f69f 2018-08-18 stsp s->first_displayed_line > 1)
2248 e5a0f69f 2018-08-18 stsp s->first_displayed_line--;
2250 e5a0f69f 2018-08-18 stsp case KEY_PPAGE:
2251 e5a0f69f 2018-08-18 stsp if (s->first_displayed_line == 1) {
2252 e5a0f69f 2018-08-18 stsp s->selected_line = 1;
2255 e5a0f69f 2018-08-18 stsp if (s->first_displayed_line > view->nlines - 2)
2256 e5a0f69f 2018-08-18 stsp s->first_displayed_line -=
2257 e5a0f69f 2018-08-18 stsp (view->nlines - 2);
2259 e5a0f69f 2018-08-18 stsp s->first_displayed_line = 1;
2262 e5a0f69f 2018-08-18 stsp case KEY_DOWN:
2263 e5a0f69f 2018-08-18 stsp if (s->selected_line < view->nlines - 2 &&
2264 e5a0f69f 2018-08-18 stsp s->first_displayed_line +
2265 e5a0f69f 2018-08-18 stsp s->selected_line <= s->blame.nlines)
2266 e5a0f69f 2018-08-18 stsp s->selected_line++;
2267 e5a0f69f 2018-08-18 stsp else if (s->last_displayed_line <
2268 e5a0f69f 2018-08-18 stsp s->blame.nlines)
2269 e5a0f69f 2018-08-18 stsp s->first_displayed_line++;
2272 e5a0f69f 2018-08-18 stsp case 'p': {
2273 e5a0f69f 2018-08-18 stsp struct got_object_id *id;
2274 e5a0f69f 2018-08-18 stsp id = get_selected_commit_id(s->blame.lines,
2275 e5a0f69f 2018-08-18 stsp s->first_displayed_line, s->selected_line);
2276 e5a0f69f 2018-08-18 stsp if (id == NULL || got_object_id_cmp(id,
2277 e5a0f69f 2018-08-18 stsp s->blamed_commit->id) == 0)
2279 e5a0f69f 2018-08-18 stsp err = open_selected_commit(&pobj, &obj,
2280 e5a0f69f 2018-08-18 stsp s->blame.lines, s->first_displayed_line,
2281 e5a0f69f 2018-08-18 stsp s->selected_line, s->repo);
2284 e5a0f69f 2018-08-18 stsp if (pobj == NULL && obj == NULL)
2286 e5a0f69f 2018-08-18 stsp if (ch == 'p' && pobj == NULL)
2288 e5a0f69f 2018-08-18 stsp s->done = 1;
2289 e5a0f69f 2018-08-18 stsp if (pthread_mutex_unlock(&s->mutex) != 0) {
2290 e5a0f69f 2018-08-18 stsp err = got_error_from_errno();
2291 e5a0f69f 2018-08-18 stsp goto done;
2293 e5a0f69f 2018-08-18 stsp thread_err = stop_blame(&s->blame);
2294 e5a0f69f 2018-08-18 stsp s->done = 0;
2295 e5a0f69f 2018-08-18 stsp if (pthread_mutex_lock(&s->mutex) != 0) {
2296 e5a0f69f 2018-08-18 stsp err = got_error_from_errno();
2297 e5a0f69f 2018-08-18 stsp goto done;
2299 e5a0f69f 2018-08-18 stsp if (thread_err)
2301 e5a0f69f 2018-08-18 stsp id = got_object_get_id(ch == 'b' ? obj : pobj);
2302 e5a0f69f 2018-08-18 stsp got_object_close(obj);
2303 e5a0f69f 2018-08-18 stsp obj = NULL;
2304 e5a0f69f 2018-08-18 stsp if (pobj) {
2305 e5a0f69f 2018-08-18 stsp got_object_close(pobj);
2306 e5a0f69f 2018-08-18 stsp pobj = NULL;
2308 6402fb3c 2018-09-15 stsp err = got_object_qid_alloc(&s->blamed_commit, id);
2310 e5a0f69f 2018-08-18 stsp goto done;
2311 e5a0f69f 2018-08-18 stsp SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
2312 e5a0f69f 2018-08-18 stsp s->blamed_commit, entry);
2313 e5a0f69f 2018-08-18 stsp err = run_blame(&s->blame, &s->mutex, view,
2314 e5a0f69f 2018-08-18 stsp &s->blame_complete,
2315 e5a0f69f 2018-08-18 stsp &s->first_displayed_line,
2316 e5a0f69f 2018-08-18 stsp &s->last_displayed_line,
2317 e5a0f69f 2018-08-18 stsp &s->selected_line, &s->done, &s->eof,
2318 e5a0f69f 2018-08-18 stsp s->path, s->blamed_commit->id, s->repo);
2323 e5a0f69f 2018-08-18 stsp case 'B': {
2324 e5a0f69f 2018-08-18 stsp struct got_object_qid *first;
2325 e5a0f69f 2018-08-18 stsp first = SIMPLEQ_FIRST(&s->blamed_commits);
2326 e5a0f69f 2018-08-18 stsp if (!got_object_id_cmp(first->id, s->commit_id))
2328 e5a0f69f 2018-08-18 stsp s->done = 1;
2329 e5a0f69f 2018-08-18 stsp if (pthread_mutex_unlock(&s->mutex) != 0) {
2330 e5a0f69f 2018-08-18 stsp err = got_error_from_errno();
2331 e5a0f69f 2018-08-18 stsp goto done;
2333 e5a0f69f 2018-08-18 stsp thread_err = stop_blame(&s->blame);
2334 e5a0f69f 2018-08-18 stsp s->done = 0;
2335 e5a0f69f 2018-08-18 stsp if (pthread_mutex_lock(&s->mutex) != 0) {
2336 e5a0f69f 2018-08-18 stsp err = got_error_from_errno();
2337 e5a0f69f 2018-08-18 stsp goto done;
2339 e5a0f69f 2018-08-18 stsp if (thread_err)
2341 e5a0f69f 2018-08-18 stsp SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
2342 e5a0f69f 2018-08-18 stsp got_object_qid_free(s->blamed_commit);
2343 e5a0f69f 2018-08-18 stsp s->blamed_commit =
2344 e5a0f69f 2018-08-18 stsp SIMPLEQ_FIRST(&s->blamed_commits);
2345 e5a0f69f 2018-08-18 stsp err = run_blame(&s->blame, &s->mutex, view,
2346 e5a0f69f 2018-08-18 stsp &s->blame_complete,
2347 e5a0f69f 2018-08-18 stsp &s->first_displayed_line,
2348 e5a0f69f 2018-08-18 stsp &s->last_displayed_line,
2349 e5a0f69f 2018-08-18 stsp &s->selected_line, &s->done, &s->eof, s->path,
2350 e5a0f69f 2018-08-18 stsp s->blamed_commit->id, s->repo);
2355 e5a0f69f 2018-08-18 stsp case KEY_ENTER:
2356 e5a0f69f 2018-08-18 stsp case '\r':
2357 e5a0f69f 2018-08-18 stsp err = open_selected_commit(&pobj, &obj,
2358 e5a0f69f 2018-08-18 stsp s->blame.lines, s->first_displayed_line,
2359 e5a0f69f 2018-08-18 stsp s->selected_line, s->repo);
2362 e5a0f69f 2018-08-18 stsp if (pobj == NULL && obj == NULL)
2364 e5a0f69f 2018-08-18 stsp diff_view = view_open(0, 0, 0, 0, view,
2365 e5a0f69f 2018-08-18 stsp TOG_VIEW_DIFF);
2366 e5a0f69f 2018-08-18 stsp if (diff_view == NULL) {
2367 e5a0f69f 2018-08-18 stsp err = got_error_from_errno();
2370 e5a0f69f 2018-08-18 stsp err = open_diff_view(diff_view, pobj, obj,
2372 e5a0f69f 2018-08-18 stsp if (err) {
2373 e5a0f69f 2018-08-18 stsp view_close(diff_view);
2376 e5a0f69f 2018-08-18 stsp *new_view = diff_view;
2377 e5a0f69f 2018-08-18 stsp if (pobj) {
2378 e5a0f69f 2018-08-18 stsp got_object_close(pobj);
2379 e5a0f69f 2018-08-18 stsp pobj = NULL;
2381 e5a0f69f 2018-08-18 stsp got_object_close(obj);
2382 e5a0f69f 2018-08-18 stsp obj = NULL;
2386 e5a0f69f 2018-08-18 stsp case KEY_NPAGE:
2388 e5a0f69f 2018-08-18 stsp if (s->last_displayed_line >= s->blame.nlines &&
2389 e5a0f69f 2018-08-18 stsp s->selected_line < view->nlines - 2) {
2390 e5a0f69f 2018-08-18 stsp s->selected_line = MIN(s->blame.nlines,
2391 e5a0f69f 2018-08-18 stsp view->nlines - 2);
2394 e5a0f69f 2018-08-18 stsp if (s->last_displayed_line + view->nlines - 2
2395 e5a0f69f 2018-08-18 stsp <= s->blame.nlines)
2396 e5a0f69f 2018-08-18 stsp s->first_displayed_line +=
2397 e5a0f69f 2018-08-18 stsp view->nlines - 2;
2399 e5a0f69f 2018-08-18 stsp s->first_displayed_line =
2400 e5a0f69f 2018-08-18 stsp s->blame.nlines -
2401 e5a0f69f 2018-08-18 stsp (view->nlines - 3);
2403 e5a0f69f 2018-08-18 stsp case KEY_RESIZE:
2404 e5a0f69f 2018-08-18 stsp if (s->selected_line > view->nlines - 2) {
2405 e5a0f69f 2018-08-18 stsp s->selected_line = MIN(s->blame.nlines,
2406 e5a0f69f 2018-08-18 stsp view->nlines - 2);
2413 e5a0f69f 2018-08-18 stsp if (pthread_mutex_unlock(&s->mutex) != 0)
2414 e5a0f69f 2018-08-18 stsp err = got_error_from_errno();
2417 b880a918 2018-07-10 stsp got_object_close(pobj);
2418 245d91c1 2018-07-12 stsp return thread_err ? thread_err : err;
2421 a70480e0 2018-06-23 stsp static const struct got_error *
2422 9f7d7167 2018-04-29 stsp cmd_blame(int argc, char *argv[])
2424 a70480e0 2018-06-23 stsp const struct got_error *error;
2425 a70480e0 2018-06-23 stsp struct got_repository *repo = NULL;
2426 69069811 2018-08-02 stsp char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2427 a70480e0 2018-06-23 stsp struct got_object_id *commit_id = NULL;
2428 a70480e0 2018-06-23 stsp char *commit_id_str = NULL;
2430 e1cd8fed 2018-08-01 stsp struct tog_view *view;
2432 a70480e0 2018-06-23 stsp #ifndef PROFILE
2433 ad242220 2018-09-08 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd", NULL)
2435 a70480e0 2018-06-23 stsp err(1, "pledge");
2438 69069811 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2439 a70480e0 2018-06-23 stsp switch (ch) {
2441 a70480e0 2018-06-23 stsp commit_id_str = optarg;
2444 69069811 2018-08-02 stsp repo_path = realpath(optarg, NULL);
2445 69069811 2018-08-02 stsp if (repo_path == NULL)
2446 69069811 2018-08-02 stsp err(1, "-r option");
2450 a70480e0 2018-06-23 stsp /* NOTREACHED */
2454 a70480e0 2018-06-23 stsp argc -= optind;
2455 a70480e0 2018-06-23 stsp argv += optind;
2457 69069811 2018-08-02 stsp if (argc == 1)
2458 69069811 2018-08-02 stsp path = argv[0];
2460 a70480e0 2018-06-23 stsp usage_blame();
2462 69069811 2018-08-02 stsp cwd = getcwd(NULL, 0);
2463 69069811 2018-08-02 stsp if (cwd == NULL) {
2464 69069811 2018-08-02 stsp error = got_error_from_errno();
2465 69069811 2018-08-02 stsp goto done;
2467 69069811 2018-08-02 stsp if (repo_path == NULL) {
2468 69069811 2018-08-02 stsp repo_path = strdup(cwd);
2469 69069811 2018-08-02 stsp if (repo_path == NULL) {
2470 69069811 2018-08-02 stsp error = got_error_from_errno();
2471 69069811 2018-08-02 stsp goto done;
2476 69069811 2018-08-02 stsp error = got_repo_open(&repo, repo_path);
2477 a70480e0 2018-06-23 stsp if (error != NULL)
2478 66b4983c 2018-06-23 stsp return error;
2480 69069811 2018-08-02 stsp error = got_repo_map_path(&in_repo_path, repo, path);
2481 69069811 2018-08-02 stsp if (error != NULL)
2482 69069811 2018-08-02 stsp goto done;
2484 a70480e0 2018-06-23 stsp if (commit_id_str == NULL) {
2485 a70480e0 2018-06-23 stsp struct got_reference *head_ref;
2486 a70480e0 2018-06-23 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
2487 a70480e0 2018-06-23 stsp if (error != NULL)
2488 66b4983c 2018-06-23 stsp goto done;
2489 a70480e0 2018-06-23 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
2490 a70480e0 2018-06-23 stsp got_ref_close(head_ref);
2492 a70480e0 2018-06-23 stsp struct got_object *obj;
2493 a70480e0 2018-06-23 stsp error = got_object_open_by_id_str(&obj, repo, commit_id_str);
2494 a70480e0 2018-06-23 stsp if (error != NULL)
2495 66b4983c 2018-06-23 stsp goto done;
2496 6402fb3c 2018-09-15 stsp commit_id = got_object_id_dup(got_object_get_id(obj));
2497 a19e88aa 2018-06-23 stsp if (commit_id == NULL)
2498 66b4983c 2018-06-23 stsp error = got_error_from_errno();
2499 a19e88aa 2018-06-23 stsp got_object_close(obj);
2501 a19e88aa 2018-06-23 stsp if (error != NULL)
2502 a19e88aa 2018-06-23 stsp goto done;
2504 b3665f43 2018-08-04 stsp view = view_open(0, 0, 0, 0, NULL, TOG_VIEW_BLAME);
2505 e1cd8fed 2018-08-01 stsp if (view == NULL) {
2506 e1cd8fed 2018-08-01 stsp error = got_error_from_errno();
2507 e1cd8fed 2018-08-01 stsp goto done;
2509 7cbe629d 2018-08-04 stsp error = open_blame_view(view, in_repo_path, commit_id, repo);
2510 7cbe629d 2018-08-04 stsp if (error)
2511 7cbe629d 2018-08-04 stsp goto done;
2512 e5a0f69f 2018-08-18 stsp error = view_loop(view);
2514 69069811 2018-08-02 stsp free(repo_path);
2515 69069811 2018-08-02 stsp free(cwd);
2516 a70480e0 2018-06-23 stsp free(commit_id);
2518 a70480e0 2018-06-23 stsp got_repo_close(repo);
2519 a70480e0 2018-06-23 stsp return error;
2522 ffd1d5e5 2018-06-23 stsp static const struct got_error *
2523 f7d12f7e 2018-08-01 stsp draw_tree_entries(struct tog_view *view,
2524 f7d12f7e 2018-08-01 stsp struct got_tree_entry **first_displayed_entry,
2525 ffd1d5e5 2018-06-23 stsp struct got_tree_entry **last_displayed_entry,
2526 ffd1d5e5 2018-06-23 stsp struct got_tree_entry **selected_entry, int *ndisplayed,
2527 f7d12f7e 2018-08-01 stsp const char *label, int show_ids, const char *parent_path,
2528 ce52c690 2018-06-23 stsp const struct got_tree_entries *entries, int selected, int limit, int isroot)
2530 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
2531 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *te;
2532 ffd1d5e5 2018-06-23 stsp wchar_t *wline;
2533 ffd1d5e5 2018-06-23 stsp int width, n;
2535 ffd1d5e5 2018-06-23 stsp *ndisplayed = 0;
2537 f7d12f7e 2018-08-01 stsp werase(view->window);
2539 ffd1d5e5 2018-06-23 stsp if (limit == 0)
2540 ffd1d5e5 2018-06-23 stsp return NULL;
2542 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, label, view->ncols);
2544 ffd1d5e5 2018-06-23 stsp return err;
2545 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2546 a3404814 2018-09-02 stsp wstandout(view->window);
2547 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
2548 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2549 a3404814 2018-09-02 stsp wstandend(view->window);
2550 2550e4c3 2018-07-13 stsp free(wline);
2551 2550e4c3 2018-07-13 stsp wline = NULL;
2552 f7d12f7e 2018-08-01 stsp if (width < view->ncols)
2553 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
2554 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
2555 ffd1d5e5 2018-06-23 stsp return NULL;
2556 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, parent_path, view->ncols);
2558 ce52c690 2018-06-23 stsp return err;
2559 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
2560 2550e4c3 2018-07-13 stsp free(wline);
2561 2550e4c3 2018-07-13 stsp wline = NULL;
2562 f7d12f7e 2018-08-01 stsp if (width < view->ncols)
2563 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
2564 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
2565 ffd1d5e5 2018-06-23 stsp return NULL;
2566 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
2567 a1eca9bb 2018-06-23 stsp if (--limit <= 0)
2568 a1eca9bb 2018-06-23 stsp return NULL;
2570 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_FIRST(&entries->head);
2571 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry == NULL) {
2572 ffd1d5e5 2018-06-23 stsp if (selected == 0) {
2573 f7d12f7e 2018-08-01 stsp wstandout(view->window);
2574 ffd1d5e5 2018-06-23 stsp *selected_entry = NULL;
2576 f7d12f7e 2018-08-01 stsp waddstr(view->window, " ..\n"); /* parent directory */
2577 ffd1d5e5 2018-06-23 stsp if (selected == 0)
2578 f7d12f7e 2018-08-01 stsp wstandend(view->window);
2579 ffd1d5e5 2018-06-23 stsp (*ndisplayed)++;
2580 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
2581 ffd1d5e5 2018-06-23 stsp return NULL;
2585 ffd1d5e5 2018-06-23 stsp while (te != *first_displayed_entry)
2586 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_NEXT(te, entry);
2589 ffd1d5e5 2018-06-23 stsp while (te) {
2590 1d13200f 2018-07-12 stsp char *line = NULL, *id_str = NULL;
2592 1d13200f 2018-07-12 stsp if (show_ids) {
2593 1d13200f 2018-07-12 stsp err = got_object_id_str(&id_str, te->id);
2595 1d13200f 2018-07-12 stsp return got_error_from_errno();
2597 1d13200f 2018-07-12 stsp if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
2598 1d13200f 2018-07-12 stsp te->name, S_ISDIR(te->mode) ? "/" : "") == -1) {
2599 1d13200f 2018-07-12 stsp free(id_str);
2600 ffd1d5e5 2018-06-23 stsp return got_error_from_errno();
2602 1d13200f 2018-07-12 stsp free(id_str);
2603 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, line, view->ncols);
2604 ffd1d5e5 2018-06-23 stsp if (err) {
2605 ffd1d5e5 2018-06-23 stsp free(line);
2608 ffd1d5e5 2018-06-23 stsp if (n == selected) {
2609 f7d12f7e 2018-08-01 stsp wstandout(view->window);
2610 ffd1d5e5 2018-06-23 stsp *selected_entry = te;
2612 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
2613 f7d12f7e 2018-08-01 stsp if (width < view->ncols)
2614 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
2615 ffd1d5e5 2018-06-23 stsp if (n == selected)
2616 f7d12f7e 2018-08-01 stsp wstandend(view->window);
2617 ffd1d5e5 2018-06-23 stsp free(line);
2618 2550e4c3 2018-07-13 stsp free(wline);
2619 2550e4c3 2018-07-13 stsp wline = NULL;
2621 ffd1d5e5 2018-06-23 stsp (*ndisplayed)++;
2622 ffd1d5e5 2018-06-23 stsp *last_displayed_entry = te;
2623 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
2625 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_NEXT(te, entry);
2628 1a57306a 2018-09-02 stsp view_vborder(view);
2629 ffd1d5e5 2018-06-23 stsp return err;
2632 ffd1d5e5 2018-06-23 stsp static void
2633 ffd1d5e5 2018-06-23 stsp tree_scroll_up(struct got_tree_entry **first_displayed_entry, int maxscroll,
2634 ffd1d5e5 2018-06-23 stsp const struct got_tree_entries *entries, int isroot)
2636 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *te, *prev;
2639 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry == NULL)
2642 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_FIRST(&entries->head);
2643 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry == te) {
2644 ffd1d5e5 2018-06-23 stsp if (!isroot)
2645 ffd1d5e5 2018-06-23 stsp *first_displayed_entry = NULL;
2649 ffd1d5e5 2018-06-23 stsp /* XXX this is stupid... switch to TAILQ? */
2650 ffd1d5e5 2018-06-23 stsp for (i = 0; i < maxscroll; i++) {
2651 ffd1d5e5 2018-06-23 stsp while (te != *first_displayed_entry) {
2652 ffd1d5e5 2018-06-23 stsp prev = te;
2653 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_NEXT(te, entry);
2655 ffd1d5e5 2018-06-23 stsp *first_displayed_entry = prev;
2656 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_FIRST(&entries->head);
2658 ffd1d5e5 2018-06-23 stsp if (!isroot && te == SIMPLEQ_FIRST(&entries->head) && i < maxscroll)
2659 ffd1d5e5 2018-06-23 stsp *first_displayed_entry = NULL;
2662 ffd1d5e5 2018-06-23 stsp static void
2663 ffd1d5e5 2018-06-23 stsp tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
2664 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *last_displayed_entry,
2665 ffd1d5e5 2018-06-23 stsp const struct got_tree_entries *entries)
2667 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *next;
2668 ffd1d5e5 2018-06-23 stsp int n = 0;
2670 ffd1d5e5 2018-06-23 stsp if (SIMPLEQ_NEXT(last_displayed_entry, entry) == NULL)
2673 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry)
2674 ffd1d5e5 2018-06-23 stsp next = SIMPLEQ_NEXT(*first_displayed_entry, entry);
2676 ffd1d5e5 2018-06-23 stsp next = SIMPLEQ_FIRST(&entries->head);
2677 ffd1d5e5 2018-06-23 stsp while (next) {
2678 ffd1d5e5 2018-06-23 stsp *first_displayed_entry = next;
2679 ffd1d5e5 2018-06-23 stsp if (++n >= maxscroll)
2681 ffd1d5e5 2018-06-23 stsp next = SIMPLEQ_NEXT(next, entry);
2685 ffd1d5e5 2018-06-23 stsp static const struct got_error *
2686 ce52c690 2018-06-23 stsp tree_entry_path(char **path, struct tog_parent_trees *parents,
2687 ce52c690 2018-06-23 stsp struct got_tree_entry *te)
2689 cb2ebc8a 2018-06-23 stsp const struct got_error *err = NULL;
2690 ffd1d5e5 2018-06-23 stsp struct tog_parent_tree *pt;
2691 ffd1d5e5 2018-06-23 stsp size_t len = 2; /* for leading slash and NUL */
2693 d9765a41 2018-06-23 stsp TAILQ_FOREACH(pt, parents, entry)
2694 ffd1d5e5 2018-06-23 stsp len += strlen(pt->selected_entry->name) + 1 /* slash */;
2696 ce52c690 2018-06-23 stsp len += strlen(te->name);
2698 ce52c690 2018-06-23 stsp *path = calloc(1, len);
2699 ffd1d5e5 2018-06-23 stsp if (path == NULL)
2700 ffd1d5e5 2018-06-23 stsp return got_error_from_errno();
2702 ce52c690 2018-06-23 stsp (*path)[0] = '/';
2703 d9765a41 2018-06-23 stsp pt = TAILQ_LAST(parents, tog_parent_trees);
2704 d9765a41 2018-06-23 stsp while (pt) {
2705 ce52c690 2018-06-23 stsp if (strlcat(*path, pt->selected_entry->name, len) >= len) {
2706 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
2707 cb2ebc8a 2018-06-23 stsp goto done;
2709 ce52c690 2018-06-23 stsp if (strlcat(*path, "/", len) >= len) {
2710 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
2711 cb2ebc8a 2018-06-23 stsp goto done;
2713 d9765a41 2018-06-23 stsp pt = TAILQ_PREV(pt, tog_parent_trees, entry);
2716 ce52c690 2018-06-23 stsp if (strlcat(*path, te->name, len) >= len) {
2717 ce52c690 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
2718 ce52c690 2018-06-23 stsp goto done;
2722 ce52c690 2018-06-23 stsp if (err) {
2723 ce52c690 2018-06-23 stsp free(*path);
2724 ce52c690 2018-06-23 stsp *path = NULL;
2726 ce52c690 2018-06-23 stsp return err;
2729 ce52c690 2018-06-23 stsp static const struct got_error *
2730 e5a0f69f 2018-08-18 stsp blame_tree_entry(struct tog_view **new_view, struct tog_view *parent_view,
2731 e5a0f69f 2018-08-18 stsp struct got_tree_entry *te, struct tog_parent_trees *parents,
2732 e5a0f69f 2018-08-18 stsp struct got_object_id *commit_id, struct got_repository *repo)
2734 ce52c690 2018-06-23 stsp const struct got_error *err = NULL;
2735 ce52c690 2018-06-23 stsp char *path;
2736 e5a0f69f 2018-08-18 stsp struct tog_view *blame_view;
2738 ce52c690 2018-06-23 stsp err = tree_entry_path(&path, parents, te);
2740 ce52c690 2018-06-23 stsp return err;
2742 e5a0f69f 2018-08-18 stsp blame_view = view_open(0, 0, 0, 0, parent_view, TOG_VIEW_BLAME);
2743 e5a0f69f 2018-08-18 stsp if (blame_view == NULL)
2744 e5a0f69f 2018-08-18 stsp return got_error_from_errno();
2746 e5a0f69f 2018-08-18 stsp err = open_blame_view(blame_view, path, commit_id, repo);
2747 e5a0f69f 2018-08-18 stsp if (err) {
2748 e5a0f69f 2018-08-18 stsp view_close(blame_view);
2749 e5a0f69f 2018-08-18 stsp free(path);
2751 e5a0f69f 2018-08-18 stsp *new_view = blame_view;
2752 69efd4c4 2018-07-18 stsp return err;
2755 69efd4c4 2018-07-18 stsp static const struct got_error *
2756 e5a0f69f 2018-08-18 stsp log_tree_entry(struct tog_view **new_view, struct tog_view *parent_view,
2757 e5a0f69f 2018-08-18 stsp struct got_tree_entry *te, struct tog_parent_trees *parents,
2758 e5a0f69f 2018-08-18 stsp struct got_object_id *commit_id, struct got_repository *repo)
2760 e5a0f69f 2018-08-18 stsp struct tog_view *log_view;
2761 69efd4c4 2018-07-18 stsp const struct got_error *err = NULL;
2762 69efd4c4 2018-07-18 stsp char *path;
2764 e5a0f69f 2018-08-18 stsp log_view = view_open(0, 0, 0, 0, parent_view, TOG_VIEW_LOG);
2765 e5a0f69f 2018-08-18 stsp if (log_view == NULL)
2766 e5a0f69f 2018-08-18 stsp return got_error_from_errno();
2768 69efd4c4 2018-07-18 stsp err = tree_entry_path(&path, parents, te);
2770 69efd4c4 2018-07-18 stsp return err;
2772 e5a0f69f 2018-08-18 stsp err = open_log_view(log_view, commit_id, repo, path);
2774 e5a0f69f 2018-08-18 stsp view_close(log_view);
2776 e5a0f69f 2018-08-18 stsp *new_view = log_view;
2777 cb2ebc8a 2018-06-23 stsp free(path);
2778 cb2ebc8a 2018-06-23 stsp return err;
2781 ffd1d5e5 2018-06-23 stsp static const struct got_error *
2782 ad80ab7b 2018-08-04 stsp open_tree_view(struct tog_view *view, struct got_tree_object *root,
2783 5221c383 2018-08-01 stsp struct got_object_id *commit_id, struct got_repository *repo)
2785 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
2786 ad80ab7b 2018-08-04 stsp char *commit_id_str = NULL;
2787 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
2789 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->parents);
2791 ffd1d5e5 2018-06-23 stsp err = got_object_id_str(&commit_id_str, commit_id);
2792 ffd1d5e5 2018-06-23 stsp if (err != NULL)
2793 ffd1d5e5 2018-06-23 stsp goto done;
2795 fb2756b9 2018-08-04 stsp if (asprintf(&s->tree_label, "commit: %s", commit_id_str) == -1) {
2796 ffd1d5e5 2018-06-23 stsp err = got_error_from_errno();
2797 ffd1d5e5 2018-06-23 stsp goto done;
2800 fb2756b9 2018-08-04 stsp s->root = s->tree = root;
2801 fb2756b9 2018-08-04 stsp s->entries = got_object_tree_get_entries(root);
2802 fb2756b9 2018-08-04 stsp s->first_displayed_entry = SIMPLEQ_FIRST(&s->entries->head);
2803 6484ec90 2018-09-29 stsp s->commit_id = got_object_id_dup(commit_id);
2804 6484ec90 2018-09-29 stsp if (s->commit_id == NULL) {
2805 6484ec90 2018-09-29 stsp err = got_error_from_errno();
2806 6484ec90 2018-09-29 stsp goto done;
2808 fb2756b9 2018-08-04 stsp s->repo = repo;
2810 e5a0f69f 2018-08-18 stsp view->show = show_tree_view;
2811 e5a0f69f 2018-08-18 stsp view->input = input_tree_view;
2812 e5a0f69f 2018-08-18 stsp view->close = close_tree_view;
2814 ad80ab7b 2018-08-04 stsp free(commit_id_str);
2815 6484ec90 2018-09-29 stsp if (err) {
2816 fb2756b9 2018-08-04 stsp free(s->tree_label);
2817 6484ec90 2018-09-29 stsp s->tree_label = NULL;
2819 ad80ab7b 2018-08-04 stsp return err;
2822 e5a0f69f 2018-08-18 stsp static const struct got_error *
2823 ad80ab7b 2018-08-04 stsp close_tree_view(struct tog_view *view)
2825 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
2827 fb2756b9 2018-08-04 stsp free(s->tree_label);
2828 6484ec90 2018-09-29 stsp s->tree_label = NULL;
2829 6484ec90 2018-09-29 stsp free(s->commit_id);
2830 6484ec90 2018-09-29 stsp s->commit_id = NULL;
2831 fb2756b9 2018-08-04 stsp while (!TAILQ_EMPTY(&s->parents)) {
2832 ad80ab7b 2018-08-04 stsp struct tog_parent_tree *parent;
2833 fb2756b9 2018-08-04 stsp parent = TAILQ_FIRST(&s->parents);
2834 fb2756b9 2018-08-04 stsp TAILQ_REMOVE(&s->parents, parent, entry);
2835 ad80ab7b 2018-08-04 stsp free(parent);
2838 fb2756b9 2018-08-04 stsp if (s->tree != s->root)
2839 fb2756b9 2018-08-04 stsp got_object_tree_close(s->tree);
2840 e5a0f69f 2018-08-18 stsp got_object_tree_close(s->root);
2842 e5a0f69f 2018-08-18 stsp return NULL;
2845 ad80ab7b 2018-08-04 stsp static const struct got_error *
2846 ad80ab7b 2018-08-04 stsp show_tree_view(struct tog_view *view)
2848 ad80ab7b 2018-08-04 stsp const struct got_error *err = NULL;
2849 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
2850 e5a0f69f 2018-08-18 stsp char *parent_path;
2852 e5a0f69f 2018-08-18 stsp err = tree_entry_path(&parent_path, &s->parents, NULL);
2854 e5a0f69f 2018-08-18 stsp return err;
2856 e5a0f69f 2018-08-18 stsp err = draw_tree_entries(view, &s->first_displayed_entry,
2857 e5a0f69f 2018-08-18 stsp &s->last_displayed_entry, &s->selected_entry,
2858 e5a0f69f 2018-08-18 stsp &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
2859 e5a0f69f 2018-08-18 stsp s->entries, s->selected, view->nlines, s->tree == s->root);
2860 e5a0f69f 2018-08-18 stsp free(parent_path);
2861 e5a0f69f 2018-08-18 stsp return err;
2864 e5a0f69f 2018-08-18 stsp static const struct got_error *
2865 e5a0f69f 2018-08-18 stsp input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
2866 e5a0f69f 2018-08-18 stsp struct tog_view *view, int ch)
2868 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
2869 e5a0f69f 2018-08-18 stsp struct tog_tree_view_state *s = &view->state.tree;
2871 e5a0f69f 2018-08-18 stsp switch (ch) {
2873 e5a0f69f 2018-08-18 stsp s->show_ids = !s->show_ids;
2876 e5a0f69f 2018-08-18 stsp if (s->selected_entry) {
2877 e5a0f69f 2018-08-18 stsp err = log_tree_entry(new_view, view,
2878 e5a0f69f 2018-08-18 stsp s->selected_entry, &s->parents,
2879 e5a0f69f 2018-08-18 stsp s->commit_id, s->repo);
2883 e5a0f69f 2018-08-18 stsp case KEY_UP:
2884 e5a0f69f 2018-08-18 stsp if (s->selected > 0)
2885 e5a0f69f 2018-08-18 stsp s->selected--;
2886 e5a0f69f 2018-08-18 stsp if (s->selected > 0)
2888 e5a0f69f 2018-08-18 stsp tree_scroll_up(&s->first_displayed_entry, 1,
2889 e5a0f69f 2018-08-18 stsp s->entries, s->tree == s->root);
2891 e5a0f69f 2018-08-18 stsp case KEY_PPAGE:
2892 e5a0f69f 2018-08-18 stsp if (SIMPLEQ_FIRST(&s->entries->head) ==
2893 e5a0f69f 2018-08-18 stsp s->first_displayed_entry) {
2894 e5a0f69f 2018-08-18 stsp if (s->tree != s->root)
2895 e5a0f69f 2018-08-18 stsp s->first_displayed_entry = NULL;
2896 e5a0f69f 2018-08-18 stsp s->selected = 0;
2899 e5a0f69f 2018-08-18 stsp tree_scroll_up(&s->first_displayed_entry,
2900 e5a0f69f 2018-08-18 stsp view->nlines, s->entries,
2901 e5a0f69f 2018-08-18 stsp s->tree == s->root);
2904 e5a0f69f 2018-08-18 stsp case KEY_DOWN:
2905 e5a0f69f 2018-08-18 stsp if (s->selected < s->ndisplayed - 1) {
2906 e5a0f69f 2018-08-18 stsp s->selected++;
2909 e5a0f69f 2018-08-18 stsp tree_scroll_down(&s->first_displayed_entry, 1,
2910 e5a0f69f 2018-08-18 stsp s->last_displayed_entry, s->entries);
2912 e5a0f69f 2018-08-18 stsp case KEY_NPAGE:
2913 e5a0f69f 2018-08-18 stsp tree_scroll_down(&s->first_displayed_entry,
2914 e5a0f69f 2018-08-18 stsp view->nlines, s->last_displayed_entry,
2915 e5a0f69f 2018-08-18 stsp s->entries);
2916 e5a0f69f 2018-08-18 stsp if (SIMPLEQ_NEXT(s->last_displayed_entry,
2919 e5a0f69f 2018-08-18 stsp /* can't scroll any further; move cursor down */
2920 e5a0f69f 2018-08-18 stsp if (s->selected < s->ndisplayed - 1)
2921 e5a0f69f 2018-08-18 stsp s->selected = s->ndisplayed - 1;
2923 e5a0f69f 2018-08-18 stsp case KEY_ENTER:
2924 e5a0f69f 2018-08-18 stsp case '\r':
2925 e5a0f69f 2018-08-18 stsp if (s->selected_entry == NULL) {
2926 e5a0f69f 2018-08-18 stsp struct tog_parent_tree *parent;
2927 7837eeac 2018-09-24 stsp case KEY_BACKSPACE:
2928 e5a0f69f 2018-08-18 stsp /* user selected '..' */
2929 e5a0f69f 2018-08-18 stsp if (s->tree == s->root)
2931 e5a0f69f 2018-08-18 stsp parent = TAILQ_FIRST(&s->parents);
2932 e5a0f69f 2018-08-18 stsp TAILQ_REMOVE(&s->parents, parent,
2934 e5a0f69f 2018-08-18 stsp got_object_tree_close(s->tree);
2935 e5a0f69f 2018-08-18 stsp s->tree = parent->tree;
2936 e5a0f69f 2018-08-18 stsp s->entries =
2937 e5a0f69f 2018-08-18 stsp got_object_tree_get_entries(s->tree);
2938 e5a0f69f 2018-08-18 stsp s->first_displayed_entry =
2939 e5a0f69f 2018-08-18 stsp parent->first_displayed_entry;
2940 e5a0f69f 2018-08-18 stsp s->selected_entry =
2941 e5a0f69f 2018-08-18 stsp parent->selected_entry;
2942 e5a0f69f 2018-08-18 stsp s->selected = parent->selected;
2943 e5a0f69f 2018-08-18 stsp free(parent);
2944 e5a0f69f 2018-08-18 stsp } else if (S_ISDIR(s->selected_entry->mode)) {
2945 e5a0f69f 2018-08-18 stsp struct tog_parent_tree *parent;
2946 e5a0f69f 2018-08-18 stsp struct got_tree_object *child;
2947 e5a0f69f 2018-08-18 stsp err = got_object_open_as_tree(&child,
2948 e5a0f69f 2018-08-18 stsp s->repo, s->selected_entry->id);
2951 e5a0f69f 2018-08-18 stsp parent = calloc(1, sizeof(*parent));
2952 e5a0f69f 2018-08-18 stsp if (parent == NULL) {
2953 e5a0f69f 2018-08-18 stsp err = got_error_from_errno();
2956 e5a0f69f 2018-08-18 stsp parent->tree = s->tree;
2957 e5a0f69f 2018-08-18 stsp parent->first_displayed_entry =
2958 e5a0f69f 2018-08-18 stsp s->first_displayed_entry;
2959 e5a0f69f 2018-08-18 stsp parent->selected_entry = s->selected_entry;
2960 e5a0f69f 2018-08-18 stsp parent->selected = s->selected;
2961 e5a0f69f 2018-08-18 stsp TAILQ_INSERT_HEAD(&s->parents, parent, entry);
2962 e5a0f69f 2018-08-18 stsp s->tree = child;
2963 e5a0f69f 2018-08-18 stsp s->entries =
2964 e5a0f69f 2018-08-18 stsp got_object_tree_get_entries(s->tree);
2965 e5a0f69f 2018-08-18 stsp s->selected = 0;
2966 e5a0f69f 2018-08-18 stsp s->first_displayed_entry = NULL;
2967 e5a0f69f 2018-08-18 stsp } else if (S_ISREG(s->selected_entry->mode)) {
2968 e5a0f69f 2018-08-18 stsp err = blame_tree_entry(new_view, view,
2969 e5a0f69f 2018-08-18 stsp s->selected_entry, &s->parents,
2970 e5a0f69f 2018-08-18 stsp s->commit_id, s->repo);
2975 e5a0f69f 2018-08-18 stsp case KEY_RESIZE:
2976 e5a0f69f 2018-08-18 stsp if (s->selected > view->nlines)
2977 e5a0f69f 2018-08-18 stsp s->selected = s->ndisplayed - 1;
2983 ffd1d5e5 2018-06-23 stsp return err;
2986 ffd1d5e5 2018-06-23 stsp __dead static void
2987 ffd1d5e5 2018-06-23 stsp usage_tree(void)
2990 ffd1d5e5 2018-06-23 stsp fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
2991 ffd1d5e5 2018-06-23 stsp getprogname());
2995 ffd1d5e5 2018-06-23 stsp static const struct got_error *
2996 ffd1d5e5 2018-06-23 stsp cmd_tree(int argc, char *argv[])
2998 ffd1d5e5 2018-06-23 stsp const struct got_error *error;
2999 ffd1d5e5 2018-06-23 stsp struct got_repository *repo = NULL;
3000 ffd1d5e5 2018-06-23 stsp char *repo_path = NULL;
3001 ffd1d5e5 2018-06-23 stsp struct got_object_id *commit_id = NULL;
3002 ffd1d5e5 2018-06-23 stsp char *commit_id_arg = NULL;
3003 ffd1d5e5 2018-06-23 stsp struct got_commit_object *commit = NULL;
3004 ffd1d5e5 2018-06-23 stsp struct got_tree_object *tree = NULL;
3006 5221c383 2018-08-01 stsp struct tog_view *view;
3008 ffd1d5e5 2018-06-23 stsp #ifndef PROFILE
3009 ad242220 2018-09-08 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd", NULL)
3011 ffd1d5e5 2018-06-23 stsp err(1, "pledge");
3014 ffd1d5e5 2018-06-23 stsp while ((ch = getopt(argc, argv, "c:")) != -1) {
3015 ffd1d5e5 2018-06-23 stsp switch (ch) {
3017 ffd1d5e5 2018-06-23 stsp commit_id_arg = optarg;
3021 ffd1d5e5 2018-06-23 stsp /* NOTREACHED */
3025 ffd1d5e5 2018-06-23 stsp argc -= optind;
3026 ffd1d5e5 2018-06-23 stsp argv += optind;
3028 ffd1d5e5 2018-06-23 stsp if (argc == 0) {
3029 ffd1d5e5 2018-06-23 stsp repo_path = getcwd(NULL, 0);
3030 ffd1d5e5 2018-06-23 stsp if (repo_path == NULL)
3031 ffd1d5e5 2018-06-23 stsp return got_error_from_errno();
3032 ffd1d5e5 2018-06-23 stsp } else if (argc == 1) {
3033 ffd1d5e5 2018-06-23 stsp repo_path = realpath(argv[0], NULL);
3034 ffd1d5e5 2018-06-23 stsp if (repo_path == NULL)
3035 ffd1d5e5 2018-06-23 stsp return got_error_from_errno();
3037 ffd1d5e5 2018-06-23 stsp usage_log();
3039 ffd1d5e5 2018-06-23 stsp error = got_repo_open(&repo, repo_path);
3040 ffd1d5e5 2018-06-23 stsp free(repo_path);
3041 ffd1d5e5 2018-06-23 stsp if (error != NULL)
3042 ffd1d5e5 2018-06-23 stsp return error;
3044 ffd1d5e5 2018-06-23 stsp if (commit_id_arg == NULL) {
3045 ffd1d5e5 2018-06-23 stsp error = get_head_commit_id(&commit_id, repo);
3046 ffd1d5e5 2018-06-23 stsp if (error != NULL)
3047 ffd1d5e5 2018-06-23 stsp goto done;
3049 ffd1d5e5 2018-06-23 stsp struct got_object *obj;
3050 ffd1d5e5 2018-06-23 stsp error = got_object_open_by_id_str(&obj, repo, commit_id_arg);
3051 ffd1d5e5 2018-06-23 stsp if (error == NULL) {
3052 6402fb3c 2018-09-15 stsp commit_id = got_object_id_dup(got_object_get_id(obj));
3053 ffd1d5e5 2018-06-23 stsp if (commit_id == NULL)
3054 ffd1d5e5 2018-06-23 stsp error = got_error_from_errno();
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_commit(&commit, repo, commit_id);
3061 ffd1d5e5 2018-06-23 stsp if (error != NULL)
3062 ffd1d5e5 2018-06-23 stsp goto done;
3064 ffd1d5e5 2018-06-23 stsp error = got_object_open_as_tree(&tree, repo, commit->tree_id);
3065 ffd1d5e5 2018-06-23 stsp if (error != NULL)
3066 ffd1d5e5 2018-06-23 stsp goto done;
3068 b3665f43 2018-08-04 stsp view = view_open(0, 0, 0, 0, NULL, TOG_VIEW_TREE);
3069 5221c383 2018-08-01 stsp if (view == NULL) {
3070 5221c383 2018-08-01 stsp error = got_error_from_errno();
3071 5221c383 2018-08-01 stsp goto done;
3073 ad80ab7b 2018-08-04 stsp error = open_tree_view(view, tree, commit_id, repo);
3074 ad80ab7b 2018-08-04 stsp if (error)
3075 ad80ab7b 2018-08-04 stsp goto done;
3076 e5a0f69f 2018-08-18 stsp error = view_loop(view);
3078 ffd1d5e5 2018-06-23 stsp free(commit_id);
3079 ffd1d5e5 2018-06-23 stsp if (commit)
3080 ffd1d5e5 2018-06-23 stsp got_object_commit_close(commit);
3082 ffd1d5e5 2018-06-23 stsp got_object_tree_close(tree);
3084 ffd1d5e5 2018-06-23 stsp got_repo_close(repo);
3085 ffd1d5e5 2018-06-23 stsp return error;
3087 5c5136c5 2018-05-20 stsp static void
3088 9f7d7167 2018-04-29 stsp init_curses(void)
3090 9f7d7167 2018-04-29 stsp initscr();
3094 9f7d7167 2018-04-29 stsp intrflush(stdscr, FALSE);
3095 9f7d7167 2018-04-29 stsp keypad(stdscr, TRUE);
3096 1f475ad8 2018-05-10 stsp curs_set(0);
3099 4ed7e80c 2018-05-20 stsp __dead static void
3100 9f7d7167 2018-04-29 stsp usage(void)
3104 c2301be8 2018-04-30 stsp fprintf(stderr, "usage: %s [-h] [command] [arg ...]\n\n"
3105 9f7d7167 2018-04-29 stsp "Available commands:\n", getprogname());
3106 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
3107 9f7d7167 2018-04-29 stsp struct tog_cmd *cmd = &tog_commands[i];
3108 c2301be8 2018-04-30 stsp fprintf(stderr, " %s: %s\n", cmd->name, cmd->descr);
3113 c2301be8 2018-04-30 stsp static char **
3114 c2301be8 2018-04-30 stsp make_argv(const char *arg0, const char *arg1)
3116 c2301be8 2018-04-30 stsp char **argv;
3117 c2301be8 2018-04-30 stsp int argc = (arg1 == NULL ? 1 : 2);
3119 c2301be8 2018-04-30 stsp argv = calloc(argc, sizeof(char *));
3120 c2301be8 2018-04-30 stsp if (argv == NULL)
3121 c2301be8 2018-04-30 stsp err(1, "calloc");
3122 c2301be8 2018-04-30 stsp argv[0] = strdup(arg0);
3123 c2301be8 2018-04-30 stsp if (argv[0] == NULL)
3124 c2301be8 2018-04-30 stsp err(1, "calloc");
3125 c2301be8 2018-04-30 stsp if (arg1) {
3126 c2301be8 2018-04-30 stsp argv[1] = strdup(arg1);
3127 c2301be8 2018-04-30 stsp if (argv[1] == NULL)
3128 c2301be8 2018-04-30 stsp err(1, "calloc");
3131 c2301be8 2018-04-30 stsp return argv;
3135 9f7d7167 2018-04-29 stsp main(int argc, char *argv[])
3137 9f7d7167 2018-04-29 stsp const struct got_error *error = NULL;
3138 9f7d7167 2018-04-29 stsp struct tog_cmd *cmd = NULL;
3139 9f7d7167 2018-04-29 stsp int ch, hflag = 0;
3140 c2301be8 2018-04-30 stsp char **cmd_argv = NULL;
3142 9f7d7167 2018-04-29 stsp setlocale(LC_ALL, "");
3144 9f7d7167 2018-04-29 stsp while ((ch = getopt(argc, argv, "h")) != -1) {
3145 9f7d7167 2018-04-29 stsp switch (ch) {
3147 9f7d7167 2018-04-29 stsp hflag = 1;
3151 9f7d7167 2018-04-29 stsp /* NOTREACHED */
3155 9f7d7167 2018-04-29 stsp argc -= optind;
3156 9f7d7167 2018-04-29 stsp argv += optind;
3157 9f7d7167 2018-04-29 stsp optind = 0;
3158 c2301be8 2018-04-30 stsp optreset = 1;
3160 c2301be8 2018-04-30 stsp if (argc == 0) {
3161 f29d3e89 2018-06-23 stsp if (hflag)
3163 c2301be8 2018-04-30 stsp /* Build an argument vector which runs a default command. */
3164 9f7d7167 2018-04-29 stsp cmd = &tog_commands[0];
3165 c2301be8 2018-04-30 stsp cmd_argv = make_argv(cmd->name, NULL);
3170 c2301be8 2018-04-30 stsp /* Did the user specific a command? */
3171 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
3172 c2301be8 2018-04-30 stsp if (strncmp(tog_commands[i].name, argv[0],
3173 9f7d7167 2018-04-29 stsp strlen(argv[0])) == 0) {
3174 9f7d7167 2018-04-29 stsp cmd = &tog_commands[i];
3175 9f7d7167 2018-04-29 stsp if (hflag)
3176 9f7d7167 2018-04-29 stsp tog_commands[i].cmd_usage();
3180 9f7d7167 2018-04-29 stsp if (cmd == NULL) {
3181 c2301be8 2018-04-30 stsp /* Did the user specify a repository? */
3182 c2301be8 2018-04-30 stsp char *repo_path = realpath(argv[0], NULL);
3183 c2301be8 2018-04-30 stsp if (repo_path) {
3184 c2301be8 2018-04-30 stsp struct got_repository *repo;
3185 c2301be8 2018-04-30 stsp error = got_repo_open(&repo, repo_path);
3186 c2301be8 2018-04-30 stsp if (error == NULL)
3187 c2301be8 2018-04-30 stsp got_repo_close(repo);
3189 ad7de8d9 2018-04-30 stsp error = got_error_from_errno();
3190 c2301be8 2018-04-30 stsp if (error) {
3191 f29d3e89 2018-06-23 stsp if (hflag) {
3192 f29d3e89 2018-06-23 stsp fprintf(stderr, "%s: '%s' is not a "
3193 f29d3e89 2018-06-23 stsp "known command\n", getprogname(),
3197 ad7de8d9 2018-04-30 stsp fprintf(stderr, "%s: '%s' is neither a known "
3198 ad7de8d9 2018-04-30 stsp "command nor a path to a repository\n",
3199 c2301be8 2018-04-30 stsp getprogname(), argv[0]);
3200 ad7de8d9 2018-04-30 stsp free(repo_path);
3203 c2301be8 2018-04-30 stsp cmd = &tog_commands[0];
3204 c2301be8 2018-04-30 stsp cmd_argv = make_argv(cmd->name, repo_path);
3206 c2301be8 2018-04-30 stsp free(repo_path);
3210 5c5136c5 2018-05-20 stsp init_curses();
3212 c2301be8 2018-04-30 stsp error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
3213 9f7d7167 2018-04-29 stsp if (error)
3214 9f7d7167 2018-04-29 stsp goto done;
3217 c2301be8 2018-04-30 stsp free(cmd_argv);
3218 9f7d7167 2018-04-29 stsp if (error)
3219 9f7d7167 2018-04-29 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);