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