2 9f7d7167 2018-04-29 stsp * Copyright (c) 2018 Stefan Sperling <stsp@openbsd.org>
4 9f7d7167 2018-04-29 stsp * Permission to use, copy, modify, and distribute this software for any
5 9f7d7167 2018-04-29 stsp * purpose with or without fee is hereby granted, provided that the above
6 9f7d7167 2018-04-29 stsp * copyright notice and this permission notice appear in all copies.
8 9f7d7167 2018-04-29 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 9f7d7167 2018-04-29 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 9f7d7167 2018-04-29 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 9f7d7167 2018-04-29 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 9f7d7167 2018-04-29 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 9f7d7167 2018-04-29 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 9f7d7167 2018-04-29 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 80ddbec8 2018-04-29 stsp #include <sys/queue.h>
18 ffd1d5e5 2018-06-23 stsp #include <sys/stat.h>
20 31120ada 2018-04-30 stsp #include <errno.h>
21 61e69b96 2018-05-20 stsp #define _XOPEN_SOURCE_EXTENDED
22 9f7d7167 2018-04-29 stsp #include <curses.h>
23 61e69b96 2018-05-20 stsp #undef _XOPEN_SOURCE_EXTENDED
24 9f7d7167 2018-04-29 stsp #include <panel.h>
25 9f7d7167 2018-04-29 stsp #include <locale.h>
26 9f7d7167 2018-04-29 stsp #include <stdlib.h>
27 26ed57b2 2018-05-19 stsp #include <stdio.h>
28 9f7d7167 2018-04-29 stsp #include <getopt.h>
29 9f7d7167 2018-04-29 stsp #include <string.h>
30 9f7d7167 2018-04-29 stsp #include <err.h>
31 80ddbec8 2018-04-29 stsp #include <unistd.h>
32 26ed57b2 2018-05-19 stsp #include <util.h>
33 26ed57b2 2018-05-19 stsp #include <limits.h>
34 61e69b96 2018-05-20 stsp #include <wchar.h>
35 788c352e 2018-06-16 stsp #include <time.h>
36 84451b3e 2018-07-10 stsp #include <pthread.h>
38 9f7d7167 2018-04-29 stsp #include "got_error.h"
39 80ddbec8 2018-04-29 stsp #include "got_object.h"
40 80ddbec8 2018-04-29 stsp #include "got_reference.h"
41 80ddbec8 2018-04-29 stsp #include "got_repository.h"
42 80ddbec8 2018-04-29 stsp #include "got_diff.h"
43 511a516b 2018-05-19 stsp #include "got_opentemp.h"
44 9ba79e04 2018-06-11 stsp #include "got_commit_graph.h"
45 00dfcb92 2018-06-11 stsp #include "got_utf8.h"
46 a70480e0 2018-06-23 stsp #include "got_blame.h"
49 881b2d3e 2018-04-30 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
52 9f7d7167 2018-04-29 stsp #ifndef nitems
53 9f7d7167 2018-04-29 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
56 9f7d7167 2018-04-29 stsp struct tog_cmd {
57 c2301be8 2018-04-30 stsp const char *name;
58 9f7d7167 2018-04-29 stsp const struct got_error *(*cmd_main)(int, char *[]);
59 9f7d7167 2018-04-29 stsp void (*cmd_usage)(void);
60 c2301be8 2018-04-30 stsp const char *descr;
63 4ed7e80c 2018-05-20 stsp __dead static void usage(void);
64 4ed7e80c 2018-05-20 stsp __dead static void usage_log(void);
65 4ed7e80c 2018-05-20 stsp __dead static void usage_diff(void);
66 4ed7e80c 2018-05-20 stsp __dead static void usage_blame(void);
67 ffd1d5e5 2018-06-23 stsp __dead static void usage_tree(void);
69 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_log(int, char *[]);
70 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_diff(int, char *[]);
71 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_blame(int, char *[]);
72 ffd1d5e5 2018-06-23 stsp static const struct got_error* cmd_tree(int, char *[]);
74 4ed7e80c 2018-05-20 stsp static struct tog_cmd tog_commands[] = {
75 cbb6b58a 2018-05-20 stsp { "log", cmd_log, usage_log,
76 9f7d7167 2018-04-29 stsp "show repository history" },
77 cbb6b58a 2018-05-20 stsp { "diff", cmd_diff, usage_diff,
78 9f7d7167 2018-04-29 stsp "compare files and directories" },
79 cbb6b58a 2018-05-20 stsp { "blame", cmd_blame, usage_blame,
80 9f7d7167 2018-04-29 stsp "show line-by-line file history" },
81 ffd1d5e5 2018-06-23 stsp { "tree", cmd_tree, usage_tree,
82 ffd1d5e5 2018-06-23 stsp "browse trees in repository" },
85 d6b05b5b 2018-08-04 stsp enum tog_view_type {
86 d6b05b5b 2018-08-04 stsp TOG_VIEW_DIFF,
87 d6b05b5b 2018-08-04 stsp TOG_VIEW_LOG,
88 ad80ab7b 2018-08-04 stsp TOG_VIEW_BLAME,
89 ad80ab7b 2018-08-04 stsp TOG_VIEW_TREE
92 ad80ab7b 2018-08-04 stsp struct tog_diff_view_state {
93 a3404814 2018-09-02 stsp struct got_object_id *id1, *id2;
95 ad80ab7b 2018-08-04 stsp int first_displayed_line;
96 ad80ab7b 2018-08-04 stsp int last_displayed_line;
100 ba4f502b 2018-08-04 stsp struct commit_queue_entry {
101 ba4f502b 2018-08-04 stsp TAILQ_ENTRY(commit_queue_entry) entry;
102 ba4f502b 2018-08-04 stsp struct got_object_id *id;
103 ba4f502b 2018-08-04 stsp struct got_commit_object *commit;
105 ba4f502b 2018-08-04 stsp TAILQ_HEAD(commit_queue_head, commit_queue_entry);
106 ba4f502b 2018-08-04 stsp struct commit_queue {
107 ba4f502b 2018-08-04 stsp int ncommits;
108 ba4f502b 2018-08-04 stsp struct commit_queue_head head;
111 b01e7d3b 2018-08-04 stsp struct tog_log_view_state {
112 b01e7d3b 2018-08-04 stsp struct got_commit_graph *graph;
113 b01e7d3b 2018-08-04 stsp struct commit_queue commits;
114 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *first_displayed_entry;
115 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *last_displayed_entry;
116 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *selected_entry;
117 b01e7d3b 2018-08-04 stsp int selected;
118 b01e7d3b 2018-08-04 stsp char *in_repo_path;
119 b01e7d3b 2018-08-04 stsp struct got_repository *repo;
122 e9424729 2018-08-04 stsp struct tog_blame_cb_args {
123 e9424729 2018-08-04 stsp pthread_mutex_t *mutex;
124 e9424729 2018-08-04 stsp struct tog_blame_line *lines; /* one per line */
125 e9424729 2018-08-04 stsp int nlines;
127 e9424729 2018-08-04 stsp struct tog_view *view;
128 e9424729 2018-08-04 stsp struct got_object_id *commit_id;
130 e9424729 2018-08-04 stsp const char *path;
131 e9424729 2018-08-04 stsp int *first_displayed_line;
132 e9424729 2018-08-04 stsp int *last_displayed_line;
133 e9424729 2018-08-04 stsp int *selected_line;
138 e9424729 2018-08-04 stsp struct tog_blame_thread_args {
139 e9424729 2018-08-04 stsp const char *path;
140 e9424729 2018-08-04 stsp struct got_repository *repo;
141 e9424729 2018-08-04 stsp struct tog_blame_cb_args *cb_args;
142 e9424729 2018-08-04 stsp int *complete;
145 e9424729 2018-08-04 stsp struct tog_blame {
147 e9424729 2018-08-04 stsp size_t filesize;
148 e9424729 2018-08-04 stsp struct tog_blame_line *lines;
149 e9424729 2018-08-04 stsp size_t nlines;
150 e9424729 2018-08-04 stsp pthread_t thread;
151 e9424729 2018-08-04 stsp struct tog_blame_thread_args thread_args;
152 e9424729 2018-08-04 stsp struct tog_blame_cb_args cb_args;
153 e9424729 2018-08-04 stsp const char *path;
156 7cbe629d 2018-08-04 stsp struct tog_blame_view_state {
157 7cbe629d 2018-08-04 stsp int first_displayed_line;
158 7cbe629d 2018-08-04 stsp int last_displayed_line;
159 7cbe629d 2018-08-04 stsp int selected_line;
160 7cbe629d 2018-08-04 stsp int blame_complete;
163 7cbe629d 2018-08-04 stsp pthread_mutex_t mutex;
164 7cbe629d 2018-08-04 stsp struct got_object_id_queue blamed_commits;
165 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
166 e5a0f69f 2018-08-18 stsp char *path;
167 7cbe629d 2018-08-04 stsp struct got_repository *repo;
168 ad80ab7b 2018-08-04 stsp struct got_object_id *commit_id;
169 e9424729 2018-08-04 stsp struct tog_blame blame;
172 ad80ab7b 2018-08-04 stsp struct tog_parent_tree {
173 ad80ab7b 2018-08-04 stsp TAILQ_ENTRY(tog_parent_tree) entry;
174 ad80ab7b 2018-08-04 stsp struct got_tree_object *tree;
175 ad80ab7b 2018-08-04 stsp struct got_tree_entry *first_displayed_entry;
176 ad80ab7b 2018-08-04 stsp struct got_tree_entry *selected_entry;
177 ad80ab7b 2018-08-04 stsp int selected;
180 ad80ab7b 2018-08-04 stsp TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
182 ad80ab7b 2018-08-04 stsp struct tog_tree_view_state {
183 ad80ab7b 2018-08-04 stsp char *tree_label;
184 ad80ab7b 2018-08-04 stsp struct got_tree_object *root;
185 ad80ab7b 2018-08-04 stsp struct got_tree_object *tree;
186 ad80ab7b 2018-08-04 stsp const struct got_tree_entries *entries;
187 ad80ab7b 2018-08-04 stsp struct got_tree_entry *first_displayed_entry;
188 ad80ab7b 2018-08-04 stsp struct got_tree_entry *last_displayed_entry;
189 ad80ab7b 2018-08-04 stsp struct got_tree_entry *selected_entry;
190 ad80ab7b 2018-08-04 stsp int nentries, ndisplayed, selected, show_ids;
191 ad80ab7b 2018-08-04 stsp struct tog_parent_trees parents;
192 7cbe629d 2018-08-04 stsp struct got_object_id *commit_id;
193 ad80ab7b 2018-08-04 stsp struct got_repository *repo;
196 bcbd79e2 2018-08-19 stsp TAILQ_HEAD(tog_view_list_head, tog_view);
197 cc3c9aac 2018-08-01 stsp struct tog_view {
198 e5a0f69f 2018-08-18 stsp TAILQ_ENTRY(tog_view) entry;
199 26ed57b2 2018-05-19 stsp WINDOW *window;
200 26ed57b2 2018-05-19 stsp PANEL *panel;
201 97ddc146 2018-08-01 stsp int nlines, ncols, begin_y, begin_x;
202 f7d12f7e 2018-08-01 stsp int lines, cols; /* copies of LINES and COLS */
203 a3404814 2018-09-02 stsp int focussed;
204 6d0fee91 2018-08-01 stsp struct tog_view *parent;
205 bcbd79e2 2018-08-19 stsp struct tog_view *child;
207 5dc9f4bc 2018-08-04 stsp /* type-specific state */
208 d6b05b5b 2018-08-04 stsp enum tog_view_type type;
210 b01e7d3b 2018-08-04 stsp struct tog_diff_view_state diff;
211 b01e7d3b 2018-08-04 stsp struct tog_log_view_state log;
212 7cbe629d 2018-08-04 stsp struct tog_blame_view_state blame;
213 ad80ab7b 2018-08-04 stsp struct tog_tree_view_state tree;
216 e5a0f69f 2018-08-18 stsp const struct got_error *(*show)(struct tog_view *);
217 e5a0f69f 2018-08-18 stsp const struct got_error *(*input)(struct tog_view **,
218 e5a0f69f 2018-08-18 stsp struct tog_view **, struct tog_view *, int);
219 bcbd79e2 2018-08-19 stsp const struct got_error *(*set_child)(struct tog_view *,
220 bcbd79e2 2018-08-19 stsp struct tog_view *);
221 e5a0f69f 2018-08-18 stsp const struct got_error *(*close)(struct tog_view *);
224 ba4f502b 2018-08-04 stsp static const struct got_error *open_diff_view(struct tog_view *,
225 ba4f502b 2018-08-04 stsp struct got_object *, struct got_object *, struct got_repository *);
226 5dc9f4bc 2018-08-04 stsp static const struct got_error *show_diff_view(struct tog_view *);
227 e5a0f69f 2018-08-18 stsp static const struct got_error *input_diff_view(struct tog_view **,
228 e5a0f69f 2018-08-18 stsp struct tog_view **, struct tog_view *, int);
229 e5a0f69f 2018-08-18 stsp static const struct got_error* close_diff_view(struct tog_view *);
231 ba4f502b 2018-08-04 stsp static const struct got_error *open_log_view(struct tog_view *,
232 ba4f502b 2018-08-04 stsp struct got_object_id *, struct got_repository *, const char *);
233 ba4f502b 2018-08-04 stsp static const struct got_error * show_log_view(struct tog_view *);
234 e5a0f69f 2018-08-18 stsp static const struct got_error *input_log_view(struct tog_view **,
235 e5a0f69f 2018-08-18 stsp struct tog_view **, struct tog_view *, int);
236 e5a0f69f 2018-08-18 stsp static const struct got_error *close_log_view(struct tog_view *);
237 bcbd79e2 2018-08-19 stsp static const struct got_error* set_child_log_view(struct tog_view *,
238 bcbd79e2 2018-08-19 stsp struct tog_view *);
240 e5a0f69f 2018-08-18 stsp static const struct got_error *open_blame_view(struct tog_view *, char *,
241 5221c383 2018-08-01 stsp struct got_object_id *, struct got_repository *);
242 7cbe629d 2018-08-04 stsp static const struct got_error *show_blame_view(struct tog_view *);
243 e5a0f69f 2018-08-18 stsp static const struct got_error *input_blame_view(struct tog_view **,
244 e5a0f69f 2018-08-18 stsp struct tog_view **, struct tog_view *, int);
245 e5a0f69f 2018-08-18 stsp static const struct got_error *close_blame_view(struct tog_view *);
247 ad80ab7b 2018-08-04 stsp static const struct got_error *open_tree_view(struct tog_view *,
248 4fc679ca 2018-08-04 stsp struct got_tree_object *, struct got_object_id *, struct got_repository *);
249 ad80ab7b 2018-08-04 stsp static const struct got_error *show_tree_view(struct tog_view *);
250 e5a0f69f 2018-08-18 stsp static const struct got_error *input_tree_view(struct tog_view **,
251 e5a0f69f 2018-08-18 stsp struct tog_view **, struct tog_view *, int);
252 e5a0f69f 2018-08-18 stsp static const struct got_error *close_tree_view(struct tog_view *);
254 e5a0f69f 2018-08-18 stsp static const struct got_error *
255 96a765a8 2018-08-04 stsp view_close(struct tog_view *view)
257 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
259 bcbd79e2 2018-08-19 stsp if (view->child)
260 bcbd79e2 2018-08-19 stsp view->child->parent = NULL;
261 bcbd79e2 2018-08-19 stsp if (view->parent)
262 bcbd79e2 2018-08-19 stsp view->parent->child = NULL;
263 e5a0f69f 2018-08-18 stsp if (view->close)
264 e5a0f69f 2018-08-18 stsp err = view->close(view);
265 ea5e7bb5 2018-08-01 stsp if (view->panel)
266 ea5e7bb5 2018-08-01 stsp del_panel(view->panel);
267 ea5e7bb5 2018-08-01 stsp if (view->window)
268 ea5e7bb5 2018-08-01 stsp delwin(view->window);
269 ea5e7bb5 2018-08-01 stsp free(view);
270 e5a0f69f 2018-08-18 stsp return err;
273 ea5e7bb5 2018-08-01 stsp static struct tog_view *
274 b3665f43 2018-08-04 stsp view_open(int nlines, int ncols, int begin_y, int begin_x,
275 d6b05b5b 2018-08-04 stsp struct tog_view *parent, enum tog_view_type type)
277 ad80ab7b 2018-08-04 stsp struct tog_view *view = calloc(1, sizeof(*view));
279 ea5e7bb5 2018-08-01 stsp if (view == NULL)
280 ea5e7bb5 2018-08-01 stsp return NULL;
282 dd04dd3f 2018-08-19 stsp if (begin_x == 0 && parent && parent->ncols - 80 > 10)
283 dd04dd3f 2018-08-19 stsp begin_x = parent->ncols - 80;
285 6d0fee91 2018-08-01 stsp view->parent = parent;
286 bcbd79e2 2018-08-19 stsp if (parent)
287 bcbd79e2 2018-08-19 stsp parent->child = view;
288 d6b05b5b 2018-08-04 stsp view->type = type;
289 f7d12f7e 2018-08-01 stsp view->lines = LINES;
290 f7d12f7e 2018-08-01 stsp view->cols = COLS;
291 207b9029 2018-08-01 stsp view->nlines = nlines ? nlines : LINES - begin_y;
292 207b9029 2018-08-01 stsp view->ncols = ncols ? ncols : COLS - begin_x;
293 97ddc146 2018-08-01 stsp view->begin_y = begin_y;
294 97ddc146 2018-08-01 stsp view->begin_x = begin_x;
295 842167bf 2018-08-01 stsp view->window = newwin(nlines, ncols, begin_y, begin_x);
296 ea5e7bb5 2018-08-01 stsp if (view->window == NULL) {
297 96a765a8 2018-08-04 stsp view_close(view);
298 ea5e7bb5 2018-08-01 stsp return NULL;
300 ea5e7bb5 2018-08-01 stsp view->panel = new_panel(view->window);
301 ea5e7bb5 2018-08-01 stsp if (view->panel == NULL) {
302 96a765a8 2018-08-04 stsp view_close(view);
303 ea5e7bb5 2018-08-01 stsp return NULL;
306 ea5e7bb5 2018-08-01 stsp keypad(view->window, TRUE);
307 ea5e7bb5 2018-08-01 stsp return view;
310 4d8c2215 2018-08-19 stsp static const struct got_error *
311 6acd1bd1 2018-08-04 stsp view_show(struct tog_view *view)
313 e5a0f69f 2018-08-18 stsp const struct got_error *err;
315 bcbd79e2 2018-08-19 stsp if (view->parent) {
316 bcbd79e2 2018-08-19 stsp err = view->parent->show(view->parent);
318 bcbd79e2 2018-08-19 stsp return err;
319 bcbd79e2 2018-08-19 stsp show_panel(view->parent->panel);
322 e5a0f69f 2018-08-18 stsp err = view->show(view);
324 e5a0f69f 2018-08-18 stsp return err;
325 cdf1ee82 2018-08-01 stsp show_panel(view->panel);
327 a3404814 2018-09-02 stsp if (view->child && view->child->begin_x > view->begin_x) {
328 bcbd79e2 2018-08-19 stsp err = view->child->show(view->child);
330 bcbd79e2 2018-08-19 stsp return err;
331 bcbd79e2 2018-08-19 stsp show_panel(view->child->panel);
334 cdf1ee82 2018-08-01 stsp update_panels();
335 e5a0f69f 2018-08-18 stsp doupdate();
337 e5a0f69f 2018-08-18 stsp return err;
340 4d8c2215 2018-08-19 stsp static const struct got_error *
341 f7d12f7e 2018-08-01 stsp view_resize(struct tog_view *view)
343 f7d12f7e 2018-08-01 stsp int nlines, ncols;
345 6d0fee91 2018-08-01 stsp while (view) {
346 6d0fee91 2018-08-01 stsp if (view->lines > LINES)
347 6d0fee91 2018-08-01 stsp nlines = view->nlines - (view->lines - LINES);
349 6d0fee91 2018-08-01 stsp nlines = view->nlines + (LINES - view->lines);
351 6d0fee91 2018-08-01 stsp if (view->cols > COLS)
352 6d0fee91 2018-08-01 stsp ncols = view->ncols - (view->cols - COLS);
354 6d0fee91 2018-08-01 stsp ncols = view->ncols + (COLS - view->cols);
356 6d0fee91 2018-08-01 stsp if (wresize(view->window, nlines, ncols) == ERR)
357 6d0fee91 2018-08-01 stsp return got_error_from_errno();
358 77a567dd 2018-08-01 stsp replace_panel(view->panel, view->window);
360 6d0fee91 2018-08-01 stsp view->nlines = nlines;
361 6d0fee91 2018-08-01 stsp view->ncols = ncols;
362 6d0fee91 2018-08-01 stsp view->lines = LINES;
363 6d0fee91 2018-08-01 stsp view->cols = COLS;
365 6d0fee91 2018-08-01 stsp view = view->parent;
368 f7d12f7e 2018-08-01 stsp return NULL;
371 e5a0f69f 2018-08-18 stsp static const struct got_error *
372 48fcc512 2018-08-18 stsp view_input(struct tog_view **new, struct tog_view **dead,
373 e4197bf9 2018-08-18 stsp struct tog_view **focus, int *done, struct tog_view *view,
374 48fcc512 2018-08-18 stsp struct tog_view_list_head *views)
376 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
377 31607d6c 2018-08-18 stsp struct tog_view *next, *prev;
380 e5a0f69f 2018-08-18 stsp *new = NULL;
381 e5a0f69f 2018-08-18 stsp *dead = NULL;
383 e5a0f69f 2018-08-18 stsp nodelay(stdscr, FALSE);
384 e5a0f69f 2018-08-18 stsp ch = wgetch(view->window);
385 e5a0f69f 2018-08-18 stsp nodelay(stdscr, TRUE);
386 e5a0f69f 2018-08-18 stsp switch (ch) {
390 48fcc512 2018-08-18 stsp next = TAILQ_NEXT(view, entry);
392 48fcc512 2018-08-18 stsp *focus = next;
394 48fcc512 2018-08-18 stsp *focus = TAILQ_FIRST(views);
395 a3404814 2018-09-02 stsp view->focussed = 0;
396 a3404814 2018-09-02 stsp (*focus)->focussed = 1;
398 31607d6c 2018-08-18 stsp case KEY_BACKSPACE:
399 31607d6c 2018-08-18 stsp prev = TAILQ_PREV(view, tog_view_list_head, entry);
401 31607d6c 2018-08-18 stsp *focus = prev;
403 31607d6c 2018-08-18 stsp *focus = TAILQ_LAST(views, tog_view_list_head);
404 786292d9 2018-09-02 stsp view->focussed = 0;
405 786292d9 2018-09-02 stsp (*focus)->focussed = 1;
408 e5a0f69f 2018-08-18 stsp err = view->input(new, dead, view, ch);
409 e5a0f69f 2018-08-18 stsp *dead = view;
414 e5a0f69f 2018-08-18 stsp case KEY_RESIZE:
415 e5a0f69f 2018-08-18 stsp err = view_resize(view);
417 e5a0f69f 2018-08-18 stsp return err;
418 e5a0f69f 2018-08-18 stsp err = view->input(new, dead, view, ch);
421 e5a0f69f 2018-08-18 stsp err = view->input(new, dead, view, ch);
425 e5a0f69f 2018-08-18 stsp return err;
428 e5a0f69f 2018-08-18 stsp static const struct got_error *
429 bcbd79e2 2018-08-19 stsp view_set_child(struct tog_view *view, struct tog_view *child)
431 bcbd79e2 2018-08-19 stsp const struct got_error *err;
433 bcbd79e2 2018-08-19 stsp if (view->set_child) {
434 bcbd79e2 2018-08-19 stsp err = view->set_child(view, child);
436 bcbd79e2 2018-08-19 stsp return err;
439 bcbd79e2 2018-08-19 stsp view->child = child;
440 bcbd79e2 2018-08-19 stsp return NULL;
444 1a57306a 2018-09-02 stsp view_vborder(struct tog_view *view)
446 1a57306a 2018-09-02 stsp if (view->child == NULL)
449 1a57306a 2018-09-02 stsp mvwvline(view->window, view->begin_y, view->child->begin_x - 1,
450 1a57306a 2018-09-02 stsp got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
454 a3404814 2018-09-02 stsp view_needs_focus_indication(struct tog_view *view)
456 a3404814 2018-09-02 stsp if (!view->focussed)
459 a3404814 2018-09-02 stsp if (view->child && view->child->begin_x > view->begin_x)
462 a3404814 2018-09-02 stsp if (view->parent && view->begin_x > view->parent->begin_x)
468 bcbd79e2 2018-08-19 stsp static const struct got_error *
469 e5a0f69f 2018-08-18 stsp view_loop(struct tog_view *view)
471 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
472 e5a0f69f 2018-08-18 stsp struct tog_view_list_head views;
473 e5a0f69f 2018-08-18 stsp struct tog_view *new_view, *dead_view;
474 e4197bf9 2018-08-18 stsp int done = 0;
476 e5a0f69f 2018-08-18 stsp TAILQ_INIT(&views);
477 e5a0f69f 2018-08-18 stsp TAILQ_INSERT_HEAD(&views, view, entry);
479 a3404814 2018-09-02 stsp view->focussed = 1;
480 e4197bf9 2018-08-18 stsp while (!TAILQ_EMPTY(&views) && !done) {
481 e5a0f69f 2018-08-18 stsp err = view_show(view);
484 e4197bf9 2018-08-18 stsp err = view_input(&new_view, &dead_view, &view, &done,
485 e4197bf9 2018-08-18 stsp view, &views);
488 e5a0f69f 2018-08-18 stsp if (dead_view) {
489 89864818 2018-09-02 stsp struct tog_view *v, *t;
490 e5a0f69f 2018-08-18 stsp TAILQ_REMOVE(&views, dead_view, entry);
491 89864818 2018-09-02 stsp TAILQ_FOREACH_SAFE(v, &views, entry, t) {
492 89864818 2018-09-02 stsp if (v->parent == dead_view) {
493 89864818 2018-09-02 stsp TAILQ_REMOVE(&views, v, entry);
494 7e1e2c14 2018-09-02 stsp err = view_close(v);
499 e5a0f69f 2018-08-18 stsp if (dead_view->parent)
500 e5a0f69f 2018-08-18 stsp view = dead_view->parent;
502 e5a0f69f 2018-08-18 stsp view = TAILQ_LAST(&views, tog_view_list_head);
504 a3404814 2018-09-02 stsp view->focussed = 1;
505 e5a0f69f 2018-08-18 stsp err = view_close(dead_view);
509 bcbd79e2 2018-08-19 stsp if (new_view) {
510 a3404814 2018-09-02 stsp view->focussed = 0;
511 bcbd79e2 2018-08-19 stsp /* TODO: de-duplicate! */
512 bcbd79e2 2018-08-19 stsp TAILQ_INSERT_TAIL(&views, new_view, entry);
513 bcbd79e2 2018-08-19 stsp if (new_view->parent) {
514 bcbd79e2 2018-08-19 stsp err = view_set_child(new_view->parent, new_view);
517 a3404814 2018-09-02 stsp new_view->parent->focussed = 0;
519 bcbd79e2 2018-08-19 stsp view = new_view;
520 a3404814 2018-09-02 stsp view->focussed = 1;
524 e5a0f69f 2018-08-18 stsp while (!TAILQ_EMPTY(&views)) {
525 e5a0f69f 2018-08-18 stsp view = TAILQ_FIRST(&views);
526 e5a0f69f 2018-08-18 stsp TAILQ_REMOVE(&views, view, entry);
527 e5a0f69f 2018-08-18 stsp view_close(view);
529 e5a0f69f 2018-08-18 stsp return err;
532 4ed7e80c 2018-05-20 stsp __dead static void
533 9f7d7167 2018-04-29 stsp usage_log(void)
536 c70c5802 2018-08-01 stsp fprintf(stderr,
537 c70c5802 2018-08-01 stsp "usage: %s log [-c commit] [-r repository-path] [path]\n",
538 9f7d7167 2018-04-29 stsp getprogname());
542 963b370f 2018-05-20 stsp /* Create newly allocated wide-character string equivalent to a byte string. */
543 80ddbec8 2018-04-29 stsp static const struct got_error *
544 963b370f 2018-05-20 stsp mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
546 00dfcb92 2018-06-11 stsp char *vis = NULL;
547 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
549 963b370f 2018-05-20 stsp *ws = NULL;
550 963b370f 2018-05-20 stsp *wlen = mbstowcs(NULL, s, 0);
551 00dfcb92 2018-06-11 stsp if (*wlen == (size_t)-1) {
552 00dfcb92 2018-06-11 stsp int vislen;
553 00dfcb92 2018-06-11 stsp if (errno != EILSEQ)
554 00dfcb92 2018-06-11 stsp return got_error_from_errno();
556 00dfcb92 2018-06-11 stsp /* byte string invalid in current encoding; try to "fix" it */
557 00dfcb92 2018-06-11 stsp err = got_mbsavis(&vis, &vislen, s);
559 00dfcb92 2018-06-11 stsp return err;
560 00dfcb92 2018-06-11 stsp *wlen = mbstowcs(NULL, vis, 0);
561 a7f50699 2018-06-11 stsp if (*wlen == (size_t)-1) {
562 a7f50699 2018-06-11 stsp err = got_error_from_errno(); /* give up */
567 963b370f 2018-05-20 stsp *ws = calloc(*wlen + 1, sizeof(*ws));
568 a7f50699 2018-06-11 stsp if (*ws == NULL) {
569 a7f50699 2018-06-11 stsp err = got_error_from_errno();
573 00dfcb92 2018-06-11 stsp if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
574 963b370f 2018-05-20 stsp err = got_error_from_errno();
579 963b370f 2018-05-20 stsp *ws = NULL;
582 963b370f 2018-05-20 stsp return err;
585 963b370f 2018-05-20 stsp /* Format a line for display, ensuring that it won't overflow a width limit. */
586 963b370f 2018-05-20 stsp static const struct got_error *
587 ffd1d5e5 2018-06-23 stsp format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit)
589 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
590 963b370f 2018-05-20 stsp int cols = 0;
591 963b370f 2018-05-20 stsp wchar_t *wline = NULL;
592 963b370f 2018-05-20 stsp size_t wlen;
595 963b370f 2018-05-20 stsp *wlinep = NULL;
596 b700b5d6 2018-07-10 stsp *widthp = 0;
598 963b370f 2018-05-20 stsp err = mbs2ws(&wline, &wlen, line);
600 963b370f 2018-05-20 stsp return err;
603 b700b5d6 2018-07-10 stsp while (i < wlen && cols < wlimit) {
604 963b370f 2018-05-20 stsp int width = wcwidth(wline[i]);
605 963b370f 2018-05-20 stsp switch (width) {
611 3c1f04f1 2018-09-13 stsp if (cols + width <= wlimit)
612 b700b5d6 2018-07-10 stsp cols += width;
616 963b370f 2018-05-20 stsp if (wline[i] == L'\t')
617 b700b5d6 2018-07-10 stsp cols += TABSIZE - ((cols + 1) % TABSIZE);
621 963b370f 2018-05-20 stsp err = got_error_from_errno();
625 963b370f 2018-05-20 stsp wline[i] = L'\0';
626 b700b5d6 2018-07-10 stsp if (widthp)
627 b700b5d6 2018-07-10 stsp *widthp = cols;
630 963b370f 2018-05-20 stsp free(wline);
632 963b370f 2018-05-20 stsp *wlinep = wline;
633 963b370f 2018-05-20 stsp return err;
636 963b370f 2018-05-20 stsp static const struct got_error *
637 2814baeb 2018-08-01 stsp draw_commit(struct tog_view *view, struct got_commit_object *commit,
638 2814baeb 2018-08-01 stsp struct got_object_id *id)
640 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
641 b39d25c7 2018-07-10 stsp char datebuf[10]; /* YY-MM-DD + SPACE + NUL */
642 80ddbec8 2018-04-29 stsp char *logmsg0 = NULL, *logmsg = NULL;
643 6d9fbc00 2018-04-29 stsp char *author0 = NULL, *author = NULL;
644 bb737323 2018-05-20 stsp wchar_t *wlogmsg = NULL, *wauthor = NULL;
645 bb737323 2018-05-20 stsp int author_width, logmsg_width;
646 6d9fbc00 2018-04-29 stsp char *newline, *smallerthan;
647 80ddbec8 2018-04-29 stsp char *line = NULL;
648 bb737323 2018-05-20 stsp int col, limit;
649 b39d25c7 2018-07-10 stsp static const size_t date_display_cols = 9;
650 bb737323 2018-05-20 stsp static const size_t author_display_cols = 16;
651 f7d12f7e 2018-08-01 stsp const int avail = view->ncols;
653 c70c5802 2018-08-01 stsp if (strftime(datebuf, sizeof(datebuf), "%g/%m/%d ",
654 c70c5802 2018-08-01 stsp &commit->tm_committer) >= sizeof(datebuf))
655 b39d25c7 2018-07-10 stsp return got_error(GOT_ERR_NO_SPACE);
657 b39d25c7 2018-07-10 stsp if (avail < date_display_cols)
658 b39d25c7 2018-07-10 stsp limit = MIN(sizeof(datebuf) - 1, avail);
660 b39d25c7 2018-07-10 stsp limit = MIN(date_display_cols, sizeof(datebuf) - 1);
661 2814baeb 2018-08-01 stsp waddnstr(view->window, datebuf, limit);
662 b39d25c7 2018-07-10 stsp col = limit + 1;
663 b39d25c7 2018-07-10 stsp if (col > avail)
666 6d9fbc00 2018-04-29 stsp author0 = strdup(commit->author);
667 6d9fbc00 2018-04-29 stsp if (author0 == NULL) {
668 80ddbec8 2018-04-29 stsp err = got_error_from_errno();
671 6d9fbc00 2018-04-29 stsp author = author0;
672 6d9fbc00 2018-04-29 stsp smallerthan = strchr(author, '<');
673 6d9fbc00 2018-04-29 stsp if (smallerthan)
674 6d9fbc00 2018-04-29 stsp *smallerthan = '\0';
676 6d9fbc00 2018-04-29 stsp char *at = strchr(author, '@');
678 6d9fbc00 2018-04-29 stsp *at = '\0';
680 b39d25c7 2018-07-10 stsp limit = avail - col;
681 bb737323 2018-05-20 stsp err = format_line(&wauthor, &author_width, author, limit);
684 2814baeb 2018-08-01 stsp waddwstr(view->window, wauthor);
685 bb737323 2018-05-20 stsp col += author_width;
686 9c2eaf34 2018-05-20 stsp while (col <= avail && author_width < author_display_cols + 1) {
687 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
689 bb737323 2018-05-20 stsp author_width++;
691 9c2eaf34 2018-05-20 stsp if (col > avail)
694 bb737323 2018-05-20 stsp logmsg0 = strdup(commit->logmsg);
695 bb737323 2018-05-20 stsp if (logmsg0 == NULL) {
696 6d9fbc00 2018-04-29 stsp err = got_error_from_errno();
699 bb737323 2018-05-20 stsp logmsg = logmsg0;
700 bb737323 2018-05-20 stsp while (*logmsg == '\n')
702 bb737323 2018-05-20 stsp newline = strchr(logmsg, '\n');
703 bb737323 2018-05-20 stsp if (newline)
704 bb737323 2018-05-20 stsp *newline = '\0';
705 bb737323 2018-05-20 stsp limit = avail - col;
706 bb737323 2018-05-20 stsp err = format_line(&wlogmsg, &logmsg_width, logmsg, limit);
709 2814baeb 2018-08-01 stsp waddwstr(view->window, wlogmsg);
710 bb737323 2018-05-20 stsp col += logmsg_width;
711 bb737323 2018-05-20 stsp while (col <= avail) {
712 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
716 80ddbec8 2018-04-29 stsp free(logmsg0);
717 bb737323 2018-05-20 stsp free(wlogmsg);
718 6d9fbc00 2018-04-29 stsp free(author0);
719 bb737323 2018-05-20 stsp free(wauthor);
720 80ddbec8 2018-04-29 stsp free(line);
721 80ddbec8 2018-04-29 stsp return err;
724 899d86c2 2018-05-10 stsp static struct commit_queue_entry *
725 899d86c2 2018-05-10 stsp alloc_commit_queue_entry(struct got_commit_object *commit,
726 899d86c2 2018-05-10 stsp struct got_object_id *id)
728 80ddbec8 2018-04-29 stsp struct commit_queue_entry *entry;
730 80ddbec8 2018-04-29 stsp entry = calloc(1, sizeof(*entry));
731 80ddbec8 2018-04-29 stsp if (entry == NULL)
732 899d86c2 2018-05-10 stsp return NULL;
734 899d86c2 2018-05-10 stsp entry->id = id;
735 99db9666 2018-05-07 stsp entry->commit = commit;
736 899d86c2 2018-05-10 stsp return entry;
739 99db9666 2018-05-07 stsp static void
740 99db9666 2018-05-07 stsp pop_commit(struct commit_queue *commits)
742 99db9666 2018-05-07 stsp struct commit_queue_entry *entry;
744 ecb28ae0 2018-07-16 stsp entry = TAILQ_FIRST(&commits->head);
745 ecb28ae0 2018-07-16 stsp TAILQ_REMOVE(&commits->head, entry, entry);
746 99db9666 2018-05-07 stsp got_object_commit_close(entry->commit);
747 ecb28ae0 2018-07-16 stsp commits->ncommits--;
748 9ba79e04 2018-06-11 stsp /* Don't free entry->id! It is owned by the commit graph. */
749 99db9666 2018-05-07 stsp free(entry);
752 99db9666 2018-05-07 stsp static void
753 99db9666 2018-05-07 stsp free_commits(struct commit_queue *commits)
755 ecb28ae0 2018-07-16 stsp while (!TAILQ_EMPTY(&commits->head))
756 99db9666 2018-05-07 stsp pop_commit(commits);
759 c4972b91 2018-05-07 stsp static const struct got_error *
760 9ba79e04 2018-06-11 stsp queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
761 ecb28ae0 2018-07-16 stsp struct got_object_id *start_id, int minqueue, int init,
762 ecb28ae0 2018-07-16 stsp struct got_repository *repo, const char *path)
764 899d86c2 2018-05-10 stsp const struct got_error *err = NULL;
765 899d86c2 2018-05-10 stsp struct got_object_id *id;
766 9ba79e04 2018-06-11 stsp struct commit_queue_entry *entry;
767 31cedeaf 2018-09-15 stsp int nqueued = 0, found_obj = 0;
768 c8f60bff 2018-07-23 stsp int is_root_path = strcmp(path, "/") == 0;
770 31cedeaf 2018-09-15 stsp err = got_commit_graph_iter_start(graph, start_id, repo);
772 c4972b91 2018-05-07 stsp return err;
774 ecb28ae0 2018-07-16 stsp entry = TAILQ_LAST(&commits->head, commit_queue_head);
775 9ba79e04 2018-06-11 stsp if (entry && got_object_id_cmp(entry->id, start_id) == 0) {
776 9ba79e04 2018-06-11 stsp /* Start ID's commit is already on the queue; skip over it. */
777 9ba79e04 2018-06-11 stsp err = got_commit_graph_iter_next(&id, graph);
778 9ba79e04 2018-06-11 stsp if (err && err->code != GOT_ERR_ITER_NEED_MORE)
779 9ba79e04 2018-06-11 stsp return err;
781 31cedeaf 2018-09-15 stsp err = got_commit_graph_fetch_commits(graph, 1, repo);
783 9ba79e04 2018-06-11 stsp return err;
786 9ba79e04 2018-06-11 stsp while (1) {
787 9ba79e04 2018-06-11 stsp struct got_commit_object *commit;
789 9ba79e04 2018-06-11 stsp err = got_commit_graph_iter_next(&id, graph);
791 ecb28ae0 2018-07-16 stsp if (err->code != GOT_ERR_ITER_NEED_MORE)
793 ecb28ae0 2018-07-16 stsp if (nqueued >= minqueue) {
794 9ba79e04 2018-06-11 stsp err = NULL;
797 31cedeaf 2018-09-15 stsp err = got_commit_graph_fetch_commits(graph, 1, repo);
799 ecb28ae0 2018-07-16 stsp return err;
802 ecb28ae0 2018-07-16 stsp if (id == NULL)
805 9ba79e04 2018-06-11 stsp err = got_object_open_as_commit(&commit, repo, id);
809 c8f60bff 2018-07-23 stsp if (!is_root_path) {
810 27d434c2 2018-09-15 stsp struct got_object_id *obj_id = NULL;
811 ecb28ae0 2018-07-16 stsp struct got_object_qid *pid;
812 ecb28ae0 2018-07-16 stsp int changed = 0;
814 27d434c2 2018-09-15 stsp err = got_object_id_by_path(&obj_id, repo, id, path);
816 c8f60bff 2018-07-23 stsp got_object_commit_close(commit);
817 ecb28ae0 2018-07-16 stsp if (err->code == GOT_ERR_NO_OBJ &&
818 ecb28ae0 2018-07-16 stsp (found_obj || !init)) {
819 ecb28ae0 2018-07-16 stsp /* History stops here. */
820 ecb28ae0 2018-07-16 stsp err = got_error(GOT_ERR_ITER_COMPLETED);
824 ecb28ae0 2018-07-16 stsp found_obj = 1;
826 ecb28ae0 2018-07-16 stsp pid = SIMPLEQ_FIRST(&commit->parent_ids);
827 ecb28ae0 2018-07-16 stsp if (pid != NULL) {
828 27d434c2 2018-09-15 stsp struct got_object_id *pobj_id;
829 27d434c2 2018-09-15 stsp err = got_object_id_by_path(&pobj_id, repo,
830 ecb28ae0 2018-07-16 stsp pid->id, path);
832 ecb28ae0 2018-07-16 stsp if (err->code != GOT_ERR_NO_OBJ) {
833 c8f60bff 2018-07-23 stsp got_object_commit_close(commit);
834 27d434c2 2018-09-15 stsp free(obj_id);
837 ecb28ae0 2018-07-16 stsp err = NULL;
838 ecb28ae0 2018-07-16 stsp changed = 1;
840 27d434c2 2018-09-15 stsp changed = (got_object_id_cmp(obj_id,
841 27d434c2 2018-09-15 stsp pobj_id) != 0);
843 27d434c2 2018-09-15 stsp free(pobj_id);
845 27d434c2 2018-09-15 stsp free(obj_id);
846 ecb28ae0 2018-07-16 stsp if (!changed) {
847 ecb28ae0 2018-07-16 stsp got_object_commit_close(commit);
852 9ba79e04 2018-06-11 stsp entry = alloc_commit_queue_entry(commit, id);
853 9ba79e04 2018-06-11 stsp if (entry == NULL) {
854 9ba79e04 2018-06-11 stsp err = got_error_from_errno();
857 ecb28ae0 2018-07-16 stsp TAILQ_INSERT_TAIL(&commits->head, entry, entry);
859 ecb28ae0 2018-07-16 stsp commits->ncommits++;
862 9ba79e04 2018-06-11 stsp return err;
865 99db9666 2018-05-07 stsp static const struct got_error *
866 9ba79e04 2018-06-11 stsp fetch_next_commit(struct commit_queue_entry **pentry,
867 9ba79e04 2018-06-11 stsp struct commit_queue_entry *entry, struct commit_queue *commits,
868 ecb28ae0 2018-07-16 stsp struct got_commit_graph *graph, struct got_repository *repo,
869 ecb28ae0 2018-07-16 stsp const char *path)
871 899d86c2 2018-05-10 stsp const struct got_error *err = NULL;
873 9ba79e04 2018-06-11 stsp *pentry = NULL;
875 ecb28ae0 2018-07-16 stsp err = queue_commits(graph, commits, entry->id, 1, 0, repo, path);
877 9ba79e04 2018-06-11 stsp return err;
879 9ba79e04 2018-06-11 stsp /* Next entry to display should now be available. */
880 9ba79e04 2018-06-11 stsp *pentry = TAILQ_NEXT(entry, entry);
881 9ba79e04 2018-06-11 stsp if (*pentry == NULL)
882 9ba79e04 2018-06-11 stsp return got_error(GOT_ERR_NO_OBJ);
884 9ba79e04 2018-06-11 stsp return NULL;
887 899d86c2 2018-05-10 stsp static const struct got_error *
888 9ba79e04 2018-06-11 stsp get_head_commit_id(struct got_object_id **head_id, struct got_repository *repo)
890 9ba79e04 2018-06-11 stsp const struct got_error *err = NULL;
891 9ba79e04 2018-06-11 stsp struct got_reference *head_ref;
893 9ba79e04 2018-06-11 stsp *head_id = NULL;
895 9ba79e04 2018-06-11 stsp err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
897 99db9666 2018-05-07 stsp return err;
899 9ba79e04 2018-06-11 stsp err = got_ref_resolve(head_id, repo, head_ref);
900 9ba79e04 2018-06-11 stsp got_ref_close(head_ref);
902 9ba79e04 2018-06-11 stsp *head_id = NULL;
903 99db9666 2018-05-07 stsp return err;
906 9ba79e04 2018-06-11 stsp return NULL;
909 0553a4e3 2018-04-30 stsp static const struct got_error *
910 2814baeb 2018-08-01 stsp draw_commits(struct tog_view *view, struct commit_queue_entry **last,
911 ecb28ae0 2018-07-16 stsp struct commit_queue_entry **selected, struct commit_queue_entry *first,
912 a7f40148 2018-07-18 stsp struct commit_queue *commits, int selected_idx, int limit,
913 a7f40148 2018-07-18 stsp struct got_commit_graph *graph, struct got_repository *repo,
914 a7f40148 2018-07-18 stsp const char *path)
916 0553a4e3 2018-04-30 stsp const struct got_error *err = NULL;
917 0553a4e3 2018-04-30 stsp struct commit_queue_entry *entry;
918 ecb28ae0 2018-07-16 stsp int ncommits, width;
919 867c6645 2018-07-10 stsp char *id_str, *header;
920 ecb28ae0 2018-07-16 stsp wchar_t *wline;
922 e0d42f60 2018-07-22 stsp entry = first;
923 e0d42f60 2018-07-22 stsp ncommits = 0;
924 e0d42f60 2018-07-22 stsp while (entry) {
925 e0d42f60 2018-07-22 stsp if (ncommits == selected_idx) {
926 e0d42f60 2018-07-22 stsp *selected = entry;
929 e0d42f60 2018-07-22 stsp entry = TAILQ_NEXT(entry, entry);
930 e0d42f60 2018-07-22 stsp ncommits++;
933 867c6645 2018-07-10 stsp err = got_object_id_str(&id_str, (*selected)->id);
935 867c6645 2018-07-10 stsp return err;
937 c3ba6f36 2018-08-01 stsp if (path && strcmp(path, "/") != 0) {
938 ecb28ae0 2018-07-16 stsp if (asprintf(&header, "commit: %s [%s]", id_str, path) == -1) {
939 ecb28ae0 2018-07-16 stsp err = got_error_from_errno();
940 ecb28ae0 2018-07-16 stsp free(id_str);
941 ecb28ae0 2018-07-16 stsp return err;
943 ecb28ae0 2018-07-16 stsp } else if (asprintf(&header, "commit: %s", id_str) == -1) {
944 867c6645 2018-07-10 stsp err = got_error_from_errno();
945 867c6645 2018-07-10 stsp free(id_str);
946 867c6645 2018-07-10 stsp return err;
948 ecb28ae0 2018-07-16 stsp free(id_str);
949 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, header, view->ncols);
951 ecb28ae0 2018-07-16 stsp free(header);
952 ecb28ae0 2018-07-16 stsp return err;
954 ecb28ae0 2018-07-16 stsp free(header);
956 2814baeb 2018-08-01 stsp werase(view->window);
958 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
959 a3404814 2018-09-02 stsp wstandout(view->window);
960 2814baeb 2018-08-01 stsp waddwstr(view->window, wline);
961 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
962 a3404814 2018-09-02 stsp wstandend(view->window);
963 f7d12f7e 2018-08-01 stsp if (width < view->ncols)
964 2814baeb 2018-08-01 stsp waddch(view->window, '\n');
965 ecb28ae0 2018-07-16 stsp free(wline);
966 ecb28ae0 2018-07-16 stsp if (limit <= 1)
967 ecb28ae0 2018-07-16 stsp return NULL;
969 899d86c2 2018-05-10 stsp entry = first;
970 899d86c2 2018-05-10 stsp *last = first;
971 867c6645 2018-07-10 stsp ncommits = 0;
972 899d86c2 2018-05-10 stsp while (entry) {
973 ecb28ae0 2018-07-16 stsp if (ncommits >= limit - 1)
975 e0d42f60 2018-07-22 stsp if (ncommits == selected_idx)
976 2814baeb 2018-08-01 stsp wstandout(view->window);
977 2814baeb 2018-08-01 stsp err = draw_commit(view, entry->commit, entry->id);
978 cd0acaa7 2018-05-20 stsp if (ncommits == selected_idx)
979 2814baeb 2018-08-01 stsp wstandend(view->window);
982 0553a4e3 2018-04-30 stsp ncommits++;
983 899d86c2 2018-05-10 stsp *last = entry;
984 a7f40148 2018-07-18 stsp if (entry == TAILQ_LAST(&commits->head, commit_queue_head)) {
985 a7f40148 2018-07-18 stsp err = queue_commits(graph, commits, entry->id, 1,
986 a7f40148 2018-07-18 stsp 0, repo, path);
988 a7f40148 2018-07-18 stsp if (err->code != GOT_ERR_ITER_COMPLETED)
989 a7f40148 2018-07-18 stsp return err;
990 a7f40148 2018-07-18 stsp err = NULL;
993 899d86c2 2018-05-10 stsp entry = TAILQ_NEXT(entry, entry);
996 1a57306a 2018-09-02 stsp view_vborder(view);
998 80ddbec8 2018-04-29 stsp return err;
1001 07b55e75 2018-05-10 stsp static void
1002 16482c3b 2018-05-20 stsp scroll_up(struct commit_queue_entry **first_displayed_entry, int maxscroll,
1003 07b55e75 2018-05-10 stsp struct commit_queue *commits)
1005 07b55e75 2018-05-10 stsp struct commit_queue_entry *entry;
1006 07b55e75 2018-05-10 stsp int nscrolled = 0;
1008 ecb28ae0 2018-07-16 stsp entry = TAILQ_FIRST(&commits->head);
1009 07b55e75 2018-05-10 stsp if (*first_displayed_entry == entry)
1012 07b55e75 2018-05-10 stsp entry = *first_displayed_entry;
1013 16482c3b 2018-05-20 stsp while (entry && nscrolled < maxscroll) {
1014 ecb28ae0 2018-07-16 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
1015 07b55e75 2018-05-10 stsp if (entry) {
1016 07b55e75 2018-05-10 stsp *first_displayed_entry = entry;
1017 07b55e75 2018-05-10 stsp nscrolled++;
1022 aa075928 2018-05-10 stsp static const struct got_error *
1023 16482c3b 2018-05-20 stsp scroll_down(struct commit_queue_entry **first_displayed_entry, int maxscroll,
1024 aa075928 2018-05-10 stsp struct commit_queue_entry *last_displayed_entry,
1025 9ba79e04 2018-06-11 stsp struct commit_queue *commits, struct got_commit_graph *graph,
1026 ecb28ae0 2018-07-16 stsp struct got_repository *repo, const char *path)
1028 aa075928 2018-05-10 stsp const struct got_error *err = NULL;
1029 dd0a52c1 2018-05-20 stsp struct commit_queue_entry *pentry;
1030 aa075928 2018-05-10 stsp int nscrolled = 0;
1033 dd0a52c1 2018-05-20 stsp pentry = TAILQ_NEXT(last_displayed_entry, entry);
1034 aa075928 2018-05-10 stsp if (pentry == NULL) {
1035 9ba79e04 2018-06-11 stsp err = fetch_next_commit(&pentry, last_displayed_entry,
1036 ecb28ae0 2018-07-16 stsp commits, graph, repo, path);
1037 9a6bf2a5 2018-05-20 stsp if (err || pentry == NULL)
1040 dd0a52c1 2018-05-20 stsp last_displayed_entry = pentry;
1042 dd0a52c1 2018-05-20 stsp pentry = TAILQ_NEXT(*first_displayed_entry, entry);
1043 dd0a52c1 2018-05-20 stsp if (pentry == NULL)
1045 aa075928 2018-05-10 stsp *first_displayed_entry = pentry;
1046 16482c3b 2018-05-20 stsp } while (++nscrolled < maxscroll);
1048 dd0a52c1 2018-05-20 stsp return err;
1051 cd0acaa7 2018-05-20 stsp static const struct got_error *
1052 e5a0f69f 2018-08-18 stsp show_commit(struct tog_view **new_view, struct tog_view *parent_view,
1053 e5a0f69f 2018-08-18 stsp struct commit_queue_entry *entry, struct got_repository *repo)
1055 cd0acaa7 2018-05-20 stsp const struct got_error *err;
1056 cd0acaa7 2018-05-20 stsp struct got_object *obj1 = NULL, *obj2 = NULL;
1057 9ba79e04 2018-06-11 stsp struct got_object_qid *parent_id;
1058 e5a0f69f 2018-08-18 stsp struct tog_view *diff_view;
1060 cd0acaa7 2018-05-20 stsp err = got_object_open(&obj2, repo, entry->id);
1062 cd0acaa7 2018-05-20 stsp return err;
1064 9ba79e04 2018-06-11 stsp parent_id = SIMPLEQ_FIRST(&entry->commit->parent_ids);
1065 9ba79e04 2018-06-11 stsp if (parent_id) {
1066 9ba79e04 2018-06-11 stsp err = got_object_open(&obj1, repo, parent_id->id);
1068 cd0acaa7 2018-05-20 stsp goto done;
1071 e5a0f69f 2018-08-18 stsp diff_view = view_open(0, 0, 0, 0, parent_view, TOG_VIEW_DIFF);
1072 e5a0f69f 2018-08-18 stsp if (diff_view == NULL) {
1073 ea5e7bb5 2018-08-01 stsp err = got_error_from_errno();
1074 ea5e7bb5 2018-08-01 stsp goto done;
1077 e5a0f69f 2018-08-18 stsp err = open_diff_view(diff_view, obj1, obj2, repo);
1078 e5a0f69f 2018-08-18 stsp if (err == NULL)
1079 e5a0f69f 2018-08-18 stsp *new_view = diff_view;
1082 cd0acaa7 2018-05-20 stsp got_object_close(obj1);
1084 cd0acaa7 2018-05-20 stsp got_object_close(obj2);
1085 cd0acaa7 2018-05-20 stsp return err;
1088 80ddbec8 2018-04-29 stsp static const struct got_error *
1089 e5a0f69f 2018-08-18 stsp browse_commit(struct tog_view **new_view, struct tog_view *parent_view,
1090 e5a0f69f 2018-08-18 stsp struct commit_queue_entry *entry, struct got_repository *repo)
1092 9343a5fb 2018-06-23 stsp const struct got_error *err = NULL;
1093 9343a5fb 2018-06-23 stsp struct got_tree_object *tree;
1094 e5a0f69f 2018-08-18 stsp struct tog_view *tree_view;
1096 9343a5fb 2018-06-23 stsp err = got_object_open_as_tree(&tree, repo, entry->commit->tree_id);
1098 9343a5fb 2018-06-23 stsp return err;
1100 e5a0f69f 2018-08-18 stsp tree_view = view_open(0, 0, 0, 0, parent_view, TOG_VIEW_TREE);
1101 e5a0f69f 2018-08-18 stsp if (tree_view == NULL)
1102 e5a0f69f 2018-08-18 stsp return got_error_from_errno();
1104 e5a0f69f 2018-08-18 stsp err = open_tree_view(tree_view, tree, entry->id, repo);
1106 e5a0f69f 2018-08-18 stsp got_object_tree_close(tree);
1108 e5a0f69f 2018-08-18 stsp *new_view = tree_view;
1109 9343a5fb 2018-06-23 stsp return err;
1112 bcbd79e2 2018-08-19 stsp static const struct got_error *
1113 bcbd79e2 2018-08-19 stsp set_child_log_view(struct tog_view *view, struct tog_view *child)
1115 bcbd79e2 2018-08-19 stsp struct tog_log_view_state *s = &view->state.log;
1116 bcbd79e2 2018-08-19 stsp struct tog_diff_view_state *ds;
1117 bcbd79e2 2018-08-19 stsp struct commit_queue_entry *commit, *child_entry = NULL;
1118 bcbd79e2 2018-08-19 stsp int selected_idx = 0;
1120 bcbd79e2 2018-08-19 stsp if (child->type != TOG_VIEW_DIFF)
1121 bcbd79e2 2018-08-19 stsp return NULL;
1122 bcbd79e2 2018-08-19 stsp ds = &child->state.diff;
1124 bcbd79e2 2018-08-19 stsp TAILQ_FOREACH(commit, &s->commits.head, entry) {
1125 a3404814 2018-09-02 stsp if (got_object_id_cmp(commit->id, ds->id2) == 0) {
1126 bcbd79e2 2018-08-19 stsp child_entry = commit;
1130 bcbd79e2 2018-08-19 stsp if (child_entry == NULL)
1131 bcbd79e2 2018-08-19 stsp return NULL;
1133 bcbd79e2 2018-08-19 stsp commit = s->first_displayed_entry;
1134 bcbd79e2 2018-08-19 stsp while (commit) {
1135 bcbd79e2 2018-08-19 stsp if (got_object_id_cmp(commit->id, child_entry->id) == 0) {
1136 bcbd79e2 2018-08-19 stsp s->selected_entry = child_entry;
1137 bcbd79e2 2018-08-19 stsp s->selected = selected_idx;
1140 bcbd79e2 2018-08-19 stsp if (commit == s->last_displayed_entry)
1142 bcbd79e2 2018-08-19 stsp selected_idx++;
1143 bcbd79e2 2018-08-19 stsp commit = TAILQ_NEXT(commit, entry);
1146 bcbd79e2 2018-08-19 stsp return show_log_view(view);
1149 9343a5fb 2018-06-23 stsp static const struct got_error *
1150 ba4f502b 2018-08-04 stsp open_log_view(struct tog_view *view, struct got_object_id *start_id,
1151 04cc582a 2018-08-01 stsp struct got_repository *repo, const char *path)
1153 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
1154 9ba79e04 2018-06-11 stsp struct got_object_id *head_id = NULL;
1155 ba4f502b 2018-08-04 stsp int nfetched;
1156 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
1158 fb2756b9 2018-08-04 stsp err = got_repo_map_path(&s->in_repo_path, repo, path);
1159 ecb28ae0 2018-07-16 stsp if (err != NULL)
1160 ecb28ae0 2018-07-16 stsp goto done;
1162 9ba79e04 2018-06-11 stsp err = get_head_commit_id(&head_id, repo);
1164 9ba79e04 2018-06-11 stsp return err;
1166 034e3b69 2018-07-22 stsp /* The graph contains all commits. */
1167 31cedeaf 2018-09-15 stsp err = got_commit_graph_open(&s->graph, head_id, "/", 0, repo);
1169 9ba79e04 2018-06-11 stsp goto done;
1170 034e3b69 2018-07-22 stsp /* The commit queue contains a subset of commits filtered by path. */
1171 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->commits.head);
1172 fb2756b9 2018-08-04 stsp s->commits.ncommits = 0;
1174 9ba79e04 2018-06-11 stsp /* Populate commit graph with a sufficient number of commits. */
1175 fb2756b9 2018-08-04 stsp err = got_commit_graph_fetch_commits_up_to(&nfetched, s->graph,
1176 fb2756b9 2018-08-04 stsp start_id, repo);
1178 9ba79e04 2018-06-11 stsp goto done;
1181 9ba79e04 2018-06-11 stsp * Open the initial batch of commits, sorted in commit graph order.
1182 9ba79e04 2018-06-11 stsp * We keep all commits open throughout the lifetime of the log view
1183 9ba79e04 2018-06-11 stsp * in order to avoid having to re-fetch commits from disk while
1184 9ba79e04 2018-06-11 stsp * updating the display.
1186 fb2756b9 2018-08-04 stsp err = queue_commits(s->graph, &s->commits, start_id, view->nlines, 1,
1187 fb2756b9 2018-08-04 stsp repo, s->in_repo_path);
1188 55198a88 2018-07-16 stsp if (err) {
1189 55198a88 2018-07-16 stsp if (err->code != GOT_ERR_ITER_COMPLETED)
1190 55198a88 2018-07-16 stsp goto done;
1191 55198a88 2018-07-16 stsp err = NULL;
1194 1bfa490b 2018-08-18 stsp s->first_displayed_entry =
1195 1bfa490b 2018-08-18 stsp TAILQ_FIRST(&s->commits.head);
1196 1bfa490b 2018-08-18 stsp s->selected_entry = s->first_displayed_entry;
1197 fb2756b9 2018-08-04 stsp s->repo = repo;
1199 e5a0f69f 2018-08-18 stsp view->show = show_log_view;
1200 e5a0f69f 2018-08-18 stsp view->input = input_log_view;
1201 e5a0f69f 2018-08-18 stsp view->close = close_log_view;
1202 bcbd79e2 2018-08-19 stsp view->set_child = set_child_log_view;
1204 ba4f502b 2018-08-04 stsp free(head_id);
1205 ba4f502b 2018-08-04 stsp return err;
1208 e5a0f69f 2018-08-18 stsp static const struct got_error *
1209 e5a0f69f 2018-08-18 stsp close_log_view(struct tog_view *view)
1211 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
1213 fb2756b9 2018-08-04 stsp if (s->graph)
1214 fb2756b9 2018-08-04 stsp got_commit_graph_close(s->graph);
1215 fb2756b9 2018-08-04 stsp free_commits(&s->commits);
1216 fb2756b9 2018-08-04 stsp free(s->in_repo_path);
1217 e5a0f69f 2018-08-18 stsp return NULL;
1220 ba4f502b 2018-08-04 stsp static const struct got_error *
1221 bcbd79e2 2018-08-19 stsp update_diff_child_view(struct tog_view *parent,
1222 bcbd79e2 2018-08-19 stsp struct commit_queue_entry *selected_entry, struct got_repository *repo)
1224 bcbd79e2 2018-08-19 stsp const struct got_error *err = NULL;
1225 bcbd79e2 2018-08-19 stsp struct tog_diff_view_state *ds;
1226 bcbd79e2 2018-08-19 stsp struct got_object *obj1 = NULL, *obj2 = NULL;
1227 bcbd79e2 2018-08-19 stsp struct got_object_qid *parent_id;
1228 bcbd79e2 2018-08-19 stsp struct tog_view *child_view = parent->child;
1230 bcbd79e2 2018-08-19 stsp if (child_view == NULL)
1231 bcbd79e2 2018-08-19 stsp return NULL;
1232 bcbd79e2 2018-08-19 stsp if (child_view->type != TOG_VIEW_DIFF)
1233 bcbd79e2 2018-08-19 stsp return NULL;
1234 bcbd79e2 2018-08-19 stsp ds = &child_view->state.diff;
1235 a3404814 2018-09-02 stsp if (got_object_id_cmp(ds->id2, selected_entry->id) == 0)
1236 bcbd79e2 2018-08-19 stsp return NULL;
1238 bcbd79e2 2018-08-19 stsp err = got_object_open(&obj2, repo, selected_entry->id);
1240 bcbd79e2 2018-08-19 stsp return err;
1242 bcbd79e2 2018-08-19 stsp parent_id = SIMPLEQ_FIRST(&selected_entry->commit->parent_ids);
1243 bcbd79e2 2018-08-19 stsp if (parent_id) {
1244 bcbd79e2 2018-08-19 stsp err = got_object_open(&obj1, repo, parent_id->id);
1246 bcbd79e2 2018-08-19 stsp goto done;
1249 bcbd79e2 2018-08-19 stsp err = close_diff_view(child_view);
1251 bcbd79e2 2018-08-19 stsp goto done;
1253 bcbd79e2 2018-08-19 stsp err = open_diff_view(child_view, obj1, obj2, repo);
1255 bcbd79e2 2018-08-19 stsp goto done;
1258 bcbd79e2 2018-08-19 stsp got_object_close(obj1);
1260 bcbd79e2 2018-08-19 stsp got_object_close(obj2);
1261 bcbd79e2 2018-08-19 stsp return err;
1264 bcbd79e2 2018-08-19 stsp static const struct got_error *
1265 ba4f502b 2018-08-04 stsp show_log_view(struct tog_view *view)
1267 bcbd79e2 2018-08-19 stsp const struct got_error *err = NULL;
1268 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
1270 bcbd79e2 2018-08-19 stsp err = draw_commits(view, &s->last_displayed_entry,
1271 e5a0f69f 2018-08-18 stsp &s->selected_entry, s->first_displayed_entry,
1272 e5a0f69f 2018-08-18 stsp &s->commits, s->selected, view->nlines, s->graph,
1273 e5a0f69f 2018-08-18 stsp s->repo, s->in_repo_path);
1275 bcbd79e2 2018-08-19 stsp return err;
1277 bcbd79e2 2018-08-19 stsp return update_diff_child_view(view, s->selected_entry, s->repo);
1280 e5a0f69f 2018-08-18 stsp static const struct got_error *
1281 e5a0f69f 2018-08-18 stsp input_log_view(struct tog_view **new_view, struct tog_view **dead_view,
1282 e5a0f69f 2018-08-18 stsp struct tog_view *view, int ch)
1284 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
1285 e5a0f69f 2018-08-18 stsp struct tog_log_view_state *s = &view->state.log;
1287 e5a0f69f 2018-08-18 stsp switch (ch) {
1289 e5a0f69f 2018-08-18 stsp case KEY_UP:
1291 e5a0f69f 2018-08-18 stsp if (s->selected > 0)
1292 e5a0f69f 2018-08-18 stsp s->selected--;
1293 e5a0f69f 2018-08-18 stsp if (s->selected > 0)
1295 e5a0f69f 2018-08-18 stsp scroll_up(&s->first_displayed_entry, 1,
1296 e5a0f69f 2018-08-18 stsp &s->commits);
1298 e5a0f69f 2018-08-18 stsp case KEY_PPAGE:
1299 e5a0f69f 2018-08-18 stsp if (TAILQ_FIRST(&s->commits.head) ==
1300 e5a0f69f 2018-08-18 stsp s->first_displayed_entry) {
1301 e5a0f69f 2018-08-18 stsp s->selected = 0;
1304 e5a0f69f 2018-08-18 stsp scroll_up(&s->first_displayed_entry,
1305 e5a0f69f 2018-08-18 stsp view->nlines, &s->commits);
1308 e5a0f69f 2018-08-18 stsp case KEY_DOWN:
1310 e5a0f69f 2018-08-18 stsp if (s->selected < MIN(view->nlines - 2,
1311 e5a0f69f 2018-08-18 stsp s->commits.ncommits - 1)) {
1312 e5a0f69f 2018-08-18 stsp s->selected++;
1315 e5a0f69f 2018-08-18 stsp err = scroll_down(&s->first_displayed_entry, 1,
1316 e5a0f69f 2018-08-18 stsp s->last_displayed_entry, &s->commits,
1317 e5a0f69f 2018-08-18 stsp s->graph, s->repo, s->in_repo_path);
1318 e5a0f69f 2018-08-18 stsp if (err) {
1319 4d7951a5 2018-08-04 stsp if (err->code != GOT_ERR_ITER_COMPLETED)
1321 4d7951a5 2018-08-04 stsp err = NULL;
1324 e5a0f69f 2018-08-18 stsp case KEY_NPAGE: {
1325 e5a0f69f 2018-08-18 stsp struct commit_queue_entry *first;
1326 e5a0f69f 2018-08-18 stsp first = s->first_displayed_entry;
1327 e5a0f69f 2018-08-18 stsp err = scroll_down(&s->first_displayed_entry,
1328 e5a0f69f 2018-08-18 stsp view->nlines, s->last_displayed_entry,
1329 e5a0f69f 2018-08-18 stsp &s->commits, s->graph, s->repo,
1330 e5a0f69f 2018-08-18 stsp s->in_repo_path);
1331 e5a0f69f 2018-08-18 stsp if (err == NULL)
1333 e5a0f69f 2018-08-18 stsp if (err->code != GOT_ERR_ITER_COMPLETED)
1335 e5a0f69f 2018-08-18 stsp if (first == s->first_displayed_entry &&
1336 e5a0f69f 2018-08-18 stsp s->selected < MIN(view->nlines - 2,
1337 e5a0f69f 2018-08-18 stsp s->commits.ncommits - 1)) {
1338 e5a0f69f 2018-08-18 stsp /* can't scroll further down */
1339 e5a0f69f 2018-08-18 stsp s->selected = MIN(view->nlines - 2,
1340 e5a0f69f 2018-08-18 stsp s->commits.ncommits - 1);
1342 e5a0f69f 2018-08-18 stsp err = NULL;
1345 e5a0f69f 2018-08-18 stsp case KEY_RESIZE:
1346 e5a0f69f 2018-08-18 stsp if (s->selected > view->nlines - 2)
1347 e5a0f69f 2018-08-18 stsp s->selected = view->nlines - 2;
1348 e5a0f69f 2018-08-18 stsp if (s->selected > s->commits.ncommits - 1)
1349 e5a0f69f 2018-08-18 stsp s->selected = s->commits.ncommits - 1;
1351 e5a0f69f 2018-08-18 stsp case KEY_ENTER:
1352 e5a0f69f 2018-08-18 stsp case '\r':
1353 e5a0f69f 2018-08-18 stsp err = show_commit(new_view, view, s->selected_entry,
1357 e5a0f69f 2018-08-18 stsp err = browse_commit(new_view, view, s->selected_entry,
1364 80ddbec8 2018-04-29 stsp return err;
1367 4ed7e80c 2018-05-20 stsp static const struct got_error *
1368 9f7d7167 2018-04-29 stsp cmd_log(int argc, char *argv[])
1370 80ddbec8 2018-04-29 stsp const struct got_error *error;
1371 ecb28ae0 2018-07-16 stsp struct got_repository *repo = NULL;
1372 899d86c2 2018-05-10 stsp struct got_object_id *start_id = NULL;
1373 ecb28ae0 2018-07-16 stsp char *path = NULL, *repo_path = NULL, *cwd = NULL;
1374 80ddbec8 2018-04-29 stsp char *start_commit = NULL;
1376 04cc582a 2018-08-01 stsp struct tog_view *view;
1378 80ddbec8 2018-04-29 stsp #ifndef PROFILE
1379 ad242220 2018-09-08 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd", NULL)
1381 80ddbec8 2018-04-29 stsp err(1, "pledge");
1384 ecb28ae0 2018-07-16 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1385 80ddbec8 2018-04-29 stsp switch (ch) {
1387 80ddbec8 2018-04-29 stsp start_commit = optarg;
1390 ecb28ae0 2018-07-16 stsp repo_path = realpath(optarg, NULL);
1391 ecb28ae0 2018-07-16 stsp if (repo_path == NULL)
1392 ecb28ae0 2018-07-16 stsp err(1, "-r option");
1396 80ddbec8 2018-04-29 stsp /* NOTREACHED */
1400 80ddbec8 2018-04-29 stsp argc -= optind;
1401 80ddbec8 2018-04-29 stsp argv += optind;
1403 ecb28ae0 2018-07-16 stsp if (argc == 0)
1404 ecb28ae0 2018-07-16 stsp path = strdup("");
1405 ecb28ae0 2018-07-16 stsp else if (argc == 1)
1406 ecb28ae0 2018-07-16 stsp path = strdup(argv[0]);
1408 80ddbec8 2018-04-29 stsp usage_log();
1409 ecb28ae0 2018-07-16 stsp if (path == NULL)
1410 ecb28ae0 2018-07-16 stsp return got_error_from_errno();
1412 ecb28ae0 2018-07-16 stsp cwd = getcwd(NULL, 0);
1413 ecb28ae0 2018-07-16 stsp if (cwd == NULL) {
1414 ecb28ae0 2018-07-16 stsp error = got_error_from_errno();
1415 ecb28ae0 2018-07-16 stsp goto done;
1417 ecb28ae0 2018-07-16 stsp if (repo_path == NULL) {
1418 ecb28ae0 2018-07-16 stsp repo_path = strdup(cwd);
1419 ecb28ae0 2018-07-16 stsp if (repo_path == NULL) {
1420 ecb28ae0 2018-07-16 stsp error = got_error_from_errno();
1421 ecb28ae0 2018-07-16 stsp goto done;
1425 80ddbec8 2018-04-29 stsp error = got_repo_open(&repo, repo_path);
1426 80ddbec8 2018-04-29 stsp if (error != NULL)
1427 ecb28ae0 2018-07-16 stsp goto done;
1429 80ddbec8 2018-04-29 stsp if (start_commit == NULL) {
1430 899d86c2 2018-05-10 stsp error = get_head_commit_id(&start_id, repo);
1431 80ddbec8 2018-04-29 stsp if (error != NULL)
1432 ecb28ae0 2018-07-16 stsp goto done;
1434 80ddbec8 2018-04-29 stsp struct got_object *obj;
1435 80ddbec8 2018-04-29 stsp error = got_object_open_by_id_str(&obj, repo, start_commit);
1436 80ddbec8 2018-04-29 stsp if (error == NULL) {
1437 6402fb3c 2018-09-15 stsp start_id = got_object_id_dup(got_object_get_id(obj));
1438 899d86c2 2018-05-10 stsp if (start_id == NULL)
1439 80ddbec8 2018-04-29 stsp error = got_error_from_errno();
1440 ecb28ae0 2018-07-16 stsp goto done;
1443 80ddbec8 2018-04-29 stsp if (error != NULL)
1444 ecb28ae0 2018-07-16 stsp goto done;
1446 b3665f43 2018-08-04 stsp view = view_open(0, 0, 0, 0, NULL, TOG_VIEW_LOG);
1447 04cc582a 2018-08-01 stsp if (view == NULL) {
1448 04cc582a 2018-08-01 stsp error = got_error_from_errno();
1449 04cc582a 2018-08-01 stsp goto done;
1451 ba4f502b 2018-08-04 stsp error = open_log_view(view, start_id, repo, path);
1452 ba4f502b 2018-08-04 stsp if (error)
1453 ba4f502b 2018-08-04 stsp goto done;
1454 e5a0f69f 2018-08-18 stsp error = view_loop(view);
1456 ecb28ae0 2018-07-16 stsp free(repo_path);
1457 ecb28ae0 2018-07-16 stsp free(cwd);
1458 ecb28ae0 2018-07-16 stsp free(path);
1459 899d86c2 2018-05-10 stsp free(start_id);
1461 ecb28ae0 2018-07-16 stsp got_repo_close(repo);
1462 80ddbec8 2018-04-29 stsp return error;
1465 4ed7e80c 2018-05-20 stsp __dead static void
1466 9f7d7167 2018-04-29 stsp usage_diff(void)
1469 9f7d7167 2018-04-29 stsp fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
1470 9f7d7167 2018-04-29 stsp getprogname());
1474 b304db33 2018-05-20 stsp static char *
1475 b304db33 2018-05-20 stsp parse_next_line(FILE *f, size_t *len)
1477 b304db33 2018-05-20 stsp char *line;
1478 b304db33 2018-05-20 stsp size_t linelen;
1479 b304db33 2018-05-20 stsp size_t lineno;
1480 b304db33 2018-05-20 stsp const char delim[3] = { '\0', '\0', '\0'};
1482 b304db33 2018-05-20 stsp line = fparseln(f, &linelen, &lineno, delim, 0);
1484 b304db33 2018-05-20 stsp *len = linelen;
1485 b304db33 2018-05-20 stsp return line;
1488 4ed7e80c 2018-05-20 stsp static const struct got_error *
1489 f7d12f7e 2018-08-01 stsp draw_file(struct tog_view *view, FILE *f, int *first_displayed_line,
1490 a3404814 2018-09-02 stsp int *last_displayed_line, int *eof, int max_lines,
1491 a3404814 2018-09-02 stsp char * header)
1493 61e69b96 2018-05-20 stsp const struct got_error *err;
1494 26ed57b2 2018-05-19 stsp int nlines = 0, nprinted = 0;
1495 b304db33 2018-05-20 stsp char *line;
1496 b304db33 2018-05-20 stsp size_t len;
1497 61e69b96 2018-05-20 stsp wchar_t *wline;
1498 e0b650dd 2018-05-20 stsp int width;
1500 26ed57b2 2018-05-19 stsp rewind(f);
1501 f7d12f7e 2018-08-01 stsp werase(view->window);
1503 a3404814 2018-09-02 stsp if (header) {
1504 a3404814 2018-09-02 stsp err = format_line(&wline, &width, header, view->ncols);
1505 a3404814 2018-09-02 stsp if (err) {
1506 a3404814 2018-09-02 stsp return err;
1509 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
1510 a3404814 2018-09-02 stsp wstandout(view->window);
1511 a3404814 2018-09-02 stsp waddwstr(view->window, wline);
1512 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
1513 a3404814 2018-09-02 stsp wstandend(view->window);
1514 a3404814 2018-09-02 stsp if (width < view->ncols)
1515 a3404814 2018-09-02 stsp waddch(view->window, '\n');
1517 a3404814 2018-09-02 stsp if (max_lines <= 1)
1518 a3404814 2018-09-02 stsp return NULL;
1519 a3404814 2018-09-02 stsp max_lines--;
1523 26ed57b2 2018-05-19 stsp while (nprinted < max_lines) {
1524 b304db33 2018-05-20 stsp line = parse_next_line(f, &len);
1525 26ed57b2 2018-05-19 stsp if (line == NULL) {
1529 26ed57b2 2018-05-19 stsp if (++nlines < *first_displayed_line) {
1530 26ed57b2 2018-05-19 stsp free(line);
1534 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, line, view->ncols);
1535 61e69b96 2018-05-20 stsp if (err) {
1536 61e69b96 2018-05-20 stsp free(line);
1537 61e69b96 2018-05-20 stsp return err;
1539 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
1540 f7d12f7e 2018-08-01 stsp if (width < view->ncols)
1541 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
1542 26ed57b2 2018-05-19 stsp if (++nprinted == 1)
1543 26ed57b2 2018-05-19 stsp *first_displayed_line = nlines;
1544 26ed57b2 2018-05-19 stsp free(line);
1545 2550e4c3 2018-07-13 stsp free(wline);
1546 2550e4c3 2018-07-13 stsp wline = NULL;
1548 26ed57b2 2018-05-19 stsp *last_displayed_line = nlines;
1550 1a57306a 2018-09-02 stsp view_vborder(view);
1552 26ed57b2 2018-05-19 stsp return NULL;
1555 4ed7e80c 2018-05-20 stsp static const struct got_error *
1556 5dc9f4bc 2018-08-04 stsp open_diff_view(struct tog_view *view, struct got_object *obj1,
1557 ea5e7bb5 2018-08-01 stsp struct got_object *obj2, struct got_repository *repo)
1559 26ed57b2 2018-05-19 stsp const struct got_error *err;
1562 cd0acaa7 2018-05-20 stsp if (obj1 != NULL && obj2 != NULL &&
1563 cd0acaa7 2018-05-20 stsp got_object_get_type(obj1) != got_object_get_type(obj2))
1564 26ed57b2 2018-05-19 stsp return got_error(GOT_ERR_OBJ_TYPE);
1566 511a516b 2018-05-19 stsp f = got_opentemp();
1567 26ed57b2 2018-05-19 stsp if (f == NULL)
1568 26ed57b2 2018-05-19 stsp return got_error_from_errno();
1570 cd0acaa7 2018-05-20 stsp switch (got_object_get_type(obj1 ? obj1 : obj2)) {
1571 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_BLOB:
1572 f6861a81 2018-09-13 stsp err = got_diff_objects_as_blobs(obj1, obj2, NULL, NULL,
1575 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_TREE:
1576 f6861a81 2018-09-13 stsp err = got_diff_objects_as_trees(obj1, obj2, "", "", repo, f);
1578 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_COMMIT:
1579 11528a82 2018-05-19 stsp err = got_diff_objects_as_commits(obj1, obj2, repo, f);
1582 26ed57b2 2018-05-19 stsp return got_error(GOT_ERR_OBJ_TYPE);
1585 26ed57b2 2018-05-19 stsp fflush(f);
1587 a3404814 2018-09-02 stsp view->state.diff.id1 = obj1 ? got_object_get_id(obj1) : NULL;
1588 a3404814 2018-09-02 stsp view->state.diff.id2 = got_object_get_id(obj2);
1589 5dc9f4bc 2018-08-04 stsp view->state.diff.f = f;
1590 5dc9f4bc 2018-08-04 stsp view->state.diff.first_displayed_line = 1;
1591 5dc9f4bc 2018-08-04 stsp view->state.diff.last_displayed_line = view->nlines;
1593 e5a0f69f 2018-08-18 stsp view->show = show_diff_view;
1594 e5a0f69f 2018-08-18 stsp view->input = input_diff_view;
1595 e5a0f69f 2018-08-18 stsp view->close = close_diff_view;
1597 5dc9f4bc 2018-08-04 stsp return NULL;
1600 e5a0f69f 2018-08-18 stsp static const struct got_error *
1601 5dc9f4bc 2018-08-04 stsp close_diff_view(struct tog_view *view)
1603 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
1605 e5a0f69f 2018-08-18 stsp if (view->state.diff.f && fclose(view->state.diff.f) == EOF)
1606 e5a0f69f 2018-08-18 stsp err = got_error_from_errno();
1607 e5a0f69f 2018-08-18 stsp return err;
1610 5dc9f4bc 2018-08-04 stsp static const struct got_error *
1611 5dc9f4bc 2018-08-04 stsp show_diff_view(struct tog_view *view)
1613 a3404814 2018-09-02 stsp const struct got_error *err;
1614 fb2756b9 2018-08-04 stsp struct tog_diff_view_state *s = &view->state.diff;
1615 a3404814 2018-09-02 stsp char *id_str1 = NULL, *id_str2, *header;
1617 a3404814 2018-09-02 stsp if (s->id1) {
1618 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str1, s->id1);
1620 a3404814 2018-09-02 stsp return err;
1622 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str2, s->id2);
1624 a3404814 2018-09-02 stsp return err;
1626 a3404814 2018-09-02 stsp if (asprintf(&header, "diff: %s %s",
1627 a3404814 2018-09-02 stsp id_str1 ? id_str1 : "/dev/null", id_str2) == -1) {
1628 a3404814 2018-09-02 stsp err = got_error_from_errno();
1629 a3404814 2018-09-02 stsp free(id_str1);
1630 a3404814 2018-09-02 stsp free(id_str2);
1631 a3404814 2018-09-02 stsp return err;
1633 a3404814 2018-09-02 stsp free(id_str1);
1634 a3404814 2018-09-02 stsp free(id_str2);
1636 e5a0f69f 2018-08-18 stsp return draw_file(view, s->f, &s->first_displayed_line,
1637 a3404814 2018-09-02 stsp &s->last_displayed_line, &s->eof, view->nlines,
1641 e5a0f69f 2018-08-18 stsp static const struct got_error *
1642 bcbd79e2 2018-08-19 stsp input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
1643 e5a0f69f 2018-08-18 stsp struct tog_view *view, int ch)
1645 bcbd79e2 2018-08-19 stsp const struct got_error *err = NULL;
1646 e5a0f69f 2018-08-18 stsp struct tog_diff_view_state *s = &view->state.diff;
1649 e5a0f69f 2018-08-18 stsp switch (ch) {
1651 e5a0f69f 2018-08-18 stsp case KEY_UP:
1652 e5a0f69f 2018-08-18 stsp if (s->first_displayed_line > 1)
1653 e5a0f69f 2018-08-18 stsp s->first_displayed_line--;
1655 e5a0f69f 2018-08-18 stsp case KEY_PPAGE:
1657 e5a0f69f 2018-08-18 stsp while (i++ < view->nlines - 1 &&
1658 e5a0f69f 2018-08-18 stsp s->first_displayed_line > 1)
1659 e5a0f69f 2018-08-18 stsp s->first_displayed_line--;
1662 e5a0f69f 2018-08-18 stsp case KEY_DOWN:
1663 e5a0f69f 2018-08-18 stsp if (!s->eof)
1664 e5a0f69f 2018-08-18 stsp s->first_displayed_line++;
1666 e5a0f69f 2018-08-18 stsp case KEY_NPAGE:
1669 e5a0f69f 2018-08-18 stsp while (!s->eof && i++ < view->nlines - 1) {
1670 e5a0f69f 2018-08-18 stsp char *line;
1671 e5a0f69f 2018-08-18 stsp line = parse_next_line(s->f, NULL);
1672 e5a0f69f 2018-08-18 stsp s->first_displayed_line++;
1673 e5a0f69f 2018-08-18 stsp if (line == NULL)
1678 bcbd79e2 2018-08-19 stsp case ']': {
1679 bcbd79e2 2018-08-19 stsp struct tog_log_view_state *ls;
1680 bcbd79e2 2018-08-19 stsp struct commit_queue_entry *entry;
1681 bcbd79e2 2018-08-19 stsp struct tog_view *diff_view;
1683 bcbd79e2 2018-08-19 stsp if (view->parent == NULL)
1685 bcbd79e2 2018-08-19 stsp if (view->parent->type != TOG_VIEW_LOG)
1687 bcbd79e2 2018-08-19 stsp ls = &view->parent->state.log;
1689 bcbd79e2 2018-08-19 stsp if (ch == '[') {
1690 bcbd79e2 2018-08-19 stsp entry = TAILQ_PREV(ls->selected_entry,
1691 bcbd79e2 2018-08-19 stsp commit_queue_head, entry);
1693 bcbd79e2 2018-08-19 stsp entry = TAILQ_NEXT(ls->selected_entry, entry);
1694 bcbd79e2 2018-08-19 stsp if (entry == NULL) {
1695 bcbd79e2 2018-08-19 stsp err = fetch_next_commit(&entry,
1696 bcbd79e2 2018-08-19 stsp ls->selected_entry,
1697 bcbd79e2 2018-08-19 stsp &ls->commits, ls->graph,
1698 bcbd79e2 2018-08-19 stsp ls->repo, ls->in_repo_path);
1703 bcbd79e2 2018-08-19 stsp if (entry == NULL)
1705 bcbd79e2 2018-08-19 stsp err = show_commit(&diff_view, view->parent,
1706 bcbd79e2 2018-08-19 stsp entry, ls->repo);
1709 bcbd79e2 2018-08-19 stsp *new_view = diff_view;
1710 bcbd79e2 2018-08-19 stsp *dead_view = view;
1717 bcbd79e2 2018-08-19 stsp return err;
1720 4ed7e80c 2018-05-20 stsp static const struct got_error *
1721 9f7d7167 2018-04-29 stsp cmd_diff(int argc, char *argv[])
1723 26ed57b2 2018-05-19 stsp const struct got_error *error = NULL;
1724 26ed57b2 2018-05-19 stsp struct got_repository *repo = NULL;
1725 26ed57b2 2018-05-19 stsp struct got_object *obj1 = NULL, *obj2 = NULL;
1726 26ed57b2 2018-05-19 stsp char *repo_path = NULL;
1727 26ed57b2 2018-05-19 stsp char *obj_id_str1 = NULL, *obj_id_str2 = NULL;
1729 ea5e7bb5 2018-08-01 stsp struct tog_view *view;
1731 26ed57b2 2018-05-19 stsp #ifndef PROFILE
1732 ad242220 2018-09-08 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd", NULL)
1734 26ed57b2 2018-05-19 stsp err(1, "pledge");
1737 26ed57b2 2018-05-19 stsp while ((ch = getopt(argc, argv, "")) != -1) {
1738 26ed57b2 2018-05-19 stsp switch (ch) {
1741 26ed57b2 2018-05-19 stsp /* NOTREACHED */
1745 26ed57b2 2018-05-19 stsp argc -= optind;
1746 26ed57b2 2018-05-19 stsp argv += optind;
1748 26ed57b2 2018-05-19 stsp if (argc == 0) {
1749 26ed57b2 2018-05-19 stsp usage_diff(); /* TODO show local worktree changes */
1750 26ed57b2 2018-05-19 stsp } else if (argc == 2) {
1751 26ed57b2 2018-05-19 stsp repo_path = getcwd(NULL, 0);
1752 26ed57b2 2018-05-19 stsp if (repo_path == NULL)
1753 26ed57b2 2018-05-19 stsp return got_error_from_errno();
1754 26ed57b2 2018-05-19 stsp obj_id_str1 = argv[0];
1755 26ed57b2 2018-05-19 stsp obj_id_str2 = argv[1];
1756 26ed57b2 2018-05-19 stsp } else if (argc == 3) {
1757 26ed57b2 2018-05-19 stsp repo_path = realpath(argv[0], NULL);
1758 26ed57b2 2018-05-19 stsp if (repo_path == NULL)
1759 26ed57b2 2018-05-19 stsp return got_error_from_errno();
1760 26ed57b2 2018-05-19 stsp obj_id_str1 = argv[1];
1761 26ed57b2 2018-05-19 stsp obj_id_str2 = argv[2];
1763 26ed57b2 2018-05-19 stsp usage_diff();
1765 26ed57b2 2018-05-19 stsp error = got_repo_open(&repo, repo_path);
1766 26ed57b2 2018-05-19 stsp free(repo_path);
1767 26ed57b2 2018-05-19 stsp if (error)
1768 26ed57b2 2018-05-19 stsp goto done;
1770 26ed57b2 2018-05-19 stsp error = got_object_open_by_id_str(&obj1, repo, obj_id_str1);
1771 26ed57b2 2018-05-19 stsp if (error)
1772 26ed57b2 2018-05-19 stsp goto done;
1774 26ed57b2 2018-05-19 stsp error = got_object_open_by_id_str(&obj2, repo, obj_id_str2);
1775 26ed57b2 2018-05-19 stsp if (error)
1776 26ed57b2 2018-05-19 stsp goto done;
1778 b3665f43 2018-08-04 stsp view = view_open(0, 0, 0, 0, NULL, TOG_VIEW_DIFF);
1779 ea5e7bb5 2018-08-01 stsp if (view == NULL) {
1780 ea5e7bb5 2018-08-01 stsp error = got_error_from_errno();
1781 ea5e7bb5 2018-08-01 stsp goto done;
1783 5dc9f4bc 2018-08-04 stsp error = open_diff_view(view, obj1, obj2, repo);
1784 5dc9f4bc 2018-08-04 stsp if (error)
1785 5dc9f4bc 2018-08-04 stsp goto done;
1786 e5a0f69f 2018-08-18 stsp error = view_loop(view);
1788 26ed57b2 2018-05-19 stsp got_repo_close(repo);
1790 26ed57b2 2018-05-19 stsp got_object_close(obj1);
1792 26ed57b2 2018-05-19 stsp got_object_close(obj2);
1793 26ed57b2 2018-05-19 stsp return error;
1796 4ed7e80c 2018-05-20 stsp __dead static void
1797 9f7d7167 2018-04-29 stsp usage_blame(void)
1800 69069811 2018-08-02 stsp fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
1801 9f7d7167 2018-04-29 stsp getprogname());
1805 84451b3e 2018-07-10 stsp struct tog_blame_line {
1806 84451b3e 2018-07-10 stsp int annotated;
1807 84451b3e 2018-07-10 stsp struct got_object_id *id;
1810 4ed7e80c 2018-05-20 stsp static const struct got_error *
1811 f7d12f7e 2018-08-01 stsp draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
1812 f7d12f7e 2018-08-01 stsp const char *path, struct tog_blame_line *lines, int nlines,
1813 f7d12f7e 2018-08-01 stsp int blame_complete, int selected_line, int *first_displayed_line,
1814 f7d12f7e 2018-08-01 stsp int *last_displayed_line, int *eof, int max_lines)
1816 84451b3e 2018-07-10 stsp const struct got_error *err;
1817 84451b3e 2018-07-10 stsp int lineno = 0, nprinted = 0;
1818 84451b3e 2018-07-10 stsp char *line;
1819 84451b3e 2018-07-10 stsp size_t len;
1820 84451b3e 2018-07-10 stsp wchar_t *wline;
1821 b700b5d6 2018-07-10 stsp int width, wlimit;
1822 84451b3e 2018-07-10 stsp struct tog_blame_line *blame_line;
1823 ee41ec32 2018-07-10 stsp struct got_object_id *prev_id = NULL;
1824 ab089a2a 2018-07-12 stsp char *id_str;
1826 ab089a2a 2018-07-12 stsp err = got_object_id_str(&id_str, id);
1828 ab089a2a 2018-07-12 stsp return err;
1830 84451b3e 2018-07-10 stsp rewind(f);
1831 f7d12f7e 2018-08-01 stsp werase(view->window);
1833 ab089a2a 2018-07-12 stsp if (asprintf(&line, "commit: %s", id_str) == -1) {
1834 ab089a2a 2018-07-12 stsp err = got_error_from_errno();
1835 ab089a2a 2018-07-12 stsp free(id_str);
1836 ab089a2a 2018-07-12 stsp return err;
1839 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, line, view->ncols);
1840 ab089a2a 2018-07-12 stsp free(line);
1841 2550e4c3 2018-07-13 stsp line = NULL;
1842 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
1843 a3404814 2018-09-02 stsp wstandout(view->window);
1844 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
1845 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
1846 a3404814 2018-09-02 stsp wstandend(view->window);
1847 2550e4c3 2018-07-13 stsp free(wline);
1848 2550e4c3 2018-07-13 stsp wline = NULL;
1849 f7d12f7e 2018-08-01 stsp if (width < view->ncols)
1850 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
1852 084063cd 2018-07-12 stsp if (asprintf(&line, "[%d/%d] %s%s",
1853 084063cd 2018-07-12 stsp *first_displayed_line - 1 + selected_line, nlines,
1854 12a0b23b 2018-07-12 stsp blame_complete ? "" : "annotating ", path) == -1) {
1855 ab089a2a 2018-07-12 stsp free(id_str);
1856 3f60a8ef 2018-07-10 stsp return got_error_from_errno();
1858 ab089a2a 2018-07-12 stsp free(id_str);
1859 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, line, view->ncols);
1860 3f60a8ef 2018-07-10 stsp free(line);
1861 2550e4c3 2018-07-13 stsp line = NULL;
1863 3f60a8ef 2018-07-10 stsp return err;
1864 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
1865 2550e4c3 2018-07-13 stsp free(wline);
1866 2550e4c3 2018-07-13 stsp wline = NULL;
1867 f7d12f7e 2018-08-01 stsp if (width < view->ncols)
1868 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
1871 ab089a2a 2018-07-12 stsp while (nprinted < max_lines - 2) {
1872 84451b3e 2018-07-10 stsp line = parse_next_line(f, &len);
1873 84451b3e 2018-07-10 stsp if (line == NULL) {
1877 84451b3e 2018-07-10 stsp if (++lineno < *first_displayed_line) {
1878 84451b3e 2018-07-10 stsp free(line);
1882 f7d12f7e 2018-08-01 stsp wlimit = view->ncols < 9 ? 0 : view->ncols - 9;
1883 b700b5d6 2018-07-10 stsp err = format_line(&wline, &width, line, wlimit);
1884 84451b3e 2018-07-10 stsp if (err) {
1885 84451b3e 2018-07-10 stsp free(line);
1886 84451b3e 2018-07-10 stsp return err;
1889 b700b5d6 2018-07-10 stsp if (nprinted == selected_line - 1)
1890 f7d12f7e 2018-08-01 stsp wstandout(view->window);
1892 84451b3e 2018-07-10 stsp blame_line = &lines[lineno - 1];
1893 ee41ec32 2018-07-10 stsp if (blame_line->annotated && prev_id &&
1894 ee41ec32 2018-07-10 stsp got_object_id_cmp(prev_id, blame_line->id) == 0)
1895 f7d12f7e 2018-08-01 stsp waddstr(view->window, " ");
1896 ee41ec32 2018-07-10 stsp else if (blame_line->annotated) {
1897 84451b3e 2018-07-10 stsp char *id_str;
1898 84451b3e 2018-07-10 stsp err = got_object_id_str(&id_str, blame_line->id);
1899 84451b3e 2018-07-10 stsp if (err) {
1900 84451b3e 2018-07-10 stsp free(line);
1901 2550e4c3 2018-07-13 stsp free(wline);
1902 84451b3e 2018-07-10 stsp return err;
1904 f7d12f7e 2018-08-01 stsp wprintw(view->window, "%.8s ", id_str);
1905 84451b3e 2018-07-10 stsp free(id_str);
1906 ee41ec32 2018-07-10 stsp prev_id = blame_line->id;
1908 f7d12f7e 2018-08-01 stsp waddstr(view->window, "........ ");
1909 ee41ec32 2018-07-10 stsp prev_id = NULL;
1912 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
1913 b700b5d6 2018-07-10 stsp while (width < wlimit) {
1914 f7d12f7e 2018-08-01 stsp waddch(view->window, ' ');
1917 b700b5d6 2018-07-10 stsp if (nprinted == selected_line - 1)
1918 f7d12f7e 2018-08-01 stsp wstandend(view->window);
1919 84451b3e 2018-07-10 stsp if (++nprinted == 1)
1920 84451b3e 2018-07-10 stsp *first_displayed_line = lineno;
1921 84451b3e 2018-07-10 stsp free(line);
1922 2550e4c3 2018-07-13 stsp free(wline);
1923 2550e4c3 2018-07-13 stsp wline = NULL;
1925 84451b3e 2018-07-10 stsp *last_displayed_line = lineno;
1927 1a57306a 2018-09-02 stsp view_vborder(view);
1929 84451b3e 2018-07-10 stsp return NULL;
1932 84451b3e 2018-07-10 stsp static const struct got_error *
1933 84451b3e 2018-07-10 stsp blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
1935 84451b3e 2018-07-10 stsp const struct got_error *err = NULL;
1936 84451b3e 2018-07-10 stsp struct tog_blame_cb_args *a = arg;
1937 84451b3e 2018-07-10 stsp struct tog_blame_line *line;
1939 d68a0a7d 2018-07-10 stsp if (nlines != a->nlines ||
1940 d68a0a7d 2018-07-10 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
1941 84451b3e 2018-07-10 stsp return got_error(GOT_ERR_RANGE);
1943 84451b3e 2018-07-10 stsp if (pthread_mutex_lock(a->mutex) != 0)
1944 84451b3e 2018-07-10 stsp return got_error_from_errno();
1946 18430de3 2018-07-10 stsp if (*a->quit) { /* user has quit the blame view */
1947 d68a0a7d 2018-07-10 stsp err = got_error(GOT_ERR_ITER_COMPLETED);
1948 d68a0a7d 2018-07-10 stsp goto done;
1951 d68a0a7d 2018-07-10 stsp if (lineno == -1)
1952 d68a0a7d 2018-07-10 stsp goto done; /* no change in this commit */
1954 84451b3e 2018-07-10 stsp line = &a->lines[lineno - 1];
1955 d68a0a7d 2018-07-10 stsp if (line->annotated)
1956 d68a0a7d 2018-07-10 stsp goto done;
1958 84451b3e 2018-07-10 stsp line->id = got_object_id_dup(id);
1959 84451b3e 2018-07-10 stsp if (line->id == NULL) {
1960 84451b3e 2018-07-10 stsp err = got_error_from_errno();
1961 84451b3e 2018-07-10 stsp goto done;
1963 84451b3e 2018-07-10 stsp line->annotated = 1;
1965 f7d12f7e 2018-08-01 stsp err = draw_blame(a->view, a->commit_id, a->f, a->path,
1966 245d91c1 2018-07-12 stsp a->lines, a->nlines, 0, *a->selected_line, a->first_displayed_line,
1967 e5a0f69f 2018-08-18 stsp a->last_displayed_line, a->eof, a->view->nlines);
1969 84451b3e 2018-07-10 stsp if (pthread_mutex_unlock(a->mutex) != 0)
1970 84451b3e 2018-07-10 stsp return got_error_from_errno();
1971 84451b3e 2018-07-10 stsp return err;
1974 84451b3e 2018-07-10 stsp static void *
1975 84451b3e 2018-07-10 stsp blame_thread(void *arg)
1977 18430de3 2018-07-10 stsp const struct got_error *err;
1978 18430de3 2018-07-10 stsp struct tog_blame_thread_args *ta = arg;
1979 245d91c1 2018-07-12 stsp struct tog_blame_cb_args *a = ta->cb_args;
1981 ab089a2a 2018-07-12 stsp err = got_blame_incremental(ta->path, a->commit_id, ta->repo,
1982 245d91c1 2018-07-12 stsp blame_cb, ta->cb_args);
1984 18430de3 2018-07-10 stsp if (pthread_mutex_lock(a->mutex) != 0)
1985 18430de3 2018-07-10 stsp return (void *)got_error_from_errno();
1987 c9beca56 2018-07-22 stsp got_repo_close(ta->repo);
1988 c9beca56 2018-07-22 stsp ta->repo = NULL;
1989 c9beca56 2018-07-22 stsp *ta->complete = 1;
1991 f7d12f7e 2018-08-01 stsp err = draw_blame(a->view, a->commit_id, a->f, a->path,
1992 f7d12f7e 2018-08-01 stsp a->lines, a->nlines, 1, *a->selected_line,
1993 e5a0f69f 2018-08-18 stsp a->first_displayed_line, a->last_displayed_line, a->eof,
1994 f7d12f7e 2018-08-01 stsp a->view->nlines);
1996 18430de3 2018-07-10 stsp if (pthread_mutex_unlock(a->mutex) != 0 && err == NULL)
1997 18430de3 2018-07-10 stsp err = got_error_from_errno();
1999 18430de3 2018-07-10 stsp return (void *)err;
2002 245d91c1 2018-07-12 stsp static struct got_object_id *
2003 245d91c1 2018-07-12 stsp get_selected_commit_id(struct tog_blame_line *lines,
2004 245d91c1 2018-07-12 stsp int first_displayed_line, int selected_line)
2006 245d91c1 2018-07-12 stsp struct tog_blame_line *line;
2008 245d91c1 2018-07-12 stsp line = &lines[first_displayed_line - 1 + selected_line - 1];
2009 245d91c1 2018-07-12 stsp if (!line->annotated)
2010 245d91c1 2018-07-12 stsp return NULL;
2012 245d91c1 2018-07-12 stsp return line->id;
2015 84451b3e 2018-07-10 stsp static const struct got_error *
2016 245d91c1 2018-07-12 stsp open_selected_commit(struct got_object **pobj, struct got_object **obj,
2017 b880a918 2018-07-10 stsp struct tog_blame_line *lines, int first_displayed_line,
2018 b880a918 2018-07-10 stsp int selected_line, struct got_repository *repo)
2020 b880a918 2018-07-10 stsp const struct got_error *err = NULL;
2021 b880a918 2018-07-10 stsp struct got_commit_object *commit = NULL;
2022 245d91c1 2018-07-12 stsp struct got_object_id *selected_id;
2023 b880a918 2018-07-10 stsp struct got_object_qid *pid;
2025 b880a918 2018-07-10 stsp *pobj = NULL;
2026 b880a918 2018-07-10 stsp *obj = NULL;
2028 245d91c1 2018-07-12 stsp selected_id = get_selected_commit_id(lines,
2029 245d91c1 2018-07-12 stsp first_displayed_line, selected_line);
2030 245d91c1 2018-07-12 stsp if (selected_id == NULL)
2031 b880a918 2018-07-10 stsp return NULL;
2033 245d91c1 2018-07-12 stsp err = got_object_open(obj, repo, selected_id);
2035 b880a918 2018-07-10 stsp goto done;
2037 b880a918 2018-07-10 stsp err = got_object_commit_open(&commit, repo, *obj);
2039 b880a918 2018-07-10 stsp goto done;
2041 b880a918 2018-07-10 stsp pid = SIMPLEQ_FIRST(&commit->parent_ids);
2042 b880a918 2018-07-10 stsp if (pid) {
2043 b880a918 2018-07-10 stsp err = got_object_open(pobj, repo, pid->id);
2045 b880a918 2018-07-10 stsp goto done;
2048 b880a918 2018-07-10 stsp if (commit)
2049 b880a918 2018-07-10 stsp got_object_commit_close(commit);
2050 b880a918 2018-07-10 stsp return err;
2053 b880a918 2018-07-10 stsp static const struct got_error *
2054 245d91c1 2018-07-12 stsp stop_blame(struct tog_blame *blame)
2056 a70480e0 2018-06-23 stsp const struct got_error *err = NULL;
2059 245d91c1 2018-07-12 stsp if (blame->thread) {
2060 245d91c1 2018-07-12 stsp if (pthread_join(blame->thread, (void **)&err) != 0)
2061 245d91c1 2018-07-12 stsp err = got_error_from_errno();
2062 245d91c1 2018-07-12 stsp if (err && err->code == GOT_ERR_ITER_COMPLETED)
2063 245d91c1 2018-07-12 stsp err = NULL;
2064 245d91c1 2018-07-12 stsp blame->thread = NULL;
2066 245d91c1 2018-07-12 stsp if (blame->thread_args.repo) {
2067 245d91c1 2018-07-12 stsp got_repo_close(blame->thread_args.repo);
2068 245d91c1 2018-07-12 stsp blame->thread_args.repo = NULL;
2070 245d91c1 2018-07-12 stsp if (blame->f) {
2071 245d91c1 2018-07-12 stsp fclose(blame->f);
2072 245d91c1 2018-07-12 stsp blame->f = NULL;
2074 245d91c1 2018-07-12 stsp for (i = 0; i < blame->nlines; i++)
2075 245d91c1 2018-07-12 stsp free(blame->lines[i].id);
2076 245d91c1 2018-07-12 stsp free(blame->lines);
2077 245d91c1 2018-07-12 stsp blame->lines = NULL;
2078 75c32340 2018-07-23 stsp free(blame->cb_args.commit_id);
2079 75c32340 2018-07-23 stsp blame->cb_args.commit_id = NULL;
2081 245d91c1 2018-07-12 stsp return err;
2084 245d91c1 2018-07-12 stsp static const struct got_error *
2085 7cc84d77 2018-08-01 stsp run_blame(struct tog_blame *blame, pthread_mutex_t *mutex,
2086 7cc84d77 2018-08-01 stsp struct tog_view *view, int *blame_complete,
2087 245d91c1 2018-07-12 stsp int *first_displayed_line, int *last_displayed_line,
2088 e5a0f69f 2018-08-18 stsp int *selected_line, int *done, int *eof, const char *path,
2089 245d91c1 2018-07-12 stsp struct got_object_id *commit_id,
2090 245d91c1 2018-07-12 stsp struct got_repository *repo)
2092 245d91c1 2018-07-12 stsp const struct got_error *err = NULL;
2093 84451b3e 2018-07-10 stsp struct got_blob_object *blob = NULL;
2094 245d91c1 2018-07-12 stsp struct got_repository *thread_repo = NULL;
2095 27d434c2 2018-09-15 stsp struct got_object_id *obj_id = NULL;
2096 27d434c2 2018-09-15 stsp struct got_object *obj = NULL;
2098 27d434c2 2018-09-15 stsp err = got_object_id_by_path(&obj_id, repo, commit_id, path);
2100 27d434c2 2018-09-15 stsp goto done;
2102 27d434c2 2018-09-15 stsp err = got_object_open(&obj, repo, obj_id);
2104 84451b3e 2018-07-10 stsp goto done;
2106 84451b3e 2018-07-10 stsp if (got_object_get_type(obj) != GOT_OBJ_TYPE_BLOB) {
2107 84451b3e 2018-07-10 stsp err = got_error(GOT_ERR_OBJ_TYPE);
2108 84451b3e 2018-07-10 stsp goto done;
2111 84451b3e 2018-07-10 stsp err = got_object_blob_open(&blob, repo, obj, 8192);
2113 a70480e0 2018-06-23 stsp goto done;
2114 245d91c1 2018-07-12 stsp blame->f = got_opentemp();
2115 245d91c1 2018-07-12 stsp if (blame->f == NULL) {
2116 84451b3e 2018-07-10 stsp err = got_error_from_errno();
2117 84451b3e 2018-07-10 stsp goto done;
2119 245d91c1 2018-07-12 stsp err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
2120 245d91c1 2018-07-12 stsp blame->f, blob);
2122 84451b3e 2018-07-10 stsp goto done;
2124 245d91c1 2018-07-12 stsp blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
2125 245d91c1 2018-07-12 stsp if (blame->lines == NULL) {
2126 84451b3e 2018-07-10 stsp err = got_error_from_errno();
2127 84451b3e 2018-07-10 stsp goto done;
2130 245d91c1 2018-07-12 stsp err = got_repo_open(&thread_repo, got_repo_get_path(repo));
2132 bd24772e 2018-07-11 stsp goto done;
2134 7cc84d77 2018-08-01 stsp blame->cb_args.view = view;
2135 245d91c1 2018-07-12 stsp blame->cb_args.lines = blame->lines;
2136 245d91c1 2018-07-12 stsp blame->cb_args.nlines = blame->nlines;
2137 245d91c1 2018-07-12 stsp blame->cb_args.mutex = mutex;
2138 245d91c1 2018-07-12 stsp blame->cb_args.commit_id = got_object_id_dup(commit_id);
2139 245d91c1 2018-07-12 stsp if (blame->cb_args.commit_id == NULL) {
2140 245d91c1 2018-07-12 stsp err = got_error_from_errno();
2141 245d91c1 2018-07-12 stsp goto done;
2143 245d91c1 2018-07-12 stsp blame->cb_args.f = blame->f;
2144 245d91c1 2018-07-12 stsp blame->cb_args.path = path;
2145 245d91c1 2018-07-12 stsp blame->cb_args.first_displayed_line = first_displayed_line;
2146 245d91c1 2018-07-12 stsp blame->cb_args.selected_line = selected_line;
2147 245d91c1 2018-07-12 stsp blame->cb_args.last_displayed_line = last_displayed_line;
2148 245d91c1 2018-07-12 stsp blame->cb_args.quit = done;
2149 e5a0f69f 2018-08-18 stsp blame->cb_args.eof = eof;
2151 245d91c1 2018-07-12 stsp blame->thread_args.path = path;
2152 245d91c1 2018-07-12 stsp blame->thread_args.repo = thread_repo;
2153 245d91c1 2018-07-12 stsp blame->thread_args.cb_args = &blame->cb_args;
2154 245d91c1 2018-07-12 stsp blame->thread_args.complete = blame_complete;
2155 245d91c1 2018-07-12 stsp *blame_complete = 0;
2157 245d91c1 2018-07-12 stsp if (pthread_create(&blame->thread, NULL, blame_thread,
2158 245d91c1 2018-07-12 stsp &blame->thread_args) != 0) {
2159 245d91c1 2018-07-12 stsp err = got_error_from_errno();
2160 245d91c1 2018-07-12 stsp goto done;
2165 245d91c1 2018-07-12 stsp got_object_blob_close(blob);
2166 27d434c2 2018-09-15 stsp free(obj_id);
2168 245d91c1 2018-07-12 stsp got_object_close(obj);
2170 245d91c1 2018-07-12 stsp stop_blame(blame);
2171 245d91c1 2018-07-12 stsp return err;
2174 245d91c1 2018-07-12 stsp static const struct got_error *
2175 e5a0f69f 2018-08-18 stsp open_blame_view(struct tog_view *view, char *path,
2176 e1cd8fed 2018-08-01 stsp struct got_object_id *commit_id, struct got_repository *repo)
2178 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL;
2179 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
2181 fb2756b9 2018-08-04 stsp SIMPLEQ_INIT(&s->blamed_commits);
2183 fb2756b9 2018-08-04 stsp if (pthread_mutex_init(&s->mutex, NULL) != 0)
2184 7cbe629d 2018-08-04 stsp return got_error_from_errno();
2186 fb2756b9 2018-08-04 stsp err = got_object_qid_alloc(&s->blamed_commit, commit_id);
2188 7cbe629d 2018-08-04 stsp return err;
2190 fb2756b9 2018-08-04 stsp SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
2191 fb2756b9 2018-08-04 stsp s->first_displayed_line = 1;
2192 fb2756b9 2018-08-04 stsp s->last_displayed_line = view->nlines;
2193 fb2756b9 2018-08-04 stsp s->selected_line = 1;
2194 fb2756b9 2018-08-04 stsp s->blame_complete = 0;
2195 fb2756b9 2018-08-04 stsp s->path = path;
2196 e5a0f69f 2018-08-18 stsp if (s->path == NULL)
2197 e5a0f69f 2018-08-18 stsp return got_error_from_errno();
2198 fb2756b9 2018-08-04 stsp s->repo = repo;
2199 fb2756b9 2018-08-04 stsp s->commit_id = commit_id;
2200 e9424729 2018-08-04 stsp memset(&s->blame, 0, sizeof(s->blame));
2202 e5a0f69f 2018-08-18 stsp view->show = show_blame_view;
2203 e5a0f69f 2018-08-18 stsp view->input = input_blame_view;
2204 e5a0f69f 2018-08-18 stsp view->close = close_blame_view;
2206 e5a0f69f 2018-08-18 stsp return run_blame(&s->blame, &s->mutex, view, &s->blame_complete,
2207 e5a0f69f 2018-08-18 stsp &s->first_displayed_line, &s->last_displayed_line,
2208 e5a0f69f 2018-08-18 stsp &s->selected_line, &s->done, &s->eof, s->path,
2209 e5a0f69f 2018-08-18 stsp s->blamed_commit->id, s->repo);
2212 e5a0f69f 2018-08-18 stsp static const struct got_error *
2213 7cbe629d 2018-08-04 stsp close_blame_view(struct tog_view *view)
2215 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
2216 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
2218 e5a0f69f 2018-08-18 stsp if (s->blame.thread)
2219 e5a0f69f 2018-08-18 stsp err = stop_blame(&s->blame);
2221 fb2756b9 2018-08-04 stsp while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
2222 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
2223 fb2756b9 2018-08-04 stsp blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
2224 fb2756b9 2018-08-04 stsp SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
2225 7cbe629d 2018-08-04 stsp got_object_qid_free(blamed_commit);
2228 e5a0f69f 2018-08-18 stsp free(s->path);
2230 e5a0f69f 2018-08-18 stsp return err;
2233 7cbe629d 2018-08-04 stsp static const struct got_error *
2234 7cbe629d 2018-08-04 stsp show_blame_view(struct tog_view *view)
2236 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
2237 e5a0f69f 2018-08-18 stsp struct tog_blame_view_state *s = &view->state.blame;
2239 e5a0f69f 2018-08-18 stsp if (pthread_mutex_lock(&s->mutex) != 0)
2240 e5a0f69f 2018-08-18 stsp return got_error_from_errno();
2242 e5a0f69f 2018-08-18 stsp err = draw_blame(view, s->blamed_commit->id, s->blame.f,
2243 e5a0f69f 2018-08-18 stsp s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
2244 e5a0f69f 2018-08-18 stsp s->selected_line, &s->first_displayed_line,
2245 e5a0f69f 2018-08-18 stsp &s->last_displayed_line, &s->eof, view->nlines);
2247 e5a0f69f 2018-08-18 stsp if (pthread_mutex_unlock(&s->mutex) != 0 && err == NULL)
2248 e5a0f69f 2018-08-18 stsp err = got_error_from_errno();
2250 e5a0f69f 2018-08-18 stsp return err;
2253 e5a0f69f 2018-08-18 stsp static const struct got_error *
2254 e5a0f69f 2018-08-18 stsp input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
2255 e5a0f69f 2018-08-18 stsp struct tog_view *view, int ch)
2257 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL, *thread_err = NULL;
2258 7cbe629d 2018-08-04 stsp struct got_object *obj = NULL, *pobj = NULL;
2259 7cbe629d 2018-08-04 stsp struct tog_view *diff_view;
2260 fb2756b9 2018-08-04 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 err = got_error_from_errno();
2264 e5a0f69f 2018-08-18 stsp goto done;
2267 e5a0f69f 2018-08-18 stsp switch (ch) {
2269 e5a0f69f 2018-08-18 stsp s->done = 1;
2270 e5a0f69f 2018-08-18 stsp if (pthread_mutex_unlock(&s->mutex) != 0) {
2271 e5a0f69f 2018-08-18 stsp err = got_error_from_errno();
2272 e5a0f69f 2018-08-18 stsp goto done;
2274 e5a0f69f 2018-08-18 stsp return stop_blame(&s->blame);
2276 e5a0f69f 2018-08-18 stsp case KEY_UP:
2277 e5a0f69f 2018-08-18 stsp if (s->selected_line > 1)
2278 e5a0f69f 2018-08-18 stsp s->selected_line--;
2279 e5a0f69f 2018-08-18 stsp else if (s->selected_line == 1 &&
2280 e5a0f69f 2018-08-18 stsp s->first_displayed_line > 1)
2281 e5a0f69f 2018-08-18 stsp s->first_displayed_line--;
2283 e5a0f69f 2018-08-18 stsp case KEY_PPAGE:
2284 e5a0f69f 2018-08-18 stsp if (s->first_displayed_line == 1) {
2285 e5a0f69f 2018-08-18 stsp s->selected_line = 1;
2288 e5a0f69f 2018-08-18 stsp if (s->first_displayed_line > view->nlines - 2)
2289 e5a0f69f 2018-08-18 stsp s->first_displayed_line -=
2290 e5a0f69f 2018-08-18 stsp (view->nlines - 2);
2292 e5a0f69f 2018-08-18 stsp s->first_displayed_line = 1;
2295 e5a0f69f 2018-08-18 stsp case KEY_DOWN:
2296 e5a0f69f 2018-08-18 stsp if (s->selected_line < view->nlines - 2 &&
2297 e5a0f69f 2018-08-18 stsp s->first_displayed_line +
2298 e5a0f69f 2018-08-18 stsp s->selected_line <= s->blame.nlines)
2299 e5a0f69f 2018-08-18 stsp s->selected_line++;
2300 e5a0f69f 2018-08-18 stsp else if (s->last_displayed_line <
2301 e5a0f69f 2018-08-18 stsp s->blame.nlines)
2302 e5a0f69f 2018-08-18 stsp s->first_displayed_line++;
2305 e5a0f69f 2018-08-18 stsp case 'p': {
2306 e5a0f69f 2018-08-18 stsp struct got_object_id *id;
2307 e5a0f69f 2018-08-18 stsp id = get_selected_commit_id(s->blame.lines,
2308 e5a0f69f 2018-08-18 stsp s->first_displayed_line, s->selected_line);
2309 e5a0f69f 2018-08-18 stsp if (id == NULL || got_object_id_cmp(id,
2310 e5a0f69f 2018-08-18 stsp s->blamed_commit->id) == 0)
2312 e5a0f69f 2018-08-18 stsp err = open_selected_commit(&pobj, &obj,
2313 e5a0f69f 2018-08-18 stsp s->blame.lines, s->first_displayed_line,
2314 e5a0f69f 2018-08-18 stsp s->selected_line, s->repo);
2317 e5a0f69f 2018-08-18 stsp if (pobj == NULL && obj == NULL)
2319 e5a0f69f 2018-08-18 stsp if (ch == 'p' && pobj == NULL)
2321 e5a0f69f 2018-08-18 stsp s->done = 1;
2322 e5a0f69f 2018-08-18 stsp if (pthread_mutex_unlock(&s->mutex) != 0) {
2323 e5a0f69f 2018-08-18 stsp err = got_error_from_errno();
2324 e5a0f69f 2018-08-18 stsp goto done;
2326 e5a0f69f 2018-08-18 stsp thread_err = stop_blame(&s->blame);
2327 e5a0f69f 2018-08-18 stsp s->done = 0;
2328 e5a0f69f 2018-08-18 stsp if (pthread_mutex_lock(&s->mutex) != 0) {
2329 e5a0f69f 2018-08-18 stsp err = got_error_from_errno();
2330 e5a0f69f 2018-08-18 stsp goto done;
2332 e5a0f69f 2018-08-18 stsp if (thread_err)
2334 e5a0f69f 2018-08-18 stsp id = got_object_get_id(ch == 'b' ? obj : pobj);
2335 e5a0f69f 2018-08-18 stsp got_object_close(obj);
2336 e5a0f69f 2018-08-18 stsp obj = NULL;
2337 e5a0f69f 2018-08-18 stsp if (pobj) {
2338 e5a0f69f 2018-08-18 stsp got_object_close(pobj);
2339 e5a0f69f 2018-08-18 stsp pobj = NULL;
2341 6402fb3c 2018-09-15 stsp err = got_object_qid_alloc(&s->blamed_commit, id);
2343 e5a0f69f 2018-08-18 stsp goto done;
2344 e5a0f69f 2018-08-18 stsp SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
2345 e5a0f69f 2018-08-18 stsp s->blamed_commit, entry);
2346 e5a0f69f 2018-08-18 stsp err = run_blame(&s->blame, &s->mutex, view,
2347 e5a0f69f 2018-08-18 stsp &s->blame_complete,
2348 e5a0f69f 2018-08-18 stsp &s->first_displayed_line,
2349 e5a0f69f 2018-08-18 stsp &s->last_displayed_line,
2350 e5a0f69f 2018-08-18 stsp &s->selected_line, &s->done, &s->eof,
2351 e5a0f69f 2018-08-18 stsp s->path, s->blamed_commit->id, s->repo);
2356 e5a0f69f 2018-08-18 stsp case 'B': {
2357 e5a0f69f 2018-08-18 stsp struct got_object_qid *first;
2358 e5a0f69f 2018-08-18 stsp first = SIMPLEQ_FIRST(&s->blamed_commits);
2359 e5a0f69f 2018-08-18 stsp if (!got_object_id_cmp(first->id, s->commit_id))
2361 e5a0f69f 2018-08-18 stsp s->done = 1;
2362 e5a0f69f 2018-08-18 stsp if (pthread_mutex_unlock(&s->mutex) != 0) {
2363 e5a0f69f 2018-08-18 stsp err = got_error_from_errno();
2364 e5a0f69f 2018-08-18 stsp goto done;
2366 e5a0f69f 2018-08-18 stsp thread_err = stop_blame(&s->blame);
2367 e5a0f69f 2018-08-18 stsp s->done = 0;
2368 e5a0f69f 2018-08-18 stsp if (pthread_mutex_lock(&s->mutex) != 0) {
2369 e5a0f69f 2018-08-18 stsp err = got_error_from_errno();
2370 e5a0f69f 2018-08-18 stsp goto done;
2372 e5a0f69f 2018-08-18 stsp if (thread_err)
2374 e5a0f69f 2018-08-18 stsp SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
2375 e5a0f69f 2018-08-18 stsp got_object_qid_free(s->blamed_commit);
2376 e5a0f69f 2018-08-18 stsp s->blamed_commit =
2377 e5a0f69f 2018-08-18 stsp SIMPLEQ_FIRST(&s->blamed_commits);
2378 e5a0f69f 2018-08-18 stsp err = run_blame(&s->blame, &s->mutex, view,
2379 e5a0f69f 2018-08-18 stsp &s->blame_complete,
2380 e5a0f69f 2018-08-18 stsp &s->first_displayed_line,
2381 e5a0f69f 2018-08-18 stsp &s->last_displayed_line,
2382 e5a0f69f 2018-08-18 stsp &s->selected_line, &s->done, &s->eof, s->path,
2383 e5a0f69f 2018-08-18 stsp s->blamed_commit->id, s->repo);
2388 e5a0f69f 2018-08-18 stsp case KEY_ENTER:
2389 e5a0f69f 2018-08-18 stsp case '\r':
2390 e5a0f69f 2018-08-18 stsp err = open_selected_commit(&pobj, &obj,
2391 e5a0f69f 2018-08-18 stsp s->blame.lines, s->first_displayed_line,
2392 e5a0f69f 2018-08-18 stsp s->selected_line, s->repo);
2395 e5a0f69f 2018-08-18 stsp if (pobj == NULL && obj == NULL)
2397 e5a0f69f 2018-08-18 stsp diff_view = view_open(0, 0, 0, 0, view,
2398 e5a0f69f 2018-08-18 stsp TOG_VIEW_DIFF);
2399 e5a0f69f 2018-08-18 stsp if (diff_view == NULL) {
2400 e5a0f69f 2018-08-18 stsp err = got_error_from_errno();
2403 e5a0f69f 2018-08-18 stsp err = open_diff_view(diff_view, pobj, obj,
2405 e5a0f69f 2018-08-18 stsp if (err) {
2406 e5a0f69f 2018-08-18 stsp view_close(diff_view);
2409 e5a0f69f 2018-08-18 stsp *new_view = diff_view;
2410 e5a0f69f 2018-08-18 stsp if (pobj) {
2411 e5a0f69f 2018-08-18 stsp got_object_close(pobj);
2412 e5a0f69f 2018-08-18 stsp pobj = NULL;
2414 e5a0f69f 2018-08-18 stsp got_object_close(obj);
2415 e5a0f69f 2018-08-18 stsp obj = NULL;
2419 e5a0f69f 2018-08-18 stsp case KEY_NPAGE:
2421 e5a0f69f 2018-08-18 stsp if (s->last_displayed_line >= s->blame.nlines &&
2422 e5a0f69f 2018-08-18 stsp s->selected_line < view->nlines - 2) {
2423 e5a0f69f 2018-08-18 stsp s->selected_line = MIN(s->blame.nlines,
2424 e5a0f69f 2018-08-18 stsp view->nlines - 2);
2427 e5a0f69f 2018-08-18 stsp if (s->last_displayed_line + view->nlines - 2
2428 e5a0f69f 2018-08-18 stsp <= s->blame.nlines)
2429 e5a0f69f 2018-08-18 stsp s->first_displayed_line +=
2430 e5a0f69f 2018-08-18 stsp view->nlines - 2;
2432 e5a0f69f 2018-08-18 stsp s->first_displayed_line =
2433 e5a0f69f 2018-08-18 stsp s->blame.nlines -
2434 e5a0f69f 2018-08-18 stsp (view->nlines - 3);
2436 e5a0f69f 2018-08-18 stsp case KEY_RESIZE:
2437 e5a0f69f 2018-08-18 stsp if (s->selected_line > view->nlines - 2) {
2438 e5a0f69f 2018-08-18 stsp s->selected_line = MIN(s->blame.nlines,
2439 e5a0f69f 2018-08-18 stsp view->nlines - 2);
2446 e5a0f69f 2018-08-18 stsp if (pthread_mutex_unlock(&s->mutex) != 0)
2447 e5a0f69f 2018-08-18 stsp err = got_error_from_errno();
2450 b880a918 2018-07-10 stsp got_object_close(pobj);
2451 245d91c1 2018-07-12 stsp return thread_err ? thread_err : err;
2454 a70480e0 2018-06-23 stsp static const struct got_error *
2455 9f7d7167 2018-04-29 stsp cmd_blame(int argc, char *argv[])
2457 a70480e0 2018-06-23 stsp const struct got_error *error;
2458 a70480e0 2018-06-23 stsp struct got_repository *repo = NULL;
2459 69069811 2018-08-02 stsp char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2460 a70480e0 2018-06-23 stsp struct got_object_id *commit_id = NULL;
2461 a70480e0 2018-06-23 stsp char *commit_id_str = NULL;
2463 e1cd8fed 2018-08-01 stsp struct tog_view *view;
2465 a70480e0 2018-06-23 stsp #ifndef PROFILE
2466 ad242220 2018-09-08 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd", NULL)
2468 a70480e0 2018-06-23 stsp err(1, "pledge");
2471 69069811 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2472 a70480e0 2018-06-23 stsp switch (ch) {
2474 a70480e0 2018-06-23 stsp commit_id_str = optarg;
2477 69069811 2018-08-02 stsp repo_path = realpath(optarg, NULL);
2478 69069811 2018-08-02 stsp if (repo_path == NULL)
2479 69069811 2018-08-02 stsp err(1, "-r option");
2483 a70480e0 2018-06-23 stsp /* NOTREACHED */
2487 a70480e0 2018-06-23 stsp argc -= optind;
2488 a70480e0 2018-06-23 stsp argv += optind;
2490 69069811 2018-08-02 stsp if (argc == 1)
2491 69069811 2018-08-02 stsp path = argv[0];
2493 a70480e0 2018-06-23 stsp usage_blame();
2495 69069811 2018-08-02 stsp cwd = getcwd(NULL, 0);
2496 69069811 2018-08-02 stsp if (cwd == NULL) {
2497 69069811 2018-08-02 stsp error = got_error_from_errno();
2498 69069811 2018-08-02 stsp goto done;
2500 69069811 2018-08-02 stsp if (repo_path == NULL) {
2501 69069811 2018-08-02 stsp repo_path = strdup(cwd);
2502 69069811 2018-08-02 stsp if (repo_path == NULL) {
2503 69069811 2018-08-02 stsp error = got_error_from_errno();
2504 69069811 2018-08-02 stsp goto done;
2509 69069811 2018-08-02 stsp error = got_repo_open(&repo, repo_path);
2510 a70480e0 2018-06-23 stsp if (error != NULL)
2511 66b4983c 2018-06-23 stsp return error;
2513 69069811 2018-08-02 stsp error = got_repo_map_path(&in_repo_path, repo, path);
2514 69069811 2018-08-02 stsp if (error != NULL)
2515 69069811 2018-08-02 stsp goto done;
2517 a70480e0 2018-06-23 stsp if (commit_id_str == NULL) {
2518 a70480e0 2018-06-23 stsp struct got_reference *head_ref;
2519 a70480e0 2018-06-23 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
2520 a70480e0 2018-06-23 stsp if (error != NULL)
2521 66b4983c 2018-06-23 stsp goto done;
2522 a70480e0 2018-06-23 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
2523 a70480e0 2018-06-23 stsp got_ref_close(head_ref);
2525 a70480e0 2018-06-23 stsp struct got_object *obj;
2526 a70480e0 2018-06-23 stsp error = got_object_open_by_id_str(&obj, repo, commit_id_str);
2527 a70480e0 2018-06-23 stsp if (error != NULL)
2528 66b4983c 2018-06-23 stsp goto done;
2529 6402fb3c 2018-09-15 stsp commit_id = got_object_id_dup(got_object_get_id(obj));
2530 a19e88aa 2018-06-23 stsp if (commit_id == NULL)
2531 66b4983c 2018-06-23 stsp error = got_error_from_errno();
2532 a19e88aa 2018-06-23 stsp got_object_close(obj);
2534 a19e88aa 2018-06-23 stsp if (error != NULL)
2535 a19e88aa 2018-06-23 stsp goto done;
2537 b3665f43 2018-08-04 stsp view = view_open(0, 0, 0, 0, NULL, TOG_VIEW_BLAME);
2538 e1cd8fed 2018-08-01 stsp if (view == NULL) {
2539 e1cd8fed 2018-08-01 stsp error = got_error_from_errno();
2540 e1cd8fed 2018-08-01 stsp goto done;
2542 7cbe629d 2018-08-04 stsp error = open_blame_view(view, in_repo_path, commit_id, repo);
2543 7cbe629d 2018-08-04 stsp if (error)
2544 7cbe629d 2018-08-04 stsp goto done;
2545 e5a0f69f 2018-08-18 stsp error = view_loop(view);
2547 69069811 2018-08-02 stsp free(repo_path);
2548 69069811 2018-08-02 stsp free(cwd);
2549 a70480e0 2018-06-23 stsp free(commit_id);
2551 a70480e0 2018-06-23 stsp got_repo_close(repo);
2552 a70480e0 2018-06-23 stsp return error;
2555 ffd1d5e5 2018-06-23 stsp static const struct got_error *
2556 f7d12f7e 2018-08-01 stsp draw_tree_entries(struct tog_view *view,
2557 f7d12f7e 2018-08-01 stsp struct got_tree_entry **first_displayed_entry,
2558 ffd1d5e5 2018-06-23 stsp struct got_tree_entry **last_displayed_entry,
2559 ffd1d5e5 2018-06-23 stsp struct got_tree_entry **selected_entry, int *ndisplayed,
2560 f7d12f7e 2018-08-01 stsp const char *label, int show_ids, const char *parent_path,
2561 ce52c690 2018-06-23 stsp const struct got_tree_entries *entries, int selected, int limit, int isroot)
2563 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
2564 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *te;
2565 ffd1d5e5 2018-06-23 stsp wchar_t *wline;
2566 ffd1d5e5 2018-06-23 stsp int width, n;
2568 ffd1d5e5 2018-06-23 stsp *ndisplayed = 0;
2570 f7d12f7e 2018-08-01 stsp werase(view->window);
2572 ffd1d5e5 2018-06-23 stsp if (limit == 0)
2573 ffd1d5e5 2018-06-23 stsp return NULL;
2575 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, label, view->ncols);
2577 ffd1d5e5 2018-06-23 stsp return err;
2578 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2579 a3404814 2018-09-02 stsp wstandout(view->window);
2580 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
2581 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2582 a3404814 2018-09-02 stsp wstandend(view->window);
2583 2550e4c3 2018-07-13 stsp free(wline);
2584 2550e4c3 2018-07-13 stsp wline = NULL;
2585 f7d12f7e 2018-08-01 stsp if (width < view->ncols)
2586 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
2587 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
2588 ffd1d5e5 2018-06-23 stsp return NULL;
2589 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, parent_path, view->ncols);
2591 ce52c690 2018-06-23 stsp return err;
2592 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
2593 2550e4c3 2018-07-13 stsp free(wline);
2594 2550e4c3 2018-07-13 stsp wline = NULL;
2595 f7d12f7e 2018-08-01 stsp if (width < view->ncols)
2596 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
2597 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
2598 ffd1d5e5 2018-06-23 stsp return NULL;
2599 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
2600 a1eca9bb 2018-06-23 stsp if (--limit <= 0)
2601 a1eca9bb 2018-06-23 stsp return NULL;
2603 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_FIRST(&entries->head);
2604 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry == NULL) {
2605 ffd1d5e5 2018-06-23 stsp if (selected == 0) {
2606 f7d12f7e 2018-08-01 stsp wstandout(view->window);
2607 ffd1d5e5 2018-06-23 stsp *selected_entry = NULL;
2609 f7d12f7e 2018-08-01 stsp waddstr(view->window, " ..\n"); /* parent directory */
2610 ffd1d5e5 2018-06-23 stsp if (selected == 0)
2611 f7d12f7e 2018-08-01 stsp wstandend(view->window);
2612 ffd1d5e5 2018-06-23 stsp (*ndisplayed)++;
2613 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
2614 ffd1d5e5 2018-06-23 stsp return NULL;
2618 ffd1d5e5 2018-06-23 stsp while (te != *first_displayed_entry)
2619 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_NEXT(te, entry);
2622 ffd1d5e5 2018-06-23 stsp while (te) {
2623 1d13200f 2018-07-12 stsp char *line = NULL, *id_str = NULL;
2625 1d13200f 2018-07-12 stsp if (show_ids) {
2626 1d13200f 2018-07-12 stsp err = got_object_id_str(&id_str, te->id);
2628 1d13200f 2018-07-12 stsp return got_error_from_errno();
2630 1d13200f 2018-07-12 stsp if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
2631 1d13200f 2018-07-12 stsp te->name, S_ISDIR(te->mode) ? "/" : "") == -1) {
2632 1d13200f 2018-07-12 stsp free(id_str);
2633 ffd1d5e5 2018-06-23 stsp return got_error_from_errno();
2635 1d13200f 2018-07-12 stsp free(id_str);
2636 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, line, view->ncols);
2637 ffd1d5e5 2018-06-23 stsp if (err) {
2638 ffd1d5e5 2018-06-23 stsp free(line);
2641 ffd1d5e5 2018-06-23 stsp if (n == selected) {
2642 f7d12f7e 2018-08-01 stsp wstandout(view->window);
2643 ffd1d5e5 2018-06-23 stsp *selected_entry = te;
2645 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
2646 f7d12f7e 2018-08-01 stsp if (width < view->ncols)
2647 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
2648 ffd1d5e5 2018-06-23 stsp if (n == selected)
2649 f7d12f7e 2018-08-01 stsp wstandend(view->window);
2650 ffd1d5e5 2018-06-23 stsp free(line);
2651 2550e4c3 2018-07-13 stsp free(wline);
2652 2550e4c3 2018-07-13 stsp wline = NULL;
2654 ffd1d5e5 2018-06-23 stsp (*ndisplayed)++;
2655 ffd1d5e5 2018-06-23 stsp *last_displayed_entry = te;
2656 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
2658 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_NEXT(te, entry);
2661 1a57306a 2018-09-02 stsp view_vborder(view);
2662 ffd1d5e5 2018-06-23 stsp return err;
2665 ffd1d5e5 2018-06-23 stsp static void
2666 ffd1d5e5 2018-06-23 stsp tree_scroll_up(struct got_tree_entry **first_displayed_entry, int maxscroll,
2667 ffd1d5e5 2018-06-23 stsp const struct got_tree_entries *entries, int isroot)
2669 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *te, *prev;
2672 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry == NULL)
2675 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_FIRST(&entries->head);
2676 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry == te) {
2677 ffd1d5e5 2018-06-23 stsp if (!isroot)
2678 ffd1d5e5 2018-06-23 stsp *first_displayed_entry = NULL;
2682 ffd1d5e5 2018-06-23 stsp /* XXX this is stupid... switch to TAILQ? */
2683 ffd1d5e5 2018-06-23 stsp for (i = 0; i < maxscroll; i++) {
2684 ffd1d5e5 2018-06-23 stsp while (te != *first_displayed_entry) {
2685 ffd1d5e5 2018-06-23 stsp prev = te;
2686 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_NEXT(te, entry);
2688 ffd1d5e5 2018-06-23 stsp *first_displayed_entry = prev;
2689 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_FIRST(&entries->head);
2691 ffd1d5e5 2018-06-23 stsp if (!isroot && te == SIMPLEQ_FIRST(&entries->head) && i < maxscroll)
2692 ffd1d5e5 2018-06-23 stsp *first_displayed_entry = NULL;
2695 ffd1d5e5 2018-06-23 stsp static void
2696 ffd1d5e5 2018-06-23 stsp tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
2697 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *last_displayed_entry,
2698 ffd1d5e5 2018-06-23 stsp const struct got_tree_entries *entries)
2700 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *next;
2701 ffd1d5e5 2018-06-23 stsp int n = 0;
2703 ffd1d5e5 2018-06-23 stsp if (SIMPLEQ_NEXT(last_displayed_entry, entry) == NULL)
2706 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry)
2707 ffd1d5e5 2018-06-23 stsp next = SIMPLEQ_NEXT(*first_displayed_entry, entry);
2709 ffd1d5e5 2018-06-23 stsp next = SIMPLEQ_FIRST(&entries->head);
2710 ffd1d5e5 2018-06-23 stsp while (next) {
2711 ffd1d5e5 2018-06-23 stsp *first_displayed_entry = next;
2712 ffd1d5e5 2018-06-23 stsp if (++n >= maxscroll)
2714 ffd1d5e5 2018-06-23 stsp next = SIMPLEQ_NEXT(next, entry);
2718 ffd1d5e5 2018-06-23 stsp static const struct got_error *
2719 ce52c690 2018-06-23 stsp tree_entry_path(char **path, struct tog_parent_trees *parents,
2720 ce52c690 2018-06-23 stsp struct got_tree_entry *te)
2722 cb2ebc8a 2018-06-23 stsp const struct got_error *err = NULL;
2723 ffd1d5e5 2018-06-23 stsp struct tog_parent_tree *pt;
2724 ffd1d5e5 2018-06-23 stsp size_t len = 2; /* for leading slash and NUL */
2726 d9765a41 2018-06-23 stsp TAILQ_FOREACH(pt, parents, entry)
2727 ffd1d5e5 2018-06-23 stsp len += strlen(pt->selected_entry->name) + 1 /* slash */;
2729 ce52c690 2018-06-23 stsp len += strlen(te->name);
2731 ce52c690 2018-06-23 stsp *path = calloc(1, len);
2732 ffd1d5e5 2018-06-23 stsp if (path == NULL)
2733 ffd1d5e5 2018-06-23 stsp return got_error_from_errno();
2735 ce52c690 2018-06-23 stsp (*path)[0] = '/';
2736 d9765a41 2018-06-23 stsp pt = TAILQ_LAST(parents, tog_parent_trees);
2737 d9765a41 2018-06-23 stsp while (pt) {
2738 ce52c690 2018-06-23 stsp if (strlcat(*path, pt->selected_entry->name, len) >= len) {
2739 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
2740 cb2ebc8a 2018-06-23 stsp goto done;
2742 ce52c690 2018-06-23 stsp if (strlcat(*path, "/", len) >= len) {
2743 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
2744 cb2ebc8a 2018-06-23 stsp goto done;
2746 d9765a41 2018-06-23 stsp pt = TAILQ_PREV(pt, tog_parent_trees, entry);
2749 ce52c690 2018-06-23 stsp if (strlcat(*path, te->name, len) >= len) {
2750 ce52c690 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
2751 ce52c690 2018-06-23 stsp goto done;
2755 ce52c690 2018-06-23 stsp if (err) {
2756 ce52c690 2018-06-23 stsp free(*path);
2757 ce52c690 2018-06-23 stsp *path = NULL;
2759 ce52c690 2018-06-23 stsp return err;
2762 ce52c690 2018-06-23 stsp static const struct got_error *
2763 e5a0f69f 2018-08-18 stsp blame_tree_entry(struct tog_view **new_view, struct tog_view *parent_view,
2764 e5a0f69f 2018-08-18 stsp struct got_tree_entry *te, struct tog_parent_trees *parents,
2765 e5a0f69f 2018-08-18 stsp struct got_object_id *commit_id, struct got_repository *repo)
2767 ce52c690 2018-06-23 stsp const struct got_error *err = NULL;
2768 ce52c690 2018-06-23 stsp char *path;
2769 e5a0f69f 2018-08-18 stsp struct tog_view *blame_view;
2771 ce52c690 2018-06-23 stsp err = tree_entry_path(&path, parents, te);
2773 ce52c690 2018-06-23 stsp return err;
2775 e5a0f69f 2018-08-18 stsp blame_view = view_open(0, 0, 0, 0, parent_view, TOG_VIEW_BLAME);
2776 e5a0f69f 2018-08-18 stsp if (blame_view == NULL)
2777 e5a0f69f 2018-08-18 stsp return got_error_from_errno();
2779 e5a0f69f 2018-08-18 stsp err = open_blame_view(blame_view, path, commit_id, repo);
2780 e5a0f69f 2018-08-18 stsp if (err) {
2781 e5a0f69f 2018-08-18 stsp view_close(blame_view);
2782 e5a0f69f 2018-08-18 stsp free(path);
2784 e5a0f69f 2018-08-18 stsp *new_view = blame_view;
2785 69efd4c4 2018-07-18 stsp return err;
2788 69efd4c4 2018-07-18 stsp static const struct got_error *
2789 e5a0f69f 2018-08-18 stsp log_tree_entry(struct tog_view **new_view, struct tog_view *parent_view,
2790 e5a0f69f 2018-08-18 stsp struct got_tree_entry *te, struct tog_parent_trees *parents,
2791 e5a0f69f 2018-08-18 stsp struct got_object_id *commit_id, struct got_repository *repo)
2793 e5a0f69f 2018-08-18 stsp struct tog_view *log_view;
2794 69efd4c4 2018-07-18 stsp const struct got_error *err = NULL;
2795 69efd4c4 2018-07-18 stsp char *path;
2797 e5a0f69f 2018-08-18 stsp log_view = view_open(0, 0, 0, 0, parent_view, TOG_VIEW_LOG);
2798 e5a0f69f 2018-08-18 stsp if (log_view == NULL)
2799 e5a0f69f 2018-08-18 stsp return got_error_from_errno();
2801 69efd4c4 2018-07-18 stsp err = tree_entry_path(&path, parents, te);
2803 69efd4c4 2018-07-18 stsp return err;
2805 e5a0f69f 2018-08-18 stsp err = open_log_view(log_view, commit_id, repo, path);
2807 e5a0f69f 2018-08-18 stsp view_close(log_view);
2809 e5a0f69f 2018-08-18 stsp *new_view = log_view;
2810 cb2ebc8a 2018-06-23 stsp free(path);
2811 cb2ebc8a 2018-06-23 stsp return err;
2814 ffd1d5e5 2018-06-23 stsp static const struct got_error *
2815 ad80ab7b 2018-08-04 stsp open_tree_view(struct tog_view *view, struct got_tree_object *root,
2816 5221c383 2018-08-01 stsp struct got_object_id *commit_id, struct got_repository *repo)
2818 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
2819 ad80ab7b 2018-08-04 stsp char *commit_id_str = NULL;
2820 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
2822 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->parents);
2824 ffd1d5e5 2018-06-23 stsp err = got_object_id_str(&commit_id_str, commit_id);
2825 ffd1d5e5 2018-06-23 stsp if (err != NULL)
2826 ffd1d5e5 2018-06-23 stsp goto done;
2828 fb2756b9 2018-08-04 stsp if (asprintf(&s->tree_label, "commit: %s", commit_id_str) == -1) {
2829 ffd1d5e5 2018-06-23 stsp err = got_error_from_errno();
2830 ffd1d5e5 2018-06-23 stsp goto done;
2833 fb2756b9 2018-08-04 stsp s->root = s->tree = root;
2834 fb2756b9 2018-08-04 stsp s->entries = got_object_tree_get_entries(root);
2835 fb2756b9 2018-08-04 stsp s->first_displayed_entry = SIMPLEQ_FIRST(&s->entries->head);
2836 fb2756b9 2018-08-04 stsp s->commit_id = commit_id;
2837 fb2756b9 2018-08-04 stsp s->repo = repo;
2839 e5a0f69f 2018-08-18 stsp view->show = show_tree_view;
2840 e5a0f69f 2018-08-18 stsp view->input = input_tree_view;
2841 e5a0f69f 2018-08-18 stsp view->close = close_tree_view;
2843 ad80ab7b 2018-08-04 stsp free(commit_id_str);
2845 fb2756b9 2018-08-04 stsp free(s->tree_label);
2846 ad80ab7b 2018-08-04 stsp return err;
2849 e5a0f69f 2018-08-18 stsp static const struct got_error *
2850 ad80ab7b 2018-08-04 stsp close_tree_view(struct tog_view *view)
2852 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
2854 fb2756b9 2018-08-04 stsp free(s->tree_label);
2855 fb2756b9 2018-08-04 stsp while (!TAILQ_EMPTY(&s->parents)) {
2856 ad80ab7b 2018-08-04 stsp struct tog_parent_tree *parent;
2857 fb2756b9 2018-08-04 stsp parent = TAILQ_FIRST(&s->parents);
2858 fb2756b9 2018-08-04 stsp TAILQ_REMOVE(&s->parents, parent, entry);
2859 ad80ab7b 2018-08-04 stsp free(parent);
2862 fb2756b9 2018-08-04 stsp if (s->tree != s->root)
2863 fb2756b9 2018-08-04 stsp got_object_tree_close(s->tree);
2864 e5a0f69f 2018-08-18 stsp got_object_tree_close(s->root);
2866 e5a0f69f 2018-08-18 stsp return NULL;
2869 ad80ab7b 2018-08-04 stsp static const struct got_error *
2870 ad80ab7b 2018-08-04 stsp show_tree_view(struct tog_view *view)
2872 ad80ab7b 2018-08-04 stsp const struct got_error *err = NULL;
2873 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
2874 e5a0f69f 2018-08-18 stsp char *parent_path;
2876 e5a0f69f 2018-08-18 stsp err = tree_entry_path(&parent_path, &s->parents, NULL);
2878 e5a0f69f 2018-08-18 stsp return err;
2880 e5a0f69f 2018-08-18 stsp err = draw_tree_entries(view, &s->first_displayed_entry,
2881 e5a0f69f 2018-08-18 stsp &s->last_displayed_entry, &s->selected_entry,
2882 e5a0f69f 2018-08-18 stsp &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
2883 e5a0f69f 2018-08-18 stsp s->entries, s->selected, view->nlines, s->tree == s->root);
2884 e5a0f69f 2018-08-18 stsp free(parent_path);
2885 e5a0f69f 2018-08-18 stsp return err;
2888 e5a0f69f 2018-08-18 stsp static const struct got_error *
2889 e5a0f69f 2018-08-18 stsp input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
2890 e5a0f69f 2018-08-18 stsp struct tog_view *view, int ch)
2892 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
2893 e5a0f69f 2018-08-18 stsp struct tog_tree_view_state *s = &view->state.tree;
2895 e5a0f69f 2018-08-18 stsp switch (ch) {
2897 e5a0f69f 2018-08-18 stsp s->show_ids = !s->show_ids;
2900 e5a0f69f 2018-08-18 stsp if (s->selected_entry) {
2901 e5a0f69f 2018-08-18 stsp err = log_tree_entry(new_view, view,
2902 e5a0f69f 2018-08-18 stsp s->selected_entry, &s->parents,
2903 e5a0f69f 2018-08-18 stsp s->commit_id, s->repo);
2907 e5a0f69f 2018-08-18 stsp case KEY_UP:
2908 e5a0f69f 2018-08-18 stsp if (s->selected > 0)
2909 e5a0f69f 2018-08-18 stsp s->selected--;
2910 e5a0f69f 2018-08-18 stsp if (s->selected > 0)
2912 e5a0f69f 2018-08-18 stsp tree_scroll_up(&s->first_displayed_entry, 1,
2913 e5a0f69f 2018-08-18 stsp s->entries, s->tree == s->root);
2915 e5a0f69f 2018-08-18 stsp case KEY_PPAGE:
2916 e5a0f69f 2018-08-18 stsp if (SIMPLEQ_FIRST(&s->entries->head) ==
2917 e5a0f69f 2018-08-18 stsp s->first_displayed_entry) {
2918 e5a0f69f 2018-08-18 stsp if (s->tree != s->root)
2919 e5a0f69f 2018-08-18 stsp s->first_displayed_entry = NULL;
2920 e5a0f69f 2018-08-18 stsp s->selected = 0;
2923 e5a0f69f 2018-08-18 stsp tree_scroll_up(&s->first_displayed_entry,
2924 e5a0f69f 2018-08-18 stsp view->nlines, s->entries,
2925 e5a0f69f 2018-08-18 stsp s->tree == s->root);
2928 e5a0f69f 2018-08-18 stsp case KEY_DOWN:
2929 e5a0f69f 2018-08-18 stsp if (s->selected < s->ndisplayed - 1) {
2930 e5a0f69f 2018-08-18 stsp s->selected++;
2933 e5a0f69f 2018-08-18 stsp tree_scroll_down(&s->first_displayed_entry, 1,
2934 e5a0f69f 2018-08-18 stsp s->last_displayed_entry, s->entries);
2936 e5a0f69f 2018-08-18 stsp case KEY_NPAGE:
2937 e5a0f69f 2018-08-18 stsp tree_scroll_down(&s->first_displayed_entry,
2938 e5a0f69f 2018-08-18 stsp view->nlines, s->last_displayed_entry,
2939 e5a0f69f 2018-08-18 stsp s->entries);
2940 e5a0f69f 2018-08-18 stsp if (SIMPLEQ_NEXT(s->last_displayed_entry,
2943 e5a0f69f 2018-08-18 stsp /* can't scroll any further; move cursor down */
2944 e5a0f69f 2018-08-18 stsp if (s->selected < s->ndisplayed - 1)
2945 e5a0f69f 2018-08-18 stsp s->selected = s->ndisplayed - 1;
2947 e5a0f69f 2018-08-18 stsp case KEY_ENTER:
2948 e5a0f69f 2018-08-18 stsp case '\r':
2949 e5a0f69f 2018-08-18 stsp if (s->selected_entry == NULL) {
2950 e5a0f69f 2018-08-18 stsp struct tog_parent_tree *parent;
2951 31607d6c 2018-08-18 stsp case KEY_LEFT:
2952 e5a0f69f 2018-08-18 stsp /* user selected '..' */
2953 e5a0f69f 2018-08-18 stsp if (s->tree == s->root)
2955 e5a0f69f 2018-08-18 stsp parent = TAILQ_FIRST(&s->parents);
2956 e5a0f69f 2018-08-18 stsp TAILQ_REMOVE(&s->parents, parent,
2958 e5a0f69f 2018-08-18 stsp got_object_tree_close(s->tree);
2959 e5a0f69f 2018-08-18 stsp s->tree = parent->tree;
2960 e5a0f69f 2018-08-18 stsp s->entries =
2961 e5a0f69f 2018-08-18 stsp got_object_tree_get_entries(s->tree);
2962 e5a0f69f 2018-08-18 stsp s->first_displayed_entry =
2963 e5a0f69f 2018-08-18 stsp parent->first_displayed_entry;
2964 e5a0f69f 2018-08-18 stsp s->selected_entry =
2965 e5a0f69f 2018-08-18 stsp parent->selected_entry;
2966 e5a0f69f 2018-08-18 stsp s->selected = parent->selected;
2967 e5a0f69f 2018-08-18 stsp free(parent);
2968 e5a0f69f 2018-08-18 stsp } else if (S_ISDIR(s->selected_entry->mode)) {
2969 e5a0f69f 2018-08-18 stsp struct tog_parent_tree *parent;
2970 e5a0f69f 2018-08-18 stsp struct got_tree_object *child;
2971 e5a0f69f 2018-08-18 stsp err = got_object_open_as_tree(&child,
2972 e5a0f69f 2018-08-18 stsp s->repo, s->selected_entry->id);
2975 e5a0f69f 2018-08-18 stsp parent = calloc(1, sizeof(*parent));
2976 e5a0f69f 2018-08-18 stsp if (parent == NULL) {
2977 e5a0f69f 2018-08-18 stsp err = got_error_from_errno();
2980 e5a0f69f 2018-08-18 stsp parent->tree = s->tree;
2981 e5a0f69f 2018-08-18 stsp parent->first_displayed_entry =
2982 e5a0f69f 2018-08-18 stsp s->first_displayed_entry;
2983 e5a0f69f 2018-08-18 stsp parent->selected_entry = s->selected_entry;
2984 e5a0f69f 2018-08-18 stsp parent->selected = s->selected;
2985 e5a0f69f 2018-08-18 stsp TAILQ_INSERT_HEAD(&s->parents, parent, entry);
2986 e5a0f69f 2018-08-18 stsp s->tree = child;
2987 e5a0f69f 2018-08-18 stsp s->entries =
2988 e5a0f69f 2018-08-18 stsp got_object_tree_get_entries(s->tree);
2989 e5a0f69f 2018-08-18 stsp s->selected = 0;
2990 e5a0f69f 2018-08-18 stsp s->first_displayed_entry = NULL;
2991 e5a0f69f 2018-08-18 stsp } else if (S_ISREG(s->selected_entry->mode)) {
2992 e5a0f69f 2018-08-18 stsp err = blame_tree_entry(new_view, view,
2993 e5a0f69f 2018-08-18 stsp s->selected_entry, &s->parents,
2994 e5a0f69f 2018-08-18 stsp s->commit_id, s->repo);
2999 e5a0f69f 2018-08-18 stsp case KEY_RESIZE:
3000 e5a0f69f 2018-08-18 stsp if (s->selected > view->nlines)
3001 e5a0f69f 2018-08-18 stsp s->selected = s->ndisplayed - 1;
3007 ffd1d5e5 2018-06-23 stsp return err;
3010 ffd1d5e5 2018-06-23 stsp __dead static void
3011 ffd1d5e5 2018-06-23 stsp usage_tree(void)
3014 ffd1d5e5 2018-06-23 stsp fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
3015 ffd1d5e5 2018-06-23 stsp getprogname());
3019 ffd1d5e5 2018-06-23 stsp static const struct got_error *
3020 ffd1d5e5 2018-06-23 stsp cmd_tree(int argc, char *argv[])
3022 ffd1d5e5 2018-06-23 stsp const struct got_error *error;
3023 ffd1d5e5 2018-06-23 stsp struct got_repository *repo = NULL;
3024 ffd1d5e5 2018-06-23 stsp char *repo_path = NULL;
3025 ffd1d5e5 2018-06-23 stsp struct got_object_id *commit_id = NULL;
3026 ffd1d5e5 2018-06-23 stsp char *commit_id_arg = NULL;
3027 ffd1d5e5 2018-06-23 stsp struct got_commit_object *commit = NULL;
3028 ffd1d5e5 2018-06-23 stsp struct got_tree_object *tree = NULL;
3030 5221c383 2018-08-01 stsp struct tog_view *view;
3032 ffd1d5e5 2018-06-23 stsp #ifndef PROFILE
3033 ad242220 2018-09-08 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd", NULL)
3035 ffd1d5e5 2018-06-23 stsp err(1, "pledge");
3038 ffd1d5e5 2018-06-23 stsp while ((ch = getopt(argc, argv, "c:")) != -1) {
3039 ffd1d5e5 2018-06-23 stsp switch (ch) {
3041 ffd1d5e5 2018-06-23 stsp commit_id_arg = optarg;
3045 ffd1d5e5 2018-06-23 stsp /* NOTREACHED */
3049 ffd1d5e5 2018-06-23 stsp argc -= optind;
3050 ffd1d5e5 2018-06-23 stsp argv += optind;
3052 ffd1d5e5 2018-06-23 stsp if (argc == 0) {
3053 ffd1d5e5 2018-06-23 stsp repo_path = getcwd(NULL, 0);
3054 ffd1d5e5 2018-06-23 stsp if (repo_path == NULL)
3055 ffd1d5e5 2018-06-23 stsp return got_error_from_errno();
3056 ffd1d5e5 2018-06-23 stsp } else if (argc == 1) {
3057 ffd1d5e5 2018-06-23 stsp repo_path = realpath(argv[0], NULL);
3058 ffd1d5e5 2018-06-23 stsp if (repo_path == NULL)
3059 ffd1d5e5 2018-06-23 stsp return got_error_from_errno();
3061 ffd1d5e5 2018-06-23 stsp usage_log();
3063 ffd1d5e5 2018-06-23 stsp error = got_repo_open(&repo, repo_path);
3064 ffd1d5e5 2018-06-23 stsp free(repo_path);
3065 ffd1d5e5 2018-06-23 stsp if (error != NULL)
3066 ffd1d5e5 2018-06-23 stsp return error;
3068 ffd1d5e5 2018-06-23 stsp if (commit_id_arg == NULL) {
3069 ffd1d5e5 2018-06-23 stsp error = get_head_commit_id(&commit_id, repo);
3070 ffd1d5e5 2018-06-23 stsp if (error != NULL)
3071 ffd1d5e5 2018-06-23 stsp goto done;
3073 ffd1d5e5 2018-06-23 stsp struct got_object *obj;
3074 ffd1d5e5 2018-06-23 stsp error = got_object_open_by_id_str(&obj, repo, commit_id_arg);
3075 ffd1d5e5 2018-06-23 stsp if (error == NULL) {
3076 6402fb3c 2018-09-15 stsp commit_id = got_object_id_dup(got_object_get_id(obj));
3077 ffd1d5e5 2018-06-23 stsp if (commit_id == NULL)
3078 ffd1d5e5 2018-06-23 stsp error = got_error_from_errno();
3081 ffd1d5e5 2018-06-23 stsp if (error != NULL)
3082 ffd1d5e5 2018-06-23 stsp goto done;
3084 ffd1d5e5 2018-06-23 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
3085 ffd1d5e5 2018-06-23 stsp if (error != NULL)
3086 ffd1d5e5 2018-06-23 stsp goto done;
3088 ffd1d5e5 2018-06-23 stsp error = got_object_open_as_tree(&tree, repo, commit->tree_id);
3089 ffd1d5e5 2018-06-23 stsp if (error != NULL)
3090 ffd1d5e5 2018-06-23 stsp goto done;
3092 b3665f43 2018-08-04 stsp view = view_open(0, 0, 0, 0, NULL, TOG_VIEW_TREE);
3093 5221c383 2018-08-01 stsp if (view == NULL) {
3094 5221c383 2018-08-01 stsp error = got_error_from_errno();
3095 5221c383 2018-08-01 stsp goto done;
3097 ad80ab7b 2018-08-04 stsp error = open_tree_view(view, tree, commit_id, repo);
3098 ad80ab7b 2018-08-04 stsp if (error)
3099 ad80ab7b 2018-08-04 stsp goto done;
3100 e5a0f69f 2018-08-18 stsp error = view_loop(view);
3102 ffd1d5e5 2018-06-23 stsp free(commit_id);
3103 ffd1d5e5 2018-06-23 stsp if (commit)
3104 ffd1d5e5 2018-06-23 stsp got_object_commit_close(commit);
3106 ffd1d5e5 2018-06-23 stsp got_object_tree_close(tree);
3108 ffd1d5e5 2018-06-23 stsp got_repo_close(repo);
3109 ffd1d5e5 2018-06-23 stsp return error;
3111 5c5136c5 2018-05-20 stsp static void
3112 9f7d7167 2018-04-29 stsp init_curses(void)
3114 9f7d7167 2018-04-29 stsp initscr();
3118 9f7d7167 2018-04-29 stsp intrflush(stdscr, FALSE);
3119 9f7d7167 2018-04-29 stsp keypad(stdscr, TRUE);
3120 1f475ad8 2018-05-10 stsp curs_set(0);
3123 4ed7e80c 2018-05-20 stsp __dead static void
3124 9f7d7167 2018-04-29 stsp usage(void)
3128 c2301be8 2018-04-30 stsp fprintf(stderr, "usage: %s [-h] [command] [arg ...]\n\n"
3129 9f7d7167 2018-04-29 stsp "Available commands:\n", getprogname());
3130 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
3131 9f7d7167 2018-04-29 stsp struct tog_cmd *cmd = &tog_commands[i];
3132 c2301be8 2018-04-30 stsp fprintf(stderr, " %s: %s\n", cmd->name, cmd->descr);
3137 c2301be8 2018-04-30 stsp static char **
3138 c2301be8 2018-04-30 stsp make_argv(const char *arg0, const char *arg1)
3140 c2301be8 2018-04-30 stsp char **argv;
3141 c2301be8 2018-04-30 stsp int argc = (arg1 == NULL ? 1 : 2);
3143 c2301be8 2018-04-30 stsp argv = calloc(argc, sizeof(char *));
3144 c2301be8 2018-04-30 stsp if (argv == NULL)
3145 c2301be8 2018-04-30 stsp err(1, "calloc");
3146 c2301be8 2018-04-30 stsp argv[0] = strdup(arg0);
3147 c2301be8 2018-04-30 stsp if (argv[0] == NULL)
3148 c2301be8 2018-04-30 stsp err(1, "calloc");
3149 c2301be8 2018-04-30 stsp if (arg1) {
3150 c2301be8 2018-04-30 stsp argv[1] = strdup(arg1);
3151 c2301be8 2018-04-30 stsp if (argv[1] == NULL)
3152 c2301be8 2018-04-30 stsp err(1, "calloc");
3155 c2301be8 2018-04-30 stsp return argv;
3159 9f7d7167 2018-04-29 stsp main(int argc, char *argv[])
3161 9f7d7167 2018-04-29 stsp const struct got_error *error = NULL;
3162 9f7d7167 2018-04-29 stsp struct tog_cmd *cmd = NULL;
3163 9f7d7167 2018-04-29 stsp int ch, hflag = 0;
3164 c2301be8 2018-04-30 stsp char **cmd_argv = NULL;
3166 9f7d7167 2018-04-29 stsp setlocale(LC_ALL, "");
3168 9f7d7167 2018-04-29 stsp while ((ch = getopt(argc, argv, "h")) != -1) {
3169 9f7d7167 2018-04-29 stsp switch (ch) {
3171 9f7d7167 2018-04-29 stsp hflag = 1;
3175 9f7d7167 2018-04-29 stsp /* NOTREACHED */
3179 9f7d7167 2018-04-29 stsp argc -= optind;
3180 9f7d7167 2018-04-29 stsp argv += optind;
3181 9f7d7167 2018-04-29 stsp optind = 0;
3182 c2301be8 2018-04-30 stsp optreset = 1;
3184 c2301be8 2018-04-30 stsp if (argc == 0) {
3185 f29d3e89 2018-06-23 stsp if (hflag)
3187 c2301be8 2018-04-30 stsp /* Build an argument vector which runs a default command. */
3188 9f7d7167 2018-04-29 stsp cmd = &tog_commands[0];
3189 c2301be8 2018-04-30 stsp cmd_argv = make_argv(cmd->name, NULL);
3194 c2301be8 2018-04-30 stsp /* Did the user specific a command? */
3195 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
3196 c2301be8 2018-04-30 stsp if (strncmp(tog_commands[i].name, argv[0],
3197 9f7d7167 2018-04-29 stsp strlen(argv[0])) == 0) {
3198 9f7d7167 2018-04-29 stsp cmd = &tog_commands[i];
3199 9f7d7167 2018-04-29 stsp if (hflag)
3200 9f7d7167 2018-04-29 stsp tog_commands[i].cmd_usage();
3204 9f7d7167 2018-04-29 stsp if (cmd == NULL) {
3205 c2301be8 2018-04-30 stsp /* Did the user specify a repository? */
3206 c2301be8 2018-04-30 stsp char *repo_path = realpath(argv[0], NULL);
3207 c2301be8 2018-04-30 stsp if (repo_path) {
3208 c2301be8 2018-04-30 stsp struct got_repository *repo;
3209 c2301be8 2018-04-30 stsp error = got_repo_open(&repo, repo_path);
3210 c2301be8 2018-04-30 stsp if (error == NULL)
3211 c2301be8 2018-04-30 stsp got_repo_close(repo);
3213 ad7de8d9 2018-04-30 stsp error = got_error_from_errno();
3214 c2301be8 2018-04-30 stsp if (error) {
3215 f29d3e89 2018-06-23 stsp if (hflag) {
3216 f29d3e89 2018-06-23 stsp fprintf(stderr, "%s: '%s' is not a "
3217 f29d3e89 2018-06-23 stsp "known command\n", getprogname(),
3221 ad7de8d9 2018-04-30 stsp fprintf(stderr, "%s: '%s' is neither a known "
3222 ad7de8d9 2018-04-30 stsp "command nor a path to a repository\n",
3223 c2301be8 2018-04-30 stsp getprogname(), argv[0]);
3224 ad7de8d9 2018-04-30 stsp free(repo_path);
3227 c2301be8 2018-04-30 stsp cmd = &tog_commands[0];
3228 c2301be8 2018-04-30 stsp cmd_argv = make_argv(cmd->name, repo_path);
3230 c2301be8 2018-04-30 stsp free(repo_path);
3234 5c5136c5 2018-05-20 stsp init_curses();
3236 c2301be8 2018-04-30 stsp error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
3237 9f7d7167 2018-04-29 stsp if (error)
3238 9f7d7167 2018-04-29 stsp goto done;
3241 c2301be8 2018-04-30 stsp free(cmd_argv);
3242 9f7d7167 2018-04-29 stsp if (error)
3243 9f7d7167 2018-04-29 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);