Blob


1 /*
2 * Copyright (c) 2017 Martin Pieuchot <mpi@openbsd.org>
3 * Copyright (c) 2018, 2019 Stefan Sperling <stsp@openbsd.org>
4 *
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 */
18 #include <sys/queue.h>
19 #include <sys/types.h>
20 #include <sys/stat.h>
21 #include <sys/param.h>
22 #include <sys/wait.h>
24 #include <err.h>
25 #include <errno.h>
26 #include <limits.h>
27 #include <locale.h>
28 #include <ctype.h>
29 #include <signal.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <unistd.h>
34 #include <libgen.h>
35 #include <time.h>
36 #include <paths.h>
38 #include "got_version.h"
39 #include "got_error.h"
40 #include "got_object.h"
41 #include "got_reference.h"
42 #include "got_repository.h"
43 #include "got_path.h"
44 #include "got_cancel.h"
45 #include "got_worktree.h"
46 #include "got_diff.h"
47 #include "got_commit_graph.h"
48 #include "got_blame.h"
49 #include "got_privsep.h"
50 #include "got_opentemp.h"
52 #ifndef nitems
53 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
54 #endif
56 static volatile sig_atomic_t sigint_received;
57 static volatile sig_atomic_t sigpipe_received;
59 static void
60 catch_sigint(int signo)
61 {
62 sigint_received = 1;
63 }
65 static void
66 catch_sigpipe(int signo)
67 {
68 sigpipe_received = 1;
69 }
72 struct got_cmd {
73 const char *cmd_name;
74 const struct got_error *(*cmd_main)(int, char *[]);
75 void (*cmd_usage)(void);
76 const char *cmd_alias;
77 };
79 __dead static void usage(int);
80 __dead static void usage_init(void);
81 __dead static void usage_import(void);
82 __dead static void usage_checkout(void);
83 __dead static void usage_update(void);
84 __dead static void usage_log(void);
85 __dead static void usage_diff(void);
86 __dead static void usage_blame(void);
87 __dead static void usage_tree(void);
88 __dead static void usage_status(void);
89 __dead static void usage_ref(void);
90 __dead static void usage_branch(void);
91 __dead static void usage_tag(void);
92 __dead static void usage_add(void);
93 __dead static void usage_remove(void);
94 __dead static void usage_revert(void);
95 __dead static void usage_commit(void);
96 __dead static void usage_cherrypick(void);
97 __dead static void usage_backout(void);
98 __dead static void usage_rebase(void);
99 __dead static void usage_histedit(void);
100 __dead static void usage_integrate(void);
101 __dead static void usage_stage(void);
102 __dead static void usage_unstage(void);
103 __dead static void usage_cat(void);
105 static const struct got_error* cmd_init(int, char *[]);
106 static const struct got_error* cmd_import(int, char *[]);
107 static const struct got_error* cmd_checkout(int, char *[]);
108 static const struct got_error* cmd_update(int, char *[]);
109 static const struct got_error* cmd_log(int, char *[]);
110 static const struct got_error* cmd_diff(int, char *[]);
111 static const struct got_error* cmd_blame(int, char *[]);
112 static const struct got_error* cmd_tree(int, char *[]);
113 static const struct got_error* cmd_status(int, char *[]);
114 static const struct got_error* cmd_ref(int, char *[]);
115 static const struct got_error* cmd_branch(int, char *[]);
116 static const struct got_error* cmd_tag(int, char *[]);
117 static const struct got_error* cmd_add(int, char *[]);
118 static const struct got_error* cmd_remove(int, char *[]);
119 static const struct got_error* cmd_revert(int, char *[]);
120 static const struct got_error* cmd_commit(int, char *[]);
121 static const struct got_error* cmd_cherrypick(int, char *[]);
122 static const struct got_error* cmd_backout(int, char *[]);
123 static const struct got_error* cmd_rebase(int, char *[]);
124 static const struct got_error* cmd_histedit(int, char *[]);
125 static const struct got_error* cmd_integrate(int, char *[]);
126 static const struct got_error* cmd_stage(int, char *[]);
127 static const struct got_error* cmd_unstage(int, char *[]);
128 static const struct got_error* cmd_cat(int, char *[]);
130 static struct got_cmd got_commands[] = {
131 { "init", cmd_init, usage_init, "in" },
132 { "import", cmd_import, usage_import, "im" },
133 { "checkout", cmd_checkout, usage_checkout, "co" },
134 { "update", cmd_update, usage_update, "up" },
135 { "log", cmd_log, usage_log, "" },
136 { "diff", cmd_diff, usage_diff, "di" },
137 { "blame", cmd_blame, usage_blame, "bl" },
138 { "tree", cmd_tree, usage_tree, "tr" },
139 { "status", cmd_status, usage_status, "st" },
140 { "ref", cmd_ref, usage_ref, "" },
141 { "branch", cmd_branch, usage_branch, "br" },
142 { "tag", cmd_tag, usage_tag, "" },
143 { "add", cmd_add, usage_add, "" },
144 { "remove", cmd_remove, usage_remove, "rm" },
145 { "revert", cmd_revert, usage_revert, "rv" },
146 { "commit", cmd_commit, usage_commit, "ci" },
147 { "cherrypick", cmd_cherrypick, usage_cherrypick, "cy" },
148 { "backout", cmd_backout, usage_backout, "bo" },
149 { "rebase", cmd_rebase, usage_rebase, "rb" },
150 { "histedit", cmd_histedit, usage_histedit, "he" },
151 { "integrate", cmd_integrate, usage_integrate,"ig" },
152 { "stage", cmd_stage, usage_stage, "sg" },
153 { "unstage", cmd_unstage, usage_unstage, "ug" },
154 { "cat", cmd_cat, usage_cat, "" },
155 };
157 static void
158 list_commands(void)
160 int i;
162 fprintf(stderr, "commands:");
163 for (i = 0; i < nitems(got_commands); i++) {
164 struct got_cmd *cmd = &got_commands[i];
165 fprintf(stderr, " %s", cmd->cmd_name);
167 fputc('\n', stderr);
170 int
171 main(int argc, char *argv[])
173 struct got_cmd *cmd;
174 unsigned int i;
175 int ch;
176 int hflag = 0, Vflag = 0;
178 setlocale(LC_CTYPE, "");
180 while ((ch = getopt(argc, argv, "hV")) != -1) {
181 switch (ch) {
182 case 'h':
183 hflag = 1;
184 break;
185 case 'V':
186 Vflag = 1;
187 break;
188 default:
189 usage(hflag);
190 /* NOTREACHED */
194 argc -= optind;
195 argv += optind;
196 optind = 0;
198 if (Vflag) {
199 got_version_print_str();
200 return 1;
203 if (argc <= 0)
204 usage(hflag);
206 signal(SIGINT, catch_sigint);
207 signal(SIGPIPE, catch_sigpipe);
209 for (i = 0; i < nitems(got_commands); i++) {
210 const struct got_error *error;
212 cmd = &got_commands[i];
214 if (strcmp(cmd->cmd_name, argv[0]) != 0 &&
215 strcmp(cmd->cmd_alias, argv[0]) != 0)
216 continue;
218 if (hflag)
219 got_commands[i].cmd_usage();
221 error = got_commands[i].cmd_main(argc, argv);
222 if (error && !(sigint_received || sigpipe_received)) {
223 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
224 return 1;
227 return 0;
230 fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
231 list_commands();
232 return 1;
235 __dead static void
236 usage(int hflag)
238 fprintf(stderr, "usage: %s [-h] [-V] command [arg ...]\n",
239 getprogname());
240 if (hflag)
241 list_commands();
242 exit(1);
245 static const struct got_error *
246 get_editor(char **abspath)
248 const struct got_error *err = NULL;
249 const char *editor;
251 *abspath = NULL;
253 editor = getenv("VISUAL");
254 if (editor == NULL)
255 editor = getenv("EDITOR");
257 if (editor) {
258 err = got_path_find_prog(abspath, editor);
259 if (err)
260 return err;
263 if (*abspath == NULL) {
264 *abspath = strdup("/bin/ed");
265 if (*abspath == NULL)
266 return got_error_from_errno("strdup");
269 return NULL;
272 static const struct got_error *
273 apply_unveil(const char *repo_path, int repo_read_only,
274 const char *worktree_path)
276 const struct got_error *err;
278 #ifdef PROFILE
279 if (unveil("gmon.out", "rwc") != 0)
280 return got_error_from_errno2("unveil", "gmon.out");
281 #endif
282 if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
283 return got_error_from_errno2("unveil", repo_path);
285 if (worktree_path && unveil(worktree_path, "rwc") != 0)
286 return got_error_from_errno2("unveil", worktree_path);
288 if (unveil("/tmp", "rwc") != 0)
289 return got_error_from_errno2("unveil", "/tmp");
291 err = got_privsep_unveil_exec_helpers();
292 if (err != NULL)
293 return err;
295 if (unveil(NULL, NULL) != 0)
296 return got_error_from_errno("unveil");
298 return NULL;
301 __dead static void
302 usage_init(void)
304 fprintf(stderr, "usage: %s init repository-path\n", getprogname());
305 exit(1);
308 static const struct got_error *
309 cmd_init(int argc, char *argv[])
311 const struct got_error *error = NULL;
312 char *repo_path = NULL;
313 int ch;
315 while ((ch = getopt(argc, argv, "")) != -1) {
316 switch (ch) {
317 default:
318 usage_init();
319 /* NOTREACHED */
323 argc -= optind;
324 argv += optind;
326 #ifndef PROFILE
327 if (pledge("stdio rpath wpath cpath unveil", NULL) == -1)
328 err(1, "pledge");
329 #endif
330 if (argc != 1)
331 usage_init();
333 repo_path = strdup(argv[0]);
334 if (repo_path == NULL)
335 return got_error_from_errno("strdup");
337 got_path_strip_trailing_slashes(repo_path);
339 error = got_path_mkdir(repo_path);
340 if (error &&
341 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
342 goto done;
344 error = apply_unveil(repo_path, 0, NULL);
345 if (error)
346 goto done;
348 error = got_repo_init(repo_path);
349 if (error != NULL)
350 goto done;
352 done:
353 free(repo_path);
354 return error;
357 __dead static void
358 usage_import(void)
360 fprintf(stderr, "usage: %s import [-b branch] [-m message] "
361 "[-r repository-path] [-I pattern] path\n", getprogname());
362 exit(1);
365 int
366 spawn_editor(const char *editor, const char *file)
368 pid_t pid;
369 sig_t sighup, sigint, sigquit;
370 int st = -1;
372 sighup = signal(SIGHUP, SIG_IGN);
373 sigint = signal(SIGINT, SIG_IGN);
374 sigquit = signal(SIGQUIT, SIG_IGN);
376 switch (pid = fork()) {
377 case -1:
378 goto doneediting;
379 case 0:
380 execl(editor, editor, file, (char *)NULL);
381 _exit(127);
384 while (waitpid(pid, &st, 0) == -1)
385 if (errno != EINTR)
386 break;
388 doneediting:
389 (void)signal(SIGHUP, sighup);
390 (void)signal(SIGINT, sigint);
391 (void)signal(SIGQUIT, sigquit);
393 if (!WIFEXITED(st)) {
394 errno = EINTR;
395 return -1;
398 return WEXITSTATUS(st);
401 static const struct got_error *
402 edit_logmsg(char **logmsg, const char *editor, const char *logmsg_path,
403 const char *initial_content)
405 const struct got_error *err = NULL;
406 char buf[1024];
407 struct stat st, st2;
408 FILE *fp;
409 int content_changed = 0;
410 size_t len;
412 *logmsg = NULL;
414 if (stat(logmsg_path, &st) == -1)
415 return got_error_from_errno2("stat", logmsg_path);
417 if (spawn_editor(editor, logmsg_path) == -1)
418 return got_error_from_errno("failed spawning editor");
420 if (stat(logmsg_path, &st2) == -1)
421 return got_error_from_errno("stat");
423 if (st.st_mtime == st2.st_mtime && st.st_size == st2.st_size)
424 return got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
425 "no changes made to commit message, aborting");
427 *logmsg = malloc(st2.st_size + 1);
428 if (*logmsg == NULL)
429 return got_error_from_errno("malloc");
430 (*logmsg)[0] = '\0';
431 len = 0;
433 fp = fopen(logmsg_path, "r");
434 if (fp == NULL) {
435 err = got_error_from_errno("fopen");
436 goto done;
438 while (fgets(buf, sizeof(buf), fp) != NULL) {
439 if (!content_changed && strcmp(buf, initial_content) != 0)
440 content_changed = 1;
441 if (buf[0] == '#' || (len == 0 && buf[0] == '\n'))
442 continue; /* remove comments and leading empty lines */
443 len = strlcat(*logmsg, buf, st2.st_size);
445 fclose(fp);
447 while (len > 0 && (*logmsg)[len - 1] == '\n') {
448 (*logmsg)[len - 1] = '\0';
449 len--;
452 if (len == 0 || !content_changed)
453 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
454 "commit message cannot be empty, aborting");
455 done:
456 if (err) {
457 free(*logmsg);
458 *logmsg = NULL;
460 return err;
463 static const struct got_error *
464 collect_import_msg(char **logmsg, const char *editor, const char *path_dir,
465 const char *branch_name)
467 char *initial_content = NULL, *logmsg_path = NULL;
468 const struct got_error *err = NULL;
469 int fd;
471 if (asprintf(&initial_content,
472 "\n# %s to be imported to branch %s\n", path_dir,
473 branch_name) == -1)
474 return got_error_from_errno("asprintf");
476 err = got_opentemp_named_fd(&logmsg_path, &fd, "/tmp/got-importmsg");
477 if (err)
478 goto done;
480 dprintf(fd, initial_content);
481 close(fd);
483 err = edit_logmsg(logmsg, editor, logmsg_path, initial_content);
484 done:
485 free(initial_content);
486 free(logmsg_path);
487 return err;
490 static const struct got_error *
491 import_progress(void *arg, const char *path)
493 printf("A %s\n", path);
494 return NULL;
497 static const struct got_error *
498 get_author(char **author, struct got_repository *repo)
500 const struct got_error *err = NULL;
501 const char *got_author, *name, *email;
503 *author = NULL;
505 name = got_repo_get_gitconfig_author_name(repo);
506 email = got_repo_get_gitconfig_author_email(repo);
507 if (name && email) {
508 if (asprintf(author, "%s <%s>", name, email) == -1)
509 return got_error_from_errno("asprintf");
510 return NULL;
513 got_author = getenv("GOT_AUTHOR");
514 if (got_author == NULL) {
515 name = got_repo_get_global_gitconfig_author_name(repo);
516 email = got_repo_get_global_gitconfig_author_email(repo);
517 if (name && email) {
518 if (asprintf(author, "%s <%s>", name, email) == -1)
519 return got_error_from_errno("asprintf");
520 return NULL;
522 /* TODO: Look up user in password database? */
523 return got_error(GOT_ERR_COMMIT_NO_AUTHOR);
526 *author = strdup(got_author);
527 if (*author == NULL)
528 return got_error_from_errno("strdup");
530 /*
531 * Really dumb email address check; we're only doing this to
532 * avoid git's object parser breaking on commits we create.
533 */
534 while (*got_author && *got_author != '<')
535 got_author++;
536 if (*got_author != '<') {
537 err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
538 goto done;
540 while (*got_author && *got_author != '@')
541 got_author++;
542 if (*got_author != '@') {
543 err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
544 goto done;
546 while (*got_author && *got_author != '>')
547 got_author++;
548 if (*got_author != '>')
549 err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
550 done:
551 if (err) {
552 free(*author);
553 *author = NULL;
555 return err;
558 static const struct got_error *
559 get_gitconfig_path(char **gitconfig_path)
561 const char *homedir = getenv("HOME");
563 *gitconfig_path = NULL;
564 if (homedir) {
565 if (asprintf(gitconfig_path, "%s/.gitconfig", homedir) == -1)
566 return got_error_from_errno("asprintf");
569 return NULL;
572 static const struct got_error *
573 cmd_import(int argc, char *argv[])
575 const struct got_error *error = NULL;
576 char *path_dir = NULL, *repo_path = NULL, *logmsg = NULL;
577 char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
578 const char *branch_name = "master";
579 char *refname = NULL, *id_str = NULL;
580 struct got_repository *repo = NULL;
581 struct got_reference *branch_ref = NULL, *head_ref = NULL;
582 struct got_object_id *new_commit_id = NULL;
583 int ch;
584 struct got_pathlist_head ignores;
585 struct got_pathlist_entry *pe;
587 TAILQ_INIT(&ignores);
589 while ((ch = getopt(argc, argv, "b:m:r:I:")) != -1) {
590 switch (ch) {
591 case 'b':
592 branch_name = optarg;
593 break;
594 case 'm':
595 logmsg = strdup(optarg);
596 if (logmsg == NULL) {
597 error = got_error_from_errno("strdup");
598 goto done;
600 break;
601 case 'r':
602 repo_path = realpath(optarg, NULL);
603 if (repo_path == NULL) {
604 error = got_error_from_errno("realpath");
605 goto done;
607 break;
608 case 'I':
609 if (optarg[0] == '\0')
610 break;
611 error = got_pathlist_insert(&pe, &ignores, optarg,
612 NULL);
613 if (error)
614 goto done;
615 break;
616 default:
617 usage_import();
618 /* NOTREACHED */
622 argc -= optind;
623 argv += optind;
625 #ifndef PROFILE
626 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
627 "unveil",
628 NULL) == -1)
629 err(1, "pledge");
630 #endif
631 if (argc != 1)
632 usage_import();
634 if (repo_path == NULL) {
635 repo_path = getcwd(NULL, 0);
636 if (repo_path == NULL)
637 return got_error_from_errno("getcwd");
639 got_path_strip_trailing_slashes(repo_path);
640 error = get_gitconfig_path(&gitconfig_path);
641 if (error)
642 goto done;
643 error = got_repo_open(&repo, repo_path, gitconfig_path);
644 if (error)
645 goto done;
647 error = get_author(&author, repo);
648 if (error)
649 return error;
651 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
652 error = got_error_from_errno("asprintf");
653 goto done;
656 error = got_ref_open(&branch_ref, repo, refname, 0);
657 if (error) {
658 if (error->code != GOT_ERR_NOT_REF)
659 goto done;
660 } else {
661 error = got_error_msg(GOT_ERR_BRANCH_EXISTS,
662 "import target branch already exists");
663 goto done;
666 path_dir = realpath(argv[0], NULL);
667 if (path_dir == NULL) {
668 error = got_error_from_errno("realpath");
669 goto done;
671 got_path_strip_trailing_slashes(path_dir);
673 /*
674 * unveil(2) traverses exec(2); if an editor is used we have
675 * to apply unveil after the log message has been written.
676 */
677 if (logmsg == NULL || strlen(logmsg) == 0) {
678 error = get_editor(&editor);
679 if (error)
680 goto done;
681 free(logmsg);
682 error = collect_import_msg(&logmsg, editor, path_dir, refname);
683 if (error)
684 goto done;
687 if (unveil(path_dir, "r") != 0)
688 return got_error_from_errno2("unveil", path_dir);
690 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
691 if (error)
692 goto done;
694 error = got_repo_import(&new_commit_id, path_dir, logmsg,
695 author, &ignores, repo, import_progress, NULL);
696 if (error)
697 goto done;
699 error = got_ref_alloc(&branch_ref, refname, new_commit_id);
700 if (error)
701 goto done;
703 error = got_ref_write(branch_ref, repo);
704 if (error)
705 goto done;
707 error = got_object_id_str(&id_str, new_commit_id);
708 if (error)
709 goto done;
711 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
712 if (error) {
713 if (error->code != GOT_ERR_NOT_REF)
714 goto done;
716 error = got_ref_alloc_symref(&head_ref, GOT_REF_HEAD,
717 branch_ref);
718 if (error)
719 goto done;
721 error = got_ref_write(head_ref, repo);
722 if (error)
723 goto done;
726 printf("Created branch %s with commit %s\n",
727 got_ref_get_name(branch_ref), id_str);
728 done:
729 free(logmsg);
730 free(repo_path);
731 free(editor);
732 free(refname);
733 free(new_commit_id);
734 free(id_str);
735 free(author);
736 free(gitconfig_path);
737 if (branch_ref)
738 got_ref_close(branch_ref);
739 if (head_ref)
740 got_ref_close(head_ref);
741 return error;
744 __dead static void
745 usage_checkout(void)
747 fprintf(stderr, "usage: %s checkout [-b branch] [-c commit] "
748 "[-p prefix] repository-path [worktree-path]\n", getprogname());
749 exit(1);
752 static const struct got_error *
753 checkout_progress(void *arg, unsigned char status, const char *path)
755 char *worktree_path = arg;
757 /* Base commit bump happens silently. */
758 if (status == GOT_STATUS_BUMP_BASE)
759 return NULL;
761 while (path[0] == '/')
762 path++;
764 printf("%c %s/%s\n", status, worktree_path, path);
765 return NULL;
768 static const struct got_error *
769 check_cancelled(void *arg)
771 if (sigint_received || sigpipe_received)
772 return got_error(GOT_ERR_CANCELLED);
773 return NULL;
776 static const struct got_error *
777 check_linear_ancestry(struct got_object_id *commit_id,
778 struct got_object_id *base_commit_id, int allow_forwards_in_time_only,
779 struct got_repository *repo)
781 const struct got_error *err = NULL;
782 struct got_object_id *yca_id;
784 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
785 commit_id, base_commit_id, repo, check_cancelled, NULL);
786 if (err)
787 return err;
789 if (yca_id == NULL)
790 return got_error(GOT_ERR_ANCESTRY);
792 /*
793 * Require a straight line of history between the target commit
794 * and the work tree's base commit.
796 * Non-linear situations such as this require a rebase:
798 * (commit) D F (base_commit)
799 * \ /
800 * C E
801 * \ /
802 * B (yca)
803 * |
804 * A
806 * 'got update' only handles linear cases:
807 * Update forwards in time: A (base/yca) - B - C - D (commit)
808 * Update backwards in time: D (base) - C - B - A (commit/yca)
809 */
810 if (allow_forwards_in_time_only) {
811 if (got_object_id_cmp(base_commit_id, yca_id) != 0)
812 return got_error(GOT_ERR_ANCESTRY);
813 } else if (got_object_id_cmp(commit_id, yca_id) != 0 &&
814 got_object_id_cmp(base_commit_id, yca_id) != 0)
815 return got_error(GOT_ERR_ANCESTRY);
817 free(yca_id);
818 return NULL;
821 static const struct got_error *
822 check_same_branch(struct got_object_id *commit_id,
823 struct got_reference *head_ref, struct got_object_id *yca_id,
824 struct got_repository *repo)
826 const struct got_error *err = NULL;
827 struct got_commit_graph *graph = NULL;
828 struct got_object_id *head_commit_id = NULL;
829 int is_same_branch = 0;
831 err = got_ref_resolve(&head_commit_id, repo, head_ref);
832 if (err)
833 goto done;
835 if (got_object_id_cmp(head_commit_id, commit_id) == 0) {
836 is_same_branch = 1;
837 goto done;
839 if (yca_id && got_object_id_cmp(commit_id, yca_id) == 0) {
840 is_same_branch = 1;
841 goto done;
844 err = got_commit_graph_open(&graph, head_commit_id, "/", 1, repo);
845 if (err)
846 goto done;
848 err = got_commit_graph_iter_start(graph, head_commit_id, repo,
849 check_cancelled, NULL);
850 if (err)
851 goto done;
853 for (;;) {
854 struct got_object_id *id;
855 err = got_commit_graph_iter_next(&id, graph);
856 if (err) {
857 if (err->code == GOT_ERR_ITER_COMPLETED) {
858 err = NULL;
859 break;
860 } else if (err->code != GOT_ERR_ITER_NEED_MORE)
861 break;
862 err = got_commit_graph_fetch_commits(graph, 1,
863 repo, check_cancelled, NULL);
864 if (err)
865 break;
868 if (id) {
869 if (yca_id && got_object_id_cmp(id, yca_id) == 0)
870 break;
871 if (got_object_id_cmp(id, commit_id) == 0) {
872 is_same_branch = 1;
873 break;
877 done:
878 if (graph)
879 got_commit_graph_close(graph);
880 free(head_commit_id);
881 if (!err && !is_same_branch)
882 err = got_error(GOT_ERR_ANCESTRY);
883 return err;
886 static const struct got_error *
887 resolve_commit_arg(struct got_object_id **commit_id,
888 const char *commit_id_arg, struct got_repository *repo)
890 const struct got_error *err;
891 struct got_reference *ref;
892 struct got_tag_object *tag;
894 err = got_repo_object_match_tag(&tag, commit_id_arg,
895 GOT_OBJ_TYPE_COMMIT, repo);
896 if (err == NULL) {
897 *commit_id = got_object_id_dup(
898 got_object_tag_get_object_id(tag));
899 if (*commit_id == NULL)
900 err = got_error_from_errno("got_object_id_dup");
901 got_object_tag_close(tag);
902 return err;
903 } else if (err->code != GOT_ERR_NO_OBJ)
904 return err;
906 err = got_ref_open(&ref, repo, commit_id_arg, 0);
907 if (err == NULL) {
908 err = got_ref_resolve(commit_id, repo, ref);
909 got_ref_close(ref);
910 } else {
911 if (err->code != GOT_ERR_NOT_REF)
912 return err;
913 err = got_repo_match_object_id_prefix(commit_id,
914 commit_id_arg, GOT_OBJ_TYPE_COMMIT, repo);
916 return err;
919 static const struct got_error *
920 cmd_checkout(int argc, char *argv[])
922 const struct got_error *error = NULL;
923 struct got_repository *repo = NULL;
924 struct got_reference *head_ref = NULL;
925 struct got_worktree *worktree = NULL;
926 char *repo_path = NULL;
927 char *worktree_path = NULL;
928 const char *path_prefix = "";
929 const char *branch_name = GOT_REF_HEAD;
930 char *commit_id_str = NULL;
931 int ch, same_path_prefix;
932 struct got_pathlist_head paths;
934 TAILQ_INIT(&paths);
936 while ((ch = getopt(argc, argv, "b:c:p:")) != -1) {
937 switch (ch) {
938 case 'b':
939 branch_name = optarg;
940 break;
941 case 'c':
942 commit_id_str = strdup(optarg);
943 if (commit_id_str == NULL)
944 return got_error_from_errno("strdup");
945 break;
946 case 'p':
947 path_prefix = optarg;
948 break;
949 default:
950 usage_checkout();
951 /* NOTREACHED */
955 argc -= optind;
956 argv += optind;
958 #ifndef PROFILE
959 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
960 "unveil", NULL) == -1)
961 err(1, "pledge");
962 #endif
963 if (argc == 1) {
964 char *cwd, *base, *dotgit;
965 repo_path = realpath(argv[0], NULL);
966 if (repo_path == NULL)
967 return got_error_from_errno2("realpath", argv[0]);
968 cwd = getcwd(NULL, 0);
969 if (cwd == NULL) {
970 error = got_error_from_errno("getcwd");
971 goto done;
973 if (path_prefix[0]) {
974 base = basename(path_prefix);
975 if (base == NULL) {
976 error = got_error_from_errno2("basename",
977 path_prefix);
978 goto done;
980 } else {
981 base = basename(repo_path);
982 if (base == NULL) {
983 error = got_error_from_errno2("basename",
984 repo_path);
985 goto done;
988 dotgit = strstr(base, ".git");
989 if (dotgit)
990 *dotgit = '\0';
991 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
992 error = got_error_from_errno("asprintf");
993 free(cwd);
994 goto done;
996 free(cwd);
997 } else if (argc == 2) {
998 repo_path = realpath(argv[0], NULL);
999 if (repo_path == NULL) {
1000 error = got_error_from_errno2("realpath", argv[0]);
1001 goto done;
1003 worktree_path = realpath(argv[1], NULL);
1004 if (worktree_path == NULL) {
1005 if (errno != ENOENT) {
1006 error = got_error_from_errno2("realpath",
1007 argv[1]);
1008 goto done;
1010 worktree_path = strdup(argv[1]);
1011 if (worktree_path == NULL) {
1012 error = got_error_from_errno("strdup");
1013 goto done;
1016 } else
1017 usage_checkout();
1019 got_path_strip_trailing_slashes(repo_path);
1020 got_path_strip_trailing_slashes(worktree_path);
1022 error = got_repo_open(&repo, repo_path, NULL);
1023 if (error != NULL)
1024 goto done;
1026 /* Pre-create work tree path for unveil(2) */
1027 error = got_path_mkdir(worktree_path);
1028 if (error) {
1029 if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
1030 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
1031 goto done;
1032 if (!got_path_dir_is_empty(worktree_path)) {
1033 error = got_error_path(worktree_path,
1034 GOT_ERR_DIR_NOT_EMPTY);
1035 goto done;
1039 error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
1040 if (error)
1041 goto done;
1043 error = got_ref_open(&head_ref, repo, branch_name, 0);
1044 if (error != NULL)
1045 goto done;
1047 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
1048 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
1049 goto done;
1051 error = got_worktree_open(&worktree, worktree_path);
1052 if (error != NULL)
1053 goto done;
1055 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
1056 path_prefix);
1057 if (error != NULL)
1058 goto done;
1059 if (!same_path_prefix) {
1060 error = got_error(GOT_ERR_PATH_PREFIX);
1061 goto done;
1064 if (commit_id_str) {
1065 struct got_object_id *commit_id;
1066 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
1067 if (error)
1068 goto done;
1069 error = check_linear_ancestry(commit_id,
1070 got_worktree_get_base_commit_id(worktree), 0, repo);
1071 if (error != NULL) {
1072 free(commit_id);
1073 goto done;
1075 error = check_same_branch(commit_id, head_ref, NULL, repo);
1076 if (error)
1077 goto done;
1078 error = got_worktree_set_base_commit_id(worktree, repo,
1079 commit_id);
1080 free(commit_id);
1081 if (error)
1082 goto done;
1085 error = got_pathlist_append(&paths, "", NULL);
1086 if (error)
1087 goto done;
1088 error = got_worktree_checkout_files(worktree, &paths, repo,
1089 checkout_progress, worktree_path, check_cancelled, NULL);
1090 if (error != NULL)
1091 goto done;
1093 printf("Now shut up and hack\n");
1095 done:
1096 got_pathlist_free(&paths);
1097 free(commit_id_str);
1098 free(repo_path);
1099 free(worktree_path);
1100 return error;
1103 __dead static void
1104 usage_update(void)
1106 fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path ...]\n",
1107 getprogname());
1108 exit(1);
1111 static const struct got_error *
1112 update_progress(void *arg, unsigned char status, const char *path)
1114 int *did_something = arg;
1116 if (status == GOT_STATUS_EXISTS)
1117 return NULL;
1119 *did_something = 1;
1121 /* Base commit bump happens silently. */
1122 if (status == GOT_STATUS_BUMP_BASE)
1123 return NULL;
1125 while (path[0] == '/')
1126 path++;
1127 printf("%c %s\n", status, path);
1128 return NULL;
1131 static const struct got_error *
1132 switch_head_ref(struct got_reference *head_ref,
1133 struct got_object_id *commit_id, struct got_worktree *worktree,
1134 struct got_repository *repo)
1136 const struct got_error *err = NULL;
1137 char *base_id_str;
1138 int ref_has_moved = 0;
1140 /* Trivial case: switching between two different references. */
1141 if (strcmp(got_ref_get_name(head_ref),
1142 got_worktree_get_head_ref_name(worktree)) != 0) {
1143 printf("Switching work tree from %s to %s\n",
1144 got_worktree_get_head_ref_name(worktree),
1145 got_ref_get_name(head_ref));
1146 return got_worktree_set_head_ref(worktree, head_ref);
1149 err = check_linear_ancestry(commit_id,
1150 got_worktree_get_base_commit_id(worktree), 0, repo);
1151 if (err) {
1152 if (err->code != GOT_ERR_ANCESTRY)
1153 return err;
1154 ref_has_moved = 1;
1156 if (!ref_has_moved)
1157 return NULL;
1159 /* Switching to a rebased branch with the same reference name. */
1160 err = got_object_id_str(&base_id_str,
1161 got_worktree_get_base_commit_id(worktree));
1162 if (err)
1163 return err;
1164 printf("Reference %s now points at a different branch\n",
1165 got_worktree_get_head_ref_name(worktree));
1166 printf("Switching work tree from %s to %s\n", base_id_str,
1167 got_worktree_get_head_ref_name(worktree));
1168 return NULL;
1171 static const struct got_error *
1172 check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
1174 const struct got_error *err;
1175 int in_progress;
1177 err = got_worktree_rebase_in_progress(&in_progress, worktree);
1178 if (err)
1179 return err;
1180 if (in_progress)
1181 return got_error(GOT_ERR_REBASING);
1183 err = got_worktree_histedit_in_progress(&in_progress, worktree);
1184 if (err)
1185 return err;
1186 if (in_progress)
1187 return got_error(GOT_ERR_HISTEDIT_BUSY);
1189 return NULL;
1192 static const struct got_error *
1193 get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
1194 char *argv[], struct got_worktree *worktree)
1196 const struct got_error *err = NULL;
1197 char *path;
1198 int i;
1200 if (argc == 0) {
1201 path = strdup("");
1202 if (path == NULL)
1203 return got_error_from_errno("strdup");
1204 return got_pathlist_append(paths, path, NULL);
1207 for (i = 0; i < argc; i++) {
1208 err = got_worktree_resolve_path(&path, worktree, argv[i]);
1209 if (err)
1210 break;
1211 err = got_pathlist_append(paths, path, NULL);
1212 if (err) {
1213 free(path);
1214 break;
1218 return err;
1221 static const struct got_error *
1222 cmd_update(int argc, char *argv[])
1224 const struct got_error *error = NULL;
1225 struct got_repository *repo = NULL;
1226 struct got_worktree *worktree = NULL;
1227 char *worktree_path = NULL;
1228 struct got_object_id *commit_id = NULL;
1229 char *commit_id_str = NULL;
1230 const char *branch_name = NULL;
1231 struct got_reference *head_ref = NULL;
1232 struct got_pathlist_head paths;
1233 struct got_pathlist_entry *pe;
1234 int ch, did_something = 0;
1236 TAILQ_INIT(&paths);
1238 while ((ch = getopt(argc, argv, "b:c:")) != -1) {
1239 switch (ch) {
1240 case 'b':
1241 branch_name = optarg;
1242 break;
1243 case 'c':
1244 commit_id_str = strdup(optarg);
1245 if (commit_id_str == NULL)
1246 return got_error_from_errno("strdup");
1247 break;
1248 default:
1249 usage_update();
1250 /* NOTREACHED */
1254 argc -= optind;
1255 argv += optind;
1257 #ifndef PROFILE
1258 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1259 "unveil", NULL) == -1)
1260 err(1, "pledge");
1261 #endif
1262 worktree_path = getcwd(NULL, 0);
1263 if (worktree_path == NULL) {
1264 error = got_error_from_errno("getcwd");
1265 goto done;
1267 error = got_worktree_open(&worktree, worktree_path);
1268 if (error)
1269 goto done;
1271 error = check_rebase_or_histedit_in_progress(worktree);
1272 if (error)
1273 goto done;
1275 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
1276 NULL);
1277 if (error != NULL)
1278 goto done;
1280 error = apply_unveil(got_repo_get_path(repo), 0,
1281 got_worktree_get_root_path(worktree));
1282 if (error)
1283 goto done;
1285 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
1286 if (error)
1287 goto done;
1289 error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
1290 got_worktree_get_head_ref_name(worktree), 0);
1291 if (error != NULL)
1292 goto done;
1293 if (commit_id_str == NULL) {
1294 error = got_ref_resolve(&commit_id, repo, head_ref);
1295 if (error != NULL)
1296 goto done;
1297 error = got_object_id_str(&commit_id_str, commit_id);
1298 if (error != NULL)
1299 goto done;
1300 } else {
1301 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
1302 free(commit_id_str);
1303 commit_id_str = NULL;
1304 if (error)
1305 goto done;
1306 error = got_object_id_str(&commit_id_str, commit_id);
1307 if (error)
1308 goto done;
1311 if (branch_name) {
1312 struct got_object_id *head_commit_id;
1313 TAILQ_FOREACH(pe, &paths, entry) {
1314 if (pe->path_len == 0)
1315 continue;
1316 error = got_error_msg(GOT_ERR_BAD_PATH,
1317 "switching between branches requires that "
1318 "the entire work tree gets updated");
1319 goto done;
1321 error = got_ref_resolve(&head_commit_id, repo, head_ref);
1322 if (error)
1323 goto done;
1324 error = check_linear_ancestry(commit_id, head_commit_id, 0,
1325 repo);
1326 free(head_commit_id);
1327 if (error != NULL)
1328 goto done;
1329 error = check_same_branch(commit_id, head_ref, NULL, repo);
1330 if (error)
1331 goto done;
1332 error = switch_head_ref(head_ref, commit_id, worktree, repo);
1333 if (error)
1334 goto done;
1335 } else {
1336 error = check_linear_ancestry(commit_id,
1337 got_worktree_get_base_commit_id(worktree), 0, repo);
1338 if (error != NULL) {
1339 if (error->code == GOT_ERR_ANCESTRY)
1340 error = got_error(GOT_ERR_BRANCH_MOVED);
1341 goto done;
1343 error = check_same_branch(commit_id, head_ref, NULL, repo);
1344 if (error)
1345 goto done;
1348 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
1349 commit_id) != 0) {
1350 error = got_worktree_set_base_commit_id(worktree, repo,
1351 commit_id);
1352 if (error)
1353 goto done;
1356 error = got_worktree_checkout_files(worktree, &paths, repo,
1357 update_progress, &did_something, check_cancelled, NULL);
1358 if (error != NULL)
1359 goto done;
1361 if (did_something)
1362 printf("Updated to commit %s\n", commit_id_str);
1363 else
1364 printf("Already up-to-date\n");
1365 done:
1366 free(worktree_path);
1367 TAILQ_FOREACH(pe, &paths, entry)
1368 free((char *)pe->path);
1369 got_pathlist_free(&paths);
1370 free(commit_id);
1371 free(commit_id_str);
1372 return error;
1375 static const struct got_error *
1376 diff_blobs(struct got_object_id *blob_id1, struct got_object_id *blob_id2,
1377 const char *path, int diff_context, int ignore_whitespace,
1378 struct got_repository *repo)
1380 const struct got_error *err = NULL;
1381 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
1383 if (blob_id1) {
1384 err = got_object_open_as_blob(&blob1, repo, blob_id1, 8192);
1385 if (err)
1386 goto done;
1389 err = got_object_open_as_blob(&blob2, repo, blob_id2, 8192);
1390 if (err)
1391 goto done;
1393 while (path[0] == '/')
1394 path++;
1395 err = got_diff_blob(blob1, blob2, path, path, diff_context,
1396 ignore_whitespace, stdout);
1397 done:
1398 if (blob1)
1399 got_object_blob_close(blob1);
1400 got_object_blob_close(blob2);
1401 return err;
1404 static const struct got_error *
1405 diff_trees(struct got_object_id *tree_id1, struct got_object_id *tree_id2,
1406 const char *path, int diff_context, int ignore_whitespace,
1407 struct got_repository *repo)
1409 const struct got_error *err = NULL;
1410 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
1411 struct got_diff_blob_output_unidiff_arg arg;
1413 if (tree_id1) {
1414 err = got_object_open_as_tree(&tree1, repo, tree_id1);
1415 if (err)
1416 goto done;
1419 err = got_object_open_as_tree(&tree2, repo, tree_id2);
1420 if (err)
1421 goto done;
1423 arg.diff_context = diff_context;
1424 arg.ignore_whitespace = ignore_whitespace;
1425 arg.outfile = stdout;
1426 while (path[0] == '/')
1427 path++;
1428 err = got_diff_tree(tree1, tree2, path, path, repo,
1429 got_diff_blob_output_unidiff, &arg, 1);
1430 done:
1431 if (tree1)
1432 got_object_tree_close(tree1);
1433 if (tree2)
1434 got_object_tree_close(tree2);
1435 return err;
1438 static const struct got_error *
1439 print_patch(struct got_commit_object *commit, struct got_object_id *id,
1440 const char *path, int diff_context, struct got_repository *repo)
1442 const struct got_error *err = NULL;
1443 struct got_commit_object *pcommit = NULL;
1444 char *id_str1 = NULL, *id_str2 = NULL;
1445 struct got_object_id *obj_id1 = NULL, *obj_id2 = NULL;
1446 struct got_object_qid *qid;
1448 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1449 if (qid != NULL) {
1450 err = got_object_open_as_commit(&pcommit, repo,
1451 qid->id);
1452 if (err)
1453 return err;
1456 if (path && path[0] != '\0') {
1457 int obj_type;
1458 err = got_object_id_by_path(&obj_id2, repo, id, path);
1459 if (err)
1460 goto done;
1461 err = got_object_id_str(&id_str2, obj_id2);
1462 if (err) {
1463 free(obj_id2);
1464 goto done;
1466 if (pcommit) {
1467 err = got_object_id_by_path(&obj_id1, repo,
1468 qid->id, path);
1469 if (err) {
1470 free(obj_id2);
1471 goto done;
1473 err = got_object_id_str(&id_str1, obj_id1);
1474 if (err) {
1475 free(obj_id2);
1476 goto done;
1479 err = got_object_get_type(&obj_type, repo, obj_id2);
1480 if (err) {
1481 free(obj_id2);
1482 goto done;
1484 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1485 switch (obj_type) {
1486 case GOT_OBJ_TYPE_BLOB:
1487 err = diff_blobs(obj_id1, obj_id2, path, diff_context,
1488 0, repo);
1489 break;
1490 case GOT_OBJ_TYPE_TREE:
1491 err = diff_trees(obj_id1, obj_id2, path, diff_context,
1492 0, repo);
1493 break;
1494 default:
1495 err = got_error(GOT_ERR_OBJ_TYPE);
1496 break;
1498 free(obj_id1);
1499 free(obj_id2);
1500 } else {
1501 obj_id2 = got_object_commit_get_tree_id(commit);
1502 err = got_object_id_str(&id_str2, obj_id2);
1503 if (err)
1504 goto done;
1505 obj_id1 = got_object_commit_get_tree_id(pcommit);
1506 err = got_object_id_str(&id_str1, obj_id1);
1507 if (err)
1508 goto done;
1509 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1510 err = diff_trees(obj_id1, obj_id2, "", diff_context, 0, repo);
1513 done:
1514 free(id_str1);
1515 free(id_str2);
1516 if (pcommit)
1517 got_object_commit_close(pcommit);
1518 return err;
1521 static char *
1522 get_datestr(time_t *time, char *datebuf)
1524 struct tm mytm, *tm;
1525 char *p, *s;
1527 tm = gmtime_r(time, &mytm);
1528 if (tm == NULL)
1529 return NULL;
1530 s = asctime_r(tm, datebuf);
1531 if (s == NULL)
1532 return NULL;
1533 p = strchr(s, '\n');
1534 if (p)
1535 *p = '\0';
1536 return s;
1539 #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
1541 static const struct got_error *
1542 print_commit(struct got_commit_object *commit, struct got_object_id *id,
1543 struct got_repository *repo, const char *path, int show_patch,
1544 int diff_context, struct got_reflist_head *refs)
1546 const struct got_error *err = NULL;
1547 char *id_str, *datestr, *logmsg0, *logmsg, *line;
1548 char datebuf[26];
1549 time_t committer_time;
1550 const char *author, *committer;
1551 char *refs_str = NULL;
1552 struct got_reflist_entry *re;
1554 SIMPLEQ_FOREACH(re, refs, entry) {
1555 char *s;
1556 const char *name;
1557 struct got_tag_object *tag = NULL;
1558 int cmp;
1560 name = got_ref_get_name(re->ref);
1561 if (strcmp(name, GOT_REF_HEAD) == 0)
1562 continue;
1563 if (strncmp(name, "refs/", 5) == 0)
1564 name += 5;
1565 if (strncmp(name, "got/", 4) == 0)
1566 continue;
1567 if (strncmp(name, "heads/", 6) == 0)
1568 name += 6;
1569 if (strncmp(name, "remotes/", 8) == 0)
1570 name += 8;
1571 if (strncmp(name, "tags/", 5) == 0) {
1572 err = got_object_open_as_tag(&tag, repo, re->id);
1573 if (err) {
1574 if (err->code != GOT_ERR_OBJ_TYPE)
1575 return err;
1576 /* Ref points at something other than a tag. */
1577 err = NULL;
1578 tag = NULL;
1581 cmp = got_object_id_cmp(tag ?
1582 got_object_tag_get_object_id(tag) : re->id, id);
1583 if (tag)
1584 got_object_tag_close(tag);
1585 if (cmp != 0)
1586 continue;
1587 s = refs_str;
1588 if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
1589 name) == -1) {
1590 err = got_error_from_errno("asprintf");
1591 free(s);
1592 return err;
1594 free(s);
1596 err = got_object_id_str(&id_str, id);
1597 if (err)
1598 return err;
1600 printf(GOT_COMMIT_SEP_STR);
1601 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
1602 refs_str ? refs_str : "", refs_str ? ")" : "");
1603 free(id_str);
1604 id_str = NULL;
1605 free(refs_str);
1606 refs_str = NULL;
1607 printf("from: %s\n", got_object_commit_get_author(commit));
1608 committer_time = got_object_commit_get_committer_time(commit);
1609 datestr = get_datestr(&committer_time, datebuf);
1610 if (datestr)
1611 printf("date: %s UTC\n", datestr);
1612 author = got_object_commit_get_author(commit);
1613 committer = got_object_commit_get_committer(commit);
1614 if (strcmp(author, committer) != 0)
1615 printf("via: %s\n", committer);
1616 if (got_object_commit_get_nparents(commit) > 1) {
1617 const struct got_object_id_queue *parent_ids;
1618 struct got_object_qid *qid;
1619 int n = 1;
1620 parent_ids = got_object_commit_get_parent_ids(commit);
1621 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
1622 err = got_object_id_str(&id_str, qid->id);
1623 if (err)
1624 return err;
1625 printf("parent %d: %s\n", n++, id_str);
1626 free(id_str);
1630 err = got_object_commit_get_logmsg(&logmsg0, commit);
1631 if (err)
1632 return err;
1634 logmsg = logmsg0;
1635 do {
1636 line = strsep(&logmsg, "\n");
1637 if (line)
1638 printf(" %s\n", line);
1639 } while (line);
1640 free(logmsg0);
1642 if (show_patch) {
1643 err = print_patch(commit, id, path, diff_context, repo);
1644 if (err == 0)
1645 printf("\n");
1648 if (fflush(stdout) != 0 && err == NULL)
1649 err = got_error_from_errno("fflush");
1650 return err;
1653 static const struct got_error *
1654 print_commits(struct got_object_id *root_id, struct got_repository *repo,
1655 char *path, int show_patch, int diff_context, int limit,
1656 int first_parent_traversal, struct got_reflist_head *refs)
1658 const struct got_error *err;
1659 struct got_commit_graph *graph;
1661 err = got_commit_graph_open(&graph, root_id, path,
1662 first_parent_traversal, repo);
1663 if (err)
1664 return err;
1665 err = got_commit_graph_iter_start(graph, root_id, repo,
1666 check_cancelled, NULL);
1667 if (err)
1668 goto done;
1669 for (;;) {
1670 struct got_commit_object *commit;
1671 struct got_object_id *id;
1673 if (sigint_received || sigpipe_received)
1674 break;
1676 err = got_commit_graph_iter_next(&id, graph);
1677 if (err) {
1678 if (err->code == GOT_ERR_ITER_COMPLETED) {
1679 err = NULL;
1680 break;
1682 if (err->code != GOT_ERR_ITER_NEED_MORE)
1683 break;
1684 err = got_commit_graph_fetch_commits(graph, 1, repo,
1685 check_cancelled, NULL);
1686 if (err)
1687 break;
1688 else
1689 continue;
1691 if (id == NULL)
1692 break;
1694 err = got_object_open_as_commit(&commit, repo, id);
1695 if (err)
1696 break;
1697 err = print_commit(commit, id, repo, path, show_patch,
1698 diff_context, refs);
1699 got_object_commit_close(commit);
1700 if (err || (limit && --limit == 0))
1701 break;
1703 done:
1704 got_commit_graph_close(graph);
1705 return err;
1708 __dead static void
1709 usage_log(void)
1711 fprintf(stderr, "usage: %s log [-c commit] [-C number] [-f] [ -l N ] [-p] "
1712 "[-r repository-path] [path]\n", getprogname());
1713 exit(1);
1716 static int
1717 get_default_log_limit(void)
1719 const char *got_default_log_limit;
1720 long long n;
1721 const char *errstr;
1723 got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
1724 if (got_default_log_limit == NULL)
1725 return 0;
1726 n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
1727 if (errstr != NULL)
1728 return 0;
1729 return n;
1732 static const struct got_error *
1733 cmd_log(int argc, char *argv[])
1735 const struct got_error *error;
1736 struct got_repository *repo = NULL;
1737 struct got_worktree *worktree = NULL;
1738 struct got_commit_object *commit = NULL;
1739 struct got_object_id *id = NULL;
1740 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
1741 char *start_commit = NULL;
1742 int diff_context = 3, ch;
1743 int show_patch = 0, limit = 0, first_parent_traversal = 0;
1744 const char *errstr;
1745 struct got_reflist_head refs;
1747 SIMPLEQ_INIT(&refs);
1749 #ifndef PROFILE
1750 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1751 NULL)
1752 == -1)
1753 err(1, "pledge");
1754 #endif
1756 limit = get_default_log_limit();
1758 while ((ch = getopt(argc, argv, "b:pc:C:l:fr:")) != -1) {
1759 switch (ch) {
1760 case 'p':
1761 show_patch = 1;
1762 break;
1763 case 'c':
1764 start_commit = optarg;
1765 break;
1766 case 'C':
1767 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
1768 &errstr);
1769 if (errstr != NULL)
1770 err(1, "-C option %s", errstr);
1771 break;
1772 case 'l':
1773 limit = strtonum(optarg, 0, INT_MAX, &errstr);
1774 if (errstr != NULL)
1775 err(1, "-l option %s", errstr);
1776 break;
1777 case 'f':
1778 first_parent_traversal = 1;
1779 break;
1780 case 'r':
1781 repo_path = realpath(optarg, NULL);
1782 if (repo_path == NULL)
1783 err(1, "-r option");
1784 got_path_strip_trailing_slashes(repo_path);
1785 break;
1786 default:
1787 usage_log();
1788 /* NOTREACHED */
1792 argc -= optind;
1793 argv += optind;
1795 cwd = getcwd(NULL, 0);
1796 if (cwd == NULL) {
1797 error = got_error_from_errno("getcwd");
1798 goto done;
1801 error = got_worktree_open(&worktree, cwd);
1802 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1803 goto done;
1804 error = NULL;
1806 if (argc == 0) {
1807 path = strdup("");
1808 if (path == NULL) {
1809 error = got_error_from_errno("strdup");
1810 goto done;
1812 } else if (argc == 1) {
1813 if (worktree) {
1814 error = got_worktree_resolve_path(&path, worktree,
1815 argv[0]);
1816 if (error)
1817 goto done;
1818 } else {
1819 path = strdup(argv[0]);
1820 if (path == NULL) {
1821 error = got_error_from_errno("strdup");
1822 goto done;
1825 } else
1826 usage_log();
1828 if (repo_path == NULL) {
1829 repo_path = worktree ?
1830 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
1832 if (repo_path == NULL) {
1833 error = got_error_from_errno("strdup");
1834 goto done;
1837 error = got_repo_open(&repo, repo_path, NULL);
1838 if (error != NULL)
1839 goto done;
1841 error = apply_unveil(got_repo_get_path(repo), 1,
1842 worktree ? got_worktree_get_root_path(worktree) : NULL);
1843 if (error)
1844 goto done;
1846 if (start_commit == NULL) {
1847 struct got_reference *head_ref;
1848 error = got_ref_open(&head_ref, repo,
1849 worktree ? got_worktree_get_head_ref_name(worktree)
1850 : GOT_REF_HEAD, 0);
1851 if (error != NULL)
1852 return error;
1853 error = got_ref_resolve(&id, repo, head_ref);
1854 got_ref_close(head_ref);
1855 if (error != NULL)
1856 return error;
1857 error = got_object_open_as_commit(&commit, repo, id);
1858 } else {
1859 struct got_reference *ref;
1860 error = got_ref_open(&ref, repo, start_commit, 0);
1861 if (error == NULL) {
1862 int obj_type;
1863 error = got_ref_resolve(&id, repo, ref);
1864 got_ref_close(ref);
1865 if (error != NULL)
1866 goto done;
1867 error = got_object_get_type(&obj_type, repo, id);
1868 if (error != NULL)
1869 goto done;
1870 if (obj_type == GOT_OBJ_TYPE_TAG) {
1871 struct got_tag_object *tag;
1872 error = got_object_open_as_tag(&tag, repo, id);
1873 if (error != NULL)
1874 goto done;
1875 if (got_object_tag_get_object_type(tag) !=
1876 GOT_OBJ_TYPE_COMMIT) {
1877 got_object_tag_close(tag);
1878 error = got_error(GOT_ERR_OBJ_TYPE);
1879 goto done;
1881 free(id);
1882 id = got_object_id_dup(
1883 got_object_tag_get_object_id(tag));
1884 if (id == NULL)
1885 error = got_error_from_errno(
1886 "got_object_id_dup");
1887 got_object_tag_close(tag);
1888 if (error)
1889 goto done;
1890 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
1891 error = got_error(GOT_ERR_OBJ_TYPE);
1892 goto done;
1894 error = got_object_open_as_commit(&commit, repo, id);
1895 if (error != NULL)
1896 goto done;
1898 if (commit == NULL) {
1899 error = got_repo_match_object_id_prefix(&id,
1900 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
1901 if (error != NULL)
1902 return error;
1905 if (error != NULL)
1906 goto done;
1908 if (worktree) {
1909 const char *prefix = got_worktree_get_path_prefix(worktree);
1910 char *p;
1911 if (asprintf(&p, "%s%s%s", prefix,
1912 (strcmp(prefix, "/") != 0) ? "/" : "", path) == -1) {
1913 error = got_error_from_errno("asprintf");
1914 goto done;
1916 error = got_repo_map_path(&in_repo_path, repo, p, 1);
1917 free(p);
1918 } else
1919 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1920 if (error != NULL)
1921 goto done;
1922 if (in_repo_path) {
1923 free(path);
1924 path = in_repo_path;
1927 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
1928 if (error)
1929 goto done;
1931 error = print_commits(id, repo, path, show_patch,
1932 diff_context, limit, first_parent_traversal, &refs);
1933 done:
1934 free(path);
1935 free(repo_path);
1936 free(cwd);
1937 free(id);
1938 if (worktree)
1939 got_worktree_close(worktree);
1940 if (repo) {
1941 const struct got_error *repo_error;
1942 repo_error = got_repo_close(repo);
1943 if (error == NULL)
1944 error = repo_error;
1946 got_ref_list_free(&refs);
1947 return error;
1950 __dead static void
1951 usage_diff(void)
1953 fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] [-s] "
1954 "[-w] [object1 object2 | path]\n", getprogname());
1955 exit(1);
1958 struct print_diff_arg {
1959 struct got_repository *repo;
1960 struct got_worktree *worktree;
1961 int diff_context;
1962 const char *id_str;
1963 int header_shown;
1964 int diff_staged;
1965 int ignore_whitespace;
1968 static const struct got_error *
1969 print_diff(void *arg, unsigned char status, unsigned char staged_status,
1970 const char *path, struct got_object_id *blob_id,
1971 struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
1973 struct print_diff_arg *a = arg;
1974 const struct got_error *err = NULL;
1975 struct got_blob_object *blob1 = NULL;
1976 FILE *f2 = NULL;
1977 char *abspath = NULL, *label1 = NULL;
1978 struct stat sb;
1980 if (a->diff_staged) {
1981 if (staged_status != GOT_STATUS_MODIFY &&
1982 staged_status != GOT_STATUS_ADD &&
1983 staged_status != GOT_STATUS_DELETE)
1984 return NULL;
1985 } else {
1986 if (staged_status == GOT_STATUS_DELETE)
1987 return NULL;
1988 if (status == GOT_STATUS_NONEXISTENT)
1989 return got_error_set_errno(ENOENT, path);
1990 if (status != GOT_STATUS_MODIFY &&
1991 status != GOT_STATUS_ADD &&
1992 status != GOT_STATUS_DELETE &&
1993 status != GOT_STATUS_CONFLICT)
1994 return NULL;
1997 if (!a->header_shown) {
1998 printf("diff %s %s%s\n", a->id_str,
1999 got_worktree_get_root_path(a->worktree),
2000 a->diff_staged ? " (staged changes)" : "");
2001 a->header_shown = 1;
2004 if (a->diff_staged) {
2005 const char *label1 = NULL, *label2 = NULL;
2006 switch (staged_status) {
2007 case GOT_STATUS_MODIFY:
2008 label1 = path;
2009 label2 = path;
2010 break;
2011 case GOT_STATUS_ADD:
2012 label2 = path;
2013 break;
2014 case GOT_STATUS_DELETE:
2015 label1 = path;
2016 break;
2017 default:
2018 return got_error(GOT_ERR_FILE_STATUS);
2020 return got_diff_objects_as_blobs(blob_id, staged_blob_id,
2021 label1, label2, a->diff_context, a->ignore_whitespace,
2022 a->repo, stdout);
2025 if (staged_status == GOT_STATUS_ADD ||
2026 staged_status == GOT_STATUS_MODIFY) {
2027 char *id_str;
2028 err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
2029 8192);
2030 if (err)
2031 goto done;
2032 err = got_object_id_str(&id_str, staged_blob_id);
2033 if (err)
2034 goto done;
2035 if (asprintf(&label1, "%s (staged)", id_str) == -1) {
2036 err = got_error_from_errno("asprintf");
2037 free(id_str);
2038 goto done;
2040 free(id_str);
2041 } else if (status != GOT_STATUS_ADD) {
2042 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
2043 if (err)
2044 goto done;
2047 if (status != GOT_STATUS_DELETE) {
2048 if (asprintf(&abspath, "%s/%s",
2049 got_worktree_get_root_path(a->worktree), path) == -1) {
2050 err = got_error_from_errno("asprintf");
2051 goto done;
2054 f2 = fopen(abspath, "r");
2055 if (f2 == NULL) {
2056 err = got_error_from_errno2("fopen", abspath);
2057 goto done;
2059 if (lstat(abspath, &sb) == -1) {
2060 err = got_error_from_errno2("lstat", abspath);
2061 goto done;
2063 } else
2064 sb.st_size = 0;
2066 err = got_diff_blob_file(blob1, label1, f2, sb.st_size, path,
2067 a->diff_context, a->ignore_whitespace, stdout);
2068 done:
2069 if (blob1)
2070 got_object_blob_close(blob1);
2071 if (f2 && fclose(f2) != 0 && err == NULL)
2072 err = got_error_from_errno("fclose");
2073 free(abspath);
2074 return err;
2077 static const struct got_error *
2078 match_object_id(struct got_object_id **id, char **label,
2079 const char *id_str, int obj_type, int resolve_tags,
2080 struct got_repository *repo)
2082 const struct got_error *err;
2083 struct got_tag_object *tag;
2084 struct got_reference *ref = NULL;
2086 *id = NULL;
2087 *label = NULL;
2089 if (resolve_tags) {
2090 err = got_repo_object_match_tag(&tag, id_str, GOT_OBJ_TYPE_ANY,
2091 repo);
2092 if (err == NULL) {
2093 *id = got_object_id_dup(
2094 got_object_tag_get_object_id(tag));
2095 if (*id == NULL)
2096 err = got_error_from_errno("got_object_id_dup");
2097 else if (asprintf(label, "refs/tags/%s",
2098 got_object_tag_get_name(tag)) == -1) {
2099 err = got_error_from_errno("asprintf");
2100 free(*id);
2101 *id = NULL;
2103 got_object_tag_close(tag);
2104 return err;
2105 } else if (err->code != GOT_ERR_NO_OBJ)
2106 return err;
2109 err = got_repo_match_object_id_prefix(id, id_str, obj_type, repo);
2110 if (err) {
2111 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
2112 return err;
2113 err = got_ref_open(&ref, repo, id_str, 0);
2114 if (err != NULL)
2115 goto done;
2116 *label = strdup(got_ref_get_name(ref));
2117 if (*label == NULL) {
2118 err = got_error_from_errno("strdup");
2119 goto done;
2121 err = got_ref_resolve(id, repo, ref);
2122 } else {
2123 err = got_object_id_str(label, *id);
2124 if (*label == NULL) {
2125 err = got_error_from_errno("strdup");
2126 goto done;
2129 done:
2130 if (ref)
2131 got_ref_close(ref);
2132 return err;
2136 static const struct got_error *
2137 cmd_diff(int argc, char *argv[])
2139 const struct got_error *error;
2140 struct got_repository *repo = NULL;
2141 struct got_worktree *worktree = NULL;
2142 char *cwd = NULL, *repo_path = NULL;
2143 struct got_object_id *id1 = NULL, *id2 = NULL;
2144 const char *id_str1 = NULL, *id_str2 = NULL;
2145 char *label1 = NULL, *label2 = NULL;
2146 int type1, type2;
2147 int diff_context = 3, diff_staged = 0, ignore_whitespace = 0, ch;
2148 const char *errstr;
2149 char *path = NULL;
2151 #ifndef PROFILE
2152 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2153 NULL) == -1)
2154 err(1, "pledge");
2155 #endif
2157 while ((ch = getopt(argc, argv, "C:r:sw")) != -1) {
2158 switch (ch) {
2159 case 'C':
2160 diff_context = strtonum(optarg, 1, INT_MAX, &errstr);
2161 if (errstr != NULL)
2162 err(1, "-C option %s", errstr);
2163 break;
2164 case 'r':
2165 repo_path = realpath(optarg, NULL);
2166 if (repo_path == NULL)
2167 err(1, "-r option");
2168 got_path_strip_trailing_slashes(repo_path);
2169 break;
2170 case 's':
2171 diff_staged = 1;
2172 break;
2173 case 'w':
2174 ignore_whitespace = 1;
2175 break;
2176 default:
2177 usage_diff();
2178 /* NOTREACHED */
2182 argc -= optind;
2183 argv += optind;
2185 cwd = getcwd(NULL, 0);
2186 if (cwd == NULL) {
2187 error = got_error_from_errno("getcwd");
2188 goto done;
2190 error = got_worktree_open(&worktree, cwd);
2191 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2192 goto done;
2193 if (argc <= 1) {
2194 if (worktree == NULL) {
2195 error = got_error(GOT_ERR_NOT_WORKTREE);
2196 goto done;
2198 if (repo_path)
2199 errx(1,
2200 "-r option can't be used when diffing a work tree");
2201 repo_path = strdup(got_worktree_get_repo_path(worktree));
2202 if (repo_path == NULL) {
2203 error = got_error_from_errno("strdup");
2204 goto done;
2206 if (argc == 1) {
2207 error = got_worktree_resolve_path(&path, worktree,
2208 argv[0]);
2209 if (error)
2210 goto done;
2211 } else {
2212 path = strdup("");
2213 if (path == NULL) {
2214 error = got_error_from_errno("strdup");
2215 goto done;
2218 } else if (argc == 2) {
2219 if (diff_staged)
2220 errx(1, "-s option can't be used when diffing "
2221 "objects in repository");
2222 id_str1 = argv[0];
2223 id_str2 = argv[1];
2224 if (worktree && repo_path == NULL) {
2225 repo_path =
2226 strdup(got_worktree_get_repo_path(worktree));
2227 if (repo_path == NULL) {
2228 error = got_error_from_errno("strdup");
2229 goto done;
2232 } else
2233 usage_diff();
2235 if (repo_path == NULL) {
2236 repo_path = getcwd(NULL, 0);
2237 if (repo_path == NULL)
2238 return got_error_from_errno("getcwd");
2241 error = got_repo_open(&repo, repo_path, NULL);
2242 free(repo_path);
2243 if (error != NULL)
2244 goto done;
2246 error = apply_unveil(got_repo_get_path(repo), 1,
2247 worktree ? got_worktree_get_root_path(worktree) : NULL);
2248 if (error)
2249 goto done;
2251 if (argc <= 1) {
2252 struct print_diff_arg arg;
2253 struct got_pathlist_head paths;
2254 char *id_str;
2256 TAILQ_INIT(&paths);
2258 error = got_object_id_str(&id_str,
2259 got_worktree_get_base_commit_id(worktree));
2260 if (error)
2261 goto done;
2262 arg.repo = repo;
2263 arg.worktree = worktree;
2264 arg.diff_context = diff_context;
2265 arg.id_str = id_str;
2266 arg.header_shown = 0;
2267 arg.diff_staged = diff_staged;
2268 arg.ignore_whitespace = ignore_whitespace;
2270 error = got_pathlist_append(&paths, path, NULL);
2271 if (error)
2272 goto done;
2274 error = got_worktree_status(worktree, &paths, repo, print_diff,
2275 &arg, check_cancelled, NULL);
2276 free(id_str);
2277 got_pathlist_free(&paths);
2278 goto done;
2281 error = match_object_id(&id1, &label1, id_str1, GOT_OBJ_TYPE_ANY, 1,
2282 repo);
2283 if (error)
2284 goto done;
2286 error = match_object_id(&id2, &label2, id_str2, GOT_OBJ_TYPE_ANY, 1,
2287 repo);
2288 if (error)
2289 goto done;
2291 error = got_object_get_type(&type1, repo, id1);
2292 if (error)
2293 goto done;
2295 error = got_object_get_type(&type2, repo, id2);
2296 if (error)
2297 goto done;
2299 if (type1 != type2) {
2300 error = got_error(GOT_ERR_OBJ_TYPE);
2301 goto done;
2304 switch (type1) {
2305 case GOT_OBJ_TYPE_BLOB:
2306 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
2307 diff_context, ignore_whitespace, repo, stdout);
2308 break;
2309 case GOT_OBJ_TYPE_TREE:
2310 error = got_diff_objects_as_trees(id1, id2, "", "",
2311 diff_context, ignore_whitespace, repo, stdout);
2312 break;
2313 case GOT_OBJ_TYPE_COMMIT:
2314 printf("diff %s %s\n", label1, label2);
2315 error = got_diff_objects_as_commits(id1, id2, diff_context,
2316 ignore_whitespace, repo, stdout);
2317 break;
2318 default:
2319 error = got_error(GOT_ERR_OBJ_TYPE);
2322 done:
2323 free(label1);
2324 free(label2);
2325 free(id1);
2326 free(id2);
2327 free(path);
2328 if (worktree)
2329 got_worktree_close(worktree);
2330 if (repo) {
2331 const struct got_error *repo_error;
2332 repo_error = got_repo_close(repo);
2333 if (error == NULL)
2334 error = repo_error;
2336 return error;
2339 __dead static void
2340 usage_blame(void)
2342 fprintf(stderr,
2343 "usage: %s blame [-c commit] [-r repository-path] path\n",
2344 getprogname());
2345 exit(1);
2348 struct blame_line {
2349 int annotated;
2350 char *id_str;
2351 char *committer;
2352 char datebuf[9]; /* YY-MM-DD + NUL */
2355 struct blame_cb_args {
2356 struct blame_line *lines;
2357 int nlines;
2358 int nlines_prec;
2359 int lineno_cur;
2360 off_t *line_offsets;
2361 FILE *f;
2362 struct got_repository *repo;
2365 static const struct got_error *
2366 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2368 const struct got_error *err = NULL;
2369 struct blame_cb_args *a = arg;
2370 struct blame_line *bline;
2371 char *line = NULL;
2372 size_t linesize = 0;
2373 struct got_commit_object *commit = NULL;
2374 off_t offset;
2375 struct tm tm;
2376 time_t committer_time;
2378 if (nlines != a->nlines ||
2379 (lineno != -1 && lineno < 1) || lineno > a->nlines)
2380 return got_error(GOT_ERR_RANGE);
2382 if (sigint_received)
2383 return got_error(GOT_ERR_ITER_COMPLETED);
2385 if (lineno == -1)
2386 return NULL; /* no change in this commit */
2388 /* Annotate this line. */
2389 bline = &a->lines[lineno - 1];
2390 if (bline->annotated)
2391 return NULL;
2392 err = got_object_id_str(&bline->id_str, id);
2393 if (err)
2394 return err;
2396 err = got_object_open_as_commit(&commit, a->repo, id);
2397 if (err)
2398 goto done;
2400 bline->committer = strdup(got_object_commit_get_committer(commit));
2401 if (bline->committer == NULL) {
2402 err = got_error_from_errno("strdup");
2403 goto done;
2406 committer_time = got_object_commit_get_committer_time(commit);
2407 if (localtime_r(&committer_time, &tm) == NULL)
2408 return got_error_from_errno("localtime_r");
2409 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%g/%m/%d",
2410 &tm) >= sizeof(bline->datebuf)) {
2411 err = got_error(GOT_ERR_NO_SPACE);
2412 goto done;
2414 bline->annotated = 1;
2416 /* Print lines annotated so far. */
2417 bline = &a->lines[a->lineno_cur - 1];
2418 if (!bline->annotated)
2419 goto done;
2421 offset = a->line_offsets[a->lineno_cur - 1];
2422 if (fseeko(a->f, offset, SEEK_SET) == -1) {
2423 err = got_error_from_errno("fseeko");
2424 goto done;
2427 while (bline->annotated) {
2428 char *smallerthan, *at, *nl, *committer;
2429 size_t len;
2431 if (getline(&line, &linesize, a->f) == -1) {
2432 if (ferror(a->f))
2433 err = got_error_from_errno("getline");
2434 break;
2437 committer = bline->committer;
2438 smallerthan = strchr(committer, '<');
2439 if (smallerthan && smallerthan[1] != '\0')
2440 committer = smallerthan + 1;
2441 at = strchr(committer, '@');
2442 if (at)
2443 *at = '\0';
2444 len = strlen(committer);
2445 if (len >= 9)
2446 committer[8] = '\0';
2448 nl = strchr(line, '\n');
2449 if (nl)
2450 *nl = '\0';
2451 printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
2452 bline->id_str, bline->datebuf, committer, line);
2454 a->lineno_cur++;
2455 bline = &a->lines[a->lineno_cur - 1];
2457 done:
2458 if (commit)
2459 got_object_commit_close(commit);
2460 free(line);
2461 return err;
2464 static const struct got_error *
2465 cmd_blame(int argc, char *argv[])
2467 const struct got_error *error;
2468 struct got_repository *repo = NULL;
2469 struct got_worktree *worktree = NULL;
2470 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2471 struct got_object_id *obj_id = NULL;
2472 struct got_object_id *commit_id = NULL;
2473 struct got_blob_object *blob = NULL;
2474 char *commit_id_str = NULL;
2475 struct blame_cb_args bca;
2476 int ch, obj_type, i;
2477 size_t filesize;
2479 memset(&bca, 0, sizeof(bca));
2481 #ifndef PROFILE
2482 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2483 NULL) == -1)
2484 err(1, "pledge");
2485 #endif
2487 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2488 switch (ch) {
2489 case 'c':
2490 commit_id_str = optarg;
2491 break;
2492 case 'r':
2493 repo_path = realpath(optarg, NULL);
2494 if (repo_path == NULL)
2495 err(1, "-r option");
2496 got_path_strip_trailing_slashes(repo_path);
2497 break;
2498 default:
2499 usage_blame();
2500 /* NOTREACHED */
2504 argc -= optind;
2505 argv += optind;
2507 if (argc == 1)
2508 path = argv[0];
2509 else
2510 usage_blame();
2512 cwd = getcwd(NULL, 0);
2513 if (cwd == NULL) {
2514 error = got_error_from_errno("getcwd");
2515 goto done;
2517 if (repo_path == NULL) {
2518 error = got_worktree_open(&worktree, cwd);
2519 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2520 goto done;
2521 else
2522 error = NULL;
2523 if (worktree) {
2524 repo_path =
2525 strdup(got_worktree_get_repo_path(worktree));
2526 if (repo_path == NULL)
2527 error = got_error_from_errno("strdup");
2528 if (error)
2529 goto done;
2530 } else {
2531 repo_path = strdup(cwd);
2532 if (repo_path == NULL) {
2533 error = got_error_from_errno("strdup");
2534 goto done;
2539 error = got_repo_open(&repo, repo_path, NULL);
2540 if (error != NULL)
2541 goto done;
2543 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2544 if (error)
2545 goto done;
2547 if (worktree) {
2548 const char *prefix = got_worktree_get_path_prefix(worktree);
2549 char *p, *worktree_subdir = cwd +
2550 strlen(got_worktree_get_root_path(worktree));
2551 if (asprintf(&p, "%s%s%s%s%s",
2552 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
2553 worktree_subdir, worktree_subdir[0] ? "/" : "",
2554 path) == -1) {
2555 error = got_error_from_errno("asprintf");
2556 goto done;
2558 error = got_repo_map_path(&in_repo_path, repo, p, 0);
2559 free(p);
2560 } else {
2561 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2563 if (error)
2564 goto done;
2566 if (commit_id_str == NULL) {
2567 struct got_reference *head_ref;
2568 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
2569 if (error != NULL)
2570 goto done;
2571 error = got_ref_resolve(&commit_id, repo, head_ref);
2572 got_ref_close(head_ref);
2573 if (error != NULL)
2574 goto done;
2575 } else {
2576 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
2577 if (error)
2578 goto done;
2581 error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
2582 if (error)
2583 goto done;
2584 if (obj_id == NULL) {
2585 error = got_error(GOT_ERR_NO_OBJ);
2586 goto done;
2589 error = got_object_get_type(&obj_type, repo, obj_id);
2590 if (error)
2591 goto done;
2593 if (obj_type != GOT_OBJ_TYPE_BLOB) {
2594 error = got_error(GOT_ERR_OBJ_TYPE);
2595 goto done;
2598 error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
2599 if (error)
2600 goto done;
2601 bca.f = got_opentemp();
2602 if (bca.f == NULL) {
2603 error = got_error_from_errno("got_opentemp");
2604 goto done;
2606 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
2607 &bca.line_offsets, bca.f, blob);
2608 if (error || bca.nlines == 0)
2609 goto done;
2611 /* Don't include \n at EOF in the blame line count. */
2612 if (bca.line_offsets[bca.nlines - 1] == filesize)
2613 bca.nlines--;
2615 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
2616 if (bca.lines == NULL) {
2617 error = got_error_from_errno("calloc");
2618 goto done;
2620 bca.lineno_cur = 1;
2621 bca.nlines_prec = 0;
2622 i = bca.nlines;
2623 while (i > 0) {
2624 i /= 10;
2625 bca.nlines_prec++;
2627 bca.repo = repo;
2629 error = got_blame(in_repo_path, commit_id, repo, blame_cb, &bca,
2630 check_cancelled, NULL);
2631 if (error)
2632 goto done;
2633 done:
2634 free(in_repo_path);
2635 free(repo_path);
2636 free(cwd);
2637 free(commit_id);
2638 free(obj_id);
2639 if (blob)
2640 got_object_blob_close(blob);
2641 if (worktree)
2642 got_worktree_close(worktree);
2643 if (repo) {
2644 const struct got_error *repo_error;
2645 repo_error = got_repo_close(repo);
2646 if (error == NULL)
2647 error = repo_error;
2649 if (bca.lines) {
2650 for (i = 0; i < bca.nlines; i++) {
2651 struct blame_line *bline = &bca.lines[i];
2652 free(bline->id_str);
2653 free(bline->committer);
2655 free(bca.lines);
2657 free(bca.line_offsets);
2658 if (bca.f && fclose(bca.f) == EOF && error == NULL)
2659 error = got_error_from_errno("fclose");
2660 return error;
2663 __dead static void
2664 usage_tree(void)
2666 fprintf(stderr,
2667 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
2668 getprogname());
2669 exit(1);
2672 static void
2673 print_entry(struct got_tree_entry *te, const char *id, const char *path,
2674 const char *root_path)
2676 int is_root_path = (strcmp(path, root_path) == 0);
2677 const char *modestr = "";
2679 path += strlen(root_path);
2680 while (path[0] == '/')
2681 path++;
2683 if (got_object_tree_entry_is_submodule(te))
2684 modestr = "$";
2685 else if (S_ISLNK(te->mode))
2686 modestr = "@";
2687 else if (S_ISDIR(te->mode))
2688 modestr = "/";
2689 else if (te->mode & S_IXUSR)
2690 modestr = "*";
2692 printf("%s%s%s%s%s\n", id ? id : "", path,
2693 is_root_path ? "" : "/", te->name, modestr);
2696 static const struct got_error *
2697 print_tree(const char *path, struct got_object_id *commit_id,
2698 int show_ids, int recurse, const char *root_path,
2699 struct got_repository *repo)
2701 const struct got_error *err = NULL;
2702 struct got_object_id *tree_id = NULL;
2703 struct got_tree_object *tree = NULL;
2704 const struct got_tree_entries *entries;
2705 struct got_tree_entry *te;
2707 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
2708 if (err)
2709 goto done;
2711 err = got_object_open_as_tree(&tree, repo, tree_id);
2712 if (err)
2713 goto done;
2714 entries = got_object_tree_get_entries(tree);
2715 te = SIMPLEQ_FIRST(&entries->head);
2716 while (te) {
2717 char *id = NULL;
2719 if (sigint_received || sigpipe_received)
2720 break;
2722 if (show_ids) {
2723 char *id_str;
2724 err = got_object_id_str(&id_str, te->id);
2725 if (err)
2726 goto done;
2727 if (asprintf(&id, "%s ", id_str) == -1) {
2728 err = got_error_from_errno("asprintf");
2729 free(id_str);
2730 goto done;
2732 free(id_str);
2734 print_entry(te, id, path, root_path);
2735 free(id);
2737 if (recurse && S_ISDIR(te->mode)) {
2738 char *child_path;
2739 if (asprintf(&child_path, "%s%s%s", path,
2740 path[0] == '/' && path[1] == '\0' ? "" : "/",
2741 te->name) == -1) {
2742 err = got_error_from_errno("asprintf");
2743 goto done;
2745 err = print_tree(child_path, commit_id, show_ids, 1,
2746 root_path, repo);
2747 free(child_path);
2748 if (err)
2749 goto done;
2752 te = SIMPLEQ_NEXT(te, entry);
2754 done:
2755 if (tree)
2756 got_object_tree_close(tree);
2757 free(tree_id);
2758 return err;
2761 static const struct got_error *
2762 cmd_tree(int argc, char *argv[])
2764 const struct got_error *error;
2765 struct got_repository *repo = NULL;
2766 struct got_worktree *worktree = NULL;
2767 const char *path;
2768 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2769 struct got_object_id *commit_id = NULL;
2770 char *commit_id_str = NULL;
2771 int show_ids = 0, recurse = 0;
2772 int ch;
2774 #ifndef PROFILE
2775 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2776 NULL) == -1)
2777 err(1, "pledge");
2778 #endif
2780 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
2781 switch (ch) {
2782 case 'c':
2783 commit_id_str = optarg;
2784 break;
2785 case 'r':
2786 repo_path = realpath(optarg, NULL);
2787 if (repo_path == NULL)
2788 err(1, "-r option");
2789 got_path_strip_trailing_slashes(repo_path);
2790 break;
2791 case 'i':
2792 show_ids = 1;
2793 break;
2794 case 'R':
2795 recurse = 1;
2796 break;
2797 default:
2798 usage_tree();
2799 /* NOTREACHED */
2803 argc -= optind;
2804 argv += optind;
2806 if (argc == 1)
2807 path = argv[0];
2808 else if (argc > 1)
2809 usage_tree();
2810 else
2811 path = NULL;
2813 cwd = getcwd(NULL, 0);
2814 if (cwd == NULL) {
2815 error = got_error_from_errno("getcwd");
2816 goto done;
2818 if (repo_path == NULL) {
2819 error = got_worktree_open(&worktree, cwd);
2820 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2821 goto done;
2822 else
2823 error = NULL;
2824 if (worktree) {
2825 repo_path =
2826 strdup(got_worktree_get_repo_path(worktree));
2827 if (repo_path == NULL)
2828 error = got_error_from_errno("strdup");
2829 if (error)
2830 goto done;
2831 } else {
2832 repo_path = strdup(cwd);
2833 if (repo_path == NULL) {
2834 error = got_error_from_errno("strdup");
2835 goto done;
2840 error = got_repo_open(&repo, repo_path, NULL);
2841 if (error != NULL)
2842 goto done;
2844 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2845 if (error)
2846 goto done;
2848 if (path == NULL) {
2849 if (worktree) {
2850 char *p, *worktree_subdir = cwd +
2851 strlen(got_worktree_get_root_path(worktree));
2852 if (asprintf(&p, "%s/%s",
2853 got_worktree_get_path_prefix(worktree),
2854 worktree_subdir) == -1) {
2855 error = got_error_from_errno("asprintf");
2856 goto done;
2858 error = got_repo_map_path(&in_repo_path, repo, p, 1);
2859 free(p);
2860 if (error)
2861 goto done;
2862 } else
2863 path = "/";
2865 if (in_repo_path == NULL) {
2866 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2867 if (error != NULL)
2868 goto done;
2871 if (commit_id_str == NULL) {
2872 struct got_reference *head_ref;
2873 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
2874 if (error != NULL)
2875 goto done;
2876 error = got_ref_resolve(&commit_id, repo, head_ref);
2877 got_ref_close(head_ref);
2878 if (error != NULL)
2879 goto done;
2880 } else {
2881 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
2882 if (error)
2883 goto done;
2886 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
2887 in_repo_path, repo);
2888 done:
2889 free(in_repo_path);
2890 free(repo_path);
2891 free(cwd);
2892 free(commit_id);
2893 if (worktree)
2894 got_worktree_close(worktree);
2895 if (repo) {
2896 const struct got_error *repo_error;
2897 repo_error = got_repo_close(repo);
2898 if (error == NULL)
2899 error = repo_error;
2901 return error;
2904 __dead static void
2905 usage_status(void)
2907 fprintf(stderr, "usage: %s status [path ...]\n", getprogname());
2908 exit(1);
2911 static const struct got_error *
2912 print_status(void *arg, unsigned char status, unsigned char staged_status,
2913 const char *path, struct got_object_id *blob_id,
2914 struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
2916 if (status == staged_status && (status == GOT_STATUS_DELETE))
2917 status = GOT_STATUS_NO_CHANGE;
2918 printf("%c%c %s\n", status, staged_status, path);
2919 return NULL;
2922 static const struct got_error *
2923 cmd_status(int argc, char *argv[])
2925 const struct got_error *error = NULL;
2926 struct got_repository *repo = NULL;
2927 struct got_worktree *worktree = NULL;
2928 char *cwd = NULL;
2929 struct got_pathlist_head paths;
2930 struct got_pathlist_entry *pe;
2931 int ch;
2933 TAILQ_INIT(&paths);
2935 while ((ch = getopt(argc, argv, "")) != -1) {
2936 switch (ch) {
2937 default:
2938 usage_status();
2939 /* NOTREACHED */
2943 argc -= optind;
2944 argv += optind;
2946 #ifndef PROFILE
2947 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2948 NULL) == -1)
2949 err(1, "pledge");
2950 #endif
2951 cwd = getcwd(NULL, 0);
2952 if (cwd == NULL) {
2953 error = got_error_from_errno("getcwd");
2954 goto done;
2957 error = got_worktree_open(&worktree, cwd);
2958 if (error != NULL)
2959 goto done;
2961 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
2962 NULL);
2963 if (error != NULL)
2964 goto done;
2966 error = apply_unveil(got_repo_get_path(repo), 1,
2967 got_worktree_get_root_path(worktree));
2968 if (error)
2969 goto done;
2971 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
2972 if (error)
2973 goto done;
2975 error = got_worktree_status(worktree, &paths, repo, print_status, NULL,
2976 check_cancelled, NULL);
2977 done:
2978 TAILQ_FOREACH(pe, &paths, entry)
2979 free((char *)pe->path);
2980 got_pathlist_free(&paths);
2981 free(cwd);
2982 return error;
2985 __dead static void
2986 usage_ref(void)
2988 fprintf(stderr,
2989 "usage: %s ref [-r repository] -l | -d name | [-s] name target\n",
2990 getprogname());
2991 exit(1);
2994 static const struct got_error *
2995 list_refs(struct got_repository *repo)
2997 static const struct got_error *err = NULL;
2998 struct got_reflist_head refs;
2999 struct got_reflist_entry *re;
3001 SIMPLEQ_INIT(&refs);
3002 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
3003 if (err)
3004 return err;
3006 SIMPLEQ_FOREACH(re, &refs, entry) {
3007 char *refstr;
3008 refstr = got_ref_to_str(re->ref);
3009 if (refstr == NULL)
3010 return got_error_from_errno("got_ref_to_str");
3011 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
3012 free(refstr);
3015 got_ref_list_free(&refs);
3016 return NULL;
3019 static const struct got_error *
3020 delete_ref(struct got_repository *repo, const char *refname)
3022 const struct got_error *err = NULL;
3023 struct got_reference *ref;
3025 err = got_ref_open(&ref, repo, refname, 0);
3026 if (err)
3027 return err;
3029 err = got_ref_delete(ref, repo);
3030 got_ref_close(ref);
3031 return err;
3034 static const struct got_error *
3035 add_ref(struct got_repository *repo, const char *refname, const char *target)
3037 const struct got_error *err = NULL;
3038 struct got_object_id *id;
3039 struct got_reference *ref = NULL;
3042 * Don't let the user create a reference named '-'.
3043 * While technically a valid reference name, this case is usually
3044 * an unintended typo.
3046 if (refname[0] == '-' && refname[1] == '\0')
3047 return got_error_path(refname, GOT_ERR_BAD_REF_NAME);
3049 err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
3050 repo);
3051 if (err) {
3052 struct got_reference *target_ref;
3054 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
3055 return err;
3056 err = got_ref_open(&target_ref, repo, target, 0);
3057 if (err)
3058 return err;
3059 err = got_ref_resolve(&id, repo, target_ref);
3060 got_ref_close(target_ref);
3061 if (err)
3062 return err;
3065 err = got_ref_alloc(&ref, refname, id);
3066 if (err)
3067 goto done;
3069 err = got_ref_write(ref, repo);
3070 done:
3071 if (ref)
3072 got_ref_close(ref);
3073 free(id);
3074 return err;
3077 static const struct got_error *
3078 add_symref(struct got_repository *repo, const char *refname, const char *target)
3080 const struct got_error *err = NULL;
3081 struct got_reference *ref = NULL;
3082 struct got_reference *target_ref = NULL;
3085 * Don't let the user create a reference named '-'.
3086 * While technically a valid reference name, this case is usually
3087 * an unintended typo.
3089 if (refname[0] == '-' && refname[1] == '\0')
3090 return got_error_path(refname, GOT_ERR_BAD_REF_NAME);
3092 err = got_ref_open(&target_ref, repo, target, 0);
3093 if (err)
3094 return err;
3096 err = got_ref_alloc_symref(&ref, refname, target_ref);
3097 if (err)
3098 goto done;
3100 err = got_ref_write(ref, repo);
3101 done:
3102 if (target_ref)
3103 got_ref_close(target_ref);
3104 if (ref)
3105 got_ref_close(ref);
3106 return err;
3109 static const struct got_error *
3110 cmd_ref(int argc, char *argv[])
3112 const struct got_error *error = NULL;
3113 struct got_repository *repo = NULL;
3114 struct got_worktree *worktree = NULL;
3115 char *cwd = NULL, *repo_path = NULL;
3116 int ch, do_list = 0, create_symref = 0;
3117 const char *delref = NULL;
3119 /* TODO: Add -s option for adding symbolic references. */
3120 while ((ch = getopt(argc, argv, "d:r:ls")) != -1) {
3121 switch (ch) {
3122 case 'd':
3123 delref = optarg;
3124 break;
3125 case 'r':
3126 repo_path = realpath(optarg, NULL);
3127 if (repo_path == NULL)
3128 err(1, "-r option");
3129 got_path_strip_trailing_slashes(repo_path);
3130 break;
3131 case 'l':
3132 do_list = 1;
3133 break;
3134 case 's':
3135 create_symref = 1;
3136 break;
3137 default:
3138 usage_ref();
3139 /* NOTREACHED */
3143 if (do_list && delref)
3144 errx(1, "-l and -d options are mutually exclusive\n");
3146 argc -= optind;
3147 argv += optind;
3149 if (do_list || delref) {
3150 if (create_symref)
3151 errx(1, "-s option cannot be used together with the "
3152 "-l or -d options");
3153 if (argc > 0)
3154 usage_ref();
3155 } else if (argc != 2)
3156 usage_ref();
3158 #ifndef PROFILE
3159 if (do_list) {
3160 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3161 NULL) == -1)
3162 err(1, "pledge");
3163 } else {
3164 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3165 "sendfd unveil", NULL) == -1)
3166 err(1, "pledge");
3168 #endif
3169 cwd = getcwd(NULL, 0);
3170 if (cwd == NULL) {
3171 error = got_error_from_errno("getcwd");
3172 goto done;
3175 if (repo_path == NULL) {
3176 error = got_worktree_open(&worktree, cwd);
3177 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3178 goto done;
3179 else
3180 error = NULL;
3181 if (worktree) {
3182 repo_path =
3183 strdup(got_worktree_get_repo_path(worktree));
3184 if (repo_path == NULL)
3185 error = got_error_from_errno("strdup");
3186 if (error)
3187 goto done;
3188 } else {
3189 repo_path = strdup(cwd);
3190 if (repo_path == NULL) {
3191 error = got_error_from_errno("strdup");
3192 goto done;
3197 error = got_repo_open(&repo, repo_path, NULL);
3198 if (error != NULL)
3199 goto done;
3201 error = apply_unveil(got_repo_get_path(repo), do_list,
3202 worktree ? got_worktree_get_root_path(worktree) : NULL);
3203 if (error)
3204 goto done;
3206 if (do_list)
3207 error = list_refs(repo);
3208 else if (delref)
3209 error = delete_ref(repo, delref);
3210 else if (create_symref)
3211 error = add_symref(repo, argv[0], argv[1]);
3212 else
3213 error = add_ref(repo, argv[0], argv[1]);
3214 done:
3215 if (repo)
3216 got_repo_close(repo);
3217 if (worktree)
3218 got_worktree_close(worktree);
3219 free(cwd);
3220 free(repo_path);
3221 return error;
3224 __dead static void
3225 usage_branch(void)
3227 fprintf(stderr,
3228 "usage: %s branch [-r repository] [-l] | -d name | "
3229 "[name [commit]]\n", getprogname());
3230 exit(1);
3233 static const struct got_error *
3234 list_branch(struct got_repository *repo, struct got_worktree *worktree,
3235 struct got_reference *ref)
3237 const struct got_error *err = NULL;
3238 const char *refname, *marker = " ";
3239 char *refstr;
3241 refname = got_ref_get_name(ref);
3242 if (worktree && strcmp(refname,
3243 got_worktree_get_head_ref_name(worktree)) == 0) {
3244 struct got_object_id *id = NULL;
3246 err = got_ref_resolve(&id, repo, ref);
3247 if (err)
3248 return err;
3249 if (got_object_id_cmp(id,
3250 got_worktree_get_base_commit_id(worktree)) == 0)
3251 marker = "* ";
3252 else
3253 marker = "~ ";
3254 free(id);
3257 if (strncmp(refname, "refs/heads/", 11) == 0)
3258 refname += 11;
3259 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
3260 refname += 18;
3262 refstr = got_ref_to_str(ref);
3263 if (refstr == NULL)
3264 return got_error_from_errno("got_ref_to_str");
3266 printf("%s%s: %s\n", marker, refname, refstr);
3267 free(refstr);
3268 return NULL;
3271 static const struct got_error *
3272 show_current_branch(struct got_repository *repo, struct got_worktree *worktree)
3274 const char *refname;
3276 if (worktree == NULL)
3277 return got_error(GOT_ERR_NOT_WORKTREE);
3279 refname = got_worktree_get_head_ref_name(worktree);
3281 if (strncmp(refname, "refs/heads/", 11) == 0)
3282 refname += 11;
3283 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
3284 refname += 18;
3286 printf("%s\n", refname);
3288 return NULL;
3291 static const struct got_error *
3292 list_branches(struct got_repository *repo, struct got_worktree *worktree)
3294 static const struct got_error *err = NULL;
3295 struct got_reflist_head refs;
3296 struct got_reflist_entry *re;
3297 struct got_reference *temp_ref = NULL;
3298 int rebase_in_progress, histedit_in_progress;
3300 SIMPLEQ_INIT(&refs);
3302 if (worktree) {
3303 err = got_worktree_rebase_in_progress(&rebase_in_progress,
3304 worktree);
3305 if (err)
3306 return err;
3308 err = got_worktree_histedit_in_progress(&histedit_in_progress,
3309 worktree);
3310 if (err)
3311 return err;
3313 if (rebase_in_progress || histedit_in_progress) {
3314 err = got_ref_open(&temp_ref, repo,
3315 got_worktree_get_head_ref_name(worktree), 0);
3316 if (err)
3317 return err;
3318 list_branch(repo, worktree, temp_ref);
3319 got_ref_close(temp_ref);
3323 err = got_ref_list(&refs, repo, "refs/heads",
3324 got_ref_cmp_by_name, NULL);
3325 if (err)
3326 return err;
3328 SIMPLEQ_FOREACH(re, &refs, entry)
3329 list_branch(repo, worktree, re->ref);
3331 got_ref_list_free(&refs);
3332 return NULL;
3335 static const struct got_error *
3336 delete_branch(struct got_repository *repo, struct got_worktree *worktree,
3337 const char *branch_name)
3339 const struct got_error *err = NULL;
3340 struct got_reference *ref = NULL;
3341 char *refname;
3343 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
3344 return got_error_from_errno("asprintf");
3346 err = got_ref_open(&ref, repo, refname, 0);
3347 if (err)
3348 goto done;
3350 if (worktree &&
3351 strcmp(got_worktree_get_head_ref_name(worktree),
3352 got_ref_get_name(ref)) == 0) {
3353 err = got_error_msg(GOT_ERR_SAME_BRANCH,
3354 "will not delete this work tree's current branch");
3355 goto done;
3358 err = got_ref_delete(ref, repo);
3359 done:
3360 if (ref)
3361 got_ref_close(ref);
3362 free(refname);
3363 return err;
3366 static const struct got_error *
3367 add_branch(struct got_repository *repo, const char *branch_name,
3368 const char *base_branch)
3370 const struct got_error *err = NULL;
3371 struct got_object_id *id = NULL;
3372 char *label;
3373 struct got_reference *ref = NULL;
3374 char *base_refname = NULL, *refname = NULL;
3377 * Don't let the user create a branch named '-'.
3378 * While technically a valid reference name, this case is usually
3379 * an unintended typo.
3381 if (branch_name[0] == '-' && branch_name[1] == '\0')
3382 return got_error_path(branch_name, GOT_ERR_BAD_REF_NAME);
3384 err = match_object_id(&id, &label, base_branch,
3385 GOT_OBJ_TYPE_COMMIT, 1, repo);
3386 if (err)
3387 return err;
3389 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
3390 err = got_error_from_errno("asprintf");
3391 goto done;
3394 err = got_ref_open(&ref, repo, refname, 0);
3395 if (err == NULL) {
3396 err = got_error(GOT_ERR_BRANCH_EXISTS);
3397 goto done;
3398 } else if (err->code != GOT_ERR_NOT_REF)
3399 goto done;
3401 err = got_ref_alloc(&ref, refname, id);
3402 if (err)
3403 goto done;
3405 err = got_ref_write(ref, repo);
3406 done:
3407 if (ref)
3408 got_ref_close(ref);
3409 free(id);
3410 free(base_refname);
3411 free(refname);
3412 return err;
3415 static const struct got_error *
3416 cmd_branch(int argc, char *argv[])
3418 const struct got_error *error = NULL;
3419 struct got_repository *repo = NULL;
3420 struct got_worktree *worktree = NULL;
3421 char *cwd = NULL, *repo_path = NULL;
3422 int ch, do_list = 0, do_show = 0;
3423 const char *delref = NULL;
3425 while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
3426 switch (ch) {
3427 case 'd':
3428 delref = optarg;
3429 break;
3430 case 'r':
3431 repo_path = realpath(optarg, NULL);
3432 if (repo_path == NULL)
3433 err(1, "-r option");
3434 got_path_strip_trailing_slashes(repo_path);
3435 break;
3436 case 'l':
3437 do_list = 1;
3438 break;
3439 default:
3440 usage_branch();
3441 /* NOTREACHED */
3445 if (do_list && delref)
3446 errx(1, "-l and -d options are mutually exclusive\n");
3448 argc -= optind;
3449 argv += optind;
3451 if (!do_list && !delref && argc == 0)
3452 do_show = 1;
3454 if (do_list || delref) {
3455 if (argc > 0)
3456 usage_branch();
3457 } else if (!do_show && (argc < 1 || argc > 2))
3458 usage_branch();
3460 #ifndef PROFILE
3461 if (do_list || do_show) {
3462 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3463 NULL) == -1)
3464 err(1, "pledge");
3465 } else {
3466 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3467 "sendfd unveil", NULL) == -1)
3468 err(1, "pledge");
3470 #endif
3471 cwd = getcwd(NULL, 0);
3472 if (cwd == NULL) {
3473 error = got_error_from_errno("getcwd");
3474 goto done;
3477 if (repo_path == NULL) {
3478 error = got_worktree_open(&worktree, cwd);
3479 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3480 goto done;
3481 else
3482 error = NULL;
3483 if (worktree) {
3484 repo_path =
3485 strdup(got_worktree_get_repo_path(worktree));
3486 if (repo_path == NULL)
3487 error = got_error_from_errno("strdup");
3488 if (error)
3489 goto done;
3490 } else {
3491 repo_path = strdup(cwd);
3492 if (repo_path == NULL) {
3493 error = got_error_from_errno("strdup");
3494 goto done;
3499 error = got_repo_open(&repo, repo_path, NULL);
3500 if (error != NULL)
3501 goto done;
3503 error = apply_unveil(got_repo_get_path(repo), do_list,
3504 worktree ? got_worktree_get_root_path(worktree) : NULL);
3505 if (error)
3506 goto done;
3508 if (do_show)
3509 error = show_current_branch(repo, worktree);
3510 else if (do_list)
3511 error = list_branches(repo, worktree);
3512 else if (delref)
3513 error = delete_branch(repo, worktree, delref);
3514 else {
3515 const char *base_branch;
3516 if (argc == 1) {
3517 base_branch = worktree ?
3518 got_worktree_get_head_ref_name(worktree) :
3519 GOT_REF_HEAD;
3520 if (strncmp(base_branch, "refs/heads/", 11) == 0)
3521 base_branch += 11;
3522 } else
3523 base_branch = argv[1];
3524 error = add_branch(repo, argv[0], base_branch);
3526 done:
3527 if (repo)
3528 got_repo_close(repo);
3529 if (worktree)
3530 got_worktree_close(worktree);
3531 free(cwd);
3532 free(repo_path);
3533 return error;
3537 __dead static void
3538 usage_tag(void)
3540 fprintf(stderr,
3541 "usage: %s tag [-r repository] | -l | "
3542 "[-m message] name [commit]\n", getprogname());
3543 exit(1);
3546 #if 0
3547 static const struct got_error *
3548 sort_tags(struct got_reflist_head *sorted, struct got_reflist_head *tags)
3550 const struct got_error *err = NULL;
3551 struct got_reflist_entry *re, *se, *new;
3552 struct got_object_id *re_id, *se_id;
3553 struct got_tag_object *re_tag, *se_tag;
3554 time_t re_time, se_time;
3556 SIMPLEQ_FOREACH(re, tags, entry) {
3557 se = SIMPLEQ_FIRST(sorted);
3558 if (se == NULL) {
3559 err = got_reflist_entry_dup(&new, re);
3560 if (err)
3561 return err;
3562 SIMPLEQ_INSERT_HEAD(sorted, new, entry);
3563 continue;
3564 } else {
3565 err = got_ref_resolve(&re_id, repo, re->ref);
3566 if (err)
3567 break;
3568 err = got_object_open_as_tag(&re_tag, repo, re_id);
3569 free(re_id);
3570 if (err)
3571 break;
3572 re_time = got_object_tag_get_tagger_time(re_tag);
3573 got_object_tag_close(re_tag);
3576 while (se) {
3577 err = got_ref_resolve(&se_id, repo, re->ref);
3578 if (err)
3579 break;
3580 err = got_object_open_as_tag(&se_tag, repo, se_id);
3581 free(se_id);
3582 if (err)
3583 break;
3584 se_time = got_object_tag_get_tagger_time(se_tag);
3585 got_object_tag_close(se_tag);
3587 if (se_time > re_time) {
3588 err = got_reflist_entry_dup(&new, re);
3589 if (err)
3590 return err;
3591 SIMPLEQ_INSERT_AFTER(sorted, se, new, entry);
3592 break;
3594 se = SIMPLEQ_NEXT(se, entry);
3595 continue;
3598 done:
3599 return err;
3601 #endif
3603 static const struct got_error *
3604 cmp_tags(void *arg, int *cmp, struct got_reference *ref1,
3605 struct got_reference *ref2)
3607 const struct got_error *err = NULL;
3608 struct got_repository *repo = arg;
3609 struct got_object_id *id1, *id2 = NULL;
3610 struct got_tag_object *tag1 = NULL, *tag2 = NULL;
3611 time_t time1, time2;
3613 *cmp = 0;
3615 err = got_ref_resolve(&id1, repo, ref1);
3616 if (err)
3617 return err;
3618 err = got_object_open_as_tag(&tag1, repo, id1);
3619 if (err)
3620 goto done;
3622 err = got_ref_resolve(&id2, repo, ref2);
3623 if (err)
3624 goto done;
3625 err = got_object_open_as_tag(&tag2, repo, id2);
3626 if (err)
3627 goto done;
3629 time1 = got_object_tag_get_tagger_time(tag1);
3630 time2 = got_object_tag_get_tagger_time(tag2);
3632 /* Put latest tags first. */
3633 if (time1 < time2)
3634 *cmp = 1;
3635 else if (time1 > time2)
3636 *cmp = -1;
3637 else
3638 err = got_ref_cmp_by_name(NULL, cmp, ref2, ref1);
3639 done:
3640 free(id1);
3641 free(id2);
3642 if (tag1)
3643 got_object_tag_close(tag1);
3644 if (tag2)
3645 got_object_tag_close(tag2);
3646 return err;
3649 static const struct got_error *
3650 list_tags(struct got_repository *repo, struct got_worktree *worktree)
3652 static const struct got_error *err = NULL;
3653 struct got_reflist_head refs;
3654 struct got_reflist_entry *re;
3656 SIMPLEQ_INIT(&refs);
3658 err = got_ref_list(&refs, repo, "refs/tags", cmp_tags, repo);
3659 if (err)
3660 return err;
3662 SIMPLEQ_FOREACH(re, &refs, entry) {
3663 const char *refname;
3664 char *refstr, *tagmsg0, *tagmsg, *line, *id_str, *datestr;
3665 char datebuf[26];
3666 time_t tagger_time;
3667 struct got_object_id *id;
3668 struct got_tag_object *tag;
3670 refname = got_ref_get_name(re->ref);
3671 if (strncmp(refname, "refs/tags/", 10) != 0)
3672 continue;
3673 refname += 10;
3674 refstr = got_ref_to_str(re->ref);
3675 if (refstr == NULL) {
3676 err = got_error_from_errno("got_ref_to_str");
3677 break;
3679 printf("%stag %s %s\n", GOT_COMMIT_SEP_STR, refname, refstr);
3680 free(refstr);
3682 err = got_ref_resolve(&id, repo, re->ref);
3683 if (err)
3684 break;
3685 err = got_object_open_as_tag(&tag, repo, id);
3686 free(id);
3687 if (err)
3688 break;
3689 printf("from: %s\n", got_object_tag_get_tagger(tag));
3690 tagger_time = got_object_tag_get_tagger_time(tag);
3691 datestr = get_datestr(&tagger_time, datebuf);
3692 if (datestr)
3693 printf("date: %s UTC\n", datestr);
3694 err = got_object_id_str(&id_str,
3695 got_object_tag_get_object_id(tag));
3696 if (err)
3697 break;
3698 switch (got_object_tag_get_object_type(tag)) {
3699 case GOT_OBJ_TYPE_BLOB:
3700 printf("object: %s %s\n", GOT_OBJ_LABEL_BLOB, id_str);
3701 break;
3702 case GOT_OBJ_TYPE_TREE:
3703 printf("object: %s %s\n", GOT_OBJ_LABEL_TREE, id_str);
3704 break;
3705 case GOT_OBJ_TYPE_COMMIT:
3706 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
3707 break;
3708 case GOT_OBJ_TYPE_TAG:
3709 printf("object: %s %s\n", GOT_OBJ_LABEL_TAG, id_str);
3710 break;
3711 default:
3712 break;
3714 free(id_str);
3715 tagmsg0 = strdup(got_object_tag_get_message(tag));
3716 got_object_tag_close(tag);
3717 if (tagmsg0 == NULL) {
3718 err = got_error_from_errno("strdup");
3719 break;
3722 tagmsg = tagmsg0;
3723 do {
3724 line = strsep(&tagmsg, "\n");
3725 if (line)
3726 printf(" %s\n", line);
3727 } while (line);
3728 free(tagmsg0);
3731 got_ref_list_free(&refs);
3732 return NULL;
3735 static const struct got_error *
3736 get_tag_message(char **tagmsg, char **tagmsg_path, const char *commit_id_str,
3737 const char *tag_name, const char *repo_path)
3739 const struct got_error *err = NULL;
3740 char *template = NULL, *initial_content = NULL;
3741 char *editor = NULL;
3742 int fd = -1;
3744 if (asprintf(&template, "/tmp/got-tagmsg") == -1) {
3745 err = got_error_from_errno("asprintf");
3746 goto done;
3749 if (asprintf(&initial_content, "\n# tagging commit %s as %s\n",
3750 commit_id_str, tag_name) == -1) {
3751 err = got_error_from_errno("asprintf");
3752 goto done;
3755 err = got_opentemp_named_fd(tagmsg_path, &fd, template);
3756 if (err)
3757 goto done;
3759 dprintf(fd, initial_content);
3760 close(fd);
3762 err = get_editor(&editor);
3763 if (err)
3764 goto done;
3765 err = edit_logmsg(tagmsg, editor, *tagmsg_path, initial_content);
3766 done:
3767 free(initial_content);
3768 free(template);
3769 free(editor);
3771 /* Editor is done; we can now apply unveil(2) */
3772 if (err == NULL) {
3773 err = apply_unveil(repo_path, 0, NULL);
3774 if (err) {
3775 free(*tagmsg);
3776 *tagmsg = NULL;
3779 return err;
3782 static const struct got_error *
3783 add_tag(struct got_repository *repo, const char *tag_name,
3784 const char *commit_arg, const char *tagmsg_arg)
3786 const struct got_error *err = NULL;
3787 struct got_object_id *commit_id = NULL, *tag_id = NULL;
3788 char *label = NULL, *commit_id_str = NULL;
3789 struct got_reference *ref = NULL;
3790 char *refname = NULL, *tagmsg = NULL, *tagger = NULL;
3791 char *tagmsg_path = NULL, *tag_id_str = NULL;
3792 int preserve_tagmsg = 0;
3795 * Don't let the user create a tag named '-'.
3796 * While technically a valid reference name, this case is usually
3797 * an unintended typo.
3799 if (tag_name[0] == '-' && tag_name[1] == '\0')
3800 return got_error_path(tag_name, GOT_ERR_BAD_REF_NAME);
3802 err = get_author(&tagger, repo);
3803 if (err)
3804 return err;
3806 err = match_object_id(&commit_id, &label, commit_arg,
3807 GOT_OBJ_TYPE_COMMIT, 1, repo);
3808 if (err)
3809 goto done;
3811 err = got_object_id_str(&commit_id_str, commit_id);
3812 if (err)
3813 goto done;
3815 if (strncmp("refs/tags/", tag_name, 10) == 0) {
3816 refname = strdup(tag_name);
3817 if (refname == NULL) {
3818 err = got_error_from_errno("strdup");
3819 goto done;
3821 tag_name += 10;
3822 } else if (asprintf(&refname, "refs/tags/%s", tag_name) == -1) {
3823 err = got_error_from_errno("asprintf");
3824 goto done;
3827 err = got_ref_open(&ref, repo, refname, 0);
3828 if (err == NULL) {
3829 err = got_error(GOT_ERR_TAG_EXISTS);
3830 goto done;
3831 } else if (err->code != GOT_ERR_NOT_REF)
3832 goto done;
3834 if (tagmsg_arg == NULL) {
3835 err = get_tag_message(&tagmsg, &tagmsg_path, commit_id_str,
3836 tag_name, got_repo_get_path(repo));
3837 if (err) {
3838 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY &&
3839 tagmsg_path != NULL)
3840 preserve_tagmsg = 1;
3841 goto done;
3845 err = got_object_tag_create(&tag_id, tag_name, commit_id,
3846 tagger, time(NULL), tagmsg ? tagmsg : tagmsg_arg, repo);
3847 if (err) {
3848 if (tagmsg_path)
3849 preserve_tagmsg = 1;
3850 goto done;
3853 err = got_ref_alloc(&ref, refname, tag_id);
3854 if (err) {
3855 if (tagmsg_path)
3856 preserve_tagmsg = 1;
3857 goto done;
3860 err = got_ref_write(ref, repo);
3861 if (err) {
3862 if (tagmsg_path)
3863 preserve_tagmsg = 1;
3864 goto done;
3867 err = got_object_id_str(&tag_id_str, tag_id);
3868 if (err) {
3869 if (tagmsg_path)
3870 preserve_tagmsg = 1;
3871 goto done;
3873 printf("Created tag %s\n", tag_id_str);
3874 done:
3875 if (preserve_tagmsg) {
3876 fprintf(stderr, "%s: tag message preserved in %s\n",
3877 getprogname(), tagmsg_path);
3878 } else if (tagmsg_path && unlink(tagmsg_path) == -1 && err == NULL)
3879 err = got_error_from_errno2("unlink", tagmsg_path);
3880 free(tag_id_str);
3881 if (ref)
3882 got_ref_close(ref);
3883 free(commit_id);
3884 free(commit_id_str);
3885 free(refname);
3886 free(tagmsg);
3887 free(tagmsg_path);
3888 free(tagger);
3889 return err;
3892 static const struct got_error *
3893 cmd_tag(int argc, char *argv[])
3895 const struct got_error *error = NULL;
3896 struct got_repository *repo = NULL;
3897 struct got_worktree *worktree = NULL;
3898 char *cwd = NULL, *repo_path = NULL, *commit_id_str = NULL;
3899 char *gitconfig_path = NULL;
3900 const char *tag_name, *commit_id_arg = NULL, *tagmsg = NULL;
3901 int ch, do_list = 0;
3903 while ((ch = getopt(argc, argv, "m:r:l")) != -1) {
3904 switch (ch) {
3905 case 'm':
3906 tagmsg = optarg;
3907 break;
3908 case 'r':
3909 repo_path = realpath(optarg, NULL);
3910 if (repo_path == NULL)
3911 err(1, "-r option");
3912 got_path_strip_trailing_slashes(repo_path);
3913 break;
3914 case 'l':
3915 do_list = 1;
3916 break;
3917 default:
3918 usage_tag();
3919 /* NOTREACHED */
3923 argc -= optind;
3924 argv += optind;
3926 if (do_list) {
3927 if (tagmsg)
3928 errx(1, "-l and -m options are mutually exclusive\n");
3929 if (argc > 0)
3930 usage_tag();
3931 } else if (argc < 1 || argc > 2)
3932 usage_tag();
3933 else if (argc > 1)
3934 commit_id_arg = argv[1];
3935 tag_name = argv[0];
3937 #ifndef PROFILE
3938 if (do_list) {
3939 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3940 NULL) == -1)
3941 err(1, "pledge");
3942 } else {
3943 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3944 "sendfd unveil", NULL) == -1)
3945 err(1, "pledge");
3947 #endif
3948 cwd = getcwd(NULL, 0);
3949 if (cwd == NULL) {
3950 error = got_error_from_errno("getcwd");
3951 goto done;
3954 if (repo_path == NULL) {
3955 error = got_worktree_open(&worktree, cwd);
3956 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3957 goto done;
3958 else
3959 error = NULL;
3960 if (worktree) {
3961 repo_path =
3962 strdup(got_worktree_get_repo_path(worktree));
3963 if (repo_path == NULL)
3964 error = got_error_from_errno("strdup");
3965 if (error)
3966 goto done;
3967 } else {
3968 repo_path = strdup(cwd);
3969 if (repo_path == NULL) {
3970 error = got_error_from_errno("strdup");
3971 goto done;
3976 if (do_list) {
3977 error = got_repo_open(&repo, repo_path, NULL);
3978 if (error != NULL)
3979 goto done;
3980 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
3981 if (error)
3982 goto done;
3983 error = list_tags(repo, worktree);
3984 } else {
3985 error = get_gitconfig_path(&gitconfig_path);
3986 if (error)
3987 goto done;
3988 error = got_repo_open(&repo, repo_path, gitconfig_path);
3989 if (error != NULL)
3990 goto done;
3992 if (tagmsg) {
3993 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
3994 if (error)
3995 goto done;
3998 if (commit_id_arg == NULL) {
3999 struct got_reference *head_ref;
4000 struct got_object_id *commit_id;
4001 error = got_ref_open(&head_ref, repo,
4002 worktree ? got_worktree_get_head_ref_name(worktree)
4003 : GOT_REF_HEAD, 0);
4004 if (error)
4005 goto done;
4006 error = got_ref_resolve(&commit_id, repo, head_ref);
4007 got_ref_close(head_ref);
4008 if (error)
4009 goto done;
4010 error = got_object_id_str(&commit_id_str, commit_id);
4011 free(commit_id);
4012 if (error)
4013 goto done;
4016 error = add_tag(repo, tag_name,
4017 commit_id_str ? commit_id_str : commit_id_arg, tagmsg);
4019 done:
4020 if (repo)
4021 got_repo_close(repo);
4022 if (worktree)
4023 got_worktree_close(worktree);
4024 free(cwd);
4025 free(repo_path);
4026 free(gitconfig_path);
4027 free(commit_id_str);
4028 return error;
4031 __dead static void
4032 usage_add(void)
4034 fprintf(stderr, "usage: %s add file-path ...\n", getprogname());
4035 exit(1);
4038 static const struct got_error *
4039 cmd_add(int argc, char *argv[])
4041 const struct got_error *error = NULL;
4042 struct got_repository *repo = NULL;
4043 struct got_worktree *worktree = NULL;
4044 char *cwd = NULL;
4045 struct got_pathlist_head paths;
4046 struct got_pathlist_entry *pe;
4047 int ch;
4049 TAILQ_INIT(&paths);
4051 while ((ch = getopt(argc, argv, "")) != -1) {
4052 switch (ch) {
4053 default:
4054 usage_add();
4055 /* NOTREACHED */
4059 argc -= optind;
4060 argv += optind;
4062 #ifndef PROFILE
4063 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4064 NULL) == -1)
4065 err(1, "pledge");
4066 #endif
4067 if (argc < 1)
4068 usage_add();
4070 cwd = getcwd(NULL, 0);
4071 if (cwd == NULL) {
4072 error = got_error_from_errno("getcwd");
4073 goto done;
4076 error = got_worktree_open(&worktree, cwd);
4077 if (error)
4078 goto done;
4080 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4081 NULL);
4082 if (error != NULL)
4083 goto done;
4085 error = apply_unveil(got_repo_get_path(repo), 1,
4086 got_worktree_get_root_path(worktree));
4087 if (error)
4088 goto done;
4090 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4091 if (error)
4092 goto done;
4094 error = got_worktree_schedule_add(worktree, &paths, print_status,
4095 NULL, repo);
4096 done:
4097 if (repo)
4098 got_repo_close(repo);
4099 if (worktree)
4100 got_worktree_close(worktree);
4101 TAILQ_FOREACH(pe, &paths, entry)
4102 free((char *)pe->path);
4103 got_pathlist_free(&paths);
4104 free(cwd);
4105 return error;
4108 __dead static void
4109 usage_remove(void)
4111 fprintf(stderr, "usage: %s remove [-f] file-path ...\n", getprogname());
4112 exit(1);
4115 static const struct got_error *
4116 print_remove_status(void *arg, unsigned char status,
4117 unsigned char staged_status, const char *path,
4118 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4119 struct got_object_id *commit_id)
4121 if (status == GOT_STATUS_NONEXISTENT)
4122 return NULL;
4123 if (status == staged_status && (status == GOT_STATUS_DELETE))
4124 status = GOT_STATUS_NO_CHANGE;
4125 printf("%c%c %s\n", status, staged_status, path);
4126 return NULL;
4129 static const struct got_error *
4130 cmd_remove(int argc, char *argv[])
4132 const struct got_error *error = NULL;
4133 struct got_worktree *worktree = NULL;
4134 struct got_repository *repo = NULL;
4135 char *cwd = NULL;
4136 struct got_pathlist_head paths;
4137 struct got_pathlist_entry *pe;
4138 int ch, delete_local_mods = 0;
4140 TAILQ_INIT(&paths);
4142 while ((ch = getopt(argc, argv, "f")) != -1) {
4143 switch (ch) {
4144 case 'f':
4145 delete_local_mods = 1;
4146 break;
4147 default:
4148 usage_add();
4149 /* NOTREACHED */
4153 argc -= optind;
4154 argv += optind;
4156 #ifndef PROFILE
4157 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4158 NULL) == -1)
4159 err(1, "pledge");
4160 #endif
4161 if (argc < 1)
4162 usage_remove();
4164 cwd = getcwd(NULL, 0);
4165 if (cwd == NULL) {
4166 error = got_error_from_errno("getcwd");
4167 goto done;
4169 error = got_worktree_open(&worktree, cwd);
4170 if (error)
4171 goto done;
4173 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4174 NULL);
4175 if (error)
4176 goto done;
4178 error = apply_unveil(got_repo_get_path(repo), 1,
4179 got_worktree_get_root_path(worktree));
4180 if (error)
4181 goto done;
4183 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4184 if (error)
4185 goto done;
4187 error = got_worktree_schedule_delete(worktree, &paths,
4188 delete_local_mods, print_remove_status, NULL, repo);
4189 if (error)
4190 goto done;
4191 done:
4192 if (repo)
4193 got_repo_close(repo);
4194 if (worktree)
4195 got_worktree_close(worktree);
4196 TAILQ_FOREACH(pe, &paths, entry)
4197 free((char *)pe->path);
4198 got_pathlist_free(&paths);
4199 free(cwd);
4200 return error;
4203 __dead static void
4204 usage_revert(void)
4206 fprintf(stderr, "usage: %s revert [-p] [-F response-script] [-R] "
4207 "path ...\n", getprogname());
4208 exit(1);
4211 static const struct got_error *
4212 revert_progress(void *arg, unsigned char status, const char *path)
4214 while (path[0] == '/')
4215 path++;
4216 printf("%c %s\n", status, path);
4217 return NULL;
4220 struct choose_patch_arg {
4221 FILE *patch_script_file;
4222 const char *action;
4225 static const struct got_error *
4226 show_change(unsigned char status, const char *path, FILE *patch_file, int n,
4227 int nchanges, const char *action)
4229 char *line = NULL;
4230 size_t linesize = 0;
4231 ssize_t linelen;
4233 switch (status) {
4234 case GOT_STATUS_ADD:
4235 printf("A %s\n%s this addition? [y/n] ", path, action);
4236 break;
4237 case GOT_STATUS_DELETE:
4238 printf("D %s\n%s this deletion? [y/n] ", path, action);
4239 break;
4240 case GOT_STATUS_MODIFY:
4241 if (fseek(patch_file, 0L, SEEK_SET) == -1)
4242 return got_error_from_errno("fseek");
4243 printf(GOT_COMMIT_SEP_STR);
4244 while ((linelen = getline(&line, &linesize, patch_file)) != -1)
4245 printf("%s", line);
4246 if (ferror(patch_file))
4247 return got_error_from_errno("getline");
4248 printf(GOT_COMMIT_SEP_STR);
4249 printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
4250 path, n, nchanges, action);
4251 break;
4252 default:
4253 return got_error_path(path, GOT_ERR_FILE_STATUS);
4256 return NULL;
4259 static const struct got_error *
4260 choose_patch(int *choice, void *arg, unsigned char status, const char *path,
4261 FILE *patch_file, int n, int nchanges)
4263 const struct got_error *err = NULL;
4264 char *line = NULL;
4265 size_t linesize = 0;
4266 ssize_t linelen;
4267 int resp = ' ';
4268 struct choose_patch_arg *a = arg;
4270 *choice = GOT_PATCH_CHOICE_NONE;
4272 if (a->patch_script_file) {
4273 char *nl;
4274 err = show_change(status, path, patch_file, n, nchanges,
4275 a->action);
4276 if (err)
4277 return err;
4278 linelen = getline(&line, &linesize, a->patch_script_file);
4279 if (linelen == -1) {
4280 if (ferror(a->patch_script_file))
4281 return got_error_from_errno("getline");
4282 return NULL;
4284 nl = strchr(line, '\n');
4285 if (nl)
4286 *nl = '\0';
4287 if (strcmp(line, "y") == 0) {
4288 *choice = GOT_PATCH_CHOICE_YES;
4289 printf("y\n");
4290 } else if (strcmp(line, "n") == 0) {
4291 *choice = GOT_PATCH_CHOICE_NO;
4292 printf("n\n");
4293 } else if (strcmp(line, "q") == 0 &&
4294 status == GOT_STATUS_MODIFY) {
4295 *choice = GOT_PATCH_CHOICE_QUIT;
4296 printf("q\n");
4297 } else
4298 printf("invalid response '%s'\n", line);
4299 free(line);
4300 return NULL;
4303 while (resp != 'y' && resp != 'n' && resp != 'q') {
4304 err = show_change(status, path, patch_file, n, nchanges,
4305 a->action);
4306 if (err)
4307 return err;
4308 resp = getchar();
4309 if (resp == '\n')
4310 resp = getchar();
4311 if (status == GOT_STATUS_MODIFY) {
4312 if (resp != 'y' && resp != 'n' && resp != 'q') {
4313 printf("invalid response '%c'\n", resp);
4314 resp = ' ';
4316 } else if (resp != 'y' && resp != 'n') {
4317 printf("invalid response '%c'\n", resp);
4318 resp = ' ';
4322 if (resp == 'y')
4323 *choice = GOT_PATCH_CHOICE_YES;
4324 else if (resp == 'n')
4325 *choice = GOT_PATCH_CHOICE_NO;
4326 else if (resp == 'q' && status == GOT_STATUS_MODIFY)
4327 *choice = GOT_PATCH_CHOICE_QUIT;
4329 return NULL;
4333 static const struct got_error *
4334 cmd_revert(int argc, char *argv[])
4336 const struct got_error *error = NULL;
4337 struct got_worktree *worktree = NULL;
4338 struct got_repository *repo = NULL;
4339 char *cwd = NULL, *path = NULL;
4340 struct got_pathlist_head paths;
4341 struct got_pathlist_entry *pe;
4342 int ch, can_recurse = 0, pflag = 0;
4343 FILE *patch_script_file = NULL;
4344 const char *patch_script_path = NULL;
4345 struct choose_patch_arg cpa;
4347 TAILQ_INIT(&paths);
4349 while ((ch = getopt(argc, argv, "pF:R")) != -1) {
4350 switch (ch) {
4351 case 'p':
4352 pflag = 1;
4353 break;
4354 case 'F':
4355 patch_script_path = optarg;
4356 break;
4357 case 'R':
4358 can_recurse = 1;
4359 break;
4360 default:
4361 usage_revert();
4362 /* NOTREACHED */
4366 argc -= optind;
4367 argv += optind;
4369 #ifndef PROFILE
4370 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4371 "unveil", NULL) == -1)
4372 err(1, "pledge");
4373 #endif
4374 if (argc < 1)
4375 usage_revert();
4376 if (patch_script_path && !pflag)
4377 errx(1, "-F option can only be used together with -p option");
4379 cwd = getcwd(NULL, 0);
4380 if (cwd == NULL) {
4381 error = got_error_from_errno("getcwd");
4382 goto done;
4384 error = got_worktree_open(&worktree, cwd);
4385 if (error)
4386 goto done;
4388 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4389 NULL);
4390 if (error != NULL)
4391 goto done;
4393 if (patch_script_path) {
4394 patch_script_file = fopen(patch_script_path, "r");
4395 if (patch_script_file == NULL) {
4396 error = got_error_from_errno2("fopen",
4397 patch_script_path);
4398 goto done;
4401 error = apply_unveil(got_repo_get_path(repo), 1,
4402 got_worktree_get_root_path(worktree));
4403 if (error)
4404 goto done;
4406 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4407 if (error)
4408 goto done;
4410 if (!can_recurse) {
4411 char *ondisk_path;
4412 struct stat sb;
4413 TAILQ_FOREACH(pe, &paths, entry) {
4414 if (asprintf(&ondisk_path, "%s/%s",
4415 got_worktree_get_root_path(worktree),
4416 pe->path) == -1) {
4417 error = got_error_from_errno("asprintf");
4418 goto done;
4420 if (lstat(ondisk_path, &sb) == -1) {
4421 if (errno == ENOENT) {
4422 free(ondisk_path);
4423 continue;
4425 error = got_error_from_errno2("lstat",
4426 ondisk_path);
4427 free(ondisk_path);
4428 goto done;
4430 free(ondisk_path);
4431 if (S_ISDIR(sb.st_mode)) {
4432 error = got_error_msg(GOT_ERR_BAD_PATH,
4433 "reverting directories requires -R option");
4434 goto done;
4439 cpa.patch_script_file = patch_script_file;
4440 cpa.action = "revert";
4441 error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
4442 pflag ? choose_patch : NULL, &cpa, repo);
4443 if (error)
4444 goto done;
4445 done:
4446 if (patch_script_file && fclose(patch_script_file) == EOF &&
4447 error == NULL)
4448 error = got_error_from_errno2("fclose", patch_script_path);
4449 if (repo)
4450 got_repo_close(repo);
4451 if (worktree)
4452 got_worktree_close(worktree);
4453 free(path);
4454 free(cwd);
4455 return error;
4458 __dead static void
4459 usage_commit(void)
4461 fprintf(stderr, "usage: %s commit [-m msg] [path ...]\n",
4462 getprogname());
4463 exit(1);
4466 struct collect_commit_logmsg_arg {
4467 const char *cmdline_log;
4468 const char *editor;
4469 const char *worktree_path;
4470 const char *branch_name;
4471 const char *repo_path;
4472 char *logmsg_path;
4476 static const struct got_error *
4477 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
4478 void *arg)
4480 char *initial_content = NULL;
4481 struct got_pathlist_entry *pe;
4482 const struct got_error *err = NULL;
4483 char *template = NULL;
4484 struct collect_commit_logmsg_arg *a = arg;
4485 int fd;
4486 size_t len;
4488 /* if a message was specified on the command line, just use it */
4489 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
4490 len = strlen(a->cmdline_log) + 1;
4491 *logmsg = malloc(len + 1);
4492 if (*logmsg == NULL)
4493 return got_error_from_errno("malloc");
4494 strlcpy(*logmsg, a->cmdline_log, len);
4495 return NULL;
4498 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
4499 return got_error_from_errno("asprintf");
4501 if (asprintf(&initial_content,
4502 "\n# changes to be committed on branch %s:\n",
4503 a->branch_name) == -1)
4504 return got_error_from_errno("asprintf");
4506 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
4507 if (err)
4508 goto done;
4510 dprintf(fd, initial_content);
4512 TAILQ_FOREACH(pe, commitable_paths, entry) {
4513 struct got_commitable *ct = pe->data;
4514 dprintf(fd, "# %c %s\n",
4515 got_commitable_get_status(ct),
4516 got_commitable_get_path(ct));
4518 close(fd);
4520 err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content);
4521 done:
4522 free(initial_content);
4523 free(template);
4525 /* Editor is done; we can now apply unveil(2) */
4526 if (err == NULL) {
4527 err = apply_unveil(a->repo_path, 0, a->worktree_path);
4528 if (err) {
4529 free(*logmsg);
4530 *logmsg = NULL;
4533 return err;
4536 static const struct got_error *
4537 cmd_commit(int argc, char *argv[])
4539 const struct got_error *error = NULL;
4540 struct got_worktree *worktree = NULL;
4541 struct got_repository *repo = NULL;
4542 char *cwd = NULL, *id_str = NULL;
4543 struct got_object_id *id = NULL;
4544 const char *logmsg = NULL;
4545 struct collect_commit_logmsg_arg cl_arg;
4546 char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
4547 int ch, rebase_in_progress, histedit_in_progress, preserve_logmsg = 0;
4548 struct got_pathlist_head paths;
4550 TAILQ_INIT(&paths);
4551 cl_arg.logmsg_path = NULL;
4553 while ((ch = getopt(argc, argv, "m:")) != -1) {
4554 switch (ch) {
4555 case 'm':
4556 logmsg = optarg;
4557 break;
4558 default:
4559 usage_commit();
4560 /* NOTREACHED */
4564 argc -= optind;
4565 argv += optind;
4567 #ifndef PROFILE
4568 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4569 "unveil", NULL) == -1)
4570 err(1, "pledge");
4571 #endif
4572 cwd = getcwd(NULL, 0);
4573 if (cwd == NULL) {
4574 error = got_error_from_errno("getcwd");
4575 goto done;
4577 error = got_worktree_open(&worktree, cwd);
4578 if (error)
4579 goto done;
4581 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
4582 if (error)
4583 goto done;
4584 if (rebase_in_progress) {
4585 error = got_error(GOT_ERR_REBASING);
4586 goto done;
4589 error = got_worktree_histedit_in_progress(&histedit_in_progress,
4590 worktree);
4591 if (error)
4592 goto done;
4594 error = get_gitconfig_path(&gitconfig_path);
4595 if (error)
4596 goto done;
4597 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4598 gitconfig_path);
4599 if (error != NULL)
4600 goto done;
4602 error = get_author(&author, repo);
4603 if (error)
4604 return error;
4607 * unveil(2) traverses exec(2); if an editor is used we have
4608 * to apply unveil after the log message has been written.
4610 if (logmsg == NULL || strlen(logmsg) == 0)
4611 error = get_editor(&editor);
4612 else
4613 error = apply_unveil(got_repo_get_path(repo), 0,
4614 got_worktree_get_root_path(worktree));
4615 if (error)
4616 goto done;
4618 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4619 if (error)
4620 goto done;
4622 cl_arg.editor = editor;
4623 cl_arg.cmdline_log = logmsg;
4624 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
4625 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
4626 if (!histedit_in_progress) {
4627 if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
4628 error = got_error(GOT_ERR_COMMIT_BRANCH);
4629 goto done;
4631 cl_arg.branch_name += 11;
4633 cl_arg.repo_path = got_repo_get_path(repo);
4634 error = got_worktree_commit(&id, worktree, &paths, author, NULL,
4635 collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
4636 if (error) {
4637 if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
4638 cl_arg.logmsg_path != NULL)
4639 preserve_logmsg = 1;
4640 goto done;
4643 error = got_object_id_str(&id_str, id);
4644 if (error)
4645 goto done;
4646 printf("Created commit %s\n", id_str);
4647 done:
4648 if (preserve_logmsg) {
4649 fprintf(stderr, "%s: log message preserved in %s\n",
4650 getprogname(), cl_arg.logmsg_path);
4651 } else if (cl_arg.logmsg_path && unlink(cl_arg.logmsg_path) == -1 &&
4652 error == NULL)
4653 error = got_error_from_errno2("unlink", cl_arg.logmsg_path);
4654 free(cl_arg.logmsg_path);
4655 if (repo)
4656 got_repo_close(repo);
4657 if (worktree)
4658 got_worktree_close(worktree);
4659 free(cwd);
4660 free(id_str);
4661 free(gitconfig_path);
4662 free(editor);
4663 free(author);
4664 return error;
4667 __dead static void
4668 usage_cherrypick(void)
4670 fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
4671 exit(1);
4674 static const struct got_error *
4675 cmd_cherrypick(int argc, char *argv[])
4677 const struct got_error *error = NULL;
4678 struct got_worktree *worktree = NULL;
4679 struct got_repository *repo = NULL;
4680 char *cwd = NULL, *commit_id_str = NULL;
4681 struct got_object_id *commit_id = NULL;
4682 struct got_commit_object *commit = NULL;
4683 struct got_object_qid *pid;
4684 struct got_reference *head_ref = NULL;
4685 int ch, did_something = 0;
4687 while ((ch = getopt(argc, argv, "")) != -1) {
4688 switch (ch) {
4689 default:
4690 usage_cherrypick();
4691 /* NOTREACHED */
4695 argc -= optind;
4696 argv += optind;
4698 #ifndef PROFILE
4699 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4700 "unveil", NULL) == -1)
4701 err(1, "pledge");
4702 #endif
4703 if (argc != 1)
4704 usage_cherrypick();
4706 cwd = getcwd(NULL, 0);
4707 if (cwd == NULL) {
4708 error = got_error_from_errno("getcwd");
4709 goto done;
4711 error = got_worktree_open(&worktree, cwd);
4712 if (error)
4713 goto done;
4715 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4716 NULL);
4717 if (error != NULL)
4718 goto done;
4720 error = apply_unveil(got_repo_get_path(repo), 0,
4721 got_worktree_get_root_path(worktree));
4722 if (error)
4723 goto done;
4725 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
4726 GOT_OBJ_TYPE_COMMIT, repo);
4727 if (error != NULL) {
4728 struct got_reference *ref;
4729 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
4730 goto done;
4731 error = got_ref_open(&ref, repo, argv[0], 0);
4732 if (error != NULL)
4733 goto done;
4734 error = got_ref_resolve(&commit_id, repo, ref);
4735 got_ref_close(ref);
4736 if (error != NULL)
4737 goto done;
4739 error = got_object_id_str(&commit_id_str, commit_id);
4740 if (error)
4741 goto done;
4743 error = got_ref_open(&head_ref, repo,
4744 got_worktree_get_head_ref_name(worktree), 0);
4745 if (error != NULL)
4746 goto done;
4748 error = check_same_branch(commit_id, head_ref, NULL, repo);
4749 if (error) {
4750 if (error->code != GOT_ERR_ANCESTRY)
4751 goto done;
4752 error = NULL;
4753 } else {
4754 error = got_error(GOT_ERR_SAME_BRANCH);
4755 goto done;
4758 error = got_object_open_as_commit(&commit, repo, commit_id);
4759 if (error)
4760 goto done;
4761 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
4762 error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
4763 commit_id, repo, update_progress, &did_something, check_cancelled,
4764 NULL);
4765 if (error != NULL)
4766 goto done;
4768 if (did_something)
4769 printf("Merged commit %s\n", commit_id_str);
4770 done:
4771 if (commit)
4772 got_object_commit_close(commit);
4773 free(commit_id_str);
4774 if (head_ref)
4775 got_ref_close(head_ref);
4776 if (worktree)
4777 got_worktree_close(worktree);
4778 if (repo)
4779 got_repo_close(repo);
4780 return error;
4783 __dead static void
4784 usage_backout(void)
4786 fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
4787 exit(1);
4790 static const struct got_error *
4791 cmd_backout(int argc, char *argv[])
4793 const struct got_error *error = NULL;
4794 struct got_worktree *worktree = NULL;
4795 struct got_repository *repo = NULL;
4796 char *cwd = NULL, *commit_id_str = NULL;
4797 struct got_object_id *commit_id = NULL;
4798 struct got_commit_object *commit = NULL;
4799 struct got_object_qid *pid;
4800 struct got_reference *head_ref = NULL;
4801 int ch, did_something = 0;
4803 while ((ch = getopt(argc, argv, "")) != -1) {
4804 switch (ch) {
4805 default:
4806 usage_backout();
4807 /* NOTREACHED */
4811 argc -= optind;
4812 argv += optind;
4814 #ifndef PROFILE
4815 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4816 "unveil", NULL) == -1)
4817 err(1, "pledge");
4818 #endif
4819 if (argc != 1)
4820 usage_backout();
4822 cwd = getcwd(NULL, 0);
4823 if (cwd == NULL) {
4824 error = got_error_from_errno("getcwd");
4825 goto done;
4827 error = got_worktree_open(&worktree, cwd);
4828 if (error)
4829 goto done;
4831 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4832 NULL);
4833 if (error != NULL)
4834 goto done;
4836 error = apply_unveil(got_repo_get_path(repo), 0,
4837 got_worktree_get_root_path(worktree));
4838 if (error)
4839 goto done;
4841 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
4842 GOT_OBJ_TYPE_COMMIT, repo);
4843 if (error != NULL) {
4844 struct got_reference *ref;
4845 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
4846 goto done;
4847 error = got_ref_open(&ref, repo, argv[0], 0);
4848 if (error != NULL)
4849 goto done;
4850 error = got_ref_resolve(&commit_id, repo, ref);
4851 got_ref_close(ref);
4852 if (error != NULL)
4853 goto done;
4855 error = got_object_id_str(&commit_id_str, commit_id);
4856 if (error)
4857 goto done;
4859 error = got_ref_open(&head_ref, repo,
4860 got_worktree_get_head_ref_name(worktree), 0);
4861 if (error != NULL)
4862 goto done;
4864 error = check_same_branch(commit_id, head_ref, NULL, repo);
4865 if (error)
4866 goto done;
4868 error = got_object_open_as_commit(&commit, repo, commit_id);
4869 if (error)
4870 goto done;
4871 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
4872 if (pid == NULL) {
4873 error = got_error(GOT_ERR_ROOT_COMMIT);
4874 goto done;
4877 error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
4878 update_progress, &did_something, check_cancelled, NULL);
4879 if (error != NULL)
4880 goto done;
4882 if (did_something)
4883 printf("Backed out commit %s\n", commit_id_str);
4884 done:
4885 if (commit)
4886 got_object_commit_close(commit);
4887 free(commit_id_str);
4888 if (head_ref)
4889 got_ref_close(head_ref);
4890 if (worktree)
4891 got_worktree_close(worktree);
4892 if (repo)
4893 got_repo_close(repo);
4894 return error;
4897 __dead static void
4898 usage_rebase(void)
4900 fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
4901 getprogname());
4902 exit(1);
4905 void
4906 trim_logmsg(char *logmsg, int limit)
4908 char *nl;
4909 size_t len;
4911 len = strlen(logmsg);
4912 if (len > limit)
4913 len = limit;
4914 logmsg[len] = '\0';
4915 nl = strchr(logmsg, '\n');
4916 if (nl)
4917 *nl = '\0';
4920 static const struct got_error *
4921 get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
4923 const struct got_error *err;
4924 char *logmsg0 = NULL;
4925 const char *s;
4927 err = got_object_commit_get_logmsg(&logmsg0, commit);
4928 if (err)
4929 return err;
4931 s = logmsg0;
4932 while (isspace((unsigned char)s[0]))
4933 s++;
4935 *logmsg = strdup(s);
4936 if (*logmsg == NULL) {
4937 err = got_error_from_errno("strdup");
4938 goto done;
4941 trim_logmsg(*logmsg, limit);
4942 done:
4943 free(logmsg0);
4944 return err;
4947 static const struct got_error *
4948 show_rebase_progress(struct got_commit_object *commit,
4949 struct got_object_id *old_id, struct got_object_id *new_id)
4951 const struct got_error *err;
4952 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
4954 err = got_object_id_str(&old_id_str, old_id);
4955 if (err)
4956 goto done;
4958 if (new_id) {
4959 err = got_object_id_str(&new_id_str, new_id);
4960 if (err)
4961 goto done;
4964 old_id_str[12] = '\0';
4965 if (new_id_str)
4966 new_id_str[12] = '\0';
4968 err = get_short_logmsg(&logmsg, 42, commit);
4969 if (err)
4970 goto done;
4972 printf("%s -> %s: %s\n", old_id_str,
4973 new_id_str ? new_id_str : "no-op change", logmsg);
4974 done:
4975 free(old_id_str);
4976 free(new_id_str);
4977 return err;
4980 static const struct got_error *
4981 rebase_progress(void *arg, unsigned char status, const char *path)
4983 unsigned char *rebase_status = arg;
4985 while (path[0] == '/')
4986 path++;
4987 printf("%c %s\n", status, path);
4989 if (*rebase_status == GOT_STATUS_CONFLICT)
4990 return NULL;
4991 if (status == GOT_STATUS_CONFLICT || status == GOT_STATUS_MERGE)
4992 *rebase_status = status;
4993 return NULL;
4996 static const struct got_error *
4997 rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
4998 struct got_reference *branch, struct got_reference *new_base_branch,
4999 struct got_reference *tmp_branch, struct got_repository *repo)
5001 printf("Switching work tree to %s\n", got_ref_get_name(branch));
5002 return got_worktree_rebase_complete(worktree, fileindex,
5003 new_base_branch, tmp_branch, branch, repo);
5006 static const struct got_error *
5007 rebase_commit(struct got_pathlist_head *merged_paths,
5008 struct got_worktree *worktree, struct got_fileindex *fileindex,
5009 struct got_reference *tmp_branch,
5010 struct got_object_id *commit_id, struct got_repository *repo)
5012 const struct got_error *error;
5013 struct got_commit_object *commit;
5014 struct got_object_id *new_commit_id;
5016 error = got_object_open_as_commit(&commit, repo, commit_id);
5017 if (error)
5018 return error;
5020 error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
5021 worktree, fileindex, tmp_branch, commit, commit_id, repo);
5022 if (error) {
5023 if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
5024 goto done;
5025 error = show_rebase_progress(commit, commit_id, NULL);
5026 } else {
5027 error = show_rebase_progress(commit, commit_id, new_commit_id);
5028 free(new_commit_id);
5030 done:
5031 got_object_commit_close(commit);
5032 return error;
5035 struct check_path_prefix_arg {
5036 const char *path_prefix;
5037 size_t len;
5038 int errcode;
5041 static const struct got_error *
5042 check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
5043 struct got_blob_object *blob2, struct got_object_id *id1,
5044 struct got_object_id *id2, const char *path1, const char *path2,
5045 mode_t mode1, mode_t mode2, struct got_repository *repo)
5047 struct check_path_prefix_arg *a = arg;
5049 if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
5050 (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
5051 return got_error(a->errcode);
5053 return NULL;
5056 static const struct got_error *
5057 check_path_prefix(struct got_object_id *parent_id,
5058 struct got_object_id *commit_id, const char *path_prefix,
5059 int errcode, struct got_repository *repo)
5061 const struct got_error *err;
5062 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
5063 struct got_commit_object *commit = NULL, *parent_commit = NULL;
5064 struct check_path_prefix_arg cpp_arg;
5066 if (got_path_is_root_dir(path_prefix))
5067 return NULL;
5069 err = got_object_open_as_commit(&commit, repo, commit_id);
5070 if (err)
5071 goto done;
5073 err = got_object_open_as_commit(&parent_commit, repo, parent_id);
5074 if (err)
5075 goto done;
5077 err = got_object_open_as_tree(&tree1, repo,
5078 got_object_commit_get_tree_id(parent_commit));
5079 if (err)
5080 goto done;
5082 err = got_object_open_as_tree(&tree2, repo,
5083 got_object_commit_get_tree_id(commit));
5084 if (err)
5085 goto done;
5087 cpp_arg.path_prefix = path_prefix;
5088 while (cpp_arg.path_prefix[0] == '/')
5089 cpp_arg.path_prefix++;
5090 cpp_arg.len = strlen(cpp_arg.path_prefix);
5091 cpp_arg.errcode = errcode;
5092 err = got_diff_tree(tree1, tree2, "", "", repo,
5093 check_path_prefix_in_diff, &cpp_arg, 0);
5094 done:
5095 if (tree1)
5096 got_object_tree_close(tree1);
5097 if (tree2)
5098 got_object_tree_close(tree2);
5099 if (commit)
5100 got_object_commit_close(commit);
5101 if (parent_commit)
5102 got_object_commit_close(parent_commit);
5103 return err;
5106 static const struct got_error *
5107 collect_commits(struct got_object_id_queue *commits,
5108 struct got_object_id *initial_commit_id,
5109 struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
5110 const char *path_prefix, int path_prefix_errcode,
5111 struct got_repository *repo)
5113 const struct got_error *err = NULL;
5114 struct got_commit_graph *graph = NULL;
5115 struct got_object_id *parent_id = NULL;
5116 struct got_object_qid *qid;
5117 struct got_object_id *commit_id = initial_commit_id;
5119 err = got_commit_graph_open(&graph, initial_commit_id, "/", 1, repo);
5120 if (err)
5121 return err;
5123 err = got_commit_graph_iter_start(graph, iter_start_id, repo,
5124 check_cancelled, NULL);
5125 if (err)
5126 goto done;
5127 while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
5128 err = got_commit_graph_iter_next(&parent_id, graph);
5129 if (err) {
5130 if (err->code == GOT_ERR_ITER_COMPLETED) {
5131 err = got_error_msg(GOT_ERR_ANCESTRY,
5132 "ran out of commits to rebase before "
5133 "youngest common ancestor commit has "
5134 "been reached?!?");
5135 goto done;
5136 } else if (err->code != GOT_ERR_ITER_NEED_MORE)
5137 goto done;
5138 err = got_commit_graph_fetch_commits(graph, 1, repo,
5139 check_cancelled, NULL);
5140 if (err)
5141 goto done;
5142 } else {
5143 err = check_path_prefix(parent_id, commit_id,
5144 path_prefix, path_prefix_errcode, repo);
5145 if (err)
5146 goto done;
5148 err = got_object_qid_alloc(&qid, commit_id);
5149 if (err)
5150 goto done;
5151 SIMPLEQ_INSERT_HEAD(commits, qid, entry);
5152 commit_id = parent_id;
5155 done:
5156 got_commit_graph_close(graph);
5157 return err;
5160 static const struct got_error *
5161 cmd_rebase(int argc, char *argv[])
5163 const struct got_error *error = NULL;
5164 struct got_worktree *worktree = NULL;
5165 struct got_repository *repo = NULL;
5166 struct got_fileindex *fileindex = NULL;
5167 char *cwd = NULL;
5168 struct got_reference *branch = NULL;
5169 struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
5170 struct got_object_id *commit_id = NULL, *parent_id = NULL;
5171 struct got_object_id *resume_commit_id = NULL;
5172 struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
5173 struct got_commit_object *commit = NULL;
5174 int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
5175 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
5176 struct got_object_id_queue commits;
5177 struct got_pathlist_head merged_paths;
5178 const struct got_object_id_queue *parent_ids;
5179 struct got_object_qid *qid, *pid;
5181 SIMPLEQ_INIT(&commits);
5182 TAILQ_INIT(&merged_paths);
5184 while ((ch = getopt(argc, argv, "ac")) != -1) {
5185 switch (ch) {
5186 case 'a':
5187 abort_rebase = 1;
5188 break;
5189 case 'c':
5190 continue_rebase = 1;
5191 break;
5192 default:
5193 usage_rebase();
5194 /* NOTREACHED */
5198 argc -= optind;
5199 argv += optind;
5201 #ifndef PROFILE
5202 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5203 "unveil", NULL) == -1)
5204 err(1, "pledge");
5205 #endif
5206 if (abort_rebase && continue_rebase)
5207 usage_rebase();
5208 else if (abort_rebase || continue_rebase) {
5209 if (argc != 0)
5210 usage_rebase();
5211 } else if (argc != 1)
5212 usage_rebase();
5214 cwd = getcwd(NULL, 0);
5215 if (cwd == NULL) {
5216 error = got_error_from_errno("getcwd");
5217 goto done;
5219 error = got_worktree_open(&worktree, cwd);
5220 if (error)
5221 goto done;
5223 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5224 NULL);
5225 if (error != NULL)
5226 goto done;
5228 error = apply_unveil(got_repo_get_path(repo), 0,
5229 got_worktree_get_root_path(worktree));
5230 if (error)
5231 goto done;
5233 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
5234 if (error)
5235 goto done;
5237 if (abort_rebase) {
5238 int did_something;
5239 if (!rebase_in_progress) {
5240 error = got_error(GOT_ERR_NOT_REBASING);
5241 goto done;
5243 error = got_worktree_rebase_continue(&resume_commit_id,
5244 &new_base_branch, &tmp_branch, &branch, &fileindex,
5245 worktree, repo);
5246 if (error)
5247 goto done;
5248 printf("Switching work tree to %s\n",
5249 got_ref_get_symref_target(new_base_branch));
5250 error = got_worktree_rebase_abort(worktree, fileindex, repo,
5251 new_base_branch, update_progress, &did_something);
5252 if (error)
5253 goto done;
5254 printf("Rebase of %s aborted\n", got_ref_get_name(branch));
5255 goto done; /* nothing else to do */
5258 if (continue_rebase) {
5259 if (!rebase_in_progress) {
5260 error = got_error(GOT_ERR_NOT_REBASING);
5261 goto done;
5263 error = got_worktree_rebase_continue(&resume_commit_id,
5264 &new_base_branch, &tmp_branch, &branch, &fileindex,
5265 worktree, repo);
5266 if (error)
5267 goto done;
5269 error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
5270 resume_commit_id, repo);
5271 if (error)
5272 goto done;
5274 yca_id = got_object_id_dup(resume_commit_id);
5275 if (yca_id == NULL) {
5276 error = got_error_from_errno("got_object_id_dup");
5277 goto done;
5279 } else {
5280 error = got_ref_open(&branch, repo, argv[0], 0);
5281 if (error != NULL)
5282 goto done;
5285 error = got_ref_resolve(&branch_head_commit_id, repo, branch);
5286 if (error)
5287 goto done;
5289 if (!continue_rebase) {
5290 struct got_object_id *base_commit_id;
5292 base_commit_id = got_worktree_get_base_commit_id(worktree);
5293 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
5294 base_commit_id, branch_head_commit_id, repo,
5295 check_cancelled, NULL);
5296 if (error)
5297 goto done;
5298 if (yca_id == NULL) {
5299 error = got_error_msg(GOT_ERR_ANCESTRY,
5300 "specified branch shares no common ancestry "
5301 "with work tree's branch");
5302 goto done;
5305 error = check_same_branch(base_commit_id, branch, yca_id, repo);
5306 if (error) {
5307 if (error->code != GOT_ERR_ANCESTRY)
5308 goto done;
5309 error = NULL;
5310 } else {
5311 error = got_error_msg(GOT_ERR_SAME_BRANCH,
5312 "specified branch resolves to a commit which "
5313 "is already contained in work tree's branch");
5314 goto done;
5316 error = got_worktree_rebase_prepare(&new_base_branch,
5317 &tmp_branch, &fileindex, worktree, branch, repo);
5318 if (error)
5319 goto done;
5322 commit_id = branch_head_commit_id;
5323 error = got_object_open_as_commit(&commit, repo, commit_id);
5324 if (error)
5325 goto done;
5327 parent_ids = got_object_commit_get_parent_ids(commit);
5328 pid = SIMPLEQ_FIRST(parent_ids);
5329 if (pid == NULL) {
5330 if (!continue_rebase) {
5331 int did_something;
5332 error = got_worktree_rebase_abort(worktree, fileindex,
5333 repo, new_base_branch, update_progress,
5334 &did_something);
5335 if (error)
5336 goto done;
5337 printf("Rebase of %s aborted\n",
5338 got_ref_get_name(branch));
5340 error = got_error(GOT_ERR_EMPTY_REBASE);
5341 goto done;
5343 error = collect_commits(&commits, commit_id, pid->id,
5344 yca_id, got_worktree_get_path_prefix(worktree),
5345 GOT_ERR_REBASE_PATH, repo);
5346 got_object_commit_close(commit);
5347 commit = NULL;
5348 if (error)
5349 goto done;
5351 if (SIMPLEQ_EMPTY(&commits)) {
5352 if (continue_rebase)
5353 error = rebase_complete(worktree, fileindex,
5354 branch, new_base_branch, tmp_branch, repo);
5355 else
5356 error = got_error(GOT_ERR_EMPTY_REBASE);
5357 goto done;
5360 pid = NULL;
5361 SIMPLEQ_FOREACH(qid, &commits, entry) {
5362 commit_id = qid->id;
5363 parent_id = pid ? pid->id : yca_id;
5364 pid = qid;
5366 error = got_worktree_rebase_merge_files(&merged_paths,
5367 worktree, fileindex, parent_id, commit_id, repo,
5368 rebase_progress, &rebase_status, check_cancelled, NULL);
5369 if (error)
5370 goto done;
5372 if (rebase_status == GOT_STATUS_CONFLICT) {
5373 got_worktree_rebase_pathlist_free(&merged_paths);
5374 break;
5377 error = rebase_commit(&merged_paths, worktree, fileindex,
5378 tmp_branch, commit_id, repo);
5379 got_worktree_rebase_pathlist_free(&merged_paths);
5380 if (error)
5381 goto done;
5384 if (rebase_status == GOT_STATUS_CONFLICT) {
5385 error = got_worktree_rebase_postpone(worktree, fileindex);
5386 if (error)
5387 goto done;
5388 error = got_error_msg(GOT_ERR_CONFLICTS,
5389 "conflicts must be resolved before rebasing can continue");
5390 } else
5391 error = rebase_complete(worktree, fileindex, branch,
5392 new_base_branch, tmp_branch, repo);
5393 done:
5394 got_object_id_queue_free(&commits);
5395 free(branch_head_commit_id);
5396 free(resume_commit_id);
5397 free(yca_id);
5398 if (commit)
5399 got_object_commit_close(commit);
5400 if (branch)
5401 got_ref_close(branch);
5402 if (new_base_branch)
5403 got_ref_close(new_base_branch);
5404 if (tmp_branch)
5405 got_ref_close(tmp_branch);
5406 if (worktree)
5407 got_worktree_close(worktree);
5408 if (repo)
5409 got_repo_close(repo);
5410 return error;
5413 __dead static void
5414 usage_histedit(void)
5416 fprintf(stderr, "usage: %s histedit [-a] [-c] [-F histedit-script]\n",
5417 getprogname());
5418 exit(1);
5421 #define GOT_HISTEDIT_PICK 'p'
5422 #define GOT_HISTEDIT_EDIT 'e'
5423 #define GOT_HISTEDIT_FOLD 'f'
5424 #define GOT_HISTEDIT_DROP 'd'
5425 #define GOT_HISTEDIT_MESG 'm'
5427 static struct got_histedit_cmd {
5428 unsigned char code;
5429 const char *name;
5430 const char *desc;
5431 } got_histedit_cmds[] = {
5432 { GOT_HISTEDIT_PICK, "pick", "use commit" },
5433 { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
5434 { GOT_HISTEDIT_FOLD, "fold", "combine with commit below" },
5435 { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
5436 { GOT_HISTEDIT_MESG, "mesg",
5437 "single-line log message for commit above (open editor if empty)" },
5440 struct got_histedit_list_entry {
5441 TAILQ_ENTRY(got_histedit_list_entry) entry;
5442 struct got_object_id *commit_id;
5443 const struct got_histedit_cmd *cmd;
5444 char *logmsg;
5446 TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
5448 static const struct got_error *
5449 histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
5450 FILE *f, struct got_repository *repo)
5452 const struct got_error *err = NULL;
5453 char *logmsg = NULL, *id_str = NULL;
5454 struct got_commit_object *commit = NULL;
5455 int n;
5457 err = got_object_open_as_commit(&commit, repo, commit_id);
5458 if (err)
5459 goto done;
5461 err = get_short_logmsg(&logmsg, 34, commit);
5462 if (err)
5463 goto done;
5465 err = got_object_id_str(&id_str, commit_id);
5466 if (err)
5467 goto done;
5469 n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
5470 if (n < 0)
5471 err = got_ferror(f, GOT_ERR_IO);
5472 done:
5473 if (commit)
5474 got_object_commit_close(commit);
5475 free(id_str);
5476 free(logmsg);
5477 return err;
5480 static const struct got_error *
5481 histedit_write_commit_list(struct got_object_id_queue *commits, FILE *f,
5482 struct got_repository *repo)
5484 const struct got_error *err = NULL;
5485 struct got_object_qid *qid;
5487 if (SIMPLEQ_EMPTY(commits))
5488 return got_error(GOT_ERR_EMPTY_HISTEDIT);
5490 SIMPLEQ_FOREACH(qid, commits, entry) {
5491 err = histedit_write_commit(qid->id, got_histedit_cmds[0].name,
5492 f, repo);
5493 if (err)
5494 break;
5497 return err;
5500 static const struct got_error *
5501 write_cmd_list(FILE *f)
5503 const struct got_error *err = NULL;
5504 int n, i;
5506 n = fprintf(f, "# Available histedit commands:\n");
5507 if (n < 0)
5508 return got_ferror(f, GOT_ERR_IO);
5510 for (i = 0; i < nitems(got_histedit_cmds); i++) {
5511 struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
5512 n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
5513 cmd->desc);
5514 if (n < 0) {
5515 err = got_ferror(f, GOT_ERR_IO);
5516 break;
5519 n = fprintf(f, "# Commits will be processed in order from top to "
5520 "bottom of this file.\n");
5521 if (n < 0)
5522 return got_ferror(f, GOT_ERR_IO);
5523 return err;
5526 static const struct got_error *
5527 histedit_syntax_error(int lineno)
5529 static char msg[42];
5530 int ret;
5532 ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
5533 lineno);
5534 if (ret == -1 || ret >= sizeof(msg))
5535 return got_error(GOT_ERR_HISTEDIT_SYNTAX);
5537 return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
5540 static const struct got_error *
5541 append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
5542 char *logmsg, struct got_repository *repo)
5544 const struct got_error *err;
5545 struct got_commit_object *folded_commit = NULL;
5546 char *id_str, *folded_logmsg = NULL;
5548 err = got_object_id_str(&id_str, hle->commit_id);
5549 if (err)
5550 return err;
5552 err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
5553 if (err)
5554 goto done;
5556 err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
5557 if (err)
5558 goto done;
5559 if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
5560 logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
5561 folded_logmsg) == -1) {
5562 err = got_error_from_errno("asprintf");
5563 goto done;
5565 done:
5566 if (folded_commit)
5567 got_object_commit_close(folded_commit);
5568 free(id_str);
5569 free(folded_logmsg);
5570 return err;
5573 static struct got_histedit_list_entry *
5574 get_folded_commits(struct got_histedit_list_entry *hle)
5576 struct got_histedit_list_entry *prev, *folded = NULL;
5578 prev = TAILQ_PREV(hle, got_histedit_list, entry);
5579 while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
5580 prev->cmd->code == GOT_HISTEDIT_DROP)) {
5581 if (prev->cmd->code == GOT_HISTEDIT_FOLD)
5582 folded = prev;
5583 prev = TAILQ_PREV(prev, got_histedit_list, entry);
5586 return folded;
5589 static const struct got_error *
5590 histedit_edit_logmsg(struct got_histedit_list_entry *hle,
5591 struct got_repository *repo)
5593 char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
5594 char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
5595 const struct got_error *err = NULL;
5596 struct got_commit_object *commit = NULL;
5597 int fd;
5598 struct got_histedit_list_entry *folded = NULL;
5600 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
5601 if (err)
5602 return err;
5604 folded = get_folded_commits(hle);
5605 if (folded) {
5606 while (folded != hle) {
5607 if (folded->cmd->code == GOT_HISTEDIT_DROP) {
5608 folded = TAILQ_NEXT(folded, entry);
5609 continue;
5611 err = append_folded_commit_msg(&new_msg, folded,
5612 logmsg, repo);
5613 if (err)
5614 goto done;
5615 free(logmsg);
5616 logmsg = new_msg;
5617 folded = TAILQ_NEXT(folded, entry);
5621 err = got_object_id_str(&id_str, hle->commit_id);
5622 if (err)
5623 goto done;
5624 err = got_object_commit_get_logmsg(&orig_logmsg, commit);
5625 if (err)
5626 goto done;
5627 if (asprintf(&new_msg,
5628 "%s\n# original log message of commit %s: %s",
5629 logmsg ? logmsg : "", id_str, orig_logmsg) == -1) {
5630 err = got_error_from_errno("asprintf");
5631 goto done;
5633 free(logmsg);
5634 logmsg = new_msg;
5636 err = got_object_id_str(&id_str, hle->commit_id);
5637 if (err)
5638 goto done;
5640 err = got_opentemp_named_fd(&logmsg_path, &fd, "/tmp/got-logmsg");
5641 if (err)
5642 goto done;
5644 dprintf(fd, logmsg);
5645 close(fd);
5647 err = get_editor(&editor);
5648 if (err)
5649 goto done;
5651 err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg);
5652 if (err) {
5653 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
5654 goto done;
5655 err = got_object_commit_get_logmsg(&hle->logmsg, commit);
5657 done:
5658 if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
5659 err = got_error_from_errno2("unlink", logmsg_path);
5660 free(logmsg_path);
5661 free(logmsg);
5662 free(orig_logmsg);
5663 free(editor);
5664 if (commit)
5665 got_object_commit_close(commit);
5666 return err;
5669 static const struct got_error *
5670 histedit_parse_list(struct got_histedit_list *histedit_cmds,
5671 FILE *f, struct got_repository *repo)
5673 const struct got_error *err = NULL;
5674 char *line = NULL, *p, *end;
5675 size_t size;
5676 ssize_t len;
5677 int lineno = 0, i;
5678 const struct got_histedit_cmd *cmd;
5679 struct got_object_id *commit_id = NULL;
5680 struct got_histedit_list_entry *hle = NULL;
5682 for (;;) {
5683 len = getline(&line, &size, f);
5684 if (len == -1) {
5685 const struct got_error *getline_err;
5686 if (feof(f))
5687 break;
5688 getline_err = got_error_from_errno("getline");
5689 err = got_ferror(f, getline_err->code);
5690 break;
5692 lineno++;
5693 p = line;
5694 while (isspace((unsigned char)p[0]))
5695 p++;
5696 if (p[0] == '#' || p[0] == '\0') {
5697 free(line);
5698 line = NULL;
5699 continue;
5701 cmd = NULL;
5702 for (i = 0; i < nitems(got_histedit_cmds); i++) {
5703 cmd = &got_histedit_cmds[i];
5704 if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
5705 isspace((unsigned char)p[strlen(cmd->name)])) {
5706 p += strlen(cmd->name);
5707 break;
5709 if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
5710 p++;
5711 break;
5714 if (i == nitems(got_histedit_cmds)) {
5715 err = histedit_syntax_error(lineno);
5716 break;
5718 while (isspace((unsigned char)p[0]))
5719 p++;
5720 if (cmd->code == GOT_HISTEDIT_MESG) {
5721 if (hle == NULL || hle->logmsg != NULL) {
5722 err = got_error(GOT_ERR_HISTEDIT_CMD);
5723 break;
5725 if (p[0] == '\0') {
5726 err = histedit_edit_logmsg(hle, repo);
5727 if (err)
5728 break;
5729 } else {
5730 hle->logmsg = strdup(p);
5731 if (hle->logmsg == NULL) {
5732 err = got_error_from_errno("strdup");
5733 break;
5736 free(line);
5737 line = NULL;
5738 continue;
5739 } else {
5740 end = p;
5741 while (end[0] && !isspace((unsigned char)end[0]))
5742 end++;
5743 *end = '\0';
5745 err = got_object_resolve_id_str(&commit_id, repo, p);
5746 if (err) {
5747 /* override error code */
5748 err = histedit_syntax_error(lineno);
5749 break;
5752 hle = malloc(sizeof(*hle));
5753 if (hle == NULL) {
5754 err = got_error_from_errno("malloc");
5755 break;
5757 hle->cmd = cmd;
5758 hle->commit_id = commit_id;
5759 hle->logmsg = NULL;
5760 commit_id = NULL;
5761 free(line);
5762 line = NULL;
5763 TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
5766 free(line);
5767 free(commit_id);
5768 return err;
5771 static const struct got_error *
5772 histedit_check_script(struct got_histedit_list *histedit_cmds,
5773 struct got_object_id_queue *commits, struct got_repository *repo)
5775 const struct got_error *err = NULL;
5776 struct got_object_qid *qid;
5777 struct got_histedit_list_entry *hle;
5778 static char msg[80];
5779 char *id_str;
5781 if (TAILQ_EMPTY(histedit_cmds))
5782 return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
5783 "histedit script contains no commands");
5784 if (SIMPLEQ_EMPTY(commits))
5785 return got_error(GOT_ERR_EMPTY_HISTEDIT);
5787 SIMPLEQ_FOREACH(qid, commits, entry) {
5788 TAILQ_FOREACH(hle, histedit_cmds, entry) {
5789 if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
5790 break;
5792 if (hle == NULL) {
5793 err = got_object_id_str(&id_str, qid->id);
5794 if (err)
5795 return err;
5796 snprintf(msg, sizeof(msg),
5797 "commit %s missing from histedit script", id_str);
5798 free(id_str);
5799 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
5803 hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
5804 if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
5805 return got_error_msg(GOT_ERR_HISTEDIT_CMD,
5806 "last commit in histedit script cannot be folded");
5808 return NULL;
5811 static const struct got_error *
5812 histedit_run_editor(struct got_histedit_list *histedit_cmds,
5813 const char *path, struct got_object_id_queue *commits,
5814 struct got_repository *repo)
5816 const struct got_error *err = NULL;
5817 char *editor;
5818 FILE *f = NULL;
5820 err = get_editor(&editor);
5821 if (err)
5822 return err;
5824 if (spawn_editor(editor, path) == -1) {
5825 err = got_error_from_errno("failed spawning editor");
5826 goto done;
5829 f = fopen(path, "r");
5830 if (f == NULL) {
5831 err = got_error_from_errno("fopen");
5832 goto done;
5834 err = histedit_parse_list(histedit_cmds, f, repo);
5835 if (err)
5836 goto done;
5838 err = histedit_check_script(histedit_cmds, commits, repo);
5839 done:
5840 if (f && fclose(f) != 0 && err == NULL)
5841 err = got_error_from_errno("fclose");
5842 free(editor);
5843 return err;
5846 static const struct got_error *
5847 histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
5848 struct got_object_id_queue *, const char *, struct got_repository *);
5850 static const struct got_error *
5851 histedit_edit_script(struct got_histedit_list *histedit_cmds,
5852 struct got_object_id_queue *commits, struct got_repository *repo)
5854 const struct got_error *err;
5855 FILE *f = NULL;
5856 char *path = NULL;
5858 err = got_opentemp_named(&path, &f, "got-histedit");
5859 if (err)
5860 return err;
5862 err = write_cmd_list(f);
5863 if (err)
5864 goto done;
5866 err = histedit_write_commit_list(commits, f, repo);
5867 if (err)
5868 goto done;
5870 if (fclose(f) != 0) {
5871 err = got_error_from_errno("fclose");
5872 goto done;
5874 f = NULL;
5876 err = histedit_run_editor(histedit_cmds, path, commits, repo);
5877 if (err) {
5878 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
5879 err->code != GOT_ERR_HISTEDIT_CMD)
5880 goto done;
5881 err = histedit_edit_list_retry(histedit_cmds, err,
5882 commits, path, repo);
5884 done:
5885 if (f && fclose(f) != 0 && err == NULL)
5886 err = got_error_from_errno("fclose");
5887 if (path && unlink(path) != 0 && err == NULL)
5888 err = got_error_from_errno2("unlink", path);
5889 free(path);
5890 return err;
5893 static const struct got_error *
5894 histedit_save_list(struct got_histedit_list *histedit_cmds,
5895 struct got_worktree *worktree, struct got_repository *repo)
5897 const struct got_error *err = NULL;
5898 char *path = NULL;
5899 FILE *f = NULL;
5900 struct got_histedit_list_entry *hle;
5901 struct got_commit_object *commit = NULL;
5903 err = got_worktree_get_histedit_script_path(&path, worktree);
5904 if (err)
5905 return err;
5907 f = fopen(path, "w");
5908 if (f == NULL) {
5909 err = got_error_from_errno2("fopen", path);
5910 goto done;
5912 TAILQ_FOREACH(hle, histedit_cmds, entry) {
5913 err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
5914 repo);
5915 if (err)
5916 break;
5918 if (hle->logmsg) {
5919 int n = fprintf(f, "%c %s\n",
5920 GOT_HISTEDIT_MESG, hle->logmsg);
5921 if (n < 0) {
5922 err = got_ferror(f, GOT_ERR_IO);
5923 break;
5927 done:
5928 if (f && fclose(f) != 0 && err == NULL)
5929 err = got_error_from_errno("fclose");
5930 free(path);
5931 if (commit)
5932 got_object_commit_close(commit);
5933 return err;
5936 void
5937 histedit_free_list(struct got_histedit_list *histedit_cmds)
5939 struct got_histedit_list_entry *hle;
5941 while ((hle = TAILQ_FIRST(histedit_cmds))) {
5942 TAILQ_REMOVE(histedit_cmds, hle, entry);
5943 free(hle);
5947 static const struct got_error *
5948 histedit_load_list(struct got_histedit_list *histedit_cmds,
5949 const char *path, struct got_repository *repo)
5951 const struct got_error *err = NULL;
5952 FILE *f = NULL;
5954 f = fopen(path, "r");
5955 if (f == NULL) {
5956 err = got_error_from_errno2("fopen", path);
5957 goto done;
5960 err = histedit_parse_list(histedit_cmds, f, repo);
5961 done:
5962 if (f && fclose(f) != 0 && err == NULL)
5963 err = got_error_from_errno("fclose");
5964 return err;
5967 static const struct got_error *
5968 histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
5969 const struct got_error *edit_err, struct got_object_id_queue *commits,
5970 const char *path, struct got_repository *repo)
5972 const struct got_error *err = NULL, *prev_err = edit_err;
5973 int resp = ' ';
5975 while (resp != 'c' && resp != 'r' && resp != 'a') {
5976 printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
5977 "or (a)bort: ", getprogname(), prev_err->msg);
5978 resp = getchar();
5979 if (resp == '\n')
5980 resp = getchar();
5981 if (resp == 'c') {
5982 histedit_free_list(histedit_cmds);
5983 err = histedit_run_editor(histedit_cmds, path, commits,
5984 repo);
5985 if (err) {
5986 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
5987 err->code != GOT_ERR_HISTEDIT_CMD)
5988 break;
5989 prev_err = err;
5990 resp = ' ';
5991 continue;
5993 break;
5994 } else if (resp == 'r') {
5995 histedit_free_list(histedit_cmds);
5996 err = histedit_edit_script(histedit_cmds,
5997 commits, repo);
5998 if (err) {
5999 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6000 err->code != GOT_ERR_HISTEDIT_CMD)
6001 break;
6002 prev_err = err;
6003 resp = ' ';
6004 continue;
6006 break;
6007 } else if (resp == 'a') {
6008 err = got_error(GOT_ERR_HISTEDIT_CANCEL);
6009 break;
6010 } else
6011 printf("invalid response '%c'\n", resp);
6014 return err;
6017 static const struct got_error *
6018 histedit_complete(struct got_worktree *worktree,
6019 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6020 struct got_reference *branch, struct got_repository *repo)
6022 printf("Switching work tree to %s\n",
6023 got_ref_get_symref_target(branch));
6024 return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
6025 branch, repo);
6028 static const struct got_error *
6029 show_histedit_progress(struct got_commit_object *commit,
6030 struct got_histedit_list_entry *hle, struct got_object_id *new_id)
6032 const struct got_error *err;
6033 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
6035 err = got_object_id_str(&old_id_str, hle->commit_id);
6036 if (err)
6037 goto done;
6039 if (new_id) {
6040 err = got_object_id_str(&new_id_str, new_id);
6041 if (err)
6042 goto done;
6045 old_id_str[12] = '\0';
6046 if (new_id_str)
6047 new_id_str[12] = '\0';
6049 if (hle->logmsg) {
6050 logmsg = strdup(hle->logmsg);
6051 if (logmsg == NULL) {
6052 err = got_error_from_errno("strdup");
6053 goto done;
6055 trim_logmsg(logmsg, 42);
6056 } else {
6057 err = get_short_logmsg(&logmsg, 42, commit);
6058 if (err)
6059 goto done;
6062 switch (hle->cmd->code) {
6063 case GOT_HISTEDIT_PICK:
6064 case GOT_HISTEDIT_EDIT:
6065 printf("%s -> %s: %s\n", old_id_str,
6066 new_id_str ? new_id_str : "no-op change", logmsg);
6067 break;
6068 case GOT_HISTEDIT_DROP:
6069 case GOT_HISTEDIT_FOLD:
6070 printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
6071 logmsg);
6072 break;
6073 default:
6074 break;
6077 done:
6078 free(old_id_str);
6079 free(new_id_str);
6080 return err;
6083 static const struct got_error *
6084 histedit_commit(struct got_pathlist_head *merged_paths,
6085 struct got_worktree *worktree, struct got_fileindex *fileindex,
6086 struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
6087 struct got_repository *repo)
6089 const struct got_error *err;
6090 struct got_commit_object *commit;
6091 struct got_object_id *new_commit_id;
6093 if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
6094 && hle->logmsg == NULL) {
6095 err = histedit_edit_logmsg(hle, repo);
6096 if (err)
6097 return err;
6100 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
6101 if (err)
6102 return err;
6104 err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
6105 worktree, fileindex, tmp_branch, commit, hle->commit_id,
6106 hle->logmsg, repo);
6107 if (err) {
6108 if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
6109 goto done;
6110 err = show_histedit_progress(commit, hle, NULL);
6111 } else {
6112 err = show_histedit_progress(commit, hle, new_commit_id);
6113 free(new_commit_id);
6115 done:
6116 got_object_commit_close(commit);
6117 return err;
6120 static const struct got_error *
6121 histedit_skip_commit(struct got_histedit_list_entry *hle,
6122 struct got_worktree *worktree, struct got_repository *repo)
6124 const struct got_error *error;
6125 struct got_commit_object *commit;
6127 error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
6128 repo);
6129 if (error)
6130 return error;
6132 error = got_object_open_as_commit(&commit, repo, hle->commit_id);
6133 if (error)
6134 return error;
6136 error = show_histedit_progress(commit, hle, NULL);
6137 got_object_commit_close(commit);
6138 return error;
6141 static const struct got_error *
6142 cmd_histedit(int argc, char *argv[])
6144 const struct got_error *error = NULL;
6145 struct got_worktree *worktree = NULL;
6146 struct got_fileindex *fileindex = NULL;
6147 struct got_repository *repo = NULL;
6148 char *cwd = NULL;
6149 struct got_reference *branch = NULL;
6150 struct got_reference *tmp_branch = NULL;
6151 struct got_object_id *resume_commit_id = NULL;
6152 struct got_object_id *base_commit_id = NULL;
6153 struct got_object_id *head_commit_id = NULL;
6154 struct got_commit_object *commit = NULL;
6155 int ch, rebase_in_progress = 0, did_something;
6156 int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
6157 const char *edit_script_path = NULL;
6158 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
6159 struct got_object_id_queue commits;
6160 struct got_pathlist_head merged_paths;
6161 const struct got_object_id_queue *parent_ids;
6162 struct got_object_qid *pid;
6163 struct got_histedit_list histedit_cmds;
6164 struct got_histedit_list_entry *hle;
6166 SIMPLEQ_INIT(&commits);
6167 TAILQ_INIT(&histedit_cmds);
6168 TAILQ_INIT(&merged_paths);
6170 while ((ch = getopt(argc, argv, "acF:")) != -1) {
6171 switch (ch) {
6172 case 'a':
6173 abort_edit = 1;
6174 break;
6175 case 'c':
6176 continue_edit = 1;
6177 break;
6178 case 'F':
6179 edit_script_path = optarg;
6180 break;
6181 default:
6182 usage_histedit();
6183 /* NOTREACHED */
6187 argc -= optind;
6188 argv += optind;
6190 #ifndef PROFILE
6191 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6192 "unveil", NULL) == -1)
6193 err(1, "pledge");
6194 #endif
6195 if (abort_edit && continue_edit)
6196 usage_histedit();
6197 if (argc != 0)
6198 usage_histedit();
6201 * This command cannot apply unveil(2) in all cases because the
6202 * user may choose to run an editor to edit the histedit script
6203 * and to edit individual commit log messages.
6204 * unveil(2) traverses exec(2); if an editor is used we have to
6205 * apply unveil after edit script and log messages have been written.
6206 * XXX TODO: Make use of unveil(2) where possible.
6209 cwd = getcwd(NULL, 0);
6210 if (cwd == NULL) {
6211 error = got_error_from_errno("getcwd");
6212 goto done;
6214 error = got_worktree_open(&worktree, cwd);
6215 if (error)
6216 goto done;
6218 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6219 NULL);
6220 if (error != NULL)
6221 goto done;
6223 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
6224 if (error)
6225 goto done;
6226 if (rebase_in_progress) {
6227 error = got_error(GOT_ERR_REBASING);
6228 goto done;
6231 error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
6232 if (error)
6233 goto done;
6235 if (edit_in_progress && abort_edit) {
6236 error = got_worktree_histedit_continue(&resume_commit_id,
6237 &tmp_branch, &branch, &base_commit_id, &fileindex,
6238 worktree, repo);
6239 if (error)
6240 goto done;
6241 printf("Switching work tree to %s\n",
6242 got_ref_get_symref_target(branch));
6243 error = got_worktree_histedit_abort(worktree, fileindex, repo,
6244 branch, base_commit_id, update_progress, &did_something);
6245 if (error)
6246 goto done;
6247 printf("Histedit of %s aborted\n",
6248 got_ref_get_symref_target(branch));
6249 goto done; /* nothing else to do */
6250 } else if (abort_edit) {
6251 error = got_error(GOT_ERR_NOT_HISTEDIT);
6252 goto done;
6255 if (continue_edit) {
6256 char *path;
6258 if (!edit_in_progress) {
6259 error = got_error(GOT_ERR_NOT_HISTEDIT);
6260 goto done;
6263 error = got_worktree_get_histedit_script_path(&path, worktree);
6264 if (error)
6265 goto done;
6267 error = histedit_load_list(&histedit_cmds, path, repo);
6268 free(path);
6269 if (error)
6270 goto done;
6272 error = got_worktree_histedit_continue(&resume_commit_id,
6273 &tmp_branch, &branch, &base_commit_id, &fileindex,
6274 worktree, repo);
6275 if (error)
6276 goto done;
6278 error = got_ref_resolve(&head_commit_id, repo, branch);
6279 if (error)
6280 goto done;
6282 error = got_object_open_as_commit(&commit, repo,
6283 head_commit_id);
6284 if (error)
6285 goto done;
6286 parent_ids = got_object_commit_get_parent_ids(commit);
6287 pid = SIMPLEQ_FIRST(parent_ids);
6288 if (pid == NULL) {
6289 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
6290 goto done;
6292 error = collect_commits(&commits, head_commit_id, pid->id,
6293 base_commit_id, got_worktree_get_path_prefix(worktree),
6294 GOT_ERR_HISTEDIT_PATH, repo);
6295 got_object_commit_close(commit);
6296 commit = NULL;
6297 if (error)
6298 goto done;
6299 } else {
6300 if (edit_in_progress) {
6301 error = got_error(GOT_ERR_HISTEDIT_BUSY);
6302 goto done;
6305 error = got_ref_open(&branch, repo,
6306 got_worktree_get_head_ref_name(worktree), 0);
6307 if (error != NULL)
6308 goto done;
6310 if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
6311 error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
6312 "will not edit commit history of a branch outside "
6313 "the \"refs/heads/\" reference namespace");
6314 goto done;
6317 error = got_ref_resolve(&head_commit_id, repo, branch);
6318 got_ref_close(branch);
6319 branch = NULL;
6320 if (error)
6321 goto done;
6323 error = got_object_open_as_commit(&commit, repo,
6324 head_commit_id);
6325 if (error)
6326 goto done;
6327 parent_ids = got_object_commit_get_parent_ids(commit);
6328 pid = SIMPLEQ_FIRST(parent_ids);
6329 if (pid == NULL) {
6330 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
6331 goto done;
6333 error = collect_commits(&commits, head_commit_id, pid->id,
6334 got_worktree_get_base_commit_id(worktree),
6335 got_worktree_get_path_prefix(worktree),
6336 GOT_ERR_HISTEDIT_PATH, repo);
6337 got_object_commit_close(commit);
6338 commit = NULL;
6339 if (error)
6340 goto done;
6342 error = got_worktree_histedit_prepare(&tmp_branch, &branch,
6343 &base_commit_id, &fileindex, worktree, repo);
6344 if (error)
6345 goto done;
6347 if (edit_script_path) {
6348 error = histedit_load_list(&histedit_cmds,
6349 edit_script_path, repo);
6350 if (error) {
6351 got_worktree_histedit_abort(worktree, fileindex,
6352 repo, branch, base_commit_id,
6353 update_progress, &did_something);
6354 goto done;
6356 } else {
6357 error = histedit_edit_script(&histedit_cmds, &commits,
6358 repo);
6359 if (error) {
6360 got_worktree_histedit_abort(worktree, fileindex,
6361 repo, branch, base_commit_id,
6362 update_progress, &did_something);
6363 goto done;
6368 error = histedit_save_list(&histedit_cmds, worktree,
6369 repo);
6370 if (error) {
6371 got_worktree_histedit_abort(worktree, fileindex,
6372 repo, branch, base_commit_id,
6373 update_progress, &did_something);
6374 goto done;
6379 error = histedit_check_script(&histedit_cmds, &commits, repo);
6380 if (error)
6381 goto done;
6383 TAILQ_FOREACH(hle, &histedit_cmds, entry) {
6384 if (resume_commit_id) {
6385 if (got_object_id_cmp(hle->commit_id,
6386 resume_commit_id) != 0)
6387 continue;
6389 resume_commit_id = NULL;
6390 if (hle->cmd->code == GOT_HISTEDIT_DROP ||
6391 hle->cmd->code == GOT_HISTEDIT_FOLD) {
6392 error = histedit_skip_commit(hle, worktree,
6393 repo);
6394 } else {
6395 error = histedit_commit(NULL, worktree,
6396 fileindex, tmp_branch, hle, repo);
6398 if (error)
6399 goto done;
6400 continue;
6403 if (hle->cmd->code == GOT_HISTEDIT_DROP) {
6404 error = histedit_skip_commit(hle, worktree, repo);
6405 if (error)
6406 goto done;
6407 continue;
6410 error = got_object_open_as_commit(&commit, repo,
6411 hle->commit_id);
6412 if (error)
6413 goto done;
6414 parent_ids = got_object_commit_get_parent_ids(commit);
6415 pid = SIMPLEQ_FIRST(parent_ids);
6417 error = got_worktree_histedit_merge_files(&merged_paths,
6418 worktree, fileindex, pid->id, hle->commit_id, repo,
6419 rebase_progress, &rebase_status, check_cancelled, NULL);
6420 if (error)
6421 goto done;
6422 got_object_commit_close(commit);
6423 commit = NULL;
6425 if (rebase_status == GOT_STATUS_CONFLICT) {
6426 got_worktree_rebase_pathlist_free(&merged_paths);
6427 break;
6430 if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
6431 char *id_str;
6432 error = got_object_id_str(&id_str, hle->commit_id);
6433 if (error)
6434 goto done;
6435 printf("Stopping histedit for amending commit %s\n",
6436 id_str);
6437 free(id_str);
6438 got_worktree_rebase_pathlist_free(&merged_paths);
6439 error = got_worktree_histedit_postpone(worktree,
6440 fileindex);
6441 goto done;
6444 if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
6445 error = histedit_skip_commit(hle, worktree, repo);
6446 if (error)
6447 goto done;
6448 continue;
6451 error = histedit_commit(&merged_paths, worktree, fileindex,
6452 tmp_branch, hle, repo);
6453 got_worktree_rebase_pathlist_free(&merged_paths);
6454 if (error)
6455 goto done;
6458 if (rebase_status == GOT_STATUS_CONFLICT) {
6459 error = got_worktree_histedit_postpone(worktree, fileindex);
6460 if (error)
6461 goto done;
6462 error = got_error_msg(GOT_ERR_CONFLICTS,
6463 "conflicts must be resolved before rebasing can continue");
6464 } else
6465 error = histedit_complete(worktree, fileindex, tmp_branch,
6466 branch, repo);
6467 done:
6468 got_object_id_queue_free(&commits);
6469 histedit_free_list(&histedit_cmds);
6470 free(head_commit_id);
6471 free(base_commit_id);
6472 free(resume_commit_id);
6473 if (commit)
6474 got_object_commit_close(commit);
6475 if (branch)
6476 got_ref_close(branch);
6477 if (tmp_branch)
6478 got_ref_close(tmp_branch);
6479 if (worktree)
6480 got_worktree_close(worktree);
6481 if (repo)
6482 got_repo_close(repo);
6483 return error;
6486 __dead static void
6487 usage_integrate(void)
6489 fprintf(stderr, "usage: %s integrate branch\n", getprogname());
6490 exit(1);
6493 static const struct got_error *
6494 cmd_integrate(int argc, char *argv[])
6496 const struct got_error *error = NULL;
6497 struct got_repository *repo = NULL;
6498 struct got_worktree *worktree = NULL;
6499 char *cwd = NULL, *refname = NULL, *base_refname = NULL;
6500 const char *branch_arg = NULL;
6501 struct got_reference *branch_ref = NULL, *base_branch_ref = NULL;
6502 struct got_fileindex *fileindex = NULL;
6503 struct got_object_id *commit_id = NULL, *base_commit_id = NULL;
6504 int ch, did_something = 0;
6506 while ((ch = getopt(argc, argv, "")) != -1) {
6507 switch (ch) {
6508 default:
6509 usage_integrate();
6510 /* NOTREACHED */
6514 argc -= optind;
6515 argv += optind;
6517 if (argc != 1)
6518 usage_integrate();
6519 branch_arg = argv[0];
6521 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6522 "unveil", NULL) == -1)
6523 err(1, "pledge");
6525 cwd = getcwd(NULL, 0);
6526 if (cwd == NULL) {
6527 error = got_error_from_errno("getcwd");
6528 goto done;
6531 error = got_worktree_open(&worktree, cwd);
6532 if (error)
6533 goto done;
6535 error = check_rebase_or_histedit_in_progress(worktree);
6536 if (error)
6537 goto done;
6539 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6540 NULL);
6541 if (error != NULL)
6542 goto done;
6544 error = apply_unveil(got_repo_get_path(repo), 0,
6545 got_worktree_get_root_path(worktree));
6546 if (error)
6547 goto done;
6549 if (asprintf(&refname, "refs/heads/%s", branch_arg) == -1) {
6550 error = got_error_from_errno("asprintf");
6551 goto done;
6554 error = got_worktree_integrate_prepare(&fileindex, &branch_ref,
6555 &base_branch_ref, worktree, refname, repo);
6556 if (error)
6557 goto done;
6559 refname = strdup(got_ref_get_name(branch_ref));
6560 if (refname == NULL) {
6561 error = got_error_from_errno("strdup");
6562 got_worktree_integrate_abort(worktree, fileindex, repo,
6563 branch_ref, base_branch_ref);
6564 goto done;
6566 base_refname = strdup(got_ref_get_name(base_branch_ref));
6567 if (base_refname == NULL) {
6568 error = got_error_from_errno("strdup");
6569 got_worktree_integrate_abort(worktree, fileindex, repo,
6570 branch_ref, base_branch_ref);
6571 goto done;
6574 error = got_ref_resolve(&commit_id, repo, branch_ref);
6575 if (error)
6576 goto done;
6578 error = got_ref_resolve(&base_commit_id, repo, base_branch_ref);
6579 if (error)
6580 goto done;
6582 if (got_object_id_cmp(commit_id, base_commit_id) == 0) {
6583 error = got_error_msg(GOT_ERR_SAME_BRANCH,
6584 "specified branch has already been integrated");
6585 got_worktree_integrate_abort(worktree, fileindex, repo,
6586 branch_ref, base_branch_ref);
6587 goto done;
6590 error = check_linear_ancestry(commit_id, base_commit_id, 1, repo);
6591 if (error) {
6592 if (error->code == GOT_ERR_ANCESTRY)
6593 error = got_error(GOT_ERR_REBASE_REQUIRED);
6594 got_worktree_integrate_abort(worktree, fileindex, repo,
6595 branch_ref, base_branch_ref);
6596 goto done;
6599 error = got_worktree_integrate_continue(worktree, fileindex, repo,
6600 branch_ref, base_branch_ref, update_progress, &did_something,
6601 check_cancelled, NULL);
6602 if (error)
6603 goto done;
6605 printf("Integrated %s into %s\n", refname, base_refname);
6606 done:
6607 if (repo)
6608 got_repo_close(repo);
6609 if (worktree)
6610 got_worktree_close(worktree);
6611 free(cwd);
6612 free(base_commit_id);
6613 free(commit_id);
6614 free(refname);
6615 free(base_refname);
6616 return error;
6619 __dead static void
6620 usage_stage(void)
6622 fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
6623 "[file-path ...]\n",
6624 getprogname());
6625 exit(1);
6628 static const struct got_error *
6629 print_stage(void *arg, unsigned char status, unsigned char staged_status,
6630 const char *path, struct got_object_id *blob_id,
6631 struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
6633 const struct got_error *err = NULL;
6634 char *id_str = NULL;
6636 if (staged_status != GOT_STATUS_ADD &&
6637 staged_status != GOT_STATUS_MODIFY &&
6638 staged_status != GOT_STATUS_DELETE)
6639 return NULL;
6641 if (staged_status == GOT_STATUS_ADD ||
6642 staged_status == GOT_STATUS_MODIFY)
6643 err = got_object_id_str(&id_str, staged_blob_id);
6644 else
6645 err = got_object_id_str(&id_str, blob_id);
6646 if (err)
6647 return err;
6649 printf("%s %c %s\n", id_str, staged_status, path);
6650 free(id_str);
6651 return NULL;
6654 static const struct got_error *
6655 cmd_stage(int argc, char *argv[])
6657 const struct got_error *error = NULL;
6658 struct got_repository *repo = NULL;
6659 struct got_worktree *worktree = NULL;
6660 char *cwd = NULL;
6661 struct got_pathlist_head paths;
6662 struct got_pathlist_entry *pe;
6663 int ch, list_stage = 0, pflag = 0;
6664 FILE *patch_script_file = NULL;
6665 const char *patch_script_path = NULL;
6666 struct choose_patch_arg cpa;
6668 TAILQ_INIT(&paths);
6670 while ((ch = getopt(argc, argv, "lpF:")) != -1) {
6671 switch (ch) {
6672 case 'l':
6673 list_stage = 1;
6674 break;
6675 case 'p':
6676 pflag = 1;
6677 break;
6678 case 'F':
6679 patch_script_path = optarg;
6680 break;
6681 default:
6682 usage_stage();
6683 /* NOTREACHED */
6687 argc -= optind;
6688 argv += optind;
6690 #ifndef PROFILE
6691 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6692 "unveil", NULL) == -1)
6693 err(1, "pledge");
6694 #endif
6695 if (list_stage && (pflag || patch_script_path))
6696 errx(1, "-l option cannot be used with other options");
6697 if (patch_script_path && !pflag)
6698 errx(1, "-F option can only be used together with -p option");
6700 cwd = getcwd(NULL, 0);
6701 if (cwd == NULL) {
6702 error = got_error_from_errno("getcwd");
6703 goto done;
6706 error = got_worktree_open(&worktree, cwd);
6707 if (error)
6708 goto done;
6710 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6711 NULL);
6712 if (error != NULL)
6713 goto done;
6715 if (patch_script_path) {
6716 patch_script_file = fopen(patch_script_path, "r");
6717 if (patch_script_file == NULL) {
6718 error = got_error_from_errno2("fopen",
6719 patch_script_path);
6720 goto done;
6723 error = apply_unveil(got_repo_get_path(repo), 0,
6724 got_worktree_get_root_path(worktree));
6725 if (error)
6726 goto done;
6728 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6729 if (error)
6730 goto done;
6732 if (list_stage)
6733 error = got_worktree_status(worktree, &paths, repo,
6734 print_stage, NULL, check_cancelled, NULL);
6735 else {
6736 cpa.patch_script_file = patch_script_file;
6737 cpa.action = "stage";
6738 error = got_worktree_stage(worktree, &paths,
6739 pflag ? NULL : print_status, NULL,
6740 pflag ? choose_patch : NULL, &cpa, repo);
6742 done:
6743 if (patch_script_file && fclose(patch_script_file) == EOF &&
6744 error == NULL)
6745 error = got_error_from_errno2("fclose", patch_script_path);
6746 if (repo)
6747 got_repo_close(repo);
6748 if (worktree)
6749 got_worktree_close(worktree);
6750 TAILQ_FOREACH(pe, &paths, entry)
6751 free((char *)pe->path);
6752 got_pathlist_free(&paths);
6753 free(cwd);
6754 return error;
6757 __dead static void
6758 usage_unstage(void)
6760 fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
6761 "[file-path ...]\n",
6762 getprogname());
6763 exit(1);
6767 static const struct got_error *
6768 cmd_unstage(int argc, char *argv[])
6770 const struct got_error *error = NULL;
6771 struct got_repository *repo = NULL;
6772 struct got_worktree *worktree = NULL;
6773 char *cwd = NULL;
6774 struct got_pathlist_head paths;
6775 struct got_pathlist_entry *pe;
6776 int ch, did_something = 0, pflag = 0;
6777 FILE *patch_script_file = NULL;
6778 const char *patch_script_path = NULL;
6779 struct choose_patch_arg cpa;
6781 TAILQ_INIT(&paths);
6783 while ((ch = getopt(argc, argv, "pF:")) != -1) {
6784 switch (ch) {
6785 case 'p':
6786 pflag = 1;
6787 break;
6788 case 'F':
6789 patch_script_path = optarg;
6790 break;
6791 default:
6792 usage_unstage();
6793 /* NOTREACHED */
6797 argc -= optind;
6798 argv += optind;
6800 #ifndef PROFILE
6801 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6802 "unveil", NULL) == -1)
6803 err(1, "pledge");
6804 #endif
6805 if (patch_script_path && !pflag)
6806 errx(1, "-F option can only be used together with -p option");
6808 cwd = getcwd(NULL, 0);
6809 if (cwd == NULL) {
6810 error = got_error_from_errno("getcwd");
6811 goto done;
6814 error = got_worktree_open(&worktree, cwd);
6815 if (error)
6816 goto done;
6818 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6819 NULL);
6820 if (error != NULL)
6821 goto done;
6823 if (patch_script_path) {
6824 patch_script_file = fopen(patch_script_path, "r");
6825 if (patch_script_file == NULL) {
6826 error = got_error_from_errno2("fopen",
6827 patch_script_path);
6828 goto done;
6832 error = apply_unveil(got_repo_get_path(repo), 0,
6833 got_worktree_get_root_path(worktree));
6834 if (error)
6835 goto done;
6837 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6838 if (error)
6839 goto done;
6841 cpa.patch_script_file = patch_script_file;
6842 cpa.action = "unstage";
6843 error = got_worktree_unstage(worktree, &paths, update_progress,
6844 &did_something, pflag ? choose_patch : NULL, &cpa, repo);
6845 done:
6846 if (patch_script_file && fclose(patch_script_file) == EOF &&
6847 error == NULL)
6848 error = got_error_from_errno2("fclose", patch_script_path);
6849 if (repo)
6850 got_repo_close(repo);
6851 if (worktree)
6852 got_worktree_close(worktree);
6853 TAILQ_FOREACH(pe, &paths, entry)
6854 free((char *)pe->path);
6855 got_pathlist_free(&paths);
6856 free(cwd);
6857 return error;
6860 __dead static void
6861 usage_cat(void)
6863 fprintf(stderr, "usage: %s cat [-r repository ] [ -c commit ] [ -P ] "
6864 "arg1 [arg2 ...]\n", getprogname());
6865 exit(1);
6868 static const struct got_error *
6869 cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
6871 const struct got_error *err;
6872 struct got_blob_object *blob;
6874 err = got_object_open_as_blob(&blob, repo, id, 8192);
6875 if (err)
6876 return err;
6878 err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
6879 got_object_blob_close(blob);
6880 return err;
6883 static const struct got_error *
6884 cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
6886 const struct got_error *err;
6887 struct got_tree_object *tree;
6888 const struct got_tree_entries *entries;
6889 struct got_tree_entry *te;
6891 err = got_object_open_as_tree(&tree, repo, id);
6892 if (err)
6893 return err;
6895 entries = got_object_tree_get_entries(tree);
6896 te = SIMPLEQ_FIRST(&entries->head);
6897 while (te) {
6898 char *id_str;
6899 if (sigint_received || sigpipe_received)
6900 break;
6901 err = got_object_id_str(&id_str, te->id);
6902 if (err)
6903 break;
6904 fprintf(outfile, "%s %.7o %s\n", id_str, te->mode, te->name);
6905 free(id_str);
6906 te = SIMPLEQ_NEXT(te, entry);
6909 got_object_tree_close(tree);
6910 return err;
6913 static const struct got_error *
6914 cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
6916 const struct got_error *err;
6917 struct got_commit_object *commit;
6918 const struct got_object_id_queue *parent_ids;
6919 struct got_object_qid *pid;
6920 char *id_str = NULL;
6921 const char *logmsg = NULL;
6923 err = got_object_open_as_commit(&commit, repo, id);
6924 if (err)
6925 return err;
6927 err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
6928 if (err)
6929 goto done;
6931 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str);
6932 parent_ids = got_object_commit_get_parent_ids(commit);
6933 fprintf(outfile, "numparents %d\n",
6934 got_object_commit_get_nparents(commit));
6935 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
6936 char *pid_str;
6937 err = got_object_id_str(&pid_str, pid->id);
6938 if (err)
6939 goto done;
6940 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_PARENT, pid_str);
6941 free(pid_str);
6943 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_AUTHOR,
6944 got_object_commit_get_author(commit),
6945 got_object_commit_get_author_time(commit));
6947 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_COMMITTER,
6948 got_object_commit_get_author(commit),
6949 got_object_commit_get_committer_time(commit));
6951 logmsg = got_object_commit_get_logmsg_raw(commit);
6952 fprintf(outfile, "messagelen %zd\n", strlen(logmsg));
6953 fprintf(outfile, "%s", logmsg);
6954 done:
6955 free(id_str);
6956 got_object_commit_close(commit);
6957 return err;
6960 static const struct got_error *
6961 cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
6963 const struct got_error *err;
6964 struct got_tag_object *tag;
6965 char *id_str = NULL;
6966 const char *tagmsg = NULL;
6968 err = got_object_open_as_tag(&tag, repo, id);
6969 if (err)
6970 return err;
6972 err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
6973 if (err)
6974 goto done;
6976 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str);
6978 switch (got_object_tag_get_object_type(tag)) {
6979 case GOT_OBJ_TYPE_BLOB:
6980 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
6981 GOT_OBJ_LABEL_BLOB);
6982 break;
6983 case GOT_OBJ_TYPE_TREE:
6984 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
6985 GOT_OBJ_LABEL_TREE);
6986 break;
6987 case GOT_OBJ_TYPE_COMMIT:
6988 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
6989 GOT_OBJ_LABEL_COMMIT);
6990 break;
6991 case GOT_OBJ_TYPE_TAG:
6992 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
6993 GOT_OBJ_LABEL_TAG);
6994 break;
6995 default:
6996 break;
6999 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TAG,
7000 got_object_tag_get_name(tag));
7002 fprintf(outfile, "%s%s %lld +0000\n", GOT_TAG_LABEL_TAGGER,
7003 got_object_tag_get_tagger(tag),
7004 got_object_tag_get_tagger_time(tag));
7006 tagmsg = got_object_tag_get_message(tag);
7007 fprintf(outfile, "messagelen %zd\n", strlen(tagmsg));
7008 fprintf(outfile, "%s", tagmsg);
7009 done:
7010 free(id_str);
7011 got_object_tag_close(tag);
7012 return err;
7015 static const struct got_error *
7016 cmd_cat(int argc, char *argv[])
7018 const struct got_error *error;
7019 struct got_repository *repo = NULL;
7020 struct got_worktree *worktree = NULL;
7021 char *cwd = NULL, *repo_path = NULL, *label = NULL;
7022 const char *commit_id_str = NULL;
7023 struct got_object_id *id = NULL, *commit_id = NULL;
7024 int ch, obj_type, i, force_path = 0;
7026 #ifndef PROFILE
7027 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
7028 NULL) == -1)
7029 err(1, "pledge");
7030 #endif
7032 while ((ch = getopt(argc, argv, "c:r:P")) != -1) {
7033 switch (ch) {
7034 case 'c':
7035 commit_id_str = optarg;
7036 break;
7037 case 'r':
7038 repo_path = realpath(optarg, NULL);
7039 if (repo_path == NULL)
7040 err(1, "-r option");
7041 got_path_strip_trailing_slashes(repo_path);
7042 break;
7043 case 'P':
7044 force_path = 1;
7045 break;
7046 default:
7047 usage_cat();
7048 /* NOTREACHED */
7052 argc -= optind;
7053 argv += optind;
7055 cwd = getcwd(NULL, 0);
7056 if (cwd == NULL) {
7057 error = got_error_from_errno("getcwd");
7058 goto done;
7060 error = got_worktree_open(&worktree, cwd);
7061 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7062 goto done;
7063 if (worktree) {
7064 if (repo_path == NULL) {
7065 repo_path = strdup(
7066 got_worktree_get_repo_path(worktree));
7067 if (repo_path == NULL) {
7068 error = got_error_from_errno("strdup");
7069 goto done;
7074 if (repo_path == NULL) {
7075 repo_path = getcwd(NULL, 0);
7076 if (repo_path == NULL)
7077 return got_error_from_errno("getcwd");
7080 error = got_repo_open(&repo, repo_path, NULL);
7081 free(repo_path);
7082 if (error != NULL)
7083 goto done;
7085 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
7086 if (error)
7087 goto done;
7089 if (commit_id_str == NULL)
7090 commit_id_str = GOT_REF_HEAD;
7091 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
7092 if (error)
7093 goto done;
7095 for (i = 0; i < argc; i++) {
7096 if (force_path) {
7097 error = got_object_id_by_path(&id, repo, commit_id,
7098 argv[i]);
7099 if (error)
7100 break;
7101 } else {
7102 error = match_object_id(&id, &label, argv[i],
7103 GOT_OBJ_TYPE_ANY, 0, repo);
7104 if (error) {
7105 if (error->code != GOT_ERR_BAD_OBJ_ID_STR &&
7106 error->code != GOT_ERR_NOT_REF)
7107 break;
7108 error = got_object_id_by_path(&id, repo,
7109 commit_id, argv[i]);
7110 if (error)
7111 break;
7115 error = got_object_get_type(&obj_type, repo, id);
7116 if (error)
7117 break;
7119 switch (obj_type) {
7120 case GOT_OBJ_TYPE_BLOB:
7121 error = cat_blob(id, repo, stdout);
7122 break;
7123 case GOT_OBJ_TYPE_TREE:
7124 error = cat_tree(id, repo, stdout);
7125 break;
7126 case GOT_OBJ_TYPE_COMMIT:
7127 error = cat_commit(id, repo, stdout);
7128 break;
7129 case GOT_OBJ_TYPE_TAG:
7130 error = cat_tag(id, repo, stdout);
7131 break;
7132 default:
7133 error = got_error(GOT_ERR_OBJ_TYPE);
7134 break;
7136 if (error)
7137 break;
7138 free(label);
7139 label = NULL;
7140 free(id);
7141 id = NULL;
7144 done:
7145 free(label);
7146 free(id);
7147 free(commit_id);
7148 if (worktree)
7149 got_worktree_close(worktree);
7150 if (repo) {
7151 const struct got_error *repo_error;
7152 repo_error = got_repo_close(repo);
7153 if (error == NULL)
7154 error = repo_error;
7156 return error;