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 0cf4efb1 2018-09-29 stsp const struct got_error *(*update_siblings)(int *, struct tog_view *,
220 0cf4efb1 2018-09-29 stsp struct tog_view*);
221 e5a0f69f 2018-08-18 stsp const struct got_error *(*input)(struct tog_view **,
222 e5a0f69f 2018-08-18 stsp struct tog_view **, struct tog_view *, int);
223 bcbd79e2 2018-08-19 stsp const struct got_error *(*set_child)(struct tog_view *,
224 bcbd79e2 2018-08-19 stsp struct tog_view *);
225 e5a0f69f 2018-08-18 stsp const struct got_error *(*close)(struct tog_view *);
228 ba4f502b 2018-08-04 stsp static const struct got_error *open_diff_view(struct tog_view *,
229 ba4f502b 2018-08-04 stsp struct got_object *, struct got_object *, struct got_repository *);
230 5dc9f4bc 2018-08-04 stsp static const struct got_error *show_diff_view(struct tog_view *);
231 0cf4efb1 2018-09-29 stsp static const struct got_error *update_siblings_diff_view(int *,
232 0cf4efb1 2018-09-29 stsp struct tog_view *, struct tog_view *);
233 e5a0f69f 2018-08-18 stsp static const struct got_error *input_diff_view(struct tog_view **,
234 e5a0f69f 2018-08-18 stsp struct tog_view **, struct tog_view *, int);
235 e5a0f69f 2018-08-18 stsp static const struct got_error* close_diff_view(struct tog_view *);
237 ba4f502b 2018-08-04 stsp static const struct got_error *open_log_view(struct tog_view *,
238 ba4f502b 2018-08-04 stsp struct got_object_id *, struct got_repository *, const char *);
239 ba4f502b 2018-08-04 stsp static const struct got_error * show_log_view(struct tog_view *);
240 0cf4efb1 2018-09-29 stsp static const struct got_error *update_siblings_log_view(int *,
241 0cf4efb1 2018-09-29 stsp struct tog_view *, struct tog_view *);
242 e5a0f69f 2018-08-18 stsp static const struct got_error *input_log_view(struct tog_view **,
243 e5a0f69f 2018-08-18 stsp struct tog_view **, struct tog_view *, int);
244 e5a0f69f 2018-08-18 stsp static const struct got_error *close_log_view(struct tog_view *);
245 bcbd79e2 2018-08-19 stsp static const struct got_error* set_child_log_view(struct tog_view *,
246 bcbd79e2 2018-08-19 stsp struct tog_view *);
248 e5a0f69f 2018-08-18 stsp static const struct got_error *open_blame_view(struct tog_view *, char *,
249 5221c383 2018-08-01 stsp struct got_object_id *, struct got_repository *);
250 7cbe629d 2018-08-04 stsp static const struct got_error *show_blame_view(struct tog_view *);
251 0cf4efb1 2018-09-29 stsp static const struct got_error *update_siblings_blame_view(int *,
252 0cf4efb1 2018-09-29 stsp struct tog_view *, struct tog_view *);
253 e5a0f69f 2018-08-18 stsp static const struct got_error *input_blame_view(struct tog_view **,
254 e5a0f69f 2018-08-18 stsp struct tog_view **, struct tog_view *, int);
255 e5a0f69f 2018-08-18 stsp static const struct got_error *close_blame_view(struct tog_view *);
257 ad80ab7b 2018-08-04 stsp static const struct got_error *open_tree_view(struct tog_view *,
258 4fc679ca 2018-08-04 stsp struct got_tree_object *, struct got_object_id *, struct got_repository *);
259 ad80ab7b 2018-08-04 stsp static const struct got_error *show_tree_view(struct tog_view *);
260 0cf4efb1 2018-09-29 stsp static const struct got_error *update_siblings_tree_view(int *,
261 0cf4efb1 2018-09-29 stsp struct tog_view *, struct tog_view *);
262 e5a0f69f 2018-08-18 stsp static const struct got_error *input_tree_view(struct tog_view **,
263 e5a0f69f 2018-08-18 stsp struct tog_view **, struct tog_view *, int);
264 e5a0f69f 2018-08-18 stsp static const struct got_error *close_tree_view(struct tog_view *);
266 e5a0f69f 2018-08-18 stsp static const struct got_error *
267 96a765a8 2018-08-04 stsp view_close(struct tog_view *view)
269 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
271 e5a0f69f 2018-08-18 stsp if (view->close)
272 e5a0f69f 2018-08-18 stsp err = view->close(view);
273 ea5e7bb5 2018-08-01 stsp if (view->panel)
274 ea5e7bb5 2018-08-01 stsp del_panel(view->panel);
275 ea5e7bb5 2018-08-01 stsp if (view->window)
276 ea5e7bb5 2018-08-01 stsp delwin(view->window);
277 ea5e7bb5 2018-08-01 stsp free(view);
278 e5a0f69f 2018-08-18 stsp return err;
281 ea5e7bb5 2018-08-01 stsp static struct tog_view *
282 b3665f43 2018-08-04 stsp view_open(int nlines, int ncols, int begin_y, int begin_x,
283 0cf4efb1 2018-09-29 stsp enum tog_view_type type)
285 ad80ab7b 2018-08-04 stsp struct tog_view *view = calloc(1, sizeof(*view));
287 ea5e7bb5 2018-08-01 stsp if (view == NULL)
288 ea5e7bb5 2018-08-01 stsp return NULL;
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 0cf4efb1 2018-09-29 stsp if (view->panel == NULL ||
304 0cf4efb1 2018-09-29 stsp set_panel_userptr(view->panel, view) != OK) {
305 96a765a8 2018-08-04 stsp view_close(view);
306 ea5e7bb5 2018-08-01 stsp return NULL;
309 ea5e7bb5 2018-08-01 stsp keypad(view->window, TRUE);
310 ea5e7bb5 2018-08-01 stsp return view;
313 4d8c2215 2018-08-19 stsp static const struct got_error *
314 0cf4efb1 2018-09-29 stsp view_show(struct tog_view *view, struct tog_view_list_head *views)
316 e5a0f69f 2018-08-18 stsp const struct got_error *err;
317 0cf4efb1 2018-09-29 stsp struct tog_view *v;
319 e5a0f69f 2018-08-18 stsp err = view->show(view);
321 e5a0f69f 2018-08-18 stsp return err;
323 0cf4efb1 2018-09-29 stsp if (!view->focussed)
324 0cf4efb1 2018-09-29 stsp return NULL;
326 0cf4efb1 2018-09-29 stsp TAILQ_FOREACH(v, views, entry) {
327 0cf4efb1 2018-09-29 stsp int updated = 0;
328 0cf4efb1 2018-09-29 stsp err = view->update_siblings(&updated, view, v);
330 bcbd79e2 2018-08-19 stsp return err;
331 0cf4efb1 2018-09-29 stsp if (updated) {
332 0cf4efb1 2018-09-29 stsp err = v->show(v);
334 0cf4efb1 2018-09-29 stsp return err;
338 e5a0f69f 2018-08-18 stsp return err;
342 0cf4efb1 2018-09-29 stsp view_split_begin_x(int begin_x)
344 0cf4efb1 2018-09-29 stsp if (begin_x > 0)
346 0cf4efb1 2018-09-29 stsp return (COLS >= 120 ? COLS/2 : 0);
349 4d8c2215 2018-08-19 stsp static const struct got_error *
350 f7d12f7e 2018-08-01 stsp view_resize(struct tog_view *view)
352 f7d12f7e 2018-08-01 stsp int nlines, ncols;
354 0cf4efb1 2018-09-29 stsp if (view->lines > LINES)
355 0cf4efb1 2018-09-29 stsp nlines = view->nlines - (view->lines - LINES);
357 0cf4efb1 2018-09-29 stsp nlines = view->nlines + (LINES - view->lines);
359 0cf4efb1 2018-09-29 stsp if (view->cols > COLS)
360 0cf4efb1 2018-09-29 stsp ncols = view->ncols - (view->cols - COLS);
362 0cf4efb1 2018-09-29 stsp ncols = view->ncols + (COLS - view->cols);
364 0cf4efb1 2018-09-29 stsp if (wresize(view->window, nlines, ncols) == ERR)
365 0cf4efb1 2018-09-29 stsp return got_error_from_errno();
366 0cf4efb1 2018-09-29 stsp replace_panel(view->panel, view->window);
368 0cf4efb1 2018-09-29 stsp view->nlines = nlines;
369 0cf4efb1 2018-09-29 stsp view->ncols = ncols;
370 0cf4efb1 2018-09-29 stsp view->lines = LINES;
371 0cf4efb1 2018-09-29 stsp view->cols = COLS;
373 0cf4efb1 2018-09-29 stsp return NULL;
376 0cf4efb1 2018-09-29 stsp static const struct got_error *
377 0cf4efb1 2018-09-29 stsp view_splitscreen(struct tog_view *view)
379 0cf4efb1 2018-09-29 stsp const struct got_error *err = NULL;
381 0cf4efb1 2018-09-29 stsp view->begin_y = 0;
382 0cf4efb1 2018-09-29 stsp view->begin_x = view_split_begin_x(0);
383 0cf4efb1 2018-09-29 stsp view->nlines = LINES;
384 0cf4efb1 2018-09-29 stsp view->ncols = COLS - view->begin_x;
385 0cf4efb1 2018-09-29 stsp view->lines = LINES;
386 0cf4efb1 2018-09-29 stsp view->cols = COLS;
387 0cf4efb1 2018-09-29 stsp err = view_resize(view);
389 0cf4efb1 2018-09-29 stsp return err;
391 0cf4efb1 2018-09-29 stsp if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
392 0cf4efb1 2018-09-29 stsp return got_error_from_errno();
394 f7d12f7e 2018-08-01 stsp return NULL;
397 e5a0f69f 2018-08-18 stsp static const struct got_error *
398 0cf4efb1 2018-09-29 stsp view_fullscreen(struct tog_view *view)
400 0cf4efb1 2018-09-29 stsp const struct got_error *err = NULL;
402 0cf4efb1 2018-09-29 stsp view->begin_x = 0;
403 0cf4efb1 2018-09-29 stsp view->begin_y = 0;
404 0cf4efb1 2018-09-29 stsp view->nlines = LINES;
405 0cf4efb1 2018-09-29 stsp view->ncols = COLS;
406 0cf4efb1 2018-09-29 stsp view->lines = LINES;
407 0cf4efb1 2018-09-29 stsp view->cols = COLS;
408 0cf4efb1 2018-09-29 stsp err = view_resize(view);
410 0cf4efb1 2018-09-29 stsp return err;
412 0cf4efb1 2018-09-29 stsp if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
413 0cf4efb1 2018-09-29 stsp return got_error_from_errno();
415 0cf4efb1 2018-09-29 stsp return NULL;
418 0cf4efb1 2018-09-29 stsp static const struct got_error *
419 48fcc512 2018-08-18 stsp view_input(struct tog_view **new, struct tog_view **dead,
420 e4197bf9 2018-08-18 stsp struct tog_view **focus, int *done, struct tog_view *view,
421 48fcc512 2018-08-18 stsp struct tog_view_list_head *views)
423 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
424 0cf4efb1 2018-09-29 stsp struct tog_view *next, *prev, *v;
427 e5a0f69f 2018-08-18 stsp *new = NULL;
428 e5a0f69f 2018-08-18 stsp *dead = NULL;
429 0cf4efb1 2018-09-29 stsp *focus = NULL;
431 e5a0f69f 2018-08-18 stsp nodelay(stdscr, FALSE);
432 e5a0f69f 2018-08-18 stsp ch = wgetch(view->window);
433 e5a0f69f 2018-08-18 stsp nodelay(stdscr, TRUE);
434 e5a0f69f 2018-08-18 stsp switch (ch) {
438 48fcc512 2018-08-18 stsp next = TAILQ_NEXT(view, entry);
440 48fcc512 2018-08-18 stsp *focus = next;
442 48fcc512 2018-08-18 stsp *focus = TAILQ_FIRST(views);
445 31607d6c 2018-08-18 stsp prev = TAILQ_PREV(view, tog_view_list_head, entry);
447 31607d6c 2018-08-18 stsp *focus = prev;
449 31607d6c 2018-08-18 stsp *focus = TAILQ_LAST(views, tog_view_list_head);
452 e5a0f69f 2018-08-18 stsp err = view->input(new, dead, view, ch);
453 e5a0f69f 2018-08-18 stsp *dead = view;
459 0cf4efb1 2018-09-29 stsp if (view->begin_x == 0)
460 0cf4efb1 2018-09-29 stsp err = view_splitscreen(view);
462 0cf4efb1 2018-09-29 stsp err = view_fullscreen(view);
465 0cf4efb1 2018-09-29 stsp err = view->input(new, dead, view, KEY_RESIZE);
468 0cf4efb1 2018-09-29 stsp *focus = view;
470 0cf4efb1 2018-09-29 stsp case KEY_RESIZE:
471 0cf4efb1 2018-09-29 stsp TAILQ_FOREACH(v, views, entry) {
472 0cf4efb1 2018-09-29 stsp err = view_resize(v);
474 0cf4efb1 2018-09-29 stsp return err;
475 0cf4efb1 2018-09-29 stsp err = v->input(new, dead, v, ch);
479 e5a0f69f 2018-08-18 stsp err = view->input(new, dead, view, ch);
483 e5a0f69f 2018-08-18 stsp return err;
487 1a57306a 2018-09-02 stsp view_vborder(struct tog_view *view)
489 0cf4efb1 2018-09-29 stsp PANEL *panel;
490 0cf4efb1 2018-09-29 stsp struct tog_view *view_above;
492 0cf4efb1 2018-09-29 stsp panel = panel_above(view->panel);
493 0cf4efb1 2018-09-29 stsp if (panel == NULL)
496 0cf4efb1 2018-09-29 stsp view_above = panel_userptr(panel);
497 0cf4efb1 2018-09-29 stsp mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
498 1a57306a 2018-09-02 stsp got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
502 a3404814 2018-09-02 stsp view_needs_focus_indication(struct tog_view *view)
504 0cf4efb1 2018-09-29 stsp PANEL *panel;
506 1004088d 2018-09-29 stsp if (!view->focussed)
509 0cf4efb1 2018-09-29 stsp panel = panel_above(view->panel);
510 0cf4efb1 2018-09-29 stsp if (panel) {
511 0cf4efb1 2018-09-29 stsp struct tog_view *view_above = panel_userptr(panel);
512 0cf4efb1 2018-09-29 stsp if (view_above->begin_x > view->begin_x)
516 0cf4efb1 2018-09-29 stsp panel = panel_below(view->panel);
517 0cf4efb1 2018-09-29 stsp if (panel) {
518 0cf4efb1 2018-09-29 stsp struct tog_view *view_below = panel_userptr(panel);
519 0cf4efb1 2018-09-29 stsp if (view->begin_x > view_below->begin_x)
526 bcbd79e2 2018-08-19 stsp static const struct got_error *
527 e5a0f69f 2018-08-18 stsp view_loop(struct tog_view *view)
529 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
530 e5a0f69f 2018-08-18 stsp struct tog_view_list_head views;
531 0cf4efb1 2018-09-29 stsp struct tog_view *new_view, *dead_view, *focus_view, *v;
532 e4197bf9 2018-08-18 stsp int done = 0;
534 e5a0f69f 2018-08-18 stsp TAILQ_INIT(&views);
535 e5a0f69f 2018-08-18 stsp TAILQ_INSERT_HEAD(&views, view, entry);
537 1004088d 2018-09-29 stsp view->focussed = 1;
538 0cf4efb1 2018-09-29 stsp err = view_show(view, &views);
540 0cf4efb1 2018-09-29 stsp return err;
541 0cf4efb1 2018-09-29 stsp update_panels();
542 0cf4efb1 2018-09-29 stsp doupdate();
543 e4197bf9 2018-08-18 stsp while (!TAILQ_EMPTY(&views) && !done) {
544 0cf4efb1 2018-09-29 stsp err = view_input(&new_view, &dead_view, &focus_view, &done,
545 e4197bf9 2018-08-18 stsp view, &views);
548 e5a0f69f 2018-08-18 stsp if (dead_view) {
549 e5a0f69f 2018-08-18 stsp TAILQ_REMOVE(&views, dead_view, entry);
550 e5a0f69f 2018-08-18 stsp err = view_close(dead_view);
553 0cf4efb1 2018-09-29 stsp if (view == dead_view) {
554 0cf4efb1 2018-09-29 stsp if (focus_view)
555 0cf4efb1 2018-09-29 stsp view = focus_view;
556 0cf4efb1 2018-09-29 stsp else if (!TAILQ_EMPTY(&views)) {
557 0cf4efb1 2018-09-29 stsp view = TAILQ_LAST(&views,
558 0cf4efb1 2018-09-29 stsp tog_view_list_head);
559 0cf4efb1 2018-09-29 stsp focus_view = view;
561 0cf4efb1 2018-09-29 stsp view = NULL;
564 bcbd79e2 2018-08-19 stsp if (new_view) {
565 0cf4efb1 2018-09-29 stsp struct tog_view *t;
566 ae73d513 2018-09-24 stsp /* Only allow one view per type. */
567 ae73d513 2018-09-24 stsp TAILQ_FOREACH_SAFE(v, &views, entry, t) {
568 ae73d513 2018-09-24 stsp if (v->type != new_view->type)
570 ae73d513 2018-09-24 stsp TAILQ_REMOVE(&views, v, entry);
571 ae73d513 2018-09-24 stsp err = view_close(v);
574 0cf4efb1 2018-09-29 stsp if (v == view)
575 0cf4efb1 2018-09-29 stsp view = new_view;
578 bcbd79e2 2018-08-19 stsp TAILQ_INSERT_TAIL(&views, new_view, entry);
579 0cf4efb1 2018-09-29 stsp focus_view = new_view;
581 0cf4efb1 2018-09-29 stsp if (focus_view) {
582 0cf4efb1 2018-09-29 stsp show_panel(focus_view->panel);
583 0cf4efb1 2018-09-29 stsp if (view) {
584 0cf4efb1 2018-09-29 stsp if (focus_view->begin_x == 0 &&
585 0cf4efb1 2018-09-29 stsp view->begin_x > 0 &&
586 0cf4efb1 2018-09-29 stsp focus_view != new_view)
587 0cf4efb1 2018-09-29 stsp show_panel(view->panel);
588 0cf4efb1 2018-09-29 stsp view->focussed = 0;
590 0cf4efb1 2018-09-29 stsp focus_view->focussed = 1;
591 0cf4efb1 2018-09-29 stsp view = focus_view;
593 0cf4efb1 2018-09-29 stsp TAILQ_FOREACH(v, &views, entry) {
594 0cf4efb1 2018-09-29 stsp err = view_show(v, &views);
598 0cf4efb1 2018-09-29 stsp update_panels();
599 0cf4efb1 2018-09-29 stsp doupdate();
602 e5a0f69f 2018-08-18 stsp while (!TAILQ_EMPTY(&views)) {
603 e5a0f69f 2018-08-18 stsp view = TAILQ_FIRST(&views);
604 e5a0f69f 2018-08-18 stsp TAILQ_REMOVE(&views, view, entry);
605 e5a0f69f 2018-08-18 stsp view_close(view);
607 e5a0f69f 2018-08-18 stsp return err;
610 4ed7e80c 2018-05-20 stsp __dead static void
611 9f7d7167 2018-04-29 stsp usage_log(void)
614 c70c5802 2018-08-01 stsp fprintf(stderr,
615 c70c5802 2018-08-01 stsp "usage: %s log [-c commit] [-r repository-path] [path]\n",
616 9f7d7167 2018-04-29 stsp getprogname());
620 963b370f 2018-05-20 stsp /* Create newly allocated wide-character string equivalent to a byte string. */
621 80ddbec8 2018-04-29 stsp static const struct got_error *
622 963b370f 2018-05-20 stsp mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
624 00dfcb92 2018-06-11 stsp char *vis = NULL;
625 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
627 963b370f 2018-05-20 stsp *ws = NULL;
628 963b370f 2018-05-20 stsp *wlen = mbstowcs(NULL, s, 0);
629 00dfcb92 2018-06-11 stsp if (*wlen == (size_t)-1) {
630 00dfcb92 2018-06-11 stsp int vislen;
631 00dfcb92 2018-06-11 stsp if (errno != EILSEQ)
632 00dfcb92 2018-06-11 stsp return got_error_from_errno();
634 00dfcb92 2018-06-11 stsp /* byte string invalid in current encoding; try to "fix" it */
635 00dfcb92 2018-06-11 stsp err = got_mbsavis(&vis, &vislen, s);
637 00dfcb92 2018-06-11 stsp return err;
638 00dfcb92 2018-06-11 stsp *wlen = mbstowcs(NULL, vis, 0);
639 a7f50699 2018-06-11 stsp if (*wlen == (size_t)-1) {
640 a7f50699 2018-06-11 stsp err = got_error_from_errno(); /* give up */
645 963b370f 2018-05-20 stsp *ws = calloc(*wlen + 1, sizeof(*ws));
646 a7f50699 2018-06-11 stsp if (*ws == NULL) {
647 a7f50699 2018-06-11 stsp err = got_error_from_errno();
651 00dfcb92 2018-06-11 stsp if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
652 963b370f 2018-05-20 stsp err = got_error_from_errno();
657 963b370f 2018-05-20 stsp *ws = NULL;
660 963b370f 2018-05-20 stsp return err;
663 963b370f 2018-05-20 stsp /* Format a line for display, ensuring that it won't overflow a width limit. */
664 963b370f 2018-05-20 stsp static const struct got_error *
665 ffd1d5e5 2018-06-23 stsp format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit)
667 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
668 963b370f 2018-05-20 stsp int cols = 0;
669 963b370f 2018-05-20 stsp wchar_t *wline = NULL;
670 963b370f 2018-05-20 stsp size_t wlen;
673 963b370f 2018-05-20 stsp *wlinep = NULL;
674 b700b5d6 2018-07-10 stsp *widthp = 0;
676 963b370f 2018-05-20 stsp err = mbs2ws(&wline, &wlen, line);
678 963b370f 2018-05-20 stsp return err;
681 b700b5d6 2018-07-10 stsp while (i < wlen && cols < wlimit) {
682 963b370f 2018-05-20 stsp int width = wcwidth(wline[i]);
683 963b370f 2018-05-20 stsp switch (width) {
689 3c1f04f1 2018-09-13 stsp if (cols + width <= wlimit)
690 b700b5d6 2018-07-10 stsp cols += width;
694 963b370f 2018-05-20 stsp if (wline[i] == L'\t')
695 b700b5d6 2018-07-10 stsp cols += TABSIZE - ((cols + 1) % TABSIZE);
699 963b370f 2018-05-20 stsp err = got_error_from_errno();
703 963b370f 2018-05-20 stsp wline[i] = L'\0';
704 b700b5d6 2018-07-10 stsp if (widthp)
705 b700b5d6 2018-07-10 stsp *widthp = cols;
708 963b370f 2018-05-20 stsp free(wline);
710 963b370f 2018-05-20 stsp *wlinep = wline;
711 963b370f 2018-05-20 stsp return err;
714 963b370f 2018-05-20 stsp static const struct got_error *
715 2814baeb 2018-08-01 stsp draw_commit(struct tog_view *view, struct got_commit_object *commit,
716 2814baeb 2018-08-01 stsp struct got_object_id *id)
718 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
719 b39d25c7 2018-07-10 stsp char datebuf[10]; /* YY-MM-DD + SPACE + NUL */
720 80ddbec8 2018-04-29 stsp char *logmsg0 = NULL, *logmsg = NULL;
721 6d9fbc00 2018-04-29 stsp char *author0 = NULL, *author = NULL;
722 bb737323 2018-05-20 stsp wchar_t *wlogmsg = NULL, *wauthor = NULL;
723 bb737323 2018-05-20 stsp int author_width, logmsg_width;
724 6d9fbc00 2018-04-29 stsp char *newline, *smallerthan;
725 80ddbec8 2018-04-29 stsp char *line = NULL;
726 bb737323 2018-05-20 stsp int col, limit;
727 b39d25c7 2018-07-10 stsp static const size_t date_display_cols = 9;
728 bb737323 2018-05-20 stsp static const size_t author_display_cols = 16;
729 f7d12f7e 2018-08-01 stsp const int avail = view->ncols;
731 c70c5802 2018-08-01 stsp if (strftime(datebuf, sizeof(datebuf), "%g/%m/%d ",
732 c70c5802 2018-08-01 stsp &commit->tm_committer) >= sizeof(datebuf))
733 b39d25c7 2018-07-10 stsp return got_error(GOT_ERR_NO_SPACE);
735 b39d25c7 2018-07-10 stsp if (avail < date_display_cols)
736 b39d25c7 2018-07-10 stsp limit = MIN(sizeof(datebuf) - 1, avail);
738 b39d25c7 2018-07-10 stsp limit = MIN(date_display_cols, sizeof(datebuf) - 1);
739 2814baeb 2018-08-01 stsp waddnstr(view->window, datebuf, limit);
740 b39d25c7 2018-07-10 stsp col = limit + 1;
741 b39d25c7 2018-07-10 stsp if (col > avail)
744 6d9fbc00 2018-04-29 stsp author0 = strdup(commit->author);
745 6d9fbc00 2018-04-29 stsp if (author0 == NULL) {
746 80ddbec8 2018-04-29 stsp err = got_error_from_errno();
749 6d9fbc00 2018-04-29 stsp author = author0;
750 6d9fbc00 2018-04-29 stsp smallerthan = strchr(author, '<');
751 6d9fbc00 2018-04-29 stsp if (smallerthan)
752 6d9fbc00 2018-04-29 stsp *smallerthan = '\0';
754 6d9fbc00 2018-04-29 stsp char *at = strchr(author, '@');
756 6d9fbc00 2018-04-29 stsp *at = '\0';
758 b39d25c7 2018-07-10 stsp limit = avail - col;
759 bb737323 2018-05-20 stsp err = format_line(&wauthor, &author_width, author, limit);
762 2814baeb 2018-08-01 stsp waddwstr(view->window, wauthor);
763 bb737323 2018-05-20 stsp col += author_width;
764 9c2eaf34 2018-05-20 stsp while (col <= avail && author_width < author_display_cols + 1) {
765 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
767 bb737323 2018-05-20 stsp author_width++;
769 9c2eaf34 2018-05-20 stsp if (col > avail)
772 bb737323 2018-05-20 stsp logmsg0 = strdup(commit->logmsg);
773 bb737323 2018-05-20 stsp if (logmsg0 == NULL) {
774 6d9fbc00 2018-04-29 stsp err = got_error_from_errno();
777 bb737323 2018-05-20 stsp logmsg = logmsg0;
778 bb737323 2018-05-20 stsp while (*logmsg == '\n')
780 bb737323 2018-05-20 stsp newline = strchr(logmsg, '\n');
781 bb737323 2018-05-20 stsp if (newline)
782 bb737323 2018-05-20 stsp *newline = '\0';
783 bb737323 2018-05-20 stsp limit = avail - col;
784 bb737323 2018-05-20 stsp err = format_line(&wlogmsg, &logmsg_width, logmsg, limit);
787 2814baeb 2018-08-01 stsp waddwstr(view->window, wlogmsg);
788 bb737323 2018-05-20 stsp col += logmsg_width;
789 bb737323 2018-05-20 stsp while (col <= avail) {
790 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
794 80ddbec8 2018-04-29 stsp free(logmsg0);
795 bb737323 2018-05-20 stsp free(wlogmsg);
796 6d9fbc00 2018-04-29 stsp free(author0);
797 bb737323 2018-05-20 stsp free(wauthor);
798 80ddbec8 2018-04-29 stsp free(line);
799 80ddbec8 2018-04-29 stsp return err;
802 899d86c2 2018-05-10 stsp static struct commit_queue_entry *
803 899d86c2 2018-05-10 stsp alloc_commit_queue_entry(struct got_commit_object *commit,
804 899d86c2 2018-05-10 stsp struct got_object_id *id)
806 80ddbec8 2018-04-29 stsp struct commit_queue_entry *entry;
808 80ddbec8 2018-04-29 stsp entry = calloc(1, sizeof(*entry));
809 80ddbec8 2018-04-29 stsp if (entry == NULL)
810 899d86c2 2018-05-10 stsp return NULL;
812 899d86c2 2018-05-10 stsp entry->id = id;
813 99db9666 2018-05-07 stsp entry->commit = commit;
814 899d86c2 2018-05-10 stsp return entry;
817 99db9666 2018-05-07 stsp static void
818 99db9666 2018-05-07 stsp pop_commit(struct commit_queue *commits)
820 99db9666 2018-05-07 stsp struct commit_queue_entry *entry;
822 ecb28ae0 2018-07-16 stsp entry = TAILQ_FIRST(&commits->head);
823 ecb28ae0 2018-07-16 stsp TAILQ_REMOVE(&commits->head, entry, entry);
824 99db9666 2018-05-07 stsp got_object_commit_close(entry->commit);
825 ecb28ae0 2018-07-16 stsp commits->ncommits--;
826 9ba79e04 2018-06-11 stsp /* Don't free entry->id! It is owned by the commit graph. */
827 99db9666 2018-05-07 stsp free(entry);
830 99db9666 2018-05-07 stsp static void
831 99db9666 2018-05-07 stsp free_commits(struct commit_queue *commits)
833 ecb28ae0 2018-07-16 stsp while (!TAILQ_EMPTY(&commits->head))
834 99db9666 2018-05-07 stsp pop_commit(commits);
837 c4972b91 2018-05-07 stsp static const struct got_error *
838 9ba79e04 2018-06-11 stsp queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
839 93e45b7c 2018-09-24 stsp struct got_object_id *start_id, int minqueue,
840 ecb28ae0 2018-07-16 stsp struct got_repository *repo, const char *path)
842 899d86c2 2018-05-10 stsp const struct got_error *err = NULL;
843 93e45b7c 2018-09-24 stsp int nqueued = 0;
845 93e45b7c 2018-09-24 stsp if (start_id) {
846 93e45b7c 2018-09-24 stsp err = got_commit_graph_iter_start(graph, start_id, repo);
848 9ba79e04 2018-06-11 stsp return err;
851 93e45b7c 2018-09-24 stsp while (nqueued < minqueue) {
852 93e45b7c 2018-09-24 stsp struct got_object_id *id;
853 9ba79e04 2018-06-11 stsp struct got_commit_object *commit;
854 93e45b7c 2018-09-24 stsp struct commit_queue_entry *entry;
856 9ba79e04 2018-06-11 stsp err = got_commit_graph_iter_next(&id, graph);
858 93e45b7c 2018-09-24 stsp if (err->code == GOT_ERR_ITER_COMPLETED) {
859 9ba79e04 2018-06-11 stsp err = NULL;
862 93e45b7c 2018-09-24 stsp if (err->code != GOT_ERR_ITER_NEED_MORE)
864 93e45b7c 2018-09-24 stsp err = got_commit_graph_fetch_commits(graph,
865 93e45b7c 2018-09-24 stsp minqueue, repo);
867 ecb28ae0 2018-07-16 stsp return err;
871 ecb28ae0 2018-07-16 stsp if (id == NULL)
874 9ba79e04 2018-06-11 stsp err = got_object_open_as_commit(&commit, repo, id);
877 9ba79e04 2018-06-11 stsp entry = alloc_commit_queue_entry(commit, id);
878 9ba79e04 2018-06-11 stsp if (entry == NULL) {
879 9ba79e04 2018-06-11 stsp err = got_error_from_errno();
883 ecb28ae0 2018-07-16 stsp TAILQ_INSERT_TAIL(&commits->head, entry, entry);
885 ecb28ae0 2018-07-16 stsp commits->ncommits++;
888 9ba79e04 2018-06-11 stsp return err;
891 99db9666 2018-05-07 stsp static const struct got_error *
892 9ba79e04 2018-06-11 stsp fetch_next_commit(struct commit_queue_entry **pentry,
893 9ba79e04 2018-06-11 stsp struct commit_queue_entry *entry, struct commit_queue *commits,
894 ecb28ae0 2018-07-16 stsp struct got_commit_graph *graph, struct got_repository *repo,
895 ecb28ae0 2018-07-16 stsp const char *path)
897 899d86c2 2018-05-10 stsp const struct got_error *err = NULL;
899 9ba79e04 2018-06-11 stsp *pentry = NULL;
901 93e45b7c 2018-09-24 stsp err = queue_commits(graph, commits, NULL, 1, repo, path);
903 9ba79e04 2018-06-11 stsp return err;
905 9ba79e04 2018-06-11 stsp /* Next entry to display should now be available. */
906 9ba79e04 2018-06-11 stsp *pentry = TAILQ_NEXT(entry, entry);
907 9ba79e04 2018-06-11 stsp return NULL;
910 899d86c2 2018-05-10 stsp static const struct got_error *
911 9ba79e04 2018-06-11 stsp get_head_commit_id(struct got_object_id **head_id, struct got_repository *repo)
913 9ba79e04 2018-06-11 stsp const struct got_error *err = NULL;
914 9ba79e04 2018-06-11 stsp struct got_reference *head_ref;
916 9ba79e04 2018-06-11 stsp *head_id = NULL;
918 9ba79e04 2018-06-11 stsp err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
920 99db9666 2018-05-07 stsp return err;
922 9ba79e04 2018-06-11 stsp err = got_ref_resolve(head_id, repo, head_ref);
923 9ba79e04 2018-06-11 stsp got_ref_close(head_ref);
925 9ba79e04 2018-06-11 stsp *head_id = NULL;
926 99db9666 2018-05-07 stsp return err;
929 9ba79e04 2018-06-11 stsp return NULL;
932 0553a4e3 2018-04-30 stsp static const struct got_error *
933 2814baeb 2018-08-01 stsp draw_commits(struct tog_view *view, struct commit_queue_entry **last,
934 ecb28ae0 2018-07-16 stsp struct commit_queue_entry **selected, struct commit_queue_entry *first,
935 a7f40148 2018-07-18 stsp struct commit_queue *commits, int selected_idx, int limit,
936 a7f40148 2018-07-18 stsp struct got_commit_graph *graph, struct got_repository *repo,
937 a7f40148 2018-07-18 stsp const char *path)
939 0553a4e3 2018-04-30 stsp const struct got_error *err = NULL;
940 0553a4e3 2018-04-30 stsp struct commit_queue_entry *entry;
941 ecb28ae0 2018-07-16 stsp int ncommits, width;
942 867c6645 2018-07-10 stsp char *id_str, *header;
943 ecb28ae0 2018-07-16 stsp wchar_t *wline;
945 e0d42f60 2018-07-22 stsp entry = first;
946 e0d42f60 2018-07-22 stsp ncommits = 0;
947 e0d42f60 2018-07-22 stsp while (entry) {
948 e0d42f60 2018-07-22 stsp if (ncommits == selected_idx) {
949 e0d42f60 2018-07-22 stsp *selected = entry;
952 e0d42f60 2018-07-22 stsp entry = TAILQ_NEXT(entry, entry);
953 e0d42f60 2018-07-22 stsp ncommits++;
956 867c6645 2018-07-10 stsp err = got_object_id_str(&id_str, (*selected)->id);
958 867c6645 2018-07-10 stsp return err;
960 c3ba6f36 2018-08-01 stsp if (path && strcmp(path, "/") != 0) {
961 ecb28ae0 2018-07-16 stsp if (asprintf(&header, "commit: %s [%s]", id_str, path) == -1) {
962 ecb28ae0 2018-07-16 stsp err = got_error_from_errno();
963 ecb28ae0 2018-07-16 stsp free(id_str);
964 ecb28ae0 2018-07-16 stsp return err;
966 ecb28ae0 2018-07-16 stsp } else if (asprintf(&header, "commit: %s", id_str) == -1) {
967 867c6645 2018-07-10 stsp err = got_error_from_errno();
968 867c6645 2018-07-10 stsp free(id_str);
969 867c6645 2018-07-10 stsp return err;
971 ecb28ae0 2018-07-16 stsp free(id_str);
972 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, header, view->ncols);
974 ecb28ae0 2018-07-16 stsp free(header);
975 ecb28ae0 2018-07-16 stsp return err;
977 ecb28ae0 2018-07-16 stsp free(header);
979 2814baeb 2018-08-01 stsp werase(view->window);
981 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
982 a3404814 2018-09-02 stsp wstandout(view->window);
983 2814baeb 2018-08-01 stsp waddwstr(view->window, wline);
984 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
985 a3404814 2018-09-02 stsp wstandend(view->window);
986 f7d12f7e 2018-08-01 stsp if (width < view->ncols)
987 2814baeb 2018-08-01 stsp waddch(view->window, '\n');
988 ecb28ae0 2018-07-16 stsp free(wline);
989 ecb28ae0 2018-07-16 stsp if (limit <= 1)
990 ecb28ae0 2018-07-16 stsp return NULL;
992 899d86c2 2018-05-10 stsp entry = first;
993 899d86c2 2018-05-10 stsp *last = first;
994 867c6645 2018-07-10 stsp ncommits = 0;
995 899d86c2 2018-05-10 stsp while (entry) {
996 ecb28ae0 2018-07-16 stsp if (ncommits >= limit - 1)
998 0cf4efb1 2018-09-29 stsp if (view->focussed && ncommits == selected_idx)
999 2814baeb 2018-08-01 stsp wstandout(view->window);
1000 2814baeb 2018-08-01 stsp err = draw_commit(view, entry->commit, entry->id);
1001 0cf4efb1 2018-09-29 stsp if (view->focussed && ncommits == selected_idx)
1002 2814baeb 2018-08-01 stsp wstandend(view->window);
1005 0553a4e3 2018-04-30 stsp ncommits++;
1006 899d86c2 2018-05-10 stsp *last = entry;
1007 a7f40148 2018-07-18 stsp if (entry == TAILQ_LAST(&commits->head, commit_queue_head)) {
1008 93e45b7c 2018-09-24 stsp err = queue_commits(graph, commits, NULL, 1,
1009 93e45b7c 2018-09-24 stsp repo, path);
1010 a7f40148 2018-07-18 stsp if (err) {
1011 a7f40148 2018-07-18 stsp if (err->code != GOT_ERR_ITER_COMPLETED)
1012 a7f40148 2018-07-18 stsp return err;
1013 a7f40148 2018-07-18 stsp err = NULL;
1016 899d86c2 2018-05-10 stsp entry = TAILQ_NEXT(entry, entry);
1019 1a57306a 2018-09-02 stsp view_vborder(view);
1021 80ddbec8 2018-04-29 stsp return err;
1024 07b55e75 2018-05-10 stsp static void
1025 16482c3b 2018-05-20 stsp scroll_up(struct commit_queue_entry **first_displayed_entry, int maxscroll,
1026 07b55e75 2018-05-10 stsp struct commit_queue *commits)
1028 07b55e75 2018-05-10 stsp struct commit_queue_entry *entry;
1029 07b55e75 2018-05-10 stsp int nscrolled = 0;
1031 ecb28ae0 2018-07-16 stsp entry = TAILQ_FIRST(&commits->head);
1032 07b55e75 2018-05-10 stsp if (*first_displayed_entry == entry)
1035 07b55e75 2018-05-10 stsp entry = *first_displayed_entry;
1036 16482c3b 2018-05-20 stsp while (entry && nscrolled < maxscroll) {
1037 ecb28ae0 2018-07-16 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
1038 07b55e75 2018-05-10 stsp if (entry) {
1039 07b55e75 2018-05-10 stsp *first_displayed_entry = entry;
1040 07b55e75 2018-05-10 stsp nscrolled++;
1045 aa075928 2018-05-10 stsp static const struct got_error *
1046 16482c3b 2018-05-20 stsp scroll_down(struct commit_queue_entry **first_displayed_entry, int maxscroll,
1047 93e45b7c 2018-09-24 stsp struct commit_queue_entry **last_displayed_entry,
1048 9ba79e04 2018-06-11 stsp struct commit_queue *commits, struct got_commit_graph *graph,
1049 ecb28ae0 2018-07-16 stsp struct got_repository *repo, const char *path)
1051 aa075928 2018-05-10 stsp const struct got_error *err = NULL;
1052 dd0a52c1 2018-05-20 stsp struct commit_queue_entry *pentry;
1053 aa075928 2018-05-10 stsp int nscrolled = 0;
1056 93e45b7c 2018-09-24 stsp pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1057 aa075928 2018-05-10 stsp if (pentry == NULL) {
1058 93e45b7c 2018-09-24 stsp err = fetch_next_commit(&pentry, *last_displayed_entry,
1059 ecb28ae0 2018-07-16 stsp commits, graph, repo, path);
1060 9a6bf2a5 2018-05-20 stsp if (err || pentry == NULL)
1063 93e45b7c 2018-09-24 stsp *last_displayed_entry = pentry;
1065 dd0a52c1 2018-05-20 stsp pentry = TAILQ_NEXT(*first_displayed_entry, entry);
1066 dd0a52c1 2018-05-20 stsp if (pentry == NULL)
1068 aa075928 2018-05-10 stsp *first_displayed_entry = pentry;
1069 16482c3b 2018-05-20 stsp } while (++nscrolled < maxscroll);
1071 dd0a52c1 2018-05-20 stsp return err;
1074 cd0acaa7 2018-05-20 stsp static const struct got_error *
1075 0cf4efb1 2018-09-29 stsp open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1076 0cf4efb1 2018-09-29 stsp struct got_object_id *commit_id, struct got_commit_object *commit,
1077 2a4718d3 2018-09-29 stsp struct got_repository *repo)
1079 cd0acaa7 2018-05-20 stsp const struct got_error *err;
1080 cd0acaa7 2018-05-20 stsp struct got_object *obj1 = NULL, *obj2 = NULL;
1081 9ba79e04 2018-06-11 stsp struct got_object_qid *parent_id;
1082 e5a0f69f 2018-08-18 stsp struct tog_view *diff_view;
1084 2a4718d3 2018-09-29 stsp err = got_object_open(&obj2, repo, commit_id);
1086 cd0acaa7 2018-05-20 stsp return err;
1088 2a4718d3 2018-09-29 stsp parent_id = SIMPLEQ_FIRST(&commit->parent_ids);
1089 9ba79e04 2018-06-11 stsp if (parent_id) {
1090 9ba79e04 2018-06-11 stsp err = got_object_open(&obj1, repo, parent_id->id);
1092 cd0acaa7 2018-05-20 stsp goto done;
1095 0cf4efb1 2018-09-29 stsp diff_view = view_open(0, 0, 0, view_split_begin_x(begin_x),
1096 0cf4efb1 2018-09-29 stsp TOG_VIEW_DIFF);
1097 e5a0f69f 2018-08-18 stsp if (diff_view == NULL) {
1098 ea5e7bb5 2018-08-01 stsp err = got_error_from_errno();
1099 ea5e7bb5 2018-08-01 stsp goto done;
1102 e5a0f69f 2018-08-18 stsp err = open_diff_view(diff_view, obj1, obj2, repo);
1103 e5a0f69f 2018-08-18 stsp if (err == NULL)
1104 e5a0f69f 2018-08-18 stsp *new_view = diff_view;
1107 cd0acaa7 2018-05-20 stsp got_object_close(obj1);
1109 cd0acaa7 2018-05-20 stsp got_object_close(obj2);
1110 cd0acaa7 2018-05-20 stsp return err;
1113 80ddbec8 2018-04-29 stsp static const struct got_error *
1114 0cf4efb1 2018-09-29 stsp browse_commit(struct tog_view **new_view, int begin_x,
1115 e5a0f69f 2018-08-18 stsp struct commit_queue_entry *entry, struct got_repository *repo)
1117 9343a5fb 2018-06-23 stsp const struct got_error *err = NULL;
1118 9343a5fb 2018-06-23 stsp struct got_tree_object *tree;
1119 e5a0f69f 2018-08-18 stsp struct tog_view *tree_view;
1121 9343a5fb 2018-06-23 stsp err = got_object_open_as_tree(&tree, repo, entry->commit->tree_id);
1123 9343a5fb 2018-06-23 stsp return err;
1125 0cf4efb1 2018-09-29 stsp tree_view = view_open(0, 0, 0, view_split_begin_x(begin_x),
1126 0cf4efb1 2018-09-29 stsp TOG_VIEW_TREE);
1127 e5a0f69f 2018-08-18 stsp if (tree_view == NULL)
1128 e5a0f69f 2018-08-18 stsp return got_error_from_errno();
1130 e5a0f69f 2018-08-18 stsp err = open_tree_view(tree_view, tree, entry->id, repo);
1132 e5a0f69f 2018-08-18 stsp got_object_tree_close(tree);
1134 e5a0f69f 2018-08-18 stsp *new_view = tree_view;
1135 9343a5fb 2018-06-23 stsp return err;
1138 bcbd79e2 2018-08-19 stsp static const struct got_error *
1139 bcbd79e2 2018-08-19 stsp set_child_log_view(struct tog_view *view, struct tog_view *child)
1141 bcbd79e2 2018-08-19 stsp struct tog_log_view_state *s = &view->state.log;
1142 bcbd79e2 2018-08-19 stsp struct tog_diff_view_state *ds;
1143 bcbd79e2 2018-08-19 stsp struct commit_queue_entry *commit, *child_entry = NULL;
1144 bcbd79e2 2018-08-19 stsp int selected_idx = 0;
1146 bcbd79e2 2018-08-19 stsp if (child->type != TOG_VIEW_DIFF)
1147 bcbd79e2 2018-08-19 stsp return NULL;
1148 bcbd79e2 2018-08-19 stsp ds = &child->state.diff;
1150 bcbd79e2 2018-08-19 stsp TAILQ_FOREACH(commit, &s->commits.head, entry) {
1151 a3404814 2018-09-02 stsp if (got_object_id_cmp(commit->id, ds->id2) == 0) {
1152 bcbd79e2 2018-08-19 stsp child_entry = commit;
1156 bcbd79e2 2018-08-19 stsp if (child_entry == NULL)
1157 bcbd79e2 2018-08-19 stsp return NULL;
1159 bcbd79e2 2018-08-19 stsp commit = s->first_displayed_entry;
1160 bcbd79e2 2018-08-19 stsp while (commit) {
1161 bcbd79e2 2018-08-19 stsp if (got_object_id_cmp(commit->id, child_entry->id) == 0) {
1162 bcbd79e2 2018-08-19 stsp s->selected_entry = child_entry;
1163 bcbd79e2 2018-08-19 stsp s->selected = selected_idx;
1166 bcbd79e2 2018-08-19 stsp if (commit == s->last_displayed_entry)
1168 bcbd79e2 2018-08-19 stsp selected_idx++;
1169 bcbd79e2 2018-08-19 stsp commit = TAILQ_NEXT(commit, entry);
1172 bcbd79e2 2018-08-19 stsp return show_log_view(view);
1175 9343a5fb 2018-06-23 stsp static const struct got_error *
1176 ba4f502b 2018-08-04 stsp open_log_view(struct tog_view *view, struct got_object_id *start_id,
1177 04cc582a 2018-08-01 stsp struct got_repository *repo, const char *path)
1179 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
1180 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
1182 fb2756b9 2018-08-04 stsp err = got_repo_map_path(&s->in_repo_path, repo, path);
1183 ecb28ae0 2018-07-16 stsp if (err != NULL)
1184 ecb28ae0 2018-07-16 stsp goto done;
1186 93e45b7c 2018-09-24 stsp err = got_commit_graph_open(&s->graph, start_id, s->in_repo_path,
1189 9ba79e04 2018-06-11 stsp goto done;
1190 93e45b7c 2018-09-24 stsp /* The commit queue only contains commits being displayed. */
1191 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->commits.head);
1192 fb2756b9 2018-08-04 stsp s->commits.ncommits = 0;
1195 9ba79e04 2018-06-11 stsp * Open the initial batch of commits, sorted in commit graph order.
1196 9ba79e04 2018-06-11 stsp * We keep all commits open throughout the lifetime of the log view
1197 9ba79e04 2018-06-11 stsp * in order to avoid having to re-fetch commits from disk while
1198 9ba79e04 2018-06-11 stsp * updating the display.
1200 93e45b7c 2018-09-24 stsp err = queue_commits(s->graph, &s->commits, start_id, view->nlines,
1201 fb2756b9 2018-08-04 stsp repo, s->in_repo_path);
1202 55198a88 2018-07-16 stsp if (err) {
1203 55198a88 2018-07-16 stsp if (err->code != GOT_ERR_ITER_COMPLETED)
1204 55198a88 2018-07-16 stsp goto done;
1205 55198a88 2018-07-16 stsp err = NULL;
1208 93e45b7c 2018-09-24 stsp s->first_displayed_entry = TAILQ_FIRST(&s->commits.head);
1209 1bfa490b 2018-08-18 stsp s->selected_entry = s->first_displayed_entry;
1210 fb2756b9 2018-08-04 stsp s->repo = repo;
1211 5036bf37 2018-09-24 stsp s->start_id = got_object_id_dup(start_id);
1212 5036bf37 2018-09-24 stsp if (s->start_id == NULL) {
1213 5036bf37 2018-09-24 stsp err = got_error_from_errno();
1214 5036bf37 2018-09-24 stsp goto done;
1217 e5a0f69f 2018-08-18 stsp view->show = show_log_view;
1218 0cf4efb1 2018-09-29 stsp view->update_siblings = update_siblings_log_view;
1219 e5a0f69f 2018-08-18 stsp view->input = input_log_view;
1220 e5a0f69f 2018-08-18 stsp view->close = close_log_view;
1221 bcbd79e2 2018-08-19 stsp view->set_child = set_child_log_view;
1223 ba4f502b 2018-08-04 stsp return err;
1226 e5a0f69f 2018-08-18 stsp static const struct got_error *
1227 e5a0f69f 2018-08-18 stsp close_log_view(struct tog_view *view)
1229 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
1231 fb2756b9 2018-08-04 stsp if (s->graph)
1232 fb2756b9 2018-08-04 stsp got_commit_graph_close(s->graph);
1233 fb2756b9 2018-08-04 stsp free_commits(&s->commits);
1234 fb2756b9 2018-08-04 stsp free(s->in_repo_path);
1235 5036bf37 2018-09-24 stsp free(s->start_id);
1236 e5a0f69f 2018-08-18 stsp return NULL;
1239 ba4f502b 2018-08-04 stsp static const struct got_error *
1240 c1fea1f8 2018-09-29 stsp update_diff_view(struct tog_view *diff_view,
1241 c1fea1f8 2018-09-29 stsp struct got_object_id *commit_id, struct got_commit_object *commit,
1242 c1fea1f8 2018-09-29 stsp struct got_repository *repo)
1244 bcbd79e2 2018-08-19 stsp const struct got_error *err = NULL;
1245 bcbd79e2 2018-08-19 stsp struct tog_diff_view_state *ds;
1246 bcbd79e2 2018-08-19 stsp struct got_object *obj1 = NULL, *obj2 = NULL;
1247 bcbd79e2 2018-08-19 stsp struct got_object_qid *parent_id;
1249 c1fea1f8 2018-09-29 stsp ds = &diff_view->state.diff;
1250 c1fea1f8 2018-09-29 stsp if (got_object_id_cmp(ds->id2, commit_id) == 0)
1251 bcbd79e2 2018-08-19 stsp return NULL;
1253 c1fea1f8 2018-09-29 stsp err = got_object_open(&obj2, repo, commit_id);
1255 bcbd79e2 2018-08-19 stsp return err;
1257 c1fea1f8 2018-09-29 stsp parent_id = SIMPLEQ_FIRST(&commit->parent_ids);
1258 bcbd79e2 2018-08-19 stsp if (parent_id) {
1259 bcbd79e2 2018-08-19 stsp err = got_object_open(&obj1, repo, parent_id->id);
1261 bcbd79e2 2018-08-19 stsp goto done;
1264 c1fea1f8 2018-09-29 stsp err = close_diff_view(diff_view);
1266 bcbd79e2 2018-08-19 stsp goto done;
1268 c1fea1f8 2018-09-29 stsp err = open_diff_view(diff_view, obj1, obj2, repo);
1270 bcbd79e2 2018-08-19 stsp goto done;
1273 bcbd79e2 2018-08-19 stsp got_object_close(obj1);
1275 bcbd79e2 2018-08-19 stsp got_object_close(obj2);
1276 bcbd79e2 2018-08-19 stsp return err;
1279 bcbd79e2 2018-08-19 stsp static const struct got_error *
1280 ba4f502b 2018-08-04 stsp show_log_view(struct tog_view *view)
1282 bcbd79e2 2018-08-19 stsp const struct got_error *err = NULL;
1283 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
1285 0cf4efb1 2018-09-29 stsp return draw_commits(view, &s->last_displayed_entry,
1286 e5a0f69f 2018-08-18 stsp &s->selected_entry, s->first_displayed_entry,
1287 e5a0f69f 2018-08-18 stsp &s->commits, s->selected, view->nlines, s->graph,
1288 e5a0f69f 2018-08-18 stsp s->repo, s->in_repo_path);
1290 bcbd79e2 2018-08-19 stsp return err;
1293 0cf4efb1 2018-09-29 stsp static const struct got_error *
1294 0cf4efb1 2018-09-29 stsp update_siblings_log_view(int *updated, struct tog_view *view,
1295 0cf4efb1 2018-09-29 stsp struct tog_view *sibling_view)
1297 0cf4efb1 2018-09-29 stsp const struct got_error *err = NULL;
1298 0cf4efb1 2018-09-29 stsp struct tog_log_view_state *s = &view->state.log;
1300 0cf4efb1 2018-09-29 stsp switch (sibling_view->type) {
1301 0cf4efb1 2018-09-29 stsp case TOG_VIEW_DIFF:
1302 0cf4efb1 2018-09-29 stsp err = update_diff_view(sibling_view, s->selected_entry->id,
1303 c1fea1f8 2018-09-29 stsp s->selected_entry->commit, s->repo);
1304 0cf4efb1 2018-09-29 stsp *updated = 1;
1310 c1fea1f8 2018-09-29 stsp return err;
1313 e5a0f69f 2018-08-18 stsp static const struct got_error *
1314 e5a0f69f 2018-08-18 stsp input_log_view(struct tog_view **new_view, struct tog_view **dead_view,
1315 e5a0f69f 2018-08-18 stsp struct tog_view *view, int ch)
1317 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
1318 e5a0f69f 2018-08-18 stsp struct tog_log_view_state *s = &view->state.log;
1319 5036bf37 2018-09-24 stsp char *parent_path;
1321 e5a0f69f 2018-08-18 stsp switch (ch) {
1323 e5a0f69f 2018-08-18 stsp case KEY_UP:
1325 e5a0f69f 2018-08-18 stsp if (s->selected > 0)
1326 e5a0f69f 2018-08-18 stsp s->selected--;
1327 e5a0f69f 2018-08-18 stsp if (s->selected > 0)
1329 e5a0f69f 2018-08-18 stsp scroll_up(&s->first_displayed_entry, 1,
1330 e5a0f69f 2018-08-18 stsp &s->commits);
1332 e5a0f69f 2018-08-18 stsp case KEY_PPAGE:
1333 e5a0f69f 2018-08-18 stsp if (TAILQ_FIRST(&s->commits.head) ==
1334 e5a0f69f 2018-08-18 stsp s->first_displayed_entry) {
1335 e5a0f69f 2018-08-18 stsp s->selected = 0;
1338 e5a0f69f 2018-08-18 stsp scroll_up(&s->first_displayed_entry,
1339 e5a0f69f 2018-08-18 stsp view->nlines, &s->commits);
1342 e5a0f69f 2018-08-18 stsp case KEY_DOWN:
1344 e5a0f69f 2018-08-18 stsp if (s->selected < MIN(view->nlines - 2,
1345 e5a0f69f 2018-08-18 stsp s->commits.ncommits - 1)) {
1346 e5a0f69f 2018-08-18 stsp s->selected++;
1349 e5a0f69f 2018-08-18 stsp err = scroll_down(&s->first_displayed_entry, 1,
1350 93e45b7c 2018-09-24 stsp &s->last_displayed_entry, &s->commits,
1351 e5a0f69f 2018-08-18 stsp s->graph, s->repo, s->in_repo_path);
1352 e5a0f69f 2018-08-18 stsp if (err) {
1353 4d7951a5 2018-08-04 stsp if (err->code != GOT_ERR_ITER_COMPLETED)
1355 4d7951a5 2018-08-04 stsp err = NULL;
1358 e5a0f69f 2018-08-18 stsp case KEY_NPAGE: {
1359 e5a0f69f 2018-08-18 stsp struct commit_queue_entry *first;
1360 e5a0f69f 2018-08-18 stsp first = s->first_displayed_entry;
1361 e5a0f69f 2018-08-18 stsp err = scroll_down(&s->first_displayed_entry,
1362 93e45b7c 2018-09-24 stsp view->nlines, &s->last_displayed_entry,
1363 e5a0f69f 2018-08-18 stsp &s->commits, s->graph, s->repo,
1364 e5a0f69f 2018-08-18 stsp s->in_repo_path);
1365 93e45b7c 2018-09-24 stsp if (err && err->code != GOT_ERR_ITER_COMPLETED)
1367 e5a0f69f 2018-08-18 stsp if (first == s->first_displayed_entry &&
1368 e5a0f69f 2018-08-18 stsp s->selected < MIN(view->nlines - 2,
1369 e5a0f69f 2018-08-18 stsp s->commits.ncommits - 1)) {
1370 e5a0f69f 2018-08-18 stsp /* can't scroll further down */
1371 e5a0f69f 2018-08-18 stsp s->selected = MIN(view->nlines - 2,
1372 e5a0f69f 2018-08-18 stsp s->commits.ncommits - 1);
1374 e5a0f69f 2018-08-18 stsp err = NULL;
1377 e5a0f69f 2018-08-18 stsp case KEY_RESIZE:
1378 e5a0f69f 2018-08-18 stsp if (s->selected > view->nlines - 2)
1379 e5a0f69f 2018-08-18 stsp s->selected = view->nlines - 2;
1380 e5a0f69f 2018-08-18 stsp if (s->selected > s->commits.ncommits - 1)
1381 e5a0f69f 2018-08-18 stsp s->selected = s->commits.ncommits - 1;
1383 e5a0f69f 2018-08-18 stsp case KEY_ENTER:
1384 e5a0f69f 2018-08-18 stsp case '\r':
1385 0cf4efb1 2018-09-29 stsp err = open_diff_view_for_commit(new_view, view->begin_x,
1386 2a4718d3 2018-09-29 stsp s->selected_entry->id, s->selected_entry->commit,
1390 0cf4efb1 2018-09-29 stsp err = browse_commit(new_view, view->begin_x,
1391 0cf4efb1 2018-09-29 stsp s->selected_entry, s->repo);
1393 5036bf37 2018-09-24 stsp case KEY_BACKSPACE:
1394 5036bf37 2018-09-24 stsp if (strcmp(s->in_repo_path, "/") == 0)
1396 5036bf37 2018-09-24 stsp parent_path = dirname(s->in_repo_path);
1397 5036bf37 2018-09-24 stsp if (parent_path && strcmp(parent_path, ".") != 0) {
1398 5036bf37 2018-09-24 stsp struct tog_view *lv;
1399 0cf4efb1 2018-09-29 stsp lv = view_open(view->nlines, view->ncols,
1400 0cf4efb1 2018-09-29 stsp view->begin_y, view->begin_x, TOG_VIEW_LOG);
1401 5036bf37 2018-09-24 stsp if (lv == NULL)
1402 5036bf37 2018-09-24 stsp return got_error_from_errno();
1403 5036bf37 2018-09-24 stsp err = open_log_view(lv, s->start_id, s->repo,
1404 5036bf37 2018-09-24 stsp parent_path);
1407 5036bf37 2018-09-24 stsp *new_view = lv;
1414 80ddbec8 2018-04-29 stsp return err;
1417 4ed7e80c 2018-05-20 stsp static const struct got_error *
1418 9f7d7167 2018-04-29 stsp cmd_log(int argc, char *argv[])
1420 80ddbec8 2018-04-29 stsp const struct got_error *error;
1421 ecb28ae0 2018-07-16 stsp struct got_repository *repo = NULL;
1422 899d86c2 2018-05-10 stsp struct got_object_id *start_id = NULL;
1423 ecb28ae0 2018-07-16 stsp char *path = NULL, *repo_path = NULL, *cwd = NULL;
1424 80ddbec8 2018-04-29 stsp char *start_commit = NULL;
1426 04cc582a 2018-08-01 stsp struct tog_view *view;
1428 80ddbec8 2018-04-29 stsp #ifndef PROFILE
1429 ad242220 2018-09-08 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd", NULL)
1431 80ddbec8 2018-04-29 stsp err(1, "pledge");
1434 ecb28ae0 2018-07-16 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1435 80ddbec8 2018-04-29 stsp switch (ch) {
1437 80ddbec8 2018-04-29 stsp start_commit = optarg;
1440 ecb28ae0 2018-07-16 stsp repo_path = realpath(optarg, NULL);
1441 ecb28ae0 2018-07-16 stsp if (repo_path == NULL)
1442 ecb28ae0 2018-07-16 stsp err(1, "-r option");
1446 80ddbec8 2018-04-29 stsp /* NOTREACHED */
1450 80ddbec8 2018-04-29 stsp argc -= optind;
1451 80ddbec8 2018-04-29 stsp argv += optind;
1453 ecb28ae0 2018-07-16 stsp if (argc == 0)
1454 ecb28ae0 2018-07-16 stsp path = strdup("");
1455 ecb28ae0 2018-07-16 stsp else if (argc == 1)
1456 ecb28ae0 2018-07-16 stsp path = strdup(argv[0]);
1458 80ddbec8 2018-04-29 stsp usage_log();
1459 ecb28ae0 2018-07-16 stsp if (path == NULL)
1460 ecb28ae0 2018-07-16 stsp return got_error_from_errno();
1462 ecb28ae0 2018-07-16 stsp cwd = getcwd(NULL, 0);
1463 ecb28ae0 2018-07-16 stsp if (cwd == NULL) {
1464 ecb28ae0 2018-07-16 stsp error = got_error_from_errno();
1465 ecb28ae0 2018-07-16 stsp goto done;
1467 ecb28ae0 2018-07-16 stsp if (repo_path == NULL) {
1468 ecb28ae0 2018-07-16 stsp repo_path = strdup(cwd);
1469 ecb28ae0 2018-07-16 stsp if (repo_path == NULL) {
1470 ecb28ae0 2018-07-16 stsp error = got_error_from_errno();
1471 ecb28ae0 2018-07-16 stsp goto done;
1475 80ddbec8 2018-04-29 stsp error = got_repo_open(&repo, repo_path);
1476 80ddbec8 2018-04-29 stsp if (error != NULL)
1477 ecb28ae0 2018-07-16 stsp goto done;
1479 80ddbec8 2018-04-29 stsp if (start_commit == NULL) {
1480 899d86c2 2018-05-10 stsp error = get_head_commit_id(&start_id, repo);
1481 80ddbec8 2018-04-29 stsp if (error != NULL)
1482 ecb28ae0 2018-07-16 stsp goto done;
1484 80ddbec8 2018-04-29 stsp struct got_object *obj;
1485 80ddbec8 2018-04-29 stsp error = got_object_open_by_id_str(&obj, repo, start_commit);
1486 80ddbec8 2018-04-29 stsp if (error == NULL) {
1487 6402fb3c 2018-09-15 stsp start_id = got_object_id_dup(got_object_get_id(obj));
1488 899d86c2 2018-05-10 stsp if (start_id == NULL)
1489 80ddbec8 2018-04-29 stsp error = got_error_from_errno();
1490 ecb28ae0 2018-07-16 stsp goto done;
1493 80ddbec8 2018-04-29 stsp if (error != NULL)
1494 ecb28ae0 2018-07-16 stsp goto done;
1496 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
1497 04cc582a 2018-08-01 stsp if (view == NULL) {
1498 04cc582a 2018-08-01 stsp error = got_error_from_errno();
1499 04cc582a 2018-08-01 stsp goto done;
1501 ba4f502b 2018-08-04 stsp error = open_log_view(view, start_id, repo, path);
1502 ba4f502b 2018-08-04 stsp if (error)
1503 ba4f502b 2018-08-04 stsp goto done;
1504 e5a0f69f 2018-08-18 stsp error = view_loop(view);
1506 ecb28ae0 2018-07-16 stsp free(repo_path);
1507 ecb28ae0 2018-07-16 stsp free(cwd);
1508 ecb28ae0 2018-07-16 stsp free(path);
1509 899d86c2 2018-05-10 stsp free(start_id);
1511 ecb28ae0 2018-07-16 stsp got_repo_close(repo);
1512 80ddbec8 2018-04-29 stsp return error;
1515 4ed7e80c 2018-05-20 stsp __dead static void
1516 9f7d7167 2018-04-29 stsp usage_diff(void)
1519 9f7d7167 2018-04-29 stsp fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
1520 9f7d7167 2018-04-29 stsp getprogname());
1524 b304db33 2018-05-20 stsp static char *
1525 b304db33 2018-05-20 stsp parse_next_line(FILE *f, size_t *len)
1527 b304db33 2018-05-20 stsp char *line;
1528 b304db33 2018-05-20 stsp size_t linelen;
1529 b304db33 2018-05-20 stsp size_t lineno;
1530 b304db33 2018-05-20 stsp const char delim[3] = { '\0', '\0', '\0'};
1532 b304db33 2018-05-20 stsp line = fparseln(f, &linelen, &lineno, delim, 0);
1534 b304db33 2018-05-20 stsp *len = linelen;
1535 b304db33 2018-05-20 stsp return line;
1538 4ed7e80c 2018-05-20 stsp static const struct got_error *
1539 f7d12f7e 2018-08-01 stsp draw_file(struct tog_view *view, FILE *f, int *first_displayed_line,
1540 a3404814 2018-09-02 stsp int *last_displayed_line, int *eof, int max_lines,
1541 a3404814 2018-09-02 stsp char * header)
1543 61e69b96 2018-05-20 stsp const struct got_error *err;
1544 26ed57b2 2018-05-19 stsp int nlines = 0, nprinted = 0;
1545 b304db33 2018-05-20 stsp char *line;
1546 b304db33 2018-05-20 stsp size_t len;
1547 61e69b96 2018-05-20 stsp wchar_t *wline;
1548 e0b650dd 2018-05-20 stsp int width;
1550 26ed57b2 2018-05-19 stsp rewind(f);
1551 f7d12f7e 2018-08-01 stsp werase(view->window);
1553 a3404814 2018-09-02 stsp if (header) {
1554 a3404814 2018-09-02 stsp err = format_line(&wline, &width, header, view->ncols);
1555 a3404814 2018-09-02 stsp if (err) {
1556 a3404814 2018-09-02 stsp return err;
1559 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
1560 a3404814 2018-09-02 stsp wstandout(view->window);
1561 a3404814 2018-09-02 stsp waddwstr(view->window, wline);
1562 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
1563 a3404814 2018-09-02 stsp wstandend(view->window);
1564 a3404814 2018-09-02 stsp if (width < view->ncols)
1565 a3404814 2018-09-02 stsp waddch(view->window, '\n');
1567 a3404814 2018-09-02 stsp if (max_lines <= 1)
1568 a3404814 2018-09-02 stsp return NULL;
1569 a3404814 2018-09-02 stsp max_lines--;
1573 26ed57b2 2018-05-19 stsp while (nprinted < max_lines) {
1574 b304db33 2018-05-20 stsp line = parse_next_line(f, &len);
1575 26ed57b2 2018-05-19 stsp if (line == NULL) {
1579 26ed57b2 2018-05-19 stsp if (++nlines < *first_displayed_line) {
1580 26ed57b2 2018-05-19 stsp free(line);
1584 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, line, view->ncols);
1585 61e69b96 2018-05-20 stsp if (err) {
1586 61e69b96 2018-05-20 stsp free(line);
1587 61e69b96 2018-05-20 stsp return err;
1589 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
1590 f7d12f7e 2018-08-01 stsp if (width < view->ncols)
1591 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
1592 26ed57b2 2018-05-19 stsp if (++nprinted == 1)
1593 26ed57b2 2018-05-19 stsp *first_displayed_line = nlines;
1594 26ed57b2 2018-05-19 stsp free(line);
1595 2550e4c3 2018-07-13 stsp free(wline);
1596 2550e4c3 2018-07-13 stsp wline = NULL;
1598 26ed57b2 2018-05-19 stsp *last_displayed_line = nlines;
1600 1a57306a 2018-09-02 stsp view_vborder(view);
1602 26ed57b2 2018-05-19 stsp return NULL;
1605 4ed7e80c 2018-05-20 stsp static const struct got_error *
1606 5dc9f4bc 2018-08-04 stsp open_diff_view(struct tog_view *view, struct got_object *obj1,
1607 ea5e7bb5 2018-08-01 stsp struct got_object *obj2, struct got_repository *repo)
1609 26ed57b2 2018-05-19 stsp const struct got_error *err;
1612 cd0acaa7 2018-05-20 stsp if (obj1 != NULL && obj2 != NULL &&
1613 cd0acaa7 2018-05-20 stsp got_object_get_type(obj1) != got_object_get_type(obj2))
1614 26ed57b2 2018-05-19 stsp return got_error(GOT_ERR_OBJ_TYPE);
1616 511a516b 2018-05-19 stsp f = got_opentemp();
1617 26ed57b2 2018-05-19 stsp if (f == NULL)
1618 26ed57b2 2018-05-19 stsp return got_error_from_errno();
1620 cd0acaa7 2018-05-20 stsp switch (got_object_get_type(obj1 ? obj1 : obj2)) {
1621 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_BLOB:
1622 f6861a81 2018-09-13 stsp err = got_diff_objects_as_blobs(obj1, obj2, NULL, NULL,
1625 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_TREE:
1626 f6861a81 2018-09-13 stsp err = got_diff_objects_as_trees(obj1, obj2, "", "", repo, f);
1628 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_COMMIT:
1629 11528a82 2018-05-19 stsp err = got_diff_objects_as_commits(obj1, obj2, repo, f);
1632 26ed57b2 2018-05-19 stsp return got_error(GOT_ERR_OBJ_TYPE);
1635 26ed57b2 2018-05-19 stsp fflush(f);
1637 a3404814 2018-09-02 stsp view->state.diff.id1 = obj1 ? got_object_get_id(obj1) : NULL;
1638 a3404814 2018-09-02 stsp view->state.diff.id2 = got_object_get_id(obj2);
1639 5dc9f4bc 2018-08-04 stsp view->state.diff.f = f;
1640 5dc9f4bc 2018-08-04 stsp view->state.diff.first_displayed_line = 1;
1641 5dc9f4bc 2018-08-04 stsp view->state.diff.last_displayed_line = view->nlines;
1643 e5a0f69f 2018-08-18 stsp view->show = show_diff_view;
1644 0cf4efb1 2018-09-29 stsp view->update_siblings = update_siblings_diff_view;
1645 e5a0f69f 2018-08-18 stsp view->input = input_diff_view;
1646 e5a0f69f 2018-08-18 stsp view->close = close_diff_view;
1648 5dc9f4bc 2018-08-04 stsp return NULL;
1651 e5a0f69f 2018-08-18 stsp static const struct got_error *
1652 5dc9f4bc 2018-08-04 stsp close_diff_view(struct tog_view *view)
1654 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
1656 e5a0f69f 2018-08-18 stsp if (view->state.diff.f && fclose(view->state.diff.f) == EOF)
1657 e5a0f69f 2018-08-18 stsp err = got_error_from_errno();
1658 e5a0f69f 2018-08-18 stsp return err;
1661 5dc9f4bc 2018-08-04 stsp static const struct got_error *
1662 5dc9f4bc 2018-08-04 stsp show_diff_view(struct tog_view *view)
1664 a3404814 2018-09-02 stsp const struct got_error *err;
1665 fb2756b9 2018-08-04 stsp struct tog_diff_view_state *s = &view->state.diff;
1666 a3404814 2018-09-02 stsp char *id_str1 = NULL, *id_str2, *header;
1668 a3404814 2018-09-02 stsp if (s->id1) {
1669 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str1, s->id1);
1671 a3404814 2018-09-02 stsp return err;
1673 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str2, s->id2);
1675 a3404814 2018-09-02 stsp return err;
1677 a3404814 2018-09-02 stsp if (asprintf(&header, "diff: %s %s",
1678 a3404814 2018-09-02 stsp id_str1 ? id_str1 : "/dev/null", id_str2) == -1) {
1679 a3404814 2018-09-02 stsp err = got_error_from_errno();
1680 a3404814 2018-09-02 stsp free(id_str1);
1681 a3404814 2018-09-02 stsp free(id_str2);
1682 a3404814 2018-09-02 stsp return err;
1684 a3404814 2018-09-02 stsp free(id_str1);
1685 a3404814 2018-09-02 stsp free(id_str2);
1687 e5a0f69f 2018-08-18 stsp return draw_file(view, s->f, &s->first_displayed_line,
1688 a3404814 2018-09-02 stsp &s->last_displayed_line, &s->eof, view->nlines,
1692 e5a0f69f 2018-08-18 stsp static const struct got_error *
1693 0cf4efb1 2018-09-29 stsp update_siblings_diff_view(int *updated, struct tog_view *view,
1694 0cf4efb1 2018-09-29 stsp struct tog_view *sibling_view)
1696 0cf4efb1 2018-09-29 stsp return NULL;
1699 0cf4efb1 2018-09-29 stsp static const struct got_error *
1700 bcbd79e2 2018-08-19 stsp input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
1701 e5a0f69f 2018-08-18 stsp struct tog_view *view, int ch)
1703 bcbd79e2 2018-08-19 stsp const struct got_error *err = NULL;
1704 e5a0f69f 2018-08-18 stsp struct tog_diff_view_state *s = &view->state.diff;
1707 e5a0f69f 2018-08-18 stsp switch (ch) {
1709 e5a0f69f 2018-08-18 stsp case KEY_UP:
1710 e5a0f69f 2018-08-18 stsp if (s->first_displayed_line > 1)
1711 e5a0f69f 2018-08-18 stsp s->first_displayed_line--;
1713 e5a0f69f 2018-08-18 stsp case KEY_PPAGE:
1715 e5a0f69f 2018-08-18 stsp while (i++ < view->nlines - 1 &&
1716 e5a0f69f 2018-08-18 stsp s->first_displayed_line > 1)
1717 e5a0f69f 2018-08-18 stsp s->first_displayed_line--;
1720 e5a0f69f 2018-08-18 stsp case KEY_DOWN:
1721 e5a0f69f 2018-08-18 stsp if (!s->eof)
1722 e5a0f69f 2018-08-18 stsp s->first_displayed_line++;
1724 e5a0f69f 2018-08-18 stsp case KEY_NPAGE:
1727 e5a0f69f 2018-08-18 stsp while (!s->eof && i++ < view->nlines - 1) {
1728 e5a0f69f 2018-08-18 stsp char *line;
1729 e5a0f69f 2018-08-18 stsp line = parse_next_line(s->f, NULL);
1730 e5a0f69f 2018-08-18 stsp s->first_displayed_line++;
1731 e5a0f69f 2018-08-18 stsp if (line == NULL)
1739 bcbd79e2 2018-08-19 stsp return err;
1742 4ed7e80c 2018-05-20 stsp static const struct got_error *
1743 9f7d7167 2018-04-29 stsp cmd_diff(int argc, char *argv[])
1745 26ed57b2 2018-05-19 stsp const struct got_error *error = NULL;
1746 26ed57b2 2018-05-19 stsp struct got_repository *repo = NULL;
1747 26ed57b2 2018-05-19 stsp struct got_object *obj1 = NULL, *obj2 = NULL;
1748 26ed57b2 2018-05-19 stsp char *repo_path = NULL;
1749 26ed57b2 2018-05-19 stsp char *obj_id_str1 = NULL, *obj_id_str2 = NULL;
1751 ea5e7bb5 2018-08-01 stsp struct tog_view *view;
1753 26ed57b2 2018-05-19 stsp #ifndef PROFILE
1754 ad242220 2018-09-08 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd", NULL)
1756 26ed57b2 2018-05-19 stsp err(1, "pledge");
1759 26ed57b2 2018-05-19 stsp while ((ch = getopt(argc, argv, "")) != -1) {
1760 26ed57b2 2018-05-19 stsp switch (ch) {
1763 26ed57b2 2018-05-19 stsp /* NOTREACHED */
1767 26ed57b2 2018-05-19 stsp argc -= optind;
1768 26ed57b2 2018-05-19 stsp argv += optind;
1770 26ed57b2 2018-05-19 stsp if (argc == 0) {
1771 26ed57b2 2018-05-19 stsp usage_diff(); /* TODO show local worktree changes */
1772 26ed57b2 2018-05-19 stsp } else if (argc == 2) {
1773 26ed57b2 2018-05-19 stsp repo_path = getcwd(NULL, 0);
1774 26ed57b2 2018-05-19 stsp if (repo_path == NULL)
1775 26ed57b2 2018-05-19 stsp return got_error_from_errno();
1776 26ed57b2 2018-05-19 stsp obj_id_str1 = argv[0];
1777 26ed57b2 2018-05-19 stsp obj_id_str2 = argv[1];
1778 26ed57b2 2018-05-19 stsp } else if (argc == 3) {
1779 26ed57b2 2018-05-19 stsp repo_path = realpath(argv[0], NULL);
1780 26ed57b2 2018-05-19 stsp if (repo_path == NULL)
1781 26ed57b2 2018-05-19 stsp return got_error_from_errno();
1782 26ed57b2 2018-05-19 stsp obj_id_str1 = argv[1];
1783 26ed57b2 2018-05-19 stsp obj_id_str2 = argv[2];
1785 26ed57b2 2018-05-19 stsp usage_diff();
1787 26ed57b2 2018-05-19 stsp error = got_repo_open(&repo, repo_path);
1788 26ed57b2 2018-05-19 stsp free(repo_path);
1789 26ed57b2 2018-05-19 stsp if (error)
1790 26ed57b2 2018-05-19 stsp goto done;
1792 26ed57b2 2018-05-19 stsp error = got_object_open_by_id_str(&obj1, repo, obj_id_str1);
1793 26ed57b2 2018-05-19 stsp if (error)
1794 26ed57b2 2018-05-19 stsp goto done;
1796 26ed57b2 2018-05-19 stsp error = got_object_open_by_id_str(&obj2, repo, obj_id_str2);
1797 26ed57b2 2018-05-19 stsp if (error)
1798 26ed57b2 2018-05-19 stsp goto done;
1800 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
1801 ea5e7bb5 2018-08-01 stsp if (view == NULL) {
1802 ea5e7bb5 2018-08-01 stsp error = got_error_from_errno();
1803 ea5e7bb5 2018-08-01 stsp goto done;
1805 5dc9f4bc 2018-08-04 stsp error = open_diff_view(view, obj1, obj2, repo);
1806 5dc9f4bc 2018-08-04 stsp if (error)
1807 5dc9f4bc 2018-08-04 stsp goto done;
1808 e5a0f69f 2018-08-18 stsp error = view_loop(view);
1810 26ed57b2 2018-05-19 stsp got_repo_close(repo);
1812 26ed57b2 2018-05-19 stsp got_object_close(obj1);
1814 26ed57b2 2018-05-19 stsp got_object_close(obj2);
1815 26ed57b2 2018-05-19 stsp return error;
1818 4ed7e80c 2018-05-20 stsp __dead static void
1819 9f7d7167 2018-04-29 stsp usage_blame(void)
1822 69069811 2018-08-02 stsp fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
1823 9f7d7167 2018-04-29 stsp getprogname());
1827 84451b3e 2018-07-10 stsp struct tog_blame_line {
1828 84451b3e 2018-07-10 stsp int annotated;
1829 84451b3e 2018-07-10 stsp struct got_object_id *id;
1832 4ed7e80c 2018-05-20 stsp static const struct got_error *
1833 f7d12f7e 2018-08-01 stsp draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
1834 f7d12f7e 2018-08-01 stsp const char *path, struct tog_blame_line *lines, int nlines,
1835 f7d12f7e 2018-08-01 stsp int blame_complete, int selected_line, int *first_displayed_line,
1836 f7d12f7e 2018-08-01 stsp int *last_displayed_line, int *eof, int max_lines)
1838 84451b3e 2018-07-10 stsp const struct got_error *err;
1839 84451b3e 2018-07-10 stsp int lineno = 0, nprinted = 0;
1840 84451b3e 2018-07-10 stsp char *line;
1841 84451b3e 2018-07-10 stsp size_t len;
1842 84451b3e 2018-07-10 stsp wchar_t *wline;
1843 b700b5d6 2018-07-10 stsp int width, wlimit;
1844 84451b3e 2018-07-10 stsp struct tog_blame_line *blame_line;
1845 ee41ec32 2018-07-10 stsp struct got_object_id *prev_id = NULL;
1846 ab089a2a 2018-07-12 stsp char *id_str;
1848 ab089a2a 2018-07-12 stsp err = got_object_id_str(&id_str, id);
1850 ab089a2a 2018-07-12 stsp return err;
1852 84451b3e 2018-07-10 stsp rewind(f);
1853 f7d12f7e 2018-08-01 stsp werase(view->window);
1855 ab089a2a 2018-07-12 stsp if (asprintf(&line, "commit: %s", id_str) == -1) {
1856 ab089a2a 2018-07-12 stsp err = got_error_from_errno();
1857 ab089a2a 2018-07-12 stsp free(id_str);
1858 ab089a2a 2018-07-12 stsp return err;
1861 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, line, view->ncols);
1862 ab089a2a 2018-07-12 stsp free(line);
1863 2550e4c3 2018-07-13 stsp line = NULL;
1864 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
1865 a3404814 2018-09-02 stsp wstandout(view->window);
1866 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
1867 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
1868 a3404814 2018-09-02 stsp wstandend(view->window);
1869 2550e4c3 2018-07-13 stsp free(wline);
1870 2550e4c3 2018-07-13 stsp wline = NULL;
1871 f7d12f7e 2018-08-01 stsp if (width < view->ncols)
1872 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
1874 084063cd 2018-07-12 stsp if (asprintf(&line, "[%d/%d] %s%s",
1875 084063cd 2018-07-12 stsp *first_displayed_line - 1 + selected_line, nlines,
1876 12a0b23b 2018-07-12 stsp blame_complete ? "" : "annotating ", path) == -1) {
1877 ab089a2a 2018-07-12 stsp free(id_str);
1878 3f60a8ef 2018-07-10 stsp return got_error_from_errno();
1880 ab089a2a 2018-07-12 stsp free(id_str);
1881 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, line, view->ncols);
1882 3f60a8ef 2018-07-10 stsp free(line);
1883 2550e4c3 2018-07-13 stsp line = NULL;
1885 3f60a8ef 2018-07-10 stsp return err;
1886 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
1887 2550e4c3 2018-07-13 stsp free(wline);
1888 2550e4c3 2018-07-13 stsp wline = NULL;
1889 f7d12f7e 2018-08-01 stsp if (width < view->ncols)
1890 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
1893 ab089a2a 2018-07-12 stsp while (nprinted < max_lines - 2) {
1894 84451b3e 2018-07-10 stsp line = parse_next_line(f, &len);
1895 84451b3e 2018-07-10 stsp if (line == NULL) {
1899 84451b3e 2018-07-10 stsp if (++lineno < *first_displayed_line) {
1900 84451b3e 2018-07-10 stsp free(line);
1904 f7d12f7e 2018-08-01 stsp wlimit = view->ncols < 9 ? 0 : view->ncols - 9;
1905 b700b5d6 2018-07-10 stsp err = format_line(&wline, &width, line, wlimit);
1906 84451b3e 2018-07-10 stsp if (err) {
1907 84451b3e 2018-07-10 stsp free(line);
1908 84451b3e 2018-07-10 stsp return err;
1911 0cf4efb1 2018-09-29 stsp if (view->focussed && nprinted == selected_line - 1)
1912 f7d12f7e 2018-08-01 stsp wstandout(view->window);
1914 84451b3e 2018-07-10 stsp blame_line = &lines[lineno - 1];
1915 ee41ec32 2018-07-10 stsp if (blame_line->annotated && prev_id &&
1916 ee41ec32 2018-07-10 stsp got_object_id_cmp(prev_id, blame_line->id) == 0)
1917 f7d12f7e 2018-08-01 stsp waddstr(view->window, " ");
1918 ee41ec32 2018-07-10 stsp else if (blame_line->annotated) {
1919 84451b3e 2018-07-10 stsp char *id_str;
1920 84451b3e 2018-07-10 stsp err = got_object_id_str(&id_str, blame_line->id);
1921 84451b3e 2018-07-10 stsp if (err) {
1922 84451b3e 2018-07-10 stsp free(line);
1923 2550e4c3 2018-07-13 stsp free(wline);
1924 84451b3e 2018-07-10 stsp return err;
1926 f7d12f7e 2018-08-01 stsp wprintw(view->window, "%.8s ", id_str);
1927 84451b3e 2018-07-10 stsp free(id_str);
1928 ee41ec32 2018-07-10 stsp prev_id = blame_line->id;
1930 f7d12f7e 2018-08-01 stsp waddstr(view->window, "........ ");
1931 ee41ec32 2018-07-10 stsp prev_id = NULL;
1934 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
1935 b700b5d6 2018-07-10 stsp while (width < wlimit) {
1936 f7d12f7e 2018-08-01 stsp waddch(view->window, ' ');
1939 0cf4efb1 2018-09-29 stsp if (view->focussed && nprinted == selected_line - 1)
1940 f7d12f7e 2018-08-01 stsp wstandend(view->window);
1941 84451b3e 2018-07-10 stsp if (++nprinted == 1)
1942 84451b3e 2018-07-10 stsp *first_displayed_line = lineno;
1943 84451b3e 2018-07-10 stsp free(line);
1944 2550e4c3 2018-07-13 stsp free(wline);
1945 2550e4c3 2018-07-13 stsp wline = NULL;
1947 84451b3e 2018-07-10 stsp *last_displayed_line = lineno;
1949 1a57306a 2018-09-02 stsp view_vborder(view);
1951 84451b3e 2018-07-10 stsp return NULL;
1954 84451b3e 2018-07-10 stsp static const struct got_error *
1955 84451b3e 2018-07-10 stsp blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
1957 84451b3e 2018-07-10 stsp const struct got_error *err = NULL;
1958 84451b3e 2018-07-10 stsp struct tog_blame_cb_args *a = arg;
1959 84451b3e 2018-07-10 stsp struct tog_blame_line *line;
1961 d68a0a7d 2018-07-10 stsp if (nlines != a->nlines ||
1962 d68a0a7d 2018-07-10 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
1963 84451b3e 2018-07-10 stsp return got_error(GOT_ERR_RANGE);
1965 84451b3e 2018-07-10 stsp if (pthread_mutex_lock(a->mutex) != 0)
1966 84451b3e 2018-07-10 stsp return got_error_from_errno();
1968 18430de3 2018-07-10 stsp if (*a->quit) { /* user has quit the blame view */
1969 d68a0a7d 2018-07-10 stsp err = got_error(GOT_ERR_ITER_COMPLETED);
1970 d68a0a7d 2018-07-10 stsp goto done;
1973 d68a0a7d 2018-07-10 stsp if (lineno == -1)
1974 d68a0a7d 2018-07-10 stsp goto done; /* no change in this commit */
1976 84451b3e 2018-07-10 stsp line = &a->lines[lineno - 1];
1977 d68a0a7d 2018-07-10 stsp if (line->annotated)
1978 d68a0a7d 2018-07-10 stsp goto done;
1980 84451b3e 2018-07-10 stsp line->id = got_object_id_dup(id);
1981 84451b3e 2018-07-10 stsp if (line->id == NULL) {
1982 84451b3e 2018-07-10 stsp err = got_error_from_errno();
1983 84451b3e 2018-07-10 stsp goto done;
1985 84451b3e 2018-07-10 stsp line->annotated = 1;
1987 f7d12f7e 2018-08-01 stsp err = draw_blame(a->view, a->commit_id, a->f, a->path,
1988 245d91c1 2018-07-12 stsp a->lines, a->nlines, 0, *a->selected_line, a->first_displayed_line,
1989 e5a0f69f 2018-08-18 stsp a->last_displayed_line, a->eof, a->view->nlines);
1991 84451b3e 2018-07-10 stsp if (pthread_mutex_unlock(a->mutex) != 0)
1992 84451b3e 2018-07-10 stsp return got_error_from_errno();
1993 84451b3e 2018-07-10 stsp return err;
1996 84451b3e 2018-07-10 stsp static void *
1997 84451b3e 2018-07-10 stsp blame_thread(void *arg)
1999 18430de3 2018-07-10 stsp const struct got_error *err;
2000 18430de3 2018-07-10 stsp struct tog_blame_thread_args *ta = arg;
2001 245d91c1 2018-07-12 stsp struct tog_blame_cb_args *a = ta->cb_args;
2003 ab089a2a 2018-07-12 stsp err = got_blame_incremental(ta->path, a->commit_id, ta->repo,
2004 245d91c1 2018-07-12 stsp blame_cb, ta->cb_args);
2006 18430de3 2018-07-10 stsp if (pthread_mutex_lock(a->mutex) != 0)
2007 18430de3 2018-07-10 stsp return (void *)got_error_from_errno();
2009 c9beca56 2018-07-22 stsp got_repo_close(ta->repo);
2010 c9beca56 2018-07-22 stsp ta->repo = NULL;
2011 c9beca56 2018-07-22 stsp *ta->complete = 1;
2013 f7d12f7e 2018-08-01 stsp err = draw_blame(a->view, a->commit_id, a->f, a->path,
2014 f7d12f7e 2018-08-01 stsp a->lines, a->nlines, 1, *a->selected_line,
2015 e5a0f69f 2018-08-18 stsp a->first_displayed_line, a->last_displayed_line, a->eof,
2016 f7d12f7e 2018-08-01 stsp a->view->nlines);
2018 18430de3 2018-07-10 stsp if (pthread_mutex_unlock(a->mutex) != 0 && err == NULL)
2019 18430de3 2018-07-10 stsp err = got_error_from_errno();
2021 18430de3 2018-07-10 stsp return (void *)err;
2024 245d91c1 2018-07-12 stsp static struct got_object_id *
2025 245d91c1 2018-07-12 stsp get_selected_commit_id(struct tog_blame_line *lines,
2026 245d91c1 2018-07-12 stsp int first_displayed_line, int selected_line)
2028 245d91c1 2018-07-12 stsp struct tog_blame_line *line;
2030 245d91c1 2018-07-12 stsp line = &lines[first_displayed_line - 1 + selected_line - 1];
2031 245d91c1 2018-07-12 stsp if (!line->annotated)
2032 245d91c1 2018-07-12 stsp return NULL;
2034 245d91c1 2018-07-12 stsp return line->id;
2037 84451b3e 2018-07-10 stsp static const struct got_error *
2038 245d91c1 2018-07-12 stsp open_selected_commit(struct got_object **pobj, struct got_object **obj,
2039 b880a918 2018-07-10 stsp struct tog_blame_line *lines, int first_displayed_line,
2040 b880a918 2018-07-10 stsp int selected_line, struct got_repository *repo)
2042 b880a918 2018-07-10 stsp const struct got_error *err = NULL;
2043 b880a918 2018-07-10 stsp struct got_commit_object *commit = NULL;
2044 245d91c1 2018-07-12 stsp struct got_object_id *selected_id;
2045 b880a918 2018-07-10 stsp struct got_object_qid *pid;
2047 b880a918 2018-07-10 stsp *pobj = NULL;
2048 b880a918 2018-07-10 stsp *obj = NULL;
2050 245d91c1 2018-07-12 stsp selected_id = get_selected_commit_id(lines,
2051 245d91c1 2018-07-12 stsp first_displayed_line, selected_line);
2052 245d91c1 2018-07-12 stsp if (selected_id == NULL)
2053 b880a918 2018-07-10 stsp return NULL;
2055 245d91c1 2018-07-12 stsp err = got_object_open(obj, repo, selected_id);
2057 b880a918 2018-07-10 stsp goto done;
2059 b880a918 2018-07-10 stsp err = got_object_commit_open(&commit, repo, *obj);
2061 b880a918 2018-07-10 stsp goto done;
2063 b880a918 2018-07-10 stsp pid = SIMPLEQ_FIRST(&commit->parent_ids);
2064 b880a918 2018-07-10 stsp if (pid) {
2065 b880a918 2018-07-10 stsp err = got_object_open(pobj, repo, pid->id);
2067 b880a918 2018-07-10 stsp goto done;
2070 b880a918 2018-07-10 stsp if (commit)
2071 b880a918 2018-07-10 stsp got_object_commit_close(commit);
2072 b880a918 2018-07-10 stsp return err;
2075 b880a918 2018-07-10 stsp static const struct got_error *
2076 245d91c1 2018-07-12 stsp stop_blame(struct tog_blame *blame)
2078 a70480e0 2018-06-23 stsp const struct got_error *err = NULL;
2081 245d91c1 2018-07-12 stsp if (blame->thread) {
2082 245d91c1 2018-07-12 stsp if (pthread_join(blame->thread, (void **)&err) != 0)
2083 245d91c1 2018-07-12 stsp err = got_error_from_errno();
2084 245d91c1 2018-07-12 stsp if (err && err->code == GOT_ERR_ITER_COMPLETED)
2085 245d91c1 2018-07-12 stsp err = NULL;
2086 245d91c1 2018-07-12 stsp blame->thread = NULL;
2088 245d91c1 2018-07-12 stsp if (blame->thread_args.repo) {
2089 245d91c1 2018-07-12 stsp got_repo_close(blame->thread_args.repo);
2090 245d91c1 2018-07-12 stsp blame->thread_args.repo = NULL;
2092 245d91c1 2018-07-12 stsp if (blame->f) {
2093 245d91c1 2018-07-12 stsp fclose(blame->f);
2094 245d91c1 2018-07-12 stsp blame->f = NULL;
2096 245d91c1 2018-07-12 stsp for (i = 0; i < blame->nlines; i++)
2097 245d91c1 2018-07-12 stsp free(blame->lines[i].id);
2098 245d91c1 2018-07-12 stsp free(blame->lines);
2099 245d91c1 2018-07-12 stsp blame->lines = NULL;
2100 75c32340 2018-07-23 stsp free(blame->cb_args.commit_id);
2101 75c32340 2018-07-23 stsp blame->cb_args.commit_id = NULL;
2103 245d91c1 2018-07-12 stsp return err;
2106 245d91c1 2018-07-12 stsp static const struct got_error *
2107 7cc84d77 2018-08-01 stsp run_blame(struct tog_blame *blame, pthread_mutex_t *mutex,
2108 7cc84d77 2018-08-01 stsp struct tog_view *view, int *blame_complete,
2109 245d91c1 2018-07-12 stsp int *first_displayed_line, int *last_displayed_line,
2110 e5a0f69f 2018-08-18 stsp int *selected_line, int *done, int *eof, const char *path,
2111 245d91c1 2018-07-12 stsp struct got_object_id *commit_id,
2112 245d91c1 2018-07-12 stsp struct got_repository *repo)
2114 245d91c1 2018-07-12 stsp const struct got_error *err = NULL;
2115 84451b3e 2018-07-10 stsp struct got_blob_object *blob = NULL;
2116 245d91c1 2018-07-12 stsp struct got_repository *thread_repo = NULL;
2117 27d434c2 2018-09-15 stsp struct got_object_id *obj_id = NULL;
2118 27d434c2 2018-09-15 stsp struct got_object *obj = NULL;
2120 27d434c2 2018-09-15 stsp err = got_object_id_by_path(&obj_id, repo, commit_id, path);
2122 27d434c2 2018-09-15 stsp goto done;
2124 27d434c2 2018-09-15 stsp err = got_object_open(&obj, repo, obj_id);
2126 84451b3e 2018-07-10 stsp goto done;
2128 84451b3e 2018-07-10 stsp if (got_object_get_type(obj) != GOT_OBJ_TYPE_BLOB) {
2129 84451b3e 2018-07-10 stsp err = got_error(GOT_ERR_OBJ_TYPE);
2130 84451b3e 2018-07-10 stsp goto done;
2133 84451b3e 2018-07-10 stsp err = got_object_blob_open(&blob, repo, obj, 8192);
2135 a70480e0 2018-06-23 stsp goto done;
2136 245d91c1 2018-07-12 stsp blame->f = got_opentemp();
2137 245d91c1 2018-07-12 stsp if (blame->f == NULL) {
2138 84451b3e 2018-07-10 stsp err = got_error_from_errno();
2139 84451b3e 2018-07-10 stsp goto done;
2141 245d91c1 2018-07-12 stsp err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
2142 245d91c1 2018-07-12 stsp blame->f, blob);
2144 84451b3e 2018-07-10 stsp goto done;
2146 245d91c1 2018-07-12 stsp blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
2147 245d91c1 2018-07-12 stsp if (blame->lines == NULL) {
2148 84451b3e 2018-07-10 stsp err = got_error_from_errno();
2149 84451b3e 2018-07-10 stsp goto done;
2152 245d91c1 2018-07-12 stsp err = got_repo_open(&thread_repo, got_repo_get_path(repo));
2154 bd24772e 2018-07-11 stsp goto done;
2156 7cc84d77 2018-08-01 stsp blame->cb_args.view = view;
2157 245d91c1 2018-07-12 stsp blame->cb_args.lines = blame->lines;
2158 245d91c1 2018-07-12 stsp blame->cb_args.nlines = blame->nlines;
2159 245d91c1 2018-07-12 stsp blame->cb_args.mutex = mutex;
2160 245d91c1 2018-07-12 stsp blame->cb_args.commit_id = got_object_id_dup(commit_id);
2161 245d91c1 2018-07-12 stsp if (blame->cb_args.commit_id == NULL) {
2162 245d91c1 2018-07-12 stsp err = got_error_from_errno();
2163 245d91c1 2018-07-12 stsp goto done;
2165 245d91c1 2018-07-12 stsp blame->cb_args.f = blame->f;
2166 245d91c1 2018-07-12 stsp blame->cb_args.path = path;
2167 245d91c1 2018-07-12 stsp blame->cb_args.first_displayed_line = first_displayed_line;
2168 245d91c1 2018-07-12 stsp blame->cb_args.selected_line = selected_line;
2169 245d91c1 2018-07-12 stsp blame->cb_args.last_displayed_line = last_displayed_line;
2170 245d91c1 2018-07-12 stsp blame->cb_args.quit = done;
2171 e5a0f69f 2018-08-18 stsp blame->cb_args.eof = eof;
2173 245d91c1 2018-07-12 stsp blame->thread_args.path = path;
2174 245d91c1 2018-07-12 stsp blame->thread_args.repo = thread_repo;
2175 245d91c1 2018-07-12 stsp blame->thread_args.cb_args = &blame->cb_args;
2176 245d91c1 2018-07-12 stsp blame->thread_args.complete = blame_complete;
2177 245d91c1 2018-07-12 stsp *blame_complete = 0;
2179 245d91c1 2018-07-12 stsp if (pthread_create(&blame->thread, NULL, blame_thread,
2180 245d91c1 2018-07-12 stsp &blame->thread_args) != 0) {
2181 245d91c1 2018-07-12 stsp err = got_error_from_errno();
2182 245d91c1 2018-07-12 stsp goto done;
2187 245d91c1 2018-07-12 stsp got_object_blob_close(blob);
2188 27d434c2 2018-09-15 stsp free(obj_id);
2190 245d91c1 2018-07-12 stsp got_object_close(obj);
2192 245d91c1 2018-07-12 stsp stop_blame(blame);
2193 245d91c1 2018-07-12 stsp return err;
2196 245d91c1 2018-07-12 stsp static const struct got_error *
2197 e5a0f69f 2018-08-18 stsp open_blame_view(struct tog_view *view, char *path,
2198 e1cd8fed 2018-08-01 stsp struct got_object_id *commit_id, struct got_repository *repo)
2200 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL;
2201 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
2203 fb2756b9 2018-08-04 stsp SIMPLEQ_INIT(&s->blamed_commits);
2205 fb2756b9 2018-08-04 stsp if (pthread_mutex_init(&s->mutex, NULL) != 0)
2206 7cbe629d 2018-08-04 stsp return got_error_from_errno();
2208 fb2756b9 2018-08-04 stsp err = got_object_qid_alloc(&s->blamed_commit, commit_id);
2210 7cbe629d 2018-08-04 stsp return err;
2212 fb2756b9 2018-08-04 stsp SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
2213 fb2756b9 2018-08-04 stsp s->first_displayed_line = 1;
2214 fb2756b9 2018-08-04 stsp s->last_displayed_line = view->nlines;
2215 fb2756b9 2018-08-04 stsp s->selected_line = 1;
2216 fb2756b9 2018-08-04 stsp s->blame_complete = 0;
2217 fb2756b9 2018-08-04 stsp s->path = path;
2218 e5a0f69f 2018-08-18 stsp if (s->path == NULL)
2219 e5a0f69f 2018-08-18 stsp return got_error_from_errno();
2220 fb2756b9 2018-08-04 stsp s->repo = repo;
2221 fb2756b9 2018-08-04 stsp s->commit_id = commit_id;
2222 e9424729 2018-08-04 stsp memset(&s->blame, 0, sizeof(s->blame));
2224 e5a0f69f 2018-08-18 stsp view->show = show_blame_view;
2225 0cf4efb1 2018-09-29 stsp view->update_siblings = update_siblings_blame_view;
2226 e5a0f69f 2018-08-18 stsp view->input = input_blame_view;
2227 e5a0f69f 2018-08-18 stsp view->close = close_blame_view;
2229 e5a0f69f 2018-08-18 stsp return run_blame(&s->blame, &s->mutex, view, &s->blame_complete,
2230 e5a0f69f 2018-08-18 stsp &s->first_displayed_line, &s->last_displayed_line,
2231 e5a0f69f 2018-08-18 stsp &s->selected_line, &s->done, &s->eof, s->path,
2232 e5a0f69f 2018-08-18 stsp s->blamed_commit->id, s->repo);
2235 e5a0f69f 2018-08-18 stsp static const struct got_error *
2236 7cbe629d 2018-08-04 stsp close_blame_view(struct tog_view *view)
2238 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
2239 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
2241 e5a0f69f 2018-08-18 stsp if (s->blame.thread)
2242 e5a0f69f 2018-08-18 stsp err = stop_blame(&s->blame);
2244 fb2756b9 2018-08-04 stsp while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
2245 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
2246 fb2756b9 2018-08-04 stsp blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
2247 fb2756b9 2018-08-04 stsp SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
2248 7cbe629d 2018-08-04 stsp got_object_qid_free(blamed_commit);
2251 e5a0f69f 2018-08-18 stsp free(s->path);
2253 e5a0f69f 2018-08-18 stsp return err;
2256 7cbe629d 2018-08-04 stsp static const struct got_error *
2257 7cbe629d 2018-08-04 stsp show_blame_view(struct tog_view *view)
2259 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
2260 e5a0f69f 2018-08-18 stsp struct tog_blame_view_state *s = &view->state.blame;
2262 e5a0f69f 2018-08-18 stsp if (pthread_mutex_lock(&s->mutex) != 0)
2263 e5a0f69f 2018-08-18 stsp return got_error_from_errno();
2265 e5a0f69f 2018-08-18 stsp err = draw_blame(view, s->blamed_commit->id, s->blame.f,
2266 e5a0f69f 2018-08-18 stsp s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
2267 e5a0f69f 2018-08-18 stsp s->selected_line, &s->first_displayed_line,
2268 e5a0f69f 2018-08-18 stsp &s->last_displayed_line, &s->eof, view->nlines);
2270 e5a0f69f 2018-08-18 stsp if (pthread_mutex_unlock(&s->mutex) != 0 && err == NULL)
2271 e5a0f69f 2018-08-18 stsp err = got_error_from_errno();
2273 e5a0f69f 2018-08-18 stsp return err;
2276 e5a0f69f 2018-08-18 stsp static const struct got_error *
2277 0cf4efb1 2018-09-29 stsp update_siblings_blame_view(int *updated, struct tog_view *view,
2278 0cf4efb1 2018-09-29 stsp struct tog_view *sibling_view)
2280 0cf4efb1 2018-09-29 stsp return NULL;
2283 0cf4efb1 2018-09-29 stsp static const struct got_error *
2284 e5a0f69f 2018-08-18 stsp input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
2285 e5a0f69f 2018-08-18 stsp struct tog_view *view, int ch)
2287 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL, *thread_err = NULL;
2288 7cbe629d 2018-08-04 stsp struct got_object *obj = NULL, *pobj = NULL;
2289 7cbe629d 2018-08-04 stsp struct tog_view *diff_view;
2290 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
2292 e5a0f69f 2018-08-18 stsp if (pthread_mutex_lock(&s->mutex) != 0) {
2293 e5a0f69f 2018-08-18 stsp err = got_error_from_errno();
2294 e5a0f69f 2018-08-18 stsp goto done;
2297 e5a0f69f 2018-08-18 stsp switch (ch) {
2299 e5a0f69f 2018-08-18 stsp s->done = 1;
2300 e5a0f69f 2018-08-18 stsp if (pthread_mutex_unlock(&s->mutex) != 0) {
2301 e5a0f69f 2018-08-18 stsp err = got_error_from_errno();
2302 e5a0f69f 2018-08-18 stsp goto done;
2304 e5a0f69f 2018-08-18 stsp return stop_blame(&s->blame);
2306 e5a0f69f 2018-08-18 stsp case KEY_UP:
2307 e5a0f69f 2018-08-18 stsp if (s->selected_line > 1)
2308 e5a0f69f 2018-08-18 stsp s->selected_line--;
2309 e5a0f69f 2018-08-18 stsp else if (s->selected_line == 1 &&
2310 e5a0f69f 2018-08-18 stsp s->first_displayed_line > 1)
2311 e5a0f69f 2018-08-18 stsp s->first_displayed_line--;
2313 e5a0f69f 2018-08-18 stsp case KEY_PPAGE:
2314 e5a0f69f 2018-08-18 stsp if (s->first_displayed_line == 1) {
2315 e5a0f69f 2018-08-18 stsp s->selected_line = 1;
2318 e5a0f69f 2018-08-18 stsp if (s->first_displayed_line > view->nlines - 2)
2319 e5a0f69f 2018-08-18 stsp s->first_displayed_line -=
2320 e5a0f69f 2018-08-18 stsp (view->nlines - 2);
2322 e5a0f69f 2018-08-18 stsp s->first_displayed_line = 1;
2325 e5a0f69f 2018-08-18 stsp case KEY_DOWN:
2326 e5a0f69f 2018-08-18 stsp if (s->selected_line < view->nlines - 2 &&
2327 e5a0f69f 2018-08-18 stsp s->first_displayed_line +
2328 e5a0f69f 2018-08-18 stsp s->selected_line <= s->blame.nlines)
2329 e5a0f69f 2018-08-18 stsp s->selected_line++;
2330 e5a0f69f 2018-08-18 stsp else if (s->last_displayed_line <
2331 e5a0f69f 2018-08-18 stsp s->blame.nlines)
2332 e5a0f69f 2018-08-18 stsp s->first_displayed_line++;
2335 e5a0f69f 2018-08-18 stsp case 'p': {
2336 e5a0f69f 2018-08-18 stsp struct got_object_id *id;
2337 e5a0f69f 2018-08-18 stsp id = get_selected_commit_id(s->blame.lines,
2338 e5a0f69f 2018-08-18 stsp s->first_displayed_line, s->selected_line);
2339 e5a0f69f 2018-08-18 stsp if (id == NULL || got_object_id_cmp(id,
2340 e5a0f69f 2018-08-18 stsp s->blamed_commit->id) == 0)
2342 e5a0f69f 2018-08-18 stsp err = open_selected_commit(&pobj, &obj,
2343 e5a0f69f 2018-08-18 stsp s->blame.lines, s->first_displayed_line,
2344 e5a0f69f 2018-08-18 stsp s->selected_line, s->repo);
2347 e5a0f69f 2018-08-18 stsp if (pobj == NULL && obj == NULL)
2349 e5a0f69f 2018-08-18 stsp if (ch == 'p' && pobj == NULL)
2351 e5a0f69f 2018-08-18 stsp s->done = 1;
2352 e5a0f69f 2018-08-18 stsp if (pthread_mutex_unlock(&s->mutex) != 0) {
2353 e5a0f69f 2018-08-18 stsp err = got_error_from_errno();
2354 e5a0f69f 2018-08-18 stsp goto done;
2356 e5a0f69f 2018-08-18 stsp thread_err = stop_blame(&s->blame);
2357 e5a0f69f 2018-08-18 stsp s->done = 0;
2358 e5a0f69f 2018-08-18 stsp if (pthread_mutex_lock(&s->mutex) != 0) {
2359 e5a0f69f 2018-08-18 stsp err = got_error_from_errno();
2360 e5a0f69f 2018-08-18 stsp goto done;
2362 e5a0f69f 2018-08-18 stsp if (thread_err)
2364 e5a0f69f 2018-08-18 stsp id = got_object_get_id(ch == 'b' ? obj : pobj);
2365 e5a0f69f 2018-08-18 stsp got_object_close(obj);
2366 e5a0f69f 2018-08-18 stsp obj = NULL;
2367 e5a0f69f 2018-08-18 stsp if (pobj) {
2368 e5a0f69f 2018-08-18 stsp got_object_close(pobj);
2369 e5a0f69f 2018-08-18 stsp pobj = NULL;
2371 6402fb3c 2018-09-15 stsp err = got_object_qid_alloc(&s->blamed_commit, id);
2373 e5a0f69f 2018-08-18 stsp goto done;
2374 e5a0f69f 2018-08-18 stsp SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
2375 e5a0f69f 2018-08-18 stsp s->blamed_commit, entry);
2376 e5a0f69f 2018-08-18 stsp err = run_blame(&s->blame, &s->mutex, view,
2377 e5a0f69f 2018-08-18 stsp &s->blame_complete,
2378 e5a0f69f 2018-08-18 stsp &s->first_displayed_line,
2379 e5a0f69f 2018-08-18 stsp &s->last_displayed_line,
2380 e5a0f69f 2018-08-18 stsp &s->selected_line, &s->done, &s->eof,
2381 e5a0f69f 2018-08-18 stsp s->path, s->blamed_commit->id, s->repo);
2386 e5a0f69f 2018-08-18 stsp case 'B': {
2387 e5a0f69f 2018-08-18 stsp struct got_object_qid *first;
2388 e5a0f69f 2018-08-18 stsp first = SIMPLEQ_FIRST(&s->blamed_commits);
2389 e5a0f69f 2018-08-18 stsp if (!got_object_id_cmp(first->id, s->commit_id))
2391 e5a0f69f 2018-08-18 stsp s->done = 1;
2392 e5a0f69f 2018-08-18 stsp if (pthread_mutex_unlock(&s->mutex) != 0) {
2393 e5a0f69f 2018-08-18 stsp err = got_error_from_errno();
2394 e5a0f69f 2018-08-18 stsp goto done;
2396 e5a0f69f 2018-08-18 stsp thread_err = stop_blame(&s->blame);
2397 e5a0f69f 2018-08-18 stsp s->done = 0;
2398 e5a0f69f 2018-08-18 stsp if (pthread_mutex_lock(&s->mutex) != 0) {
2399 e5a0f69f 2018-08-18 stsp err = got_error_from_errno();
2400 e5a0f69f 2018-08-18 stsp goto done;
2402 e5a0f69f 2018-08-18 stsp if (thread_err)
2404 e5a0f69f 2018-08-18 stsp SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
2405 e5a0f69f 2018-08-18 stsp got_object_qid_free(s->blamed_commit);
2406 e5a0f69f 2018-08-18 stsp s->blamed_commit =
2407 e5a0f69f 2018-08-18 stsp SIMPLEQ_FIRST(&s->blamed_commits);
2408 e5a0f69f 2018-08-18 stsp err = run_blame(&s->blame, &s->mutex, view,
2409 e5a0f69f 2018-08-18 stsp &s->blame_complete,
2410 e5a0f69f 2018-08-18 stsp &s->first_displayed_line,
2411 e5a0f69f 2018-08-18 stsp &s->last_displayed_line,
2412 e5a0f69f 2018-08-18 stsp &s->selected_line, &s->done, &s->eof, s->path,
2413 e5a0f69f 2018-08-18 stsp s->blamed_commit->id, s->repo);
2418 e5a0f69f 2018-08-18 stsp case KEY_ENTER:
2419 e5a0f69f 2018-08-18 stsp case '\r':
2420 e5a0f69f 2018-08-18 stsp err = open_selected_commit(&pobj, &obj,
2421 e5a0f69f 2018-08-18 stsp s->blame.lines, s->first_displayed_line,
2422 e5a0f69f 2018-08-18 stsp s->selected_line, s->repo);
2425 e5a0f69f 2018-08-18 stsp if (pobj == NULL && obj == NULL)
2427 0cf4efb1 2018-09-29 stsp diff_view = view_open(0, 0, 0,
2428 0cf4efb1 2018-09-29 stsp view_split_begin_x(view->begin_x), TOG_VIEW_DIFF);
2429 e5a0f69f 2018-08-18 stsp if (diff_view == NULL) {
2430 e5a0f69f 2018-08-18 stsp err = got_error_from_errno();
2433 e5a0f69f 2018-08-18 stsp err = open_diff_view(diff_view, pobj, obj,
2435 e5a0f69f 2018-08-18 stsp if (err) {
2436 e5a0f69f 2018-08-18 stsp view_close(diff_view);
2439 e5a0f69f 2018-08-18 stsp *new_view = diff_view;
2440 e5a0f69f 2018-08-18 stsp if (pobj) {
2441 e5a0f69f 2018-08-18 stsp got_object_close(pobj);
2442 e5a0f69f 2018-08-18 stsp pobj = NULL;
2444 e5a0f69f 2018-08-18 stsp got_object_close(obj);
2445 e5a0f69f 2018-08-18 stsp obj = NULL;
2449 e5a0f69f 2018-08-18 stsp case KEY_NPAGE:
2451 e5a0f69f 2018-08-18 stsp if (s->last_displayed_line >= s->blame.nlines &&
2452 e5a0f69f 2018-08-18 stsp s->selected_line < view->nlines - 2) {
2453 e5a0f69f 2018-08-18 stsp s->selected_line = MIN(s->blame.nlines,
2454 e5a0f69f 2018-08-18 stsp view->nlines - 2);
2457 e5a0f69f 2018-08-18 stsp if (s->last_displayed_line + view->nlines - 2
2458 e5a0f69f 2018-08-18 stsp <= s->blame.nlines)
2459 e5a0f69f 2018-08-18 stsp s->first_displayed_line +=
2460 e5a0f69f 2018-08-18 stsp view->nlines - 2;
2462 e5a0f69f 2018-08-18 stsp s->first_displayed_line =
2463 e5a0f69f 2018-08-18 stsp s->blame.nlines -
2464 e5a0f69f 2018-08-18 stsp (view->nlines - 3);
2466 e5a0f69f 2018-08-18 stsp case KEY_RESIZE:
2467 e5a0f69f 2018-08-18 stsp if (s->selected_line > view->nlines - 2) {
2468 e5a0f69f 2018-08-18 stsp s->selected_line = MIN(s->blame.nlines,
2469 e5a0f69f 2018-08-18 stsp view->nlines - 2);
2476 e5a0f69f 2018-08-18 stsp if (pthread_mutex_unlock(&s->mutex) != 0)
2477 e5a0f69f 2018-08-18 stsp err = got_error_from_errno();
2480 b880a918 2018-07-10 stsp got_object_close(pobj);
2481 245d91c1 2018-07-12 stsp return thread_err ? thread_err : err;
2484 a70480e0 2018-06-23 stsp static const struct got_error *
2485 9f7d7167 2018-04-29 stsp cmd_blame(int argc, char *argv[])
2487 a70480e0 2018-06-23 stsp const struct got_error *error;
2488 a70480e0 2018-06-23 stsp struct got_repository *repo = NULL;
2489 69069811 2018-08-02 stsp char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2490 a70480e0 2018-06-23 stsp struct got_object_id *commit_id = NULL;
2491 a70480e0 2018-06-23 stsp char *commit_id_str = NULL;
2493 e1cd8fed 2018-08-01 stsp struct tog_view *view;
2495 a70480e0 2018-06-23 stsp #ifndef PROFILE
2496 ad242220 2018-09-08 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd", NULL)
2498 a70480e0 2018-06-23 stsp err(1, "pledge");
2501 69069811 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2502 a70480e0 2018-06-23 stsp switch (ch) {
2504 a70480e0 2018-06-23 stsp commit_id_str = optarg;
2507 69069811 2018-08-02 stsp repo_path = realpath(optarg, NULL);
2508 69069811 2018-08-02 stsp if (repo_path == NULL)
2509 69069811 2018-08-02 stsp err(1, "-r option");
2513 a70480e0 2018-06-23 stsp /* NOTREACHED */
2517 a70480e0 2018-06-23 stsp argc -= optind;
2518 a70480e0 2018-06-23 stsp argv += optind;
2520 69069811 2018-08-02 stsp if (argc == 1)
2521 69069811 2018-08-02 stsp path = argv[0];
2523 a70480e0 2018-06-23 stsp usage_blame();
2525 69069811 2018-08-02 stsp cwd = getcwd(NULL, 0);
2526 69069811 2018-08-02 stsp if (cwd == NULL) {
2527 69069811 2018-08-02 stsp error = got_error_from_errno();
2528 69069811 2018-08-02 stsp goto done;
2530 69069811 2018-08-02 stsp if (repo_path == NULL) {
2531 69069811 2018-08-02 stsp repo_path = strdup(cwd);
2532 69069811 2018-08-02 stsp if (repo_path == NULL) {
2533 69069811 2018-08-02 stsp error = got_error_from_errno();
2534 69069811 2018-08-02 stsp goto done;
2539 69069811 2018-08-02 stsp error = got_repo_open(&repo, repo_path);
2540 a70480e0 2018-06-23 stsp if (error != NULL)
2541 66b4983c 2018-06-23 stsp return error;
2543 69069811 2018-08-02 stsp error = got_repo_map_path(&in_repo_path, repo, path);
2544 69069811 2018-08-02 stsp if (error != NULL)
2545 69069811 2018-08-02 stsp goto done;
2547 a70480e0 2018-06-23 stsp if (commit_id_str == NULL) {
2548 a70480e0 2018-06-23 stsp struct got_reference *head_ref;
2549 a70480e0 2018-06-23 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
2550 a70480e0 2018-06-23 stsp if (error != NULL)
2551 66b4983c 2018-06-23 stsp goto done;
2552 a70480e0 2018-06-23 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
2553 a70480e0 2018-06-23 stsp got_ref_close(head_ref);
2555 a70480e0 2018-06-23 stsp struct got_object *obj;
2556 a70480e0 2018-06-23 stsp error = got_object_open_by_id_str(&obj, repo, commit_id_str);
2557 a70480e0 2018-06-23 stsp if (error != NULL)
2558 66b4983c 2018-06-23 stsp goto done;
2559 6402fb3c 2018-09-15 stsp commit_id = got_object_id_dup(got_object_get_id(obj));
2560 a19e88aa 2018-06-23 stsp if (commit_id == NULL)
2561 66b4983c 2018-06-23 stsp error = got_error_from_errno();
2562 a19e88aa 2018-06-23 stsp got_object_close(obj);
2564 a19e88aa 2018-06-23 stsp if (error != NULL)
2565 a19e88aa 2018-06-23 stsp goto done;
2567 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
2568 e1cd8fed 2018-08-01 stsp if (view == NULL) {
2569 e1cd8fed 2018-08-01 stsp error = got_error_from_errno();
2570 e1cd8fed 2018-08-01 stsp goto done;
2572 7cbe629d 2018-08-04 stsp error = open_blame_view(view, in_repo_path, commit_id, repo);
2573 7cbe629d 2018-08-04 stsp if (error)
2574 7cbe629d 2018-08-04 stsp goto done;
2575 e5a0f69f 2018-08-18 stsp error = view_loop(view);
2577 69069811 2018-08-02 stsp free(repo_path);
2578 69069811 2018-08-02 stsp free(cwd);
2579 a70480e0 2018-06-23 stsp free(commit_id);
2581 a70480e0 2018-06-23 stsp got_repo_close(repo);
2582 a70480e0 2018-06-23 stsp return error;
2585 ffd1d5e5 2018-06-23 stsp static const struct got_error *
2586 f7d12f7e 2018-08-01 stsp draw_tree_entries(struct tog_view *view,
2587 f7d12f7e 2018-08-01 stsp struct got_tree_entry **first_displayed_entry,
2588 ffd1d5e5 2018-06-23 stsp struct got_tree_entry **last_displayed_entry,
2589 ffd1d5e5 2018-06-23 stsp struct got_tree_entry **selected_entry, int *ndisplayed,
2590 f7d12f7e 2018-08-01 stsp const char *label, int show_ids, const char *parent_path,
2591 ce52c690 2018-06-23 stsp const struct got_tree_entries *entries, int selected, int limit, int isroot)
2593 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
2594 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *te;
2595 ffd1d5e5 2018-06-23 stsp wchar_t *wline;
2596 ffd1d5e5 2018-06-23 stsp int width, n;
2598 ffd1d5e5 2018-06-23 stsp *ndisplayed = 0;
2600 f7d12f7e 2018-08-01 stsp werase(view->window);
2602 ffd1d5e5 2018-06-23 stsp if (limit == 0)
2603 ffd1d5e5 2018-06-23 stsp return NULL;
2605 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, label, view->ncols);
2607 ffd1d5e5 2018-06-23 stsp return err;
2608 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2609 a3404814 2018-09-02 stsp wstandout(view->window);
2610 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
2611 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2612 a3404814 2018-09-02 stsp wstandend(view->window);
2613 2550e4c3 2018-07-13 stsp free(wline);
2614 2550e4c3 2018-07-13 stsp wline = NULL;
2615 f7d12f7e 2018-08-01 stsp if (width < view->ncols)
2616 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
2617 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
2618 ffd1d5e5 2018-06-23 stsp return NULL;
2619 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, parent_path, view->ncols);
2621 ce52c690 2018-06-23 stsp return err;
2622 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
2623 2550e4c3 2018-07-13 stsp free(wline);
2624 2550e4c3 2018-07-13 stsp wline = NULL;
2625 f7d12f7e 2018-08-01 stsp if (width < view->ncols)
2626 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
2627 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
2628 ffd1d5e5 2018-06-23 stsp return NULL;
2629 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
2630 a1eca9bb 2018-06-23 stsp if (--limit <= 0)
2631 a1eca9bb 2018-06-23 stsp return NULL;
2633 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_FIRST(&entries->head);
2634 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry == NULL) {
2635 ffd1d5e5 2018-06-23 stsp if (selected == 0) {
2636 0cf4efb1 2018-09-29 stsp if (view->focussed)
2637 0cf4efb1 2018-09-29 stsp wstandout(view->window);
2638 ffd1d5e5 2018-06-23 stsp *selected_entry = NULL;
2640 f7d12f7e 2018-08-01 stsp waddstr(view->window, " ..\n"); /* parent directory */
2641 0cf4efb1 2018-09-29 stsp if (selected == 0 && view->focussed)
2642 f7d12f7e 2018-08-01 stsp wstandend(view->window);
2643 ffd1d5e5 2018-06-23 stsp (*ndisplayed)++;
2644 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
2645 ffd1d5e5 2018-06-23 stsp return NULL;
2649 ffd1d5e5 2018-06-23 stsp while (te != *first_displayed_entry)
2650 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_NEXT(te, entry);
2653 ffd1d5e5 2018-06-23 stsp while (te) {
2654 1d13200f 2018-07-12 stsp char *line = NULL, *id_str = NULL;
2656 1d13200f 2018-07-12 stsp if (show_ids) {
2657 1d13200f 2018-07-12 stsp err = got_object_id_str(&id_str, te->id);
2659 1d13200f 2018-07-12 stsp return got_error_from_errno();
2661 1d13200f 2018-07-12 stsp if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
2662 1d13200f 2018-07-12 stsp te->name, S_ISDIR(te->mode) ? "/" : "") == -1) {
2663 1d13200f 2018-07-12 stsp free(id_str);
2664 ffd1d5e5 2018-06-23 stsp return got_error_from_errno();
2666 1d13200f 2018-07-12 stsp free(id_str);
2667 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, line, view->ncols);
2668 ffd1d5e5 2018-06-23 stsp if (err) {
2669 ffd1d5e5 2018-06-23 stsp free(line);
2672 ffd1d5e5 2018-06-23 stsp if (n == selected) {
2673 0cf4efb1 2018-09-29 stsp if (view->focussed)
2674 0cf4efb1 2018-09-29 stsp wstandout(view->window);
2675 ffd1d5e5 2018-06-23 stsp *selected_entry = te;
2677 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
2678 f7d12f7e 2018-08-01 stsp if (width < view->ncols)
2679 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
2680 0cf4efb1 2018-09-29 stsp if (n == selected && view->focussed)
2681 f7d12f7e 2018-08-01 stsp wstandend(view->window);
2682 ffd1d5e5 2018-06-23 stsp free(line);
2683 2550e4c3 2018-07-13 stsp free(wline);
2684 2550e4c3 2018-07-13 stsp wline = NULL;
2686 ffd1d5e5 2018-06-23 stsp (*ndisplayed)++;
2687 ffd1d5e5 2018-06-23 stsp *last_displayed_entry = te;
2688 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
2690 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_NEXT(te, entry);
2693 1a57306a 2018-09-02 stsp view_vborder(view);
2694 ffd1d5e5 2018-06-23 stsp return err;
2697 ffd1d5e5 2018-06-23 stsp static void
2698 ffd1d5e5 2018-06-23 stsp tree_scroll_up(struct got_tree_entry **first_displayed_entry, int maxscroll,
2699 ffd1d5e5 2018-06-23 stsp const struct got_tree_entries *entries, int isroot)
2701 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *te, *prev;
2704 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry == NULL)
2707 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_FIRST(&entries->head);
2708 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry == te) {
2709 ffd1d5e5 2018-06-23 stsp if (!isroot)
2710 ffd1d5e5 2018-06-23 stsp *first_displayed_entry = NULL;
2714 ffd1d5e5 2018-06-23 stsp /* XXX this is stupid... switch to TAILQ? */
2715 ffd1d5e5 2018-06-23 stsp for (i = 0; i < maxscroll; i++) {
2716 ffd1d5e5 2018-06-23 stsp while (te != *first_displayed_entry) {
2717 ffd1d5e5 2018-06-23 stsp prev = te;
2718 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_NEXT(te, entry);
2720 ffd1d5e5 2018-06-23 stsp *first_displayed_entry = prev;
2721 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_FIRST(&entries->head);
2723 ffd1d5e5 2018-06-23 stsp if (!isroot && te == SIMPLEQ_FIRST(&entries->head) && i < maxscroll)
2724 ffd1d5e5 2018-06-23 stsp *first_displayed_entry = NULL;
2727 ffd1d5e5 2018-06-23 stsp static void
2728 ffd1d5e5 2018-06-23 stsp tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
2729 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *last_displayed_entry,
2730 ffd1d5e5 2018-06-23 stsp const struct got_tree_entries *entries)
2732 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *next;
2733 ffd1d5e5 2018-06-23 stsp int n = 0;
2735 ffd1d5e5 2018-06-23 stsp if (SIMPLEQ_NEXT(last_displayed_entry, entry) == NULL)
2738 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry)
2739 ffd1d5e5 2018-06-23 stsp next = SIMPLEQ_NEXT(*first_displayed_entry, entry);
2741 ffd1d5e5 2018-06-23 stsp next = SIMPLEQ_FIRST(&entries->head);
2742 ffd1d5e5 2018-06-23 stsp while (next) {
2743 ffd1d5e5 2018-06-23 stsp *first_displayed_entry = next;
2744 ffd1d5e5 2018-06-23 stsp if (++n >= maxscroll)
2746 ffd1d5e5 2018-06-23 stsp next = SIMPLEQ_NEXT(next, entry);
2750 ffd1d5e5 2018-06-23 stsp static const struct got_error *
2751 ce52c690 2018-06-23 stsp tree_entry_path(char **path, struct tog_parent_trees *parents,
2752 ce52c690 2018-06-23 stsp struct got_tree_entry *te)
2754 cb2ebc8a 2018-06-23 stsp const struct got_error *err = NULL;
2755 ffd1d5e5 2018-06-23 stsp struct tog_parent_tree *pt;
2756 ffd1d5e5 2018-06-23 stsp size_t len = 2; /* for leading slash and NUL */
2758 d9765a41 2018-06-23 stsp TAILQ_FOREACH(pt, parents, entry)
2759 ffd1d5e5 2018-06-23 stsp len += strlen(pt->selected_entry->name) + 1 /* slash */;
2761 ce52c690 2018-06-23 stsp len += strlen(te->name);
2763 ce52c690 2018-06-23 stsp *path = calloc(1, len);
2764 ffd1d5e5 2018-06-23 stsp if (path == NULL)
2765 ffd1d5e5 2018-06-23 stsp return got_error_from_errno();
2767 ce52c690 2018-06-23 stsp (*path)[0] = '/';
2768 d9765a41 2018-06-23 stsp pt = TAILQ_LAST(parents, tog_parent_trees);
2769 d9765a41 2018-06-23 stsp while (pt) {
2770 ce52c690 2018-06-23 stsp if (strlcat(*path, pt->selected_entry->name, len) >= len) {
2771 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
2772 cb2ebc8a 2018-06-23 stsp goto done;
2774 ce52c690 2018-06-23 stsp if (strlcat(*path, "/", len) >= len) {
2775 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
2776 cb2ebc8a 2018-06-23 stsp goto done;
2778 d9765a41 2018-06-23 stsp pt = TAILQ_PREV(pt, tog_parent_trees, entry);
2781 ce52c690 2018-06-23 stsp if (strlcat(*path, te->name, len) >= len) {
2782 ce52c690 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
2783 ce52c690 2018-06-23 stsp goto done;
2787 ce52c690 2018-06-23 stsp if (err) {
2788 ce52c690 2018-06-23 stsp free(*path);
2789 ce52c690 2018-06-23 stsp *path = NULL;
2791 ce52c690 2018-06-23 stsp return err;
2794 ce52c690 2018-06-23 stsp static const struct got_error *
2795 0cf4efb1 2018-09-29 stsp blame_tree_entry(struct tog_view **new_view, int begin_x,
2796 e5a0f69f 2018-08-18 stsp struct got_tree_entry *te, struct tog_parent_trees *parents,
2797 e5a0f69f 2018-08-18 stsp struct got_object_id *commit_id, struct got_repository *repo)
2799 ce52c690 2018-06-23 stsp const struct got_error *err = NULL;
2800 ce52c690 2018-06-23 stsp char *path;
2801 e5a0f69f 2018-08-18 stsp struct tog_view *blame_view;
2803 ce52c690 2018-06-23 stsp err = tree_entry_path(&path, parents, te);
2805 ce52c690 2018-06-23 stsp return err;
2807 0cf4efb1 2018-09-29 stsp blame_view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
2808 e5a0f69f 2018-08-18 stsp if (blame_view == NULL)
2809 e5a0f69f 2018-08-18 stsp return got_error_from_errno();
2811 e5a0f69f 2018-08-18 stsp err = open_blame_view(blame_view, path, commit_id, repo);
2812 e5a0f69f 2018-08-18 stsp if (err) {
2813 e5a0f69f 2018-08-18 stsp view_close(blame_view);
2814 e5a0f69f 2018-08-18 stsp free(path);
2816 e5a0f69f 2018-08-18 stsp *new_view = blame_view;
2817 69efd4c4 2018-07-18 stsp return err;
2820 69efd4c4 2018-07-18 stsp static const struct got_error *
2821 0cf4efb1 2018-09-29 stsp log_tree_entry(struct tog_view **new_view,
2822 e5a0f69f 2018-08-18 stsp struct got_tree_entry *te, struct tog_parent_trees *parents,
2823 e5a0f69f 2018-08-18 stsp struct got_object_id *commit_id, struct got_repository *repo)
2825 e5a0f69f 2018-08-18 stsp struct tog_view *log_view;
2826 69efd4c4 2018-07-18 stsp const struct got_error *err = NULL;
2827 69efd4c4 2018-07-18 stsp char *path;
2829 0cf4efb1 2018-09-29 stsp log_view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
2830 e5a0f69f 2018-08-18 stsp if (log_view == NULL)
2831 e5a0f69f 2018-08-18 stsp return got_error_from_errno();
2833 69efd4c4 2018-07-18 stsp err = tree_entry_path(&path, parents, te);
2835 69efd4c4 2018-07-18 stsp return err;
2837 e5a0f69f 2018-08-18 stsp err = open_log_view(log_view, commit_id, repo, path);
2839 e5a0f69f 2018-08-18 stsp view_close(log_view);
2841 e5a0f69f 2018-08-18 stsp *new_view = log_view;
2842 cb2ebc8a 2018-06-23 stsp free(path);
2843 cb2ebc8a 2018-06-23 stsp return err;
2846 ffd1d5e5 2018-06-23 stsp static const struct got_error *
2847 ad80ab7b 2018-08-04 stsp open_tree_view(struct tog_view *view, struct got_tree_object *root,
2848 5221c383 2018-08-01 stsp struct got_object_id *commit_id, struct got_repository *repo)
2850 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
2851 ad80ab7b 2018-08-04 stsp char *commit_id_str = NULL;
2852 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
2854 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->parents);
2856 ffd1d5e5 2018-06-23 stsp err = got_object_id_str(&commit_id_str, commit_id);
2857 ffd1d5e5 2018-06-23 stsp if (err != NULL)
2858 ffd1d5e5 2018-06-23 stsp goto done;
2860 fb2756b9 2018-08-04 stsp if (asprintf(&s->tree_label, "commit: %s", commit_id_str) == -1) {
2861 ffd1d5e5 2018-06-23 stsp err = got_error_from_errno();
2862 ffd1d5e5 2018-06-23 stsp goto done;
2865 fb2756b9 2018-08-04 stsp s->root = s->tree = root;
2866 fb2756b9 2018-08-04 stsp s->entries = got_object_tree_get_entries(root);
2867 fb2756b9 2018-08-04 stsp s->first_displayed_entry = SIMPLEQ_FIRST(&s->entries->head);
2868 6484ec90 2018-09-29 stsp s->commit_id = got_object_id_dup(commit_id);
2869 6484ec90 2018-09-29 stsp if (s->commit_id == NULL) {
2870 6484ec90 2018-09-29 stsp err = got_error_from_errno();
2871 6484ec90 2018-09-29 stsp goto done;
2873 fb2756b9 2018-08-04 stsp s->repo = repo;
2875 e5a0f69f 2018-08-18 stsp view->show = show_tree_view;
2876 0cf4efb1 2018-09-29 stsp view->update_siblings = update_siblings_tree_view;
2877 e5a0f69f 2018-08-18 stsp view->input = input_tree_view;
2878 e5a0f69f 2018-08-18 stsp view->close = close_tree_view;
2880 ad80ab7b 2018-08-04 stsp free(commit_id_str);
2881 6484ec90 2018-09-29 stsp if (err) {
2882 fb2756b9 2018-08-04 stsp free(s->tree_label);
2883 6484ec90 2018-09-29 stsp s->tree_label = NULL;
2885 ad80ab7b 2018-08-04 stsp return err;
2888 e5a0f69f 2018-08-18 stsp static const struct got_error *
2889 ad80ab7b 2018-08-04 stsp close_tree_view(struct tog_view *view)
2891 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
2893 fb2756b9 2018-08-04 stsp free(s->tree_label);
2894 6484ec90 2018-09-29 stsp s->tree_label = NULL;
2895 6484ec90 2018-09-29 stsp free(s->commit_id);
2896 6484ec90 2018-09-29 stsp s->commit_id = NULL;
2897 fb2756b9 2018-08-04 stsp while (!TAILQ_EMPTY(&s->parents)) {
2898 ad80ab7b 2018-08-04 stsp struct tog_parent_tree *parent;
2899 fb2756b9 2018-08-04 stsp parent = TAILQ_FIRST(&s->parents);
2900 fb2756b9 2018-08-04 stsp TAILQ_REMOVE(&s->parents, parent, entry);
2901 ad80ab7b 2018-08-04 stsp free(parent);
2904 fb2756b9 2018-08-04 stsp if (s->tree != s->root)
2905 fb2756b9 2018-08-04 stsp got_object_tree_close(s->tree);
2906 e5a0f69f 2018-08-18 stsp got_object_tree_close(s->root);
2908 e5a0f69f 2018-08-18 stsp return NULL;
2911 ad80ab7b 2018-08-04 stsp static const struct got_error *
2912 ad80ab7b 2018-08-04 stsp show_tree_view(struct tog_view *view)
2914 ad80ab7b 2018-08-04 stsp const struct got_error *err = NULL;
2915 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
2916 e5a0f69f 2018-08-18 stsp char *parent_path;
2918 e5a0f69f 2018-08-18 stsp err = tree_entry_path(&parent_path, &s->parents, NULL);
2920 e5a0f69f 2018-08-18 stsp return err;
2922 e5a0f69f 2018-08-18 stsp err = draw_tree_entries(view, &s->first_displayed_entry,
2923 e5a0f69f 2018-08-18 stsp &s->last_displayed_entry, &s->selected_entry,
2924 e5a0f69f 2018-08-18 stsp &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
2925 e5a0f69f 2018-08-18 stsp s->entries, s->selected, view->nlines, s->tree == s->root);
2926 e5a0f69f 2018-08-18 stsp free(parent_path);
2927 e5a0f69f 2018-08-18 stsp return err;
2930 e5a0f69f 2018-08-18 stsp static const struct got_error *
2931 0cf4efb1 2018-09-29 stsp update_siblings_tree_view(int *updated, struct tog_view *view,
2932 0cf4efb1 2018-09-29 stsp struct tog_view *sibling_view)
2934 0cf4efb1 2018-09-29 stsp return NULL;
2937 0cf4efb1 2018-09-29 stsp static const struct got_error *
2938 e5a0f69f 2018-08-18 stsp input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
2939 e5a0f69f 2018-08-18 stsp struct tog_view *view, int ch)
2941 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
2942 e5a0f69f 2018-08-18 stsp struct tog_tree_view_state *s = &view->state.tree;
2944 e5a0f69f 2018-08-18 stsp switch (ch) {
2946 e5a0f69f 2018-08-18 stsp s->show_ids = !s->show_ids;
2949 e5a0f69f 2018-08-18 stsp if (s->selected_entry) {
2950 0cf4efb1 2018-09-29 stsp err = log_tree_entry(new_view,
2951 e5a0f69f 2018-08-18 stsp s->selected_entry, &s->parents,
2952 e5a0f69f 2018-08-18 stsp s->commit_id, s->repo);
2956 e5a0f69f 2018-08-18 stsp case KEY_UP:
2957 e5a0f69f 2018-08-18 stsp if (s->selected > 0)
2958 e5a0f69f 2018-08-18 stsp s->selected--;
2959 e5a0f69f 2018-08-18 stsp if (s->selected > 0)
2961 e5a0f69f 2018-08-18 stsp tree_scroll_up(&s->first_displayed_entry, 1,
2962 e5a0f69f 2018-08-18 stsp s->entries, s->tree == s->root);
2964 e5a0f69f 2018-08-18 stsp case KEY_PPAGE:
2965 e5a0f69f 2018-08-18 stsp if (SIMPLEQ_FIRST(&s->entries->head) ==
2966 e5a0f69f 2018-08-18 stsp s->first_displayed_entry) {
2967 e5a0f69f 2018-08-18 stsp if (s->tree != s->root)
2968 e5a0f69f 2018-08-18 stsp s->first_displayed_entry = NULL;
2969 e5a0f69f 2018-08-18 stsp s->selected = 0;
2972 e5a0f69f 2018-08-18 stsp tree_scroll_up(&s->first_displayed_entry,
2973 e5a0f69f 2018-08-18 stsp view->nlines, s->entries,
2974 e5a0f69f 2018-08-18 stsp s->tree == s->root);
2977 e5a0f69f 2018-08-18 stsp case KEY_DOWN:
2978 e5a0f69f 2018-08-18 stsp if (s->selected < s->ndisplayed - 1) {
2979 e5a0f69f 2018-08-18 stsp s->selected++;
2982 e5a0f69f 2018-08-18 stsp tree_scroll_down(&s->first_displayed_entry, 1,
2983 e5a0f69f 2018-08-18 stsp s->last_displayed_entry, s->entries);
2985 e5a0f69f 2018-08-18 stsp case KEY_NPAGE:
2986 e5a0f69f 2018-08-18 stsp tree_scroll_down(&s->first_displayed_entry,
2987 e5a0f69f 2018-08-18 stsp view->nlines, s->last_displayed_entry,
2988 e5a0f69f 2018-08-18 stsp s->entries);
2989 e5a0f69f 2018-08-18 stsp if (SIMPLEQ_NEXT(s->last_displayed_entry,
2992 e5a0f69f 2018-08-18 stsp /* can't scroll any further; move cursor down */
2993 e5a0f69f 2018-08-18 stsp if (s->selected < s->ndisplayed - 1)
2994 e5a0f69f 2018-08-18 stsp s->selected = s->ndisplayed - 1;
2996 e5a0f69f 2018-08-18 stsp case KEY_ENTER:
2997 e5a0f69f 2018-08-18 stsp case '\r':
2998 e5a0f69f 2018-08-18 stsp if (s->selected_entry == NULL) {
2999 e5a0f69f 2018-08-18 stsp struct tog_parent_tree *parent;
3000 7837eeac 2018-09-24 stsp case KEY_BACKSPACE:
3001 e5a0f69f 2018-08-18 stsp /* user selected '..' */
3002 e5a0f69f 2018-08-18 stsp if (s->tree == s->root)
3004 e5a0f69f 2018-08-18 stsp parent = TAILQ_FIRST(&s->parents);
3005 e5a0f69f 2018-08-18 stsp TAILQ_REMOVE(&s->parents, parent,
3007 e5a0f69f 2018-08-18 stsp got_object_tree_close(s->tree);
3008 e5a0f69f 2018-08-18 stsp s->tree = parent->tree;
3009 e5a0f69f 2018-08-18 stsp s->entries =
3010 e5a0f69f 2018-08-18 stsp got_object_tree_get_entries(s->tree);
3011 e5a0f69f 2018-08-18 stsp s->first_displayed_entry =
3012 e5a0f69f 2018-08-18 stsp parent->first_displayed_entry;
3013 e5a0f69f 2018-08-18 stsp s->selected_entry =
3014 e5a0f69f 2018-08-18 stsp parent->selected_entry;
3015 e5a0f69f 2018-08-18 stsp s->selected = parent->selected;
3016 e5a0f69f 2018-08-18 stsp free(parent);
3017 e5a0f69f 2018-08-18 stsp } else if (S_ISDIR(s->selected_entry->mode)) {
3018 e5a0f69f 2018-08-18 stsp struct tog_parent_tree *parent;
3019 e5a0f69f 2018-08-18 stsp struct got_tree_object *child;
3020 e5a0f69f 2018-08-18 stsp err = got_object_open_as_tree(&child,
3021 e5a0f69f 2018-08-18 stsp s->repo, s->selected_entry->id);
3024 e5a0f69f 2018-08-18 stsp parent = calloc(1, sizeof(*parent));
3025 e5a0f69f 2018-08-18 stsp if (parent == NULL) {
3026 e5a0f69f 2018-08-18 stsp err = got_error_from_errno();
3029 e5a0f69f 2018-08-18 stsp parent->tree = s->tree;
3030 e5a0f69f 2018-08-18 stsp parent->first_displayed_entry =
3031 e5a0f69f 2018-08-18 stsp s->first_displayed_entry;
3032 e5a0f69f 2018-08-18 stsp parent->selected_entry = s->selected_entry;
3033 e5a0f69f 2018-08-18 stsp parent->selected = s->selected;
3034 e5a0f69f 2018-08-18 stsp TAILQ_INSERT_HEAD(&s->parents, parent, entry);
3035 e5a0f69f 2018-08-18 stsp s->tree = child;
3036 e5a0f69f 2018-08-18 stsp s->entries =
3037 e5a0f69f 2018-08-18 stsp got_object_tree_get_entries(s->tree);
3038 e5a0f69f 2018-08-18 stsp s->selected = 0;
3039 e5a0f69f 2018-08-18 stsp s->first_displayed_entry = NULL;
3040 e5a0f69f 2018-08-18 stsp } else if (S_ISREG(s->selected_entry->mode)) {
3041 0cf4efb1 2018-09-29 stsp err = blame_tree_entry(new_view, view->begin_x,
3042 e5a0f69f 2018-08-18 stsp s->selected_entry, &s->parents,
3043 e5a0f69f 2018-08-18 stsp s->commit_id, s->repo);
3048 e5a0f69f 2018-08-18 stsp case KEY_RESIZE:
3049 e5a0f69f 2018-08-18 stsp if (s->selected > view->nlines)
3050 e5a0f69f 2018-08-18 stsp s->selected = s->ndisplayed - 1;
3056 ffd1d5e5 2018-06-23 stsp return err;
3059 ffd1d5e5 2018-06-23 stsp __dead static void
3060 ffd1d5e5 2018-06-23 stsp usage_tree(void)
3063 ffd1d5e5 2018-06-23 stsp fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
3064 ffd1d5e5 2018-06-23 stsp getprogname());
3068 ffd1d5e5 2018-06-23 stsp static const struct got_error *
3069 ffd1d5e5 2018-06-23 stsp cmd_tree(int argc, char *argv[])
3071 ffd1d5e5 2018-06-23 stsp const struct got_error *error;
3072 ffd1d5e5 2018-06-23 stsp struct got_repository *repo = NULL;
3073 ffd1d5e5 2018-06-23 stsp char *repo_path = NULL;
3074 ffd1d5e5 2018-06-23 stsp struct got_object_id *commit_id = NULL;
3075 ffd1d5e5 2018-06-23 stsp char *commit_id_arg = NULL;
3076 ffd1d5e5 2018-06-23 stsp struct got_commit_object *commit = NULL;
3077 ffd1d5e5 2018-06-23 stsp struct got_tree_object *tree = NULL;
3079 5221c383 2018-08-01 stsp struct tog_view *view;
3081 ffd1d5e5 2018-06-23 stsp #ifndef PROFILE
3082 ad242220 2018-09-08 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd", NULL)
3084 ffd1d5e5 2018-06-23 stsp err(1, "pledge");
3087 ffd1d5e5 2018-06-23 stsp while ((ch = getopt(argc, argv, "c:")) != -1) {
3088 ffd1d5e5 2018-06-23 stsp switch (ch) {
3090 ffd1d5e5 2018-06-23 stsp commit_id_arg = optarg;
3094 ffd1d5e5 2018-06-23 stsp /* NOTREACHED */
3098 ffd1d5e5 2018-06-23 stsp argc -= optind;
3099 ffd1d5e5 2018-06-23 stsp argv += optind;
3101 ffd1d5e5 2018-06-23 stsp if (argc == 0) {
3102 ffd1d5e5 2018-06-23 stsp repo_path = getcwd(NULL, 0);
3103 ffd1d5e5 2018-06-23 stsp if (repo_path == NULL)
3104 ffd1d5e5 2018-06-23 stsp return got_error_from_errno();
3105 ffd1d5e5 2018-06-23 stsp } else if (argc == 1) {
3106 ffd1d5e5 2018-06-23 stsp repo_path = realpath(argv[0], NULL);
3107 ffd1d5e5 2018-06-23 stsp if (repo_path == NULL)
3108 ffd1d5e5 2018-06-23 stsp return got_error_from_errno();
3110 ffd1d5e5 2018-06-23 stsp usage_log();
3112 ffd1d5e5 2018-06-23 stsp error = got_repo_open(&repo, repo_path);
3113 ffd1d5e5 2018-06-23 stsp free(repo_path);
3114 ffd1d5e5 2018-06-23 stsp if (error != NULL)
3115 ffd1d5e5 2018-06-23 stsp return error;
3117 ffd1d5e5 2018-06-23 stsp if (commit_id_arg == NULL) {
3118 ffd1d5e5 2018-06-23 stsp error = get_head_commit_id(&commit_id, repo);
3119 ffd1d5e5 2018-06-23 stsp if (error != NULL)
3120 ffd1d5e5 2018-06-23 stsp goto done;
3122 ffd1d5e5 2018-06-23 stsp struct got_object *obj;
3123 ffd1d5e5 2018-06-23 stsp error = got_object_open_by_id_str(&obj, repo, commit_id_arg);
3124 ffd1d5e5 2018-06-23 stsp if (error == NULL) {
3125 6402fb3c 2018-09-15 stsp commit_id = got_object_id_dup(got_object_get_id(obj));
3126 ffd1d5e5 2018-06-23 stsp if (commit_id == NULL)
3127 ffd1d5e5 2018-06-23 stsp error = got_error_from_errno();
3130 ffd1d5e5 2018-06-23 stsp if (error != NULL)
3131 ffd1d5e5 2018-06-23 stsp goto done;
3133 ffd1d5e5 2018-06-23 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
3134 ffd1d5e5 2018-06-23 stsp if (error != NULL)
3135 ffd1d5e5 2018-06-23 stsp goto done;
3137 ffd1d5e5 2018-06-23 stsp error = got_object_open_as_tree(&tree, repo, commit->tree_id);
3138 ffd1d5e5 2018-06-23 stsp if (error != NULL)
3139 ffd1d5e5 2018-06-23 stsp goto done;
3141 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
3142 5221c383 2018-08-01 stsp if (view == NULL) {
3143 5221c383 2018-08-01 stsp error = got_error_from_errno();
3144 5221c383 2018-08-01 stsp goto done;
3146 ad80ab7b 2018-08-04 stsp error = open_tree_view(view, tree, commit_id, repo);
3147 ad80ab7b 2018-08-04 stsp if (error)
3148 ad80ab7b 2018-08-04 stsp goto done;
3149 e5a0f69f 2018-08-18 stsp error = view_loop(view);
3151 ffd1d5e5 2018-06-23 stsp free(commit_id);
3152 ffd1d5e5 2018-06-23 stsp if (commit)
3153 ffd1d5e5 2018-06-23 stsp got_object_commit_close(commit);
3155 ffd1d5e5 2018-06-23 stsp got_object_tree_close(tree);
3157 ffd1d5e5 2018-06-23 stsp got_repo_close(repo);
3158 ffd1d5e5 2018-06-23 stsp return error;
3160 5c5136c5 2018-05-20 stsp static void
3161 9f7d7167 2018-04-29 stsp init_curses(void)
3163 9f7d7167 2018-04-29 stsp initscr();
3167 9f7d7167 2018-04-29 stsp intrflush(stdscr, FALSE);
3168 9f7d7167 2018-04-29 stsp keypad(stdscr, TRUE);
3169 1f475ad8 2018-05-10 stsp curs_set(0);
3172 4ed7e80c 2018-05-20 stsp __dead static void
3173 9f7d7167 2018-04-29 stsp usage(void)
3177 c2301be8 2018-04-30 stsp fprintf(stderr, "usage: %s [-h] [command] [arg ...]\n\n"
3178 9f7d7167 2018-04-29 stsp "Available commands:\n", getprogname());
3179 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
3180 9f7d7167 2018-04-29 stsp struct tog_cmd *cmd = &tog_commands[i];
3181 c2301be8 2018-04-30 stsp fprintf(stderr, " %s: %s\n", cmd->name, cmd->descr);
3186 c2301be8 2018-04-30 stsp static char **
3187 c2301be8 2018-04-30 stsp make_argv(const char *arg0, const char *arg1)
3189 c2301be8 2018-04-30 stsp char **argv;
3190 c2301be8 2018-04-30 stsp int argc = (arg1 == NULL ? 1 : 2);
3192 c2301be8 2018-04-30 stsp argv = calloc(argc, sizeof(char *));
3193 c2301be8 2018-04-30 stsp if (argv == NULL)
3194 c2301be8 2018-04-30 stsp err(1, "calloc");
3195 c2301be8 2018-04-30 stsp argv[0] = strdup(arg0);
3196 c2301be8 2018-04-30 stsp if (argv[0] == NULL)
3197 c2301be8 2018-04-30 stsp err(1, "calloc");
3198 c2301be8 2018-04-30 stsp if (arg1) {
3199 c2301be8 2018-04-30 stsp argv[1] = strdup(arg1);
3200 c2301be8 2018-04-30 stsp if (argv[1] == NULL)
3201 c2301be8 2018-04-30 stsp err(1, "calloc");
3204 c2301be8 2018-04-30 stsp return argv;
3208 9f7d7167 2018-04-29 stsp main(int argc, char *argv[])
3210 9f7d7167 2018-04-29 stsp const struct got_error *error = NULL;
3211 9f7d7167 2018-04-29 stsp struct tog_cmd *cmd = NULL;
3212 9f7d7167 2018-04-29 stsp int ch, hflag = 0;
3213 c2301be8 2018-04-30 stsp char **cmd_argv = NULL;
3215 9f7d7167 2018-04-29 stsp setlocale(LC_ALL, "");
3217 9f7d7167 2018-04-29 stsp while ((ch = getopt(argc, argv, "h")) != -1) {
3218 9f7d7167 2018-04-29 stsp switch (ch) {
3220 9f7d7167 2018-04-29 stsp hflag = 1;
3224 9f7d7167 2018-04-29 stsp /* NOTREACHED */
3228 9f7d7167 2018-04-29 stsp argc -= optind;
3229 9f7d7167 2018-04-29 stsp argv += optind;
3230 9f7d7167 2018-04-29 stsp optind = 0;
3231 c2301be8 2018-04-30 stsp optreset = 1;
3233 c2301be8 2018-04-30 stsp if (argc == 0) {
3234 f29d3e89 2018-06-23 stsp if (hflag)
3236 c2301be8 2018-04-30 stsp /* Build an argument vector which runs a default command. */
3237 9f7d7167 2018-04-29 stsp cmd = &tog_commands[0];
3238 c2301be8 2018-04-30 stsp cmd_argv = make_argv(cmd->name, NULL);
3243 c2301be8 2018-04-30 stsp /* Did the user specific a command? */
3244 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
3245 c2301be8 2018-04-30 stsp if (strncmp(tog_commands[i].name, argv[0],
3246 9f7d7167 2018-04-29 stsp strlen(argv[0])) == 0) {
3247 9f7d7167 2018-04-29 stsp cmd = &tog_commands[i];
3248 9f7d7167 2018-04-29 stsp if (hflag)
3249 9f7d7167 2018-04-29 stsp tog_commands[i].cmd_usage();
3253 9f7d7167 2018-04-29 stsp if (cmd == NULL) {
3254 c2301be8 2018-04-30 stsp /* Did the user specify a repository? */
3255 c2301be8 2018-04-30 stsp char *repo_path = realpath(argv[0], NULL);
3256 c2301be8 2018-04-30 stsp if (repo_path) {
3257 c2301be8 2018-04-30 stsp struct got_repository *repo;
3258 c2301be8 2018-04-30 stsp error = got_repo_open(&repo, repo_path);
3259 c2301be8 2018-04-30 stsp if (error == NULL)
3260 c2301be8 2018-04-30 stsp got_repo_close(repo);
3262 ad7de8d9 2018-04-30 stsp error = got_error_from_errno();
3263 c2301be8 2018-04-30 stsp if (error) {
3264 f29d3e89 2018-06-23 stsp if (hflag) {
3265 f29d3e89 2018-06-23 stsp fprintf(stderr, "%s: '%s' is not a "
3266 f29d3e89 2018-06-23 stsp "known command\n", getprogname(),
3270 ad7de8d9 2018-04-30 stsp fprintf(stderr, "%s: '%s' is neither a known "
3271 ad7de8d9 2018-04-30 stsp "command nor a path to a repository\n",
3272 c2301be8 2018-04-30 stsp getprogname(), argv[0]);
3273 ad7de8d9 2018-04-30 stsp free(repo_path);
3276 c2301be8 2018-04-30 stsp cmd = &tog_commands[0];
3277 c2301be8 2018-04-30 stsp cmd_argv = make_argv(cmd->name, repo_path);
3279 c2301be8 2018-04-30 stsp free(repo_path);
3283 5c5136c5 2018-05-20 stsp init_curses();
3285 c2301be8 2018-04-30 stsp error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
3286 9f7d7167 2018-04-29 stsp if (error)
3287 9f7d7167 2018-04-29 stsp goto done;
3290 c2301be8 2018-04-30 stsp free(cmd_argv);
3291 9f7d7167 2018-04-29 stsp if (error)
3292 9f7d7167 2018-04-29 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);