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>
37 #include <regex.h>
39 #include "got_version.h"
40 #include "got_error.h"
41 #include "got_object.h"
42 #include "got_reference.h"
43 #include "got_repository.h"
44 #include "got_path.h"
45 #include "got_cancel.h"
46 #include "got_worktree.h"
47 #include "got_diff.h"
48 #include "got_commit_graph.h"
49 #include "got_blame.h"
50 #include "got_privsep.h"
51 #include "got_opentemp.h"
53 #ifndef nitems
54 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
55 #endif
57 static volatile sig_atomic_t sigint_received;
58 static volatile sig_atomic_t sigpipe_received;
60 static void
61 catch_sigint(int signo)
62 {
63 sigint_received = 1;
64 }
66 static void
67 catch_sigpipe(int signo)
68 {
69 sigpipe_received = 1;
70 }
73 struct got_cmd {
74 const char *cmd_name;
75 const struct got_error *(*cmd_main)(int, char *[]);
76 void (*cmd_usage)(void);
77 const char *cmd_alias;
78 };
80 __dead static void usage(int);
81 __dead static void usage_init(void);
82 __dead static void usage_import(void);
83 __dead static void usage_checkout(void);
84 __dead static void usage_update(void);
85 __dead static void usage_log(void);
86 __dead static void usage_diff(void);
87 __dead static void usage_blame(void);
88 __dead static void usage_tree(void);
89 __dead static void usage_status(void);
90 __dead static void usage_ref(void);
91 __dead static void usage_branch(void);
92 __dead static void usage_tag(void);
93 __dead static void usage_add(void);
94 __dead static void usage_remove(void);
95 __dead static void usage_revert(void);
96 __dead static void usage_commit(void);
97 __dead static void usage_cherrypick(void);
98 __dead static void usage_backout(void);
99 __dead static void usage_rebase(void);
100 __dead static void usage_histedit(void);
101 __dead static void usage_integrate(void);
102 __dead static void usage_stage(void);
103 __dead static void usage_unstage(void);
104 __dead static void usage_cat(void);
106 static const struct got_error* cmd_init(int, char *[]);
107 static const struct got_error* cmd_import(int, char *[]);
108 static const struct got_error* cmd_checkout(int, char *[]);
109 static const struct got_error* cmd_update(int, char *[]);
110 static const struct got_error* cmd_log(int, char *[]);
111 static const struct got_error* cmd_diff(int, char *[]);
112 static const struct got_error* cmd_blame(int, char *[]);
113 static const struct got_error* cmd_tree(int, char *[]);
114 static const struct got_error* cmd_status(int, char *[]);
115 static const struct got_error* cmd_ref(int, char *[]);
116 static const struct got_error* cmd_branch(int, char *[]);
117 static const struct got_error* cmd_tag(int, char *[]);
118 static const struct got_error* cmd_add(int, char *[]);
119 static const struct got_error* cmd_remove(int, char *[]);
120 static const struct got_error* cmd_revert(int, char *[]);
121 static const struct got_error* cmd_commit(int, char *[]);
122 static const struct got_error* cmd_cherrypick(int, char *[]);
123 static const struct got_error* cmd_backout(int, char *[]);
124 static const struct got_error* cmd_rebase(int, char *[]);
125 static const struct got_error* cmd_histedit(int, char *[]);
126 static const struct got_error* cmd_integrate(int, char *[]);
127 static const struct got_error* cmd_stage(int, char *[]);
128 static const struct got_error* cmd_unstage(int, char *[]);
129 static const struct got_error* cmd_cat(int, char *[]);
131 static struct got_cmd got_commands[] = {
132 { "init", cmd_init, usage_init, "in" },
133 { "import", cmd_import, usage_import, "im" },
134 { "checkout", cmd_checkout, usage_checkout, "co" },
135 { "update", cmd_update, usage_update, "up" },
136 { "log", cmd_log, usage_log, "" },
137 { "diff", cmd_diff, usage_diff, "di" },
138 { "blame", cmd_blame, usage_blame, "bl" },
139 { "tree", cmd_tree, usage_tree, "tr" },
140 { "status", cmd_status, usage_status, "st" },
141 { "ref", cmd_ref, usage_ref, "" },
142 { "branch", cmd_branch, usage_branch, "br" },
143 { "tag", cmd_tag, usage_tag, "" },
144 { "add", cmd_add, usage_add, "" },
145 { "remove", cmd_remove, usage_remove, "rm" },
146 { "revert", cmd_revert, usage_revert, "rv" },
147 { "commit", cmd_commit, usage_commit, "ci" },
148 { "cherrypick", cmd_cherrypick, usage_cherrypick, "cy" },
149 { "backout", cmd_backout, usage_backout, "bo" },
150 { "rebase", cmd_rebase, usage_rebase, "rb" },
151 { "histedit", cmd_histedit, usage_histedit, "he" },
152 { "integrate", cmd_integrate, usage_integrate,"ig" },
153 { "stage", cmd_stage, usage_stage, "sg" },
154 { "unstage", cmd_unstage, usage_unstage, "ug" },
155 { "cat", cmd_cat, usage_cat, "" },
156 };
158 static void
159 list_commands(void)
161 int i;
163 fprintf(stderr, "commands:");
164 for (i = 0; i < nitems(got_commands); i++) {
165 struct got_cmd *cmd = &got_commands[i];
166 fprintf(stderr, " %s", cmd->cmd_name);
168 fputc('\n', stderr);
171 int
172 main(int argc, char *argv[])
174 struct got_cmd *cmd;
175 unsigned int i;
176 int ch;
177 int hflag = 0, Vflag = 0;
179 setlocale(LC_CTYPE, "");
181 while ((ch = getopt(argc, argv, "hV")) != -1) {
182 switch (ch) {
183 case 'h':
184 hflag = 1;
185 break;
186 case 'V':
187 Vflag = 1;
188 break;
189 default:
190 usage(hflag);
191 /* NOTREACHED */
195 argc -= optind;
196 argv += optind;
197 optind = 0;
199 if (Vflag) {
200 got_version_print_str();
201 return 1;
204 if (argc <= 0)
205 usage(hflag);
207 signal(SIGINT, catch_sigint);
208 signal(SIGPIPE, catch_sigpipe);
210 for (i = 0; i < nitems(got_commands); i++) {
211 const struct got_error *error;
213 cmd = &got_commands[i];
215 if (strcmp(cmd->cmd_name, argv[0]) != 0 &&
216 strcmp(cmd->cmd_alias, argv[0]) != 0)
217 continue;
219 if (hflag)
220 got_commands[i].cmd_usage();
222 error = got_commands[i].cmd_main(argc, argv);
223 if (error && error->code != GOT_ERR_CANCELLED &&
224 error->code != GOT_ERR_PRIVSEP_EXIT &&
225 !(sigpipe_received &&
226 error->code == GOT_ERR_ERRNO && errno == EPIPE) &&
227 !(sigint_received &&
228 error->code == GOT_ERR_ERRNO && errno == EINTR)) {
229 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
230 return 1;
233 return 0;
236 fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
237 list_commands();
238 return 1;
241 __dead static void
242 usage(int hflag)
244 fprintf(stderr, "usage: %s [-h] [-V] command [arg ...]\n",
245 getprogname());
246 if (hflag)
247 list_commands();
248 exit(1);
251 static const struct got_error *
252 get_editor(char **abspath)
254 const struct got_error *err = NULL;
255 const char *editor;
257 *abspath = NULL;
259 editor = getenv("VISUAL");
260 if (editor == NULL)
261 editor = getenv("EDITOR");
263 if (editor) {
264 err = got_path_find_prog(abspath, editor);
265 if (err)
266 return err;
269 if (*abspath == NULL) {
270 *abspath = strdup("/bin/ed");
271 if (*abspath == NULL)
272 return got_error_from_errno("strdup");
275 return NULL;
278 static const struct got_error *
279 apply_unveil(const char *repo_path, int repo_read_only,
280 const char *worktree_path)
282 const struct got_error *err;
284 #ifdef PROFILE
285 if (unveil("gmon.out", "rwc") != 0)
286 return got_error_from_errno2("unveil", "gmon.out");
287 #endif
288 if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
289 return got_error_from_errno2("unveil", repo_path);
291 if (worktree_path && unveil(worktree_path, "rwc") != 0)
292 return got_error_from_errno2("unveil", worktree_path);
294 if (unveil("/tmp", "rwc") != 0)
295 return got_error_from_errno2("unveil", "/tmp");
297 err = got_privsep_unveil_exec_helpers();
298 if (err != NULL)
299 return err;
301 if (unveil(NULL, NULL) != 0)
302 return got_error_from_errno("unveil");
304 return NULL;
307 __dead static void
308 usage_init(void)
310 fprintf(stderr, "usage: %s init repository-path\n", getprogname());
311 exit(1);
314 static const struct got_error *
315 cmd_init(int argc, char *argv[])
317 const struct got_error *error = NULL;
318 char *repo_path = NULL;
319 int ch;
321 while ((ch = getopt(argc, argv, "")) != -1) {
322 switch (ch) {
323 default:
324 usage_init();
325 /* NOTREACHED */
329 argc -= optind;
330 argv += optind;
332 #ifndef PROFILE
333 if (pledge("stdio rpath wpath cpath unveil", NULL) == -1)
334 err(1, "pledge");
335 #endif
336 if (argc != 1)
337 usage_init();
339 repo_path = strdup(argv[0]);
340 if (repo_path == NULL)
341 return got_error_from_errno("strdup");
343 got_path_strip_trailing_slashes(repo_path);
345 error = got_path_mkdir(repo_path);
346 if (error &&
347 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
348 goto done;
350 error = apply_unveil(repo_path, 0, NULL);
351 if (error)
352 goto done;
354 error = got_repo_init(repo_path);
355 if (error != NULL)
356 goto done;
358 done:
359 free(repo_path);
360 return error;
363 __dead static void
364 usage_import(void)
366 fprintf(stderr, "usage: %s import [-b branch] [-m message] "
367 "[-r repository-path] [-I pattern] path\n", getprogname());
368 exit(1);
371 int
372 spawn_editor(const char *editor, const char *file)
374 pid_t pid;
375 sig_t sighup, sigint, sigquit;
376 int st = -1;
378 sighup = signal(SIGHUP, SIG_IGN);
379 sigint = signal(SIGINT, SIG_IGN);
380 sigquit = signal(SIGQUIT, SIG_IGN);
382 switch (pid = fork()) {
383 case -1:
384 goto doneediting;
385 case 0:
386 execl(editor, editor, file, (char *)NULL);
387 _exit(127);
390 while (waitpid(pid, &st, 0) == -1)
391 if (errno != EINTR)
392 break;
394 doneediting:
395 (void)signal(SIGHUP, sighup);
396 (void)signal(SIGINT, sigint);
397 (void)signal(SIGQUIT, sigquit);
399 if (!WIFEXITED(st)) {
400 errno = EINTR;
401 return -1;
404 return WEXITSTATUS(st);
407 static const struct got_error *
408 edit_logmsg(char **logmsg, const char *editor, const char *logmsg_path,
409 const char *initial_content)
411 const struct got_error *err = NULL;
412 char buf[1024];
413 struct stat st, st2;
414 FILE *fp;
415 int content_changed = 0;
416 size_t len;
418 *logmsg = NULL;
420 if (stat(logmsg_path, &st) == -1)
421 return got_error_from_errno2("stat", logmsg_path);
423 if (spawn_editor(editor, logmsg_path) == -1)
424 return got_error_from_errno("failed spawning editor");
426 if (stat(logmsg_path, &st2) == -1)
427 return got_error_from_errno("stat");
429 if (st.st_mtime == st2.st_mtime && st.st_size == st2.st_size)
430 return got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
431 "no changes made to commit message, aborting");
433 *logmsg = malloc(st2.st_size + 1);
434 if (*logmsg == NULL)
435 return got_error_from_errno("malloc");
436 (*logmsg)[0] = '\0';
437 len = 0;
439 fp = fopen(logmsg_path, "r");
440 if (fp == NULL) {
441 err = got_error_from_errno("fopen");
442 goto done;
444 while (fgets(buf, sizeof(buf), fp) != NULL) {
445 if (!content_changed && strcmp(buf, initial_content) != 0)
446 content_changed = 1;
447 if (buf[0] == '#' || (len == 0 && buf[0] == '\n'))
448 continue; /* remove comments and leading empty lines */
449 len = strlcat(*logmsg, buf, st2.st_size);
451 fclose(fp);
453 while (len > 0 && (*logmsg)[len - 1] == '\n') {
454 (*logmsg)[len - 1] = '\0';
455 len--;
458 if (len == 0 || !content_changed)
459 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
460 "commit message cannot be empty, aborting");
461 done:
462 if (err) {
463 free(*logmsg);
464 *logmsg = NULL;
466 return err;
469 static const struct got_error *
470 collect_import_msg(char **logmsg, char **logmsg_path, const char *editor,
471 const char *path_dir, const char *branch_name)
473 char *initial_content = NULL;
474 const struct got_error *err = NULL;
475 int fd;
477 if (asprintf(&initial_content,
478 "\n# %s to be imported to branch %s\n", path_dir,
479 branch_name) == -1)
480 return got_error_from_errno("asprintf");
482 err = got_opentemp_named_fd(logmsg_path, &fd, "/tmp/got-importmsg");
483 if (err)
484 goto done;
486 dprintf(fd, initial_content);
487 close(fd);
489 err = edit_logmsg(logmsg, editor, *logmsg_path, initial_content);
490 done:
491 free(initial_content);
492 return err;
495 static const struct got_error *
496 import_progress(void *arg, const char *path)
498 printf("A %s\n", path);
499 return NULL;
502 static const struct got_error *
503 get_author(char **author, struct got_repository *repo)
505 const struct got_error *err = NULL;
506 const char *got_author, *name, *email;
508 *author = NULL;
510 name = got_repo_get_gitconfig_author_name(repo);
511 email = got_repo_get_gitconfig_author_email(repo);
512 if (name && email) {
513 if (asprintf(author, "%s <%s>", name, email) == -1)
514 return got_error_from_errno("asprintf");
515 return NULL;
518 got_author = getenv("GOT_AUTHOR");
519 if (got_author == NULL) {
520 name = got_repo_get_global_gitconfig_author_name(repo);
521 email = got_repo_get_global_gitconfig_author_email(repo);
522 if (name && email) {
523 if (asprintf(author, "%s <%s>", name, email) == -1)
524 return got_error_from_errno("asprintf");
525 return NULL;
527 /* TODO: Look up user in password database? */
528 return got_error(GOT_ERR_COMMIT_NO_AUTHOR);
531 *author = strdup(got_author);
532 if (*author == NULL)
533 return got_error_from_errno("strdup");
535 /*
536 * Really dumb email address check; we're only doing this to
537 * avoid git's object parser breaking on commits we create.
538 */
539 while (*got_author && *got_author != '<')
540 got_author++;
541 if (*got_author != '<') {
542 err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
543 goto done;
545 while (*got_author && *got_author != '@')
546 got_author++;
547 if (*got_author != '@') {
548 err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
549 goto done;
551 while (*got_author && *got_author != '>')
552 got_author++;
553 if (*got_author != '>')
554 err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
555 done:
556 if (err) {
557 free(*author);
558 *author = NULL;
560 return err;
563 static const struct got_error *
564 get_gitconfig_path(char **gitconfig_path)
566 const char *homedir = getenv("HOME");
568 *gitconfig_path = NULL;
569 if (homedir) {
570 if (asprintf(gitconfig_path, "%s/.gitconfig", homedir) == -1)
571 return got_error_from_errno("asprintf");
574 return NULL;
577 static const struct got_error *
578 cmd_import(int argc, char *argv[])
580 const struct got_error *error = NULL;
581 char *path_dir = NULL, *repo_path = NULL, *logmsg = NULL;
582 char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
583 const char *branch_name = "main";
584 char *refname = NULL, *id_str = NULL, *logmsg_path = NULL;
585 struct got_repository *repo = NULL;
586 struct got_reference *branch_ref = NULL, *head_ref = NULL;
587 struct got_object_id *new_commit_id = NULL;
588 int ch;
589 struct got_pathlist_head ignores;
590 struct got_pathlist_entry *pe;
591 int preserve_logmsg = 0;
593 TAILQ_INIT(&ignores);
595 while ((ch = getopt(argc, argv, "b:m:r:I:")) != -1) {
596 switch (ch) {
597 case 'b':
598 branch_name = optarg;
599 break;
600 case 'm':
601 logmsg = strdup(optarg);
602 if (logmsg == NULL) {
603 error = got_error_from_errno("strdup");
604 goto done;
606 break;
607 case 'r':
608 repo_path = realpath(optarg, NULL);
609 if (repo_path == NULL) {
610 error = got_error_from_errno2("realpath",
611 optarg);
612 goto done;
614 break;
615 case 'I':
616 if (optarg[0] == '\0')
617 break;
618 error = got_pathlist_insert(&pe, &ignores, optarg,
619 NULL);
620 if (error)
621 goto done;
622 break;
623 default:
624 usage_import();
625 /* NOTREACHED */
629 argc -= optind;
630 argv += optind;
632 #ifndef PROFILE
633 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
634 "unveil",
635 NULL) == -1)
636 err(1, "pledge");
637 #endif
638 if (argc != 1)
639 usage_import();
641 if (repo_path == NULL) {
642 repo_path = getcwd(NULL, 0);
643 if (repo_path == NULL)
644 return got_error_from_errno("getcwd");
646 got_path_strip_trailing_slashes(repo_path);
647 error = get_gitconfig_path(&gitconfig_path);
648 if (error)
649 goto done;
650 error = got_repo_open(&repo, repo_path, gitconfig_path);
651 if (error)
652 goto done;
654 error = get_author(&author, repo);
655 if (error)
656 return error;
658 /*
659 * Don't let the user create a branch name with a leading '-'.
660 * While technically a valid reference name, this case is usually
661 * an unintended typo.
662 */
663 if (branch_name[0] == '-')
664 return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
666 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
667 error = got_error_from_errno("asprintf");
668 goto done;
671 error = got_ref_open(&branch_ref, repo, refname, 0);
672 if (error) {
673 if (error->code != GOT_ERR_NOT_REF)
674 goto done;
675 } else {
676 error = got_error_msg(GOT_ERR_BRANCH_EXISTS,
677 "import target branch already exists");
678 goto done;
681 path_dir = realpath(argv[0], NULL);
682 if (path_dir == NULL) {
683 error = got_error_from_errno2("realpath", argv[0]);
684 goto done;
686 got_path_strip_trailing_slashes(path_dir);
688 /*
689 * unveil(2) traverses exec(2); if an editor is used we have
690 * to apply unveil after the log message has been written.
691 */
692 if (logmsg == NULL || strlen(logmsg) == 0) {
693 error = get_editor(&editor);
694 if (error)
695 goto done;
696 free(logmsg);
697 error = collect_import_msg(&logmsg, &logmsg_path, editor,
698 path_dir, refname);
699 if (error) {
700 if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
701 logmsg_path != NULL)
702 preserve_logmsg = 1;
703 goto done;
707 if (unveil(path_dir, "r") != 0) {
708 error = got_error_from_errno2("unveil", path_dir);
709 if (logmsg_path)
710 preserve_logmsg = 1;
711 goto done;
714 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
715 if (error) {
716 if (logmsg_path)
717 preserve_logmsg = 1;
718 goto done;
721 error = got_repo_import(&new_commit_id, path_dir, logmsg,
722 author, &ignores, repo, import_progress, NULL);
723 if (error) {
724 if (logmsg_path)
725 preserve_logmsg = 1;
726 goto done;
729 error = got_ref_alloc(&branch_ref, refname, new_commit_id);
730 if (error) {
731 if (logmsg_path)
732 preserve_logmsg = 1;
733 goto done;
736 error = got_ref_write(branch_ref, repo);
737 if (error) {
738 if (logmsg_path)
739 preserve_logmsg = 1;
740 goto done;
743 error = got_object_id_str(&id_str, new_commit_id);
744 if (error) {
745 if (logmsg_path)
746 preserve_logmsg = 1;
747 goto done;
750 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
751 if (error) {
752 if (error->code != GOT_ERR_NOT_REF) {
753 if (logmsg_path)
754 preserve_logmsg = 1;
755 goto done;
758 error = got_ref_alloc_symref(&head_ref, GOT_REF_HEAD,
759 branch_ref);
760 if (error) {
761 if (logmsg_path)
762 preserve_logmsg = 1;
763 goto done;
766 error = got_ref_write(head_ref, repo);
767 if (error) {
768 if (logmsg_path)
769 preserve_logmsg = 1;
770 goto done;
774 printf("Created branch %s with commit %s\n",
775 got_ref_get_name(branch_ref), id_str);
776 done:
777 if (preserve_logmsg) {
778 fprintf(stderr, "%s: log message preserved in %s\n",
779 getprogname(), logmsg_path);
780 } else if (logmsg_path && unlink(logmsg_path) == -1 && error == NULL)
781 error = got_error_from_errno2("unlink", logmsg_path);
782 free(logmsg);
783 free(logmsg_path);
784 free(repo_path);
785 free(editor);
786 free(refname);
787 free(new_commit_id);
788 free(id_str);
789 free(author);
790 free(gitconfig_path);
791 if (branch_ref)
792 got_ref_close(branch_ref);
793 if (head_ref)
794 got_ref_close(head_ref);
795 return error;
798 __dead static void
799 usage_checkout(void)
801 fprintf(stderr, "usage: %s checkout [-b branch] [-c commit] "
802 "[-p prefix] repository-path [worktree-path]\n", getprogname());
803 exit(1);
806 static const struct got_error *
807 checkout_progress(void *arg, unsigned char status, const char *path)
809 char *worktree_path = arg;
811 /* Base commit bump happens silently. */
812 if (status == GOT_STATUS_BUMP_BASE)
813 return NULL;
815 while (path[0] == '/')
816 path++;
818 printf("%c %s/%s\n", status, worktree_path, path);
819 return NULL;
822 static const struct got_error *
823 check_cancelled(void *arg)
825 if (sigint_received || sigpipe_received)
826 return got_error(GOT_ERR_CANCELLED);
827 return NULL;
830 static const struct got_error *
831 check_linear_ancestry(struct got_object_id *commit_id,
832 struct got_object_id *base_commit_id, int allow_forwards_in_time_only,
833 struct got_repository *repo)
835 const struct got_error *err = NULL;
836 struct got_object_id *yca_id;
838 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
839 commit_id, base_commit_id, repo, check_cancelled, NULL);
840 if (err)
841 return err;
843 if (yca_id == NULL)
844 return got_error(GOT_ERR_ANCESTRY);
846 /*
847 * Require a straight line of history between the target commit
848 * and the work tree's base commit.
850 * Non-linear situations such as this require a rebase:
852 * (commit) D F (base_commit)
853 * \ /
854 * C E
855 * \ /
856 * B (yca)
857 * |
858 * A
860 * 'got update' only handles linear cases:
861 * Update forwards in time: A (base/yca) - B - C - D (commit)
862 * Update backwards in time: D (base) - C - B - A (commit/yca)
863 */
864 if (allow_forwards_in_time_only) {
865 if (got_object_id_cmp(base_commit_id, yca_id) != 0)
866 return got_error(GOT_ERR_ANCESTRY);
867 } else if (got_object_id_cmp(commit_id, yca_id) != 0 &&
868 got_object_id_cmp(base_commit_id, yca_id) != 0)
869 return got_error(GOT_ERR_ANCESTRY);
871 free(yca_id);
872 return NULL;
875 static const struct got_error *
876 check_same_branch(struct got_object_id *commit_id,
877 struct got_reference *head_ref, struct got_object_id *yca_id,
878 struct got_repository *repo)
880 const struct got_error *err = NULL;
881 struct got_commit_graph *graph = NULL;
882 struct got_object_id *head_commit_id = NULL;
883 int is_same_branch = 0;
885 err = got_ref_resolve(&head_commit_id, repo, head_ref);
886 if (err)
887 goto done;
889 if (got_object_id_cmp(head_commit_id, commit_id) == 0) {
890 is_same_branch = 1;
891 goto done;
893 if (yca_id && got_object_id_cmp(commit_id, yca_id) == 0) {
894 is_same_branch = 1;
895 goto done;
898 err = got_commit_graph_open(&graph, head_commit_id, "/", 1, repo);
899 if (err)
900 goto done;
902 err = got_commit_graph_iter_start(graph, head_commit_id, repo,
903 check_cancelled, NULL);
904 if (err)
905 goto done;
907 for (;;) {
908 struct got_object_id *id;
909 err = got_commit_graph_iter_next(&id, graph);
910 if (err) {
911 if (err->code == GOT_ERR_ITER_COMPLETED) {
912 err = NULL;
913 break;
914 } else if (err->code != GOT_ERR_ITER_NEED_MORE)
915 break;
916 err = got_commit_graph_fetch_commits(graph, 1,
917 repo, check_cancelled, NULL);
918 if (err)
919 break;
922 if (id) {
923 if (yca_id && got_object_id_cmp(id, yca_id) == 0)
924 break;
925 if (got_object_id_cmp(id, commit_id) == 0) {
926 is_same_branch = 1;
927 break;
931 done:
932 if (graph)
933 got_commit_graph_close(graph);
934 free(head_commit_id);
935 if (!err && !is_same_branch)
936 err = got_error(GOT_ERR_ANCESTRY);
937 return err;
940 static const struct got_error *
941 resolve_commit_arg(struct got_object_id **commit_id,
942 const char *commit_id_arg, struct got_repository *repo)
944 const struct got_error *err;
945 struct got_reference *ref;
946 struct got_tag_object *tag;
948 err = got_repo_object_match_tag(&tag, commit_id_arg,
949 GOT_OBJ_TYPE_COMMIT, repo);
950 if (err == NULL) {
951 *commit_id = got_object_id_dup(
952 got_object_tag_get_object_id(tag));
953 if (*commit_id == NULL)
954 err = got_error_from_errno("got_object_id_dup");
955 got_object_tag_close(tag);
956 return err;
957 } else if (err->code != GOT_ERR_NO_OBJ)
958 return err;
960 err = got_ref_open(&ref, repo, commit_id_arg, 0);
961 if (err == NULL) {
962 err = got_ref_resolve(commit_id, repo, ref);
963 got_ref_close(ref);
964 } else {
965 if (err->code != GOT_ERR_NOT_REF)
966 return err;
967 err = got_repo_match_object_id_prefix(commit_id,
968 commit_id_arg, GOT_OBJ_TYPE_COMMIT, repo);
970 return err;
973 static const struct got_error *
974 cmd_checkout(int argc, char *argv[])
976 const struct got_error *error = NULL;
977 struct got_repository *repo = NULL;
978 struct got_reference *head_ref = NULL;
979 struct got_worktree *worktree = NULL;
980 char *repo_path = NULL;
981 char *worktree_path = NULL;
982 const char *path_prefix = "";
983 const char *branch_name = GOT_REF_HEAD;
984 char *commit_id_str = NULL;
985 int ch, same_path_prefix;
986 struct got_pathlist_head paths;
988 TAILQ_INIT(&paths);
990 while ((ch = getopt(argc, argv, "b:c:p:")) != -1) {
991 switch (ch) {
992 case 'b':
993 branch_name = optarg;
994 break;
995 case 'c':
996 commit_id_str = strdup(optarg);
997 if (commit_id_str == NULL)
998 return got_error_from_errno("strdup");
999 break;
1000 case 'p':
1001 path_prefix = optarg;
1002 break;
1003 default:
1004 usage_checkout();
1005 /* NOTREACHED */
1009 argc -= optind;
1010 argv += optind;
1012 #ifndef PROFILE
1013 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1014 "unveil", NULL) == -1)
1015 err(1, "pledge");
1016 #endif
1017 if (argc == 1) {
1018 char *cwd, *base, *dotgit;
1019 repo_path = realpath(argv[0], NULL);
1020 if (repo_path == NULL)
1021 return got_error_from_errno2("realpath", argv[0]);
1022 cwd = getcwd(NULL, 0);
1023 if (cwd == NULL) {
1024 error = got_error_from_errno("getcwd");
1025 goto done;
1027 if (path_prefix[0]) {
1028 base = basename(path_prefix);
1029 if (base == NULL) {
1030 error = got_error_from_errno2("basename",
1031 path_prefix);
1032 goto done;
1034 } else {
1035 base = basename(repo_path);
1036 if (base == NULL) {
1037 error = got_error_from_errno2("basename",
1038 repo_path);
1039 goto done;
1042 dotgit = strstr(base, ".git");
1043 if (dotgit)
1044 *dotgit = '\0';
1045 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
1046 error = got_error_from_errno("asprintf");
1047 free(cwd);
1048 goto done;
1050 free(cwd);
1051 } else if (argc == 2) {
1052 repo_path = realpath(argv[0], NULL);
1053 if (repo_path == NULL) {
1054 error = got_error_from_errno2("realpath", argv[0]);
1055 goto done;
1057 worktree_path = realpath(argv[1], NULL);
1058 if (worktree_path == NULL) {
1059 if (errno != ENOENT) {
1060 error = got_error_from_errno2("realpath",
1061 argv[1]);
1062 goto done;
1064 worktree_path = strdup(argv[1]);
1065 if (worktree_path == NULL) {
1066 error = got_error_from_errno("strdup");
1067 goto done;
1070 } else
1071 usage_checkout();
1073 got_path_strip_trailing_slashes(repo_path);
1074 got_path_strip_trailing_slashes(worktree_path);
1076 error = got_repo_open(&repo, repo_path, NULL);
1077 if (error != NULL)
1078 goto done;
1080 /* Pre-create work tree path for unveil(2) */
1081 error = got_path_mkdir(worktree_path);
1082 if (error) {
1083 if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
1084 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
1085 goto done;
1086 if (!got_path_dir_is_empty(worktree_path)) {
1087 error = got_error_path(worktree_path,
1088 GOT_ERR_DIR_NOT_EMPTY);
1089 goto done;
1093 error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
1094 if (error)
1095 goto done;
1097 error = got_ref_open(&head_ref, repo, branch_name, 0);
1098 if (error != NULL)
1099 goto done;
1101 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
1102 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
1103 goto done;
1105 error = got_worktree_open(&worktree, worktree_path);
1106 if (error != NULL)
1107 goto done;
1109 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
1110 path_prefix);
1111 if (error != NULL)
1112 goto done;
1113 if (!same_path_prefix) {
1114 error = got_error(GOT_ERR_PATH_PREFIX);
1115 goto done;
1118 if (commit_id_str) {
1119 struct got_object_id *commit_id;
1120 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
1121 if (error)
1122 goto done;
1123 error = check_linear_ancestry(commit_id,
1124 got_worktree_get_base_commit_id(worktree), 0, repo);
1125 if (error != NULL) {
1126 free(commit_id);
1127 goto done;
1129 error = check_same_branch(commit_id, head_ref, NULL, repo);
1130 if (error)
1131 goto done;
1132 error = got_worktree_set_base_commit_id(worktree, repo,
1133 commit_id);
1134 free(commit_id);
1135 if (error)
1136 goto done;
1139 error = got_pathlist_append(&paths, "", NULL);
1140 if (error)
1141 goto done;
1142 error = got_worktree_checkout_files(worktree, &paths, repo,
1143 checkout_progress, worktree_path, check_cancelled, NULL);
1144 if (error != NULL)
1145 goto done;
1147 printf("Now shut up and hack\n");
1149 done:
1150 got_pathlist_free(&paths);
1151 free(commit_id_str);
1152 free(repo_path);
1153 free(worktree_path);
1154 return error;
1157 __dead static void
1158 usage_update(void)
1160 fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path ...]\n",
1161 getprogname());
1162 exit(1);
1165 static const struct got_error *
1166 update_progress(void *arg, unsigned char status, const char *path)
1168 int *did_something = arg;
1170 if (status == GOT_STATUS_EXISTS)
1171 return NULL;
1173 *did_something = 1;
1175 /* Base commit bump happens silently. */
1176 if (status == GOT_STATUS_BUMP_BASE)
1177 return NULL;
1179 while (path[0] == '/')
1180 path++;
1181 printf("%c %s\n", status, path);
1182 return NULL;
1185 static const struct got_error *
1186 switch_head_ref(struct got_reference *head_ref,
1187 struct got_object_id *commit_id, struct got_worktree *worktree,
1188 struct got_repository *repo)
1190 const struct got_error *err = NULL;
1191 char *base_id_str;
1192 int ref_has_moved = 0;
1194 /* Trivial case: switching between two different references. */
1195 if (strcmp(got_ref_get_name(head_ref),
1196 got_worktree_get_head_ref_name(worktree)) != 0) {
1197 printf("Switching work tree from %s to %s\n",
1198 got_worktree_get_head_ref_name(worktree),
1199 got_ref_get_name(head_ref));
1200 return got_worktree_set_head_ref(worktree, head_ref);
1203 err = check_linear_ancestry(commit_id,
1204 got_worktree_get_base_commit_id(worktree), 0, repo);
1205 if (err) {
1206 if (err->code != GOT_ERR_ANCESTRY)
1207 return err;
1208 ref_has_moved = 1;
1210 if (!ref_has_moved)
1211 return NULL;
1213 /* Switching to a rebased branch with the same reference name. */
1214 err = got_object_id_str(&base_id_str,
1215 got_worktree_get_base_commit_id(worktree));
1216 if (err)
1217 return err;
1218 printf("Reference %s now points at a different branch\n",
1219 got_worktree_get_head_ref_name(worktree));
1220 printf("Switching work tree from %s to %s\n", base_id_str,
1221 got_worktree_get_head_ref_name(worktree));
1222 return NULL;
1225 static const struct got_error *
1226 check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
1228 const struct got_error *err;
1229 int in_progress;
1231 err = got_worktree_rebase_in_progress(&in_progress, worktree);
1232 if (err)
1233 return err;
1234 if (in_progress)
1235 return got_error(GOT_ERR_REBASING);
1237 err = got_worktree_histedit_in_progress(&in_progress, worktree);
1238 if (err)
1239 return err;
1240 if (in_progress)
1241 return got_error(GOT_ERR_HISTEDIT_BUSY);
1243 return NULL;
1246 static const struct got_error *
1247 get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
1248 char *argv[], struct got_worktree *worktree)
1250 const struct got_error *err = NULL;
1251 char *path;
1252 int i;
1254 if (argc == 0) {
1255 path = strdup("");
1256 if (path == NULL)
1257 return got_error_from_errno("strdup");
1258 return got_pathlist_append(paths, path, NULL);
1261 for (i = 0; i < argc; i++) {
1262 err = got_worktree_resolve_path(&path, worktree, argv[i]);
1263 if (err)
1264 break;
1265 err = got_pathlist_append(paths, path, NULL);
1266 if (err) {
1267 free(path);
1268 break;
1272 return err;
1275 static const struct got_error *
1276 cmd_update(int argc, char *argv[])
1278 const struct got_error *error = NULL;
1279 struct got_repository *repo = NULL;
1280 struct got_worktree *worktree = NULL;
1281 char *worktree_path = NULL;
1282 struct got_object_id *commit_id = NULL;
1283 char *commit_id_str = NULL;
1284 const char *branch_name = NULL;
1285 struct got_reference *head_ref = NULL;
1286 struct got_pathlist_head paths;
1287 struct got_pathlist_entry *pe;
1288 int ch, did_something = 0;
1290 TAILQ_INIT(&paths);
1292 while ((ch = getopt(argc, argv, "b:c:")) != -1) {
1293 switch (ch) {
1294 case 'b':
1295 branch_name = optarg;
1296 break;
1297 case 'c':
1298 commit_id_str = strdup(optarg);
1299 if (commit_id_str == NULL)
1300 return got_error_from_errno("strdup");
1301 break;
1302 default:
1303 usage_update();
1304 /* NOTREACHED */
1308 argc -= optind;
1309 argv += optind;
1311 #ifndef PROFILE
1312 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1313 "unveil", NULL) == -1)
1314 err(1, "pledge");
1315 #endif
1316 worktree_path = getcwd(NULL, 0);
1317 if (worktree_path == NULL) {
1318 error = got_error_from_errno("getcwd");
1319 goto done;
1321 error = got_worktree_open(&worktree, worktree_path);
1322 if (error)
1323 goto done;
1325 error = check_rebase_or_histedit_in_progress(worktree);
1326 if (error)
1327 goto done;
1329 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
1330 NULL);
1331 if (error != NULL)
1332 goto done;
1334 error = apply_unveil(got_repo_get_path(repo), 0,
1335 got_worktree_get_root_path(worktree));
1336 if (error)
1337 goto done;
1339 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
1340 if (error)
1341 goto done;
1343 error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
1344 got_worktree_get_head_ref_name(worktree), 0);
1345 if (error != NULL)
1346 goto done;
1347 if (commit_id_str == NULL) {
1348 error = got_ref_resolve(&commit_id, repo, head_ref);
1349 if (error != NULL)
1350 goto done;
1351 error = got_object_id_str(&commit_id_str, commit_id);
1352 if (error != NULL)
1353 goto done;
1354 } else {
1355 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
1356 free(commit_id_str);
1357 commit_id_str = NULL;
1358 if (error)
1359 goto done;
1360 error = got_object_id_str(&commit_id_str, commit_id);
1361 if (error)
1362 goto done;
1365 if (branch_name) {
1366 struct got_object_id *head_commit_id;
1367 TAILQ_FOREACH(pe, &paths, entry) {
1368 if (pe->path_len == 0)
1369 continue;
1370 error = got_error_msg(GOT_ERR_BAD_PATH,
1371 "switching between branches requires that "
1372 "the entire work tree gets updated");
1373 goto done;
1375 error = got_ref_resolve(&head_commit_id, repo, head_ref);
1376 if (error)
1377 goto done;
1378 error = check_linear_ancestry(commit_id, head_commit_id, 0,
1379 repo);
1380 free(head_commit_id);
1381 if (error != NULL)
1382 goto done;
1383 error = check_same_branch(commit_id, head_ref, NULL, repo);
1384 if (error)
1385 goto done;
1386 error = switch_head_ref(head_ref, commit_id, worktree, repo);
1387 if (error)
1388 goto done;
1389 } else {
1390 error = check_linear_ancestry(commit_id,
1391 got_worktree_get_base_commit_id(worktree), 0, repo);
1392 if (error != NULL) {
1393 if (error->code == GOT_ERR_ANCESTRY)
1394 error = got_error(GOT_ERR_BRANCH_MOVED);
1395 goto done;
1397 error = check_same_branch(commit_id, head_ref, NULL, repo);
1398 if (error)
1399 goto done;
1402 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
1403 commit_id) != 0) {
1404 error = got_worktree_set_base_commit_id(worktree, repo,
1405 commit_id);
1406 if (error)
1407 goto done;
1410 error = got_worktree_checkout_files(worktree, &paths, repo,
1411 update_progress, &did_something, check_cancelled, NULL);
1412 if (error != NULL)
1413 goto done;
1415 if (did_something)
1416 printf("Updated to commit %s\n", commit_id_str);
1417 else
1418 printf("Already up-to-date\n");
1419 done:
1420 free(worktree_path);
1421 TAILQ_FOREACH(pe, &paths, entry)
1422 free((char *)pe->path);
1423 got_pathlist_free(&paths);
1424 free(commit_id);
1425 free(commit_id_str);
1426 return error;
1429 static const struct got_error *
1430 diff_blobs(struct got_object_id *blob_id1, struct got_object_id *blob_id2,
1431 const char *path, int diff_context, int ignore_whitespace,
1432 struct got_repository *repo)
1434 const struct got_error *err = NULL;
1435 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
1437 if (blob_id1) {
1438 err = got_object_open_as_blob(&blob1, repo, blob_id1, 8192);
1439 if (err)
1440 goto done;
1443 err = got_object_open_as_blob(&blob2, repo, blob_id2, 8192);
1444 if (err)
1445 goto done;
1447 while (path[0] == '/')
1448 path++;
1449 err = got_diff_blob(blob1, blob2, path, path, diff_context,
1450 ignore_whitespace, stdout);
1451 done:
1452 if (blob1)
1453 got_object_blob_close(blob1);
1454 got_object_blob_close(blob2);
1455 return err;
1458 static const struct got_error *
1459 diff_trees(struct got_object_id *tree_id1, struct got_object_id *tree_id2,
1460 const char *path, int diff_context, int ignore_whitespace,
1461 struct got_repository *repo)
1463 const struct got_error *err = NULL;
1464 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
1465 struct got_diff_blob_output_unidiff_arg arg;
1467 if (tree_id1) {
1468 err = got_object_open_as_tree(&tree1, repo, tree_id1);
1469 if (err)
1470 goto done;
1473 err = got_object_open_as_tree(&tree2, repo, tree_id2);
1474 if (err)
1475 goto done;
1477 arg.diff_context = diff_context;
1478 arg.ignore_whitespace = ignore_whitespace;
1479 arg.outfile = stdout;
1480 while (path[0] == '/')
1481 path++;
1482 err = got_diff_tree(tree1, tree2, path, path, repo,
1483 got_diff_blob_output_unidiff, &arg, 1);
1484 done:
1485 if (tree1)
1486 got_object_tree_close(tree1);
1487 if (tree2)
1488 got_object_tree_close(tree2);
1489 return err;
1492 static const struct got_error *
1493 print_patch(struct got_commit_object *commit, struct got_object_id *id,
1494 const char *path, int diff_context, struct got_repository *repo)
1496 const struct got_error *err = NULL;
1497 struct got_commit_object *pcommit = NULL;
1498 char *id_str1 = NULL, *id_str2 = NULL;
1499 struct got_object_id *obj_id1 = NULL, *obj_id2 = NULL;
1500 struct got_object_qid *qid;
1502 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1503 if (qid != NULL) {
1504 err = got_object_open_as_commit(&pcommit, repo,
1505 qid->id);
1506 if (err)
1507 return err;
1510 if (path && path[0] != '\0') {
1511 int obj_type;
1512 err = got_object_id_by_path(&obj_id2, repo, id, path);
1513 if (err)
1514 goto done;
1515 err = got_object_id_str(&id_str2, obj_id2);
1516 if (err) {
1517 free(obj_id2);
1518 goto done;
1520 if (pcommit) {
1521 err = got_object_id_by_path(&obj_id1, repo,
1522 qid->id, path);
1523 if (err) {
1524 free(obj_id2);
1525 goto done;
1527 err = got_object_id_str(&id_str1, obj_id1);
1528 if (err) {
1529 free(obj_id2);
1530 goto done;
1533 err = got_object_get_type(&obj_type, repo, obj_id2);
1534 if (err) {
1535 free(obj_id2);
1536 goto done;
1538 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1539 switch (obj_type) {
1540 case GOT_OBJ_TYPE_BLOB:
1541 err = diff_blobs(obj_id1, obj_id2, path, diff_context,
1542 0, repo);
1543 break;
1544 case GOT_OBJ_TYPE_TREE:
1545 err = diff_trees(obj_id1, obj_id2, path, diff_context,
1546 0, repo);
1547 break;
1548 default:
1549 err = got_error(GOT_ERR_OBJ_TYPE);
1550 break;
1552 free(obj_id1);
1553 free(obj_id2);
1554 } else {
1555 obj_id2 = got_object_commit_get_tree_id(commit);
1556 err = got_object_id_str(&id_str2, obj_id2);
1557 if (err)
1558 goto done;
1559 obj_id1 = got_object_commit_get_tree_id(pcommit);
1560 err = got_object_id_str(&id_str1, obj_id1);
1561 if (err)
1562 goto done;
1563 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1564 err = diff_trees(obj_id1, obj_id2, "", diff_context, 0, repo);
1567 done:
1568 free(id_str1);
1569 free(id_str2);
1570 if (pcommit)
1571 got_object_commit_close(pcommit);
1572 return err;
1575 static char *
1576 get_datestr(time_t *time, char *datebuf)
1578 struct tm mytm, *tm;
1579 char *p, *s;
1581 tm = gmtime_r(time, &mytm);
1582 if (tm == NULL)
1583 return NULL;
1584 s = asctime_r(tm, datebuf);
1585 if (s == NULL)
1586 return NULL;
1587 p = strchr(s, '\n');
1588 if (p)
1589 *p = '\0';
1590 return s;
1593 static const struct got_error *
1594 match_logmsg(int *have_match, struct got_object_id *id,
1595 struct got_commit_object *commit, regex_t *regex)
1597 const struct got_error *err = NULL;
1598 regmatch_t regmatch;
1599 char *id_str = NULL, *logmsg = NULL;
1601 *have_match = 0;
1603 err = got_object_id_str(&id_str, id);
1604 if (err)
1605 return err;
1607 err = got_object_commit_get_logmsg(&logmsg, commit);
1608 if (err)
1609 goto done;
1611 if (regexec(regex, logmsg, 1, &regmatch, 0) == 0)
1612 *have_match = 1;
1613 done:
1614 free(id_str);
1615 free(logmsg);
1616 return err;
1619 #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
1621 static const struct got_error *
1622 print_commit(struct got_commit_object *commit, struct got_object_id *id,
1623 struct got_repository *repo, const char *path, int show_patch,
1624 int diff_context, struct got_reflist_head *refs)
1626 const struct got_error *err = NULL;
1627 char *id_str, *datestr, *logmsg0, *logmsg, *line;
1628 char datebuf[26];
1629 time_t committer_time;
1630 const char *author, *committer;
1631 char *refs_str = NULL;
1632 struct got_reflist_entry *re;
1634 SIMPLEQ_FOREACH(re, refs, entry) {
1635 char *s;
1636 const char *name;
1637 struct got_tag_object *tag = NULL;
1638 int cmp;
1640 name = got_ref_get_name(re->ref);
1641 if (strcmp(name, GOT_REF_HEAD) == 0)
1642 continue;
1643 if (strncmp(name, "refs/", 5) == 0)
1644 name += 5;
1645 if (strncmp(name, "got/", 4) == 0)
1646 continue;
1647 if (strncmp(name, "heads/", 6) == 0)
1648 name += 6;
1649 if (strncmp(name, "remotes/", 8) == 0)
1650 name += 8;
1651 if (strncmp(name, "tags/", 5) == 0) {
1652 err = got_object_open_as_tag(&tag, repo, re->id);
1653 if (err) {
1654 if (err->code != GOT_ERR_OBJ_TYPE)
1655 return err;
1656 /* Ref points at something other than a tag. */
1657 err = NULL;
1658 tag = NULL;
1661 cmp = got_object_id_cmp(tag ?
1662 got_object_tag_get_object_id(tag) : re->id, id);
1663 if (tag)
1664 got_object_tag_close(tag);
1665 if (cmp != 0)
1666 continue;
1667 s = refs_str;
1668 if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
1669 name) == -1) {
1670 err = got_error_from_errno("asprintf");
1671 free(s);
1672 return err;
1674 free(s);
1676 err = got_object_id_str(&id_str, id);
1677 if (err)
1678 return err;
1680 printf(GOT_COMMIT_SEP_STR);
1681 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
1682 refs_str ? refs_str : "", refs_str ? ")" : "");
1683 free(id_str);
1684 id_str = NULL;
1685 free(refs_str);
1686 refs_str = NULL;
1687 printf("from: %s\n", got_object_commit_get_author(commit));
1688 committer_time = got_object_commit_get_committer_time(commit);
1689 datestr = get_datestr(&committer_time, datebuf);
1690 if (datestr)
1691 printf("date: %s UTC\n", datestr);
1692 author = got_object_commit_get_author(commit);
1693 committer = got_object_commit_get_committer(commit);
1694 if (strcmp(author, committer) != 0)
1695 printf("via: %s\n", committer);
1696 if (got_object_commit_get_nparents(commit) > 1) {
1697 const struct got_object_id_queue *parent_ids;
1698 struct got_object_qid *qid;
1699 int n = 1;
1700 parent_ids = got_object_commit_get_parent_ids(commit);
1701 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
1702 err = got_object_id_str(&id_str, qid->id);
1703 if (err)
1704 return err;
1705 printf("parent %d: %s\n", n++, id_str);
1706 free(id_str);
1710 err = got_object_commit_get_logmsg(&logmsg0, commit);
1711 if (err)
1712 return err;
1714 logmsg = logmsg0;
1715 do {
1716 line = strsep(&logmsg, "\n");
1717 if (line)
1718 printf(" %s\n", line);
1719 } while (line);
1720 free(logmsg0);
1722 if (show_patch) {
1723 err = print_patch(commit, id, path, diff_context, repo);
1724 if (err == 0)
1725 printf("\n");
1728 if (fflush(stdout) != 0 && err == NULL)
1729 err = got_error_from_errno("fflush");
1730 return err;
1733 static const struct got_error *
1734 print_commits(struct got_object_id *root_id, struct got_repository *repo,
1735 const char *path, int show_patch, const char *search_pattern,
1736 int diff_context, int limit, int first_parent_traversal,
1737 struct got_reflist_head *refs)
1739 const struct got_error *err;
1740 struct got_commit_graph *graph;
1741 regex_t regex;
1742 int have_match;
1744 if (search_pattern &&
1745 regcomp(&regex, search_pattern, REG_EXTENDED | REG_NOSUB | REG_NEWLINE))
1746 return got_error_msg(GOT_ERR_REGEX, search_pattern);
1748 err = got_commit_graph_open(&graph, root_id, path,
1749 first_parent_traversal, repo);
1750 if (err)
1751 return err;
1752 err = got_commit_graph_iter_start(graph, root_id, repo,
1753 check_cancelled, NULL);
1754 if (err)
1755 goto done;
1756 for (;;) {
1757 struct got_commit_object *commit;
1758 struct got_object_id *id;
1760 if (sigint_received || sigpipe_received)
1761 break;
1763 err = got_commit_graph_iter_next(&id, graph);
1764 if (err) {
1765 if (err->code == GOT_ERR_ITER_COMPLETED) {
1766 err = NULL;
1767 break;
1769 if (err->code != GOT_ERR_ITER_NEED_MORE)
1770 break;
1771 err = got_commit_graph_fetch_commits(graph, 1, repo,
1772 check_cancelled, NULL);
1773 if (err)
1774 break;
1775 else
1776 continue;
1778 if (id == NULL)
1779 break;
1781 err = got_object_open_as_commit(&commit, repo, id);
1782 if (err)
1783 break;
1785 if (search_pattern) {
1786 err = match_logmsg(&have_match, id, commit, &regex);
1787 if (err) {
1788 got_object_commit_close(commit);
1789 break;
1791 if (have_match == 0) {
1792 got_object_commit_close(commit);
1793 continue;
1797 err = print_commit(commit, id, repo, path, show_patch,
1798 diff_context, refs);
1799 got_object_commit_close(commit);
1800 if (err || (limit && --limit == 0))
1801 break;
1803 done:
1804 if (search_pattern)
1805 regfree(&regex);
1806 got_commit_graph_close(graph);
1807 return err;
1810 __dead static void
1811 usage_log(void)
1813 fprintf(stderr, "usage: %s log [-c commit] [-C number] [-f] [ -l N ] [-p] "
1814 "[-s search-pattern] [-r repository-path] [path]\n", getprogname());
1815 exit(1);
1818 static int
1819 get_default_log_limit(void)
1821 const char *got_default_log_limit;
1822 long long n;
1823 const char *errstr;
1825 got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
1826 if (got_default_log_limit == NULL)
1827 return 0;
1828 n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
1829 if (errstr != NULL)
1830 return 0;
1831 return n;
1834 static const struct got_error *
1835 cmd_log(int argc, char *argv[])
1837 const struct got_error *error;
1838 struct got_repository *repo = NULL;
1839 struct got_worktree *worktree = NULL;
1840 struct got_commit_object *commit = NULL;
1841 struct got_object_id *id = NULL;
1842 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
1843 const char *start_commit = NULL, *search_pattern = NULL;
1844 int diff_context = -1, ch;
1845 int show_patch = 0, limit = 0, first_parent_traversal = 0;
1846 const char *errstr;
1847 struct got_reflist_head refs;
1849 SIMPLEQ_INIT(&refs);
1851 #ifndef PROFILE
1852 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1853 NULL)
1854 == -1)
1855 err(1, "pledge");
1856 #endif
1858 limit = get_default_log_limit();
1860 while ((ch = getopt(argc, argv, "b:pc:C:l:fr:s:")) != -1) {
1861 switch (ch) {
1862 case 'p':
1863 show_patch = 1;
1864 break;
1865 case 'c':
1866 start_commit = optarg;
1867 break;
1868 case 'C':
1869 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
1870 &errstr);
1871 if (errstr != NULL)
1872 err(1, "-C option %s", errstr);
1873 break;
1874 case 'l':
1875 limit = strtonum(optarg, 0, INT_MAX, &errstr);
1876 if (errstr != NULL)
1877 err(1, "-l option %s", errstr);
1878 break;
1879 case 'f':
1880 first_parent_traversal = 1;
1881 break;
1882 case 'r':
1883 repo_path = realpath(optarg, NULL);
1884 if (repo_path == NULL)
1885 return got_error_from_errno2("realpath",
1886 optarg);
1887 got_path_strip_trailing_slashes(repo_path);
1888 break;
1889 case 's':
1890 search_pattern = optarg;
1891 break;
1892 default:
1893 usage_log();
1894 /* NOTREACHED */
1898 argc -= optind;
1899 argv += optind;
1901 if (diff_context == -1)
1902 diff_context = 3;
1903 else if (!show_patch)
1904 errx(1, "-C reguires -p");
1906 cwd = getcwd(NULL, 0);
1907 if (cwd == NULL) {
1908 error = got_error_from_errno("getcwd");
1909 goto done;
1912 error = got_worktree_open(&worktree, cwd);
1913 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1914 goto done;
1915 error = NULL;
1917 if (argc == 0) {
1918 path = strdup("");
1919 if (path == NULL) {
1920 error = got_error_from_errno("strdup");
1921 goto done;
1923 } else if (argc == 1) {
1924 if (worktree) {
1925 error = got_worktree_resolve_path(&path, worktree,
1926 argv[0]);
1927 if (error)
1928 goto done;
1929 } else {
1930 path = strdup(argv[0]);
1931 if (path == NULL) {
1932 error = got_error_from_errno("strdup");
1933 goto done;
1936 } else
1937 usage_log();
1939 if (repo_path == NULL) {
1940 repo_path = worktree ?
1941 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
1943 if (repo_path == NULL) {
1944 error = got_error_from_errno("strdup");
1945 goto done;
1948 error = got_repo_open(&repo, repo_path, NULL);
1949 if (error != NULL)
1950 goto done;
1952 error = apply_unveil(got_repo_get_path(repo), 1,
1953 worktree ? got_worktree_get_root_path(worktree) : NULL);
1954 if (error)
1955 goto done;
1957 if (start_commit == NULL) {
1958 struct got_reference *head_ref;
1959 error = got_ref_open(&head_ref, repo,
1960 worktree ? got_worktree_get_head_ref_name(worktree)
1961 : GOT_REF_HEAD, 0);
1962 if (error != NULL)
1963 return error;
1964 error = got_ref_resolve(&id, repo, head_ref);
1965 got_ref_close(head_ref);
1966 if (error != NULL)
1967 return error;
1968 error = got_object_open_as_commit(&commit, repo, id);
1969 } else {
1970 struct got_reference *ref;
1971 error = got_ref_open(&ref, repo, start_commit, 0);
1972 if (error == NULL) {
1973 int obj_type;
1974 error = got_ref_resolve(&id, repo, ref);
1975 got_ref_close(ref);
1976 if (error != NULL)
1977 goto done;
1978 error = got_object_get_type(&obj_type, repo, id);
1979 if (error != NULL)
1980 goto done;
1981 if (obj_type == GOT_OBJ_TYPE_TAG) {
1982 struct got_tag_object *tag;
1983 error = got_object_open_as_tag(&tag, repo, id);
1984 if (error != NULL)
1985 goto done;
1986 if (got_object_tag_get_object_type(tag) !=
1987 GOT_OBJ_TYPE_COMMIT) {
1988 got_object_tag_close(tag);
1989 error = got_error(GOT_ERR_OBJ_TYPE);
1990 goto done;
1992 free(id);
1993 id = got_object_id_dup(
1994 got_object_tag_get_object_id(tag));
1995 if (id == NULL)
1996 error = got_error_from_errno(
1997 "got_object_id_dup");
1998 got_object_tag_close(tag);
1999 if (error)
2000 goto done;
2001 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
2002 error = got_error(GOT_ERR_OBJ_TYPE);
2003 goto done;
2005 error = got_object_open_as_commit(&commit, repo, id);
2006 if (error != NULL)
2007 goto done;
2009 if (commit == NULL) {
2010 error = got_repo_match_object_id_prefix(&id,
2011 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
2012 if (error != NULL)
2013 return error;
2016 if (error != NULL)
2017 goto done;
2019 if (worktree) {
2020 const char *prefix = got_worktree_get_path_prefix(worktree);
2021 char *p;
2022 if (asprintf(&p, "%s%s%s", prefix,
2023 (strcmp(prefix, "/") != 0) ? "/" : "", path) == -1) {
2024 error = got_error_from_errno("asprintf");
2025 goto done;
2027 error = got_repo_map_path(&in_repo_path, repo, p, 1);
2028 free(p);
2029 } else
2030 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2031 if (error != NULL)
2032 goto done;
2033 if (in_repo_path) {
2034 free(path);
2035 path = in_repo_path;
2038 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
2039 if (error)
2040 goto done;
2042 error = print_commits(id, repo, path, show_patch, search_pattern,
2043 diff_context, limit, first_parent_traversal, &refs);
2044 done:
2045 free(path);
2046 free(repo_path);
2047 free(cwd);
2048 free(id);
2049 if (worktree)
2050 got_worktree_close(worktree);
2051 if (repo) {
2052 const struct got_error *repo_error;
2053 repo_error = got_repo_close(repo);
2054 if (error == NULL)
2055 error = repo_error;
2057 got_ref_list_free(&refs);
2058 return error;
2061 __dead static void
2062 usage_diff(void)
2064 fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] [-s] "
2065 "[-w] [object1 object2 | path]\n", getprogname());
2066 exit(1);
2069 struct print_diff_arg {
2070 struct got_repository *repo;
2071 struct got_worktree *worktree;
2072 int diff_context;
2073 const char *id_str;
2074 int header_shown;
2075 int diff_staged;
2076 int ignore_whitespace;
2079 static const struct got_error *
2080 print_diff(void *arg, unsigned char status, unsigned char staged_status,
2081 const char *path, struct got_object_id *blob_id,
2082 struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
2084 struct print_diff_arg *a = arg;
2085 const struct got_error *err = NULL;
2086 struct got_blob_object *blob1 = NULL;
2087 FILE *f2 = NULL;
2088 char *abspath = NULL, *label1 = NULL;
2089 struct stat sb;
2091 if (a->diff_staged) {
2092 if (staged_status != GOT_STATUS_MODIFY &&
2093 staged_status != GOT_STATUS_ADD &&
2094 staged_status != GOT_STATUS_DELETE)
2095 return NULL;
2096 } else {
2097 if (staged_status == GOT_STATUS_DELETE)
2098 return NULL;
2099 if (status == GOT_STATUS_NONEXISTENT)
2100 return got_error_set_errno(ENOENT, path);
2101 if (status != GOT_STATUS_MODIFY &&
2102 status != GOT_STATUS_ADD &&
2103 status != GOT_STATUS_DELETE &&
2104 status != GOT_STATUS_CONFLICT)
2105 return NULL;
2108 if (!a->header_shown) {
2109 printf("diff %s %s%s\n", a->id_str,
2110 got_worktree_get_root_path(a->worktree),
2111 a->diff_staged ? " (staged changes)" : "");
2112 a->header_shown = 1;
2115 if (a->diff_staged) {
2116 const char *label1 = NULL, *label2 = NULL;
2117 switch (staged_status) {
2118 case GOT_STATUS_MODIFY:
2119 label1 = path;
2120 label2 = path;
2121 break;
2122 case GOT_STATUS_ADD:
2123 label2 = path;
2124 break;
2125 case GOT_STATUS_DELETE:
2126 label1 = path;
2127 break;
2128 default:
2129 return got_error(GOT_ERR_FILE_STATUS);
2131 return got_diff_objects_as_blobs(blob_id, staged_blob_id,
2132 label1, label2, a->diff_context, a->ignore_whitespace,
2133 a->repo, stdout);
2136 if (staged_status == GOT_STATUS_ADD ||
2137 staged_status == GOT_STATUS_MODIFY) {
2138 char *id_str;
2139 err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
2140 8192);
2141 if (err)
2142 goto done;
2143 err = got_object_id_str(&id_str, staged_blob_id);
2144 if (err)
2145 goto done;
2146 if (asprintf(&label1, "%s (staged)", id_str) == -1) {
2147 err = got_error_from_errno("asprintf");
2148 free(id_str);
2149 goto done;
2151 free(id_str);
2152 } else if (status != GOT_STATUS_ADD) {
2153 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
2154 if (err)
2155 goto done;
2158 if (status != GOT_STATUS_DELETE) {
2159 if (asprintf(&abspath, "%s/%s",
2160 got_worktree_get_root_path(a->worktree), path) == -1) {
2161 err = got_error_from_errno("asprintf");
2162 goto done;
2165 f2 = fopen(abspath, "r");
2166 if (f2 == NULL) {
2167 err = got_error_from_errno2("fopen", abspath);
2168 goto done;
2170 if (lstat(abspath, &sb) == -1) {
2171 err = got_error_from_errno2("lstat", abspath);
2172 goto done;
2174 } else
2175 sb.st_size = 0;
2177 err = got_diff_blob_file(blob1, label1, f2, sb.st_size, path,
2178 a->diff_context, a->ignore_whitespace, stdout);
2179 done:
2180 if (blob1)
2181 got_object_blob_close(blob1);
2182 if (f2 && fclose(f2) != 0 && err == NULL)
2183 err = got_error_from_errno("fclose");
2184 free(abspath);
2185 return err;
2188 static const struct got_error *
2189 match_object_id(struct got_object_id **id, char **label,
2190 const char *id_str, int obj_type, int resolve_tags,
2191 struct got_repository *repo)
2193 const struct got_error *err;
2194 struct got_tag_object *tag;
2195 struct got_reference *ref = NULL;
2197 *id = NULL;
2198 *label = NULL;
2200 if (resolve_tags) {
2201 err = got_repo_object_match_tag(&tag, id_str, GOT_OBJ_TYPE_ANY,
2202 repo);
2203 if (err == NULL) {
2204 *id = got_object_id_dup(
2205 got_object_tag_get_object_id(tag));
2206 if (*id == NULL)
2207 err = got_error_from_errno("got_object_id_dup");
2208 else if (asprintf(label, "refs/tags/%s",
2209 got_object_tag_get_name(tag)) == -1) {
2210 err = got_error_from_errno("asprintf");
2211 free(*id);
2212 *id = NULL;
2214 got_object_tag_close(tag);
2215 return err;
2216 } else if (err->code != GOT_ERR_NO_OBJ)
2217 return err;
2220 err = got_repo_match_object_id_prefix(id, id_str, obj_type, repo);
2221 if (err) {
2222 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
2223 return err;
2224 err = got_ref_open(&ref, repo, id_str, 0);
2225 if (err != NULL)
2226 goto done;
2227 *label = strdup(got_ref_get_name(ref));
2228 if (*label == NULL) {
2229 err = got_error_from_errno("strdup");
2230 goto done;
2232 err = got_ref_resolve(id, repo, ref);
2233 } else {
2234 err = got_object_id_str(label, *id);
2235 if (*label == NULL) {
2236 err = got_error_from_errno("strdup");
2237 goto done;
2240 done:
2241 if (ref)
2242 got_ref_close(ref);
2243 return err;
2247 static const struct got_error *
2248 cmd_diff(int argc, char *argv[])
2250 const struct got_error *error;
2251 struct got_repository *repo = NULL;
2252 struct got_worktree *worktree = NULL;
2253 char *cwd = NULL, *repo_path = NULL;
2254 struct got_object_id *id1 = NULL, *id2 = NULL;
2255 const char *id_str1 = NULL, *id_str2 = NULL;
2256 char *label1 = NULL, *label2 = NULL;
2257 int type1, type2;
2258 int diff_context = 3, diff_staged = 0, ignore_whitespace = 0, ch;
2259 const char *errstr;
2260 char *path = NULL;
2262 #ifndef PROFILE
2263 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2264 NULL) == -1)
2265 err(1, "pledge");
2266 #endif
2268 while ((ch = getopt(argc, argv, "C:r:sw")) != -1) {
2269 switch (ch) {
2270 case 'C':
2271 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
2272 &errstr);
2273 if (errstr != NULL)
2274 err(1, "-C option %s", errstr);
2275 break;
2276 case 'r':
2277 repo_path = realpath(optarg, NULL);
2278 if (repo_path == NULL)
2279 return got_error_from_errno2("realpath",
2280 optarg);
2281 got_path_strip_trailing_slashes(repo_path);
2282 break;
2283 case 's':
2284 diff_staged = 1;
2285 break;
2286 case 'w':
2287 ignore_whitespace = 1;
2288 break;
2289 default:
2290 usage_diff();
2291 /* NOTREACHED */
2295 argc -= optind;
2296 argv += optind;
2298 cwd = getcwd(NULL, 0);
2299 if (cwd == NULL) {
2300 error = got_error_from_errno("getcwd");
2301 goto done;
2303 error = got_worktree_open(&worktree, cwd);
2304 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2305 goto done;
2306 if (argc <= 1) {
2307 if (worktree == NULL) {
2308 error = got_error(GOT_ERR_NOT_WORKTREE);
2309 goto done;
2311 if (repo_path)
2312 errx(1,
2313 "-r option can't be used when diffing a work tree");
2314 repo_path = strdup(got_worktree_get_repo_path(worktree));
2315 if (repo_path == NULL) {
2316 error = got_error_from_errno("strdup");
2317 goto done;
2319 if (argc == 1) {
2320 error = got_worktree_resolve_path(&path, worktree,
2321 argv[0]);
2322 if (error)
2323 goto done;
2324 } else {
2325 path = strdup("");
2326 if (path == NULL) {
2327 error = got_error_from_errno("strdup");
2328 goto done;
2331 } else if (argc == 2) {
2332 if (diff_staged)
2333 errx(1, "-s option can't be used when diffing "
2334 "objects in repository");
2335 id_str1 = argv[0];
2336 id_str2 = argv[1];
2337 if (worktree && repo_path == NULL) {
2338 repo_path =
2339 strdup(got_worktree_get_repo_path(worktree));
2340 if (repo_path == NULL) {
2341 error = got_error_from_errno("strdup");
2342 goto done;
2345 } else
2346 usage_diff();
2348 if (repo_path == NULL) {
2349 repo_path = getcwd(NULL, 0);
2350 if (repo_path == NULL)
2351 return got_error_from_errno("getcwd");
2354 error = got_repo_open(&repo, repo_path, NULL);
2355 free(repo_path);
2356 if (error != NULL)
2357 goto done;
2359 error = apply_unveil(got_repo_get_path(repo), 1,
2360 worktree ? got_worktree_get_root_path(worktree) : NULL);
2361 if (error)
2362 goto done;
2364 if (argc <= 1) {
2365 struct print_diff_arg arg;
2366 struct got_pathlist_head paths;
2367 char *id_str;
2369 TAILQ_INIT(&paths);
2371 error = got_object_id_str(&id_str,
2372 got_worktree_get_base_commit_id(worktree));
2373 if (error)
2374 goto done;
2375 arg.repo = repo;
2376 arg.worktree = worktree;
2377 arg.diff_context = diff_context;
2378 arg.id_str = id_str;
2379 arg.header_shown = 0;
2380 arg.diff_staged = diff_staged;
2381 arg.ignore_whitespace = ignore_whitespace;
2383 error = got_pathlist_append(&paths, path, NULL);
2384 if (error)
2385 goto done;
2387 error = got_worktree_status(worktree, &paths, repo, print_diff,
2388 &arg, check_cancelled, NULL);
2389 free(id_str);
2390 got_pathlist_free(&paths);
2391 goto done;
2394 error = match_object_id(&id1, &label1, id_str1, GOT_OBJ_TYPE_ANY, 1,
2395 repo);
2396 if (error)
2397 goto done;
2399 error = match_object_id(&id2, &label2, id_str2, GOT_OBJ_TYPE_ANY, 1,
2400 repo);
2401 if (error)
2402 goto done;
2404 error = got_object_get_type(&type1, repo, id1);
2405 if (error)
2406 goto done;
2408 error = got_object_get_type(&type2, repo, id2);
2409 if (error)
2410 goto done;
2412 if (type1 != type2) {
2413 error = got_error(GOT_ERR_OBJ_TYPE);
2414 goto done;
2417 switch (type1) {
2418 case GOT_OBJ_TYPE_BLOB:
2419 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
2420 diff_context, ignore_whitespace, repo, stdout);
2421 break;
2422 case GOT_OBJ_TYPE_TREE:
2423 error = got_diff_objects_as_trees(id1, id2, "", "",
2424 diff_context, ignore_whitespace, repo, stdout);
2425 break;
2426 case GOT_OBJ_TYPE_COMMIT:
2427 printf("diff %s %s\n", label1, label2);
2428 error = got_diff_objects_as_commits(id1, id2, diff_context,
2429 ignore_whitespace, repo, stdout);
2430 break;
2431 default:
2432 error = got_error(GOT_ERR_OBJ_TYPE);
2435 done:
2436 free(label1);
2437 free(label2);
2438 free(id1);
2439 free(id2);
2440 free(path);
2441 if (worktree)
2442 got_worktree_close(worktree);
2443 if (repo) {
2444 const struct got_error *repo_error;
2445 repo_error = got_repo_close(repo);
2446 if (error == NULL)
2447 error = repo_error;
2449 return error;
2452 __dead static void
2453 usage_blame(void)
2455 fprintf(stderr,
2456 "usage: %s blame [-c commit] [-r repository-path] path\n",
2457 getprogname());
2458 exit(1);
2461 struct blame_line {
2462 int annotated;
2463 char *id_str;
2464 char *committer;
2465 char datebuf[11]; /* YYYY-MM-DD + NUL */
2468 struct blame_cb_args {
2469 struct blame_line *lines;
2470 int nlines;
2471 int nlines_prec;
2472 int lineno_cur;
2473 off_t *line_offsets;
2474 FILE *f;
2475 struct got_repository *repo;
2478 static const struct got_error *
2479 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2481 const struct got_error *err = NULL;
2482 struct blame_cb_args *a = arg;
2483 struct blame_line *bline;
2484 char *line = NULL;
2485 size_t linesize = 0;
2486 struct got_commit_object *commit = NULL;
2487 off_t offset;
2488 struct tm tm;
2489 time_t committer_time;
2491 if (nlines != a->nlines ||
2492 (lineno != -1 && lineno < 1) || lineno > a->nlines)
2493 return got_error(GOT_ERR_RANGE);
2495 if (sigint_received)
2496 return got_error(GOT_ERR_ITER_COMPLETED);
2498 if (lineno == -1)
2499 return NULL; /* no change in this commit */
2501 /* Annotate this line. */
2502 bline = &a->lines[lineno - 1];
2503 if (bline->annotated)
2504 return NULL;
2505 err = got_object_id_str(&bline->id_str, id);
2506 if (err)
2507 return err;
2509 err = got_object_open_as_commit(&commit, a->repo, id);
2510 if (err)
2511 goto done;
2513 bline->committer = strdup(got_object_commit_get_committer(commit));
2514 if (bline->committer == NULL) {
2515 err = got_error_from_errno("strdup");
2516 goto done;
2519 committer_time = got_object_commit_get_committer_time(commit);
2520 if (localtime_r(&committer_time, &tm) == NULL)
2521 return got_error_from_errno("localtime_r");
2522 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
2523 &tm) >= sizeof(bline->datebuf)) {
2524 err = got_error(GOT_ERR_NO_SPACE);
2525 goto done;
2527 bline->annotated = 1;
2529 /* Print lines annotated so far. */
2530 bline = &a->lines[a->lineno_cur - 1];
2531 if (!bline->annotated)
2532 goto done;
2534 offset = a->line_offsets[a->lineno_cur - 1];
2535 if (fseeko(a->f, offset, SEEK_SET) == -1) {
2536 err = got_error_from_errno("fseeko");
2537 goto done;
2540 while (bline->annotated) {
2541 char *smallerthan, *at, *nl, *committer;
2542 size_t len;
2544 if (getline(&line, &linesize, a->f) == -1) {
2545 if (ferror(a->f))
2546 err = got_error_from_errno("getline");
2547 break;
2550 committer = bline->committer;
2551 smallerthan = strchr(committer, '<');
2552 if (smallerthan && smallerthan[1] != '\0')
2553 committer = smallerthan + 1;
2554 at = strchr(committer, '@');
2555 if (at)
2556 *at = '\0';
2557 len = strlen(committer);
2558 if (len >= 9)
2559 committer[8] = '\0';
2561 nl = strchr(line, '\n');
2562 if (nl)
2563 *nl = '\0';
2564 printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
2565 bline->id_str, bline->datebuf, committer, line);
2567 a->lineno_cur++;
2568 bline = &a->lines[a->lineno_cur - 1];
2570 done:
2571 if (commit)
2572 got_object_commit_close(commit);
2573 free(line);
2574 return err;
2577 static const struct got_error *
2578 cmd_blame(int argc, char *argv[])
2580 const struct got_error *error;
2581 struct got_repository *repo = NULL;
2582 struct got_worktree *worktree = NULL;
2583 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2584 struct got_object_id *obj_id = NULL;
2585 struct got_object_id *commit_id = NULL;
2586 struct got_blob_object *blob = NULL;
2587 char *commit_id_str = NULL;
2588 struct blame_cb_args bca;
2589 int ch, obj_type, i;
2590 size_t filesize;
2592 memset(&bca, 0, sizeof(bca));
2594 #ifndef PROFILE
2595 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2596 NULL) == -1)
2597 err(1, "pledge");
2598 #endif
2600 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2601 switch (ch) {
2602 case 'c':
2603 commit_id_str = optarg;
2604 break;
2605 case 'r':
2606 repo_path = realpath(optarg, NULL);
2607 if (repo_path == NULL)
2608 return got_error_from_errno2("realpath",
2609 optarg);
2610 got_path_strip_trailing_slashes(repo_path);
2611 break;
2612 default:
2613 usage_blame();
2614 /* NOTREACHED */
2618 argc -= optind;
2619 argv += optind;
2621 if (argc == 1)
2622 path = argv[0];
2623 else
2624 usage_blame();
2626 cwd = getcwd(NULL, 0);
2627 if (cwd == NULL) {
2628 error = got_error_from_errno("getcwd");
2629 goto done;
2631 if (repo_path == NULL) {
2632 error = got_worktree_open(&worktree, cwd);
2633 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2634 goto done;
2635 else
2636 error = NULL;
2637 if (worktree) {
2638 repo_path =
2639 strdup(got_worktree_get_repo_path(worktree));
2640 if (repo_path == NULL)
2641 error = got_error_from_errno("strdup");
2642 if (error)
2643 goto done;
2644 } else {
2645 repo_path = strdup(cwd);
2646 if (repo_path == NULL) {
2647 error = got_error_from_errno("strdup");
2648 goto done;
2653 error = got_repo_open(&repo, repo_path, NULL);
2654 if (error != NULL)
2655 goto done;
2657 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2658 if (error)
2659 goto done;
2661 if (worktree) {
2662 const char *prefix = got_worktree_get_path_prefix(worktree);
2663 char *p, *worktree_subdir = cwd +
2664 strlen(got_worktree_get_root_path(worktree));
2665 if (asprintf(&p, "%s%s%s%s%s",
2666 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
2667 worktree_subdir, worktree_subdir[0] ? "/" : "",
2668 path) == -1) {
2669 error = got_error_from_errno("asprintf");
2670 goto done;
2672 error = got_repo_map_path(&in_repo_path, repo, p, 0);
2673 free(p);
2674 } else {
2675 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2677 if (error)
2678 goto done;
2680 if (commit_id_str == NULL) {
2681 struct got_reference *head_ref;
2682 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
2683 if (error != NULL)
2684 goto done;
2685 error = got_ref_resolve(&commit_id, repo, head_ref);
2686 got_ref_close(head_ref);
2687 if (error != NULL)
2688 goto done;
2689 } else {
2690 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
2691 if (error)
2692 goto done;
2695 error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
2696 if (error)
2697 goto done;
2698 if (obj_id == NULL) {
2699 error = got_error(GOT_ERR_NO_OBJ);
2700 goto done;
2703 error = got_object_get_type(&obj_type, repo, obj_id);
2704 if (error)
2705 goto done;
2707 if (obj_type != GOT_OBJ_TYPE_BLOB) {
2708 error = got_error(GOT_ERR_OBJ_TYPE);
2709 goto done;
2712 error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
2713 if (error)
2714 goto done;
2715 bca.f = got_opentemp();
2716 if (bca.f == NULL) {
2717 error = got_error_from_errno("got_opentemp");
2718 goto done;
2720 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
2721 &bca.line_offsets, bca.f, blob);
2722 if (error || bca.nlines == 0)
2723 goto done;
2725 /* Don't include \n at EOF in the blame line count. */
2726 if (bca.line_offsets[bca.nlines - 1] == filesize)
2727 bca.nlines--;
2729 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
2730 if (bca.lines == NULL) {
2731 error = got_error_from_errno("calloc");
2732 goto done;
2734 bca.lineno_cur = 1;
2735 bca.nlines_prec = 0;
2736 i = bca.nlines;
2737 while (i > 0) {
2738 i /= 10;
2739 bca.nlines_prec++;
2741 bca.repo = repo;
2743 error = got_blame(in_repo_path, commit_id, repo, blame_cb, &bca,
2744 check_cancelled, NULL);
2745 if (error)
2746 goto done;
2747 done:
2748 free(in_repo_path);
2749 free(repo_path);
2750 free(cwd);
2751 free(commit_id);
2752 free(obj_id);
2753 if (blob)
2754 got_object_blob_close(blob);
2755 if (worktree)
2756 got_worktree_close(worktree);
2757 if (repo) {
2758 const struct got_error *repo_error;
2759 repo_error = got_repo_close(repo);
2760 if (error == NULL)
2761 error = repo_error;
2763 if (bca.lines) {
2764 for (i = 0; i < bca.nlines; i++) {
2765 struct blame_line *bline = &bca.lines[i];
2766 free(bline->id_str);
2767 free(bline->committer);
2769 free(bca.lines);
2771 free(bca.line_offsets);
2772 if (bca.f && fclose(bca.f) == EOF && error == NULL)
2773 error = got_error_from_errno("fclose");
2774 return error;
2777 __dead static void
2778 usage_tree(void)
2780 fprintf(stderr,
2781 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
2782 getprogname());
2783 exit(1);
2786 static void
2787 print_entry(struct got_tree_entry *te, const char *id, const char *path,
2788 const char *root_path)
2790 int is_root_path = (strcmp(path, root_path) == 0);
2791 const char *modestr = "";
2792 mode_t mode = got_tree_entry_get_mode(te);
2794 path += strlen(root_path);
2795 while (path[0] == '/')
2796 path++;
2798 if (got_object_tree_entry_is_submodule(te))
2799 modestr = "$";
2800 else if (S_ISLNK(mode))
2801 modestr = "@";
2802 else if (S_ISDIR(mode))
2803 modestr = "/";
2804 else if (mode & S_IXUSR)
2805 modestr = "*";
2807 printf("%s%s%s%s%s\n", id ? id : "", path,
2808 is_root_path ? "" : "/", got_tree_entry_get_name(te), modestr);
2811 static const struct got_error *
2812 print_tree(const char *path, struct got_object_id *commit_id,
2813 int show_ids, int recurse, const char *root_path,
2814 struct got_repository *repo)
2816 const struct got_error *err = NULL;
2817 struct got_object_id *tree_id = NULL;
2818 struct got_tree_object *tree = NULL;
2819 int nentries, i;
2821 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
2822 if (err)
2823 goto done;
2825 err = got_object_open_as_tree(&tree, repo, tree_id);
2826 if (err)
2827 goto done;
2828 nentries = got_object_tree_get_nentries(tree);
2829 for (i = 0; i < nentries; i++) {
2830 struct got_tree_entry *te;
2831 char *id = NULL;
2833 if (sigint_received || sigpipe_received)
2834 break;
2836 te = got_object_tree_get_entry(tree, i);
2837 if (show_ids) {
2838 char *id_str;
2839 err = got_object_id_str(&id_str,
2840 got_tree_entry_get_id(te));
2841 if (err)
2842 goto done;
2843 if (asprintf(&id, "%s ", id_str) == -1) {
2844 err = got_error_from_errno("asprintf");
2845 free(id_str);
2846 goto done;
2848 free(id_str);
2850 print_entry(te, id, path, root_path);
2851 free(id);
2853 if (recurse && S_ISDIR(got_tree_entry_get_mode(te))) {
2854 char *child_path;
2855 if (asprintf(&child_path, "%s%s%s", path,
2856 path[0] == '/' && path[1] == '\0' ? "" : "/",
2857 got_tree_entry_get_name(te)) == -1) {
2858 err = got_error_from_errno("asprintf");
2859 goto done;
2861 err = print_tree(child_path, commit_id, show_ids, 1,
2862 root_path, repo);
2863 free(child_path);
2864 if (err)
2865 goto done;
2868 done:
2869 if (tree)
2870 got_object_tree_close(tree);
2871 free(tree_id);
2872 return err;
2875 static const struct got_error *
2876 cmd_tree(int argc, char *argv[])
2878 const struct got_error *error;
2879 struct got_repository *repo = NULL;
2880 struct got_worktree *worktree = NULL;
2881 const char *path;
2882 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2883 struct got_object_id *commit_id = NULL;
2884 char *commit_id_str = NULL;
2885 int show_ids = 0, recurse = 0;
2886 int ch;
2888 #ifndef PROFILE
2889 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2890 NULL) == -1)
2891 err(1, "pledge");
2892 #endif
2894 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
2895 switch (ch) {
2896 case 'c':
2897 commit_id_str = optarg;
2898 break;
2899 case 'r':
2900 repo_path = realpath(optarg, NULL);
2901 if (repo_path == NULL)
2902 return got_error_from_errno2("realpath",
2903 optarg);
2904 got_path_strip_trailing_slashes(repo_path);
2905 break;
2906 case 'i':
2907 show_ids = 1;
2908 break;
2909 case 'R':
2910 recurse = 1;
2911 break;
2912 default:
2913 usage_tree();
2914 /* NOTREACHED */
2918 argc -= optind;
2919 argv += optind;
2921 if (argc == 1)
2922 path = argv[0];
2923 else if (argc > 1)
2924 usage_tree();
2925 else
2926 path = NULL;
2928 cwd = getcwd(NULL, 0);
2929 if (cwd == NULL) {
2930 error = got_error_from_errno("getcwd");
2931 goto done;
2933 if (repo_path == NULL) {
2934 error = got_worktree_open(&worktree, cwd);
2935 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2936 goto done;
2937 else
2938 error = NULL;
2939 if (worktree) {
2940 repo_path =
2941 strdup(got_worktree_get_repo_path(worktree));
2942 if (repo_path == NULL)
2943 error = got_error_from_errno("strdup");
2944 if (error)
2945 goto done;
2946 } else {
2947 repo_path = strdup(cwd);
2948 if (repo_path == NULL) {
2949 error = got_error_from_errno("strdup");
2950 goto done;
2955 error = got_repo_open(&repo, repo_path, NULL);
2956 if (error != NULL)
2957 goto done;
2959 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2960 if (error)
2961 goto done;
2963 if (path == NULL) {
2964 if (worktree) {
2965 char *p, *worktree_subdir = cwd +
2966 strlen(got_worktree_get_root_path(worktree));
2967 if (asprintf(&p, "%s/%s",
2968 got_worktree_get_path_prefix(worktree),
2969 worktree_subdir) == -1) {
2970 error = got_error_from_errno("asprintf");
2971 goto done;
2973 error = got_repo_map_path(&in_repo_path, repo, p, 1);
2974 free(p);
2975 if (error)
2976 goto done;
2977 } else
2978 path = "/";
2980 if (in_repo_path == NULL) {
2981 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2982 if (error != NULL)
2983 goto done;
2986 if (commit_id_str == NULL) {
2987 struct got_reference *head_ref;
2988 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
2989 if (error != NULL)
2990 goto done;
2991 error = got_ref_resolve(&commit_id, repo, head_ref);
2992 got_ref_close(head_ref);
2993 if (error != NULL)
2994 goto done;
2995 } else {
2996 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
2997 if (error)
2998 goto done;
3001 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
3002 in_repo_path, repo);
3003 done:
3004 free(in_repo_path);
3005 free(repo_path);
3006 free(cwd);
3007 free(commit_id);
3008 if (worktree)
3009 got_worktree_close(worktree);
3010 if (repo) {
3011 const struct got_error *repo_error;
3012 repo_error = got_repo_close(repo);
3013 if (error == NULL)
3014 error = repo_error;
3016 return error;
3019 __dead static void
3020 usage_status(void)
3022 fprintf(stderr, "usage: %s status [path ...]\n", getprogname());
3023 exit(1);
3026 static const struct got_error *
3027 print_status(void *arg, unsigned char status, unsigned char staged_status,
3028 const char *path, struct got_object_id *blob_id,
3029 struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
3031 if (status == staged_status && (status == GOT_STATUS_DELETE))
3032 status = GOT_STATUS_NO_CHANGE;
3033 printf("%c%c %s\n", status, staged_status, path);
3034 return NULL;
3037 static const struct got_error *
3038 cmd_status(int argc, char *argv[])
3040 const struct got_error *error = NULL;
3041 struct got_repository *repo = NULL;
3042 struct got_worktree *worktree = NULL;
3043 char *cwd = NULL;
3044 struct got_pathlist_head paths;
3045 struct got_pathlist_entry *pe;
3046 int ch;
3048 TAILQ_INIT(&paths);
3050 while ((ch = getopt(argc, argv, "")) != -1) {
3051 switch (ch) {
3052 default:
3053 usage_status();
3054 /* NOTREACHED */
3058 argc -= optind;
3059 argv += optind;
3061 #ifndef PROFILE
3062 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3063 NULL) == -1)
3064 err(1, "pledge");
3065 #endif
3066 cwd = getcwd(NULL, 0);
3067 if (cwd == NULL) {
3068 error = got_error_from_errno("getcwd");
3069 goto done;
3072 error = got_worktree_open(&worktree, cwd);
3073 if (error != NULL)
3074 goto done;
3076 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
3077 NULL);
3078 if (error != NULL)
3079 goto done;
3081 error = apply_unveil(got_repo_get_path(repo), 1,
3082 got_worktree_get_root_path(worktree));
3083 if (error)
3084 goto done;
3086 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3087 if (error)
3088 goto done;
3090 error = got_worktree_status(worktree, &paths, repo, print_status, NULL,
3091 check_cancelled, NULL);
3092 done:
3093 TAILQ_FOREACH(pe, &paths, entry)
3094 free((char *)pe->path);
3095 got_pathlist_free(&paths);
3096 free(cwd);
3097 return error;
3100 __dead static void
3101 usage_ref(void)
3103 fprintf(stderr,
3104 "usage: %s ref [-r repository] -l | -d name | [-s] name target\n",
3105 getprogname());
3106 exit(1);
3109 static const struct got_error *
3110 list_refs(struct got_repository *repo)
3112 static const struct got_error *err = NULL;
3113 struct got_reflist_head refs;
3114 struct got_reflist_entry *re;
3116 SIMPLEQ_INIT(&refs);
3117 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
3118 if (err)
3119 return err;
3121 SIMPLEQ_FOREACH(re, &refs, entry) {
3122 char *refstr;
3123 refstr = got_ref_to_str(re->ref);
3124 if (refstr == NULL)
3125 return got_error_from_errno("got_ref_to_str");
3126 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
3127 free(refstr);
3130 got_ref_list_free(&refs);
3131 return NULL;
3134 static const struct got_error *
3135 delete_ref(struct got_repository *repo, const char *refname)
3137 const struct got_error *err = NULL;
3138 struct got_reference *ref;
3140 err = got_ref_open(&ref, repo, refname, 0);
3141 if (err)
3142 return err;
3144 err = got_ref_delete(ref, repo);
3145 got_ref_close(ref);
3146 return err;
3149 static const struct got_error *
3150 add_ref(struct got_repository *repo, const char *refname, const char *target)
3152 const struct got_error *err = NULL;
3153 struct got_object_id *id;
3154 struct got_reference *ref = NULL;
3157 * Don't let the user create a reference name with a leading '-'.
3158 * While technically a valid reference name, this case is usually
3159 * an unintended typo.
3161 if (refname[0] == '-')
3162 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
3164 err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
3165 repo);
3166 if (err) {
3167 struct got_reference *target_ref;
3169 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
3170 return err;
3171 err = got_ref_open(&target_ref, repo, target, 0);
3172 if (err)
3173 return err;
3174 err = got_ref_resolve(&id, repo, target_ref);
3175 got_ref_close(target_ref);
3176 if (err)
3177 return err;
3180 err = got_ref_alloc(&ref, refname, id);
3181 if (err)
3182 goto done;
3184 err = got_ref_write(ref, repo);
3185 done:
3186 if (ref)
3187 got_ref_close(ref);
3188 free(id);
3189 return err;
3192 static const struct got_error *
3193 add_symref(struct got_repository *repo, const char *refname, const char *target)
3195 const struct got_error *err = NULL;
3196 struct got_reference *ref = NULL;
3197 struct got_reference *target_ref = NULL;
3200 * Don't let the user create a reference name with a leading '-'.
3201 * While technically a valid reference name, this case is usually
3202 * an unintended typo.
3204 if (refname[0] == '-')
3205 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
3207 err = got_ref_open(&target_ref, repo, target, 0);
3208 if (err)
3209 return err;
3211 err = got_ref_alloc_symref(&ref, refname, target_ref);
3212 if (err)
3213 goto done;
3215 err = got_ref_write(ref, repo);
3216 done:
3217 if (target_ref)
3218 got_ref_close(target_ref);
3219 if (ref)
3220 got_ref_close(ref);
3221 return err;
3224 static const struct got_error *
3225 cmd_ref(int argc, char *argv[])
3227 const struct got_error *error = NULL;
3228 struct got_repository *repo = NULL;
3229 struct got_worktree *worktree = NULL;
3230 char *cwd = NULL, *repo_path = NULL;
3231 int ch, do_list = 0, create_symref = 0;
3232 const char *delref = NULL;
3234 /* TODO: Add -s option for adding symbolic references. */
3235 while ((ch = getopt(argc, argv, "d:r:ls")) != -1) {
3236 switch (ch) {
3237 case 'd':
3238 delref = optarg;
3239 break;
3240 case 'r':
3241 repo_path = realpath(optarg, NULL);
3242 if (repo_path == NULL)
3243 return got_error_from_errno2("realpath",
3244 optarg);
3245 got_path_strip_trailing_slashes(repo_path);
3246 break;
3247 case 'l':
3248 do_list = 1;
3249 break;
3250 case 's':
3251 create_symref = 1;
3252 break;
3253 default:
3254 usage_ref();
3255 /* NOTREACHED */
3259 if (do_list && delref)
3260 errx(1, "-l and -d options are mutually exclusive\n");
3262 argc -= optind;
3263 argv += optind;
3265 if (do_list || delref) {
3266 if (create_symref)
3267 errx(1, "-s option cannot be used together with the "
3268 "-l or -d options");
3269 if (argc > 0)
3270 usage_ref();
3271 } else if (argc != 2)
3272 usage_ref();
3274 #ifndef PROFILE
3275 if (do_list) {
3276 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3277 NULL) == -1)
3278 err(1, "pledge");
3279 } else {
3280 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3281 "sendfd unveil", NULL) == -1)
3282 err(1, "pledge");
3284 #endif
3285 cwd = getcwd(NULL, 0);
3286 if (cwd == NULL) {
3287 error = got_error_from_errno("getcwd");
3288 goto done;
3291 if (repo_path == NULL) {
3292 error = got_worktree_open(&worktree, cwd);
3293 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3294 goto done;
3295 else
3296 error = NULL;
3297 if (worktree) {
3298 repo_path =
3299 strdup(got_worktree_get_repo_path(worktree));
3300 if (repo_path == NULL)
3301 error = got_error_from_errno("strdup");
3302 if (error)
3303 goto done;
3304 } else {
3305 repo_path = strdup(cwd);
3306 if (repo_path == NULL) {
3307 error = got_error_from_errno("strdup");
3308 goto done;
3313 error = got_repo_open(&repo, repo_path, NULL);
3314 if (error != NULL)
3315 goto done;
3317 error = apply_unveil(got_repo_get_path(repo), do_list,
3318 worktree ? got_worktree_get_root_path(worktree) : NULL);
3319 if (error)
3320 goto done;
3322 if (do_list)
3323 error = list_refs(repo);
3324 else if (delref)
3325 error = delete_ref(repo, delref);
3326 else if (create_symref)
3327 error = add_symref(repo, argv[0], argv[1]);
3328 else
3329 error = add_ref(repo, argv[0], argv[1]);
3330 done:
3331 if (repo)
3332 got_repo_close(repo);
3333 if (worktree)
3334 got_worktree_close(worktree);
3335 free(cwd);
3336 free(repo_path);
3337 return error;
3340 __dead static void
3341 usage_branch(void)
3343 fprintf(stderr,
3344 "usage: %s branch [-c commit] [-r repository] [-l] | -d name | "
3345 "[name]\n", getprogname());
3346 exit(1);
3349 static const struct got_error *
3350 list_branch(struct got_repository *repo, struct got_worktree *worktree,
3351 struct got_reference *ref)
3353 const struct got_error *err = NULL;
3354 const char *refname, *marker = " ";
3355 char *refstr;
3357 refname = got_ref_get_name(ref);
3358 if (worktree && strcmp(refname,
3359 got_worktree_get_head_ref_name(worktree)) == 0) {
3360 struct got_object_id *id = NULL;
3362 err = got_ref_resolve(&id, repo, ref);
3363 if (err)
3364 return err;
3365 if (got_object_id_cmp(id,
3366 got_worktree_get_base_commit_id(worktree)) == 0)
3367 marker = "* ";
3368 else
3369 marker = "~ ";
3370 free(id);
3373 if (strncmp(refname, "refs/heads/", 11) == 0)
3374 refname += 11;
3375 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
3376 refname += 18;
3378 refstr = got_ref_to_str(ref);
3379 if (refstr == NULL)
3380 return got_error_from_errno("got_ref_to_str");
3382 printf("%s%s: %s\n", marker, refname, refstr);
3383 free(refstr);
3384 return NULL;
3387 static const struct got_error *
3388 show_current_branch(struct got_repository *repo, struct got_worktree *worktree)
3390 const char *refname;
3392 if (worktree == NULL)
3393 return got_error(GOT_ERR_NOT_WORKTREE);
3395 refname = got_worktree_get_head_ref_name(worktree);
3397 if (strncmp(refname, "refs/heads/", 11) == 0)
3398 refname += 11;
3399 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
3400 refname += 18;
3402 printf("%s\n", refname);
3404 return NULL;
3407 static const struct got_error *
3408 list_branches(struct got_repository *repo, struct got_worktree *worktree)
3410 static const struct got_error *err = NULL;
3411 struct got_reflist_head refs;
3412 struct got_reflist_entry *re;
3413 struct got_reference *temp_ref = NULL;
3414 int rebase_in_progress, histedit_in_progress;
3416 SIMPLEQ_INIT(&refs);
3418 if (worktree) {
3419 err = got_worktree_rebase_in_progress(&rebase_in_progress,
3420 worktree);
3421 if (err)
3422 return err;
3424 err = got_worktree_histedit_in_progress(&histedit_in_progress,
3425 worktree);
3426 if (err)
3427 return err;
3429 if (rebase_in_progress || histedit_in_progress) {
3430 err = got_ref_open(&temp_ref, repo,
3431 got_worktree_get_head_ref_name(worktree), 0);
3432 if (err)
3433 return err;
3434 list_branch(repo, worktree, temp_ref);
3435 got_ref_close(temp_ref);
3439 err = got_ref_list(&refs, repo, "refs/heads",
3440 got_ref_cmp_by_name, NULL);
3441 if (err)
3442 return err;
3444 SIMPLEQ_FOREACH(re, &refs, entry)
3445 list_branch(repo, worktree, re->ref);
3447 got_ref_list_free(&refs);
3448 return NULL;
3451 static const struct got_error *
3452 delete_branch(struct got_repository *repo, struct got_worktree *worktree,
3453 const char *branch_name)
3455 const struct got_error *err = NULL;
3456 struct got_reference *ref = NULL;
3457 char *refname;
3459 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
3460 return got_error_from_errno("asprintf");
3462 err = got_ref_open(&ref, repo, refname, 0);
3463 if (err)
3464 goto done;
3466 if (worktree &&
3467 strcmp(got_worktree_get_head_ref_name(worktree),
3468 got_ref_get_name(ref)) == 0) {
3469 err = got_error_msg(GOT_ERR_SAME_BRANCH,
3470 "will not delete this work tree's current branch");
3471 goto done;
3474 err = got_ref_delete(ref, repo);
3475 done:
3476 if (ref)
3477 got_ref_close(ref);
3478 free(refname);
3479 return err;
3482 static const struct got_error *
3483 add_branch(struct got_repository *repo, const char *branch_name,
3484 struct got_object_id *base_commit_id)
3486 const struct got_error *err = NULL;
3487 struct got_reference *ref = NULL;
3488 char *base_refname = NULL, *refname = NULL;
3491 * Don't let the user create a branch name with a leading '-'.
3492 * While technically a valid reference name, this case is usually
3493 * an unintended typo.
3495 if (branch_name[0] == '-')
3496 return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
3498 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
3499 err = got_error_from_errno("asprintf");
3500 goto done;
3503 err = got_ref_open(&ref, repo, refname, 0);
3504 if (err == NULL) {
3505 err = got_error(GOT_ERR_BRANCH_EXISTS);
3506 goto done;
3507 } else if (err->code != GOT_ERR_NOT_REF)
3508 goto done;
3510 err = got_ref_alloc(&ref, refname, base_commit_id);
3511 if (err)
3512 goto done;
3514 err = got_ref_write(ref, repo);
3515 done:
3516 if (ref)
3517 got_ref_close(ref);
3518 free(base_refname);
3519 free(refname);
3520 return err;
3523 static const struct got_error *
3524 cmd_branch(int argc, char *argv[])
3526 const struct got_error *error = NULL;
3527 struct got_repository *repo = NULL;
3528 struct got_worktree *worktree = NULL;
3529 char *cwd = NULL, *repo_path = NULL;
3530 int ch, do_list = 0, do_show = 0;
3531 const char *delref = NULL, *commit_id_arg = NULL;
3533 while ((ch = getopt(argc, argv, "c:d:r:l")) != -1) {
3534 switch (ch) {
3535 case 'c':
3536 commit_id_arg = optarg;
3537 break;
3538 case 'd':
3539 delref = optarg;
3540 break;
3541 case 'r':
3542 repo_path = realpath(optarg, NULL);
3543 if (repo_path == NULL)
3544 return got_error_from_errno2("realpath",
3545 optarg);
3546 got_path_strip_trailing_slashes(repo_path);
3547 break;
3548 case 'l':
3549 do_list = 1;
3550 break;
3551 default:
3552 usage_branch();
3553 /* NOTREACHED */
3557 if (do_list && delref)
3558 errx(1, "-l and -d options are mutually exclusive\n");
3560 argc -= optind;
3561 argv += optind;
3563 if (!do_list && !delref && argc == 0)
3564 do_show = 1;
3566 if ((do_list || delref || do_show) && commit_id_arg != NULL)
3567 errx(1, "-c option can only be used when creating a branch");
3569 if (do_list || delref) {
3570 if (argc > 0)
3571 usage_branch();
3572 } else if (!do_show && argc != 1)
3573 usage_branch();
3575 #ifndef PROFILE
3576 if (do_list || do_show) {
3577 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3578 NULL) == -1)
3579 err(1, "pledge");
3580 } else {
3581 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3582 "sendfd unveil", NULL) == -1)
3583 err(1, "pledge");
3585 #endif
3586 cwd = getcwd(NULL, 0);
3587 if (cwd == NULL) {
3588 error = got_error_from_errno("getcwd");
3589 goto done;
3592 if (repo_path == NULL) {
3593 error = got_worktree_open(&worktree, cwd);
3594 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3595 goto done;
3596 else
3597 error = NULL;
3598 if (worktree) {
3599 repo_path =
3600 strdup(got_worktree_get_repo_path(worktree));
3601 if (repo_path == NULL)
3602 error = got_error_from_errno("strdup");
3603 if (error)
3604 goto done;
3605 } else {
3606 repo_path = strdup(cwd);
3607 if (repo_path == NULL) {
3608 error = got_error_from_errno("strdup");
3609 goto done;
3614 error = got_repo_open(&repo, repo_path, NULL);
3615 if (error != NULL)
3616 goto done;
3618 error = apply_unveil(got_repo_get_path(repo), do_list,
3619 worktree ? got_worktree_get_root_path(worktree) : NULL);
3620 if (error)
3621 goto done;
3623 if (do_show)
3624 error = show_current_branch(repo, worktree);
3625 else if (do_list)
3626 error = list_branches(repo, worktree);
3627 else if (delref)
3628 error = delete_branch(repo, worktree, delref);
3629 else {
3630 struct got_object_id *commit_id;
3631 if (commit_id_arg == NULL)
3632 commit_id_arg = worktree ?
3633 got_worktree_get_head_ref_name(worktree) :
3634 GOT_REF_HEAD;
3635 error = resolve_commit_arg(&commit_id, commit_id_arg, repo);
3636 if (error)
3637 goto done;
3638 error = add_branch(repo, argv[0], commit_id);
3639 free(commit_id);
3641 done:
3642 if (repo)
3643 got_repo_close(repo);
3644 if (worktree)
3645 got_worktree_close(worktree);
3646 free(cwd);
3647 free(repo_path);
3648 return error;
3652 __dead static void
3653 usage_tag(void)
3655 fprintf(stderr,
3656 "usage: %s tag [-r repository] | -l | "
3657 "[-m message] name [commit]\n", getprogname());
3658 exit(1);
3661 #if 0
3662 static const struct got_error *
3663 sort_tags(struct got_reflist_head *sorted, struct got_reflist_head *tags)
3665 const struct got_error *err = NULL;
3666 struct got_reflist_entry *re, *se, *new;
3667 struct got_object_id *re_id, *se_id;
3668 struct got_tag_object *re_tag, *se_tag;
3669 time_t re_time, se_time;
3671 SIMPLEQ_FOREACH(re, tags, entry) {
3672 se = SIMPLEQ_FIRST(sorted);
3673 if (se == NULL) {
3674 err = got_reflist_entry_dup(&new, re);
3675 if (err)
3676 return err;
3677 SIMPLEQ_INSERT_HEAD(sorted, new, entry);
3678 continue;
3679 } else {
3680 err = got_ref_resolve(&re_id, repo, re->ref);
3681 if (err)
3682 break;
3683 err = got_object_open_as_tag(&re_tag, repo, re_id);
3684 free(re_id);
3685 if (err)
3686 break;
3687 re_time = got_object_tag_get_tagger_time(re_tag);
3688 got_object_tag_close(re_tag);
3691 while (se) {
3692 err = got_ref_resolve(&se_id, repo, re->ref);
3693 if (err)
3694 break;
3695 err = got_object_open_as_tag(&se_tag, repo, se_id);
3696 free(se_id);
3697 if (err)
3698 break;
3699 se_time = got_object_tag_get_tagger_time(se_tag);
3700 got_object_tag_close(se_tag);
3702 if (se_time > re_time) {
3703 err = got_reflist_entry_dup(&new, re);
3704 if (err)
3705 return err;
3706 SIMPLEQ_INSERT_AFTER(sorted, se, new, entry);
3707 break;
3709 se = SIMPLEQ_NEXT(se, entry);
3710 continue;
3713 done:
3714 return err;
3716 #endif
3718 static const struct got_error *
3719 cmp_tags(void *arg, int *cmp, struct got_reference *ref1,
3720 struct got_reference *ref2)
3722 const struct got_error *err = NULL;
3723 struct got_repository *repo = arg;
3724 struct got_object_id *id1, *id2 = NULL;
3725 struct got_tag_object *tag1 = NULL, *tag2 = NULL;
3726 time_t time1, time2;
3728 *cmp = 0;
3730 err = got_ref_resolve(&id1, repo, ref1);
3731 if (err)
3732 return err;
3733 err = got_object_open_as_tag(&tag1, repo, id1);
3734 if (err)
3735 goto done;
3737 err = got_ref_resolve(&id2, repo, ref2);
3738 if (err)
3739 goto done;
3740 err = got_object_open_as_tag(&tag2, repo, id2);
3741 if (err)
3742 goto done;
3744 time1 = got_object_tag_get_tagger_time(tag1);
3745 time2 = got_object_tag_get_tagger_time(tag2);
3747 /* Put latest tags first. */
3748 if (time1 < time2)
3749 *cmp = 1;
3750 else if (time1 > time2)
3751 *cmp = -1;
3752 else
3753 err = got_ref_cmp_by_name(NULL, cmp, ref2, ref1);
3754 done:
3755 free(id1);
3756 free(id2);
3757 if (tag1)
3758 got_object_tag_close(tag1);
3759 if (tag2)
3760 got_object_tag_close(tag2);
3761 return err;
3764 static const struct got_error *
3765 list_tags(struct got_repository *repo, struct got_worktree *worktree)
3767 static const struct got_error *err = NULL;
3768 struct got_reflist_head refs;
3769 struct got_reflist_entry *re;
3771 SIMPLEQ_INIT(&refs);
3773 err = got_ref_list(&refs, repo, "refs/tags", cmp_tags, repo);
3774 if (err)
3775 return err;
3777 SIMPLEQ_FOREACH(re, &refs, entry) {
3778 const char *refname;
3779 char *refstr, *tagmsg0, *tagmsg, *line, *id_str, *datestr;
3780 char datebuf[26];
3781 time_t tagger_time;
3782 struct got_object_id *id;
3783 struct got_tag_object *tag;
3785 refname = got_ref_get_name(re->ref);
3786 if (strncmp(refname, "refs/tags/", 10) != 0)
3787 continue;
3788 refname += 10;
3789 refstr = got_ref_to_str(re->ref);
3790 if (refstr == NULL) {
3791 err = got_error_from_errno("got_ref_to_str");
3792 break;
3794 printf("%stag %s %s\n", GOT_COMMIT_SEP_STR, refname, refstr);
3795 free(refstr);
3797 err = got_ref_resolve(&id, repo, re->ref);
3798 if (err)
3799 break;
3800 err = got_object_open_as_tag(&tag, repo, id);
3801 free(id);
3802 if (err)
3803 break;
3804 printf("from: %s\n", got_object_tag_get_tagger(tag));
3805 tagger_time = got_object_tag_get_tagger_time(tag);
3806 datestr = get_datestr(&tagger_time, datebuf);
3807 if (datestr)
3808 printf("date: %s UTC\n", datestr);
3809 err = got_object_id_str(&id_str,
3810 got_object_tag_get_object_id(tag));
3811 if (err)
3812 break;
3813 switch (got_object_tag_get_object_type(tag)) {
3814 case GOT_OBJ_TYPE_BLOB:
3815 printf("object: %s %s\n", GOT_OBJ_LABEL_BLOB, id_str);
3816 break;
3817 case GOT_OBJ_TYPE_TREE:
3818 printf("object: %s %s\n", GOT_OBJ_LABEL_TREE, id_str);
3819 break;
3820 case GOT_OBJ_TYPE_COMMIT:
3821 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
3822 break;
3823 case GOT_OBJ_TYPE_TAG:
3824 printf("object: %s %s\n", GOT_OBJ_LABEL_TAG, id_str);
3825 break;
3826 default:
3827 break;
3829 free(id_str);
3830 tagmsg0 = strdup(got_object_tag_get_message(tag));
3831 got_object_tag_close(tag);
3832 if (tagmsg0 == NULL) {
3833 err = got_error_from_errno("strdup");
3834 break;
3837 tagmsg = tagmsg0;
3838 do {
3839 line = strsep(&tagmsg, "\n");
3840 if (line)
3841 printf(" %s\n", line);
3842 } while (line);
3843 free(tagmsg0);
3846 got_ref_list_free(&refs);
3847 return NULL;
3850 static const struct got_error *
3851 get_tag_message(char **tagmsg, char **tagmsg_path, const char *commit_id_str,
3852 const char *tag_name, const char *repo_path)
3854 const struct got_error *err = NULL;
3855 char *template = NULL, *initial_content = NULL;
3856 char *editor = NULL;
3857 int fd = -1;
3859 if (asprintf(&template, "/tmp/got-tagmsg") == -1) {
3860 err = got_error_from_errno("asprintf");
3861 goto done;
3864 if (asprintf(&initial_content, "\n# tagging commit %s as %s\n",
3865 commit_id_str, tag_name) == -1) {
3866 err = got_error_from_errno("asprintf");
3867 goto done;
3870 err = got_opentemp_named_fd(tagmsg_path, &fd, template);
3871 if (err)
3872 goto done;
3874 dprintf(fd, initial_content);
3875 close(fd);
3877 err = get_editor(&editor);
3878 if (err)
3879 goto done;
3880 err = edit_logmsg(tagmsg, editor, *tagmsg_path, initial_content);
3881 done:
3882 free(initial_content);
3883 free(template);
3884 free(editor);
3886 /* Editor is done; we can now apply unveil(2) */
3887 if (err == NULL) {
3888 err = apply_unveil(repo_path, 0, NULL);
3889 if (err) {
3890 free(*tagmsg);
3891 *tagmsg = NULL;
3894 return err;
3897 static const struct got_error *
3898 add_tag(struct got_repository *repo, const char *tag_name,
3899 const char *commit_arg, const char *tagmsg_arg)
3901 const struct got_error *err = NULL;
3902 struct got_object_id *commit_id = NULL, *tag_id = NULL;
3903 char *label = NULL, *commit_id_str = NULL;
3904 struct got_reference *ref = NULL;
3905 char *refname = NULL, *tagmsg = NULL, *tagger = NULL;
3906 char *tagmsg_path = NULL, *tag_id_str = NULL;
3907 int preserve_tagmsg = 0;
3910 * Don't let the user create a tag name with a leading '-'.
3911 * While technically a valid reference name, this case is usually
3912 * an unintended typo.
3914 if (tag_name[0] == '-')
3915 return got_error_path(tag_name, GOT_ERR_REF_NAME_MINUS);
3917 err = get_author(&tagger, repo);
3918 if (err)
3919 return err;
3921 err = match_object_id(&commit_id, &label, commit_arg,
3922 GOT_OBJ_TYPE_COMMIT, 1, repo);
3923 if (err)
3924 goto done;
3926 err = got_object_id_str(&commit_id_str, commit_id);
3927 if (err)
3928 goto done;
3930 if (strncmp("refs/tags/", tag_name, 10) == 0) {
3931 refname = strdup(tag_name);
3932 if (refname == NULL) {
3933 err = got_error_from_errno("strdup");
3934 goto done;
3936 tag_name += 10;
3937 } else if (asprintf(&refname, "refs/tags/%s", tag_name) == -1) {
3938 err = got_error_from_errno("asprintf");
3939 goto done;
3942 err = got_ref_open(&ref, repo, refname, 0);
3943 if (err == NULL) {
3944 err = got_error(GOT_ERR_TAG_EXISTS);
3945 goto done;
3946 } else if (err->code != GOT_ERR_NOT_REF)
3947 goto done;
3949 if (tagmsg_arg == NULL) {
3950 err = get_tag_message(&tagmsg, &tagmsg_path, commit_id_str,
3951 tag_name, got_repo_get_path(repo));
3952 if (err) {
3953 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY &&
3954 tagmsg_path != NULL)
3955 preserve_tagmsg = 1;
3956 goto done;
3960 err = got_object_tag_create(&tag_id, tag_name, commit_id,
3961 tagger, time(NULL), tagmsg ? tagmsg : tagmsg_arg, repo);
3962 if (err) {
3963 if (tagmsg_path)
3964 preserve_tagmsg = 1;
3965 goto done;
3968 err = got_ref_alloc(&ref, refname, tag_id);
3969 if (err) {
3970 if (tagmsg_path)
3971 preserve_tagmsg = 1;
3972 goto done;
3975 err = got_ref_write(ref, repo);
3976 if (err) {
3977 if (tagmsg_path)
3978 preserve_tagmsg = 1;
3979 goto done;
3982 err = got_object_id_str(&tag_id_str, tag_id);
3983 if (err) {
3984 if (tagmsg_path)
3985 preserve_tagmsg = 1;
3986 goto done;
3988 printf("Created tag %s\n", tag_id_str);
3989 done:
3990 if (preserve_tagmsg) {
3991 fprintf(stderr, "%s: tag message preserved in %s\n",
3992 getprogname(), tagmsg_path);
3993 } else if (tagmsg_path && unlink(tagmsg_path) == -1 && err == NULL)
3994 err = got_error_from_errno2("unlink", tagmsg_path);
3995 free(tag_id_str);
3996 if (ref)
3997 got_ref_close(ref);
3998 free(commit_id);
3999 free(commit_id_str);
4000 free(refname);
4001 free(tagmsg);
4002 free(tagmsg_path);
4003 free(tagger);
4004 return err;
4007 static const struct got_error *
4008 cmd_tag(int argc, char *argv[])
4010 const struct got_error *error = NULL;
4011 struct got_repository *repo = NULL;
4012 struct got_worktree *worktree = NULL;
4013 char *cwd = NULL, *repo_path = NULL, *commit_id_str = NULL;
4014 char *gitconfig_path = NULL;
4015 const char *tag_name, *commit_id_arg = NULL, *tagmsg = NULL;
4016 int ch, do_list = 0;
4018 while ((ch = getopt(argc, argv, "m:r:l")) != -1) {
4019 switch (ch) {
4020 case 'm':
4021 tagmsg = optarg;
4022 break;
4023 case 'r':
4024 repo_path = realpath(optarg, NULL);
4025 if (repo_path == NULL)
4026 return got_error_from_errno2("realpath",
4027 optarg);
4028 got_path_strip_trailing_slashes(repo_path);
4029 break;
4030 case 'l':
4031 do_list = 1;
4032 break;
4033 default:
4034 usage_tag();
4035 /* NOTREACHED */
4039 argc -= optind;
4040 argv += optind;
4042 if (do_list) {
4043 if (tagmsg)
4044 errx(1, "-l and -m options are mutually exclusive\n");
4045 if (argc > 0)
4046 usage_tag();
4047 } else if (argc < 1 || argc > 2)
4048 usage_tag();
4049 else if (argc > 1)
4050 commit_id_arg = argv[1];
4051 tag_name = argv[0];
4053 #ifndef PROFILE
4054 if (do_list) {
4055 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
4056 NULL) == -1)
4057 err(1, "pledge");
4058 } else {
4059 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
4060 "sendfd unveil", NULL) == -1)
4061 err(1, "pledge");
4063 #endif
4064 cwd = getcwd(NULL, 0);
4065 if (cwd == NULL) {
4066 error = got_error_from_errno("getcwd");
4067 goto done;
4070 if (repo_path == NULL) {
4071 error = got_worktree_open(&worktree, cwd);
4072 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4073 goto done;
4074 else
4075 error = NULL;
4076 if (worktree) {
4077 repo_path =
4078 strdup(got_worktree_get_repo_path(worktree));
4079 if (repo_path == NULL)
4080 error = got_error_from_errno("strdup");
4081 if (error)
4082 goto done;
4083 } else {
4084 repo_path = strdup(cwd);
4085 if (repo_path == NULL) {
4086 error = got_error_from_errno("strdup");
4087 goto done;
4092 if (do_list) {
4093 error = got_repo_open(&repo, repo_path, NULL);
4094 if (error != NULL)
4095 goto done;
4096 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4097 if (error)
4098 goto done;
4099 error = list_tags(repo, worktree);
4100 } else {
4101 error = get_gitconfig_path(&gitconfig_path);
4102 if (error)
4103 goto done;
4104 error = got_repo_open(&repo, repo_path, gitconfig_path);
4105 if (error != NULL)
4106 goto done;
4108 if (tagmsg) {
4109 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4110 if (error)
4111 goto done;
4114 if (commit_id_arg == NULL) {
4115 struct got_reference *head_ref;
4116 struct got_object_id *commit_id;
4117 error = got_ref_open(&head_ref, repo,
4118 worktree ? got_worktree_get_head_ref_name(worktree)
4119 : GOT_REF_HEAD, 0);
4120 if (error)
4121 goto done;
4122 error = got_ref_resolve(&commit_id, repo, head_ref);
4123 got_ref_close(head_ref);
4124 if (error)
4125 goto done;
4126 error = got_object_id_str(&commit_id_str, commit_id);
4127 free(commit_id);
4128 if (error)
4129 goto done;
4132 error = add_tag(repo, tag_name,
4133 commit_id_str ? commit_id_str : commit_id_arg, tagmsg);
4135 done:
4136 if (repo)
4137 got_repo_close(repo);
4138 if (worktree)
4139 got_worktree_close(worktree);
4140 free(cwd);
4141 free(repo_path);
4142 free(gitconfig_path);
4143 free(commit_id_str);
4144 return error;
4147 __dead static void
4148 usage_add(void)
4150 fprintf(stderr, "usage: %s add [-R] [-I] file-path ...\n",
4151 getprogname());
4152 exit(1);
4155 static const struct got_error *
4156 add_progress(void *arg, unsigned char status, const char *path)
4158 while (path[0] == '/')
4159 path++;
4160 printf("%c %s\n", status, path);
4161 return NULL;
4164 static const struct got_error *
4165 cmd_add(int argc, char *argv[])
4167 const struct got_error *error = NULL;
4168 struct got_repository *repo = NULL;
4169 struct got_worktree *worktree = NULL;
4170 char *cwd = NULL;
4171 struct got_pathlist_head paths;
4172 struct got_pathlist_entry *pe;
4173 int ch, can_recurse = 0, no_ignores = 0;
4175 TAILQ_INIT(&paths);
4177 while ((ch = getopt(argc, argv, "IR")) != -1) {
4178 switch (ch) {
4179 case 'I':
4180 no_ignores = 1;
4181 break;
4182 case 'R':
4183 can_recurse = 1;
4184 break;
4185 default:
4186 usage_add();
4187 /* NOTREACHED */
4191 argc -= optind;
4192 argv += optind;
4194 #ifndef PROFILE
4195 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4196 NULL) == -1)
4197 err(1, "pledge");
4198 #endif
4199 if (argc < 1)
4200 usage_add();
4202 cwd = getcwd(NULL, 0);
4203 if (cwd == NULL) {
4204 error = got_error_from_errno("getcwd");
4205 goto done;
4208 error = got_worktree_open(&worktree, cwd);
4209 if (error)
4210 goto done;
4212 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4213 NULL);
4214 if (error != NULL)
4215 goto done;
4217 error = apply_unveil(got_repo_get_path(repo), 1,
4218 got_worktree_get_root_path(worktree));
4219 if (error)
4220 goto done;
4222 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4223 if (error)
4224 goto done;
4226 if (!can_recurse && no_ignores) {
4227 error = got_error_msg(GOT_ERR_BAD_PATH,
4228 "disregarding ignores requires -R option");
4229 goto done;
4233 if (!can_recurse) {
4234 char *ondisk_path;
4235 struct stat sb;
4236 TAILQ_FOREACH(pe, &paths, entry) {
4237 if (asprintf(&ondisk_path, "%s/%s",
4238 got_worktree_get_root_path(worktree),
4239 pe->path) == -1) {
4240 error = got_error_from_errno("asprintf");
4241 goto done;
4243 if (lstat(ondisk_path, &sb) == -1) {
4244 if (errno == ENOENT) {
4245 free(ondisk_path);
4246 continue;
4248 error = got_error_from_errno2("lstat",
4249 ondisk_path);
4250 free(ondisk_path);
4251 goto done;
4253 free(ondisk_path);
4254 if (S_ISDIR(sb.st_mode)) {
4255 error = got_error_msg(GOT_ERR_BAD_PATH,
4256 "adding directories requires -R option");
4257 goto done;
4262 error = got_worktree_schedule_add(worktree, &paths, add_progress,
4263 NULL, repo, no_ignores);
4264 done:
4265 if (repo)
4266 got_repo_close(repo);
4267 if (worktree)
4268 got_worktree_close(worktree);
4269 TAILQ_FOREACH(pe, &paths, entry)
4270 free((char *)pe->path);
4271 got_pathlist_free(&paths);
4272 free(cwd);
4273 return error;
4276 __dead static void
4277 usage_remove(void)
4279 fprintf(stderr, "usage: %s remove [-f] [-R] file-path ...\n",
4280 getprogname());
4281 exit(1);
4284 static const struct got_error *
4285 print_remove_status(void *arg, unsigned char status,
4286 unsigned char staged_status, const char *path)
4288 while (path[0] == '/')
4289 path++;
4290 if (status == GOT_STATUS_NONEXISTENT)
4291 return NULL;
4292 if (status == staged_status && (status == GOT_STATUS_DELETE))
4293 status = GOT_STATUS_NO_CHANGE;
4294 printf("%c%c %s\n", status, staged_status, path);
4295 return NULL;
4298 static const struct got_error *
4299 cmd_remove(int argc, char *argv[])
4301 const struct got_error *error = NULL;
4302 struct got_worktree *worktree = NULL;
4303 struct got_repository *repo = NULL;
4304 char *cwd = NULL;
4305 struct got_pathlist_head paths;
4306 struct got_pathlist_entry *pe;
4307 int ch, delete_local_mods = 0, can_recurse = 0;
4309 TAILQ_INIT(&paths);
4311 while ((ch = getopt(argc, argv, "fR")) != -1) {
4312 switch (ch) {
4313 case 'f':
4314 delete_local_mods = 1;
4315 break;
4316 case 'R':
4317 can_recurse = 1;
4318 break;
4319 default:
4320 usage_remove();
4321 /* NOTREACHED */
4325 argc -= optind;
4326 argv += optind;
4328 #ifndef PROFILE
4329 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4330 NULL) == -1)
4331 err(1, "pledge");
4332 #endif
4333 if (argc < 1)
4334 usage_remove();
4336 cwd = getcwd(NULL, 0);
4337 if (cwd == NULL) {
4338 error = got_error_from_errno("getcwd");
4339 goto done;
4341 error = got_worktree_open(&worktree, cwd);
4342 if (error)
4343 goto done;
4345 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4346 NULL);
4347 if (error)
4348 goto done;
4350 error = apply_unveil(got_repo_get_path(repo), 1,
4351 got_worktree_get_root_path(worktree));
4352 if (error)
4353 goto done;
4355 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4356 if (error)
4357 goto done;
4359 if (!can_recurse) {
4360 char *ondisk_path;
4361 struct stat sb;
4362 TAILQ_FOREACH(pe, &paths, entry) {
4363 if (asprintf(&ondisk_path, "%s/%s",
4364 got_worktree_get_root_path(worktree),
4365 pe->path) == -1) {
4366 error = got_error_from_errno("asprintf");
4367 goto done;
4369 if (lstat(ondisk_path, &sb) == -1) {
4370 if (errno == ENOENT) {
4371 free(ondisk_path);
4372 continue;
4374 error = got_error_from_errno2("lstat",
4375 ondisk_path);
4376 free(ondisk_path);
4377 goto done;
4379 free(ondisk_path);
4380 if (S_ISDIR(sb.st_mode)) {
4381 error = got_error_msg(GOT_ERR_BAD_PATH,
4382 "removing directories requires -R option");
4383 goto done;
4388 error = got_worktree_schedule_delete(worktree, &paths,
4389 delete_local_mods, print_remove_status, NULL, repo);
4390 if (error)
4391 goto done;
4392 done:
4393 if (repo)
4394 got_repo_close(repo);
4395 if (worktree)
4396 got_worktree_close(worktree);
4397 TAILQ_FOREACH(pe, &paths, entry)
4398 free((char *)pe->path);
4399 got_pathlist_free(&paths);
4400 free(cwd);
4401 return error;
4404 __dead static void
4405 usage_revert(void)
4407 fprintf(stderr, "usage: %s revert [-p] [-F response-script] [-R] "
4408 "path ...\n", getprogname());
4409 exit(1);
4412 static const struct got_error *
4413 revert_progress(void *arg, unsigned char status, const char *path)
4415 while (path[0] == '/')
4416 path++;
4417 printf("%c %s\n", status, path);
4418 return NULL;
4421 struct choose_patch_arg {
4422 FILE *patch_script_file;
4423 const char *action;
4426 static const struct got_error *
4427 show_change(unsigned char status, const char *path, FILE *patch_file, int n,
4428 int nchanges, const char *action)
4430 char *line = NULL;
4431 size_t linesize = 0;
4432 ssize_t linelen;
4434 switch (status) {
4435 case GOT_STATUS_ADD:
4436 printf("A %s\n%s this addition? [y/n] ", path, action);
4437 break;
4438 case GOT_STATUS_DELETE:
4439 printf("D %s\n%s this deletion? [y/n] ", path, action);
4440 break;
4441 case GOT_STATUS_MODIFY:
4442 if (fseek(patch_file, 0L, SEEK_SET) == -1)
4443 return got_error_from_errno("fseek");
4444 printf(GOT_COMMIT_SEP_STR);
4445 while ((linelen = getline(&line, &linesize, patch_file)) != -1)
4446 printf("%s", line);
4447 if (ferror(patch_file))
4448 return got_error_from_errno("getline");
4449 printf(GOT_COMMIT_SEP_STR);
4450 printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
4451 path, n, nchanges, action);
4452 break;
4453 default:
4454 return got_error_path(path, GOT_ERR_FILE_STATUS);
4457 return NULL;
4460 static const struct got_error *
4461 choose_patch(int *choice, void *arg, unsigned char status, const char *path,
4462 FILE *patch_file, int n, int nchanges)
4464 const struct got_error *err = NULL;
4465 char *line = NULL;
4466 size_t linesize = 0;
4467 ssize_t linelen;
4468 int resp = ' ';
4469 struct choose_patch_arg *a = arg;
4471 *choice = GOT_PATCH_CHOICE_NONE;
4473 if (a->patch_script_file) {
4474 char *nl;
4475 err = show_change(status, path, patch_file, n, nchanges,
4476 a->action);
4477 if (err)
4478 return err;
4479 linelen = getline(&line, &linesize, a->patch_script_file);
4480 if (linelen == -1) {
4481 if (ferror(a->patch_script_file))
4482 return got_error_from_errno("getline");
4483 return NULL;
4485 nl = strchr(line, '\n');
4486 if (nl)
4487 *nl = '\0';
4488 if (strcmp(line, "y") == 0) {
4489 *choice = GOT_PATCH_CHOICE_YES;
4490 printf("y\n");
4491 } else if (strcmp(line, "n") == 0) {
4492 *choice = GOT_PATCH_CHOICE_NO;
4493 printf("n\n");
4494 } else if (strcmp(line, "q") == 0 &&
4495 status == GOT_STATUS_MODIFY) {
4496 *choice = GOT_PATCH_CHOICE_QUIT;
4497 printf("q\n");
4498 } else
4499 printf("invalid response '%s'\n", line);
4500 free(line);
4501 return NULL;
4504 while (resp != 'y' && resp != 'n' && resp != 'q') {
4505 err = show_change(status, path, patch_file, n, nchanges,
4506 a->action);
4507 if (err)
4508 return err;
4509 resp = getchar();
4510 if (resp == '\n')
4511 resp = getchar();
4512 if (status == GOT_STATUS_MODIFY) {
4513 if (resp != 'y' && resp != 'n' && resp != 'q') {
4514 printf("invalid response '%c'\n", resp);
4515 resp = ' ';
4517 } else if (resp != 'y' && resp != 'n') {
4518 printf("invalid response '%c'\n", resp);
4519 resp = ' ';
4523 if (resp == 'y')
4524 *choice = GOT_PATCH_CHOICE_YES;
4525 else if (resp == 'n')
4526 *choice = GOT_PATCH_CHOICE_NO;
4527 else if (resp == 'q' && status == GOT_STATUS_MODIFY)
4528 *choice = GOT_PATCH_CHOICE_QUIT;
4530 return NULL;
4534 static const struct got_error *
4535 cmd_revert(int argc, char *argv[])
4537 const struct got_error *error = NULL;
4538 struct got_worktree *worktree = NULL;
4539 struct got_repository *repo = NULL;
4540 char *cwd = NULL, *path = NULL;
4541 struct got_pathlist_head paths;
4542 struct got_pathlist_entry *pe;
4543 int ch, can_recurse = 0, pflag = 0;
4544 FILE *patch_script_file = NULL;
4545 const char *patch_script_path = NULL;
4546 struct choose_patch_arg cpa;
4548 TAILQ_INIT(&paths);
4550 while ((ch = getopt(argc, argv, "pF:R")) != -1) {
4551 switch (ch) {
4552 case 'p':
4553 pflag = 1;
4554 break;
4555 case 'F':
4556 patch_script_path = optarg;
4557 break;
4558 case 'R':
4559 can_recurse = 1;
4560 break;
4561 default:
4562 usage_revert();
4563 /* NOTREACHED */
4567 argc -= optind;
4568 argv += optind;
4570 #ifndef PROFILE
4571 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4572 "unveil", NULL) == -1)
4573 err(1, "pledge");
4574 #endif
4575 if (argc < 1)
4576 usage_revert();
4577 if (patch_script_path && !pflag)
4578 errx(1, "-F option can only be used together with -p option");
4580 cwd = getcwd(NULL, 0);
4581 if (cwd == NULL) {
4582 error = got_error_from_errno("getcwd");
4583 goto done;
4585 error = got_worktree_open(&worktree, cwd);
4586 if (error)
4587 goto done;
4589 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4590 NULL);
4591 if (error != NULL)
4592 goto done;
4594 if (patch_script_path) {
4595 patch_script_file = fopen(patch_script_path, "r");
4596 if (patch_script_file == NULL) {
4597 error = got_error_from_errno2("fopen",
4598 patch_script_path);
4599 goto done;
4602 error = apply_unveil(got_repo_get_path(repo), 1,
4603 got_worktree_get_root_path(worktree));
4604 if (error)
4605 goto done;
4607 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4608 if (error)
4609 goto done;
4611 if (!can_recurse) {
4612 char *ondisk_path;
4613 struct stat sb;
4614 TAILQ_FOREACH(pe, &paths, entry) {
4615 if (asprintf(&ondisk_path, "%s/%s",
4616 got_worktree_get_root_path(worktree),
4617 pe->path) == -1) {
4618 error = got_error_from_errno("asprintf");
4619 goto done;
4621 if (lstat(ondisk_path, &sb) == -1) {
4622 if (errno == ENOENT) {
4623 free(ondisk_path);
4624 continue;
4626 error = got_error_from_errno2("lstat",
4627 ondisk_path);
4628 free(ondisk_path);
4629 goto done;
4631 free(ondisk_path);
4632 if (S_ISDIR(sb.st_mode)) {
4633 error = got_error_msg(GOT_ERR_BAD_PATH,
4634 "reverting directories requires -R option");
4635 goto done;
4640 cpa.patch_script_file = patch_script_file;
4641 cpa.action = "revert";
4642 error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
4643 pflag ? choose_patch : NULL, &cpa, repo);
4644 if (error)
4645 goto done;
4646 done:
4647 if (patch_script_file && fclose(patch_script_file) == EOF &&
4648 error == NULL)
4649 error = got_error_from_errno2("fclose", patch_script_path);
4650 if (repo)
4651 got_repo_close(repo);
4652 if (worktree)
4653 got_worktree_close(worktree);
4654 free(path);
4655 free(cwd);
4656 return error;
4659 __dead static void
4660 usage_commit(void)
4662 fprintf(stderr, "usage: %s commit [-m msg] [path ...]\n",
4663 getprogname());
4664 exit(1);
4667 struct collect_commit_logmsg_arg {
4668 const char *cmdline_log;
4669 const char *editor;
4670 const char *worktree_path;
4671 const char *branch_name;
4672 const char *repo_path;
4673 char *logmsg_path;
4677 static const struct got_error *
4678 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
4679 void *arg)
4681 char *initial_content = NULL;
4682 struct got_pathlist_entry *pe;
4683 const struct got_error *err = NULL;
4684 char *template = NULL;
4685 struct collect_commit_logmsg_arg *a = arg;
4686 int fd;
4687 size_t len;
4689 /* if a message was specified on the command line, just use it */
4690 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
4691 len = strlen(a->cmdline_log) + 1;
4692 *logmsg = malloc(len + 1);
4693 if (*logmsg == NULL)
4694 return got_error_from_errno("malloc");
4695 strlcpy(*logmsg, a->cmdline_log, len);
4696 return NULL;
4699 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
4700 return got_error_from_errno("asprintf");
4702 if (asprintf(&initial_content,
4703 "\n# changes to be committed on branch %s:\n",
4704 a->branch_name) == -1)
4705 return got_error_from_errno("asprintf");
4707 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
4708 if (err)
4709 goto done;
4711 dprintf(fd, initial_content);
4713 TAILQ_FOREACH(pe, commitable_paths, entry) {
4714 struct got_commitable *ct = pe->data;
4715 dprintf(fd, "# %c %s\n",
4716 got_commitable_get_status(ct),
4717 got_commitable_get_path(ct));
4719 close(fd);
4721 err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content);
4722 done:
4723 free(initial_content);
4724 free(template);
4726 /* Editor is done; we can now apply unveil(2) */
4727 if (err == NULL) {
4728 err = apply_unveil(a->repo_path, 0, a->worktree_path);
4729 if (err) {
4730 free(*logmsg);
4731 *logmsg = NULL;
4734 return err;
4737 static const struct got_error *
4738 cmd_commit(int argc, char *argv[])
4740 const struct got_error *error = NULL;
4741 struct got_worktree *worktree = NULL;
4742 struct got_repository *repo = NULL;
4743 char *cwd = NULL, *id_str = NULL;
4744 struct got_object_id *id = NULL;
4745 const char *logmsg = NULL;
4746 struct collect_commit_logmsg_arg cl_arg;
4747 char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
4748 int ch, rebase_in_progress, histedit_in_progress, preserve_logmsg = 0;
4749 struct got_pathlist_head paths;
4751 TAILQ_INIT(&paths);
4752 cl_arg.logmsg_path = NULL;
4754 while ((ch = getopt(argc, argv, "m:")) != -1) {
4755 switch (ch) {
4756 case 'm':
4757 logmsg = optarg;
4758 break;
4759 default:
4760 usage_commit();
4761 /* NOTREACHED */
4765 argc -= optind;
4766 argv += optind;
4768 #ifndef PROFILE
4769 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4770 "unveil", NULL) == -1)
4771 err(1, "pledge");
4772 #endif
4773 cwd = getcwd(NULL, 0);
4774 if (cwd == NULL) {
4775 error = got_error_from_errno("getcwd");
4776 goto done;
4778 error = got_worktree_open(&worktree, cwd);
4779 if (error)
4780 goto done;
4782 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
4783 if (error)
4784 goto done;
4785 if (rebase_in_progress) {
4786 error = got_error(GOT_ERR_REBASING);
4787 goto done;
4790 error = got_worktree_histedit_in_progress(&histedit_in_progress,
4791 worktree);
4792 if (error)
4793 goto done;
4795 error = get_gitconfig_path(&gitconfig_path);
4796 if (error)
4797 goto done;
4798 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4799 gitconfig_path);
4800 if (error != NULL)
4801 goto done;
4803 error = get_author(&author, repo);
4804 if (error)
4805 return error;
4808 * unveil(2) traverses exec(2); if an editor is used we have
4809 * to apply unveil after the log message has been written.
4811 if (logmsg == NULL || strlen(logmsg) == 0)
4812 error = get_editor(&editor);
4813 else
4814 error = apply_unveil(got_repo_get_path(repo), 0,
4815 got_worktree_get_root_path(worktree));
4816 if (error)
4817 goto done;
4819 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4820 if (error)
4821 goto done;
4823 cl_arg.editor = editor;
4824 cl_arg.cmdline_log = logmsg;
4825 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
4826 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
4827 if (!histedit_in_progress) {
4828 if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
4829 error = got_error(GOT_ERR_COMMIT_BRANCH);
4830 goto done;
4832 cl_arg.branch_name += 11;
4834 cl_arg.repo_path = got_repo_get_path(repo);
4835 error = got_worktree_commit(&id, worktree, &paths, author, NULL,
4836 collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
4837 if (error) {
4838 if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
4839 cl_arg.logmsg_path != NULL)
4840 preserve_logmsg = 1;
4841 goto done;
4844 error = got_object_id_str(&id_str, id);
4845 if (error)
4846 goto done;
4847 printf("Created commit %s\n", id_str);
4848 done:
4849 if (preserve_logmsg) {
4850 fprintf(stderr, "%s: log message preserved in %s\n",
4851 getprogname(), cl_arg.logmsg_path);
4852 } else if (cl_arg.logmsg_path && unlink(cl_arg.logmsg_path) == -1 &&
4853 error == NULL)
4854 error = got_error_from_errno2("unlink", cl_arg.logmsg_path);
4855 free(cl_arg.logmsg_path);
4856 if (repo)
4857 got_repo_close(repo);
4858 if (worktree)
4859 got_worktree_close(worktree);
4860 free(cwd);
4861 free(id_str);
4862 free(gitconfig_path);
4863 free(editor);
4864 free(author);
4865 return error;
4868 __dead static void
4869 usage_cherrypick(void)
4871 fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
4872 exit(1);
4875 static const struct got_error *
4876 cmd_cherrypick(int argc, char *argv[])
4878 const struct got_error *error = NULL;
4879 struct got_worktree *worktree = NULL;
4880 struct got_repository *repo = NULL;
4881 char *cwd = NULL, *commit_id_str = NULL;
4882 struct got_object_id *commit_id = NULL;
4883 struct got_commit_object *commit = NULL;
4884 struct got_object_qid *pid;
4885 struct got_reference *head_ref = NULL;
4886 int ch, did_something = 0;
4888 while ((ch = getopt(argc, argv, "")) != -1) {
4889 switch (ch) {
4890 default:
4891 usage_cherrypick();
4892 /* NOTREACHED */
4896 argc -= optind;
4897 argv += optind;
4899 #ifndef PROFILE
4900 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4901 "unveil", NULL) == -1)
4902 err(1, "pledge");
4903 #endif
4904 if (argc != 1)
4905 usage_cherrypick();
4907 cwd = getcwd(NULL, 0);
4908 if (cwd == NULL) {
4909 error = got_error_from_errno("getcwd");
4910 goto done;
4912 error = got_worktree_open(&worktree, cwd);
4913 if (error)
4914 goto done;
4916 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4917 NULL);
4918 if (error != NULL)
4919 goto done;
4921 error = apply_unveil(got_repo_get_path(repo), 0,
4922 got_worktree_get_root_path(worktree));
4923 if (error)
4924 goto done;
4926 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
4927 GOT_OBJ_TYPE_COMMIT, repo);
4928 if (error != NULL) {
4929 struct got_reference *ref;
4930 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
4931 goto done;
4932 error = got_ref_open(&ref, repo, argv[0], 0);
4933 if (error != NULL)
4934 goto done;
4935 error = got_ref_resolve(&commit_id, repo, ref);
4936 got_ref_close(ref);
4937 if (error != NULL)
4938 goto done;
4940 error = got_object_id_str(&commit_id_str, commit_id);
4941 if (error)
4942 goto done;
4944 error = got_ref_open(&head_ref, repo,
4945 got_worktree_get_head_ref_name(worktree), 0);
4946 if (error != NULL)
4947 goto done;
4949 error = check_same_branch(commit_id, head_ref, NULL, repo);
4950 if (error) {
4951 if (error->code != GOT_ERR_ANCESTRY)
4952 goto done;
4953 error = NULL;
4954 } else {
4955 error = got_error(GOT_ERR_SAME_BRANCH);
4956 goto done;
4959 error = got_object_open_as_commit(&commit, repo, commit_id);
4960 if (error)
4961 goto done;
4962 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
4963 error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
4964 commit_id, repo, update_progress, &did_something, check_cancelled,
4965 NULL);
4966 if (error != NULL)
4967 goto done;
4969 if (did_something)
4970 printf("Merged commit %s\n", commit_id_str);
4971 done:
4972 if (commit)
4973 got_object_commit_close(commit);
4974 free(commit_id_str);
4975 if (head_ref)
4976 got_ref_close(head_ref);
4977 if (worktree)
4978 got_worktree_close(worktree);
4979 if (repo)
4980 got_repo_close(repo);
4981 return error;
4984 __dead static void
4985 usage_backout(void)
4987 fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
4988 exit(1);
4991 static const struct got_error *
4992 cmd_backout(int argc, char *argv[])
4994 const struct got_error *error = NULL;
4995 struct got_worktree *worktree = NULL;
4996 struct got_repository *repo = NULL;
4997 char *cwd = NULL, *commit_id_str = NULL;
4998 struct got_object_id *commit_id = NULL;
4999 struct got_commit_object *commit = NULL;
5000 struct got_object_qid *pid;
5001 struct got_reference *head_ref = NULL;
5002 int ch, did_something = 0;
5004 while ((ch = getopt(argc, argv, "")) != -1) {
5005 switch (ch) {
5006 default:
5007 usage_backout();
5008 /* NOTREACHED */
5012 argc -= optind;
5013 argv += optind;
5015 #ifndef PROFILE
5016 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5017 "unveil", NULL) == -1)
5018 err(1, "pledge");
5019 #endif
5020 if (argc != 1)
5021 usage_backout();
5023 cwd = getcwd(NULL, 0);
5024 if (cwd == NULL) {
5025 error = got_error_from_errno("getcwd");
5026 goto done;
5028 error = got_worktree_open(&worktree, cwd);
5029 if (error)
5030 goto done;
5032 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5033 NULL);
5034 if (error != NULL)
5035 goto done;
5037 error = apply_unveil(got_repo_get_path(repo), 0,
5038 got_worktree_get_root_path(worktree));
5039 if (error)
5040 goto done;
5042 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
5043 GOT_OBJ_TYPE_COMMIT, repo);
5044 if (error != NULL) {
5045 struct got_reference *ref;
5046 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
5047 goto done;
5048 error = got_ref_open(&ref, repo, argv[0], 0);
5049 if (error != NULL)
5050 goto done;
5051 error = got_ref_resolve(&commit_id, repo, ref);
5052 got_ref_close(ref);
5053 if (error != NULL)
5054 goto done;
5056 error = got_object_id_str(&commit_id_str, commit_id);
5057 if (error)
5058 goto done;
5060 error = got_ref_open(&head_ref, repo,
5061 got_worktree_get_head_ref_name(worktree), 0);
5062 if (error != NULL)
5063 goto done;
5065 error = check_same_branch(commit_id, head_ref, NULL, repo);
5066 if (error)
5067 goto done;
5069 error = got_object_open_as_commit(&commit, repo, commit_id);
5070 if (error)
5071 goto done;
5072 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
5073 if (pid == NULL) {
5074 error = got_error(GOT_ERR_ROOT_COMMIT);
5075 goto done;
5078 error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
5079 update_progress, &did_something, check_cancelled, NULL);
5080 if (error != NULL)
5081 goto done;
5083 if (did_something)
5084 printf("Backed out commit %s\n", commit_id_str);
5085 done:
5086 if (commit)
5087 got_object_commit_close(commit);
5088 free(commit_id_str);
5089 if (head_ref)
5090 got_ref_close(head_ref);
5091 if (worktree)
5092 got_worktree_close(worktree);
5093 if (repo)
5094 got_repo_close(repo);
5095 return error;
5098 __dead static void
5099 usage_rebase(void)
5101 fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
5102 getprogname());
5103 exit(1);
5106 void
5107 trim_logmsg(char *logmsg, int limit)
5109 char *nl;
5110 size_t len;
5112 len = strlen(logmsg);
5113 if (len > limit)
5114 len = limit;
5115 logmsg[len] = '\0';
5116 nl = strchr(logmsg, '\n');
5117 if (nl)
5118 *nl = '\0';
5121 static const struct got_error *
5122 get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
5124 const struct got_error *err;
5125 char *logmsg0 = NULL;
5126 const char *s;
5128 err = got_object_commit_get_logmsg(&logmsg0, commit);
5129 if (err)
5130 return err;
5132 s = logmsg0;
5133 while (isspace((unsigned char)s[0]))
5134 s++;
5136 *logmsg = strdup(s);
5137 if (*logmsg == NULL) {
5138 err = got_error_from_errno("strdup");
5139 goto done;
5142 trim_logmsg(*logmsg, limit);
5143 done:
5144 free(logmsg0);
5145 return err;
5148 static const struct got_error *
5149 show_rebase_progress(struct got_commit_object *commit,
5150 struct got_object_id *old_id, struct got_object_id *new_id)
5152 const struct got_error *err;
5153 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
5155 err = got_object_id_str(&old_id_str, old_id);
5156 if (err)
5157 goto done;
5159 if (new_id) {
5160 err = got_object_id_str(&new_id_str, new_id);
5161 if (err)
5162 goto done;
5165 old_id_str[12] = '\0';
5166 if (new_id_str)
5167 new_id_str[12] = '\0';
5169 err = get_short_logmsg(&logmsg, 42, commit);
5170 if (err)
5171 goto done;
5173 printf("%s -> %s: %s\n", old_id_str,
5174 new_id_str ? new_id_str : "no-op change", logmsg);
5175 done:
5176 free(old_id_str);
5177 free(new_id_str);
5178 return err;
5181 static const struct got_error *
5182 rebase_progress(void *arg, unsigned char status, const char *path)
5184 unsigned char *rebase_status = arg;
5186 while (path[0] == '/')
5187 path++;
5188 printf("%c %s\n", status, path);
5190 if (*rebase_status == GOT_STATUS_CONFLICT)
5191 return NULL;
5192 if (status == GOT_STATUS_CONFLICT || status == GOT_STATUS_MERGE)
5193 *rebase_status = status;
5194 return NULL;
5197 static const struct got_error *
5198 rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
5199 struct got_reference *branch, struct got_reference *new_base_branch,
5200 struct got_reference *tmp_branch, struct got_repository *repo)
5202 printf("Switching work tree to %s\n", got_ref_get_name(branch));
5203 return got_worktree_rebase_complete(worktree, fileindex,
5204 new_base_branch, tmp_branch, branch, repo);
5207 static const struct got_error *
5208 rebase_commit(struct got_pathlist_head *merged_paths,
5209 struct got_worktree *worktree, struct got_fileindex *fileindex,
5210 struct got_reference *tmp_branch,
5211 struct got_object_id *commit_id, struct got_repository *repo)
5213 const struct got_error *error;
5214 struct got_commit_object *commit;
5215 struct got_object_id *new_commit_id;
5217 error = got_object_open_as_commit(&commit, repo, commit_id);
5218 if (error)
5219 return error;
5221 error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
5222 worktree, fileindex, tmp_branch, commit, commit_id, repo);
5223 if (error) {
5224 if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
5225 goto done;
5226 error = show_rebase_progress(commit, commit_id, NULL);
5227 } else {
5228 error = show_rebase_progress(commit, commit_id, new_commit_id);
5229 free(new_commit_id);
5231 done:
5232 got_object_commit_close(commit);
5233 return error;
5236 struct check_path_prefix_arg {
5237 const char *path_prefix;
5238 size_t len;
5239 int errcode;
5242 static const struct got_error *
5243 check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
5244 struct got_blob_object *blob2, struct got_object_id *id1,
5245 struct got_object_id *id2, const char *path1, const char *path2,
5246 mode_t mode1, mode_t mode2, struct got_repository *repo)
5248 struct check_path_prefix_arg *a = arg;
5250 if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
5251 (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
5252 return got_error(a->errcode);
5254 return NULL;
5257 static const struct got_error *
5258 check_path_prefix(struct got_object_id *parent_id,
5259 struct got_object_id *commit_id, const char *path_prefix,
5260 int errcode, struct got_repository *repo)
5262 const struct got_error *err;
5263 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
5264 struct got_commit_object *commit = NULL, *parent_commit = NULL;
5265 struct check_path_prefix_arg cpp_arg;
5267 if (got_path_is_root_dir(path_prefix))
5268 return NULL;
5270 err = got_object_open_as_commit(&commit, repo, commit_id);
5271 if (err)
5272 goto done;
5274 err = got_object_open_as_commit(&parent_commit, repo, parent_id);
5275 if (err)
5276 goto done;
5278 err = got_object_open_as_tree(&tree1, repo,
5279 got_object_commit_get_tree_id(parent_commit));
5280 if (err)
5281 goto done;
5283 err = got_object_open_as_tree(&tree2, repo,
5284 got_object_commit_get_tree_id(commit));
5285 if (err)
5286 goto done;
5288 cpp_arg.path_prefix = path_prefix;
5289 while (cpp_arg.path_prefix[0] == '/')
5290 cpp_arg.path_prefix++;
5291 cpp_arg.len = strlen(cpp_arg.path_prefix);
5292 cpp_arg.errcode = errcode;
5293 err = got_diff_tree(tree1, tree2, "", "", repo,
5294 check_path_prefix_in_diff, &cpp_arg, 0);
5295 done:
5296 if (tree1)
5297 got_object_tree_close(tree1);
5298 if (tree2)
5299 got_object_tree_close(tree2);
5300 if (commit)
5301 got_object_commit_close(commit);
5302 if (parent_commit)
5303 got_object_commit_close(parent_commit);
5304 return err;
5307 static const struct got_error *
5308 collect_commits(struct got_object_id_queue *commits,
5309 struct got_object_id *initial_commit_id,
5310 struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
5311 const char *path_prefix, int path_prefix_errcode,
5312 struct got_repository *repo)
5314 const struct got_error *err = NULL;
5315 struct got_commit_graph *graph = NULL;
5316 struct got_object_id *parent_id = NULL;
5317 struct got_object_qid *qid;
5318 struct got_object_id *commit_id = initial_commit_id;
5320 err = got_commit_graph_open(&graph, initial_commit_id, "/", 1, repo);
5321 if (err)
5322 return err;
5324 err = got_commit_graph_iter_start(graph, iter_start_id, repo,
5325 check_cancelled, NULL);
5326 if (err)
5327 goto done;
5328 while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
5329 err = got_commit_graph_iter_next(&parent_id, graph);
5330 if (err) {
5331 if (err->code == GOT_ERR_ITER_COMPLETED) {
5332 err = got_error_msg(GOT_ERR_ANCESTRY,
5333 "ran out of commits to rebase before "
5334 "youngest common ancestor commit has "
5335 "been reached?!?");
5336 goto done;
5337 } else if (err->code != GOT_ERR_ITER_NEED_MORE)
5338 goto done;
5339 err = got_commit_graph_fetch_commits(graph, 1, repo,
5340 check_cancelled, NULL);
5341 if (err)
5342 goto done;
5343 } else {
5344 err = check_path_prefix(parent_id, commit_id,
5345 path_prefix, path_prefix_errcode, repo);
5346 if (err)
5347 goto done;
5349 err = got_object_qid_alloc(&qid, commit_id);
5350 if (err)
5351 goto done;
5352 SIMPLEQ_INSERT_HEAD(commits, qid, entry);
5353 commit_id = parent_id;
5356 done:
5357 got_commit_graph_close(graph);
5358 return err;
5361 static const struct got_error *
5362 cmd_rebase(int argc, char *argv[])
5364 const struct got_error *error = NULL;
5365 struct got_worktree *worktree = NULL;
5366 struct got_repository *repo = NULL;
5367 struct got_fileindex *fileindex = NULL;
5368 char *cwd = NULL;
5369 struct got_reference *branch = NULL;
5370 struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
5371 struct got_object_id *commit_id = NULL, *parent_id = NULL;
5372 struct got_object_id *resume_commit_id = NULL;
5373 struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
5374 struct got_commit_object *commit = NULL;
5375 int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
5376 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
5377 struct got_object_id_queue commits;
5378 struct got_pathlist_head merged_paths;
5379 const struct got_object_id_queue *parent_ids;
5380 struct got_object_qid *qid, *pid;
5382 SIMPLEQ_INIT(&commits);
5383 TAILQ_INIT(&merged_paths);
5385 while ((ch = getopt(argc, argv, "ac")) != -1) {
5386 switch (ch) {
5387 case 'a':
5388 abort_rebase = 1;
5389 break;
5390 case 'c':
5391 continue_rebase = 1;
5392 break;
5393 default:
5394 usage_rebase();
5395 /* NOTREACHED */
5399 argc -= optind;
5400 argv += optind;
5402 #ifndef PROFILE
5403 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5404 "unveil", NULL) == -1)
5405 err(1, "pledge");
5406 #endif
5407 if (abort_rebase && continue_rebase)
5408 usage_rebase();
5409 else if (abort_rebase || continue_rebase) {
5410 if (argc != 0)
5411 usage_rebase();
5412 } else if (argc != 1)
5413 usage_rebase();
5415 cwd = getcwd(NULL, 0);
5416 if (cwd == NULL) {
5417 error = got_error_from_errno("getcwd");
5418 goto done;
5420 error = got_worktree_open(&worktree, cwd);
5421 if (error)
5422 goto done;
5424 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5425 NULL);
5426 if (error != NULL)
5427 goto done;
5429 error = apply_unveil(got_repo_get_path(repo), 0,
5430 got_worktree_get_root_path(worktree));
5431 if (error)
5432 goto done;
5434 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
5435 if (error)
5436 goto done;
5438 if (abort_rebase) {
5439 int did_something;
5440 if (!rebase_in_progress) {
5441 error = got_error(GOT_ERR_NOT_REBASING);
5442 goto done;
5444 error = got_worktree_rebase_continue(&resume_commit_id,
5445 &new_base_branch, &tmp_branch, &branch, &fileindex,
5446 worktree, repo);
5447 if (error)
5448 goto done;
5449 printf("Switching work tree to %s\n",
5450 got_ref_get_symref_target(new_base_branch));
5451 error = got_worktree_rebase_abort(worktree, fileindex, repo,
5452 new_base_branch, update_progress, &did_something);
5453 if (error)
5454 goto done;
5455 printf("Rebase of %s aborted\n", got_ref_get_name(branch));
5456 goto done; /* nothing else to do */
5459 if (continue_rebase) {
5460 if (!rebase_in_progress) {
5461 error = got_error(GOT_ERR_NOT_REBASING);
5462 goto done;
5464 error = got_worktree_rebase_continue(&resume_commit_id,
5465 &new_base_branch, &tmp_branch, &branch, &fileindex,
5466 worktree, repo);
5467 if (error)
5468 goto done;
5470 error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
5471 resume_commit_id, repo);
5472 if (error)
5473 goto done;
5475 yca_id = got_object_id_dup(resume_commit_id);
5476 if (yca_id == NULL) {
5477 error = got_error_from_errno("got_object_id_dup");
5478 goto done;
5480 } else {
5481 error = got_ref_open(&branch, repo, argv[0], 0);
5482 if (error != NULL)
5483 goto done;
5486 error = got_ref_resolve(&branch_head_commit_id, repo, branch);
5487 if (error)
5488 goto done;
5490 if (!continue_rebase) {
5491 struct got_object_id *base_commit_id;
5493 base_commit_id = got_worktree_get_base_commit_id(worktree);
5494 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
5495 base_commit_id, branch_head_commit_id, repo,
5496 check_cancelled, NULL);
5497 if (error)
5498 goto done;
5499 if (yca_id == NULL) {
5500 error = got_error_msg(GOT_ERR_ANCESTRY,
5501 "specified branch shares no common ancestry "
5502 "with work tree's branch");
5503 goto done;
5506 error = check_same_branch(base_commit_id, branch, yca_id, repo);
5507 if (error) {
5508 if (error->code != GOT_ERR_ANCESTRY)
5509 goto done;
5510 error = NULL;
5511 } else {
5512 error = got_error_msg(GOT_ERR_SAME_BRANCH,
5513 "specified branch resolves to a commit which "
5514 "is already contained in work tree's branch");
5515 goto done;
5517 error = got_worktree_rebase_prepare(&new_base_branch,
5518 &tmp_branch, &fileindex, worktree, branch, repo);
5519 if (error)
5520 goto done;
5523 commit_id = branch_head_commit_id;
5524 error = got_object_open_as_commit(&commit, repo, commit_id);
5525 if (error)
5526 goto done;
5528 parent_ids = got_object_commit_get_parent_ids(commit);
5529 pid = SIMPLEQ_FIRST(parent_ids);
5530 if (pid == NULL) {
5531 if (!continue_rebase) {
5532 int did_something;
5533 error = got_worktree_rebase_abort(worktree, fileindex,
5534 repo, new_base_branch, update_progress,
5535 &did_something);
5536 if (error)
5537 goto done;
5538 printf("Rebase of %s aborted\n",
5539 got_ref_get_name(branch));
5541 error = got_error(GOT_ERR_EMPTY_REBASE);
5542 goto done;
5544 error = collect_commits(&commits, commit_id, pid->id,
5545 yca_id, got_worktree_get_path_prefix(worktree),
5546 GOT_ERR_REBASE_PATH, repo);
5547 got_object_commit_close(commit);
5548 commit = NULL;
5549 if (error)
5550 goto done;
5552 if (SIMPLEQ_EMPTY(&commits)) {
5553 if (continue_rebase) {
5554 error = rebase_complete(worktree, fileindex,
5555 branch, new_base_branch, tmp_branch, repo);
5556 goto done;
5557 } else {
5558 /* Fast-forward the reference of the branch. */
5559 struct got_object_id *new_head_commit_id;
5560 char *id_str;
5561 error = got_ref_resolve(&new_head_commit_id, repo,
5562 new_base_branch);
5563 if (error)
5564 goto done;
5565 error = got_object_id_str(&id_str, new_head_commit_id);
5566 printf("Forwarding %s to commit %s\n",
5567 got_ref_get_name(branch), id_str);
5568 free(id_str);
5569 error = got_ref_change_ref(branch,
5570 new_head_commit_id);
5571 if (error)
5572 goto done;
5576 pid = NULL;
5577 SIMPLEQ_FOREACH(qid, &commits, entry) {
5578 commit_id = qid->id;
5579 parent_id = pid ? pid->id : yca_id;
5580 pid = qid;
5582 error = got_worktree_rebase_merge_files(&merged_paths,
5583 worktree, fileindex, parent_id, commit_id, repo,
5584 rebase_progress, &rebase_status, check_cancelled, NULL);
5585 if (error)
5586 goto done;
5588 if (rebase_status == GOT_STATUS_CONFLICT) {
5589 got_worktree_rebase_pathlist_free(&merged_paths);
5590 break;
5593 error = rebase_commit(&merged_paths, worktree, fileindex,
5594 tmp_branch, commit_id, repo);
5595 got_worktree_rebase_pathlist_free(&merged_paths);
5596 if (error)
5597 goto done;
5600 if (rebase_status == GOT_STATUS_CONFLICT) {
5601 error = got_worktree_rebase_postpone(worktree, fileindex);
5602 if (error)
5603 goto done;
5604 error = got_error_msg(GOT_ERR_CONFLICTS,
5605 "conflicts must be resolved before rebasing can continue");
5606 } else
5607 error = rebase_complete(worktree, fileindex, branch,
5608 new_base_branch, tmp_branch, repo);
5609 done:
5610 got_object_id_queue_free(&commits);
5611 free(branch_head_commit_id);
5612 free(resume_commit_id);
5613 free(yca_id);
5614 if (commit)
5615 got_object_commit_close(commit);
5616 if (branch)
5617 got_ref_close(branch);
5618 if (new_base_branch)
5619 got_ref_close(new_base_branch);
5620 if (tmp_branch)
5621 got_ref_close(tmp_branch);
5622 if (worktree)
5623 got_worktree_close(worktree);
5624 if (repo)
5625 got_repo_close(repo);
5626 return error;
5629 __dead static void
5630 usage_histedit(void)
5632 fprintf(stderr, "usage: %s histedit [-a] [-c] [-F histedit-script]\n",
5633 getprogname());
5634 exit(1);
5637 #define GOT_HISTEDIT_PICK 'p'
5638 #define GOT_HISTEDIT_EDIT 'e'
5639 #define GOT_HISTEDIT_FOLD 'f'
5640 #define GOT_HISTEDIT_DROP 'd'
5641 #define GOT_HISTEDIT_MESG 'm'
5643 static struct got_histedit_cmd {
5644 unsigned char code;
5645 const char *name;
5646 const char *desc;
5647 } got_histedit_cmds[] = {
5648 { GOT_HISTEDIT_PICK, "pick", "use commit" },
5649 { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
5650 { GOT_HISTEDIT_FOLD, "fold", "combine with commit below" },
5651 { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
5652 { GOT_HISTEDIT_MESG, "mesg",
5653 "single-line log message for commit above (open editor if empty)" },
5656 struct got_histedit_list_entry {
5657 TAILQ_ENTRY(got_histedit_list_entry) entry;
5658 struct got_object_id *commit_id;
5659 const struct got_histedit_cmd *cmd;
5660 char *logmsg;
5662 TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
5664 static const struct got_error *
5665 histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
5666 FILE *f, struct got_repository *repo)
5668 const struct got_error *err = NULL;
5669 char *logmsg = NULL, *id_str = NULL;
5670 struct got_commit_object *commit = NULL;
5671 int n;
5673 err = got_object_open_as_commit(&commit, repo, commit_id);
5674 if (err)
5675 goto done;
5677 err = get_short_logmsg(&logmsg, 34, commit);
5678 if (err)
5679 goto done;
5681 err = got_object_id_str(&id_str, commit_id);
5682 if (err)
5683 goto done;
5685 n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
5686 if (n < 0)
5687 err = got_ferror(f, GOT_ERR_IO);
5688 done:
5689 if (commit)
5690 got_object_commit_close(commit);
5691 free(id_str);
5692 free(logmsg);
5693 return err;
5696 static const struct got_error *
5697 histedit_write_commit_list(struct got_object_id_queue *commits, FILE *f,
5698 struct got_repository *repo)
5700 const struct got_error *err = NULL;
5701 struct got_object_qid *qid;
5703 if (SIMPLEQ_EMPTY(commits))
5704 return got_error(GOT_ERR_EMPTY_HISTEDIT);
5706 SIMPLEQ_FOREACH(qid, commits, entry) {
5707 err = histedit_write_commit(qid->id, got_histedit_cmds[0].name,
5708 f, repo);
5709 if (err)
5710 break;
5713 return err;
5716 static const struct got_error *
5717 write_cmd_list(FILE *f)
5719 const struct got_error *err = NULL;
5720 int n, i;
5722 n = fprintf(f, "# Available histedit commands:\n");
5723 if (n < 0)
5724 return got_ferror(f, GOT_ERR_IO);
5726 for (i = 0; i < nitems(got_histedit_cmds); i++) {
5727 struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
5728 n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
5729 cmd->desc);
5730 if (n < 0) {
5731 err = got_ferror(f, GOT_ERR_IO);
5732 break;
5735 n = fprintf(f, "# Commits will be processed in order from top to "
5736 "bottom of this file.\n");
5737 if (n < 0)
5738 return got_ferror(f, GOT_ERR_IO);
5739 return err;
5742 static const struct got_error *
5743 histedit_syntax_error(int lineno)
5745 static char msg[42];
5746 int ret;
5748 ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
5749 lineno);
5750 if (ret == -1 || ret >= sizeof(msg))
5751 return got_error(GOT_ERR_HISTEDIT_SYNTAX);
5753 return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
5756 static const struct got_error *
5757 append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
5758 char *logmsg, struct got_repository *repo)
5760 const struct got_error *err;
5761 struct got_commit_object *folded_commit = NULL;
5762 char *id_str, *folded_logmsg = NULL;
5764 err = got_object_id_str(&id_str, hle->commit_id);
5765 if (err)
5766 return err;
5768 err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
5769 if (err)
5770 goto done;
5772 err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
5773 if (err)
5774 goto done;
5775 if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
5776 logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
5777 folded_logmsg) == -1) {
5778 err = got_error_from_errno("asprintf");
5779 goto done;
5781 done:
5782 if (folded_commit)
5783 got_object_commit_close(folded_commit);
5784 free(id_str);
5785 free(folded_logmsg);
5786 return err;
5789 static struct got_histedit_list_entry *
5790 get_folded_commits(struct got_histedit_list_entry *hle)
5792 struct got_histedit_list_entry *prev, *folded = NULL;
5794 prev = TAILQ_PREV(hle, got_histedit_list, entry);
5795 while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
5796 prev->cmd->code == GOT_HISTEDIT_DROP)) {
5797 if (prev->cmd->code == GOT_HISTEDIT_FOLD)
5798 folded = prev;
5799 prev = TAILQ_PREV(prev, got_histedit_list, entry);
5802 return folded;
5805 static const struct got_error *
5806 histedit_edit_logmsg(struct got_histedit_list_entry *hle,
5807 struct got_repository *repo)
5809 char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
5810 char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
5811 const struct got_error *err = NULL;
5812 struct got_commit_object *commit = NULL;
5813 int fd;
5814 struct got_histedit_list_entry *folded = NULL;
5816 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
5817 if (err)
5818 return err;
5820 folded = get_folded_commits(hle);
5821 if (folded) {
5822 while (folded != hle) {
5823 if (folded->cmd->code == GOT_HISTEDIT_DROP) {
5824 folded = TAILQ_NEXT(folded, entry);
5825 continue;
5827 err = append_folded_commit_msg(&new_msg, folded,
5828 logmsg, repo);
5829 if (err)
5830 goto done;
5831 free(logmsg);
5832 logmsg = new_msg;
5833 folded = TAILQ_NEXT(folded, entry);
5837 err = got_object_id_str(&id_str, hle->commit_id);
5838 if (err)
5839 goto done;
5840 err = got_object_commit_get_logmsg(&orig_logmsg, commit);
5841 if (err)
5842 goto done;
5843 if (asprintf(&new_msg,
5844 "%s\n# original log message of commit %s: %s",
5845 logmsg ? logmsg : "", id_str, orig_logmsg) == -1) {
5846 err = got_error_from_errno("asprintf");
5847 goto done;
5849 free(logmsg);
5850 logmsg = new_msg;
5852 err = got_object_id_str(&id_str, hle->commit_id);
5853 if (err)
5854 goto done;
5856 err = got_opentemp_named_fd(&logmsg_path, &fd, "/tmp/got-logmsg");
5857 if (err)
5858 goto done;
5860 dprintf(fd, logmsg);
5861 close(fd);
5863 err = get_editor(&editor);
5864 if (err)
5865 goto done;
5867 err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg);
5868 if (err) {
5869 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
5870 goto done;
5871 err = got_object_commit_get_logmsg(&hle->logmsg, commit);
5873 done:
5874 if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
5875 err = got_error_from_errno2("unlink", logmsg_path);
5876 free(logmsg_path);
5877 free(logmsg);
5878 free(orig_logmsg);
5879 free(editor);
5880 if (commit)
5881 got_object_commit_close(commit);
5882 return err;
5885 static const struct got_error *
5886 histedit_parse_list(struct got_histedit_list *histedit_cmds,
5887 FILE *f, struct got_repository *repo)
5889 const struct got_error *err = NULL;
5890 char *line = NULL, *p, *end;
5891 size_t size;
5892 ssize_t len;
5893 int lineno = 0, i;
5894 const struct got_histedit_cmd *cmd;
5895 struct got_object_id *commit_id = NULL;
5896 struct got_histedit_list_entry *hle = NULL;
5898 for (;;) {
5899 len = getline(&line, &size, f);
5900 if (len == -1) {
5901 const struct got_error *getline_err;
5902 if (feof(f))
5903 break;
5904 getline_err = got_error_from_errno("getline");
5905 err = got_ferror(f, getline_err->code);
5906 break;
5908 lineno++;
5909 p = line;
5910 while (isspace((unsigned char)p[0]))
5911 p++;
5912 if (p[0] == '#' || p[0] == '\0') {
5913 free(line);
5914 line = NULL;
5915 continue;
5917 cmd = NULL;
5918 for (i = 0; i < nitems(got_histedit_cmds); i++) {
5919 cmd = &got_histedit_cmds[i];
5920 if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
5921 isspace((unsigned char)p[strlen(cmd->name)])) {
5922 p += strlen(cmd->name);
5923 break;
5925 if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
5926 p++;
5927 break;
5930 if (i == nitems(got_histedit_cmds)) {
5931 err = histedit_syntax_error(lineno);
5932 break;
5934 while (isspace((unsigned char)p[0]))
5935 p++;
5936 if (cmd->code == GOT_HISTEDIT_MESG) {
5937 if (hle == NULL || hle->logmsg != NULL) {
5938 err = got_error(GOT_ERR_HISTEDIT_CMD);
5939 break;
5941 if (p[0] == '\0') {
5942 err = histedit_edit_logmsg(hle, repo);
5943 if (err)
5944 break;
5945 } else {
5946 hle->logmsg = strdup(p);
5947 if (hle->logmsg == NULL) {
5948 err = got_error_from_errno("strdup");
5949 break;
5952 free(line);
5953 line = NULL;
5954 continue;
5955 } else {
5956 end = p;
5957 while (end[0] && !isspace((unsigned char)end[0]))
5958 end++;
5959 *end = '\0';
5961 err = got_object_resolve_id_str(&commit_id, repo, p);
5962 if (err) {
5963 /* override error code */
5964 err = histedit_syntax_error(lineno);
5965 break;
5968 hle = malloc(sizeof(*hle));
5969 if (hle == NULL) {
5970 err = got_error_from_errno("malloc");
5971 break;
5973 hle->cmd = cmd;
5974 hle->commit_id = commit_id;
5975 hle->logmsg = NULL;
5976 commit_id = NULL;
5977 free(line);
5978 line = NULL;
5979 TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
5982 free(line);
5983 free(commit_id);
5984 return err;
5987 static const struct got_error *
5988 histedit_check_script(struct got_histedit_list *histedit_cmds,
5989 struct got_object_id_queue *commits, struct got_repository *repo)
5991 const struct got_error *err = NULL;
5992 struct got_object_qid *qid;
5993 struct got_histedit_list_entry *hle;
5994 static char msg[80];
5995 char *id_str;
5997 if (TAILQ_EMPTY(histedit_cmds))
5998 return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
5999 "histedit script contains no commands");
6000 if (SIMPLEQ_EMPTY(commits))
6001 return got_error(GOT_ERR_EMPTY_HISTEDIT);
6003 SIMPLEQ_FOREACH(qid, commits, entry) {
6004 TAILQ_FOREACH(hle, histedit_cmds, entry) {
6005 if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
6006 break;
6008 if (hle == NULL) {
6009 err = got_object_id_str(&id_str, qid->id);
6010 if (err)
6011 return err;
6012 snprintf(msg, sizeof(msg),
6013 "commit %s missing from histedit script", id_str);
6014 free(id_str);
6015 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
6019 hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
6020 if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
6021 return got_error_msg(GOT_ERR_HISTEDIT_CMD,
6022 "last commit in histedit script cannot be folded");
6024 return NULL;
6027 static const struct got_error *
6028 histedit_run_editor(struct got_histedit_list *histedit_cmds,
6029 const char *path, struct got_object_id_queue *commits,
6030 struct got_repository *repo)
6032 const struct got_error *err = NULL;
6033 char *editor;
6034 FILE *f = NULL;
6036 err = get_editor(&editor);
6037 if (err)
6038 return err;
6040 if (spawn_editor(editor, path) == -1) {
6041 err = got_error_from_errno("failed spawning editor");
6042 goto done;
6045 f = fopen(path, "r");
6046 if (f == NULL) {
6047 err = got_error_from_errno("fopen");
6048 goto done;
6050 err = histedit_parse_list(histedit_cmds, f, repo);
6051 if (err)
6052 goto done;
6054 err = histedit_check_script(histedit_cmds, commits, repo);
6055 done:
6056 if (f && fclose(f) != 0 && err == NULL)
6057 err = got_error_from_errno("fclose");
6058 free(editor);
6059 return err;
6062 static const struct got_error *
6063 histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
6064 struct got_object_id_queue *, const char *, struct got_repository *);
6066 static const struct got_error *
6067 histedit_edit_script(struct got_histedit_list *histedit_cmds,
6068 struct got_object_id_queue *commits, struct got_repository *repo)
6070 const struct got_error *err;
6071 FILE *f = NULL;
6072 char *path = NULL;
6074 err = got_opentemp_named(&path, &f, "got-histedit");
6075 if (err)
6076 return err;
6078 err = write_cmd_list(f);
6079 if (err)
6080 goto done;
6082 err = histedit_write_commit_list(commits, f, repo);
6083 if (err)
6084 goto done;
6086 if (fclose(f) != 0) {
6087 err = got_error_from_errno("fclose");
6088 goto done;
6090 f = NULL;
6092 err = histedit_run_editor(histedit_cmds, path, commits, repo);
6093 if (err) {
6094 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6095 err->code != GOT_ERR_HISTEDIT_CMD)
6096 goto done;
6097 err = histedit_edit_list_retry(histedit_cmds, err,
6098 commits, path, repo);
6100 done:
6101 if (f && fclose(f) != 0 && err == NULL)
6102 err = got_error_from_errno("fclose");
6103 if (path && unlink(path) != 0 && err == NULL)
6104 err = got_error_from_errno2("unlink", path);
6105 free(path);
6106 return err;
6109 static const struct got_error *
6110 histedit_save_list(struct got_histedit_list *histedit_cmds,
6111 struct got_worktree *worktree, struct got_repository *repo)
6113 const struct got_error *err = NULL;
6114 char *path = NULL;
6115 FILE *f = NULL;
6116 struct got_histedit_list_entry *hle;
6117 struct got_commit_object *commit = NULL;
6119 err = got_worktree_get_histedit_script_path(&path, worktree);
6120 if (err)
6121 return err;
6123 f = fopen(path, "w");
6124 if (f == NULL) {
6125 err = got_error_from_errno2("fopen", path);
6126 goto done;
6128 TAILQ_FOREACH(hle, histedit_cmds, entry) {
6129 err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
6130 repo);
6131 if (err)
6132 break;
6134 if (hle->logmsg) {
6135 int n = fprintf(f, "%c %s\n",
6136 GOT_HISTEDIT_MESG, hle->logmsg);
6137 if (n < 0) {
6138 err = got_ferror(f, GOT_ERR_IO);
6139 break;
6143 done:
6144 if (f && fclose(f) != 0 && err == NULL)
6145 err = got_error_from_errno("fclose");
6146 free(path);
6147 if (commit)
6148 got_object_commit_close(commit);
6149 return err;
6152 void
6153 histedit_free_list(struct got_histedit_list *histedit_cmds)
6155 struct got_histedit_list_entry *hle;
6157 while ((hle = TAILQ_FIRST(histedit_cmds))) {
6158 TAILQ_REMOVE(histedit_cmds, hle, entry);
6159 free(hle);
6163 static const struct got_error *
6164 histedit_load_list(struct got_histedit_list *histedit_cmds,
6165 const char *path, struct got_repository *repo)
6167 const struct got_error *err = NULL;
6168 FILE *f = NULL;
6170 f = fopen(path, "r");
6171 if (f == NULL) {
6172 err = got_error_from_errno2("fopen", path);
6173 goto done;
6176 err = histedit_parse_list(histedit_cmds, f, repo);
6177 done:
6178 if (f && fclose(f) != 0 && err == NULL)
6179 err = got_error_from_errno("fclose");
6180 return err;
6183 static const struct got_error *
6184 histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
6185 const struct got_error *edit_err, struct got_object_id_queue *commits,
6186 const char *path, struct got_repository *repo)
6188 const struct got_error *err = NULL, *prev_err = edit_err;
6189 int resp = ' ';
6191 while (resp != 'c' && resp != 'r' && resp != 'a') {
6192 printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
6193 "or (a)bort: ", getprogname(), prev_err->msg);
6194 resp = getchar();
6195 if (resp == '\n')
6196 resp = getchar();
6197 if (resp == 'c') {
6198 histedit_free_list(histedit_cmds);
6199 err = histedit_run_editor(histedit_cmds, path, commits,
6200 repo);
6201 if (err) {
6202 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6203 err->code != GOT_ERR_HISTEDIT_CMD)
6204 break;
6205 prev_err = err;
6206 resp = ' ';
6207 continue;
6209 break;
6210 } else if (resp == 'r') {
6211 histedit_free_list(histedit_cmds);
6212 err = histedit_edit_script(histedit_cmds,
6213 commits, repo);
6214 if (err) {
6215 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6216 err->code != GOT_ERR_HISTEDIT_CMD)
6217 break;
6218 prev_err = err;
6219 resp = ' ';
6220 continue;
6222 break;
6223 } else if (resp == 'a') {
6224 err = got_error(GOT_ERR_HISTEDIT_CANCEL);
6225 break;
6226 } else
6227 printf("invalid response '%c'\n", resp);
6230 return err;
6233 static const struct got_error *
6234 histedit_complete(struct got_worktree *worktree,
6235 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6236 struct got_reference *branch, struct got_repository *repo)
6238 printf("Switching work tree to %s\n",
6239 got_ref_get_symref_target(branch));
6240 return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
6241 branch, repo);
6244 static const struct got_error *
6245 show_histedit_progress(struct got_commit_object *commit,
6246 struct got_histedit_list_entry *hle, struct got_object_id *new_id)
6248 const struct got_error *err;
6249 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
6251 err = got_object_id_str(&old_id_str, hle->commit_id);
6252 if (err)
6253 goto done;
6255 if (new_id) {
6256 err = got_object_id_str(&new_id_str, new_id);
6257 if (err)
6258 goto done;
6261 old_id_str[12] = '\0';
6262 if (new_id_str)
6263 new_id_str[12] = '\0';
6265 if (hle->logmsg) {
6266 logmsg = strdup(hle->logmsg);
6267 if (logmsg == NULL) {
6268 err = got_error_from_errno("strdup");
6269 goto done;
6271 trim_logmsg(logmsg, 42);
6272 } else {
6273 err = get_short_logmsg(&logmsg, 42, commit);
6274 if (err)
6275 goto done;
6278 switch (hle->cmd->code) {
6279 case GOT_HISTEDIT_PICK:
6280 case GOT_HISTEDIT_EDIT:
6281 printf("%s -> %s: %s\n", old_id_str,
6282 new_id_str ? new_id_str : "no-op change", logmsg);
6283 break;
6284 case GOT_HISTEDIT_DROP:
6285 case GOT_HISTEDIT_FOLD:
6286 printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
6287 logmsg);
6288 break;
6289 default:
6290 break;
6293 done:
6294 free(old_id_str);
6295 free(new_id_str);
6296 return err;
6299 static const struct got_error *
6300 histedit_commit(struct got_pathlist_head *merged_paths,
6301 struct got_worktree *worktree, struct got_fileindex *fileindex,
6302 struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
6303 struct got_repository *repo)
6305 const struct got_error *err;
6306 struct got_commit_object *commit;
6307 struct got_object_id *new_commit_id;
6309 if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
6310 && hle->logmsg == NULL) {
6311 err = histedit_edit_logmsg(hle, repo);
6312 if (err)
6313 return err;
6316 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
6317 if (err)
6318 return err;
6320 err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
6321 worktree, fileindex, tmp_branch, commit, hle->commit_id,
6322 hle->logmsg, repo);
6323 if (err) {
6324 if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
6325 goto done;
6326 err = show_histedit_progress(commit, hle, NULL);
6327 } else {
6328 err = show_histedit_progress(commit, hle, new_commit_id);
6329 free(new_commit_id);
6331 done:
6332 got_object_commit_close(commit);
6333 return err;
6336 static const struct got_error *
6337 histedit_skip_commit(struct got_histedit_list_entry *hle,
6338 struct got_worktree *worktree, struct got_repository *repo)
6340 const struct got_error *error;
6341 struct got_commit_object *commit;
6343 error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
6344 repo);
6345 if (error)
6346 return error;
6348 error = got_object_open_as_commit(&commit, repo, hle->commit_id);
6349 if (error)
6350 return error;
6352 error = show_histedit_progress(commit, hle, NULL);
6353 got_object_commit_close(commit);
6354 return error;
6357 static const struct got_error *
6358 cmd_histedit(int argc, char *argv[])
6360 const struct got_error *error = NULL;
6361 struct got_worktree *worktree = NULL;
6362 struct got_fileindex *fileindex = NULL;
6363 struct got_repository *repo = NULL;
6364 char *cwd = NULL;
6365 struct got_reference *branch = NULL;
6366 struct got_reference *tmp_branch = NULL;
6367 struct got_object_id *resume_commit_id = NULL;
6368 struct got_object_id *base_commit_id = NULL;
6369 struct got_object_id *head_commit_id = NULL;
6370 struct got_commit_object *commit = NULL;
6371 int ch, rebase_in_progress = 0, did_something;
6372 int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
6373 const char *edit_script_path = NULL;
6374 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
6375 struct got_object_id_queue commits;
6376 struct got_pathlist_head merged_paths;
6377 const struct got_object_id_queue *parent_ids;
6378 struct got_object_qid *pid;
6379 struct got_histedit_list histedit_cmds;
6380 struct got_histedit_list_entry *hle;
6382 SIMPLEQ_INIT(&commits);
6383 TAILQ_INIT(&histedit_cmds);
6384 TAILQ_INIT(&merged_paths);
6386 while ((ch = getopt(argc, argv, "acF:")) != -1) {
6387 switch (ch) {
6388 case 'a':
6389 abort_edit = 1;
6390 break;
6391 case 'c':
6392 continue_edit = 1;
6393 break;
6394 case 'F':
6395 edit_script_path = optarg;
6396 break;
6397 default:
6398 usage_histedit();
6399 /* NOTREACHED */
6403 argc -= optind;
6404 argv += optind;
6406 #ifndef PROFILE
6407 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6408 "unveil", NULL) == -1)
6409 err(1, "pledge");
6410 #endif
6411 if (abort_edit && continue_edit)
6412 usage_histedit();
6413 if (argc != 0)
6414 usage_histedit();
6417 * This command cannot apply unveil(2) in all cases because the
6418 * user may choose to run an editor to edit the histedit script
6419 * and to edit individual commit log messages.
6420 * unveil(2) traverses exec(2); if an editor is used we have to
6421 * apply unveil after edit script and log messages have been written.
6422 * XXX TODO: Make use of unveil(2) where possible.
6425 cwd = getcwd(NULL, 0);
6426 if (cwd == NULL) {
6427 error = got_error_from_errno("getcwd");
6428 goto done;
6430 error = got_worktree_open(&worktree, cwd);
6431 if (error)
6432 goto done;
6434 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6435 NULL);
6436 if (error != NULL)
6437 goto done;
6439 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
6440 if (error)
6441 goto done;
6442 if (rebase_in_progress) {
6443 error = got_error(GOT_ERR_REBASING);
6444 goto done;
6447 error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
6448 if (error)
6449 goto done;
6451 if (edit_in_progress && abort_edit) {
6452 error = got_worktree_histedit_continue(&resume_commit_id,
6453 &tmp_branch, &branch, &base_commit_id, &fileindex,
6454 worktree, repo);
6455 if (error)
6456 goto done;
6457 printf("Switching work tree to %s\n",
6458 got_ref_get_symref_target(branch));
6459 error = got_worktree_histedit_abort(worktree, fileindex, repo,
6460 branch, base_commit_id, update_progress, &did_something);
6461 if (error)
6462 goto done;
6463 printf("Histedit of %s aborted\n",
6464 got_ref_get_symref_target(branch));
6465 goto done; /* nothing else to do */
6466 } else if (abort_edit) {
6467 error = got_error(GOT_ERR_NOT_HISTEDIT);
6468 goto done;
6471 if (continue_edit) {
6472 char *path;
6474 if (!edit_in_progress) {
6475 error = got_error(GOT_ERR_NOT_HISTEDIT);
6476 goto done;
6479 error = got_worktree_get_histedit_script_path(&path, worktree);
6480 if (error)
6481 goto done;
6483 error = histedit_load_list(&histedit_cmds, path, repo);
6484 free(path);
6485 if (error)
6486 goto done;
6488 error = got_worktree_histedit_continue(&resume_commit_id,
6489 &tmp_branch, &branch, &base_commit_id, &fileindex,
6490 worktree, repo);
6491 if (error)
6492 goto done;
6494 error = got_ref_resolve(&head_commit_id, repo, branch);
6495 if (error)
6496 goto done;
6498 error = got_object_open_as_commit(&commit, repo,
6499 head_commit_id);
6500 if (error)
6501 goto done;
6502 parent_ids = got_object_commit_get_parent_ids(commit);
6503 pid = SIMPLEQ_FIRST(parent_ids);
6504 if (pid == NULL) {
6505 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
6506 goto done;
6508 error = collect_commits(&commits, head_commit_id, pid->id,
6509 base_commit_id, got_worktree_get_path_prefix(worktree),
6510 GOT_ERR_HISTEDIT_PATH, repo);
6511 got_object_commit_close(commit);
6512 commit = NULL;
6513 if (error)
6514 goto done;
6515 } else {
6516 if (edit_in_progress) {
6517 error = got_error(GOT_ERR_HISTEDIT_BUSY);
6518 goto done;
6521 error = got_ref_open(&branch, repo,
6522 got_worktree_get_head_ref_name(worktree), 0);
6523 if (error != NULL)
6524 goto done;
6526 if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
6527 error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
6528 "will not edit commit history of a branch outside "
6529 "the \"refs/heads/\" reference namespace");
6530 goto done;
6533 error = got_ref_resolve(&head_commit_id, repo, branch);
6534 got_ref_close(branch);
6535 branch = NULL;
6536 if (error)
6537 goto done;
6539 error = got_object_open_as_commit(&commit, repo,
6540 head_commit_id);
6541 if (error)
6542 goto done;
6543 parent_ids = got_object_commit_get_parent_ids(commit);
6544 pid = SIMPLEQ_FIRST(parent_ids);
6545 if (pid == NULL) {
6546 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
6547 goto done;
6549 error = collect_commits(&commits, head_commit_id, pid->id,
6550 got_worktree_get_base_commit_id(worktree),
6551 got_worktree_get_path_prefix(worktree),
6552 GOT_ERR_HISTEDIT_PATH, repo);
6553 got_object_commit_close(commit);
6554 commit = NULL;
6555 if (error)
6556 goto done;
6558 error = got_worktree_histedit_prepare(&tmp_branch, &branch,
6559 &base_commit_id, &fileindex, worktree, repo);
6560 if (error)
6561 goto done;
6563 if (edit_script_path) {
6564 error = histedit_load_list(&histedit_cmds,
6565 edit_script_path, repo);
6566 if (error) {
6567 got_worktree_histedit_abort(worktree, fileindex,
6568 repo, branch, base_commit_id,
6569 update_progress, &did_something);
6570 goto done;
6572 } else {
6573 error = histedit_edit_script(&histedit_cmds, &commits,
6574 repo);
6575 if (error) {
6576 got_worktree_histedit_abort(worktree, fileindex,
6577 repo, branch, base_commit_id,
6578 update_progress, &did_something);
6579 goto done;
6584 error = histedit_save_list(&histedit_cmds, worktree,
6585 repo);
6586 if (error) {
6587 got_worktree_histedit_abort(worktree, fileindex,
6588 repo, branch, base_commit_id,
6589 update_progress, &did_something);
6590 goto done;
6595 error = histedit_check_script(&histedit_cmds, &commits, repo);
6596 if (error)
6597 goto done;
6599 TAILQ_FOREACH(hle, &histedit_cmds, entry) {
6600 if (resume_commit_id) {
6601 if (got_object_id_cmp(hle->commit_id,
6602 resume_commit_id) != 0)
6603 continue;
6605 resume_commit_id = NULL;
6606 if (hle->cmd->code == GOT_HISTEDIT_DROP ||
6607 hle->cmd->code == GOT_HISTEDIT_FOLD) {
6608 error = histedit_skip_commit(hle, worktree,
6609 repo);
6610 } else {
6611 error = histedit_commit(NULL, worktree,
6612 fileindex, tmp_branch, hle, repo);
6614 if (error)
6615 goto done;
6616 continue;
6619 if (hle->cmd->code == GOT_HISTEDIT_DROP) {
6620 error = histedit_skip_commit(hle, worktree, repo);
6621 if (error)
6622 goto done;
6623 continue;
6626 error = got_object_open_as_commit(&commit, repo,
6627 hle->commit_id);
6628 if (error)
6629 goto done;
6630 parent_ids = got_object_commit_get_parent_ids(commit);
6631 pid = SIMPLEQ_FIRST(parent_ids);
6633 error = got_worktree_histedit_merge_files(&merged_paths,
6634 worktree, fileindex, pid->id, hle->commit_id, repo,
6635 rebase_progress, &rebase_status, check_cancelled, NULL);
6636 if (error)
6637 goto done;
6638 got_object_commit_close(commit);
6639 commit = NULL;
6641 if (rebase_status == GOT_STATUS_CONFLICT) {
6642 got_worktree_rebase_pathlist_free(&merged_paths);
6643 break;
6646 if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
6647 char *id_str;
6648 error = got_object_id_str(&id_str, hle->commit_id);
6649 if (error)
6650 goto done;
6651 printf("Stopping histedit for amending commit %s\n",
6652 id_str);
6653 free(id_str);
6654 got_worktree_rebase_pathlist_free(&merged_paths);
6655 error = got_worktree_histedit_postpone(worktree,
6656 fileindex);
6657 goto done;
6660 if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
6661 error = histedit_skip_commit(hle, worktree, repo);
6662 if (error)
6663 goto done;
6664 continue;
6667 error = histedit_commit(&merged_paths, worktree, fileindex,
6668 tmp_branch, hle, repo);
6669 got_worktree_rebase_pathlist_free(&merged_paths);
6670 if (error)
6671 goto done;
6674 if (rebase_status == GOT_STATUS_CONFLICT) {
6675 error = got_worktree_histedit_postpone(worktree, fileindex);
6676 if (error)
6677 goto done;
6678 error = got_error_msg(GOT_ERR_CONFLICTS,
6679 "conflicts must be resolved before rebasing can continue");
6680 } else
6681 error = histedit_complete(worktree, fileindex, tmp_branch,
6682 branch, repo);
6683 done:
6684 got_object_id_queue_free(&commits);
6685 histedit_free_list(&histedit_cmds);
6686 free(head_commit_id);
6687 free(base_commit_id);
6688 free(resume_commit_id);
6689 if (commit)
6690 got_object_commit_close(commit);
6691 if (branch)
6692 got_ref_close(branch);
6693 if (tmp_branch)
6694 got_ref_close(tmp_branch);
6695 if (worktree)
6696 got_worktree_close(worktree);
6697 if (repo)
6698 got_repo_close(repo);
6699 return error;
6702 __dead static void
6703 usage_integrate(void)
6705 fprintf(stderr, "usage: %s integrate branch\n", getprogname());
6706 exit(1);
6709 static const struct got_error *
6710 cmd_integrate(int argc, char *argv[])
6712 const struct got_error *error = NULL;
6713 struct got_repository *repo = NULL;
6714 struct got_worktree *worktree = NULL;
6715 char *cwd = NULL, *refname = NULL, *base_refname = NULL;
6716 const char *branch_arg = NULL;
6717 struct got_reference *branch_ref = NULL, *base_branch_ref = NULL;
6718 struct got_fileindex *fileindex = NULL;
6719 struct got_object_id *commit_id = NULL, *base_commit_id = NULL;
6720 int ch, did_something = 0;
6722 while ((ch = getopt(argc, argv, "")) != -1) {
6723 switch (ch) {
6724 default:
6725 usage_integrate();
6726 /* NOTREACHED */
6730 argc -= optind;
6731 argv += optind;
6733 if (argc != 1)
6734 usage_integrate();
6735 branch_arg = argv[0];
6737 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6738 "unveil", NULL) == -1)
6739 err(1, "pledge");
6741 cwd = getcwd(NULL, 0);
6742 if (cwd == NULL) {
6743 error = got_error_from_errno("getcwd");
6744 goto done;
6747 error = got_worktree_open(&worktree, cwd);
6748 if (error)
6749 goto done;
6751 error = check_rebase_or_histedit_in_progress(worktree);
6752 if (error)
6753 goto done;
6755 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6756 NULL);
6757 if (error != NULL)
6758 goto done;
6760 error = apply_unveil(got_repo_get_path(repo), 0,
6761 got_worktree_get_root_path(worktree));
6762 if (error)
6763 goto done;
6765 if (asprintf(&refname, "refs/heads/%s", branch_arg) == -1) {
6766 error = got_error_from_errno("asprintf");
6767 goto done;
6770 error = got_worktree_integrate_prepare(&fileindex, &branch_ref,
6771 &base_branch_ref, worktree, refname, repo);
6772 if (error)
6773 goto done;
6775 refname = strdup(got_ref_get_name(branch_ref));
6776 if (refname == NULL) {
6777 error = got_error_from_errno("strdup");
6778 got_worktree_integrate_abort(worktree, fileindex, repo,
6779 branch_ref, base_branch_ref);
6780 goto done;
6782 base_refname = strdup(got_ref_get_name(base_branch_ref));
6783 if (base_refname == NULL) {
6784 error = got_error_from_errno("strdup");
6785 got_worktree_integrate_abort(worktree, fileindex, repo,
6786 branch_ref, base_branch_ref);
6787 goto done;
6790 error = got_ref_resolve(&commit_id, repo, branch_ref);
6791 if (error)
6792 goto done;
6794 error = got_ref_resolve(&base_commit_id, repo, base_branch_ref);
6795 if (error)
6796 goto done;
6798 if (got_object_id_cmp(commit_id, base_commit_id) == 0) {
6799 error = got_error_msg(GOT_ERR_SAME_BRANCH,
6800 "specified branch has already been integrated");
6801 got_worktree_integrate_abort(worktree, fileindex, repo,
6802 branch_ref, base_branch_ref);
6803 goto done;
6806 error = check_linear_ancestry(commit_id, base_commit_id, 1, repo);
6807 if (error) {
6808 if (error->code == GOT_ERR_ANCESTRY)
6809 error = got_error(GOT_ERR_REBASE_REQUIRED);
6810 got_worktree_integrate_abort(worktree, fileindex, repo,
6811 branch_ref, base_branch_ref);
6812 goto done;
6815 error = got_worktree_integrate_continue(worktree, fileindex, repo,
6816 branch_ref, base_branch_ref, update_progress, &did_something,
6817 check_cancelled, NULL);
6818 if (error)
6819 goto done;
6821 printf("Integrated %s into %s\n", refname, base_refname);
6822 done:
6823 if (repo)
6824 got_repo_close(repo);
6825 if (worktree)
6826 got_worktree_close(worktree);
6827 free(cwd);
6828 free(base_commit_id);
6829 free(commit_id);
6830 free(refname);
6831 free(base_refname);
6832 return error;
6835 __dead static void
6836 usage_stage(void)
6838 fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
6839 "[file-path ...]\n",
6840 getprogname());
6841 exit(1);
6844 static const struct got_error *
6845 print_stage(void *arg, unsigned char status, unsigned char staged_status,
6846 const char *path, struct got_object_id *blob_id,
6847 struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
6849 const struct got_error *err = NULL;
6850 char *id_str = NULL;
6852 if (staged_status != GOT_STATUS_ADD &&
6853 staged_status != GOT_STATUS_MODIFY &&
6854 staged_status != GOT_STATUS_DELETE)
6855 return NULL;
6857 if (staged_status == GOT_STATUS_ADD ||
6858 staged_status == GOT_STATUS_MODIFY)
6859 err = got_object_id_str(&id_str, staged_blob_id);
6860 else
6861 err = got_object_id_str(&id_str, blob_id);
6862 if (err)
6863 return err;
6865 printf("%s %c %s\n", id_str, staged_status, path);
6866 free(id_str);
6867 return NULL;
6870 static const struct got_error *
6871 cmd_stage(int argc, char *argv[])
6873 const struct got_error *error = NULL;
6874 struct got_repository *repo = NULL;
6875 struct got_worktree *worktree = NULL;
6876 char *cwd = NULL;
6877 struct got_pathlist_head paths;
6878 struct got_pathlist_entry *pe;
6879 int ch, list_stage = 0, pflag = 0;
6880 FILE *patch_script_file = NULL;
6881 const char *patch_script_path = NULL;
6882 struct choose_patch_arg cpa;
6884 TAILQ_INIT(&paths);
6886 while ((ch = getopt(argc, argv, "lpF:")) != -1) {
6887 switch (ch) {
6888 case 'l':
6889 list_stage = 1;
6890 break;
6891 case 'p':
6892 pflag = 1;
6893 break;
6894 case 'F':
6895 patch_script_path = optarg;
6896 break;
6897 default:
6898 usage_stage();
6899 /* NOTREACHED */
6903 argc -= optind;
6904 argv += optind;
6906 #ifndef PROFILE
6907 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6908 "unveil", NULL) == -1)
6909 err(1, "pledge");
6910 #endif
6911 if (list_stage && (pflag || patch_script_path))
6912 errx(1, "-l option cannot be used with other options");
6913 if (patch_script_path && !pflag)
6914 errx(1, "-F option can only be used together with -p option");
6916 cwd = getcwd(NULL, 0);
6917 if (cwd == NULL) {
6918 error = got_error_from_errno("getcwd");
6919 goto done;
6922 error = got_worktree_open(&worktree, cwd);
6923 if (error)
6924 goto done;
6926 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6927 NULL);
6928 if (error != NULL)
6929 goto done;
6931 if (patch_script_path) {
6932 patch_script_file = fopen(patch_script_path, "r");
6933 if (patch_script_file == NULL) {
6934 error = got_error_from_errno2("fopen",
6935 patch_script_path);
6936 goto done;
6939 error = apply_unveil(got_repo_get_path(repo), 0,
6940 got_worktree_get_root_path(worktree));
6941 if (error)
6942 goto done;
6944 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6945 if (error)
6946 goto done;
6948 if (list_stage)
6949 error = got_worktree_status(worktree, &paths, repo,
6950 print_stage, NULL, check_cancelled, NULL);
6951 else {
6952 cpa.patch_script_file = patch_script_file;
6953 cpa.action = "stage";
6954 error = got_worktree_stage(worktree, &paths,
6955 pflag ? NULL : print_status, NULL,
6956 pflag ? choose_patch : NULL, &cpa, repo);
6958 done:
6959 if (patch_script_file && fclose(patch_script_file) == EOF &&
6960 error == NULL)
6961 error = got_error_from_errno2("fclose", patch_script_path);
6962 if (repo)
6963 got_repo_close(repo);
6964 if (worktree)
6965 got_worktree_close(worktree);
6966 TAILQ_FOREACH(pe, &paths, entry)
6967 free((char *)pe->path);
6968 got_pathlist_free(&paths);
6969 free(cwd);
6970 return error;
6973 __dead static void
6974 usage_unstage(void)
6976 fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
6977 "[file-path ...]\n",
6978 getprogname());
6979 exit(1);
6983 static const struct got_error *
6984 cmd_unstage(int argc, char *argv[])
6986 const struct got_error *error = NULL;
6987 struct got_repository *repo = NULL;
6988 struct got_worktree *worktree = NULL;
6989 char *cwd = NULL;
6990 struct got_pathlist_head paths;
6991 struct got_pathlist_entry *pe;
6992 int ch, did_something = 0, pflag = 0;
6993 FILE *patch_script_file = NULL;
6994 const char *patch_script_path = NULL;
6995 struct choose_patch_arg cpa;
6997 TAILQ_INIT(&paths);
6999 while ((ch = getopt(argc, argv, "pF:")) != -1) {
7000 switch (ch) {
7001 case 'p':
7002 pflag = 1;
7003 break;
7004 case 'F':
7005 patch_script_path = optarg;
7006 break;
7007 default:
7008 usage_unstage();
7009 /* NOTREACHED */
7013 argc -= optind;
7014 argv += optind;
7016 #ifndef PROFILE
7017 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7018 "unveil", NULL) == -1)
7019 err(1, "pledge");
7020 #endif
7021 if (patch_script_path && !pflag)
7022 errx(1, "-F option can only be used together with -p option");
7024 cwd = getcwd(NULL, 0);
7025 if (cwd == NULL) {
7026 error = got_error_from_errno("getcwd");
7027 goto done;
7030 error = got_worktree_open(&worktree, cwd);
7031 if (error)
7032 goto done;
7034 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7035 NULL);
7036 if (error != NULL)
7037 goto done;
7039 if (patch_script_path) {
7040 patch_script_file = fopen(patch_script_path, "r");
7041 if (patch_script_file == NULL) {
7042 error = got_error_from_errno2("fopen",
7043 patch_script_path);
7044 goto done;
7048 error = apply_unveil(got_repo_get_path(repo), 0,
7049 got_worktree_get_root_path(worktree));
7050 if (error)
7051 goto done;
7053 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7054 if (error)
7055 goto done;
7057 cpa.patch_script_file = patch_script_file;
7058 cpa.action = "unstage";
7059 error = got_worktree_unstage(worktree, &paths, update_progress,
7060 &did_something, pflag ? choose_patch : NULL, &cpa, repo);
7061 done:
7062 if (patch_script_file && fclose(patch_script_file) == EOF &&
7063 error == NULL)
7064 error = got_error_from_errno2("fclose", patch_script_path);
7065 if (repo)
7066 got_repo_close(repo);
7067 if (worktree)
7068 got_worktree_close(worktree);
7069 TAILQ_FOREACH(pe, &paths, entry)
7070 free((char *)pe->path);
7071 got_pathlist_free(&paths);
7072 free(cwd);
7073 return error;
7076 __dead static void
7077 usage_cat(void)
7079 fprintf(stderr, "usage: %s cat [-r repository ] [ -c commit ] [ -P ] "
7080 "arg1 [arg2 ...]\n", getprogname());
7081 exit(1);
7084 static const struct got_error *
7085 cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7087 const struct got_error *err;
7088 struct got_blob_object *blob;
7090 err = got_object_open_as_blob(&blob, repo, id, 8192);
7091 if (err)
7092 return err;
7094 err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
7095 got_object_blob_close(blob);
7096 return err;
7099 static const struct got_error *
7100 cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7102 const struct got_error *err;
7103 struct got_tree_object *tree;
7104 int nentries, i;
7106 err = got_object_open_as_tree(&tree, repo, id);
7107 if (err)
7108 return err;
7110 nentries = got_object_tree_get_nentries(tree);
7111 for (i = 0; i < nentries; i++) {
7112 struct got_tree_entry *te;
7113 char *id_str;
7114 if (sigint_received || sigpipe_received)
7115 break;
7116 te = got_object_tree_get_entry(tree, i);
7117 err = got_object_id_str(&id_str, got_tree_entry_get_id(te));
7118 if (err)
7119 break;
7120 fprintf(outfile, "%s %.7o %s\n", id_str,
7121 got_tree_entry_get_mode(te),
7122 got_tree_entry_get_name(te));
7123 free(id_str);
7126 got_object_tree_close(tree);
7127 return err;
7130 static const struct got_error *
7131 cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7133 const struct got_error *err;
7134 struct got_commit_object *commit;
7135 const struct got_object_id_queue *parent_ids;
7136 struct got_object_qid *pid;
7137 char *id_str = NULL;
7138 const char *logmsg = NULL;
7140 err = got_object_open_as_commit(&commit, repo, id);
7141 if (err)
7142 return err;
7144 err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
7145 if (err)
7146 goto done;
7148 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str);
7149 parent_ids = got_object_commit_get_parent_ids(commit);
7150 fprintf(outfile, "numparents %d\n",
7151 got_object_commit_get_nparents(commit));
7152 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
7153 char *pid_str;
7154 err = got_object_id_str(&pid_str, pid->id);
7155 if (err)
7156 goto done;
7157 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_PARENT, pid_str);
7158 free(pid_str);
7160 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_AUTHOR,
7161 got_object_commit_get_author(commit),
7162 got_object_commit_get_author_time(commit));
7164 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_COMMITTER,
7165 got_object_commit_get_author(commit),
7166 got_object_commit_get_committer_time(commit));
7168 logmsg = got_object_commit_get_logmsg_raw(commit);
7169 fprintf(outfile, "messagelen %zd\n", strlen(logmsg));
7170 fprintf(outfile, "%s", logmsg);
7171 done:
7172 free(id_str);
7173 got_object_commit_close(commit);
7174 return err;
7177 static const struct got_error *
7178 cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7180 const struct got_error *err;
7181 struct got_tag_object *tag;
7182 char *id_str = NULL;
7183 const char *tagmsg = NULL;
7185 err = got_object_open_as_tag(&tag, repo, id);
7186 if (err)
7187 return err;
7189 err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
7190 if (err)
7191 goto done;
7193 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str);
7195 switch (got_object_tag_get_object_type(tag)) {
7196 case GOT_OBJ_TYPE_BLOB:
7197 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7198 GOT_OBJ_LABEL_BLOB);
7199 break;
7200 case GOT_OBJ_TYPE_TREE:
7201 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7202 GOT_OBJ_LABEL_TREE);
7203 break;
7204 case GOT_OBJ_TYPE_COMMIT:
7205 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7206 GOT_OBJ_LABEL_COMMIT);
7207 break;
7208 case GOT_OBJ_TYPE_TAG:
7209 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7210 GOT_OBJ_LABEL_TAG);
7211 break;
7212 default:
7213 break;
7216 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TAG,
7217 got_object_tag_get_name(tag));
7219 fprintf(outfile, "%s%s %lld +0000\n", GOT_TAG_LABEL_TAGGER,
7220 got_object_tag_get_tagger(tag),
7221 got_object_tag_get_tagger_time(tag));
7223 tagmsg = got_object_tag_get_message(tag);
7224 fprintf(outfile, "messagelen %zd\n", strlen(tagmsg));
7225 fprintf(outfile, "%s", tagmsg);
7226 done:
7227 free(id_str);
7228 got_object_tag_close(tag);
7229 return err;
7232 static const struct got_error *
7233 cmd_cat(int argc, char *argv[])
7235 const struct got_error *error;
7236 struct got_repository *repo = NULL;
7237 struct got_worktree *worktree = NULL;
7238 char *cwd = NULL, *repo_path = NULL, *label = NULL;
7239 const char *commit_id_str = NULL;
7240 struct got_object_id *id = NULL, *commit_id = NULL;
7241 int ch, obj_type, i, force_path = 0;
7243 #ifndef PROFILE
7244 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
7245 NULL) == -1)
7246 err(1, "pledge");
7247 #endif
7249 while ((ch = getopt(argc, argv, "c:r:P")) != -1) {
7250 switch (ch) {
7251 case 'c':
7252 commit_id_str = optarg;
7253 break;
7254 case 'r':
7255 repo_path = realpath(optarg, NULL);
7256 if (repo_path == NULL)
7257 return got_error_from_errno2("realpath",
7258 optarg);
7259 got_path_strip_trailing_slashes(repo_path);
7260 break;
7261 case 'P':
7262 force_path = 1;
7263 break;
7264 default:
7265 usage_cat();
7266 /* NOTREACHED */
7270 argc -= optind;
7271 argv += optind;
7273 cwd = getcwd(NULL, 0);
7274 if (cwd == NULL) {
7275 error = got_error_from_errno("getcwd");
7276 goto done;
7278 error = got_worktree_open(&worktree, cwd);
7279 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7280 goto done;
7281 if (worktree) {
7282 if (repo_path == NULL) {
7283 repo_path = strdup(
7284 got_worktree_get_repo_path(worktree));
7285 if (repo_path == NULL) {
7286 error = got_error_from_errno("strdup");
7287 goto done;
7292 if (repo_path == NULL) {
7293 repo_path = getcwd(NULL, 0);
7294 if (repo_path == NULL)
7295 return got_error_from_errno("getcwd");
7298 error = got_repo_open(&repo, repo_path, NULL);
7299 free(repo_path);
7300 if (error != NULL)
7301 goto done;
7303 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
7304 if (error)
7305 goto done;
7307 if (commit_id_str == NULL)
7308 commit_id_str = GOT_REF_HEAD;
7309 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
7310 if (error)
7311 goto done;
7313 for (i = 0; i < argc; i++) {
7314 if (force_path) {
7315 error = got_object_id_by_path(&id, repo, commit_id,
7316 argv[i]);
7317 if (error)
7318 break;
7319 } else {
7320 error = match_object_id(&id, &label, argv[i],
7321 GOT_OBJ_TYPE_ANY, 0, repo);
7322 if (error) {
7323 if (error->code != GOT_ERR_BAD_OBJ_ID_STR &&
7324 error->code != GOT_ERR_NOT_REF)
7325 break;
7326 error = got_object_id_by_path(&id, repo,
7327 commit_id, argv[i]);
7328 if (error)
7329 break;
7333 error = got_object_get_type(&obj_type, repo, id);
7334 if (error)
7335 break;
7337 switch (obj_type) {
7338 case GOT_OBJ_TYPE_BLOB:
7339 error = cat_blob(id, repo, stdout);
7340 break;
7341 case GOT_OBJ_TYPE_TREE:
7342 error = cat_tree(id, repo, stdout);
7343 break;
7344 case GOT_OBJ_TYPE_COMMIT:
7345 error = cat_commit(id, repo, stdout);
7346 break;
7347 case GOT_OBJ_TYPE_TAG:
7348 error = cat_tag(id, repo, stdout);
7349 break;
7350 default:
7351 error = got_error(GOT_ERR_OBJ_TYPE);
7352 break;
7354 if (error)
7355 break;
7356 free(label);
7357 label = NULL;
7358 free(id);
7359 id = NULL;
7362 done:
7363 free(label);
7364 free(id);
7365 free(commit_id);
7366 if (worktree)
7367 got_worktree_close(worktree);
7368 if (repo) {
7369 const struct got_error *repo_error;
7370 repo_error = got_repo_close(repo);
7371 if (error == NULL)
7372 error = repo_error;
7374 return error;