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