Blame


1 9f7d7167 2018-04-29 stsp /*
2 9f7d7167 2018-04-29 stsp * Copyright (c) 2018 Stefan Sperling <stsp@openbsd.org>
3 9f7d7167 2018-04-29 stsp *
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.
7 9f7d7167 2018-04-29 stsp *
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.
15 9f7d7167 2018-04-29 stsp */
16 9f7d7167 2018-04-29 stsp
17 80ddbec8 2018-04-29 stsp #include <sys/queue.h>
18 ffd1d5e5 2018-06-23 stsp #include <sys/stat.h>
19 80ddbec8 2018-04-29 stsp
20 31120ada 2018-04-30 stsp #include <errno.h>
21 61e69b96 2018-05-20 stsp #define _XOPEN_SOURCE_EXTENDED
22 9f7d7167 2018-04-29 stsp #include <curses.h>
23 61e69b96 2018-05-20 stsp #undef _XOPEN_SOURCE_EXTENDED
24 9f7d7167 2018-04-29 stsp #include <panel.h>
25 9f7d7167 2018-04-29 stsp #include <locale.h>
26 9f7d7167 2018-04-29 stsp #include <stdlib.h>
27 26ed57b2 2018-05-19 stsp #include <stdio.h>
28 9f7d7167 2018-04-29 stsp #include <getopt.h>
29 9f7d7167 2018-04-29 stsp #include <string.h>
30 9f7d7167 2018-04-29 stsp #include <err.h>
31 80ddbec8 2018-04-29 stsp #include <unistd.h>
32 26ed57b2 2018-05-19 stsp #include <util.h>
33 26ed57b2 2018-05-19 stsp #include <limits.h>
34 61e69b96 2018-05-20 stsp #include <wchar.h>
35 788c352e 2018-06-16 stsp #include <time.h>
36 84451b3e 2018-07-10 stsp #include <pthread.h>
37 9f7d7167 2018-04-29 stsp
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"
47 9f7d7167 2018-04-29 stsp
48 881b2d3e 2018-04-30 stsp #ifndef MIN
49 881b2d3e 2018-04-30 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
50 881b2d3e 2018-04-30 stsp #endif
51 881b2d3e 2018-04-30 stsp
52 9f7d7167 2018-04-29 stsp #ifndef nitems
53 9f7d7167 2018-04-29 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
54 9f7d7167 2018-04-29 stsp #endif
55 9f7d7167 2018-04-29 stsp
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;
61 9f7d7167 2018-04-29 stsp };
62 9f7d7167 2018-04-29 stsp
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);
68 9f7d7167 2018-04-29 stsp
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 *[]);
73 9f7d7167 2018-04-29 stsp
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" },
83 9f7d7167 2018-04-29 stsp };
84 9f7d7167 2018-04-29 stsp
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
90 d6b05b5b 2018-08-04 stsp };
91 d6b05b5b 2018-08-04 stsp
92 ad80ab7b 2018-08-04 stsp struct tog_diff_view_state {
93 a3404814 2018-09-02 stsp struct got_object_id *id1, *id2;
94 ad80ab7b 2018-08-04 stsp FILE *f;
95 ad80ab7b 2018-08-04 stsp int first_displayed_line;
96 ad80ab7b 2018-08-04 stsp int last_displayed_line;
97 e5a0f69f 2018-08-18 stsp int eof;
98 ad80ab7b 2018-08-04 stsp };
99 ad80ab7b 2018-08-04 stsp
100 ba4f502b 2018-08-04 stsp struct commit_queue_entry {
101 ba4f502b 2018-08-04 stsp TAILQ_ENTRY(commit_queue_entry) entry;
102 ba4f502b 2018-08-04 stsp struct got_object_id *id;
103 ba4f502b 2018-08-04 stsp struct got_commit_object *commit;
104 ba4f502b 2018-08-04 stsp };
105 ba4f502b 2018-08-04 stsp TAILQ_HEAD(commit_queue_head, commit_queue_entry);
106 ba4f502b 2018-08-04 stsp struct commit_queue {
107 ba4f502b 2018-08-04 stsp int ncommits;
108 ba4f502b 2018-08-04 stsp struct commit_queue_head head;
109 b01e7d3b 2018-08-04 stsp };
110 b01e7d3b 2018-08-04 stsp
111 b01e7d3b 2018-08-04 stsp struct tog_log_view_state {
112 b01e7d3b 2018-08-04 stsp struct got_commit_graph *graph;
113 b01e7d3b 2018-08-04 stsp struct commit_queue commits;
114 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *first_displayed_entry;
115 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *last_displayed_entry;
116 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *selected_entry;
117 b01e7d3b 2018-08-04 stsp int selected;
118 b01e7d3b 2018-08-04 stsp char *in_repo_path;
119 b01e7d3b 2018-08-04 stsp struct got_repository *repo;
120 ba4f502b 2018-08-04 stsp };
121 ba4f502b 2018-08-04 stsp
122 e9424729 2018-08-04 stsp struct tog_blame_cb_args {
123 e9424729 2018-08-04 stsp pthread_mutex_t *mutex;
124 e9424729 2018-08-04 stsp struct tog_blame_line *lines; /* one per line */
125 e9424729 2018-08-04 stsp int nlines;
126 e9424729 2018-08-04 stsp
127 e9424729 2018-08-04 stsp struct tog_view *view;
128 e9424729 2018-08-04 stsp struct got_object_id *commit_id;
129 e9424729 2018-08-04 stsp FILE *f;
130 e9424729 2018-08-04 stsp const char *path;
131 e9424729 2018-08-04 stsp int *first_displayed_line;
132 e9424729 2018-08-04 stsp int *last_displayed_line;
133 e9424729 2018-08-04 stsp int *selected_line;
134 e9424729 2018-08-04 stsp int *quit;
135 e5a0f69f 2018-08-18 stsp int *eof;
136 e9424729 2018-08-04 stsp };
137 e9424729 2018-08-04 stsp
138 e9424729 2018-08-04 stsp struct tog_blame_thread_args {
139 e9424729 2018-08-04 stsp const char *path;
140 e9424729 2018-08-04 stsp struct got_repository *repo;
141 e9424729 2018-08-04 stsp struct tog_blame_cb_args *cb_args;
142 e9424729 2018-08-04 stsp int *complete;
143 e9424729 2018-08-04 stsp };
144 e9424729 2018-08-04 stsp
145 e9424729 2018-08-04 stsp struct tog_blame {
146 e9424729 2018-08-04 stsp FILE *f;
147 e9424729 2018-08-04 stsp size_t filesize;
148 e9424729 2018-08-04 stsp struct tog_blame_line *lines;
149 e9424729 2018-08-04 stsp size_t nlines;
150 e9424729 2018-08-04 stsp pthread_t thread;
151 e9424729 2018-08-04 stsp struct tog_blame_thread_args thread_args;
152 e9424729 2018-08-04 stsp struct tog_blame_cb_args cb_args;
153 e9424729 2018-08-04 stsp const char *path;
154 e9424729 2018-08-04 stsp };
155 e9424729 2018-08-04 stsp
156 7cbe629d 2018-08-04 stsp struct tog_blame_view_state {
157 7cbe629d 2018-08-04 stsp int first_displayed_line;
158 7cbe629d 2018-08-04 stsp int last_displayed_line;
159 7cbe629d 2018-08-04 stsp int selected_line;
160 7cbe629d 2018-08-04 stsp int blame_complete;
161 e5a0f69f 2018-08-18 stsp int eof;
162 e5a0f69f 2018-08-18 stsp int done;
163 7cbe629d 2018-08-04 stsp pthread_mutex_t mutex;
164 7cbe629d 2018-08-04 stsp struct got_object_id_queue blamed_commits;
165 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
166 e5a0f69f 2018-08-18 stsp char *path;
167 7cbe629d 2018-08-04 stsp struct got_repository *repo;
168 ad80ab7b 2018-08-04 stsp struct got_object_id *commit_id;
169 e9424729 2018-08-04 stsp struct tog_blame blame;
170 ad80ab7b 2018-08-04 stsp };
171 ad80ab7b 2018-08-04 stsp
172 ad80ab7b 2018-08-04 stsp struct tog_parent_tree {
173 ad80ab7b 2018-08-04 stsp TAILQ_ENTRY(tog_parent_tree) entry;
174 ad80ab7b 2018-08-04 stsp struct got_tree_object *tree;
175 ad80ab7b 2018-08-04 stsp struct got_tree_entry *first_displayed_entry;
176 ad80ab7b 2018-08-04 stsp struct got_tree_entry *selected_entry;
177 ad80ab7b 2018-08-04 stsp int selected;
178 ad80ab7b 2018-08-04 stsp };
179 ad80ab7b 2018-08-04 stsp
180 ad80ab7b 2018-08-04 stsp TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
181 ad80ab7b 2018-08-04 stsp
182 ad80ab7b 2018-08-04 stsp struct tog_tree_view_state {
183 ad80ab7b 2018-08-04 stsp char *tree_label;
184 ad80ab7b 2018-08-04 stsp struct got_tree_object *root;
185 ad80ab7b 2018-08-04 stsp struct got_tree_object *tree;
186 ad80ab7b 2018-08-04 stsp const struct got_tree_entries *entries;
187 ad80ab7b 2018-08-04 stsp struct got_tree_entry *first_displayed_entry;
188 ad80ab7b 2018-08-04 stsp struct got_tree_entry *last_displayed_entry;
189 ad80ab7b 2018-08-04 stsp struct got_tree_entry *selected_entry;
190 ad80ab7b 2018-08-04 stsp int nentries, ndisplayed, selected, show_ids;
191 ad80ab7b 2018-08-04 stsp struct tog_parent_trees parents;
192 7cbe629d 2018-08-04 stsp struct got_object_id *commit_id;
193 ad80ab7b 2018-08-04 stsp struct got_repository *repo;
194 7cbe629d 2018-08-04 stsp };
195 7cbe629d 2018-08-04 stsp
196 bcbd79e2 2018-08-19 stsp TAILQ_HEAD(tog_view_list_head, tog_view);
197 cc3c9aac 2018-08-01 stsp struct tog_view {
198 e5a0f69f 2018-08-18 stsp TAILQ_ENTRY(tog_view) entry;
199 26ed57b2 2018-05-19 stsp WINDOW *window;
200 26ed57b2 2018-05-19 stsp PANEL *panel;
201 97ddc146 2018-08-01 stsp int nlines, ncols, begin_y, begin_x;
202 f7d12f7e 2018-08-01 stsp int lines, cols; /* copies of LINES and COLS */
203 a3404814 2018-09-02 stsp int focussed;
204 6d0fee91 2018-08-01 stsp struct tog_view *parent;
205 bcbd79e2 2018-08-19 stsp struct tog_view *child;
206 5dc9f4bc 2018-08-04 stsp
207 5dc9f4bc 2018-08-04 stsp /* type-specific state */
208 d6b05b5b 2018-08-04 stsp enum tog_view_type type;
209 5dc9f4bc 2018-08-04 stsp union {
210 b01e7d3b 2018-08-04 stsp struct tog_diff_view_state diff;
211 b01e7d3b 2018-08-04 stsp struct tog_log_view_state log;
212 7cbe629d 2018-08-04 stsp struct tog_blame_view_state blame;
213 ad80ab7b 2018-08-04 stsp struct tog_tree_view_state tree;
214 5dc9f4bc 2018-08-04 stsp } state;
215 e5a0f69f 2018-08-18 stsp
216 e5a0f69f 2018-08-18 stsp const struct got_error *(*show)(struct tog_view *);
217 e5a0f69f 2018-08-18 stsp const struct got_error *(*input)(struct tog_view **,
218 e5a0f69f 2018-08-18 stsp struct tog_view **, struct tog_view *, int);
219 bcbd79e2 2018-08-19 stsp const struct got_error *(*set_child)(struct tog_view *,
220 bcbd79e2 2018-08-19 stsp struct tog_view *);
221 e5a0f69f 2018-08-18 stsp const struct got_error *(*close)(struct tog_view *);
222 cc3c9aac 2018-08-01 stsp };
223 cd0acaa7 2018-05-20 stsp
224 ba4f502b 2018-08-04 stsp static const struct got_error *open_diff_view(struct tog_view *,
225 ba4f502b 2018-08-04 stsp struct got_object *, struct got_object *, struct got_repository *);
226 5dc9f4bc 2018-08-04 stsp static const struct got_error *show_diff_view(struct tog_view *);
227 e5a0f69f 2018-08-18 stsp static const struct got_error *input_diff_view(struct tog_view **,
228 e5a0f69f 2018-08-18 stsp struct tog_view **, struct tog_view *, int);
229 e5a0f69f 2018-08-18 stsp static const struct got_error* close_diff_view(struct tog_view *);
230 e5a0f69f 2018-08-18 stsp
231 ba4f502b 2018-08-04 stsp static const struct got_error *open_log_view(struct tog_view *,
232 ba4f502b 2018-08-04 stsp struct got_object_id *, struct got_repository *, const char *);
233 ba4f502b 2018-08-04 stsp static const struct got_error * show_log_view(struct tog_view *);
234 e5a0f69f 2018-08-18 stsp static const struct got_error *input_log_view(struct tog_view **,
235 e5a0f69f 2018-08-18 stsp struct tog_view **, struct tog_view *, int);
236 e5a0f69f 2018-08-18 stsp static const struct got_error *close_log_view(struct tog_view *);
237 bcbd79e2 2018-08-19 stsp static const struct got_error* set_child_log_view(struct tog_view *,
238 bcbd79e2 2018-08-19 stsp struct tog_view *);
239 e5a0f69f 2018-08-18 stsp
240 e5a0f69f 2018-08-18 stsp static const struct got_error *open_blame_view(struct tog_view *, char *,
241 5221c383 2018-08-01 stsp struct got_object_id *, struct got_repository *);
242 7cbe629d 2018-08-04 stsp static const struct got_error *show_blame_view(struct tog_view *);
243 e5a0f69f 2018-08-18 stsp static const struct got_error *input_blame_view(struct tog_view **,
244 e5a0f69f 2018-08-18 stsp struct tog_view **, struct tog_view *, int);
245 e5a0f69f 2018-08-18 stsp static const struct got_error *close_blame_view(struct tog_view *);
246 e5a0f69f 2018-08-18 stsp
247 ad80ab7b 2018-08-04 stsp static const struct got_error *open_tree_view(struct tog_view *,
248 4fc679ca 2018-08-04 stsp struct got_tree_object *, struct got_object_id *, struct got_repository *);
249 ad80ab7b 2018-08-04 stsp static const struct got_error *show_tree_view(struct tog_view *);
250 e5a0f69f 2018-08-18 stsp static const struct got_error *input_tree_view(struct tog_view **,
251 e5a0f69f 2018-08-18 stsp struct tog_view **, struct tog_view *, int);
252 e5a0f69f 2018-08-18 stsp static const struct got_error *close_tree_view(struct tog_view *);
253 26ed57b2 2018-05-19 stsp
254 e5a0f69f 2018-08-18 stsp static const struct got_error *
255 96a765a8 2018-08-04 stsp view_close(struct tog_view *view)
256 ea5e7bb5 2018-08-01 stsp {
257 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
258 e5a0f69f 2018-08-18 stsp
259 bcbd79e2 2018-08-19 stsp if (view->child)
260 bcbd79e2 2018-08-19 stsp view->child->parent = NULL;
261 bcbd79e2 2018-08-19 stsp if (view->parent)
262 bcbd79e2 2018-08-19 stsp view->parent->child = NULL;
263 e5a0f69f 2018-08-18 stsp if (view->close)
264 e5a0f69f 2018-08-18 stsp err = view->close(view);
265 ea5e7bb5 2018-08-01 stsp if (view->panel)
266 ea5e7bb5 2018-08-01 stsp del_panel(view->panel);
267 ea5e7bb5 2018-08-01 stsp if (view->window)
268 ea5e7bb5 2018-08-01 stsp delwin(view->window);
269 ea5e7bb5 2018-08-01 stsp free(view);
270 e5a0f69f 2018-08-18 stsp return err;
271 ea5e7bb5 2018-08-01 stsp }
272 ea5e7bb5 2018-08-01 stsp
273 ea5e7bb5 2018-08-01 stsp static struct tog_view *
274 b3665f43 2018-08-04 stsp view_open(int nlines, int ncols, int begin_y, int begin_x,
275 d6b05b5b 2018-08-04 stsp struct tog_view *parent, enum tog_view_type type)
276 ea5e7bb5 2018-08-01 stsp {
277 ad80ab7b 2018-08-04 stsp struct tog_view *view = calloc(1, sizeof(*view));
278 ea5e7bb5 2018-08-01 stsp
279 ea5e7bb5 2018-08-01 stsp if (view == NULL)
280 ea5e7bb5 2018-08-01 stsp return NULL;
281 ea5e7bb5 2018-08-01 stsp
282 dd04dd3f 2018-08-19 stsp if (begin_x == 0 && parent && parent->ncols - 80 > 10)
283 dd04dd3f 2018-08-19 stsp begin_x = parent->ncols - 80;
284 dd04dd3f 2018-08-19 stsp
285 6d0fee91 2018-08-01 stsp view->parent = parent;
286 bcbd79e2 2018-08-19 stsp if (parent)
287 bcbd79e2 2018-08-19 stsp parent->child = view;
288 d6b05b5b 2018-08-04 stsp view->type = type;
289 f7d12f7e 2018-08-01 stsp view->lines = LINES;
290 f7d12f7e 2018-08-01 stsp view->cols = COLS;
291 207b9029 2018-08-01 stsp view->nlines = nlines ? nlines : LINES - begin_y;
292 207b9029 2018-08-01 stsp view->ncols = ncols ? ncols : COLS - begin_x;
293 97ddc146 2018-08-01 stsp view->begin_y = begin_y;
294 97ddc146 2018-08-01 stsp view->begin_x = begin_x;
295 842167bf 2018-08-01 stsp view->window = newwin(nlines, ncols, begin_y, begin_x);
296 ea5e7bb5 2018-08-01 stsp if (view->window == NULL) {
297 96a765a8 2018-08-04 stsp view_close(view);
298 ea5e7bb5 2018-08-01 stsp return NULL;
299 ea5e7bb5 2018-08-01 stsp }
300 ea5e7bb5 2018-08-01 stsp view->panel = new_panel(view->window);
301 ea5e7bb5 2018-08-01 stsp if (view->panel == NULL) {
302 96a765a8 2018-08-04 stsp view_close(view);
303 ea5e7bb5 2018-08-01 stsp return NULL;
304 ea5e7bb5 2018-08-01 stsp }
305 ea5e7bb5 2018-08-01 stsp
306 ea5e7bb5 2018-08-01 stsp keypad(view->window, TRUE);
307 ea5e7bb5 2018-08-01 stsp return view;
308 f7d12f7e 2018-08-01 stsp }
309 f7d12f7e 2018-08-01 stsp
310 4d8c2215 2018-08-19 stsp static const struct got_error *
311 6acd1bd1 2018-08-04 stsp view_show(struct tog_view *view)
312 cdf1ee82 2018-08-01 stsp {
313 e5a0f69f 2018-08-18 stsp const struct got_error *err;
314 e5a0f69f 2018-08-18 stsp
315 bcbd79e2 2018-08-19 stsp if (view->parent) {
316 bcbd79e2 2018-08-19 stsp err = view->parent->show(view->parent);
317 bcbd79e2 2018-08-19 stsp if (err)
318 bcbd79e2 2018-08-19 stsp return err;
319 bcbd79e2 2018-08-19 stsp show_panel(view->parent->panel);
320 bcbd79e2 2018-08-19 stsp }
321 bcbd79e2 2018-08-19 stsp
322 e5a0f69f 2018-08-18 stsp err = view->show(view);
323 e5a0f69f 2018-08-18 stsp if (err)
324 e5a0f69f 2018-08-18 stsp return err;
325 cdf1ee82 2018-08-01 stsp show_panel(view->panel);
326 bcbd79e2 2018-08-19 stsp
327 a3404814 2018-09-02 stsp if (view->child && view->child->begin_x > view->begin_x) {
328 bcbd79e2 2018-08-19 stsp err = view->child->show(view->child);
329 bcbd79e2 2018-08-19 stsp if (err)
330 bcbd79e2 2018-08-19 stsp return err;
331 bcbd79e2 2018-08-19 stsp show_panel(view->child->panel);
332 bcbd79e2 2018-08-19 stsp }
333 bcbd79e2 2018-08-19 stsp
334 cdf1ee82 2018-08-01 stsp update_panels();
335 e5a0f69f 2018-08-18 stsp doupdate();
336 e5a0f69f 2018-08-18 stsp
337 e5a0f69f 2018-08-18 stsp return err;
338 cdf1ee82 2018-08-01 stsp }
339 cdf1ee82 2018-08-01 stsp
340 4d8c2215 2018-08-19 stsp static const struct got_error *
341 f7d12f7e 2018-08-01 stsp view_resize(struct tog_view *view)
342 f7d12f7e 2018-08-01 stsp {
343 f7d12f7e 2018-08-01 stsp int nlines, ncols;
344 f7d12f7e 2018-08-01 stsp
345 6d0fee91 2018-08-01 stsp while (view) {
346 6d0fee91 2018-08-01 stsp if (view->lines > LINES)
347 6d0fee91 2018-08-01 stsp nlines = view->nlines - (view->lines - LINES);
348 6d0fee91 2018-08-01 stsp else
349 6d0fee91 2018-08-01 stsp nlines = view->nlines + (LINES - view->lines);
350 f7d12f7e 2018-08-01 stsp
351 6d0fee91 2018-08-01 stsp if (view->cols > COLS)
352 6d0fee91 2018-08-01 stsp ncols = view->ncols - (view->cols - COLS);
353 6d0fee91 2018-08-01 stsp else
354 6d0fee91 2018-08-01 stsp ncols = view->ncols + (COLS - view->cols);
355 f7d12f7e 2018-08-01 stsp
356 6d0fee91 2018-08-01 stsp if (wresize(view->window, nlines, ncols) == ERR)
357 6d0fee91 2018-08-01 stsp return got_error_from_errno();
358 77a567dd 2018-08-01 stsp replace_panel(view->panel, view->window);
359 f7d12f7e 2018-08-01 stsp
360 6d0fee91 2018-08-01 stsp view->nlines = nlines;
361 6d0fee91 2018-08-01 stsp view->ncols = ncols;
362 6d0fee91 2018-08-01 stsp view->lines = LINES;
363 6d0fee91 2018-08-01 stsp view->cols = COLS;
364 6d0fee91 2018-08-01 stsp
365 6d0fee91 2018-08-01 stsp view = view->parent;
366 6d0fee91 2018-08-01 stsp }
367 6d0fee91 2018-08-01 stsp
368 f7d12f7e 2018-08-01 stsp return NULL;
369 e5a0f69f 2018-08-18 stsp }
370 e5a0f69f 2018-08-18 stsp
371 e5a0f69f 2018-08-18 stsp static const struct got_error *
372 48fcc512 2018-08-18 stsp view_input(struct tog_view **new, struct tog_view **dead,
373 e4197bf9 2018-08-18 stsp struct tog_view **focus, int *done, struct tog_view *view,
374 48fcc512 2018-08-18 stsp struct tog_view_list_head *views)
375 e5a0f69f 2018-08-18 stsp {
376 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
377 31607d6c 2018-08-18 stsp struct tog_view *next, *prev;
378 e5a0f69f 2018-08-18 stsp int ch;
379 e5a0f69f 2018-08-18 stsp
380 e5a0f69f 2018-08-18 stsp *new = NULL;
381 e5a0f69f 2018-08-18 stsp *dead = NULL;
382 e5a0f69f 2018-08-18 stsp
383 e5a0f69f 2018-08-18 stsp nodelay(stdscr, FALSE);
384 e5a0f69f 2018-08-18 stsp ch = wgetch(view->window);
385 e5a0f69f 2018-08-18 stsp nodelay(stdscr, TRUE);
386 e5a0f69f 2018-08-18 stsp switch (ch) {
387 e5a0f69f 2018-08-18 stsp case ERR:
388 48fcc512 2018-08-18 stsp break;
389 48fcc512 2018-08-18 stsp case '\t':
390 48fcc512 2018-08-18 stsp next = TAILQ_NEXT(view, entry);
391 48fcc512 2018-08-18 stsp if (next)
392 48fcc512 2018-08-18 stsp *focus = next;
393 48fcc512 2018-08-18 stsp else
394 48fcc512 2018-08-18 stsp *focus = TAILQ_FIRST(views);
395 a3404814 2018-09-02 stsp view->focussed = 0;
396 a3404814 2018-09-02 stsp (*focus)->focussed = 1;
397 e5a0f69f 2018-08-18 stsp break;
398 31607d6c 2018-08-18 stsp case KEY_BACKSPACE:
399 31607d6c 2018-08-18 stsp prev = TAILQ_PREV(view, tog_view_list_head, entry);
400 31607d6c 2018-08-18 stsp if (prev)
401 31607d6c 2018-08-18 stsp *focus = prev;
402 31607d6c 2018-08-18 stsp else
403 31607d6c 2018-08-18 stsp *focus = TAILQ_LAST(views, tog_view_list_head);
404 786292d9 2018-09-02 stsp view->focussed = 0;
405 786292d9 2018-09-02 stsp (*focus)->focussed = 1;
406 31607d6c 2018-08-18 stsp break;
407 e5a0f69f 2018-08-18 stsp case 'q':
408 e5a0f69f 2018-08-18 stsp err = view->input(new, dead, view, ch);
409 e5a0f69f 2018-08-18 stsp *dead = view;
410 e4197bf9 2018-08-18 stsp break;
411 e4197bf9 2018-08-18 stsp case 'Q':
412 e4197bf9 2018-08-18 stsp *done = 1;
413 e5a0f69f 2018-08-18 stsp break;
414 e5a0f69f 2018-08-18 stsp case KEY_RESIZE:
415 e5a0f69f 2018-08-18 stsp err = view_resize(view);
416 e5a0f69f 2018-08-18 stsp if (err)
417 e5a0f69f 2018-08-18 stsp return err;
418 e5a0f69f 2018-08-18 stsp err = view->input(new, dead, view, ch);
419 e5a0f69f 2018-08-18 stsp break;
420 e5a0f69f 2018-08-18 stsp default:
421 e5a0f69f 2018-08-18 stsp err = view->input(new, dead, view, ch);
422 e5a0f69f 2018-08-18 stsp break;
423 e5a0f69f 2018-08-18 stsp }
424 e5a0f69f 2018-08-18 stsp
425 e5a0f69f 2018-08-18 stsp return err;
426 e5a0f69f 2018-08-18 stsp }
427 e5a0f69f 2018-08-18 stsp
428 e5a0f69f 2018-08-18 stsp static const struct got_error *
429 bcbd79e2 2018-08-19 stsp view_set_child(struct tog_view *view, struct tog_view *child)
430 bcbd79e2 2018-08-19 stsp {
431 bcbd79e2 2018-08-19 stsp const struct got_error *err;
432 bcbd79e2 2018-08-19 stsp
433 bcbd79e2 2018-08-19 stsp if (view->set_child) {
434 bcbd79e2 2018-08-19 stsp err = view->set_child(view, child);
435 bcbd79e2 2018-08-19 stsp if (err)
436 bcbd79e2 2018-08-19 stsp return err;
437 bcbd79e2 2018-08-19 stsp }
438 bcbd79e2 2018-08-19 stsp
439 bcbd79e2 2018-08-19 stsp view->child = child;
440 bcbd79e2 2018-08-19 stsp return NULL;
441 1a57306a 2018-09-02 stsp }
442 1a57306a 2018-09-02 stsp
443 1a57306a 2018-09-02 stsp void
444 1a57306a 2018-09-02 stsp view_vborder(struct tog_view *view)
445 1a57306a 2018-09-02 stsp {
446 1a57306a 2018-09-02 stsp if (view->child == NULL)
447 1a57306a 2018-09-02 stsp return;
448 1a57306a 2018-09-02 stsp
449 1a57306a 2018-09-02 stsp mvwvline(view->window, view->begin_y, view->child->begin_x - 1,
450 1a57306a 2018-09-02 stsp got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
451 bcbd79e2 2018-08-19 stsp }
452 bcbd79e2 2018-08-19 stsp
453 a3404814 2018-09-02 stsp int
454 a3404814 2018-09-02 stsp view_needs_focus_indication(struct tog_view *view)
455 a3404814 2018-09-02 stsp {
456 a3404814 2018-09-02 stsp if (!view->focussed)
457 a3404814 2018-09-02 stsp return 0;
458 a3404814 2018-09-02 stsp
459 a3404814 2018-09-02 stsp if (view->child && view->child->begin_x > view->begin_x)
460 a3404814 2018-09-02 stsp return 1;
461 a3404814 2018-09-02 stsp
462 a3404814 2018-09-02 stsp if (view->parent && view->begin_x > view->parent->begin_x)
463 a3404814 2018-09-02 stsp return 1;
464 a3404814 2018-09-02 stsp
465 a3404814 2018-09-02 stsp return 0;
466 a3404814 2018-09-02 stsp }
467 a3404814 2018-09-02 stsp
468 bcbd79e2 2018-08-19 stsp static const struct got_error *
469 e5a0f69f 2018-08-18 stsp view_loop(struct tog_view *view)
470 e5a0f69f 2018-08-18 stsp {
471 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
472 e5a0f69f 2018-08-18 stsp struct tog_view_list_head views;
473 e5a0f69f 2018-08-18 stsp struct tog_view *new_view, *dead_view;
474 e4197bf9 2018-08-18 stsp int done = 0;
475 e5a0f69f 2018-08-18 stsp
476 e5a0f69f 2018-08-18 stsp TAILQ_INIT(&views);
477 e5a0f69f 2018-08-18 stsp TAILQ_INSERT_HEAD(&views, view, entry);
478 e5a0f69f 2018-08-18 stsp
479 a3404814 2018-09-02 stsp view->focussed = 1;
480 e4197bf9 2018-08-18 stsp while (!TAILQ_EMPTY(&views) && !done) {
481 e5a0f69f 2018-08-18 stsp err = view_show(view);
482 e5a0f69f 2018-08-18 stsp if (err)
483 e5a0f69f 2018-08-18 stsp break;
484 e4197bf9 2018-08-18 stsp err = view_input(&new_view, &dead_view, &view, &done,
485 e4197bf9 2018-08-18 stsp view, &views);
486 e5a0f69f 2018-08-18 stsp if (err)
487 e5a0f69f 2018-08-18 stsp break;
488 e5a0f69f 2018-08-18 stsp if (dead_view) {
489 89864818 2018-09-02 stsp struct tog_view *v, *t;
490 e5a0f69f 2018-08-18 stsp TAILQ_REMOVE(&views, dead_view, entry);
491 89864818 2018-09-02 stsp TAILQ_FOREACH_SAFE(v, &views, entry, t) {
492 89864818 2018-09-02 stsp if (v->parent == dead_view) {
493 89864818 2018-09-02 stsp TAILQ_REMOVE(&views, v, entry);
494 7e1e2c14 2018-09-02 stsp err = view_close(v);
495 7e1e2c14 2018-09-02 stsp if (err)
496 7e1e2c14 2018-09-02 stsp goto done;
497 89864818 2018-09-02 stsp }
498 f8405c92 2018-09-02 stsp }
499 e5a0f69f 2018-08-18 stsp if (dead_view->parent)
500 e5a0f69f 2018-08-18 stsp view = dead_view->parent;
501 e5a0f69f 2018-08-18 stsp else
502 e5a0f69f 2018-08-18 stsp view = TAILQ_LAST(&views, tog_view_list_head);
503 a3404814 2018-09-02 stsp if (view)
504 a3404814 2018-09-02 stsp view->focussed = 1;
505 e5a0f69f 2018-08-18 stsp err = view_close(dead_view);
506 e5a0f69f 2018-08-18 stsp if (err)
507 e5a0f69f 2018-08-18 stsp goto done;
508 e5a0f69f 2018-08-18 stsp }
509 bcbd79e2 2018-08-19 stsp if (new_view) {
510 a3404814 2018-09-02 stsp view->focussed = 0;
511 bcbd79e2 2018-08-19 stsp /* TODO: de-duplicate! */
512 bcbd79e2 2018-08-19 stsp TAILQ_INSERT_TAIL(&views, new_view, entry);
513 bcbd79e2 2018-08-19 stsp if (new_view->parent) {
514 bcbd79e2 2018-08-19 stsp err = view_set_child(new_view->parent, new_view);
515 bcbd79e2 2018-08-19 stsp if (err)
516 bcbd79e2 2018-08-19 stsp goto done;
517 a3404814 2018-09-02 stsp new_view->parent->focussed = 0;
518 bcbd79e2 2018-08-19 stsp }
519 bcbd79e2 2018-08-19 stsp view = new_view;
520 a3404814 2018-09-02 stsp view->focussed = 1;
521 bcbd79e2 2018-08-19 stsp }
522 e5a0f69f 2018-08-18 stsp }
523 e5a0f69f 2018-08-18 stsp done:
524 e5a0f69f 2018-08-18 stsp while (!TAILQ_EMPTY(&views)) {
525 e5a0f69f 2018-08-18 stsp view = TAILQ_FIRST(&views);
526 e5a0f69f 2018-08-18 stsp TAILQ_REMOVE(&views, view, entry);
527 e5a0f69f 2018-08-18 stsp view_close(view);
528 e5a0f69f 2018-08-18 stsp }
529 e5a0f69f 2018-08-18 stsp return err;
530 ea5e7bb5 2018-08-01 stsp }
531 ea5e7bb5 2018-08-01 stsp
532 4ed7e80c 2018-05-20 stsp __dead static void
533 9f7d7167 2018-04-29 stsp usage_log(void)
534 9f7d7167 2018-04-29 stsp {
535 80ddbec8 2018-04-29 stsp endwin();
536 c70c5802 2018-08-01 stsp fprintf(stderr,
537 c70c5802 2018-08-01 stsp "usage: %s log [-c commit] [-r repository-path] [path]\n",
538 9f7d7167 2018-04-29 stsp getprogname());
539 9f7d7167 2018-04-29 stsp exit(1);
540 80ddbec8 2018-04-29 stsp }
541 80ddbec8 2018-04-29 stsp
542 963b370f 2018-05-20 stsp /* Create newly allocated wide-character string equivalent to a byte string. */
543 80ddbec8 2018-04-29 stsp static const struct got_error *
544 963b370f 2018-05-20 stsp mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
545 963b370f 2018-05-20 stsp {
546 00dfcb92 2018-06-11 stsp char *vis = NULL;
547 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
548 963b370f 2018-05-20 stsp
549 963b370f 2018-05-20 stsp *ws = NULL;
550 963b370f 2018-05-20 stsp *wlen = mbstowcs(NULL, s, 0);
551 00dfcb92 2018-06-11 stsp if (*wlen == (size_t)-1) {
552 00dfcb92 2018-06-11 stsp int vislen;
553 00dfcb92 2018-06-11 stsp if (errno != EILSEQ)
554 00dfcb92 2018-06-11 stsp return got_error_from_errno();
555 00dfcb92 2018-06-11 stsp
556 00dfcb92 2018-06-11 stsp /* byte string invalid in current encoding; try to "fix" it */
557 00dfcb92 2018-06-11 stsp err = got_mbsavis(&vis, &vislen, s);
558 00dfcb92 2018-06-11 stsp if (err)
559 00dfcb92 2018-06-11 stsp return err;
560 00dfcb92 2018-06-11 stsp *wlen = mbstowcs(NULL, vis, 0);
561 a7f50699 2018-06-11 stsp if (*wlen == (size_t)-1) {
562 a7f50699 2018-06-11 stsp err = got_error_from_errno(); /* give up */
563 a7f50699 2018-06-11 stsp goto done;
564 a7f50699 2018-06-11 stsp }
565 00dfcb92 2018-06-11 stsp }
566 963b370f 2018-05-20 stsp
567 963b370f 2018-05-20 stsp *ws = calloc(*wlen + 1, sizeof(*ws));
568 a7f50699 2018-06-11 stsp if (*ws == NULL) {
569 a7f50699 2018-06-11 stsp err = got_error_from_errno();
570 a7f50699 2018-06-11 stsp goto done;
571 a7f50699 2018-06-11 stsp }
572 963b370f 2018-05-20 stsp
573 00dfcb92 2018-06-11 stsp if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
574 963b370f 2018-05-20 stsp err = got_error_from_errno();
575 a7f50699 2018-06-11 stsp done:
576 00dfcb92 2018-06-11 stsp free(vis);
577 963b370f 2018-05-20 stsp if (err) {
578 963b370f 2018-05-20 stsp free(*ws);
579 963b370f 2018-05-20 stsp *ws = NULL;
580 963b370f 2018-05-20 stsp *wlen = 0;
581 963b370f 2018-05-20 stsp }
582 963b370f 2018-05-20 stsp return err;
583 963b370f 2018-05-20 stsp }
584 963b370f 2018-05-20 stsp
585 963b370f 2018-05-20 stsp /* Format a line for display, ensuring that it won't overflow a width limit. */
586 963b370f 2018-05-20 stsp static const struct got_error *
587 ffd1d5e5 2018-06-23 stsp format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit)
588 963b370f 2018-05-20 stsp {
589 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
590 963b370f 2018-05-20 stsp int cols = 0;
591 963b370f 2018-05-20 stsp wchar_t *wline = NULL;
592 963b370f 2018-05-20 stsp size_t wlen;
593 963b370f 2018-05-20 stsp int i;
594 963b370f 2018-05-20 stsp
595 963b370f 2018-05-20 stsp *wlinep = NULL;
596 b700b5d6 2018-07-10 stsp *widthp = 0;
597 963b370f 2018-05-20 stsp
598 963b370f 2018-05-20 stsp err = mbs2ws(&wline, &wlen, line);
599 963b370f 2018-05-20 stsp if (err)
600 963b370f 2018-05-20 stsp return err;
601 963b370f 2018-05-20 stsp
602 963b370f 2018-05-20 stsp i = 0;
603 b700b5d6 2018-07-10 stsp while (i < wlen && cols < wlimit) {
604 963b370f 2018-05-20 stsp int width = wcwidth(wline[i]);
605 963b370f 2018-05-20 stsp switch (width) {
606 963b370f 2018-05-20 stsp case 0:
607 b700b5d6 2018-07-10 stsp i++;
608 963b370f 2018-05-20 stsp break;
609 963b370f 2018-05-20 stsp case 1:
610 963b370f 2018-05-20 stsp case 2:
611 3c1f04f1 2018-09-13 stsp if (cols + width <= wlimit)
612 b700b5d6 2018-07-10 stsp cols += width;
613 3c1f04f1 2018-09-13 stsp i++;
614 963b370f 2018-05-20 stsp break;
615 963b370f 2018-05-20 stsp case -1:
616 963b370f 2018-05-20 stsp if (wline[i] == L'\t')
617 b700b5d6 2018-07-10 stsp cols += TABSIZE - ((cols + 1) % TABSIZE);
618 b700b5d6 2018-07-10 stsp i++;
619 963b370f 2018-05-20 stsp break;
620 963b370f 2018-05-20 stsp default:
621 963b370f 2018-05-20 stsp err = got_error_from_errno();
622 963b370f 2018-05-20 stsp goto done;
623 963b370f 2018-05-20 stsp }
624 963b370f 2018-05-20 stsp }
625 963b370f 2018-05-20 stsp wline[i] = L'\0';
626 b700b5d6 2018-07-10 stsp if (widthp)
627 b700b5d6 2018-07-10 stsp *widthp = cols;
628 963b370f 2018-05-20 stsp done:
629 963b370f 2018-05-20 stsp if (err)
630 963b370f 2018-05-20 stsp free(wline);
631 963b370f 2018-05-20 stsp else
632 963b370f 2018-05-20 stsp *wlinep = wline;
633 963b370f 2018-05-20 stsp return err;
634 963b370f 2018-05-20 stsp }
635 963b370f 2018-05-20 stsp
636 963b370f 2018-05-20 stsp static const struct got_error *
637 2814baeb 2018-08-01 stsp draw_commit(struct tog_view *view, struct got_commit_object *commit,
638 2814baeb 2018-08-01 stsp struct got_object_id *id)
639 80ddbec8 2018-04-29 stsp {
640 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
641 b39d25c7 2018-07-10 stsp char datebuf[10]; /* YY-MM-DD + SPACE + NUL */
642 80ddbec8 2018-04-29 stsp char *logmsg0 = NULL, *logmsg = NULL;
643 6d9fbc00 2018-04-29 stsp char *author0 = NULL, *author = NULL;
644 bb737323 2018-05-20 stsp wchar_t *wlogmsg = NULL, *wauthor = NULL;
645 bb737323 2018-05-20 stsp int author_width, logmsg_width;
646 6d9fbc00 2018-04-29 stsp char *newline, *smallerthan;
647 80ddbec8 2018-04-29 stsp char *line = NULL;
648 bb737323 2018-05-20 stsp int col, limit;
649 b39d25c7 2018-07-10 stsp static const size_t date_display_cols = 9;
650 bb737323 2018-05-20 stsp static const size_t author_display_cols = 16;
651 f7d12f7e 2018-08-01 stsp const int avail = view->ncols;
652 80ddbec8 2018-04-29 stsp
653 c70c5802 2018-08-01 stsp if (strftime(datebuf, sizeof(datebuf), "%g/%m/%d ",
654 c70c5802 2018-08-01 stsp &commit->tm_committer) >= sizeof(datebuf))
655 b39d25c7 2018-07-10 stsp return got_error(GOT_ERR_NO_SPACE);
656 b39d25c7 2018-07-10 stsp
657 b39d25c7 2018-07-10 stsp if (avail < date_display_cols)
658 b39d25c7 2018-07-10 stsp limit = MIN(sizeof(datebuf) - 1, avail);
659 b39d25c7 2018-07-10 stsp else
660 b39d25c7 2018-07-10 stsp limit = MIN(date_display_cols, sizeof(datebuf) - 1);
661 2814baeb 2018-08-01 stsp waddnstr(view->window, datebuf, limit);
662 b39d25c7 2018-07-10 stsp col = limit + 1;
663 b39d25c7 2018-07-10 stsp if (col > avail)
664 b39d25c7 2018-07-10 stsp goto done;
665 b39d25c7 2018-07-10 stsp
666 6d9fbc00 2018-04-29 stsp author0 = strdup(commit->author);
667 6d9fbc00 2018-04-29 stsp if (author0 == NULL) {
668 80ddbec8 2018-04-29 stsp err = got_error_from_errno();
669 80ddbec8 2018-04-29 stsp goto done;
670 80ddbec8 2018-04-29 stsp }
671 6d9fbc00 2018-04-29 stsp author = author0;
672 6d9fbc00 2018-04-29 stsp smallerthan = strchr(author, '<');
673 6d9fbc00 2018-04-29 stsp if (smallerthan)
674 6d9fbc00 2018-04-29 stsp *smallerthan = '\0';
675 6d9fbc00 2018-04-29 stsp else {
676 6d9fbc00 2018-04-29 stsp char *at = strchr(author, '@');
677 6d9fbc00 2018-04-29 stsp if (at)
678 6d9fbc00 2018-04-29 stsp *at = '\0';
679 6d9fbc00 2018-04-29 stsp }
680 b39d25c7 2018-07-10 stsp limit = avail - col;
681 bb737323 2018-05-20 stsp err = format_line(&wauthor, &author_width, author, limit);
682 bb737323 2018-05-20 stsp if (err)
683 bb737323 2018-05-20 stsp goto done;
684 2814baeb 2018-08-01 stsp waddwstr(view->window, wauthor);
685 bb737323 2018-05-20 stsp col += author_width;
686 9c2eaf34 2018-05-20 stsp while (col <= avail && author_width < author_display_cols + 1) {
687 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
688 bb737323 2018-05-20 stsp col++;
689 bb737323 2018-05-20 stsp author_width++;
690 bb737323 2018-05-20 stsp }
691 9c2eaf34 2018-05-20 stsp if (col > avail)
692 9c2eaf34 2018-05-20 stsp goto done;
693 80ddbec8 2018-04-29 stsp
694 bb737323 2018-05-20 stsp logmsg0 = strdup(commit->logmsg);
695 bb737323 2018-05-20 stsp if (logmsg0 == NULL) {
696 6d9fbc00 2018-04-29 stsp err = got_error_from_errno();
697 6d9fbc00 2018-04-29 stsp goto done;
698 6d9fbc00 2018-04-29 stsp }
699 bb737323 2018-05-20 stsp logmsg = logmsg0;
700 bb737323 2018-05-20 stsp while (*logmsg == '\n')
701 bb737323 2018-05-20 stsp logmsg++;
702 bb737323 2018-05-20 stsp newline = strchr(logmsg, '\n');
703 bb737323 2018-05-20 stsp if (newline)
704 bb737323 2018-05-20 stsp *newline = '\0';
705 bb737323 2018-05-20 stsp limit = avail - col;
706 bb737323 2018-05-20 stsp err = format_line(&wlogmsg, &logmsg_width, logmsg, limit);
707 bb737323 2018-05-20 stsp if (err)
708 bb737323 2018-05-20 stsp goto done;
709 2814baeb 2018-08-01 stsp waddwstr(view->window, wlogmsg);
710 bb737323 2018-05-20 stsp col += logmsg_width;
711 bb737323 2018-05-20 stsp while (col <= avail) {
712 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
713 bb737323 2018-05-20 stsp col++;
714 881b2d3e 2018-04-30 stsp }
715 80ddbec8 2018-04-29 stsp done:
716 80ddbec8 2018-04-29 stsp free(logmsg0);
717 bb737323 2018-05-20 stsp free(wlogmsg);
718 6d9fbc00 2018-04-29 stsp free(author0);
719 bb737323 2018-05-20 stsp free(wauthor);
720 80ddbec8 2018-04-29 stsp free(line);
721 80ddbec8 2018-04-29 stsp return err;
722 80ddbec8 2018-04-29 stsp }
723 26ed57b2 2018-05-19 stsp
724 899d86c2 2018-05-10 stsp static struct commit_queue_entry *
725 899d86c2 2018-05-10 stsp alloc_commit_queue_entry(struct got_commit_object *commit,
726 899d86c2 2018-05-10 stsp struct got_object_id *id)
727 80ddbec8 2018-04-29 stsp {
728 80ddbec8 2018-04-29 stsp struct commit_queue_entry *entry;
729 80ddbec8 2018-04-29 stsp
730 80ddbec8 2018-04-29 stsp entry = calloc(1, sizeof(*entry));
731 80ddbec8 2018-04-29 stsp if (entry == NULL)
732 899d86c2 2018-05-10 stsp return NULL;
733 99db9666 2018-05-07 stsp
734 899d86c2 2018-05-10 stsp entry->id = id;
735 99db9666 2018-05-07 stsp entry->commit = commit;
736 899d86c2 2018-05-10 stsp return entry;
737 99db9666 2018-05-07 stsp }
738 80ddbec8 2018-04-29 stsp
739 99db9666 2018-05-07 stsp static void
740 99db9666 2018-05-07 stsp pop_commit(struct commit_queue *commits)
741 99db9666 2018-05-07 stsp {
742 99db9666 2018-05-07 stsp struct commit_queue_entry *entry;
743 99db9666 2018-05-07 stsp
744 ecb28ae0 2018-07-16 stsp entry = TAILQ_FIRST(&commits->head);
745 ecb28ae0 2018-07-16 stsp TAILQ_REMOVE(&commits->head, entry, entry);
746 99db9666 2018-05-07 stsp got_object_commit_close(entry->commit);
747 ecb28ae0 2018-07-16 stsp commits->ncommits--;
748 9ba79e04 2018-06-11 stsp /* Don't free entry->id! It is owned by the commit graph. */
749 99db9666 2018-05-07 stsp free(entry);
750 99db9666 2018-05-07 stsp }
751 99db9666 2018-05-07 stsp
752 99db9666 2018-05-07 stsp static void
753 99db9666 2018-05-07 stsp free_commits(struct commit_queue *commits)
754 99db9666 2018-05-07 stsp {
755 ecb28ae0 2018-07-16 stsp while (!TAILQ_EMPTY(&commits->head))
756 99db9666 2018-05-07 stsp pop_commit(commits);
757 c4972b91 2018-05-07 stsp }
758 c4972b91 2018-05-07 stsp
759 c4972b91 2018-05-07 stsp static const struct got_error *
760 9ba79e04 2018-06-11 stsp queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
761 ecb28ae0 2018-07-16 stsp struct got_object_id *start_id, int minqueue, int init,
762 ecb28ae0 2018-07-16 stsp struct got_repository *repo, const char *path)
763 c4972b91 2018-05-07 stsp {
764 899d86c2 2018-05-10 stsp const struct got_error *err = NULL;
765 899d86c2 2018-05-10 stsp struct got_object_id *id;
766 9ba79e04 2018-06-11 stsp struct commit_queue_entry *entry;
767 31cedeaf 2018-09-15 stsp int nqueued = 0, found_obj = 0;
768 c8f60bff 2018-07-23 stsp int is_root_path = strcmp(path, "/") == 0;
769 c4972b91 2018-05-07 stsp
770 31cedeaf 2018-09-15 stsp err = got_commit_graph_iter_start(graph, start_id, repo);
771 c4972b91 2018-05-07 stsp if (err)
772 c4972b91 2018-05-07 stsp return err;
773 c4972b91 2018-05-07 stsp
774 ecb28ae0 2018-07-16 stsp entry = TAILQ_LAST(&commits->head, commit_queue_head);
775 9ba79e04 2018-06-11 stsp if (entry && got_object_id_cmp(entry->id, start_id) == 0) {
776 9ba79e04 2018-06-11 stsp /* Start ID's commit is already on the queue; skip over it. */
777 9ba79e04 2018-06-11 stsp err = got_commit_graph_iter_next(&id, graph);
778 9ba79e04 2018-06-11 stsp if (err && err->code != GOT_ERR_ITER_NEED_MORE)
779 9ba79e04 2018-06-11 stsp return err;
780 9ba79e04 2018-06-11 stsp
781 31cedeaf 2018-09-15 stsp err = got_commit_graph_fetch_commits(graph, 1, repo);
782 9ba79e04 2018-06-11 stsp if (err)
783 9ba79e04 2018-06-11 stsp return err;
784 c4972b91 2018-05-07 stsp }
785 c4972b91 2018-05-07 stsp
786 9ba79e04 2018-06-11 stsp while (1) {
787 9ba79e04 2018-06-11 stsp struct got_commit_object *commit;
788 899d86c2 2018-05-10 stsp
789 9ba79e04 2018-06-11 stsp err = got_commit_graph_iter_next(&id, graph);
790 9ba79e04 2018-06-11 stsp if (err) {
791 ecb28ae0 2018-07-16 stsp if (err->code != GOT_ERR_ITER_NEED_MORE)
792 ecb28ae0 2018-07-16 stsp break;
793 ecb28ae0 2018-07-16 stsp if (nqueued >= minqueue) {
794 9ba79e04 2018-06-11 stsp err = NULL;
795 ecb28ae0 2018-07-16 stsp break;
796 ecb28ae0 2018-07-16 stsp }
797 31cedeaf 2018-09-15 stsp err = got_commit_graph_fetch_commits(graph, 1, repo);
798 ecb28ae0 2018-07-16 stsp if (err)
799 ecb28ae0 2018-07-16 stsp return err;
800 ecb28ae0 2018-07-16 stsp continue;
801 9ba79e04 2018-06-11 stsp }
802 ecb28ae0 2018-07-16 stsp if (id == NULL)
803 ecb28ae0 2018-07-16 stsp break;
804 899d86c2 2018-05-10 stsp
805 9ba79e04 2018-06-11 stsp err = got_object_open_as_commit(&commit, repo, id);
806 9ba79e04 2018-06-11 stsp if (err)
807 9ba79e04 2018-06-11 stsp break;
808 899d86c2 2018-05-10 stsp
809 c8f60bff 2018-07-23 stsp if (!is_root_path) {
810 27d434c2 2018-09-15 stsp struct got_object_id *obj_id = NULL;
811 ecb28ae0 2018-07-16 stsp struct got_object_qid *pid;
812 ecb28ae0 2018-07-16 stsp int changed = 0;
813 ecb28ae0 2018-07-16 stsp
814 27d434c2 2018-09-15 stsp err = got_object_id_by_path(&obj_id, repo, id, path);
815 ecb28ae0 2018-07-16 stsp if (err) {
816 c8f60bff 2018-07-23 stsp got_object_commit_close(commit);
817 ecb28ae0 2018-07-16 stsp if (err->code == GOT_ERR_NO_OBJ &&
818 ecb28ae0 2018-07-16 stsp (found_obj || !init)) {
819 ecb28ae0 2018-07-16 stsp /* History stops here. */
820 ecb28ae0 2018-07-16 stsp err = got_error(GOT_ERR_ITER_COMPLETED);
821 ecb28ae0 2018-07-16 stsp }
822 ecb28ae0 2018-07-16 stsp break;
823 ecb28ae0 2018-07-16 stsp }
824 ecb28ae0 2018-07-16 stsp found_obj = 1;
825 ecb28ae0 2018-07-16 stsp
826 ecb28ae0 2018-07-16 stsp pid = SIMPLEQ_FIRST(&commit->parent_ids);
827 ecb28ae0 2018-07-16 stsp if (pid != NULL) {
828 27d434c2 2018-09-15 stsp struct got_object_id *pobj_id;
829 27d434c2 2018-09-15 stsp err = got_object_id_by_path(&pobj_id, repo,
830 ecb28ae0 2018-07-16 stsp pid->id, path);
831 ecb28ae0 2018-07-16 stsp if (err) {
832 ecb28ae0 2018-07-16 stsp if (err->code != GOT_ERR_NO_OBJ) {
833 c8f60bff 2018-07-23 stsp got_object_commit_close(commit);
834 27d434c2 2018-09-15 stsp free(obj_id);
835 ecb28ae0 2018-07-16 stsp break;
836 ecb28ae0 2018-07-16 stsp }
837 ecb28ae0 2018-07-16 stsp err = NULL;
838 ecb28ae0 2018-07-16 stsp changed = 1;
839 ecb28ae0 2018-07-16 stsp } else {
840 27d434c2 2018-09-15 stsp changed = (got_object_id_cmp(obj_id,
841 27d434c2 2018-09-15 stsp pobj_id) != 0);
842 ecb28ae0 2018-07-16 stsp }
843 27d434c2 2018-09-15 stsp free(pobj_id);
844 ecb28ae0 2018-07-16 stsp }
845 27d434c2 2018-09-15 stsp free(obj_id);
846 ecb28ae0 2018-07-16 stsp if (!changed) {
847 ecb28ae0 2018-07-16 stsp got_object_commit_close(commit);
848 ecb28ae0 2018-07-16 stsp continue;
849 ecb28ae0 2018-07-16 stsp }
850 ecb28ae0 2018-07-16 stsp }
851 ecb28ae0 2018-07-16 stsp
852 9ba79e04 2018-06-11 stsp entry = alloc_commit_queue_entry(commit, id);
853 9ba79e04 2018-06-11 stsp if (entry == NULL) {
854 9ba79e04 2018-06-11 stsp err = got_error_from_errno();
855 9ba79e04 2018-06-11 stsp break;
856 9ba79e04 2018-06-11 stsp }
857 ecb28ae0 2018-07-16 stsp TAILQ_INSERT_TAIL(&commits->head, entry, entry);
858 ecb28ae0 2018-07-16 stsp nqueued++;
859 ecb28ae0 2018-07-16 stsp commits->ncommits++;
860 899d86c2 2018-05-10 stsp }
861 899d86c2 2018-05-10 stsp
862 9ba79e04 2018-06-11 stsp return err;
863 99db9666 2018-05-07 stsp }
864 99db9666 2018-05-07 stsp
865 99db9666 2018-05-07 stsp static const struct got_error *
866 9ba79e04 2018-06-11 stsp fetch_next_commit(struct commit_queue_entry **pentry,
867 9ba79e04 2018-06-11 stsp struct commit_queue_entry *entry, struct commit_queue *commits,
868 ecb28ae0 2018-07-16 stsp struct got_commit_graph *graph, struct got_repository *repo,
869 ecb28ae0 2018-07-16 stsp const char *path)
870 899d86c2 2018-05-10 stsp {
871 899d86c2 2018-05-10 stsp const struct got_error *err = NULL;
872 899d86c2 2018-05-10 stsp
873 9ba79e04 2018-06-11 stsp *pentry = NULL;
874 899d86c2 2018-05-10 stsp
875 ecb28ae0 2018-07-16 stsp err = queue_commits(graph, commits, entry->id, 1, 0, repo, path);
876 ecb28ae0 2018-07-16 stsp if (err)
877 9ba79e04 2018-06-11 stsp return err;
878 899d86c2 2018-05-10 stsp
879 9ba79e04 2018-06-11 stsp /* Next entry to display should now be available. */
880 9ba79e04 2018-06-11 stsp *pentry = TAILQ_NEXT(entry, entry);
881 9ba79e04 2018-06-11 stsp if (*pentry == NULL)
882 9ba79e04 2018-06-11 stsp return got_error(GOT_ERR_NO_OBJ);
883 899d86c2 2018-05-10 stsp
884 9ba79e04 2018-06-11 stsp return NULL;
885 899d86c2 2018-05-10 stsp }
886 899d86c2 2018-05-10 stsp
887 899d86c2 2018-05-10 stsp static const struct got_error *
888 9ba79e04 2018-06-11 stsp get_head_commit_id(struct got_object_id **head_id, struct got_repository *repo)
889 99db9666 2018-05-07 stsp {
890 9ba79e04 2018-06-11 stsp const struct got_error *err = NULL;
891 9ba79e04 2018-06-11 stsp struct got_reference *head_ref;
892 99db9666 2018-05-07 stsp
893 9ba79e04 2018-06-11 stsp *head_id = NULL;
894 899d86c2 2018-05-10 stsp
895 9ba79e04 2018-06-11 stsp err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
896 99db9666 2018-05-07 stsp if (err)
897 99db9666 2018-05-07 stsp return err;
898 99db9666 2018-05-07 stsp
899 9ba79e04 2018-06-11 stsp err = got_ref_resolve(head_id, repo, head_ref);
900 9ba79e04 2018-06-11 stsp got_ref_close(head_ref);
901 9ba79e04 2018-06-11 stsp if (err) {
902 9ba79e04 2018-06-11 stsp *head_id = NULL;
903 99db9666 2018-05-07 stsp return err;
904 0553a4e3 2018-04-30 stsp }
905 80ddbec8 2018-04-29 stsp
906 9ba79e04 2018-06-11 stsp return NULL;
907 0553a4e3 2018-04-30 stsp }
908 0553a4e3 2018-04-30 stsp
909 0553a4e3 2018-04-30 stsp static const struct got_error *
910 2814baeb 2018-08-01 stsp draw_commits(struct tog_view *view, struct commit_queue_entry **last,
911 ecb28ae0 2018-07-16 stsp struct commit_queue_entry **selected, struct commit_queue_entry *first,
912 a7f40148 2018-07-18 stsp struct commit_queue *commits, int selected_idx, int limit,
913 a7f40148 2018-07-18 stsp struct got_commit_graph *graph, struct got_repository *repo,
914 a7f40148 2018-07-18 stsp const char *path)
915 0553a4e3 2018-04-30 stsp {
916 0553a4e3 2018-04-30 stsp const struct got_error *err = NULL;
917 0553a4e3 2018-04-30 stsp struct commit_queue_entry *entry;
918 ecb28ae0 2018-07-16 stsp int ncommits, width;
919 867c6645 2018-07-10 stsp char *id_str, *header;
920 ecb28ae0 2018-07-16 stsp wchar_t *wline;
921 0553a4e3 2018-04-30 stsp
922 e0d42f60 2018-07-22 stsp entry = first;
923 e0d42f60 2018-07-22 stsp ncommits = 0;
924 e0d42f60 2018-07-22 stsp while (entry) {
925 e0d42f60 2018-07-22 stsp if (ncommits == selected_idx) {
926 e0d42f60 2018-07-22 stsp *selected = entry;
927 e0d42f60 2018-07-22 stsp break;
928 e0d42f60 2018-07-22 stsp }
929 e0d42f60 2018-07-22 stsp entry = TAILQ_NEXT(entry, entry);
930 e0d42f60 2018-07-22 stsp ncommits++;
931 e0d42f60 2018-07-22 stsp }
932 e0d42f60 2018-07-22 stsp
933 867c6645 2018-07-10 stsp err = got_object_id_str(&id_str, (*selected)->id);
934 867c6645 2018-07-10 stsp if (err)
935 867c6645 2018-07-10 stsp return err;
936 867c6645 2018-07-10 stsp
937 c3ba6f36 2018-08-01 stsp if (path && strcmp(path, "/") != 0) {
938 ecb28ae0 2018-07-16 stsp if (asprintf(&header, "commit: %s [%s]", id_str, path) == -1) {
939 ecb28ae0 2018-07-16 stsp err = got_error_from_errno();
940 ecb28ae0 2018-07-16 stsp free(id_str);
941 ecb28ae0 2018-07-16 stsp return err;
942 ecb28ae0 2018-07-16 stsp }
943 ecb28ae0 2018-07-16 stsp } else if (asprintf(&header, "commit: %s", id_str) == -1) {
944 867c6645 2018-07-10 stsp err = got_error_from_errno();
945 867c6645 2018-07-10 stsp free(id_str);
946 867c6645 2018-07-10 stsp return err;
947 867c6645 2018-07-10 stsp }
948 ecb28ae0 2018-07-16 stsp free(id_str);
949 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, header, view->ncols);
950 ecb28ae0 2018-07-16 stsp if (err) {
951 ecb28ae0 2018-07-16 stsp free(header);
952 ecb28ae0 2018-07-16 stsp return err;
953 ecb28ae0 2018-07-16 stsp }
954 ecb28ae0 2018-07-16 stsp free(header);
955 867c6645 2018-07-10 stsp
956 2814baeb 2018-08-01 stsp werase(view->window);
957 867c6645 2018-07-10 stsp
958 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
959 a3404814 2018-09-02 stsp wstandout(view->window);
960 2814baeb 2018-08-01 stsp waddwstr(view->window, wline);
961 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
962 a3404814 2018-09-02 stsp wstandend(view->window);
963 f7d12f7e 2018-08-01 stsp if (width < view->ncols)
964 2814baeb 2018-08-01 stsp waddch(view->window, '\n');
965 ecb28ae0 2018-07-16 stsp free(wline);
966 ecb28ae0 2018-07-16 stsp if (limit <= 1)
967 ecb28ae0 2018-07-16 stsp return NULL;
968 0553a4e3 2018-04-30 stsp
969 899d86c2 2018-05-10 stsp entry = first;
970 899d86c2 2018-05-10 stsp *last = first;
971 867c6645 2018-07-10 stsp ncommits = 0;
972 899d86c2 2018-05-10 stsp while (entry) {
973 ecb28ae0 2018-07-16 stsp if (ncommits >= limit - 1)
974 899d86c2 2018-05-10 stsp break;
975 e0d42f60 2018-07-22 stsp if (ncommits == selected_idx)
976 2814baeb 2018-08-01 stsp wstandout(view->window);
977 2814baeb 2018-08-01 stsp err = draw_commit(view, entry->commit, entry->id);
978 cd0acaa7 2018-05-20 stsp if (ncommits == selected_idx)
979 2814baeb 2018-08-01 stsp wstandend(view->window);
980 0553a4e3 2018-04-30 stsp if (err)
981 0553a4e3 2018-04-30 stsp break;
982 0553a4e3 2018-04-30 stsp ncommits++;
983 899d86c2 2018-05-10 stsp *last = entry;
984 a7f40148 2018-07-18 stsp if (entry == TAILQ_LAST(&commits->head, commit_queue_head)) {
985 a7f40148 2018-07-18 stsp err = queue_commits(graph, commits, entry->id, 1,
986 a7f40148 2018-07-18 stsp 0, repo, path);
987 a7f40148 2018-07-18 stsp if (err) {
988 a7f40148 2018-07-18 stsp if (err->code != GOT_ERR_ITER_COMPLETED)
989 a7f40148 2018-07-18 stsp return err;
990 a7f40148 2018-07-18 stsp err = NULL;
991 a7f40148 2018-07-18 stsp }
992 a7f40148 2018-07-18 stsp }
993 899d86c2 2018-05-10 stsp entry = TAILQ_NEXT(entry, entry);
994 80ddbec8 2018-04-29 stsp }
995 80ddbec8 2018-04-29 stsp
996 1a57306a 2018-09-02 stsp view_vborder(view);
997 0553a4e3 2018-04-30 stsp
998 80ddbec8 2018-04-29 stsp return err;
999 9f7d7167 2018-04-29 stsp }
1000 07b55e75 2018-05-10 stsp
1001 07b55e75 2018-05-10 stsp static void
1002 16482c3b 2018-05-20 stsp scroll_up(struct commit_queue_entry **first_displayed_entry, int maxscroll,
1003 07b55e75 2018-05-10 stsp struct commit_queue *commits)
1004 07b55e75 2018-05-10 stsp {
1005 07b55e75 2018-05-10 stsp struct commit_queue_entry *entry;
1006 07b55e75 2018-05-10 stsp int nscrolled = 0;
1007 07b55e75 2018-05-10 stsp
1008 ecb28ae0 2018-07-16 stsp entry = TAILQ_FIRST(&commits->head);
1009 07b55e75 2018-05-10 stsp if (*first_displayed_entry == entry)
1010 07b55e75 2018-05-10 stsp return;
1011 9f7d7167 2018-04-29 stsp
1012 07b55e75 2018-05-10 stsp entry = *first_displayed_entry;
1013 16482c3b 2018-05-20 stsp while (entry && nscrolled < maxscroll) {
1014 ecb28ae0 2018-07-16 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
1015 07b55e75 2018-05-10 stsp if (entry) {
1016 07b55e75 2018-05-10 stsp *first_displayed_entry = entry;
1017 07b55e75 2018-05-10 stsp nscrolled++;
1018 07b55e75 2018-05-10 stsp }
1019 07b55e75 2018-05-10 stsp }
1020 aa075928 2018-05-10 stsp }
1021 aa075928 2018-05-10 stsp
1022 aa075928 2018-05-10 stsp static const struct got_error *
1023 16482c3b 2018-05-20 stsp scroll_down(struct commit_queue_entry **first_displayed_entry, int maxscroll,
1024 aa075928 2018-05-10 stsp struct commit_queue_entry *last_displayed_entry,
1025 9ba79e04 2018-06-11 stsp struct commit_queue *commits, struct got_commit_graph *graph,
1026 ecb28ae0 2018-07-16 stsp struct got_repository *repo, const char *path)
1027 aa075928 2018-05-10 stsp {
1028 aa075928 2018-05-10 stsp const struct got_error *err = NULL;
1029 dd0a52c1 2018-05-20 stsp struct commit_queue_entry *pentry;
1030 aa075928 2018-05-10 stsp int nscrolled = 0;
1031 aa075928 2018-05-10 stsp
1032 aa075928 2018-05-10 stsp do {
1033 dd0a52c1 2018-05-20 stsp pentry = TAILQ_NEXT(last_displayed_entry, entry);
1034 aa075928 2018-05-10 stsp if (pentry == NULL) {
1035 9ba79e04 2018-06-11 stsp err = fetch_next_commit(&pentry, last_displayed_entry,
1036 ecb28ae0 2018-07-16 stsp commits, graph, repo, path);
1037 9a6bf2a5 2018-05-20 stsp if (err || pentry == NULL)
1038 aa075928 2018-05-10 stsp break;
1039 aa075928 2018-05-10 stsp }
1040 dd0a52c1 2018-05-20 stsp last_displayed_entry = pentry;
1041 aa075928 2018-05-10 stsp
1042 dd0a52c1 2018-05-20 stsp pentry = TAILQ_NEXT(*first_displayed_entry, entry);
1043 dd0a52c1 2018-05-20 stsp if (pentry == NULL)
1044 dd0a52c1 2018-05-20 stsp break;
1045 aa075928 2018-05-10 stsp *first_displayed_entry = pentry;
1046 16482c3b 2018-05-20 stsp } while (++nscrolled < maxscroll);
1047 aa075928 2018-05-10 stsp
1048 dd0a52c1 2018-05-20 stsp return err;
1049 07b55e75 2018-05-10 stsp }
1050 4a7f7875 2018-05-10 stsp
1051 cd0acaa7 2018-05-20 stsp static const struct got_error *
1052 e5a0f69f 2018-08-18 stsp show_commit(struct tog_view **new_view, struct tog_view *parent_view,
1053 e5a0f69f 2018-08-18 stsp struct commit_queue_entry *entry, struct got_repository *repo)
1054 cd0acaa7 2018-05-20 stsp {
1055 cd0acaa7 2018-05-20 stsp const struct got_error *err;
1056 cd0acaa7 2018-05-20 stsp struct got_object *obj1 = NULL, *obj2 = NULL;
1057 9ba79e04 2018-06-11 stsp struct got_object_qid *parent_id;
1058 e5a0f69f 2018-08-18 stsp struct tog_view *diff_view;
1059 cd0acaa7 2018-05-20 stsp
1060 cd0acaa7 2018-05-20 stsp err = got_object_open(&obj2, repo, entry->id);
1061 cd0acaa7 2018-05-20 stsp if (err)
1062 cd0acaa7 2018-05-20 stsp return err;
1063 cd0acaa7 2018-05-20 stsp
1064 9ba79e04 2018-06-11 stsp parent_id = SIMPLEQ_FIRST(&entry->commit->parent_ids);
1065 9ba79e04 2018-06-11 stsp if (parent_id) {
1066 9ba79e04 2018-06-11 stsp err = got_object_open(&obj1, repo, parent_id->id);
1067 cd0acaa7 2018-05-20 stsp if (err)
1068 cd0acaa7 2018-05-20 stsp goto done;
1069 cd0acaa7 2018-05-20 stsp }
1070 cd0acaa7 2018-05-20 stsp
1071 e5a0f69f 2018-08-18 stsp diff_view = view_open(0, 0, 0, 0, parent_view, TOG_VIEW_DIFF);
1072 e5a0f69f 2018-08-18 stsp if (diff_view == NULL) {
1073 ea5e7bb5 2018-08-01 stsp err = got_error_from_errno();
1074 ea5e7bb5 2018-08-01 stsp goto done;
1075 ea5e7bb5 2018-08-01 stsp }
1076 ea5e7bb5 2018-08-01 stsp
1077 e5a0f69f 2018-08-18 stsp err = open_diff_view(diff_view, obj1, obj2, repo);
1078 e5a0f69f 2018-08-18 stsp if (err == NULL)
1079 e5a0f69f 2018-08-18 stsp *new_view = diff_view;
1080 cd0acaa7 2018-05-20 stsp done:
1081 cd0acaa7 2018-05-20 stsp if (obj1)
1082 cd0acaa7 2018-05-20 stsp got_object_close(obj1);
1083 cd0acaa7 2018-05-20 stsp if (obj2)
1084 cd0acaa7 2018-05-20 stsp got_object_close(obj2);
1085 cd0acaa7 2018-05-20 stsp return err;
1086 4a7f7875 2018-05-10 stsp }
1087 4a7f7875 2018-05-10 stsp
1088 80ddbec8 2018-04-29 stsp static const struct got_error *
1089 e5a0f69f 2018-08-18 stsp browse_commit(struct tog_view **new_view, struct tog_view *parent_view,
1090 e5a0f69f 2018-08-18 stsp struct commit_queue_entry *entry, struct got_repository *repo)
1091 9343a5fb 2018-06-23 stsp {
1092 9343a5fb 2018-06-23 stsp const struct got_error *err = NULL;
1093 9343a5fb 2018-06-23 stsp struct got_tree_object *tree;
1094 e5a0f69f 2018-08-18 stsp struct tog_view *tree_view;
1095 9343a5fb 2018-06-23 stsp
1096 9343a5fb 2018-06-23 stsp err = got_object_open_as_tree(&tree, repo, entry->commit->tree_id);
1097 9343a5fb 2018-06-23 stsp if (err)
1098 9343a5fb 2018-06-23 stsp return err;
1099 9343a5fb 2018-06-23 stsp
1100 e5a0f69f 2018-08-18 stsp tree_view = view_open(0, 0, 0, 0, parent_view, TOG_VIEW_TREE);
1101 e5a0f69f 2018-08-18 stsp if (tree_view == NULL)
1102 e5a0f69f 2018-08-18 stsp return got_error_from_errno();
1103 e5a0f69f 2018-08-18 stsp
1104 e5a0f69f 2018-08-18 stsp err = open_tree_view(tree_view, tree, entry->id, repo);
1105 ad80ab7b 2018-08-04 stsp if (err)
1106 e5a0f69f 2018-08-18 stsp got_object_tree_close(tree);
1107 e5a0f69f 2018-08-18 stsp else
1108 e5a0f69f 2018-08-18 stsp *new_view = tree_view;
1109 9343a5fb 2018-06-23 stsp return err;
1110 bcbd79e2 2018-08-19 stsp }
1111 bcbd79e2 2018-08-19 stsp
1112 bcbd79e2 2018-08-19 stsp static const struct got_error *
1113 bcbd79e2 2018-08-19 stsp set_child_log_view(struct tog_view *view, struct tog_view *child)
1114 bcbd79e2 2018-08-19 stsp {
1115 bcbd79e2 2018-08-19 stsp struct tog_log_view_state *s = &view->state.log;
1116 bcbd79e2 2018-08-19 stsp struct tog_diff_view_state *ds;
1117 bcbd79e2 2018-08-19 stsp struct commit_queue_entry *commit, *child_entry = NULL;
1118 bcbd79e2 2018-08-19 stsp int selected_idx = 0;
1119 bcbd79e2 2018-08-19 stsp
1120 bcbd79e2 2018-08-19 stsp if (child->type != TOG_VIEW_DIFF)
1121 bcbd79e2 2018-08-19 stsp return NULL;
1122 bcbd79e2 2018-08-19 stsp ds = &child->state.diff;
1123 bcbd79e2 2018-08-19 stsp
1124 bcbd79e2 2018-08-19 stsp TAILQ_FOREACH(commit, &s->commits.head, entry) {
1125 a3404814 2018-09-02 stsp if (got_object_id_cmp(commit->id, ds->id2) == 0) {
1126 bcbd79e2 2018-08-19 stsp child_entry = commit;
1127 bcbd79e2 2018-08-19 stsp break;
1128 bcbd79e2 2018-08-19 stsp }
1129 bcbd79e2 2018-08-19 stsp }
1130 bcbd79e2 2018-08-19 stsp if (child_entry == NULL)
1131 bcbd79e2 2018-08-19 stsp return NULL;
1132 bcbd79e2 2018-08-19 stsp
1133 bcbd79e2 2018-08-19 stsp commit = s->first_displayed_entry;
1134 bcbd79e2 2018-08-19 stsp while (commit) {
1135 bcbd79e2 2018-08-19 stsp if (got_object_id_cmp(commit->id, child_entry->id) == 0) {
1136 bcbd79e2 2018-08-19 stsp s->selected_entry = child_entry;
1137 bcbd79e2 2018-08-19 stsp s->selected = selected_idx;
1138 bcbd79e2 2018-08-19 stsp break;
1139 bcbd79e2 2018-08-19 stsp }
1140 bcbd79e2 2018-08-19 stsp if (commit == s->last_displayed_entry)
1141 bcbd79e2 2018-08-19 stsp break;
1142 bcbd79e2 2018-08-19 stsp selected_idx++;
1143 bcbd79e2 2018-08-19 stsp commit = TAILQ_NEXT(commit, entry);
1144 bcbd79e2 2018-08-19 stsp }
1145 bcbd79e2 2018-08-19 stsp
1146 bcbd79e2 2018-08-19 stsp return show_log_view(view);
1147 9343a5fb 2018-06-23 stsp }
1148 9343a5fb 2018-06-23 stsp
1149 9343a5fb 2018-06-23 stsp static const struct got_error *
1150 ba4f502b 2018-08-04 stsp open_log_view(struct tog_view *view, struct got_object_id *start_id,
1151 04cc582a 2018-08-01 stsp struct got_repository *repo, const char *path)
1152 80ddbec8 2018-04-29 stsp {
1153 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
1154 9ba79e04 2018-06-11 stsp struct got_object_id *head_id = NULL;
1155 ba4f502b 2018-08-04 stsp int nfetched;
1156 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
1157 80ddbec8 2018-04-29 stsp
1158 fb2756b9 2018-08-04 stsp err = got_repo_map_path(&s->in_repo_path, repo, path);
1159 ecb28ae0 2018-07-16 stsp if (err != NULL)
1160 ecb28ae0 2018-07-16 stsp goto done;
1161 ecb28ae0 2018-07-16 stsp
1162 9ba79e04 2018-06-11 stsp err = get_head_commit_id(&head_id, repo);
1163 9ba79e04 2018-06-11 stsp if (err)
1164 9ba79e04 2018-06-11 stsp return err;
1165 9ba79e04 2018-06-11 stsp
1166 034e3b69 2018-07-22 stsp /* The graph contains all commits. */
1167 31cedeaf 2018-09-15 stsp err = got_commit_graph_open(&s->graph, head_id, "/", 0, repo);
1168 9ba79e04 2018-06-11 stsp if (err)
1169 9ba79e04 2018-06-11 stsp goto done;
1170 034e3b69 2018-07-22 stsp /* The commit queue contains a subset of commits filtered by path. */
1171 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->commits.head);
1172 fb2756b9 2018-08-04 stsp s->commits.ncommits = 0;
1173 9ba79e04 2018-06-11 stsp
1174 9ba79e04 2018-06-11 stsp /* Populate commit graph with a sufficient number of commits. */
1175 fb2756b9 2018-08-04 stsp err = got_commit_graph_fetch_commits_up_to(&nfetched, s->graph,
1176 fb2756b9 2018-08-04 stsp start_id, repo);
1177 9ba79e04 2018-06-11 stsp if (err)
1178 9ba79e04 2018-06-11 stsp goto done;
1179 9ba79e04 2018-06-11 stsp
1180 9ba79e04 2018-06-11 stsp /*
1181 9ba79e04 2018-06-11 stsp * Open the initial batch of commits, sorted in commit graph order.
1182 9ba79e04 2018-06-11 stsp * We keep all commits open throughout the lifetime of the log view
1183 9ba79e04 2018-06-11 stsp * in order to avoid having to re-fetch commits from disk while
1184 9ba79e04 2018-06-11 stsp * updating the display.
1185 9ba79e04 2018-06-11 stsp */
1186 fb2756b9 2018-08-04 stsp err = queue_commits(s->graph, &s->commits, start_id, view->nlines, 1,
1187 fb2756b9 2018-08-04 stsp repo, s->in_repo_path);
1188 55198a88 2018-07-16 stsp if (err) {
1189 55198a88 2018-07-16 stsp if (err->code != GOT_ERR_ITER_COMPLETED)
1190 55198a88 2018-07-16 stsp goto done;
1191 55198a88 2018-07-16 stsp err = NULL;
1192 2814baeb 2018-08-01 stsp }
1193 2814baeb 2018-08-01 stsp
1194 1bfa490b 2018-08-18 stsp s->first_displayed_entry =
1195 1bfa490b 2018-08-18 stsp TAILQ_FIRST(&s->commits.head);
1196 1bfa490b 2018-08-18 stsp s->selected_entry = s->first_displayed_entry;
1197 fb2756b9 2018-08-04 stsp s->repo = repo;
1198 e5a0f69f 2018-08-18 stsp
1199 e5a0f69f 2018-08-18 stsp view->show = show_log_view;
1200 e5a0f69f 2018-08-18 stsp view->input = input_log_view;
1201 e5a0f69f 2018-08-18 stsp view->close = close_log_view;
1202 bcbd79e2 2018-08-19 stsp view->set_child = set_child_log_view;
1203 ba4f502b 2018-08-04 stsp done:
1204 ba4f502b 2018-08-04 stsp free(head_id);
1205 ba4f502b 2018-08-04 stsp return err;
1206 ba4f502b 2018-08-04 stsp }
1207 ba4f502b 2018-08-04 stsp
1208 e5a0f69f 2018-08-18 stsp static const struct got_error *
1209 e5a0f69f 2018-08-18 stsp close_log_view(struct tog_view *view)
1210 ba4f502b 2018-08-04 stsp {
1211 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
1212 4d7951a5 2018-08-04 stsp
1213 fb2756b9 2018-08-04 stsp if (s->graph)
1214 fb2756b9 2018-08-04 stsp got_commit_graph_close(s->graph);
1215 fb2756b9 2018-08-04 stsp free_commits(&s->commits);
1216 fb2756b9 2018-08-04 stsp free(s->in_repo_path);
1217 e5a0f69f 2018-08-18 stsp return NULL;
1218 ba4f502b 2018-08-04 stsp }
1219 ba4f502b 2018-08-04 stsp
1220 ba4f502b 2018-08-04 stsp static const struct got_error *
1221 bcbd79e2 2018-08-19 stsp update_diff_child_view(struct tog_view *parent,
1222 bcbd79e2 2018-08-19 stsp struct commit_queue_entry *selected_entry, struct got_repository *repo)
1223 bcbd79e2 2018-08-19 stsp {
1224 bcbd79e2 2018-08-19 stsp const struct got_error *err = NULL;
1225 bcbd79e2 2018-08-19 stsp struct tog_diff_view_state *ds;
1226 bcbd79e2 2018-08-19 stsp struct got_object *obj1 = NULL, *obj2 = NULL;
1227 bcbd79e2 2018-08-19 stsp struct got_object_qid *parent_id;
1228 bcbd79e2 2018-08-19 stsp struct tog_view *child_view = parent->child;
1229 bcbd79e2 2018-08-19 stsp
1230 bcbd79e2 2018-08-19 stsp if (child_view == NULL)
1231 bcbd79e2 2018-08-19 stsp return NULL;
1232 bcbd79e2 2018-08-19 stsp if (child_view->type != TOG_VIEW_DIFF)
1233 bcbd79e2 2018-08-19 stsp return NULL;
1234 bcbd79e2 2018-08-19 stsp ds = &child_view->state.diff;
1235 a3404814 2018-09-02 stsp if (got_object_id_cmp(ds->id2, selected_entry->id) == 0)
1236 bcbd79e2 2018-08-19 stsp return NULL;
1237 bcbd79e2 2018-08-19 stsp
1238 bcbd79e2 2018-08-19 stsp err = got_object_open(&obj2, repo, selected_entry->id);
1239 bcbd79e2 2018-08-19 stsp if (err)
1240 bcbd79e2 2018-08-19 stsp return err;
1241 bcbd79e2 2018-08-19 stsp
1242 bcbd79e2 2018-08-19 stsp parent_id = SIMPLEQ_FIRST(&selected_entry->commit->parent_ids);
1243 bcbd79e2 2018-08-19 stsp if (parent_id) {
1244 bcbd79e2 2018-08-19 stsp err = got_object_open(&obj1, repo, parent_id->id);
1245 bcbd79e2 2018-08-19 stsp if (err)
1246 bcbd79e2 2018-08-19 stsp goto done;
1247 bcbd79e2 2018-08-19 stsp }
1248 bcbd79e2 2018-08-19 stsp
1249 bcbd79e2 2018-08-19 stsp err = close_diff_view(child_view);
1250 bcbd79e2 2018-08-19 stsp if (err)
1251 bcbd79e2 2018-08-19 stsp goto done;
1252 bcbd79e2 2018-08-19 stsp
1253 bcbd79e2 2018-08-19 stsp err = open_diff_view(child_view, obj1, obj2, repo);
1254 bcbd79e2 2018-08-19 stsp if (err)
1255 bcbd79e2 2018-08-19 stsp goto done;
1256 bcbd79e2 2018-08-19 stsp done:
1257 bcbd79e2 2018-08-19 stsp if (obj1)
1258 bcbd79e2 2018-08-19 stsp got_object_close(obj1);
1259 bcbd79e2 2018-08-19 stsp if (obj2)
1260 bcbd79e2 2018-08-19 stsp got_object_close(obj2);
1261 bcbd79e2 2018-08-19 stsp return err;
1262 bcbd79e2 2018-08-19 stsp }
1263 bcbd79e2 2018-08-19 stsp
1264 bcbd79e2 2018-08-19 stsp static const struct got_error *
1265 ba4f502b 2018-08-04 stsp show_log_view(struct tog_view *view)
1266 ba4f502b 2018-08-04 stsp {
1267 bcbd79e2 2018-08-19 stsp const struct got_error *err = NULL;
1268 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
1269 ba4f502b 2018-08-04 stsp
1270 bcbd79e2 2018-08-19 stsp err = draw_commits(view, &s->last_displayed_entry,
1271 e5a0f69f 2018-08-18 stsp &s->selected_entry, s->first_displayed_entry,
1272 e5a0f69f 2018-08-18 stsp &s->commits, s->selected, view->nlines, s->graph,
1273 e5a0f69f 2018-08-18 stsp s->repo, s->in_repo_path);
1274 bcbd79e2 2018-08-19 stsp if (err)
1275 bcbd79e2 2018-08-19 stsp return err;
1276 bcbd79e2 2018-08-19 stsp
1277 bcbd79e2 2018-08-19 stsp return update_diff_child_view(view, s->selected_entry, s->repo);
1278 e5a0f69f 2018-08-18 stsp }
1279 04cc582a 2018-08-01 stsp
1280 e5a0f69f 2018-08-18 stsp static const struct got_error *
1281 e5a0f69f 2018-08-18 stsp input_log_view(struct tog_view **new_view, struct tog_view **dead_view,
1282 e5a0f69f 2018-08-18 stsp struct tog_view *view, int ch)
1283 e5a0f69f 2018-08-18 stsp {
1284 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
1285 e5a0f69f 2018-08-18 stsp struct tog_log_view_state *s = &view->state.log;
1286 80ddbec8 2018-04-29 stsp
1287 e5a0f69f 2018-08-18 stsp switch (ch) {
1288 e5a0f69f 2018-08-18 stsp case 'k':
1289 e5a0f69f 2018-08-18 stsp case KEY_UP:
1290 bcbd79e2 2018-08-19 stsp case '[':
1291 e5a0f69f 2018-08-18 stsp if (s->selected > 0)
1292 e5a0f69f 2018-08-18 stsp s->selected--;
1293 e5a0f69f 2018-08-18 stsp if (s->selected > 0)
1294 31120ada 2018-04-30 stsp break;
1295 e5a0f69f 2018-08-18 stsp scroll_up(&s->first_displayed_entry, 1,
1296 e5a0f69f 2018-08-18 stsp &s->commits);
1297 e5a0f69f 2018-08-18 stsp break;
1298 e5a0f69f 2018-08-18 stsp case KEY_PPAGE:
1299 e5a0f69f 2018-08-18 stsp if (TAILQ_FIRST(&s->commits.head) ==
1300 e5a0f69f 2018-08-18 stsp s->first_displayed_entry) {
1301 e5a0f69f 2018-08-18 stsp s->selected = 0;
1302 80ddbec8 2018-04-29 stsp break;
1303 e5a0f69f 2018-08-18 stsp }
1304 e5a0f69f 2018-08-18 stsp scroll_up(&s->first_displayed_entry,
1305 e5a0f69f 2018-08-18 stsp view->nlines, &s->commits);
1306 e5a0f69f 2018-08-18 stsp break;
1307 e5a0f69f 2018-08-18 stsp case 'j':
1308 e5a0f69f 2018-08-18 stsp case KEY_DOWN:
1309 bcbd79e2 2018-08-19 stsp case ']':
1310 e5a0f69f 2018-08-18 stsp if (s->selected < MIN(view->nlines - 2,
1311 e5a0f69f 2018-08-18 stsp s->commits.ncommits - 1)) {
1312 e5a0f69f 2018-08-18 stsp s->selected++;
1313 80ddbec8 2018-04-29 stsp break;
1314 e5a0f69f 2018-08-18 stsp }
1315 e5a0f69f 2018-08-18 stsp err = scroll_down(&s->first_displayed_entry, 1,
1316 e5a0f69f 2018-08-18 stsp s->last_displayed_entry, &s->commits,
1317 e5a0f69f 2018-08-18 stsp s->graph, s->repo, s->in_repo_path);
1318 e5a0f69f 2018-08-18 stsp if (err) {
1319 4d7951a5 2018-08-04 stsp if (err->code != GOT_ERR_ITER_COMPLETED)
1320 e5a0f69f 2018-08-18 stsp break;
1321 4d7951a5 2018-08-04 stsp err = NULL;
1322 ecb28ae0 2018-07-16 stsp }
1323 e5a0f69f 2018-08-18 stsp break;
1324 e5a0f69f 2018-08-18 stsp case KEY_NPAGE: {
1325 e5a0f69f 2018-08-18 stsp struct commit_queue_entry *first;
1326 e5a0f69f 2018-08-18 stsp first = s->first_displayed_entry;
1327 e5a0f69f 2018-08-18 stsp err = scroll_down(&s->first_displayed_entry,
1328 e5a0f69f 2018-08-18 stsp view->nlines, s->last_displayed_entry,
1329 e5a0f69f 2018-08-18 stsp &s->commits, s->graph, s->repo,
1330 e5a0f69f 2018-08-18 stsp s->in_repo_path);
1331 e5a0f69f 2018-08-18 stsp if (err == NULL)
1332 cd0acaa7 2018-05-20 stsp break;
1333 e5a0f69f 2018-08-18 stsp if (err->code != GOT_ERR_ITER_COMPLETED)
1334 9343a5fb 2018-06-23 stsp break;
1335 e5a0f69f 2018-08-18 stsp if (first == s->first_displayed_entry &&
1336 e5a0f69f 2018-08-18 stsp s->selected < MIN(view->nlines - 2,
1337 e5a0f69f 2018-08-18 stsp s->commits.ncommits - 1)) {
1338 e5a0f69f 2018-08-18 stsp /* can't scroll further down */
1339 e5a0f69f 2018-08-18 stsp s->selected = MIN(view->nlines - 2,
1340 e5a0f69f 2018-08-18 stsp s->commits.ncommits - 1);
1341 e5a0f69f 2018-08-18 stsp }
1342 e5a0f69f 2018-08-18 stsp err = NULL;
1343 e5a0f69f 2018-08-18 stsp break;
1344 80ddbec8 2018-04-29 stsp }
1345 e5a0f69f 2018-08-18 stsp case KEY_RESIZE:
1346 e5a0f69f 2018-08-18 stsp if (s->selected > view->nlines - 2)
1347 e5a0f69f 2018-08-18 stsp s->selected = view->nlines - 2;
1348 e5a0f69f 2018-08-18 stsp if (s->selected > s->commits.ncommits - 1)
1349 e5a0f69f 2018-08-18 stsp s->selected = s->commits.ncommits - 1;
1350 e5a0f69f 2018-08-18 stsp break;
1351 e5a0f69f 2018-08-18 stsp case KEY_ENTER:
1352 e5a0f69f 2018-08-18 stsp case '\r':
1353 e5a0f69f 2018-08-18 stsp err = show_commit(new_view, view, s->selected_entry,
1354 e5a0f69f 2018-08-18 stsp s->repo);
1355 e5a0f69f 2018-08-18 stsp break;
1356 e5a0f69f 2018-08-18 stsp case 't':
1357 e5a0f69f 2018-08-18 stsp err = browse_commit(new_view, view, s->selected_entry,
1358 e5a0f69f 2018-08-18 stsp s->repo);
1359 e5a0f69f 2018-08-18 stsp break;
1360 e5a0f69f 2018-08-18 stsp default:
1361 e5a0f69f 2018-08-18 stsp break;
1362 899d86c2 2018-05-10 stsp }
1363 e5a0f69f 2018-08-18 stsp
1364 80ddbec8 2018-04-29 stsp return err;
1365 80ddbec8 2018-04-29 stsp }
1366 80ddbec8 2018-04-29 stsp
1367 4ed7e80c 2018-05-20 stsp static const struct got_error *
1368 9f7d7167 2018-04-29 stsp cmd_log(int argc, char *argv[])
1369 9f7d7167 2018-04-29 stsp {
1370 80ddbec8 2018-04-29 stsp const struct got_error *error;
1371 ecb28ae0 2018-07-16 stsp struct got_repository *repo = NULL;
1372 899d86c2 2018-05-10 stsp struct got_object_id *start_id = NULL;
1373 ecb28ae0 2018-07-16 stsp char *path = NULL, *repo_path = NULL, *cwd = NULL;
1374 80ddbec8 2018-04-29 stsp char *start_commit = NULL;
1375 80ddbec8 2018-04-29 stsp int ch;
1376 04cc582a 2018-08-01 stsp struct tog_view *view;
1377 80ddbec8 2018-04-29 stsp
1378 80ddbec8 2018-04-29 stsp #ifndef PROFILE
1379 ad242220 2018-09-08 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd", NULL)
1380 ad242220 2018-09-08 stsp == -1)
1381 80ddbec8 2018-04-29 stsp err(1, "pledge");
1382 80ddbec8 2018-04-29 stsp #endif
1383 80ddbec8 2018-04-29 stsp
1384 ecb28ae0 2018-07-16 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1385 80ddbec8 2018-04-29 stsp switch (ch) {
1386 80ddbec8 2018-04-29 stsp case 'c':
1387 80ddbec8 2018-04-29 stsp start_commit = optarg;
1388 80ddbec8 2018-04-29 stsp break;
1389 ecb28ae0 2018-07-16 stsp case 'r':
1390 ecb28ae0 2018-07-16 stsp repo_path = realpath(optarg, NULL);
1391 ecb28ae0 2018-07-16 stsp if (repo_path == NULL)
1392 ecb28ae0 2018-07-16 stsp err(1, "-r option");
1393 ecb28ae0 2018-07-16 stsp break;
1394 80ddbec8 2018-04-29 stsp default:
1395 80ddbec8 2018-04-29 stsp usage();
1396 80ddbec8 2018-04-29 stsp /* NOTREACHED */
1397 80ddbec8 2018-04-29 stsp }
1398 80ddbec8 2018-04-29 stsp }
1399 80ddbec8 2018-04-29 stsp
1400 80ddbec8 2018-04-29 stsp argc -= optind;
1401 80ddbec8 2018-04-29 stsp argv += optind;
1402 80ddbec8 2018-04-29 stsp
1403 ecb28ae0 2018-07-16 stsp if (argc == 0)
1404 ecb28ae0 2018-07-16 stsp path = strdup("");
1405 ecb28ae0 2018-07-16 stsp else if (argc == 1)
1406 ecb28ae0 2018-07-16 stsp path = strdup(argv[0]);
1407 ecb28ae0 2018-07-16 stsp else
1408 80ddbec8 2018-04-29 stsp usage_log();
1409 ecb28ae0 2018-07-16 stsp if (path == NULL)
1410 ecb28ae0 2018-07-16 stsp return got_error_from_errno();
1411 80ddbec8 2018-04-29 stsp
1412 ecb28ae0 2018-07-16 stsp cwd = getcwd(NULL, 0);
1413 ecb28ae0 2018-07-16 stsp if (cwd == NULL) {
1414 ecb28ae0 2018-07-16 stsp error = got_error_from_errno();
1415 ecb28ae0 2018-07-16 stsp goto done;
1416 ecb28ae0 2018-07-16 stsp }
1417 ecb28ae0 2018-07-16 stsp if (repo_path == NULL) {
1418 ecb28ae0 2018-07-16 stsp repo_path = strdup(cwd);
1419 ecb28ae0 2018-07-16 stsp if (repo_path == NULL) {
1420 ecb28ae0 2018-07-16 stsp error = got_error_from_errno();
1421 ecb28ae0 2018-07-16 stsp goto done;
1422 ecb28ae0 2018-07-16 stsp }
1423 ecb28ae0 2018-07-16 stsp }
1424 ecb28ae0 2018-07-16 stsp
1425 80ddbec8 2018-04-29 stsp error = got_repo_open(&repo, repo_path);
1426 80ddbec8 2018-04-29 stsp if (error != NULL)
1427 ecb28ae0 2018-07-16 stsp goto done;
1428 80ddbec8 2018-04-29 stsp
1429 80ddbec8 2018-04-29 stsp if (start_commit == NULL) {
1430 899d86c2 2018-05-10 stsp error = get_head_commit_id(&start_id, repo);
1431 80ddbec8 2018-04-29 stsp if (error != NULL)
1432 ecb28ae0 2018-07-16 stsp goto done;
1433 80ddbec8 2018-04-29 stsp } else {
1434 80ddbec8 2018-04-29 stsp struct got_object *obj;
1435 80ddbec8 2018-04-29 stsp error = got_object_open_by_id_str(&obj, repo, start_commit);
1436 80ddbec8 2018-04-29 stsp if (error == NULL) {
1437 6402fb3c 2018-09-15 stsp start_id = got_object_id_dup(got_object_get_id(obj));
1438 899d86c2 2018-05-10 stsp if (start_id == NULL)
1439 80ddbec8 2018-04-29 stsp error = got_error_from_errno();
1440 ecb28ae0 2018-07-16 stsp goto done;
1441 80ddbec8 2018-04-29 stsp }
1442 80ddbec8 2018-04-29 stsp }
1443 80ddbec8 2018-04-29 stsp if (error != NULL)
1444 ecb28ae0 2018-07-16 stsp goto done;
1445 ecb28ae0 2018-07-16 stsp
1446 b3665f43 2018-08-04 stsp view = view_open(0, 0, 0, 0, NULL, TOG_VIEW_LOG);
1447 04cc582a 2018-08-01 stsp if (view == NULL) {
1448 04cc582a 2018-08-01 stsp error = got_error_from_errno();
1449 04cc582a 2018-08-01 stsp goto done;
1450 04cc582a 2018-08-01 stsp }
1451 ba4f502b 2018-08-04 stsp error = open_log_view(view, start_id, repo, path);
1452 ba4f502b 2018-08-04 stsp if (error)
1453 ba4f502b 2018-08-04 stsp goto done;
1454 e5a0f69f 2018-08-18 stsp error = view_loop(view);
1455 ecb28ae0 2018-07-16 stsp done:
1456 ecb28ae0 2018-07-16 stsp free(repo_path);
1457 ecb28ae0 2018-07-16 stsp free(cwd);
1458 ecb28ae0 2018-07-16 stsp free(path);
1459 899d86c2 2018-05-10 stsp free(start_id);
1460 ecb28ae0 2018-07-16 stsp if (repo)
1461 ecb28ae0 2018-07-16 stsp got_repo_close(repo);
1462 80ddbec8 2018-04-29 stsp return error;
1463 9f7d7167 2018-04-29 stsp }
1464 9f7d7167 2018-04-29 stsp
1465 4ed7e80c 2018-05-20 stsp __dead static void
1466 9f7d7167 2018-04-29 stsp usage_diff(void)
1467 9f7d7167 2018-04-29 stsp {
1468 80ddbec8 2018-04-29 stsp endwin();
1469 9f7d7167 2018-04-29 stsp fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
1470 9f7d7167 2018-04-29 stsp getprogname());
1471 9f7d7167 2018-04-29 stsp exit(1);
1472 b304db33 2018-05-20 stsp }
1473 b304db33 2018-05-20 stsp
1474 b304db33 2018-05-20 stsp static char *
1475 b304db33 2018-05-20 stsp parse_next_line(FILE *f, size_t *len)
1476 b304db33 2018-05-20 stsp {
1477 b304db33 2018-05-20 stsp char *line;
1478 b304db33 2018-05-20 stsp size_t linelen;
1479 b304db33 2018-05-20 stsp size_t lineno;
1480 b304db33 2018-05-20 stsp const char delim[3] = { '\0', '\0', '\0'};
1481 b304db33 2018-05-20 stsp
1482 b304db33 2018-05-20 stsp line = fparseln(f, &linelen, &lineno, delim, 0);
1483 b304db33 2018-05-20 stsp if (len)
1484 b304db33 2018-05-20 stsp *len = linelen;
1485 b304db33 2018-05-20 stsp return line;
1486 26ed57b2 2018-05-19 stsp }
1487 26ed57b2 2018-05-19 stsp
1488 4ed7e80c 2018-05-20 stsp static const struct got_error *
1489 f7d12f7e 2018-08-01 stsp draw_file(struct tog_view *view, FILE *f, int *first_displayed_line,
1490 a3404814 2018-09-02 stsp int *last_displayed_line, int *eof, int max_lines,
1491 a3404814 2018-09-02 stsp char * header)
1492 26ed57b2 2018-05-19 stsp {
1493 61e69b96 2018-05-20 stsp const struct got_error *err;
1494 26ed57b2 2018-05-19 stsp int nlines = 0, nprinted = 0;
1495 b304db33 2018-05-20 stsp char *line;
1496 b304db33 2018-05-20 stsp size_t len;
1497 61e69b96 2018-05-20 stsp wchar_t *wline;
1498 e0b650dd 2018-05-20 stsp int width;
1499 26ed57b2 2018-05-19 stsp
1500 26ed57b2 2018-05-19 stsp rewind(f);
1501 f7d12f7e 2018-08-01 stsp werase(view->window);
1502 a3404814 2018-09-02 stsp
1503 a3404814 2018-09-02 stsp if (header) {
1504 a3404814 2018-09-02 stsp err = format_line(&wline, &width, header, view->ncols);
1505 a3404814 2018-09-02 stsp if (err) {
1506 a3404814 2018-09-02 stsp return err;
1507 a3404814 2018-09-02 stsp }
1508 a3404814 2018-09-02 stsp
1509 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
1510 a3404814 2018-09-02 stsp wstandout(view->window);
1511 a3404814 2018-09-02 stsp waddwstr(view->window, wline);
1512 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
1513 a3404814 2018-09-02 stsp wstandend(view->window);
1514 a3404814 2018-09-02 stsp if (width < view->ncols)
1515 a3404814 2018-09-02 stsp waddch(view->window, '\n');
1516 26ed57b2 2018-05-19 stsp
1517 a3404814 2018-09-02 stsp if (max_lines <= 1)
1518 a3404814 2018-09-02 stsp return NULL;
1519 a3404814 2018-09-02 stsp max_lines--;
1520 a3404814 2018-09-02 stsp }
1521 a3404814 2018-09-02 stsp
1522 26ed57b2 2018-05-19 stsp *eof = 0;
1523 26ed57b2 2018-05-19 stsp while (nprinted < max_lines) {
1524 b304db33 2018-05-20 stsp line = parse_next_line(f, &len);
1525 26ed57b2 2018-05-19 stsp if (line == NULL) {
1526 26ed57b2 2018-05-19 stsp *eof = 1;
1527 26ed57b2 2018-05-19 stsp break;
1528 26ed57b2 2018-05-19 stsp }
1529 26ed57b2 2018-05-19 stsp if (++nlines < *first_displayed_line) {
1530 26ed57b2 2018-05-19 stsp free(line);
1531 26ed57b2 2018-05-19 stsp continue;
1532 26ed57b2 2018-05-19 stsp }
1533 26ed57b2 2018-05-19 stsp
1534 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, line, view->ncols);
1535 61e69b96 2018-05-20 stsp if (err) {
1536 61e69b96 2018-05-20 stsp free(line);
1537 61e69b96 2018-05-20 stsp return err;
1538 61e69b96 2018-05-20 stsp }
1539 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
1540 f7d12f7e 2018-08-01 stsp if (width < view->ncols)
1541 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
1542 26ed57b2 2018-05-19 stsp if (++nprinted == 1)
1543 26ed57b2 2018-05-19 stsp *first_displayed_line = nlines;
1544 26ed57b2 2018-05-19 stsp free(line);
1545 2550e4c3 2018-07-13 stsp free(wline);
1546 2550e4c3 2018-07-13 stsp wline = NULL;
1547 26ed57b2 2018-05-19 stsp }
1548 26ed57b2 2018-05-19 stsp *last_displayed_line = nlines;
1549 26ed57b2 2018-05-19 stsp
1550 1a57306a 2018-09-02 stsp view_vborder(view);
1551 26ed57b2 2018-05-19 stsp
1552 26ed57b2 2018-05-19 stsp return NULL;
1553 9f7d7167 2018-04-29 stsp }
1554 9f7d7167 2018-04-29 stsp
1555 4ed7e80c 2018-05-20 stsp static const struct got_error *
1556 5dc9f4bc 2018-08-04 stsp open_diff_view(struct tog_view *view, struct got_object *obj1,
1557 ea5e7bb5 2018-08-01 stsp struct got_object *obj2, struct got_repository *repo)
1558 26ed57b2 2018-05-19 stsp {
1559 26ed57b2 2018-05-19 stsp const struct got_error *err;
1560 26ed57b2 2018-05-19 stsp FILE *f;
1561 26ed57b2 2018-05-19 stsp
1562 cd0acaa7 2018-05-20 stsp if (obj1 != NULL && obj2 != NULL &&
1563 cd0acaa7 2018-05-20 stsp got_object_get_type(obj1) != got_object_get_type(obj2))
1564 26ed57b2 2018-05-19 stsp return got_error(GOT_ERR_OBJ_TYPE);
1565 26ed57b2 2018-05-19 stsp
1566 511a516b 2018-05-19 stsp f = got_opentemp();
1567 26ed57b2 2018-05-19 stsp if (f == NULL)
1568 26ed57b2 2018-05-19 stsp return got_error_from_errno();
1569 26ed57b2 2018-05-19 stsp
1570 cd0acaa7 2018-05-20 stsp switch (got_object_get_type(obj1 ? obj1 : obj2)) {
1571 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_BLOB:
1572 f6861a81 2018-09-13 stsp err = got_diff_objects_as_blobs(obj1, obj2, NULL, NULL,
1573 f6861a81 2018-09-13 stsp repo, f);
1574 26ed57b2 2018-05-19 stsp break;
1575 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_TREE:
1576 f6861a81 2018-09-13 stsp err = got_diff_objects_as_trees(obj1, obj2, "", "", repo, f);
1577 26ed57b2 2018-05-19 stsp break;
1578 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_COMMIT:
1579 11528a82 2018-05-19 stsp err = got_diff_objects_as_commits(obj1, obj2, repo, f);
1580 26ed57b2 2018-05-19 stsp break;
1581 26ed57b2 2018-05-19 stsp default:
1582 26ed57b2 2018-05-19 stsp return got_error(GOT_ERR_OBJ_TYPE);
1583 26ed57b2 2018-05-19 stsp }
1584 26ed57b2 2018-05-19 stsp
1585 26ed57b2 2018-05-19 stsp fflush(f);
1586 5dc9f4bc 2018-08-04 stsp
1587 a3404814 2018-09-02 stsp view->state.diff.id1 = obj1 ? got_object_get_id(obj1) : NULL;
1588 a3404814 2018-09-02 stsp view->state.diff.id2 = got_object_get_id(obj2);
1589 5dc9f4bc 2018-08-04 stsp view->state.diff.f = f;
1590 5dc9f4bc 2018-08-04 stsp view->state.diff.first_displayed_line = 1;
1591 5dc9f4bc 2018-08-04 stsp view->state.diff.last_displayed_line = view->nlines;
1592 5dc9f4bc 2018-08-04 stsp
1593 e5a0f69f 2018-08-18 stsp view->show = show_diff_view;
1594 e5a0f69f 2018-08-18 stsp view->input = input_diff_view;
1595 e5a0f69f 2018-08-18 stsp view->close = close_diff_view;
1596 e5a0f69f 2018-08-18 stsp
1597 5dc9f4bc 2018-08-04 stsp return NULL;
1598 5dc9f4bc 2018-08-04 stsp }
1599 5dc9f4bc 2018-08-04 stsp
1600 e5a0f69f 2018-08-18 stsp static const struct got_error *
1601 5dc9f4bc 2018-08-04 stsp close_diff_view(struct tog_view *view)
1602 5dc9f4bc 2018-08-04 stsp {
1603 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
1604 e5a0f69f 2018-08-18 stsp
1605 e5a0f69f 2018-08-18 stsp if (view->state.diff.f && fclose(view->state.diff.f) == EOF)
1606 e5a0f69f 2018-08-18 stsp err = got_error_from_errno();
1607 e5a0f69f 2018-08-18 stsp return err;
1608 5dc9f4bc 2018-08-04 stsp }
1609 5dc9f4bc 2018-08-04 stsp
1610 5dc9f4bc 2018-08-04 stsp static const struct got_error *
1611 5dc9f4bc 2018-08-04 stsp show_diff_view(struct tog_view *view)
1612 5dc9f4bc 2018-08-04 stsp {
1613 a3404814 2018-09-02 stsp const struct got_error *err;
1614 fb2756b9 2018-08-04 stsp struct tog_diff_view_state *s = &view->state.diff;
1615 a3404814 2018-09-02 stsp char *id_str1 = NULL, *id_str2, *header;
1616 a3404814 2018-09-02 stsp
1617 a3404814 2018-09-02 stsp if (s->id1) {
1618 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str1, s->id1);
1619 a3404814 2018-09-02 stsp if (err)
1620 a3404814 2018-09-02 stsp return err;
1621 a3404814 2018-09-02 stsp }
1622 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str2, s->id2);
1623 a3404814 2018-09-02 stsp if (err)
1624 a3404814 2018-09-02 stsp return err;
1625 26ed57b2 2018-05-19 stsp
1626 a3404814 2018-09-02 stsp if (asprintf(&header, "diff: %s %s",
1627 a3404814 2018-09-02 stsp id_str1 ? id_str1 : "/dev/null", id_str2) == -1) {
1628 a3404814 2018-09-02 stsp err = got_error_from_errno();
1629 a3404814 2018-09-02 stsp free(id_str1);
1630 a3404814 2018-09-02 stsp free(id_str2);
1631 a3404814 2018-09-02 stsp return err;
1632 a3404814 2018-09-02 stsp }
1633 a3404814 2018-09-02 stsp free(id_str1);
1634 a3404814 2018-09-02 stsp free(id_str2);
1635 a3404814 2018-09-02 stsp
1636 e5a0f69f 2018-08-18 stsp return draw_file(view, s->f, &s->first_displayed_line,
1637 a3404814 2018-09-02 stsp &s->last_displayed_line, &s->eof, view->nlines,
1638 a3404814 2018-09-02 stsp header);
1639 e5a0f69f 2018-08-18 stsp }
1640 26ed57b2 2018-05-19 stsp
1641 e5a0f69f 2018-08-18 stsp static const struct got_error *
1642 bcbd79e2 2018-08-19 stsp input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
1643 e5a0f69f 2018-08-18 stsp struct tog_view *view, int ch)
1644 e5a0f69f 2018-08-18 stsp {
1645 bcbd79e2 2018-08-19 stsp const struct got_error *err = NULL;
1646 e5a0f69f 2018-08-18 stsp struct tog_diff_view_state *s = &view->state.diff;
1647 e5a0f69f 2018-08-18 stsp int i;
1648 e5a0f69f 2018-08-18 stsp
1649 e5a0f69f 2018-08-18 stsp switch (ch) {
1650 e5a0f69f 2018-08-18 stsp case 'k':
1651 e5a0f69f 2018-08-18 stsp case KEY_UP:
1652 e5a0f69f 2018-08-18 stsp if (s->first_displayed_line > 1)
1653 e5a0f69f 2018-08-18 stsp s->first_displayed_line--;
1654 26ed57b2 2018-05-19 stsp break;
1655 e5a0f69f 2018-08-18 stsp case KEY_PPAGE:
1656 e5a0f69f 2018-08-18 stsp i = 0;
1657 e5a0f69f 2018-08-18 stsp while (i++ < view->nlines - 1 &&
1658 e5a0f69f 2018-08-18 stsp s->first_displayed_line > 1)
1659 e5a0f69f 2018-08-18 stsp s->first_displayed_line--;
1660 e5a0f69f 2018-08-18 stsp break;
1661 e5a0f69f 2018-08-18 stsp case 'j':
1662 e5a0f69f 2018-08-18 stsp case KEY_DOWN:
1663 e5a0f69f 2018-08-18 stsp if (!s->eof)
1664 e5a0f69f 2018-08-18 stsp s->first_displayed_line++;
1665 e5a0f69f 2018-08-18 stsp break;
1666 e5a0f69f 2018-08-18 stsp case KEY_NPAGE:
1667 e5a0f69f 2018-08-18 stsp case ' ':
1668 e5a0f69f 2018-08-18 stsp i = 0;
1669 e5a0f69f 2018-08-18 stsp while (!s->eof && i++ < view->nlines - 1) {
1670 e5a0f69f 2018-08-18 stsp char *line;
1671 e5a0f69f 2018-08-18 stsp line = parse_next_line(s->f, NULL);
1672 e5a0f69f 2018-08-18 stsp s->first_displayed_line++;
1673 e5a0f69f 2018-08-18 stsp if (line == NULL)
1674 e5a0f69f 2018-08-18 stsp break;
1675 bcbd79e2 2018-08-19 stsp }
1676 bcbd79e2 2018-08-19 stsp break;
1677 bcbd79e2 2018-08-19 stsp case '[':
1678 bcbd79e2 2018-08-19 stsp case ']': {
1679 bcbd79e2 2018-08-19 stsp struct tog_log_view_state *ls;
1680 bcbd79e2 2018-08-19 stsp struct commit_queue_entry *entry;
1681 bcbd79e2 2018-08-19 stsp struct tog_view *diff_view;
1682 bcbd79e2 2018-08-19 stsp
1683 bcbd79e2 2018-08-19 stsp if (view->parent == NULL)
1684 bcbd79e2 2018-08-19 stsp break;
1685 bcbd79e2 2018-08-19 stsp if (view->parent->type != TOG_VIEW_LOG)
1686 bcbd79e2 2018-08-19 stsp break;
1687 bcbd79e2 2018-08-19 stsp ls = &view->parent->state.log;
1688 bcbd79e2 2018-08-19 stsp
1689 bcbd79e2 2018-08-19 stsp if (ch == '[') {
1690 bcbd79e2 2018-08-19 stsp entry = TAILQ_PREV(ls->selected_entry,
1691 bcbd79e2 2018-08-19 stsp commit_queue_head, entry);
1692 bcbd79e2 2018-08-19 stsp } else {
1693 bcbd79e2 2018-08-19 stsp entry = TAILQ_NEXT(ls->selected_entry, entry);
1694 bcbd79e2 2018-08-19 stsp if (entry == NULL) {
1695 bcbd79e2 2018-08-19 stsp err = fetch_next_commit(&entry,
1696 bcbd79e2 2018-08-19 stsp ls->selected_entry,
1697 bcbd79e2 2018-08-19 stsp &ls->commits, ls->graph,
1698 bcbd79e2 2018-08-19 stsp ls->repo, ls->in_repo_path);
1699 bcbd79e2 2018-08-19 stsp if (err)
1700 bcbd79e2 2018-08-19 stsp break;
1701 bcbd79e2 2018-08-19 stsp }
1702 e5a0f69f 2018-08-18 stsp }
1703 bcbd79e2 2018-08-19 stsp if (entry == NULL)
1704 bcbd79e2 2018-08-19 stsp break;
1705 bcbd79e2 2018-08-19 stsp err = show_commit(&diff_view, view->parent,
1706 bcbd79e2 2018-08-19 stsp entry, ls->repo);
1707 bcbd79e2 2018-08-19 stsp if (err)
1708 bcbd79e2 2018-08-19 stsp break;
1709 bcbd79e2 2018-08-19 stsp *new_view = diff_view;
1710 bcbd79e2 2018-08-19 stsp *dead_view = view;
1711 e5a0f69f 2018-08-18 stsp break;
1712 bcbd79e2 2018-08-19 stsp }
1713 e5a0f69f 2018-08-18 stsp default:
1714 e5a0f69f 2018-08-18 stsp break;
1715 26ed57b2 2018-05-19 stsp }
1716 e5a0f69f 2018-08-18 stsp
1717 bcbd79e2 2018-08-19 stsp return err;
1718 26ed57b2 2018-05-19 stsp }
1719 26ed57b2 2018-05-19 stsp
1720 4ed7e80c 2018-05-20 stsp static const struct got_error *
1721 9f7d7167 2018-04-29 stsp cmd_diff(int argc, char *argv[])
1722 9f7d7167 2018-04-29 stsp {
1723 26ed57b2 2018-05-19 stsp const struct got_error *error = NULL;
1724 26ed57b2 2018-05-19 stsp struct got_repository *repo = NULL;
1725 26ed57b2 2018-05-19 stsp struct got_object *obj1 = NULL, *obj2 = NULL;
1726 26ed57b2 2018-05-19 stsp char *repo_path = NULL;
1727 26ed57b2 2018-05-19 stsp char *obj_id_str1 = NULL, *obj_id_str2 = NULL;
1728 26ed57b2 2018-05-19 stsp int ch;
1729 ea5e7bb5 2018-08-01 stsp struct tog_view *view;
1730 26ed57b2 2018-05-19 stsp
1731 26ed57b2 2018-05-19 stsp #ifndef PROFILE
1732 ad242220 2018-09-08 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd", NULL)
1733 ad242220 2018-09-08 stsp == -1)
1734 26ed57b2 2018-05-19 stsp err(1, "pledge");
1735 26ed57b2 2018-05-19 stsp #endif
1736 26ed57b2 2018-05-19 stsp
1737 26ed57b2 2018-05-19 stsp while ((ch = getopt(argc, argv, "")) != -1) {
1738 26ed57b2 2018-05-19 stsp switch (ch) {
1739 26ed57b2 2018-05-19 stsp default:
1740 26ed57b2 2018-05-19 stsp usage();
1741 26ed57b2 2018-05-19 stsp /* NOTREACHED */
1742 26ed57b2 2018-05-19 stsp }
1743 26ed57b2 2018-05-19 stsp }
1744 26ed57b2 2018-05-19 stsp
1745 26ed57b2 2018-05-19 stsp argc -= optind;
1746 26ed57b2 2018-05-19 stsp argv += optind;
1747 26ed57b2 2018-05-19 stsp
1748 26ed57b2 2018-05-19 stsp if (argc == 0) {
1749 26ed57b2 2018-05-19 stsp usage_diff(); /* TODO show local worktree changes */
1750 26ed57b2 2018-05-19 stsp } else if (argc == 2) {
1751 26ed57b2 2018-05-19 stsp repo_path = getcwd(NULL, 0);
1752 26ed57b2 2018-05-19 stsp if (repo_path == NULL)
1753 26ed57b2 2018-05-19 stsp return got_error_from_errno();
1754 26ed57b2 2018-05-19 stsp obj_id_str1 = argv[0];
1755 26ed57b2 2018-05-19 stsp obj_id_str2 = argv[1];
1756 26ed57b2 2018-05-19 stsp } else if (argc == 3) {
1757 26ed57b2 2018-05-19 stsp repo_path = realpath(argv[0], NULL);
1758 26ed57b2 2018-05-19 stsp if (repo_path == NULL)
1759 26ed57b2 2018-05-19 stsp return got_error_from_errno();
1760 26ed57b2 2018-05-19 stsp obj_id_str1 = argv[1];
1761 26ed57b2 2018-05-19 stsp obj_id_str2 = argv[2];
1762 26ed57b2 2018-05-19 stsp } else
1763 26ed57b2 2018-05-19 stsp usage_diff();
1764 26ed57b2 2018-05-19 stsp
1765 26ed57b2 2018-05-19 stsp error = got_repo_open(&repo, repo_path);
1766 26ed57b2 2018-05-19 stsp free(repo_path);
1767 26ed57b2 2018-05-19 stsp if (error)
1768 26ed57b2 2018-05-19 stsp goto done;
1769 26ed57b2 2018-05-19 stsp
1770 26ed57b2 2018-05-19 stsp error = got_object_open_by_id_str(&obj1, repo, obj_id_str1);
1771 26ed57b2 2018-05-19 stsp if (error)
1772 26ed57b2 2018-05-19 stsp goto done;
1773 26ed57b2 2018-05-19 stsp
1774 26ed57b2 2018-05-19 stsp error = got_object_open_by_id_str(&obj2, repo, obj_id_str2);
1775 26ed57b2 2018-05-19 stsp if (error)
1776 26ed57b2 2018-05-19 stsp goto done;
1777 26ed57b2 2018-05-19 stsp
1778 b3665f43 2018-08-04 stsp view = view_open(0, 0, 0, 0, NULL, TOG_VIEW_DIFF);
1779 ea5e7bb5 2018-08-01 stsp if (view == NULL) {
1780 ea5e7bb5 2018-08-01 stsp error = got_error_from_errno();
1781 ea5e7bb5 2018-08-01 stsp goto done;
1782 ea5e7bb5 2018-08-01 stsp }
1783 5dc9f4bc 2018-08-04 stsp error = open_diff_view(view, obj1, obj2, repo);
1784 5dc9f4bc 2018-08-04 stsp if (error)
1785 5dc9f4bc 2018-08-04 stsp goto done;
1786 e5a0f69f 2018-08-18 stsp error = view_loop(view);
1787 26ed57b2 2018-05-19 stsp done:
1788 26ed57b2 2018-05-19 stsp got_repo_close(repo);
1789 26ed57b2 2018-05-19 stsp if (obj1)
1790 26ed57b2 2018-05-19 stsp got_object_close(obj1);
1791 26ed57b2 2018-05-19 stsp if (obj2)
1792 26ed57b2 2018-05-19 stsp got_object_close(obj2);
1793 26ed57b2 2018-05-19 stsp return error;
1794 9f7d7167 2018-04-29 stsp }
1795 9f7d7167 2018-04-29 stsp
1796 4ed7e80c 2018-05-20 stsp __dead static void
1797 9f7d7167 2018-04-29 stsp usage_blame(void)
1798 9f7d7167 2018-04-29 stsp {
1799 80ddbec8 2018-04-29 stsp endwin();
1800 69069811 2018-08-02 stsp fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
1801 9f7d7167 2018-04-29 stsp getprogname());
1802 9f7d7167 2018-04-29 stsp exit(1);
1803 9f7d7167 2018-04-29 stsp }
1804 84451b3e 2018-07-10 stsp
1805 84451b3e 2018-07-10 stsp struct tog_blame_line {
1806 84451b3e 2018-07-10 stsp int annotated;
1807 84451b3e 2018-07-10 stsp struct got_object_id *id;
1808 84451b3e 2018-07-10 stsp };
1809 9f7d7167 2018-04-29 stsp
1810 4ed7e80c 2018-05-20 stsp static const struct got_error *
1811 f7d12f7e 2018-08-01 stsp draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
1812 f7d12f7e 2018-08-01 stsp const char *path, struct tog_blame_line *lines, int nlines,
1813 f7d12f7e 2018-08-01 stsp int blame_complete, int selected_line, int *first_displayed_line,
1814 f7d12f7e 2018-08-01 stsp int *last_displayed_line, int *eof, int max_lines)
1815 84451b3e 2018-07-10 stsp {
1816 84451b3e 2018-07-10 stsp const struct got_error *err;
1817 84451b3e 2018-07-10 stsp int lineno = 0, nprinted = 0;
1818 84451b3e 2018-07-10 stsp char *line;
1819 84451b3e 2018-07-10 stsp size_t len;
1820 84451b3e 2018-07-10 stsp wchar_t *wline;
1821 b700b5d6 2018-07-10 stsp int width, wlimit;
1822 84451b3e 2018-07-10 stsp struct tog_blame_line *blame_line;
1823 ee41ec32 2018-07-10 stsp struct got_object_id *prev_id = NULL;
1824 ab089a2a 2018-07-12 stsp char *id_str;
1825 ab089a2a 2018-07-12 stsp
1826 ab089a2a 2018-07-12 stsp err = got_object_id_str(&id_str, id);
1827 ab089a2a 2018-07-12 stsp if (err)
1828 ab089a2a 2018-07-12 stsp return err;
1829 84451b3e 2018-07-10 stsp
1830 84451b3e 2018-07-10 stsp rewind(f);
1831 f7d12f7e 2018-08-01 stsp werase(view->window);
1832 84451b3e 2018-07-10 stsp
1833 ab089a2a 2018-07-12 stsp if (asprintf(&line, "commit: %s", id_str) == -1) {
1834 ab089a2a 2018-07-12 stsp err = got_error_from_errno();
1835 ab089a2a 2018-07-12 stsp free(id_str);
1836 ab089a2a 2018-07-12 stsp return err;
1837 ab089a2a 2018-07-12 stsp }
1838 ab089a2a 2018-07-12 stsp
1839 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, line, view->ncols);
1840 ab089a2a 2018-07-12 stsp free(line);
1841 2550e4c3 2018-07-13 stsp line = NULL;
1842 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
1843 a3404814 2018-09-02 stsp wstandout(view->window);
1844 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
1845 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
1846 a3404814 2018-09-02 stsp wstandend(view->window);
1847 2550e4c3 2018-07-13 stsp free(wline);
1848 2550e4c3 2018-07-13 stsp wline = NULL;
1849 f7d12f7e 2018-08-01 stsp if (width < view->ncols)
1850 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
1851 ab089a2a 2018-07-12 stsp
1852 084063cd 2018-07-12 stsp if (asprintf(&line, "[%d/%d] %s%s",
1853 084063cd 2018-07-12 stsp *first_displayed_line - 1 + selected_line, nlines,
1854 12a0b23b 2018-07-12 stsp blame_complete ? "" : "annotating ", path) == -1) {
1855 ab089a2a 2018-07-12 stsp free(id_str);
1856 3f60a8ef 2018-07-10 stsp return got_error_from_errno();
1857 ab089a2a 2018-07-12 stsp }
1858 ab089a2a 2018-07-12 stsp free(id_str);
1859 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, line, view->ncols);
1860 3f60a8ef 2018-07-10 stsp free(line);
1861 2550e4c3 2018-07-13 stsp line = NULL;
1862 3f60a8ef 2018-07-10 stsp if (err)
1863 3f60a8ef 2018-07-10 stsp return err;
1864 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
1865 2550e4c3 2018-07-13 stsp free(wline);
1866 2550e4c3 2018-07-13 stsp wline = NULL;
1867 f7d12f7e 2018-08-01 stsp if (width < view->ncols)
1868 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
1869 3f60a8ef 2018-07-10 stsp
1870 84451b3e 2018-07-10 stsp *eof = 0;
1871 ab089a2a 2018-07-12 stsp while (nprinted < max_lines - 2) {
1872 84451b3e 2018-07-10 stsp line = parse_next_line(f, &len);
1873 84451b3e 2018-07-10 stsp if (line == NULL) {
1874 84451b3e 2018-07-10 stsp *eof = 1;
1875 84451b3e 2018-07-10 stsp break;
1876 84451b3e 2018-07-10 stsp }
1877 84451b3e 2018-07-10 stsp if (++lineno < *first_displayed_line) {
1878 84451b3e 2018-07-10 stsp free(line);
1879 84451b3e 2018-07-10 stsp continue;
1880 84451b3e 2018-07-10 stsp }
1881 84451b3e 2018-07-10 stsp
1882 f7d12f7e 2018-08-01 stsp wlimit = view->ncols < 9 ? 0 : view->ncols - 9;
1883 b700b5d6 2018-07-10 stsp err = format_line(&wline, &width, line, wlimit);
1884 84451b3e 2018-07-10 stsp if (err) {
1885 84451b3e 2018-07-10 stsp free(line);
1886 84451b3e 2018-07-10 stsp return err;
1887 84451b3e 2018-07-10 stsp }
1888 84451b3e 2018-07-10 stsp
1889 b700b5d6 2018-07-10 stsp if (nprinted == selected_line - 1)
1890 f7d12f7e 2018-08-01 stsp wstandout(view->window);
1891 b700b5d6 2018-07-10 stsp
1892 84451b3e 2018-07-10 stsp blame_line = &lines[lineno - 1];
1893 ee41ec32 2018-07-10 stsp if (blame_line->annotated && prev_id &&
1894 ee41ec32 2018-07-10 stsp got_object_id_cmp(prev_id, blame_line->id) == 0)
1895 f7d12f7e 2018-08-01 stsp waddstr(view->window, " ");
1896 ee41ec32 2018-07-10 stsp else if (blame_line->annotated) {
1897 84451b3e 2018-07-10 stsp char *id_str;
1898 84451b3e 2018-07-10 stsp err = got_object_id_str(&id_str, blame_line->id);
1899 84451b3e 2018-07-10 stsp if (err) {
1900 84451b3e 2018-07-10 stsp free(line);
1901 2550e4c3 2018-07-13 stsp free(wline);
1902 84451b3e 2018-07-10 stsp return err;
1903 84451b3e 2018-07-10 stsp }
1904 f7d12f7e 2018-08-01 stsp wprintw(view->window, "%.8s ", id_str);
1905 84451b3e 2018-07-10 stsp free(id_str);
1906 ee41ec32 2018-07-10 stsp prev_id = blame_line->id;
1907 ee41ec32 2018-07-10 stsp } else {
1908 f7d12f7e 2018-08-01 stsp waddstr(view->window, "........ ");
1909 ee41ec32 2018-07-10 stsp prev_id = NULL;
1910 ee41ec32 2018-07-10 stsp }
1911 84451b3e 2018-07-10 stsp
1912 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
1913 b700b5d6 2018-07-10 stsp while (width < wlimit) {
1914 f7d12f7e 2018-08-01 stsp waddch(view->window, ' ');
1915 b700b5d6 2018-07-10 stsp width++;
1916 b700b5d6 2018-07-10 stsp }
1917 b700b5d6 2018-07-10 stsp if (nprinted == selected_line - 1)
1918 f7d12f7e 2018-08-01 stsp wstandend(view->window);
1919 84451b3e 2018-07-10 stsp if (++nprinted == 1)
1920 84451b3e 2018-07-10 stsp *first_displayed_line = lineno;
1921 84451b3e 2018-07-10 stsp free(line);
1922 2550e4c3 2018-07-13 stsp free(wline);
1923 2550e4c3 2018-07-13 stsp wline = NULL;
1924 84451b3e 2018-07-10 stsp }
1925 84451b3e 2018-07-10 stsp *last_displayed_line = lineno;
1926 84451b3e 2018-07-10 stsp
1927 1a57306a 2018-09-02 stsp view_vborder(view);
1928 84451b3e 2018-07-10 stsp
1929 84451b3e 2018-07-10 stsp return NULL;
1930 84451b3e 2018-07-10 stsp }
1931 84451b3e 2018-07-10 stsp
1932 84451b3e 2018-07-10 stsp static const struct got_error *
1933 84451b3e 2018-07-10 stsp blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
1934 84451b3e 2018-07-10 stsp {
1935 84451b3e 2018-07-10 stsp const struct got_error *err = NULL;
1936 84451b3e 2018-07-10 stsp struct tog_blame_cb_args *a = arg;
1937 84451b3e 2018-07-10 stsp struct tog_blame_line *line;
1938 84451b3e 2018-07-10 stsp
1939 d68a0a7d 2018-07-10 stsp if (nlines != a->nlines ||
1940 d68a0a7d 2018-07-10 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
1941 84451b3e 2018-07-10 stsp return got_error(GOT_ERR_RANGE);
1942 84451b3e 2018-07-10 stsp
1943 84451b3e 2018-07-10 stsp if (pthread_mutex_lock(a->mutex) != 0)
1944 84451b3e 2018-07-10 stsp return got_error_from_errno();
1945 84451b3e 2018-07-10 stsp
1946 18430de3 2018-07-10 stsp if (*a->quit) { /* user has quit the blame view */
1947 d68a0a7d 2018-07-10 stsp err = got_error(GOT_ERR_ITER_COMPLETED);
1948 d68a0a7d 2018-07-10 stsp goto done;
1949 d68a0a7d 2018-07-10 stsp }
1950 d68a0a7d 2018-07-10 stsp
1951 d68a0a7d 2018-07-10 stsp if (lineno == -1)
1952 d68a0a7d 2018-07-10 stsp goto done; /* no change in this commit */
1953 d68a0a7d 2018-07-10 stsp
1954 84451b3e 2018-07-10 stsp line = &a->lines[lineno - 1];
1955 d68a0a7d 2018-07-10 stsp if (line->annotated)
1956 d68a0a7d 2018-07-10 stsp goto done;
1957 d68a0a7d 2018-07-10 stsp
1958 84451b3e 2018-07-10 stsp line->id = got_object_id_dup(id);
1959 84451b3e 2018-07-10 stsp if (line->id == NULL) {
1960 84451b3e 2018-07-10 stsp err = got_error_from_errno();
1961 84451b3e 2018-07-10 stsp goto done;
1962 84451b3e 2018-07-10 stsp }
1963 84451b3e 2018-07-10 stsp line->annotated = 1;
1964 84451b3e 2018-07-10 stsp
1965 f7d12f7e 2018-08-01 stsp err = draw_blame(a->view, a->commit_id, a->f, a->path,
1966 245d91c1 2018-07-12 stsp a->lines, a->nlines, 0, *a->selected_line, a->first_displayed_line,
1967 e5a0f69f 2018-08-18 stsp a->last_displayed_line, a->eof, a->view->nlines);
1968 84451b3e 2018-07-10 stsp done:
1969 84451b3e 2018-07-10 stsp if (pthread_mutex_unlock(a->mutex) != 0)
1970 84451b3e 2018-07-10 stsp return got_error_from_errno();
1971 84451b3e 2018-07-10 stsp return err;
1972 84451b3e 2018-07-10 stsp }
1973 84451b3e 2018-07-10 stsp
1974 84451b3e 2018-07-10 stsp static void *
1975 84451b3e 2018-07-10 stsp blame_thread(void *arg)
1976 84451b3e 2018-07-10 stsp {
1977 18430de3 2018-07-10 stsp const struct got_error *err;
1978 18430de3 2018-07-10 stsp struct tog_blame_thread_args *ta = arg;
1979 245d91c1 2018-07-12 stsp struct tog_blame_cb_args *a = ta->cb_args;
1980 18430de3 2018-07-10 stsp
1981 ab089a2a 2018-07-12 stsp err = got_blame_incremental(ta->path, a->commit_id, ta->repo,
1982 245d91c1 2018-07-12 stsp blame_cb, ta->cb_args);
1983 18430de3 2018-07-10 stsp
1984 18430de3 2018-07-10 stsp if (pthread_mutex_lock(a->mutex) != 0)
1985 18430de3 2018-07-10 stsp return (void *)got_error_from_errno();
1986 18430de3 2018-07-10 stsp
1987 c9beca56 2018-07-22 stsp got_repo_close(ta->repo);
1988 c9beca56 2018-07-22 stsp ta->repo = NULL;
1989 c9beca56 2018-07-22 stsp *ta->complete = 1;
1990 c9beca56 2018-07-22 stsp if (!err)
1991 f7d12f7e 2018-08-01 stsp err = draw_blame(a->view, a->commit_id, a->f, a->path,
1992 f7d12f7e 2018-08-01 stsp a->lines, a->nlines, 1, *a->selected_line,
1993 e5a0f69f 2018-08-18 stsp a->first_displayed_line, a->last_displayed_line, a->eof,
1994 f7d12f7e 2018-08-01 stsp a->view->nlines);
1995 18430de3 2018-07-10 stsp
1996 18430de3 2018-07-10 stsp if (pthread_mutex_unlock(a->mutex) != 0 && err == NULL)
1997 18430de3 2018-07-10 stsp err = got_error_from_errno();
1998 18430de3 2018-07-10 stsp
1999 18430de3 2018-07-10 stsp return (void *)err;
2000 84451b3e 2018-07-10 stsp }
2001 84451b3e 2018-07-10 stsp
2002 245d91c1 2018-07-12 stsp static struct got_object_id *
2003 245d91c1 2018-07-12 stsp get_selected_commit_id(struct tog_blame_line *lines,
2004 245d91c1 2018-07-12 stsp int first_displayed_line, int selected_line)
2005 245d91c1 2018-07-12 stsp {
2006 245d91c1 2018-07-12 stsp struct tog_blame_line *line;
2007 b880a918 2018-07-10 stsp
2008 245d91c1 2018-07-12 stsp line = &lines[first_displayed_line - 1 + selected_line - 1];
2009 245d91c1 2018-07-12 stsp if (!line->annotated)
2010 245d91c1 2018-07-12 stsp return NULL;
2011 245d91c1 2018-07-12 stsp
2012 245d91c1 2018-07-12 stsp return line->id;
2013 245d91c1 2018-07-12 stsp }
2014 245d91c1 2018-07-12 stsp
2015 84451b3e 2018-07-10 stsp static const struct got_error *
2016 245d91c1 2018-07-12 stsp open_selected_commit(struct got_object **pobj, struct got_object **obj,
2017 b880a918 2018-07-10 stsp struct tog_blame_line *lines, int first_displayed_line,
2018 b880a918 2018-07-10 stsp int selected_line, struct got_repository *repo)
2019 b880a918 2018-07-10 stsp {
2020 b880a918 2018-07-10 stsp const struct got_error *err = NULL;
2021 b880a918 2018-07-10 stsp struct got_commit_object *commit = NULL;
2022 245d91c1 2018-07-12 stsp struct got_object_id *selected_id;
2023 b880a918 2018-07-10 stsp struct got_object_qid *pid;
2024 b880a918 2018-07-10 stsp
2025 b880a918 2018-07-10 stsp *pobj = NULL;
2026 b880a918 2018-07-10 stsp *obj = NULL;
2027 b880a918 2018-07-10 stsp
2028 245d91c1 2018-07-12 stsp selected_id = get_selected_commit_id(lines,
2029 245d91c1 2018-07-12 stsp first_displayed_line, selected_line);
2030 245d91c1 2018-07-12 stsp if (selected_id == NULL)
2031 b880a918 2018-07-10 stsp return NULL;
2032 b880a918 2018-07-10 stsp
2033 245d91c1 2018-07-12 stsp err = got_object_open(obj, repo, selected_id);
2034 b880a918 2018-07-10 stsp if (err)
2035 b880a918 2018-07-10 stsp goto done;
2036 b880a918 2018-07-10 stsp
2037 b880a918 2018-07-10 stsp err = got_object_commit_open(&commit, repo, *obj);
2038 b880a918 2018-07-10 stsp if (err)
2039 b880a918 2018-07-10 stsp goto done;
2040 b880a918 2018-07-10 stsp
2041 b880a918 2018-07-10 stsp pid = SIMPLEQ_FIRST(&commit->parent_ids);
2042 b880a918 2018-07-10 stsp if (pid) {
2043 b880a918 2018-07-10 stsp err = got_object_open(pobj, repo, pid->id);
2044 b880a918 2018-07-10 stsp if (err)
2045 b880a918 2018-07-10 stsp goto done;
2046 b880a918 2018-07-10 stsp }
2047 b880a918 2018-07-10 stsp done:
2048 b880a918 2018-07-10 stsp if (commit)
2049 b880a918 2018-07-10 stsp got_object_commit_close(commit);
2050 b880a918 2018-07-10 stsp return err;
2051 b880a918 2018-07-10 stsp }
2052 245d91c1 2018-07-12 stsp
2053 b880a918 2018-07-10 stsp static const struct got_error *
2054 245d91c1 2018-07-12 stsp stop_blame(struct tog_blame *blame)
2055 a70480e0 2018-06-23 stsp {
2056 a70480e0 2018-06-23 stsp const struct got_error *err = NULL;
2057 245d91c1 2018-07-12 stsp int i;
2058 245d91c1 2018-07-12 stsp
2059 245d91c1 2018-07-12 stsp if (blame->thread) {
2060 245d91c1 2018-07-12 stsp if (pthread_join(blame->thread, (void **)&err) != 0)
2061 245d91c1 2018-07-12 stsp err = got_error_from_errno();
2062 245d91c1 2018-07-12 stsp if (err && err->code == GOT_ERR_ITER_COMPLETED)
2063 245d91c1 2018-07-12 stsp err = NULL;
2064 245d91c1 2018-07-12 stsp blame->thread = NULL;
2065 245d91c1 2018-07-12 stsp }
2066 245d91c1 2018-07-12 stsp if (blame->thread_args.repo) {
2067 245d91c1 2018-07-12 stsp got_repo_close(blame->thread_args.repo);
2068 245d91c1 2018-07-12 stsp blame->thread_args.repo = NULL;
2069 245d91c1 2018-07-12 stsp }
2070 245d91c1 2018-07-12 stsp if (blame->f) {
2071 245d91c1 2018-07-12 stsp fclose(blame->f);
2072 245d91c1 2018-07-12 stsp blame->f = NULL;
2073 245d91c1 2018-07-12 stsp }
2074 245d91c1 2018-07-12 stsp for (i = 0; i < blame->nlines; i++)
2075 245d91c1 2018-07-12 stsp free(blame->lines[i].id);
2076 245d91c1 2018-07-12 stsp free(blame->lines);
2077 245d91c1 2018-07-12 stsp blame->lines = NULL;
2078 75c32340 2018-07-23 stsp free(blame->cb_args.commit_id);
2079 75c32340 2018-07-23 stsp blame->cb_args.commit_id = NULL;
2080 245d91c1 2018-07-12 stsp
2081 245d91c1 2018-07-12 stsp return err;
2082 245d91c1 2018-07-12 stsp }
2083 245d91c1 2018-07-12 stsp
2084 245d91c1 2018-07-12 stsp static const struct got_error *
2085 7cc84d77 2018-08-01 stsp run_blame(struct tog_blame *blame, pthread_mutex_t *mutex,
2086 7cc84d77 2018-08-01 stsp struct tog_view *view, int *blame_complete,
2087 245d91c1 2018-07-12 stsp int *first_displayed_line, int *last_displayed_line,
2088 e5a0f69f 2018-08-18 stsp int *selected_line, int *done, int *eof, const char *path,
2089 245d91c1 2018-07-12 stsp struct got_object_id *commit_id,
2090 245d91c1 2018-07-12 stsp struct got_repository *repo)
2091 245d91c1 2018-07-12 stsp {
2092 245d91c1 2018-07-12 stsp const struct got_error *err = NULL;
2093 84451b3e 2018-07-10 stsp struct got_blob_object *blob = NULL;
2094 245d91c1 2018-07-12 stsp struct got_repository *thread_repo = NULL;
2095 27d434c2 2018-09-15 stsp struct got_object_id *obj_id = NULL;
2096 27d434c2 2018-09-15 stsp struct got_object *obj = NULL;
2097 a70480e0 2018-06-23 stsp
2098 27d434c2 2018-09-15 stsp err = got_object_id_by_path(&obj_id, repo, commit_id, path);
2099 27d434c2 2018-09-15 stsp if (err)
2100 27d434c2 2018-09-15 stsp goto done;
2101 27d434c2 2018-09-15 stsp
2102 27d434c2 2018-09-15 stsp err = got_object_open(&obj, repo, obj_id);
2103 84451b3e 2018-07-10 stsp if (err)
2104 84451b3e 2018-07-10 stsp goto done;
2105 27d434c2 2018-09-15 stsp
2106 84451b3e 2018-07-10 stsp if (got_object_get_type(obj) != GOT_OBJ_TYPE_BLOB) {
2107 84451b3e 2018-07-10 stsp err = got_error(GOT_ERR_OBJ_TYPE);
2108 84451b3e 2018-07-10 stsp goto done;
2109 84451b3e 2018-07-10 stsp }
2110 a70480e0 2018-06-23 stsp
2111 84451b3e 2018-07-10 stsp err = got_object_blob_open(&blob, repo, obj, 8192);
2112 a70480e0 2018-06-23 stsp if (err)
2113 a70480e0 2018-06-23 stsp goto done;
2114 245d91c1 2018-07-12 stsp blame->f = got_opentemp();
2115 245d91c1 2018-07-12 stsp if (blame->f == NULL) {
2116 84451b3e 2018-07-10 stsp err = got_error_from_errno();
2117 84451b3e 2018-07-10 stsp goto done;
2118 84451b3e 2018-07-10 stsp }
2119 245d91c1 2018-07-12 stsp err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
2120 245d91c1 2018-07-12 stsp blame->f, blob);
2121 84451b3e 2018-07-10 stsp if (err)
2122 84451b3e 2018-07-10 stsp goto done;
2123 a70480e0 2018-06-23 stsp
2124 245d91c1 2018-07-12 stsp blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
2125 245d91c1 2018-07-12 stsp if (blame->lines == NULL) {
2126 84451b3e 2018-07-10 stsp err = got_error_from_errno();
2127 84451b3e 2018-07-10 stsp goto done;
2128 84451b3e 2018-07-10 stsp }
2129 a70480e0 2018-06-23 stsp
2130 245d91c1 2018-07-12 stsp err = got_repo_open(&thread_repo, got_repo_get_path(repo));
2131 bd24772e 2018-07-11 stsp if (err)
2132 bd24772e 2018-07-11 stsp goto done;
2133 bd24772e 2018-07-11 stsp
2134 7cc84d77 2018-08-01 stsp blame->cb_args.view = view;
2135 245d91c1 2018-07-12 stsp blame->cb_args.lines = blame->lines;
2136 245d91c1 2018-07-12 stsp blame->cb_args.nlines = blame->nlines;
2137 245d91c1 2018-07-12 stsp blame->cb_args.mutex = mutex;
2138 245d91c1 2018-07-12 stsp blame->cb_args.commit_id = got_object_id_dup(commit_id);
2139 245d91c1 2018-07-12 stsp if (blame->cb_args.commit_id == NULL) {
2140 245d91c1 2018-07-12 stsp err = got_error_from_errno();
2141 245d91c1 2018-07-12 stsp goto done;
2142 245d91c1 2018-07-12 stsp }
2143 245d91c1 2018-07-12 stsp blame->cb_args.f = blame->f;
2144 245d91c1 2018-07-12 stsp blame->cb_args.path = path;
2145 245d91c1 2018-07-12 stsp blame->cb_args.first_displayed_line = first_displayed_line;
2146 245d91c1 2018-07-12 stsp blame->cb_args.selected_line = selected_line;
2147 245d91c1 2018-07-12 stsp blame->cb_args.last_displayed_line = last_displayed_line;
2148 245d91c1 2018-07-12 stsp blame->cb_args.quit = done;
2149 e5a0f69f 2018-08-18 stsp blame->cb_args.eof = eof;
2150 245d91c1 2018-07-12 stsp
2151 245d91c1 2018-07-12 stsp blame->thread_args.path = path;
2152 245d91c1 2018-07-12 stsp blame->thread_args.repo = thread_repo;
2153 245d91c1 2018-07-12 stsp blame->thread_args.cb_args = &blame->cb_args;
2154 245d91c1 2018-07-12 stsp blame->thread_args.complete = blame_complete;
2155 245d91c1 2018-07-12 stsp *blame_complete = 0;
2156 245d91c1 2018-07-12 stsp
2157 245d91c1 2018-07-12 stsp if (pthread_create(&blame->thread, NULL, blame_thread,
2158 245d91c1 2018-07-12 stsp &blame->thread_args) != 0) {
2159 245d91c1 2018-07-12 stsp err = got_error_from_errno();
2160 245d91c1 2018-07-12 stsp goto done;
2161 245d91c1 2018-07-12 stsp }
2162 245d91c1 2018-07-12 stsp
2163 245d91c1 2018-07-12 stsp done:
2164 245d91c1 2018-07-12 stsp if (blob)
2165 245d91c1 2018-07-12 stsp got_object_blob_close(blob);
2166 27d434c2 2018-09-15 stsp free(obj_id);
2167 245d91c1 2018-07-12 stsp if (obj)
2168 245d91c1 2018-07-12 stsp got_object_close(obj);
2169 245d91c1 2018-07-12 stsp if (err)
2170 245d91c1 2018-07-12 stsp stop_blame(blame);
2171 245d91c1 2018-07-12 stsp return err;
2172 245d91c1 2018-07-12 stsp }
2173 245d91c1 2018-07-12 stsp
2174 245d91c1 2018-07-12 stsp static const struct got_error *
2175 e5a0f69f 2018-08-18 stsp open_blame_view(struct tog_view *view, char *path,
2176 e1cd8fed 2018-08-01 stsp struct got_object_id *commit_id, struct got_repository *repo)
2177 245d91c1 2018-07-12 stsp {
2178 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL;
2179 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
2180 dbc6a6b6 2018-07-12 stsp
2181 fb2756b9 2018-08-04 stsp SIMPLEQ_INIT(&s->blamed_commits);
2182 245d91c1 2018-07-12 stsp
2183 fb2756b9 2018-08-04 stsp if (pthread_mutex_init(&s->mutex, NULL) != 0)
2184 7cbe629d 2018-08-04 stsp return got_error_from_errno();
2185 245d91c1 2018-07-12 stsp
2186 fb2756b9 2018-08-04 stsp err = got_object_qid_alloc(&s->blamed_commit, commit_id);
2187 dbc6a6b6 2018-07-12 stsp if (err)
2188 7cbe629d 2018-08-04 stsp return err;
2189 245d91c1 2018-07-12 stsp
2190 fb2756b9 2018-08-04 stsp SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
2191 fb2756b9 2018-08-04 stsp s->first_displayed_line = 1;
2192 fb2756b9 2018-08-04 stsp s->last_displayed_line = view->nlines;
2193 fb2756b9 2018-08-04 stsp s->selected_line = 1;
2194 fb2756b9 2018-08-04 stsp s->blame_complete = 0;
2195 fb2756b9 2018-08-04 stsp s->path = path;
2196 e5a0f69f 2018-08-18 stsp if (s->path == NULL)
2197 e5a0f69f 2018-08-18 stsp return got_error_from_errno();
2198 fb2756b9 2018-08-04 stsp s->repo = repo;
2199 fb2756b9 2018-08-04 stsp s->commit_id = commit_id;
2200 e9424729 2018-08-04 stsp memset(&s->blame, 0, sizeof(s->blame));
2201 7cbe629d 2018-08-04 stsp
2202 e5a0f69f 2018-08-18 stsp view->show = show_blame_view;
2203 e5a0f69f 2018-08-18 stsp view->input = input_blame_view;
2204 e5a0f69f 2018-08-18 stsp view->close = close_blame_view;
2205 e5a0f69f 2018-08-18 stsp
2206 e5a0f69f 2018-08-18 stsp return run_blame(&s->blame, &s->mutex, view, &s->blame_complete,
2207 e5a0f69f 2018-08-18 stsp &s->first_displayed_line, &s->last_displayed_line,
2208 e5a0f69f 2018-08-18 stsp &s->selected_line, &s->done, &s->eof, s->path,
2209 e5a0f69f 2018-08-18 stsp s->blamed_commit->id, s->repo);
2210 7cbe629d 2018-08-04 stsp }
2211 7cbe629d 2018-08-04 stsp
2212 e5a0f69f 2018-08-18 stsp static const struct got_error *
2213 7cbe629d 2018-08-04 stsp close_blame_view(struct tog_view *view)
2214 7cbe629d 2018-08-04 stsp {
2215 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
2216 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
2217 7cbe629d 2018-08-04 stsp
2218 e5a0f69f 2018-08-18 stsp if (s->blame.thread)
2219 e5a0f69f 2018-08-18 stsp err = stop_blame(&s->blame);
2220 e5a0f69f 2018-08-18 stsp
2221 fb2756b9 2018-08-04 stsp while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
2222 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
2223 fb2756b9 2018-08-04 stsp blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
2224 fb2756b9 2018-08-04 stsp SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
2225 7cbe629d 2018-08-04 stsp got_object_qid_free(blamed_commit);
2226 7cbe629d 2018-08-04 stsp }
2227 e5a0f69f 2018-08-18 stsp
2228 e5a0f69f 2018-08-18 stsp free(s->path);
2229 e5a0f69f 2018-08-18 stsp
2230 e5a0f69f 2018-08-18 stsp return err;
2231 7cbe629d 2018-08-04 stsp }
2232 7cbe629d 2018-08-04 stsp
2233 7cbe629d 2018-08-04 stsp static const struct got_error *
2234 7cbe629d 2018-08-04 stsp show_blame_view(struct tog_view *view)
2235 7cbe629d 2018-08-04 stsp {
2236 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
2237 e5a0f69f 2018-08-18 stsp struct tog_blame_view_state *s = &view->state.blame;
2238 e5a0f69f 2018-08-18 stsp
2239 e5a0f69f 2018-08-18 stsp if (pthread_mutex_lock(&s->mutex) != 0)
2240 e5a0f69f 2018-08-18 stsp return got_error_from_errno();
2241 e5a0f69f 2018-08-18 stsp
2242 e5a0f69f 2018-08-18 stsp err = draw_blame(view, s->blamed_commit->id, s->blame.f,
2243 e5a0f69f 2018-08-18 stsp s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
2244 e5a0f69f 2018-08-18 stsp s->selected_line, &s->first_displayed_line,
2245 e5a0f69f 2018-08-18 stsp &s->last_displayed_line, &s->eof, view->nlines);
2246 e5a0f69f 2018-08-18 stsp
2247 e5a0f69f 2018-08-18 stsp if (pthread_mutex_unlock(&s->mutex) != 0 && err == NULL)
2248 e5a0f69f 2018-08-18 stsp err = got_error_from_errno();
2249 e5a0f69f 2018-08-18 stsp
2250 e5a0f69f 2018-08-18 stsp return err;
2251 e5a0f69f 2018-08-18 stsp }
2252 e5a0f69f 2018-08-18 stsp
2253 e5a0f69f 2018-08-18 stsp static const struct got_error *
2254 e5a0f69f 2018-08-18 stsp input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
2255 e5a0f69f 2018-08-18 stsp struct tog_view *view, int ch)
2256 e5a0f69f 2018-08-18 stsp {
2257 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL, *thread_err = NULL;
2258 7cbe629d 2018-08-04 stsp struct got_object *obj = NULL, *pobj = NULL;
2259 7cbe629d 2018-08-04 stsp struct tog_view *diff_view;
2260 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
2261 7cbe629d 2018-08-04 stsp
2262 e5a0f69f 2018-08-18 stsp if (pthread_mutex_lock(&s->mutex) != 0) {
2263 e5a0f69f 2018-08-18 stsp err = got_error_from_errno();
2264 e5a0f69f 2018-08-18 stsp goto done;
2265 e5a0f69f 2018-08-18 stsp }
2266 a70480e0 2018-06-23 stsp
2267 e5a0f69f 2018-08-18 stsp switch (ch) {
2268 e5a0f69f 2018-08-18 stsp case 'q':
2269 e5a0f69f 2018-08-18 stsp s->done = 1;
2270 e5a0f69f 2018-08-18 stsp if (pthread_mutex_unlock(&s->mutex) != 0) {
2271 e5a0f69f 2018-08-18 stsp err = got_error_from_errno();
2272 e5a0f69f 2018-08-18 stsp goto done;
2273 e5a0f69f 2018-08-18 stsp }
2274 e5a0f69f 2018-08-18 stsp return stop_blame(&s->blame);
2275 e5a0f69f 2018-08-18 stsp case 'k':
2276 e5a0f69f 2018-08-18 stsp case KEY_UP:
2277 e5a0f69f 2018-08-18 stsp if (s->selected_line > 1)
2278 e5a0f69f 2018-08-18 stsp s->selected_line--;
2279 e5a0f69f 2018-08-18 stsp else if (s->selected_line == 1 &&
2280 e5a0f69f 2018-08-18 stsp s->first_displayed_line > 1)
2281 e5a0f69f 2018-08-18 stsp s->first_displayed_line--;
2282 a70480e0 2018-06-23 stsp break;
2283 e5a0f69f 2018-08-18 stsp case KEY_PPAGE:
2284 e5a0f69f 2018-08-18 stsp if (s->first_displayed_line == 1) {
2285 e5a0f69f 2018-08-18 stsp s->selected_line = 1;
2286 a70480e0 2018-06-23 stsp break;
2287 e5a0f69f 2018-08-18 stsp }
2288 e5a0f69f 2018-08-18 stsp if (s->first_displayed_line > view->nlines - 2)
2289 e5a0f69f 2018-08-18 stsp s->first_displayed_line -=
2290 e5a0f69f 2018-08-18 stsp (view->nlines - 2);
2291 e5a0f69f 2018-08-18 stsp else
2292 e5a0f69f 2018-08-18 stsp s->first_displayed_line = 1;
2293 e5a0f69f 2018-08-18 stsp break;
2294 e5a0f69f 2018-08-18 stsp case 'j':
2295 e5a0f69f 2018-08-18 stsp case KEY_DOWN:
2296 e5a0f69f 2018-08-18 stsp if (s->selected_line < view->nlines - 2 &&
2297 e5a0f69f 2018-08-18 stsp s->first_displayed_line +
2298 e5a0f69f 2018-08-18 stsp s->selected_line <= s->blame.nlines)
2299 e5a0f69f 2018-08-18 stsp s->selected_line++;
2300 e5a0f69f 2018-08-18 stsp else if (s->last_displayed_line <
2301 e5a0f69f 2018-08-18 stsp s->blame.nlines)
2302 e5a0f69f 2018-08-18 stsp s->first_displayed_line++;
2303 e5a0f69f 2018-08-18 stsp break;
2304 e5a0f69f 2018-08-18 stsp case 'b':
2305 e5a0f69f 2018-08-18 stsp case 'p': {
2306 e5a0f69f 2018-08-18 stsp struct got_object_id *id;
2307 e5a0f69f 2018-08-18 stsp id = get_selected_commit_id(s->blame.lines,
2308 e5a0f69f 2018-08-18 stsp s->first_displayed_line, s->selected_line);
2309 e5a0f69f 2018-08-18 stsp if (id == NULL || got_object_id_cmp(id,
2310 e5a0f69f 2018-08-18 stsp s->blamed_commit->id) == 0)
2311 a70480e0 2018-06-23 stsp break;
2312 e5a0f69f 2018-08-18 stsp err = open_selected_commit(&pobj, &obj,
2313 e5a0f69f 2018-08-18 stsp s->blame.lines, s->first_displayed_line,
2314 e5a0f69f 2018-08-18 stsp s->selected_line, s->repo);
2315 e5a0f69f 2018-08-18 stsp if (err)
2316 a70480e0 2018-06-23 stsp break;
2317 e5a0f69f 2018-08-18 stsp if (pobj == NULL && obj == NULL)
2318 b700b5d6 2018-07-10 stsp break;
2319 e5a0f69f 2018-08-18 stsp if (ch == 'p' && pobj == NULL)
2320 dbc6a6b6 2018-07-12 stsp break;
2321 e5a0f69f 2018-08-18 stsp s->done = 1;
2322 e5a0f69f 2018-08-18 stsp if (pthread_mutex_unlock(&s->mutex) != 0) {
2323 e5a0f69f 2018-08-18 stsp err = got_error_from_errno();
2324 e5a0f69f 2018-08-18 stsp goto done;
2325 dbc6a6b6 2018-07-12 stsp }
2326 e5a0f69f 2018-08-18 stsp thread_err = stop_blame(&s->blame);
2327 e5a0f69f 2018-08-18 stsp s->done = 0;
2328 e5a0f69f 2018-08-18 stsp if (pthread_mutex_lock(&s->mutex) != 0) {
2329 e5a0f69f 2018-08-18 stsp err = got_error_from_errno();
2330 e5a0f69f 2018-08-18 stsp goto done;
2331 e5a0f69f 2018-08-18 stsp }
2332 e5a0f69f 2018-08-18 stsp if (thread_err)
2333 245d91c1 2018-07-12 stsp break;
2334 e5a0f69f 2018-08-18 stsp id = got_object_get_id(ch == 'b' ? obj : pobj);
2335 e5a0f69f 2018-08-18 stsp got_object_close(obj);
2336 e5a0f69f 2018-08-18 stsp obj = NULL;
2337 e5a0f69f 2018-08-18 stsp if (pobj) {
2338 e5a0f69f 2018-08-18 stsp got_object_close(pobj);
2339 e5a0f69f 2018-08-18 stsp pobj = NULL;
2340 e5a0f69f 2018-08-18 stsp }
2341 6402fb3c 2018-09-15 stsp err = got_object_qid_alloc(&s->blamed_commit, id);
2342 e5a0f69f 2018-08-18 stsp if (err)
2343 e5a0f69f 2018-08-18 stsp goto done;
2344 e5a0f69f 2018-08-18 stsp SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
2345 e5a0f69f 2018-08-18 stsp s->blamed_commit, entry);
2346 e5a0f69f 2018-08-18 stsp err = run_blame(&s->blame, &s->mutex, view,
2347 e5a0f69f 2018-08-18 stsp &s->blame_complete,
2348 e5a0f69f 2018-08-18 stsp &s->first_displayed_line,
2349 e5a0f69f 2018-08-18 stsp &s->last_displayed_line,
2350 e5a0f69f 2018-08-18 stsp &s->selected_line, &s->done, &s->eof,
2351 e5a0f69f 2018-08-18 stsp s->path, s->blamed_commit->id, s->repo);
2352 e5a0f69f 2018-08-18 stsp if (err)
2353 a70480e0 2018-06-23 stsp break;
2354 e5a0f69f 2018-08-18 stsp break;
2355 84451b3e 2018-07-10 stsp }
2356 e5a0f69f 2018-08-18 stsp case 'B': {
2357 e5a0f69f 2018-08-18 stsp struct got_object_qid *first;
2358 e5a0f69f 2018-08-18 stsp first = SIMPLEQ_FIRST(&s->blamed_commits);
2359 e5a0f69f 2018-08-18 stsp if (!got_object_id_cmp(first->id, s->commit_id))
2360 e5a0f69f 2018-08-18 stsp break;
2361 e5a0f69f 2018-08-18 stsp s->done = 1;
2362 e5a0f69f 2018-08-18 stsp if (pthread_mutex_unlock(&s->mutex) != 0) {
2363 e5a0f69f 2018-08-18 stsp err = got_error_from_errno();
2364 e5a0f69f 2018-08-18 stsp goto done;
2365 e5a0f69f 2018-08-18 stsp }
2366 e5a0f69f 2018-08-18 stsp thread_err = stop_blame(&s->blame);
2367 e5a0f69f 2018-08-18 stsp s->done = 0;
2368 e5a0f69f 2018-08-18 stsp if (pthread_mutex_lock(&s->mutex) != 0) {
2369 e5a0f69f 2018-08-18 stsp err = got_error_from_errno();
2370 e5a0f69f 2018-08-18 stsp goto done;
2371 e5a0f69f 2018-08-18 stsp }
2372 e5a0f69f 2018-08-18 stsp if (thread_err)
2373 e5a0f69f 2018-08-18 stsp break;
2374 e5a0f69f 2018-08-18 stsp SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
2375 e5a0f69f 2018-08-18 stsp got_object_qid_free(s->blamed_commit);
2376 e5a0f69f 2018-08-18 stsp s->blamed_commit =
2377 e5a0f69f 2018-08-18 stsp SIMPLEQ_FIRST(&s->blamed_commits);
2378 e5a0f69f 2018-08-18 stsp err = run_blame(&s->blame, &s->mutex, view,
2379 e5a0f69f 2018-08-18 stsp &s->blame_complete,
2380 e5a0f69f 2018-08-18 stsp &s->first_displayed_line,
2381 e5a0f69f 2018-08-18 stsp &s->last_displayed_line,
2382 e5a0f69f 2018-08-18 stsp &s->selected_line, &s->done, &s->eof, s->path,
2383 e5a0f69f 2018-08-18 stsp s->blamed_commit->id, s->repo);
2384 e5a0f69f 2018-08-18 stsp if (err)
2385 e5a0f69f 2018-08-18 stsp break;
2386 245d91c1 2018-07-12 stsp break;
2387 e5a0f69f 2018-08-18 stsp }
2388 e5a0f69f 2018-08-18 stsp case KEY_ENTER:
2389 e5a0f69f 2018-08-18 stsp case '\r':
2390 e5a0f69f 2018-08-18 stsp err = open_selected_commit(&pobj, &obj,
2391 e5a0f69f 2018-08-18 stsp s->blame.lines, s->first_displayed_line,
2392 e5a0f69f 2018-08-18 stsp s->selected_line, s->repo);
2393 e5a0f69f 2018-08-18 stsp if (err)
2394 e5a0f69f 2018-08-18 stsp break;
2395 e5a0f69f 2018-08-18 stsp if (pobj == NULL && obj == NULL)
2396 e5a0f69f 2018-08-18 stsp break;
2397 e5a0f69f 2018-08-18 stsp diff_view = view_open(0, 0, 0, 0, view,
2398 e5a0f69f 2018-08-18 stsp TOG_VIEW_DIFF);
2399 e5a0f69f 2018-08-18 stsp if (diff_view == NULL) {
2400 e5a0f69f 2018-08-18 stsp err = got_error_from_errno();
2401 e5a0f69f 2018-08-18 stsp break;
2402 e5a0f69f 2018-08-18 stsp }
2403 e5a0f69f 2018-08-18 stsp err = open_diff_view(diff_view, pobj, obj,
2404 e5a0f69f 2018-08-18 stsp s->repo);
2405 e5a0f69f 2018-08-18 stsp if (err) {
2406 e5a0f69f 2018-08-18 stsp view_close(diff_view);
2407 e5a0f69f 2018-08-18 stsp break;
2408 e5a0f69f 2018-08-18 stsp }
2409 e5a0f69f 2018-08-18 stsp *new_view = diff_view;
2410 e5a0f69f 2018-08-18 stsp if (pobj) {
2411 e5a0f69f 2018-08-18 stsp got_object_close(pobj);
2412 e5a0f69f 2018-08-18 stsp pobj = NULL;
2413 e5a0f69f 2018-08-18 stsp }
2414 e5a0f69f 2018-08-18 stsp got_object_close(obj);
2415 e5a0f69f 2018-08-18 stsp obj = NULL;
2416 e5a0f69f 2018-08-18 stsp if (err)
2417 e5a0f69f 2018-08-18 stsp break;
2418 e5a0f69f 2018-08-18 stsp break;
2419 e5a0f69f 2018-08-18 stsp case KEY_NPAGE:
2420 e5a0f69f 2018-08-18 stsp case ' ':
2421 e5a0f69f 2018-08-18 stsp if (s->last_displayed_line >= s->blame.nlines &&
2422 e5a0f69f 2018-08-18 stsp s->selected_line < view->nlines - 2) {
2423 e5a0f69f 2018-08-18 stsp s->selected_line = MIN(s->blame.nlines,
2424 e5a0f69f 2018-08-18 stsp view->nlines - 2);
2425 e5a0f69f 2018-08-18 stsp break;
2426 e5a0f69f 2018-08-18 stsp }
2427 e5a0f69f 2018-08-18 stsp if (s->last_displayed_line + view->nlines - 2
2428 e5a0f69f 2018-08-18 stsp <= s->blame.nlines)
2429 e5a0f69f 2018-08-18 stsp s->first_displayed_line +=
2430 e5a0f69f 2018-08-18 stsp view->nlines - 2;
2431 e5a0f69f 2018-08-18 stsp else
2432 e5a0f69f 2018-08-18 stsp s->first_displayed_line =
2433 e5a0f69f 2018-08-18 stsp s->blame.nlines -
2434 e5a0f69f 2018-08-18 stsp (view->nlines - 3);
2435 e5a0f69f 2018-08-18 stsp break;
2436 e5a0f69f 2018-08-18 stsp case KEY_RESIZE:
2437 e5a0f69f 2018-08-18 stsp if (s->selected_line > view->nlines - 2) {
2438 e5a0f69f 2018-08-18 stsp s->selected_line = MIN(s->blame.nlines,
2439 e5a0f69f 2018-08-18 stsp view->nlines - 2);
2440 e5a0f69f 2018-08-18 stsp }
2441 e5a0f69f 2018-08-18 stsp break;
2442 e5a0f69f 2018-08-18 stsp default:
2443 e5a0f69f 2018-08-18 stsp break;
2444 a70480e0 2018-06-23 stsp }
2445 e5a0f69f 2018-08-18 stsp
2446 e5a0f69f 2018-08-18 stsp if (pthread_mutex_unlock(&s->mutex) != 0)
2447 e5a0f69f 2018-08-18 stsp err = got_error_from_errno();
2448 a70480e0 2018-06-23 stsp done:
2449 b880a918 2018-07-10 stsp if (pobj)
2450 b880a918 2018-07-10 stsp got_object_close(pobj);
2451 245d91c1 2018-07-12 stsp return thread_err ? thread_err : err;
2452 a70480e0 2018-06-23 stsp }
2453 a70480e0 2018-06-23 stsp
2454 a70480e0 2018-06-23 stsp static const struct got_error *
2455 9f7d7167 2018-04-29 stsp cmd_blame(int argc, char *argv[])
2456 9f7d7167 2018-04-29 stsp {
2457 a70480e0 2018-06-23 stsp const struct got_error *error;
2458 a70480e0 2018-06-23 stsp struct got_repository *repo = NULL;
2459 69069811 2018-08-02 stsp char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2460 a70480e0 2018-06-23 stsp struct got_object_id *commit_id = NULL;
2461 a70480e0 2018-06-23 stsp char *commit_id_str = NULL;
2462 a70480e0 2018-06-23 stsp int ch;
2463 e1cd8fed 2018-08-01 stsp struct tog_view *view;
2464 a70480e0 2018-06-23 stsp
2465 a70480e0 2018-06-23 stsp #ifndef PROFILE
2466 ad242220 2018-09-08 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd", NULL)
2467 ad242220 2018-09-08 stsp == -1)
2468 a70480e0 2018-06-23 stsp err(1, "pledge");
2469 a70480e0 2018-06-23 stsp #endif
2470 a70480e0 2018-06-23 stsp
2471 69069811 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2472 a70480e0 2018-06-23 stsp switch (ch) {
2473 a70480e0 2018-06-23 stsp case 'c':
2474 a70480e0 2018-06-23 stsp commit_id_str = optarg;
2475 a70480e0 2018-06-23 stsp break;
2476 69069811 2018-08-02 stsp case 'r':
2477 69069811 2018-08-02 stsp repo_path = realpath(optarg, NULL);
2478 69069811 2018-08-02 stsp if (repo_path == NULL)
2479 69069811 2018-08-02 stsp err(1, "-r option");
2480 69069811 2018-08-02 stsp break;
2481 a70480e0 2018-06-23 stsp default:
2482 a70480e0 2018-06-23 stsp usage();
2483 a70480e0 2018-06-23 stsp /* NOTREACHED */
2484 a70480e0 2018-06-23 stsp }
2485 a70480e0 2018-06-23 stsp }
2486 a70480e0 2018-06-23 stsp
2487 a70480e0 2018-06-23 stsp argc -= optind;
2488 a70480e0 2018-06-23 stsp argv += optind;
2489 a70480e0 2018-06-23 stsp
2490 69069811 2018-08-02 stsp if (argc == 1)
2491 69069811 2018-08-02 stsp path = argv[0];
2492 69069811 2018-08-02 stsp else
2493 a70480e0 2018-06-23 stsp usage_blame();
2494 69069811 2018-08-02 stsp
2495 69069811 2018-08-02 stsp cwd = getcwd(NULL, 0);
2496 69069811 2018-08-02 stsp if (cwd == NULL) {
2497 69069811 2018-08-02 stsp error = got_error_from_errno();
2498 69069811 2018-08-02 stsp goto done;
2499 69069811 2018-08-02 stsp }
2500 69069811 2018-08-02 stsp if (repo_path == NULL) {
2501 69069811 2018-08-02 stsp repo_path = strdup(cwd);
2502 69069811 2018-08-02 stsp if (repo_path == NULL) {
2503 69069811 2018-08-02 stsp error = got_error_from_errno();
2504 69069811 2018-08-02 stsp goto done;
2505 69069811 2018-08-02 stsp }
2506 69069811 2018-08-02 stsp }
2507 69069811 2018-08-02 stsp
2508 69069811 2018-08-02 stsp
2509 69069811 2018-08-02 stsp error = got_repo_open(&repo, repo_path);
2510 a70480e0 2018-06-23 stsp if (error != NULL)
2511 66b4983c 2018-06-23 stsp return error;
2512 69069811 2018-08-02 stsp
2513 69069811 2018-08-02 stsp error = got_repo_map_path(&in_repo_path, repo, path);
2514 69069811 2018-08-02 stsp if (error != NULL)
2515 69069811 2018-08-02 stsp goto done;
2516 a70480e0 2018-06-23 stsp
2517 a70480e0 2018-06-23 stsp if (commit_id_str == NULL) {
2518 a70480e0 2018-06-23 stsp struct got_reference *head_ref;
2519 a70480e0 2018-06-23 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
2520 a70480e0 2018-06-23 stsp if (error != NULL)
2521 66b4983c 2018-06-23 stsp goto done;
2522 a70480e0 2018-06-23 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
2523 a70480e0 2018-06-23 stsp got_ref_close(head_ref);
2524 a70480e0 2018-06-23 stsp } else {
2525 a70480e0 2018-06-23 stsp struct got_object *obj;
2526 a70480e0 2018-06-23 stsp error = got_object_open_by_id_str(&obj, repo, commit_id_str);
2527 a70480e0 2018-06-23 stsp if (error != NULL)
2528 66b4983c 2018-06-23 stsp goto done;
2529 6402fb3c 2018-09-15 stsp commit_id = got_object_id_dup(got_object_get_id(obj));
2530 a19e88aa 2018-06-23 stsp if (commit_id == NULL)
2531 66b4983c 2018-06-23 stsp error = got_error_from_errno();
2532 a19e88aa 2018-06-23 stsp got_object_close(obj);
2533 a70480e0 2018-06-23 stsp }
2534 a19e88aa 2018-06-23 stsp if (error != NULL)
2535 a19e88aa 2018-06-23 stsp goto done;
2536 a70480e0 2018-06-23 stsp
2537 b3665f43 2018-08-04 stsp view = view_open(0, 0, 0, 0, NULL, TOG_VIEW_BLAME);
2538 e1cd8fed 2018-08-01 stsp if (view == NULL) {
2539 e1cd8fed 2018-08-01 stsp error = got_error_from_errno();
2540 e1cd8fed 2018-08-01 stsp goto done;
2541 e1cd8fed 2018-08-01 stsp }
2542 7cbe629d 2018-08-04 stsp error = open_blame_view(view, in_repo_path, commit_id, repo);
2543 7cbe629d 2018-08-04 stsp if (error)
2544 7cbe629d 2018-08-04 stsp goto done;
2545 e5a0f69f 2018-08-18 stsp error = view_loop(view);
2546 a70480e0 2018-06-23 stsp done:
2547 69069811 2018-08-02 stsp free(repo_path);
2548 69069811 2018-08-02 stsp free(cwd);
2549 a70480e0 2018-06-23 stsp free(commit_id);
2550 a70480e0 2018-06-23 stsp if (repo)
2551 a70480e0 2018-06-23 stsp got_repo_close(repo);
2552 a70480e0 2018-06-23 stsp return error;
2553 ffd1d5e5 2018-06-23 stsp }
2554 ffd1d5e5 2018-06-23 stsp
2555 ffd1d5e5 2018-06-23 stsp static const struct got_error *
2556 f7d12f7e 2018-08-01 stsp draw_tree_entries(struct tog_view *view,
2557 f7d12f7e 2018-08-01 stsp struct got_tree_entry **first_displayed_entry,
2558 ffd1d5e5 2018-06-23 stsp struct got_tree_entry **last_displayed_entry,
2559 ffd1d5e5 2018-06-23 stsp struct got_tree_entry **selected_entry, int *ndisplayed,
2560 f7d12f7e 2018-08-01 stsp const char *label, int show_ids, const char *parent_path,
2561 ce52c690 2018-06-23 stsp const struct got_tree_entries *entries, int selected, int limit, int isroot)
2562 ffd1d5e5 2018-06-23 stsp {
2563 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
2564 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *te;
2565 ffd1d5e5 2018-06-23 stsp wchar_t *wline;
2566 ffd1d5e5 2018-06-23 stsp int width, n;
2567 ffd1d5e5 2018-06-23 stsp
2568 ffd1d5e5 2018-06-23 stsp *ndisplayed = 0;
2569 ffd1d5e5 2018-06-23 stsp
2570 f7d12f7e 2018-08-01 stsp werase(view->window);
2571 ffd1d5e5 2018-06-23 stsp
2572 ffd1d5e5 2018-06-23 stsp if (limit == 0)
2573 ffd1d5e5 2018-06-23 stsp return NULL;
2574 ffd1d5e5 2018-06-23 stsp
2575 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, label, view->ncols);
2576 ffd1d5e5 2018-06-23 stsp if (err)
2577 ffd1d5e5 2018-06-23 stsp return err;
2578 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2579 a3404814 2018-09-02 stsp wstandout(view->window);
2580 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
2581 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2582 a3404814 2018-09-02 stsp wstandend(view->window);
2583 2550e4c3 2018-07-13 stsp free(wline);
2584 2550e4c3 2018-07-13 stsp wline = NULL;
2585 f7d12f7e 2018-08-01 stsp if (width < view->ncols)
2586 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
2587 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
2588 ffd1d5e5 2018-06-23 stsp return NULL;
2589 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, parent_path, view->ncols);
2590 ce52c690 2018-06-23 stsp if (err)
2591 ce52c690 2018-06-23 stsp return err;
2592 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
2593 2550e4c3 2018-07-13 stsp free(wline);
2594 2550e4c3 2018-07-13 stsp wline = NULL;
2595 f7d12f7e 2018-08-01 stsp if (width < view->ncols)
2596 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
2597 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
2598 ffd1d5e5 2018-06-23 stsp return NULL;
2599 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
2600 a1eca9bb 2018-06-23 stsp if (--limit <= 0)
2601 a1eca9bb 2018-06-23 stsp return NULL;
2602 ffd1d5e5 2018-06-23 stsp
2603 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_FIRST(&entries->head);
2604 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry == NULL) {
2605 ffd1d5e5 2018-06-23 stsp if (selected == 0) {
2606 f7d12f7e 2018-08-01 stsp wstandout(view->window);
2607 ffd1d5e5 2018-06-23 stsp *selected_entry = NULL;
2608 ffd1d5e5 2018-06-23 stsp }
2609 f7d12f7e 2018-08-01 stsp waddstr(view->window, " ..\n"); /* parent directory */
2610 ffd1d5e5 2018-06-23 stsp if (selected == 0)
2611 f7d12f7e 2018-08-01 stsp wstandend(view->window);
2612 ffd1d5e5 2018-06-23 stsp (*ndisplayed)++;
2613 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
2614 ffd1d5e5 2018-06-23 stsp return NULL;
2615 ffd1d5e5 2018-06-23 stsp n = 1;
2616 ffd1d5e5 2018-06-23 stsp } else {
2617 ffd1d5e5 2018-06-23 stsp n = 0;
2618 ffd1d5e5 2018-06-23 stsp while (te != *first_displayed_entry)
2619 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_NEXT(te, entry);
2620 ffd1d5e5 2018-06-23 stsp }
2621 ffd1d5e5 2018-06-23 stsp
2622 ffd1d5e5 2018-06-23 stsp while (te) {
2623 1d13200f 2018-07-12 stsp char *line = NULL, *id_str = NULL;
2624 1d13200f 2018-07-12 stsp
2625 1d13200f 2018-07-12 stsp if (show_ids) {
2626 1d13200f 2018-07-12 stsp err = got_object_id_str(&id_str, te->id);
2627 1d13200f 2018-07-12 stsp if (err)
2628 1d13200f 2018-07-12 stsp return got_error_from_errno();
2629 1d13200f 2018-07-12 stsp }
2630 1d13200f 2018-07-12 stsp if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
2631 1d13200f 2018-07-12 stsp te->name, S_ISDIR(te->mode) ? "/" : "") == -1) {
2632 1d13200f 2018-07-12 stsp free(id_str);
2633 ffd1d5e5 2018-06-23 stsp return got_error_from_errno();
2634 1d13200f 2018-07-12 stsp }
2635 1d13200f 2018-07-12 stsp free(id_str);
2636 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, line, view->ncols);
2637 ffd1d5e5 2018-06-23 stsp if (err) {
2638 ffd1d5e5 2018-06-23 stsp free(line);
2639 ffd1d5e5 2018-06-23 stsp break;
2640 ffd1d5e5 2018-06-23 stsp }
2641 ffd1d5e5 2018-06-23 stsp if (n == selected) {
2642 f7d12f7e 2018-08-01 stsp wstandout(view->window);
2643 ffd1d5e5 2018-06-23 stsp *selected_entry = te;
2644 ffd1d5e5 2018-06-23 stsp }
2645 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
2646 f7d12f7e 2018-08-01 stsp if (width < view->ncols)
2647 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
2648 ffd1d5e5 2018-06-23 stsp if (n == selected)
2649 f7d12f7e 2018-08-01 stsp wstandend(view->window);
2650 ffd1d5e5 2018-06-23 stsp free(line);
2651 2550e4c3 2018-07-13 stsp free(wline);
2652 2550e4c3 2018-07-13 stsp wline = NULL;
2653 ffd1d5e5 2018-06-23 stsp n++;
2654 ffd1d5e5 2018-06-23 stsp (*ndisplayed)++;
2655 ffd1d5e5 2018-06-23 stsp *last_displayed_entry = te;
2656 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
2657 ffd1d5e5 2018-06-23 stsp break;
2658 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_NEXT(te, entry);
2659 ffd1d5e5 2018-06-23 stsp }
2660 ffd1d5e5 2018-06-23 stsp
2661 1a57306a 2018-09-02 stsp view_vborder(view);
2662 ffd1d5e5 2018-06-23 stsp return err;
2663 ffd1d5e5 2018-06-23 stsp }
2664 ffd1d5e5 2018-06-23 stsp
2665 ffd1d5e5 2018-06-23 stsp static void
2666 ffd1d5e5 2018-06-23 stsp tree_scroll_up(struct got_tree_entry **first_displayed_entry, int maxscroll,
2667 ffd1d5e5 2018-06-23 stsp const struct got_tree_entries *entries, int isroot)
2668 ffd1d5e5 2018-06-23 stsp {
2669 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *te, *prev;
2670 ffd1d5e5 2018-06-23 stsp int i;
2671 ffd1d5e5 2018-06-23 stsp
2672 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry == NULL)
2673 ffd1d5e5 2018-06-23 stsp return;
2674 ffd1d5e5 2018-06-23 stsp
2675 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_FIRST(&entries->head);
2676 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry == te) {
2677 ffd1d5e5 2018-06-23 stsp if (!isroot)
2678 ffd1d5e5 2018-06-23 stsp *first_displayed_entry = NULL;
2679 ffd1d5e5 2018-06-23 stsp return;
2680 ffd1d5e5 2018-06-23 stsp }
2681 ffd1d5e5 2018-06-23 stsp
2682 ffd1d5e5 2018-06-23 stsp /* XXX this is stupid... switch to TAILQ? */
2683 ffd1d5e5 2018-06-23 stsp for (i = 0; i < maxscroll; i++) {
2684 ffd1d5e5 2018-06-23 stsp while (te != *first_displayed_entry) {
2685 ffd1d5e5 2018-06-23 stsp prev = te;
2686 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_NEXT(te, entry);
2687 ffd1d5e5 2018-06-23 stsp }
2688 ffd1d5e5 2018-06-23 stsp *first_displayed_entry = prev;
2689 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_FIRST(&entries->head);
2690 ffd1d5e5 2018-06-23 stsp }
2691 ffd1d5e5 2018-06-23 stsp if (!isroot && te == SIMPLEQ_FIRST(&entries->head) && i < maxscroll)
2692 ffd1d5e5 2018-06-23 stsp *first_displayed_entry = NULL;
2693 ffd1d5e5 2018-06-23 stsp }
2694 ffd1d5e5 2018-06-23 stsp
2695 ffd1d5e5 2018-06-23 stsp static void
2696 ffd1d5e5 2018-06-23 stsp tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
2697 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *last_displayed_entry,
2698 ffd1d5e5 2018-06-23 stsp const struct got_tree_entries *entries)
2699 ffd1d5e5 2018-06-23 stsp {
2700 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *next;
2701 ffd1d5e5 2018-06-23 stsp int n = 0;
2702 ffd1d5e5 2018-06-23 stsp
2703 ffd1d5e5 2018-06-23 stsp if (SIMPLEQ_NEXT(last_displayed_entry, entry) == NULL)
2704 ffd1d5e5 2018-06-23 stsp return;
2705 ffd1d5e5 2018-06-23 stsp
2706 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry)
2707 ffd1d5e5 2018-06-23 stsp next = SIMPLEQ_NEXT(*first_displayed_entry, entry);
2708 ffd1d5e5 2018-06-23 stsp else
2709 ffd1d5e5 2018-06-23 stsp next = SIMPLEQ_FIRST(&entries->head);
2710 ffd1d5e5 2018-06-23 stsp while (next) {
2711 ffd1d5e5 2018-06-23 stsp *first_displayed_entry = next;
2712 ffd1d5e5 2018-06-23 stsp if (++n >= maxscroll)
2713 ffd1d5e5 2018-06-23 stsp break;
2714 ffd1d5e5 2018-06-23 stsp next = SIMPLEQ_NEXT(next, entry);
2715 ffd1d5e5 2018-06-23 stsp }
2716 ffd1d5e5 2018-06-23 stsp }
2717 ffd1d5e5 2018-06-23 stsp
2718 ffd1d5e5 2018-06-23 stsp static const struct got_error *
2719 ce52c690 2018-06-23 stsp tree_entry_path(char **path, struct tog_parent_trees *parents,
2720 ce52c690 2018-06-23 stsp struct got_tree_entry *te)
2721 ffd1d5e5 2018-06-23 stsp {
2722 cb2ebc8a 2018-06-23 stsp const struct got_error *err = NULL;
2723 ffd1d5e5 2018-06-23 stsp struct tog_parent_tree *pt;
2724 ffd1d5e5 2018-06-23 stsp size_t len = 2; /* for leading slash and NUL */
2725 ffd1d5e5 2018-06-23 stsp
2726 d9765a41 2018-06-23 stsp TAILQ_FOREACH(pt, parents, entry)
2727 ffd1d5e5 2018-06-23 stsp len += strlen(pt->selected_entry->name) + 1 /* slash */;
2728 ce52c690 2018-06-23 stsp if (te)
2729 ce52c690 2018-06-23 stsp len += strlen(te->name);
2730 ce52c690 2018-06-23 stsp
2731 ce52c690 2018-06-23 stsp *path = calloc(1, len);
2732 ffd1d5e5 2018-06-23 stsp if (path == NULL)
2733 ffd1d5e5 2018-06-23 stsp return got_error_from_errno();
2734 ffd1d5e5 2018-06-23 stsp
2735 ce52c690 2018-06-23 stsp (*path)[0] = '/';
2736 d9765a41 2018-06-23 stsp pt = TAILQ_LAST(parents, tog_parent_trees);
2737 d9765a41 2018-06-23 stsp while (pt) {
2738 ce52c690 2018-06-23 stsp if (strlcat(*path, pt->selected_entry->name, len) >= len) {
2739 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
2740 cb2ebc8a 2018-06-23 stsp goto done;
2741 cb2ebc8a 2018-06-23 stsp }
2742 ce52c690 2018-06-23 stsp if (strlcat(*path, "/", len) >= len) {
2743 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
2744 cb2ebc8a 2018-06-23 stsp goto done;
2745 cb2ebc8a 2018-06-23 stsp }
2746 d9765a41 2018-06-23 stsp pt = TAILQ_PREV(pt, tog_parent_trees, entry);
2747 ffd1d5e5 2018-06-23 stsp }
2748 ce52c690 2018-06-23 stsp if (te) {
2749 ce52c690 2018-06-23 stsp if (strlcat(*path, te->name, len) >= len) {
2750 ce52c690 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
2751 ce52c690 2018-06-23 stsp goto done;
2752 ce52c690 2018-06-23 stsp }
2753 cb2ebc8a 2018-06-23 stsp }
2754 ce52c690 2018-06-23 stsp done:
2755 ce52c690 2018-06-23 stsp if (err) {
2756 ce52c690 2018-06-23 stsp free(*path);
2757 ce52c690 2018-06-23 stsp *path = NULL;
2758 ce52c690 2018-06-23 stsp }
2759 ce52c690 2018-06-23 stsp return err;
2760 ce52c690 2018-06-23 stsp }
2761 ce52c690 2018-06-23 stsp
2762 ce52c690 2018-06-23 stsp static const struct got_error *
2763 e5a0f69f 2018-08-18 stsp blame_tree_entry(struct tog_view **new_view, struct tog_view *parent_view,
2764 e5a0f69f 2018-08-18 stsp struct got_tree_entry *te, struct tog_parent_trees *parents,
2765 e5a0f69f 2018-08-18 stsp struct got_object_id *commit_id, struct got_repository *repo)
2766 ce52c690 2018-06-23 stsp {
2767 ce52c690 2018-06-23 stsp const struct got_error *err = NULL;
2768 ce52c690 2018-06-23 stsp char *path;
2769 e5a0f69f 2018-08-18 stsp struct tog_view *blame_view;
2770 69efd4c4 2018-07-18 stsp
2771 ce52c690 2018-06-23 stsp err = tree_entry_path(&path, parents, te);
2772 ce52c690 2018-06-23 stsp if (err)
2773 ce52c690 2018-06-23 stsp return err;
2774 ffd1d5e5 2018-06-23 stsp
2775 e5a0f69f 2018-08-18 stsp blame_view = view_open(0, 0, 0, 0, parent_view, TOG_VIEW_BLAME);
2776 e5a0f69f 2018-08-18 stsp if (blame_view == NULL)
2777 e5a0f69f 2018-08-18 stsp return got_error_from_errno();
2778 cdf1ee82 2018-08-01 stsp
2779 e5a0f69f 2018-08-18 stsp err = open_blame_view(blame_view, path, commit_id, repo);
2780 e5a0f69f 2018-08-18 stsp if (err) {
2781 e5a0f69f 2018-08-18 stsp view_close(blame_view);
2782 e5a0f69f 2018-08-18 stsp free(path);
2783 e5a0f69f 2018-08-18 stsp } else
2784 e5a0f69f 2018-08-18 stsp *new_view = blame_view;
2785 69efd4c4 2018-07-18 stsp return err;
2786 69efd4c4 2018-07-18 stsp }
2787 69efd4c4 2018-07-18 stsp
2788 69efd4c4 2018-07-18 stsp static const struct got_error *
2789 e5a0f69f 2018-08-18 stsp log_tree_entry(struct tog_view **new_view, struct tog_view *parent_view,
2790 e5a0f69f 2018-08-18 stsp struct got_tree_entry *te, struct tog_parent_trees *parents,
2791 e5a0f69f 2018-08-18 stsp struct got_object_id *commit_id, struct got_repository *repo)
2792 69efd4c4 2018-07-18 stsp {
2793 e5a0f69f 2018-08-18 stsp struct tog_view *log_view;
2794 69efd4c4 2018-07-18 stsp const struct got_error *err = NULL;
2795 69efd4c4 2018-07-18 stsp char *path;
2796 69efd4c4 2018-07-18 stsp
2797 e5a0f69f 2018-08-18 stsp log_view = view_open(0, 0, 0, 0, parent_view, TOG_VIEW_LOG);
2798 e5a0f69f 2018-08-18 stsp if (log_view == NULL)
2799 e5a0f69f 2018-08-18 stsp return got_error_from_errno();
2800 e5a0f69f 2018-08-18 stsp
2801 69efd4c4 2018-07-18 stsp err = tree_entry_path(&path, parents, te);
2802 69efd4c4 2018-07-18 stsp if (err)
2803 69efd4c4 2018-07-18 stsp return err;
2804 69efd4c4 2018-07-18 stsp
2805 e5a0f69f 2018-08-18 stsp err = open_log_view(log_view, commit_id, repo, path);
2806 ba4f502b 2018-08-04 stsp if (err)
2807 e5a0f69f 2018-08-18 stsp view_close(log_view);
2808 e5a0f69f 2018-08-18 stsp else
2809 e5a0f69f 2018-08-18 stsp *new_view = log_view;
2810 cb2ebc8a 2018-06-23 stsp free(path);
2811 cb2ebc8a 2018-06-23 stsp return err;
2812 ffd1d5e5 2018-06-23 stsp }
2813 ffd1d5e5 2018-06-23 stsp
2814 ffd1d5e5 2018-06-23 stsp static const struct got_error *
2815 ad80ab7b 2018-08-04 stsp open_tree_view(struct tog_view *view, struct got_tree_object *root,
2816 5221c383 2018-08-01 stsp struct got_object_id *commit_id, struct got_repository *repo)
2817 ffd1d5e5 2018-06-23 stsp {
2818 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
2819 ad80ab7b 2018-08-04 stsp char *commit_id_str = NULL;
2820 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
2821 ffd1d5e5 2018-06-23 stsp
2822 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->parents);
2823 ffd1d5e5 2018-06-23 stsp
2824 ffd1d5e5 2018-06-23 stsp err = got_object_id_str(&commit_id_str, commit_id);
2825 ffd1d5e5 2018-06-23 stsp if (err != NULL)
2826 ffd1d5e5 2018-06-23 stsp goto done;
2827 ffd1d5e5 2018-06-23 stsp
2828 fb2756b9 2018-08-04 stsp if (asprintf(&s->tree_label, "commit: %s", commit_id_str) == -1) {
2829 ffd1d5e5 2018-06-23 stsp err = got_error_from_errno();
2830 ffd1d5e5 2018-06-23 stsp goto done;
2831 ffd1d5e5 2018-06-23 stsp }
2832 ffd1d5e5 2018-06-23 stsp
2833 fb2756b9 2018-08-04 stsp s->root = s->tree = root;
2834 fb2756b9 2018-08-04 stsp s->entries = got_object_tree_get_entries(root);
2835 fb2756b9 2018-08-04 stsp s->first_displayed_entry = SIMPLEQ_FIRST(&s->entries->head);
2836 fb2756b9 2018-08-04 stsp s->commit_id = commit_id;
2837 fb2756b9 2018-08-04 stsp s->repo = repo;
2838 e5a0f69f 2018-08-18 stsp
2839 e5a0f69f 2018-08-18 stsp view->show = show_tree_view;
2840 e5a0f69f 2018-08-18 stsp view->input = input_tree_view;
2841 e5a0f69f 2018-08-18 stsp view->close = close_tree_view;
2842 ad80ab7b 2018-08-04 stsp done:
2843 ad80ab7b 2018-08-04 stsp free(commit_id_str);
2844 ad80ab7b 2018-08-04 stsp if (err)
2845 fb2756b9 2018-08-04 stsp free(s->tree_label);
2846 ad80ab7b 2018-08-04 stsp return err;
2847 ad80ab7b 2018-08-04 stsp }
2848 ad80ab7b 2018-08-04 stsp
2849 e5a0f69f 2018-08-18 stsp static const struct got_error *
2850 ad80ab7b 2018-08-04 stsp close_tree_view(struct tog_view *view)
2851 ad80ab7b 2018-08-04 stsp {
2852 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
2853 ad80ab7b 2018-08-04 stsp
2854 fb2756b9 2018-08-04 stsp free(s->tree_label);
2855 fb2756b9 2018-08-04 stsp while (!TAILQ_EMPTY(&s->parents)) {
2856 ad80ab7b 2018-08-04 stsp struct tog_parent_tree *parent;
2857 fb2756b9 2018-08-04 stsp parent = TAILQ_FIRST(&s->parents);
2858 fb2756b9 2018-08-04 stsp TAILQ_REMOVE(&s->parents, parent, entry);
2859 ad80ab7b 2018-08-04 stsp free(parent);
2860 ad80ab7b 2018-08-04 stsp
2861 ad80ab7b 2018-08-04 stsp }
2862 fb2756b9 2018-08-04 stsp if (s->tree != s->root)
2863 fb2756b9 2018-08-04 stsp got_object_tree_close(s->tree);
2864 e5a0f69f 2018-08-18 stsp got_object_tree_close(s->root);
2865 e5a0f69f 2018-08-18 stsp
2866 e5a0f69f 2018-08-18 stsp return NULL;
2867 ad80ab7b 2018-08-04 stsp }
2868 ad80ab7b 2018-08-04 stsp
2869 ad80ab7b 2018-08-04 stsp static const struct got_error *
2870 ad80ab7b 2018-08-04 stsp show_tree_view(struct tog_view *view)
2871 ad80ab7b 2018-08-04 stsp {
2872 ad80ab7b 2018-08-04 stsp const struct got_error *err = NULL;
2873 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
2874 e5a0f69f 2018-08-18 stsp char *parent_path;
2875 ad80ab7b 2018-08-04 stsp
2876 e5a0f69f 2018-08-18 stsp err = tree_entry_path(&parent_path, &s->parents, NULL);
2877 e5a0f69f 2018-08-18 stsp if (err)
2878 e5a0f69f 2018-08-18 stsp return err;
2879 ffd1d5e5 2018-06-23 stsp
2880 e5a0f69f 2018-08-18 stsp err = draw_tree_entries(view, &s->first_displayed_entry,
2881 e5a0f69f 2018-08-18 stsp &s->last_displayed_entry, &s->selected_entry,
2882 e5a0f69f 2018-08-18 stsp &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
2883 e5a0f69f 2018-08-18 stsp s->entries, s->selected, view->nlines, s->tree == s->root);
2884 e5a0f69f 2018-08-18 stsp free(parent_path);
2885 e5a0f69f 2018-08-18 stsp return err;
2886 e5a0f69f 2018-08-18 stsp }
2887 ce52c690 2018-06-23 stsp
2888 e5a0f69f 2018-08-18 stsp static const struct got_error *
2889 e5a0f69f 2018-08-18 stsp input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
2890 e5a0f69f 2018-08-18 stsp struct tog_view *view, int ch)
2891 e5a0f69f 2018-08-18 stsp {
2892 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
2893 e5a0f69f 2018-08-18 stsp struct tog_tree_view_state *s = &view->state.tree;
2894 ffd1d5e5 2018-06-23 stsp
2895 e5a0f69f 2018-08-18 stsp switch (ch) {
2896 e5a0f69f 2018-08-18 stsp case 'i':
2897 e5a0f69f 2018-08-18 stsp s->show_ids = !s->show_ids;
2898 ffd1d5e5 2018-06-23 stsp break;
2899 e5a0f69f 2018-08-18 stsp case 'l':
2900 e5a0f69f 2018-08-18 stsp if (s->selected_entry) {
2901 e5a0f69f 2018-08-18 stsp err = log_tree_entry(new_view, view,
2902 e5a0f69f 2018-08-18 stsp s->selected_entry, &s->parents,
2903 e5a0f69f 2018-08-18 stsp s->commit_id, s->repo);
2904 e5a0f69f 2018-08-18 stsp }
2905 e5a0f69f 2018-08-18 stsp break;
2906 e5a0f69f 2018-08-18 stsp case 'k':
2907 e5a0f69f 2018-08-18 stsp case KEY_UP:
2908 e5a0f69f 2018-08-18 stsp if (s->selected > 0)
2909 e5a0f69f 2018-08-18 stsp s->selected--;
2910 e5a0f69f 2018-08-18 stsp if (s->selected > 0)
2911 ffd1d5e5 2018-06-23 stsp break;
2912 e5a0f69f 2018-08-18 stsp tree_scroll_up(&s->first_displayed_entry, 1,
2913 e5a0f69f 2018-08-18 stsp s->entries, s->tree == s->root);
2914 e5a0f69f 2018-08-18 stsp break;
2915 e5a0f69f 2018-08-18 stsp case KEY_PPAGE:
2916 e5a0f69f 2018-08-18 stsp if (SIMPLEQ_FIRST(&s->entries->head) ==
2917 e5a0f69f 2018-08-18 stsp s->first_displayed_entry) {
2918 e5a0f69f 2018-08-18 stsp if (s->tree != s->root)
2919 e5a0f69f 2018-08-18 stsp s->first_displayed_entry = NULL;
2920 e5a0f69f 2018-08-18 stsp s->selected = 0;
2921 69efd4c4 2018-07-18 stsp break;
2922 e5a0f69f 2018-08-18 stsp }
2923 e5a0f69f 2018-08-18 stsp tree_scroll_up(&s->first_displayed_entry,
2924 e5a0f69f 2018-08-18 stsp view->nlines, s->entries,
2925 e5a0f69f 2018-08-18 stsp s->tree == s->root);
2926 e5a0f69f 2018-08-18 stsp break;
2927 e5a0f69f 2018-08-18 stsp case 'j':
2928 e5a0f69f 2018-08-18 stsp case KEY_DOWN:
2929 e5a0f69f 2018-08-18 stsp if (s->selected < s->ndisplayed - 1) {
2930 e5a0f69f 2018-08-18 stsp s->selected++;
2931 1d13200f 2018-07-12 stsp break;
2932 e5a0f69f 2018-08-18 stsp }
2933 e5a0f69f 2018-08-18 stsp tree_scroll_down(&s->first_displayed_entry, 1,
2934 e5a0f69f 2018-08-18 stsp s->last_displayed_entry, s->entries);
2935 e5a0f69f 2018-08-18 stsp break;
2936 e5a0f69f 2018-08-18 stsp case KEY_NPAGE:
2937 e5a0f69f 2018-08-18 stsp tree_scroll_down(&s->first_displayed_entry,
2938 e5a0f69f 2018-08-18 stsp view->nlines, s->last_displayed_entry,
2939 e5a0f69f 2018-08-18 stsp s->entries);
2940 e5a0f69f 2018-08-18 stsp if (SIMPLEQ_NEXT(s->last_displayed_entry,
2941 e5a0f69f 2018-08-18 stsp entry))
2942 ffd1d5e5 2018-06-23 stsp break;
2943 e5a0f69f 2018-08-18 stsp /* can't scroll any further; move cursor down */
2944 e5a0f69f 2018-08-18 stsp if (s->selected < s->ndisplayed - 1)
2945 e5a0f69f 2018-08-18 stsp s->selected = s->ndisplayed - 1;
2946 e5a0f69f 2018-08-18 stsp break;
2947 e5a0f69f 2018-08-18 stsp case KEY_ENTER:
2948 e5a0f69f 2018-08-18 stsp case '\r':
2949 e5a0f69f 2018-08-18 stsp if (s->selected_entry == NULL) {
2950 e5a0f69f 2018-08-18 stsp struct tog_parent_tree *parent;
2951 31607d6c 2018-08-18 stsp case KEY_LEFT:
2952 e5a0f69f 2018-08-18 stsp /* user selected '..' */
2953 e5a0f69f 2018-08-18 stsp if (s->tree == s->root)
2954 ffd1d5e5 2018-06-23 stsp break;
2955 e5a0f69f 2018-08-18 stsp parent = TAILQ_FIRST(&s->parents);
2956 e5a0f69f 2018-08-18 stsp TAILQ_REMOVE(&s->parents, parent,
2957 e5a0f69f 2018-08-18 stsp entry);
2958 e5a0f69f 2018-08-18 stsp got_object_tree_close(s->tree);
2959 e5a0f69f 2018-08-18 stsp s->tree = parent->tree;
2960 e5a0f69f 2018-08-18 stsp s->entries =
2961 e5a0f69f 2018-08-18 stsp got_object_tree_get_entries(s->tree);
2962 e5a0f69f 2018-08-18 stsp s->first_displayed_entry =
2963 e5a0f69f 2018-08-18 stsp parent->first_displayed_entry;
2964 e5a0f69f 2018-08-18 stsp s->selected_entry =
2965 e5a0f69f 2018-08-18 stsp parent->selected_entry;
2966 e5a0f69f 2018-08-18 stsp s->selected = parent->selected;
2967 e5a0f69f 2018-08-18 stsp free(parent);
2968 e5a0f69f 2018-08-18 stsp } else if (S_ISDIR(s->selected_entry->mode)) {
2969 e5a0f69f 2018-08-18 stsp struct tog_parent_tree *parent;
2970 e5a0f69f 2018-08-18 stsp struct got_tree_object *child;
2971 e5a0f69f 2018-08-18 stsp err = got_object_open_as_tree(&child,
2972 e5a0f69f 2018-08-18 stsp s->repo, s->selected_entry->id);
2973 e5a0f69f 2018-08-18 stsp if (err)
2974 ffd1d5e5 2018-06-23 stsp break;
2975 e5a0f69f 2018-08-18 stsp parent = calloc(1, sizeof(*parent));
2976 e5a0f69f 2018-08-18 stsp if (parent == NULL) {
2977 e5a0f69f 2018-08-18 stsp err = got_error_from_errno();
2978 e5a0f69f 2018-08-18 stsp break;
2979 ffd1d5e5 2018-06-23 stsp }
2980 e5a0f69f 2018-08-18 stsp parent->tree = s->tree;
2981 e5a0f69f 2018-08-18 stsp parent->first_displayed_entry =
2982 e5a0f69f 2018-08-18 stsp s->first_displayed_entry;
2983 e5a0f69f 2018-08-18 stsp parent->selected_entry = s->selected_entry;
2984 e5a0f69f 2018-08-18 stsp parent->selected = s->selected;
2985 e5a0f69f 2018-08-18 stsp TAILQ_INSERT_HEAD(&s->parents, parent, entry);
2986 e5a0f69f 2018-08-18 stsp s->tree = child;
2987 e5a0f69f 2018-08-18 stsp s->entries =
2988 e5a0f69f 2018-08-18 stsp got_object_tree_get_entries(s->tree);
2989 e5a0f69f 2018-08-18 stsp s->selected = 0;
2990 e5a0f69f 2018-08-18 stsp s->first_displayed_entry = NULL;
2991 e5a0f69f 2018-08-18 stsp } else if (S_ISREG(s->selected_entry->mode)) {
2992 e5a0f69f 2018-08-18 stsp err = blame_tree_entry(new_view, view,
2993 e5a0f69f 2018-08-18 stsp s->selected_entry, &s->parents,
2994 e5a0f69f 2018-08-18 stsp s->commit_id, s->repo);
2995 e5a0f69f 2018-08-18 stsp if (err)
2996 ffd1d5e5 2018-06-23 stsp break;
2997 e5a0f69f 2018-08-18 stsp }
2998 e5a0f69f 2018-08-18 stsp break;
2999 e5a0f69f 2018-08-18 stsp case KEY_RESIZE:
3000 e5a0f69f 2018-08-18 stsp if (s->selected > view->nlines)
3001 e5a0f69f 2018-08-18 stsp s->selected = s->ndisplayed - 1;
3002 e5a0f69f 2018-08-18 stsp break;
3003 e5a0f69f 2018-08-18 stsp default:
3004 e5a0f69f 2018-08-18 stsp break;
3005 ffd1d5e5 2018-06-23 stsp }
3006 e5a0f69f 2018-08-18 stsp
3007 ffd1d5e5 2018-06-23 stsp return err;
3008 9f7d7167 2018-04-29 stsp }
3009 9f7d7167 2018-04-29 stsp
3010 ffd1d5e5 2018-06-23 stsp __dead static void
3011 ffd1d5e5 2018-06-23 stsp usage_tree(void)
3012 ffd1d5e5 2018-06-23 stsp {
3013 ffd1d5e5 2018-06-23 stsp endwin();
3014 ffd1d5e5 2018-06-23 stsp fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
3015 ffd1d5e5 2018-06-23 stsp getprogname());
3016 ffd1d5e5 2018-06-23 stsp exit(1);
3017 ffd1d5e5 2018-06-23 stsp }
3018 ffd1d5e5 2018-06-23 stsp
3019 ffd1d5e5 2018-06-23 stsp static const struct got_error *
3020 ffd1d5e5 2018-06-23 stsp cmd_tree(int argc, char *argv[])
3021 ffd1d5e5 2018-06-23 stsp {
3022 ffd1d5e5 2018-06-23 stsp const struct got_error *error;
3023 ffd1d5e5 2018-06-23 stsp struct got_repository *repo = NULL;
3024 ffd1d5e5 2018-06-23 stsp char *repo_path = NULL;
3025 ffd1d5e5 2018-06-23 stsp struct got_object_id *commit_id = NULL;
3026 ffd1d5e5 2018-06-23 stsp char *commit_id_arg = NULL;
3027 ffd1d5e5 2018-06-23 stsp struct got_commit_object *commit = NULL;
3028 ffd1d5e5 2018-06-23 stsp struct got_tree_object *tree = NULL;
3029 ffd1d5e5 2018-06-23 stsp int ch;
3030 5221c383 2018-08-01 stsp struct tog_view *view;
3031 ffd1d5e5 2018-06-23 stsp
3032 ffd1d5e5 2018-06-23 stsp #ifndef PROFILE
3033 ad242220 2018-09-08 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd", NULL)
3034 ad242220 2018-09-08 stsp == -1)
3035 ffd1d5e5 2018-06-23 stsp err(1, "pledge");
3036 ffd1d5e5 2018-06-23 stsp #endif
3037 ffd1d5e5 2018-06-23 stsp
3038 ffd1d5e5 2018-06-23 stsp while ((ch = getopt(argc, argv, "c:")) != -1) {
3039 ffd1d5e5 2018-06-23 stsp switch (ch) {
3040 ffd1d5e5 2018-06-23 stsp case 'c':
3041 ffd1d5e5 2018-06-23 stsp commit_id_arg = optarg;
3042 ffd1d5e5 2018-06-23 stsp break;
3043 ffd1d5e5 2018-06-23 stsp default:
3044 ffd1d5e5 2018-06-23 stsp usage();
3045 ffd1d5e5 2018-06-23 stsp /* NOTREACHED */
3046 ffd1d5e5 2018-06-23 stsp }
3047 ffd1d5e5 2018-06-23 stsp }
3048 ffd1d5e5 2018-06-23 stsp
3049 ffd1d5e5 2018-06-23 stsp argc -= optind;
3050 ffd1d5e5 2018-06-23 stsp argv += optind;
3051 ffd1d5e5 2018-06-23 stsp
3052 ffd1d5e5 2018-06-23 stsp if (argc == 0) {
3053 ffd1d5e5 2018-06-23 stsp repo_path = getcwd(NULL, 0);
3054 ffd1d5e5 2018-06-23 stsp if (repo_path == NULL)
3055 ffd1d5e5 2018-06-23 stsp return got_error_from_errno();
3056 ffd1d5e5 2018-06-23 stsp } else if (argc == 1) {
3057 ffd1d5e5 2018-06-23 stsp repo_path = realpath(argv[0], NULL);
3058 ffd1d5e5 2018-06-23 stsp if (repo_path == NULL)
3059 ffd1d5e5 2018-06-23 stsp return got_error_from_errno();
3060 ffd1d5e5 2018-06-23 stsp } else
3061 ffd1d5e5 2018-06-23 stsp usage_log();
3062 ffd1d5e5 2018-06-23 stsp
3063 ffd1d5e5 2018-06-23 stsp error = got_repo_open(&repo, repo_path);
3064 ffd1d5e5 2018-06-23 stsp free(repo_path);
3065 ffd1d5e5 2018-06-23 stsp if (error != NULL)
3066 ffd1d5e5 2018-06-23 stsp return error;
3067 ffd1d5e5 2018-06-23 stsp
3068 ffd1d5e5 2018-06-23 stsp if (commit_id_arg == NULL) {
3069 ffd1d5e5 2018-06-23 stsp error = get_head_commit_id(&commit_id, repo);
3070 ffd1d5e5 2018-06-23 stsp if (error != NULL)
3071 ffd1d5e5 2018-06-23 stsp goto done;
3072 ffd1d5e5 2018-06-23 stsp } else {
3073 ffd1d5e5 2018-06-23 stsp struct got_object *obj;
3074 ffd1d5e5 2018-06-23 stsp error = got_object_open_by_id_str(&obj, repo, commit_id_arg);
3075 ffd1d5e5 2018-06-23 stsp if (error == NULL) {
3076 6402fb3c 2018-09-15 stsp commit_id = got_object_id_dup(got_object_get_id(obj));
3077 ffd1d5e5 2018-06-23 stsp if (commit_id == NULL)
3078 ffd1d5e5 2018-06-23 stsp error = got_error_from_errno();
3079 ffd1d5e5 2018-06-23 stsp }
3080 ffd1d5e5 2018-06-23 stsp }
3081 ffd1d5e5 2018-06-23 stsp if (error != NULL)
3082 ffd1d5e5 2018-06-23 stsp goto done;
3083 ffd1d5e5 2018-06-23 stsp
3084 ffd1d5e5 2018-06-23 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
3085 ffd1d5e5 2018-06-23 stsp if (error != NULL)
3086 ffd1d5e5 2018-06-23 stsp goto done;
3087 ffd1d5e5 2018-06-23 stsp
3088 ffd1d5e5 2018-06-23 stsp error = got_object_open_as_tree(&tree, repo, commit->tree_id);
3089 ffd1d5e5 2018-06-23 stsp if (error != NULL)
3090 ffd1d5e5 2018-06-23 stsp goto done;
3091 ffd1d5e5 2018-06-23 stsp
3092 b3665f43 2018-08-04 stsp view = view_open(0, 0, 0, 0, NULL, TOG_VIEW_TREE);
3093 5221c383 2018-08-01 stsp if (view == NULL) {
3094 5221c383 2018-08-01 stsp error = got_error_from_errno();
3095 5221c383 2018-08-01 stsp goto done;
3096 5221c383 2018-08-01 stsp }
3097 ad80ab7b 2018-08-04 stsp error = open_tree_view(view, tree, commit_id, repo);
3098 ad80ab7b 2018-08-04 stsp if (error)
3099 ad80ab7b 2018-08-04 stsp goto done;
3100 e5a0f69f 2018-08-18 stsp error = view_loop(view);
3101 ffd1d5e5 2018-06-23 stsp done:
3102 ffd1d5e5 2018-06-23 stsp free(commit_id);
3103 ffd1d5e5 2018-06-23 stsp if (commit)
3104 ffd1d5e5 2018-06-23 stsp got_object_commit_close(commit);
3105 ffd1d5e5 2018-06-23 stsp if (tree)
3106 ffd1d5e5 2018-06-23 stsp got_object_tree_close(tree);
3107 ffd1d5e5 2018-06-23 stsp if (repo)
3108 ffd1d5e5 2018-06-23 stsp got_repo_close(repo);
3109 ffd1d5e5 2018-06-23 stsp return error;
3110 ffd1d5e5 2018-06-23 stsp }
3111 5c5136c5 2018-05-20 stsp static void
3112 9f7d7167 2018-04-29 stsp init_curses(void)
3113 9f7d7167 2018-04-29 stsp {
3114 9f7d7167 2018-04-29 stsp initscr();
3115 9f7d7167 2018-04-29 stsp cbreak();
3116 9f7d7167 2018-04-29 stsp noecho();
3117 9f7d7167 2018-04-29 stsp nonl();
3118 9f7d7167 2018-04-29 stsp intrflush(stdscr, FALSE);
3119 9f7d7167 2018-04-29 stsp keypad(stdscr, TRUE);
3120 1f475ad8 2018-05-10 stsp curs_set(0);
3121 9f7d7167 2018-04-29 stsp }
3122 9f7d7167 2018-04-29 stsp
3123 4ed7e80c 2018-05-20 stsp __dead static void
3124 9f7d7167 2018-04-29 stsp usage(void)
3125 9f7d7167 2018-04-29 stsp {
3126 9f7d7167 2018-04-29 stsp int i;
3127 9f7d7167 2018-04-29 stsp
3128 c2301be8 2018-04-30 stsp fprintf(stderr, "usage: %s [-h] [command] [arg ...]\n\n"
3129 9f7d7167 2018-04-29 stsp "Available commands:\n", getprogname());
3130 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
3131 9f7d7167 2018-04-29 stsp struct tog_cmd *cmd = &tog_commands[i];
3132 c2301be8 2018-04-30 stsp fprintf(stderr, " %s: %s\n", cmd->name, cmd->descr);
3133 9f7d7167 2018-04-29 stsp }
3134 9f7d7167 2018-04-29 stsp exit(1);
3135 9f7d7167 2018-04-29 stsp }
3136 9f7d7167 2018-04-29 stsp
3137 c2301be8 2018-04-30 stsp static char **
3138 c2301be8 2018-04-30 stsp make_argv(const char *arg0, const char *arg1)
3139 c2301be8 2018-04-30 stsp {
3140 c2301be8 2018-04-30 stsp char **argv;
3141 c2301be8 2018-04-30 stsp int argc = (arg1 == NULL ? 1 : 2);
3142 c2301be8 2018-04-30 stsp
3143 c2301be8 2018-04-30 stsp argv = calloc(argc, sizeof(char *));
3144 c2301be8 2018-04-30 stsp if (argv == NULL)
3145 c2301be8 2018-04-30 stsp err(1, "calloc");
3146 c2301be8 2018-04-30 stsp argv[0] = strdup(arg0);
3147 c2301be8 2018-04-30 stsp if (argv[0] == NULL)
3148 c2301be8 2018-04-30 stsp err(1, "calloc");
3149 c2301be8 2018-04-30 stsp if (arg1) {
3150 c2301be8 2018-04-30 stsp argv[1] = strdup(arg1);
3151 c2301be8 2018-04-30 stsp if (argv[1] == NULL)
3152 c2301be8 2018-04-30 stsp err(1, "calloc");
3153 c2301be8 2018-04-30 stsp }
3154 c2301be8 2018-04-30 stsp
3155 c2301be8 2018-04-30 stsp return argv;
3156 c2301be8 2018-04-30 stsp }
3157 c2301be8 2018-04-30 stsp
3158 9f7d7167 2018-04-29 stsp int
3159 9f7d7167 2018-04-29 stsp main(int argc, char *argv[])
3160 9f7d7167 2018-04-29 stsp {
3161 9f7d7167 2018-04-29 stsp const struct got_error *error = NULL;
3162 9f7d7167 2018-04-29 stsp struct tog_cmd *cmd = NULL;
3163 9f7d7167 2018-04-29 stsp int ch, hflag = 0;
3164 c2301be8 2018-04-30 stsp char **cmd_argv = NULL;
3165 9f7d7167 2018-04-29 stsp
3166 9f7d7167 2018-04-29 stsp setlocale(LC_ALL, "");
3167 9f7d7167 2018-04-29 stsp
3168 9f7d7167 2018-04-29 stsp while ((ch = getopt(argc, argv, "h")) != -1) {
3169 9f7d7167 2018-04-29 stsp switch (ch) {
3170 9f7d7167 2018-04-29 stsp case 'h':
3171 9f7d7167 2018-04-29 stsp hflag = 1;
3172 9f7d7167 2018-04-29 stsp break;
3173 9f7d7167 2018-04-29 stsp default:
3174 9f7d7167 2018-04-29 stsp usage();
3175 9f7d7167 2018-04-29 stsp /* NOTREACHED */
3176 9f7d7167 2018-04-29 stsp }
3177 9f7d7167 2018-04-29 stsp }
3178 9f7d7167 2018-04-29 stsp
3179 9f7d7167 2018-04-29 stsp argc -= optind;
3180 9f7d7167 2018-04-29 stsp argv += optind;
3181 9f7d7167 2018-04-29 stsp optind = 0;
3182 c2301be8 2018-04-30 stsp optreset = 1;
3183 9f7d7167 2018-04-29 stsp
3184 c2301be8 2018-04-30 stsp if (argc == 0) {
3185 f29d3e89 2018-06-23 stsp if (hflag)
3186 f29d3e89 2018-06-23 stsp usage();
3187 c2301be8 2018-04-30 stsp /* Build an argument vector which runs a default command. */
3188 9f7d7167 2018-04-29 stsp cmd = &tog_commands[0];
3189 c2301be8 2018-04-30 stsp cmd_argv = make_argv(cmd->name, NULL);
3190 c2301be8 2018-04-30 stsp argc = 1;
3191 c2301be8 2018-04-30 stsp } else {
3192 9f7d7167 2018-04-29 stsp int i;
3193 9f7d7167 2018-04-29 stsp
3194 c2301be8 2018-04-30 stsp /* Did the user specific a command? */
3195 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
3196 c2301be8 2018-04-30 stsp if (strncmp(tog_commands[i].name, argv[0],
3197 9f7d7167 2018-04-29 stsp strlen(argv[0])) == 0) {
3198 9f7d7167 2018-04-29 stsp cmd = &tog_commands[i];
3199 9f7d7167 2018-04-29 stsp if (hflag)
3200 9f7d7167 2018-04-29 stsp tog_commands[i].cmd_usage();
3201 9f7d7167 2018-04-29 stsp break;
3202 9f7d7167 2018-04-29 stsp }
3203 9f7d7167 2018-04-29 stsp }
3204 9f7d7167 2018-04-29 stsp if (cmd == NULL) {
3205 c2301be8 2018-04-30 stsp /* Did the user specify a repository? */
3206 c2301be8 2018-04-30 stsp char *repo_path = realpath(argv[0], NULL);
3207 c2301be8 2018-04-30 stsp if (repo_path) {
3208 c2301be8 2018-04-30 stsp struct got_repository *repo;
3209 c2301be8 2018-04-30 stsp error = got_repo_open(&repo, repo_path);
3210 c2301be8 2018-04-30 stsp if (error == NULL)
3211 c2301be8 2018-04-30 stsp got_repo_close(repo);
3212 c2301be8 2018-04-30 stsp } else
3213 ad7de8d9 2018-04-30 stsp error = got_error_from_errno();
3214 c2301be8 2018-04-30 stsp if (error) {
3215 f29d3e89 2018-06-23 stsp if (hflag) {
3216 f29d3e89 2018-06-23 stsp fprintf(stderr, "%s: '%s' is not a "
3217 f29d3e89 2018-06-23 stsp "known command\n", getprogname(),
3218 f29d3e89 2018-06-23 stsp argv[0]);
3219 f29d3e89 2018-06-23 stsp usage();
3220 f29d3e89 2018-06-23 stsp }
3221 ad7de8d9 2018-04-30 stsp fprintf(stderr, "%s: '%s' is neither a known "
3222 ad7de8d9 2018-04-30 stsp "command nor a path to a repository\n",
3223 c2301be8 2018-04-30 stsp getprogname(), argv[0]);
3224 ad7de8d9 2018-04-30 stsp free(repo_path);
3225 c2301be8 2018-04-30 stsp return 1;
3226 c2301be8 2018-04-30 stsp }
3227 c2301be8 2018-04-30 stsp cmd = &tog_commands[0];
3228 c2301be8 2018-04-30 stsp cmd_argv = make_argv(cmd->name, repo_path);
3229 c2301be8 2018-04-30 stsp argc = 2;
3230 c2301be8 2018-04-30 stsp free(repo_path);
3231 9f7d7167 2018-04-29 stsp }
3232 9f7d7167 2018-04-29 stsp }
3233 9f7d7167 2018-04-29 stsp
3234 5c5136c5 2018-05-20 stsp init_curses();
3235 9f7d7167 2018-04-29 stsp
3236 c2301be8 2018-04-30 stsp error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
3237 9f7d7167 2018-04-29 stsp if (error)
3238 9f7d7167 2018-04-29 stsp goto done;
3239 9f7d7167 2018-04-29 stsp done:
3240 9f7d7167 2018-04-29 stsp endwin();
3241 c2301be8 2018-04-30 stsp free(cmd_argv);
3242 9f7d7167 2018-04-29 stsp if (error)
3243 9f7d7167 2018-04-29 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
3244 9f7d7167 2018-04-29 stsp return 0;
3245 9f7d7167 2018-04-29 stsp }