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 80ddbec8 2018-04-29 stsp static const struct got_error *
96 0553a4e3 2018-04-30 stsp draw_commit(struct got_commit_object *commit, struct got_object_id *id)
98 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
99 80ddbec8 2018-04-29 stsp char *logmsg0 = NULL, *logmsg = NULL;
100 6d9fbc00 2018-04-29 stsp char *author0 = NULL, *author = NULL;
101 6d9fbc00 2018-04-29 stsp char *newline, *smallerthan;
102 80ddbec8 2018-04-29 stsp char *line = NULL;
103 6d9fbc00 2018-04-29 stsp char *id_str = NULL;
104 881b2d3e 2018-04-30 stsp const size_t id_display_len = 8;
105 881b2d3e 2018-04-30 stsp const size_t author_display_len = 16;
106 881b2d3e 2018-04-30 stsp size_t id_len, author_len, logmsg_len, avail;
107 881b2d3e 2018-04-30 stsp int i, col;
109 80ddbec8 2018-04-29 stsp err = got_object_id_str(&id_str, id);
111 80ddbec8 2018-04-29 stsp return err;
112 881b2d3e 2018-04-30 stsp id_len = strlen(id_str);
114 80ddbec8 2018-04-29 stsp logmsg0 = strdup(commit->logmsg);
115 6d9fbc00 2018-04-29 stsp if (logmsg0 == NULL) {
116 6d9fbc00 2018-04-29 stsp err = got_error_from_errno();
119 80ddbec8 2018-04-29 stsp logmsg = logmsg0;
120 80ddbec8 2018-04-29 stsp while (*logmsg == '\n')
122 80ddbec8 2018-04-29 stsp newline = strchr(logmsg, '\n');
123 6d9fbc00 2018-04-29 stsp if (newline)
124 80ddbec8 2018-04-29 stsp *newline = '\0';
125 881b2d3e 2018-04-30 stsp logmsg_len = strlen(logmsg);
127 6d9fbc00 2018-04-29 stsp author0 = strdup(commit->author);
128 6d9fbc00 2018-04-29 stsp if (author0 == NULL) {
129 80ddbec8 2018-04-29 stsp err = got_error_from_errno();
132 6d9fbc00 2018-04-29 stsp author = author0;
133 6d9fbc00 2018-04-29 stsp smallerthan = strchr(author, '<');
134 6d9fbc00 2018-04-29 stsp if (smallerthan)
135 6d9fbc00 2018-04-29 stsp *smallerthan = '\0';
137 6d9fbc00 2018-04-29 stsp char *at = strchr(author, '@');
139 6d9fbc00 2018-04-29 stsp *at = '\0';
141 881b2d3e 2018-04-30 stsp author_len = strlen(author);
143 881b2d3e 2018-04-30 stsp avail = COLS - 1;
144 881b2d3e 2018-04-30 stsp line = calloc(avail + 1, sizeof(*line));
145 881b2d3e 2018-04-30 stsp if (line == NULL) {
146 6d9fbc00 2018-04-29 stsp err = got_error_from_errno();
151 881b2d3e 2018-04-30 stsp for (i = 0; i < MIN(id_display_len, id_len); i++) {
152 881b2d3e 2018-04-30 stsp if (col >= avail)
154 881b2d3e 2018-04-30 stsp line[col++] = id_str[i];
156 881b2d3e 2018-04-30 stsp while (i < id_display_len) {
157 881b2d3e 2018-04-30 stsp if (col >= avail)
159 881b2d3e 2018-04-30 stsp line[col++] = ' ';
162 881b2d3e 2018-04-30 stsp if (col >= avail)
164 881b2d3e 2018-04-30 stsp line[col++] = ' ';
165 881b2d3e 2018-04-30 stsp for (i = 0; i < MIN(author_display_len, author_len); i++) {
166 881b2d3e 2018-04-30 stsp if (col >= avail)
168 881b2d3e 2018-04-30 stsp line[col++] = author[i];
170 881b2d3e 2018-04-30 stsp while (i < author_display_len) {
171 881b2d3e 2018-04-30 stsp if (col >= avail)
173 881b2d3e 2018-04-30 stsp line[col++] = ' ';
176 881b2d3e 2018-04-30 stsp if (col >= avail)
178 881b2d3e 2018-04-30 stsp line[col++] = ' ';
180 881b2d3e 2018-04-30 stsp while (col < avail && *logmsg)
181 881b2d3e 2018-04-30 stsp line[col++] = *logmsg++;
182 881b2d3e 2018-04-30 stsp while (col < avail)
183 881b2d3e 2018-04-30 stsp line[col++] = ' ';
185 881b2d3e 2018-04-30 stsp waddstr(tog_log_view.window, line);
186 80ddbec8 2018-04-29 stsp waddch(tog_log_view.window, '\n');
188 80ddbec8 2018-04-29 stsp free(logmsg0);
189 6d9fbc00 2018-04-29 stsp free(author0);
190 80ddbec8 2018-04-29 stsp free(line);
191 6d9fbc00 2018-04-29 stsp free(id_str);
192 80ddbec8 2018-04-29 stsp return err;
195 80ddbec8 2018-04-29 stsp struct commit_queue_entry {
196 80ddbec8 2018-04-29 stsp TAILQ_ENTRY(commit_queue_entry) entry;
197 80ddbec8 2018-04-29 stsp struct got_object_id *id;
198 80ddbec8 2018-04-29 stsp struct got_commit_object *commit;
200 0553a4e3 2018-04-30 stsp TAILQ_HEAD(commit_queue, commit_queue_entry);
202 899d86c2 2018-05-10 stsp static struct commit_queue_entry *
203 899d86c2 2018-05-10 stsp alloc_commit_queue_entry(struct got_commit_object *commit,
204 899d86c2 2018-05-10 stsp struct got_object_id *id)
206 80ddbec8 2018-04-29 stsp struct commit_queue_entry *entry;
208 80ddbec8 2018-04-29 stsp entry = calloc(1, sizeof(*entry));
209 80ddbec8 2018-04-29 stsp if (entry == NULL)
210 899d86c2 2018-05-10 stsp return NULL;
212 899d86c2 2018-05-10 stsp entry->id = id;
213 99db9666 2018-05-07 stsp entry->commit = commit;
214 899d86c2 2018-05-10 stsp return entry;
217 99db9666 2018-05-07 stsp static void
218 99db9666 2018-05-07 stsp pop_commit(struct commit_queue *commits)
220 99db9666 2018-05-07 stsp struct commit_queue_entry *entry;
222 99db9666 2018-05-07 stsp entry = TAILQ_FIRST(commits);
223 99db9666 2018-05-07 stsp TAILQ_REMOVE(commits, entry, entry);
224 99db9666 2018-05-07 stsp got_object_commit_close(entry->commit);
225 99db9666 2018-05-07 stsp free(entry->id);
226 99db9666 2018-05-07 stsp free(entry);
229 99db9666 2018-05-07 stsp static void
230 99db9666 2018-05-07 stsp free_commits(struct commit_queue *commits)
232 99db9666 2018-05-07 stsp while (!TAILQ_EMPTY(commits))
233 99db9666 2018-05-07 stsp pop_commit(commits);
236 c4972b91 2018-05-07 stsp static const struct got_error *
237 899d86c2 2018-05-10 stsp fetch_parent_commit(struct commit_queue_entry **pentry,
238 899d86c2 2018-05-10 stsp struct commit_queue_entry *entry, struct got_repository *repo)
240 899d86c2 2018-05-10 stsp const struct got_error *err = NULL;
241 c4972b91 2018-05-07 stsp struct got_object *obj = NULL;
242 899d86c2 2018-05-10 stsp struct got_commit_object *commit;
243 899d86c2 2018-05-10 stsp struct got_object_id *id;
244 c4972b91 2018-05-07 stsp struct got_parent_id *pid;
246 899d86c2 2018-05-10 stsp *pentry = NULL;
248 c4972b91 2018-05-07 stsp /* Follow the first parent (TODO: handle merge commits). */
249 c4972b91 2018-05-07 stsp pid = SIMPLEQ_FIRST(&entry->commit->parent_ids);
250 c4972b91 2018-05-07 stsp if (pid == NULL)
251 c4972b91 2018-05-07 stsp return NULL;
252 c4972b91 2018-05-07 stsp err = got_object_open(&obj, repo, pid->id);
254 c4972b91 2018-05-07 stsp return err;
255 c4972b91 2018-05-07 stsp if (got_object_get_type(obj) != GOT_OBJ_TYPE_COMMIT) {
256 c4972b91 2018-05-07 stsp err = got_error(GOT_ERR_OBJ_TYPE);
257 c4972b91 2018-05-07 stsp got_object_close(obj);
258 c4972b91 2018-05-07 stsp return err;
261 899d86c2 2018-05-10 stsp err = got_object_commit_open(&commit, repo, obj);
262 c4972b91 2018-05-07 stsp got_object_close(obj);
264 c4972b91 2018-05-07 stsp return err;
266 899d86c2 2018-05-10 stsp id = got_object_id_dup(pid->id);
267 899d86c2 2018-05-10 stsp if (id == NULL) {
268 c4972b91 2018-05-07 stsp err = got_error_from_errno();
269 899d86c2 2018-05-10 stsp got_object_commit_close(commit);
270 899d86c2 2018-05-10 stsp return err;;
273 899d86c2 2018-05-10 stsp *pentry = alloc_commit_queue_entry(commit, id);
274 899d86c2 2018-05-10 stsp if (*pentry == NULL) {
275 c4972b91 2018-05-07 stsp err = got_error_from_errno();
276 899d86c2 2018-05-10 stsp got_object_commit_close(commit);
279 899d86c2 2018-05-10 stsp return err;;
282 899d86c2 2018-05-10 stsp static const struct got_error *
283 899d86c2 2018-05-10 stsp get_head_commit_id(struct got_object_id **head_id, struct got_repository *repo)
285 899d86c2 2018-05-10 stsp const struct got_error *err = NULL;
286 899d86c2 2018-05-10 stsp struct got_reference *head_ref;
288 899d86c2 2018-05-10 stsp *head_id = NULL;
290 899d86c2 2018-05-10 stsp err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
292 899d86c2 2018-05-10 stsp return err;
294 899d86c2 2018-05-10 stsp err = got_ref_resolve(head_id, repo, head_ref);
295 899d86c2 2018-05-10 stsp got_ref_close(head_ref);
297 899d86c2 2018-05-10 stsp *head_id = NULL;
298 899d86c2 2018-05-10 stsp return err;
301 c4972b91 2018-05-07 stsp return NULL;
304 99db9666 2018-05-07 stsp static const struct got_error *
305 899d86c2 2018-05-10 stsp prepend_commits(int *ncommits, struct commit_queue *commits,
306 899d86c2 2018-05-10 stsp struct got_object_id *first_id, struct got_object_id *last_id,
307 899d86c2 2018-05-10 stsp int limit, struct got_repository *repo)
309 899d86c2 2018-05-10 stsp const struct got_error *err = NULL;
310 899d86c2 2018-05-10 stsp struct got_object *first_obj = NULL, *last_obj = NULL;
311 899d86c2 2018-05-10 stsp struct got_commit_object *commit = NULL;
312 899d86c2 2018-05-10 stsp struct got_object_id *id = NULL;
313 899d86c2 2018-05-10 stsp struct commit_queue_entry *entry, *old_head_entry;
315 899d86c2 2018-05-10 stsp *ncommits = 0;
317 899d86c2 2018-05-10 stsp err = got_object_open(&first_obj, repo, first_id);
320 899d86c2 2018-05-10 stsp if (got_object_get_type(first_obj) != GOT_OBJ_TYPE_COMMIT) {
321 899d86c2 2018-05-10 stsp err = got_error(GOT_ERR_OBJ_TYPE);
324 899d86c2 2018-05-10 stsp err = got_object_open(&last_obj, repo, last_id);
327 899d86c2 2018-05-10 stsp if (got_object_get_type(last_obj) != GOT_OBJ_TYPE_COMMIT) {
328 899d86c2 2018-05-10 stsp err = got_error(GOT_ERR_OBJ_TYPE);
332 899d86c2 2018-05-10 stsp err = got_object_commit_open(&commit, repo, first_obj);
336 899d86c2 2018-05-10 stsp id = got_object_id_dup(first_id);
337 899d86c2 2018-05-10 stsp if (id == NULL) {
338 899d86c2 2018-05-10 stsp err = got_error_from_errno();
342 899d86c2 2018-05-10 stsp entry = alloc_commit_queue_entry(commit, id);
343 899d86c2 2018-05-10 stsp if (entry == NULL)
344 899d86c2 2018-05-10 stsp return got_error_from_errno();
346 899d86c2 2018-05-10 stsp old_head_entry = TAILQ_FIRST(commits);
347 899d86c2 2018-05-10 stsp if (old_head_entry)
348 899d86c2 2018-05-10 stsp TAILQ_INSERT_BEFORE(old_head_entry, entry, entry);
350 899d86c2 2018-05-10 stsp TAILQ_INSERT_HEAD(commits, entry, entry);
352 899d86c2 2018-05-10 stsp *ncommits = 1;
355 899d86c2 2018-05-10 stsp * Fetch parent commits.
356 899d86c2 2018-05-10 stsp * XXX If first and last commit aren't ancestrally related this loop
357 899d86c2 2018-05-10 stsp * we will keep iterating until a root commit is encountered.
359 899d86c2 2018-05-10 stsp while (1) {
360 899d86c2 2018-05-10 stsp struct commit_queue_entry *pentry;
362 899d86c2 2018-05-10 stsp err = fetch_parent_commit(&pentry, entry, repo);
365 899d86c2 2018-05-10 stsp if (pentry == NULL)
369 899d86c2 2018-05-10 stsp * Fill up to old HEAD commit if commit queue was not empty.
370 899d86c2 2018-05-10 stsp * We must not leave a gap in history.
372 899d86c2 2018-05-10 stsp if (old_head_entry &&
373 899d86c2 2018-05-10 stsp got_object_id_cmp(pentry->id, old_head_entry->id) == 0)
376 899d86c2 2018-05-10 stsp TAILQ_INSERT_AFTER(commits, entry, pentry, entry);
377 899d86c2 2018-05-10 stsp (*ncommits)++;
378 899d86c2 2018-05-10 stsp if (*ncommits >= limit)
381 899d86c2 2018-05-10 stsp /* Fill up to last requested commit if queue was empty. */
382 899d86c2 2018-05-10 stsp if (old_head_entry == NULL &&
383 899d86c2 2018-05-10 stsp got_object_id_cmp(pentry->id, last_id) == 0)
386 899d86c2 2018-05-10 stsp entry = pentry;
390 899d86c2 2018-05-10 stsp if (first_obj)
391 899d86c2 2018-05-10 stsp got_object_close(first_obj);
392 899d86c2 2018-05-10 stsp if (last_obj)
393 899d86c2 2018-05-10 stsp got_object_close(last_obj);
394 899d86c2 2018-05-10 stsp return err;
397 899d86c2 2018-05-10 stsp static const struct got_error *
398 899d86c2 2018-05-10 stsp fetch_commits(struct commit_queue_entry **start_entry,
399 899d86c2 2018-05-10 stsp struct got_object_id *start_id, struct commit_queue *commits,
400 899d86c2 2018-05-10 stsp int limit, struct got_repository *repo)
402 99db9666 2018-05-07 stsp const struct got_error *err;
403 99db9666 2018-05-07 stsp struct commit_queue_entry *entry;
404 99db9666 2018-05-07 stsp int ncommits = 0;
405 899d86c2 2018-05-10 stsp struct got_object_id *head_id = NULL;
407 899d86c2 2018-05-10 stsp *start_entry = NULL;
409 899d86c2 2018-05-10 stsp err = get_head_commit_id(&head_id, repo);
411 99db9666 2018-05-07 stsp return err;
413 899d86c2 2018-05-10 stsp /* Prepend HEAD commit and all ancestors up to start commit. */
414 899d86c2 2018-05-10 stsp err = prepend_commits(&ncommits, commits, head_id, start_id, limit,
417 99db9666 2018-05-07 stsp return err;
419 899d86c2 2018-05-10 stsp if (got_object_id_cmp(head_id, start_id) == 0)
420 899d86c2 2018-05-10 stsp *start_entry = TAILQ_FIRST(commits);
422 899d86c2 2018-05-10 stsp *start_entry = TAILQ_LAST(commits, commit_queue);
424 899d86c2 2018-05-10 stsp if (ncommits >= limit)
425 899d86c2 2018-05-10 stsp return NULL;
427 899d86c2 2018-05-10 stsp /* Append more commits from start commit up to the requested limit. */
428 899d86c2 2018-05-10 stsp entry = TAILQ_LAST(commits, commit_queue);
429 899d86c2 2018-05-10 stsp while (entry && ncommits < limit) {
430 899d86c2 2018-05-10 stsp struct commit_queue_entry *pentry;
432 899d86c2 2018-05-10 stsp err = fetch_parent_commit(&pentry, entry, repo);
435 899d86c2 2018-05-10 stsp if (pentry)
436 899d86c2 2018-05-10 stsp TAILQ_INSERT_TAIL(commits, pentry, entry);
437 899d86c2 2018-05-10 stsp entry = pentry;
438 0553a4e3 2018-04-30 stsp ncommits++;
442 899d86c2 2018-05-10 stsp *start_entry = NULL;
443 0553a4e3 2018-04-30 stsp return err;
446 0553a4e3 2018-04-30 stsp static const struct got_error *
447 cd0acaa7 2018-05-20 stsp draw_commits(struct commit_queue_entry **last, struct commit_queue_entry **selected,
448 cd0acaa7 2018-05-20 stsp struct commit_queue_entry *first, int selected_idx, int limit)
450 0553a4e3 2018-04-30 stsp const struct got_error *err = NULL;
451 0553a4e3 2018-04-30 stsp struct commit_queue_entry *entry;
452 0553a4e3 2018-04-30 stsp int ncommits = 0;
454 9c2de6ef 2018-05-20 stsp werase(tog_log_view.window);
456 899d86c2 2018-05-10 stsp entry = first;
457 899d86c2 2018-05-10 stsp *last = first;
458 899d86c2 2018-05-10 stsp while (entry) {
459 899d86c2 2018-05-10 stsp if (ncommits == limit)
461 cd0acaa7 2018-05-20 stsp if (ncommits == selected_idx) {
462 0553a4e3 2018-04-30 stsp wstandout(tog_log_view.window);
463 cd0acaa7 2018-05-20 stsp *selected = entry;
465 0553a4e3 2018-04-30 stsp err = draw_commit(entry->commit, entry->id);
466 cd0acaa7 2018-05-20 stsp if (ncommits == selected_idx)
467 0553a4e3 2018-04-30 stsp wstandend(tog_log_view.window);
470 0553a4e3 2018-04-30 stsp ncommits++;
471 899d86c2 2018-05-10 stsp *last = entry;
472 899d86c2 2018-05-10 stsp entry = TAILQ_NEXT(entry, entry);
475 80ddbec8 2018-04-29 stsp update_panels();
476 80ddbec8 2018-04-29 stsp doupdate();
478 80ddbec8 2018-04-29 stsp return err;
481 07b55e75 2018-05-10 stsp static void
482 16482c3b 2018-05-20 stsp scroll_up(struct commit_queue_entry **first_displayed_entry, int maxscroll,
483 07b55e75 2018-05-10 stsp struct commit_queue *commits)
485 07b55e75 2018-05-10 stsp struct commit_queue_entry *entry;
486 07b55e75 2018-05-10 stsp int nscrolled = 0;
488 07b55e75 2018-05-10 stsp entry = TAILQ_FIRST(commits);
489 07b55e75 2018-05-10 stsp if (*first_displayed_entry == entry)
492 07b55e75 2018-05-10 stsp entry = *first_displayed_entry;
493 16482c3b 2018-05-20 stsp while (entry && nscrolled < maxscroll) {
494 07b55e75 2018-05-10 stsp entry = TAILQ_PREV(entry, commit_queue, entry);
495 07b55e75 2018-05-10 stsp if (entry) {
496 07b55e75 2018-05-10 stsp *first_displayed_entry = entry;
497 07b55e75 2018-05-10 stsp nscrolled++;
502 aa075928 2018-05-10 stsp static const struct got_error *
503 16482c3b 2018-05-20 stsp scroll_down(struct commit_queue_entry **first_displayed_entry, int maxscroll,
504 aa075928 2018-05-10 stsp struct commit_queue_entry *last_displayed_entry,
505 aa075928 2018-05-10 stsp struct commit_queue *commits, struct got_repository *repo)
507 aa075928 2018-05-10 stsp const struct got_error *err = NULL;
508 dd0a52c1 2018-05-20 stsp struct commit_queue_entry *pentry;
509 aa075928 2018-05-10 stsp int nscrolled = 0;
512 dd0a52c1 2018-05-20 stsp pentry = TAILQ_NEXT(last_displayed_entry, entry);
513 aa075928 2018-05-10 stsp if (pentry == NULL) {
514 dd0a52c1 2018-05-20 stsp err = fetch_parent_commit(&pentry,
515 dd0a52c1 2018-05-20 stsp last_displayed_entry, repo);
516 9a6bf2a5 2018-05-20 stsp if (err || pentry == NULL)
518 aa075928 2018-05-10 stsp TAILQ_INSERT_TAIL(commits, pentry, entry);
520 dd0a52c1 2018-05-20 stsp last_displayed_entry = pentry;
522 dd0a52c1 2018-05-20 stsp pentry = TAILQ_NEXT(*first_displayed_entry, entry);
523 dd0a52c1 2018-05-20 stsp if (pentry == NULL)
525 aa075928 2018-05-10 stsp *first_displayed_entry = pentry;
526 16482c3b 2018-05-20 stsp } while (++nscrolled < maxscroll);
528 dd0a52c1 2018-05-20 stsp return err;
532 4a7f7875 2018-05-10 stsp num_parents(struct commit_queue_entry *entry)
534 4a7f7875 2018-05-10 stsp int nparents = 0;
536 4a7f7875 2018-05-10 stsp while (entry) {
537 4a7f7875 2018-05-10 stsp entry = TAILQ_NEXT(entry, entry);
538 4a7f7875 2018-05-10 stsp nparents++;
541 4a7f7875 2018-05-10 stsp return nparents;
544 cd0acaa7 2018-05-20 stsp static const struct got_error *
545 cd0acaa7 2018-05-20 stsp show_commit(struct commit_queue_entry *entry, struct got_repository *repo)
547 cd0acaa7 2018-05-20 stsp const struct got_error *err;
548 350efba7 2018-05-20 stsp struct commit_queue_entry *pentry;
549 cd0acaa7 2018-05-20 stsp struct got_object *obj1 = NULL, *obj2 = NULL;
551 cd0acaa7 2018-05-20 stsp err = got_object_open(&obj2, repo, entry->id);
553 cd0acaa7 2018-05-20 stsp return err;
555 350efba7 2018-05-20 stsp pentry = TAILQ_NEXT(entry, entry);
556 350efba7 2018-05-20 stsp if (pentry == NULL) {
557 350efba7 2018-05-20 stsp err = fetch_parent_commit(&pentry, entry, repo);
559 350efba7 2018-05-20 stsp return err;
561 350efba7 2018-05-20 stsp if (pentry) {
562 350efba7 2018-05-20 stsp err = got_object_open(&obj1, repo, pentry->id);
567 cd0acaa7 2018-05-20 stsp err = show_diff_view(obj1, obj2, repo);
570 cd0acaa7 2018-05-20 stsp got_object_close(obj1);
572 cd0acaa7 2018-05-20 stsp got_object_close(obj2);
573 cd0acaa7 2018-05-20 stsp return err;
576 80ddbec8 2018-04-29 stsp static const struct got_error *
577 80ddbec8 2018-04-29 stsp show_log_view(struct got_object_id *start_id, struct got_repository *repo)
579 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
580 c4972b91 2018-05-07 stsp struct got_object_id *id;
581 4a7f7875 2018-05-10 stsp int ch, done = 0, selected = 0, nparents;
582 0553a4e3 2018-04-30 stsp struct commit_queue commits;
583 899d86c2 2018-05-10 stsp struct commit_queue_entry *first_displayed_entry = NULL;
584 899d86c2 2018-05-10 stsp struct commit_queue_entry *last_displayed_entry = NULL;
585 cd0acaa7 2018-05-20 stsp struct commit_queue_entry *selected_entry = NULL;
587 c4972b91 2018-05-07 stsp id = got_object_id_dup(start_id);
588 c4972b91 2018-05-07 stsp if (id == NULL)
589 c4972b91 2018-05-07 stsp return got_error_from_errno();
591 80ddbec8 2018-04-29 stsp if (tog_log_view.window == NULL) {
592 80ddbec8 2018-04-29 stsp tog_log_view.window = newwin(0, 0, 0, 0);
593 80ddbec8 2018-04-29 stsp if (tog_log_view.window == NULL)
594 80ddbec8 2018-04-29 stsp return got_error_from_errno();
595 00a838fa 2018-04-29 stsp keypad(tog_log_view.window, TRUE);
597 80ddbec8 2018-04-29 stsp if (tog_log_view.panel == NULL) {
598 80ddbec8 2018-04-29 stsp tog_log_view.panel = new_panel(tog_log_view.window);
599 80ddbec8 2018-04-29 stsp if (tog_log_view.panel == NULL)
600 80ddbec8 2018-04-29 stsp return got_error_from_errno();
602 cd0acaa7 2018-05-20 stsp show_panel(tog_log_view.panel);
604 899d86c2 2018-05-10 stsp TAILQ_INIT(&commits);
605 899d86c2 2018-05-10 stsp err = fetch_commits(&first_displayed_entry, id, &commits, LINES, repo);
608 899d86c2 2018-05-10 stsp while (!done) {
609 cd0acaa7 2018-05-20 stsp err = draw_commits(&last_displayed_entry, &selected_entry,
610 cd0acaa7 2018-05-20 stsp first_displayed_entry, selected, LINES);
614 80ddbec8 2018-04-29 stsp nodelay(stdscr, FALSE);
615 80ddbec8 2018-04-29 stsp ch = wgetch(tog_log_view.window);
616 f7182337 2018-05-20 stsp nodelay(stdscr, TRUE);
617 80ddbec8 2018-04-29 stsp switch (ch) {
619 31120ada 2018-04-30 stsp if (errno) {
620 31120ada 2018-04-30 stsp err = got_error_from_errno();
628 80ddbec8 2018-04-29 stsp case KEY_UP:
629 80ddbec8 2018-04-29 stsp if (selected > 0)
630 80ddbec8 2018-04-29 stsp selected--;
631 8ec9698d 2018-05-10 stsp if (selected > 0)
633 07b55e75 2018-05-10 stsp scroll_up(&first_displayed_entry, 1, &commits);
635 48531068 2018-05-10 stsp case KEY_PPAGE:
636 dfc1d240 2018-05-10 stsp if (TAILQ_FIRST(&commits) ==
637 dfc1d240 2018-05-10 stsp first_displayed_entry) {
638 dfc1d240 2018-05-10 stsp selected = 0;
641 48531068 2018-05-10 stsp scroll_up(&first_displayed_entry, LINES,
645 80ddbec8 2018-04-29 stsp case KEY_DOWN:
646 80ee4603 2018-05-10 stsp nparents = num_parents(first_displayed_entry);
647 06abe2cd 2018-05-10 stsp if (selected < LINES - 1 &&
648 15c91275 2018-05-20 stsp selected < nparents - 1) {
649 15c91275 2018-05-20 stsp selected++;
652 15c91275 2018-05-20 stsp err = scroll_down(&first_displayed_entry, 1,
653 15c91275 2018-05-20 stsp last_displayed_entry, &commits, repo);
657 4a7f7875 2018-05-10 stsp case KEY_NPAGE:
658 e50ee4f1 2018-05-10 stsp err = scroll_down(&first_displayed_entry, LINES,
659 e50ee4f1 2018-05-10 stsp last_displayed_entry, &commits, repo);
662 dd0a52c1 2018-05-20 stsp if (last_displayed_entry->commit->nparents > 0)
664 dd0a52c1 2018-05-20 stsp /* can't scroll any further; move cursor down */
665 4a7f7875 2018-05-10 stsp nparents = num_parents(first_displayed_entry);
666 dd0a52c1 2018-05-20 stsp if (selected < LINES - 1 ||
667 dd0a52c1 2018-05-20 stsp selected < nparents - 1)
668 dd0a52c1 2018-05-20 stsp selected = MIN(LINES - 1, nparents - 1);
670 d6df9be4 2018-04-30 stsp case KEY_RESIZE:
671 31120ada 2018-04-30 stsp if (selected > LINES)
672 31120ada 2018-04-30 stsp selected = LINES - 1;
674 cd0acaa7 2018-05-20 stsp case KEY_ENTER:
676 cd0acaa7 2018-05-20 stsp err = show_commit(selected_entry, repo);
679 cd0acaa7 2018-05-20 stsp show_panel(tog_log_view.panel);
686 0553a4e3 2018-04-30 stsp free_commits(&commits);
687 80ddbec8 2018-04-29 stsp return err;
690 4ed7e80c 2018-05-20 stsp static const struct got_error *
691 9f7d7167 2018-04-29 stsp cmd_log(int argc, char *argv[])
693 80ddbec8 2018-04-29 stsp const struct got_error *error;
694 80ddbec8 2018-04-29 stsp struct got_repository *repo;
695 899d86c2 2018-05-10 stsp struct got_object_id *start_id = NULL;
696 80ddbec8 2018-04-29 stsp char *repo_path = NULL;
697 80ddbec8 2018-04-29 stsp char *start_commit = NULL;
700 80ddbec8 2018-04-29 stsp #ifndef PROFILE
701 80ddbec8 2018-04-29 stsp if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
702 80ddbec8 2018-04-29 stsp err(1, "pledge");
705 80ddbec8 2018-04-29 stsp while ((ch = getopt(argc, argv, "c:")) != -1) {
706 80ddbec8 2018-04-29 stsp switch (ch) {
708 80ddbec8 2018-04-29 stsp start_commit = optarg;
712 80ddbec8 2018-04-29 stsp /* NOTREACHED */
716 80ddbec8 2018-04-29 stsp argc -= optind;
717 80ddbec8 2018-04-29 stsp argv += optind;
719 80ddbec8 2018-04-29 stsp if (argc == 0) {
720 80ddbec8 2018-04-29 stsp repo_path = getcwd(NULL, 0);
721 80ddbec8 2018-04-29 stsp if (repo_path == NULL)
722 80ddbec8 2018-04-29 stsp return got_error_from_errno();
723 80ddbec8 2018-04-29 stsp } else if (argc == 1) {
724 80ddbec8 2018-04-29 stsp repo_path = realpath(argv[0], NULL);
725 80ddbec8 2018-04-29 stsp if (repo_path == NULL)
726 80ddbec8 2018-04-29 stsp return got_error_from_errno();
728 80ddbec8 2018-04-29 stsp usage_log();
730 80ddbec8 2018-04-29 stsp error = got_repo_open(&repo, repo_path);
731 80ddbec8 2018-04-29 stsp free(repo_path);
732 80ddbec8 2018-04-29 stsp if (error != NULL)
733 80ddbec8 2018-04-29 stsp return error;
735 80ddbec8 2018-04-29 stsp if (start_commit == NULL) {
736 899d86c2 2018-05-10 stsp error = get_head_commit_id(&start_id, repo);
737 80ddbec8 2018-04-29 stsp if (error != NULL)
738 80ddbec8 2018-04-29 stsp return error;
740 80ddbec8 2018-04-29 stsp struct got_object *obj;
741 80ddbec8 2018-04-29 stsp error = got_object_open_by_id_str(&obj, repo, start_commit);
742 80ddbec8 2018-04-29 stsp if (error == NULL) {
743 899d86c2 2018-05-10 stsp start_id = got_object_get_id(obj);
744 899d86c2 2018-05-10 stsp if (start_id == NULL)
745 80ddbec8 2018-04-29 stsp error = got_error_from_errno();
748 80ddbec8 2018-04-29 stsp if (error != NULL)
749 80ddbec8 2018-04-29 stsp return error;
750 899d86c2 2018-05-10 stsp error = show_log_view(start_id, repo);
751 899d86c2 2018-05-10 stsp free(start_id);
752 80ddbec8 2018-04-29 stsp got_repo_close(repo);
753 80ddbec8 2018-04-29 stsp return error;
756 4ed7e80c 2018-05-20 stsp __dead static void
757 9f7d7167 2018-04-29 stsp usage_diff(void)
760 9f7d7167 2018-04-29 stsp fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
761 9f7d7167 2018-04-29 stsp getprogname());
765 b304db33 2018-05-20 stsp static char *
766 b304db33 2018-05-20 stsp parse_next_line(FILE *f, size_t *len)
768 b304db33 2018-05-20 stsp char *line;
769 b304db33 2018-05-20 stsp size_t linelen;
770 b304db33 2018-05-20 stsp size_t lineno;
771 b304db33 2018-05-20 stsp const char delim[3] = { '\0', '\0', '\0'};
773 b304db33 2018-05-20 stsp line = fparseln(f, &linelen, &lineno, delim, 0);
775 b304db33 2018-05-20 stsp *len = linelen;
776 b304db33 2018-05-20 stsp return line;
779 61e69b96 2018-05-20 stsp /* Format a line for display, ensuring that it won't overflow COLS columns. */
780 4ed7e80c 2018-05-20 stsp static const struct got_error *
781 61e69b96 2018-05-20 stsp format_line(wchar_t **wlinep, char *line)
783 61e69b96 2018-05-20 stsp const struct got_error *err = NULL;
784 61e69b96 2018-05-20 stsp int cols = 0;
785 61e69b96 2018-05-20 stsp wchar_t *wline = NULL;
786 61e69b96 2018-05-20 stsp size_t wlen;
789 61e69b96 2018-05-20 stsp *wlinep = NULL;
791 61e69b96 2018-05-20 stsp wlen = mbstowcs(NULL, line, 0);
792 61e69b96 2018-05-20 stsp if (wlen == (size_t)-1)
793 61e69b96 2018-05-20 stsp return got_error_from_errno();
795 61e69b96 2018-05-20 stsp wline = calloc(wlen + 1, sizeof(wchar_t));
796 61e69b96 2018-05-20 stsp if (wline == NULL)
797 61e69b96 2018-05-20 stsp return got_error_from_errno();
799 61e69b96 2018-05-20 stsp if (mbstowcs(wline, line, wlen) != wlen) {
800 61e69b96 2018-05-20 stsp err = got_error_from_errno();
805 61e69b96 2018-05-20 stsp while (i < wlen && cols <= COLS) {
806 61e69b96 2018-05-20 stsp int width = wcwidth(wline[i]);
807 61e69b96 2018-05-20 stsp switch (width) {
812 61e69b96 2018-05-20 stsp cols += width;
815 61e69b96 2018-05-20 stsp if (wline[i] == L'\t')
816 61e69b96 2018-05-20 stsp cols += TABSIZE;
819 61e69b96 2018-05-20 stsp err = got_error_from_errno();
822 61e69b96 2018-05-20 stsp if (cols <= COLS)
825 61e69b96 2018-05-20 stsp wline[i] = L'\0';
828 61e69b96 2018-05-20 stsp free(wline);
830 61e69b96 2018-05-20 stsp *wlinep = wline;
831 61e69b96 2018-05-20 stsp return err;
834 61e69b96 2018-05-20 stsp static const struct got_error *
835 26ed57b2 2018-05-19 stsp draw_diff(FILE *f, int *first_displayed_line, int *last_displayed_line,
836 26ed57b2 2018-05-19 stsp int *eof, int max_lines)
838 61e69b96 2018-05-20 stsp const struct got_error *err;
839 26ed57b2 2018-05-19 stsp int nlines = 0, nprinted = 0;
840 b304db33 2018-05-20 stsp char *line;
841 b304db33 2018-05-20 stsp size_t len;
842 61e69b96 2018-05-20 stsp wchar_t *wline;
845 9c2de6ef 2018-05-20 stsp werase(tog_diff_view.window);
848 26ed57b2 2018-05-19 stsp while (nprinted < max_lines) {
849 b304db33 2018-05-20 stsp line = parse_next_line(f, &len);
850 26ed57b2 2018-05-19 stsp if (line == NULL) {
854 26ed57b2 2018-05-19 stsp if (++nlines < *first_displayed_line) {
855 26ed57b2 2018-05-19 stsp free(line);
859 61e69b96 2018-05-20 stsp err = format_line(&wline, line);
861 61e69b96 2018-05-20 stsp free(line);
862 61e69b96 2018-05-20 stsp return err;
864 61e69b96 2018-05-20 stsp waddwstr(tog_diff_view.window, wline);
865 26ed57b2 2018-05-19 stsp waddch(tog_diff_view.window, '\n');
866 26ed57b2 2018-05-19 stsp if (++nprinted == 1)
867 26ed57b2 2018-05-19 stsp *first_displayed_line = nlines;
868 26ed57b2 2018-05-19 stsp free(line);
870 26ed57b2 2018-05-19 stsp *last_displayed_line = nlines;
872 26ed57b2 2018-05-19 stsp update_panels();
873 26ed57b2 2018-05-19 stsp doupdate();
875 26ed57b2 2018-05-19 stsp return NULL;
878 4ed7e80c 2018-05-20 stsp static const struct got_error *
879 26ed57b2 2018-05-19 stsp show_diff_view(struct got_object *obj1, struct got_object *obj2,
880 26ed57b2 2018-05-19 stsp struct got_repository *repo)
882 26ed57b2 2018-05-19 stsp const struct got_error *err;
884 26ed57b2 2018-05-19 stsp int ch, done = 0, first_displayed_line = 1, last_displayed_line = LINES;
885 b304db33 2018-05-20 stsp int eof, i;
887 cd0acaa7 2018-05-20 stsp if (obj1 != NULL && obj2 != NULL &&
888 cd0acaa7 2018-05-20 stsp got_object_get_type(obj1) != got_object_get_type(obj2))
889 26ed57b2 2018-05-19 stsp return got_error(GOT_ERR_OBJ_TYPE);
891 511a516b 2018-05-19 stsp f = got_opentemp();
892 26ed57b2 2018-05-19 stsp if (f == NULL)
893 26ed57b2 2018-05-19 stsp return got_error_from_errno();
895 cd0acaa7 2018-05-20 stsp switch (got_object_get_type(obj1 ? obj1 : obj2)) {
896 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_BLOB:
897 11528a82 2018-05-19 stsp err = got_diff_objects_as_blobs(obj1, obj2, repo, f);
899 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_TREE:
900 11528a82 2018-05-19 stsp err = got_diff_objects_as_trees(obj1, obj2, repo, f);
902 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_COMMIT:
903 11528a82 2018-05-19 stsp err = got_diff_objects_as_commits(obj1, obj2, repo, f);
906 26ed57b2 2018-05-19 stsp return got_error(GOT_ERR_OBJ_TYPE);
911 26ed57b2 2018-05-19 stsp if (tog_diff_view.window == NULL) {
912 26ed57b2 2018-05-19 stsp tog_diff_view.window = newwin(0, 0, 0, 0);
913 26ed57b2 2018-05-19 stsp if (tog_diff_view.window == NULL)
914 26ed57b2 2018-05-19 stsp return got_error_from_errno();
915 26ed57b2 2018-05-19 stsp keypad(tog_diff_view.window, TRUE);
917 26ed57b2 2018-05-19 stsp if (tog_diff_view.panel == NULL) {
918 26ed57b2 2018-05-19 stsp tog_diff_view.panel = new_panel(tog_diff_view.window);
919 26ed57b2 2018-05-19 stsp if (tog_diff_view.panel == NULL)
920 26ed57b2 2018-05-19 stsp return got_error_from_errno();
922 26ed57b2 2018-05-19 stsp show_panel(tog_diff_view.panel);
924 26ed57b2 2018-05-19 stsp while (!done) {
925 26ed57b2 2018-05-19 stsp err = draw_diff(f, &first_displayed_line, &last_displayed_line,
926 26ed57b2 2018-05-19 stsp &eof, LINES);
929 26ed57b2 2018-05-19 stsp nodelay(stdscr, FALSE);
930 26ed57b2 2018-05-19 stsp ch = wgetch(tog_diff_view.window);
931 f7182337 2018-05-20 stsp nodelay(stdscr, TRUE);
932 26ed57b2 2018-05-19 stsp switch (ch) {
937 26ed57b2 2018-05-19 stsp case KEY_UP:
938 925e6f23 2018-05-20 stsp case KEY_BACKSPACE:
939 26ed57b2 2018-05-19 stsp if (first_displayed_line > 1)
940 b304db33 2018-05-20 stsp first_displayed_line--;
942 b304db33 2018-05-20 stsp case KEY_PPAGE:
944 d56caa55 2018-05-20 stsp while (i++ < LINES - 1 &&
945 d56caa55 2018-05-20 stsp first_displayed_line > 1)
946 26ed57b2 2018-05-19 stsp first_displayed_line--;
949 26ed57b2 2018-05-19 stsp case KEY_DOWN:
950 925e6f23 2018-05-20 stsp case KEY_ENTER:
953 26ed57b2 2018-05-19 stsp first_displayed_line++;
955 b304db33 2018-05-20 stsp case KEY_NPAGE:
958 b304db33 2018-05-20 stsp while (!eof && i++ < LINES - 1) {
959 b304db33 2018-05-20 stsp char *line = parse_next_line(f, NULL);
960 b304db33 2018-05-20 stsp first_displayed_line++;
961 b304db33 2018-05-20 stsp if (line == NULL)
970 26ed57b2 2018-05-19 stsp return err;
973 4ed7e80c 2018-05-20 stsp static const struct got_error *
974 9f7d7167 2018-04-29 stsp cmd_diff(int argc, char *argv[])
976 26ed57b2 2018-05-19 stsp const struct got_error *error = NULL;
977 26ed57b2 2018-05-19 stsp struct got_repository *repo = NULL;
978 26ed57b2 2018-05-19 stsp struct got_object *obj1 = NULL, *obj2 = NULL;
979 26ed57b2 2018-05-19 stsp char *repo_path = NULL;
980 26ed57b2 2018-05-19 stsp char *obj_id_str1 = NULL, *obj_id_str2 = NULL;
983 26ed57b2 2018-05-19 stsp #ifndef PROFILE
984 26ed57b2 2018-05-19 stsp if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
985 26ed57b2 2018-05-19 stsp err(1, "pledge");
988 26ed57b2 2018-05-19 stsp while ((ch = getopt(argc, argv, "")) != -1) {
989 26ed57b2 2018-05-19 stsp switch (ch) {
992 26ed57b2 2018-05-19 stsp /* NOTREACHED */
996 26ed57b2 2018-05-19 stsp argc -= optind;
997 26ed57b2 2018-05-19 stsp argv += optind;
999 26ed57b2 2018-05-19 stsp if (argc == 0) {
1000 26ed57b2 2018-05-19 stsp usage_diff(); /* TODO show local worktree changes */
1001 26ed57b2 2018-05-19 stsp } else if (argc == 2) {
1002 26ed57b2 2018-05-19 stsp repo_path = getcwd(NULL, 0);
1003 26ed57b2 2018-05-19 stsp if (repo_path == NULL)
1004 26ed57b2 2018-05-19 stsp return got_error_from_errno();
1005 26ed57b2 2018-05-19 stsp obj_id_str1 = argv[0];
1006 26ed57b2 2018-05-19 stsp obj_id_str2 = argv[1];
1007 26ed57b2 2018-05-19 stsp } else if (argc == 3) {
1008 26ed57b2 2018-05-19 stsp repo_path = realpath(argv[0], NULL);
1009 26ed57b2 2018-05-19 stsp if (repo_path == NULL)
1010 26ed57b2 2018-05-19 stsp return got_error_from_errno();
1011 26ed57b2 2018-05-19 stsp obj_id_str1 = argv[1];
1012 26ed57b2 2018-05-19 stsp obj_id_str2 = argv[2];
1014 26ed57b2 2018-05-19 stsp usage_diff();
1016 26ed57b2 2018-05-19 stsp error = got_repo_open(&repo, repo_path);
1017 26ed57b2 2018-05-19 stsp free(repo_path);
1018 26ed57b2 2018-05-19 stsp if (error)
1019 26ed57b2 2018-05-19 stsp goto done;
1021 26ed57b2 2018-05-19 stsp error = got_object_open_by_id_str(&obj1, repo, obj_id_str1);
1022 26ed57b2 2018-05-19 stsp if (error)
1023 26ed57b2 2018-05-19 stsp goto done;
1025 26ed57b2 2018-05-19 stsp error = got_object_open_by_id_str(&obj2, repo, obj_id_str2);
1026 26ed57b2 2018-05-19 stsp if (error)
1027 26ed57b2 2018-05-19 stsp goto done;
1029 26ed57b2 2018-05-19 stsp error = show_diff_view(obj1, obj2, repo);
1031 26ed57b2 2018-05-19 stsp got_repo_close(repo);
1033 26ed57b2 2018-05-19 stsp got_object_close(obj1);
1035 26ed57b2 2018-05-19 stsp got_object_close(obj2);
1036 26ed57b2 2018-05-19 stsp return error;
1039 4ed7e80c 2018-05-20 stsp __dead static void
1040 9f7d7167 2018-04-29 stsp usage_blame(void)
1043 9f7d7167 2018-04-29 stsp fprintf(stderr, "usage: %s blame [repository-path] blob-object\n",
1044 9f7d7167 2018-04-29 stsp getprogname());
1048 4ed7e80c 2018-05-20 stsp static const struct got_error *
1049 9f7d7167 2018-04-29 stsp cmd_blame(int argc, char *argv[])
1051 9f7d7167 2018-04-29 stsp return got_error(GOT_ERR_NOT_IMPL);
1054 5c5136c5 2018-05-20 stsp static void
1055 9f7d7167 2018-04-29 stsp init_curses(void)
1057 9f7d7167 2018-04-29 stsp initscr();
1061 9f7d7167 2018-04-29 stsp intrflush(stdscr, FALSE);
1062 9f7d7167 2018-04-29 stsp keypad(stdscr, TRUE);
1063 1f475ad8 2018-05-10 stsp curs_set(0);
1066 4ed7e80c 2018-05-20 stsp __dead static void
1067 9f7d7167 2018-04-29 stsp usage(void)
1071 c2301be8 2018-04-30 stsp fprintf(stderr, "usage: %s [-h] [command] [arg ...]\n\n"
1072 9f7d7167 2018-04-29 stsp "Available commands:\n", getprogname());
1073 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
1074 9f7d7167 2018-04-29 stsp struct tog_cmd *cmd = &tog_commands[i];
1075 c2301be8 2018-04-30 stsp fprintf(stderr, " %s: %s\n", cmd->name, cmd->descr);
1080 c2301be8 2018-04-30 stsp static char **
1081 c2301be8 2018-04-30 stsp make_argv(const char *arg0, const char *arg1)
1083 c2301be8 2018-04-30 stsp char **argv;
1084 c2301be8 2018-04-30 stsp int argc = (arg1 == NULL ? 1 : 2);
1086 c2301be8 2018-04-30 stsp argv = calloc(argc, sizeof(char *));
1087 c2301be8 2018-04-30 stsp if (argv == NULL)
1088 c2301be8 2018-04-30 stsp err(1, "calloc");
1089 c2301be8 2018-04-30 stsp argv[0] = strdup(arg0);
1090 c2301be8 2018-04-30 stsp if (argv[0] == NULL)
1091 c2301be8 2018-04-30 stsp err(1, "calloc");
1092 c2301be8 2018-04-30 stsp if (arg1) {
1093 c2301be8 2018-04-30 stsp argv[1] = strdup(arg1);
1094 c2301be8 2018-04-30 stsp if (argv[1] == NULL)
1095 c2301be8 2018-04-30 stsp err(1, "calloc");
1098 c2301be8 2018-04-30 stsp return argv;
1102 9f7d7167 2018-04-29 stsp main(int argc, char *argv[])
1104 9f7d7167 2018-04-29 stsp const struct got_error *error = NULL;
1105 9f7d7167 2018-04-29 stsp struct tog_cmd *cmd = NULL;
1106 9f7d7167 2018-04-29 stsp int ch, hflag = 0;
1107 c2301be8 2018-04-30 stsp char **cmd_argv = NULL;
1109 9f7d7167 2018-04-29 stsp setlocale(LC_ALL, "");
1111 9f7d7167 2018-04-29 stsp while ((ch = getopt(argc, argv, "h")) != -1) {
1112 9f7d7167 2018-04-29 stsp switch (ch) {
1114 9f7d7167 2018-04-29 stsp hflag = 1;
1118 9f7d7167 2018-04-29 stsp /* NOTREACHED */
1122 9f7d7167 2018-04-29 stsp argc -= optind;
1123 9f7d7167 2018-04-29 stsp argv += optind;
1124 9f7d7167 2018-04-29 stsp optind = 0;
1125 c2301be8 2018-04-30 stsp optreset = 1;
1127 c2301be8 2018-04-30 stsp if (argc == 0) {
1128 c2301be8 2018-04-30 stsp /* Build an argument vector which runs a default command. */
1129 9f7d7167 2018-04-29 stsp cmd = &tog_commands[0];
1130 c2301be8 2018-04-30 stsp cmd_argv = make_argv(cmd->name, NULL);
1135 c2301be8 2018-04-30 stsp /* Did the user specific a command? */
1136 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
1137 c2301be8 2018-04-30 stsp if (strncmp(tog_commands[i].name, argv[0],
1138 9f7d7167 2018-04-29 stsp strlen(argv[0])) == 0) {
1139 9f7d7167 2018-04-29 stsp cmd = &tog_commands[i];
1140 9f7d7167 2018-04-29 stsp if (hflag)
1141 9f7d7167 2018-04-29 stsp tog_commands[i].cmd_usage();
1145 9f7d7167 2018-04-29 stsp if (cmd == NULL) {
1146 c2301be8 2018-04-30 stsp /* Did the user specify a repository? */
1147 c2301be8 2018-04-30 stsp char *repo_path = realpath(argv[0], NULL);
1148 c2301be8 2018-04-30 stsp if (repo_path) {
1149 c2301be8 2018-04-30 stsp struct got_repository *repo;
1150 c2301be8 2018-04-30 stsp error = got_repo_open(&repo, repo_path);
1151 c2301be8 2018-04-30 stsp if (error == NULL)
1152 c2301be8 2018-04-30 stsp got_repo_close(repo);
1154 ad7de8d9 2018-04-30 stsp error = got_error_from_errno();
1155 c2301be8 2018-04-30 stsp if (error) {
1156 ad7de8d9 2018-04-30 stsp fprintf(stderr, "%s: '%s' is neither a known "
1157 ad7de8d9 2018-04-30 stsp "command nor a path to a repository\n",
1158 c2301be8 2018-04-30 stsp getprogname(), argv[0]);
1159 ad7de8d9 2018-04-30 stsp free(repo_path);
1162 c2301be8 2018-04-30 stsp cmd = &tog_commands[0];
1163 c2301be8 2018-04-30 stsp cmd_argv = make_argv(cmd->name, repo_path);
1165 c2301be8 2018-04-30 stsp free(repo_path);
1169 5c5136c5 2018-05-20 stsp init_curses();
1171 c2301be8 2018-04-30 stsp error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
1172 9f7d7167 2018-04-29 stsp if (error)
1173 9f7d7167 2018-04-29 stsp goto done;
1176 c2301be8 2018-04-30 stsp free(cmd_argv);
1177 9f7d7167 2018-04-29 stsp if (error)
1178 9f7d7167 2018-04-29 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);