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 char *path, int show_patch, char *search_pattern, int diff_context,
1736 int limit, int first_parent_traversal, struct got_reflist_head *refs)
1738 const struct got_error *err;
1739 struct got_commit_graph *graph;
1740 regex_t regex;
1741 int have_match;
1743 if (search_pattern &&
1744 regcomp(&regex, search_pattern, REG_EXTENDED | REG_NOSUB | REG_NEWLINE))
1745 return got_error_msg(GOT_ERR_REGEX, search_pattern);
1747 err = got_commit_graph_open(&graph, root_id, path,
1748 first_parent_traversal, repo);
1749 if (err)
1750 return err;
1751 err = got_commit_graph_iter_start(graph, root_id, repo,
1752 check_cancelled, NULL);
1753 if (err)
1754 goto done;
1755 for (;;) {
1756 struct got_commit_object *commit;
1757 struct got_object_id *id;
1759 if (sigint_received || sigpipe_received)
1760 break;
1762 err = got_commit_graph_iter_next(&id, graph);
1763 if (err) {
1764 if (err->code == GOT_ERR_ITER_COMPLETED) {
1765 err = NULL;
1766 break;
1768 if (err->code != GOT_ERR_ITER_NEED_MORE)
1769 break;
1770 err = got_commit_graph_fetch_commits(graph, 1, repo,
1771 check_cancelled, NULL);
1772 if (err)
1773 break;
1774 else
1775 continue;
1777 if (id == NULL)
1778 break;
1780 err = got_object_open_as_commit(&commit, repo, id);
1781 if (err)
1782 break;
1784 if (search_pattern) {
1785 err = match_logmsg(&have_match, id, commit, &regex);
1786 if (err) {
1787 got_object_commit_close(commit);
1788 break;
1790 if (have_match == 0) {
1791 got_object_commit_close(commit);
1792 continue;
1796 err = print_commit(commit, id, repo, path, show_patch,
1797 diff_context, refs);
1798 got_object_commit_close(commit);
1799 if (err || (limit && --limit == 0))
1800 break;
1802 done:
1803 if (search_pattern)
1804 regfree(&regex);
1805 got_commit_graph_close(graph);
1806 return err;
1809 __dead static void
1810 usage_log(void)
1812 fprintf(stderr, "usage: %s log [-c commit] [-C number] [-f] [ -l N ] [-p] "
1813 "[-s search-pattern] [-r repository-path] [path]\n", getprogname());
1814 exit(1);
1817 static int
1818 get_default_log_limit(void)
1820 const char *got_default_log_limit;
1821 long long n;
1822 const char *errstr;
1824 got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
1825 if (got_default_log_limit == NULL)
1826 return 0;
1827 n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
1828 if (errstr != NULL)
1829 return 0;
1830 return n;
1833 static const struct got_error *
1834 cmd_log(int argc, char *argv[])
1836 const struct got_error *error;
1837 struct got_repository *repo = NULL;
1838 struct got_worktree *worktree = NULL;
1839 struct got_commit_object *commit = NULL;
1840 struct got_object_id *id = NULL;
1841 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
1842 char *start_commit = NULL, *search_pattern = NULL;
1843 int diff_context = 3, ch;
1844 int show_patch = 0, limit = 0, first_parent_traversal = 0;
1845 const char *errstr;
1846 struct got_reflist_head refs;
1848 SIMPLEQ_INIT(&refs);
1850 #ifndef PROFILE
1851 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1852 NULL)
1853 == -1)
1854 err(1, "pledge");
1855 #endif
1857 limit = get_default_log_limit();
1859 while ((ch = getopt(argc, argv, "b:pc:C:l:fr:s:")) != -1) {
1860 switch (ch) {
1861 case 'p':
1862 show_patch = 1;
1863 break;
1864 case 'c':
1865 start_commit = optarg;
1866 break;
1867 case 'C':
1868 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
1869 &errstr);
1870 if (errstr != NULL)
1871 err(1, "-C option %s", errstr);
1872 break;
1873 case 'l':
1874 limit = strtonum(optarg, 0, INT_MAX, &errstr);
1875 if (errstr != NULL)
1876 err(1, "-l option %s", errstr);
1877 break;
1878 case 'f':
1879 first_parent_traversal = 1;
1880 break;
1881 case 'r':
1882 repo_path = realpath(optarg, NULL);
1883 if (repo_path == NULL)
1884 return got_error_from_errno2("realpath",
1885 optarg);
1886 got_path_strip_trailing_slashes(repo_path);
1887 break;
1888 case 's':
1889 search_pattern = optarg;
1890 break;
1891 default:
1892 usage_log();
1893 /* NOTREACHED */
1897 argc -= optind;
1898 argv += optind;
1900 cwd = getcwd(NULL, 0);
1901 if (cwd == NULL) {
1902 error = got_error_from_errno("getcwd");
1903 goto done;
1906 error = got_worktree_open(&worktree, cwd);
1907 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1908 goto done;
1909 error = NULL;
1911 if (argc == 0) {
1912 path = strdup("");
1913 if (path == NULL) {
1914 error = got_error_from_errno("strdup");
1915 goto done;
1917 } else if (argc == 1) {
1918 if (worktree) {
1919 error = got_worktree_resolve_path(&path, worktree,
1920 argv[0]);
1921 if (error)
1922 goto done;
1923 } else {
1924 path = strdup(argv[0]);
1925 if (path == NULL) {
1926 error = got_error_from_errno("strdup");
1927 goto done;
1930 } else
1931 usage_log();
1933 if (repo_path == NULL) {
1934 repo_path = worktree ?
1935 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
1937 if (repo_path == NULL) {
1938 error = got_error_from_errno("strdup");
1939 goto done;
1942 error = got_repo_open(&repo, repo_path, NULL);
1943 if (error != NULL)
1944 goto done;
1946 error = apply_unveil(got_repo_get_path(repo), 1,
1947 worktree ? got_worktree_get_root_path(worktree) : NULL);
1948 if (error)
1949 goto done;
1951 if (start_commit == NULL) {
1952 struct got_reference *head_ref;
1953 error = got_ref_open(&head_ref, repo,
1954 worktree ? got_worktree_get_head_ref_name(worktree)
1955 : GOT_REF_HEAD, 0);
1956 if (error != NULL)
1957 return error;
1958 error = got_ref_resolve(&id, repo, head_ref);
1959 got_ref_close(head_ref);
1960 if (error != NULL)
1961 return error;
1962 error = got_object_open_as_commit(&commit, repo, id);
1963 } else {
1964 struct got_reference *ref;
1965 error = got_ref_open(&ref, repo, start_commit, 0);
1966 if (error == NULL) {
1967 int obj_type;
1968 error = got_ref_resolve(&id, repo, ref);
1969 got_ref_close(ref);
1970 if (error != NULL)
1971 goto done;
1972 error = got_object_get_type(&obj_type, repo, id);
1973 if (error != NULL)
1974 goto done;
1975 if (obj_type == GOT_OBJ_TYPE_TAG) {
1976 struct got_tag_object *tag;
1977 error = got_object_open_as_tag(&tag, repo, id);
1978 if (error != NULL)
1979 goto done;
1980 if (got_object_tag_get_object_type(tag) !=
1981 GOT_OBJ_TYPE_COMMIT) {
1982 got_object_tag_close(tag);
1983 error = got_error(GOT_ERR_OBJ_TYPE);
1984 goto done;
1986 free(id);
1987 id = got_object_id_dup(
1988 got_object_tag_get_object_id(tag));
1989 if (id == NULL)
1990 error = got_error_from_errno(
1991 "got_object_id_dup");
1992 got_object_tag_close(tag);
1993 if (error)
1994 goto done;
1995 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
1996 error = got_error(GOT_ERR_OBJ_TYPE);
1997 goto done;
1999 error = got_object_open_as_commit(&commit, repo, id);
2000 if (error != NULL)
2001 goto done;
2003 if (commit == NULL) {
2004 error = got_repo_match_object_id_prefix(&id,
2005 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
2006 if (error != NULL)
2007 return error;
2010 if (error != NULL)
2011 goto done;
2013 if (worktree) {
2014 const char *prefix = got_worktree_get_path_prefix(worktree);
2015 char *p;
2016 if (asprintf(&p, "%s%s%s", prefix,
2017 (strcmp(prefix, "/") != 0) ? "/" : "", path) == -1) {
2018 error = got_error_from_errno("asprintf");
2019 goto done;
2021 error = got_repo_map_path(&in_repo_path, repo, p, 1);
2022 free(p);
2023 } else
2024 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2025 if (error != NULL)
2026 goto done;
2027 if (in_repo_path) {
2028 free(path);
2029 path = in_repo_path;
2032 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
2033 if (error)
2034 goto done;
2036 error = print_commits(id, repo, path, show_patch, search_pattern,
2037 diff_context, limit, first_parent_traversal, &refs);
2038 done:
2039 free(path);
2040 free(repo_path);
2041 free(cwd);
2042 free(id);
2043 if (worktree)
2044 got_worktree_close(worktree);
2045 if (repo) {
2046 const struct got_error *repo_error;
2047 repo_error = got_repo_close(repo);
2048 if (error == NULL)
2049 error = repo_error;
2051 got_ref_list_free(&refs);
2052 return error;
2055 __dead static void
2056 usage_diff(void)
2058 fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] [-s] "
2059 "[-w] [object1 object2 | path]\n", getprogname());
2060 exit(1);
2063 struct print_diff_arg {
2064 struct got_repository *repo;
2065 struct got_worktree *worktree;
2066 int diff_context;
2067 const char *id_str;
2068 int header_shown;
2069 int diff_staged;
2070 int ignore_whitespace;
2073 static const struct got_error *
2074 print_diff(void *arg, unsigned char status, unsigned char staged_status,
2075 const char *path, struct got_object_id *blob_id,
2076 struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
2078 struct print_diff_arg *a = arg;
2079 const struct got_error *err = NULL;
2080 struct got_blob_object *blob1 = NULL;
2081 FILE *f2 = NULL;
2082 char *abspath = NULL, *label1 = NULL;
2083 struct stat sb;
2085 if (a->diff_staged) {
2086 if (staged_status != GOT_STATUS_MODIFY &&
2087 staged_status != GOT_STATUS_ADD &&
2088 staged_status != GOT_STATUS_DELETE)
2089 return NULL;
2090 } else {
2091 if (staged_status == GOT_STATUS_DELETE)
2092 return NULL;
2093 if (status == GOT_STATUS_NONEXISTENT)
2094 return got_error_set_errno(ENOENT, path);
2095 if (status != GOT_STATUS_MODIFY &&
2096 status != GOT_STATUS_ADD &&
2097 status != GOT_STATUS_DELETE &&
2098 status != GOT_STATUS_CONFLICT)
2099 return NULL;
2102 if (!a->header_shown) {
2103 printf("diff %s %s%s\n", a->id_str,
2104 got_worktree_get_root_path(a->worktree),
2105 a->diff_staged ? " (staged changes)" : "");
2106 a->header_shown = 1;
2109 if (a->diff_staged) {
2110 const char *label1 = NULL, *label2 = NULL;
2111 switch (staged_status) {
2112 case GOT_STATUS_MODIFY:
2113 label1 = path;
2114 label2 = path;
2115 break;
2116 case GOT_STATUS_ADD:
2117 label2 = path;
2118 break;
2119 case GOT_STATUS_DELETE:
2120 label1 = path;
2121 break;
2122 default:
2123 return got_error(GOT_ERR_FILE_STATUS);
2125 return got_diff_objects_as_blobs(blob_id, staged_blob_id,
2126 label1, label2, a->diff_context, a->ignore_whitespace,
2127 a->repo, stdout);
2130 if (staged_status == GOT_STATUS_ADD ||
2131 staged_status == GOT_STATUS_MODIFY) {
2132 char *id_str;
2133 err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
2134 8192);
2135 if (err)
2136 goto done;
2137 err = got_object_id_str(&id_str, staged_blob_id);
2138 if (err)
2139 goto done;
2140 if (asprintf(&label1, "%s (staged)", id_str) == -1) {
2141 err = got_error_from_errno("asprintf");
2142 free(id_str);
2143 goto done;
2145 free(id_str);
2146 } else if (status != GOT_STATUS_ADD) {
2147 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
2148 if (err)
2149 goto done;
2152 if (status != GOT_STATUS_DELETE) {
2153 if (asprintf(&abspath, "%s/%s",
2154 got_worktree_get_root_path(a->worktree), path) == -1) {
2155 err = got_error_from_errno("asprintf");
2156 goto done;
2159 f2 = fopen(abspath, "r");
2160 if (f2 == NULL) {
2161 err = got_error_from_errno2("fopen", abspath);
2162 goto done;
2164 if (lstat(abspath, &sb) == -1) {
2165 err = got_error_from_errno2("lstat", abspath);
2166 goto done;
2168 } else
2169 sb.st_size = 0;
2171 err = got_diff_blob_file(blob1, label1, f2, sb.st_size, path,
2172 a->diff_context, a->ignore_whitespace, stdout);
2173 done:
2174 if (blob1)
2175 got_object_blob_close(blob1);
2176 if (f2 && fclose(f2) != 0 && err == NULL)
2177 err = got_error_from_errno("fclose");
2178 free(abspath);
2179 return err;
2182 static const struct got_error *
2183 match_object_id(struct got_object_id **id, char **label,
2184 const char *id_str, int obj_type, int resolve_tags,
2185 struct got_repository *repo)
2187 const struct got_error *err;
2188 struct got_tag_object *tag;
2189 struct got_reference *ref = NULL;
2191 *id = NULL;
2192 *label = NULL;
2194 if (resolve_tags) {
2195 err = got_repo_object_match_tag(&tag, id_str, GOT_OBJ_TYPE_ANY,
2196 repo);
2197 if (err == NULL) {
2198 *id = got_object_id_dup(
2199 got_object_tag_get_object_id(tag));
2200 if (*id == NULL)
2201 err = got_error_from_errno("got_object_id_dup");
2202 else if (asprintf(label, "refs/tags/%s",
2203 got_object_tag_get_name(tag)) == -1) {
2204 err = got_error_from_errno("asprintf");
2205 free(*id);
2206 *id = NULL;
2208 got_object_tag_close(tag);
2209 return err;
2210 } else if (err->code != GOT_ERR_NO_OBJ)
2211 return err;
2214 err = got_repo_match_object_id_prefix(id, id_str, obj_type, repo);
2215 if (err) {
2216 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
2217 return err;
2218 err = got_ref_open(&ref, repo, id_str, 0);
2219 if (err != NULL)
2220 goto done;
2221 *label = strdup(got_ref_get_name(ref));
2222 if (*label == NULL) {
2223 err = got_error_from_errno("strdup");
2224 goto done;
2226 err = got_ref_resolve(id, repo, ref);
2227 } else {
2228 err = got_object_id_str(label, *id);
2229 if (*label == NULL) {
2230 err = got_error_from_errno("strdup");
2231 goto done;
2234 done:
2235 if (ref)
2236 got_ref_close(ref);
2237 return err;
2241 static const struct got_error *
2242 cmd_diff(int argc, char *argv[])
2244 const struct got_error *error;
2245 struct got_repository *repo = NULL;
2246 struct got_worktree *worktree = NULL;
2247 char *cwd = NULL, *repo_path = NULL;
2248 struct got_object_id *id1 = NULL, *id2 = NULL;
2249 const char *id_str1 = NULL, *id_str2 = NULL;
2250 char *label1 = NULL, *label2 = NULL;
2251 int type1, type2;
2252 int diff_context = 3, diff_staged = 0, ignore_whitespace = 0, ch;
2253 const char *errstr;
2254 char *path = NULL;
2256 #ifndef PROFILE
2257 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2258 NULL) == -1)
2259 err(1, "pledge");
2260 #endif
2262 while ((ch = getopt(argc, argv, "C:r:sw")) != -1) {
2263 switch (ch) {
2264 case 'C':
2265 diff_context = strtonum(optarg, 1, INT_MAX, &errstr);
2266 if (errstr != NULL)
2267 err(1, "-C option %s", errstr);
2268 break;
2269 case 'r':
2270 repo_path = realpath(optarg, NULL);
2271 if (repo_path == NULL)
2272 return got_error_from_errno2("realpath",
2273 optarg);
2274 got_path_strip_trailing_slashes(repo_path);
2275 break;
2276 case 's':
2277 diff_staged = 1;
2278 break;
2279 case 'w':
2280 ignore_whitespace = 1;
2281 break;
2282 default:
2283 usage_diff();
2284 /* NOTREACHED */
2288 argc -= optind;
2289 argv += optind;
2291 cwd = getcwd(NULL, 0);
2292 if (cwd == NULL) {
2293 error = got_error_from_errno("getcwd");
2294 goto done;
2296 error = got_worktree_open(&worktree, cwd);
2297 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2298 goto done;
2299 if (argc <= 1) {
2300 if (worktree == NULL) {
2301 error = got_error(GOT_ERR_NOT_WORKTREE);
2302 goto done;
2304 if (repo_path)
2305 errx(1,
2306 "-r option can't be used when diffing a work tree");
2307 repo_path = strdup(got_worktree_get_repo_path(worktree));
2308 if (repo_path == NULL) {
2309 error = got_error_from_errno("strdup");
2310 goto done;
2312 if (argc == 1) {
2313 error = got_worktree_resolve_path(&path, worktree,
2314 argv[0]);
2315 if (error)
2316 goto done;
2317 } else {
2318 path = strdup("");
2319 if (path == NULL) {
2320 error = got_error_from_errno("strdup");
2321 goto done;
2324 } else if (argc == 2) {
2325 if (diff_staged)
2326 errx(1, "-s option can't be used when diffing "
2327 "objects in repository");
2328 id_str1 = argv[0];
2329 id_str2 = argv[1];
2330 if (worktree && repo_path == NULL) {
2331 repo_path =
2332 strdup(got_worktree_get_repo_path(worktree));
2333 if (repo_path == NULL) {
2334 error = got_error_from_errno("strdup");
2335 goto done;
2338 } else
2339 usage_diff();
2341 if (repo_path == NULL) {
2342 repo_path = getcwd(NULL, 0);
2343 if (repo_path == NULL)
2344 return got_error_from_errno("getcwd");
2347 error = got_repo_open(&repo, repo_path, NULL);
2348 free(repo_path);
2349 if (error != NULL)
2350 goto done;
2352 error = apply_unveil(got_repo_get_path(repo), 1,
2353 worktree ? got_worktree_get_root_path(worktree) : NULL);
2354 if (error)
2355 goto done;
2357 if (argc <= 1) {
2358 struct print_diff_arg arg;
2359 struct got_pathlist_head paths;
2360 char *id_str;
2362 TAILQ_INIT(&paths);
2364 error = got_object_id_str(&id_str,
2365 got_worktree_get_base_commit_id(worktree));
2366 if (error)
2367 goto done;
2368 arg.repo = repo;
2369 arg.worktree = worktree;
2370 arg.diff_context = diff_context;
2371 arg.id_str = id_str;
2372 arg.header_shown = 0;
2373 arg.diff_staged = diff_staged;
2374 arg.ignore_whitespace = ignore_whitespace;
2376 error = got_pathlist_append(&paths, path, NULL);
2377 if (error)
2378 goto done;
2380 error = got_worktree_status(worktree, &paths, repo, print_diff,
2381 &arg, check_cancelled, NULL);
2382 free(id_str);
2383 got_pathlist_free(&paths);
2384 goto done;
2387 error = match_object_id(&id1, &label1, id_str1, GOT_OBJ_TYPE_ANY, 1,
2388 repo);
2389 if (error)
2390 goto done;
2392 error = match_object_id(&id2, &label2, id_str2, GOT_OBJ_TYPE_ANY, 1,
2393 repo);
2394 if (error)
2395 goto done;
2397 error = got_object_get_type(&type1, repo, id1);
2398 if (error)
2399 goto done;
2401 error = got_object_get_type(&type2, repo, id2);
2402 if (error)
2403 goto done;
2405 if (type1 != type2) {
2406 error = got_error(GOT_ERR_OBJ_TYPE);
2407 goto done;
2410 switch (type1) {
2411 case GOT_OBJ_TYPE_BLOB:
2412 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
2413 diff_context, ignore_whitespace, repo, stdout);
2414 break;
2415 case GOT_OBJ_TYPE_TREE:
2416 error = got_diff_objects_as_trees(id1, id2, "", "",
2417 diff_context, ignore_whitespace, repo, stdout);
2418 break;
2419 case GOT_OBJ_TYPE_COMMIT:
2420 printf("diff %s %s\n", label1, label2);
2421 error = got_diff_objects_as_commits(id1, id2, diff_context,
2422 ignore_whitespace, repo, stdout);
2423 break;
2424 default:
2425 error = got_error(GOT_ERR_OBJ_TYPE);
2428 done:
2429 free(label1);
2430 free(label2);
2431 free(id1);
2432 free(id2);
2433 free(path);
2434 if (worktree)
2435 got_worktree_close(worktree);
2436 if (repo) {
2437 const struct got_error *repo_error;
2438 repo_error = got_repo_close(repo);
2439 if (error == NULL)
2440 error = repo_error;
2442 return error;
2445 __dead static void
2446 usage_blame(void)
2448 fprintf(stderr,
2449 "usage: %s blame [-c commit] [-r repository-path] path\n",
2450 getprogname());
2451 exit(1);
2454 struct blame_line {
2455 int annotated;
2456 char *id_str;
2457 char *committer;
2458 char datebuf[11]; /* YYYY-MM-DD + NUL */
2461 struct blame_cb_args {
2462 struct blame_line *lines;
2463 int nlines;
2464 int nlines_prec;
2465 int lineno_cur;
2466 off_t *line_offsets;
2467 FILE *f;
2468 struct got_repository *repo;
2471 static const struct got_error *
2472 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2474 const struct got_error *err = NULL;
2475 struct blame_cb_args *a = arg;
2476 struct blame_line *bline;
2477 char *line = NULL;
2478 size_t linesize = 0;
2479 struct got_commit_object *commit = NULL;
2480 off_t offset;
2481 struct tm tm;
2482 time_t committer_time;
2484 if (nlines != a->nlines ||
2485 (lineno != -1 && lineno < 1) || lineno > a->nlines)
2486 return got_error(GOT_ERR_RANGE);
2488 if (sigint_received)
2489 return got_error(GOT_ERR_ITER_COMPLETED);
2491 if (lineno == -1)
2492 return NULL; /* no change in this commit */
2494 /* Annotate this line. */
2495 bline = &a->lines[lineno - 1];
2496 if (bline->annotated)
2497 return NULL;
2498 err = got_object_id_str(&bline->id_str, id);
2499 if (err)
2500 return err;
2502 err = got_object_open_as_commit(&commit, a->repo, id);
2503 if (err)
2504 goto done;
2506 bline->committer = strdup(got_object_commit_get_committer(commit));
2507 if (bline->committer == NULL) {
2508 err = got_error_from_errno("strdup");
2509 goto done;
2512 committer_time = got_object_commit_get_committer_time(commit);
2513 if (localtime_r(&committer_time, &tm) == NULL)
2514 return got_error_from_errno("localtime_r");
2515 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G/%m/%d",
2516 &tm) >= sizeof(bline->datebuf)) {
2517 err = got_error(GOT_ERR_NO_SPACE);
2518 goto done;
2520 bline->annotated = 1;
2522 /* Print lines annotated so far. */
2523 bline = &a->lines[a->lineno_cur - 1];
2524 if (!bline->annotated)
2525 goto done;
2527 offset = a->line_offsets[a->lineno_cur - 1];
2528 if (fseeko(a->f, offset, SEEK_SET) == -1) {
2529 err = got_error_from_errno("fseeko");
2530 goto done;
2533 while (bline->annotated) {
2534 char *smallerthan, *at, *nl, *committer;
2535 size_t len;
2537 if (getline(&line, &linesize, a->f) == -1) {
2538 if (ferror(a->f))
2539 err = got_error_from_errno("getline");
2540 break;
2543 committer = bline->committer;
2544 smallerthan = strchr(committer, '<');
2545 if (smallerthan && smallerthan[1] != '\0')
2546 committer = smallerthan + 1;
2547 at = strchr(committer, '@');
2548 if (at)
2549 *at = '\0';
2550 len = strlen(committer);
2551 if (len >= 9)
2552 committer[8] = '\0';
2554 nl = strchr(line, '\n');
2555 if (nl)
2556 *nl = '\0';
2557 printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
2558 bline->id_str, bline->datebuf, committer, line);
2560 a->lineno_cur++;
2561 bline = &a->lines[a->lineno_cur - 1];
2563 done:
2564 if (commit)
2565 got_object_commit_close(commit);
2566 free(line);
2567 return err;
2570 static const struct got_error *
2571 cmd_blame(int argc, char *argv[])
2573 const struct got_error *error;
2574 struct got_repository *repo = NULL;
2575 struct got_worktree *worktree = NULL;
2576 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2577 struct got_object_id *obj_id = NULL;
2578 struct got_object_id *commit_id = NULL;
2579 struct got_blob_object *blob = NULL;
2580 char *commit_id_str = NULL;
2581 struct blame_cb_args bca;
2582 int ch, obj_type, i;
2583 size_t filesize;
2585 memset(&bca, 0, sizeof(bca));
2587 #ifndef PROFILE
2588 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2589 NULL) == -1)
2590 err(1, "pledge");
2591 #endif
2593 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2594 switch (ch) {
2595 case 'c':
2596 commit_id_str = optarg;
2597 break;
2598 case 'r':
2599 repo_path = realpath(optarg, NULL);
2600 if (repo_path == NULL)
2601 return got_error_from_errno2("realpath",
2602 optarg);
2603 got_path_strip_trailing_slashes(repo_path);
2604 break;
2605 default:
2606 usage_blame();
2607 /* NOTREACHED */
2611 argc -= optind;
2612 argv += optind;
2614 if (argc == 1)
2615 path = argv[0];
2616 else
2617 usage_blame();
2619 cwd = getcwd(NULL, 0);
2620 if (cwd == NULL) {
2621 error = got_error_from_errno("getcwd");
2622 goto done;
2624 if (repo_path == NULL) {
2625 error = got_worktree_open(&worktree, cwd);
2626 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2627 goto done;
2628 else
2629 error = NULL;
2630 if (worktree) {
2631 repo_path =
2632 strdup(got_worktree_get_repo_path(worktree));
2633 if (repo_path == NULL)
2634 error = got_error_from_errno("strdup");
2635 if (error)
2636 goto done;
2637 } else {
2638 repo_path = strdup(cwd);
2639 if (repo_path == NULL) {
2640 error = got_error_from_errno("strdup");
2641 goto done;
2646 error = got_repo_open(&repo, repo_path, NULL);
2647 if (error != NULL)
2648 goto done;
2650 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2651 if (error)
2652 goto done;
2654 if (worktree) {
2655 const char *prefix = got_worktree_get_path_prefix(worktree);
2656 char *p, *worktree_subdir = cwd +
2657 strlen(got_worktree_get_root_path(worktree));
2658 if (asprintf(&p, "%s%s%s%s%s",
2659 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
2660 worktree_subdir, worktree_subdir[0] ? "/" : "",
2661 path) == -1) {
2662 error = got_error_from_errno("asprintf");
2663 goto done;
2665 error = got_repo_map_path(&in_repo_path, repo, p, 0);
2666 free(p);
2667 } else {
2668 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2670 if (error)
2671 goto done;
2673 if (commit_id_str == NULL) {
2674 struct got_reference *head_ref;
2675 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
2676 if (error != NULL)
2677 goto done;
2678 error = got_ref_resolve(&commit_id, repo, head_ref);
2679 got_ref_close(head_ref);
2680 if (error != NULL)
2681 goto done;
2682 } else {
2683 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
2684 if (error)
2685 goto done;
2688 error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
2689 if (error)
2690 goto done;
2691 if (obj_id == NULL) {
2692 error = got_error(GOT_ERR_NO_OBJ);
2693 goto done;
2696 error = got_object_get_type(&obj_type, repo, obj_id);
2697 if (error)
2698 goto done;
2700 if (obj_type != GOT_OBJ_TYPE_BLOB) {
2701 error = got_error(GOT_ERR_OBJ_TYPE);
2702 goto done;
2705 error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
2706 if (error)
2707 goto done;
2708 bca.f = got_opentemp();
2709 if (bca.f == NULL) {
2710 error = got_error_from_errno("got_opentemp");
2711 goto done;
2713 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
2714 &bca.line_offsets, bca.f, blob);
2715 if (error || bca.nlines == 0)
2716 goto done;
2718 /* Don't include \n at EOF in the blame line count. */
2719 if (bca.line_offsets[bca.nlines - 1] == filesize)
2720 bca.nlines--;
2722 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
2723 if (bca.lines == NULL) {
2724 error = got_error_from_errno("calloc");
2725 goto done;
2727 bca.lineno_cur = 1;
2728 bca.nlines_prec = 0;
2729 i = bca.nlines;
2730 while (i > 0) {
2731 i /= 10;
2732 bca.nlines_prec++;
2734 bca.repo = repo;
2736 error = got_blame(in_repo_path, commit_id, repo, blame_cb, &bca,
2737 check_cancelled, NULL);
2738 if (error)
2739 goto done;
2740 done:
2741 free(in_repo_path);
2742 free(repo_path);
2743 free(cwd);
2744 free(commit_id);
2745 free(obj_id);
2746 if (blob)
2747 got_object_blob_close(blob);
2748 if (worktree)
2749 got_worktree_close(worktree);
2750 if (repo) {
2751 const struct got_error *repo_error;
2752 repo_error = got_repo_close(repo);
2753 if (error == NULL)
2754 error = repo_error;
2756 if (bca.lines) {
2757 for (i = 0; i < bca.nlines; i++) {
2758 struct blame_line *bline = &bca.lines[i];
2759 free(bline->id_str);
2760 free(bline->committer);
2762 free(bca.lines);
2764 free(bca.line_offsets);
2765 if (bca.f && fclose(bca.f) == EOF && error == NULL)
2766 error = got_error_from_errno("fclose");
2767 return error;
2770 __dead static void
2771 usage_tree(void)
2773 fprintf(stderr,
2774 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
2775 getprogname());
2776 exit(1);
2779 static void
2780 print_entry(struct got_tree_entry *te, const char *id, const char *path,
2781 const char *root_path)
2783 int is_root_path = (strcmp(path, root_path) == 0);
2784 const char *modestr = "";
2785 mode_t mode = got_tree_entry_get_mode(te);
2787 path += strlen(root_path);
2788 while (path[0] == '/')
2789 path++;
2791 if (got_object_tree_entry_is_submodule(te))
2792 modestr = "$";
2793 else if (S_ISLNK(mode))
2794 modestr = "@";
2795 else if (S_ISDIR(mode))
2796 modestr = "/";
2797 else if (mode & S_IXUSR)
2798 modestr = "*";
2800 printf("%s%s%s%s%s\n", id ? id : "", path,
2801 is_root_path ? "" : "/", got_tree_entry_get_name(te), modestr);
2804 static const struct got_error *
2805 print_tree(const char *path, struct got_object_id *commit_id,
2806 int show_ids, int recurse, const char *root_path,
2807 struct got_repository *repo)
2809 const struct got_error *err = NULL;
2810 struct got_object_id *tree_id = NULL;
2811 struct got_tree_object *tree = NULL;
2812 int nentries, i;
2814 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
2815 if (err)
2816 goto done;
2818 err = got_object_open_as_tree(&tree, repo, tree_id);
2819 if (err)
2820 goto done;
2821 nentries = got_object_tree_get_nentries(tree);
2822 for (i = 0; i < nentries; i++) {
2823 struct got_tree_entry *te;
2824 char *id = NULL;
2826 if (sigint_received || sigpipe_received)
2827 break;
2829 te = got_object_tree_get_entry(tree, i);
2830 if (show_ids) {
2831 char *id_str;
2832 err = got_object_id_str(&id_str,
2833 got_tree_entry_get_id(te));
2834 if (err)
2835 goto done;
2836 if (asprintf(&id, "%s ", id_str) == -1) {
2837 err = got_error_from_errno("asprintf");
2838 free(id_str);
2839 goto done;
2841 free(id_str);
2843 print_entry(te, id, path, root_path);
2844 free(id);
2846 if (recurse && S_ISDIR(got_tree_entry_get_mode(te))) {
2847 char *child_path;
2848 if (asprintf(&child_path, "%s%s%s", path,
2849 path[0] == '/' && path[1] == '\0' ? "" : "/",
2850 got_tree_entry_get_name(te)) == -1) {
2851 err = got_error_from_errno("asprintf");
2852 goto done;
2854 err = print_tree(child_path, commit_id, show_ids, 1,
2855 root_path, repo);
2856 free(child_path);
2857 if (err)
2858 goto done;
2861 done:
2862 if (tree)
2863 got_object_tree_close(tree);
2864 free(tree_id);
2865 return err;
2868 static const struct got_error *
2869 cmd_tree(int argc, char *argv[])
2871 const struct got_error *error;
2872 struct got_repository *repo = NULL;
2873 struct got_worktree *worktree = NULL;
2874 const char *path;
2875 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2876 struct got_object_id *commit_id = NULL;
2877 char *commit_id_str = NULL;
2878 int show_ids = 0, recurse = 0;
2879 int ch;
2881 #ifndef PROFILE
2882 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2883 NULL) == -1)
2884 err(1, "pledge");
2885 #endif
2887 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
2888 switch (ch) {
2889 case 'c':
2890 commit_id_str = optarg;
2891 break;
2892 case 'r':
2893 repo_path = realpath(optarg, NULL);
2894 if (repo_path == NULL)
2895 return got_error_from_errno2("realpath",
2896 optarg);
2897 got_path_strip_trailing_slashes(repo_path);
2898 break;
2899 case 'i':
2900 show_ids = 1;
2901 break;
2902 case 'R':
2903 recurse = 1;
2904 break;
2905 default:
2906 usage_tree();
2907 /* NOTREACHED */
2911 argc -= optind;
2912 argv += optind;
2914 if (argc == 1)
2915 path = argv[0];
2916 else if (argc > 1)
2917 usage_tree();
2918 else
2919 path = NULL;
2921 cwd = getcwd(NULL, 0);
2922 if (cwd == NULL) {
2923 error = got_error_from_errno("getcwd");
2924 goto done;
2926 if (repo_path == NULL) {
2927 error = got_worktree_open(&worktree, cwd);
2928 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2929 goto done;
2930 else
2931 error = NULL;
2932 if (worktree) {
2933 repo_path =
2934 strdup(got_worktree_get_repo_path(worktree));
2935 if (repo_path == NULL)
2936 error = got_error_from_errno("strdup");
2937 if (error)
2938 goto done;
2939 } else {
2940 repo_path = strdup(cwd);
2941 if (repo_path == NULL) {
2942 error = got_error_from_errno("strdup");
2943 goto done;
2948 error = got_repo_open(&repo, repo_path, NULL);
2949 if (error != NULL)
2950 goto done;
2952 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2953 if (error)
2954 goto done;
2956 if (path == NULL) {
2957 if (worktree) {
2958 char *p, *worktree_subdir = cwd +
2959 strlen(got_worktree_get_root_path(worktree));
2960 if (asprintf(&p, "%s/%s",
2961 got_worktree_get_path_prefix(worktree),
2962 worktree_subdir) == -1) {
2963 error = got_error_from_errno("asprintf");
2964 goto done;
2966 error = got_repo_map_path(&in_repo_path, repo, p, 1);
2967 free(p);
2968 if (error)
2969 goto done;
2970 } else
2971 path = "/";
2973 if (in_repo_path == NULL) {
2974 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2975 if (error != NULL)
2976 goto done;
2979 if (commit_id_str == NULL) {
2980 struct got_reference *head_ref;
2981 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
2982 if (error != NULL)
2983 goto done;
2984 error = got_ref_resolve(&commit_id, repo, head_ref);
2985 got_ref_close(head_ref);
2986 if (error != NULL)
2987 goto done;
2988 } else {
2989 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
2990 if (error)
2991 goto done;
2994 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
2995 in_repo_path, repo);
2996 done:
2997 free(in_repo_path);
2998 free(repo_path);
2999 free(cwd);
3000 free(commit_id);
3001 if (worktree)
3002 got_worktree_close(worktree);
3003 if (repo) {
3004 const struct got_error *repo_error;
3005 repo_error = got_repo_close(repo);
3006 if (error == NULL)
3007 error = repo_error;
3009 return error;
3012 __dead static void
3013 usage_status(void)
3015 fprintf(stderr, "usage: %s status [path ...]\n", getprogname());
3016 exit(1);
3019 static const struct got_error *
3020 print_status(void *arg, unsigned char status, unsigned char staged_status,
3021 const char *path, struct got_object_id *blob_id,
3022 struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
3024 if (status == staged_status && (status == GOT_STATUS_DELETE))
3025 status = GOT_STATUS_NO_CHANGE;
3026 printf("%c%c %s\n", status, staged_status, path);
3027 return NULL;
3030 static const struct got_error *
3031 cmd_status(int argc, char *argv[])
3033 const struct got_error *error = NULL;
3034 struct got_repository *repo = NULL;
3035 struct got_worktree *worktree = NULL;
3036 char *cwd = NULL;
3037 struct got_pathlist_head paths;
3038 struct got_pathlist_entry *pe;
3039 int ch;
3041 TAILQ_INIT(&paths);
3043 while ((ch = getopt(argc, argv, "")) != -1) {
3044 switch (ch) {
3045 default:
3046 usage_status();
3047 /* NOTREACHED */
3051 argc -= optind;
3052 argv += optind;
3054 #ifndef PROFILE
3055 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3056 NULL) == -1)
3057 err(1, "pledge");
3058 #endif
3059 cwd = getcwd(NULL, 0);
3060 if (cwd == NULL) {
3061 error = got_error_from_errno("getcwd");
3062 goto done;
3065 error = got_worktree_open(&worktree, cwd);
3066 if (error != NULL)
3067 goto done;
3069 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
3070 NULL);
3071 if (error != NULL)
3072 goto done;
3074 error = apply_unveil(got_repo_get_path(repo), 1,
3075 got_worktree_get_root_path(worktree));
3076 if (error)
3077 goto done;
3079 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3080 if (error)
3081 goto done;
3083 error = got_worktree_status(worktree, &paths, repo, print_status, NULL,
3084 check_cancelled, NULL);
3085 done:
3086 TAILQ_FOREACH(pe, &paths, entry)
3087 free((char *)pe->path);
3088 got_pathlist_free(&paths);
3089 free(cwd);
3090 return error;
3093 __dead static void
3094 usage_ref(void)
3096 fprintf(stderr,
3097 "usage: %s ref [-r repository] -l | -d name | [-s] name target\n",
3098 getprogname());
3099 exit(1);
3102 static const struct got_error *
3103 list_refs(struct got_repository *repo)
3105 static const struct got_error *err = NULL;
3106 struct got_reflist_head refs;
3107 struct got_reflist_entry *re;
3109 SIMPLEQ_INIT(&refs);
3110 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
3111 if (err)
3112 return err;
3114 SIMPLEQ_FOREACH(re, &refs, entry) {
3115 char *refstr;
3116 refstr = got_ref_to_str(re->ref);
3117 if (refstr == NULL)
3118 return got_error_from_errno("got_ref_to_str");
3119 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
3120 free(refstr);
3123 got_ref_list_free(&refs);
3124 return NULL;
3127 static const struct got_error *
3128 delete_ref(struct got_repository *repo, const char *refname)
3130 const struct got_error *err = NULL;
3131 struct got_reference *ref;
3133 err = got_ref_open(&ref, repo, refname, 0);
3134 if (err)
3135 return err;
3137 err = got_ref_delete(ref, repo);
3138 got_ref_close(ref);
3139 return err;
3142 static const struct got_error *
3143 add_ref(struct got_repository *repo, const char *refname, const char *target)
3145 const struct got_error *err = NULL;
3146 struct got_object_id *id;
3147 struct got_reference *ref = NULL;
3150 * Don't let the user create a reference name with a leading '-'.
3151 * While technically a valid reference name, this case is usually
3152 * an unintended typo.
3154 if (refname[0] == '-')
3155 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
3157 err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
3158 repo);
3159 if (err) {
3160 struct got_reference *target_ref;
3162 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
3163 return err;
3164 err = got_ref_open(&target_ref, repo, target, 0);
3165 if (err)
3166 return err;
3167 err = got_ref_resolve(&id, repo, target_ref);
3168 got_ref_close(target_ref);
3169 if (err)
3170 return err;
3173 err = got_ref_alloc(&ref, refname, id);
3174 if (err)
3175 goto done;
3177 err = got_ref_write(ref, repo);
3178 done:
3179 if (ref)
3180 got_ref_close(ref);
3181 free(id);
3182 return err;
3185 static const struct got_error *
3186 add_symref(struct got_repository *repo, const char *refname, const char *target)
3188 const struct got_error *err = NULL;
3189 struct got_reference *ref = NULL;
3190 struct got_reference *target_ref = NULL;
3193 * Don't let the user create a reference name with a leading '-'.
3194 * While technically a valid reference name, this case is usually
3195 * an unintended typo.
3197 if (refname[0] == '-')
3198 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
3200 err = got_ref_open(&target_ref, repo, target, 0);
3201 if (err)
3202 return err;
3204 err = got_ref_alloc_symref(&ref, refname, target_ref);
3205 if (err)
3206 goto done;
3208 err = got_ref_write(ref, repo);
3209 done:
3210 if (target_ref)
3211 got_ref_close(target_ref);
3212 if (ref)
3213 got_ref_close(ref);
3214 return err;
3217 static const struct got_error *
3218 cmd_ref(int argc, char *argv[])
3220 const struct got_error *error = NULL;
3221 struct got_repository *repo = NULL;
3222 struct got_worktree *worktree = NULL;
3223 char *cwd = NULL, *repo_path = NULL;
3224 int ch, do_list = 0, create_symref = 0;
3225 const char *delref = NULL;
3227 /* TODO: Add -s option for adding symbolic references. */
3228 while ((ch = getopt(argc, argv, "d:r:ls")) != -1) {
3229 switch (ch) {
3230 case 'd':
3231 delref = optarg;
3232 break;
3233 case 'r':
3234 repo_path = realpath(optarg, NULL);
3235 if (repo_path == NULL)
3236 return got_error_from_errno2("realpath",
3237 optarg);
3238 got_path_strip_trailing_slashes(repo_path);
3239 break;
3240 case 'l':
3241 do_list = 1;
3242 break;
3243 case 's':
3244 create_symref = 1;
3245 break;
3246 default:
3247 usage_ref();
3248 /* NOTREACHED */
3252 if (do_list && delref)
3253 errx(1, "-l and -d options are mutually exclusive\n");
3255 argc -= optind;
3256 argv += optind;
3258 if (do_list || delref) {
3259 if (create_symref)
3260 errx(1, "-s option cannot be used together with the "
3261 "-l or -d options");
3262 if (argc > 0)
3263 usage_ref();
3264 } else if (argc != 2)
3265 usage_ref();
3267 #ifndef PROFILE
3268 if (do_list) {
3269 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3270 NULL) == -1)
3271 err(1, "pledge");
3272 } else {
3273 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3274 "sendfd unveil", NULL) == -1)
3275 err(1, "pledge");
3277 #endif
3278 cwd = getcwd(NULL, 0);
3279 if (cwd == NULL) {
3280 error = got_error_from_errno("getcwd");
3281 goto done;
3284 if (repo_path == NULL) {
3285 error = got_worktree_open(&worktree, cwd);
3286 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3287 goto done;
3288 else
3289 error = NULL;
3290 if (worktree) {
3291 repo_path =
3292 strdup(got_worktree_get_repo_path(worktree));
3293 if (repo_path == NULL)
3294 error = got_error_from_errno("strdup");
3295 if (error)
3296 goto done;
3297 } else {
3298 repo_path = strdup(cwd);
3299 if (repo_path == NULL) {
3300 error = got_error_from_errno("strdup");
3301 goto done;
3306 error = got_repo_open(&repo, repo_path, NULL);
3307 if (error != NULL)
3308 goto done;
3310 error = apply_unveil(got_repo_get_path(repo), do_list,
3311 worktree ? got_worktree_get_root_path(worktree) : NULL);
3312 if (error)
3313 goto done;
3315 if (do_list)
3316 error = list_refs(repo);
3317 else if (delref)
3318 error = delete_ref(repo, delref);
3319 else if (create_symref)
3320 error = add_symref(repo, argv[0], argv[1]);
3321 else
3322 error = add_ref(repo, argv[0], argv[1]);
3323 done:
3324 if (repo)
3325 got_repo_close(repo);
3326 if (worktree)
3327 got_worktree_close(worktree);
3328 free(cwd);
3329 free(repo_path);
3330 return error;
3333 __dead static void
3334 usage_branch(void)
3336 fprintf(stderr,
3337 "usage: %s branch [-c commit] [-r repository] [-l] | -d name | "
3338 "[name]\n", getprogname());
3339 exit(1);
3342 static const struct got_error *
3343 list_branch(struct got_repository *repo, struct got_worktree *worktree,
3344 struct got_reference *ref)
3346 const struct got_error *err = NULL;
3347 const char *refname, *marker = " ";
3348 char *refstr;
3350 refname = got_ref_get_name(ref);
3351 if (worktree && strcmp(refname,
3352 got_worktree_get_head_ref_name(worktree)) == 0) {
3353 struct got_object_id *id = NULL;
3355 err = got_ref_resolve(&id, repo, ref);
3356 if (err)
3357 return err;
3358 if (got_object_id_cmp(id,
3359 got_worktree_get_base_commit_id(worktree)) == 0)
3360 marker = "* ";
3361 else
3362 marker = "~ ";
3363 free(id);
3366 if (strncmp(refname, "refs/heads/", 11) == 0)
3367 refname += 11;
3368 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
3369 refname += 18;
3371 refstr = got_ref_to_str(ref);
3372 if (refstr == NULL)
3373 return got_error_from_errno("got_ref_to_str");
3375 printf("%s%s: %s\n", marker, refname, refstr);
3376 free(refstr);
3377 return NULL;
3380 static const struct got_error *
3381 show_current_branch(struct got_repository *repo, struct got_worktree *worktree)
3383 const char *refname;
3385 if (worktree == NULL)
3386 return got_error(GOT_ERR_NOT_WORKTREE);
3388 refname = got_worktree_get_head_ref_name(worktree);
3390 if (strncmp(refname, "refs/heads/", 11) == 0)
3391 refname += 11;
3392 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
3393 refname += 18;
3395 printf("%s\n", refname);
3397 return NULL;
3400 static const struct got_error *
3401 list_branches(struct got_repository *repo, struct got_worktree *worktree)
3403 static const struct got_error *err = NULL;
3404 struct got_reflist_head refs;
3405 struct got_reflist_entry *re;
3406 struct got_reference *temp_ref = NULL;
3407 int rebase_in_progress, histedit_in_progress;
3409 SIMPLEQ_INIT(&refs);
3411 if (worktree) {
3412 err = got_worktree_rebase_in_progress(&rebase_in_progress,
3413 worktree);
3414 if (err)
3415 return err;
3417 err = got_worktree_histedit_in_progress(&histedit_in_progress,
3418 worktree);
3419 if (err)
3420 return err;
3422 if (rebase_in_progress || histedit_in_progress) {
3423 err = got_ref_open(&temp_ref, repo,
3424 got_worktree_get_head_ref_name(worktree), 0);
3425 if (err)
3426 return err;
3427 list_branch(repo, worktree, temp_ref);
3428 got_ref_close(temp_ref);
3432 err = got_ref_list(&refs, repo, "refs/heads",
3433 got_ref_cmp_by_name, NULL);
3434 if (err)
3435 return err;
3437 SIMPLEQ_FOREACH(re, &refs, entry)
3438 list_branch(repo, worktree, re->ref);
3440 got_ref_list_free(&refs);
3441 return NULL;
3444 static const struct got_error *
3445 delete_branch(struct got_repository *repo, struct got_worktree *worktree,
3446 const char *branch_name)
3448 const struct got_error *err = NULL;
3449 struct got_reference *ref = NULL;
3450 char *refname;
3452 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
3453 return got_error_from_errno("asprintf");
3455 err = got_ref_open(&ref, repo, refname, 0);
3456 if (err)
3457 goto done;
3459 if (worktree &&
3460 strcmp(got_worktree_get_head_ref_name(worktree),
3461 got_ref_get_name(ref)) == 0) {
3462 err = got_error_msg(GOT_ERR_SAME_BRANCH,
3463 "will not delete this work tree's current branch");
3464 goto done;
3467 err = got_ref_delete(ref, repo);
3468 done:
3469 if (ref)
3470 got_ref_close(ref);
3471 free(refname);
3472 return err;
3475 static const struct got_error *
3476 add_branch(struct got_repository *repo, const char *branch_name,
3477 struct got_object_id *base_commit_id)
3479 const struct got_error *err = NULL;
3480 struct got_reference *ref = NULL;
3481 char *base_refname = NULL, *refname = NULL;
3484 * Don't let the user create a branch name with a leading '-'.
3485 * While technically a valid reference name, this case is usually
3486 * an unintended typo.
3488 if (branch_name[0] == '-')
3489 return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
3491 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
3492 err = got_error_from_errno("asprintf");
3493 goto done;
3496 err = got_ref_open(&ref, repo, refname, 0);
3497 if (err == NULL) {
3498 err = got_error(GOT_ERR_BRANCH_EXISTS);
3499 goto done;
3500 } else if (err->code != GOT_ERR_NOT_REF)
3501 goto done;
3503 err = got_ref_alloc(&ref, refname, base_commit_id);
3504 if (err)
3505 goto done;
3507 err = got_ref_write(ref, repo);
3508 done:
3509 if (ref)
3510 got_ref_close(ref);
3511 free(base_refname);
3512 free(refname);
3513 return err;
3516 static const struct got_error *
3517 cmd_branch(int argc, char *argv[])
3519 const struct got_error *error = NULL;
3520 struct got_repository *repo = NULL;
3521 struct got_worktree *worktree = NULL;
3522 char *cwd = NULL, *repo_path = NULL;
3523 int ch, do_list = 0, do_show = 0;
3524 const char *delref = NULL, *commit_id_arg = NULL;
3526 while ((ch = getopt(argc, argv, "c:d:r:l")) != -1) {
3527 switch (ch) {
3528 case 'c':
3529 commit_id_arg = optarg;
3530 break;
3531 case 'd':
3532 delref = optarg;
3533 break;
3534 case 'r':
3535 repo_path = realpath(optarg, NULL);
3536 if (repo_path == NULL)
3537 return got_error_from_errno2("realpath",
3538 optarg);
3539 got_path_strip_trailing_slashes(repo_path);
3540 break;
3541 case 'l':
3542 do_list = 1;
3543 break;
3544 default:
3545 usage_branch();
3546 /* NOTREACHED */
3550 if (do_list && delref)
3551 errx(1, "-l and -d options are mutually exclusive\n");
3553 argc -= optind;
3554 argv += optind;
3556 if (!do_list && !delref && argc == 0)
3557 do_show = 1;
3559 if ((do_list || delref || do_show) && commit_id_arg != NULL)
3560 errx(1, "-c option can only be used when creating a branch");
3562 if (do_list || delref) {
3563 if (argc > 0)
3564 usage_branch();
3565 } else if (!do_show && argc != 1)
3566 usage_branch();
3568 #ifndef PROFILE
3569 if (do_list || do_show) {
3570 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3571 NULL) == -1)
3572 err(1, "pledge");
3573 } else {
3574 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3575 "sendfd unveil", NULL) == -1)
3576 err(1, "pledge");
3578 #endif
3579 cwd = getcwd(NULL, 0);
3580 if (cwd == NULL) {
3581 error = got_error_from_errno("getcwd");
3582 goto done;
3585 if (repo_path == NULL) {
3586 error = got_worktree_open(&worktree, cwd);
3587 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3588 goto done;
3589 else
3590 error = NULL;
3591 if (worktree) {
3592 repo_path =
3593 strdup(got_worktree_get_repo_path(worktree));
3594 if (repo_path == NULL)
3595 error = got_error_from_errno("strdup");
3596 if (error)
3597 goto done;
3598 } else {
3599 repo_path = strdup(cwd);
3600 if (repo_path == NULL) {
3601 error = got_error_from_errno("strdup");
3602 goto done;
3607 error = got_repo_open(&repo, repo_path, NULL);
3608 if (error != NULL)
3609 goto done;
3611 error = apply_unveil(got_repo_get_path(repo), do_list,
3612 worktree ? got_worktree_get_root_path(worktree) : NULL);
3613 if (error)
3614 goto done;
3616 if (do_show)
3617 error = show_current_branch(repo, worktree);
3618 else if (do_list)
3619 error = list_branches(repo, worktree);
3620 else if (delref)
3621 error = delete_branch(repo, worktree, delref);
3622 else {
3623 struct got_object_id *commit_id;
3624 if (commit_id_arg == NULL)
3625 commit_id_arg = worktree ?
3626 got_worktree_get_head_ref_name(worktree) :
3627 GOT_REF_HEAD;
3628 error = resolve_commit_arg(&commit_id, commit_id_arg, repo);
3629 if (error)
3630 goto done;
3631 error = add_branch(repo, argv[0], commit_id);
3632 free(commit_id);
3634 done:
3635 if (repo)
3636 got_repo_close(repo);
3637 if (worktree)
3638 got_worktree_close(worktree);
3639 free(cwd);
3640 free(repo_path);
3641 return error;
3645 __dead static void
3646 usage_tag(void)
3648 fprintf(stderr,
3649 "usage: %s tag [-r repository] | -l | "
3650 "[-m message] name [commit]\n", getprogname());
3651 exit(1);
3654 #if 0
3655 static const struct got_error *
3656 sort_tags(struct got_reflist_head *sorted, struct got_reflist_head *tags)
3658 const struct got_error *err = NULL;
3659 struct got_reflist_entry *re, *se, *new;
3660 struct got_object_id *re_id, *se_id;
3661 struct got_tag_object *re_tag, *se_tag;
3662 time_t re_time, se_time;
3664 SIMPLEQ_FOREACH(re, tags, entry) {
3665 se = SIMPLEQ_FIRST(sorted);
3666 if (se == NULL) {
3667 err = got_reflist_entry_dup(&new, re);
3668 if (err)
3669 return err;
3670 SIMPLEQ_INSERT_HEAD(sorted, new, entry);
3671 continue;
3672 } else {
3673 err = got_ref_resolve(&re_id, repo, re->ref);
3674 if (err)
3675 break;
3676 err = got_object_open_as_tag(&re_tag, repo, re_id);
3677 free(re_id);
3678 if (err)
3679 break;
3680 re_time = got_object_tag_get_tagger_time(re_tag);
3681 got_object_tag_close(re_tag);
3684 while (se) {
3685 err = got_ref_resolve(&se_id, repo, re->ref);
3686 if (err)
3687 break;
3688 err = got_object_open_as_tag(&se_tag, repo, se_id);
3689 free(se_id);
3690 if (err)
3691 break;
3692 se_time = got_object_tag_get_tagger_time(se_tag);
3693 got_object_tag_close(se_tag);
3695 if (se_time > re_time) {
3696 err = got_reflist_entry_dup(&new, re);
3697 if (err)
3698 return err;
3699 SIMPLEQ_INSERT_AFTER(sorted, se, new, entry);
3700 break;
3702 se = SIMPLEQ_NEXT(se, entry);
3703 continue;
3706 done:
3707 return err;
3709 #endif
3711 static const struct got_error *
3712 cmp_tags(void *arg, int *cmp, struct got_reference *ref1,
3713 struct got_reference *ref2)
3715 const struct got_error *err = NULL;
3716 struct got_repository *repo = arg;
3717 struct got_object_id *id1, *id2 = NULL;
3718 struct got_tag_object *tag1 = NULL, *tag2 = NULL;
3719 time_t time1, time2;
3721 *cmp = 0;
3723 err = got_ref_resolve(&id1, repo, ref1);
3724 if (err)
3725 return err;
3726 err = got_object_open_as_tag(&tag1, repo, id1);
3727 if (err)
3728 goto done;
3730 err = got_ref_resolve(&id2, repo, ref2);
3731 if (err)
3732 goto done;
3733 err = got_object_open_as_tag(&tag2, repo, id2);
3734 if (err)
3735 goto done;
3737 time1 = got_object_tag_get_tagger_time(tag1);
3738 time2 = got_object_tag_get_tagger_time(tag2);
3740 /* Put latest tags first. */
3741 if (time1 < time2)
3742 *cmp = 1;
3743 else if (time1 > time2)
3744 *cmp = -1;
3745 else
3746 err = got_ref_cmp_by_name(NULL, cmp, ref2, ref1);
3747 done:
3748 free(id1);
3749 free(id2);
3750 if (tag1)
3751 got_object_tag_close(tag1);
3752 if (tag2)
3753 got_object_tag_close(tag2);
3754 return err;
3757 static const struct got_error *
3758 list_tags(struct got_repository *repo, struct got_worktree *worktree)
3760 static const struct got_error *err = NULL;
3761 struct got_reflist_head refs;
3762 struct got_reflist_entry *re;
3764 SIMPLEQ_INIT(&refs);
3766 err = got_ref_list(&refs, repo, "refs/tags", cmp_tags, repo);
3767 if (err)
3768 return err;
3770 SIMPLEQ_FOREACH(re, &refs, entry) {
3771 const char *refname;
3772 char *refstr, *tagmsg0, *tagmsg, *line, *id_str, *datestr;
3773 char datebuf[26];
3774 time_t tagger_time;
3775 struct got_object_id *id;
3776 struct got_tag_object *tag;
3778 refname = got_ref_get_name(re->ref);
3779 if (strncmp(refname, "refs/tags/", 10) != 0)
3780 continue;
3781 refname += 10;
3782 refstr = got_ref_to_str(re->ref);
3783 if (refstr == NULL) {
3784 err = got_error_from_errno("got_ref_to_str");
3785 break;
3787 printf("%stag %s %s\n", GOT_COMMIT_SEP_STR, refname, refstr);
3788 free(refstr);
3790 err = got_ref_resolve(&id, repo, re->ref);
3791 if (err)
3792 break;
3793 err = got_object_open_as_tag(&tag, repo, id);
3794 free(id);
3795 if (err)
3796 break;
3797 printf("from: %s\n", got_object_tag_get_tagger(tag));
3798 tagger_time = got_object_tag_get_tagger_time(tag);
3799 datestr = get_datestr(&tagger_time, datebuf);
3800 if (datestr)
3801 printf("date: %s UTC\n", datestr);
3802 err = got_object_id_str(&id_str,
3803 got_object_tag_get_object_id(tag));
3804 if (err)
3805 break;
3806 switch (got_object_tag_get_object_type(tag)) {
3807 case GOT_OBJ_TYPE_BLOB:
3808 printf("object: %s %s\n", GOT_OBJ_LABEL_BLOB, id_str);
3809 break;
3810 case GOT_OBJ_TYPE_TREE:
3811 printf("object: %s %s\n", GOT_OBJ_LABEL_TREE, id_str);
3812 break;
3813 case GOT_OBJ_TYPE_COMMIT:
3814 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
3815 break;
3816 case GOT_OBJ_TYPE_TAG:
3817 printf("object: %s %s\n", GOT_OBJ_LABEL_TAG, id_str);
3818 break;
3819 default:
3820 break;
3822 free(id_str);
3823 tagmsg0 = strdup(got_object_tag_get_message(tag));
3824 got_object_tag_close(tag);
3825 if (tagmsg0 == NULL) {
3826 err = got_error_from_errno("strdup");
3827 break;
3830 tagmsg = tagmsg0;
3831 do {
3832 line = strsep(&tagmsg, "\n");
3833 if (line)
3834 printf(" %s\n", line);
3835 } while (line);
3836 free(tagmsg0);
3839 got_ref_list_free(&refs);
3840 return NULL;
3843 static const struct got_error *
3844 get_tag_message(char **tagmsg, char **tagmsg_path, const char *commit_id_str,
3845 const char *tag_name, const char *repo_path)
3847 const struct got_error *err = NULL;
3848 char *template = NULL, *initial_content = NULL;
3849 char *editor = NULL;
3850 int fd = -1;
3852 if (asprintf(&template, "/tmp/got-tagmsg") == -1) {
3853 err = got_error_from_errno("asprintf");
3854 goto done;
3857 if (asprintf(&initial_content, "\n# tagging commit %s as %s\n",
3858 commit_id_str, tag_name) == -1) {
3859 err = got_error_from_errno("asprintf");
3860 goto done;
3863 err = got_opentemp_named_fd(tagmsg_path, &fd, template);
3864 if (err)
3865 goto done;
3867 dprintf(fd, initial_content);
3868 close(fd);
3870 err = get_editor(&editor);
3871 if (err)
3872 goto done;
3873 err = edit_logmsg(tagmsg, editor, *tagmsg_path, initial_content);
3874 done:
3875 free(initial_content);
3876 free(template);
3877 free(editor);
3879 /* Editor is done; we can now apply unveil(2) */
3880 if (err == NULL) {
3881 err = apply_unveil(repo_path, 0, NULL);
3882 if (err) {
3883 free(*tagmsg);
3884 *tagmsg = NULL;
3887 return err;
3890 static const struct got_error *
3891 add_tag(struct got_repository *repo, const char *tag_name,
3892 const char *commit_arg, const char *tagmsg_arg)
3894 const struct got_error *err = NULL;
3895 struct got_object_id *commit_id = NULL, *tag_id = NULL;
3896 char *label = NULL, *commit_id_str = NULL;
3897 struct got_reference *ref = NULL;
3898 char *refname = NULL, *tagmsg = NULL, *tagger = NULL;
3899 char *tagmsg_path = NULL, *tag_id_str = NULL;
3900 int preserve_tagmsg = 0;
3903 * Don't let the user create a tag name with a leading '-'.
3904 * While technically a valid reference name, this case is usually
3905 * an unintended typo.
3907 if (tag_name[0] == '-')
3908 return got_error_path(tag_name, GOT_ERR_REF_NAME_MINUS);
3910 err = get_author(&tagger, repo);
3911 if (err)
3912 return err;
3914 err = match_object_id(&commit_id, &label, commit_arg,
3915 GOT_OBJ_TYPE_COMMIT, 1, repo);
3916 if (err)
3917 goto done;
3919 err = got_object_id_str(&commit_id_str, commit_id);
3920 if (err)
3921 goto done;
3923 if (strncmp("refs/tags/", tag_name, 10) == 0) {
3924 refname = strdup(tag_name);
3925 if (refname == NULL) {
3926 err = got_error_from_errno("strdup");
3927 goto done;
3929 tag_name += 10;
3930 } else if (asprintf(&refname, "refs/tags/%s", tag_name) == -1) {
3931 err = got_error_from_errno("asprintf");
3932 goto done;
3935 err = got_ref_open(&ref, repo, refname, 0);
3936 if (err == NULL) {
3937 err = got_error(GOT_ERR_TAG_EXISTS);
3938 goto done;
3939 } else if (err->code != GOT_ERR_NOT_REF)
3940 goto done;
3942 if (tagmsg_arg == NULL) {
3943 err = get_tag_message(&tagmsg, &tagmsg_path, commit_id_str,
3944 tag_name, got_repo_get_path(repo));
3945 if (err) {
3946 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY &&
3947 tagmsg_path != NULL)
3948 preserve_tagmsg = 1;
3949 goto done;
3953 err = got_object_tag_create(&tag_id, tag_name, commit_id,
3954 tagger, time(NULL), tagmsg ? tagmsg : tagmsg_arg, repo);
3955 if (err) {
3956 if (tagmsg_path)
3957 preserve_tagmsg = 1;
3958 goto done;
3961 err = got_ref_alloc(&ref, refname, tag_id);
3962 if (err) {
3963 if (tagmsg_path)
3964 preserve_tagmsg = 1;
3965 goto done;
3968 err = got_ref_write(ref, repo);
3969 if (err) {
3970 if (tagmsg_path)
3971 preserve_tagmsg = 1;
3972 goto done;
3975 err = got_object_id_str(&tag_id_str, tag_id);
3976 if (err) {
3977 if (tagmsg_path)
3978 preserve_tagmsg = 1;
3979 goto done;
3981 printf("Created tag %s\n", tag_id_str);
3982 done:
3983 if (preserve_tagmsg) {
3984 fprintf(stderr, "%s: tag message preserved in %s\n",
3985 getprogname(), tagmsg_path);
3986 } else if (tagmsg_path && unlink(tagmsg_path) == -1 && err == NULL)
3987 err = got_error_from_errno2("unlink", tagmsg_path);
3988 free(tag_id_str);
3989 if (ref)
3990 got_ref_close(ref);
3991 free(commit_id);
3992 free(commit_id_str);
3993 free(refname);
3994 free(tagmsg);
3995 free(tagmsg_path);
3996 free(tagger);
3997 return err;
4000 static const struct got_error *
4001 cmd_tag(int argc, char *argv[])
4003 const struct got_error *error = NULL;
4004 struct got_repository *repo = NULL;
4005 struct got_worktree *worktree = NULL;
4006 char *cwd = NULL, *repo_path = NULL, *commit_id_str = NULL;
4007 char *gitconfig_path = NULL;
4008 const char *tag_name, *commit_id_arg = NULL, *tagmsg = NULL;
4009 int ch, do_list = 0;
4011 while ((ch = getopt(argc, argv, "m:r:l")) != -1) {
4012 switch (ch) {
4013 case 'm':
4014 tagmsg = optarg;
4015 break;
4016 case 'r':
4017 repo_path = realpath(optarg, NULL);
4018 if (repo_path == NULL)
4019 return got_error_from_errno2("realpath",
4020 optarg);
4021 got_path_strip_trailing_slashes(repo_path);
4022 break;
4023 case 'l':
4024 do_list = 1;
4025 break;
4026 default:
4027 usage_tag();
4028 /* NOTREACHED */
4032 argc -= optind;
4033 argv += optind;
4035 if (do_list) {
4036 if (tagmsg)
4037 errx(1, "-l and -m options are mutually exclusive\n");
4038 if (argc > 0)
4039 usage_tag();
4040 } else if (argc < 1 || argc > 2)
4041 usage_tag();
4042 else if (argc > 1)
4043 commit_id_arg = argv[1];
4044 tag_name = argv[0];
4046 #ifndef PROFILE
4047 if (do_list) {
4048 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
4049 NULL) == -1)
4050 err(1, "pledge");
4051 } else {
4052 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
4053 "sendfd unveil", NULL) == -1)
4054 err(1, "pledge");
4056 #endif
4057 cwd = getcwd(NULL, 0);
4058 if (cwd == NULL) {
4059 error = got_error_from_errno("getcwd");
4060 goto done;
4063 if (repo_path == NULL) {
4064 error = got_worktree_open(&worktree, cwd);
4065 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4066 goto done;
4067 else
4068 error = NULL;
4069 if (worktree) {
4070 repo_path =
4071 strdup(got_worktree_get_repo_path(worktree));
4072 if (repo_path == NULL)
4073 error = got_error_from_errno("strdup");
4074 if (error)
4075 goto done;
4076 } else {
4077 repo_path = strdup(cwd);
4078 if (repo_path == NULL) {
4079 error = got_error_from_errno("strdup");
4080 goto done;
4085 if (do_list) {
4086 error = got_repo_open(&repo, repo_path, NULL);
4087 if (error != NULL)
4088 goto done;
4089 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4090 if (error)
4091 goto done;
4092 error = list_tags(repo, worktree);
4093 } else {
4094 error = get_gitconfig_path(&gitconfig_path);
4095 if (error)
4096 goto done;
4097 error = got_repo_open(&repo, repo_path, gitconfig_path);
4098 if (error != NULL)
4099 goto done;
4101 if (tagmsg) {
4102 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4103 if (error)
4104 goto done;
4107 if (commit_id_arg == NULL) {
4108 struct got_reference *head_ref;
4109 struct got_object_id *commit_id;
4110 error = got_ref_open(&head_ref, repo,
4111 worktree ? got_worktree_get_head_ref_name(worktree)
4112 : GOT_REF_HEAD, 0);
4113 if (error)
4114 goto done;
4115 error = got_ref_resolve(&commit_id, repo, head_ref);
4116 got_ref_close(head_ref);
4117 if (error)
4118 goto done;
4119 error = got_object_id_str(&commit_id_str, commit_id);
4120 free(commit_id);
4121 if (error)
4122 goto done;
4125 error = add_tag(repo, tag_name,
4126 commit_id_str ? commit_id_str : commit_id_arg, tagmsg);
4128 done:
4129 if (repo)
4130 got_repo_close(repo);
4131 if (worktree)
4132 got_worktree_close(worktree);
4133 free(cwd);
4134 free(repo_path);
4135 free(gitconfig_path);
4136 free(commit_id_str);
4137 return error;
4140 __dead static void
4141 usage_add(void)
4143 fprintf(stderr, "usage: %s add file-path ...\n", getprogname());
4144 exit(1);
4147 static const struct got_error *
4148 add_progress(void *arg, unsigned char status, const char *path)
4150 while (path[0] == '/')
4151 path++;
4152 printf("%c %s\n", status, path);
4153 return NULL;
4156 static const struct got_error *
4157 cmd_add(int argc, char *argv[])
4159 const struct got_error *error = NULL;
4160 struct got_repository *repo = NULL;
4161 struct got_worktree *worktree = NULL;
4162 char *cwd = NULL;
4163 struct got_pathlist_head paths;
4164 struct got_pathlist_entry *pe;
4165 int ch, can_recurse = 0;
4167 TAILQ_INIT(&paths);
4169 while ((ch = getopt(argc, argv, "R")) != -1) {
4170 switch (ch) {
4171 case 'R':
4172 can_recurse = 1;
4173 break;
4174 default:
4175 usage_add();
4176 /* NOTREACHED */
4180 argc -= optind;
4181 argv += optind;
4183 #ifndef PROFILE
4184 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4185 NULL) == -1)
4186 err(1, "pledge");
4187 #endif
4188 if (argc < 1)
4189 usage_add();
4191 cwd = getcwd(NULL, 0);
4192 if (cwd == NULL) {
4193 error = got_error_from_errno("getcwd");
4194 goto done;
4197 error = got_worktree_open(&worktree, cwd);
4198 if (error)
4199 goto done;
4201 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4202 NULL);
4203 if (error != NULL)
4204 goto done;
4206 error = apply_unveil(got_repo_get_path(repo), 1,
4207 got_worktree_get_root_path(worktree));
4208 if (error)
4209 goto done;
4211 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4212 if (error)
4213 goto done;
4215 if (!can_recurse) {
4216 char *ondisk_path;
4217 struct stat sb;
4218 TAILQ_FOREACH(pe, &paths, entry) {
4219 if (asprintf(&ondisk_path, "%s/%s",
4220 got_worktree_get_root_path(worktree),
4221 pe->path) == -1) {
4222 error = got_error_from_errno("asprintf");
4223 goto done;
4225 if (lstat(ondisk_path, &sb) == -1) {
4226 if (errno == ENOENT) {
4227 free(ondisk_path);
4228 continue;
4230 error = got_error_from_errno2("lstat",
4231 ondisk_path);
4232 free(ondisk_path);
4233 goto done;
4235 free(ondisk_path);
4236 if (S_ISDIR(sb.st_mode)) {
4237 error = got_error_msg(GOT_ERR_BAD_PATH,
4238 "adding directories requires -R option");
4239 goto done;
4243 error = got_worktree_schedule_add(worktree, &paths, add_progress,
4244 NULL, repo);
4245 done:
4246 if (repo)
4247 got_repo_close(repo);
4248 if (worktree)
4249 got_worktree_close(worktree);
4250 TAILQ_FOREACH(pe, &paths, entry)
4251 free((char *)pe->path);
4252 got_pathlist_free(&paths);
4253 free(cwd);
4254 return error;
4257 __dead static void
4258 usage_remove(void)
4260 fprintf(stderr, "usage: %s remove [-f] file-path ...\n", getprogname());
4261 exit(1);
4264 static const struct got_error *
4265 print_remove_status(void *arg, unsigned char status,
4266 unsigned char staged_status, const char *path,
4267 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4268 struct got_object_id *commit_id)
4270 if (status == GOT_STATUS_NONEXISTENT)
4271 return NULL;
4272 if (status == staged_status && (status == GOT_STATUS_DELETE))
4273 status = GOT_STATUS_NO_CHANGE;
4274 printf("%c%c %s\n", status, staged_status, path);
4275 return NULL;
4278 static const struct got_error *
4279 cmd_remove(int argc, char *argv[])
4281 const struct got_error *error = NULL;
4282 struct got_worktree *worktree = NULL;
4283 struct got_repository *repo = NULL;
4284 char *cwd = NULL;
4285 struct got_pathlist_head paths;
4286 struct got_pathlist_entry *pe;
4287 int ch, delete_local_mods = 0;
4289 TAILQ_INIT(&paths);
4291 while ((ch = getopt(argc, argv, "f")) != -1) {
4292 switch (ch) {
4293 case 'f':
4294 delete_local_mods = 1;
4295 break;
4296 default:
4297 usage_add();
4298 /* NOTREACHED */
4302 argc -= optind;
4303 argv += optind;
4305 #ifndef PROFILE
4306 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4307 NULL) == -1)
4308 err(1, "pledge");
4309 #endif
4310 if (argc < 1)
4311 usage_remove();
4313 cwd = getcwd(NULL, 0);
4314 if (cwd == NULL) {
4315 error = got_error_from_errno("getcwd");
4316 goto done;
4318 error = got_worktree_open(&worktree, cwd);
4319 if (error)
4320 goto done;
4322 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4323 NULL);
4324 if (error)
4325 goto done;
4327 error = apply_unveil(got_repo_get_path(repo), 1,
4328 got_worktree_get_root_path(worktree));
4329 if (error)
4330 goto done;
4332 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4333 if (error)
4334 goto done;
4336 error = got_worktree_schedule_delete(worktree, &paths,
4337 delete_local_mods, print_remove_status, NULL, repo);
4338 if (error)
4339 goto done;
4340 done:
4341 if (repo)
4342 got_repo_close(repo);
4343 if (worktree)
4344 got_worktree_close(worktree);
4345 TAILQ_FOREACH(pe, &paths, entry)
4346 free((char *)pe->path);
4347 got_pathlist_free(&paths);
4348 free(cwd);
4349 return error;
4352 __dead static void
4353 usage_revert(void)
4355 fprintf(stderr, "usage: %s revert [-p] [-F response-script] [-R] "
4356 "path ...\n", getprogname());
4357 exit(1);
4360 static const struct got_error *
4361 revert_progress(void *arg, unsigned char status, const char *path)
4363 while (path[0] == '/')
4364 path++;
4365 printf("%c %s\n", status, path);
4366 return NULL;
4369 struct choose_patch_arg {
4370 FILE *patch_script_file;
4371 const char *action;
4374 static const struct got_error *
4375 show_change(unsigned char status, const char *path, FILE *patch_file, int n,
4376 int nchanges, const char *action)
4378 char *line = NULL;
4379 size_t linesize = 0;
4380 ssize_t linelen;
4382 switch (status) {
4383 case GOT_STATUS_ADD:
4384 printf("A %s\n%s this addition? [y/n] ", path, action);
4385 break;
4386 case GOT_STATUS_DELETE:
4387 printf("D %s\n%s this deletion? [y/n] ", path, action);
4388 break;
4389 case GOT_STATUS_MODIFY:
4390 if (fseek(patch_file, 0L, SEEK_SET) == -1)
4391 return got_error_from_errno("fseek");
4392 printf(GOT_COMMIT_SEP_STR);
4393 while ((linelen = getline(&line, &linesize, patch_file)) != -1)
4394 printf("%s", line);
4395 if (ferror(patch_file))
4396 return got_error_from_errno("getline");
4397 printf(GOT_COMMIT_SEP_STR);
4398 printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
4399 path, n, nchanges, action);
4400 break;
4401 default:
4402 return got_error_path(path, GOT_ERR_FILE_STATUS);
4405 return NULL;
4408 static const struct got_error *
4409 choose_patch(int *choice, void *arg, unsigned char status, const char *path,
4410 FILE *patch_file, int n, int nchanges)
4412 const struct got_error *err = NULL;
4413 char *line = NULL;
4414 size_t linesize = 0;
4415 ssize_t linelen;
4416 int resp = ' ';
4417 struct choose_patch_arg *a = arg;
4419 *choice = GOT_PATCH_CHOICE_NONE;
4421 if (a->patch_script_file) {
4422 char *nl;
4423 err = show_change(status, path, patch_file, n, nchanges,
4424 a->action);
4425 if (err)
4426 return err;
4427 linelen = getline(&line, &linesize, a->patch_script_file);
4428 if (linelen == -1) {
4429 if (ferror(a->patch_script_file))
4430 return got_error_from_errno("getline");
4431 return NULL;
4433 nl = strchr(line, '\n');
4434 if (nl)
4435 *nl = '\0';
4436 if (strcmp(line, "y") == 0) {
4437 *choice = GOT_PATCH_CHOICE_YES;
4438 printf("y\n");
4439 } else if (strcmp(line, "n") == 0) {
4440 *choice = GOT_PATCH_CHOICE_NO;
4441 printf("n\n");
4442 } else if (strcmp(line, "q") == 0 &&
4443 status == GOT_STATUS_MODIFY) {
4444 *choice = GOT_PATCH_CHOICE_QUIT;
4445 printf("q\n");
4446 } else
4447 printf("invalid response '%s'\n", line);
4448 free(line);
4449 return NULL;
4452 while (resp != 'y' && resp != 'n' && resp != 'q') {
4453 err = show_change(status, path, patch_file, n, nchanges,
4454 a->action);
4455 if (err)
4456 return err;
4457 resp = getchar();
4458 if (resp == '\n')
4459 resp = getchar();
4460 if (status == GOT_STATUS_MODIFY) {
4461 if (resp != 'y' && resp != 'n' && resp != 'q') {
4462 printf("invalid response '%c'\n", resp);
4463 resp = ' ';
4465 } else if (resp != 'y' && resp != 'n') {
4466 printf("invalid response '%c'\n", resp);
4467 resp = ' ';
4471 if (resp == 'y')
4472 *choice = GOT_PATCH_CHOICE_YES;
4473 else if (resp == 'n')
4474 *choice = GOT_PATCH_CHOICE_NO;
4475 else if (resp == 'q' && status == GOT_STATUS_MODIFY)
4476 *choice = GOT_PATCH_CHOICE_QUIT;
4478 return NULL;
4482 static const struct got_error *
4483 cmd_revert(int argc, char *argv[])
4485 const struct got_error *error = NULL;
4486 struct got_worktree *worktree = NULL;
4487 struct got_repository *repo = NULL;
4488 char *cwd = NULL, *path = NULL;
4489 struct got_pathlist_head paths;
4490 struct got_pathlist_entry *pe;
4491 int ch, can_recurse = 0, pflag = 0;
4492 FILE *patch_script_file = NULL;
4493 const char *patch_script_path = NULL;
4494 struct choose_patch_arg cpa;
4496 TAILQ_INIT(&paths);
4498 while ((ch = getopt(argc, argv, "pF:R")) != -1) {
4499 switch (ch) {
4500 case 'p':
4501 pflag = 1;
4502 break;
4503 case 'F':
4504 patch_script_path = optarg;
4505 break;
4506 case 'R':
4507 can_recurse = 1;
4508 break;
4509 default:
4510 usage_revert();
4511 /* NOTREACHED */
4515 argc -= optind;
4516 argv += optind;
4518 #ifndef PROFILE
4519 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4520 "unveil", NULL) == -1)
4521 err(1, "pledge");
4522 #endif
4523 if (argc < 1)
4524 usage_revert();
4525 if (patch_script_path && !pflag)
4526 errx(1, "-F option can only be used together with -p option");
4528 cwd = getcwd(NULL, 0);
4529 if (cwd == NULL) {
4530 error = got_error_from_errno("getcwd");
4531 goto done;
4533 error = got_worktree_open(&worktree, cwd);
4534 if (error)
4535 goto done;
4537 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4538 NULL);
4539 if (error != NULL)
4540 goto done;
4542 if (patch_script_path) {
4543 patch_script_file = fopen(patch_script_path, "r");
4544 if (patch_script_file == NULL) {
4545 error = got_error_from_errno2("fopen",
4546 patch_script_path);
4547 goto done;
4550 error = apply_unveil(got_repo_get_path(repo), 1,
4551 got_worktree_get_root_path(worktree));
4552 if (error)
4553 goto done;
4555 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4556 if (error)
4557 goto done;
4559 if (!can_recurse) {
4560 char *ondisk_path;
4561 struct stat sb;
4562 TAILQ_FOREACH(pe, &paths, entry) {
4563 if (asprintf(&ondisk_path, "%s/%s",
4564 got_worktree_get_root_path(worktree),
4565 pe->path) == -1) {
4566 error = got_error_from_errno("asprintf");
4567 goto done;
4569 if (lstat(ondisk_path, &sb) == -1) {
4570 if (errno == ENOENT) {
4571 free(ondisk_path);
4572 continue;
4574 error = got_error_from_errno2("lstat",
4575 ondisk_path);
4576 free(ondisk_path);
4577 goto done;
4579 free(ondisk_path);
4580 if (S_ISDIR(sb.st_mode)) {
4581 error = got_error_msg(GOT_ERR_BAD_PATH,
4582 "reverting directories requires -R option");
4583 goto done;
4588 cpa.patch_script_file = patch_script_file;
4589 cpa.action = "revert";
4590 error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
4591 pflag ? choose_patch : NULL, &cpa, repo);
4592 if (error)
4593 goto done;
4594 done:
4595 if (patch_script_file && fclose(patch_script_file) == EOF &&
4596 error == NULL)
4597 error = got_error_from_errno2("fclose", patch_script_path);
4598 if (repo)
4599 got_repo_close(repo);
4600 if (worktree)
4601 got_worktree_close(worktree);
4602 free(path);
4603 free(cwd);
4604 return error;
4607 __dead static void
4608 usage_commit(void)
4610 fprintf(stderr, "usage: %s commit [-m msg] [path ...]\n",
4611 getprogname());
4612 exit(1);
4615 struct collect_commit_logmsg_arg {
4616 const char *cmdline_log;
4617 const char *editor;
4618 const char *worktree_path;
4619 const char *branch_name;
4620 const char *repo_path;
4621 char *logmsg_path;
4625 static const struct got_error *
4626 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
4627 void *arg)
4629 char *initial_content = NULL;
4630 struct got_pathlist_entry *pe;
4631 const struct got_error *err = NULL;
4632 char *template = NULL;
4633 struct collect_commit_logmsg_arg *a = arg;
4634 int fd;
4635 size_t len;
4637 /* if a message was specified on the command line, just use it */
4638 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
4639 len = strlen(a->cmdline_log) + 1;
4640 *logmsg = malloc(len + 1);
4641 if (*logmsg == NULL)
4642 return got_error_from_errno("malloc");
4643 strlcpy(*logmsg, a->cmdline_log, len);
4644 return NULL;
4647 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
4648 return got_error_from_errno("asprintf");
4650 if (asprintf(&initial_content,
4651 "\n# changes to be committed on branch %s:\n",
4652 a->branch_name) == -1)
4653 return got_error_from_errno("asprintf");
4655 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
4656 if (err)
4657 goto done;
4659 dprintf(fd, initial_content);
4661 TAILQ_FOREACH(pe, commitable_paths, entry) {
4662 struct got_commitable *ct = pe->data;
4663 dprintf(fd, "# %c %s\n",
4664 got_commitable_get_status(ct),
4665 got_commitable_get_path(ct));
4667 close(fd);
4669 err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content);
4670 done:
4671 free(initial_content);
4672 free(template);
4674 /* Editor is done; we can now apply unveil(2) */
4675 if (err == NULL) {
4676 err = apply_unveil(a->repo_path, 0, a->worktree_path);
4677 if (err) {
4678 free(*logmsg);
4679 *logmsg = NULL;
4682 return err;
4685 static const struct got_error *
4686 cmd_commit(int argc, char *argv[])
4688 const struct got_error *error = NULL;
4689 struct got_worktree *worktree = NULL;
4690 struct got_repository *repo = NULL;
4691 char *cwd = NULL, *id_str = NULL;
4692 struct got_object_id *id = NULL;
4693 const char *logmsg = NULL;
4694 struct collect_commit_logmsg_arg cl_arg;
4695 char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
4696 int ch, rebase_in_progress, histedit_in_progress, preserve_logmsg = 0;
4697 struct got_pathlist_head paths;
4699 TAILQ_INIT(&paths);
4700 cl_arg.logmsg_path = NULL;
4702 while ((ch = getopt(argc, argv, "m:")) != -1) {
4703 switch (ch) {
4704 case 'm':
4705 logmsg = optarg;
4706 break;
4707 default:
4708 usage_commit();
4709 /* NOTREACHED */
4713 argc -= optind;
4714 argv += optind;
4716 #ifndef PROFILE
4717 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4718 "unveil", NULL) == -1)
4719 err(1, "pledge");
4720 #endif
4721 cwd = getcwd(NULL, 0);
4722 if (cwd == NULL) {
4723 error = got_error_from_errno("getcwd");
4724 goto done;
4726 error = got_worktree_open(&worktree, cwd);
4727 if (error)
4728 goto done;
4730 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
4731 if (error)
4732 goto done;
4733 if (rebase_in_progress) {
4734 error = got_error(GOT_ERR_REBASING);
4735 goto done;
4738 error = got_worktree_histedit_in_progress(&histedit_in_progress,
4739 worktree);
4740 if (error)
4741 goto done;
4743 error = get_gitconfig_path(&gitconfig_path);
4744 if (error)
4745 goto done;
4746 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4747 gitconfig_path);
4748 if (error != NULL)
4749 goto done;
4751 error = get_author(&author, repo);
4752 if (error)
4753 return error;
4756 * unveil(2) traverses exec(2); if an editor is used we have
4757 * to apply unveil after the log message has been written.
4759 if (logmsg == NULL || strlen(logmsg) == 0)
4760 error = get_editor(&editor);
4761 else
4762 error = apply_unveil(got_repo_get_path(repo), 0,
4763 got_worktree_get_root_path(worktree));
4764 if (error)
4765 goto done;
4767 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4768 if (error)
4769 goto done;
4771 cl_arg.editor = editor;
4772 cl_arg.cmdline_log = logmsg;
4773 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
4774 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
4775 if (!histedit_in_progress) {
4776 if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
4777 error = got_error(GOT_ERR_COMMIT_BRANCH);
4778 goto done;
4780 cl_arg.branch_name += 11;
4782 cl_arg.repo_path = got_repo_get_path(repo);
4783 error = got_worktree_commit(&id, worktree, &paths, author, NULL,
4784 collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
4785 if (error) {
4786 if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
4787 cl_arg.logmsg_path != NULL)
4788 preserve_logmsg = 1;
4789 goto done;
4792 error = got_object_id_str(&id_str, id);
4793 if (error)
4794 goto done;
4795 printf("Created commit %s\n", id_str);
4796 done:
4797 if (preserve_logmsg) {
4798 fprintf(stderr, "%s: log message preserved in %s\n",
4799 getprogname(), cl_arg.logmsg_path);
4800 } else if (cl_arg.logmsg_path && unlink(cl_arg.logmsg_path) == -1 &&
4801 error == NULL)
4802 error = got_error_from_errno2("unlink", cl_arg.logmsg_path);
4803 free(cl_arg.logmsg_path);
4804 if (repo)
4805 got_repo_close(repo);
4806 if (worktree)
4807 got_worktree_close(worktree);
4808 free(cwd);
4809 free(id_str);
4810 free(gitconfig_path);
4811 free(editor);
4812 free(author);
4813 return error;
4816 __dead static void
4817 usage_cherrypick(void)
4819 fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
4820 exit(1);
4823 static const struct got_error *
4824 cmd_cherrypick(int argc, char *argv[])
4826 const struct got_error *error = NULL;
4827 struct got_worktree *worktree = NULL;
4828 struct got_repository *repo = NULL;
4829 char *cwd = NULL, *commit_id_str = NULL;
4830 struct got_object_id *commit_id = NULL;
4831 struct got_commit_object *commit = NULL;
4832 struct got_object_qid *pid;
4833 struct got_reference *head_ref = NULL;
4834 int ch, did_something = 0;
4836 while ((ch = getopt(argc, argv, "")) != -1) {
4837 switch (ch) {
4838 default:
4839 usage_cherrypick();
4840 /* NOTREACHED */
4844 argc -= optind;
4845 argv += optind;
4847 #ifndef PROFILE
4848 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4849 "unveil", NULL) == -1)
4850 err(1, "pledge");
4851 #endif
4852 if (argc != 1)
4853 usage_cherrypick();
4855 cwd = getcwd(NULL, 0);
4856 if (cwd == NULL) {
4857 error = got_error_from_errno("getcwd");
4858 goto done;
4860 error = got_worktree_open(&worktree, cwd);
4861 if (error)
4862 goto done;
4864 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4865 NULL);
4866 if (error != NULL)
4867 goto done;
4869 error = apply_unveil(got_repo_get_path(repo), 0,
4870 got_worktree_get_root_path(worktree));
4871 if (error)
4872 goto done;
4874 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
4875 GOT_OBJ_TYPE_COMMIT, repo);
4876 if (error != NULL) {
4877 struct got_reference *ref;
4878 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
4879 goto done;
4880 error = got_ref_open(&ref, repo, argv[0], 0);
4881 if (error != NULL)
4882 goto done;
4883 error = got_ref_resolve(&commit_id, repo, ref);
4884 got_ref_close(ref);
4885 if (error != NULL)
4886 goto done;
4888 error = got_object_id_str(&commit_id_str, commit_id);
4889 if (error)
4890 goto done;
4892 error = got_ref_open(&head_ref, repo,
4893 got_worktree_get_head_ref_name(worktree), 0);
4894 if (error != NULL)
4895 goto done;
4897 error = check_same_branch(commit_id, head_ref, NULL, repo);
4898 if (error) {
4899 if (error->code != GOT_ERR_ANCESTRY)
4900 goto done;
4901 error = NULL;
4902 } else {
4903 error = got_error(GOT_ERR_SAME_BRANCH);
4904 goto done;
4907 error = got_object_open_as_commit(&commit, repo, commit_id);
4908 if (error)
4909 goto done;
4910 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
4911 error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
4912 commit_id, repo, update_progress, &did_something, check_cancelled,
4913 NULL);
4914 if (error != NULL)
4915 goto done;
4917 if (did_something)
4918 printf("Merged commit %s\n", commit_id_str);
4919 done:
4920 if (commit)
4921 got_object_commit_close(commit);
4922 free(commit_id_str);
4923 if (head_ref)
4924 got_ref_close(head_ref);
4925 if (worktree)
4926 got_worktree_close(worktree);
4927 if (repo)
4928 got_repo_close(repo);
4929 return error;
4932 __dead static void
4933 usage_backout(void)
4935 fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
4936 exit(1);
4939 static const struct got_error *
4940 cmd_backout(int argc, char *argv[])
4942 const struct got_error *error = NULL;
4943 struct got_worktree *worktree = NULL;
4944 struct got_repository *repo = NULL;
4945 char *cwd = NULL, *commit_id_str = NULL;
4946 struct got_object_id *commit_id = NULL;
4947 struct got_commit_object *commit = NULL;
4948 struct got_object_qid *pid;
4949 struct got_reference *head_ref = NULL;
4950 int ch, did_something = 0;
4952 while ((ch = getopt(argc, argv, "")) != -1) {
4953 switch (ch) {
4954 default:
4955 usage_backout();
4956 /* NOTREACHED */
4960 argc -= optind;
4961 argv += optind;
4963 #ifndef PROFILE
4964 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4965 "unveil", NULL) == -1)
4966 err(1, "pledge");
4967 #endif
4968 if (argc != 1)
4969 usage_backout();
4971 cwd = getcwd(NULL, 0);
4972 if (cwd == NULL) {
4973 error = got_error_from_errno("getcwd");
4974 goto done;
4976 error = got_worktree_open(&worktree, cwd);
4977 if (error)
4978 goto done;
4980 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4981 NULL);
4982 if (error != NULL)
4983 goto done;
4985 error = apply_unveil(got_repo_get_path(repo), 0,
4986 got_worktree_get_root_path(worktree));
4987 if (error)
4988 goto done;
4990 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
4991 GOT_OBJ_TYPE_COMMIT, repo);
4992 if (error != NULL) {
4993 struct got_reference *ref;
4994 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
4995 goto done;
4996 error = got_ref_open(&ref, repo, argv[0], 0);
4997 if (error != NULL)
4998 goto done;
4999 error = got_ref_resolve(&commit_id, repo, ref);
5000 got_ref_close(ref);
5001 if (error != NULL)
5002 goto done;
5004 error = got_object_id_str(&commit_id_str, commit_id);
5005 if (error)
5006 goto done;
5008 error = got_ref_open(&head_ref, repo,
5009 got_worktree_get_head_ref_name(worktree), 0);
5010 if (error != NULL)
5011 goto done;
5013 error = check_same_branch(commit_id, head_ref, NULL, repo);
5014 if (error)
5015 goto done;
5017 error = got_object_open_as_commit(&commit, repo, commit_id);
5018 if (error)
5019 goto done;
5020 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
5021 if (pid == NULL) {
5022 error = got_error(GOT_ERR_ROOT_COMMIT);
5023 goto done;
5026 error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
5027 update_progress, &did_something, check_cancelled, NULL);
5028 if (error != NULL)
5029 goto done;
5031 if (did_something)
5032 printf("Backed out commit %s\n", commit_id_str);
5033 done:
5034 if (commit)
5035 got_object_commit_close(commit);
5036 free(commit_id_str);
5037 if (head_ref)
5038 got_ref_close(head_ref);
5039 if (worktree)
5040 got_worktree_close(worktree);
5041 if (repo)
5042 got_repo_close(repo);
5043 return error;
5046 __dead static void
5047 usage_rebase(void)
5049 fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
5050 getprogname());
5051 exit(1);
5054 void
5055 trim_logmsg(char *logmsg, int limit)
5057 char *nl;
5058 size_t len;
5060 len = strlen(logmsg);
5061 if (len > limit)
5062 len = limit;
5063 logmsg[len] = '\0';
5064 nl = strchr(logmsg, '\n');
5065 if (nl)
5066 *nl = '\0';
5069 static const struct got_error *
5070 get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
5072 const struct got_error *err;
5073 char *logmsg0 = NULL;
5074 const char *s;
5076 err = got_object_commit_get_logmsg(&logmsg0, commit);
5077 if (err)
5078 return err;
5080 s = logmsg0;
5081 while (isspace((unsigned char)s[0]))
5082 s++;
5084 *logmsg = strdup(s);
5085 if (*logmsg == NULL) {
5086 err = got_error_from_errno("strdup");
5087 goto done;
5090 trim_logmsg(*logmsg, limit);
5091 done:
5092 free(logmsg0);
5093 return err;
5096 static const struct got_error *
5097 show_rebase_progress(struct got_commit_object *commit,
5098 struct got_object_id *old_id, struct got_object_id *new_id)
5100 const struct got_error *err;
5101 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
5103 err = got_object_id_str(&old_id_str, old_id);
5104 if (err)
5105 goto done;
5107 if (new_id) {
5108 err = got_object_id_str(&new_id_str, new_id);
5109 if (err)
5110 goto done;
5113 old_id_str[12] = '\0';
5114 if (new_id_str)
5115 new_id_str[12] = '\0';
5117 err = get_short_logmsg(&logmsg, 42, commit);
5118 if (err)
5119 goto done;
5121 printf("%s -> %s: %s\n", old_id_str,
5122 new_id_str ? new_id_str : "no-op change", logmsg);
5123 done:
5124 free(old_id_str);
5125 free(new_id_str);
5126 return err;
5129 static const struct got_error *
5130 rebase_progress(void *arg, unsigned char status, const char *path)
5132 unsigned char *rebase_status = arg;
5134 while (path[0] == '/')
5135 path++;
5136 printf("%c %s\n", status, path);
5138 if (*rebase_status == GOT_STATUS_CONFLICT)
5139 return NULL;
5140 if (status == GOT_STATUS_CONFLICT || status == GOT_STATUS_MERGE)
5141 *rebase_status = status;
5142 return NULL;
5145 static const struct got_error *
5146 rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
5147 struct got_reference *branch, struct got_reference *new_base_branch,
5148 struct got_reference *tmp_branch, struct got_repository *repo)
5150 printf("Switching work tree to %s\n", got_ref_get_name(branch));
5151 return got_worktree_rebase_complete(worktree, fileindex,
5152 new_base_branch, tmp_branch, branch, repo);
5155 static const struct got_error *
5156 rebase_commit(struct got_pathlist_head *merged_paths,
5157 struct got_worktree *worktree, struct got_fileindex *fileindex,
5158 struct got_reference *tmp_branch,
5159 struct got_object_id *commit_id, struct got_repository *repo)
5161 const struct got_error *error;
5162 struct got_commit_object *commit;
5163 struct got_object_id *new_commit_id;
5165 error = got_object_open_as_commit(&commit, repo, commit_id);
5166 if (error)
5167 return error;
5169 error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
5170 worktree, fileindex, tmp_branch, commit, commit_id, repo);
5171 if (error) {
5172 if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
5173 goto done;
5174 error = show_rebase_progress(commit, commit_id, NULL);
5175 } else {
5176 error = show_rebase_progress(commit, commit_id, new_commit_id);
5177 free(new_commit_id);
5179 done:
5180 got_object_commit_close(commit);
5181 return error;
5184 struct check_path_prefix_arg {
5185 const char *path_prefix;
5186 size_t len;
5187 int errcode;
5190 static const struct got_error *
5191 check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
5192 struct got_blob_object *blob2, struct got_object_id *id1,
5193 struct got_object_id *id2, const char *path1, const char *path2,
5194 mode_t mode1, mode_t mode2, struct got_repository *repo)
5196 struct check_path_prefix_arg *a = arg;
5198 if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
5199 (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
5200 return got_error(a->errcode);
5202 return NULL;
5205 static const struct got_error *
5206 check_path_prefix(struct got_object_id *parent_id,
5207 struct got_object_id *commit_id, const char *path_prefix,
5208 int errcode, struct got_repository *repo)
5210 const struct got_error *err;
5211 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
5212 struct got_commit_object *commit = NULL, *parent_commit = NULL;
5213 struct check_path_prefix_arg cpp_arg;
5215 if (got_path_is_root_dir(path_prefix))
5216 return NULL;
5218 err = got_object_open_as_commit(&commit, repo, commit_id);
5219 if (err)
5220 goto done;
5222 err = got_object_open_as_commit(&parent_commit, repo, parent_id);
5223 if (err)
5224 goto done;
5226 err = got_object_open_as_tree(&tree1, repo,
5227 got_object_commit_get_tree_id(parent_commit));
5228 if (err)
5229 goto done;
5231 err = got_object_open_as_tree(&tree2, repo,
5232 got_object_commit_get_tree_id(commit));
5233 if (err)
5234 goto done;
5236 cpp_arg.path_prefix = path_prefix;
5237 while (cpp_arg.path_prefix[0] == '/')
5238 cpp_arg.path_prefix++;
5239 cpp_arg.len = strlen(cpp_arg.path_prefix);
5240 cpp_arg.errcode = errcode;
5241 err = got_diff_tree(tree1, tree2, "", "", repo,
5242 check_path_prefix_in_diff, &cpp_arg, 0);
5243 done:
5244 if (tree1)
5245 got_object_tree_close(tree1);
5246 if (tree2)
5247 got_object_tree_close(tree2);
5248 if (commit)
5249 got_object_commit_close(commit);
5250 if (parent_commit)
5251 got_object_commit_close(parent_commit);
5252 return err;
5255 static const struct got_error *
5256 collect_commits(struct got_object_id_queue *commits,
5257 struct got_object_id *initial_commit_id,
5258 struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
5259 const char *path_prefix, int path_prefix_errcode,
5260 struct got_repository *repo)
5262 const struct got_error *err = NULL;
5263 struct got_commit_graph *graph = NULL;
5264 struct got_object_id *parent_id = NULL;
5265 struct got_object_qid *qid;
5266 struct got_object_id *commit_id = initial_commit_id;
5268 err = got_commit_graph_open(&graph, initial_commit_id, "/", 1, repo);
5269 if (err)
5270 return err;
5272 err = got_commit_graph_iter_start(graph, iter_start_id, repo,
5273 check_cancelled, NULL);
5274 if (err)
5275 goto done;
5276 while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
5277 err = got_commit_graph_iter_next(&parent_id, graph);
5278 if (err) {
5279 if (err->code == GOT_ERR_ITER_COMPLETED) {
5280 err = got_error_msg(GOT_ERR_ANCESTRY,
5281 "ran out of commits to rebase before "
5282 "youngest common ancestor commit has "
5283 "been reached?!?");
5284 goto done;
5285 } else if (err->code != GOT_ERR_ITER_NEED_MORE)
5286 goto done;
5287 err = got_commit_graph_fetch_commits(graph, 1, repo,
5288 check_cancelled, NULL);
5289 if (err)
5290 goto done;
5291 } else {
5292 err = check_path_prefix(parent_id, commit_id,
5293 path_prefix, path_prefix_errcode, repo);
5294 if (err)
5295 goto done;
5297 err = got_object_qid_alloc(&qid, commit_id);
5298 if (err)
5299 goto done;
5300 SIMPLEQ_INSERT_HEAD(commits, qid, entry);
5301 commit_id = parent_id;
5304 done:
5305 got_commit_graph_close(graph);
5306 return err;
5309 static const struct got_error *
5310 cmd_rebase(int argc, char *argv[])
5312 const struct got_error *error = NULL;
5313 struct got_worktree *worktree = NULL;
5314 struct got_repository *repo = NULL;
5315 struct got_fileindex *fileindex = NULL;
5316 char *cwd = NULL;
5317 struct got_reference *branch = NULL;
5318 struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
5319 struct got_object_id *commit_id = NULL, *parent_id = NULL;
5320 struct got_object_id *resume_commit_id = NULL;
5321 struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
5322 struct got_commit_object *commit = NULL;
5323 int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
5324 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
5325 struct got_object_id_queue commits;
5326 struct got_pathlist_head merged_paths;
5327 const struct got_object_id_queue *parent_ids;
5328 struct got_object_qid *qid, *pid;
5330 SIMPLEQ_INIT(&commits);
5331 TAILQ_INIT(&merged_paths);
5333 while ((ch = getopt(argc, argv, "ac")) != -1) {
5334 switch (ch) {
5335 case 'a':
5336 abort_rebase = 1;
5337 break;
5338 case 'c':
5339 continue_rebase = 1;
5340 break;
5341 default:
5342 usage_rebase();
5343 /* NOTREACHED */
5347 argc -= optind;
5348 argv += optind;
5350 #ifndef PROFILE
5351 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5352 "unveil", NULL) == -1)
5353 err(1, "pledge");
5354 #endif
5355 if (abort_rebase && continue_rebase)
5356 usage_rebase();
5357 else if (abort_rebase || continue_rebase) {
5358 if (argc != 0)
5359 usage_rebase();
5360 } else if (argc != 1)
5361 usage_rebase();
5363 cwd = getcwd(NULL, 0);
5364 if (cwd == NULL) {
5365 error = got_error_from_errno("getcwd");
5366 goto done;
5368 error = got_worktree_open(&worktree, cwd);
5369 if (error)
5370 goto done;
5372 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5373 NULL);
5374 if (error != NULL)
5375 goto done;
5377 error = apply_unveil(got_repo_get_path(repo), 0,
5378 got_worktree_get_root_path(worktree));
5379 if (error)
5380 goto done;
5382 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
5383 if (error)
5384 goto done;
5386 if (abort_rebase) {
5387 int did_something;
5388 if (!rebase_in_progress) {
5389 error = got_error(GOT_ERR_NOT_REBASING);
5390 goto done;
5392 error = got_worktree_rebase_continue(&resume_commit_id,
5393 &new_base_branch, &tmp_branch, &branch, &fileindex,
5394 worktree, repo);
5395 if (error)
5396 goto done;
5397 printf("Switching work tree to %s\n",
5398 got_ref_get_symref_target(new_base_branch));
5399 error = got_worktree_rebase_abort(worktree, fileindex, repo,
5400 new_base_branch, update_progress, &did_something);
5401 if (error)
5402 goto done;
5403 printf("Rebase of %s aborted\n", got_ref_get_name(branch));
5404 goto done; /* nothing else to do */
5407 if (continue_rebase) {
5408 if (!rebase_in_progress) {
5409 error = got_error(GOT_ERR_NOT_REBASING);
5410 goto done;
5412 error = got_worktree_rebase_continue(&resume_commit_id,
5413 &new_base_branch, &tmp_branch, &branch, &fileindex,
5414 worktree, repo);
5415 if (error)
5416 goto done;
5418 error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
5419 resume_commit_id, repo);
5420 if (error)
5421 goto done;
5423 yca_id = got_object_id_dup(resume_commit_id);
5424 if (yca_id == NULL) {
5425 error = got_error_from_errno("got_object_id_dup");
5426 goto done;
5428 } else {
5429 error = got_ref_open(&branch, repo, argv[0], 0);
5430 if (error != NULL)
5431 goto done;
5434 error = got_ref_resolve(&branch_head_commit_id, repo, branch);
5435 if (error)
5436 goto done;
5438 if (!continue_rebase) {
5439 struct got_object_id *base_commit_id;
5441 base_commit_id = got_worktree_get_base_commit_id(worktree);
5442 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
5443 base_commit_id, branch_head_commit_id, repo,
5444 check_cancelled, NULL);
5445 if (error)
5446 goto done;
5447 if (yca_id == NULL) {
5448 error = got_error_msg(GOT_ERR_ANCESTRY,
5449 "specified branch shares no common ancestry "
5450 "with work tree's branch");
5451 goto done;
5454 error = check_same_branch(base_commit_id, branch, yca_id, repo);
5455 if (error) {
5456 if (error->code != GOT_ERR_ANCESTRY)
5457 goto done;
5458 error = NULL;
5459 } else {
5460 error = got_error_msg(GOT_ERR_SAME_BRANCH,
5461 "specified branch resolves to a commit which "
5462 "is already contained in work tree's branch");
5463 goto done;
5465 error = got_worktree_rebase_prepare(&new_base_branch,
5466 &tmp_branch, &fileindex, worktree, branch, repo);
5467 if (error)
5468 goto done;
5471 commit_id = branch_head_commit_id;
5472 error = got_object_open_as_commit(&commit, repo, commit_id);
5473 if (error)
5474 goto done;
5476 parent_ids = got_object_commit_get_parent_ids(commit);
5477 pid = SIMPLEQ_FIRST(parent_ids);
5478 if (pid == NULL) {
5479 if (!continue_rebase) {
5480 int did_something;
5481 error = got_worktree_rebase_abort(worktree, fileindex,
5482 repo, new_base_branch, update_progress,
5483 &did_something);
5484 if (error)
5485 goto done;
5486 printf("Rebase of %s aborted\n",
5487 got_ref_get_name(branch));
5489 error = got_error(GOT_ERR_EMPTY_REBASE);
5490 goto done;
5492 error = collect_commits(&commits, commit_id, pid->id,
5493 yca_id, got_worktree_get_path_prefix(worktree),
5494 GOT_ERR_REBASE_PATH, repo);
5495 got_object_commit_close(commit);
5496 commit = NULL;
5497 if (error)
5498 goto done;
5500 if (SIMPLEQ_EMPTY(&commits)) {
5501 if (continue_rebase) {
5502 error = rebase_complete(worktree, fileindex,
5503 branch, new_base_branch, tmp_branch, repo);
5504 goto done;
5505 } else {
5506 /* Fast-forward the reference of the branch. */
5507 struct got_object_id *new_head_commit_id;
5508 char *id_str;
5509 error = got_ref_resolve(&new_head_commit_id, repo,
5510 new_base_branch);
5511 if (error)
5512 goto done;
5513 error = got_object_id_str(&id_str, new_head_commit_id);
5514 printf("Forwarding %s to commit %s\n",
5515 got_ref_get_name(branch), id_str);
5516 free(id_str);
5517 error = got_ref_change_ref(branch,
5518 new_head_commit_id);
5519 if (error)
5520 goto done;
5524 pid = NULL;
5525 SIMPLEQ_FOREACH(qid, &commits, entry) {
5526 commit_id = qid->id;
5527 parent_id = pid ? pid->id : yca_id;
5528 pid = qid;
5530 error = got_worktree_rebase_merge_files(&merged_paths,
5531 worktree, fileindex, parent_id, commit_id, repo,
5532 rebase_progress, &rebase_status, check_cancelled, NULL);
5533 if (error)
5534 goto done;
5536 if (rebase_status == GOT_STATUS_CONFLICT) {
5537 got_worktree_rebase_pathlist_free(&merged_paths);
5538 break;
5541 error = rebase_commit(&merged_paths, worktree, fileindex,
5542 tmp_branch, commit_id, repo);
5543 got_worktree_rebase_pathlist_free(&merged_paths);
5544 if (error)
5545 goto done;
5548 if (rebase_status == GOT_STATUS_CONFLICT) {
5549 error = got_worktree_rebase_postpone(worktree, fileindex);
5550 if (error)
5551 goto done;
5552 error = got_error_msg(GOT_ERR_CONFLICTS,
5553 "conflicts must be resolved before rebasing can continue");
5554 } else
5555 error = rebase_complete(worktree, fileindex, branch,
5556 new_base_branch, tmp_branch, repo);
5557 done:
5558 got_object_id_queue_free(&commits);
5559 free(branch_head_commit_id);
5560 free(resume_commit_id);
5561 free(yca_id);
5562 if (commit)
5563 got_object_commit_close(commit);
5564 if (branch)
5565 got_ref_close(branch);
5566 if (new_base_branch)
5567 got_ref_close(new_base_branch);
5568 if (tmp_branch)
5569 got_ref_close(tmp_branch);
5570 if (worktree)
5571 got_worktree_close(worktree);
5572 if (repo)
5573 got_repo_close(repo);
5574 return error;
5577 __dead static void
5578 usage_histedit(void)
5580 fprintf(stderr, "usage: %s histedit [-a] [-c] [-F histedit-script]\n",
5581 getprogname());
5582 exit(1);
5585 #define GOT_HISTEDIT_PICK 'p'
5586 #define GOT_HISTEDIT_EDIT 'e'
5587 #define GOT_HISTEDIT_FOLD 'f'
5588 #define GOT_HISTEDIT_DROP 'd'
5589 #define GOT_HISTEDIT_MESG 'm'
5591 static struct got_histedit_cmd {
5592 unsigned char code;
5593 const char *name;
5594 const char *desc;
5595 } got_histedit_cmds[] = {
5596 { GOT_HISTEDIT_PICK, "pick", "use commit" },
5597 { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
5598 { GOT_HISTEDIT_FOLD, "fold", "combine with commit below" },
5599 { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
5600 { GOT_HISTEDIT_MESG, "mesg",
5601 "single-line log message for commit above (open editor if empty)" },
5604 struct got_histedit_list_entry {
5605 TAILQ_ENTRY(got_histedit_list_entry) entry;
5606 struct got_object_id *commit_id;
5607 const struct got_histedit_cmd *cmd;
5608 char *logmsg;
5610 TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
5612 static const struct got_error *
5613 histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
5614 FILE *f, struct got_repository *repo)
5616 const struct got_error *err = NULL;
5617 char *logmsg = NULL, *id_str = NULL;
5618 struct got_commit_object *commit = NULL;
5619 int n;
5621 err = got_object_open_as_commit(&commit, repo, commit_id);
5622 if (err)
5623 goto done;
5625 err = get_short_logmsg(&logmsg, 34, commit);
5626 if (err)
5627 goto done;
5629 err = got_object_id_str(&id_str, commit_id);
5630 if (err)
5631 goto done;
5633 n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
5634 if (n < 0)
5635 err = got_ferror(f, GOT_ERR_IO);
5636 done:
5637 if (commit)
5638 got_object_commit_close(commit);
5639 free(id_str);
5640 free(logmsg);
5641 return err;
5644 static const struct got_error *
5645 histedit_write_commit_list(struct got_object_id_queue *commits, FILE *f,
5646 struct got_repository *repo)
5648 const struct got_error *err = NULL;
5649 struct got_object_qid *qid;
5651 if (SIMPLEQ_EMPTY(commits))
5652 return got_error(GOT_ERR_EMPTY_HISTEDIT);
5654 SIMPLEQ_FOREACH(qid, commits, entry) {
5655 err = histedit_write_commit(qid->id, got_histedit_cmds[0].name,
5656 f, repo);
5657 if (err)
5658 break;
5661 return err;
5664 static const struct got_error *
5665 write_cmd_list(FILE *f)
5667 const struct got_error *err = NULL;
5668 int n, i;
5670 n = fprintf(f, "# Available histedit commands:\n");
5671 if (n < 0)
5672 return got_ferror(f, GOT_ERR_IO);
5674 for (i = 0; i < nitems(got_histedit_cmds); i++) {
5675 struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
5676 n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
5677 cmd->desc);
5678 if (n < 0) {
5679 err = got_ferror(f, GOT_ERR_IO);
5680 break;
5683 n = fprintf(f, "# Commits will be processed in order from top to "
5684 "bottom of this file.\n");
5685 if (n < 0)
5686 return got_ferror(f, GOT_ERR_IO);
5687 return err;
5690 static const struct got_error *
5691 histedit_syntax_error(int lineno)
5693 static char msg[42];
5694 int ret;
5696 ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
5697 lineno);
5698 if (ret == -1 || ret >= sizeof(msg))
5699 return got_error(GOT_ERR_HISTEDIT_SYNTAX);
5701 return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
5704 static const struct got_error *
5705 append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
5706 char *logmsg, struct got_repository *repo)
5708 const struct got_error *err;
5709 struct got_commit_object *folded_commit = NULL;
5710 char *id_str, *folded_logmsg = NULL;
5712 err = got_object_id_str(&id_str, hle->commit_id);
5713 if (err)
5714 return err;
5716 err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
5717 if (err)
5718 goto done;
5720 err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
5721 if (err)
5722 goto done;
5723 if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
5724 logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
5725 folded_logmsg) == -1) {
5726 err = got_error_from_errno("asprintf");
5727 goto done;
5729 done:
5730 if (folded_commit)
5731 got_object_commit_close(folded_commit);
5732 free(id_str);
5733 free(folded_logmsg);
5734 return err;
5737 static struct got_histedit_list_entry *
5738 get_folded_commits(struct got_histedit_list_entry *hle)
5740 struct got_histedit_list_entry *prev, *folded = NULL;
5742 prev = TAILQ_PREV(hle, got_histedit_list, entry);
5743 while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
5744 prev->cmd->code == GOT_HISTEDIT_DROP)) {
5745 if (prev->cmd->code == GOT_HISTEDIT_FOLD)
5746 folded = prev;
5747 prev = TAILQ_PREV(prev, got_histedit_list, entry);
5750 return folded;
5753 static const struct got_error *
5754 histedit_edit_logmsg(struct got_histedit_list_entry *hle,
5755 struct got_repository *repo)
5757 char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
5758 char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
5759 const struct got_error *err = NULL;
5760 struct got_commit_object *commit = NULL;
5761 int fd;
5762 struct got_histedit_list_entry *folded = NULL;
5764 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
5765 if (err)
5766 return err;
5768 folded = get_folded_commits(hle);
5769 if (folded) {
5770 while (folded != hle) {
5771 if (folded->cmd->code == GOT_HISTEDIT_DROP) {
5772 folded = TAILQ_NEXT(folded, entry);
5773 continue;
5775 err = append_folded_commit_msg(&new_msg, folded,
5776 logmsg, repo);
5777 if (err)
5778 goto done;
5779 free(logmsg);
5780 logmsg = new_msg;
5781 folded = TAILQ_NEXT(folded, entry);
5785 err = got_object_id_str(&id_str, hle->commit_id);
5786 if (err)
5787 goto done;
5788 err = got_object_commit_get_logmsg(&orig_logmsg, commit);
5789 if (err)
5790 goto done;
5791 if (asprintf(&new_msg,
5792 "%s\n# original log message of commit %s: %s",
5793 logmsg ? logmsg : "", id_str, orig_logmsg) == -1) {
5794 err = got_error_from_errno("asprintf");
5795 goto done;
5797 free(logmsg);
5798 logmsg = new_msg;
5800 err = got_object_id_str(&id_str, hle->commit_id);
5801 if (err)
5802 goto done;
5804 err = got_opentemp_named_fd(&logmsg_path, &fd, "/tmp/got-logmsg");
5805 if (err)
5806 goto done;
5808 dprintf(fd, logmsg);
5809 close(fd);
5811 err = get_editor(&editor);
5812 if (err)
5813 goto done;
5815 err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg);
5816 if (err) {
5817 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
5818 goto done;
5819 err = got_object_commit_get_logmsg(&hle->logmsg, commit);
5821 done:
5822 if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
5823 err = got_error_from_errno2("unlink", logmsg_path);
5824 free(logmsg_path);
5825 free(logmsg);
5826 free(orig_logmsg);
5827 free(editor);
5828 if (commit)
5829 got_object_commit_close(commit);
5830 return err;
5833 static const struct got_error *
5834 histedit_parse_list(struct got_histedit_list *histedit_cmds,
5835 FILE *f, struct got_repository *repo)
5837 const struct got_error *err = NULL;
5838 char *line = NULL, *p, *end;
5839 size_t size;
5840 ssize_t len;
5841 int lineno = 0, i;
5842 const struct got_histedit_cmd *cmd;
5843 struct got_object_id *commit_id = NULL;
5844 struct got_histedit_list_entry *hle = NULL;
5846 for (;;) {
5847 len = getline(&line, &size, f);
5848 if (len == -1) {
5849 const struct got_error *getline_err;
5850 if (feof(f))
5851 break;
5852 getline_err = got_error_from_errno("getline");
5853 err = got_ferror(f, getline_err->code);
5854 break;
5856 lineno++;
5857 p = line;
5858 while (isspace((unsigned char)p[0]))
5859 p++;
5860 if (p[0] == '#' || p[0] == '\0') {
5861 free(line);
5862 line = NULL;
5863 continue;
5865 cmd = NULL;
5866 for (i = 0; i < nitems(got_histedit_cmds); i++) {
5867 cmd = &got_histedit_cmds[i];
5868 if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
5869 isspace((unsigned char)p[strlen(cmd->name)])) {
5870 p += strlen(cmd->name);
5871 break;
5873 if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
5874 p++;
5875 break;
5878 if (i == nitems(got_histedit_cmds)) {
5879 err = histedit_syntax_error(lineno);
5880 break;
5882 while (isspace((unsigned char)p[0]))
5883 p++;
5884 if (cmd->code == GOT_HISTEDIT_MESG) {
5885 if (hle == NULL || hle->logmsg != NULL) {
5886 err = got_error(GOT_ERR_HISTEDIT_CMD);
5887 break;
5889 if (p[0] == '\0') {
5890 err = histedit_edit_logmsg(hle, repo);
5891 if (err)
5892 break;
5893 } else {
5894 hle->logmsg = strdup(p);
5895 if (hle->logmsg == NULL) {
5896 err = got_error_from_errno("strdup");
5897 break;
5900 free(line);
5901 line = NULL;
5902 continue;
5903 } else {
5904 end = p;
5905 while (end[0] && !isspace((unsigned char)end[0]))
5906 end++;
5907 *end = '\0';
5909 err = got_object_resolve_id_str(&commit_id, repo, p);
5910 if (err) {
5911 /* override error code */
5912 err = histedit_syntax_error(lineno);
5913 break;
5916 hle = malloc(sizeof(*hle));
5917 if (hle == NULL) {
5918 err = got_error_from_errno("malloc");
5919 break;
5921 hle->cmd = cmd;
5922 hle->commit_id = commit_id;
5923 hle->logmsg = NULL;
5924 commit_id = NULL;
5925 free(line);
5926 line = NULL;
5927 TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
5930 free(line);
5931 free(commit_id);
5932 return err;
5935 static const struct got_error *
5936 histedit_check_script(struct got_histedit_list *histedit_cmds,
5937 struct got_object_id_queue *commits, struct got_repository *repo)
5939 const struct got_error *err = NULL;
5940 struct got_object_qid *qid;
5941 struct got_histedit_list_entry *hle;
5942 static char msg[80];
5943 char *id_str;
5945 if (TAILQ_EMPTY(histedit_cmds))
5946 return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
5947 "histedit script contains no commands");
5948 if (SIMPLEQ_EMPTY(commits))
5949 return got_error(GOT_ERR_EMPTY_HISTEDIT);
5951 SIMPLEQ_FOREACH(qid, commits, entry) {
5952 TAILQ_FOREACH(hle, histedit_cmds, entry) {
5953 if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
5954 break;
5956 if (hle == NULL) {
5957 err = got_object_id_str(&id_str, qid->id);
5958 if (err)
5959 return err;
5960 snprintf(msg, sizeof(msg),
5961 "commit %s missing from histedit script", id_str);
5962 free(id_str);
5963 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
5967 hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
5968 if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
5969 return got_error_msg(GOT_ERR_HISTEDIT_CMD,
5970 "last commit in histedit script cannot be folded");
5972 return NULL;
5975 static const struct got_error *
5976 histedit_run_editor(struct got_histedit_list *histedit_cmds,
5977 const char *path, struct got_object_id_queue *commits,
5978 struct got_repository *repo)
5980 const struct got_error *err = NULL;
5981 char *editor;
5982 FILE *f = NULL;
5984 err = get_editor(&editor);
5985 if (err)
5986 return err;
5988 if (spawn_editor(editor, path) == -1) {
5989 err = got_error_from_errno("failed spawning editor");
5990 goto done;
5993 f = fopen(path, "r");
5994 if (f == NULL) {
5995 err = got_error_from_errno("fopen");
5996 goto done;
5998 err = histedit_parse_list(histedit_cmds, f, repo);
5999 if (err)
6000 goto done;
6002 err = histedit_check_script(histedit_cmds, commits, repo);
6003 done:
6004 if (f && fclose(f) != 0 && err == NULL)
6005 err = got_error_from_errno("fclose");
6006 free(editor);
6007 return err;
6010 static const struct got_error *
6011 histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
6012 struct got_object_id_queue *, const char *, struct got_repository *);
6014 static const struct got_error *
6015 histedit_edit_script(struct got_histedit_list *histedit_cmds,
6016 struct got_object_id_queue *commits, struct got_repository *repo)
6018 const struct got_error *err;
6019 FILE *f = NULL;
6020 char *path = NULL;
6022 err = got_opentemp_named(&path, &f, "got-histedit");
6023 if (err)
6024 return err;
6026 err = write_cmd_list(f);
6027 if (err)
6028 goto done;
6030 err = histedit_write_commit_list(commits, f, repo);
6031 if (err)
6032 goto done;
6034 if (fclose(f) != 0) {
6035 err = got_error_from_errno("fclose");
6036 goto done;
6038 f = NULL;
6040 err = histedit_run_editor(histedit_cmds, path, commits, repo);
6041 if (err) {
6042 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6043 err->code != GOT_ERR_HISTEDIT_CMD)
6044 goto done;
6045 err = histedit_edit_list_retry(histedit_cmds, err,
6046 commits, path, repo);
6048 done:
6049 if (f && fclose(f) != 0 && err == NULL)
6050 err = got_error_from_errno("fclose");
6051 if (path && unlink(path) != 0 && err == NULL)
6052 err = got_error_from_errno2("unlink", path);
6053 free(path);
6054 return err;
6057 static const struct got_error *
6058 histedit_save_list(struct got_histedit_list *histedit_cmds,
6059 struct got_worktree *worktree, struct got_repository *repo)
6061 const struct got_error *err = NULL;
6062 char *path = NULL;
6063 FILE *f = NULL;
6064 struct got_histedit_list_entry *hle;
6065 struct got_commit_object *commit = NULL;
6067 err = got_worktree_get_histedit_script_path(&path, worktree);
6068 if (err)
6069 return err;
6071 f = fopen(path, "w");
6072 if (f == NULL) {
6073 err = got_error_from_errno2("fopen", path);
6074 goto done;
6076 TAILQ_FOREACH(hle, histedit_cmds, entry) {
6077 err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
6078 repo);
6079 if (err)
6080 break;
6082 if (hle->logmsg) {
6083 int n = fprintf(f, "%c %s\n",
6084 GOT_HISTEDIT_MESG, hle->logmsg);
6085 if (n < 0) {
6086 err = got_ferror(f, GOT_ERR_IO);
6087 break;
6091 done:
6092 if (f && fclose(f) != 0 && err == NULL)
6093 err = got_error_from_errno("fclose");
6094 free(path);
6095 if (commit)
6096 got_object_commit_close(commit);
6097 return err;
6100 void
6101 histedit_free_list(struct got_histedit_list *histedit_cmds)
6103 struct got_histedit_list_entry *hle;
6105 while ((hle = TAILQ_FIRST(histedit_cmds))) {
6106 TAILQ_REMOVE(histedit_cmds, hle, entry);
6107 free(hle);
6111 static const struct got_error *
6112 histedit_load_list(struct got_histedit_list *histedit_cmds,
6113 const char *path, struct got_repository *repo)
6115 const struct got_error *err = NULL;
6116 FILE *f = NULL;
6118 f = fopen(path, "r");
6119 if (f == NULL) {
6120 err = got_error_from_errno2("fopen", path);
6121 goto done;
6124 err = histedit_parse_list(histedit_cmds, f, repo);
6125 done:
6126 if (f && fclose(f) != 0 && err == NULL)
6127 err = got_error_from_errno("fclose");
6128 return err;
6131 static const struct got_error *
6132 histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
6133 const struct got_error *edit_err, struct got_object_id_queue *commits,
6134 const char *path, struct got_repository *repo)
6136 const struct got_error *err = NULL, *prev_err = edit_err;
6137 int resp = ' ';
6139 while (resp != 'c' && resp != 'r' && resp != 'a') {
6140 printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
6141 "or (a)bort: ", getprogname(), prev_err->msg);
6142 resp = getchar();
6143 if (resp == '\n')
6144 resp = getchar();
6145 if (resp == 'c') {
6146 histedit_free_list(histedit_cmds);
6147 err = histedit_run_editor(histedit_cmds, path, commits,
6148 repo);
6149 if (err) {
6150 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6151 err->code != GOT_ERR_HISTEDIT_CMD)
6152 break;
6153 prev_err = err;
6154 resp = ' ';
6155 continue;
6157 break;
6158 } else if (resp == 'r') {
6159 histedit_free_list(histedit_cmds);
6160 err = histedit_edit_script(histedit_cmds,
6161 commits, repo);
6162 if (err) {
6163 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6164 err->code != GOT_ERR_HISTEDIT_CMD)
6165 break;
6166 prev_err = err;
6167 resp = ' ';
6168 continue;
6170 break;
6171 } else if (resp == 'a') {
6172 err = got_error(GOT_ERR_HISTEDIT_CANCEL);
6173 break;
6174 } else
6175 printf("invalid response '%c'\n", resp);
6178 return err;
6181 static const struct got_error *
6182 histedit_complete(struct got_worktree *worktree,
6183 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6184 struct got_reference *branch, struct got_repository *repo)
6186 printf("Switching work tree to %s\n",
6187 got_ref_get_symref_target(branch));
6188 return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
6189 branch, repo);
6192 static const struct got_error *
6193 show_histedit_progress(struct got_commit_object *commit,
6194 struct got_histedit_list_entry *hle, struct got_object_id *new_id)
6196 const struct got_error *err;
6197 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
6199 err = got_object_id_str(&old_id_str, hle->commit_id);
6200 if (err)
6201 goto done;
6203 if (new_id) {
6204 err = got_object_id_str(&new_id_str, new_id);
6205 if (err)
6206 goto done;
6209 old_id_str[12] = '\0';
6210 if (new_id_str)
6211 new_id_str[12] = '\0';
6213 if (hle->logmsg) {
6214 logmsg = strdup(hle->logmsg);
6215 if (logmsg == NULL) {
6216 err = got_error_from_errno("strdup");
6217 goto done;
6219 trim_logmsg(logmsg, 42);
6220 } else {
6221 err = get_short_logmsg(&logmsg, 42, commit);
6222 if (err)
6223 goto done;
6226 switch (hle->cmd->code) {
6227 case GOT_HISTEDIT_PICK:
6228 case GOT_HISTEDIT_EDIT:
6229 printf("%s -> %s: %s\n", old_id_str,
6230 new_id_str ? new_id_str : "no-op change", logmsg);
6231 break;
6232 case GOT_HISTEDIT_DROP:
6233 case GOT_HISTEDIT_FOLD:
6234 printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
6235 logmsg);
6236 break;
6237 default:
6238 break;
6241 done:
6242 free(old_id_str);
6243 free(new_id_str);
6244 return err;
6247 static const struct got_error *
6248 histedit_commit(struct got_pathlist_head *merged_paths,
6249 struct got_worktree *worktree, struct got_fileindex *fileindex,
6250 struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
6251 struct got_repository *repo)
6253 const struct got_error *err;
6254 struct got_commit_object *commit;
6255 struct got_object_id *new_commit_id;
6257 if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
6258 && hle->logmsg == NULL) {
6259 err = histedit_edit_logmsg(hle, repo);
6260 if (err)
6261 return err;
6264 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
6265 if (err)
6266 return err;
6268 err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
6269 worktree, fileindex, tmp_branch, commit, hle->commit_id,
6270 hle->logmsg, repo);
6271 if (err) {
6272 if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
6273 goto done;
6274 err = show_histedit_progress(commit, hle, NULL);
6275 } else {
6276 err = show_histedit_progress(commit, hle, new_commit_id);
6277 free(new_commit_id);
6279 done:
6280 got_object_commit_close(commit);
6281 return err;
6284 static const struct got_error *
6285 histedit_skip_commit(struct got_histedit_list_entry *hle,
6286 struct got_worktree *worktree, struct got_repository *repo)
6288 const struct got_error *error;
6289 struct got_commit_object *commit;
6291 error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
6292 repo);
6293 if (error)
6294 return error;
6296 error = got_object_open_as_commit(&commit, repo, hle->commit_id);
6297 if (error)
6298 return error;
6300 error = show_histedit_progress(commit, hle, NULL);
6301 got_object_commit_close(commit);
6302 return error;
6305 static const struct got_error *
6306 cmd_histedit(int argc, char *argv[])
6308 const struct got_error *error = NULL;
6309 struct got_worktree *worktree = NULL;
6310 struct got_fileindex *fileindex = NULL;
6311 struct got_repository *repo = NULL;
6312 char *cwd = NULL;
6313 struct got_reference *branch = NULL;
6314 struct got_reference *tmp_branch = NULL;
6315 struct got_object_id *resume_commit_id = NULL;
6316 struct got_object_id *base_commit_id = NULL;
6317 struct got_object_id *head_commit_id = NULL;
6318 struct got_commit_object *commit = NULL;
6319 int ch, rebase_in_progress = 0, did_something;
6320 int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
6321 const char *edit_script_path = NULL;
6322 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
6323 struct got_object_id_queue commits;
6324 struct got_pathlist_head merged_paths;
6325 const struct got_object_id_queue *parent_ids;
6326 struct got_object_qid *pid;
6327 struct got_histedit_list histedit_cmds;
6328 struct got_histedit_list_entry *hle;
6330 SIMPLEQ_INIT(&commits);
6331 TAILQ_INIT(&histedit_cmds);
6332 TAILQ_INIT(&merged_paths);
6334 while ((ch = getopt(argc, argv, "acF:")) != -1) {
6335 switch (ch) {
6336 case 'a':
6337 abort_edit = 1;
6338 break;
6339 case 'c':
6340 continue_edit = 1;
6341 break;
6342 case 'F':
6343 edit_script_path = optarg;
6344 break;
6345 default:
6346 usage_histedit();
6347 /* NOTREACHED */
6351 argc -= optind;
6352 argv += optind;
6354 #ifndef PROFILE
6355 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6356 "unveil", NULL) == -1)
6357 err(1, "pledge");
6358 #endif
6359 if (abort_edit && continue_edit)
6360 usage_histedit();
6361 if (argc != 0)
6362 usage_histedit();
6365 * This command cannot apply unveil(2) in all cases because the
6366 * user may choose to run an editor to edit the histedit script
6367 * and to edit individual commit log messages.
6368 * unveil(2) traverses exec(2); if an editor is used we have to
6369 * apply unveil after edit script and log messages have been written.
6370 * XXX TODO: Make use of unveil(2) where possible.
6373 cwd = getcwd(NULL, 0);
6374 if (cwd == NULL) {
6375 error = got_error_from_errno("getcwd");
6376 goto done;
6378 error = got_worktree_open(&worktree, cwd);
6379 if (error)
6380 goto done;
6382 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6383 NULL);
6384 if (error != NULL)
6385 goto done;
6387 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
6388 if (error)
6389 goto done;
6390 if (rebase_in_progress) {
6391 error = got_error(GOT_ERR_REBASING);
6392 goto done;
6395 error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
6396 if (error)
6397 goto done;
6399 if (edit_in_progress && abort_edit) {
6400 error = got_worktree_histedit_continue(&resume_commit_id,
6401 &tmp_branch, &branch, &base_commit_id, &fileindex,
6402 worktree, repo);
6403 if (error)
6404 goto done;
6405 printf("Switching work tree to %s\n",
6406 got_ref_get_symref_target(branch));
6407 error = got_worktree_histedit_abort(worktree, fileindex, repo,
6408 branch, base_commit_id, update_progress, &did_something);
6409 if (error)
6410 goto done;
6411 printf("Histedit of %s aborted\n",
6412 got_ref_get_symref_target(branch));
6413 goto done; /* nothing else to do */
6414 } else if (abort_edit) {
6415 error = got_error(GOT_ERR_NOT_HISTEDIT);
6416 goto done;
6419 if (continue_edit) {
6420 char *path;
6422 if (!edit_in_progress) {
6423 error = got_error(GOT_ERR_NOT_HISTEDIT);
6424 goto done;
6427 error = got_worktree_get_histedit_script_path(&path, worktree);
6428 if (error)
6429 goto done;
6431 error = histedit_load_list(&histedit_cmds, path, repo);
6432 free(path);
6433 if (error)
6434 goto done;
6436 error = got_worktree_histedit_continue(&resume_commit_id,
6437 &tmp_branch, &branch, &base_commit_id, &fileindex,
6438 worktree, repo);
6439 if (error)
6440 goto done;
6442 error = got_ref_resolve(&head_commit_id, repo, branch);
6443 if (error)
6444 goto done;
6446 error = got_object_open_as_commit(&commit, repo,
6447 head_commit_id);
6448 if (error)
6449 goto done;
6450 parent_ids = got_object_commit_get_parent_ids(commit);
6451 pid = SIMPLEQ_FIRST(parent_ids);
6452 if (pid == NULL) {
6453 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
6454 goto done;
6456 error = collect_commits(&commits, head_commit_id, pid->id,
6457 base_commit_id, got_worktree_get_path_prefix(worktree),
6458 GOT_ERR_HISTEDIT_PATH, repo);
6459 got_object_commit_close(commit);
6460 commit = NULL;
6461 if (error)
6462 goto done;
6463 } else {
6464 if (edit_in_progress) {
6465 error = got_error(GOT_ERR_HISTEDIT_BUSY);
6466 goto done;
6469 error = got_ref_open(&branch, repo,
6470 got_worktree_get_head_ref_name(worktree), 0);
6471 if (error != NULL)
6472 goto done;
6474 if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
6475 error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
6476 "will not edit commit history of a branch outside "
6477 "the \"refs/heads/\" reference namespace");
6478 goto done;
6481 error = got_ref_resolve(&head_commit_id, repo, branch);
6482 got_ref_close(branch);
6483 branch = NULL;
6484 if (error)
6485 goto done;
6487 error = got_object_open_as_commit(&commit, repo,
6488 head_commit_id);
6489 if (error)
6490 goto done;
6491 parent_ids = got_object_commit_get_parent_ids(commit);
6492 pid = SIMPLEQ_FIRST(parent_ids);
6493 if (pid == NULL) {
6494 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
6495 goto done;
6497 error = collect_commits(&commits, head_commit_id, pid->id,
6498 got_worktree_get_base_commit_id(worktree),
6499 got_worktree_get_path_prefix(worktree),
6500 GOT_ERR_HISTEDIT_PATH, repo);
6501 got_object_commit_close(commit);
6502 commit = NULL;
6503 if (error)
6504 goto done;
6506 error = got_worktree_histedit_prepare(&tmp_branch, &branch,
6507 &base_commit_id, &fileindex, worktree, repo);
6508 if (error)
6509 goto done;
6511 if (edit_script_path) {
6512 error = histedit_load_list(&histedit_cmds,
6513 edit_script_path, repo);
6514 if (error) {
6515 got_worktree_histedit_abort(worktree, fileindex,
6516 repo, branch, base_commit_id,
6517 update_progress, &did_something);
6518 goto done;
6520 } else {
6521 error = histedit_edit_script(&histedit_cmds, &commits,
6522 repo);
6523 if (error) {
6524 got_worktree_histedit_abort(worktree, fileindex,
6525 repo, branch, base_commit_id,
6526 update_progress, &did_something);
6527 goto done;
6532 error = histedit_save_list(&histedit_cmds, worktree,
6533 repo);
6534 if (error) {
6535 got_worktree_histedit_abort(worktree, fileindex,
6536 repo, branch, base_commit_id,
6537 update_progress, &did_something);
6538 goto done;
6543 error = histedit_check_script(&histedit_cmds, &commits, repo);
6544 if (error)
6545 goto done;
6547 TAILQ_FOREACH(hle, &histedit_cmds, entry) {
6548 if (resume_commit_id) {
6549 if (got_object_id_cmp(hle->commit_id,
6550 resume_commit_id) != 0)
6551 continue;
6553 resume_commit_id = NULL;
6554 if (hle->cmd->code == GOT_HISTEDIT_DROP ||
6555 hle->cmd->code == GOT_HISTEDIT_FOLD) {
6556 error = histedit_skip_commit(hle, worktree,
6557 repo);
6558 } else {
6559 error = histedit_commit(NULL, worktree,
6560 fileindex, tmp_branch, hle, repo);
6562 if (error)
6563 goto done;
6564 continue;
6567 if (hle->cmd->code == GOT_HISTEDIT_DROP) {
6568 error = histedit_skip_commit(hle, worktree, repo);
6569 if (error)
6570 goto done;
6571 continue;
6574 error = got_object_open_as_commit(&commit, repo,
6575 hle->commit_id);
6576 if (error)
6577 goto done;
6578 parent_ids = got_object_commit_get_parent_ids(commit);
6579 pid = SIMPLEQ_FIRST(parent_ids);
6581 error = got_worktree_histedit_merge_files(&merged_paths,
6582 worktree, fileindex, pid->id, hle->commit_id, repo,
6583 rebase_progress, &rebase_status, check_cancelled, NULL);
6584 if (error)
6585 goto done;
6586 got_object_commit_close(commit);
6587 commit = NULL;
6589 if (rebase_status == GOT_STATUS_CONFLICT) {
6590 got_worktree_rebase_pathlist_free(&merged_paths);
6591 break;
6594 if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
6595 char *id_str;
6596 error = got_object_id_str(&id_str, hle->commit_id);
6597 if (error)
6598 goto done;
6599 printf("Stopping histedit for amending commit %s\n",
6600 id_str);
6601 free(id_str);
6602 got_worktree_rebase_pathlist_free(&merged_paths);
6603 error = got_worktree_histedit_postpone(worktree,
6604 fileindex);
6605 goto done;
6608 if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
6609 error = histedit_skip_commit(hle, worktree, repo);
6610 if (error)
6611 goto done;
6612 continue;
6615 error = histedit_commit(&merged_paths, worktree, fileindex,
6616 tmp_branch, hle, repo);
6617 got_worktree_rebase_pathlist_free(&merged_paths);
6618 if (error)
6619 goto done;
6622 if (rebase_status == GOT_STATUS_CONFLICT) {
6623 error = got_worktree_histedit_postpone(worktree, fileindex);
6624 if (error)
6625 goto done;
6626 error = got_error_msg(GOT_ERR_CONFLICTS,
6627 "conflicts must be resolved before rebasing can continue");
6628 } else
6629 error = histedit_complete(worktree, fileindex, tmp_branch,
6630 branch, repo);
6631 done:
6632 got_object_id_queue_free(&commits);
6633 histedit_free_list(&histedit_cmds);
6634 free(head_commit_id);
6635 free(base_commit_id);
6636 free(resume_commit_id);
6637 if (commit)
6638 got_object_commit_close(commit);
6639 if (branch)
6640 got_ref_close(branch);
6641 if (tmp_branch)
6642 got_ref_close(tmp_branch);
6643 if (worktree)
6644 got_worktree_close(worktree);
6645 if (repo)
6646 got_repo_close(repo);
6647 return error;
6650 __dead static void
6651 usage_integrate(void)
6653 fprintf(stderr, "usage: %s integrate branch\n", getprogname());
6654 exit(1);
6657 static const struct got_error *
6658 cmd_integrate(int argc, char *argv[])
6660 const struct got_error *error = NULL;
6661 struct got_repository *repo = NULL;
6662 struct got_worktree *worktree = NULL;
6663 char *cwd = NULL, *refname = NULL, *base_refname = NULL;
6664 const char *branch_arg = NULL;
6665 struct got_reference *branch_ref = NULL, *base_branch_ref = NULL;
6666 struct got_fileindex *fileindex = NULL;
6667 struct got_object_id *commit_id = NULL, *base_commit_id = NULL;
6668 int ch, did_something = 0;
6670 while ((ch = getopt(argc, argv, "")) != -1) {
6671 switch (ch) {
6672 default:
6673 usage_integrate();
6674 /* NOTREACHED */
6678 argc -= optind;
6679 argv += optind;
6681 if (argc != 1)
6682 usage_integrate();
6683 branch_arg = argv[0];
6685 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6686 "unveil", NULL) == -1)
6687 err(1, "pledge");
6689 cwd = getcwd(NULL, 0);
6690 if (cwd == NULL) {
6691 error = got_error_from_errno("getcwd");
6692 goto done;
6695 error = got_worktree_open(&worktree, cwd);
6696 if (error)
6697 goto done;
6699 error = check_rebase_or_histedit_in_progress(worktree);
6700 if (error)
6701 goto done;
6703 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6704 NULL);
6705 if (error != NULL)
6706 goto done;
6708 error = apply_unveil(got_repo_get_path(repo), 0,
6709 got_worktree_get_root_path(worktree));
6710 if (error)
6711 goto done;
6713 if (asprintf(&refname, "refs/heads/%s", branch_arg) == -1) {
6714 error = got_error_from_errno("asprintf");
6715 goto done;
6718 error = got_worktree_integrate_prepare(&fileindex, &branch_ref,
6719 &base_branch_ref, worktree, refname, repo);
6720 if (error)
6721 goto done;
6723 refname = strdup(got_ref_get_name(branch_ref));
6724 if (refname == NULL) {
6725 error = got_error_from_errno("strdup");
6726 got_worktree_integrate_abort(worktree, fileindex, repo,
6727 branch_ref, base_branch_ref);
6728 goto done;
6730 base_refname = strdup(got_ref_get_name(base_branch_ref));
6731 if (base_refname == NULL) {
6732 error = got_error_from_errno("strdup");
6733 got_worktree_integrate_abort(worktree, fileindex, repo,
6734 branch_ref, base_branch_ref);
6735 goto done;
6738 error = got_ref_resolve(&commit_id, repo, branch_ref);
6739 if (error)
6740 goto done;
6742 error = got_ref_resolve(&base_commit_id, repo, base_branch_ref);
6743 if (error)
6744 goto done;
6746 if (got_object_id_cmp(commit_id, base_commit_id) == 0) {
6747 error = got_error_msg(GOT_ERR_SAME_BRANCH,
6748 "specified branch has already been integrated");
6749 got_worktree_integrate_abort(worktree, fileindex, repo,
6750 branch_ref, base_branch_ref);
6751 goto done;
6754 error = check_linear_ancestry(commit_id, base_commit_id, 1, repo);
6755 if (error) {
6756 if (error->code == GOT_ERR_ANCESTRY)
6757 error = got_error(GOT_ERR_REBASE_REQUIRED);
6758 got_worktree_integrate_abort(worktree, fileindex, repo,
6759 branch_ref, base_branch_ref);
6760 goto done;
6763 error = got_worktree_integrate_continue(worktree, fileindex, repo,
6764 branch_ref, base_branch_ref, update_progress, &did_something,
6765 check_cancelled, NULL);
6766 if (error)
6767 goto done;
6769 printf("Integrated %s into %s\n", refname, base_refname);
6770 done:
6771 if (repo)
6772 got_repo_close(repo);
6773 if (worktree)
6774 got_worktree_close(worktree);
6775 free(cwd);
6776 free(base_commit_id);
6777 free(commit_id);
6778 free(refname);
6779 free(base_refname);
6780 return error;
6783 __dead static void
6784 usage_stage(void)
6786 fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
6787 "[file-path ...]\n",
6788 getprogname());
6789 exit(1);
6792 static const struct got_error *
6793 print_stage(void *arg, unsigned char status, unsigned char staged_status,
6794 const char *path, struct got_object_id *blob_id,
6795 struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
6797 const struct got_error *err = NULL;
6798 char *id_str = NULL;
6800 if (staged_status != GOT_STATUS_ADD &&
6801 staged_status != GOT_STATUS_MODIFY &&
6802 staged_status != GOT_STATUS_DELETE)
6803 return NULL;
6805 if (staged_status == GOT_STATUS_ADD ||
6806 staged_status == GOT_STATUS_MODIFY)
6807 err = got_object_id_str(&id_str, staged_blob_id);
6808 else
6809 err = got_object_id_str(&id_str, blob_id);
6810 if (err)
6811 return err;
6813 printf("%s %c %s\n", id_str, staged_status, path);
6814 free(id_str);
6815 return NULL;
6818 static const struct got_error *
6819 cmd_stage(int argc, char *argv[])
6821 const struct got_error *error = NULL;
6822 struct got_repository *repo = NULL;
6823 struct got_worktree *worktree = NULL;
6824 char *cwd = NULL;
6825 struct got_pathlist_head paths;
6826 struct got_pathlist_entry *pe;
6827 int ch, list_stage = 0, pflag = 0;
6828 FILE *patch_script_file = NULL;
6829 const char *patch_script_path = NULL;
6830 struct choose_patch_arg cpa;
6832 TAILQ_INIT(&paths);
6834 while ((ch = getopt(argc, argv, "lpF:")) != -1) {
6835 switch (ch) {
6836 case 'l':
6837 list_stage = 1;
6838 break;
6839 case 'p':
6840 pflag = 1;
6841 break;
6842 case 'F':
6843 patch_script_path = optarg;
6844 break;
6845 default:
6846 usage_stage();
6847 /* NOTREACHED */
6851 argc -= optind;
6852 argv += optind;
6854 #ifndef PROFILE
6855 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6856 "unveil", NULL) == -1)
6857 err(1, "pledge");
6858 #endif
6859 if (list_stage && (pflag || patch_script_path))
6860 errx(1, "-l option cannot be used with other options");
6861 if (patch_script_path && !pflag)
6862 errx(1, "-F option can only be used together with -p option");
6864 cwd = getcwd(NULL, 0);
6865 if (cwd == NULL) {
6866 error = got_error_from_errno("getcwd");
6867 goto done;
6870 error = got_worktree_open(&worktree, cwd);
6871 if (error)
6872 goto done;
6874 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6875 NULL);
6876 if (error != NULL)
6877 goto done;
6879 if (patch_script_path) {
6880 patch_script_file = fopen(patch_script_path, "r");
6881 if (patch_script_file == NULL) {
6882 error = got_error_from_errno2("fopen",
6883 patch_script_path);
6884 goto done;
6887 error = apply_unveil(got_repo_get_path(repo), 0,
6888 got_worktree_get_root_path(worktree));
6889 if (error)
6890 goto done;
6892 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6893 if (error)
6894 goto done;
6896 if (list_stage)
6897 error = got_worktree_status(worktree, &paths, repo,
6898 print_stage, NULL, check_cancelled, NULL);
6899 else {
6900 cpa.patch_script_file = patch_script_file;
6901 cpa.action = "stage";
6902 error = got_worktree_stage(worktree, &paths,
6903 pflag ? NULL : print_status, NULL,
6904 pflag ? choose_patch : NULL, &cpa, repo);
6906 done:
6907 if (patch_script_file && fclose(patch_script_file) == EOF &&
6908 error == NULL)
6909 error = got_error_from_errno2("fclose", patch_script_path);
6910 if (repo)
6911 got_repo_close(repo);
6912 if (worktree)
6913 got_worktree_close(worktree);
6914 TAILQ_FOREACH(pe, &paths, entry)
6915 free((char *)pe->path);
6916 got_pathlist_free(&paths);
6917 free(cwd);
6918 return error;
6921 __dead static void
6922 usage_unstage(void)
6924 fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
6925 "[file-path ...]\n",
6926 getprogname());
6927 exit(1);
6931 static const struct got_error *
6932 cmd_unstage(int argc, char *argv[])
6934 const struct got_error *error = NULL;
6935 struct got_repository *repo = NULL;
6936 struct got_worktree *worktree = NULL;
6937 char *cwd = NULL;
6938 struct got_pathlist_head paths;
6939 struct got_pathlist_entry *pe;
6940 int ch, did_something = 0, pflag = 0;
6941 FILE *patch_script_file = NULL;
6942 const char *patch_script_path = NULL;
6943 struct choose_patch_arg cpa;
6945 TAILQ_INIT(&paths);
6947 while ((ch = getopt(argc, argv, "pF:")) != -1) {
6948 switch (ch) {
6949 case 'p':
6950 pflag = 1;
6951 break;
6952 case 'F':
6953 patch_script_path = optarg;
6954 break;
6955 default:
6956 usage_unstage();
6957 /* NOTREACHED */
6961 argc -= optind;
6962 argv += optind;
6964 #ifndef PROFILE
6965 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6966 "unveil", NULL) == -1)
6967 err(1, "pledge");
6968 #endif
6969 if (patch_script_path && !pflag)
6970 errx(1, "-F option can only be used together with -p option");
6972 cwd = getcwd(NULL, 0);
6973 if (cwd == NULL) {
6974 error = got_error_from_errno("getcwd");
6975 goto done;
6978 error = got_worktree_open(&worktree, cwd);
6979 if (error)
6980 goto done;
6982 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6983 NULL);
6984 if (error != NULL)
6985 goto done;
6987 if (patch_script_path) {
6988 patch_script_file = fopen(patch_script_path, "r");
6989 if (patch_script_file == NULL) {
6990 error = got_error_from_errno2("fopen",
6991 patch_script_path);
6992 goto done;
6996 error = apply_unveil(got_repo_get_path(repo), 0,
6997 got_worktree_get_root_path(worktree));
6998 if (error)
6999 goto done;
7001 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7002 if (error)
7003 goto done;
7005 cpa.patch_script_file = patch_script_file;
7006 cpa.action = "unstage";
7007 error = got_worktree_unstage(worktree, &paths, update_progress,
7008 &did_something, pflag ? choose_patch : NULL, &cpa, repo);
7009 done:
7010 if (patch_script_file && fclose(patch_script_file) == EOF &&
7011 error == NULL)
7012 error = got_error_from_errno2("fclose", patch_script_path);
7013 if (repo)
7014 got_repo_close(repo);
7015 if (worktree)
7016 got_worktree_close(worktree);
7017 TAILQ_FOREACH(pe, &paths, entry)
7018 free((char *)pe->path);
7019 got_pathlist_free(&paths);
7020 free(cwd);
7021 return error;
7024 __dead static void
7025 usage_cat(void)
7027 fprintf(stderr, "usage: %s cat [-r repository ] [ -c commit ] [ -P ] "
7028 "arg1 [arg2 ...]\n", getprogname());
7029 exit(1);
7032 static const struct got_error *
7033 cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7035 const struct got_error *err;
7036 struct got_blob_object *blob;
7038 err = got_object_open_as_blob(&blob, repo, id, 8192);
7039 if (err)
7040 return err;
7042 err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
7043 got_object_blob_close(blob);
7044 return err;
7047 static const struct got_error *
7048 cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7050 const struct got_error *err;
7051 struct got_tree_object *tree;
7052 int nentries, i;
7054 err = got_object_open_as_tree(&tree, repo, id);
7055 if (err)
7056 return err;
7058 nentries = got_object_tree_get_nentries(tree);
7059 for (i = 0; i < nentries; i++) {
7060 struct got_tree_entry *te;
7061 char *id_str;
7062 if (sigint_received || sigpipe_received)
7063 break;
7064 te = got_object_tree_get_entry(tree, i);
7065 err = got_object_id_str(&id_str, got_tree_entry_get_id(te));
7066 if (err)
7067 break;
7068 fprintf(outfile, "%s %.7o %s\n", id_str,
7069 got_tree_entry_get_mode(te),
7070 got_tree_entry_get_name(te));
7071 free(id_str);
7074 got_object_tree_close(tree);
7075 return err;
7078 static const struct got_error *
7079 cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7081 const struct got_error *err;
7082 struct got_commit_object *commit;
7083 const struct got_object_id_queue *parent_ids;
7084 struct got_object_qid *pid;
7085 char *id_str = NULL;
7086 const char *logmsg = NULL;
7088 err = got_object_open_as_commit(&commit, repo, id);
7089 if (err)
7090 return err;
7092 err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
7093 if (err)
7094 goto done;
7096 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str);
7097 parent_ids = got_object_commit_get_parent_ids(commit);
7098 fprintf(outfile, "numparents %d\n",
7099 got_object_commit_get_nparents(commit));
7100 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
7101 char *pid_str;
7102 err = got_object_id_str(&pid_str, pid->id);
7103 if (err)
7104 goto done;
7105 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_PARENT, pid_str);
7106 free(pid_str);
7108 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_AUTHOR,
7109 got_object_commit_get_author(commit),
7110 got_object_commit_get_author_time(commit));
7112 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_COMMITTER,
7113 got_object_commit_get_author(commit),
7114 got_object_commit_get_committer_time(commit));
7116 logmsg = got_object_commit_get_logmsg_raw(commit);
7117 fprintf(outfile, "messagelen %zd\n", strlen(logmsg));
7118 fprintf(outfile, "%s", logmsg);
7119 done:
7120 free(id_str);
7121 got_object_commit_close(commit);
7122 return err;
7125 static const struct got_error *
7126 cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7128 const struct got_error *err;
7129 struct got_tag_object *tag;
7130 char *id_str = NULL;
7131 const char *tagmsg = NULL;
7133 err = got_object_open_as_tag(&tag, repo, id);
7134 if (err)
7135 return err;
7137 err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
7138 if (err)
7139 goto done;
7141 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str);
7143 switch (got_object_tag_get_object_type(tag)) {
7144 case GOT_OBJ_TYPE_BLOB:
7145 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7146 GOT_OBJ_LABEL_BLOB);
7147 break;
7148 case GOT_OBJ_TYPE_TREE:
7149 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7150 GOT_OBJ_LABEL_TREE);
7151 break;
7152 case GOT_OBJ_TYPE_COMMIT:
7153 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7154 GOT_OBJ_LABEL_COMMIT);
7155 break;
7156 case GOT_OBJ_TYPE_TAG:
7157 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7158 GOT_OBJ_LABEL_TAG);
7159 break;
7160 default:
7161 break;
7164 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TAG,
7165 got_object_tag_get_name(tag));
7167 fprintf(outfile, "%s%s %lld +0000\n", GOT_TAG_LABEL_TAGGER,
7168 got_object_tag_get_tagger(tag),
7169 got_object_tag_get_tagger_time(tag));
7171 tagmsg = got_object_tag_get_message(tag);
7172 fprintf(outfile, "messagelen %zd\n", strlen(tagmsg));
7173 fprintf(outfile, "%s", tagmsg);
7174 done:
7175 free(id_str);
7176 got_object_tag_close(tag);
7177 return err;
7180 static const struct got_error *
7181 cmd_cat(int argc, char *argv[])
7183 const struct got_error *error;
7184 struct got_repository *repo = NULL;
7185 struct got_worktree *worktree = NULL;
7186 char *cwd = NULL, *repo_path = NULL, *label = NULL;
7187 const char *commit_id_str = NULL;
7188 struct got_object_id *id = NULL, *commit_id = NULL;
7189 int ch, obj_type, i, force_path = 0;
7191 #ifndef PROFILE
7192 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
7193 NULL) == -1)
7194 err(1, "pledge");
7195 #endif
7197 while ((ch = getopt(argc, argv, "c:r:P")) != -1) {
7198 switch (ch) {
7199 case 'c':
7200 commit_id_str = optarg;
7201 break;
7202 case 'r':
7203 repo_path = realpath(optarg, NULL);
7204 if (repo_path == NULL)
7205 return got_error_from_errno2("realpath",
7206 optarg);
7207 got_path_strip_trailing_slashes(repo_path);
7208 break;
7209 case 'P':
7210 force_path = 1;
7211 break;
7212 default:
7213 usage_cat();
7214 /* NOTREACHED */
7218 argc -= optind;
7219 argv += optind;
7221 cwd = getcwd(NULL, 0);
7222 if (cwd == NULL) {
7223 error = got_error_from_errno("getcwd");
7224 goto done;
7226 error = got_worktree_open(&worktree, cwd);
7227 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7228 goto done;
7229 if (worktree) {
7230 if (repo_path == NULL) {
7231 repo_path = strdup(
7232 got_worktree_get_repo_path(worktree));
7233 if (repo_path == NULL) {
7234 error = got_error_from_errno("strdup");
7235 goto done;
7240 if (repo_path == NULL) {
7241 repo_path = getcwd(NULL, 0);
7242 if (repo_path == NULL)
7243 return got_error_from_errno("getcwd");
7246 error = got_repo_open(&repo, repo_path, NULL);
7247 free(repo_path);
7248 if (error != NULL)
7249 goto done;
7251 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
7252 if (error)
7253 goto done;
7255 if (commit_id_str == NULL)
7256 commit_id_str = GOT_REF_HEAD;
7257 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
7258 if (error)
7259 goto done;
7261 for (i = 0; i < argc; i++) {
7262 if (force_path) {
7263 error = got_object_id_by_path(&id, repo, commit_id,
7264 argv[i]);
7265 if (error)
7266 break;
7267 } else {
7268 error = match_object_id(&id, &label, argv[i],
7269 GOT_OBJ_TYPE_ANY, 0, repo);
7270 if (error) {
7271 if (error->code != GOT_ERR_BAD_OBJ_ID_STR &&
7272 error->code != GOT_ERR_NOT_REF)
7273 break;
7274 error = got_object_id_by_path(&id, repo,
7275 commit_id, argv[i]);
7276 if (error)
7277 break;
7281 error = got_object_get_type(&obj_type, repo, id);
7282 if (error)
7283 break;
7285 switch (obj_type) {
7286 case GOT_OBJ_TYPE_BLOB:
7287 error = cat_blob(id, repo, stdout);
7288 break;
7289 case GOT_OBJ_TYPE_TREE:
7290 error = cat_tree(id, repo, stdout);
7291 break;
7292 case GOT_OBJ_TYPE_COMMIT:
7293 error = cat_commit(id, repo, stdout);
7294 break;
7295 case GOT_OBJ_TYPE_TAG:
7296 error = cat_tag(id, repo, stdout);
7297 break;
7298 default:
7299 error = got_error(GOT_ERR_OBJ_TYPE);
7300 break;
7302 if (error)
7303 break;
7304 free(label);
7305 label = NULL;
7306 free(id);
7307 id = NULL;
7310 done:
7311 free(label);
7312 free(id);
7313 free(commit_id);
7314 if (worktree)
7315 got_worktree_close(worktree);
7316 if (repo) {
7317 const struct got_error *repo_error;
7318 repo_error = got_repo_close(repo);
7319 if (error == NULL)
7320 error = repo_error;
7322 return error;