2 9f7d7167 2018-04-29 stsp * Copyright (c) 2018 Stefan Sperling <stsp@openbsd.org>
4 9f7d7167 2018-04-29 stsp * Permission to use, copy, modify, and distribute this software for any
5 9f7d7167 2018-04-29 stsp * purpose with or without fee is hereby granted, provided that the above
6 9f7d7167 2018-04-29 stsp * copyright notice and this permission notice appear in all copies.
8 9f7d7167 2018-04-29 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 9f7d7167 2018-04-29 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 9f7d7167 2018-04-29 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 9f7d7167 2018-04-29 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 9f7d7167 2018-04-29 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 9f7d7167 2018-04-29 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 9f7d7167 2018-04-29 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 80ddbec8 2018-04-29 stsp #include <sys/queue.h>
19 31120ada 2018-04-30 stsp #include <errno.h>
20 61e69b96 2018-05-20 stsp #define _XOPEN_SOURCE_EXTENDED
21 9f7d7167 2018-04-29 stsp #include <curses.h>
22 61e69b96 2018-05-20 stsp #undef _XOPEN_SOURCE_EXTENDED
23 9f7d7167 2018-04-29 stsp #include <panel.h>
24 9f7d7167 2018-04-29 stsp #include <locale.h>
25 9f7d7167 2018-04-29 stsp #include <stdlib.h>
26 26ed57b2 2018-05-19 stsp #include <stdio.h>
27 9f7d7167 2018-04-29 stsp #include <getopt.h>
28 9f7d7167 2018-04-29 stsp #include <string.h>
29 9f7d7167 2018-04-29 stsp #include <err.h>
30 80ddbec8 2018-04-29 stsp #include <unistd.h>
31 26ed57b2 2018-05-19 stsp #include <util.h>
32 26ed57b2 2018-05-19 stsp #include <limits.h>
33 61e69b96 2018-05-20 stsp #include <wchar.h>
35 9f7d7167 2018-04-29 stsp #include "got_error.h"
36 80ddbec8 2018-04-29 stsp #include "got_object.h"
37 80ddbec8 2018-04-29 stsp #include "got_reference.h"
38 80ddbec8 2018-04-29 stsp #include "got_repository.h"
39 80ddbec8 2018-04-29 stsp #include "got_diff.h"
40 511a516b 2018-05-19 stsp #include "got_opentemp.h"
43 881b2d3e 2018-04-30 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
46 9f7d7167 2018-04-29 stsp #ifndef nitems
47 9f7d7167 2018-04-29 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
50 9f7d7167 2018-04-29 stsp struct tog_cmd {
51 c2301be8 2018-04-30 stsp const char *name;
52 9f7d7167 2018-04-29 stsp const struct got_error *(*cmd_main)(int, char *[]);
53 9f7d7167 2018-04-29 stsp void (*cmd_usage)(void);
54 c2301be8 2018-04-30 stsp const char *descr;
57 4ed7e80c 2018-05-20 stsp __dead static void usage(void);
58 4ed7e80c 2018-05-20 stsp __dead static void usage_log(void);
59 4ed7e80c 2018-05-20 stsp __dead static void usage_diff(void);
60 4ed7e80c 2018-05-20 stsp __dead static void usage_blame(void);
62 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_log(int, char *[]);
63 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_diff(int, char *[]);
64 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_blame(int, char *[]);
66 4ed7e80c 2018-05-20 stsp static struct tog_cmd tog_commands[] = {
67 cbb6b58a 2018-05-20 stsp { "log", cmd_log, usage_log,
68 9f7d7167 2018-04-29 stsp "show repository history" },
69 cbb6b58a 2018-05-20 stsp { "diff", cmd_diff, usage_diff,
70 9f7d7167 2018-04-29 stsp "compare files and directories" },
71 cbb6b58a 2018-05-20 stsp { "blame", cmd_blame, usage_blame,
72 9f7d7167 2018-04-29 stsp "show line-by-line file history" },
75 fed328cc 2018-05-20 stsp static struct tog_view {
76 26ed57b2 2018-05-19 stsp WINDOW *window;
77 26ed57b2 2018-05-19 stsp PANEL *panel;
78 fed328cc 2018-05-20 stsp } tog_log_view, tog_diff_view;
80 cd0acaa7 2018-05-20 stsp static const struct got_error *
81 cd0acaa7 2018-05-20 stsp show_diff_view(struct got_object *, struct got_object *,
82 cd0acaa7 2018-05-20 stsp struct got_repository *);
83 cd0acaa7 2018-05-20 stsp static const struct got_error *
84 cd0acaa7 2018-05-20 stsp show_log_view(struct got_object_id *, struct got_repository *);
86 4ed7e80c 2018-05-20 stsp __dead static void
87 9f7d7167 2018-04-29 stsp usage_log(void)
90 80ddbec8 2018-04-29 stsp fprintf(stderr, "usage: %s log [-c commit] [repository-path]\n",
91 9f7d7167 2018-04-29 stsp getprogname());
95 963b370f 2018-05-20 stsp /* Create newly allocated wide-character string equivalent to a byte string. */
96 80ddbec8 2018-04-29 stsp static const struct got_error *
97 963b370f 2018-05-20 stsp mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
99 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
101 963b370f 2018-05-20 stsp *ws = NULL;
102 963b370f 2018-05-20 stsp *wlen = mbstowcs(NULL, s, 0);
103 963b370f 2018-05-20 stsp if (*wlen == (size_t)-1)
104 963b370f 2018-05-20 stsp return got_error_from_errno();
106 963b370f 2018-05-20 stsp *ws = calloc(*wlen + 1, sizeof(*ws));
107 963b370f 2018-05-20 stsp if (*ws == NULL)
108 963b370f 2018-05-20 stsp return got_error_from_errno();
110 963b370f 2018-05-20 stsp if (mbstowcs(*ws, s, *wlen) != *wlen)
111 963b370f 2018-05-20 stsp err = got_error_from_errno();
115 963b370f 2018-05-20 stsp *ws = NULL;
118 963b370f 2018-05-20 stsp return err;
121 963b370f 2018-05-20 stsp /* Format a line for display, ensuring that it won't overflow a width limit. */
122 963b370f 2018-05-20 stsp static const struct got_error *
123 963b370f 2018-05-20 stsp format_line(wchar_t **wlinep, int *widthp, char *line, int wlimit)
125 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
126 963b370f 2018-05-20 stsp int cols = 0;
127 963b370f 2018-05-20 stsp wchar_t *wline = NULL;
128 963b370f 2018-05-20 stsp size_t wlen;
131 963b370f 2018-05-20 stsp *wlinep = NULL;
133 963b370f 2018-05-20 stsp err = mbs2ws(&wline, &wlen, line);
135 963b370f 2018-05-20 stsp return err;
138 963b370f 2018-05-20 stsp while (i < wlen && cols <= wlimit) {
139 963b370f 2018-05-20 stsp int width = wcwidth(wline[i]);
140 963b370f 2018-05-20 stsp switch (width) {
145 963b370f 2018-05-20 stsp cols += width;
148 963b370f 2018-05-20 stsp if (wline[i] == L'\t')
149 963b370f 2018-05-20 stsp cols += TABSIZE;
152 963b370f 2018-05-20 stsp err = got_error_from_errno();
155 963b370f 2018-05-20 stsp if (cols <= COLS) {
157 963b370f 2018-05-20 stsp if (widthp)
158 963b370f 2018-05-20 stsp *widthp = cols;
161 963b370f 2018-05-20 stsp wline[i] = L'\0';
164 963b370f 2018-05-20 stsp free(wline);
166 963b370f 2018-05-20 stsp *wlinep = wline;
167 963b370f 2018-05-20 stsp return err;
170 963b370f 2018-05-20 stsp static const struct got_error *
171 0553a4e3 2018-04-30 stsp draw_commit(struct got_commit_object *commit, struct got_object_id *id)
173 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
174 80ddbec8 2018-04-29 stsp char *logmsg0 = NULL, *logmsg = NULL;
175 6d9fbc00 2018-04-29 stsp char *author0 = NULL, *author = NULL;
176 bb737323 2018-05-20 stsp wchar_t *wlogmsg = NULL, *wauthor = NULL;
177 bb737323 2018-05-20 stsp int author_width, logmsg_width;
178 6d9fbc00 2018-04-29 stsp char *newline, *smallerthan;
179 80ddbec8 2018-04-29 stsp char *line = NULL;
180 6d9fbc00 2018-04-29 stsp char *id_str = NULL;
181 bb737323 2018-05-20 stsp size_t id_len;
182 bb737323 2018-05-20 stsp int col, limit;
183 bb737323 2018-05-20 stsp static const size_t id_display_cols = 8;
184 bb737323 2018-05-20 stsp static const size_t author_display_cols = 16;
185 bb737323 2018-05-20 stsp const int avail = COLS - 1;
187 80ddbec8 2018-04-29 stsp err = got_object_id_str(&id_str, id);
189 80ddbec8 2018-04-29 stsp return err;
190 881b2d3e 2018-04-30 stsp id_len = strlen(id_str);
191 bb737323 2018-05-20 stsp if (avail < id_display_cols) {
192 bb737323 2018-05-20 stsp limit = MIN(id_len, avail);
193 bb737323 2018-05-20 stsp waddnstr(tog_log_view.window, id_str, limit);
195 bb737323 2018-05-20 stsp limit = MIN(id_display_cols, id_len);
196 bb737323 2018-05-20 stsp waddnstr(tog_log_view.window, id_str, limit);
198 bb737323 2018-05-20 stsp col = limit + 1;
199 bb737323 2018-05-20 stsp while (col < avail && col < id_display_cols + 2) {
200 bb737323 2018-05-20 stsp waddch(tog_log_view.window, ' ');
203 bb737323 2018-05-20 stsp if (col >= avail)
204 bb737323 2018-05-20 stsp goto endline;
206 6d9fbc00 2018-04-29 stsp author0 = strdup(commit->author);
207 6d9fbc00 2018-04-29 stsp if (author0 == NULL) {
208 80ddbec8 2018-04-29 stsp err = got_error_from_errno();
211 6d9fbc00 2018-04-29 stsp author = author0;
212 6d9fbc00 2018-04-29 stsp smallerthan = strchr(author, '<');
213 6d9fbc00 2018-04-29 stsp if (smallerthan)
214 6d9fbc00 2018-04-29 stsp *smallerthan = '\0';
216 6d9fbc00 2018-04-29 stsp char *at = strchr(author, '@');
218 6d9fbc00 2018-04-29 stsp *at = '\0';
220 bb737323 2018-05-20 stsp limit = MIN(avail, author_display_cols);
221 bb737323 2018-05-20 stsp err = format_line(&wauthor, &author_width, author, limit);
224 bb737323 2018-05-20 stsp waddwstr(tog_log_view.window, wauthor);
225 bb737323 2018-05-20 stsp col += author_width;
226 bb737323 2018-05-20 stsp while (col < avail && author_width < author_display_cols + 1) {
227 bb737323 2018-05-20 stsp waddch(tog_log_view.window, ' ');
229 bb737323 2018-05-20 stsp author_width++;
231 bb737323 2018-05-20 stsp if (col >= avail)
232 bb737323 2018-05-20 stsp goto endline;
234 bb737323 2018-05-20 stsp logmsg0 = strdup(commit->logmsg);
235 bb737323 2018-05-20 stsp if (logmsg0 == NULL) {
236 6d9fbc00 2018-04-29 stsp err = got_error_from_errno();
239 bb737323 2018-05-20 stsp logmsg = logmsg0;
240 bb737323 2018-05-20 stsp while (*logmsg == '\n')
242 bb737323 2018-05-20 stsp newline = strchr(logmsg, '\n');
243 bb737323 2018-05-20 stsp if (newline)
244 bb737323 2018-05-20 stsp *newline = '\0';
245 bb737323 2018-05-20 stsp limit = avail - col;
246 bb737323 2018-05-20 stsp err = format_line(&wlogmsg, &logmsg_width, logmsg, limit);
249 bb737323 2018-05-20 stsp waddwstr(tog_log_view.window, wlogmsg);
250 bb737323 2018-05-20 stsp col += logmsg_width;
251 bb737323 2018-05-20 stsp while (col <= avail) {
252 bb737323 2018-05-20 stsp waddch(tog_log_view.window, ' ');
256 80ddbec8 2018-04-29 stsp waddch(tog_log_view.window, '\n');
258 80ddbec8 2018-04-29 stsp free(logmsg0);
259 bb737323 2018-05-20 stsp free(wlogmsg);
260 6d9fbc00 2018-04-29 stsp free(author0);
261 bb737323 2018-05-20 stsp free(wauthor);
262 80ddbec8 2018-04-29 stsp free(line);
263 6d9fbc00 2018-04-29 stsp free(id_str);
264 80ddbec8 2018-04-29 stsp return err;
267 80ddbec8 2018-04-29 stsp struct commit_queue_entry {
268 80ddbec8 2018-04-29 stsp TAILQ_ENTRY(commit_queue_entry) entry;
269 80ddbec8 2018-04-29 stsp struct got_object_id *id;
270 80ddbec8 2018-04-29 stsp struct got_commit_object *commit;
272 0553a4e3 2018-04-30 stsp TAILQ_HEAD(commit_queue, commit_queue_entry);
274 899d86c2 2018-05-10 stsp static struct commit_queue_entry *
275 899d86c2 2018-05-10 stsp alloc_commit_queue_entry(struct got_commit_object *commit,
276 899d86c2 2018-05-10 stsp struct got_object_id *id)
278 80ddbec8 2018-04-29 stsp struct commit_queue_entry *entry;
280 80ddbec8 2018-04-29 stsp entry = calloc(1, sizeof(*entry));
281 80ddbec8 2018-04-29 stsp if (entry == NULL)
282 899d86c2 2018-05-10 stsp return NULL;
284 899d86c2 2018-05-10 stsp entry->id = id;
285 99db9666 2018-05-07 stsp entry->commit = commit;
286 899d86c2 2018-05-10 stsp return entry;
289 99db9666 2018-05-07 stsp static void
290 99db9666 2018-05-07 stsp pop_commit(struct commit_queue *commits)
292 99db9666 2018-05-07 stsp struct commit_queue_entry *entry;
294 99db9666 2018-05-07 stsp entry = TAILQ_FIRST(commits);
295 99db9666 2018-05-07 stsp TAILQ_REMOVE(commits, entry, entry);
296 99db9666 2018-05-07 stsp got_object_commit_close(entry->commit);
297 99db9666 2018-05-07 stsp free(entry->id);
298 99db9666 2018-05-07 stsp free(entry);
301 99db9666 2018-05-07 stsp static void
302 99db9666 2018-05-07 stsp free_commits(struct commit_queue *commits)
304 99db9666 2018-05-07 stsp while (!TAILQ_EMPTY(commits))
305 99db9666 2018-05-07 stsp pop_commit(commits);
308 c4972b91 2018-05-07 stsp static const struct got_error *
309 899d86c2 2018-05-10 stsp fetch_parent_commit(struct commit_queue_entry **pentry,
310 899d86c2 2018-05-10 stsp struct commit_queue_entry *entry, struct got_repository *repo)
312 899d86c2 2018-05-10 stsp const struct got_error *err = NULL;
313 c4972b91 2018-05-07 stsp struct got_object *obj = NULL;
314 899d86c2 2018-05-10 stsp struct got_commit_object *commit;
315 899d86c2 2018-05-10 stsp struct got_object_id *id;
316 c4972b91 2018-05-07 stsp struct got_parent_id *pid;
318 899d86c2 2018-05-10 stsp *pentry = NULL;
320 c4972b91 2018-05-07 stsp /* Follow the first parent (TODO: handle merge commits). */
321 c4972b91 2018-05-07 stsp pid = SIMPLEQ_FIRST(&entry->commit->parent_ids);
322 c4972b91 2018-05-07 stsp if (pid == NULL)
323 c4972b91 2018-05-07 stsp return NULL;
324 c4972b91 2018-05-07 stsp err = got_object_open(&obj, repo, pid->id);
326 c4972b91 2018-05-07 stsp return err;
327 c4972b91 2018-05-07 stsp if (got_object_get_type(obj) != GOT_OBJ_TYPE_COMMIT) {
328 c4972b91 2018-05-07 stsp err = got_error(GOT_ERR_OBJ_TYPE);
329 c4972b91 2018-05-07 stsp got_object_close(obj);
330 c4972b91 2018-05-07 stsp return err;
333 899d86c2 2018-05-10 stsp err = got_object_commit_open(&commit, repo, obj);
334 c4972b91 2018-05-07 stsp got_object_close(obj);
336 c4972b91 2018-05-07 stsp return err;
338 899d86c2 2018-05-10 stsp id = got_object_id_dup(pid->id);
339 899d86c2 2018-05-10 stsp if (id == NULL) {
340 c4972b91 2018-05-07 stsp err = got_error_from_errno();
341 899d86c2 2018-05-10 stsp got_object_commit_close(commit);
342 899d86c2 2018-05-10 stsp return err;;
345 899d86c2 2018-05-10 stsp *pentry = alloc_commit_queue_entry(commit, id);
346 899d86c2 2018-05-10 stsp if (*pentry == NULL) {
347 c4972b91 2018-05-07 stsp err = got_error_from_errno();
348 899d86c2 2018-05-10 stsp got_object_commit_close(commit);
351 899d86c2 2018-05-10 stsp return err;;
354 899d86c2 2018-05-10 stsp static const struct got_error *
355 899d86c2 2018-05-10 stsp get_head_commit_id(struct got_object_id **head_id, struct got_repository *repo)
357 899d86c2 2018-05-10 stsp const struct got_error *err = NULL;
358 899d86c2 2018-05-10 stsp struct got_reference *head_ref;
360 899d86c2 2018-05-10 stsp *head_id = NULL;
362 899d86c2 2018-05-10 stsp err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
364 899d86c2 2018-05-10 stsp return err;
366 899d86c2 2018-05-10 stsp err = got_ref_resolve(head_id, repo, head_ref);
367 899d86c2 2018-05-10 stsp got_ref_close(head_ref);
369 899d86c2 2018-05-10 stsp *head_id = NULL;
370 899d86c2 2018-05-10 stsp return err;
373 c4972b91 2018-05-07 stsp return NULL;
376 99db9666 2018-05-07 stsp static const struct got_error *
377 899d86c2 2018-05-10 stsp prepend_commits(int *ncommits, struct commit_queue *commits,
378 899d86c2 2018-05-10 stsp struct got_object_id *first_id, struct got_object_id *last_id,
379 899d86c2 2018-05-10 stsp int limit, struct got_repository *repo)
381 899d86c2 2018-05-10 stsp const struct got_error *err = NULL;
382 899d86c2 2018-05-10 stsp struct got_object *first_obj = NULL, *last_obj = NULL;
383 899d86c2 2018-05-10 stsp struct got_commit_object *commit = NULL;
384 899d86c2 2018-05-10 stsp struct got_object_id *id = NULL;
385 899d86c2 2018-05-10 stsp struct commit_queue_entry *entry, *old_head_entry;
387 899d86c2 2018-05-10 stsp *ncommits = 0;
389 899d86c2 2018-05-10 stsp err = got_object_open(&first_obj, repo, first_id);
392 899d86c2 2018-05-10 stsp if (got_object_get_type(first_obj) != GOT_OBJ_TYPE_COMMIT) {
393 899d86c2 2018-05-10 stsp err = got_error(GOT_ERR_OBJ_TYPE);
396 899d86c2 2018-05-10 stsp err = got_object_open(&last_obj, repo, last_id);
399 899d86c2 2018-05-10 stsp if (got_object_get_type(last_obj) != GOT_OBJ_TYPE_COMMIT) {
400 899d86c2 2018-05-10 stsp err = got_error(GOT_ERR_OBJ_TYPE);
404 899d86c2 2018-05-10 stsp err = got_object_commit_open(&commit, repo, first_obj);
408 899d86c2 2018-05-10 stsp id = got_object_id_dup(first_id);
409 899d86c2 2018-05-10 stsp if (id == NULL) {
410 899d86c2 2018-05-10 stsp err = got_error_from_errno();
414 899d86c2 2018-05-10 stsp entry = alloc_commit_queue_entry(commit, id);
415 899d86c2 2018-05-10 stsp if (entry == NULL)
416 899d86c2 2018-05-10 stsp return got_error_from_errno();
418 899d86c2 2018-05-10 stsp old_head_entry = TAILQ_FIRST(commits);
419 899d86c2 2018-05-10 stsp if (old_head_entry)
420 899d86c2 2018-05-10 stsp TAILQ_INSERT_BEFORE(old_head_entry, entry, entry);
422 899d86c2 2018-05-10 stsp TAILQ_INSERT_HEAD(commits, entry, entry);
424 899d86c2 2018-05-10 stsp *ncommits = 1;
427 899d86c2 2018-05-10 stsp * Fetch parent commits.
428 899d86c2 2018-05-10 stsp * XXX If first and last commit aren't ancestrally related this loop
429 899d86c2 2018-05-10 stsp * we will keep iterating until a root commit is encountered.
431 899d86c2 2018-05-10 stsp while (1) {
432 899d86c2 2018-05-10 stsp struct commit_queue_entry *pentry;
434 899d86c2 2018-05-10 stsp err = fetch_parent_commit(&pentry, entry, repo);
437 899d86c2 2018-05-10 stsp if (pentry == NULL)
441 899d86c2 2018-05-10 stsp * Fill up to old HEAD commit if commit queue was not empty.
442 899d86c2 2018-05-10 stsp * We must not leave a gap in history.
444 899d86c2 2018-05-10 stsp if (old_head_entry &&
445 899d86c2 2018-05-10 stsp got_object_id_cmp(pentry->id, old_head_entry->id) == 0)
448 899d86c2 2018-05-10 stsp TAILQ_INSERT_AFTER(commits, entry, pentry, entry);
449 899d86c2 2018-05-10 stsp (*ncommits)++;
450 899d86c2 2018-05-10 stsp if (*ncommits >= limit)
453 899d86c2 2018-05-10 stsp /* Fill up to last requested commit if queue was empty. */
454 899d86c2 2018-05-10 stsp if (old_head_entry == NULL &&
455 899d86c2 2018-05-10 stsp got_object_id_cmp(pentry->id, last_id) == 0)
458 899d86c2 2018-05-10 stsp entry = pentry;
462 899d86c2 2018-05-10 stsp if (first_obj)
463 899d86c2 2018-05-10 stsp got_object_close(first_obj);
464 899d86c2 2018-05-10 stsp if (last_obj)
465 899d86c2 2018-05-10 stsp got_object_close(last_obj);
466 899d86c2 2018-05-10 stsp return err;
469 899d86c2 2018-05-10 stsp static const struct got_error *
470 899d86c2 2018-05-10 stsp fetch_commits(struct commit_queue_entry **start_entry,
471 899d86c2 2018-05-10 stsp struct got_object_id *start_id, struct commit_queue *commits,
472 899d86c2 2018-05-10 stsp int limit, struct got_repository *repo)
474 99db9666 2018-05-07 stsp const struct got_error *err;
475 99db9666 2018-05-07 stsp struct commit_queue_entry *entry;
476 99db9666 2018-05-07 stsp int ncommits = 0;
477 899d86c2 2018-05-10 stsp struct got_object_id *head_id = NULL;
479 899d86c2 2018-05-10 stsp *start_entry = NULL;
481 899d86c2 2018-05-10 stsp err = get_head_commit_id(&head_id, repo);
483 99db9666 2018-05-07 stsp return err;
485 899d86c2 2018-05-10 stsp /* Prepend HEAD commit and all ancestors up to start commit. */
486 899d86c2 2018-05-10 stsp err = prepend_commits(&ncommits, commits, head_id, start_id, limit,
489 99db9666 2018-05-07 stsp return err;
491 899d86c2 2018-05-10 stsp if (got_object_id_cmp(head_id, start_id) == 0)
492 899d86c2 2018-05-10 stsp *start_entry = TAILQ_FIRST(commits);
494 899d86c2 2018-05-10 stsp *start_entry = TAILQ_LAST(commits, commit_queue);
496 899d86c2 2018-05-10 stsp if (ncommits >= limit)
497 899d86c2 2018-05-10 stsp return NULL;
499 899d86c2 2018-05-10 stsp /* Append more commits from start commit up to the requested limit. */
500 899d86c2 2018-05-10 stsp entry = TAILQ_LAST(commits, commit_queue);
501 899d86c2 2018-05-10 stsp while (entry && ncommits < limit) {
502 899d86c2 2018-05-10 stsp struct commit_queue_entry *pentry;
504 899d86c2 2018-05-10 stsp err = fetch_parent_commit(&pentry, entry, repo);
507 899d86c2 2018-05-10 stsp if (pentry)
508 899d86c2 2018-05-10 stsp TAILQ_INSERT_TAIL(commits, pentry, entry);
509 899d86c2 2018-05-10 stsp entry = pentry;
510 0553a4e3 2018-04-30 stsp ncommits++;
514 899d86c2 2018-05-10 stsp *start_entry = NULL;
515 0553a4e3 2018-04-30 stsp return err;
518 0553a4e3 2018-04-30 stsp static const struct got_error *
519 cd0acaa7 2018-05-20 stsp draw_commits(struct commit_queue_entry **last, struct commit_queue_entry **selected,
520 cd0acaa7 2018-05-20 stsp struct commit_queue_entry *first, int selected_idx, int limit)
522 0553a4e3 2018-04-30 stsp const struct got_error *err = NULL;
523 0553a4e3 2018-04-30 stsp struct commit_queue_entry *entry;
524 0553a4e3 2018-04-30 stsp int ncommits = 0;
526 9c2de6ef 2018-05-20 stsp werase(tog_log_view.window);
528 899d86c2 2018-05-10 stsp entry = first;
529 899d86c2 2018-05-10 stsp *last = first;
530 899d86c2 2018-05-10 stsp while (entry) {
531 899d86c2 2018-05-10 stsp if (ncommits == limit)
533 cd0acaa7 2018-05-20 stsp if (ncommits == selected_idx) {
534 0553a4e3 2018-04-30 stsp wstandout(tog_log_view.window);
535 cd0acaa7 2018-05-20 stsp *selected = entry;
537 0553a4e3 2018-04-30 stsp err = draw_commit(entry->commit, entry->id);
538 cd0acaa7 2018-05-20 stsp if (ncommits == selected_idx)
539 0553a4e3 2018-04-30 stsp wstandend(tog_log_view.window);
542 0553a4e3 2018-04-30 stsp ncommits++;
543 899d86c2 2018-05-10 stsp *last = entry;
544 899d86c2 2018-05-10 stsp entry = TAILQ_NEXT(entry, entry);
547 80ddbec8 2018-04-29 stsp update_panels();
548 80ddbec8 2018-04-29 stsp doupdate();
550 80ddbec8 2018-04-29 stsp return err;
553 07b55e75 2018-05-10 stsp static void
554 16482c3b 2018-05-20 stsp scroll_up(struct commit_queue_entry **first_displayed_entry, int maxscroll,
555 07b55e75 2018-05-10 stsp struct commit_queue *commits)
557 07b55e75 2018-05-10 stsp struct commit_queue_entry *entry;
558 07b55e75 2018-05-10 stsp int nscrolled = 0;
560 07b55e75 2018-05-10 stsp entry = TAILQ_FIRST(commits);
561 07b55e75 2018-05-10 stsp if (*first_displayed_entry == entry)
564 07b55e75 2018-05-10 stsp entry = *first_displayed_entry;
565 16482c3b 2018-05-20 stsp while (entry && nscrolled < maxscroll) {
566 07b55e75 2018-05-10 stsp entry = TAILQ_PREV(entry, commit_queue, entry);
567 07b55e75 2018-05-10 stsp if (entry) {
568 07b55e75 2018-05-10 stsp *first_displayed_entry = entry;
569 07b55e75 2018-05-10 stsp nscrolled++;
574 aa075928 2018-05-10 stsp static const struct got_error *
575 16482c3b 2018-05-20 stsp scroll_down(struct commit_queue_entry **first_displayed_entry, int maxscroll,
576 aa075928 2018-05-10 stsp struct commit_queue_entry *last_displayed_entry,
577 aa075928 2018-05-10 stsp struct commit_queue *commits, struct got_repository *repo)
579 aa075928 2018-05-10 stsp const struct got_error *err = NULL;
580 dd0a52c1 2018-05-20 stsp struct commit_queue_entry *pentry;
581 aa075928 2018-05-10 stsp int nscrolled = 0;
584 dd0a52c1 2018-05-20 stsp pentry = TAILQ_NEXT(last_displayed_entry, entry);
585 aa075928 2018-05-10 stsp if (pentry == NULL) {
586 dd0a52c1 2018-05-20 stsp err = fetch_parent_commit(&pentry,
587 dd0a52c1 2018-05-20 stsp last_displayed_entry, repo);
588 9a6bf2a5 2018-05-20 stsp if (err || pentry == NULL)
590 aa075928 2018-05-10 stsp TAILQ_INSERT_TAIL(commits, pentry, entry);
592 dd0a52c1 2018-05-20 stsp last_displayed_entry = pentry;
594 dd0a52c1 2018-05-20 stsp pentry = TAILQ_NEXT(*first_displayed_entry, entry);
595 dd0a52c1 2018-05-20 stsp if (pentry == NULL)
597 aa075928 2018-05-10 stsp *first_displayed_entry = pentry;
598 16482c3b 2018-05-20 stsp } while (++nscrolled < maxscroll);
600 dd0a52c1 2018-05-20 stsp return err;
604 4a7f7875 2018-05-10 stsp num_parents(struct commit_queue_entry *entry)
606 4a7f7875 2018-05-10 stsp int nparents = 0;
608 4a7f7875 2018-05-10 stsp while (entry) {
609 4a7f7875 2018-05-10 stsp entry = TAILQ_NEXT(entry, entry);
610 4a7f7875 2018-05-10 stsp nparents++;
613 4a7f7875 2018-05-10 stsp return nparents;
616 cd0acaa7 2018-05-20 stsp static const struct got_error *
617 cd0acaa7 2018-05-20 stsp show_commit(struct commit_queue_entry *entry, struct got_repository *repo)
619 cd0acaa7 2018-05-20 stsp const struct got_error *err;
620 350efba7 2018-05-20 stsp struct commit_queue_entry *pentry;
621 cd0acaa7 2018-05-20 stsp struct got_object *obj1 = NULL, *obj2 = NULL;
623 cd0acaa7 2018-05-20 stsp err = got_object_open(&obj2, repo, entry->id);
625 cd0acaa7 2018-05-20 stsp return err;
627 350efba7 2018-05-20 stsp pentry = TAILQ_NEXT(entry, entry);
628 350efba7 2018-05-20 stsp if (pentry == NULL) {
629 350efba7 2018-05-20 stsp err = fetch_parent_commit(&pentry, entry, repo);
631 350efba7 2018-05-20 stsp return err;
633 350efba7 2018-05-20 stsp if (pentry) {
634 350efba7 2018-05-20 stsp err = got_object_open(&obj1, repo, pentry->id);
639 cd0acaa7 2018-05-20 stsp err = show_diff_view(obj1, obj2, repo);
642 cd0acaa7 2018-05-20 stsp got_object_close(obj1);
644 cd0acaa7 2018-05-20 stsp got_object_close(obj2);
645 cd0acaa7 2018-05-20 stsp return err;
648 80ddbec8 2018-04-29 stsp static const struct got_error *
649 80ddbec8 2018-04-29 stsp show_log_view(struct got_object_id *start_id, struct got_repository *repo)
651 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
652 c4972b91 2018-05-07 stsp struct got_object_id *id;
653 4a7f7875 2018-05-10 stsp int ch, done = 0, selected = 0, nparents;
654 0553a4e3 2018-04-30 stsp struct commit_queue commits;
655 899d86c2 2018-05-10 stsp struct commit_queue_entry *first_displayed_entry = NULL;
656 899d86c2 2018-05-10 stsp struct commit_queue_entry *last_displayed_entry = NULL;
657 cd0acaa7 2018-05-20 stsp struct commit_queue_entry *selected_entry = NULL;
659 c4972b91 2018-05-07 stsp id = got_object_id_dup(start_id);
660 c4972b91 2018-05-07 stsp if (id == NULL)
661 c4972b91 2018-05-07 stsp return got_error_from_errno();
663 80ddbec8 2018-04-29 stsp if (tog_log_view.window == NULL) {
664 80ddbec8 2018-04-29 stsp tog_log_view.window = newwin(0, 0, 0, 0);
665 80ddbec8 2018-04-29 stsp if (tog_log_view.window == NULL)
666 80ddbec8 2018-04-29 stsp return got_error_from_errno();
667 00a838fa 2018-04-29 stsp keypad(tog_log_view.window, TRUE);
669 80ddbec8 2018-04-29 stsp if (tog_log_view.panel == NULL) {
670 80ddbec8 2018-04-29 stsp tog_log_view.panel = new_panel(tog_log_view.window);
671 80ddbec8 2018-04-29 stsp if (tog_log_view.panel == NULL)
672 80ddbec8 2018-04-29 stsp return got_error_from_errno();
674 cd0acaa7 2018-05-20 stsp show_panel(tog_log_view.panel);
676 899d86c2 2018-05-10 stsp TAILQ_INIT(&commits);
677 899d86c2 2018-05-10 stsp err = fetch_commits(&first_displayed_entry, id, &commits, LINES, repo);
680 899d86c2 2018-05-10 stsp while (!done) {
681 cd0acaa7 2018-05-20 stsp err = draw_commits(&last_displayed_entry, &selected_entry,
682 cd0acaa7 2018-05-20 stsp first_displayed_entry, selected, LINES);
686 80ddbec8 2018-04-29 stsp nodelay(stdscr, FALSE);
687 80ddbec8 2018-04-29 stsp ch = wgetch(tog_log_view.window);
688 f7182337 2018-05-20 stsp nodelay(stdscr, TRUE);
689 80ddbec8 2018-04-29 stsp switch (ch) {
691 31120ada 2018-04-30 stsp if (errno) {
692 31120ada 2018-04-30 stsp err = got_error_from_errno();
700 80ddbec8 2018-04-29 stsp case KEY_UP:
701 80ddbec8 2018-04-29 stsp if (selected > 0)
702 80ddbec8 2018-04-29 stsp selected--;
703 8ec9698d 2018-05-10 stsp if (selected > 0)
705 07b55e75 2018-05-10 stsp scroll_up(&first_displayed_entry, 1, &commits);
707 48531068 2018-05-10 stsp case KEY_PPAGE:
708 dfc1d240 2018-05-10 stsp if (TAILQ_FIRST(&commits) ==
709 dfc1d240 2018-05-10 stsp first_displayed_entry) {
710 dfc1d240 2018-05-10 stsp selected = 0;
713 48531068 2018-05-10 stsp scroll_up(&first_displayed_entry, LINES,
717 80ddbec8 2018-04-29 stsp case KEY_DOWN:
718 80ee4603 2018-05-10 stsp nparents = num_parents(first_displayed_entry);
719 06abe2cd 2018-05-10 stsp if (selected < LINES - 1 &&
720 15c91275 2018-05-20 stsp selected < nparents - 1) {
721 15c91275 2018-05-20 stsp selected++;
724 15c91275 2018-05-20 stsp err = scroll_down(&first_displayed_entry, 1,
725 15c91275 2018-05-20 stsp last_displayed_entry, &commits, repo);
729 4a7f7875 2018-05-10 stsp case KEY_NPAGE:
730 e50ee4f1 2018-05-10 stsp err = scroll_down(&first_displayed_entry, LINES,
731 e50ee4f1 2018-05-10 stsp last_displayed_entry, &commits, repo);
734 dd0a52c1 2018-05-20 stsp if (last_displayed_entry->commit->nparents > 0)
736 dd0a52c1 2018-05-20 stsp /* can't scroll any further; move cursor down */
737 4a7f7875 2018-05-10 stsp nparents = num_parents(first_displayed_entry);
738 dd0a52c1 2018-05-20 stsp if (selected < LINES - 1 ||
739 dd0a52c1 2018-05-20 stsp selected < nparents - 1)
740 dd0a52c1 2018-05-20 stsp selected = MIN(LINES - 1, nparents - 1);
742 d6df9be4 2018-04-30 stsp case KEY_RESIZE:
743 31120ada 2018-04-30 stsp if (selected > LINES)
744 31120ada 2018-04-30 stsp selected = LINES - 1;
746 cd0acaa7 2018-05-20 stsp case KEY_ENTER:
748 cd0acaa7 2018-05-20 stsp err = show_commit(selected_entry, repo);
751 cd0acaa7 2018-05-20 stsp show_panel(tog_log_view.panel);
758 0553a4e3 2018-04-30 stsp free_commits(&commits);
759 80ddbec8 2018-04-29 stsp return err;
762 4ed7e80c 2018-05-20 stsp static const struct got_error *
763 9f7d7167 2018-04-29 stsp cmd_log(int argc, char *argv[])
765 80ddbec8 2018-04-29 stsp const struct got_error *error;
766 80ddbec8 2018-04-29 stsp struct got_repository *repo;
767 899d86c2 2018-05-10 stsp struct got_object_id *start_id = NULL;
768 80ddbec8 2018-04-29 stsp char *repo_path = NULL;
769 80ddbec8 2018-04-29 stsp char *start_commit = NULL;
772 80ddbec8 2018-04-29 stsp #ifndef PROFILE
773 80ddbec8 2018-04-29 stsp if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
774 80ddbec8 2018-04-29 stsp err(1, "pledge");
777 80ddbec8 2018-04-29 stsp while ((ch = getopt(argc, argv, "c:")) != -1) {
778 80ddbec8 2018-04-29 stsp switch (ch) {
780 80ddbec8 2018-04-29 stsp start_commit = optarg;
784 80ddbec8 2018-04-29 stsp /* NOTREACHED */
788 80ddbec8 2018-04-29 stsp argc -= optind;
789 80ddbec8 2018-04-29 stsp argv += optind;
791 80ddbec8 2018-04-29 stsp if (argc == 0) {
792 80ddbec8 2018-04-29 stsp repo_path = getcwd(NULL, 0);
793 80ddbec8 2018-04-29 stsp if (repo_path == NULL)
794 80ddbec8 2018-04-29 stsp return got_error_from_errno();
795 80ddbec8 2018-04-29 stsp } else if (argc == 1) {
796 80ddbec8 2018-04-29 stsp repo_path = realpath(argv[0], NULL);
797 80ddbec8 2018-04-29 stsp if (repo_path == NULL)
798 80ddbec8 2018-04-29 stsp return got_error_from_errno();
800 80ddbec8 2018-04-29 stsp usage_log();
802 80ddbec8 2018-04-29 stsp error = got_repo_open(&repo, repo_path);
803 80ddbec8 2018-04-29 stsp free(repo_path);
804 80ddbec8 2018-04-29 stsp if (error != NULL)
805 80ddbec8 2018-04-29 stsp return error;
807 80ddbec8 2018-04-29 stsp if (start_commit == NULL) {
808 899d86c2 2018-05-10 stsp error = get_head_commit_id(&start_id, repo);
809 80ddbec8 2018-04-29 stsp if (error != NULL)
810 80ddbec8 2018-04-29 stsp return error;
812 80ddbec8 2018-04-29 stsp struct got_object *obj;
813 80ddbec8 2018-04-29 stsp error = got_object_open_by_id_str(&obj, repo, start_commit);
814 80ddbec8 2018-04-29 stsp if (error == NULL) {
815 899d86c2 2018-05-10 stsp start_id = got_object_get_id(obj);
816 899d86c2 2018-05-10 stsp if (start_id == NULL)
817 80ddbec8 2018-04-29 stsp error = got_error_from_errno();
820 80ddbec8 2018-04-29 stsp if (error != NULL)
821 80ddbec8 2018-04-29 stsp return error;
822 899d86c2 2018-05-10 stsp error = show_log_view(start_id, repo);
823 899d86c2 2018-05-10 stsp free(start_id);
824 80ddbec8 2018-04-29 stsp got_repo_close(repo);
825 80ddbec8 2018-04-29 stsp return error;
828 4ed7e80c 2018-05-20 stsp __dead static void
829 9f7d7167 2018-04-29 stsp usage_diff(void)
832 9f7d7167 2018-04-29 stsp fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
833 9f7d7167 2018-04-29 stsp getprogname());
837 b304db33 2018-05-20 stsp static char *
838 b304db33 2018-05-20 stsp parse_next_line(FILE *f, size_t *len)
840 b304db33 2018-05-20 stsp char *line;
841 b304db33 2018-05-20 stsp size_t linelen;
842 b304db33 2018-05-20 stsp size_t lineno;
843 b304db33 2018-05-20 stsp const char delim[3] = { '\0', '\0', '\0'};
845 b304db33 2018-05-20 stsp line = fparseln(f, &linelen, &lineno, delim, 0);
847 b304db33 2018-05-20 stsp *len = linelen;
848 b304db33 2018-05-20 stsp return line;
851 4ed7e80c 2018-05-20 stsp static const struct got_error *
852 26ed57b2 2018-05-19 stsp draw_diff(FILE *f, int *first_displayed_line, int *last_displayed_line,
853 26ed57b2 2018-05-19 stsp int *eof, int max_lines)
855 61e69b96 2018-05-20 stsp const struct got_error *err;
856 26ed57b2 2018-05-19 stsp int nlines = 0, nprinted = 0;
857 b304db33 2018-05-20 stsp char *line;
858 b304db33 2018-05-20 stsp size_t len;
859 61e69b96 2018-05-20 stsp wchar_t *wline;
862 9c2de6ef 2018-05-20 stsp werase(tog_diff_view.window);
865 26ed57b2 2018-05-19 stsp while (nprinted < max_lines) {
866 b304db33 2018-05-20 stsp line = parse_next_line(f, &len);
867 26ed57b2 2018-05-19 stsp if (line == NULL) {
871 26ed57b2 2018-05-19 stsp if (++nlines < *first_displayed_line) {
872 26ed57b2 2018-05-19 stsp free(line);
876 bb737323 2018-05-20 stsp err = format_line(&wline, NULL, line, COLS);
878 61e69b96 2018-05-20 stsp free(line);
879 61e69b96 2018-05-20 stsp return err;
881 61e69b96 2018-05-20 stsp waddwstr(tog_diff_view.window, wline);
882 26ed57b2 2018-05-19 stsp waddch(tog_diff_view.window, '\n');
883 26ed57b2 2018-05-19 stsp if (++nprinted == 1)
884 26ed57b2 2018-05-19 stsp *first_displayed_line = nlines;
885 26ed57b2 2018-05-19 stsp free(line);
887 26ed57b2 2018-05-19 stsp *last_displayed_line = nlines;
889 26ed57b2 2018-05-19 stsp update_panels();
890 26ed57b2 2018-05-19 stsp doupdate();
892 26ed57b2 2018-05-19 stsp return NULL;
895 4ed7e80c 2018-05-20 stsp static const struct got_error *
896 26ed57b2 2018-05-19 stsp show_diff_view(struct got_object *obj1, struct got_object *obj2,
897 26ed57b2 2018-05-19 stsp struct got_repository *repo)
899 26ed57b2 2018-05-19 stsp const struct got_error *err;
901 26ed57b2 2018-05-19 stsp int ch, done = 0, first_displayed_line = 1, last_displayed_line = LINES;
902 b304db33 2018-05-20 stsp int eof, i;
904 cd0acaa7 2018-05-20 stsp if (obj1 != NULL && obj2 != NULL &&
905 cd0acaa7 2018-05-20 stsp got_object_get_type(obj1) != got_object_get_type(obj2))
906 26ed57b2 2018-05-19 stsp return got_error(GOT_ERR_OBJ_TYPE);
908 511a516b 2018-05-19 stsp f = got_opentemp();
909 26ed57b2 2018-05-19 stsp if (f == NULL)
910 26ed57b2 2018-05-19 stsp return got_error_from_errno();
912 cd0acaa7 2018-05-20 stsp switch (got_object_get_type(obj1 ? obj1 : obj2)) {
913 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_BLOB:
914 11528a82 2018-05-19 stsp err = got_diff_objects_as_blobs(obj1, obj2, repo, f);
916 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_TREE:
917 11528a82 2018-05-19 stsp err = got_diff_objects_as_trees(obj1, obj2, repo, f);
919 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_COMMIT:
920 11528a82 2018-05-19 stsp err = got_diff_objects_as_commits(obj1, obj2, repo, f);
923 26ed57b2 2018-05-19 stsp return got_error(GOT_ERR_OBJ_TYPE);
928 26ed57b2 2018-05-19 stsp if (tog_diff_view.window == NULL) {
929 26ed57b2 2018-05-19 stsp tog_diff_view.window = newwin(0, 0, 0, 0);
930 26ed57b2 2018-05-19 stsp if (tog_diff_view.window == NULL)
931 26ed57b2 2018-05-19 stsp return got_error_from_errno();
932 26ed57b2 2018-05-19 stsp keypad(tog_diff_view.window, TRUE);
934 26ed57b2 2018-05-19 stsp if (tog_diff_view.panel == NULL) {
935 26ed57b2 2018-05-19 stsp tog_diff_view.panel = new_panel(tog_diff_view.window);
936 26ed57b2 2018-05-19 stsp if (tog_diff_view.panel == NULL)
937 26ed57b2 2018-05-19 stsp return got_error_from_errno();
939 26ed57b2 2018-05-19 stsp show_panel(tog_diff_view.panel);
941 26ed57b2 2018-05-19 stsp while (!done) {
942 26ed57b2 2018-05-19 stsp err = draw_diff(f, &first_displayed_line, &last_displayed_line,
943 26ed57b2 2018-05-19 stsp &eof, LINES);
946 26ed57b2 2018-05-19 stsp nodelay(stdscr, FALSE);
947 26ed57b2 2018-05-19 stsp ch = wgetch(tog_diff_view.window);
948 f7182337 2018-05-20 stsp nodelay(stdscr, TRUE);
949 26ed57b2 2018-05-19 stsp switch (ch) {
954 26ed57b2 2018-05-19 stsp case KEY_UP:
955 925e6f23 2018-05-20 stsp case KEY_BACKSPACE:
956 26ed57b2 2018-05-19 stsp if (first_displayed_line > 1)
957 b304db33 2018-05-20 stsp first_displayed_line--;
959 b304db33 2018-05-20 stsp case KEY_PPAGE:
961 d56caa55 2018-05-20 stsp while (i++ < LINES - 1 &&
962 d56caa55 2018-05-20 stsp first_displayed_line > 1)
963 26ed57b2 2018-05-19 stsp first_displayed_line--;
966 26ed57b2 2018-05-19 stsp case KEY_DOWN:
967 925e6f23 2018-05-20 stsp case KEY_ENTER:
970 26ed57b2 2018-05-19 stsp first_displayed_line++;
972 b304db33 2018-05-20 stsp case KEY_NPAGE:
975 b304db33 2018-05-20 stsp while (!eof && i++ < LINES - 1) {
976 b304db33 2018-05-20 stsp char *line = parse_next_line(f, NULL);
977 b304db33 2018-05-20 stsp first_displayed_line++;
978 b304db33 2018-05-20 stsp if (line == NULL)
987 26ed57b2 2018-05-19 stsp return err;
990 4ed7e80c 2018-05-20 stsp static const struct got_error *
991 9f7d7167 2018-04-29 stsp cmd_diff(int argc, char *argv[])
993 26ed57b2 2018-05-19 stsp const struct got_error *error = NULL;
994 26ed57b2 2018-05-19 stsp struct got_repository *repo = NULL;
995 26ed57b2 2018-05-19 stsp struct got_object *obj1 = NULL, *obj2 = NULL;
996 26ed57b2 2018-05-19 stsp char *repo_path = NULL;
997 26ed57b2 2018-05-19 stsp char *obj_id_str1 = NULL, *obj_id_str2 = NULL;
1000 26ed57b2 2018-05-19 stsp #ifndef PROFILE
1001 26ed57b2 2018-05-19 stsp if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
1002 26ed57b2 2018-05-19 stsp err(1, "pledge");
1005 26ed57b2 2018-05-19 stsp while ((ch = getopt(argc, argv, "")) != -1) {
1006 26ed57b2 2018-05-19 stsp switch (ch) {
1009 26ed57b2 2018-05-19 stsp /* NOTREACHED */
1013 26ed57b2 2018-05-19 stsp argc -= optind;
1014 26ed57b2 2018-05-19 stsp argv += optind;
1016 26ed57b2 2018-05-19 stsp if (argc == 0) {
1017 26ed57b2 2018-05-19 stsp usage_diff(); /* TODO show local worktree changes */
1018 26ed57b2 2018-05-19 stsp } else if (argc == 2) {
1019 26ed57b2 2018-05-19 stsp repo_path = getcwd(NULL, 0);
1020 26ed57b2 2018-05-19 stsp if (repo_path == NULL)
1021 26ed57b2 2018-05-19 stsp return got_error_from_errno();
1022 26ed57b2 2018-05-19 stsp obj_id_str1 = argv[0];
1023 26ed57b2 2018-05-19 stsp obj_id_str2 = argv[1];
1024 26ed57b2 2018-05-19 stsp } else if (argc == 3) {
1025 26ed57b2 2018-05-19 stsp repo_path = realpath(argv[0], NULL);
1026 26ed57b2 2018-05-19 stsp if (repo_path == NULL)
1027 26ed57b2 2018-05-19 stsp return got_error_from_errno();
1028 26ed57b2 2018-05-19 stsp obj_id_str1 = argv[1];
1029 26ed57b2 2018-05-19 stsp obj_id_str2 = argv[2];
1031 26ed57b2 2018-05-19 stsp usage_diff();
1033 26ed57b2 2018-05-19 stsp error = got_repo_open(&repo, repo_path);
1034 26ed57b2 2018-05-19 stsp free(repo_path);
1035 26ed57b2 2018-05-19 stsp if (error)
1036 26ed57b2 2018-05-19 stsp goto done;
1038 26ed57b2 2018-05-19 stsp error = got_object_open_by_id_str(&obj1, repo, obj_id_str1);
1039 26ed57b2 2018-05-19 stsp if (error)
1040 26ed57b2 2018-05-19 stsp goto done;
1042 26ed57b2 2018-05-19 stsp error = got_object_open_by_id_str(&obj2, repo, obj_id_str2);
1043 26ed57b2 2018-05-19 stsp if (error)
1044 26ed57b2 2018-05-19 stsp goto done;
1046 26ed57b2 2018-05-19 stsp error = show_diff_view(obj1, obj2, repo);
1048 26ed57b2 2018-05-19 stsp got_repo_close(repo);
1050 26ed57b2 2018-05-19 stsp got_object_close(obj1);
1052 26ed57b2 2018-05-19 stsp got_object_close(obj2);
1053 26ed57b2 2018-05-19 stsp return error;
1056 4ed7e80c 2018-05-20 stsp __dead static void
1057 9f7d7167 2018-04-29 stsp usage_blame(void)
1060 9f7d7167 2018-04-29 stsp fprintf(stderr, "usage: %s blame [repository-path] blob-object\n",
1061 9f7d7167 2018-04-29 stsp getprogname());
1065 4ed7e80c 2018-05-20 stsp static const struct got_error *
1066 9f7d7167 2018-04-29 stsp cmd_blame(int argc, char *argv[])
1068 9f7d7167 2018-04-29 stsp return got_error(GOT_ERR_NOT_IMPL);
1071 5c5136c5 2018-05-20 stsp static void
1072 9f7d7167 2018-04-29 stsp init_curses(void)
1074 9f7d7167 2018-04-29 stsp initscr();
1078 9f7d7167 2018-04-29 stsp intrflush(stdscr, FALSE);
1079 9f7d7167 2018-04-29 stsp keypad(stdscr, TRUE);
1080 1f475ad8 2018-05-10 stsp curs_set(0);
1083 4ed7e80c 2018-05-20 stsp __dead static void
1084 9f7d7167 2018-04-29 stsp usage(void)
1088 c2301be8 2018-04-30 stsp fprintf(stderr, "usage: %s [-h] [command] [arg ...]\n\n"
1089 9f7d7167 2018-04-29 stsp "Available commands:\n", getprogname());
1090 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
1091 9f7d7167 2018-04-29 stsp struct tog_cmd *cmd = &tog_commands[i];
1092 c2301be8 2018-04-30 stsp fprintf(stderr, " %s: %s\n", cmd->name, cmd->descr);
1097 c2301be8 2018-04-30 stsp static char **
1098 c2301be8 2018-04-30 stsp make_argv(const char *arg0, const char *arg1)
1100 c2301be8 2018-04-30 stsp char **argv;
1101 c2301be8 2018-04-30 stsp int argc = (arg1 == NULL ? 1 : 2);
1103 c2301be8 2018-04-30 stsp argv = calloc(argc, sizeof(char *));
1104 c2301be8 2018-04-30 stsp if (argv == NULL)
1105 c2301be8 2018-04-30 stsp err(1, "calloc");
1106 c2301be8 2018-04-30 stsp argv[0] = strdup(arg0);
1107 c2301be8 2018-04-30 stsp if (argv[0] == NULL)
1108 c2301be8 2018-04-30 stsp err(1, "calloc");
1109 c2301be8 2018-04-30 stsp if (arg1) {
1110 c2301be8 2018-04-30 stsp argv[1] = strdup(arg1);
1111 c2301be8 2018-04-30 stsp if (argv[1] == NULL)
1112 c2301be8 2018-04-30 stsp err(1, "calloc");
1115 c2301be8 2018-04-30 stsp return argv;
1119 9f7d7167 2018-04-29 stsp main(int argc, char *argv[])
1121 9f7d7167 2018-04-29 stsp const struct got_error *error = NULL;
1122 9f7d7167 2018-04-29 stsp struct tog_cmd *cmd = NULL;
1123 9f7d7167 2018-04-29 stsp int ch, hflag = 0;
1124 c2301be8 2018-04-30 stsp char **cmd_argv = NULL;
1126 9f7d7167 2018-04-29 stsp setlocale(LC_ALL, "");
1128 9f7d7167 2018-04-29 stsp while ((ch = getopt(argc, argv, "h")) != -1) {
1129 9f7d7167 2018-04-29 stsp switch (ch) {
1131 9f7d7167 2018-04-29 stsp hflag = 1;
1135 9f7d7167 2018-04-29 stsp /* NOTREACHED */
1139 9f7d7167 2018-04-29 stsp argc -= optind;
1140 9f7d7167 2018-04-29 stsp argv += optind;
1141 9f7d7167 2018-04-29 stsp optind = 0;
1142 c2301be8 2018-04-30 stsp optreset = 1;
1144 c2301be8 2018-04-30 stsp if (argc == 0) {
1145 c2301be8 2018-04-30 stsp /* Build an argument vector which runs a default command. */
1146 9f7d7167 2018-04-29 stsp cmd = &tog_commands[0];
1147 c2301be8 2018-04-30 stsp cmd_argv = make_argv(cmd->name, NULL);
1152 c2301be8 2018-04-30 stsp /* Did the user specific a command? */
1153 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
1154 c2301be8 2018-04-30 stsp if (strncmp(tog_commands[i].name, argv[0],
1155 9f7d7167 2018-04-29 stsp strlen(argv[0])) == 0) {
1156 9f7d7167 2018-04-29 stsp cmd = &tog_commands[i];
1157 9f7d7167 2018-04-29 stsp if (hflag)
1158 9f7d7167 2018-04-29 stsp tog_commands[i].cmd_usage();
1162 9f7d7167 2018-04-29 stsp if (cmd == NULL) {
1163 c2301be8 2018-04-30 stsp /* Did the user specify a repository? */
1164 c2301be8 2018-04-30 stsp char *repo_path = realpath(argv[0], NULL);
1165 c2301be8 2018-04-30 stsp if (repo_path) {
1166 c2301be8 2018-04-30 stsp struct got_repository *repo;
1167 c2301be8 2018-04-30 stsp error = got_repo_open(&repo, repo_path);
1168 c2301be8 2018-04-30 stsp if (error == NULL)
1169 c2301be8 2018-04-30 stsp got_repo_close(repo);
1171 ad7de8d9 2018-04-30 stsp error = got_error_from_errno();
1172 c2301be8 2018-04-30 stsp if (error) {
1173 ad7de8d9 2018-04-30 stsp fprintf(stderr, "%s: '%s' is neither a known "
1174 ad7de8d9 2018-04-30 stsp "command nor a path to a repository\n",
1175 c2301be8 2018-04-30 stsp getprogname(), argv[0]);
1176 ad7de8d9 2018-04-30 stsp free(repo_path);
1179 c2301be8 2018-04-30 stsp cmd = &tog_commands[0];
1180 c2301be8 2018-04-30 stsp cmd_argv = make_argv(cmd->name, repo_path);
1182 c2301be8 2018-04-30 stsp free(repo_path);
1186 5c5136c5 2018-05-20 stsp init_curses();
1188 c2301be8 2018-04-30 stsp error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
1189 9f7d7167 2018-04-29 stsp if (error)
1190 9f7d7167 2018-04-29 stsp goto done;
1193 c2301be8 2018-04-30 stsp free(cmd_argv);
1194 9f7d7167 2018-04-29 stsp if (error)
1195 9f7d7167 2018-04-29 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);