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