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 9f7d7167 2018-04-29 stsp #include <curses.h>
21 9f7d7167 2018-04-29 stsp #include <panel.h>
22 9f7d7167 2018-04-29 stsp #include <locale.h>
23 9f7d7167 2018-04-29 stsp #include <stdlib.h>
24 26ed57b2 2018-05-19 stsp #include <stdio.h>
25 9f7d7167 2018-04-29 stsp #include <getopt.h>
26 9f7d7167 2018-04-29 stsp #include <string.h>
27 9f7d7167 2018-04-29 stsp #include <err.h>
28 80ddbec8 2018-04-29 stsp #include <unistd.h>
29 26ed57b2 2018-05-19 stsp #include <util.h>
30 26ed57b2 2018-05-19 stsp #include <limits.h>
32 9f7d7167 2018-04-29 stsp #include "got_error.h"
33 80ddbec8 2018-04-29 stsp #include "got_object.h"
34 80ddbec8 2018-04-29 stsp #include "got_reference.h"
35 80ddbec8 2018-04-29 stsp #include "got_repository.h"
36 80ddbec8 2018-04-29 stsp #include "got_diff.h"
39 881b2d3e 2018-04-30 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
42 9f7d7167 2018-04-29 stsp #ifndef nitems
43 9f7d7167 2018-04-29 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
46 9f7d7167 2018-04-29 stsp enum tog_view_id {
47 9f7d7167 2018-04-29 stsp TOG_VIEW_LOG,
48 9f7d7167 2018-04-29 stsp TOG_VIEW_DIFF,
49 9f7d7167 2018-04-29 stsp TOG_VIEW_BLAME,
52 9f7d7167 2018-04-29 stsp struct tog_cmd {
53 c2301be8 2018-04-30 stsp const char *name;
54 9f7d7167 2018-04-29 stsp const struct got_error *(*cmd_main)(int, char *[]);
55 9f7d7167 2018-04-29 stsp void (*cmd_usage)(void);
56 9f7d7167 2018-04-29 stsp enum tog_view_id view;
57 c2301be8 2018-04-30 stsp const char *descr;
60 9f7d7167 2018-04-29 stsp __dead void usage(void);
61 9f7d7167 2018-04-29 stsp __dead void usage_log(void);
62 9f7d7167 2018-04-29 stsp __dead void usage_diff(void);
63 9f7d7167 2018-04-29 stsp __dead void usage_blame(void);
65 9f7d7167 2018-04-29 stsp const struct got_error* cmd_log(int, char *[]);
66 9f7d7167 2018-04-29 stsp const struct got_error* cmd_diff(int, char *[]);
67 9f7d7167 2018-04-29 stsp const struct got_error* cmd_blame(int, char *[]);
69 9f7d7167 2018-04-29 stsp struct tog_cmd tog_commands[] = {
70 9f7d7167 2018-04-29 stsp { "log", cmd_log, usage_log, TOG_VIEW_LOG,
71 9f7d7167 2018-04-29 stsp "show repository history" },
72 9f7d7167 2018-04-29 stsp { "diff", cmd_diff, usage_diff, TOG_VIEW_DIFF,
73 9f7d7167 2018-04-29 stsp "compare files and directories" },
74 9f7d7167 2018-04-29 stsp { "blame", cmd_blame, usage_blame, TOG_VIEW_BLAME,
75 9f7d7167 2018-04-29 stsp "show line-by-line file history" },
78 9f7d7167 2018-04-29 stsp /* globals */
79 9f7d7167 2018-04-29 stsp WINDOW *tog_main_win;
80 9f7d7167 2018-04-29 stsp PANEL *tog_main_panel;
81 80ddbec8 2018-04-29 stsp static struct tog_log_view {
82 80ddbec8 2018-04-29 stsp WINDOW *window;
83 80ddbec8 2018-04-29 stsp PANEL *panel;
84 80ddbec8 2018-04-29 stsp } tog_log_view;
85 26ed57b2 2018-05-19 stsp static struct tog_diff_view {
86 26ed57b2 2018-05-19 stsp WINDOW *window;
87 26ed57b2 2018-05-19 stsp PANEL *panel;
88 26ed57b2 2018-05-19 stsp } tog_diff_view;
91 26ed57b2 2018-05-19 stsp tog_opentempfd(void)
93 26ed57b2 2018-05-19 stsp char name[PATH_MAX];
96 26ed57b2 2018-05-19 stsp if (strlcpy(name, "/tmp/tog.XXXXXXXX", sizeof(name)) >= sizeof(name))
99 26ed57b2 2018-05-19 stsp fd = mkstemp(name);
100 26ed57b2 2018-05-19 stsp unlink(name);
105 26ed57b2 2018-05-19 stsp tog_opentemp(void)
110 26ed57b2 2018-05-19 stsp fd = tog_opentempfd();
111 26ed57b2 2018-05-19 stsp if (fd < 0)
112 26ed57b2 2018-05-19 stsp return NULL;
114 26ed57b2 2018-05-19 stsp f = fdopen(fd, "w+");
115 26ed57b2 2018-05-19 stsp if (f == NULL) {
117 26ed57b2 2018-05-19 stsp return NULL;
123 9f7d7167 2018-04-29 stsp __dead void
124 9f7d7167 2018-04-29 stsp usage_log(void)
127 80ddbec8 2018-04-29 stsp fprintf(stderr, "usage: %s log [-c commit] [repository-path]\n",
128 9f7d7167 2018-04-29 stsp getprogname());
132 80ddbec8 2018-04-29 stsp static const struct got_error *
133 0553a4e3 2018-04-30 stsp draw_commit(struct got_commit_object *commit, struct got_object_id *id)
135 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
136 80ddbec8 2018-04-29 stsp char *logmsg0 = NULL, *logmsg = NULL;
137 6d9fbc00 2018-04-29 stsp char *author0 = NULL, *author = NULL;
138 6d9fbc00 2018-04-29 stsp char *newline, *smallerthan;
139 80ddbec8 2018-04-29 stsp char *line = NULL;
140 6d9fbc00 2018-04-29 stsp char *id_str = NULL;
141 881b2d3e 2018-04-30 stsp const size_t id_display_len = 8;
142 881b2d3e 2018-04-30 stsp const size_t author_display_len = 16;
143 881b2d3e 2018-04-30 stsp size_t id_len, author_len, logmsg_len, avail;
144 881b2d3e 2018-04-30 stsp int i, col;
146 80ddbec8 2018-04-29 stsp err = got_object_id_str(&id_str, id);
148 80ddbec8 2018-04-29 stsp return err;
149 881b2d3e 2018-04-30 stsp id_len = strlen(id_str);
151 80ddbec8 2018-04-29 stsp logmsg0 = strdup(commit->logmsg);
152 6d9fbc00 2018-04-29 stsp if (logmsg0 == NULL) {
153 6d9fbc00 2018-04-29 stsp err = got_error_from_errno();
156 80ddbec8 2018-04-29 stsp logmsg = logmsg0;
157 80ddbec8 2018-04-29 stsp while (*logmsg == '\n')
159 80ddbec8 2018-04-29 stsp newline = strchr(logmsg, '\n');
160 6d9fbc00 2018-04-29 stsp if (newline)
161 80ddbec8 2018-04-29 stsp *newline = '\0';
162 881b2d3e 2018-04-30 stsp logmsg_len = strlen(logmsg);
164 6d9fbc00 2018-04-29 stsp author0 = strdup(commit->author);
165 6d9fbc00 2018-04-29 stsp if (author0 == NULL) {
166 80ddbec8 2018-04-29 stsp err = got_error_from_errno();
169 6d9fbc00 2018-04-29 stsp author = author0;
170 6d9fbc00 2018-04-29 stsp smallerthan = strchr(author, '<');
171 6d9fbc00 2018-04-29 stsp if (smallerthan)
172 6d9fbc00 2018-04-29 stsp *smallerthan = '\0';
174 6d9fbc00 2018-04-29 stsp char *at = strchr(author, '@');
176 6d9fbc00 2018-04-29 stsp *at = '\0';
178 881b2d3e 2018-04-30 stsp author_len = strlen(author);
180 881b2d3e 2018-04-30 stsp avail = COLS - 1;
181 881b2d3e 2018-04-30 stsp line = calloc(avail + 1, sizeof(*line));
182 881b2d3e 2018-04-30 stsp if (line == NULL) {
183 6d9fbc00 2018-04-29 stsp err = got_error_from_errno();
188 881b2d3e 2018-04-30 stsp for (i = 0; i < MIN(id_display_len, id_len); i++) {
189 881b2d3e 2018-04-30 stsp if (col >= avail)
191 881b2d3e 2018-04-30 stsp line[col++] = id_str[i];
193 881b2d3e 2018-04-30 stsp while (i < id_display_len) {
194 881b2d3e 2018-04-30 stsp if (col >= avail)
196 881b2d3e 2018-04-30 stsp line[col++] = ' ';
199 881b2d3e 2018-04-30 stsp if (col >= avail)
201 881b2d3e 2018-04-30 stsp line[col++] = ' ';
202 881b2d3e 2018-04-30 stsp for (i = 0; i < MIN(author_display_len, author_len); i++) {
203 881b2d3e 2018-04-30 stsp if (col >= avail)
205 881b2d3e 2018-04-30 stsp line[col++] = author[i];
207 881b2d3e 2018-04-30 stsp while (i < author_display_len) {
208 881b2d3e 2018-04-30 stsp if (col >= avail)
210 881b2d3e 2018-04-30 stsp line[col++] = ' ';
213 881b2d3e 2018-04-30 stsp if (col >= avail)
215 881b2d3e 2018-04-30 stsp line[col++] = ' ';
217 881b2d3e 2018-04-30 stsp while (col < avail && *logmsg)
218 881b2d3e 2018-04-30 stsp line[col++] = *logmsg++;
219 881b2d3e 2018-04-30 stsp while (col < avail)
220 881b2d3e 2018-04-30 stsp line[col++] = ' ';
222 881b2d3e 2018-04-30 stsp waddstr(tog_log_view.window, line);
223 80ddbec8 2018-04-29 stsp waddch(tog_log_view.window, '\n');
225 80ddbec8 2018-04-29 stsp free(logmsg0);
226 6d9fbc00 2018-04-29 stsp free(author0);
227 80ddbec8 2018-04-29 stsp free(line);
228 6d9fbc00 2018-04-29 stsp free(id_str);
229 80ddbec8 2018-04-29 stsp return err;
232 80ddbec8 2018-04-29 stsp struct commit_queue_entry {
233 80ddbec8 2018-04-29 stsp TAILQ_ENTRY(commit_queue_entry) entry;
234 80ddbec8 2018-04-29 stsp struct got_object_id *id;
235 80ddbec8 2018-04-29 stsp struct got_commit_object *commit;
237 0553a4e3 2018-04-30 stsp TAILQ_HEAD(commit_queue, commit_queue_entry);
239 899d86c2 2018-05-10 stsp static struct commit_queue_entry *
240 899d86c2 2018-05-10 stsp alloc_commit_queue_entry(struct got_commit_object *commit,
241 899d86c2 2018-05-10 stsp struct got_object_id *id)
243 80ddbec8 2018-04-29 stsp struct commit_queue_entry *entry;
245 80ddbec8 2018-04-29 stsp entry = calloc(1, sizeof(*entry));
246 80ddbec8 2018-04-29 stsp if (entry == NULL)
247 899d86c2 2018-05-10 stsp return NULL;
249 899d86c2 2018-05-10 stsp entry->id = id;
250 99db9666 2018-05-07 stsp entry->commit = commit;
251 899d86c2 2018-05-10 stsp return entry;
254 99db9666 2018-05-07 stsp static void
255 99db9666 2018-05-07 stsp pop_commit(struct commit_queue *commits)
257 99db9666 2018-05-07 stsp struct commit_queue_entry *entry;
259 99db9666 2018-05-07 stsp entry = TAILQ_FIRST(commits);
260 99db9666 2018-05-07 stsp TAILQ_REMOVE(commits, entry, entry);
261 99db9666 2018-05-07 stsp got_object_commit_close(entry->commit);
262 99db9666 2018-05-07 stsp free(entry->id);
263 99db9666 2018-05-07 stsp free(entry);
266 99db9666 2018-05-07 stsp static void
267 99db9666 2018-05-07 stsp free_commits(struct commit_queue *commits)
269 99db9666 2018-05-07 stsp while (!TAILQ_EMPTY(commits))
270 99db9666 2018-05-07 stsp pop_commit(commits);
274 c4972b91 2018-05-07 stsp static const struct got_error *
275 899d86c2 2018-05-10 stsp fetch_parent_commit(struct commit_queue_entry **pentry,
276 899d86c2 2018-05-10 stsp struct commit_queue_entry *entry, struct got_repository *repo)
278 899d86c2 2018-05-10 stsp const struct got_error *err = NULL;
279 c4972b91 2018-05-07 stsp struct got_object *obj = NULL;
280 899d86c2 2018-05-10 stsp struct got_commit_object *commit;
281 899d86c2 2018-05-10 stsp struct got_object_id *id;
282 c4972b91 2018-05-07 stsp struct got_parent_id *pid;
284 899d86c2 2018-05-10 stsp *pentry = NULL;
286 c4972b91 2018-05-07 stsp /* Follow the first parent (TODO: handle merge commits). */
287 c4972b91 2018-05-07 stsp pid = SIMPLEQ_FIRST(&entry->commit->parent_ids);
288 c4972b91 2018-05-07 stsp if (pid == NULL)
289 c4972b91 2018-05-07 stsp return NULL;
290 c4972b91 2018-05-07 stsp err = got_object_open(&obj, repo, pid->id);
292 c4972b91 2018-05-07 stsp return err;
293 c4972b91 2018-05-07 stsp if (got_object_get_type(obj) != GOT_OBJ_TYPE_COMMIT) {
294 c4972b91 2018-05-07 stsp err = got_error(GOT_ERR_OBJ_TYPE);
295 c4972b91 2018-05-07 stsp got_object_close(obj);
296 c4972b91 2018-05-07 stsp return err;
299 899d86c2 2018-05-10 stsp err = got_object_commit_open(&commit, repo, obj);
300 c4972b91 2018-05-07 stsp got_object_close(obj);
302 c4972b91 2018-05-07 stsp return err;
304 899d86c2 2018-05-10 stsp id = got_object_id_dup(pid->id);
305 899d86c2 2018-05-10 stsp if (id == NULL) {
306 c4972b91 2018-05-07 stsp err = got_error_from_errno();
307 899d86c2 2018-05-10 stsp got_object_commit_close(commit);
308 899d86c2 2018-05-10 stsp return err;;
311 899d86c2 2018-05-10 stsp *pentry = alloc_commit_queue_entry(commit, id);
312 899d86c2 2018-05-10 stsp if (*pentry == NULL) {
313 c4972b91 2018-05-07 stsp err = got_error_from_errno();
314 899d86c2 2018-05-10 stsp got_object_commit_close(commit);
317 899d86c2 2018-05-10 stsp return err;;
320 899d86c2 2018-05-10 stsp static const struct got_error *
321 899d86c2 2018-05-10 stsp get_head_commit_id(struct got_object_id **head_id, struct got_repository *repo)
323 899d86c2 2018-05-10 stsp const struct got_error *err = NULL;
324 899d86c2 2018-05-10 stsp struct got_reference *head_ref;
326 899d86c2 2018-05-10 stsp *head_id = NULL;
328 899d86c2 2018-05-10 stsp err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
330 899d86c2 2018-05-10 stsp return err;
332 899d86c2 2018-05-10 stsp err = got_ref_resolve(head_id, repo, head_ref);
333 899d86c2 2018-05-10 stsp got_ref_close(head_ref);
335 899d86c2 2018-05-10 stsp *head_id = NULL;
336 899d86c2 2018-05-10 stsp return err;
339 c4972b91 2018-05-07 stsp return NULL;
342 99db9666 2018-05-07 stsp static const struct got_error *
343 899d86c2 2018-05-10 stsp prepend_commits(int *ncommits, struct commit_queue *commits,
344 899d86c2 2018-05-10 stsp struct got_object_id *first_id, struct got_object_id *last_id,
345 899d86c2 2018-05-10 stsp int limit, struct got_repository *repo)
347 899d86c2 2018-05-10 stsp const struct got_error *err = NULL;
348 899d86c2 2018-05-10 stsp struct got_object *first_obj = NULL, *last_obj = NULL;
349 899d86c2 2018-05-10 stsp struct got_commit_object *commit = NULL;
350 899d86c2 2018-05-10 stsp struct got_object_id *id = NULL;
351 899d86c2 2018-05-10 stsp struct commit_queue_entry *entry, *old_head_entry;
353 899d86c2 2018-05-10 stsp *ncommits = 0;
355 899d86c2 2018-05-10 stsp err = got_object_open(&first_obj, repo, first_id);
358 899d86c2 2018-05-10 stsp if (got_object_get_type(first_obj) != GOT_OBJ_TYPE_COMMIT) {
359 899d86c2 2018-05-10 stsp err = got_error(GOT_ERR_OBJ_TYPE);
362 899d86c2 2018-05-10 stsp err = got_object_open(&last_obj, repo, last_id);
365 899d86c2 2018-05-10 stsp if (got_object_get_type(last_obj) != GOT_OBJ_TYPE_COMMIT) {
366 899d86c2 2018-05-10 stsp err = got_error(GOT_ERR_OBJ_TYPE);
370 899d86c2 2018-05-10 stsp err = got_object_commit_open(&commit, repo, first_obj);
374 899d86c2 2018-05-10 stsp id = got_object_id_dup(first_id);
375 899d86c2 2018-05-10 stsp if (id == NULL) {
376 899d86c2 2018-05-10 stsp err = got_error_from_errno();
380 899d86c2 2018-05-10 stsp entry = alloc_commit_queue_entry(commit, id);
381 899d86c2 2018-05-10 stsp if (entry == NULL)
382 899d86c2 2018-05-10 stsp return got_error_from_errno();
384 899d86c2 2018-05-10 stsp old_head_entry = TAILQ_FIRST(commits);
385 899d86c2 2018-05-10 stsp if (old_head_entry)
386 899d86c2 2018-05-10 stsp TAILQ_INSERT_BEFORE(old_head_entry, entry, entry);
388 899d86c2 2018-05-10 stsp TAILQ_INSERT_HEAD(commits, entry, entry);
390 899d86c2 2018-05-10 stsp *ncommits = 1;
393 899d86c2 2018-05-10 stsp * Fetch parent commits.
394 899d86c2 2018-05-10 stsp * XXX If first and last commit aren't ancestrally related this loop
395 899d86c2 2018-05-10 stsp * we will keep iterating until a root commit is encountered.
397 899d86c2 2018-05-10 stsp while (1) {
398 899d86c2 2018-05-10 stsp struct commit_queue_entry *pentry;
400 899d86c2 2018-05-10 stsp err = fetch_parent_commit(&pentry, entry, repo);
403 899d86c2 2018-05-10 stsp if (pentry == NULL)
407 899d86c2 2018-05-10 stsp * Fill up to old HEAD commit if commit queue was not empty.
408 899d86c2 2018-05-10 stsp * We must not leave a gap in history.
410 899d86c2 2018-05-10 stsp if (old_head_entry &&
411 899d86c2 2018-05-10 stsp got_object_id_cmp(pentry->id, old_head_entry->id) == 0)
414 899d86c2 2018-05-10 stsp TAILQ_INSERT_AFTER(commits, entry, pentry, entry);
415 899d86c2 2018-05-10 stsp (*ncommits)++;
416 899d86c2 2018-05-10 stsp if (*ncommits >= limit)
419 899d86c2 2018-05-10 stsp /* Fill up to last requested commit if queue was empty. */
420 899d86c2 2018-05-10 stsp if (old_head_entry == NULL &&
421 899d86c2 2018-05-10 stsp got_object_id_cmp(pentry->id, last_id) == 0)
424 899d86c2 2018-05-10 stsp entry = pentry;
428 899d86c2 2018-05-10 stsp if (first_obj)
429 899d86c2 2018-05-10 stsp got_object_close(first_obj);
430 899d86c2 2018-05-10 stsp if (last_obj)
431 899d86c2 2018-05-10 stsp got_object_close(last_obj);
432 899d86c2 2018-05-10 stsp return err;
435 899d86c2 2018-05-10 stsp static const struct got_error *
436 899d86c2 2018-05-10 stsp fetch_commits(struct commit_queue_entry **start_entry,
437 899d86c2 2018-05-10 stsp struct got_object_id *start_id, struct commit_queue *commits,
438 899d86c2 2018-05-10 stsp int limit, struct got_repository *repo)
440 99db9666 2018-05-07 stsp const struct got_error *err;
441 99db9666 2018-05-07 stsp struct commit_queue_entry *entry;
442 99db9666 2018-05-07 stsp int ncommits = 0;
443 899d86c2 2018-05-10 stsp struct got_object_id *head_id = NULL;
445 899d86c2 2018-05-10 stsp *start_entry = NULL;
447 899d86c2 2018-05-10 stsp err = get_head_commit_id(&head_id, repo);
449 99db9666 2018-05-07 stsp return err;
451 899d86c2 2018-05-10 stsp /* Prepend HEAD commit and all ancestors up to start commit. */
452 899d86c2 2018-05-10 stsp err = prepend_commits(&ncommits, commits, head_id, start_id, limit,
455 99db9666 2018-05-07 stsp return err;
457 899d86c2 2018-05-10 stsp if (got_object_id_cmp(head_id, start_id) == 0)
458 899d86c2 2018-05-10 stsp *start_entry = TAILQ_FIRST(commits);
460 899d86c2 2018-05-10 stsp *start_entry = TAILQ_LAST(commits, commit_queue);
462 899d86c2 2018-05-10 stsp if (ncommits >= limit)
463 899d86c2 2018-05-10 stsp return NULL;
465 899d86c2 2018-05-10 stsp /* Append more commits from start commit up to the requested limit. */
466 899d86c2 2018-05-10 stsp entry = TAILQ_LAST(commits, commit_queue);
467 899d86c2 2018-05-10 stsp while (entry && ncommits < limit) {
468 899d86c2 2018-05-10 stsp struct commit_queue_entry *pentry;
470 899d86c2 2018-05-10 stsp err = fetch_parent_commit(&pentry, entry, repo);
473 899d86c2 2018-05-10 stsp if (pentry)
474 899d86c2 2018-05-10 stsp TAILQ_INSERT_TAIL(commits, pentry, entry);
475 899d86c2 2018-05-10 stsp entry = pentry;
476 0553a4e3 2018-04-30 stsp ncommits++;
480 899d86c2 2018-05-10 stsp *start_entry = NULL;
481 0553a4e3 2018-04-30 stsp return err;
484 0553a4e3 2018-04-30 stsp static const struct got_error *
485 4a7f7875 2018-05-10 stsp draw_commits(struct commit_queue_entry **last, struct commit_queue_entry *first,
486 4a7f7875 2018-05-10 stsp int selected, int limit)
488 0553a4e3 2018-04-30 stsp const struct got_error *err = NULL;
489 0553a4e3 2018-04-30 stsp struct commit_queue_entry *entry;
490 0553a4e3 2018-04-30 stsp int ncommits = 0;
492 0553a4e3 2018-04-30 stsp wclear(tog_log_view.window);
494 899d86c2 2018-05-10 stsp entry = first;
495 899d86c2 2018-05-10 stsp *last = first;
496 899d86c2 2018-05-10 stsp while (entry) {
497 899d86c2 2018-05-10 stsp if (ncommits == limit)
499 0553a4e3 2018-04-30 stsp if (ncommits == selected)
500 0553a4e3 2018-04-30 stsp wstandout(tog_log_view.window);
501 0553a4e3 2018-04-30 stsp err = draw_commit(entry->commit, entry->id);
502 0553a4e3 2018-04-30 stsp if (ncommits == selected)
503 0553a4e3 2018-04-30 stsp wstandend(tog_log_view.window);
506 0553a4e3 2018-04-30 stsp ncommits++;
507 899d86c2 2018-05-10 stsp *last = entry;
508 899d86c2 2018-05-10 stsp entry = TAILQ_NEXT(entry, entry);
511 80ddbec8 2018-04-29 stsp update_panels();
512 80ddbec8 2018-04-29 stsp doupdate();
514 80ddbec8 2018-04-29 stsp return err;
517 07b55e75 2018-05-10 stsp static void
518 07b55e75 2018-05-10 stsp scroll_up(struct commit_queue_entry **first_displayed_entry, int n,
519 07b55e75 2018-05-10 stsp struct commit_queue *commits)
521 07b55e75 2018-05-10 stsp struct commit_queue_entry *entry;
522 07b55e75 2018-05-10 stsp int nscrolled = 0;
524 07b55e75 2018-05-10 stsp entry = TAILQ_FIRST(commits);
525 07b55e75 2018-05-10 stsp if (*first_displayed_entry == entry)
528 07b55e75 2018-05-10 stsp entry = *first_displayed_entry;
529 07b55e75 2018-05-10 stsp while (entry && nscrolled < n) {
530 07b55e75 2018-05-10 stsp entry = TAILQ_PREV(entry, commit_queue, entry);
531 07b55e75 2018-05-10 stsp if (entry) {
532 07b55e75 2018-05-10 stsp *first_displayed_entry = entry;
533 07b55e75 2018-05-10 stsp nscrolled++;
538 aa075928 2018-05-10 stsp static const struct got_error *
539 aa075928 2018-05-10 stsp scroll_down(struct commit_queue_entry **first_displayed_entry, int n,
540 aa075928 2018-05-10 stsp struct commit_queue_entry *last_displayed_entry,
541 aa075928 2018-05-10 stsp struct commit_queue *commits, struct got_repository *repo)
543 aa075928 2018-05-10 stsp const struct got_error *err = NULL;
544 aa075928 2018-05-10 stsp struct commit_queue_entry *entry;
545 aa075928 2018-05-10 stsp int nscrolled = 0;
547 aa075928 2018-05-10 stsp if (last_displayed_entry->commit->nparents == 0)
548 aa075928 2018-05-10 stsp return NULL;
550 aa075928 2018-05-10 stsp entry = *first_displayed_entry;
552 aa075928 2018-05-10 stsp struct commit_queue_entry *pentry;
554 aa075928 2018-05-10 stsp pentry = TAILQ_NEXT(entry, entry);
555 aa075928 2018-05-10 stsp if (pentry == NULL) {
556 aa075928 2018-05-10 stsp err = fetch_parent_commit(&pentry, entry, repo);
559 aa075928 2018-05-10 stsp if (pentry == NULL) {
560 aa075928 2018-05-10 stsp *first_displayed_entry = entry;
561 aa075928 2018-05-10 stsp return NULL;
563 aa075928 2018-05-10 stsp TAILQ_INSERT_TAIL(commits, pentry, entry);
564 aa075928 2018-05-10 stsp last_displayed_entry = pentry;
567 aa075928 2018-05-10 stsp *first_displayed_entry = pentry;
568 aa075928 2018-05-10 stsp entry = pentry;
570 aa075928 2018-05-10 stsp if (TAILQ_LAST(commits, commit_queue) == last_displayed_entry) {
571 aa075928 2018-05-10 stsp err = fetch_parent_commit(&pentry, last_displayed_entry,
575 aa075928 2018-05-10 stsp if (pentry) {
576 aa075928 2018-05-10 stsp TAILQ_INSERT_TAIL(commits, pentry, entry);
577 aa075928 2018-05-10 stsp last_displayed_entry = pentry;
580 aa075928 2018-05-10 stsp } while (++nscrolled < n);
582 aa075928 2018-05-10 stsp return NULL;
586 4a7f7875 2018-05-10 stsp num_parents(struct commit_queue_entry *entry)
588 4a7f7875 2018-05-10 stsp int nparents = 0;
590 4a7f7875 2018-05-10 stsp while (entry) {
591 4a7f7875 2018-05-10 stsp entry = TAILQ_NEXT(entry, entry);
592 4a7f7875 2018-05-10 stsp nparents++;
595 4a7f7875 2018-05-10 stsp return nparents;
598 80ddbec8 2018-04-29 stsp static const struct got_error *
599 80ddbec8 2018-04-29 stsp show_log_view(struct got_object_id *start_id, struct got_repository *repo)
601 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
602 c4972b91 2018-05-07 stsp struct got_object_id *id;
603 4a7f7875 2018-05-10 stsp int ch, done = 0, selected = 0, nparents;
604 0553a4e3 2018-04-30 stsp struct commit_queue commits;
605 899d86c2 2018-05-10 stsp struct commit_queue_entry *first_displayed_entry = NULL;
606 899d86c2 2018-05-10 stsp struct commit_queue_entry *last_displayed_entry = NULL;
608 c4972b91 2018-05-07 stsp id = got_object_id_dup(start_id);
609 c4972b91 2018-05-07 stsp if (id == NULL)
610 c4972b91 2018-05-07 stsp return got_error_from_errno();
612 80ddbec8 2018-04-29 stsp if (tog_log_view.window == NULL) {
613 80ddbec8 2018-04-29 stsp tog_log_view.window = newwin(0, 0, 0, 0);
614 80ddbec8 2018-04-29 stsp if (tog_log_view.window == NULL)
615 80ddbec8 2018-04-29 stsp return got_error_from_errno();
616 00a838fa 2018-04-29 stsp keypad(tog_log_view.window, TRUE);
618 80ddbec8 2018-04-29 stsp if (tog_log_view.panel == NULL) {
619 80ddbec8 2018-04-29 stsp tog_log_view.panel = new_panel(tog_log_view.window);
620 80ddbec8 2018-04-29 stsp if (tog_log_view.panel == NULL)
621 80ddbec8 2018-04-29 stsp return got_error_from_errno();
624 899d86c2 2018-05-10 stsp TAILQ_INIT(&commits);
625 899d86c2 2018-05-10 stsp err = fetch_commits(&first_displayed_entry, id, &commits, LINES, repo);
628 899d86c2 2018-05-10 stsp while (!done) {
629 899d86c2 2018-05-10 stsp err = draw_commits(&last_displayed_entry, first_displayed_entry,
630 899d86c2 2018-05-10 stsp selected, LINES);
634 80ddbec8 2018-04-29 stsp nodelay(stdscr, FALSE);
635 80ddbec8 2018-04-29 stsp ch = wgetch(tog_log_view.window);
636 80ddbec8 2018-04-29 stsp switch (ch) {
638 31120ada 2018-04-30 stsp if (errno) {
639 31120ada 2018-04-30 stsp err = got_error_from_errno();
647 80ddbec8 2018-04-29 stsp case KEY_UP:
648 80ddbec8 2018-04-29 stsp if (selected > 0)
649 80ddbec8 2018-04-29 stsp selected--;
650 8ec9698d 2018-05-10 stsp if (selected > 0)
652 07b55e75 2018-05-10 stsp scroll_up(&first_displayed_entry, 1, &commits);
654 48531068 2018-05-10 stsp case KEY_PPAGE:
655 dfc1d240 2018-05-10 stsp if (TAILQ_FIRST(&commits) ==
656 dfc1d240 2018-05-10 stsp first_displayed_entry) {
657 dfc1d240 2018-05-10 stsp selected = 0;
660 48531068 2018-05-10 stsp scroll_up(&first_displayed_entry, LINES,
664 80ddbec8 2018-04-29 stsp case KEY_DOWN:
665 80ee4603 2018-05-10 stsp nparents = num_parents(first_displayed_entry);
666 06abe2cd 2018-05-10 stsp if (selected < LINES - 1 &&
667 06abe2cd 2018-05-10 stsp selected < nparents - 1)
668 80ddbec8 2018-04-29 stsp selected++;
669 06abe2cd 2018-05-10 stsp if (selected < LINES - 1 &&
670 06abe2cd 2018-05-10 stsp selected < nparents - 1)
672 aa075928 2018-05-10 stsp err = scroll_down(&first_displayed_entry, 1,
673 aa075928 2018-05-10 stsp last_displayed_entry, &commits, repo);
677 4a7f7875 2018-05-10 stsp case KEY_NPAGE:
678 4a7f7875 2018-05-10 stsp nparents = num_parents(first_displayed_entry);
679 dfc1d240 2018-05-10 stsp if (nparents < LINES - 1 &&
680 dfc1d240 2018-05-10 stsp selected < nparents - 1) {
681 dfc1d240 2018-05-10 stsp selected = nparents - 1;
684 e50ee4f1 2018-05-10 stsp err = scroll_down(&first_displayed_entry, LINES,
685 e50ee4f1 2018-05-10 stsp last_displayed_entry, &commits, repo);
688 4a7f7875 2018-05-10 stsp nparents = num_parents(first_displayed_entry);
689 dfc1d240 2018-05-10 stsp if (selected > nparents)
690 dfc1d240 2018-05-10 stsp selected = nparents - 1;
692 d6df9be4 2018-04-30 stsp case KEY_RESIZE:
693 31120ada 2018-04-30 stsp if (selected > LINES)
694 31120ada 2018-04-30 stsp selected = LINES - 1;
699 80ddbec8 2018-04-29 stsp nodelay(stdscr, TRUE);
702 0553a4e3 2018-04-30 stsp free_commits(&commits);
703 80ddbec8 2018-04-29 stsp return err;
706 9f7d7167 2018-04-29 stsp const struct got_error *
707 9f7d7167 2018-04-29 stsp cmd_log(int argc, char *argv[])
709 80ddbec8 2018-04-29 stsp const struct got_error *error;
710 80ddbec8 2018-04-29 stsp struct got_repository *repo;
711 899d86c2 2018-05-10 stsp struct got_object_id *start_id = NULL;
712 80ddbec8 2018-04-29 stsp char *repo_path = NULL;
713 80ddbec8 2018-04-29 stsp char *start_commit = NULL;
716 80ddbec8 2018-04-29 stsp #ifndef PROFILE
717 80ddbec8 2018-04-29 stsp if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
718 80ddbec8 2018-04-29 stsp err(1, "pledge");
721 80ddbec8 2018-04-29 stsp while ((ch = getopt(argc, argv, "c:")) != -1) {
722 80ddbec8 2018-04-29 stsp switch (ch) {
724 80ddbec8 2018-04-29 stsp start_commit = optarg;
728 80ddbec8 2018-04-29 stsp /* NOTREACHED */
732 80ddbec8 2018-04-29 stsp argc -= optind;
733 80ddbec8 2018-04-29 stsp argv += optind;
735 80ddbec8 2018-04-29 stsp if (argc == 0) {
736 80ddbec8 2018-04-29 stsp repo_path = getcwd(NULL, 0);
737 80ddbec8 2018-04-29 stsp if (repo_path == NULL)
738 80ddbec8 2018-04-29 stsp return got_error_from_errno();
739 80ddbec8 2018-04-29 stsp } else if (argc == 1) {
740 80ddbec8 2018-04-29 stsp repo_path = realpath(argv[0], NULL);
741 80ddbec8 2018-04-29 stsp if (repo_path == NULL)
742 80ddbec8 2018-04-29 stsp return got_error_from_errno();
744 80ddbec8 2018-04-29 stsp usage_log();
746 80ddbec8 2018-04-29 stsp error = got_repo_open(&repo, repo_path);
747 80ddbec8 2018-04-29 stsp free(repo_path);
748 80ddbec8 2018-04-29 stsp if (error != NULL)
749 80ddbec8 2018-04-29 stsp return error;
751 80ddbec8 2018-04-29 stsp if (start_commit == NULL) {
752 899d86c2 2018-05-10 stsp error = get_head_commit_id(&start_id, repo);
753 80ddbec8 2018-04-29 stsp if (error != NULL)
754 80ddbec8 2018-04-29 stsp return error;
756 80ddbec8 2018-04-29 stsp struct got_object *obj;
757 80ddbec8 2018-04-29 stsp error = got_object_open_by_id_str(&obj, repo, start_commit);
758 80ddbec8 2018-04-29 stsp if (error == NULL) {
759 899d86c2 2018-05-10 stsp start_id = got_object_get_id(obj);
760 899d86c2 2018-05-10 stsp if (start_id == NULL)
761 80ddbec8 2018-04-29 stsp error = got_error_from_errno();
764 80ddbec8 2018-04-29 stsp if (error != NULL)
765 80ddbec8 2018-04-29 stsp return error;
766 899d86c2 2018-05-10 stsp error = show_log_view(start_id, repo);
767 899d86c2 2018-05-10 stsp free(start_id);
768 80ddbec8 2018-04-29 stsp got_repo_close(repo);
769 80ddbec8 2018-04-29 stsp return error;
772 9f7d7167 2018-04-29 stsp __dead void
773 9f7d7167 2018-04-29 stsp usage_diff(void)
776 9f7d7167 2018-04-29 stsp fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
777 9f7d7167 2018-04-29 stsp getprogname());
781 26ed57b2 2018-05-19 stsp static const struct got_error *
782 26ed57b2 2018-05-19 stsp diff_blobs(FILE *outfile, struct got_object *obj1, struct got_object *obj2,
783 26ed57b2 2018-05-19 stsp struct got_repository *repo)
785 26ed57b2 2018-05-19 stsp const struct got_error *err;
786 26ed57b2 2018-05-19 stsp struct got_blob_object *blob1 = NULL, *blob2 = NULL;
788 26ed57b2 2018-05-19 stsp err = got_object_blob_open(&blob1, repo, obj1, 8192);
791 26ed57b2 2018-05-19 stsp err = got_object_blob_open(&blob2, repo, obj2, 8192);
795 26ed57b2 2018-05-19 stsp err = got_diff_blob(blob1, blob2, NULL, NULL, outfile);
798 26ed57b2 2018-05-19 stsp got_object_blob_close(blob1);
800 26ed57b2 2018-05-19 stsp got_object_blob_close(blob2);
801 26ed57b2 2018-05-19 stsp return err;
804 26ed57b2 2018-05-19 stsp static const struct got_error *
805 26ed57b2 2018-05-19 stsp diff_trees(FILE *outfile, struct got_object *obj1, struct got_object *obj2,
806 26ed57b2 2018-05-19 stsp struct got_repository *repo)
808 26ed57b2 2018-05-19 stsp const struct got_error *err;
809 26ed57b2 2018-05-19 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
811 26ed57b2 2018-05-19 stsp err = got_object_tree_open(&tree1, repo, obj1);
814 26ed57b2 2018-05-19 stsp err = got_object_tree_open(&tree2, repo, obj2);
818 26ed57b2 2018-05-19 stsp err = got_diff_tree(tree1, tree2, repo, outfile);
821 26ed57b2 2018-05-19 stsp got_object_tree_close(tree1);
823 26ed57b2 2018-05-19 stsp got_object_tree_close(tree2);
824 26ed57b2 2018-05-19 stsp return err;
827 26ed57b2 2018-05-19 stsp static const struct got_error *
828 26ed57b2 2018-05-19 stsp diff_commits(FILE *outfile, struct got_object *obj1, struct got_object *obj2,
829 26ed57b2 2018-05-19 stsp struct got_repository *repo)
831 26ed57b2 2018-05-19 stsp const struct got_error *err;
832 26ed57b2 2018-05-19 stsp struct got_commit_object *commit1 = NULL, *commit2 = NULL;
833 26ed57b2 2018-05-19 stsp struct got_object *tree_obj1 = NULL, *tree_obj2 = NULL;
835 26ed57b2 2018-05-19 stsp err = got_object_commit_open(&commit1, repo, obj1);
838 26ed57b2 2018-05-19 stsp err = got_object_commit_open(&commit2, repo, obj2);
842 26ed57b2 2018-05-19 stsp err = got_object_open(&tree_obj1, repo, commit1->tree_id);
845 26ed57b2 2018-05-19 stsp err = got_object_open(&tree_obj2, repo, commit2->tree_id);
849 26ed57b2 2018-05-19 stsp err = diff_trees(outfile, tree_obj1, tree_obj2, repo);
851 26ed57b2 2018-05-19 stsp if (tree_obj1)
852 26ed57b2 2018-05-19 stsp got_object_close(tree_obj1);
853 26ed57b2 2018-05-19 stsp if (tree_obj2)
854 26ed57b2 2018-05-19 stsp got_object_close(tree_obj2);
855 26ed57b2 2018-05-19 stsp if (commit1)
856 26ed57b2 2018-05-19 stsp got_object_commit_close(commit1);
857 26ed57b2 2018-05-19 stsp if (commit2)
858 26ed57b2 2018-05-19 stsp got_object_commit_close(commit2);
859 26ed57b2 2018-05-19 stsp return err;
863 26ed57b2 2018-05-19 stsp const struct got_error *
864 26ed57b2 2018-05-19 stsp draw_diff(FILE *f, int *first_displayed_line, int *last_displayed_line,
865 26ed57b2 2018-05-19 stsp int *eof, int max_lines)
867 26ed57b2 2018-05-19 stsp int nlines = 0, nprinted = 0;
870 26ed57b2 2018-05-19 stsp wclear(tog_diff_view.window);
873 26ed57b2 2018-05-19 stsp while (nprinted < max_lines) {
874 26ed57b2 2018-05-19 stsp char *line;
875 26ed57b2 2018-05-19 stsp size_t lineno;
876 26ed57b2 2018-05-19 stsp size_t linelen;
877 26ed57b2 2018-05-19 stsp const char delim[3] = { '\0', '\0', '\0'};
879 26ed57b2 2018-05-19 stsp line = fparseln(f, &linelen, &lineno, delim, 0);
880 26ed57b2 2018-05-19 stsp if (line == NULL) {
884 26ed57b2 2018-05-19 stsp if (++nlines < *first_displayed_line) {
885 26ed57b2 2018-05-19 stsp free(line);
889 26ed57b2 2018-05-19 stsp if (linelen > COLS - 1)
890 26ed57b2 2018-05-19 stsp line[COLS - 1] = '\0';
891 26ed57b2 2018-05-19 stsp waddstr(tog_diff_view.window, line);
892 26ed57b2 2018-05-19 stsp waddch(tog_diff_view.window, '\n');
893 26ed57b2 2018-05-19 stsp if (++nprinted == 1)
894 26ed57b2 2018-05-19 stsp *first_displayed_line = nlines;
895 26ed57b2 2018-05-19 stsp free(line);
897 26ed57b2 2018-05-19 stsp *last_displayed_line = nlines;
899 26ed57b2 2018-05-19 stsp update_panels();
900 26ed57b2 2018-05-19 stsp doupdate();
902 26ed57b2 2018-05-19 stsp return NULL;
905 9f7d7167 2018-04-29 stsp const struct got_error *
906 26ed57b2 2018-05-19 stsp show_diff_view(struct got_object *obj1, struct got_object *obj2,
907 26ed57b2 2018-05-19 stsp struct got_repository *repo)
909 26ed57b2 2018-05-19 stsp const struct got_error *err;
911 26ed57b2 2018-05-19 stsp int ch, done = 0, first_displayed_line = 1, last_displayed_line = LINES;
914 26ed57b2 2018-05-19 stsp if (got_object_get_type(obj1) != got_object_get_type(obj2))
915 26ed57b2 2018-05-19 stsp return got_error(GOT_ERR_OBJ_TYPE);
917 26ed57b2 2018-05-19 stsp f = tog_opentemp();
918 26ed57b2 2018-05-19 stsp if (f == NULL)
919 26ed57b2 2018-05-19 stsp return got_error_from_errno();
921 26ed57b2 2018-05-19 stsp switch (got_object_get_type(obj1)) {
922 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_BLOB:
923 26ed57b2 2018-05-19 stsp err = diff_blobs(f, obj1, obj2, repo);
925 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_TREE:
926 26ed57b2 2018-05-19 stsp err = diff_trees(f, obj1, obj2, repo);
928 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_COMMIT:
929 26ed57b2 2018-05-19 stsp err = diff_commits(f, obj1, obj2, repo);
932 26ed57b2 2018-05-19 stsp return got_error(GOT_ERR_OBJ_TYPE);
937 26ed57b2 2018-05-19 stsp if (tog_diff_view.window == NULL) {
938 26ed57b2 2018-05-19 stsp tog_diff_view.window = newwin(0, 0, 0, 0);
939 26ed57b2 2018-05-19 stsp if (tog_diff_view.window == NULL)
940 26ed57b2 2018-05-19 stsp return got_error_from_errno();
941 26ed57b2 2018-05-19 stsp keypad(tog_diff_view.window, TRUE);
943 26ed57b2 2018-05-19 stsp if (tog_diff_view.panel == NULL) {
944 26ed57b2 2018-05-19 stsp tog_diff_view.panel = new_panel(tog_diff_view.window);
945 26ed57b2 2018-05-19 stsp if (tog_diff_view.panel == NULL)
946 26ed57b2 2018-05-19 stsp return got_error_from_errno();
948 26ed57b2 2018-05-19 stsp show_panel(tog_diff_view.panel);
950 26ed57b2 2018-05-19 stsp while (!done) {
951 26ed57b2 2018-05-19 stsp err = draw_diff(f, &first_displayed_line, &last_displayed_line,
952 26ed57b2 2018-05-19 stsp &eof, LINES);
955 26ed57b2 2018-05-19 stsp nodelay(stdscr, FALSE);
956 26ed57b2 2018-05-19 stsp ch = wgetch(tog_diff_view.window);
957 26ed57b2 2018-05-19 stsp switch (ch) {
962 26ed57b2 2018-05-19 stsp case KEY_UP:
963 26ed57b2 2018-05-19 stsp if (first_displayed_line > 1)
964 26ed57b2 2018-05-19 stsp first_displayed_line--;
967 26ed57b2 2018-05-19 stsp case KEY_DOWN:
969 26ed57b2 2018-05-19 stsp first_displayed_line++;
974 26ed57b2 2018-05-19 stsp nodelay(stdscr, TRUE);
977 26ed57b2 2018-05-19 stsp return err;
980 26ed57b2 2018-05-19 stsp const struct got_error *
981 9f7d7167 2018-04-29 stsp cmd_diff(int argc, char *argv[])
983 26ed57b2 2018-05-19 stsp const struct got_error *error = NULL;
984 26ed57b2 2018-05-19 stsp struct got_repository *repo = NULL;
985 26ed57b2 2018-05-19 stsp struct got_object *obj1 = NULL, *obj2 = NULL;
986 26ed57b2 2018-05-19 stsp char *repo_path = NULL;
987 26ed57b2 2018-05-19 stsp char *obj_id_str1 = NULL, *obj_id_str2 = NULL;
990 26ed57b2 2018-05-19 stsp #ifndef PROFILE
991 26ed57b2 2018-05-19 stsp if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
992 26ed57b2 2018-05-19 stsp err(1, "pledge");
995 26ed57b2 2018-05-19 stsp while ((ch = getopt(argc, argv, "")) != -1) {
996 26ed57b2 2018-05-19 stsp switch (ch) {
999 26ed57b2 2018-05-19 stsp /* NOTREACHED */
1003 26ed57b2 2018-05-19 stsp argc -= optind;
1004 26ed57b2 2018-05-19 stsp argv += optind;
1006 26ed57b2 2018-05-19 stsp if (argc == 0) {
1007 26ed57b2 2018-05-19 stsp usage_diff(); /* TODO show local worktree changes */
1008 26ed57b2 2018-05-19 stsp } else if (argc == 2) {
1009 26ed57b2 2018-05-19 stsp repo_path = getcwd(NULL, 0);
1010 26ed57b2 2018-05-19 stsp if (repo_path == NULL)
1011 26ed57b2 2018-05-19 stsp return got_error_from_errno();
1012 26ed57b2 2018-05-19 stsp obj_id_str1 = argv[0];
1013 26ed57b2 2018-05-19 stsp obj_id_str2 = argv[1];
1014 26ed57b2 2018-05-19 stsp } else if (argc == 3) {
1015 26ed57b2 2018-05-19 stsp repo_path = realpath(argv[0], NULL);
1016 26ed57b2 2018-05-19 stsp if (repo_path == NULL)
1017 26ed57b2 2018-05-19 stsp return got_error_from_errno();
1018 26ed57b2 2018-05-19 stsp obj_id_str1 = argv[1];
1019 26ed57b2 2018-05-19 stsp obj_id_str2 = argv[2];
1021 26ed57b2 2018-05-19 stsp usage_diff();
1023 26ed57b2 2018-05-19 stsp error = got_repo_open(&repo, repo_path);
1024 26ed57b2 2018-05-19 stsp free(repo_path);
1025 26ed57b2 2018-05-19 stsp if (error)
1026 26ed57b2 2018-05-19 stsp goto done;
1028 26ed57b2 2018-05-19 stsp error = got_object_open_by_id_str(&obj1, repo, obj_id_str1);
1029 26ed57b2 2018-05-19 stsp if (error)
1030 26ed57b2 2018-05-19 stsp goto done;
1032 26ed57b2 2018-05-19 stsp error = got_object_open_by_id_str(&obj2, repo, obj_id_str2);
1033 26ed57b2 2018-05-19 stsp if (error)
1034 26ed57b2 2018-05-19 stsp goto done;
1036 26ed57b2 2018-05-19 stsp error = show_diff_view(obj1, obj2, repo);
1038 26ed57b2 2018-05-19 stsp got_repo_close(repo);
1040 26ed57b2 2018-05-19 stsp got_object_close(obj1);
1042 26ed57b2 2018-05-19 stsp got_object_close(obj2);
1043 26ed57b2 2018-05-19 stsp return error;
1046 9f7d7167 2018-04-29 stsp __dead void
1047 9f7d7167 2018-04-29 stsp usage_blame(void)
1050 9f7d7167 2018-04-29 stsp fprintf(stderr, "usage: %s blame [repository-path] blob-object\n",
1051 9f7d7167 2018-04-29 stsp getprogname());
1055 9f7d7167 2018-04-29 stsp const struct got_error *
1056 9f7d7167 2018-04-29 stsp cmd_blame(int argc, char *argv[])
1058 9f7d7167 2018-04-29 stsp return got_error(GOT_ERR_NOT_IMPL);
1061 9f7d7167 2018-04-29 stsp static const struct got_error *
1062 9f7d7167 2018-04-29 stsp init_curses(void)
1064 9f7d7167 2018-04-29 stsp initscr();
1068 9f7d7167 2018-04-29 stsp intrflush(stdscr, FALSE);
1069 9f7d7167 2018-04-29 stsp keypad(stdscr, TRUE);
1070 1f475ad8 2018-05-10 stsp curs_set(0);
1072 9f7d7167 2018-04-29 stsp tog_main_win = newwin(0, 0, 0, 0);
1073 9f7d7167 2018-04-29 stsp if (tog_main_win == NULL)
1074 9f7d7167 2018-04-29 stsp return got_error_from_errno();
1075 9f7d7167 2018-04-29 stsp tog_main_panel = new_panel(tog_main_win);
1076 9f7d7167 2018-04-29 stsp if (tog_main_panel == NULL)
1077 9f7d7167 2018-04-29 stsp return got_error_from_errno();
1079 9f7d7167 2018-04-29 stsp return NULL;
1082 9f7d7167 2018-04-29 stsp __dead void
1083 9f7d7167 2018-04-29 stsp usage(void)
1087 c2301be8 2018-04-30 stsp fprintf(stderr, "usage: %s [-h] [command] [arg ...]\n\n"
1088 9f7d7167 2018-04-29 stsp "Available commands:\n", getprogname());
1089 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
1090 9f7d7167 2018-04-29 stsp struct tog_cmd *cmd = &tog_commands[i];
1091 c2301be8 2018-04-30 stsp fprintf(stderr, " %s: %s\n", cmd->name, cmd->descr);
1096 c2301be8 2018-04-30 stsp static char **
1097 c2301be8 2018-04-30 stsp make_argv(const char *arg0, const char *arg1)
1099 c2301be8 2018-04-30 stsp char **argv;
1100 c2301be8 2018-04-30 stsp int argc = (arg1 == NULL ? 1 : 2);
1102 c2301be8 2018-04-30 stsp argv = calloc(argc, sizeof(char *));
1103 c2301be8 2018-04-30 stsp if (argv == NULL)
1104 c2301be8 2018-04-30 stsp err(1, "calloc");
1105 c2301be8 2018-04-30 stsp argv[0] = strdup(arg0);
1106 c2301be8 2018-04-30 stsp if (argv[0] == NULL)
1107 c2301be8 2018-04-30 stsp err(1, "calloc");
1108 c2301be8 2018-04-30 stsp if (arg1) {
1109 c2301be8 2018-04-30 stsp argv[1] = strdup(arg1);
1110 c2301be8 2018-04-30 stsp if (argv[1] == NULL)
1111 c2301be8 2018-04-30 stsp err(1, "calloc");
1114 c2301be8 2018-04-30 stsp return argv;
1118 9f7d7167 2018-04-29 stsp main(int argc, char *argv[])
1120 9f7d7167 2018-04-29 stsp const struct got_error *error = NULL;
1121 9f7d7167 2018-04-29 stsp struct tog_cmd *cmd = NULL;
1122 9f7d7167 2018-04-29 stsp int ch, hflag = 0;
1123 c2301be8 2018-04-30 stsp char **cmd_argv = NULL;
1125 9f7d7167 2018-04-29 stsp setlocale(LC_ALL, "");
1127 9f7d7167 2018-04-29 stsp while ((ch = getopt(argc, argv, "h")) != -1) {
1128 9f7d7167 2018-04-29 stsp switch (ch) {
1130 9f7d7167 2018-04-29 stsp hflag = 1;
1134 9f7d7167 2018-04-29 stsp /* NOTREACHED */
1138 9f7d7167 2018-04-29 stsp argc -= optind;
1139 9f7d7167 2018-04-29 stsp argv += optind;
1140 9f7d7167 2018-04-29 stsp optind = 0;
1141 c2301be8 2018-04-30 stsp optreset = 1;
1143 c2301be8 2018-04-30 stsp if (argc == 0) {
1144 c2301be8 2018-04-30 stsp /* Build an argument vector which runs a default command. */
1145 9f7d7167 2018-04-29 stsp cmd = &tog_commands[0];
1146 c2301be8 2018-04-30 stsp cmd_argv = make_argv(cmd->name, NULL);
1151 c2301be8 2018-04-30 stsp /* Did the user specific a command? */
1152 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
1153 c2301be8 2018-04-30 stsp if (strncmp(tog_commands[i].name, argv[0],
1154 9f7d7167 2018-04-29 stsp strlen(argv[0])) == 0) {
1155 9f7d7167 2018-04-29 stsp cmd = &tog_commands[i];
1156 9f7d7167 2018-04-29 stsp if (hflag)
1157 9f7d7167 2018-04-29 stsp tog_commands[i].cmd_usage();
1161 9f7d7167 2018-04-29 stsp if (cmd == NULL) {
1162 c2301be8 2018-04-30 stsp /* Did the user specify a repository? */
1163 c2301be8 2018-04-30 stsp char *repo_path = realpath(argv[0], NULL);
1164 c2301be8 2018-04-30 stsp if (repo_path) {
1165 c2301be8 2018-04-30 stsp struct got_repository *repo;
1166 c2301be8 2018-04-30 stsp error = got_repo_open(&repo, repo_path);
1167 c2301be8 2018-04-30 stsp if (error == NULL)
1168 c2301be8 2018-04-30 stsp got_repo_close(repo);
1170 ad7de8d9 2018-04-30 stsp error = got_error_from_errno();
1171 c2301be8 2018-04-30 stsp if (error) {
1172 ad7de8d9 2018-04-30 stsp fprintf(stderr, "%s: '%s' is neither a known "
1173 ad7de8d9 2018-04-30 stsp "command nor a path to a repository\n",
1174 c2301be8 2018-04-30 stsp getprogname(), argv[0]);
1175 ad7de8d9 2018-04-30 stsp free(repo_path);
1178 c2301be8 2018-04-30 stsp cmd = &tog_commands[0];
1179 c2301be8 2018-04-30 stsp cmd_argv = make_argv(cmd->name, repo_path);
1181 c2301be8 2018-04-30 stsp free(repo_path);
1185 9f7d7167 2018-04-29 stsp error = init_curses();
1186 9f7d7167 2018-04-29 stsp if (error) {
1187 80ddbec8 2018-04-29 stsp fprintf(stderr, "cannot initialize ncurses: %s\n", error->msg);
1191 c2301be8 2018-04-30 stsp error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
1192 9f7d7167 2018-04-29 stsp if (error)
1193 9f7d7167 2018-04-29 stsp goto done;
1196 c2301be8 2018-04-30 stsp free(cmd_argv);
1197 9f7d7167 2018-04-29 stsp if (error)
1198 9f7d7167 2018-04-29 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);