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 {
94 ad80ab7b 2018-08-04 stsp int first_displayed_line;
95 ad80ab7b 2018-08-04 stsp int last_displayed_line;
99 ba4f502b 2018-08-04 stsp struct commit_queue_entry {
100 ba4f502b 2018-08-04 stsp TAILQ_ENTRY(commit_queue_entry) entry;
101 ba4f502b 2018-08-04 stsp struct got_object_id *id;
102 ba4f502b 2018-08-04 stsp struct got_commit_object *commit;
104 ba4f502b 2018-08-04 stsp TAILQ_HEAD(commit_queue_head, commit_queue_entry);
105 ba4f502b 2018-08-04 stsp struct commit_queue {
106 ba4f502b 2018-08-04 stsp int ncommits;
107 ba4f502b 2018-08-04 stsp struct commit_queue_head head;
110 b01e7d3b 2018-08-04 stsp struct tog_log_view_state {
111 b01e7d3b 2018-08-04 stsp struct got_commit_graph *graph;
112 b01e7d3b 2018-08-04 stsp struct commit_queue commits;
113 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *first_displayed_entry;
114 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *last_displayed_entry;
115 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *selected_entry;
116 b01e7d3b 2018-08-04 stsp int selected;
117 b01e7d3b 2018-08-04 stsp char *in_repo_path;
118 b01e7d3b 2018-08-04 stsp struct got_repository *repo;
121 e9424729 2018-08-04 stsp struct tog_blame_cb_args {
122 e9424729 2018-08-04 stsp pthread_mutex_t *mutex;
123 e9424729 2018-08-04 stsp struct tog_blame_line *lines; /* one per line */
124 e9424729 2018-08-04 stsp int nlines;
126 e9424729 2018-08-04 stsp struct tog_view *view;
127 e9424729 2018-08-04 stsp struct got_object_id *commit_id;
129 e9424729 2018-08-04 stsp const char *path;
130 e9424729 2018-08-04 stsp int *first_displayed_line;
131 e9424729 2018-08-04 stsp int *last_displayed_line;
132 e9424729 2018-08-04 stsp int *selected_line;
137 e9424729 2018-08-04 stsp struct tog_blame_thread_args {
138 e9424729 2018-08-04 stsp const char *path;
139 e9424729 2018-08-04 stsp struct got_repository *repo;
140 e9424729 2018-08-04 stsp struct tog_blame_cb_args *cb_args;
141 e9424729 2018-08-04 stsp int *complete;
144 e9424729 2018-08-04 stsp struct tog_blame {
146 e9424729 2018-08-04 stsp size_t filesize;
147 e9424729 2018-08-04 stsp struct tog_blame_line *lines;
148 e9424729 2018-08-04 stsp size_t nlines;
149 e9424729 2018-08-04 stsp pthread_t thread;
150 e9424729 2018-08-04 stsp struct tog_blame_thread_args thread_args;
151 e9424729 2018-08-04 stsp struct tog_blame_cb_args cb_args;
152 e9424729 2018-08-04 stsp const char *path;
155 7cbe629d 2018-08-04 stsp struct tog_blame_view_state {
156 7cbe629d 2018-08-04 stsp int first_displayed_line;
157 7cbe629d 2018-08-04 stsp int last_displayed_line;
158 7cbe629d 2018-08-04 stsp int selected_line;
159 7cbe629d 2018-08-04 stsp int blame_complete;
162 7cbe629d 2018-08-04 stsp pthread_mutex_t mutex;
163 7cbe629d 2018-08-04 stsp struct got_object_id_queue blamed_commits;
164 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
165 e5a0f69f 2018-08-18 stsp char *path;
166 7cbe629d 2018-08-04 stsp struct got_repository *repo;
167 ad80ab7b 2018-08-04 stsp struct got_object_id *commit_id;
168 e9424729 2018-08-04 stsp struct tog_blame blame;
171 ad80ab7b 2018-08-04 stsp struct tog_parent_tree {
172 ad80ab7b 2018-08-04 stsp TAILQ_ENTRY(tog_parent_tree) entry;
173 ad80ab7b 2018-08-04 stsp struct got_tree_object *tree;
174 ad80ab7b 2018-08-04 stsp struct got_tree_entry *first_displayed_entry;
175 ad80ab7b 2018-08-04 stsp struct got_tree_entry *selected_entry;
176 ad80ab7b 2018-08-04 stsp int selected;
179 ad80ab7b 2018-08-04 stsp TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
181 ad80ab7b 2018-08-04 stsp struct tog_tree_view_state {
182 ad80ab7b 2018-08-04 stsp char *tree_label;
183 ad80ab7b 2018-08-04 stsp struct got_tree_object *root;
184 ad80ab7b 2018-08-04 stsp struct got_tree_object *tree;
185 ad80ab7b 2018-08-04 stsp const struct got_tree_entries *entries;
186 ad80ab7b 2018-08-04 stsp struct got_tree_entry *first_displayed_entry;
187 ad80ab7b 2018-08-04 stsp struct got_tree_entry *last_displayed_entry;
188 ad80ab7b 2018-08-04 stsp struct got_tree_entry *selected_entry;
189 ad80ab7b 2018-08-04 stsp int nentries, ndisplayed, selected, show_ids;
190 ad80ab7b 2018-08-04 stsp struct tog_parent_trees parents;
191 7cbe629d 2018-08-04 stsp struct got_object_id *commit_id;
192 ad80ab7b 2018-08-04 stsp struct got_repository *repo;
195 cc3c9aac 2018-08-01 stsp struct tog_view {
196 e5a0f69f 2018-08-18 stsp TAILQ_ENTRY(tog_view) entry;
197 26ed57b2 2018-05-19 stsp WINDOW *window;
198 26ed57b2 2018-05-19 stsp PANEL *panel;
199 97ddc146 2018-08-01 stsp int nlines, ncols, begin_y, begin_x;
200 f7d12f7e 2018-08-01 stsp int lines, cols; /* copies of LINES and COLS */
201 6d0fee91 2018-08-01 stsp struct tog_view *parent;
203 5dc9f4bc 2018-08-04 stsp /* type-specific state */
204 d6b05b5b 2018-08-04 stsp enum tog_view_type type;
206 b01e7d3b 2018-08-04 stsp struct tog_diff_view_state diff;
207 b01e7d3b 2018-08-04 stsp struct tog_log_view_state log;
208 7cbe629d 2018-08-04 stsp struct tog_blame_view_state blame;
209 ad80ab7b 2018-08-04 stsp struct tog_tree_view_state tree;
212 e5a0f69f 2018-08-18 stsp const struct got_error *(*show)(struct tog_view *);
213 e5a0f69f 2018-08-18 stsp const struct got_error *(*input)(struct tog_view **,
214 e5a0f69f 2018-08-18 stsp struct tog_view **, struct tog_view *, int);
215 e5a0f69f 2018-08-18 stsp const struct got_error *(*close)(struct tog_view *);
217 e5a0f69f 2018-08-18 stsp TAILQ_HEAD(tog_view_list_head, tog_view);
219 ba4f502b 2018-08-04 stsp static const struct got_error *open_diff_view(struct tog_view *,
220 ba4f502b 2018-08-04 stsp struct got_object *, struct got_object *, struct got_repository *);
221 5dc9f4bc 2018-08-04 stsp static const struct got_error *show_diff_view(struct tog_view *);
222 e5a0f69f 2018-08-18 stsp static const struct got_error *input_diff_view(struct tog_view **,
223 e5a0f69f 2018-08-18 stsp struct tog_view **, struct tog_view *, int);
224 e5a0f69f 2018-08-18 stsp static const struct got_error* close_diff_view(struct tog_view *);
226 ba4f502b 2018-08-04 stsp static const struct got_error *open_log_view(struct tog_view *,
227 ba4f502b 2018-08-04 stsp struct got_object_id *, struct got_repository *, const char *);
228 ba4f502b 2018-08-04 stsp static const struct got_error * show_log_view(struct tog_view *);
229 e5a0f69f 2018-08-18 stsp static const struct got_error *input_log_view(struct tog_view **,
230 e5a0f69f 2018-08-18 stsp struct tog_view **, struct tog_view *, int);
231 e5a0f69f 2018-08-18 stsp static const struct got_error *close_log_view(struct tog_view *);
233 e5a0f69f 2018-08-18 stsp static const struct got_error *open_blame_view(struct tog_view *, char *,
234 5221c383 2018-08-01 stsp struct got_object_id *, struct got_repository *);
235 7cbe629d 2018-08-04 stsp static const struct got_error *show_blame_view(struct tog_view *);
236 e5a0f69f 2018-08-18 stsp static const struct got_error *input_blame_view(struct tog_view **,
237 e5a0f69f 2018-08-18 stsp struct tog_view **, struct tog_view *, int);
238 e5a0f69f 2018-08-18 stsp static const struct got_error *close_blame_view(struct tog_view *);
240 ad80ab7b 2018-08-04 stsp static const struct got_error *open_tree_view(struct tog_view *,
241 4fc679ca 2018-08-04 stsp struct got_tree_object *, struct got_object_id *, struct got_repository *);
242 ad80ab7b 2018-08-04 stsp static const struct got_error *show_tree_view(struct tog_view *);
243 e5a0f69f 2018-08-18 stsp static const struct got_error *input_tree_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_tree_view(struct tog_view *);
247 e5a0f69f 2018-08-18 stsp static const struct got_error *
248 96a765a8 2018-08-04 stsp view_close(struct tog_view *view)
250 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
252 e5a0f69f 2018-08-18 stsp if (view->close)
253 e5a0f69f 2018-08-18 stsp err = view->close(view);
254 ea5e7bb5 2018-08-01 stsp if (view->panel)
255 ea5e7bb5 2018-08-01 stsp del_panel(view->panel);
256 ea5e7bb5 2018-08-01 stsp if (view->window)
257 ea5e7bb5 2018-08-01 stsp delwin(view->window);
258 ea5e7bb5 2018-08-01 stsp free(view);
259 e5a0f69f 2018-08-18 stsp return err;
262 ea5e7bb5 2018-08-01 stsp static struct tog_view *
263 b3665f43 2018-08-04 stsp view_open(int nlines, int ncols, int begin_y, int begin_x,
264 d6b05b5b 2018-08-04 stsp struct tog_view *parent, enum tog_view_type type)
266 ad80ab7b 2018-08-04 stsp struct tog_view *view = calloc(1, sizeof(*view));
268 ea5e7bb5 2018-08-01 stsp if (view == NULL)
269 ea5e7bb5 2018-08-01 stsp return NULL;
271 6d0fee91 2018-08-01 stsp view->parent = parent;
272 d6b05b5b 2018-08-04 stsp view->type = type;
273 f7d12f7e 2018-08-01 stsp view->lines = LINES;
274 f7d12f7e 2018-08-01 stsp view->cols = COLS;
275 207b9029 2018-08-01 stsp view->nlines = nlines ? nlines : LINES - begin_y;
276 207b9029 2018-08-01 stsp view->ncols = ncols ? ncols : COLS - begin_x;
277 97ddc146 2018-08-01 stsp view->begin_y = begin_y;
278 97ddc146 2018-08-01 stsp view->begin_x = begin_x;
279 842167bf 2018-08-01 stsp view->window = newwin(nlines, ncols, begin_y, begin_x);
280 ea5e7bb5 2018-08-01 stsp if (view->window == NULL) {
281 96a765a8 2018-08-04 stsp view_close(view);
282 ea5e7bb5 2018-08-01 stsp return NULL;
284 ea5e7bb5 2018-08-01 stsp view->panel = new_panel(view->window);
285 ea5e7bb5 2018-08-01 stsp if (view->panel == NULL) {
286 96a765a8 2018-08-04 stsp view_close(view);
287 ea5e7bb5 2018-08-01 stsp return NULL;
290 ea5e7bb5 2018-08-01 stsp keypad(view->window, TRUE);
291 ea5e7bb5 2018-08-01 stsp return view;
294 e5a0f69f 2018-08-18 stsp const struct got_error *
295 6acd1bd1 2018-08-04 stsp view_show(struct tog_view *view)
297 e5a0f69f 2018-08-18 stsp const struct got_error *err;
299 e5a0f69f 2018-08-18 stsp err = view->show(view);
301 e5a0f69f 2018-08-18 stsp return err;
302 cdf1ee82 2018-08-01 stsp show_panel(view->panel);
303 cdf1ee82 2018-08-01 stsp update_panels();
304 e5a0f69f 2018-08-18 stsp doupdate();
306 e5a0f69f 2018-08-18 stsp return err;
309 f7d12f7e 2018-08-01 stsp const struct got_error *
310 f7d12f7e 2018-08-01 stsp view_resize(struct tog_view *view)
312 f7d12f7e 2018-08-01 stsp int nlines, ncols;
314 6d0fee91 2018-08-01 stsp while (view) {
315 6d0fee91 2018-08-01 stsp if (view->lines > LINES)
316 6d0fee91 2018-08-01 stsp nlines = view->nlines - (view->lines - LINES);
318 6d0fee91 2018-08-01 stsp nlines = view->nlines + (LINES - view->lines);
320 6d0fee91 2018-08-01 stsp if (view->cols > COLS)
321 6d0fee91 2018-08-01 stsp ncols = view->ncols - (view->cols - COLS);
323 6d0fee91 2018-08-01 stsp ncols = view->ncols + (COLS - view->cols);
325 6d0fee91 2018-08-01 stsp if (wresize(view->window, nlines, ncols) == ERR)
326 6d0fee91 2018-08-01 stsp return got_error_from_errno();
327 77a567dd 2018-08-01 stsp replace_panel(view->panel, view->window);
329 6d0fee91 2018-08-01 stsp view->nlines = nlines;
330 6d0fee91 2018-08-01 stsp view->ncols = ncols;
331 6d0fee91 2018-08-01 stsp view->lines = LINES;
332 6d0fee91 2018-08-01 stsp view->cols = COLS;
334 6d0fee91 2018-08-01 stsp view = view->parent;
337 f7d12f7e 2018-08-01 stsp return NULL;
340 e5a0f69f 2018-08-18 stsp static const struct got_error *
341 48fcc512 2018-08-18 stsp view_input(struct tog_view **new, struct tog_view **dead,
342 e4197bf9 2018-08-18 stsp struct tog_view **focus, int *done, struct tog_view *view,
343 48fcc512 2018-08-18 stsp struct tog_view_list_head *views)
345 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
346 31607d6c 2018-08-18 stsp struct tog_view *next, *prev;
349 e5a0f69f 2018-08-18 stsp *new = NULL;
350 e5a0f69f 2018-08-18 stsp *dead = NULL;
352 e5a0f69f 2018-08-18 stsp nodelay(stdscr, FALSE);
353 e5a0f69f 2018-08-18 stsp ch = wgetch(view->window);
354 e5a0f69f 2018-08-18 stsp nodelay(stdscr, TRUE);
355 e5a0f69f 2018-08-18 stsp switch (ch) {
359 48fcc512 2018-08-18 stsp next = TAILQ_NEXT(view, entry);
361 48fcc512 2018-08-18 stsp *focus = next;
363 48fcc512 2018-08-18 stsp *focus = TAILQ_FIRST(views);
365 31607d6c 2018-08-18 stsp case KEY_BACKSPACE:
366 31607d6c 2018-08-18 stsp prev = TAILQ_PREV(view, tog_view_list_head, entry);
368 31607d6c 2018-08-18 stsp *focus = prev;
370 31607d6c 2018-08-18 stsp *focus = TAILQ_LAST(views, tog_view_list_head);
373 e5a0f69f 2018-08-18 stsp err = view->input(new, dead, view, ch);
374 e5a0f69f 2018-08-18 stsp *dead = view;
379 e5a0f69f 2018-08-18 stsp case KEY_RESIZE:
380 e5a0f69f 2018-08-18 stsp err = view_resize(view);
382 e5a0f69f 2018-08-18 stsp return err;
383 e5a0f69f 2018-08-18 stsp err = view->input(new, dead, view, ch);
386 e5a0f69f 2018-08-18 stsp err = view->input(new, dead, view, ch);
390 e5a0f69f 2018-08-18 stsp return err;
393 e5a0f69f 2018-08-18 stsp static const struct got_error *
394 e5a0f69f 2018-08-18 stsp view_loop(struct tog_view *view)
396 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
397 e5a0f69f 2018-08-18 stsp struct tog_view_list_head views;
398 e5a0f69f 2018-08-18 stsp struct tog_view *new_view, *dead_view;
399 e4197bf9 2018-08-18 stsp int done = 0;
401 e5a0f69f 2018-08-18 stsp TAILQ_INIT(&views);
402 e5a0f69f 2018-08-18 stsp TAILQ_INSERT_HEAD(&views, view, entry);
404 e4197bf9 2018-08-18 stsp while (!TAILQ_EMPTY(&views) && !done) {
405 e5a0f69f 2018-08-18 stsp err = view_show(view);
408 e4197bf9 2018-08-18 stsp err = view_input(&new_view, &dead_view, &view, &done,
409 e4197bf9 2018-08-18 stsp view, &views);
412 e5a0f69f 2018-08-18 stsp if (new_view) {
413 268ae8f5 2018-08-18 stsp /* TODO: de-duplicate! */
414 e5a0f69f 2018-08-18 stsp TAILQ_INSERT_TAIL(&views, new_view, entry);
415 e5a0f69f 2018-08-18 stsp view = new_view;
417 e5a0f69f 2018-08-18 stsp if (dead_view) {
418 e5a0f69f 2018-08-18 stsp TAILQ_REMOVE(&views, dead_view, entry);
419 e5a0f69f 2018-08-18 stsp TAILQ_FOREACH(view, &views, entry) {
420 e5a0f69f 2018-08-18 stsp if (view->parent == dead_view)
421 e5a0f69f 2018-08-18 stsp view->parent = NULL;
423 e5a0f69f 2018-08-18 stsp if (dead_view->parent)
424 e5a0f69f 2018-08-18 stsp view = dead_view->parent;
426 e5a0f69f 2018-08-18 stsp view = TAILQ_LAST(&views, tog_view_list_head);
427 e5a0f69f 2018-08-18 stsp err = view_close(dead_view);
433 e5a0f69f 2018-08-18 stsp while (!TAILQ_EMPTY(&views)) {
434 e5a0f69f 2018-08-18 stsp view = TAILQ_FIRST(&views);
435 e5a0f69f 2018-08-18 stsp TAILQ_REMOVE(&views, view, entry);
436 e5a0f69f 2018-08-18 stsp view_close(view);
438 e5a0f69f 2018-08-18 stsp return err;
441 4ed7e80c 2018-05-20 stsp __dead static void
442 9f7d7167 2018-04-29 stsp usage_log(void)
445 c70c5802 2018-08-01 stsp fprintf(stderr,
446 c70c5802 2018-08-01 stsp "usage: %s log [-c commit] [-r repository-path] [path]\n",
447 9f7d7167 2018-04-29 stsp getprogname());
451 963b370f 2018-05-20 stsp /* Create newly allocated wide-character string equivalent to a byte string. */
452 80ddbec8 2018-04-29 stsp static const struct got_error *
453 963b370f 2018-05-20 stsp mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
455 00dfcb92 2018-06-11 stsp char *vis = NULL;
456 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
458 963b370f 2018-05-20 stsp *ws = NULL;
459 963b370f 2018-05-20 stsp *wlen = mbstowcs(NULL, s, 0);
460 00dfcb92 2018-06-11 stsp if (*wlen == (size_t)-1) {
461 00dfcb92 2018-06-11 stsp int vislen;
462 00dfcb92 2018-06-11 stsp if (errno != EILSEQ)
463 00dfcb92 2018-06-11 stsp return got_error_from_errno();
465 00dfcb92 2018-06-11 stsp /* byte string invalid in current encoding; try to "fix" it */
466 00dfcb92 2018-06-11 stsp err = got_mbsavis(&vis, &vislen, s);
468 00dfcb92 2018-06-11 stsp return err;
469 00dfcb92 2018-06-11 stsp *wlen = mbstowcs(NULL, vis, 0);
470 a7f50699 2018-06-11 stsp if (*wlen == (size_t)-1) {
471 a7f50699 2018-06-11 stsp err = got_error_from_errno(); /* give up */
476 963b370f 2018-05-20 stsp *ws = calloc(*wlen + 1, sizeof(*ws));
477 a7f50699 2018-06-11 stsp if (*ws == NULL) {
478 a7f50699 2018-06-11 stsp err = got_error_from_errno();
482 00dfcb92 2018-06-11 stsp if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
483 963b370f 2018-05-20 stsp err = got_error_from_errno();
488 963b370f 2018-05-20 stsp *ws = NULL;
491 963b370f 2018-05-20 stsp return err;
494 963b370f 2018-05-20 stsp /* Format a line for display, ensuring that it won't overflow a width limit. */
495 963b370f 2018-05-20 stsp static const struct got_error *
496 ffd1d5e5 2018-06-23 stsp format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit)
498 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
499 963b370f 2018-05-20 stsp int cols = 0;
500 963b370f 2018-05-20 stsp wchar_t *wline = NULL;
501 963b370f 2018-05-20 stsp size_t wlen;
504 963b370f 2018-05-20 stsp *wlinep = NULL;
505 b700b5d6 2018-07-10 stsp *widthp = 0;
507 963b370f 2018-05-20 stsp err = mbs2ws(&wline, &wlen, line);
509 963b370f 2018-05-20 stsp return err;
512 b700b5d6 2018-07-10 stsp while (i < wlen && cols < wlimit) {
513 963b370f 2018-05-20 stsp int width = wcwidth(wline[i]);
514 963b370f 2018-05-20 stsp switch (width) {
520 b700b5d6 2018-07-10 stsp if (cols + width <= wlimit) {
521 b700b5d6 2018-07-10 stsp cols += width;
526 963b370f 2018-05-20 stsp if (wline[i] == L'\t')
527 b700b5d6 2018-07-10 stsp cols += TABSIZE - ((cols + 1) % TABSIZE);
531 963b370f 2018-05-20 stsp err = got_error_from_errno();
535 963b370f 2018-05-20 stsp wline[i] = L'\0';
536 b700b5d6 2018-07-10 stsp if (widthp)
537 b700b5d6 2018-07-10 stsp *widthp = cols;
540 963b370f 2018-05-20 stsp free(wline);
542 963b370f 2018-05-20 stsp *wlinep = wline;
543 963b370f 2018-05-20 stsp return err;
546 963b370f 2018-05-20 stsp static const struct got_error *
547 2814baeb 2018-08-01 stsp draw_commit(struct tog_view *view, struct got_commit_object *commit,
548 2814baeb 2018-08-01 stsp struct got_object_id *id)
550 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
551 b39d25c7 2018-07-10 stsp char datebuf[10]; /* YY-MM-DD + SPACE + NUL */
552 80ddbec8 2018-04-29 stsp char *logmsg0 = NULL, *logmsg = NULL;
553 6d9fbc00 2018-04-29 stsp char *author0 = NULL, *author = NULL;
554 bb737323 2018-05-20 stsp wchar_t *wlogmsg = NULL, *wauthor = NULL;
555 bb737323 2018-05-20 stsp int author_width, logmsg_width;
556 6d9fbc00 2018-04-29 stsp char *newline, *smallerthan;
557 80ddbec8 2018-04-29 stsp char *line = NULL;
558 bb737323 2018-05-20 stsp int col, limit;
559 b39d25c7 2018-07-10 stsp static const size_t date_display_cols = 9;
560 bb737323 2018-05-20 stsp static const size_t author_display_cols = 16;
561 f7d12f7e 2018-08-01 stsp const int avail = view->ncols;
563 c70c5802 2018-08-01 stsp if (strftime(datebuf, sizeof(datebuf), "%g/%m/%d ",
564 c70c5802 2018-08-01 stsp &commit->tm_committer) >= sizeof(datebuf))
565 b39d25c7 2018-07-10 stsp return got_error(GOT_ERR_NO_SPACE);
567 b39d25c7 2018-07-10 stsp if (avail < date_display_cols)
568 b39d25c7 2018-07-10 stsp limit = MIN(sizeof(datebuf) - 1, avail);
570 b39d25c7 2018-07-10 stsp limit = MIN(date_display_cols, sizeof(datebuf) - 1);
571 2814baeb 2018-08-01 stsp waddnstr(view->window, datebuf, limit);
572 b39d25c7 2018-07-10 stsp col = limit + 1;
573 b39d25c7 2018-07-10 stsp if (col > avail)
576 6d9fbc00 2018-04-29 stsp author0 = strdup(commit->author);
577 6d9fbc00 2018-04-29 stsp if (author0 == NULL) {
578 80ddbec8 2018-04-29 stsp err = got_error_from_errno();
581 6d9fbc00 2018-04-29 stsp author = author0;
582 6d9fbc00 2018-04-29 stsp smallerthan = strchr(author, '<');
583 6d9fbc00 2018-04-29 stsp if (smallerthan)
584 6d9fbc00 2018-04-29 stsp *smallerthan = '\0';
586 6d9fbc00 2018-04-29 stsp char *at = strchr(author, '@');
588 6d9fbc00 2018-04-29 stsp *at = '\0';
590 b39d25c7 2018-07-10 stsp limit = avail - col;
591 bb737323 2018-05-20 stsp err = format_line(&wauthor, &author_width, author, limit);
594 2814baeb 2018-08-01 stsp waddwstr(view->window, wauthor);
595 bb737323 2018-05-20 stsp col += author_width;
596 9c2eaf34 2018-05-20 stsp while (col <= avail && author_width < author_display_cols + 1) {
597 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
599 bb737323 2018-05-20 stsp author_width++;
601 9c2eaf34 2018-05-20 stsp if (col > avail)
604 bb737323 2018-05-20 stsp logmsg0 = strdup(commit->logmsg);
605 bb737323 2018-05-20 stsp if (logmsg0 == NULL) {
606 6d9fbc00 2018-04-29 stsp err = got_error_from_errno();
609 bb737323 2018-05-20 stsp logmsg = logmsg0;
610 bb737323 2018-05-20 stsp while (*logmsg == '\n')
612 bb737323 2018-05-20 stsp newline = strchr(logmsg, '\n');
613 bb737323 2018-05-20 stsp if (newline)
614 bb737323 2018-05-20 stsp *newline = '\0';
615 bb737323 2018-05-20 stsp limit = avail - col;
616 bb737323 2018-05-20 stsp err = format_line(&wlogmsg, &logmsg_width, logmsg, limit);
619 2814baeb 2018-08-01 stsp waddwstr(view->window, wlogmsg);
620 bb737323 2018-05-20 stsp col += logmsg_width;
621 bb737323 2018-05-20 stsp while (col <= avail) {
622 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
626 80ddbec8 2018-04-29 stsp free(logmsg0);
627 bb737323 2018-05-20 stsp free(wlogmsg);
628 6d9fbc00 2018-04-29 stsp free(author0);
629 bb737323 2018-05-20 stsp free(wauthor);
630 80ddbec8 2018-04-29 stsp free(line);
631 80ddbec8 2018-04-29 stsp return err;
634 899d86c2 2018-05-10 stsp static struct commit_queue_entry *
635 899d86c2 2018-05-10 stsp alloc_commit_queue_entry(struct got_commit_object *commit,
636 899d86c2 2018-05-10 stsp struct got_object_id *id)
638 80ddbec8 2018-04-29 stsp struct commit_queue_entry *entry;
640 80ddbec8 2018-04-29 stsp entry = calloc(1, sizeof(*entry));
641 80ddbec8 2018-04-29 stsp if (entry == NULL)
642 899d86c2 2018-05-10 stsp return NULL;
644 899d86c2 2018-05-10 stsp entry->id = id;
645 99db9666 2018-05-07 stsp entry->commit = commit;
646 899d86c2 2018-05-10 stsp return entry;
649 99db9666 2018-05-07 stsp static void
650 99db9666 2018-05-07 stsp pop_commit(struct commit_queue *commits)
652 99db9666 2018-05-07 stsp struct commit_queue_entry *entry;
654 ecb28ae0 2018-07-16 stsp entry = TAILQ_FIRST(&commits->head);
655 ecb28ae0 2018-07-16 stsp TAILQ_REMOVE(&commits->head, entry, entry);
656 99db9666 2018-05-07 stsp got_object_commit_close(entry->commit);
657 ecb28ae0 2018-07-16 stsp commits->ncommits--;
658 9ba79e04 2018-06-11 stsp /* Don't free entry->id! It is owned by the commit graph. */
659 99db9666 2018-05-07 stsp free(entry);
662 99db9666 2018-05-07 stsp static void
663 99db9666 2018-05-07 stsp free_commits(struct commit_queue *commits)
665 ecb28ae0 2018-07-16 stsp while (!TAILQ_EMPTY(&commits->head))
666 99db9666 2018-05-07 stsp pop_commit(commits);
669 c4972b91 2018-05-07 stsp static const struct got_error *
670 9ba79e04 2018-06-11 stsp queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
671 ecb28ae0 2018-07-16 stsp struct got_object_id *start_id, int minqueue, int init,
672 ecb28ae0 2018-07-16 stsp struct got_repository *repo, const char *path)
674 899d86c2 2018-05-10 stsp const struct got_error *err = NULL;
675 899d86c2 2018-05-10 stsp struct got_object_id *id;
676 9ba79e04 2018-06-11 stsp struct commit_queue_entry *entry;
677 ecb28ae0 2018-07-16 stsp int nfetched, nqueued = 0, found_obj = 0;
678 c8f60bff 2018-07-23 stsp int is_root_path = strcmp(path, "/") == 0;
680 9ba79e04 2018-06-11 stsp err = got_commit_graph_iter_start(graph, start_id);
682 c4972b91 2018-05-07 stsp return err;
684 ecb28ae0 2018-07-16 stsp entry = TAILQ_LAST(&commits->head, commit_queue_head);
685 9ba79e04 2018-06-11 stsp if (entry && got_object_id_cmp(entry->id, start_id) == 0) {
686 9ba79e04 2018-06-11 stsp int nfetched;
688 9ba79e04 2018-06-11 stsp /* Start ID's commit is already on the queue; skip over it. */
689 9ba79e04 2018-06-11 stsp err = got_commit_graph_iter_next(&id, graph);
690 9ba79e04 2018-06-11 stsp if (err && err->code != GOT_ERR_ITER_NEED_MORE)
691 9ba79e04 2018-06-11 stsp return err;
693 9ba79e04 2018-06-11 stsp err = got_commit_graph_fetch_commits(&nfetched, graph, 1, repo);
695 9ba79e04 2018-06-11 stsp return err;
698 9ba79e04 2018-06-11 stsp while (1) {
699 9ba79e04 2018-06-11 stsp struct got_commit_object *commit;
701 9ba79e04 2018-06-11 stsp err = got_commit_graph_iter_next(&id, graph);
703 ecb28ae0 2018-07-16 stsp if (err->code != GOT_ERR_ITER_NEED_MORE)
705 ecb28ae0 2018-07-16 stsp if (nqueued >= minqueue) {
706 9ba79e04 2018-06-11 stsp err = NULL;
709 ecb28ae0 2018-07-16 stsp err = got_commit_graph_fetch_commits(&nfetched,
710 ecb28ae0 2018-07-16 stsp graph, 1, repo);
712 ecb28ae0 2018-07-16 stsp return err;
715 ecb28ae0 2018-07-16 stsp if (id == NULL)
718 9ba79e04 2018-06-11 stsp err = got_object_open_as_commit(&commit, repo, id);
722 c8f60bff 2018-07-23 stsp if (!is_root_path) {
723 ecb28ae0 2018-07-16 stsp struct got_object *obj;
724 ecb28ae0 2018-07-16 stsp struct got_object_qid *pid;
725 ecb28ae0 2018-07-16 stsp int changed = 0;
727 ecb28ae0 2018-07-16 stsp err = got_object_open_by_path(&obj, repo, id, path);
729 c8f60bff 2018-07-23 stsp got_object_commit_close(commit);
730 ecb28ae0 2018-07-16 stsp if (err->code == GOT_ERR_NO_OBJ &&
731 ecb28ae0 2018-07-16 stsp (found_obj || !init)) {
732 ecb28ae0 2018-07-16 stsp /* History stops here. */
733 ecb28ae0 2018-07-16 stsp err = got_error(GOT_ERR_ITER_COMPLETED);
737 ecb28ae0 2018-07-16 stsp found_obj = 1;
739 ecb28ae0 2018-07-16 stsp pid = SIMPLEQ_FIRST(&commit->parent_ids);
740 ecb28ae0 2018-07-16 stsp if (pid != NULL) {
741 ecb28ae0 2018-07-16 stsp struct got_object *pobj;
742 ecb28ae0 2018-07-16 stsp err = got_object_open_by_path(&pobj, repo,
743 ecb28ae0 2018-07-16 stsp pid->id, path);
745 ecb28ae0 2018-07-16 stsp if (err->code != GOT_ERR_NO_OBJ) {
746 ecb28ae0 2018-07-16 stsp got_object_close(obj);
747 c8f60bff 2018-07-23 stsp got_object_commit_close(commit);
750 ecb28ae0 2018-07-16 stsp err = NULL;
751 ecb28ae0 2018-07-16 stsp changed = 1;
753 c2f16475 2018-07-23 stsp struct got_object_id *id, *pid;
754 c2f16475 2018-07-23 stsp id = got_object_get_id(obj);
755 c2f16475 2018-07-23 stsp if (id == NULL) {
756 c2f16475 2018-07-23 stsp err = got_error_from_errno();
757 c8f60bff 2018-07-23 stsp got_object_close(obj);
758 c8f60bff 2018-07-23 stsp got_object_close(pobj);
761 c2f16475 2018-07-23 stsp pid = got_object_get_id(pobj);
762 c2f16475 2018-07-23 stsp if (pid == NULL) {
763 c2f16475 2018-07-23 stsp err = got_error_from_errno();
765 c8f60bff 2018-07-23 stsp got_object_close(obj);
766 c8f60bff 2018-07-23 stsp got_object_close(pobj);
770 c2f16475 2018-07-23 stsp (got_object_id_cmp(id, pid) != 0);
771 ecb28ae0 2018-07-16 stsp got_object_close(pobj);
776 c8f60bff 2018-07-23 stsp got_object_close(obj);
777 ecb28ae0 2018-07-16 stsp if (!changed) {
778 ecb28ae0 2018-07-16 stsp got_object_commit_close(commit);
783 9ba79e04 2018-06-11 stsp entry = alloc_commit_queue_entry(commit, id);
784 9ba79e04 2018-06-11 stsp if (entry == NULL) {
785 9ba79e04 2018-06-11 stsp err = got_error_from_errno();
788 ecb28ae0 2018-07-16 stsp TAILQ_INSERT_TAIL(&commits->head, entry, entry);
790 ecb28ae0 2018-07-16 stsp commits->ncommits++;
793 9ba79e04 2018-06-11 stsp return err;
796 99db9666 2018-05-07 stsp static const struct got_error *
797 9ba79e04 2018-06-11 stsp fetch_next_commit(struct commit_queue_entry **pentry,
798 9ba79e04 2018-06-11 stsp struct commit_queue_entry *entry, struct commit_queue *commits,
799 ecb28ae0 2018-07-16 stsp struct got_commit_graph *graph, struct got_repository *repo,
800 ecb28ae0 2018-07-16 stsp const char *path)
802 899d86c2 2018-05-10 stsp const struct got_error *err = NULL;
804 9ba79e04 2018-06-11 stsp *pentry = NULL;
806 ecb28ae0 2018-07-16 stsp err = queue_commits(graph, commits, entry->id, 1, 0, repo, path);
808 9ba79e04 2018-06-11 stsp return err;
810 9ba79e04 2018-06-11 stsp /* Next entry to display should now be available. */
811 9ba79e04 2018-06-11 stsp *pentry = TAILQ_NEXT(entry, entry);
812 9ba79e04 2018-06-11 stsp if (*pentry == NULL)
813 9ba79e04 2018-06-11 stsp return got_error(GOT_ERR_NO_OBJ);
815 9ba79e04 2018-06-11 stsp return NULL;
818 899d86c2 2018-05-10 stsp static const struct got_error *
819 9ba79e04 2018-06-11 stsp get_head_commit_id(struct got_object_id **head_id, struct got_repository *repo)
821 9ba79e04 2018-06-11 stsp const struct got_error *err = NULL;
822 9ba79e04 2018-06-11 stsp struct got_reference *head_ref;
824 9ba79e04 2018-06-11 stsp *head_id = NULL;
826 9ba79e04 2018-06-11 stsp err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
828 99db9666 2018-05-07 stsp return err;
830 9ba79e04 2018-06-11 stsp err = got_ref_resolve(head_id, repo, head_ref);
831 9ba79e04 2018-06-11 stsp got_ref_close(head_ref);
833 9ba79e04 2018-06-11 stsp *head_id = NULL;
834 99db9666 2018-05-07 stsp return err;
837 9ba79e04 2018-06-11 stsp return NULL;
840 0553a4e3 2018-04-30 stsp static const struct got_error *
841 2814baeb 2018-08-01 stsp draw_commits(struct tog_view *view, struct commit_queue_entry **last,
842 ecb28ae0 2018-07-16 stsp struct commit_queue_entry **selected, struct commit_queue_entry *first,
843 a7f40148 2018-07-18 stsp struct commit_queue *commits, int selected_idx, int limit,
844 a7f40148 2018-07-18 stsp struct got_commit_graph *graph, struct got_repository *repo,
845 a7f40148 2018-07-18 stsp const char *path)
847 0553a4e3 2018-04-30 stsp const struct got_error *err = NULL;
848 0553a4e3 2018-04-30 stsp struct commit_queue_entry *entry;
849 ecb28ae0 2018-07-16 stsp int ncommits, width;
850 867c6645 2018-07-10 stsp char *id_str, *header;
851 ecb28ae0 2018-07-16 stsp wchar_t *wline;
853 e0d42f60 2018-07-22 stsp entry = first;
854 e0d42f60 2018-07-22 stsp ncommits = 0;
855 e0d42f60 2018-07-22 stsp while (entry) {
856 e0d42f60 2018-07-22 stsp if (ncommits == selected_idx) {
857 e0d42f60 2018-07-22 stsp *selected = entry;
860 e0d42f60 2018-07-22 stsp entry = TAILQ_NEXT(entry, entry);
861 e0d42f60 2018-07-22 stsp ncommits++;
864 867c6645 2018-07-10 stsp err = got_object_id_str(&id_str, (*selected)->id);
866 867c6645 2018-07-10 stsp return err;
868 c3ba6f36 2018-08-01 stsp if (path && strcmp(path, "/") != 0) {
869 ecb28ae0 2018-07-16 stsp if (asprintf(&header, "commit: %s [%s]", id_str, path) == -1) {
870 ecb28ae0 2018-07-16 stsp err = got_error_from_errno();
871 ecb28ae0 2018-07-16 stsp free(id_str);
872 ecb28ae0 2018-07-16 stsp return err;
874 ecb28ae0 2018-07-16 stsp } else if (asprintf(&header, "commit: %s", id_str) == -1) {
875 867c6645 2018-07-10 stsp err = got_error_from_errno();
876 867c6645 2018-07-10 stsp free(id_str);
877 867c6645 2018-07-10 stsp return err;
879 ecb28ae0 2018-07-16 stsp free(id_str);
880 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, header, view->ncols);
882 ecb28ae0 2018-07-16 stsp free(header);
883 ecb28ae0 2018-07-16 stsp return err;
885 ecb28ae0 2018-07-16 stsp free(header);
887 2814baeb 2018-08-01 stsp werase(view->window);
889 2814baeb 2018-08-01 stsp waddwstr(view->window, wline);
890 f7d12f7e 2018-08-01 stsp if (width < view->ncols)
891 2814baeb 2018-08-01 stsp waddch(view->window, '\n');
892 ecb28ae0 2018-07-16 stsp free(wline);
893 ecb28ae0 2018-07-16 stsp if (limit <= 1)
894 ecb28ae0 2018-07-16 stsp return NULL;
896 899d86c2 2018-05-10 stsp entry = first;
897 899d86c2 2018-05-10 stsp *last = first;
898 867c6645 2018-07-10 stsp ncommits = 0;
899 899d86c2 2018-05-10 stsp while (entry) {
900 ecb28ae0 2018-07-16 stsp if (ncommits >= limit - 1)
902 e0d42f60 2018-07-22 stsp if (ncommits == selected_idx)
903 2814baeb 2018-08-01 stsp wstandout(view->window);
904 2814baeb 2018-08-01 stsp err = draw_commit(view, entry->commit, entry->id);
905 cd0acaa7 2018-05-20 stsp if (ncommits == selected_idx)
906 2814baeb 2018-08-01 stsp wstandend(view->window);
909 0553a4e3 2018-04-30 stsp ncommits++;
910 899d86c2 2018-05-10 stsp *last = entry;
911 a7f40148 2018-07-18 stsp if (entry == TAILQ_LAST(&commits->head, commit_queue_head)) {
912 a7f40148 2018-07-18 stsp err = queue_commits(graph, commits, entry->id, 1,
913 a7f40148 2018-07-18 stsp 0, repo, path);
915 a7f40148 2018-07-18 stsp if (err->code != GOT_ERR_ITER_COMPLETED)
916 a7f40148 2018-07-18 stsp return err;
917 a7f40148 2018-07-18 stsp err = NULL;
920 899d86c2 2018-05-10 stsp entry = TAILQ_NEXT(entry, entry);
923 80ddbec8 2018-04-29 stsp update_panels();
925 80ddbec8 2018-04-29 stsp return err;
928 07b55e75 2018-05-10 stsp static void
929 16482c3b 2018-05-20 stsp scroll_up(struct commit_queue_entry **first_displayed_entry, int maxscroll,
930 07b55e75 2018-05-10 stsp struct commit_queue *commits)
932 07b55e75 2018-05-10 stsp struct commit_queue_entry *entry;
933 07b55e75 2018-05-10 stsp int nscrolled = 0;
935 ecb28ae0 2018-07-16 stsp entry = TAILQ_FIRST(&commits->head);
936 07b55e75 2018-05-10 stsp if (*first_displayed_entry == entry)
939 07b55e75 2018-05-10 stsp entry = *first_displayed_entry;
940 16482c3b 2018-05-20 stsp while (entry && nscrolled < maxscroll) {
941 ecb28ae0 2018-07-16 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
942 07b55e75 2018-05-10 stsp if (entry) {
943 07b55e75 2018-05-10 stsp *first_displayed_entry = entry;
944 07b55e75 2018-05-10 stsp nscrolled++;
949 aa075928 2018-05-10 stsp static const struct got_error *
950 16482c3b 2018-05-20 stsp scroll_down(struct commit_queue_entry **first_displayed_entry, int maxscroll,
951 aa075928 2018-05-10 stsp struct commit_queue_entry *last_displayed_entry,
952 9ba79e04 2018-06-11 stsp struct commit_queue *commits, struct got_commit_graph *graph,
953 ecb28ae0 2018-07-16 stsp struct got_repository *repo, const char *path)
955 aa075928 2018-05-10 stsp const struct got_error *err = NULL;
956 dd0a52c1 2018-05-20 stsp struct commit_queue_entry *pentry;
957 aa075928 2018-05-10 stsp int nscrolled = 0;
960 dd0a52c1 2018-05-20 stsp pentry = TAILQ_NEXT(last_displayed_entry, entry);
961 aa075928 2018-05-10 stsp if (pentry == NULL) {
962 9ba79e04 2018-06-11 stsp err = fetch_next_commit(&pentry, last_displayed_entry,
963 ecb28ae0 2018-07-16 stsp commits, graph, repo, path);
964 9a6bf2a5 2018-05-20 stsp if (err || pentry == NULL)
967 dd0a52c1 2018-05-20 stsp last_displayed_entry = pentry;
969 dd0a52c1 2018-05-20 stsp pentry = TAILQ_NEXT(*first_displayed_entry, entry);
970 dd0a52c1 2018-05-20 stsp if (pentry == NULL)
972 aa075928 2018-05-10 stsp *first_displayed_entry = pentry;
973 16482c3b 2018-05-20 stsp } while (++nscrolled < maxscroll);
975 dd0a52c1 2018-05-20 stsp return err;
978 cd0acaa7 2018-05-20 stsp static const struct got_error *
979 e5a0f69f 2018-08-18 stsp show_commit(struct tog_view **new_view, struct tog_view *parent_view,
980 e5a0f69f 2018-08-18 stsp struct commit_queue_entry *entry, struct got_repository *repo)
982 cd0acaa7 2018-05-20 stsp const struct got_error *err;
983 cd0acaa7 2018-05-20 stsp struct got_object *obj1 = NULL, *obj2 = NULL;
984 9ba79e04 2018-06-11 stsp struct got_object_qid *parent_id;
985 e5a0f69f 2018-08-18 stsp struct tog_view *diff_view;
987 cd0acaa7 2018-05-20 stsp err = got_object_open(&obj2, repo, entry->id);
989 cd0acaa7 2018-05-20 stsp return err;
991 9ba79e04 2018-06-11 stsp parent_id = SIMPLEQ_FIRST(&entry->commit->parent_ids);
992 9ba79e04 2018-06-11 stsp if (parent_id) {
993 9ba79e04 2018-06-11 stsp err = got_object_open(&obj1, repo, parent_id->id);
998 e5a0f69f 2018-08-18 stsp diff_view = view_open(0, 0, 0, 0, parent_view, TOG_VIEW_DIFF);
999 e5a0f69f 2018-08-18 stsp if (diff_view == NULL) {
1000 ea5e7bb5 2018-08-01 stsp err = got_error_from_errno();
1001 ea5e7bb5 2018-08-01 stsp goto done;
1004 e5a0f69f 2018-08-18 stsp err = open_diff_view(diff_view, obj1, obj2, repo);
1005 e5a0f69f 2018-08-18 stsp if (err == NULL)
1006 e5a0f69f 2018-08-18 stsp *new_view = diff_view;
1009 cd0acaa7 2018-05-20 stsp got_object_close(obj1);
1011 cd0acaa7 2018-05-20 stsp got_object_close(obj2);
1012 cd0acaa7 2018-05-20 stsp return err;
1015 80ddbec8 2018-04-29 stsp static const struct got_error *
1016 e5a0f69f 2018-08-18 stsp browse_commit(struct tog_view **new_view, struct tog_view *parent_view,
1017 e5a0f69f 2018-08-18 stsp struct commit_queue_entry *entry, struct got_repository *repo)
1019 9343a5fb 2018-06-23 stsp const struct got_error *err = NULL;
1020 9343a5fb 2018-06-23 stsp struct got_tree_object *tree;
1021 e5a0f69f 2018-08-18 stsp struct tog_view *tree_view;
1023 9343a5fb 2018-06-23 stsp err = got_object_open_as_tree(&tree, repo, entry->commit->tree_id);
1025 9343a5fb 2018-06-23 stsp return err;
1027 e5a0f69f 2018-08-18 stsp tree_view = view_open(0, 0, 0, 0, parent_view, TOG_VIEW_TREE);
1028 e5a0f69f 2018-08-18 stsp if (tree_view == NULL)
1029 e5a0f69f 2018-08-18 stsp return got_error_from_errno();
1031 e5a0f69f 2018-08-18 stsp err = open_tree_view(tree_view, tree, entry->id, repo);
1033 e5a0f69f 2018-08-18 stsp got_object_tree_close(tree);
1035 e5a0f69f 2018-08-18 stsp *new_view = tree_view;
1036 9343a5fb 2018-06-23 stsp return err;
1039 9343a5fb 2018-06-23 stsp static const struct got_error *
1040 ba4f502b 2018-08-04 stsp open_log_view(struct tog_view *view, struct got_object_id *start_id,
1041 04cc582a 2018-08-01 stsp struct got_repository *repo, const char *path)
1043 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
1044 9ba79e04 2018-06-11 stsp struct got_object_id *head_id = NULL;
1045 ba4f502b 2018-08-04 stsp int nfetched;
1046 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
1048 fb2756b9 2018-08-04 stsp err = got_repo_map_path(&s->in_repo_path, repo, path);
1049 ecb28ae0 2018-07-16 stsp if (err != NULL)
1050 ecb28ae0 2018-07-16 stsp goto done;
1052 9ba79e04 2018-06-11 stsp err = get_head_commit_id(&head_id, repo);
1054 9ba79e04 2018-06-11 stsp return err;
1056 034e3b69 2018-07-22 stsp /* The graph contains all commits. */
1057 fb2756b9 2018-08-04 stsp err = got_commit_graph_open(&s->graph, head_id, 0, repo);
1059 9ba79e04 2018-06-11 stsp goto done;
1060 034e3b69 2018-07-22 stsp /* The commit queue contains a subset of commits filtered by path. */
1061 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->commits.head);
1062 fb2756b9 2018-08-04 stsp s->commits.ncommits = 0;
1064 9ba79e04 2018-06-11 stsp /* Populate commit graph with a sufficient number of commits. */
1065 fb2756b9 2018-08-04 stsp err = got_commit_graph_fetch_commits_up_to(&nfetched, s->graph,
1066 fb2756b9 2018-08-04 stsp start_id, repo);
1068 9ba79e04 2018-06-11 stsp goto done;
1071 9ba79e04 2018-06-11 stsp * Open the initial batch of commits, sorted in commit graph order.
1072 9ba79e04 2018-06-11 stsp * We keep all commits open throughout the lifetime of the log view
1073 9ba79e04 2018-06-11 stsp * in order to avoid having to re-fetch commits from disk while
1074 9ba79e04 2018-06-11 stsp * updating the display.
1076 fb2756b9 2018-08-04 stsp err = queue_commits(s->graph, &s->commits, start_id, view->nlines, 1,
1077 fb2756b9 2018-08-04 stsp repo, s->in_repo_path);
1078 55198a88 2018-07-16 stsp if (err) {
1079 55198a88 2018-07-16 stsp if (err->code != GOT_ERR_ITER_COMPLETED)
1080 55198a88 2018-07-16 stsp goto done;
1081 55198a88 2018-07-16 stsp err = NULL;
1084 1bfa490b 2018-08-18 stsp s->first_displayed_entry =
1085 1bfa490b 2018-08-18 stsp TAILQ_FIRST(&s->commits.head);
1086 1bfa490b 2018-08-18 stsp s->selected_entry = s->first_displayed_entry;
1087 fb2756b9 2018-08-04 stsp s->repo = repo;
1089 e5a0f69f 2018-08-18 stsp view->show = show_log_view;
1090 e5a0f69f 2018-08-18 stsp view->input = input_log_view;
1091 e5a0f69f 2018-08-18 stsp view->close = close_log_view;
1093 ba4f502b 2018-08-04 stsp free(head_id);
1094 ba4f502b 2018-08-04 stsp return err;
1097 e5a0f69f 2018-08-18 stsp static const struct got_error *
1098 e5a0f69f 2018-08-18 stsp close_log_view(struct tog_view *view)
1100 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
1102 fb2756b9 2018-08-04 stsp if (s->graph)
1103 fb2756b9 2018-08-04 stsp got_commit_graph_close(s->graph);
1104 fb2756b9 2018-08-04 stsp free_commits(&s->commits);
1105 fb2756b9 2018-08-04 stsp free(s->in_repo_path);
1106 e5a0f69f 2018-08-18 stsp return NULL;
1109 ba4f502b 2018-08-04 stsp static const struct got_error *
1110 ba4f502b 2018-08-04 stsp show_log_view(struct tog_view *view)
1112 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
1114 e5a0f69f 2018-08-18 stsp return draw_commits(view, &s->last_displayed_entry,
1115 e5a0f69f 2018-08-18 stsp &s->selected_entry, s->first_displayed_entry,
1116 e5a0f69f 2018-08-18 stsp &s->commits, s->selected, view->nlines, s->graph,
1117 e5a0f69f 2018-08-18 stsp s->repo, s->in_repo_path);
1120 e5a0f69f 2018-08-18 stsp static const struct got_error *
1121 e5a0f69f 2018-08-18 stsp input_log_view(struct tog_view **new_view, struct tog_view **dead_view,
1122 e5a0f69f 2018-08-18 stsp struct tog_view *view, int ch)
1124 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
1125 e5a0f69f 2018-08-18 stsp struct tog_log_view_state *s = &view->state.log;
1127 e5a0f69f 2018-08-18 stsp switch (ch) {
1129 e5a0f69f 2018-08-18 stsp case KEY_UP:
1130 e5a0f69f 2018-08-18 stsp if (s->selected > 0)
1131 e5a0f69f 2018-08-18 stsp s->selected--;
1132 e5a0f69f 2018-08-18 stsp if (s->selected > 0)
1134 e5a0f69f 2018-08-18 stsp scroll_up(&s->first_displayed_entry, 1,
1135 e5a0f69f 2018-08-18 stsp &s->commits);
1137 e5a0f69f 2018-08-18 stsp case KEY_PPAGE:
1138 e5a0f69f 2018-08-18 stsp if (TAILQ_FIRST(&s->commits.head) ==
1139 e5a0f69f 2018-08-18 stsp s->first_displayed_entry) {
1140 e5a0f69f 2018-08-18 stsp s->selected = 0;
1143 e5a0f69f 2018-08-18 stsp scroll_up(&s->first_displayed_entry,
1144 e5a0f69f 2018-08-18 stsp view->nlines, &s->commits);
1147 e5a0f69f 2018-08-18 stsp case KEY_DOWN:
1148 e5a0f69f 2018-08-18 stsp if (s->selected < MIN(view->nlines - 2,
1149 e5a0f69f 2018-08-18 stsp s->commits.ncommits - 1)) {
1150 e5a0f69f 2018-08-18 stsp s->selected++;
1153 e5a0f69f 2018-08-18 stsp err = scroll_down(&s->first_displayed_entry, 1,
1154 e5a0f69f 2018-08-18 stsp s->last_displayed_entry, &s->commits,
1155 e5a0f69f 2018-08-18 stsp s->graph, s->repo, s->in_repo_path);
1156 e5a0f69f 2018-08-18 stsp if (err) {
1157 4d7951a5 2018-08-04 stsp if (err->code != GOT_ERR_ITER_COMPLETED)
1159 4d7951a5 2018-08-04 stsp err = NULL;
1162 e5a0f69f 2018-08-18 stsp case KEY_NPAGE: {
1163 e5a0f69f 2018-08-18 stsp struct commit_queue_entry *first;
1164 e5a0f69f 2018-08-18 stsp first = s->first_displayed_entry;
1165 e5a0f69f 2018-08-18 stsp err = scroll_down(&s->first_displayed_entry,
1166 e5a0f69f 2018-08-18 stsp view->nlines, s->last_displayed_entry,
1167 e5a0f69f 2018-08-18 stsp &s->commits, s->graph, s->repo,
1168 e5a0f69f 2018-08-18 stsp s->in_repo_path);
1169 e5a0f69f 2018-08-18 stsp if (err == NULL)
1171 e5a0f69f 2018-08-18 stsp if (err->code != GOT_ERR_ITER_COMPLETED)
1173 e5a0f69f 2018-08-18 stsp if (first == s->first_displayed_entry &&
1174 e5a0f69f 2018-08-18 stsp s->selected < MIN(view->nlines - 2,
1175 e5a0f69f 2018-08-18 stsp s->commits.ncommits - 1)) {
1176 e5a0f69f 2018-08-18 stsp /* can't scroll further down */
1177 e5a0f69f 2018-08-18 stsp s->selected = MIN(view->nlines - 2,
1178 e5a0f69f 2018-08-18 stsp s->commits.ncommits - 1);
1180 e5a0f69f 2018-08-18 stsp err = NULL;
1183 e5a0f69f 2018-08-18 stsp case KEY_RESIZE:
1184 e5a0f69f 2018-08-18 stsp if (s->selected > view->nlines - 2)
1185 e5a0f69f 2018-08-18 stsp s->selected = view->nlines - 2;
1186 e5a0f69f 2018-08-18 stsp if (s->selected > s->commits.ncommits - 1)
1187 e5a0f69f 2018-08-18 stsp s->selected = s->commits.ncommits - 1;
1189 e5a0f69f 2018-08-18 stsp case KEY_ENTER:
1190 e5a0f69f 2018-08-18 stsp case '\r':
1191 e5a0f69f 2018-08-18 stsp err = show_commit(new_view, view, s->selected_entry,
1195 e5a0f69f 2018-08-18 stsp err = browse_commit(new_view, view, s->selected_entry,
1202 80ddbec8 2018-04-29 stsp return err;
1205 4ed7e80c 2018-05-20 stsp static const struct got_error *
1206 9f7d7167 2018-04-29 stsp cmd_log(int argc, char *argv[])
1208 80ddbec8 2018-04-29 stsp const struct got_error *error;
1209 ecb28ae0 2018-07-16 stsp struct got_repository *repo = NULL;
1210 899d86c2 2018-05-10 stsp struct got_object_id *start_id = NULL;
1211 ecb28ae0 2018-07-16 stsp char *path = NULL, *repo_path = NULL, *cwd = NULL;
1212 80ddbec8 2018-04-29 stsp char *start_commit = NULL;
1214 04cc582a 2018-08-01 stsp struct tog_view *view;
1216 80ddbec8 2018-04-29 stsp #ifndef PROFILE
1217 80ddbec8 2018-04-29 stsp if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
1218 80ddbec8 2018-04-29 stsp err(1, "pledge");
1221 ecb28ae0 2018-07-16 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1222 80ddbec8 2018-04-29 stsp switch (ch) {
1224 80ddbec8 2018-04-29 stsp start_commit = optarg;
1227 ecb28ae0 2018-07-16 stsp repo_path = realpath(optarg, NULL);
1228 ecb28ae0 2018-07-16 stsp if (repo_path == NULL)
1229 ecb28ae0 2018-07-16 stsp err(1, "-r option");
1233 80ddbec8 2018-04-29 stsp /* NOTREACHED */
1237 80ddbec8 2018-04-29 stsp argc -= optind;
1238 80ddbec8 2018-04-29 stsp argv += optind;
1240 ecb28ae0 2018-07-16 stsp if (argc == 0)
1241 ecb28ae0 2018-07-16 stsp path = strdup("");
1242 ecb28ae0 2018-07-16 stsp else if (argc == 1)
1243 ecb28ae0 2018-07-16 stsp path = strdup(argv[0]);
1245 80ddbec8 2018-04-29 stsp usage_log();
1246 ecb28ae0 2018-07-16 stsp if (path == NULL)
1247 ecb28ae0 2018-07-16 stsp return got_error_from_errno();
1249 ecb28ae0 2018-07-16 stsp cwd = getcwd(NULL, 0);
1250 ecb28ae0 2018-07-16 stsp if (cwd == NULL) {
1251 ecb28ae0 2018-07-16 stsp error = got_error_from_errno();
1252 ecb28ae0 2018-07-16 stsp goto done;
1254 ecb28ae0 2018-07-16 stsp if (repo_path == NULL) {
1255 ecb28ae0 2018-07-16 stsp repo_path = strdup(cwd);
1256 ecb28ae0 2018-07-16 stsp if (repo_path == NULL) {
1257 ecb28ae0 2018-07-16 stsp error = got_error_from_errno();
1258 ecb28ae0 2018-07-16 stsp goto done;
1262 80ddbec8 2018-04-29 stsp error = got_repo_open(&repo, repo_path);
1263 80ddbec8 2018-04-29 stsp if (error != NULL)
1264 ecb28ae0 2018-07-16 stsp goto done;
1266 80ddbec8 2018-04-29 stsp if (start_commit == NULL) {
1267 899d86c2 2018-05-10 stsp error = get_head_commit_id(&start_id, repo);
1268 80ddbec8 2018-04-29 stsp if (error != NULL)
1269 ecb28ae0 2018-07-16 stsp goto done;
1271 80ddbec8 2018-04-29 stsp struct got_object *obj;
1272 80ddbec8 2018-04-29 stsp error = got_object_open_by_id_str(&obj, repo, start_commit);
1273 80ddbec8 2018-04-29 stsp if (error == NULL) {
1274 899d86c2 2018-05-10 stsp start_id = got_object_get_id(obj);
1275 899d86c2 2018-05-10 stsp if (start_id == NULL)
1276 80ddbec8 2018-04-29 stsp error = got_error_from_errno();
1277 ecb28ae0 2018-07-16 stsp goto done;
1280 80ddbec8 2018-04-29 stsp if (error != NULL)
1281 ecb28ae0 2018-07-16 stsp goto done;
1283 b3665f43 2018-08-04 stsp view = view_open(0, 0, 0, 0, NULL, TOG_VIEW_LOG);
1284 04cc582a 2018-08-01 stsp if (view == NULL) {
1285 04cc582a 2018-08-01 stsp error = got_error_from_errno();
1286 04cc582a 2018-08-01 stsp goto done;
1288 ba4f502b 2018-08-04 stsp error = open_log_view(view, start_id, repo, path);
1289 ba4f502b 2018-08-04 stsp if (error)
1290 ba4f502b 2018-08-04 stsp goto done;
1291 e5a0f69f 2018-08-18 stsp error = view_loop(view);
1293 ecb28ae0 2018-07-16 stsp free(repo_path);
1294 ecb28ae0 2018-07-16 stsp free(cwd);
1295 ecb28ae0 2018-07-16 stsp free(path);
1296 899d86c2 2018-05-10 stsp free(start_id);
1298 ecb28ae0 2018-07-16 stsp got_repo_close(repo);
1299 80ddbec8 2018-04-29 stsp return error;
1302 4ed7e80c 2018-05-20 stsp __dead static void
1303 9f7d7167 2018-04-29 stsp usage_diff(void)
1306 9f7d7167 2018-04-29 stsp fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
1307 9f7d7167 2018-04-29 stsp getprogname());
1311 b304db33 2018-05-20 stsp static char *
1312 b304db33 2018-05-20 stsp parse_next_line(FILE *f, size_t *len)
1314 b304db33 2018-05-20 stsp char *line;
1315 b304db33 2018-05-20 stsp size_t linelen;
1316 b304db33 2018-05-20 stsp size_t lineno;
1317 b304db33 2018-05-20 stsp const char delim[3] = { '\0', '\0', '\0'};
1319 b304db33 2018-05-20 stsp line = fparseln(f, &linelen, &lineno, delim, 0);
1321 b304db33 2018-05-20 stsp *len = linelen;
1322 b304db33 2018-05-20 stsp return line;
1325 4ed7e80c 2018-05-20 stsp static const struct got_error *
1326 f7d12f7e 2018-08-01 stsp draw_file(struct tog_view *view, FILE *f, int *first_displayed_line,
1327 a70480e0 2018-06-23 stsp int *last_displayed_line, int *eof, int max_lines)
1329 61e69b96 2018-05-20 stsp const struct got_error *err;
1330 26ed57b2 2018-05-19 stsp int nlines = 0, nprinted = 0;
1331 b304db33 2018-05-20 stsp char *line;
1332 b304db33 2018-05-20 stsp size_t len;
1333 61e69b96 2018-05-20 stsp wchar_t *wline;
1334 e0b650dd 2018-05-20 stsp int width;
1336 26ed57b2 2018-05-19 stsp rewind(f);
1337 f7d12f7e 2018-08-01 stsp werase(view->window);
1340 26ed57b2 2018-05-19 stsp while (nprinted < max_lines) {
1341 b304db33 2018-05-20 stsp line = parse_next_line(f, &len);
1342 26ed57b2 2018-05-19 stsp if (line == NULL) {
1346 26ed57b2 2018-05-19 stsp if (++nlines < *first_displayed_line) {
1347 26ed57b2 2018-05-19 stsp free(line);
1351 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, line, view->ncols);
1352 61e69b96 2018-05-20 stsp if (err) {
1353 61e69b96 2018-05-20 stsp free(line);
1354 2550e4c3 2018-07-13 stsp free(wline);
1355 61e69b96 2018-05-20 stsp return err;
1357 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
1358 f7d12f7e 2018-08-01 stsp if (width < view->ncols)
1359 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
1360 26ed57b2 2018-05-19 stsp if (++nprinted == 1)
1361 26ed57b2 2018-05-19 stsp *first_displayed_line = nlines;
1362 26ed57b2 2018-05-19 stsp free(line);
1363 2550e4c3 2018-07-13 stsp free(wline);
1364 2550e4c3 2018-07-13 stsp wline = NULL;
1366 26ed57b2 2018-05-19 stsp *last_displayed_line = nlines;
1368 26ed57b2 2018-05-19 stsp update_panels();
1370 26ed57b2 2018-05-19 stsp return NULL;
1373 4ed7e80c 2018-05-20 stsp static const struct got_error *
1374 5dc9f4bc 2018-08-04 stsp open_diff_view(struct tog_view *view, struct got_object *obj1,
1375 ea5e7bb5 2018-08-01 stsp struct got_object *obj2, struct got_repository *repo)
1377 26ed57b2 2018-05-19 stsp const struct got_error *err;
1380 cd0acaa7 2018-05-20 stsp if (obj1 != NULL && obj2 != NULL &&
1381 cd0acaa7 2018-05-20 stsp got_object_get_type(obj1) != got_object_get_type(obj2))
1382 26ed57b2 2018-05-19 stsp return got_error(GOT_ERR_OBJ_TYPE);
1384 511a516b 2018-05-19 stsp f = got_opentemp();
1385 26ed57b2 2018-05-19 stsp if (f == NULL)
1386 26ed57b2 2018-05-19 stsp return got_error_from_errno();
1388 cd0acaa7 2018-05-20 stsp switch (got_object_get_type(obj1 ? obj1 : obj2)) {
1389 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_BLOB:
1390 11528a82 2018-05-19 stsp err = got_diff_objects_as_blobs(obj1, obj2, repo, f);
1392 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_TREE:
1393 11528a82 2018-05-19 stsp err = got_diff_objects_as_trees(obj1, obj2, repo, f);
1395 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_COMMIT:
1396 11528a82 2018-05-19 stsp err = got_diff_objects_as_commits(obj1, obj2, repo, f);
1399 26ed57b2 2018-05-19 stsp return got_error(GOT_ERR_OBJ_TYPE);
1402 26ed57b2 2018-05-19 stsp fflush(f);
1404 5dc9f4bc 2018-08-04 stsp view->state.diff.f = f;
1405 5dc9f4bc 2018-08-04 stsp view->state.diff.first_displayed_line = 1;
1406 5dc9f4bc 2018-08-04 stsp view->state.diff.last_displayed_line = view->nlines;
1408 e5a0f69f 2018-08-18 stsp view->show = show_diff_view;
1409 e5a0f69f 2018-08-18 stsp view->input = input_diff_view;
1410 e5a0f69f 2018-08-18 stsp view->close = close_diff_view;
1412 5dc9f4bc 2018-08-04 stsp return NULL;
1415 e5a0f69f 2018-08-18 stsp static const struct got_error *
1416 5dc9f4bc 2018-08-04 stsp close_diff_view(struct tog_view *view)
1418 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
1420 e5a0f69f 2018-08-18 stsp if (view->state.diff.f && fclose(view->state.diff.f) == EOF)
1421 e5a0f69f 2018-08-18 stsp err = got_error_from_errno();
1423 e5a0f69f 2018-08-18 stsp return err;
1426 5dc9f4bc 2018-08-04 stsp static const struct got_error *
1427 5dc9f4bc 2018-08-04 stsp show_diff_view(struct tog_view *view)
1429 fb2756b9 2018-08-04 stsp struct tog_diff_view_state *s = &view->state.diff;
1431 e5a0f69f 2018-08-18 stsp return draw_file(view, s->f, &s->first_displayed_line,
1432 e5a0f69f 2018-08-18 stsp &s->last_displayed_line, &s->eof, view->nlines);
1435 e5a0f69f 2018-08-18 stsp static const struct got_error *
1436 e5a0f69f 2018-08-18 stsp input_diff_view(struct tog_view **new, struct tog_view **dead,
1437 e5a0f69f 2018-08-18 stsp struct tog_view *view, int ch)
1439 e5a0f69f 2018-08-18 stsp struct tog_diff_view_state *s = &view->state.diff;
1442 e5a0f69f 2018-08-18 stsp switch (ch) {
1444 e5a0f69f 2018-08-18 stsp case KEY_UP:
1445 e5a0f69f 2018-08-18 stsp if (s->first_displayed_line > 1)
1446 e5a0f69f 2018-08-18 stsp s->first_displayed_line--;
1448 e5a0f69f 2018-08-18 stsp case KEY_PPAGE:
1450 e5a0f69f 2018-08-18 stsp while (i++ < view->nlines - 1 &&
1451 e5a0f69f 2018-08-18 stsp s->first_displayed_line > 1)
1452 e5a0f69f 2018-08-18 stsp s->first_displayed_line--;
1455 e5a0f69f 2018-08-18 stsp case KEY_DOWN:
1456 e5a0f69f 2018-08-18 stsp if (!s->eof)
1457 e5a0f69f 2018-08-18 stsp s->first_displayed_line++;
1459 e5a0f69f 2018-08-18 stsp case KEY_NPAGE:
1462 e5a0f69f 2018-08-18 stsp while (!s->eof && i++ < view->nlines - 1) {
1463 e5a0f69f 2018-08-18 stsp char *line;
1464 e5a0f69f 2018-08-18 stsp line = parse_next_line(s->f, NULL);
1465 e5a0f69f 2018-08-18 stsp s->first_displayed_line++;
1466 e5a0f69f 2018-08-18 stsp if (line == NULL)
1474 e5a0f69f 2018-08-18 stsp return NULL;
1477 4ed7e80c 2018-05-20 stsp static const struct got_error *
1478 9f7d7167 2018-04-29 stsp cmd_diff(int argc, char *argv[])
1480 26ed57b2 2018-05-19 stsp const struct got_error *error = NULL;
1481 26ed57b2 2018-05-19 stsp struct got_repository *repo = NULL;
1482 26ed57b2 2018-05-19 stsp struct got_object *obj1 = NULL, *obj2 = NULL;
1483 26ed57b2 2018-05-19 stsp char *repo_path = NULL;
1484 26ed57b2 2018-05-19 stsp char *obj_id_str1 = NULL, *obj_id_str2 = NULL;
1486 ea5e7bb5 2018-08-01 stsp struct tog_view *view;
1488 26ed57b2 2018-05-19 stsp #ifndef PROFILE
1489 26ed57b2 2018-05-19 stsp if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
1490 26ed57b2 2018-05-19 stsp err(1, "pledge");
1493 26ed57b2 2018-05-19 stsp while ((ch = getopt(argc, argv, "")) != -1) {
1494 26ed57b2 2018-05-19 stsp switch (ch) {
1497 26ed57b2 2018-05-19 stsp /* NOTREACHED */
1501 26ed57b2 2018-05-19 stsp argc -= optind;
1502 26ed57b2 2018-05-19 stsp argv += optind;
1504 26ed57b2 2018-05-19 stsp if (argc == 0) {
1505 26ed57b2 2018-05-19 stsp usage_diff(); /* TODO show local worktree changes */
1506 26ed57b2 2018-05-19 stsp } else if (argc == 2) {
1507 26ed57b2 2018-05-19 stsp repo_path = getcwd(NULL, 0);
1508 26ed57b2 2018-05-19 stsp if (repo_path == NULL)
1509 26ed57b2 2018-05-19 stsp return got_error_from_errno();
1510 26ed57b2 2018-05-19 stsp obj_id_str1 = argv[0];
1511 26ed57b2 2018-05-19 stsp obj_id_str2 = argv[1];
1512 26ed57b2 2018-05-19 stsp } else if (argc == 3) {
1513 26ed57b2 2018-05-19 stsp repo_path = realpath(argv[0], NULL);
1514 26ed57b2 2018-05-19 stsp if (repo_path == NULL)
1515 26ed57b2 2018-05-19 stsp return got_error_from_errno();
1516 26ed57b2 2018-05-19 stsp obj_id_str1 = argv[1];
1517 26ed57b2 2018-05-19 stsp obj_id_str2 = argv[2];
1519 26ed57b2 2018-05-19 stsp usage_diff();
1521 26ed57b2 2018-05-19 stsp error = got_repo_open(&repo, repo_path);
1522 26ed57b2 2018-05-19 stsp free(repo_path);
1523 26ed57b2 2018-05-19 stsp if (error)
1524 26ed57b2 2018-05-19 stsp goto done;
1526 26ed57b2 2018-05-19 stsp error = got_object_open_by_id_str(&obj1, repo, obj_id_str1);
1527 26ed57b2 2018-05-19 stsp if (error)
1528 26ed57b2 2018-05-19 stsp goto done;
1530 26ed57b2 2018-05-19 stsp error = got_object_open_by_id_str(&obj2, repo, obj_id_str2);
1531 26ed57b2 2018-05-19 stsp if (error)
1532 26ed57b2 2018-05-19 stsp goto done;
1534 b3665f43 2018-08-04 stsp view = view_open(0, 0, 0, 0, NULL, TOG_VIEW_DIFF);
1535 ea5e7bb5 2018-08-01 stsp if (view == NULL) {
1536 ea5e7bb5 2018-08-01 stsp error = got_error_from_errno();
1537 ea5e7bb5 2018-08-01 stsp goto done;
1539 5dc9f4bc 2018-08-04 stsp error = open_diff_view(view, obj1, obj2, repo);
1540 5dc9f4bc 2018-08-04 stsp if (error)
1541 5dc9f4bc 2018-08-04 stsp goto done;
1542 e5a0f69f 2018-08-18 stsp error = view_loop(view);
1544 26ed57b2 2018-05-19 stsp got_repo_close(repo);
1546 26ed57b2 2018-05-19 stsp got_object_close(obj1);
1548 26ed57b2 2018-05-19 stsp got_object_close(obj2);
1549 26ed57b2 2018-05-19 stsp return error;
1552 4ed7e80c 2018-05-20 stsp __dead static void
1553 9f7d7167 2018-04-29 stsp usage_blame(void)
1556 69069811 2018-08-02 stsp fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
1557 9f7d7167 2018-04-29 stsp getprogname());
1561 84451b3e 2018-07-10 stsp struct tog_blame_line {
1562 84451b3e 2018-07-10 stsp int annotated;
1563 84451b3e 2018-07-10 stsp struct got_object_id *id;
1566 4ed7e80c 2018-05-20 stsp static const struct got_error *
1567 f7d12f7e 2018-08-01 stsp draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
1568 f7d12f7e 2018-08-01 stsp const char *path, struct tog_blame_line *lines, int nlines,
1569 f7d12f7e 2018-08-01 stsp int blame_complete, int selected_line, int *first_displayed_line,
1570 f7d12f7e 2018-08-01 stsp int *last_displayed_line, int *eof, int max_lines)
1572 84451b3e 2018-07-10 stsp const struct got_error *err;
1573 84451b3e 2018-07-10 stsp int lineno = 0, nprinted = 0;
1574 84451b3e 2018-07-10 stsp char *line;
1575 84451b3e 2018-07-10 stsp size_t len;
1576 84451b3e 2018-07-10 stsp wchar_t *wline;
1577 b700b5d6 2018-07-10 stsp int width, wlimit;
1578 84451b3e 2018-07-10 stsp struct tog_blame_line *blame_line;
1579 ee41ec32 2018-07-10 stsp struct got_object_id *prev_id = NULL;
1580 ab089a2a 2018-07-12 stsp char *id_str;
1582 ab089a2a 2018-07-12 stsp err = got_object_id_str(&id_str, id);
1584 ab089a2a 2018-07-12 stsp return err;
1586 84451b3e 2018-07-10 stsp rewind(f);
1587 f7d12f7e 2018-08-01 stsp werase(view->window);
1589 ab089a2a 2018-07-12 stsp if (asprintf(&line, "commit: %s", id_str) == -1) {
1590 ab089a2a 2018-07-12 stsp err = got_error_from_errno();
1591 ab089a2a 2018-07-12 stsp free(id_str);
1592 ab089a2a 2018-07-12 stsp return err;
1595 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, line, view->ncols);
1596 ab089a2a 2018-07-12 stsp free(line);
1597 2550e4c3 2018-07-13 stsp line = NULL;
1598 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
1599 2550e4c3 2018-07-13 stsp free(wline);
1600 2550e4c3 2018-07-13 stsp wline = NULL;
1601 f7d12f7e 2018-08-01 stsp if (width < view->ncols)
1602 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
1604 084063cd 2018-07-12 stsp if (asprintf(&line, "[%d/%d] %s%s",
1605 084063cd 2018-07-12 stsp *first_displayed_line - 1 + selected_line, nlines,
1606 12a0b23b 2018-07-12 stsp blame_complete ? "" : "annotating ", path) == -1) {
1607 ab089a2a 2018-07-12 stsp free(id_str);
1608 3f60a8ef 2018-07-10 stsp return got_error_from_errno();
1610 ab089a2a 2018-07-12 stsp free(id_str);
1611 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, line, view->ncols);
1612 3f60a8ef 2018-07-10 stsp free(line);
1613 2550e4c3 2018-07-13 stsp line = NULL;
1615 3f60a8ef 2018-07-10 stsp return err;
1616 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
1617 2550e4c3 2018-07-13 stsp free(wline);
1618 2550e4c3 2018-07-13 stsp wline = NULL;
1619 f7d12f7e 2018-08-01 stsp if (width < view->ncols)
1620 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
1623 ab089a2a 2018-07-12 stsp while (nprinted < max_lines - 2) {
1624 84451b3e 2018-07-10 stsp line = parse_next_line(f, &len);
1625 84451b3e 2018-07-10 stsp if (line == NULL) {
1629 84451b3e 2018-07-10 stsp if (++lineno < *first_displayed_line) {
1630 84451b3e 2018-07-10 stsp free(line);
1634 f7d12f7e 2018-08-01 stsp wlimit = view->ncols < 9 ? 0 : view->ncols - 9;
1635 b700b5d6 2018-07-10 stsp err = format_line(&wline, &width, line, wlimit);
1636 84451b3e 2018-07-10 stsp if (err) {
1637 84451b3e 2018-07-10 stsp free(line);
1638 84451b3e 2018-07-10 stsp return err;
1641 b700b5d6 2018-07-10 stsp if (nprinted == selected_line - 1)
1642 f7d12f7e 2018-08-01 stsp wstandout(view->window);
1644 84451b3e 2018-07-10 stsp blame_line = &lines[lineno - 1];
1645 ee41ec32 2018-07-10 stsp if (blame_line->annotated && prev_id &&
1646 ee41ec32 2018-07-10 stsp got_object_id_cmp(prev_id, blame_line->id) == 0)
1647 f7d12f7e 2018-08-01 stsp waddstr(view->window, " ");
1648 ee41ec32 2018-07-10 stsp else if (blame_line->annotated) {
1649 84451b3e 2018-07-10 stsp char *id_str;
1650 84451b3e 2018-07-10 stsp err = got_object_id_str(&id_str, blame_line->id);
1651 84451b3e 2018-07-10 stsp if (err) {
1652 84451b3e 2018-07-10 stsp free(line);
1653 2550e4c3 2018-07-13 stsp free(wline);
1654 84451b3e 2018-07-10 stsp return err;
1656 f7d12f7e 2018-08-01 stsp wprintw(view->window, "%.8s ", id_str);
1657 84451b3e 2018-07-10 stsp free(id_str);
1658 ee41ec32 2018-07-10 stsp prev_id = blame_line->id;
1660 f7d12f7e 2018-08-01 stsp waddstr(view->window, "........ ");
1661 ee41ec32 2018-07-10 stsp prev_id = NULL;
1664 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
1665 b700b5d6 2018-07-10 stsp while (width < wlimit) {
1666 f7d12f7e 2018-08-01 stsp waddch(view->window, ' ');
1669 b700b5d6 2018-07-10 stsp if (nprinted == selected_line - 1)
1670 f7d12f7e 2018-08-01 stsp wstandend(view->window);
1671 84451b3e 2018-07-10 stsp if (++nprinted == 1)
1672 84451b3e 2018-07-10 stsp *first_displayed_line = lineno;
1673 84451b3e 2018-07-10 stsp free(line);
1674 2550e4c3 2018-07-13 stsp free(wline);
1675 2550e4c3 2018-07-13 stsp wline = NULL;
1677 84451b3e 2018-07-10 stsp *last_displayed_line = lineno;
1679 84451b3e 2018-07-10 stsp update_panels();
1681 84451b3e 2018-07-10 stsp return NULL;
1684 84451b3e 2018-07-10 stsp static const struct got_error *
1685 84451b3e 2018-07-10 stsp blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
1687 84451b3e 2018-07-10 stsp const struct got_error *err = NULL;
1688 84451b3e 2018-07-10 stsp struct tog_blame_cb_args *a = arg;
1689 84451b3e 2018-07-10 stsp struct tog_blame_line *line;
1691 d68a0a7d 2018-07-10 stsp if (nlines != a->nlines ||
1692 d68a0a7d 2018-07-10 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
1693 84451b3e 2018-07-10 stsp return got_error(GOT_ERR_RANGE);
1695 84451b3e 2018-07-10 stsp if (pthread_mutex_lock(a->mutex) != 0)
1696 84451b3e 2018-07-10 stsp return got_error_from_errno();
1698 18430de3 2018-07-10 stsp if (*a->quit) { /* user has quit the blame view */
1699 d68a0a7d 2018-07-10 stsp err = got_error(GOT_ERR_ITER_COMPLETED);
1700 d68a0a7d 2018-07-10 stsp goto done;
1703 d68a0a7d 2018-07-10 stsp if (lineno == -1)
1704 d68a0a7d 2018-07-10 stsp goto done; /* no change in this commit */
1706 84451b3e 2018-07-10 stsp line = &a->lines[lineno - 1];
1707 d68a0a7d 2018-07-10 stsp if (line->annotated)
1708 d68a0a7d 2018-07-10 stsp goto done;
1710 84451b3e 2018-07-10 stsp line->id = got_object_id_dup(id);
1711 84451b3e 2018-07-10 stsp if (line->id == NULL) {
1712 84451b3e 2018-07-10 stsp err = got_error_from_errno();
1713 84451b3e 2018-07-10 stsp goto done;
1715 84451b3e 2018-07-10 stsp line->annotated = 1;
1717 f7d12f7e 2018-08-01 stsp err = draw_blame(a->view, a->commit_id, a->f, a->path,
1718 245d91c1 2018-07-12 stsp a->lines, a->nlines, 0, *a->selected_line, a->first_displayed_line,
1719 e5a0f69f 2018-08-18 stsp a->last_displayed_line, a->eof, a->view->nlines);
1721 84451b3e 2018-07-10 stsp if (pthread_mutex_unlock(a->mutex) != 0)
1722 84451b3e 2018-07-10 stsp return got_error_from_errno();
1723 84451b3e 2018-07-10 stsp return err;
1726 84451b3e 2018-07-10 stsp static void *
1727 84451b3e 2018-07-10 stsp blame_thread(void *arg)
1729 18430de3 2018-07-10 stsp const struct got_error *err;
1730 18430de3 2018-07-10 stsp struct tog_blame_thread_args *ta = arg;
1731 245d91c1 2018-07-12 stsp struct tog_blame_cb_args *a = ta->cb_args;
1733 ab089a2a 2018-07-12 stsp err = got_blame_incremental(ta->path, a->commit_id, ta->repo,
1734 245d91c1 2018-07-12 stsp blame_cb, ta->cb_args);
1736 18430de3 2018-07-10 stsp if (pthread_mutex_lock(a->mutex) != 0)
1737 18430de3 2018-07-10 stsp return (void *)got_error_from_errno();
1739 c9beca56 2018-07-22 stsp got_repo_close(ta->repo);
1740 c9beca56 2018-07-22 stsp ta->repo = NULL;
1741 c9beca56 2018-07-22 stsp *ta->complete = 1;
1743 f7d12f7e 2018-08-01 stsp err = draw_blame(a->view, a->commit_id, a->f, a->path,
1744 f7d12f7e 2018-08-01 stsp a->lines, a->nlines, 1, *a->selected_line,
1745 e5a0f69f 2018-08-18 stsp a->first_displayed_line, a->last_displayed_line, a->eof,
1746 f7d12f7e 2018-08-01 stsp a->view->nlines);
1748 18430de3 2018-07-10 stsp if (pthread_mutex_unlock(a->mutex) != 0 && err == NULL)
1749 18430de3 2018-07-10 stsp err = got_error_from_errno();
1751 18430de3 2018-07-10 stsp return (void *)err;
1754 245d91c1 2018-07-12 stsp static struct got_object_id *
1755 245d91c1 2018-07-12 stsp get_selected_commit_id(struct tog_blame_line *lines,
1756 245d91c1 2018-07-12 stsp int first_displayed_line, int selected_line)
1758 245d91c1 2018-07-12 stsp struct tog_blame_line *line;
1760 245d91c1 2018-07-12 stsp line = &lines[first_displayed_line - 1 + selected_line - 1];
1761 245d91c1 2018-07-12 stsp if (!line->annotated)
1762 245d91c1 2018-07-12 stsp return NULL;
1764 245d91c1 2018-07-12 stsp return line->id;
1767 84451b3e 2018-07-10 stsp static const struct got_error *
1768 245d91c1 2018-07-12 stsp open_selected_commit(struct got_object **pobj, struct got_object **obj,
1769 b880a918 2018-07-10 stsp struct tog_blame_line *lines, int first_displayed_line,
1770 b880a918 2018-07-10 stsp int selected_line, struct got_repository *repo)
1772 b880a918 2018-07-10 stsp const struct got_error *err = NULL;
1773 b880a918 2018-07-10 stsp struct got_commit_object *commit = NULL;
1774 245d91c1 2018-07-12 stsp struct got_object_id *selected_id;
1775 b880a918 2018-07-10 stsp struct got_object_qid *pid;
1777 b880a918 2018-07-10 stsp *pobj = NULL;
1778 b880a918 2018-07-10 stsp *obj = NULL;
1780 245d91c1 2018-07-12 stsp selected_id = get_selected_commit_id(lines,
1781 245d91c1 2018-07-12 stsp first_displayed_line, selected_line);
1782 245d91c1 2018-07-12 stsp if (selected_id == NULL)
1783 b880a918 2018-07-10 stsp return NULL;
1785 245d91c1 2018-07-12 stsp err = got_object_open(obj, repo, selected_id);
1787 b880a918 2018-07-10 stsp goto done;
1789 b880a918 2018-07-10 stsp err = got_object_commit_open(&commit, repo, *obj);
1791 b880a918 2018-07-10 stsp goto done;
1793 b880a918 2018-07-10 stsp pid = SIMPLEQ_FIRST(&commit->parent_ids);
1794 b880a918 2018-07-10 stsp if (pid) {
1795 b880a918 2018-07-10 stsp err = got_object_open(pobj, repo, pid->id);
1797 b880a918 2018-07-10 stsp goto done;
1800 b880a918 2018-07-10 stsp if (commit)
1801 b880a918 2018-07-10 stsp got_object_commit_close(commit);
1802 b880a918 2018-07-10 stsp return err;
1805 b880a918 2018-07-10 stsp static const struct got_error *
1806 245d91c1 2018-07-12 stsp stop_blame(struct tog_blame *blame)
1808 a70480e0 2018-06-23 stsp const struct got_error *err = NULL;
1811 245d91c1 2018-07-12 stsp if (blame->thread) {
1812 245d91c1 2018-07-12 stsp if (pthread_join(blame->thread, (void **)&err) != 0)
1813 245d91c1 2018-07-12 stsp err = got_error_from_errno();
1814 245d91c1 2018-07-12 stsp if (err && err->code == GOT_ERR_ITER_COMPLETED)
1815 245d91c1 2018-07-12 stsp err = NULL;
1816 245d91c1 2018-07-12 stsp blame->thread = NULL;
1818 245d91c1 2018-07-12 stsp if (blame->thread_args.repo) {
1819 245d91c1 2018-07-12 stsp got_repo_close(blame->thread_args.repo);
1820 245d91c1 2018-07-12 stsp blame->thread_args.repo = NULL;
1822 245d91c1 2018-07-12 stsp if (blame->f) {
1823 245d91c1 2018-07-12 stsp fclose(blame->f);
1824 245d91c1 2018-07-12 stsp blame->f = NULL;
1826 245d91c1 2018-07-12 stsp for (i = 0; i < blame->nlines; i++)
1827 245d91c1 2018-07-12 stsp free(blame->lines[i].id);
1828 245d91c1 2018-07-12 stsp free(blame->lines);
1829 245d91c1 2018-07-12 stsp blame->lines = NULL;
1830 75c32340 2018-07-23 stsp free(blame->cb_args.commit_id);
1831 75c32340 2018-07-23 stsp blame->cb_args.commit_id = NULL;
1833 245d91c1 2018-07-12 stsp return err;
1836 245d91c1 2018-07-12 stsp static const struct got_error *
1837 7cc84d77 2018-08-01 stsp run_blame(struct tog_blame *blame, pthread_mutex_t *mutex,
1838 7cc84d77 2018-08-01 stsp struct tog_view *view, int *blame_complete,
1839 245d91c1 2018-07-12 stsp int *first_displayed_line, int *last_displayed_line,
1840 e5a0f69f 2018-08-18 stsp int *selected_line, int *done, int *eof, const char *path,
1841 245d91c1 2018-07-12 stsp struct got_object_id *commit_id,
1842 245d91c1 2018-07-12 stsp struct got_repository *repo)
1844 245d91c1 2018-07-12 stsp const struct got_error *err = NULL;
1845 84451b3e 2018-07-10 stsp struct got_blob_object *blob = NULL;
1846 245d91c1 2018-07-12 stsp struct got_repository *thread_repo = NULL;
1847 245d91c1 2018-07-12 stsp struct got_object *obj;
1849 84451b3e 2018-07-10 stsp err = got_object_open_by_path(&obj, repo, commit_id, path);
1851 84451b3e 2018-07-10 stsp goto done;
1852 84451b3e 2018-07-10 stsp if (got_object_get_type(obj) != GOT_OBJ_TYPE_BLOB) {
1853 84451b3e 2018-07-10 stsp err = got_error(GOT_ERR_OBJ_TYPE);
1854 84451b3e 2018-07-10 stsp goto done;
1857 84451b3e 2018-07-10 stsp err = got_object_blob_open(&blob, repo, obj, 8192);
1859 a70480e0 2018-06-23 stsp goto done;
1860 245d91c1 2018-07-12 stsp blame->f = got_opentemp();
1861 245d91c1 2018-07-12 stsp if (blame->f == NULL) {
1862 84451b3e 2018-07-10 stsp err = got_error_from_errno();
1863 84451b3e 2018-07-10 stsp goto done;
1865 245d91c1 2018-07-12 stsp err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
1866 245d91c1 2018-07-12 stsp blame->f, blob);
1868 84451b3e 2018-07-10 stsp goto done;
1870 245d91c1 2018-07-12 stsp blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
1871 245d91c1 2018-07-12 stsp if (blame->lines == NULL) {
1872 84451b3e 2018-07-10 stsp err = got_error_from_errno();
1873 84451b3e 2018-07-10 stsp goto done;
1876 245d91c1 2018-07-12 stsp err = got_repo_open(&thread_repo, got_repo_get_path(repo));
1878 bd24772e 2018-07-11 stsp goto done;
1880 7cc84d77 2018-08-01 stsp blame->cb_args.view = view;
1881 245d91c1 2018-07-12 stsp blame->cb_args.lines = blame->lines;
1882 245d91c1 2018-07-12 stsp blame->cb_args.nlines = blame->nlines;
1883 245d91c1 2018-07-12 stsp blame->cb_args.mutex = mutex;
1884 245d91c1 2018-07-12 stsp blame->cb_args.commit_id = got_object_id_dup(commit_id);
1885 245d91c1 2018-07-12 stsp if (blame->cb_args.commit_id == NULL) {
1886 245d91c1 2018-07-12 stsp err = got_error_from_errno();
1887 245d91c1 2018-07-12 stsp goto done;
1889 245d91c1 2018-07-12 stsp blame->cb_args.f = blame->f;
1890 245d91c1 2018-07-12 stsp blame->cb_args.path = path;
1891 245d91c1 2018-07-12 stsp blame->cb_args.first_displayed_line = first_displayed_line;
1892 245d91c1 2018-07-12 stsp blame->cb_args.selected_line = selected_line;
1893 245d91c1 2018-07-12 stsp blame->cb_args.last_displayed_line = last_displayed_line;
1894 245d91c1 2018-07-12 stsp blame->cb_args.quit = done;
1895 e5a0f69f 2018-08-18 stsp blame->cb_args.eof = eof;
1897 245d91c1 2018-07-12 stsp blame->thread_args.path = path;
1898 245d91c1 2018-07-12 stsp blame->thread_args.repo = thread_repo;
1899 245d91c1 2018-07-12 stsp blame->thread_args.cb_args = &blame->cb_args;
1900 245d91c1 2018-07-12 stsp blame->thread_args.complete = blame_complete;
1901 245d91c1 2018-07-12 stsp *blame_complete = 0;
1903 245d91c1 2018-07-12 stsp if (pthread_create(&blame->thread, NULL, blame_thread,
1904 245d91c1 2018-07-12 stsp &blame->thread_args) != 0) {
1905 245d91c1 2018-07-12 stsp err = got_error_from_errno();
1906 245d91c1 2018-07-12 stsp goto done;
1911 245d91c1 2018-07-12 stsp got_object_blob_close(blob);
1913 245d91c1 2018-07-12 stsp got_object_close(obj);
1915 245d91c1 2018-07-12 stsp stop_blame(blame);
1916 245d91c1 2018-07-12 stsp return err;
1919 245d91c1 2018-07-12 stsp static const struct got_error *
1920 e5a0f69f 2018-08-18 stsp open_blame_view(struct tog_view *view, char *path,
1921 e1cd8fed 2018-08-01 stsp struct got_object_id *commit_id, struct got_repository *repo)
1923 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL;
1924 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
1926 fb2756b9 2018-08-04 stsp SIMPLEQ_INIT(&s->blamed_commits);
1928 fb2756b9 2018-08-04 stsp if (pthread_mutex_init(&s->mutex, NULL) != 0)
1929 7cbe629d 2018-08-04 stsp return got_error_from_errno();
1931 fb2756b9 2018-08-04 stsp err = got_object_qid_alloc(&s->blamed_commit, commit_id);
1933 7cbe629d 2018-08-04 stsp return err;
1935 fb2756b9 2018-08-04 stsp SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
1936 fb2756b9 2018-08-04 stsp s->first_displayed_line = 1;
1937 fb2756b9 2018-08-04 stsp s->last_displayed_line = view->nlines;
1938 fb2756b9 2018-08-04 stsp s->selected_line = 1;
1939 fb2756b9 2018-08-04 stsp s->blame_complete = 0;
1940 fb2756b9 2018-08-04 stsp s->path = path;
1941 e5a0f69f 2018-08-18 stsp if (s->path == NULL)
1942 e5a0f69f 2018-08-18 stsp return got_error_from_errno();
1943 fb2756b9 2018-08-04 stsp s->repo = repo;
1944 fb2756b9 2018-08-04 stsp s->commit_id = commit_id;
1945 e9424729 2018-08-04 stsp memset(&s->blame, 0, sizeof(s->blame));
1947 e5a0f69f 2018-08-18 stsp view->show = show_blame_view;
1948 e5a0f69f 2018-08-18 stsp view->input = input_blame_view;
1949 e5a0f69f 2018-08-18 stsp view->close = close_blame_view;
1951 e5a0f69f 2018-08-18 stsp return run_blame(&s->blame, &s->mutex, view, &s->blame_complete,
1952 e5a0f69f 2018-08-18 stsp &s->first_displayed_line, &s->last_displayed_line,
1953 e5a0f69f 2018-08-18 stsp &s->selected_line, &s->done, &s->eof, s->path,
1954 e5a0f69f 2018-08-18 stsp s->blamed_commit->id, s->repo);
1957 e5a0f69f 2018-08-18 stsp static const struct got_error *
1958 7cbe629d 2018-08-04 stsp close_blame_view(struct tog_view *view)
1960 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
1961 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
1963 e5a0f69f 2018-08-18 stsp if (s->blame.thread)
1964 e5a0f69f 2018-08-18 stsp err = stop_blame(&s->blame);
1966 fb2756b9 2018-08-04 stsp while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
1967 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
1968 fb2756b9 2018-08-04 stsp blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
1969 fb2756b9 2018-08-04 stsp SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
1970 7cbe629d 2018-08-04 stsp got_object_qid_free(blamed_commit);
1973 e5a0f69f 2018-08-18 stsp free(s->path);
1975 e5a0f69f 2018-08-18 stsp return err;
1978 7cbe629d 2018-08-04 stsp static const struct got_error *
1979 7cbe629d 2018-08-04 stsp show_blame_view(struct tog_view *view)
1981 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
1982 e5a0f69f 2018-08-18 stsp struct tog_blame_view_state *s = &view->state.blame;
1984 e5a0f69f 2018-08-18 stsp if (pthread_mutex_lock(&s->mutex) != 0)
1985 e5a0f69f 2018-08-18 stsp return got_error_from_errno();
1987 e5a0f69f 2018-08-18 stsp err = draw_blame(view, s->blamed_commit->id, s->blame.f,
1988 e5a0f69f 2018-08-18 stsp s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
1989 e5a0f69f 2018-08-18 stsp s->selected_line, &s->first_displayed_line,
1990 e5a0f69f 2018-08-18 stsp &s->last_displayed_line, &s->eof, view->nlines);
1992 e5a0f69f 2018-08-18 stsp if (pthread_mutex_unlock(&s->mutex) != 0 && err == NULL)
1993 e5a0f69f 2018-08-18 stsp err = got_error_from_errno();
1995 e5a0f69f 2018-08-18 stsp return err;
1998 e5a0f69f 2018-08-18 stsp static const struct got_error *
1999 e5a0f69f 2018-08-18 stsp input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
2000 e5a0f69f 2018-08-18 stsp struct tog_view *view, int ch)
2002 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL, *thread_err = NULL;
2003 7cbe629d 2018-08-04 stsp struct got_object *obj = NULL, *pobj = NULL;
2004 7cbe629d 2018-08-04 stsp struct tog_view *diff_view;
2005 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
2007 e5a0f69f 2018-08-18 stsp if (pthread_mutex_lock(&s->mutex) != 0) {
2008 e5a0f69f 2018-08-18 stsp err = got_error_from_errno();
2009 e5a0f69f 2018-08-18 stsp goto done;
2012 e5a0f69f 2018-08-18 stsp switch (ch) {
2014 e5a0f69f 2018-08-18 stsp s->done = 1;
2015 e5a0f69f 2018-08-18 stsp if (pthread_mutex_unlock(&s->mutex) != 0) {
2016 e5a0f69f 2018-08-18 stsp err = got_error_from_errno();
2017 e5a0f69f 2018-08-18 stsp goto done;
2019 e5a0f69f 2018-08-18 stsp return stop_blame(&s->blame);
2021 e5a0f69f 2018-08-18 stsp case KEY_UP:
2022 e5a0f69f 2018-08-18 stsp if (s->selected_line > 1)
2023 e5a0f69f 2018-08-18 stsp s->selected_line--;
2024 e5a0f69f 2018-08-18 stsp else if (s->selected_line == 1 &&
2025 e5a0f69f 2018-08-18 stsp s->first_displayed_line > 1)
2026 e5a0f69f 2018-08-18 stsp s->first_displayed_line--;
2028 e5a0f69f 2018-08-18 stsp case KEY_PPAGE:
2029 e5a0f69f 2018-08-18 stsp if (s->first_displayed_line == 1) {
2030 e5a0f69f 2018-08-18 stsp s->selected_line = 1;
2033 e5a0f69f 2018-08-18 stsp if (s->first_displayed_line > view->nlines - 2)
2034 e5a0f69f 2018-08-18 stsp s->first_displayed_line -=
2035 e5a0f69f 2018-08-18 stsp (view->nlines - 2);
2037 e5a0f69f 2018-08-18 stsp s->first_displayed_line = 1;
2040 e5a0f69f 2018-08-18 stsp case KEY_DOWN:
2041 e5a0f69f 2018-08-18 stsp if (s->selected_line < view->nlines - 2 &&
2042 e5a0f69f 2018-08-18 stsp s->first_displayed_line +
2043 e5a0f69f 2018-08-18 stsp s->selected_line <= s->blame.nlines)
2044 e5a0f69f 2018-08-18 stsp s->selected_line++;
2045 e5a0f69f 2018-08-18 stsp else if (s->last_displayed_line <
2046 e5a0f69f 2018-08-18 stsp s->blame.nlines)
2047 e5a0f69f 2018-08-18 stsp s->first_displayed_line++;
2050 e5a0f69f 2018-08-18 stsp case 'p': {
2051 e5a0f69f 2018-08-18 stsp struct got_object_id *id;
2052 e5a0f69f 2018-08-18 stsp id = get_selected_commit_id(s->blame.lines,
2053 e5a0f69f 2018-08-18 stsp s->first_displayed_line, s->selected_line);
2054 e5a0f69f 2018-08-18 stsp if (id == NULL || got_object_id_cmp(id,
2055 e5a0f69f 2018-08-18 stsp s->blamed_commit->id) == 0)
2057 e5a0f69f 2018-08-18 stsp err = open_selected_commit(&pobj, &obj,
2058 e5a0f69f 2018-08-18 stsp s->blame.lines, s->first_displayed_line,
2059 e5a0f69f 2018-08-18 stsp s->selected_line, s->repo);
2062 e5a0f69f 2018-08-18 stsp if (pobj == NULL && obj == NULL)
2064 e5a0f69f 2018-08-18 stsp if (ch == 'p' && pobj == NULL)
2066 e5a0f69f 2018-08-18 stsp s->done = 1;
2067 e5a0f69f 2018-08-18 stsp if (pthread_mutex_unlock(&s->mutex) != 0) {
2068 e5a0f69f 2018-08-18 stsp err = got_error_from_errno();
2069 e5a0f69f 2018-08-18 stsp goto done;
2071 e5a0f69f 2018-08-18 stsp thread_err = stop_blame(&s->blame);
2072 e5a0f69f 2018-08-18 stsp s->done = 0;
2073 e5a0f69f 2018-08-18 stsp if (pthread_mutex_lock(&s->mutex) != 0) {
2074 e5a0f69f 2018-08-18 stsp err = got_error_from_errno();
2075 e5a0f69f 2018-08-18 stsp goto done;
2077 e5a0f69f 2018-08-18 stsp if (thread_err)
2079 e5a0f69f 2018-08-18 stsp id = got_object_get_id(ch == 'b' ? obj : pobj);
2080 e5a0f69f 2018-08-18 stsp got_object_close(obj);
2081 e5a0f69f 2018-08-18 stsp obj = NULL;
2082 e5a0f69f 2018-08-18 stsp if (pobj) {
2083 e5a0f69f 2018-08-18 stsp got_object_close(pobj);
2084 e5a0f69f 2018-08-18 stsp pobj = NULL;
2086 e5a0f69f 2018-08-18 stsp if (id == NULL) {
2087 e5a0f69f 2018-08-18 stsp err = got_error_from_errno();
2090 e5a0f69f 2018-08-18 stsp err = got_object_qid_alloc(
2091 e5a0f69f 2018-08-18 stsp &s->blamed_commit, id);
2094 e5a0f69f 2018-08-18 stsp goto done;
2095 e5a0f69f 2018-08-18 stsp SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
2096 e5a0f69f 2018-08-18 stsp s->blamed_commit, entry);
2097 e5a0f69f 2018-08-18 stsp err = run_blame(&s->blame, &s->mutex, view,
2098 e5a0f69f 2018-08-18 stsp &s->blame_complete,
2099 e5a0f69f 2018-08-18 stsp &s->first_displayed_line,
2100 e5a0f69f 2018-08-18 stsp &s->last_displayed_line,
2101 e5a0f69f 2018-08-18 stsp &s->selected_line, &s->done, &s->eof,
2102 e5a0f69f 2018-08-18 stsp s->path, s->blamed_commit->id, s->repo);
2107 e5a0f69f 2018-08-18 stsp case 'B': {
2108 e5a0f69f 2018-08-18 stsp struct got_object_qid *first;
2109 e5a0f69f 2018-08-18 stsp first = SIMPLEQ_FIRST(&s->blamed_commits);
2110 e5a0f69f 2018-08-18 stsp if (!got_object_id_cmp(first->id, s->commit_id))
2112 e5a0f69f 2018-08-18 stsp s->done = 1;
2113 e5a0f69f 2018-08-18 stsp if (pthread_mutex_unlock(&s->mutex) != 0) {
2114 e5a0f69f 2018-08-18 stsp err = got_error_from_errno();
2115 e5a0f69f 2018-08-18 stsp goto done;
2117 e5a0f69f 2018-08-18 stsp thread_err = stop_blame(&s->blame);
2118 e5a0f69f 2018-08-18 stsp s->done = 0;
2119 e5a0f69f 2018-08-18 stsp if (pthread_mutex_lock(&s->mutex) != 0) {
2120 e5a0f69f 2018-08-18 stsp err = got_error_from_errno();
2121 e5a0f69f 2018-08-18 stsp goto done;
2123 e5a0f69f 2018-08-18 stsp if (thread_err)
2125 e5a0f69f 2018-08-18 stsp SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
2126 e5a0f69f 2018-08-18 stsp got_object_qid_free(s->blamed_commit);
2127 e5a0f69f 2018-08-18 stsp s->blamed_commit =
2128 e5a0f69f 2018-08-18 stsp SIMPLEQ_FIRST(&s->blamed_commits);
2129 e5a0f69f 2018-08-18 stsp err = run_blame(&s->blame, &s->mutex, view,
2130 e5a0f69f 2018-08-18 stsp &s->blame_complete,
2131 e5a0f69f 2018-08-18 stsp &s->first_displayed_line,
2132 e5a0f69f 2018-08-18 stsp &s->last_displayed_line,
2133 e5a0f69f 2018-08-18 stsp &s->selected_line, &s->done, &s->eof, s->path,
2134 e5a0f69f 2018-08-18 stsp s->blamed_commit->id, s->repo);
2139 e5a0f69f 2018-08-18 stsp case KEY_ENTER:
2140 e5a0f69f 2018-08-18 stsp case '\r':
2141 e5a0f69f 2018-08-18 stsp err = open_selected_commit(&pobj, &obj,
2142 e5a0f69f 2018-08-18 stsp s->blame.lines, s->first_displayed_line,
2143 e5a0f69f 2018-08-18 stsp s->selected_line, s->repo);
2146 e5a0f69f 2018-08-18 stsp if (pobj == NULL && obj == NULL)
2148 e5a0f69f 2018-08-18 stsp diff_view = view_open(0, 0, 0, 0, view,
2149 e5a0f69f 2018-08-18 stsp TOG_VIEW_DIFF);
2150 e5a0f69f 2018-08-18 stsp if (diff_view == NULL) {
2151 e5a0f69f 2018-08-18 stsp err = got_error_from_errno();
2154 e5a0f69f 2018-08-18 stsp err = open_diff_view(diff_view, pobj, obj,
2156 e5a0f69f 2018-08-18 stsp if (err) {
2157 e5a0f69f 2018-08-18 stsp view_close(diff_view);
2160 e5a0f69f 2018-08-18 stsp *new_view = diff_view;
2161 e5a0f69f 2018-08-18 stsp if (pobj) {
2162 e5a0f69f 2018-08-18 stsp got_object_close(pobj);
2163 e5a0f69f 2018-08-18 stsp pobj = NULL;
2165 e5a0f69f 2018-08-18 stsp got_object_close(obj);
2166 e5a0f69f 2018-08-18 stsp obj = NULL;
2170 e5a0f69f 2018-08-18 stsp case KEY_NPAGE:
2172 e5a0f69f 2018-08-18 stsp if (s->last_displayed_line >= s->blame.nlines &&
2173 e5a0f69f 2018-08-18 stsp s->selected_line < view->nlines - 2) {
2174 e5a0f69f 2018-08-18 stsp s->selected_line = MIN(s->blame.nlines,
2175 e5a0f69f 2018-08-18 stsp view->nlines - 2);
2178 e5a0f69f 2018-08-18 stsp if (s->last_displayed_line + view->nlines - 2
2179 e5a0f69f 2018-08-18 stsp <= s->blame.nlines)
2180 e5a0f69f 2018-08-18 stsp s->first_displayed_line +=
2181 e5a0f69f 2018-08-18 stsp view->nlines - 2;
2183 e5a0f69f 2018-08-18 stsp s->first_displayed_line =
2184 e5a0f69f 2018-08-18 stsp s->blame.nlines -
2185 e5a0f69f 2018-08-18 stsp (view->nlines - 3);
2187 e5a0f69f 2018-08-18 stsp case KEY_RESIZE:
2188 e5a0f69f 2018-08-18 stsp if (s->selected_line > view->nlines - 2) {
2189 e5a0f69f 2018-08-18 stsp s->selected_line = MIN(s->blame.nlines,
2190 e5a0f69f 2018-08-18 stsp view->nlines - 2);
2197 e5a0f69f 2018-08-18 stsp if (pthread_mutex_unlock(&s->mutex) != 0)
2198 e5a0f69f 2018-08-18 stsp err = got_error_from_errno();
2201 b880a918 2018-07-10 stsp got_object_close(pobj);
2202 245d91c1 2018-07-12 stsp return thread_err ? thread_err : err;
2205 a70480e0 2018-06-23 stsp static const struct got_error *
2206 9f7d7167 2018-04-29 stsp cmd_blame(int argc, char *argv[])
2208 a70480e0 2018-06-23 stsp const struct got_error *error;
2209 a70480e0 2018-06-23 stsp struct got_repository *repo = NULL;
2210 69069811 2018-08-02 stsp char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2211 a70480e0 2018-06-23 stsp struct got_object_id *commit_id = NULL;
2212 a70480e0 2018-06-23 stsp char *commit_id_str = NULL;
2214 e1cd8fed 2018-08-01 stsp struct tog_view *view;
2216 a70480e0 2018-06-23 stsp #ifndef PROFILE
2217 a70480e0 2018-06-23 stsp if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
2218 a70480e0 2018-06-23 stsp err(1, "pledge");
2221 69069811 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2222 a70480e0 2018-06-23 stsp switch (ch) {
2224 a70480e0 2018-06-23 stsp commit_id_str = optarg;
2227 69069811 2018-08-02 stsp repo_path = realpath(optarg, NULL);
2228 69069811 2018-08-02 stsp if (repo_path == NULL)
2229 69069811 2018-08-02 stsp err(1, "-r option");
2233 a70480e0 2018-06-23 stsp /* NOTREACHED */
2237 a70480e0 2018-06-23 stsp argc -= optind;
2238 a70480e0 2018-06-23 stsp argv += optind;
2240 69069811 2018-08-02 stsp if (argc == 1)
2241 69069811 2018-08-02 stsp path = argv[0];
2243 a70480e0 2018-06-23 stsp usage_blame();
2245 69069811 2018-08-02 stsp cwd = getcwd(NULL, 0);
2246 69069811 2018-08-02 stsp if (cwd == NULL) {
2247 69069811 2018-08-02 stsp error = got_error_from_errno();
2248 69069811 2018-08-02 stsp goto done;
2250 69069811 2018-08-02 stsp if (repo_path == NULL) {
2251 69069811 2018-08-02 stsp repo_path = strdup(cwd);
2252 69069811 2018-08-02 stsp if (repo_path == NULL) {
2253 69069811 2018-08-02 stsp error = got_error_from_errno();
2254 69069811 2018-08-02 stsp goto done;
2259 69069811 2018-08-02 stsp error = got_repo_open(&repo, repo_path);
2260 a70480e0 2018-06-23 stsp if (error != NULL)
2261 66b4983c 2018-06-23 stsp return error;
2263 69069811 2018-08-02 stsp error = got_repo_map_path(&in_repo_path, repo, path);
2264 69069811 2018-08-02 stsp if (error != NULL)
2265 69069811 2018-08-02 stsp goto done;
2267 a70480e0 2018-06-23 stsp if (commit_id_str == NULL) {
2268 a70480e0 2018-06-23 stsp struct got_reference *head_ref;
2269 a70480e0 2018-06-23 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
2270 a70480e0 2018-06-23 stsp if (error != NULL)
2271 66b4983c 2018-06-23 stsp goto done;
2272 a70480e0 2018-06-23 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
2273 a70480e0 2018-06-23 stsp got_ref_close(head_ref);
2275 a70480e0 2018-06-23 stsp struct got_object *obj;
2276 a70480e0 2018-06-23 stsp error = got_object_open_by_id_str(&obj, repo, commit_id_str);
2277 a70480e0 2018-06-23 stsp if (error != NULL)
2278 66b4983c 2018-06-23 stsp goto done;
2279 a70480e0 2018-06-23 stsp commit_id = got_object_get_id(obj);
2280 a19e88aa 2018-06-23 stsp if (commit_id == NULL)
2281 66b4983c 2018-06-23 stsp error = got_error_from_errno();
2282 a19e88aa 2018-06-23 stsp got_object_close(obj);
2284 a19e88aa 2018-06-23 stsp if (error != NULL)
2285 a19e88aa 2018-06-23 stsp goto done;
2287 b3665f43 2018-08-04 stsp view = view_open(0, 0, 0, 0, NULL, TOG_VIEW_BLAME);
2288 e1cd8fed 2018-08-01 stsp if (view == NULL) {
2289 e1cd8fed 2018-08-01 stsp error = got_error_from_errno();
2290 e1cd8fed 2018-08-01 stsp goto done;
2292 7cbe629d 2018-08-04 stsp error = open_blame_view(view, in_repo_path, commit_id, repo);
2293 7cbe629d 2018-08-04 stsp if (error)
2294 7cbe629d 2018-08-04 stsp goto done;
2295 e5a0f69f 2018-08-18 stsp error = view_loop(view);
2297 69069811 2018-08-02 stsp free(repo_path);
2298 69069811 2018-08-02 stsp free(cwd);
2299 a70480e0 2018-06-23 stsp free(commit_id);
2301 a70480e0 2018-06-23 stsp got_repo_close(repo);
2302 a70480e0 2018-06-23 stsp return error;
2305 ffd1d5e5 2018-06-23 stsp static const struct got_error *
2306 f7d12f7e 2018-08-01 stsp draw_tree_entries(struct tog_view *view,
2307 f7d12f7e 2018-08-01 stsp struct got_tree_entry **first_displayed_entry,
2308 ffd1d5e5 2018-06-23 stsp struct got_tree_entry **last_displayed_entry,
2309 ffd1d5e5 2018-06-23 stsp struct got_tree_entry **selected_entry, int *ndisplayed,
2310 f7d12f7e 2018-08-01 stsp const char *label, int show_ids, const char *parent_path,
2311 ce52c690 2018-06-23 stsp const struct got_tree_entries *entries, int selected, int limit, int isroot)
2313 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
2314 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *te;
2315 ffd1d5e5 2018-06-23 stsp wchar_t *wline;
2316 ffd1d5e5 2018-06-23 stsp int width, n;
2318 ffd1d5e5 2018-06-23 stsp *ndisplayed = 0;
2320 f7d12f7e 2018-08-01 stsp werase(view->window);
2322 ffd1d5e5 2018-06-23 stsp if (limit == 0)
2323 ffd1d5e5 2018-06-23 stsp return NULL;
2325 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, label, view->ncols);
2327 ffd1d5e5 2018-06-23 stsp return err;
2328 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
2329 2550e4c3 2018-07-13 stsp free(wline);
2330 2550e4c3 2018-07-13 stsp wline = NULL;
2331 f7d12f7e 2018-08-01 stsp if (width < view->ncols)
2332 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
2333 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
2334 ffd1d5e5 2018-06-23 stsp return NULL;
2335 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, parent_path, view->ncols);
2337 ce52c690 2018-06-23 stsp return err;
2338 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
2339 2550e4c3 2018-07-13 stsp free(wline);
2340 2550e4c3 2018-07-13 stsp wline = NULL;
2341 f7d12f7e 2018-08-01 stsp if (width < view->ncols)
2342 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
2343 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
2344 ffd1d5e5 2018-06-23 stsp return NULL;
2345 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
2346 a1eca9bb 2018-06-23 stsp if (--limit <= 0)
2347 a1eca9bb 2018-06-23 stsp return NULL;
2349 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_FIRST(&entries->head);
2350 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry == NULL) {
2351 ffd1d5e5 2018-06-23 stsp if (selected == 0) {
2352 f7d12f7e 2018-08-01 stsp wstandout(view->window);
2353 ffd1d5e5 2018-06-23 stsp *selected_entry = NULL;
2355 f7d12f7e 2018-08-01 stsp waddstr(view->window, " ..\n"); /* parent directory */
2356 ffd1d5e5 2018-06-23 stsp if (selected == 0)
2357 f7d12f7e 2018-08-01 stsp wstandend(view->window);
2358 ffd1d5e5 2018-06-23 stsp (*ndisplayed)++;
2359 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
2360 ffd1d5e5 2018-06-23 stsp return NULL;
2364 ffd1d5e5 2018-06-23 stsp while (te != *first_displayed_entry)
2365 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_NEXT(te, entry);
2368 ffd1d5e5 2018-06-23 stsp while (te) {
2369 1d13200f 2018-07-12 stsp char *line = NULL, *id_str = NULL;
2371 1d13200f 2018-07-12 stsp if (show_ids) {
2372 1d13200f 2018-07-12 stsp err = got_object_id_str(&id_str, te->id);
2374 1d13200f 2018-07-12 stsp return got_error_from_errno();
2376 1d13200f 2018-07-12 stsp if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
2377 1d13200f 2018-07-12 stsp te->name, S_ISDIR(te->mode) ? "/" : "") == -1) {
2378 1d13200f 2018-07-12 stsp free(id_str);
2379 ffd1d5e5 2018-06-23 stsp return got_error_from_errno();
2381 1d13200f 2018-07-12 stsp free(id_str);
2382 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, line, view->ncols);
2383 ffd1d5e5 2018-06-23 stsp if (err) {
2384 ffd1d5e5 2018-06-23 stsp free(line);
2387 ffd1d5e5 2018-06-23 stsp if (n == selected) {
2388 f7d12f7e 2018-08-01 stsp wstandout(view->window);
2389 ffd1d5e5 2018-06-23 stsp *selected_entry = te;
2391 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
2392 f7d12f7e 2018-08-01 stsp if (width < view->ncols)
2393 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
2394 ffd1d5e5 2018-06-23 stsp if (n == selected)
2395 f7d12f7e 2018-08-01 stsp wstandend(view->window);
2396 ffd1d5e5 2018-06-23 stsp free(line);
2397 2550e4c3 2018-07-13 stsp free(wline);
2398 2550e4c3 2018-07-13 stsp wline = NULL;
2400 ffd1d5e5 2018-06-23 stsp (*ndisplayed)++;
2401 ffd1d5e5 2018-06-23 stsp *last_displayed_entry = te;
2402 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
2404 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_NEXT(te, entry);
2407 ffd1d5e5 2018-06-23 stsp return err;
2410 ffd1d5e5 2018-06-23 stsp static void
2411 ffd1d5e5 2018-06-23 stsp tree_scroll_up(struct got_tree_entry **first_displayed_entry, int maxscroll,
2412 ffd1d5e5 2018-06-23 stsp const struct got_tree_entries *entries, int isroot)
2414 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *te, *prev;
2417 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry == NULL)
2420 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_FIRST(&entries->head);
2421 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry == te) {
2422 ffd1d5e5 2018-06-23 stsp if (!isroot)
2423 ffd1d5e5 2018-06-23 stsp *first_displayed_entry = NULL;
2427 ffd1d5e5 2018-06-23 stsp /* XXX this is stupid... switch to TAILQ? */
2428 ffd1d5e5 2018-06-23 stsp for (i = 0; i < maxscroll; i++) {
2429 ffd1d5e5 2018-06-23 stsp while (te != *first_displayed_entry) {
2430 ffd1d5e5 2018-06-23 stsp prev = te;
2431 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_NEXT(te, entry);
2433 ffd1d5e5 2018-06-23 stsp *first_displayed_entry = prev;
2434 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_FIRST(&entries->head);
2436 ffd1d5e5 2018-06-23 stsp if (!isroot && te == SIMPLEQ_FIRST(&entries->head) && i < maxscroll)
2437 ffd1d5e5 2018-06-23 stsp *first_displayed_entry = NULL;
2440 ffd1d5e5 2018-06-23 stsp static void
2441 ffd1d5e5 2018-06-23 stsp tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
2442 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *last_displayed_entry,
2443 ffd1d5e5 2018-06-23 stsp const struct got_tree_entries *entries)
2445 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *next;
2446 ffd1d5e5 2018-06-23 stsp int n = 0;
2448 ffd1d5e5 2018-06-23 stsp if (SIMPLEQ_NEXT(last_displayed_entry, entry) == NULL)
2451 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry)
2452 ffd1d5e5 2018-06-23 stsp next = SIMPLEQ_NEXT(*first_displayed_entry, entry);
2454 ffd1d5e5 2018-06-23 stsp next = SIMPLEQ_FIRST(&entries->head);
2455 ffd1d5e5 2018-06-23 stsp while (next) {
2456 ffd1d5e5 2018-06-23 stsp *first_displayed_entry = next;
2457 ffd1d5e5 2018-06-23 stsp if (++n >= maxscroll)
2459 ffd1d5e5 2018-06-23 stsp next = SIMPLEQ_NEXT(next, entry);
2463 ffd1d5e5 2018-06-23 stsp static const struct got_error *
2464 ce52c690 2018-06-23 stsp tree_entry_path(char **path, struct tog_parent_trees *parents,
2465 ce52c690 2018-06-23 stsp struct got_tree_entry *te)
2467 cb2ebc8a 2018-06-23 stsp const struct got_error *err = NULL;
2468 ffd1d5e5 2018-06-23 stsp struct tog_parent_tree *pt;
2469 ffd1d5e5 2018-06-23 stsp size_t len = 2; /* for leading slash and NUL */
2471 d9765a41 2018-06-23 stsp TAILQ_FOREACH(pt, parents, entry)
2472 ffd1d5e5 2018-06-23 stsp len += strlen(pt->selected_entry->name) + 1 /* slash */;
2474 ce52c690 2018-06-23 stsp len += strlen(te->name);
2476 ce52c690 2018-06-23 stsp *path = calloc(1, len);
2477 ffd1d5e5 2018-06-23 stsp if (path == NULL)
2478 ffd1d5e5 2018-06-23 stsp return got_error_from_errno();
2480 ce52c690 2018-06-23 stsp (*path)[0] = '/';
2481 d9765a41 2018-06-23 stsp pt = TAILQ_LAST(parents, tog_parent_trees);
2482 d9765a41 2018-06-23 stsp while (pt) {
2483 ce52c690 2018-06-23 stsp if (strlcat(*path, pt->selected_entry->name, len) >= len) {
2484 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
2485 cb2ebc8a 2018-06-23 stsp goto done;
2487 ce52c690 2018-06-23 stsp if (strlcat(*path, "/", len) >= len) {
2488 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
2489 cb2ebc8a 2018-06-23 stsp goto done;
2491 d9765a41 2018-06-23 stsp pt = TAILQ_PREV(pt, tog_parent_trees, entry);
2494 ce52c690 2018-06-23 stsp if (strlcat(*path, te->name, len) >= len) {
2495 ce52c690 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
2496 ce52c690 2018-06-23 stsp goto done;
2500 ce52c690 2018-06-23 stsp if (err) {
2501 ce52c690 2018-06-23 stsp free(*path);
2502 ce52c690 2018-06-23 stsp *path = NULL;
2504 ce52c690 2018-06-23 stsp return err;
2507 ce52c690 2018-06-23 stsp static const struct got_error *
2508 e5a0f69f 2018-08-18 stsp blame_tree_entry(struct tog_view **new_view, struct tog_view *parent_view,
2509 e5a0f69f 2018-08-18 stsp struct got_tree_entry *te, struct tog_parent_trees *parents,
2510 e5a0f69f 2018-08-18 stsp struct got_object_id *commit_id, struct got_repository *repo)
2512 ce52c690 2018-06-23 stsp const struct got_error *err = NULL;
2513 ce52c690 2018-06-23 stsp char *path;
2514 e5a0f69f 2018-08-18 stsp struct tog_view *blame_view;
2516 ce52c690 2018-06-23 stsp err = tree_entry_path(&path, parents, te);
2518 ce52c690 2018-06-23 stsp return err;
2520 e5a0f69f 2018-08-18 stsp blame_view = view_open(0, 0, 0, 0, parent_view, TOG_VIEW_BLAME);
2521 e5a0f69f 2018-08-18 stsp if (blame_view == NULL)
2522 e5a0f69f 2018-08-18 stsp return got_error_from_errno();
2524 e5a0f69f 2018-08-18 stsp err = open_blame_view(blame_view, path, commit_id, repo);
2525 e5a0f69f 2018-08-18 stsp if (err) {
2526 e5a0f69f 2018-08-18 stsp view_close(blame_view);
2527 e5a0f69f 2018-08-18 stsp free(path);
2529 e5a0f69f 2018-08-18 stsp *new_view = blame_view;
2530 69efd4c4 2018-07-18 stsp return err;
2533 69efd4c4 2018-07-18 stsp static const struct got_error *
2534 e5a0f69f 2018-08-18 stsp log_tree_entry(struct tog_view **new_view, struct tog_view *parent_view,
2535 e5a0f69f 2018-08-18 stsp struct got_tree_entry *te, struct tog_parent_trees *parents,
2536 e5a0f69f 2018-08-18 stsp struct got_object_id *commit_id, struct got_repository *repo)
2538 e5a0f69f 2018-08-18 stsp struct tog_view *log_view;
2539 69efd4c4 2018-07-18 stsp const struct got_error *err = NULL;
2540 69efd4c4 2018-07-18 stsp char *path;
2542 e5a0f69f 2018-08-18 stsp log_view = view_open(0, 0, 0, 0, parent_view, TOG_VIEW_LOG);
2543 e5a0f69f 2018-08-18 stsp if (log_view == NULL)
2544 e5a0f69f 2018-08-18 stsp return got_error_from_errno();
2546 69efd4c4 2018-07-18 stsp err = tree_entry_path(&path, parents, te);
2548 69efd4c4 2018-07-18 stsp return err;
2550 e5a0f69f 2018-08-18 stsp err = open_log_view(log_view, commit_id, repo, path);
2552 e5a0f69f 2018-08-18 stsp view_close(log_view);
2554 e5a0f69f 2018-08-18 stsp *new_view = log_view;
2555 cb2ebc8a 2018-06-23 stsp free(path);
2556 cb2ebc8a 2018-06-23 stsp return err;
2559 ffd1d5e5 2018-06-23 stsp static const struct got_error *
2560 ad80ab7b 2018-08-04 stsp open_tree_view(struct tog_view *view, struct got_tree_object *root,
2561 5221c383 2018-08-01 stsp struct got_object_id *commit_id, struct got_repository *repo)
2563 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
2564 ad80ab7b 2018-08-04 stsp char *commit_id_str = NULL;
2565 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
2567 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->parents);
2569 ffd1d5e5 2018-06-23 stsp err = got_object_id_str(&commit_id_str, commit_id);
2570 ffd1d5e5 2018-06-23 stsp if (err != NULL)
2571 ffd1d5e5 2018-06-23 stsp goto done;
2573 fb2756b9 2018-08-04 stsp if (asprintf(&s->tree_label, "commit: %s", commit_id_str) == -1) {
2574 ffd1d5e5 2018-06-23 stsp err = got_error_from_errno();
2575 ffd1d5e5 2018-06-23 stsp goto done;
2578 fb2756b9 2018-08-04 stsp s->root = s->tree = root;
2579 fb2756b9 2018-08-04 stsp s->entries = got_object_tree_get_entries(root);
2580 fb2756b9 2018-08-04 stsp s->first_displayed_entry = SIMPLEQ_FIRST(&s->entries->head);
2581 fb2756b9 2018-08-04 stsp s->commit_id = commit_id;
2582 fb2756b9 2018-08-04 stsp s->repo = repo;
2584 e5a0f69f 2018-08-18 stsp view->show = show_tree_view;
2585 e5a0f69f 2018-08-18 stsp view->input = input_tree_view;
2586 e5a0f69f 2018-08-18 stsp view->close = close_tree_view;
2588 ad80ab7b 2018-08-04 stsp free(commit_id_str);
2590 fb2756b9 2018-08-04 stsp free(s->tree_label);
2591 ad80ab7b 2018-08-04 stsp return err;
2594 e5a0f69f 2018-08-18 stsp static const struct got_error *
2595 ad80ab7b 2018-08-04 stsp close_tree_view(struct tog_view *view)
2597 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
2599 fb2756b9 2018-08-04 stsp free(s->tree_label);
2600 fb2756b9 2018-08-04 stsp while (!TAILQ_EMPTY(&s->parents)) {
2601 ad80ab7b 2018-08-04 stsp struct tog_parent_tree *parent;
2602 fb2756b9 2018-08-04 stsp parent = TAILQ_FIRST(&s->parents);
2603 fb2756b9 2018-08-04 stsp TAILQ_REMOVE(&s->parents, parent, entry);
2604 ad80ab7b 2018-08-04 stsp free(parent);
2607 fb2756b9 2018-08-04 stsp if (s->tree != s->root)
2608 fb2756b9 2018-08-04 stsp got_object_tree_close(s->tree);
2609 e5a0f69f 2018-08-18 stsp got_object_tree_close(s->root);
2611 e5a0f69f 2018-08-18 stsp return NULL;
2614 ad80ab7b 2018-08-04 stsp static const struct got_error *
2615 ad80ab7b 2018-08-04 stsp show_tree_view(struct tog_view *view)
2617 ad80ab7b 2018-08-04 stsp const struct got_error *err = NULL;
2618 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
2619 e5a0f69f 2018-08-18 stsp char *parent_path;
2621 e5a0f69f 2018-08-18 stsp err = tree_entry_path(&parent_path, &s->parents, NULL);
2623 e5a0f69f 2018-08-18 stsp return err;
2625 e5a0f69f 2018-08-18 stsp err = draw_tree_entries(view, &s->first_displayed_entry,
2626 e5a0f69f 2018-08-18 stsp &s->last_displayed_entry, &s->selected_entry,
2627 e5a0f69f 2018-08-18 stsp &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
2628 e5a0f69f 2018-08-18 stsp s->entries, s->selected, view->nlines, s->tree == s->root);
2629 e5a0f69f 2018-08-18 stsp free(parent_path);
2630 e5a0f69f 2018-08-18 stsp return err;
2633 e5a0f69f 2018-08-18 stsp static const struct got_error *
2634 e5a0f69f 2018-08-18 stsp input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
2635 e5a0f69f 2018-08-18 stsp struct tog_view *view, int ch)
2637 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
2638 e5a0f69f 2018-08-18 stsp struct tog_tree_view_state *s = &view->state.tree;
2640 e5a0f69f 2018-08-18 stsp switch (ch) {
2642 e5a0f69f 2018-08-18 stsp s->show_ids = !s->show_ids;
2645 e5a0f69f 2018-08-18 stsp if (s->selected_entry) {
2646 e5a0f69f 2018-08-18 stsp err = log_tree_entry(new_view, view,
2647 e5a0f69f 2018-08-18 stsp s->selected_entry, &s->parents,
2648 e5a0f69f 2018-08-18 stsp s->commit_id, s->repo);
2652 e5a0f69f 2018-08-18 stsp case KEY_UP:
2653 e5a0f69f 2018-08-18 stsp if (s->selected > 0)
2654 e5a0f69f 2018-08-18 stsp s->selected--;
2655 e5a0f69f 2018-08-18 stsp if (s->selected > 0)
2657 e5a0f69f 2018-08-18 stsp tree_scroll_up(&s->first_displayed_entry, 1,
2658 e5a0f69f 2018-08-18 stsp s->entries, s->tree == s->root);
2660 e5a0f69f 2018-08-18 stsp case KEY_PPAGE:
2661 e5a0f69f 2018-08-18 stsp if (SIMPLEQ_FIRST(&s->entries->head) ==
2662 e5a0f69f 2018-08-18 stsp s->first_displayed_entry) {
2663 e5a0f69f 2018-08-18 stsp if (s->tree != s->root)
2664 e5a0f69f 2018-08-18 stsp s->first_displayed_entry = NULL;
2665 e5a0f69f 2018-08-18 stsp s->selected = 0;
2668 e5a0f69f 2018-08-18 stsp tree_scroll_up(&s->first_displayed_entry,
2669 e5a0f69f 2018-08-18 stsp view->nlines, s->entries,
2670 e5a0f69f 2018-08-18 stsp s->tree == s->root);
2673 e5a0f69f 2018-08-18 stsp case KEY_DOWN:
2674 e5a0f69f 2018-08-18 stsp if (s->selected < s->ndisplayed - 1) {
2675 e5a0f69f 2018-08-18 stsp s->selected++;
2678 e5a0f69f 2018-08-18 stsp tree_scroll_down(&s->first_displayed_entry, 1,
2679 e5a0f69f 2018-08-18 stsp s->last_displayed_entry, s->entries);
2681 e5a0f69f 2018-08-18 stsp case KEY_NPAGE:
2682 e5a0f69f 2018-08-18 stsp tree_scroll_down(&s->first_displayed_entry,
2683 e5a0f69f 2018-08-18 stsp view->nlines, s->last_displayed_entry,
2684 e5a0f69f 2018-08-18 stsp s->entries);
2685 e5a0f69f 2018-08-18 stsp if (SIMPLEQ_NEXT(s->last_displayed_entry,
2688 e5a0f69f 2018-08-18 stsp /* can't scroll any further; move cursor down */
2689 e5a0f69f 2018-08-18 stsp if (s->selected < s->ndisplayed - 1)
2690 e5a0f69f 2018-08-18 stsp s->selected = s->ndisplayed - 1;
2692 e5a0f69f 2018-08-18 stsp case KEY_ENTER:
2693 e5a0f69f 2018-08-18 stsp case '\r':
2694 e5a0f69f 2018-08-18 stsp if (s->selected_entry == NULL) {
2695 e5a0f69f 2018-08-18 stsp struct tog_parent_tree *parent;
2696 31607d6c 2018-08-18 stsp case KEY_LEFT:
2697 e5a0f69f 2018-08-18 stsp /* user selected '..' */
2698 e5a0f69f 2018-08-18 stsp if (s->tree == s->root)
2700 e5a0f69f 2018-08-18 stsp parent = TAILQ_FIRST(&s->parents);
2701 e5a0f69f 2018-08-18 stsp TAILQ_REMOVE(&s->parents, parent,
2703 e5a0f69f 2018-08-18 stsp got_object_tree_close(s->tree);
2704 e5a0f69f 2018-08-18 stsp s->tree = parent->tree;
2705 e5a0f69f 2018-08-18 stsp s->entries =
2706 e5a0f69f 2018-08-18 stsp got_object_tree_get_entries(s->tree);
2707 e5a0f69f 2018-08-18 stsp s->first_displayed_entry =
2708 e5a0f69f 2018-08-18 stsp parent->first_displayed_entry;
2709 e5a0f69f 2018-08-18 stsp s->selected_entry =
2710 e5a0f69f 2018-08-18 stsp parent->selected_entry;
2711 e5a0f69f 2018-08-18 stsp s->selected = parent->selected;
2712 e5a0f69f 2018-08-18 stsp free(parent);
2713 e5a0f69f 2018-08-18 stsp } else if (S_ISDIR(s->selected_entry->mode)) {
2714 e5a0f69f 2018-08-18 stsp struct tog_parent_tree *parent;
2715 e5a0f69f 2018-08-18 stsp struct got_tree_object *child;
2716 e5a0f69f 2018-08-18 stsp err = got_object_open_as_tree(&child,
2717 e5a0f69f 2018-08-18 stsp s->repo, s->selected_entry->id);
2720 e5a0f69f 2018-08-18 stsp parent = calloc(1, sizeof(*parent));
2721 e5a0f69f 2018-08-18 stsp if (parent == NULL) {
2722 e5a0f69f 2018-08-18 stsp err = got_error_from_errno();
2725 e5a0f69f 2018-08-18 stsp parent->tree = s->tree;
2726 e5a0f69f 2018-08-18 stsp parent->first_displayed_entry =
2727 e5a0f69f 2018-08-18 stsp s->first_displayed_entry;
2728 e5a0f69f 2018-08-18 stsp parent->selected_entry = s->selected_entry;
2729 e5a0f69f 2018-08-18 stsp parent->selected = s->selected;
2730 e5a0f69f 2018-08-18 stsp TAILQ_INSERT_HEAD(&s->parents, parent, entry);
2731 e5a0f69f 2018-08-18 stsp s->tree = child;
2732 e5a0f69f 2018-08-18 stsp s->entries =
2733 e5a0f69f 2018-08-18 stsp got_object_tree_get_entries(s->tree);
2734 e5a0f69f 2018-08-18 stsp s->selected = 0;
2735 e5a0f69f 2018-08-18 stsp s->first_displayed_entry = NULL;
2736 e5a0f69f 2018-08-18 stsp } else if (S_ISREG(s->selected_entry->mode)) {
2737 e5a0f69f 2018-08-18 stsp err = blame_tree_entry(new_view, view,
2738 e5a0f69f 2018-08-18 stsp s->selected_entry, &s->parents,
2739 e5a0f69f 2018-08-18 stsp s->commit_id, s->repo);
2744 e5a0f69f 2018-08-18 stsp case KEY_RESIZE:
2745 e5a0f69f 2018-08-18 stsp if (s->selected > view->nlines)
2746 e5a0f69f 2018-08-18 stsp s->selected = s->ndisplayed - 1;
2752 ffd1d5e5 2018-06-23 stsp return err;
2755 ffd1d5e5 2018-06-23 stsp __dead static void
2756 ffd1d5e5 2018-06-23 stsp usage_tree(void)
2759 ffd1d5e5 2018-06-23 stsp fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
2760 ffd1d5e5 2018-06-23 stsp getprogname());
2764 ffd1d5e5 2018-06-23 stsp static const struct got_error *
2765 ffd1d5e5 2018-06-23 stsp cmd_tree(int argc, char *argv[])
2767 ffd1d5e5 2018-06-23 stsp const struct got_error *error;
2768 ffd1d5e5 2018-06-23 stsp struct got_repository *repo = NULL;
2769 ffd1d5e5 2018-06-23 stsp char *repo_path = NULL;
2770 ffd1d5e5 2018-06-23 stsp struct got_object_id *commit_id = NULL;
2771 ffd1d5e5 2018-06-23 stsp char *commit_id_arg = NULL;
2772 ffd1d5e5 2018-06-23 stsp struct got_commit_object *commit = NULL;
2773 ffd1d5e5 2018-06-23 stsp struct got_tree_object *tree = NULL;
2775 5221c383 2018-08-01 stsp struct tog_view *view;
2777 ffd1d5e5 2018-06-23 stsp #ifndef PROFILE
2778 ffd1d5e5 2018-06-23 stsp if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
2779 ffd1d5e5 2018-06-23 stsp err(1, "pledge");
2782 ffd1d5e5 2018-06-23 stsp while ((ch = getopt(argc, argv, "c:")) != -1) {
2783 ffd1d5e5 2018-06-23 stsp switch (ch) {
2785 ffd1d5e5 2018-06-23 stsp commit_id_arg = optarg;
2789 ffd1d5e5 2018-06-23 stsp /* NOTREACHED */
2793 ffd1d5e5 2018-06-23 stsp argc -= optind;
2794 ffd1d5e5 2018-06-23 stsp argv += optind;
2796 ffd1d5e5 2018-06-23 stsp if (argc == 0) {
2797 ffd1d5e5 2018-06-23 stsp repo_path = getcwd(NULL, 0);
2798 ffd1d5e5 2018-06-23 stsp if (repo_path == NULL)
2799 ffd1d5e5 2018-06-23 stsp return got_error_from_errno();
2800 ffd1d5e5 2018-06-23 stsp } else if (argc == 1) {
2801 ffd1d5e5 2018-06-23 stsp repo_path = realpath(argv[0], NULL);
2802 ffd1d5e5 2018-06-23 stsp if (repo_path == NULL)
2803 ffd1d5e5 2018-06-23 stsp return got_error_from_errno();
2805 ffd1d5e5 2018-06-23 stsp usage_log();
2807 ffd1d5e5 2018-06-23 stsp error = got_repo_open(&repo, repo_path);
2808 ffd1d5e5 2018-06-23 stsp free(repo_path);
2809 ffd1d5e5 2018-06-23 stsp if (error != NULL)
2810 ffd1d5e5 2018-06-23 stsp return error;
2812 ffd1d5e5 2018-06-23 stsp if (commit_id_arg == NULL) {
2813 ffd1d5e5 2018-06-23 stsp error = get_head_commit_id(&commit_id, repo);
2814 ffd1d5e5 2018-06-23 stsp if (error != NULL)
2815 ffd1d5e5 2018-06-23 stsp goto done;
2817 ffd1d5e5 2018-06-23 stsp struct got_object *obj;
2818 ffd1d5e5 2018-06-23 stsp error = got_object_open_by_id_str(&obj, repo, commit_id_arg);
2819 ffd1d5e5 2018-06-23 stsp if (error == NULL) {
2820 ffd1d5e5 2018-06-23 stsp commit_id = got_object_get_id(obj);
2821 ffd1d5e5 2018-06-23 stsp if (commit_id == NULL)
2822 ffd1d5e5 2018-06-23 stsp error = got_error_from_errno();
2825 ffd1d5e5 2018-06-23 stsp if (error != NULL)
2826 ffd1d5e5 2018-06-23 stsp goto done;
2828 ffd1d5e5 2018-06-23 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
2829 ffd1d5e5 2018-06-23 stsp if (error != NULL)
2830 ffd1d5e5 2018-06-23 stsp goto done;
2832 ffd1d5e5 2018-06-23 stsp error = got_object_open_as_tree(&tree, repo, commit->tree_id);
2833 ffd1d5e5 2018-06-23 stsp if (error != NULL)
2834 ffd1d5e5 2018-06-23 stsp goto done;
2836 b3665f43 2018-08-04 stsp view = view_open(0, 0, 0, 0, NULL, TOG_VIEW_TREE);
2837 5221c383 2018-08-01 stsp if (view == NULL) {
2838 5221c383 2018-08-01 stsp error = got_error_from_errno();
2839 5221c383 2018-08-01 stsp goto done;
2841 ad80ab7b 2018-08-04 stsp error = open_tree_view(view, tree, commit_id, repo);
2842 ad80ab7b 2018-08-04 stsp if (error)
2843 ad80ab7b 2018-08-04 stsp goto done;
2844 e5a0f69f 2018-08-18 stsp error = view_loop(view);
2846 ffd1d5e5 2018-06-23 stsp free(commit_id);
2847 ffd1d5e5 2018-06-23 stsp if (commit)
2848 ffd1d5e5 2018-06-23 stsp got_object_commit_close(commit);
2850 ffd1d5e5 2018-06-23 stsp got_object_tree_close(tree);
2852 ffd1d5e5 2018-06-23 stsp got_repo_close(repo);
2853 ffd1d5e5 2018-06-23 stsp return error;
2855 5c5136c5 2018-05-20 stsp static void
2856 9f7d7167 2018-04-29 stsp init_curses(void)
2858 9f7d7167 2018-04-29 stsp initscr();
2862 9f7d7167 2018-04-29 stsp intrflush(stdscr, FALSE);
2863 9f7d7167 2018-04-29 stsp keypad(stdscr, TRUE);
2864 1f475ad8 2018-05-10 stsp curs_set(0);
2867 4ed7e80c 2018-05-20 stsp __dead static void
2868 9f7d7167 2018-04-29 stsp usage(void)
2872 c2301be8 2018-04-30 stsp fprintf(stderr, "usage: %s [-h] [command] [arg ...]\n\n"
2873 9f7d7167 2018-04-29 stsp "Available commands:\n", getprogname());
2874 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
2875 9f7d7167 2018-04-29 stsp struct tog_cmd *cmd = &tog_commands[i];
2876 c2301be8 2018-04-30 stsp fprintf(stderr, " %s: %s\n", cmd->name, cmd->descr);
2881 c2301be8 2018-04-30 stsp static char **
2882 c2301be8 2018-04-30 stsp make_argv(const char *arg0, const char *arg1)
2884 c2301be8 2018-04-30 stsp char **argv;
2885 c2301be8 2018-04-30 stsp int argc = (arg1 == NULL ? 1 : 2);
2887 c2301be8 2018-04-30 stsp argv = calloc(argc, sizeof(char *));
2888 c2301be8 2018-04-30 stsp if (argv == NULL)
2889 c2301be8 2018-04-30 stsp err(1, "calloc");
2890 c2301be8 2018-04-30 stsp argv[0] = strdup(arg0);
2891 c2301be8 2018-04-30 stsp if (argv[0] == NULL)
2892 c2301be8 2018-04-30 stsp err(1, "calloc");
2893 c2301be8 2018-04-30 stsp if (arg1) {
2894 c2301be8 2018-04-30 stsp argv[1] = strdup(arg1);
2895 c2301be8 2018-04-30 stsp if (argv[1] == NULL)
2896 c2301be8 2018-04-30 stsp err(1, "calloc");
2899 c2301be8 2018-04-30 stsp return argv;
2903 9f7d7167 2018-04-29 stsp main(int argc, char *argv[])
2905 9f7d7167 2018-04-29 stsp const struct got_error *error = NULL;
2906 9f7d7167 2018-04-29 stsp struct tog_cmd *cmd = NULL;
2907 9f7d7167 2018-04-29 stsp int ch, hflag = 0;
2908 c2301be8 2018-04-30 stsp char **cmd_argv = NULL;
2910 9f7d7167 2018-04-29 stsp setlocale(LC_ALL, "");
2912 9f7d7167 2018-04-29 stsp while ((ch = getopt(argc, argv, "h")) != -1) {
2913 9f7d7167 2018-04-29 stsp switch (ch) {
2915 9f7d7167 2018-04-29 stsp hflag = 1;
2919 9f7d7167 2018-04-29 stsp /* NOTREACHED */
2923 9f7d7167 2018-04-29 stsp argc -= optind;
2924 9f7d7167 2018-04-29 stsp argv += optind;
2925 9f7d7167 2018-04-29 stsp optind = 0;
2926 c2301be8 2018-04-30 stsp optreset = 1;
2928 c2301be8 2018-04-30 stsp if (argc == 0) {
2929 f29d3e89 2018-06-23 stsp if (hflag)
2931 c2301be8 2018-04-30 stsp /* Build an argument vector which runs a default command. */
2932 9f7d7167 2018-04-29 stsp cmd = &tog_commands[0];
2933 c2301be8 2018-04-30 stsp cmd_argv = make_argv(cmd->name, NULL);
2938 c2301be8 2018-04-30 stsp /* Did the user specific a command? */
2939 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
2940 c2301be8 2018-04-30 stsp if (strncmp(tog_commands[i].name, argv[0],
2941 9f7d7167 2018-04-29 stsp strlen(argv[0])) == 0) {
2942 9f7d7167 2018-04-29 stsp cmd = &tog_commands[i];
2943 9f7d7167 2018-04-29 stsp if (hflag)
2944 9f7d7167 2018-04-29 stsp tog_commands[i].cmd_usage();
2948 9f7d7167 2018-04-29 stsp if (cmd == NULL) {
2949 c2301be8 2018-04-30 stsp /* Did the user specify a repository? */
2950 c2301be8 2018-04-30 stsp char *repo_path = realpath(argv[0], NULL);
2951 c2301be8 2018-04-30 stsp if (repo_path) {
2952 c2301be8 2018-04-30 stsp struct got_repository *repo;
2953 c2301be8 2018-04-30 stsp error = got_repo_open(&repo, repo_path);
2954 c2301be8 2018-04-30 stsp if (error == NULL)
2955 c2301be8 2018-04-30 stsp got_repo_close(repo);
2957 ad7de8d9 2018-04-30 stsp error = got_error_from_errno();
2958 c2301be8 2018-04-30 stsp if (error) {
2959 f29d3e89 2018-06-23 stsp if (hflag) {
2960 f29d3e89 2018-06-23 stsp fprintf(stderr, "%s: '%s' is not a "
2961 f29d3e89 2018-06-23 stsp "known command\n", getprogname(),
2965 ad7de8d9 2018-04-30 stsp fprintf(stderr, "%s: '%s' is neither a known "
2966 ad7de8d9 2018-04-30 stsp "command nor a path to a repository\n",
2967 c2301be8 2018-04-30 stsp getprogname(), argv[0]);
2968 ad7de8d9 2018-04-30 stsp free(repo_path);
2971 c2301be8 2018-04-30 stsp cmd = &tog_commands[0];
2972 c2301be8 2018-04-30 stsp cmd_argv = make_argv(cmd->name, repo_path);
2974 c2301be8 2018-04-30 stsp free(repo_path);
2978 5c5136c5 2018-05-20 stsp init_curses();
2980 c2301be8 2018-04-30 stsp error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
2981 9f7d7167 2018-04-29 stsp if (error)
2982 9f7d7167 2018-04-29 stsp goto done;
2985 c2301be8 2018-04-30 stsp free(cmd_argv);
2986 9f7d7167 2018-04-29 stsp if (error)
2987 9f7d7167 2018-04-29 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);