2 f42b1b34 2018-03-12 stsp * Copyright (c) 2017 Martin Pieuchot <mpi@openbsd.org>
3 5d56da81 2019-01-13 stsp * Copyright (c) 2018, 2019 Stefan Sperling <stsp@openbsd.org>
5 5c860e29 2018-03-12 stsp * Permission to use, copy, modify, and distribute this software for any
6 5c860e29 2018-03-12 stsp * purpose with or without fee is hereby granted, provided that the above
7 5c860e29 2018-03-12 stsp * copyright notice and this permission notice appear in all copies.
9 5c860e29 2018-03-12 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 5c860e29 2018-03-12 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 5c860e29 2018-03-12 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 5c860e29 2018-03-12 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 5c860e29 2018-03-12 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 5c860e29 2018-03-12 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 5c860e29 2018-03-12 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 f42b1b34 2018-03-12 stsp #include <sys/queue.h>
19 c0768b0f 2018-06-10 stsp #include <sys/types.h>
20 5de5890b 2018-10-18 stsp #include <sys/stat.h>
21 3c45a30a 2019-05-12 jcs #include <sys/param.h>
22 33ad4cbe 2019-05-12 jcs #include <sys/wait.h>
24 5c860e29 2018-03-12 stsp #include <err.h>
25 5c860e29 2018-03-12 stsp #include <errno.h>
26 12ce7a6c 2019-08-12 stsp #include <limits.h>
27 5c860e29 2018-03-12 stsp #include <locale.h>
28 818c7501 2019-07-11 stsp #include <ctype.h>
29 99437157 2018-11-11 stsp #include <signal.h>
30 5c860e29 2018-03-12 stsp #include <stdio.h>
31 5c860e29 2018-03-12 stsp #include <stdlib.h>
32 5c860e29 2018-03-12 stsp #include <string.h>
33 5c860e29 2018-03-12 stsp #include <unistd.h>
34 c09a553d 2018-03-12 stsp #include <libgen.h>
35 c0768b0f 2018-06-10 stsp #include <time.h>
36 33ad4cbe 2019-05-12 jcs #include <paths.h>
38 53ccebc2 2019-07-30 stsp #include "got_version.h"
39 f42b1b34 2018-03-12 stsp #include "got_error.h"
40 f42b1b34 2018-03-12 stsp #include "got_object.h"
41 5261c201 2018-04-01 stsp #include "got_reference.h"
42 f42b1b34 2018-03-12 stsp #include "got_repository.h"
43 1dd54920 2019-05-11 stsp #include "got_path.h"
44 e6209546 2019-08-22 stsp #include "got_cancel.h"
45 c09a553d 2018-03-12 stsp #include "got_worktree.h"
46 79109fed 2018-03-27 stsp #include "got_diff.h"
47 372ccdbb 2018-06-10 stsp #include "got_commit_graph.h"
48 404c43c4 2018-06-21 stsp #include "got_blame.h"
49 63219cd2 2019-01-04 stsp #include "got_privsep.h"
50 793c30b5 2019-05-13 stsp #include "got_opentemp.h"
52 5c860e29 2018-03-12 stsp #ifndef nitems
53 5c860e29 2018-03-12 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
56 99437157 2018-11-11 stsp static volatile sig_atomic_t sigint_received;
57 99437157 2018-11-11 stsp static volatile sig_atomic_t sigpipe_received;
60 99437157 2018-11-11 stsp catch_sigint(int signo)
62 99437157 2018-11-11 stsp sigint_received = 1;
66 99437157 2018-11-11 stsp catch_sigpipe(int signo)
68 99437157 2018-11-11 stsp sigpipe_received = 1;
72 8cfb4057 2019-07-09 stsp struct got_cmd {
73 5c860e29 2018-03-12 stsp const char *cmd_name;
74 d7d4f210 2018-03-12 stsp const struct got_error *(*cmd_main)(int, char *[]);
75 1b6b95a8 2018-03-12 stsp void (*cmd_usage)(void);
76 97b3a7be 2019-07-09 stsp const char *cmd_alias;
79 ce5b7c56 2019-07-09 stsp __dead static void usage(int);
80 2c7829a4 2019-06-17 stsp __dead static void usage_init(void);
81 3ce1b845 2019-07-15 stsp __dead static void usage_import(void);
82 4ed7e80c 2018-05-20 stsp __dead static void usage_checkout(void);
83 507dc3bb 2018-12-29 stsp __dead static void usage_update(void);
84 4ed7e80c 2018-05-20 stsp __dead static void usage_log(void);
85 4ed7e80c 2018-05-20 stsp __dead static void usage_diff(void);
86 404c43c4 2018-06-21 stsp __dead static void usage_blame(void);
87 5de5890b 2018-10-18 stsp __dead static void usage_tree(void);
88 6bad629b 2019-02-04 stsp __dead static void usage_status(void);
89 d0eebce4 2019-03-11 stsp __dead static void usage_ref(void);
90 4e759de4 2019-06-26 stsp __dead static void usage_branch(void);
91 8e7bd50a 2019-08-22 stsp __dead static void usage_tag(void);
92 d00136be 2019-03-26 stsp __dead static void usage_add(void);
93 648e4ef7 2019-07-09 stsp __dead static void usage_remove(void);
94 a129376b 2019-03-28 stsp __dead static void usage_revert(void);
95 c4296144 2019-05-09 stsp __dead static void usage_commit(void);
96 234035bc 2019-06-01 stsp __dead static void usage_cherrypick(void);
97 5ef14e63 2019-06-02 stsp __dead static void usage_backout(void);
98 818c7501 2019-07-11 stsp __dead static void usage_rebase(void);
99 0ebf8283 2019-07-24 stsp __dead static void usage_histedit(void);
100 715dc77e 2019-08-03 stsp __dead static void usage_stage(void);
101 ad493afc 2019-08-03 stsp __dead static void usage_unstage(void);
102 01073a5d 2019-08-22 stsp __dead static void usage_cat(void);
104 2c7829a4 2019-06-17 stsp static const struct got_error* cmd_init(int, char *[]);
105 3ce1b845 2019-07-15 stsp static const struct got_error* cmd_import(int, char *[]);
106 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_checkout(int, char *[]);
107 507dc3bb 2018-12-29 stsp static const struct got_error* cmd_update(int, char *[]);
108 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_log(int, char *[]);
109 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_diff(int, char *[]);
110 404c43c4 2018-06-21 stsp static const struct got_error* cmd_blame(int, char *[]);
111 5de5890b 2018-10-18 stsp static const struct got_error* cmd_tree(int, char *[]);
112 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_status(int, char *[]);
113 d0eebce4 2019-03-11 stsp static const struct got_error* cmd_ref(int, char *[]);
114 4e759de4 2019-06-26 stsp static const struct got_error* cmd_branch(int, char *[]);
115 8e7bd50a 2019-08-22 stsp static const struct got_error* cmd_tag(int, char *[]);
116 d00136be 2019-03-26 stsp static const struct got_error* cmd_add(int, char *[]);
117 648e4ef7 2019-07-09 stsp static const struct got_error* cmd_remove(int, char *[]);
118 a129376b 2019-03-28 stsp static const struct got_error* cmd_revert(int, char *[]);
119 c4296144 2019-05-09 stsp static const struct got_error* cmd_commit(int, char *[]);
120 234035bc 2019-06-01 stsp static const struct got_error* cmd_cherrypick(int, char *[]);
121 5ef14e63 2019-06-02 stsp static const struct got_error* cmd_backout(int, char *[]);
122 818c7501 2019-07-11 stsp static const struct got_error* cmd_rebase(int, char *[]);
123 0ebf8283 2019-07-24 stsp static const struct got_error* cmd_histedit(int, char *[]);
124 715dc77e 2019-08-03 stsp static const struct got_error* cmd_stage(int, char *[]);
125 ad493afc 2019-08-03 stsp static const struct got_error* cmd_unstage(int, char *[]);
126 01073a5d 2019-08-22 stsp static const struct got_error* cmd_cat(int, char *[]);
128 8cfb4057 2019-07-09 stsp static struct got_cmd got_commands[] = {
129 bc26cce8 2019-08-04 stsp { "init", cmd_init, usage_init, "in" },
130 bc26cce8 2019-08-04 stsp { "import", cmd_import, usage_import, "im" },
131 97b3a7be 2019-07-09 stsp { "checkout", cmd_checkout, usage_checkout, "co" },
132 97b3a7be 2019-07-09 stsp { "update", cmd_update, usage_update, "up" },
133 97b3a7be 2019-07-09 stsp { "log", cmd_log, usage_log, "" },
134 bc26cce8 2019-08-04 stsp { "diff", cmd_diff, usage_diff, "di" },
135 bc26cce8 2019-08-04 stsp { "blame", cmd_blame, usage_blame, "bl" },
136 bc26cce8 2019-08-04 stsp { "tree", cmd_tree, usage_tree, "tr" },
137 97b3a7be 2019-07-09 stsp { "status", cmd_status, usage_status, "st" },
138 97b3a7be 2019-07-09 stsp { "ref", cmd_ref, usage_ref, "" },
139 97b3a7be 2019-07-09 stsp { "branch", cmd_branch, usage_branch, "br" },
140 8e7bd50a 2019-08-22 stsp { "tag", cmd_tag, usage_tag, "" },
141 97b3a7be 2019-07-09 stsp { "add", cmd_add, usage_add, "" },
142 648e4ef7 2019-07-09 stsp { "remove", cmd_remove, usage_remove, "rm" },
143 97b3a7be 2019-07-09 stsp { "revert", cmd_revert, usage_revert, "rv" },
144 97b3a7be 2019-07-09 stsp { "commit", cmd_commit, usage_commit, "ci" },
145 016477fd 2019-07-09 stsp { "cherrypick", cmd_cherrypick, usage_cherrypick, "cy" },
146 97b3a7be 2019-07-09 stsp { "backout", cmd_backout, usage_backout, "bo" },
147 818c7501 2019-07-11 stsp { "rebase", cmd_rebase, usage_rebase, "rb" },
148 0ebf8283 2019-07-24 stsp { "histedit", cmd_histedit, usage_histedit, "he" },
149 715dc77e 2019-08-03 stsp { "stage", cmd_stage, usage_stage, "sg" },
150 ad493afc 2019-08-03 stsp { "unstage", cmd_unstage, usage_unstage, "ug" },
151 01073a5d 2019-08-22 stsp { "cat", cmd_cat, usage_cat, "" },
154 ce5b7c56 2019-07-09 stsp static void
155 ce5b7c56 2019-07-09 stsp list_commands(void)
159 ce5b7c56 2019-07-09 stsp fprintf(stderr, "commands:");
160 ce5b7c56 2019-07-09 stsp for (i = 0; i < nitems(got_commands); i++) {
161 ce5b7c56 2019-07-09 stsp struct got_cmd *cmd = &got_commands[i];
162 ce5b7c56 2019-07-09 stsp fprintf(stderr, " %s", cmd->cmd_name);
164 ce5b7c56 2019-07-09 stsp fputc('\n', stderr);
168 5c860e29 2018-03-12 stsp main(int argc, char *argv[])
170 8cfb4057 2019-07-09 stsp struct got_cmd *cmd;
171 5c860e29 2018-03-12 stsp unsigned int i;
173 53ccebc2 2019-07-30 stsp int hflag = 0, Vflag = 0;
175 289e3cbf 2019-02-04 stsp setlocale(LC_CTYPE, "");
177 53ccebc2 2019-07-30 stsp while ((ch = getopt(argc, argv, "hV")) != -1) {
178 5c860e29 2018-03-12 stsp switch (ch) {
186 ce5b7c56 2019-07-09 stsp usage(hflag);
187 5c860e29 2018-03-12 stsp /* NOTREACHED */
191 5c860e29 2018-03-12 stsp argc -= optind;
192 5c860e29 2018-03-12 stsp argv += optind;
193 1e70621d 2018-03-27 stsp optind = 0;
195 53ccebc2 2019-07-30 stsp if (Vflag) {
196 53ccebc2 2019-07-30 stsp got_version_print_str();
200 5c860e29 2018-03-12 stsp if (argc <= 0)
201 ce5b7c56 2019-07-09 stsp usage(hflag);
203 99437157 2018-11-11 stsp signal(SIGINT, catch_sigint);
204 99437157 2018-11-11 stsp signal(SIGPIPE, catch_sigpipe);
206 5c860e29 2018-03-12 stsp for (i = 0; i < nitems(got_commands); i++) {
207 d7d4f210 2018-03-12 stsp const struct got_error *error;
209 5c860e29 2018-03-12 stsp cmd = &got_commands[i];
211 97b3a7be 2019-07-09 stsp if (strcmp(cmd->cmd_name, argv[0]) != 0 &&
212 97b3a7be 2019-07-09 stsp strcmp(cmd->cmd_alias, argv[0]) != 0)
216 1b6b95a8 2018-03-12 stsp got_commands[i].cmd_usage();
218 d7d4f210 2018-03-12 stsp error = got_commands[i].cmd_main(argc, argv);
219 80d5f134 2018-11-11 stsp if (error && !(sigint_received || sigpipe_received)) {
220 d7d4f210 2018-03-12 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
227 20ecf764 2018-03-12 stsp fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
228 ce5b7c56 2019-07-09 stsp list_commands();
232 4ed7e80c 2018-05-20 stsp __dead static void
233 ce5b7c56 2019-07-09 stsp usage(int hflag)
235 53ccebc2 2019-07-30 stsp fprintf(stderr, "usage: %s [-h] [-V] command [arg ...]\n",
236 53ccebc2 2019-07-30 stsp getprogname());
238 ce5b7c56 2019-07-09 stsp list_commands();
242 0266afb7 2019-01-04 stsp static const struct got_error *
243 0ee7065d 2019-05-13 stsp get_editor(char **abspath)
245 0ee7065d 2019-05-13 stsp const struct got_error *err = NULL;
246 e2ba3d07 2019-05-13 stsp const char *editor;
248 8920fa04 2019-08-18 stsp *abspath = NULL;
250 e2ba3d07 2019-05-13 stsp editor = getenv("VISUAL");
251 e2ba3d07 2019-05-13 stsp if (editor == NULL)
252 e2ba3d07 2019-05-13 stsp editor = getenv("EDITOR");
254 0ee7065d 2019-05-13 stsp if (editor) {
255 0ee7065d 2019-05-13 stsp err = got_path_find_prog(abspath, editor);
257 0ee7065d 2019-05-13 stsp return err;
260 0ee7065d 2019-05-13 stsp if (*abspath == NULL) {
261 0ee7065d 2019-05-13 stsp *abspath = strdup("/bin/ed");
262 0ee7065d 2019-05-13 stsp if (*abspath == NULL)
263 0ee7065d 2019-05-13 stsp return got_error_from_errno("strdup");
266 e2ba3d07 2019-05-13 stsp return NULL;
269 e2ba3d07 2019-05-13 stsp static const struct got_error *
270 d0eebce4 2019-03-11 stsp apply_unveil(const char *repo_path, int repo_read_only,
271 c530dc23 2019-07-23 stsp const char *worktree_path)
273 163ce85a 2019-05-13 stsp const struct got_error *err;
275 37c06ea4 2019-07-15 stsp #ifdef PROFILE
276 37c06ea4 2019-07-15 stsp if (unveil("gmon.out", "rwc") != 0)
277 37c06ea4 2019-07-15 stsp return got_error_from_errno2("unveil", "gmon.out");
279 d0eebce4 2019-03-11 stsp if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
280 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", repo_path);
282 0266afb7 2019-01-04 stsp if (worktree_path && unveil(worktree_path, "rwc") != 0)
283 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", worktree_path);
285 f12d0dbe 2019-01-04 stsp if (unveil("/tmp", "rwc") != 0)
286 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", "/tmp");
288 163ce85a 2019-05-13 stsp err = got_privsep_unveil_exec_helpers();
289 163ce85a 2019-05-13 stsp if (err != NULL)
290 163ce85a 2019-05-13 stsp return err;
292 0266afb7 2019-01-04 stsp if (unveil(NULL, NULL) != 0)
293 638f9024 2019-05-13 stsp return got_error_from_errno("unveil");
295 0266afb7 2019-01-04 stsp return NULL;
298 2c7829a4 2019-06-17 stsp __dead static void
299 2c7829a4 2019-06-17 stsp usage_init(void)
301 09ea71ba 2019-07-27 stsp fprintf(stderr, "usage: %s init repository-path\n", getprogname());
305 2c7829a4 2019-06-17 stsp static const struct got_error *
306 2c7829a4 2019-06-17 stsp cmd_init(int argc, char *argv[])
308 2c7829a4 2019-06-17 stsp const struct got_error *error = NULL;
309 2c7829a4 2019-06-17 stsp char *repo_path = NULL;
312 2c7829a4 2019-06-17 stsp while ((ch = getopt(argc, argv, "")) != -1) {
313 2c7829a4 2019-06-17 stsp switch (ch) {
315 2c7829a4 2019-06-17 stsp usage_init();
316 2c7829a4 2019-06-17 stsp /* NOTREACHED */
320 2c7829a4 2019-06-17 stsp argc -= optind;
321 2c7829a4 2019-06-17 stsp argv += optind;
323 2c7829a4 2019-06-17 stsp #ifndef PROFILE
324 2c7829a4 2019-06-17 stsp if (pledge("stdio rpath wpath cpath unveil", NULL) == -1)
325 2c7829a4 2019-06-17 stsp err(1, "pledge");
327 2c7829a4 2019-06-17 stsp if (argc != 1)
328 bc20e173 2019-06-17 stsp usage_init();
330 2c7829a4 2019-06-17 stsp repo_path = strdup(argv[0]);
331 2c7829a4 2019-06-17 stsp if (repo_path == NULL)
332 2c7829a4 2019-06-17 stsp return got_error_from_errno("strdup");
334 2c7829a4 2019-06-17 stsp got_path_strip_trailing_slashes(repo_path);
336 2c7829a4 2019-06-17 stsp error = got_path_mkdir(repo_path);
337 2c7829a4 2019-06-17 stsp if (error &&
338 2c7829a4 2019-06-17 stsp !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
341 c530dc23 2019-07-23 stsp error = apply_unveil(repo_path, 0, NULL);
345 2c7829a4 2019-06-17 stsp error = got_repo_init(repo_path);
346 2c7829a4 2019-06-17 stsp if (error != NULL)
350 3ce1b845 2019-07-15 stsp free(repo_path);
351 3ce1b845 2019-07-15 stsp return error;
354 3ce1b845 2019-07-15 stsp __dead static void
355 3ce1b845 2019-07-15 stsp usage_import(void)
357 3ce1b845 2019-07-15 stsp fprintf(stderr, "usage: %s import [-b branch] [-m message] "
358 3ce1b845 2019-07-15 stsp "[-r repository-path] [-I pattern] path\n", getprogname());
363 3ce1b845 2019-07-15 stsp spawn_editor(const char *editor, const char *file)
366 3ce1b845 2019-07-15 stsp sig_t sighup, sigint, sigquit;
367 3ce1b845 2019-07-15 stsp int st = -1;
369 3ce1b845 2019-07-15 stsp sighup = signal(SIGHUP, SIG_IGN);
370 3ce1b845 2019-07-15 stsp sigint = signal(SIGINT, SIG_IGN);
371 3ce1b845 2019-07-15 stsp sigquit = signal(SIGQUIT, SIG_IGN);
373 3ce1b845 2019-07-15 stsp switch (pid = fork()) {
375 3ce1b845 2019-07-15 stsp goto doneediting;
377 3ce1b845 2019-07-15 stsp execl(editor, editor, file, (char *)NULL);
378 3ce1b845 2019-07-15 stsp _exit(127);
381 3ce1b845 2019-07-15 stsp while (waitpid(pid, &st, 0) == -1)
382 3ce1b845 2019-07-15 stsp if (errno != EINTR)
385 3ce1b845 2019-07-15 stsp doneediting:
386 3ce1b845 2019-07-15 stsp (void)signal(SIGHUP, sighup);
387 3ce1b845 2019-07-15 stsp (void)signal(SIGINT, sigint);
388 3ce1b845 2019-07-15 stsp (void)signal(SIGQUIT, sigquit);
390 3ce1b845 2019-07-15 stsp if (!WIFEXITED(st)) {
391 3ce1b845 2019-07-15 stsp errno = EINTR;
395 3ce1b845 2019-07-15 stsp return WEXITSTATUS(st);
398 3ce1b845 2019-07-15 stsp static const struct got_error *
399 3ce1b845 2019-07-15 stsp edit_logmsg(char **logmsg, const char *editor, const char *logmsg_path,
400 3ce1b845 2019-07-15 stsp const char *initial_content)
402 3ce1b845 2019-07-15 stsp const struct got_error *err = NULL;
403 3ce1b845 2019-07-15 stsp char buf[1024];
404 3ce1b845 2019-07-15 stsp struct stat st, st2;
406 3ce1b845 2019-07-15 stsp int content_changed = 0;
407 3ce1b845 2019-07-15 stsp size_t len;
409 3ce1b845 2019-07-15 stsp *logmsg = NULL;
411 3ce1b845 2019-07-15 stsp if (stat(logmsg_path, &st) == -1)
412 3ce1b845 2019-07-15 stsp return got_error_from_errno2("stat", logmsg_path);
414 3ce1b845 2019-07-15 stsp if (spawn_editor(editor, logmsg_path) == -1)
415 3ce1b845 2019-07-15 stsp return got_error_from_errno("failed spawning editor");
417 3ce1b845 2019-07-15 stsp if (stat(logmsg_path, &st2) == -1)
418 3ce1b845 2019-07-15 stsp return got_error_from_errno("stat");
420 3ce1b845 2019-07-15 stsp if (st.st_mtime == st2.st_mtime && st.st_size == st2.st_size)
421 3ce1b845 2019-07-15 stsp return got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
422 3ce1b845 2019-07-15 stsp "no changes made to commit message, aborting");
424 3ce1b845 2019-07-15 stsp *logmsg = malloc(st2.st_size + 1);
425 3ce1b845 2019-07-15 stsp if (*logmsg == NULL)
426 3ce1b845 2019-07-15 stsp return got_error_from_errno("malloc");
427 3ce1b845 2019-07-15 stsp (*logmsg)[0] = '\0';
430 3ce1b845 2019-07-15 stsp fp = fopen(logmsg_path, "r");
431 3ce1b845 2019-07-15 stsp if (fp == NULL) {
432 3ce1b845 2019-07-15 stsp err = got_error_from_errno("fopen");
435 3ce1b845 2019-07-15 stsp while (fgets(buf, sizeof(buf), fp) != NULL) {
436 3ce1b845 2019-07-15 stsp if (!content_changed && strcmp(buf, initial_content) != 0)
437 3ce1b845 2019-07-15 stsp content_changed = 1;
438 3ce1b845 2019-07-15 stsp if (buf[0] == '#' || (len == 0 && buf[0] == '\n'))
439 3ce1b845 2019-07-15 stsp continue; /* remove comments and leading empty lines */
440 3ce1b845 2019-07-15 stsp len = strlcat(*logmsg, buf, st2.st_size);
442 3ce1b845 2019-07-15 stsp fclose(fp);
444 3ce1b845 2019-07-15 stsp while (len > 0 && (*logmsg)[len - 1] == '\n') {
445 3ce1b845 2019-07-15 stsp (*logmsg)[len - 1] = '\0';
449 3ce1b845 2019-07-15 stsp if (len == 0 || !content_changed)
450 3ce1b845 2019-07-15 stsp err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
451 3ce1b845 2019-07-15 stsp "commit message cannot be empty, aborting");
454 3ce1b845 2019-07-15 stsp free(*logmsg);
455 3ce1b845 2019-07-15 stsp *logmsg = NULL;
457 3ce1b845 2019-07-15 stsp return err;
460 3ce1b845 2019-07-15 stsp static const struct got_error *
461 3ce1b845 2019-07-15 stsp collect_import_msg(char **logmsg, const char *editor, const char *path_dir,
462 3ce1b845 2019-07-15 stsp const char *branch_name)
464 3ce1b845 2019-07-15 stsp char *initial_content = NULL, *logmsg_path = NULL;
465 3ce1b845 2019-07-15 stsp const struct got_error *err = NULL;
468 3ce1b845 2019-07-15 stsp if (asprintf(&initial_content,
469 3ce1b845 2019-07-15 stsp "\n# %s to be imported to branch %s\n", path_dir,
470 3ce1b845 2019-07-15 stsp branch_name) == -1)
471 3ce1b845 2019-07-15 stsp return got_error_from_errno("asprintf");
473 3ce1b845 2019-07-15 stsp err = got_opentemp_named_fd(&logmsg_path, &fd, "/tmp/got-importmsg");
477 3ce1b845 2019-07-15 stsp dprintf(fd, initial_content);
480 3ce1b845 2019-07-15 stsp err = edit_logmsg(logmsg, editor, logmsg_path, initial_content);
482 3ce1b845 2019-07-15 stsp free(initial_content);
483 3ce1b845 2019-07-15 stsp free(logmsg_path);
484 3ce1b845 2019-07-15 stsp return err;
487 3ce1b845 2019-07-15 stsp static const struct got_error *
488 3ce1b845 2019-07-15 stsp import_progress(void *arg, const char *path)
490 3ce1b845 2019-07-15 stsp printf("A %s\n", path);
491 3ce1b845 2019-07-15 stsp return NULL;
494 3ce1b845 2019-07-15 stsp static const struct got_error *
495 84792843 2019-08-09 stsp get_author(const char **author)
497 84792843 2019-08-09 stsp const char *got_author;
499 84792843 2019-08-09 stsp *author = NULL;
501 84792843 2019-08-09 stsp got_author = getenv("GOT_AUTHOR");
502 84792843 2019-08-09 stsp if (got_author == NULL) {
503 84792843 2019-08-09 stsp /* TODO: Look up user in password database? */
504 84792843 2019-08-09 stsp return got_error(GOT_ERR_COMMIT_NO_AUTHOR);
507 84792843 2019-08-09 stsp *author = got_author;
510 84792843 2019-08-09 stsp * Really dumb email address check; we're only doing this to
511 84792843 2019-08-09 stsp * avoid git's object parser breaking on commits we create.
513 84792843 2019-08-09 stsp while (*got_author && *got_author != '<')
514 84792843 2019-08-09 stsp got_author++;
515 84792843 2019-08-09 stsp if (*got_author != '<')
516 84792843 2019-08-09 stsp return got_error(GOT_ERR_COMMIT_NO_EMAIL);
517 84792843 2019-08-09 stsp while (*got_author && *got_author != '@')
518 84792843 2019-08-09 stsp got_author++;
519 84792843 2019-08-09 stsp if (*got_author != '@')
520 84792843 2019-08-09 stsp return got_error(GOT_ERR_COMMIT_NO_EMAIL);
521 84792843 2019-08-09 stsp while (*got_author && *got_author != '>')
522 84792843 2019-08-09 stsp got_author++;
523 84792843 2019-08-09 stsp if (*got_author != '>')
524 84792843 2019-08-09 stsp return got_error(GOT_ERR_COMMIT_NO_EMAIL);
526 84792843 2019-08-09 stsp return NULL;
529 84792843 2019-08-09 stsp static const struct got_error *
530 3ce1b845 2019-07-15 stsp cmd_import(int argc, char *argv[])
532 3ce1b845 2019-07-15 stsp const struct got_error *error = NULL;
533 3ce1b845 2019-07-15 stsp char *path_dir = NULL, *repo_path = NULL, *logmsg = NULL;
534 3ce1b845 2019-07-15 stsp char *editor = NULL;
535 84792843 2019-08-09 stsp const char *author;
536 3ce1b845 2019-07-15 stsp const char *branch_name = "master";
537 3ce1b845 2019-07-15 stsp char *refname = NULL, *id_str = NULL;
538 3ce1b845 2019-07-15 stsp struct got_repository *repo = NULL;
539 3ce1b845 2019-07-15 stsp struct got_reference *branch_ref = NULL, *head_ref = NULL;
540 3ce1b845 2019-07-15 stsp struct got_object_id *new_commit_id = NULL;
542 3ce1b845 2019-07-15 stsp struct got_pathlist_head ignores;
543 3ce1b845 2019-07-15 stsp struct got_pathlist_entry *pe;
545 3ce1b845 2019-07-15 stsp TAILQ_INIT(&ignores);
547 3ce1b845 2019-07-15 stsp while ((ch = getopt(argc, argv, "b:m:r:I:")) != -1) {
548 3ce1b845 2019-07-15 stsp switch (ch) {
550 3ce1b845 2019-07-15 stsp branch_name = optarg;
553 3ce1b845 2019-07-15 stsp logmsg = strdup(optarg);
554 3ce1b845 2019-07-15 stsp if (logmsg == NULL) {
555 3ce1b845 2019-07-15 stsp error = got_error_from_errno("strdup");
560 3ce1b845 2019-07-15 stsp repo_path = realpath(optarg, NULL);
561 3ce1b845 2019-07-15 stsp if (repo_path == NULL) {
562 3ce1b845 2019-07-15 stsp error = got_error_from_errno("realpath");
567 3ce1b845 2019-07-15 stsp if (optarg[0] == '\0')
569 3ce1b845 2019-07-15 stsp error = got_pathlist_insert(&pe, &ignores, optarg,
575 b2b65d18 2019-08-22 stsp usage_import();
576 3ce1b845 2019-07-15 stsp /* NOTREACHED */
580 3ce1b845 2019-07-15 stsp argc -= optind;
581 3ce1b845 2019-07-15 stsp argv += optind;
583 3ce1b845 2019-07-15 stsp #ifndef PROFILE
584 3ce1b845 2019-07-15 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec unveil",
585 3ce1b845 2019-07-15 stsp NULL) == -1)
586 3ce1b845 2019-07-15 stsp err(1, "pledge");
588 3ce1b845 2019-07-15 stsp if (argc != 1)
589 3ce1b845 2019-07-15 stsp usage_import();
591 84792843 2019-08-09 stsp error = get_author(&author);
593 84792843 2019-08-09 stsp return error;
595 3ce1b845 2019-07-15 stsp if (repo_path == NULL) {
596 3ce1b845 2019-07-15 stsp repo_path = getcwd(NULL, 0);
597 3ce1b845 2019-07-15 stsp if (repo_path == NULL)
598 3ce1b845 2019-07-15 stsp return got_error_from_errno("getcwd");
600 3ce1b845 2019-07-15 stsp got_path_strip_trailing_slashes(repo_path);
601 3ce1b845 2019-07-15 stsp error = got_repo_open(&repo, repo_path);
605 3ce1b845 2019-07-15 stsp if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
606 3ce1b845 2019-07-15 stsp error = got_error_from_errno("asprintf");
610 3ce1b845 2019-07-15 stsp error = got_ref_open(&branch_ref, repo, refname, 0);
611 3ce1b845 2019-07-15 stsp if (error) {
612 3ce1b845 2019-07-15 stsp if (error->code != GOT_ERR_NOT_REF)
615 3ce1b845 2019-07-15 stsp error = got_error_msg(GOT_ERR_BRANCH_EXISTS,
616 3ce1b845 2019-07-15 stsp "import target branch already exists");
620 3ce1b845 2019-07-15 stsp path_dir = realpath(argv[0], NULL);
621 3ce1b845 2019-07-15 stsp if (path_dir == NULL) {
622 3ce1b845 2019-07-15 stsp error = got_error_from_errno("realpath");
625 3ce1b845 2019-07-15 stsp got_path_strip_trailing_slashes(path_dir);
628 3ce1b845 2019-07-15 stsp * unveil(2) traverses exec(2); if an editor is used we have
629 3ce1b845 2019-07-15 stsp * to apply unveil after the log message has been written.
631 3ce1b845 2019-07-15 stsp if (logmsg == NULL || strlen(logmsg) == 0) {
632 3ce1b845 2019-07-15 stsp error = get_editor(&editor);
635 3ce1b845 2019-07-15 stsp error = collect_import_msg(&logmsg, editor, path_dir, refname);
640 3ce1b845 2019-07-15 stsp if (unveil(path_dir, "r") != 0)
641 3ce1b845 2019-07-15 stsp return got_error_from_errno2("unveil", path_dir);
643 c530dc23 2019-07-23 stsp error = apply_unveil(got_repo_get_path(repo), 0, NULL);
647 3ce1b845 2019-07-15 stsp error = got_repo_import(&new_commit_id, path_dir, logmsg,
648 84792843 2019-08-09 stsp author, &ignores, repo, import_progress, NULL);
652 3ce1b845 2019-07-15 stsp error = got_ref_alloc(&branch_ref, refname, new_commit_id);
656 3ce1b845 2019-07-15 stsp error = got_ref_write(branch_ref, repo);
660 3ce1b845 2019-07-15 stsp error = got_object_id_str(&id_str, new_commit_id);
664 3ce1b845 2019-07-15 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
665 3ce1b845 2019-07-15 stsp if (error) {
666 3ce1b845 2019-07-15 stsp if (error->code != GOT_ERR_NOT_REF)
669 3ce1b845 2019-07-15 stsp error = got_ref_alloc_symref(&head_ref, GOT_REF_HEAD,
670 3ce1b845 2019-07-15 stsp branch_ref);
674 3ce1b845 2019-07-15 stsp error = got_ref_write(head_ref, repo);
679 3ce1b845 2019-07-15 stsp printf("Created branch %s with commit %s\n",
680 3ce1b845 2019-07-15 stsp got_ref_get_name(branch_ref), id_str);
682 2c7829a4 2019-06-17 stsp free(repo_path);
683 3ce1b845 2019-07-15 stsp free(editor);
684 3ce1b845 2019-07-15 stsp free(refname);
685 3ce1b845 2019-07-15 stsp free(new_commit_id);
686 3ce1b845 2019-07-15 stsp free(id_str);
687 3ce1b845 2019-07-15 stsp if (branch_ref)
688 3ce1b845 2019-07-15 stsp got_ref_close(branch_ref);
689 3ce1b845 2019-07-15 stsp if (head_ref)
690 3ce1b845 2019-07-15 stsp got_ref_close(head_ref);
691 2c7829a4 2019-06-17 stsp return error;
694 4ed7e80c 2018-05-20 stsp __dead static void
695 c09a553d 2018-03-12 stsp usage_checkout(void)
697 08573d5b 2019-05-14 stsp fprintf(stderr, "usage: %s checkout [-b branch] [-c commit] "
698 08573d5b 2019-05-14 stsp "[-p prefix] repository-path [worktree-path]\n", getprogname());
702 1ee397ad 2019-07-12 stsp static const struct got_error *
703 a0eb853d 2018-12-29 stsp checkout_progress(void *arg, unsigned char status, const char *path)
705 92a684f4 2018-03-12 stsp char *worktree_path = arg;
707 a484d721 2019-06-10 stsp /* Base commit bump happens silently. */
708 a484d721 2019-06-10 stsp if (status == GOT_STATUS_BUMP_BASE)
709 1ee397ad 2019-07-12 stsp return NULL;
711 92a684f4 2018-03-12 stsp while (path[0] == '/')
714 d7b62c98 2018-12-27 stsp printf("%c %s/%s\n", status, worktree_path, path);
715 1ee397ad 2019-07-12 stsp return NULL;
718 99437157 2018-11-11 stsp static const struct got_error *
719 6bad629b 2019-02-04 stsp check_cancelled(void *arg)
721 99437157 2018-11-11 stsp if (sigint_received || sigpipe_received)
722 99437157 2018-11-11 stsp return got_error(GOT_ERR_CANCELLED);
723 99437157 2018-11-11 stsp return NULL;
726 8069f636 2019-01-12 stsp static const struct got_error *
727 024e9686 2019-05-14 stsp check_linear_ancestry(struct got_object_id *commit_id,
728 024e9686 2019-05-14 stsp struct got_object_id *base_commit_id, struct got_repository *repo)
730 d5bea539 2019-05-13 stsp const struct got_error *err = NULL;
731 024e9686 2019-05-14 stsp struct got_object_id *yca_id;
733 d5bea539 2019-05-13 stsp err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
734 6fb7cd11 2019-08-22 stsp commit_id, base_commit_id, repo, check_cancelled, NULL);
736 36a38700 2019-05-10 stsp return err;
738 d5bea539 2019-05-13 stsp if (yca_id == NULL)
739 d5bea539 2019-05-13 stsp return got_error(GOT_ERR_ANCESTRY);
742 d5bea539 2019-05-13 stsp * Require a straight line of history between the target commit
743 d5bea539 2019-05-13 stsp * and the work tree's base commit.
745 d5751d49 2019-05-14 stsp * Non-linear situations such as this require a rebase:
747 d5bea539 2019-05-13 stsp * (commit) D F (base_commit)
755 d5bea539 2019-05-13 stsp * 'got update' only handles linear cases:
756 d5bea539 2019-05-13 stsp * Update forwards in time: A (base/yca) - B - C - D (commit)
757 efa2b6f7 2019-05-14 stsp * Update backwards in time: D (base) - C - B - A (commit/yca)
759 d5bea539 2019-05-13 stsp if (got_object_id_cmp(commit_id, yca_id) != 0 &&
760 d5bea539 2019-05-13 stsp got_object_id_cmp(base_commit_id, yca_id) != 0)
761 d5bea539 2019-05-13 stsp return got_error(GOT_ERR_ANCESTRY);
763 d5bea539 2019-05-13 stsp free(yca_id);
764 d5bea539 2019-05-13 stsp return NULL;
767 a367ff0f 2019-05-14 stsp static const struct got_error *
768 a367ff0f 2019-05-14 stsp check_same_branch(struct got_object_id *commit_id,
769 a51a74b3 2019-07-27 stsp struct got_reference *head_ref, struct got_object_id *yca_id,
770 a51a74b3 2019-07-27 stsp struct got_repository *repo)
772 a367ff0f 2019-05-14 stsp const struct got_error *err = NULL;
773 a367ff0f 2019-05-14 stsp struct got_commit_graph *graph = NULL;
774 a367ff0f 2019-05-14 stsp struct got_object_id *head_commit_id = NULL;
775 a367ff0f 2019-05-14 stsp int is_same_branch = 0;
777 a367ff0f 2019-05-14 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
781 a51a74b3 2019-07-27 stsp if (got_object_id_cmp(head_commit_id, commit_id) == 0) {
782 a51a74b3 2019-07-27 stsp is_same_branch = 1;
785 a51a74b3 2019-07-27 stsp if (yca_id && got_object_id_cmp(commit_id, yca_id) == 0) {
786 a51a74b3 2019-07-27 stsp is_same_branch = 1;
790 a367ff0f 2019-05-14 stsp err = got_commit_graph_open(&graph, head_commit_id, "/", 1, repo);
794 6fb7cd11 2019-08-22 stsp err = got_commit_graph_iter_start(graph, head_commit_id, repo,
795 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
800 a367ff0f 2019-05-14 stsp struct got_object_id *id;
801 a367ff0f 2019-05-14 stsp err = got_commit_graph_iter_next(&id, graph);
803 a367ff0f 2019-05-14 stsp if (err->code == GOT_ERR_ITER_COMPLETED) {
804 a367ff0f 2019-05-14 stsp err = NULL;
806 a14c42fa 2019-07-15 stsp } else if (err->code != GOT_ERR_ITER_NEED_MORE)
808 a367ff0f 2019-05-14 stsp err = got_commit_graph_fetch_commits(graph, 1,
809 6fb7cd11 2019-08-22 stsp repo, check_cancelled, NULL);
815 a51a74b3 2019-07-27 stsp if (yca_id && got_object_id_cmp(id, yca_id) == 0)
817 a367ff0f 2019-05-14 stsp if (got_object_id_cmp(id, commit_id) == 0) {
818 a367ff0f 2019-05-14 stsp is_same_branch = 1;
825 a367ff0f 2019-05-14 stsp got_commit_graph_close(graph);
826 a367ff0f 2019-05-14 stsp free(head_commit_id);
827 a367ff0f 2019-05-14 stsp if (!err && !is_same_branch)
828 a367ff0f 2019-05-14 stsp err = got_error(GOT_ERR_ANCESTRY);
829 04f57cb3 2019-07-25 stsp return err;
832 04f57cb3 2019-07-25 stsp static const struct got_error *
833 04f57cb3 2019-07-25 stsp resolve_commit_arg(struct got_object_id **commit_id,
834 04f57cb3 2019-07-25 stsp const char *commit_id_arg, struct got_repository *repo)
836 04f57cb3 2019-07-25 stsp const struct got_error *err;
837 04f57cb3 2019-07-25 stsp struct got_reference *ref;
838 303e2782 2019-08-09 stsp struct got_tag_object *tag;
840 303e2782 2019-08-09 stsp err = got_repo_object_match_tag(&tag, commit_id_arg,
841 303e2782 2019-08-09 stsp GOT_OBJ_TYPE_COMMIT, repo);
842 303e2782 2019-08-09 stsp if (err == NULL) {
843 303e2782 2019-08-09 stsp *commit_id = got_object_id_dup(
844 303e2782 2019-08-09 stsp got_object_tag_get_object_id(tag));
845 303e2782 2019-08-09 stsp if (*commit_id == NULL)
846 303e2782 2019-08-09 stsp err = got_error_from_errno("got_object_id_dup");
847 303e2782 2019-08-09 stsp got_object_tag_close(tag);
848 303e2782 2019-08-09 stsp return err;
849 303e2782 2019-08-09 stsp } else if (err->code != GOT_ERR_NO_OBJ)
850 303e2782 2019-08-09 stsp return err;
852 04f57cb3 2019-07-25 stsp err = got_ref_open(&ref, repo, commit_id_arg, 0);
853 04f57cb3 2019-07-25 stsp if (err == NULL) {
854 04f57cb3 2019-07-25 stsp err = got_ref_resolve(commit_id, repo, ref);
855 04f57cb3 2019-07-25 stsp got_ref_close(ref);
857 04f57cb3 2019-07-25 stsp if (err->code != GOT_ERR_NOT_REF)
858 04f57cb3 2019-07-25 stsp return err;
859 04f57cb3 2019-07-25 stsp err = got_repo_match_object_id_prefix(commit_id,
860 04f57cb3 2019-07-25 stsp commit_id_arg, GOT_OBJ_TYPE_COMMIT, repo);
862 a367ff0f 2019-05-14 stsp return err;
865 4ed7e80c 2018-05-20 stsp static const struct got_error *
866 c09a553d 2018-03-12 stsp cmd_checkout(int argc, char *argv[])
868 c09a553d 2018-03-12 stsp const struct got_error *error = NULL;
869 c09a553d 2018-03-12 stsp struct got_repository *repo = NULL;
870 c09a553d 2018-03-12 stsp struct got_reference *head_ref = NULL;
871 c09a553d 2018-03-12 stsp struct got_worktree *worktree = NULL;
872 c09a553d 2018-03-12 stsp char *repo_path = NULL;
873 c09a553d 2018-03-12 stsp char *worktree_path = NULL;
874 0bb8a95e 2018-03-12 stsp const char *path_prefix = "";
875 08573d5b 2019-05-14 stsp const char *branch_name = GOT_REF_HEAD;
876 8069f636 2019-01-12 stsp char *commit_id_str = NULL;
877 72151b04 2019-05-11 stsp int ch, same_path_prefix;
878 f2ea84fa 2019-07-27 stsp struct got_pathlist_head paths;
880 f2ea84fa 2019-07-27 stsp TAILQ_INIT(&paths);
882 08573d5b 2019-05-14 stsp while ((ch = getopt(argc, argv, "b:c:p:")) != -1) {
883 0bb8a95e 2018-03-12 stsp switch (ch) {
885 08573d5b 2019-05-14 stsp branch_name = optarg;
888 8069f636 2019-01-12 stsp commit_id_str = strdup(optarg);
889 8069f636 2019-01-12 stsp if (commit_id_str == NULL)
890 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
893 0bb8a95e 2018-03-12 stsp path_prefix = optarg;
896 2deda0b9 2019-03-07 stsp usage_checkout();
897 0bb8a95e 2018-03-12 stsp /* NOTREACHED */
901 0bb8a95e 2018-03-12 stsp argc -= optind;
902 0bb8a95e 2018-03-12 stsp argv += optind;
904 6715a751 2018-03-16 stsp #ifndef PROFILE
905 68ed9ba5 2019-02-10 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
906 68ed9ba5 2019-02-10 stsp "unveil", NULL) == -1)
907 c09a553d 2018-03-12 stsp err(1, "pledge");
909 0bb8a95e 2018-03-12 stsp if (argc == 1) {
910 c09a553d 2018-03-12 stsp char *cwd, *base, *dotgit;
911 76089277 2018-04-01 stsp repo_path = realpath(argv[0], NULL);
912 76089277 2018-04-01 stsp if (repo_path == NULL)
913 638f9024 2019-05-13 stsp return got_error_from_errno2("realpath", argv[0]);
914 c09a553d 2018-03-12 stsp cwd = getcwd(NULL, 0);
915 76089277 2018-04-01 stsp if (cwd == NULL) {
916 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
919 230a42bd 2019-05-11 jcs if (path_prefix[0]) {
920 230a42bd 2019-05-11 jcs base = basename(path_prefix);
921 230a42bd 2019-05-11 jcs if (base == NULL) {
922 638f9024 2019-05-13 stsp error = got_error_from_errno2("basename",
923 230a42bd 2019-05-11 jcs path_prefix);
927 5d7c1dab 2018-04-01 stsp base = basename(repo_path);
928 230a42bd 2019-05-11 jcs if (base == NULL) {
929 638f9024 2019-05-13 stsp error = got_error_from_errno2("basename",
934 c09a553d 2018-03-12 stsp dotgit = strstr(base, ".git");
935 c09a553d 2018-03-12 stsp if (dotgit)
936 c09a553d 2018-03-12 stsp *dotgit = '\0';
937 c09a553d 2018-03-12 stsp if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
938 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
943 0bb8a95e 2018-03-12 stsp } else if (argc == 2) {
944 76089277 2018-04-01 stsp repo_path = realpath(argv[0], NULL);
945 76089277 2018-04-01 stsp if (repo_path == NULL) {
946 638f9024 2019-05-13 stsp error = got_error_from_errno2("realpath", argv[0]);
949 f7b38925 2018-04-01 stsp worktree_path = realpath(argv[1], NULL);
950 76089277 2018-04-01 stsp if (worktree_path == NULL) {
951 b4b3a7dd 2019-07-22 stsp if (errno != ENOENT) {
952 b4b3a7dd 2019-07-22 stsp error = got_error_from_errno2("realpath",
956 b4b3a7dd 2019-07-22 stsp worktree_path = strdup(argv[1]);
957 b4b3a7dd 2019-07-22 stsp if (worktree_path == NULL) {
958 b4b3a7dd 2019-07-22 stsp error = got_error_from_errno("strdup");
963 c09a553d 2018-03-12 stsp usage_checkout();
965 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
966 72151b04 2019-05-11 stsp got_path_strip_trailing_slashes(worktree_path);
968 c09a553d 2018-03-12 stsp error = got_repo_open(&repo, repo_path);
969 c09a553d 2018-03-12 stsp if (error != NULL)
972 c530dc23 2019-07-23 stsp /* Pre-create work tree path for unveil(2) */
973 c530dc23 2019-07-23 stsp error = got_path_mkdir(worktree_path);
974 c530dc23 2019-07-23 stsp if (error) {
975 80c1b583 2019-08-07 stsp if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
976 80c1b583 2019-08-07 stsp !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
978 c530dc23 2019-07-23 stsp if (!got_path_dir_is_empty(worktree_path)) {
979 c530dc23 2019-07-23 stsp error = got_error_path(worktree_path,
980 c530dc23 2019-07-23 stsp GOT_ERR_DIR_NOT_EMPTY);
985 c530dc23 2019-07-23 stsp error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
989 08573d5b 2019-05-14 stsp error = got_ref_open(&head_ref, repo, branch_name, 0);
990 c09a553d 2018-03-12 stsp if (error != NULL)
993 0bb8a95e 2018-03-12 stsp error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
994 d70b8e30 2018-12-27 stsp if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
997 c09a553d 2018-03-12 stsp error = got_worktree_open(&worktree, worktree_path);
998 c09a553d 2018-03-12 stsp if (error != NULL)
1001 e5dc7198 2018-12-29 stsp error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
1002 e5dc7198 2018-12-29 stsp path_prefix);
1003 e5dc7198 2018-12-29 stsp if (error != NULL)
1004 e5dc7198 2018-12-29 stsp goto done;
1005 e5dc7198 2018-12-29 stsp if (!same_path_prefix) {
1006 49520a32 2018-12-29 stsp error = got_error(GOT_ERR_PATH_PREFIX);
1007 49520a32 2018-12-29 stsp goto done;
1010 8069f636 2019-01-12 stsp if (commit_id_str) {
1011 04f57cb3 2019-07-25 stsp struct got_object_id *commit_id;
1012 04f57cb3 2019-07-25 stsp error = resolve_commit_arg(&commit_id, commit_id_str, repo);
1013 30837e32 2019-07-25 stsp if (error)
1014 8069f636 2019-01-12 stsp goto done;
1015 024e9686 2019-05-14 stsp error = check_linear_ancestry(commit_id,
1016 024e9686 2019-05-14 stsp got_worktree_get_base_commit_id(worktree), repo);
1017 8069f636 2019-01-12 stsp if (error != NULL) {
1018 8069f636 2019-01-12 stsp free(commit_id);
1019 8069f636 2019-01-12 stsp goto done;
1021 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
1022 45d344f6 2019-05-14 stsp if (error)
1023 45d344f6 2019-05-14 stsp goto done;
1024 8069f636 2019-01-12 stsp error = got_worktree_set_base_commit_id(worktree, repo,
1025 8069f636 2019-01-12 stsp commit_id);
1026 8069f636 2019-01-12 stsp free(commit_id);
1027 8069f636 2019-01-12 stsp if (error)
1028 8069f636 2019-01-12 stsp goto done;
1031 adc19d55 2019-07-28 stsp error = got_pathlist_append(&paths, "", NULL);
1032 f2ea84fa 2019-07-27 stsp if (error)
1033 f2ea84fa 2019-07-27 stsp goto done;
1034 f2ea84fa 2019-07-27 stsp error = got_worktree_checkout_files(worktree, &paths, repo,
1035 6bad629b 2019-02-04 stsp checkout_progress, worktree_path, check_cancelled, NULL);
1036 c09a553d 2018-03-12 stsp if (error != NULL)
1037 c09a553d 2018-03-12 stsp goto done;
1039 b65ae19a 2018-04-24 stsp printf("Now shut up and hack\n");
1042 f2ea84fa 2019-07-27 stsp got_pathlist_free(&paths);
1043 8069f636 2019-01-12 stsp free(commit_id_str);
1044 76089277 2018-04-01 stsp free(repo_path);
1045 507dc3bb 2018-12-29 stsp free(worktree_path);
1046 507dc3bb 2018-12-29 stsp return error;
1049 507dc3bb 2018-12-29 stsp __dead static void
1050 507dc3bb 2018-12-29 stsp usage_update(void)
1052 f2ea84fa 2019-07-27 stsp fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path ...]\n",
1053 507dc3bb 2018-12-29 stsp getprogname());
1057 1ee397ad 2019-07-12 stsp static const struct got_error *
1058 507dc3bb 2018-12-29 stsp update_progress(void *arg, unsigned char status, const char *path)
1060 784955db 2019-01-12 stsp int *did_something = arg;
1062 507dc3bb 2018-12-29 stsp if (status == GOT_STATUS_EXISTS)
1063 1ee397ad 2019-07-12 stsp return NULL;
1065 1545c615 2019-02-10 stsp *did_something = 1;
1067 a484d721 2019-06-10 stsp /* Base commit bump happens silently. */
1068 a484d721 2019-06-10 stsp if (status == GOT_STATUS_BUMP_BASE)
1069 1ee397ad 2019-07-12 stsp return NULL;
1071 507dc3bb 2018-12-29 stsp while (path[0] == '/')
1073 507dc3bb 2018-12-29 stsp printf("%c %s\n", status, path);
1074 1ee397ad 2019-07-12 stsp return NULL;
1077 be7061eb 2018-12-30 stsp static const struct got_error *
1078 a1fb16d8 2019-05-24 stsp switch_head_ref(struct got_reference *head_ref,
1079 a1fb16d8 2019-05-24 stsp struct got_object_id *commit_id, struct got_worktree *worktree,
1080 a1fb16d8 2019-05-24 stsp struct got_repository *repo)
1082 a1fb16d8 2019-05-24 stsp const struct got_error *err = NULL;
1083 a1fb16d8 2019-05-24 stsp char *base_id_str;
1084 a1fb16d8 2019-05-24 stsp int ref_has_moved = 0;
1086 a1fb16d8 2019-05-24 stsp /* Trivial case: switching between two different references. */
1087 a1fb16d8 2019-05-24 stsp if (strcmp(got_ref_get_name(head_ref),
1088 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree)) != 0) {
1089 a1fb16d8 2019-05-24 stsp printf("Switching work tree from %s to %s\n",
1090 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree),
1091 a1fb16d8 2019-05-24 stsp got_ref_get_name(head_ref));
1092 a1fb16d8 2019-05-24 stsp return got_worktree_set_head_ref(worktree, head_ref);
1095 a1fb16d8 2019-05-24 stsp err = check_linear_ancestry(commit_id,
1096 a1fb16d8 2019-05-24 stsp got_worktree_get_base_commit_id(worktree), repo);
1097 a1fb16d8 2019-05-24 stsp if (err) {
1098 a1fb16d8 2019-05-24 stsp if (err->code != GOT_ERR_ANCESTRY)
1099 a1fb16d8 2019-05-24 stsp return err;
1100 a1fb16d8 2019-05-24 stsp ref_has_moved = 1;
1102 a1fb16d8 2019-05-24 stsp if (!ref_has_moved)
1103 a1fb16d8 2019-05-24 stsp return NULL;
1105 a1fb16d8 2019-05-24 stsp /* Switching to a rebased branch with the same reference name. */
1106 a1fb16d8 2019-05-24 stsp err = got_object_id_str(&base_id_str,
1107 a1fb16d8 2019-05-24 stsp got_worktree_get_base_commit_id(worktree));
1109 a1fb16d8 2019-05-24 stsp return err;
1110 a1fb16d8 2019-05-24 stsp printf("Reference %s now points at a different branch\n",
1111 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree));
1112 a1fb16d8 2019-05-24 stsp printf("Switching work tree from %s to %s\n", base_id_str,
1113 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree));
1114 0ebf8283 2019-07-24 stsp return NULL;
1117 0ebf8283 2019-07-24 stsp static const struct got_error *
1118 0ebf8283 2019-07-24 stsp check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
1120 0ebf8283 2019-07-24 stsp const struct got_error *err;
1121 0ebf8283 2019-07-24 stsp int in_progress;
1123 0ebf8283 2019-07-24 stsp err = got_worktree_rebase_in_progress(&in_progress, worktree);
1125 0ebf8283 2019-07-24 stsp return err;
1126 0ebf8283 2019-07-24 stsp if (in_progress)
1127 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_REBASING);
1129 0ebf8283 2019-07-24 stsp err = got_worktree_histedit_in_progress(&in_progress, worktree);
1131 0ebf8283 2019-07-24 stsp return err;
1132 0ebf8283 2019-07-24 stsp if (in_progress)
1133 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_HISTEDIT_BUSY);
1135 a1fb16d8 2019-05-24 stsp return NULL;
1138 a5edda0a 2019-07-27 stsp static const struct got_error *
1139 a5edda0a 2019-07-27 stsp get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
1140 a5edda0a 2019-07-27 stsp char *argv[], struct got_worktree *worktree)
1142 a0de39f3 2019-08-09 stsp const struct got_error *err = NULL;
1143 a5edda0a 2019-07-27 stsp char *path;
1146 a5edda0a 2019-07-27 stsp if (argc == 0) {
1147 a5edda0a 2019-07-27 stsp path = strdup("");
1148 a5edda0a 2019-07-27 stsp if (path == NULL)
1149 a5edda0a 2019-07-27 stsp return got_error_from_errno("strdup");
1150 adc19d55 2019-07-28 stsp return got_pathlist_append(paths, path, NULL);
1153 a5edda0a 2019-07-27 stsp for (i = 0; i < argc; i++) {
1154 a5edda0a 2019-07-27 stsp err = got_worktree_resolve_path(&path, worktree, argv[i]);
1157 adc19d55 2019-07-28 stsp err = got_pathlist_append(paths, path, NULL);
1158 a5edda0a 2019-07-27 stsp if (err) {
1159 a5edda0a 2019-07-27 stsp free(path);
1164 a5edda0a 2019-07-27 stsp return err;
1167 a1fb16d8 2019-05-24 stsp static const struct got_error *
1168 507dc3bb 2018-12-29 stsp cmd_update(int argc, char *argv[])
1170 507dc3bb 2018-12-29 stsp const struct got_error *error = NULL;
1171 507dc3bb 2018-12-29 stsp struct got_repository *repo = NULL;
1172 507dc3bb 2018-12-29 stsp struct got_worktree *worktree = NULL;
1173 f2ea84fa 2019-07-27 stsp char *worktree_path = NULL;
1174 507dc3bb 2018-12-29 stsp struct got_object_id *commit_id = NULL;
1175 9c4b8182 2019-01-02 stsp char *commit_id_str = NULL;
1176 024e9686 2019-05-14 stsp const char *branch_name = NULL;
1177 024e9686 2019-05-14 stsp struct got_reference *head_ref = NULL;
1178 f2ea84fa 2019-07-27 stsp struct got_pathlist_head paths;
1179 f2ea84fa 2019-07-27 stsp struct got_pathlist_entry *pe;
1180 0ebf8283 2019-07-24 stsp int ch, did_something = 0;
1182 f2ea84fa 2019-07-27 stsp TAILQ_INIT(&paths);
1184 024e9686 2019-05-14 stsp while ((ch = getopt(argc, argv, "b:c:")) != -1) {
1185 507dc3bb 2018-12-29 stsp switch (ch) {
1187 024e9686 2019-05-14 stsp branch_name = optarg;
1190 9c4b8182 2019-01-02 stsp commit_id_str = strdup(optarg);
1191 9c4b8182 2019-01-02 stsp if (commit_id_str == NULL)
1192 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
1195 2deda0b9 2019-03-07 stsp usage_update();
1196 507dc3bb 2018-12-29 stsp /* NOTREACHED */
1200 507dc3bb 2018-12-29 stsp argc -= optind;
1201 507dc3bb 2018-12-29 stsp argv += optind;
1203 507dc3bb 2018-12-29 stsp #ifndef PROFILE
1204 68ed9ba5 2019-02-10 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1205 68ed9ba5 2019-02-10 stsp "unveil", NULL) == -1)
1206 507dc3bb 2018-12-29 stsp err(1, "pledge");
1208 c4cdcb68 2019-04-03 stsp worktree_path = getcwd(NULL, 0);
1209 c4cdcb68 2019-04-03 stsp if (worktree_path == NULL) {
1210 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
1211 c4cdcb68 2019-04-03 stsp goto done;
1213 c4cdcb68 2019-04-03 stsp error = got_worktree_open(&worktree, worktree_path);
1214 7d5807f4 2019-07-11 stsp if (error)
1215 7d5807f4 2019-07-11 stsp goto done;
1217 0ebf8283 2019-07-24 stsp error = check_rebase_or_histedit_in_progress(worktree);
1218 f2ea84fa 2019-07-27 stsp if (error)
1219 f2ea84fa 2019-07-27 stsp goto done;
1221 507dc3bb 2018-12-29 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
1222 507dc3bb 2018-12-29 stsp if (error != NULL)
1223 507dc3bb 2018-12-29 stsp goto done;
1225 97430839 2019-03-11 stsp error = apply_unveil(got_repo_get_path(repo), 0,
1226 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
1227 087fb88c 2019-08-04 stsp if (error)
1228 087fb88c 2019-08-04 stsp goto done;
1230 087fb88c 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
1231 0266afb7 2019-01-04 stsp if (error)
1232 0266afb7 2019-01-04 stsp goto done;
1234 a1fb16d8 2019-05-24 stsp error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
1235 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree), 0);
1236 024e9686 2019-05-14 stsp if (error != NULL)
1237 024e9686 2019-05-14 stsp goto done;
1238 507dc3bb 2018-12-29 stsp if (commit_id_str == NULL) {
1239 507dc3bb 2018-12-29 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
1240 9c4b8182 2019-01-02 stsp if (error != NULL)
1241 9c4b8182 2019-01-02 stsp goto done;
1242 9c4b8182 2019-01-02 stsp error = got_object_id_str(&commit_id_str, commit_id);
1243 507dc3bb 2018-12-29 stsp if (error != NULL)
1244 507dc3bb 2018-12-29 stsp goto done;
1246 04f57cb3 2019-07-25 stsp error = resolve_commit_arg(&commit_id, commit_id_str, repo);
1247 04f57cb3 2019-07-25 stsp free(commit_id_str);
1248 bb787f09 2019-07-25 stsp commit_id_str = NULL;
1249 30837e32 2019-07-25 stsp if (error)
1250 a297e751 2019-07-11 stsp goto done;
1251 a297e751 2019-07-11 stsp error = got_object_id_str(&commit_id_str, commit_id);
1252 a297e751 2019-07-11 stsp if (error)
1253 507dc3bb 2018-12-29 stsp goto done;
1256 a1fb16d8 2019-05-24 stsp if (branch_name) {
1257 024e9686 2019-05-14 stsp struct got_object_id *head_commit_id;
1258 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, &paths, entry) {
1259 f2b16ada 2019-08-02 stsp if (pe->path_len == 0)
1261 f2ea84fa 2019-07-27 stsp error = got_error_msg(GOT_ERR_BAD_PATH,
1262 f2ea84fa 2019-07-27 stsp "switching between branches requires that "
1263 f2ea84fa 2019-07-27 stsp "the entire work tree gets updated");
1264 024e9686 2019-05-14 stsp goto done;
1266 024e9686 2019-05-14 stsp error = got_ref_resolve(&head_commit_id, repo, head_ref);
1267 024e9686 2019-05-14 stsp if (error)
1268 024e9686 2019-05-14 stsp goto done;
1269 024e9686 2019-05-14 stsp error = check_linear_ancestry(commit_id, head_commit_id, repo);
1270 a367ff0f 2019-05-14 stsp free(head_commit_id);
1271 024e9686 2019-05-14 stsp if (error != NULL)
1272 024e9686 2019-05-14 stsp goto done;
1273 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
1274 a367ff0f 2019-05-14 stsp if (error)
1275 a367ff0f 2019-05-14 stsp goto done;
1276 a1fb16d8 2019-05-24 stsp error = switch_head_ref(head_ref, commit_id, worktree, repo);
1277 024e9686 2019-05-14 stsp if (error)
1278 024e9686 2019-05-14 stsp goto done;
1280 024e9686 2019-05-14 stsp error = check_linear_ancestry(commit_id,
1281 024e9686 2019-05-14 stsp got_worktree_get_base_commit_id(worktree), repo);
1282 a367ff0f 2019-05-14 stsp if (error != NULL) {
1283 a367ff0f 2019-05-14 stsp if (error->code == GOT_ERR_ANCESTRY)
1284 a367ff0f 2019-05-14 stsp error = got_error(GOT_ERR_BRANCH_MOVED);
1285 024e9686 2019-05-14 stsp goto done;
1287 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
1288 a367ff0f 2019-05-14 stsp if (error)
1289 a367ff0f 2019-05-14 stsp goto done;
1292 507dc3bb 2018-12-29 stsp if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
1293 507dc3bb 2018-12-29 stsp commit_id) != 0) {
1294 507dc3bb 2018-12-29 stsp error = got_worktree_set_base_commit_id(worktree, repo,
1295 507dc3bb 2018-12-29 stsp commit_id);
1296 507dc3bb 2018-12-29 stsp if (error)
1297 507dc3bb 2018-12-29 stsp goto done;
1300 f2ea84fa 2019-07-27 stsp error = got_worktree_checkout_files(worktree, &paths, repo,
1301 6bad629b 2019-02-04 stsp update_progress, &did_something, check_cancelled, NULL);
1302 507dc3bb 2018-12-29 stsp if (error != NULL)
1303 507dc3bb 2018-12-29 stsp goto done;
1305 784955db 2019-01-12 stsp if (did_something)
1306 784955db 2019-01-12 stsp printf("Updated to commit %s\n", commit_id_str);
1308 784955db 2019-01-12 stsp printf("Already up-to-date\n");
1310 c09a553d 2018-03-12 stsp free(worktree_path);
1311 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, &paths, entry)
1312 f2ea84fa 2019-07-27 stsp free((char *)pe->path);
1313 f2ea84fa 2019-07-27 stsp got_pathlist_free(&paths);
1314 507dc3bb 2018-12-29 stsp free(commit_id);
1315 9c4b8182 2019-01-02 stsp free(commit_id_str);
1316 c09a553d 2018-03-12 stsp return error;
1319 f42b1b34 2018-03-12 stsp static const struct got_error *
1320 44392932 2019-08-25 stsp diff_blobs(struct got_object_id *blob_id1, struct got_object_id *blob_id2,
1321 44392932 2019-08-25 stsp const char *path, int diff_context, struct got_repository *repo)
1323 f42b1b34 2018-03-12 stsp const struct got_error *err = NULL;
1324 44392932 2019-08-25 stsp struct got_blob_object *blob1 = NULL, *blob2 = NULL;
1326 44392932 2019-08-25 stsp if (blob_id1) {
1327 44392932 2019-08-25 stsp err = got_object_open_as_blob(&blob1, repo, blob_id1, 8192);
1329 44392932 2019-08-25 stsp goto done;
1332 44392932 2019-08-25 stsp err = got_object_open_as_blob(&blob2, repo, blob_id2, 8192);
1334 44392932 2019-08-25 stsp goto done;
1336 44392932 2019-08-25 stsp while (path[0] == '/')
1338 44392932 2019-08-25 stsp err = got_diff_blob(blob1, blob2, path, path, diff_context,
1341 44392932 2019-08-25 stsp if (blob1)
1342 44392932 2019-08-25 stsp got_object_blob_close(blob1);
1343 44392932 2019-08-25 stsp got_object_blob_close(blob2);
1344 44392932 2019-08-25 stsp return err;
1347 44392932 2019-08-25 stsp static const struct got_error *
1348 44392932 2019-08-25 stsp diff_trees(struct got_object_id *tree_id1, struct got_object_id *tree_id2,
1349 44392932 2019-08-25 stsp const char *path, int diff_context, struct got_repository *repo)
1351 44392932 2019-08-25 stsp const struct got_error *err = NULL;
1352 44392932 2019-08-25 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
1353 44392932 2019-08-25 stsp struct got_diff_blob_output_unidiff_arg arg;
1355 44392932 2019-08-25 stsp if (tree_id1) {
1356 44392932 2019-08-25 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
1358 44392932 2019-08-25 stsp goto done;
1361 44392932 2019-08-25 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
1363 0f2b3dca 2018-12-22 stsp goto done;
1365 aaa13589 2019-06-01 stsp arg.diff_context = diff_context;
1366 aaa13589 2019-06-01 stsp arg.outfile = stdout;
1367 44392932 2019-08-25 stsp while (path[0] == '/')
1369 44392932 2019-08-25 stsp err = got_diff_tree(tree1, tree2, path, path, repo,
1370 31b4484f 2019-07-27 stsp got_diff_blob_output_unidiff, &arg, 1);
1372 79109fed 2018-03-27 stsp if (tree1)
1373 79109fed 2018-03-27 stsp got_object_tree_close(tree1);
1374 79109fed 2018-03-27 stsp got_object_tree_close(tree2);
1375 44392932 2019-08-25 stsp return err;
1378 44392932 2019-08-25 stsp static const struct got_error *
1379 44392932 2019-08-25 stsp print_patch(struct got_commit_object *commit, struct got_object_id *id,
1380 44392932 2019-08-25 stsp const char *path, int diff_context, struct got_repository *repo)
1382 44392932 2019-08-25 stsp const struct got_error *err = NULL;
1383 44392932 2019-08-25 stsp struct got_commit_object *pcommit = NULL;
1384 44392932 2019-08-25 stsp char *id_str1 = NULL, *id_str2 = NULL;
1385 44392932 2019-08-25 stsp struct got_object_id *obj_id1 = NULL, *obj_id2 = NULL;
1386 44392932 2019-08-25 stsp struct got_object_qid *qid;
1388 44392932 2019-08-25 stsp qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1389 44392932 2019-08-25 stsp if (qid != NULL) {
1390 44392932 2019-08-25 stsp err = got_object_open_as_commit(&pcommit, repo,
1393 44392932 2019-08-25 stsp return err;
1396 44392932 2019-08-25 stsp if (path && path[0] != '\0') {
1397 44392932 2019-08-25 stsp int obj_type;
1398 44392932 2019-08-25 stsp err = got_object_id_by_path(&obj_id2, repo, id, path);
1400 44392932 2019-08-25 stsp goto done;
1401 44392932 2019-08-25 stsp err = got_object_id_str(&id_str2, obj_id2);
1402 44392932 2019-08-25 stsp if (err) {
1403 44392932 2019-08-25 stsp free(obj_id2);
1404 44392932 2019-08-25 stsp goto done;
1406 44392932 2019-08-25 stsp if (pcommit) {
1407 44392932 2019-08-25 stsp err = got_object_id_by_path(&obj_id1, repo,
1408 44392932 2019-08-25 stsp qid->id, path);
1409 44392932 2019-08-25 stsp if (err) {
1410 44392932 2019-08-25 stsp free(obj_id2);
1411 44392932 2019-08-25 stsp goto done;
1413 44392932 2019-08-25 stsp err = got_object_id_str(&id_str1, obj_id1);
1414 44392932 2019-08-25 stsp if (err) {
1415 44392932 2019-08-25 stsp free(obj_id2);
1416 44392932 2019-08-25 stsp goto done;
1419 44392932 2019-08-25 stsp err = got_object_get_type(&obj_type, repo, obj_id2);
1420 44392932 2019-08-25 stsp if (err) {
1421 44392932 2019-08-25 stsp free(obj_id2);
1422 44392932 2019-08-25 stsp goto done;
1424 44392932 2019-08-25 stsp printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1425 44392932 2019-08-25 stsp switch (obj_type) {
1426 44392932 2019-08-25 stsp case GOT_OBJ_TYPE_BLOB:
1427 44392932 2019-08-25 stsp err = diff_blobs(obj_id1, obj_id2, path, diff_context,
1430 44392932 2019-08-25 stsp case GOT_OBJ_TYPE_TREE:
1431 44392932 2019-08-25 stsp err = diff_trees(obj_id1, obj_id2, path, diff_context,
1435 44392932 2019-08-25 stsp err = got_error(GOT_ERR_OBJ_TYPE);
1438 44392932 2019-08-25 stsp free(obj_id1);
1439 44392932 2019-08-25 stsp free(obj_id2);
1441 44392932 2019-08-25 stsp obj_id2 = got_object_commit_get_tree_id(commit);
1442 44392932 2019-08-25 stsp err = got_object_id_str(&id_str2, obj_id2);
1444 44392932 2019-08-25 stsp goto done;
1445 44392932 2019-08-25 stsp obj_id1 = got_object_commit_get_tree_id(pcommit);
1446 44392932 2019-08-25 stsp err = got_object_id_str(&id_str1, obj_id1);
1448 44392932 2019-08-25 stsp goto done;
1449 44392932 2019-08-25 stsp printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1450 44392932 2019-08-25 stsp err = diff_trees(obj_id1, obj_id2, "", diff_context, repo);
1454 0f2b3dca 2018-12-22 stsp free(id_str1);
1455 0f2b3dca 2018-12-22 stsp free(id_str2);
1456 44392932 2019-08-25 stsp if (pcommit)
1457 44392932 2019-08-25 stsp got_object_commit_close(pcommit);
1458 79109fed 2018-03-27 stsp return err;
1461 4bb494d5 2018-06-16 stsp static char *
1462 6c281f94 2018-06-11 stsp get_datestr(time_t *time, char *datebuf)
1464 09867e48 2019-08-13 stsp struct tm mytm, *tm;
1465 09867e48 2019-08-13 stsp char *p, *s;
1467 09867e48 2019-08-13 stsp tm = gmtime_r(time, &mytm);
1468 09867e48 2019-08-13 stsp if (tm == NULL)
1469 09867e48 2019-08-13 stsp return NULL;
1470 09867e48 2019-08-13 stsp s = asctime_r(tm, datebuf);
1471 09867e48 2019-08-13 stsp if (s == NULL)
1472 09867e48 2019-08-13 stsp return NULL;
1473 6c281f94 2018-06-11 stsp p = strchr(s, '\n');
1475 6c281f94 2018-06-11 stsp *p = '\0';
1479 dc424a06 2019-08-07 stsp #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
1481 79109fed 2018-03-27 stsp static const struct got_error *
1482 79109fed 2018-03-27 stsp print_commit(struct got_commit_object *commit, struct got_object_id *id,
1483 44392932 2019-08-25 stsp struct got_repository *repo, const char *path, int show_patch,
1484 44392932 2019-08-25 stsp int diff_context, struct got_reflist_head *refs)
1486 79109fed 2018-03-27 stsp const struct got_error *err = NULL;
1487 621015ac 2018-07-23 stsp char *id_str, *datestr, *logmsg0, *logmsg, *line;
1488 6c281f94 2018-06-11 stsp char datebuf[26];
1489 45d799e2 2018-12-23 stsp time_t committer_time;
1490 45d799e2 2018-12-23 stsp const char *author, *committer;
1491 199a4027 2019-02-02 stsp char *refs_str = NULL;
1492 199a4027 2019-02-02 stsp struct got_reflist_entry *re;
1494 199a4027 2019-02-02 stsp SIMPLEQ_FOREACH(re, refs, entry) {
1496 199a4027 2019-02-02 stsp const char *name;
1497 a436ad14 2019-08-13 stsp struct got_tag_object *tag = NULL;
1500 199a4027 2019-02-02 stsp name = got_ref_get_name(re->ref);
1501 d9498b20 2019-02-05 stsp if (strcmp(name, GOT_REF_HEAD) == 0)
1503 199a4027 2019-02-02 stsp if (strncmp(name, "refs/", 5) == 0)
1504 199a4027 2019-02-02 stsp name += 5;
1505 7143d404 2019-03-12 stsp if (strncmp(name, "got/", 4) == 0)
1507 e34f9ed6 2019-02-02 stsp if (strncmp(name, "heads/", 6) == 0)
1508 e34f9ed6 2019-02-02 stsp name += 6;
1509 141c2bff 2019-02-04 stsp if (strncmp(name, "remotes/", 8) == 0)
1510 141c2bff 2019-02-04 stsp name += 8;
1511 a436ad14 2019-08-13 stsp if (strncmp(name, "tags/", 5) == 0) {
1512 a436ad14 2019-08-13 stsp err = got_object_open_as_tag(&tag, repo, re->id);
1513 5d844a1e 2019-08-13 stsp if (err) {
1514 5d844a1e 2019-08-13 stsp if (err->code != GOT_ERR_OBJ_TYPE)
1515 5d844a1e 2019-08-13 stsp return err;
1516 5d844a1e 2019-08-13 stsp /* Ref points at something other than a tag. */
1517 5d844a1e 2019-08-13 stsp err = NULL;
1518 5d844a1e 2019-08-13 stsp tag = NULL;
1521 a436ad14 2019-08-13 stsp cmp = got_object_id_cmp(tag ?
1522 a436ad14 2019-08-13 stsp got_object_tag_get_object_id(tag) : re->id, id);
1524 a436ad14 2019-08-13 stsp got_object_tag_close(tag);
1525 a436ad14 2019-08-13 stsp if (cmp != 0)
1527 199a4027 2019-02-02 stsp s = refs_str;
1528 199a4027 2019-02-02 stsp if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
1529 199a4027 2019-02-02 stsp name) == -1) {
1530 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1532 34ca4898 2019-08-13 stsp return err;
1536 832c249c 2018-06-10 stsp err = got_object_id_str(&id_str, id);
1538 8bf5b3c9 2018-03-17 stsp return err;
1540 dc424a06 2019-08-07 stsp printf(GOT_COMMIT_SEP_STR);
1541 199a4027 2019-02-02 stsp printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
1542 199a4027 2019-02-02 stsp refs_str ? refs_str : "", refs_str ? ")" : "");
1543 832c249c 2018-06-10 stsp free(id_str);
1544 d3d493d7 2019-02-21 stsp id_str = NULL;
1545 d3d493d7 2019-02-21 stsp free(refs_str);
1546 d3d493d7 2019-02-21 stsp refs_str = NULL;
1547 45d799e2 2018-12-23 stsp printf("from: %s\n", got_object_commit_get_author(commit));
1548 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
1549 45d799e2 2018-12-23 stsp datestr = get_datestr(&committer_time, datebuf);
1550 09867e48 2019-08-13 stsp if (datestr)
1551 09867e48 2019-08-13 stsp printf("date: %s UTC\n", datestr);
1552 45d799e2 2018-12-23 stsp author = got_object_commit_get_author(commit);
1553 45d799e2 2018-12-23 stsp committer = got_object_commit_get_committer(commit);
1554 45d799e2 2018-12-23 stsp if (strcmp(author, committer) != 0)
1555 45d799e2 2018-12-23 stsp printf("via: %s\n", committer);
1556 45d799e2 2018-12-23 stsp if (got_object_commit_get_nparents(commit) > 1) {
1557 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
1558 79f35eb3 2018-06-11 stsp struct got_object_qid *qid;
1559 3fe1abad 2018-06-10 stsp int n = 1;
1560 45d799e2 2018-12-23 stsp parent_ids = got_object_commit_get_parent_ids(commit);
1561 45d799e2 2018-12-23 stsp SIMPLEQ_FOREACH(qid, parent_ids, entry) {
1562 79f35eb3 2018-06-11 stsp err = got_object_id_str(&id_str, qid->id);
1564 a0603db2 2018-06-10 stsp return err;
1565 3fe1abad 2018-06-10 stsp printf("parent %d: %s\n", n++, id_str);
1566 a0603db2 2018-06-10 stsp free(id_str);
1570 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
1572 5943eee2 2019-08-13 stsp return err;
1574 621015ac 2018-07-23 stsp logmsg = logmsg0;
1576 832c249c 2018-06-10 stsp line = strsep(&logmsg, "\n");
1578 832c249c 2018-06-10 stsp printf(" %s\n", line);
1579 832c249c 2018-06-10 stsp } while (line);
1580 621015ac 2018-07-23 stsp free(logmsg0);
1582 971751ac 2018-03-27 stsp if (show_patch) {
1583 44392932 2019-08-25 stsp err = print_patch(commit, id, path, diff_context, repo);
1584 971751ac 2018-03-27 stsp if (err == 0)
1585 971751ac 2018-03-27 stsp printf("\n");
1588 cbe7f848 2019-02-11 stsp if (fflush(stdout) != 0 && err == NULL)
1589 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
1590 07862c20 2018-09-15 stsp return err;
1593 f42b1b34 2018-03-12 stsp static const struct got_error *
1594 15a94983 2018-12-23 stsp print_commits(struct got_object_id *root_id, struct got_repository *repo,
1595 15a94983 2018-12-23 stsp char *path, int show_patch, int diff_context, int limit,
1596 199a4027 2019-02-02 stsp int first_parent_traversal, struct got_reflist_head *refs)
1598 f42b1b34 2018-03-12 stsp const struct got_error *err;
1599 372ccdbb 2018-06-10 stsp struct got_commit_graph *graph;
1601 31cedeaf 2018-09-15 stsp err = got_commit_graph_open(&graph, root_id, path,
1602 31cedeaf 2018-09-15 stsp first_parent_traversal, repo);
1604 f42b1b34 2018-03-12 stsp return err;
1605 6fb7cd11 2019-08-22 stsp err = got_commit_graph_iter_start(graph, root_id, repo,
1606 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
1608 fcc85cad 2018-07-22 stsp goto done;
1610 372ccdbb 2018-06-10 stsp struct got_commit_object *commit;
1611 372ccdbb 2018-06-10 stsp struct got_object_id *id;
1613 84453469 2018-11-11 stsp if (sigint_received || sigpipe_received)
1616 b43fbaa0 2018-06-11 stsp err = got_commit_graph_iter_next(&id, graph);
1617 372ccdbb 2018-06-10 stsp if (err) {
1618 9ba79e04 2018-06-11 stsp if (err->code == GOT_ERR_ITER_COMPLETED) {
1619 9ba79e04 2018-06-11 stsp err = NULL;
1622 372ccdbb 2018-06-10 stsp if (err->code != GOT_ERR_ITER_NEED_MORE)
1624 6fb7cd11 2019-08-22 stsp err = got_commit_graph_fetch_commits(graph, 1, repo,
1625 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
1631 b43fbaa0 2018-06-11 stsp if (id == NULL)
1634 f8e900f3 2018-06-11 stsp err = got_object_open_as_commit(&commit, repo, id);
1637 44392932 2019-08-25 stsp err = print_commit(commit, id, repo, path, show_patch,
1638 44392932 2019-08-25 stsp diff_context, refs);
1639 b43fbaa0 2018-06-11 stsp got_object_commit_close(commit);
1640 372ccdbb 2018-06-10 stsp if (err || (limit && --limit == 0))
1644 372ccdbb 2018-06-10 stsp got_commit_graph_close(graph);
1645 f42b1b34 2018-03-12 stsp return err;
1648 4ed7e80c 2018-05-20 stsp __dead static void
1649 6f3d1eb0 2018-03-12 stsp usage_log(void)
1651 cc54c501 2019-07-15 stsp fprintf(stderr, "usage: %s log [-c commit] [-C number] [-f] [ -l N ] [-p] "
1652 04ca23f4 2018-07-16 stsp "[-r repository-path] [path]\n", getprogname());
1656 b1ebc001 2019-08-13 stsp static int
1657 b1ebc001 2019-08-13 stsp get_default_log_limit(void)
1659 b1ebc001 2019-08-13 stsp const char *got_default_log_limit;
1660 b1ebc001 2019-08-13 stsp long long n;
1661 b1ebc001 2019-08-13 stsp const char *errstr;
1663 b1ebc001 2019-08-13 stsp got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
1664 b1ebc001 2019-08-13 stsp if (got_default_log_limit == NULL)
1666 b1ebc001 2019-08-13 stsp n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
1667 b1ebc001 2019-08-13 stsp if (errstr != NULL)
1672 4ed7e80c 2018-05-20 stsp static const struct got_error *
1673 f42b1b34 2018-03-12 stsp cmd_log(int argc, char *argv[])
1675 f42b1b34 2018-03-12 stsp const struct got_error *error;
1676 04ca23f4 2018-07-16 stsp struct got_repository *repo = NULL;
1677 cffc0aa4 2019-02-05 stsp struct got_worktree *worktree = NULL;
1678 15a94983 2018-12-23 stsp struct got_commit_object *commit = NULL;
1679 3235492e 2018-04-01 stsp struct got_object_id *id = NULL;
1680 04ca23f4 2018-07-16 stsp char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
1681 d142fc45 2018-04-01 stsp char *start_commit = NULL;
1682 c0cc5c62 2018-10-18 stsp int diff_context = 3, ch;
1683 1fd6d7ea 2018-06-13 stsp int show_patch = 0, limit = 0, first_parent_traversal = 0;
1684 64a96a6d 2018-04-01 stsp const char *errstr;
1685 199a4027 2019-02-02 stsp struct got_reflist_head refs;
1687 1b3893a2 2019-03-18 stsp SIMPLEQ_INIT(&refs);
1689 6715a751 2018-03-16 stsp #ifndef PROFILE
1690 6098196c 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1693 f42b1b34 2018-03-12 stsp err(1, "pledge");
1696 b1ebc001 2019-08-13 stsp limit = get_default_log_limit();
1698 cc54c501 2019-07-15 stsp while ((ch = getopt(argc, argv, "b:pc:C:l:fr:")) != -1) {
1699 79109fed 2018-03-27 stsp switch (ch) {
1701 79109fed 2018-03-27 stsp show_patch = 1;
1704 d142fc45 2018-04-01 stsp start_commit = optarg;
1707 4a8520aa 2018-10-18 stsp diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
1709 c0cc5c62 2018-10-18 stsp if (errstr != NULL)
1710 c0cc5c62 2018-10-18 stsp err(1, "-C option %s", errstr);
1713 b1ebc001 2019-08-13 stsp limit = strtonum(optarg, 0, INT_MAX, &errstr);
1714 64a96a6d 2018-04-01 stsp if (errstr != NULL)
1715 64a96a6d 2018-04-01 stsp err(1, "-l option %s", errstr);
1718 cc54c501 2019-07-15 stsp first_parent_traversal = 1;
1721 04ca23f4 2018-07-16 stsp repo_path = realpath(optarg, NULL);
1722 04ca23f4 2018-07-16 stsp if (repo_path == NULL)
1723 04ca23f4 2018-07-16 stsp err(1, "-r option");
1724 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
1727 2deda0b9 2019-03-07 stsp usage_log();
1728 79109fed 2018-03-27 stsp /* NOTREACHED */
1732 79109fed 2018-03-27 stsp argc -= optind;
1733 79109fed 2018-03-27 stsp argv += optind;
1735 04ca23f4 2018-07-16 stsp cwd = getcwd(NULL, 0);
1736 04ca23f4 2018-07-16 stsp if (cwd == NULL) {
1737 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
1738 04ca23f4 2018-07-16 stsp goto done;
1741 cffc0aa4 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
1742 cffc0aa4 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
1743 cffc0aa4 2019-02-05 stsp goto done;
1744 cffc0aa4 2019-02-05 stsp error = NULL;
1746 e7301579 2019-03-18 stsp if (argc == 0) {
1747 cbd1af7a 2019-03-18 stsp path = strdup("");
1748 e7301579 2019-03-18 stsp if (path == NULL) {
1749 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
1750 cbd1af7a 2019-03-18 stsp goto done;
1752 e7301579 2019-03-18 stsp } else if (argc == 1) {
1753 e7301579 2019-03-18 stsp if (worktree) {
1754 e7301579 2019-03-18 stsp error = got_worktree_resolve_path(&path, worktree,
1756 e7301579 2019-03-18 stsp if (error)
1757 e7301579 2019-03-18 stsp goto done;
1759 e7301579 2019-03-18 stsp path = strdup(argv[0]);
1760 e7301579 2019-03-18 stsp if (path == NULL) {
1761 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
1762 e7301579 2019-03-18 stsp goto done;
1766 cbd1af7a 2019-03-18 stsp usage_log();
1768 5486daa2 2019-05-11 stsp if (repo_path == NULL) {
1769 5486daa2 2019-05-11 stsp repo_path = worktree ?
1770 5486daa2 2019-05-11 stsp strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
1772 04ca23f4 2018-07-16 stsp if (repo_path == NULL) {
1773 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
1774 cffc0aa4 2019-02-05 stsp goto done;
1777 f42b1b34 2018-03-12 stsp error = got_repo_open(&repo, repo_path);
1778 d7d4f210 2018-03-12 stsp if (error != NULL)
1779 04ca23f4 2018-07-16 stsp goto done;
1781 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), 1,
1782 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
1783 c02c541e 2019-03-29 stsp if (error)
1784 c02c541e 2019-03-29 stsp goto done;
1786 d142fc45 2018-04-01 stsp if (start_commit == NULL) {
1787 3235492e 2018-04-01 stsp struct got_reference *head_ref;
1788 1cc14b9f 2019-05-14 stsp error = got_ref_open(&head_ref, repo,
1789 1cc14b9f 2019-05-14 stsp worktree ? got_worktree_get_head_ref_name(worktree)
1790 1cc14b9f 2019-05-14 stsp : GOT_REF_HEAD, 0);
1791 3235492e 2018-04-01 stsp if (error != NULL)
1792 3235492e 2018-04-01 stsp return error;
1793 3235492e 2018-04-01 stsp error = got_ref_resolve(&id, repo, head_ref);
1794 3235492e 2018-04-01 stsp got_ref_close(head_ref);
1795 3235492e 2018-04-01 stsp if (error != NULL)
1796 3235492e 2018-04-01 stsp return error;
1797 15a94983 2018-12-23 stsp error = got_object_open_as_commit(&commit, repo, id);
1799 d1f2edc9 2018-06-13 stsp struct got_reference *ref;
1800 2f17228e 2019-05-12 stsp error = got_ref_open(&ref, repo, start_commit, 0);
1801 3235492e 2018-04-01 stsp if (error == NULL) {
1802 1caad61b 2019-02-01 stsp int obj_type;
1803 d1f2edc9 2018-06-13 stsp error = got_ref_resolve(&id, repo, ref);
1804 d1f2edc9 2018-06-13 stsp got_ref_close(ref);
1805 d1f2edc9 2018-06-13 stsp if (error != NULL)
1806 1caad61b 2019-02-01 stsp goto done;
1807 1caad61b 2019-02-01 stsp error = got_object_get_type(&obj_type, repo, id);
1808 1caad61b 2019-02-01 stsp if (error != NULL)
1809 1caad61b 2019-02-01 stsp goto done;
1810 1caad61b 2019-02-01 stsp if (obj_type == GOT_OBJ_TYPE_TAG) {
1811 1caad61b 2019-02-01 stsp struct got_tag_object *tag;
1812 1caad61b 2019-02-01 stsp error = got_object_open_as_tag(&tag, repo, id);
1813 1caad61b 2019-02-01 stsp if (error != NULL)
1814 1caad61b 2019-02-01 stsp goto done;
1815 1caad61b 2019-02-01 stsp if (got_object_tag_get_object_type(tag) !=
1816 1caad61b 2019-02-01 stsp GOT_OBJ_TYPE_COMMIT) {
1817 1caad61b 2019-02-01 stsp got_object_tag_close(tag);
1818 1caad61b 2019-02-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
1819 1caad61b 2019-02-01 stsp goto done;
1822 1caad61b 2019-02-01 stsp id = got_object_id_dup(
1823 1caad61b 2019-02-01 stsp got_object_tag_get_object_id(tag));
1824 1caad61b 2019-02-01 stsp if (id == NULL)
1825 638f9024 2019-05-13 stsp error = got_error_from_errno(
1826 230a42bd 2019-05-11 jcs "got_object_id_dup");
1827 1caad61b 2019-02-01 stsp got_object_tag_close(tag);
1828 1caad61b 2019-02-01 stsp if (error)
1829 1caad61b 2019-02-01 stsp goto done;
1830 1caad61b 2019-02-01 stsp } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
1831 1caad61b 2019-02-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
1832 1caad61b 2019-02-01 stsp goto done;
1834 15a94983 2018-12-23 stsp error = got_object_open_as_commit(&commit, repo, id);
1835 d1f2edc9 2018-06-13 stsp if (error != NULL)
1836 1caad61b 2019-02-01 stsp goto done;
1838 15a94983 2018-12-23 stsp if (commit == NULL) {
1839 e09a504c 2019-06-28 stsp error = got_repo_match_object_id_prefix(&id,
1840 dd88155e 2019-06-29 stsp start_commit, GOT_OBJ_TYPE_COMMIT, repo);
1841 d1f2edc9 2018-06-13 stsp if (error != NULL)
1842 d1f2edc9 2018-06-13 stsp return error;
1845 d7d4f210 2018-03-12 stsp if (error != NULL)
1846 04ca23f4 2018-07-16 stsp goto done;
1848 deeabeae 2019-08-27 stsp if (worktree) {
1849 deeabeae 2019-08-27 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
1851 deeabeae 2019-08-27 stsp if (asprintf(&p, "%s%s%s", prefix,
1852 deeabeae 2019-08-27 stsp (strcmp(prefix, "/") != 0) ? "/" : "", path) == -1) {
1853 deeabeae 2019-08-27 stsp error = got_error_from_errno("asprintf");
1854 deeabeae 2019-08-27 stsp goto done;
1856 deeabeae 2019-08-27 stsp error = got_repo_map_path(&in_repo_path, repo, p, 1);
1859 deeabeae 2019-08-27 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
1860 04ca23f4 2018-07-16 stsp if (error != NULL)
1861 04ca23f4 2018-07-16 stsp goto done;
1862 04ca23f4 2018-07-16 stsp if (in_repo_path) {
1863 04ca23f4 2018-07-16 stsp free(path);
1864 04ca23f4 2018-07-16 stsp path = in_repo_path;
1867 b8bad2ba 2019-08-23 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
1868 199a4027 2019-02-02 stsp if (error)
1869 199a4027 2019-02-02 stsp goto done;
1871 15a94983 2018-12-23 stsp error = print_commits(id, repo, path, show_patch,
1872 199a4027 2019-02-02 stsp diff_context, limit, first_parent_traversal, &refs);
1874 04ca23f4 2018-07-16 stsp free(path);
1875 04ca23f4 2018-07-16 stsp free(repo_path);
1876 04ca23f4 2018-07-16 stsp free(cwd);
1878 cffc0aa4 2019-02-05 stsp if (worktree)
1879 cffc0aa4 2019-02-05 stsp got_worktree_close(worktree);
1880 ad242220 2018-09-08 stsp if (repo) {
1881 ad242220 2018-09-08 stsp const struct got_error *repo_error;
1882 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
1883 ad242220 2018-09-08 stsp if (error == NULL)
1884 ad242220 2018-09-08 stsp error = repo_error;
1886 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
1887 8bf5b3c9 2018-03-17 stsp return error;
1890 4ed7e80c 2018-05-20 stsp __dead static void
1891 3f8b7d6a 2018-04-01 stsp usage_diff(void)
1893 98eaaa12 2019-08-03 stsp fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] [-s] "
1894 927df6b7 2019-02-10 stsp "[object1 object2 | path]\n", getprogname());
1898 b72f483a 2019-02-05 stsp struct print_diff_arg {
1899 b72f483a 2019-02-05 stsp struct got_repository *repo;
1900 b72f483a 2019-02-05 stsp struct got_worktree *worktree;
1901 b72f483a 2019-02-05 stsp int diff_context;
1902 3fc0c068 2019-02-10 stsp const char *id_str;
1903 3fc0c068 2019-02-10 stsp int header_shown;
1904 98eaaa12 2019-08-03 stsp int diff_staged;
1907 4ed7e80c 2018-05-20 stsp static const struct got_error *
1908 88d0e355 2019-08-03 stsp print_diff(void *arg, unsigned char status, unsigned char staged_status,
1909 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
1910 537ac44b 2019-08-03 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
1912 b72f483a 2019-02-05 stsp struct print_diff_arg *a = arg;
1913 b72f483a 2019-02-05 stsp const struct got_error *err = NULL;
1914 b72f483a 2019-02-05 stsp struct got_blob_object *blob1 = NULL;
1915 b72f483a 2019-02-05 stsp FILE *f2 = NULL;
1916 4ce46740 2019-08-08 stsp char *abspath = NULL, *label1 = NULL;
1917 b72f483a 2019-02-05 stsp struct stat sb;
1919 98eaaa12 2019-08-03 stsp if (a->diff_staged) {
1920 98eaaa12 2019-08-03 stsp if (staged_status != GOT_STATUS_MODIFY &&
1921 98eaaa12 2019-08-03 stsp staged_status != GOT_STATUS_ADD &&
1922 98eaaa12 2019-08-03 stsp staged_status != GOT_STATUS_DELETE)
1923 98eaaa12 2019-08-03 stsp return NULL;
1925 98eaaa12 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
1926 98eaaa12 2019-08-03 stsp return NULL;
1927 2a06fe5f 2019-08-24 stsp if (status == GOT_STATUS_NONEXISTENT)
1928 2a06fe5f 2019-08-24 stsp return got_error_set_errno(ENOENT, path);
1929 98eaaa12 2019-08-03 stsp if (status != GOT_STATUS_MODIFY &&
1930 98eaaa12 2019-08-03 stsp status != GOT_STATUS_ADD &&
1931 98eaaa12 2019-08-03 stsp status != GOT_STATUS_DELETE &&
1932 98eaaa12 2019-08-03 stsp status != GOT_STATUS_CONFLICT)
1933 98eaaa12 2019-08-03 stsp return NULL;
1936 3fc0c068 2019-02-10 stsp if (!a->header_shown) {
1937 98eaaa12 2019-08-03 stsp printf("diff %s %s%s\n", a->id_str,
1938 98eaaa12 2019-08-03 stsp got_worktree_get_root_path(a->worktree),
1939 98eaaa12 2019-08-03 stsp a->diff_staged ? " (staged changes)" : "");
1940 3fc0c068 2019-02-10 stsp a->header_shown = 1;
1943 98eaaa12 2019-08-03 stsp if (a->diff_staged) {
1944 98eaaa12 2019-08-03 stsp const char *label1 = NULL, *label2 = NULL;
1945 98eaaa12 2019-08-03 stsp switch (staged_status) {
1946 98eaaa12 2019-08-03 stsp case GOT_STATUS_MODIFY:
1947 98eaaa12 2019-08-03 stsp label1 = path;
1948 98eaaa12 2019-08-03 stsp label2 = path;
1950 98eaaa12 2019-08-03 stsp case GOT_STATUS_ADD:
1951 98eaaa12 2019-08-03 stsp label2 = path;
1953 98eaaa12 2019-08-03 stsp case GOT_STATUS_DELETE:
1954 98eaaa12 2019-08-03 stsp label1 = path;
1957 98eaaa12 2019-08-03 stsp return got_error(GOT_ERR_FILE_STATUS);
1959 98eaaa12 2019-08-03 stsp return got_diff_objects_as_blobs(blob_id, staged_blob_id,
1960 98eaaa12 2019-08-03 stsp label1, label2, a->diff_context, a->repo, stdout);
1963 408b4ebc 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
1964 4ce46740 2019-08-08 stsp staged_status == GOT_STATUS_MODIFY) {
1965 4ce46740 2019-08-08 stsp char *id_str;
1966 408b4ebc 2019-08-03 stsp err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
1969 4ce46740 2019-08-08 stsp goto done;
1970 4ce46740 2019-08-08 stsp err = got_object_id_str(&id_str, staged_blob_id);
1972 4ce46740 2019-08-08 stsp goto done;
1973 4ce46740 2019-08-08 stsp if (asprintf(&label1, "%s (staged)", id_str) == -1) {
1974 4ce46740 2019-08-08 stsp err = got_error_from_errno("asprintf");
1975 4ce46740 2019-08-08 stsp free(id_str);
1976 4ce46740 2019-08-08 stsp goto done;
1978 4ce46740 2019-08-08 stsp free(id_str);
1979 4ce46740 2019-08-08 stsp } else if (status != GOT_STATUS_ADD) {
1980 016a88dd 2019-05-13 stsp err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
1982 4ce46740 2019-08-08 stsp goto done;
1985 7154f6ce 2019-03-27 stsp if (status != GOT_STATUS_DELETE) {
1986 2ec1f75b 2019-03-26 stsp if (asprintf(&abspath, "%s/%s",
1987 2ec1f75b 2019-03-26 stsp got_worktree_get_root_path(a->worktree), path) == -1) {
1988 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1989 2ec1f75b 2019-03-26 stsp goto done;
1992 2ec1f75b 2019-03-26 stsp f2 = fopen(abspath, "r");
1993 2ec1f75b 2019-03-26 stsp if (f2 == NULL) {
1994 638f9024 2019-05-13 stsp err = got_error_from_errno2("fopen", abspath);
1995 2ec1f75b 2019-03-26 stsp goto done;
1997 2ec1f75b 2019-03-26 stsp if (lstat(abspath, &sb) == -1) {
1998 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", abspath);
1999 2ec1f75b 2019-03-26 stsp goto done;
2002 2ec1f75b 2019-03-26 stsp sb.st_size = 0;
2004 4ce46740 2019-08-08 stsp err = got_diff_blob_file(blob1, label1, f2, sb.st_size, path,
2005 4ce46740 2019-08-08 stsp a->diff_context, stdout);
2007 b72f483a 2019-02-05 stsp if (blob1)
2008 b72f483a 2019-02-05 stsp got_object_blob_close(blob1);
2009 fb43ecf1 2019-02-11 stsp if (f2 && fclose(f2) != 0 && err == NULL)
2010 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
2011 b72f483a 2019-02-05 stsp free(abspath);
2012 927df6b7 2019-02-10 stsp return err;
2015 927df6b7 2019-02-10 stsp static const struct got_error *
2016 0adb7196 2019-08-11 stsp match_object_id(struct got_object_id **id, char **label,
2017 01073a5d 2019-08-22 stsp const char *id_str, int obj_type, int resolve_tags,
2018 01073a5d 2019-08-22 stsp struct got_repository *repo)
2020 0adb7196 2019-08-11 stsp const struct got_error *err;
2021 d24820bf 2019-08-11 stsp struct got_tag_object *tag;
2022 0adb7196 2019-08-11 stsp struct got_reference *ref = NULL;
2024 0adb7196 2019-08-11 stsp *id = NULL;
2025 0adb7196 2019-08-11 stsp *label = NULL;
2027 01073a5d 2019-08-22 stsp if (resolve_tags) {
2028 01073a5d 2019-08-22 stsp err = got_repo_object_match_tag(&tag, id_str, GOT_OBJ_TYPE_ANY,
2030 01073a5d 2019-08-22 stsp if (err == NULL) {
2031 01073a5d 2019-08-22 stsp *id = got_object_id_dup(
2032 01073a5d 2019-08-22 stsp got_object_tag_get_object_id(tag));
2033 01073a5d 2019-08-22 stsp if (*id == NULL)
2034 01073a5d 2019-08-22 stsp err = got_error_from_errno("got_object_id_dup");
2035 01073a5d 2019-08-22 stsp else if (asprintf(label, "refs/tags/%s",
2036 01073a5d 2019-08-22 stsp got_object_tag_get_name(tag)) == -1) {
2037 01073a5d 2019-08-22 stsp err = got_error_from_errno("asprintf");
2038 32f0ab81 2019-08-28 hiltjo free(*id);
2039 01073a5d 2019-08-22 stsp *id = NULL;
2041 01073a5d 2019-08-22 stsp got_object_tag_close(tag);
2042 01073a5d 2019-08-22 stsp return err;
2043 01073a5d 2019-08-22 stsp } else if (err->code != GOT_ERR_NO_OBJ)
2044 01073a5d 2019-08-22 stsp return err;
2047 0adb7196 2019-08-11 stsp err = got_repo_match_object_id_prefix(id, id_str, obj_type, repo);
2048 0adb7196 2019-08-11 stsp if (err) {
2049 0adb7196 2019-08-11 stsp if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
2050 0adb7196 2019-08-11 stsp return err;
2051 0adb7196 2019-08-11 stsp err = got_ref_open(&ref, repo, id_str, 0);
2052 0adb7196 2019-08-11 stsp if (err != NULL)
2053 0adb7196 2019-08-11 stsp goto done;
2054 0adb7196 2019-08-11 stsp *label = strdup(got_ref_get_name(ref));
2055 0adb7196 2019-08-11 stsp if (*label == NULL) {
2056 0adb7196 2019-08-11 stsp err = got_error_from_errno("strdup");
2057 0adb7196 2019-08-11 stsp goto done;
2059 0adb7196 2019-08-11 stsp err = got_ref_resolve(id, repo, ref);
2061 0adb7196 2019-08-11 stsp err = got_object_id_str(label, *id);
2062 0adb7196 2019-08-11 stsp if (*label == NULL) {
2063 0adb7196 2019-08-11 stsp err = got_error_from_errno("strdup");
2064 0adb7196 2019-08-11 stsp goto done;
2069 0adb7196 2019-08-11 stsp got_ref_close(ref);
2070 0adb7196 2019-08-11 stsp return err;
2074 0adb7196 2019-08-11 stsp static const struct got_error *
2075 b00d56cd 2018-04-01 stsp cmd_diff(int argc, char *argv[])
2077 b00d56cd 2018-04-01 stsp const struct got_error *error;
2078 b00d56cd 2018-04-01 stsp struct got_repository *repo = NULL;
2079 b72f483a 2019-02-05 stsp struct got_worktree *worktree = NULL;
2080 b72f483a 2019-02-05 stsp char *cwd = NULL, *repo_path = NULL;
2081 15a94983 2018-12-23 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
2082 cd628e99 2019-05-28 stsp const char *id_str1 = NULL, *id_str2 = NULL;
2083 5e70831e 2019-05-28 stsp char *label1 = NULL, *label2 = NULL;
2084 15a94983 2018-12-23 stsp int type1, type2;
2085 98eaaa12 2019-08-03 stsp int diff_context = 3, diff_staged = 0, ch;
2086 c0cc5c62 2018-10-18 stsp const char *errstr;
2087 927df6b7 2019-02-10 stsp char *path = NULL;
2089 b00d56cd 2018-04-01 stsp #ifndef PROFILE
2090 25eccc22 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2091 25eccc22 2019-01-04 stsp NULL) == -1)
2092 b00d56cd 2018-04-01 stsp err(1, "pledge");
2095 98eaaa12 2019-08-03 stsp while ((ch = getopt(argc, argv, "C:r:s")) != -1) {
2096 b00d56cd 2018-04-01 stsp switch (ch) {
2098 c0cc5c62 2018-10-18 stsp diff_context = strtonum(optarg, 1, INT_MAX, &errstr);
2099 c0cc5c62 2018-10-18 stsp if (errstr != NULL)
2100 c0cc5c62 2018-10-18 stsp err(1, "-C option %s", errstr);
2103 b72f483a 2019-02-05 stsp repo_path = realpath(optarg, NULL);
2104 b72f483a 2019-02-05 stsp if (repo_path == NULL)
2105 b72f483a 2019-02-05 stsp err(1, "-r option");
2106 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
2109 98eaaa12 2019-08-03 stsp diff_staged = 1;
2112 2deda0b9 2019-03-07 stsp usage_diff();
2113 b00d56cd 2018-04-01 stsp /* NOTREACHED */
2117 b00d56cd 2018-04-01 stsp argc -= optind;
2118 b00d56cd 2018-04-01 stsp argv += optind;
2120 b72f483a 2019-02-05 stsp cwd = getcwd(NULL, 0);
2121 b72f483a 2019-02-05 stsp if (cwd == NULL) {
2122 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2123 b72f483a 2019-02-05 stsp goto done;
2125 927df6b7 2019-02-10 stsp error = got_worktree_open(&worktree, cwd);
2126 927df6b7 2019-02-10 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
2127 927df6b7 2019-02-10 stsp goto done;
2128 927df6b7 2019-02-10 stsp if (argc <= 1) {
2129 927df6b7 2019-02-10 stsp if (worktree == NULL) {
2130 927df6b7 2019-02-10 stsp error = got_error(GOT_ERR_NOT_WORKTREE);
2131 927df6b7 2019-02-10 stsp goto done;
2133 b72f483a 2019-02-05 stsp if (repo_path)
2135 b72f483a 2019-02-05 stsp "-r option can't be used when diffing a work tree");
2136 b72f483a 2019-02-05 stsp repo_path = strdup(got_worktree_get_repo_path(worktree));
2137 927df6b7 2019-02-10 stsp if (repo_path == NULL) {
2138 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2139 927df6b7 2019-02-10 stsp goto done;
2141 927df6b7 2019-02-10 stsp if (argc == 1) {
2142 6c7ab921 2019-03-18 stsp error = got_worktree_resolve_path(&path, worktree,
2144 927df6b7 2019-02-10 stsp if (error)
2145 927df6b7 2019-02-10 stsp goto done;
2147 927df6b7 2019-02-10 stsp path = strdup("");
2148 927df6b7 2019-02-10 stsp if (path == NULL) {
2149 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2150 927df6b7 2019-02-10 stsp goto done;
2153 b72f483a 2019-02-05 stsp } else if (argc == 2) {
2154 98eaaa12 2019-08-03 stsp if (diff_staged)
2155 98eaaa12 2019-08-03 stsp errx(1, "-s option can't be used when diffing "
2156 98eaaa12 2019-08-03 stsp "objects in repository");
2157 15a94983 2018-12-23 stsp id_str1 = argv[0];
2158 15a94983 2018-12-23 stsp id_str2 = argv[1];
2159 30db809c 2019-06-05 stsp if (worktree && repo_path == NULL) {
2160 30db809c 2019-06-05 stsp repo_path =
2161 30db809c 2019-06-05 stsp strdup(got_worktree_get_repo_path(worktree));
2162 30db809c 2019-06-05 stsp if (repo_path == NULL) {
2163 30db809c 2019-06-05 stsp error = got_error_from_errno("strdup");
2164 30db809c 2019-06-05 stsp goto done;
2168 b00d56cd 2018-04-01 stsp usage_diff();
2170 b72f483a 2019-02-05 stsp if (repo_path == NULL) {
2171 b72f483a 2019-02-05 stsp repo_path = getcwd(NULL, 0);
2172 b72f483a 2019-02-05 stsp if (repo_path == NULL)
2173 638f9024 2019-05-13 stsp return got_error_from_errno("getcwd");
2176 b00d56cd 2018-04-01 stsp error = got_repo_open(&repo, repo_path);
2177 76089277 2018-04-01 stsp free(repo_path);
2178 b00d56cd 2018-04-01 stsp if (error != NULL)
2179 b00d56cd 2018-04-01 stsp goto done;
2181 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), 1,
2182 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
2183 c02c541e 2019-03-29 stsp if (error)
2184 c02c541e 2019-03-29 stsp goto done;
2186 30db809c 2019-06-05 stsp if (argc <= 1) {
2187 b72f483a 2019-02-05 stsp struct print_diff_arg arg;
2188 72ea6654 2019-07-27 stsp struct got_pathlist_head paths;
2189 b72f483a 2019-02-05 stsp char *id_str;
2191 72ea6654 2019-07-27 stsp TAILQ_INIT(&paths);
2193 b72f483a 2019-02-05 stsp error = got_object_id_str(&id_str,
2194 b72f483a 2019-02-05 stsp got_worktree_get_base_commit_id(worktree));
2195 b72f483a 2019-02-05 stsp if (error)
2196 b72f483a 2019-02-05 stsp goto done;
2197 b72f483a 2019-02-05 stsp arg.repo = repo;
2198 b72f483a 2019-02-05 stsp arg.worktree = worktree;
2199 b72f483a 2019-02-05 stsp arg.diff_context = diff_context;
2200 3fc0c068 2019-02-10 stsp arg.id_str = id_str;
2201 3fc0c068 2019-02-10 stsp arg.header_shown = 0;
2202 98eaaa12 2019-08-03 stsp arg.diff_staged = diff_staged;
2204 adc19d55 2019-07-28 stsp error = got_pathlist_append(&paths, path, NULL);
2205 72ea6654 2019-07-27 stsp if (error)
2206 72ea6654 2019-07-27 stsp goto done;
2208 72ea6654 2019-07-27 stsp error = got_worktree_status(worktree, &paths, repo, print_diff,
2209 b72f483a 2019-02-05 stsp &arg, check_cancelled, NULL);
2210 b72f483a 2019-02-05 stsp free(id_str);
2211 72ea6654 2019-07-27 stsp got_pathlist_free(&paths);
2212 b72f483a 2019-02-05 stsp goto done;
2215 01073a5d 2019-08-22 stsp error = match_object_id(&id1, &label1, id_str1, GOT_OBJ_TYPE_ANY, 1,
2217 0adb7196 2019-08-11 stsp if (error)
2218 0adb7196 2019-08-11 stsp goto done;
2220 01073a5d 2019-08-22 stsp error = match_object_id(&id2, &label2, id_str2, GOT_OBJ_TYPE_ANY, 1,
2222 0adb7196 2019-08-11 stsp if (error)
2223 0adb7196 2019-08-11 stsp goto done;
2225 15a94983 2018-12-23 stsp error = got_object_get_type(&type1, repo, id1);
2226 15a94983 2018-12-23 stsp if (error)
2227 15a94983 2018-12-23 stsp goto done;
2229 15a94983 2018-12-23 stsp error = got_object_get_type(&type2, repo, id2);
2230 15a94983 2018-12-23 stsp if (error)
2231 15a94983 2018-12-23 stsp goto done;
2233 15a94983 2018-12-23 stsp if (type1 != type2) {
2234 b00d56cd 2018-04-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
2235 b00d56cd 2018-04-01 stsp goto done;
2238 15a94983 2018-12-23 stsp switch (type1) {
2239 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_BLOB:
2240 15a94983 2018-12-23 stsp error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
2241 54156555 2018-12-24 stsp diff_context, repo, stdout);
2243 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_TREE:
2244 15a94983 2018-12-23 stsp error = got_diff_objects_as_trees(id1, id2, "", "",
2245 54156555 2018-12-24 stsp diff_context, repo, stdout);
2247 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_COMMIT:
2248 5e70831e 2019-05-28 stsp printf("diff %s %s\n", label1, label2);
2249 15a94983 2018-12-23 stsp error = got_diff_objects_as_commits(id1, id2, diff_context,
2250 c0cc5c62 2018-10-18 stsp repo, stdout);
2253 b00d56cd 2018-04-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
2257 5e70831e 2019-05-28 stsp free(label1);
2258 5e70831e 2019-05-28 stsp free(label2);
2259 15a94983 2018-12-23 stsp free(id1);
2260 15a94983 2018-12-23 stsp free(id2);
2261 927df6b7 2019-02-10 stsp free(path);
2262 b72f483a 2019-02-05 stsp if (worktree)
2263 b72f483a 2019-02-05 stsp got_worktree_close(worktree);
2264 ad242220 2018-09-08 stsp if (repo) {
2265 ad242220 2018-09-08 stsp const struct got_error *repo_error;
2266 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
2267 ad242220 2018-09-08 stsp if (error == NULL)
2268 ad242220 2018-09-08 stsp error = repo_error;
2270 b00d56cd 2018-04-01 stsp return error;
2273 404c43c4 2018-06-21 stsp __dead static void
2274 404c43c4 2018-06-21 stsp usage_blame(void)
2276 9270e621 2019-02-05 stsp fprintf(stderr,
2277 9270e621 2019-02-05 stsp "usage: %s blame [-c commit] [-r repository-path] path\n",
2278 404c43c4 2018-06-21 stsp getprogname());
2282 28315671 2019-08-14 stsp struct blame_line {
2283 28315671 2019-08-14 stsp int annotated;
2284 28315671 2019-08-14 stsp char *id_str;
2285 82f6abb8 2019-08-14 stsp char *committer;
2286 bcb49d15 2019-08-14 stsp char datebuf[9]; /* YY-MM-DD + NUL */
2289 28315671 2019-08-14 stsp struct blame_cb_args {
2290 28315671 2019-08-14 stsp struct blame_line *lines;
2291 28315671 2019-08-14 stsp int nlines;
2292 7ef28ff8 2019-08-14 stsp int nlines_prec;
2293 28315671 2019-08-14 stsp int lineno_cur;
2294 28315671 2019-08-14 stsp off_t *line_offsets;
2296 82f6abb8 2019-08-14 stsp struct got_repository *repo;
2299 404c43c4 2018-06-21 stsp static const struct got_error *
2300 28315671 2019-08-14 stsp blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2302 28315671 2019-08-14 stsp const struct got_error *err = NULL;
2303 28315671 2019-08-14 stsp struct blame_cb_args *a = arg;
2304 28315671 2019-08-14 stsp struct blame_line *bline;
2305 82f6abb8 2019-08-14 stsp char *line = NULL;
2306 28315671 2019-08-14 stsp size_t linesize = 0;
2307 82f6abb8 2019-08-14 stsp struct got_commit_object *commit = NULL;
2308 28315671 2019-08-14 stsp off_t offset;
2309 bcb49d15 2019-08-14 stsp struct tm tm;
2310 bcb49d15 2019-08-14 stsp time_t committer_time;
2312 28315671 2019-08-14 stsp if (nlines != a->nlines ||
2313 28315671 2019-08-14 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
2314 28315671 2019-08-14 stsp return got_error(GOT_ERR_RANGE);
2316 28315671 2019-08-14 stsp if (sigint_received)
2317 28315671 2019-08-14 stsp return got_error(GOT_ERR_ITER_COMPLETED);
2319 2839f8b9 2019-08-18 stsp if (lineno == -1)
2320 2839f8b9 2019-08-18 stsp return NULL; /* no change in this commit */
2322 28315671 2019-08-14 stsp /* Annotate this line. */
2323 28315671 2019-08-14 stsp bline = &a->lines[lineno - 1];
2324 28315671 2019-08-14 stsp if (bline->annotated)
2325 28315671 2019-08-14 stsp return NULL;
2326 28315671 2019-08-14 stsp err = got_object_id_str(&bline->id_str, id);
2328 28315671 2019-08-14 stsp return err;
2330 82f6abb8 2019-08-14 stsp err = got_object_open_as_commit(&commit, a->repo, id);
2332 82f6abb8 2019-08-14 stsp goto done;
2334 82f6abb8 2019-08-14 stsp bline->committer = strdup(got_object_commit_get_committer(commit));
2335 82f6abb8 2019-08-14 stsp if (bline->committer == NULL) {
2336 82f6abb8 2019-08-14 stsp err = got_error_from_errno("strdup");
2337 bcb49d15 2019-08-14 stsp goto done;
2340 bcb49d15 2019-08-14 stsp committer_time = got_object_commit_get_committer_time(commit);
2341 bcb49d15 2019-08-14 stsp if (localtime_r(&committer_time, &tm) == NULL)
2342 bcb49d15 2019-08-14 stsp return got_error_from_errno("localtime_r");
2343 bcb49d15 2019-08-14 stsp if (strftime(bline->datebuf, sizeof(bline->datebuf), "%g/%m/%d",
2344 bcb49d15 2019-08-14 stsp &tm) >= sizeof(bline->datebuf)) {
2345 bcb49d15 2019-08-14 stsp err = got_error(GOT_ERR_NO_SPACE);
2346 82f6abb8 2019-08-14 stsp goto done;
2348 28315671 2019-08-14 stsp bline->annotated = 1;
2350 28315671 2019-08-14 stsp /* Print lines annotated so far. */
2351 28315671 2019-08-14 stsp bline = &a->lines[a->lineno_cur - 1];
2352 28315671 2019-08-14 stsp if (!bline->annotated)
2353 82f6abb8 2019-08-14 stsp goto done;
2355 28315671 2019-08-14 stsp offset = a->line_offsets[a->lineno_cur - 1];
2356 82f6abb8 2019-08-14 stsp if (fseeko(a->f, offset, SEEK_SET) == -1) {
2357 82f6abb8 2019-08-14 stsp err = got_error_from_errno("fseeko");
2358 82f6abb8 2019-08-14 stsp goto done;
2361 28315671 2019-08-14 stsp while (bline->annotated) {
2362 82f6abb8 2019-08-14 stsp char *smallerthan, *at, *nl, *committer;
2363 82f6abb8 2019-08-14 stsp size_t len;
2365 28315671 2019-08-14 stsp if (getline(&line, &linesize, a->f) == (ssize_t)-1) {
2366 28315671 2019-08-14 stsp if (ferror(a->f))
2367 28315671 2019-08-14 stsp err = got_error_from_errno("getline");
2371 82f6abb8 2019-08-14 stsp committer = bline->committer;
2372 82f6abb8 2019-08-14 stsp smallerthan = strchr(committer, '<');
2373 82f6abb8 2019-08-14 stsp if (smallerthan && smallerthan[1] != '\0')
2374 82f6abb8 2019-08-14 stsp committer = smallerthan + 1;
2375 82f6abb8 2019-08-14 stsp at = strchr(committer, '@');
2377 82f6abb8 2019-08-14 stsp *at = '\0';
2378 82f6abb8 2019-08-14 stsp len = strlen(committer);
2379 82f6abb8 2019-08-14 stsp if (len >= 9)
2380 82f6abb8 2019-08-14 stsp committer[8] = '\0';
2382 28315671 2019-08-14 stsp nl = strchr(line, '\n');
2384 28315671 2019-08-14 stsp *nl = '\0';
2385 bcb49d15 2019-08-14 stsp printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
2386 bcb49d15 2019-08-14 stsp bline->id_str, bline->datebuf, committer, line);
2388 28315671 2019-08-14 stsp a->lineno_cur++;
2389 28315671 2019-08-14 stsp bline = &a->lines[a->lineno_cur - 1];
2392 82f6abb8 2019-08-14 stsp if (commit)
2393 82f6abb8 2019-08-14 stsp got_object_commit_close(commit);
2394 28315671 2019-08-14 stsp free(line);
2395 28315671 2019-08-14 stsp return err;
2398 28315671 2019-08-14 stsp static const struct got_error *
2399 404c43c4 2018-06-21 stsp cmd_blame(int argc, char *argv[])
2401 404c43c4 2018-06-21 stsp const struct got_error *error;
2402 404c43c4 2018-06-21 stsp struct got_repository *repo = NULL;
2403 0c06baac 2019-02-05 stsp struct got_worktree *worktree = NULL;
2404 66bea077 2018-08-02 stsp char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2405 28315671 2019-08-14 stsp struct got_object_id *obj_id = NULL;
2406 404c43c4 2018-06-21 stsp struct got_object_id *commit_id = NULL;
2407 28315671 2019-08-14 stsp struct got_blob_object *blob = NULL;
2408 404c43c4 2018-06-21 stsp char *commit_id_str = NULL;
2409 28315671 2019-08-14 stsp struct blame_cb_args bca;
2410 28315671 2019-08-14 stsp int ch, obj_type, i;
2411 b02560ec 2019-08-19 stsp size_t filesize;
2413 90f3c347 2019-08-19 stsp memset(&bca, 0, sizeof(bca));
2415 404c43c4 2018-06-21 stsp #ifndef PROFILE
2416 36e2fb66 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2417 36e2fb66 2019-01-04 stsp NULL) == -1)
2418 404c43c4 2018-06-21 stsp err(1, "pledge");
2421 66bea077 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2422 404c43c4 2018-06-21 stsp switch (ch) {
2424 404c43c4 2018-06-21 stsp commit_id_str = optarg;
2427 66bea077 2018-08-02 stsp repo_path = realpath(optarg, NULL);
2428 66bea077 2018-08-02 stsp if (repo_path == NULL)
2429 66bea077 2018-08-02 stsp err(1, "-r option");
2430 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
2433 2deda0b9 2019-03-07 stsp usage_blame();
2434 404c43c4 2018-06-21 stsp /* NOTREACHED */
2438 404c43c4 2018-06-21 stsp argc -= optind;
2439 404c43c4 2018-06-21 stsp argv += optind;
2441 a39318fd 2018-08-02 stsp if (argc == 1)
2442 404c43c4 2018-06-21 stsp path = argv[0];
2444 404c43c4 2018-06-21 stsp usage_blame();
2446 66bea077 2018-08-02 stsp cwd = getcwd(NULL, 0);
2447 66bea077 2018-08-02 stsp if (cwd == NULL) {
2448 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2449 66bea077 2018-08-02 stsp goto done;
2451 66bea077 2018-08-02 stsp if (repo_path == NULL) {
2452 0c06baac 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
2453 0c06baac 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
2454 66bea077 2018-08-02 stsp goto done;
2456 0c06baac 2019-02-05 stsp error = NULL;
2457 0c06baac 2019-02-05 stsp if (worktree) {
2458 0c06baac 2019-02-05 stsp repo_path =
2459 0c06baac 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
2460 0c06baac 2019-02-05 stsp if (repo_path == NULL)
2461 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2462 0c06baac 2019-02-05 stsp if (error)
2463 0c06baac 2019-02-05 stsp goto done;
2465 0c06baac 2019-02-05 stsp repo_path = strdup(cwd);
2466 0c06baac 2019-02-05 stsp if (repo_path == NULL) {
2467 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2468 0c06baac 2019-02-05 stsp goto done;
2473 c02c541e 2019-03-29 stsp error = got_repo_open(&repo, repo_path);
2474 c02c541e 2019-03-29 stsp if (error != NULL)
2475 36e2fb66 2019-01-04 stsp goto done;
2477 c530dc23 2019-07-23 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2478 c02c541e 2019-03-29 stsp if (error)
2479 404c43c4 2018-06-21 stsp goto done;
2481 0c06baac 2019-02-05 stsp if (worktree) {
2482 6efaaa2d 2019-02-05 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
2483 0c06baac 2019-02-05 stsp char *p, *worktree_subdir = cwd +
2484 0c06baac 2019-02-05 stsp strlen(got_worktree_get_root_path(worktree));
2485 6efaaa2d 2019-02-05 stsp if (asprintf(&p, "%s%s%s%s%s",
2486 6efaaa2d 2019-02-05 stsp prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
2487 6efaaa2d 2019-02-05 stsp worktree_subdir, worktree_subdir[0] ? "/" : "",
2488 6efaaa2d 2019-02-05 stsp path) == -1) {
2489 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
2490 0c06baac 2019-02-05 stsp goto done;
2492 6efaaa2d 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, p, 0);
2495 0c06baac 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
2497 0c06baac 2019-02-05 stsp if (error)
2498 66bea077 2018-08-02 stsp goto done;
2500 404c43c4 2018-06-21 stsp if (commit_id_str == NULL) {
2501 404c43c4 2018-06-21 stsp struct got_reference *head_ref;
2502 2f17228e 2019-05-12 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
2503 404c43c4 2018-06-21 stsp if (error != NULL)
2504 66bea077 2018-08-02 stsp goto done;
2505 404c43c4 2018-06-21 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
2506 404c43c4 2018-06-21 stsp got_ref_close(head_ref);
2507 404c43c4 2018-06-21 stsp if (error != NULL)
2508 66bea077 2018-08-02 stsp goto done;
2510 04f57cb3 2019-07-25 stsp error = resolve_commit_arg(&commit_id, commit_id_str, repo);
2511 30837e32 2019-07-25 stsp if (error)
2512 66bea077 2018-08-02 stsp goto done;
2515 28315671 2019-08-14 stsp error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
2516 28315671 2019-08-14 stsp if (error)
2517 28315671 2019-08-14 stsp goto done;
2518 28315671 2019-08-14 stsp if (obj_id == NULL) {
2519 28315671 2019-08-14 stsp error = got_error(GOT_ERR_NO_OBJ);
2520 28315671 2019-08-14 stsp goto done;
2523 28315671 2019-08-14 stsp error = got_object_get_type(&obj_type, repo, obj_id);
2524 28315671 2019-08-14 stsp if (error)
2525 28315671 2019-08-14 stsp goto done;
2527 28315671 2019-08-14 stsp if (obj_type != GOT_OBJ_TYPE_BLOB) {
2528 28315671 2019-08-14 stsp error = got_error(GOT_ERR_OBJ_TYPE);
2529 28315671 2019-08-14 stsp goto done;
2532 28315671 2019-08-14 stsp error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
2533 28315671 2019-08-14 stsp if (error)
2534 28315671 2019-08-14 stsp goto done;
2535 28315671 2019-08-14 stsp bca.f = got_opentemp();
2536 28315671 2019-08-14 stsp if (bca.f == NULL) {
2537 28315671 2019-08-14 stsp error = got_error_from_errno("got_opentemp");
2538 28315671 2019-08-14 stsp goto done;
2540 b02560ec 2019-08-19 stsp error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
2541 28315671 2019-08-14 stsp &bca.line_offsets, bca.f, blob);
2542 7ef28ff8 2019-08-14 stsp if (error || bca.nlines == 0)
2543 28315671 2019-08-14 stsp goto done;
2545 b02560ec 2019-08-19 stsp /* Don't include \n at EOF in the blame line count. */
2546 b02560ec 2019-08-19 stsp if (bca.line_offsets[bca.nlines - 1] == filesize)
2547 b02560ec 2019-08-19 stsp bca.nlines--;
2549 28315671 2019-08-14 stsp bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
2550 28315671 2019-08-14 stsp if (bca.lines == NULL) {
2551 28315671 2019-08-14 stsp error = got_error_from_errno("calloc");
2552 28315671 2019-08-14 stsp goto done;
2554 28315671 2019-08-14 stsp bca.lineno_cur = 1;
2555 7ef28ff8 2019-08-14 stsp bca.nlines_prec = 0;
2556 7ef28ff8 2019-08-14 stsp i = bca.nlines;
2557 7ef28ff8 2019-08-14 stsp while (i > 0) {
2559 7ef28ff8 2019-08-14 stsp bca.nlines_prec++;
2561 82f6abb8 2019-08-14 stsp bca.repo = repo;
2563 6fb7cd11 2019-08-22 stsp error = got_blame(in_repo_path, commit_id, repo, blame_cb, &bca,
2564 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
2565 28315671 2019-08-14 stsp if (error)
2566 28315671 2019-08-14 stsp goto done;
2568 66bea077 2018-08-02 stsp free(in_repo_path);
2569 66bea077 2018-08-02 stsp free(repo_path);
2570 66bea077 2018-08-02 stsp free(cwd);
2571 404c43c4 2018-06-21 stsp free(commit_id);
2572 28315671 2019-08-14 stsp free(obj_id);
2574 28315671 2019-08-14 stsp got_object_blob_close(blob);
2575 0c06baac 2019-02-05 stsp if (worktree)
2576 0c06baac 2019-02-05 stsp got_worktree_close(worktree);
2577 ad242220 2018-09-08 stsp if (repo) {
2578 ad242220 2018-09-08 stsp const struct got_error *repo_error;
2579 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
2580 ad242220 2018-09-08 stsp if (error == NULL)
2581 ad242220 2018-09-08 stsp error = repo_error;
2583 28315671 2019-08-14 stsp for (i = 0; i < bca.nlines; i++) {
2584 28315671 2019-08-14 stsp struct blame_line *bline = &bca.lines[i];
2585 28315671 2019-08-14 stsp free(bline->id_str);
2586 82f6abb8 2019-08-14 stsp free(bline->committer);
2588 28315671 2019-08-14 stsp free(bca.lines);
2589 28315671 2019-08-14 stsp free(bca.line_offsets);
2590 28315671 2019-08-14 stsp if (bca.f && fclose(bca.f) == EOF && error == NULL)
2591 28315671 2019-08-14 stsp error = got_error_from_errno("fclose");
2592 404c43c4 2018-06-21 stsp return error;
2595 5de5890b 2018-10-18 stsp __dead static void
2596 5de5890b 2018-10-18 stsp usage_tree(void)
2598 c1669e2e 2019-01-09 stsp fprintf(stderr,
2599 c1669e2e 2019-01-09 stsp "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
2600 5de5890b 2018-10-18 stsp getprogname());
2604 c1669e2e 2019-01-09 stsp static void
2605 c1669e2e 2019-01-09 stsp print_entry(struct got_tree_entry *te, const char *id, const char *path,
2606 c1669e2e 2019-01-09 stsp const char *root_path)
2608 c1669e2e 2019-01-09 stsp int is_root_path = (strcmp(path, root_path) == 0);
2609 848d6979 2019-08-12 stsp const char *modestr = "";
2611 c1669e2e 2019-01-09 stsp path += strlen(root_path);
2612 c1669e2e 2019-01-09 stsp while (path[0] == '/')
2615 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
2616 63c5ca5d 2019-08-24 stsp modestr = "$";
2617 63c5ca5d 2019-08-24 stsp else if (S_ISLNK(te->mode))
2618 848d6979 2019-08-12 stsp modestr = "@";
2619 848d6979 2019-08-12 stsp else if (S_ISDIR(te->mode))
2620 848d6979 2019-08-12 stsp modestr = "/";
2621 848d6979 2019-08-12 stsp else if (te->mode & S_IXUSR)
2622 848d6979 2019-08-12 stsp modestr = "*";
2624 c1669e2e 2019-01-09 stsp printf("%s%s%s%s%s\n", id ? id : "", path,
2625 848d6979 2019-08-12 stsp is_root_path ? "" : "/", te->name, modestr);
2628 5de5890b 2018-10-18 stsp static const struct got_error *
2629 5de5890b 2018-10-18 stsp print_tree(const char *path, struct got_object_id *commit_id,
2630 c1669e2e 2019-01-09 stsp int show_ids, int recurse, const char *root_path,
2631 c1669e2e 2019-01-09 stsp struct got_repository *repo)
2633 5de5890b 2018-10-18 stsp const struct got_error *err = NULL;
2634 5de5890b 2018-10-18 stsp struct got_object_id *tree_id = NULL;
2635 5de5890b 2018-10-18 stsp struct got_tree_object *tree = NULL;
2636 5de5890b 2018-10-18 stsp const struct got_tree_entries *entries;
2637 5de5890b 2018-10-18 stsp struct got_tree_entry *te;
2639 5de5890b 2018-10-18 stsp err = got_object_id_by_path(&tree_id, repo, commit_id, path);
2641 5de5890b 2018-10-18 stsp goto done;
2643 5de5890b 2018-10-18 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
2645 5de5890b 2018-10-18 stsp goto done;
2646 5de5890b 2018-10-18 stsp entries = got_object_tree_get_entries(tree);
2647 5de5890b 2018-10-18 stsp te = SIMPLEQ_FIRST(&entries->head);
2648 5de5890b 2018-10-18 stsp while (te) {
2649 5de5890b 2018-10-18 stsp char *id = NULL;
2651 84453469 2018-11-11 stsp if (sigint_received || sigpipe_received)
2654 5de5890b 2018-10-18 stsp if (show_ids) {
2655 5de5890b 2018-10-18 stsp char *id_str;
2656 5de5890b 2018-10-18 stsp err = got_object_id_str(&id_str, te->id);
2658 5de5890b 2018-10-18 stsp goto done;
2659 5de5890b 2018-10-18 stsp if (asprintf(&id, "%s ", id_str) == -1) {
2660 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2661 5de5890b 2018-10-18 stsp free(id_str);
2662 5de5890b 2018-10-18 stsp goto done;
2664 5de5890b 2018-10-18 stsp free(id_str);
2666 c1669e2e 2019-01-09 stsp print_entry(te, id, path, root_path);
2669 c1669e2e 2019-01-09 stsp if (recurse && S_ISDIR(te->mode)) {
2670 c1669e2e 2019-01-09 stsp char *child_path;
2671 c1669e2e 2019-01-09 stsp if (asprintf(&child_path, "%s%s%s", path,
2672 c1669e2e 2019-01-09 stsp path[0] == '/' && path[1] == '\0' ? "" : "/",
2673 c1669e2e 2019-01-09 stsp te->name) == -1) {
2674 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2675 c1669e2e 2019-01-09 stsp goto done;
2677 c1669e2e 2019-01-09 stsp err = print_tree(child_path, commit_id, show_ids, 1,
2678 c1669e2e 2019-01-09 stsp root_path, repo);
2679 c1669e2e 2019-01-09 stsp free(child_path);
2681 c1669e2e 2019-01-09 stsp goto done;
2684 c1669e2e 2019-01-09 stsp te = SIMPLEQ_NEXT(te, entry);
2688 5de5890b 2018-10-18 stsp got_object_tree_close(tree);
2689 5de5890b 2018-10-18 stsp free(tree_id);
2690 5de5890b 2018-10-18 stsp return err;
2693 5de5890b 2018-10-18 stsp static const struct got_error *
2694 5de5890b 2018-10-18 stsp cmd_tree(int argc, char *argv[])
2696 5de5890b 2018-10-18 stsp const struct got_error *error;
2697 5de5890b 2018-10-18 stsp struct got_repository *repo = NULL;
2698 7a2c19d6 2019-02-05 stsp struct got_worktree *worktree = NULL;
2699 7a2c19d6 2019-02-05 stsp const char *path;
2700 7a2c19d6 2019-02-05 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2701 5de5890b 2018-10-18 stsp struct got_object_id *commit_id = NULL;
2702 5de5890b 2018-10-18 stsp char *commit_id_str = NULL;
2703 c1669e2e 2019-01-09 stsp int show_ids = 0, recurse = 0;
2706 5de5890b 2018-10-18 stsp #ifndef PROFILE
2707 0f8d269b 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2708 0f8d269b 2019-01-04 stsp NULL) == -1)
2709 5de5890b 2018-10-18 stsp err(1, "pledge");
2712 c1669e2e 2019-01-09 stsp while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
2713 5de5890b 2018-10-18 stsp switch (ch) {
2715 5de5890b 2018-10-18 stsp commit_id_str = optarg;
2718 5de5890b 2018-10-18 stsp repo_path = realpath(optarg, NULL);
2719 5de5890b 2018-10-18 stsp if (repo_path == NULL)
2720 5de5890b 2018-10-18 stsp err(1, "-r option");
2721 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
2724 5de5890b 2018-10-18 stsp show_ids = 1;
2727 c1669e2e 2019-01-09 stsp recurse = 1;
2730 2deda0b9 2019-03-07 stsp usage_tree();
2731 5de5890b 2018-10-18 stsp /* NOTREACHED */
2735 5de5890b 2018-10-18 stsp argc -= optind;
2736 5de5890b 2018-10-18 stsp argv += optind;
2738 5de5890b 2018-10-18 stsp if (argc == 1)
2739 5de5890b 2018-10-18 stsp path = argv[0];
2740 5de5890b 2018-10-18 stsp else if (argc > 1)
2741 5de5890b 2018-10-18 stsp usage_tree();
2743 7a2c19d6 2019-02-05 stsp path = NULL;
2745 9bf7a39b 2019-02-05 stsp cwd = getcwd(NULL, 0);
2746 9bf7a39b 2019-02-05 stsp if (cwd == NULL) {
2747 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2748 9bf7a39b 2019-02-05 stsp goto done;
2750 5de5890b 2018-10-18 stsp if (repo_path == NULL) {
2751 7a2c19d6 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
2752 8994de28 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
2753 7a2c19d6 2019-02-05 stsp goto done;
2755 7a2c19d6 2019-02-05 stsp error = NULL;
2756 7a2c19d6 2019-02-05 stsp if (worktree) {
2757 7a2c19d6 2019-02-05 stsp repo_path =
2758 7a2c19d6 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
2759 7a2c19d6 2019-02-05 stsp if (repo_path == NULL)
2760 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2761 7a2c19d6 2019-02-05 stsp if (error)
2762 7a2c19d6 2019-02-05 stsp goto done;
2764 7a2c19d6 2019-02-05 stsp repo_path = strdup(cwd);
2765 7a2c19d6 2019-02-05 stsp if (repo_path == NULL) {
2766 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2767 7a2c19d6 2019-02-05 stsp goto done;
2772 5de5890b 2018-10-18 stsp error = got_repo_open(&repo, repo_path);
2773 5de5890b 2018-10-18 stsp if (error != NULL)
2774 c02c541e 2019-03-29 stsp goto done;
2776 c530dc23 2019-07-23 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2777 c02c541e 2019-03-29 stsp if (error)
2778 5de5890b 2018-10-18 stsp goto done;
2780 9bf7a39b 2019-02-05 stsp if (path == NULL) {
2781 9bf7a39b 2019-02-05 stsp if (worktree) {
2782 9bf7a39b 2019-02-05 stsp char *p, *worktree_subdir = cwd +
2783 9bf7a39b 2019-02-05 stsp strlen(got_worktree_get_root_path(worktree));
2784 9bf7a39b 2019-02-05 stsp if (asprintf(&p, "%s/%s",
2785 9bf7a39b 2019-02-05 stsp got_worktree_get_path_prefix(worktree),
2786 9bf7a39b 2019-02-05 stsp worktree_subdir) == -1) {
2787 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
2788 9bf7a39b 2019-02-05 stsp goto done;
2790 9bf7a39b 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, p, 1);
2792 9bf7a39b 2019-02-05 stsp if (error)
2793 9bf7a39b 2019-02-05 stsp goto done;
2795 9bf7a39b 2019-02-05 stsp path = "/";
2797 9bf7a39b 2019-02-05 stsp if (in_repo_path == NULL) {
2798 9bf7a39b 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
2799 9bf7a39b 2019-02-05 stsp if (error != NULL)
2800 9bf7a39b 2019-02-05 stsp goto done;
2803 5de5890b 2018-10-18 stsp if (commit_id_str == NULL) {
2804 5de5890b 2018-10-18 stsp struct got_reference *head_ref;
2805 2f17228e 2019-05-12 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
2806 5de5890b 2018-10-18 stsp if (error != NULL)
2807 5de5890b 2018-10-18 stsp goto done;
2808 5de5890b 2018-10-18 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
2809 5de5890b 2018-10-18 stsp got_ref_close(head_ref);
2810 5de5890b 2018-10-18 stsp if (error != NULL)
2811 5de5890b 2018-10-18 stsp goto done;
2813 04f57cb3 2019-07-25 stsp error = resolve_commit_arg(&commit_id, commit_id_str, repo);
2814 30837e32 2019-07-25 stsp if (error)
2815 5de5890b 2018-10-18 stsp goto done;
2818 c1669e2e 2019-01-09 stsp error = print_tree(in_repo_path, commit_id, show_ids, recurse,
2819 c1669e2e 2019-01-09 stsp in_repo_path, repo);
2821 5de5890b 2018-10-18 stsp free(in_repo_path);
2822 5de5890b 2018-10-18 stsp free(repo_path);
2823 5de5890b 2018-10-18 stsp free(cwd);
2824 5de5890b 2018-10-18 stsp free(commit_id);
2825 7a2c19d6 2019-02-05 stsp if (worktree)
2826 7a2c19d6 2019-02-05 stsp got_worktree_close(worktree);
2827 5de5890b 2018-10-18 stsp if (repo) {
2828 5de5890b 2018-10-18 stsp const struct got_error *repo_error;
2829 5de5890b 2018-10-18 stsp repo_error = got_repo_close(repo);
2830 5de5890b 2018-10-18 stsp if (error == NULL)
2831 5de5890b 2018-10-18 stsp error = repo_error;
2833 5de5890b 2018-10-18 stsp return error;
2836 6bad629b 2019-02-04 stsp __dead static void
2837 6bad629b 2019-02-04 stsp usage_status(void)
2839 72ea6654 2019-07-27 stsp fprintf(stderr, "usage: %s status [path ...]\n", getprogname());
2843 b72f483a 2019-02-05 stsp static const struct got_error *
2844 88d0e355 2019-08-03 stsp print_status(void *arg, unsigned char status, unsigned char staged_status,
2845 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
2846 537ac44b 2019-08-03 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
2848 244725f2 2019-08-03 stsp if (status == staged_status && (status == GOT_STATUS_DELETE))
2849 c363b2c1 2019-08-03 stsp status = GOT_STATUS_NO_CHANGE;
2850 88d0e355 2019-08-03 stsp printf("%c%c %s\n", status, staged_status, path);
2851 b72f483a 2019-02-05 stsp return NULL;
2854 6bad629b 2019-02-04 stsp static const struct got_error *
2855 6bad629b 2019-02-04 stsp cmd_status(int argc, char *argv[])
2857 6bad629b 2019-02-04 stsp const struct got_error *error = NULL;
2858 6bad629b 2019-02-04 stsp struct got_repository *repo = NULL;
2859 6bad629b 2019-02-04 stsp struct got_worktree *worktree = NULL;
2860 f86a1bf5 2019-07-27 stsp char *cwd = NULL;
2861 72ea6654 2019-07-27 stsp struct got_pathlist_head paths;
2862 a5edda0a 2019-07-27 stsp struct got_pathlist_entry *pe;
2865 72ea6654 2019-07-27 stsp TAILQ_INIT(&paths);
2867 6bad629b 2019-02-04 stsp while ((ch = getopt(argc, argv, "")) != -1) {
2868 6bad629b 2019-02-04 stsp switch (ch) {
2870 2deda0b9 2019-03-07 stsp usage_status();
2871 6bad629b 2019-02-04 stsp /* NOTREACHED */
2875 6bad629b 2019-02-04 stsp argc -= optind;
2876 6bad629b 2019-02-04 stsp argv += optind;
2878 6bad629b 2019-02-04 stsp #ifndef PROFILE
2879 6bad629b 2019-02-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2880 6bad629b 2019-02-04 stsp NULL) == -1)
2881 6bad629b 2019-02-04 stsp err(1, "pledge");
2883 927df6b7 2019-02-10 stsp cwd = getcwd(NULL, 0);
2884 927df6b7 2019-02-10 stsp if (cwd == NULL) {
2885 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2886 927df6b7 2019-02-10 stsp goto done;
2889 927df6b7 2019-02-10 stsp error = got_worktree_open(&worktree, cwd);
2890 927df6b7 2019-02-10 stsp if (error != NULL)
2891 a5edda0a 2019-07-27 stsp goto done;
2893 6bad629b 2019-02-04 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2894 6bad629b 2019-02-04 stsp if (error != NULL)
2895 6bad629b 2019-02-04 stsp goto done;
2897 d0eebce4 2019-03-11 stsp error = apply_unveil(got_repo_get_path(repo), 1,
2898 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
2899 087fb88c 2019-08-04 stsp if (error)
2900 087fb88c 2019-08-04 stsp goto done;
2902 087fb88c 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
2903 6bad629b 2019-02-04 stsp if (error)
2904 6bad629b 2019-02-04 stsp goto done;
2906 72ea6654 2019-07-27 stsp error = got_worktree_status(worktree, &paths, repo, print_status, NULL,
2907 6bad629b 2019-02-04 stsp check_cancelled, NULL);
2909 a5edda0a 2019-07-27 stsp TAILQ_FOREACH(pe, &paths, entry)
2910 a5edda0a 2019-07-27 stsp free((char *)pe->path);
2911 72ea6654 2019-07-27 stsp got_pathlist_free(&paths);
2912 927df6b7 2019-02-10 stsp free(cwd);
2913 d0eebce4 2019-03-11 stsp return error;
2916 d0eebce4 2019-03-11 stsp __dead static void
2917 d0eebce4 2019-03-11 stsp usage_ref(void)
2919 d0eebce4 2019-03-11 stsp fprintf(stderr,
2920 d1c1ae5f 2019-08-12 stsp "usage: %s ref [-r repository] -l | -d name | [-s] name target\n",
2921 d0eebce4 2019-03-11 stsp getprogname());
2925 d0eebce4 2019-03-11 stsp static const struct got_error *
2926 d0eebce4 2019-03-11 stsp list_refs(struct got_repository *repo)
2928 d0eebce4 2019-03-11 stsp static const struct got_error *err = NULL;
2929 d0eebce4 2019-03-11 stsp struct got_reflist_head refs;
2930 d0eebce4 2019-03-11 stsp struct got_reflist_entry *re;
2932 d0eebce4 2019-03-11 stsp SIMPLEQ_INIT(&refs);
2933 b8bad2ba 2019-08-23 stsp err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
2935 d0eebce4 2019-03-11 stsp return err;
2937 d0eebce4 2019-03-11 stsp SIMPLEQ_FOREACH(re, &refs, entry) {
2938 d0eebce4 2019-03-11 stsp char *refstr;
2939 d0eebce4 2019-03-11 stsp refstr = got_ref_to_str(re->ref);
2940 d0eebce4 2019-03-11 stsp if (refstr == NULL)
2941 638f9024 2019-05-13 stsp return got_error_from_errno("got_ref_to_str");
2942 d0eebce4 2019-03-11 stsp printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
2943 d0eebce4 2019-03-11 stsp free(refstr);
2946 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
2947 d0eebce4 2019-03-11 stsp return NULL;
2950 d0eebce4 2019-03-11 stsp static const struct got_error *
2951 d0eebce4 2019-03-11 stsp delete_ref(struct got_repository *repo, const char *refname)
2953 d0eebce4 2019-03-11 stsp const struct got_error *err = NULL;
2954 d0eebce4 2019-03-11 stsp struct got_reference *ref;
2956 2f17228e 2019-05-12 stsp err = got_ref_open(&ref, repo, refname, 0);
2958 d0eebce4 2019-03-11 stsp return err;
2960 d0eebce4 2019-03-11 stsp err = got_ref_delete(ref, repo);
2961 d0eebce4 2019-03-11 stsp got_ref_close(ref);
2962 d0eebce4 2019-03-11 stsp return err;
2965 d0eebce4 2019-03-11 stsp static const struct got_error *
2966 d83d9d5c 2019-05-13 stsp add_ref(struct got_repository *repo, const char *refname, const char *target)
2968 d0eebce4 2019-03-11 stsp const struct got_error *err = NULL;
2969 d0eebce4 2019-03-11 stsp struct got_object_id *id;
2970 d0eebce4 2019-03-11 stsp struct got_reference *ref = NULL;
2973 d1644381 2019-07-14 stsp * Don't let the user create a reference named '-'.
2974 d1644381 2019-07-14 stsp * While technically a valid reference name, this case is usually
2975 d1644381 2019-07-14 stsp * an unintended typo.
2977 d1644381 2019-07-14 stsp if (refname[0] == '-' && refname[1] == '\0')
2978 d1644381 2019-07-14 stsp return got_error(GOT_ERR_BAD_REF_NAME);
2980 dd88155e 2019-06-29 stsp err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
2982 d83d9d5c 2019-05-13 stsp if (err) {
2983 d83d9d5c 2019-05-13 stsp struct got_reference *target_ref;
2985 d83d9d5c 2019-05-13 stsp if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
2986 d83d9d5c 2019-05-13 stsp return err;
2987 d83d9d5c 2019-05-13 stsp err = got_ref_open(&target_ref, repo, target, 0);
2989 d83d9d5c 2019-05-13 stsp return err;
2990 d83d9d5c 2019-05-13 stsp err = got_ref_resolve(&id, repo, target_ref);
2991 d83d9d5c 2019-05-13 stsp got_ref_close(target_ref);
2993 d83d9d5c 2019-05-13 stsp return err;
2996 d0eebce4 2019-03-11 stsp err = got_ref_alloc(&ref, refname, id);
2998 d0eebce4 2019-03-11 stsp goto done;
3000 d0eebce4 2019-03-11 stsp err = got_ref_write(ref, repo);
3003 d0eebce4 2019-03-11 stsp got_ref_close(ref);
3005 d1c1ae5f 2019-08-12 stsp return err;
3008 d1c1ae5f 2019-08-12 stsp static const struct got_error *
3009 d1c1ae5f 2019-08-12 stsp add_symref(struct got_repository *repo, const char *refname, const char *target)
3011 d1c1ae5f 2019-08-12 stsp const struct got_error *err = NULL;
3012 d1c1ae5f 2019-08-12 stsp struct got_reference *ref = NULL;
3013 d1c1ae5f 2019-08-12 stsp struct got_reference *target_ref = NULL;
3016 d1c1ae5f 2019-08-12 stsp * Don't let the user create a reference named '-'.
3017 d1c1ae5f 2019-08-12 stsp * While technically a valid reference name, this case is usually
3018 d1c1ae5f 2019-08-12 stsp * an unintended typo.
3020 d1c1ae5f 2019-08-12 stsp if (refname[0] == '-' && refname[1] == '\0')
3021 d1c1ae5f 2019-08-12 stsp return got_error(GOT_ERR_BAD_REF_NAME);
3023 d1c1ae5f 2019-08-12 stsp err = got_ref_open(&target_ref, repo, target, 0);
3025 d1c1ae5f 2019-08-12 stsp return err;
3027 d1c1ae5f 2019-08-12 stsp err = got_ref_alloc_symref(&ref, refname, target_ref);
3029 d1c1ae5f 2019-08-12 stsp goto done;
3031 d1c1ae5f 2019-08-12 stsp err = got_ref_write(ref, repo);
3033 d1c1ae5f 2019-08-12 stsp if (target_ref)
3034 d1c1ae5f 2019-08-12 stsp got_ref_close(target_ref);
3036 d1c1ae5f 2019-08-12 stsp got_ref_close(ref);
3037 d0eebce4 2019-03-11 stsp return err;
3040 d0eebce4 2019-03-11 stsp static const struct got_error *
3041 d0eebce4 2019-03-11 stsp cmd_ref(int argc, char *argv[])
3043 d0eebce4 2019-03-11 stsp const struct got_error *error = NULL;
3044 d0eebce4 2019-03-11 stsp struct got_repository *repo = NULL;
3045 d0eebce4 2019-03-11 stsp struct got_worktree *worktree = NULL;
3046 d0eebce4 2019-03-11 stsp char *cwd = NULL, *repo_path = NULL;
3047 d1c1ae5f 2019-08-12 stsp int ch, do_list = 0, create_symref = 0;
3048 d0eebce4 2019-03-11 stsp const char *delref = NULL;
3050 d0eebce4 2019-03-11 stsp /* TODO: Add -s option for adding symbolic references. */
3051 d1c1ae5f 2019-08-12 stsp while ((ch = getopt(argc, argv, "d:r:ls")) != -1) {
3052 d0eebce4 2019-03-11 stsp switch (ch) {
3054 d0eebce4 2019-03-11 stsp delref = optarg;
3057 d0eebce4 2019-03-11 stsp repo_path = realpath(optarg, NULL);
3058 d0eebce4 2019-03-11 stsp if (repo_path == NULL)
3059 d0eebce4 2019-03-11 stsp err(1, "-r option");
3060 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
3063 d0eebce4 2019-03-11 stsp do_list = 1;
3066 d1c1ae5f 2019-08-12 stsp create_symref = 1;
3069 d0eebce4 2019-03-11 stsp usage_ref();
3070 d0eebce4 2019-03-11 stsp /* NOTREACHED */
3074 d0eebce4 2019-03-11 stsp if (do_list && delref)
3075 d0eebce4 2019-03-11 stsp errx(1, "-l and -d options are mutually exclusive\n");
3077 d0eebce4 2019-03-11 stsp argc -= optind;
3078 d0eebce4 2019-03-11 stsp argv += optind;
3080 d0eebce4 2019-03-11 stsp if (do_list || delref) {
3081 d1c1ae5f 2019-08-12 stsp if (create_symref)
3082 d1c1ae5f 2019-08-12 stsp errx(1, "-s option cannot be used together with the "
3083 d1c1ae5f 2019-08-12 stsp "-l or -d options");
3084 d0eebce4 2019-03-11 stsp if (argc > 0)
3085 d0eebce4 2019-03-11 stsp usage_ref();
3086 d0eebce4 2019-03-11 stsp } else if (argc != 2)
3087 d0eebce4 2019-03-11 stsp usage_ref();
3089 d0eebce4 2019-03-11 stsp #ifndef PROFILE
3090 e0b57350 2019-03-12 stsp if (do_list) {
3091 e0b57350 2019-03-12 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3092 e0b57350 2019-03-12 stsp NULL) == -1)
3093 e0b57350 2019-03-12 stsp err(1, "pledge");
3095 e0b57350 2019-03-12 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3096 e0b57350 2019-03-12 stsp "sendfd unveil", NULL) == -1)
3097 e0b57350 2019-03-12 stsp err(1, "pledge");
3100 d0eebce4 2019-03-11 stsp cwd = getcwd(NULL, 0);
3101 d0eebce4 2019-03-11 stsp if (cwd == NULL) {
3102 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
3103 d0eebce4 2019-03-11 stsp goto done;
3106 d0eebce4 2019-03-11 stsp if (repo_path == NULL) {
3107 d0eebce4 2019-03-11 stsp error = got_worktree_open(&worktree, cwd);
3108 d0eebce4 2019-03-11 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
3109 d0eebce4 2019-03-11 stsp goto done;
3111 d0eebce4 2019-03-11 stsp error = NULL;
3112 d0eebce4 2019-03-11 stsp if (worktree) {
3113 d0eebce4 2019-03-11 stsp repo_path =
3114 d0eebce4 2019-03-11 stsp strdup(got_worktree_get_repo_path(worktree));
3115 d0eebce4 2019-03-11 stsp if (repo_path == NULL)
3116 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
3117 d0eebce4 2019-03-11 stsp if (error)
3118 d0eebce4 2019-03-11 stsp goto done;
3120 d0eebce4 2019-03-11 stsp repo_path = strdup(cwd);
3121 d0eebce4 2019-03-11 stsp if (repo_path == NULL) {
3122 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
3123 d0eebce4 2019-03-11 stsp goto done;
3128 d0eebce4 2019-03-11 stsp error = got_repo_open(&repo, repo_path);
3129 d0eebce4 2019-03-11 stsp if (error != NULL)
3130 d0eebce4 2019-03-11 stsp goto done;
3132 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), do_list,
3133 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
3134 c02c541e 2019-03-29 stsp if (error)
3135 c02c541e 2019-03-29 stsp goto done;
3137 d0eebce4 2019-03-11 stsp if (do_list)
3138 d0eebce4 2019-03-11 stsp error = list_refs(repo);
3139 d0eebce4 2019-03-11 stsp else if (delref)
3140 d0eebce4 2019-03-11 stsp error = delete_ref(repo, delref);
3141 d1c1ae5f 2019-08-12 stsp else if (create_symref)
3142 d1c1ae5f 2019-08-12 stsp error = add_symref(repo, argv[0], argv[1]);
3144 d0eebce4 2019-03-11 stsp error = add_ref(repo, argv[0], argv[1]);
3147 4e759de4 2019-06-26 stsp got_repo_close(repo);
3148 4e759de4 2019-06-26 stsp if (worktree)
3149 4e759de4 2019-06-26 stsp got_worktree_close(worktree);
3150 4e759de4 2019-06-26 stsp free(cwd);
3151 4e759de4 2019-06-26 stsp free(repo_path);
3152 4e759de4 2019-06-26 stsp return error;
3155 4e759de4 2019-06-26 stsp __dead static void
3156 4e759de4 2019-06-26 stsp usage_branch(void)
3158 4e759de4 2019-06-26 stsp fprintf(stderr,
3159 4e759de4 2019-06-26 stsp "usage: %s branch [-r repository] -l | -d name | "
3160 a4f89d48 2019-08-25 stsp "name [commit]\n", getprogname());
3164 4e759de4 2019-06-26 stsp static const struct got_error *
3165 ba882ee3 2019-07-11 stsp list_branches(struct got_repository *repo, struct got_worktree *worktree)
3167 4e759de4 2019-06-26 stsp static const struct got_error *err = NULL;
3168 4e759de4 2019-06-26 stsp struct got_reflist_head refs;
3169 4e759de4 2019-06-26 stsp struct got_reflist_entry *re;
3171 4e759de4 2019-06-26 stsp SIMPLEQ_INIT(&refs);
3173 b8bad2ba 2019-08-23 stsp err = got_ref_list(&refs, repo, "refs/heads",
3174 b8bad2ba 2019-08-23 stsp got_ref_cmp_by_name, NULL);
3176 4e759de4 2019-06-26 stsp return err;
3178 4e759de4 2019-06-26 stsp SIMPLEQ_FOREACH(re, &refs, entry) {
3179 ba882ee3 2019-07-11 stsp const char *refname, *marker = " ";
3180 4e759de4 2019-06-26 stsp char *refstr;
3181 4e759de4 2019-06-26 stsp refname = got_ref_get_name(re->ref);
3182 ba882ee3 2019-07-11 stsp if (worktree && strcmp(refname,
3183 ba882ee3 2019-07-11 stsp got_worktree_get_head_ref_name(worktree)) == 0) {
3184 ba882ee3 2019-07-11 stsp struct got_object_id *id = NULL;
3185 ba882ee3 2019-07-11 stsp err = got_ref_resolve(&id, repo, re->ref);
3187 ba882ee3 2019-07-11 stsp return err;
3188 ba882ee3 2019-07-11 stsp if (got_object_id_cmp(id,
3189 ba882ee3 2019-07-11 stsp got_worktree_get_base_commit_id(worktree)) == 0)
3190 ba882ee3 2019-07-11 stsp marker = "* ";
3192 ba882ee3 2019-07-11 stsp marker = "~ ";
3195 29606af7 2019-08-23 stsp refname += strlen("refs/heads/");
3196 4e759de4 2019-06-26 stsp refstr = got_ref_to_str(re->ref);
3197 4e759de4 2019-06-26 stsp if (refstr == NULL)
3198 4e759de4 2019-06-26 stsp return got_error_from_errno("got_ref_to_str");
3199 ba882ee3 2019-07-11 stsp printf("%s%s: %s\n", marker, refname, refstr);
3200 4e759de4 2019-06-26 stsp free(refstr);
3203 4e759de4 2019-06-26 stsp got_ref_list_free(&refs);
3204 4e759de4 2019-06-26 stsp return NULL;
3207 4e759de4 2019-06-26 stsp static const struct got_error *
3208 45cd4e47 2019-08-25 stsp delete_branch(struct got_repository *repo, struct got_worktree *worktree,
3209 45cd4e47 2019-08-25 stsp const char *branch_name)
3211 4e759de4 2019-06-26 stsp const struct got_error *err = NULL;
3212 45cd4e47 2019-08-25 stsp struct got_reference *ref = NULL;
3213 4e759de4 2019-06-26 stsp char *refname;
3215 4e759de4 2019-06-26 stsp if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
3216 4e759de4 2019-06-26 stsp return got_error_from_errno("asprintf");
3218 4e759de4 2019-06-26 stsp err = got_ref_open(&ref, repo, refname, 0);
3220 4e759de4 2019-06-26 stsp goto done;
3222 45cd4e47 2019-08-25 stsp if (worktree &&
3223 45cd4e47 2019-08-25 stsp strcmp(got_worktree_get_head_ref_name(worktree),
3224 45cd4e47 2019-08-25 stsp got_ref_get_name(ref)) == 0) {
3225 45cd4e47 2019-08-25 stsp err = got_error_msg(GOT_ERR_SAME_BRANCH,
3226 45cd4e47 2019-08-25 stsp "will not delete this work tree's current branch");
3227 45cd4e47 2019-08-25 stsp goto done;
3230 45cd4e47 2019-08-25 stsp err = got_ref_delete(ref, repo);
3233 45cd4e47 2019-08-25 stsp got_ref_close(ref);
3234 4e759de4 2019-06-26 stsp free(refname);
3235 4e759de4 2019-06-26 stsp return err;
3238 4e759de4 2019-06-26 stsp static const struct got_error *
3239 4e759de4 2019-06-26 stsp add_branch(struct got_repository *repo, const char *branch_name,
3240 4e759de4 2019-06-26 stsp const char *base_branch)
3242 4e759de4 2019-06-26 stsp const struct got_error *err = NULL;
3243 4e759de4 2019-06-26 stsp struct got_object_id *id = NULL;
3244 a4f89d48 2019-08-25 stsp char *label;
3245 4e759de4 2019-06-26 stsp struct got_reference *ref = NULL;
3246 4e759de4 2019-06-26 stsp char *base_refname = NULL, *refname = NULL;
3249 d3f84d51 2019-07-11 stsp * Don't let the user create a branch named '-'.
3250 d3f84d51 2019-07-11 stsp * While technically a valid reference name, this case is usually
3251 d3f84d51 2019-07-11 stsp * an unintended typo.
3253 d3f84d51 2019-07-11 stsp if (branch_name[0] == '-' && branch_name[1] == '\0')
3254 d3f84d51 2019-07-11 stsp return got_error(GOT_ERR_BAD_REF_NAME);
3256 a4f89d48 2019-08-25 stsp err = match_object_id(&id, &label, base_branch,
3257 a4f89d48 2019-08-25 stsp GOT_OBJ_TYPE_COMMIT, 1, repo);
3259 a4f89d48 2019-08-25 stsp return err;
3261 4e759de4 2019-06-26 stsp if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
3262 4e759de4 2019-06-26 stsp err = got_error_from_errno("asprintf");
3263 4e759de4 2019-06-26 stsp goto done;
3266 4e759de4 2019-06-26 stsp err = got_ref_open(&ref, repo, refname, 0);
3267 4e759de4 2019-06-26 stsp if (err == NULL) {
3268 4e759de4 2019-06-26 stsp err = got_error(GOT_ERR_BRANCH_EXISTS);
3269 4e759de4 2019-06-26 stsp goto done;
3270 4e759de4 2019-06-26 stsp } else if (err->code != GOT_ERR_NOT_REF)
3271 4e759de4 2019-06-26 stsp goto done;
3273 4e759de4 2019-06-26 stsp err = got_ref_alloc(&ref, refname, id);
3275 4e759de4 2019-06-26 stsp goto done;
3277 4e759de4 2019-06-26 stsp err = got_ref_write(ref, repo);
3280 4e759de4 2019-06-26 stsp got_ref_close(ref);
3282 4e759de4 2019-06-26 stsp free(base_refname);
3283 4e759de4 2019-06-26 stsp free(refname);
3284 4e759de4 2019-06-26 stsp return err;
3287 4e759de4 2019-06-26 stsp static const struct got_error *
3288 4e759de4 2019-06-26 stsp cmd_branch(int argc, char *argv[])
3290 4e759de4 2019-06-26 stsp const struct got_error *error = NULL;
3291 4e759de4 2019-06-26 stsp struct got_repository *repo = NULL;
3292 4e759de4 2019-06-26 stsp struct got_worktree *worktree = NULL;
3293 4e759de4 2019-06-26 stsp char *cwd = NULL, *repo_path = NULL;
3294 4e759de4 2019-06-26 stsp int ch, do_list = 0;
3295 4e759de4 2019-06-26 stsp const char *delref = NULL;
3297 4e759de4 2019-06-26 stsp while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
3298 4e759de4 2019-06-26 stsp switch (ch) {
3300 4e759de4 2019-06-26 stsp delref = optarg;
3303 4e759de4 2019-06-26 stsp repo_path = realpath(optarg, NULL);
3304 4e759de4 2019-06-26 stsp if (repo_path == NULL)
3305 4e759de4 2019-06-26 stsp err(1, "-r option");
3306 4e759de4 2019-06-26 stsp got_path_strip_trailing_slashes(repo_path);
3309 4e759de4 2019-06-26 stsp do_list = 1;
3312 4e759de4 2019-06-26 stsp usage_branch();
3313 4e759de4 2019-06-26 stsp /* NOTREACHED */
3317 4e759de4 2019-06-26 stsp if (do_list && delref)
3318 4e759de4 2019-06-26 stsp errx(1, "-l and -d options are mutually exclusive\n");
3320 4e759de4 2019-06-26 stsp argc -= optind;
3321 4e759de4 2019-06-26 stsp argv += optind;
3323 4e759de4 2019-06-26 stsp if (do_list || delref) {
3324 4e759de4 2019-06-26 stsp if (argc > 0)
3325 4e759de4 2019-06-26 stsp usage_branch();
3326 4e759de4 2019-06-26 stsp } else if (argc < 1 || argc > 2)
3327 4e759de4 2019-06-26 stsp usage_branch();
3329 4e759de4 2019-06-26 stsp #ifndef PROFILE
3330 4e759de4 2019-06-26 stsp if (do_list) {
3331 4e759de4 2019-06-26 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3332 4e759de4 2019-06-26 stsp NULL) == -1)
3333 4e759de4 2019-06-26 stsp err(1, "pledge");
3335 4e759de4 2019-06-26 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3336 4e759de4 2019-06-26 stsp "sendfd unveil", NULL) == -1)
3337 4e759de4 2019-06-26 stsp err(1, "pledge");
3340 4e759de4 2019-06-26 stsp cwd = getcwd(NULL, 0);
3341 4e759de4 2019-06-26 stsp if (cwd == NULL) {
3342 4e759de4 2019-06-26 stsp error = got_error_from_errno("getcwd");
3343 4e759de4 2019-06-26 stsp goto done;
3346 4e759de4 2019-06-26 stsp if (repo_path == NULL) {
3347 4e759de4 2019-06-26 stsp error = got_worktree_open(&worktree, cwd);
3348 4e759de4 2019-06-26 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
3349 4e759de4 2019-06-26 stsp goto done;
3351 4e759de4 2019-06-26 stsp error = NULL;
3352 4e759de4 2019-06-26 stsp if (worktree) {
3353 4e759de4 2019-06-26 stsp repo_path =
3354 4e759de4 2019-06-26 stsp strdup(got_worktree_get_repo_path(worktree));
3355 4e759de4 2019-06-26 stsp if (repo_path == NULL)
3356 4e759de4 2019-06-26 stsp error = got_error_from_errno("strdup");
3357 4e759de4 2019-06-26 stsp if (error)
3358 4e759de4 2019-06-26 stsp goto done;
3360 4e759de4 2019-06-26 stsp repo_path = strdup(cwd);
3361 4e759de4 2019-06-26 stsp if (repo_path == NULL) {
3362 4e759de4 2019-06-26 stsp error = got_error_from_errno("strdup");
3363 4e759de4 2019-06-26 stsp goto done;
3368 4e759de4 2019-06-26 stsp error = got_repo_open(&repo, repo_path);
3369 4e759de4 2019-06-26 stsp if (error != NULL)
3370 4e759de4 2019-06-26 stsp goto done;
3372 4e759de4 2019-06-26 stsp error = apply_unveil(got_repo_get_path(repo), do_list,
3373 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
3374 4e759de4 2019-06-26 stsp if (error)
3375 4e759de4 2019-06-26 stsp goto done;
3377 4e759de4 2019-06-26 stsp if (do_list)
3378 ba882ee3 2019-07-11 stsp error = list_branches(repo, worktree);
3379 4e759de4 2019-06-26 stsp else if (delref)
3380 45cd4e47 2019-08-25 stsp error = delete_branch(repo, worktree, delref);
3382 4e759de4 2019-06-26 stsp const char *base_branch;
3383 4e759de4 2019-06-26 stsp if (argc == 1) {
3384 4e759de4 2019-06-26 stsp base_branch = worktree ?
3385 4e759de4 2019-06-26 stsp got_worktree_get_head_ref_name(worktree) :
3386 4e759de4 2019-06-26 stsp GOT_REF_HEAD;
3387 dc5351b4 2019-07-30 stsp if (strncmp(base_branch, "refs/heads/", 11) == 0)
3388 dc5351b4 2019-07-30 stsp base_branch += 11;
3390 4e759de4 2019-06-26 stsp base_branch = argv[1];
3391 4e759de4 2019-06-26 stsp error = add_branch(repo, argv[0], base_branch);
3395 d0eebce4 2019-03-11 stsp got_repo_close(repo);
3396 d0eebce4 2019-03-11 stsp if (worktree)
3397 d0eebce4 2019-03-11 stsp got_worktree_close(worktree);
3398 d0eebce4 2019-03-11 stsp free(cwd);
3399 d0eebce4 2019-03-11 stsp free(repo_path);
3400 d00136be 2019-03-26 stsp return error;
3404 d00136be 2019-03-26 stsp __dead static void
3405 8e7bd50a 2019-08-22 stsp usage_tag(void)
3407 8e7bd50a 2019-08-22 stsp fprintf(stderr,
3408 c904c63e 2019-08-22 stsp "usage: %s tag [-r repository] | -l | "
3409 8e7bd50a 2019-08-22 stsp "[-m message] name [commit]\n", getprogname());
3414 b8bad2ba 2019-08-23 stsp static const struct got_error *
3415 b8bad2ba 2019-08-23 stsp sort_tags(struct got_reflist_head *sorted, struct got_reflist_head *tags)
3417 b8bad2ba 2019-08-23 stsp const struct got_error *err = NULL;
3418 b8bad2ba 2019-08-23 stsp struct got_reflist_entry *re, *se, *new;
3419 b8bad2ba 2019-08-23 stsp struct got_object_id *re_id, *se_id;
3420 b8bad2ba 2019-08-23 stsp struct got_tag_object *re_tag, *se_tag;
3421 b8bad2ba 2019-08-23 stsp time_t re_time, se_time;
3423 b8bad2ba 2019-08-23 stsp SIMPLEQ_FOREACH(re, tags, entry) {
3424 b8bad2ba 2019-08-23 stsp se = SIMPLEQ_FIRST(sorted);
3425 b8bad2ba 2019-08-23 stsp if (se == NULL) {
3426 b8bad2ba 2019-08-23 stsp err = got_reflist_entry_dup(&new, re);
3428 b8bad2ba 2019-08-23 stsp return err;
3429 b8bad2ba 2019-08-23 stsp SIMPLEQ_INSERT_HEAD(sorted, new, entry);
3432 b8bad2ba 2019-08-23 stsp err = got_ref_resolve(&re_id, repo, re->ref);
3435 b8bad2ba 2019-08-23 stsp err = got_object_open_as_tag(&re_tag, repo, re_id);
3436 b8bad2ba 2019-08-23 stsp free(re_id);
3439 b8bad2ba 2019-08-23 stsp re_time = got_object_tag_get_tagger_time(re_tag);
3440 b8bad2ba 2019-08-23 stsp got_object_tag_close(re_tag);
3443 b8bad2ba 2019-08-23 stsp while (se) {
3444 b8bad2ba 2019-08-23 stsp err = got_ref_resolve(&se_id, repo, re->ref);
3447 b8bad2ba 2019-08-23 stsp err = got_object_open_as_tag(&se_tag, repo, se_id);
3448 b8bad2ba 2019-08-23 stsp free(se_id);
3451 b8bad2ba 2019-08-23 stsp se_time = got_object_tag_get_tagger_time(se_tag);
3452 b8bad2ba 2019-08-23 stsp got_object_tag_close(se_tag);
3454 b8bad2ba 2019-08-23 stsp if (se_time > re_time) {
3455 b8bad2ba 2019-08-23 stsp err = got_reflist_entry_dup(&new, re);
3457 b8bad2ba 2019-08-23 stsp return err;
3458 b8bad2ba 2019-08-23 stsp SIMPLEQ_INSERT_AFTER(sorted, se, new, entry);
3461 b8bad2ba 2019-08-23 stsp se = SIMPLEQ_NEXT(se, entry);
3466 b8bad2ba 2019-08-23 stsp return err;
3470 8e7bd50a 2019-08-22 stsp static const struct got_error *
3471 b8bad2ba 2019-08-23 stsp cmp_tags(void *arg, int *cmp, struct got_reference *ref1,
3472 b8bad2ba 2019-08-23 stsp struct got_reference *ref2)
3474 b8bad2ba 2019-08-23 stsp const struct got_error *err = NULL;
3475 b8bad2ba 2019-08-23 stsp struct got_repository *repo = arg;
3476 b8bad2ba 2019-08-23 stsp struct got_object_id *id1, *id2 = NULL;
3477 b8bad2ba 2019-08-23 stsp struct got_tag_object *tag1 = NULL, *tag2 = NULL;
3478 b8bad2ba 2019-08-23 stsp time_t time1, time2;
3482 b8bad2ba 2019-08-23 stsp err = got_ref_resolve(&id1, repo, ref1);
3484 b8bad2ba 2019-08-23 stsp return err;
3485 b8bad2ba 2019-08-23 stsp err = got_object_open_as_tag(&tag1, repo, id1);
3487 b8bad2ba 2019-08-23 stsp goto done;
3489 b8bad2ba 2019-08-23 stsp err = got_ref_resolve(&id2, repo, ref2);
3491 b8bad2ba 2019-08-23 stsp goto done;
3492 b8bad2ba 2019-08-23 stsp err = got_object_open_as_tag(&tag2, repo, id2);
3494 b8bad2ba 2019-08-23 stsp goto done;
3496 b8bad2ba 2019-08-23 stsp time1 = got_object_tag_get_tagger_time(tag1);
3497 b8bad2ba 2019-08-23 stsp time2 = got_object_tag_get_tagger_time(tag2);
3499 b8bad2ba 2019-08-23 stsp /* Put latest tags first. */
3500 b8bad2ba 2019-08-23 stsp if (time1 < time2)
3502 b8bad2ba 2019-08-23 stsp else if (time1 > time2)
3503 b8bad2ba 2019-08-23 stsp *cmp = -1;
3505 b8bad2ba 2019-08-23 stsp err = got_ref_cmp_by_name(NULL, cmp, ref2, ref1);
3507 b8bad2ba 2019-08-23 stsp free(id1);
3508 b8bad2ba 2019-08-23 stsp free(id2);
3510 b8bad2ba 2019-08-23 stsp got_object_tag_close(tag1);
3512 b8bad2ba 2019-08-23 stsp got_object_tag_close(tag2);
3513 b8bad2ba 2019-08-23 stsp return err;
3516 b8bad2ba 2019-08-23 stsp static const struct got_error *
3517 8e7bd50a 2019-08-22 stsp list_tags(struct got_repository *repo, struct got_worktree *worktree)
3519 8e7bd50a 2019-08-22 stsp static const struct got_error *err = NULL;
3520 8e7bd50a 2019-08-22 stsp struct got_reflist_head refs;
3521 8e7bd50a 2019-08-22 stsp struct got_reflist_entry *re;
3523 8e7bd50a 2019-08-22 stsp SIMPLEQ_INIT(&refs);
3525 b8bad2ba 2019-08-23 stsp err = got_ref_list(&refs, repo, "refs/tags", cmp_tags, repo);
3527 8e7bd50a 2019-08-22 stsp return err;
3529 8e7bd50a 2019-08-22 stsp SIMPLEQ_FOREACH(re, &refs, entry) {
3530 8e7bd50a 2019-08-22 stsp const char *refname;
3531 8e7bd50a 2019-08-22 stsp char *refstr, *tagmsg0, *tagmsg, *line, *id_str, *datestr;
3532 8e7bd50a 2019-08-22 stsp char datebuf[26];
3533 8e7bd50a 2019-08-22 stsp time_t tagger_time;
3534 8e7bd50a 2019-08-22 stsp struct got_object_id *id;
3535 8e7bd50a 2019-08-22 stsp struct got_tag_object *tag;
3537 8e7bd50a 2019-08-22 stsp refname = got_ref_get_name(re->ref);
3538 8e7bd50a 2019-08-22 stsp if (strncmp(refname, "refs/tags/", 10) != 0)
3540 8e7bd50a 2019-08-22 stsp refname += 10;
3541 8e7bd50a 2019-08-22 stsp refstr = got_ref_to_str(re->ref);
3542 8e7bd50a 2019-08-22 stsp if (refstr == NULL) {
3543 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("got_ref_to_str");
3546 8e7bd50a 2019-08-22 stsp printf("%stag %s %s\n", GOT_COMMIT_SEP_STR, refname, refstr);
3547 8e7bd50a 2019-08-22 stsp free(refstr);
3549 8e7bd50a 2019-08-22 stsp err = got_ref_resolve(&id, repo, re->ref);
3552 8e7bd50a 2019-08-22 stsp err = got_object_open_as_tag(&tag, repo, id);
3556 2417344c 2019-08-23 stsp printf("from: %s\n", got_object_tag_get_tagger(tag));
3557 2417344c 2019-08-23 stsp tagger_time = got_object_tag_get_tagger_time(tag);
3558 2417344c 2019-08-23 stsp datestr = get_datestr(&tagger_time, datebuf);
3559 2417344c 2019-08-23 stsp if (datestr)
3560 2417344c 2019-08-23 stsp printf("date: %s UTC\n", datestr);
3561 8e7bd50a 2019-08-22 stsp err = got_object_id_str(&id_str,
3562 8e7bd50a 2019-08-22 stsp got_object_tag_get_object_id(tag));
3565 8e7bd50a 2019-08-22 stsp switch (got_object_tag_get_object_type(tag)) {
3566 8e7bd50a 2019-08-22 stsp case GOT_OBJ_TYPE_BLOB:
3567 2417344c 2019-08-23 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_BLOB, id_str);
3569 8e7bd50a 2019-08-22 stsp case GOT_OBJ_TYPE_TREE:
3570 2417344c 2019-08-23 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_TREE, id_str);
3572 8e7bd50a 2019-08-22 stsp case GOT_OBJ_TYPE_COMMIT:
3573 2417344c 2019-08-23 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
3575 8e7bd50a 2019-08-22 stsp case GOT_OBJ_TYPE_TAG:
3576 2417344c 2019-08-23 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_TAG, id_str);
3581 8e7bd50a 2019-08-22 stsp free(id_str);
3582 8e7bd50a 2019-08-22 stsp tagmsg0 = strdup(got_object_tag_get_message(tag));
3583 8e7bd50a 2019-08-22 stsp got_object_tag_close(tag);
3584 8e7bd50a 2019-08-22 stsp if (tagmsg0 == NULL) {
3585 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("strdup");
3589 8e7bd50a 2019-08-22 stsp tagmsg = tagmsg0;
3591 8e7bd50a 2019-08-22 stsp line = strsep(&tagmsg, "\n");
3593 8e7bd50a 2019-08-22 stsp printf(" %s\n", line);
3594 8e7bd50a 2019-08-22 stsp } while (line);
3595 8e7bd50a 2019-08-22 stsp free(tagmsg0);
3598 8e7bd50a 2019-08-22 stsp got_ref_list_free(&refs);
3599 8e7bd50a 2019-08-22 stsp return NULL;
3602 8e7bd50a 2019-08-22 stsp static const struct got_error *
3603 62870f63 2019-08-22 stsp get_tag_message(char **tagmsg, const char *commit_id_str,
3604 62870f63 2019-08-22 stsp const char *tag_name, const char *repo_path)
3606 8e7bd50a 2019-08-22 stsp const struct got_error *err = NULL;
3607 8e7bd50a 2019-08-22 stsp char *template = NULL, *initial_content = NULL;
3608 8e7bd50a 2019-08-22 stsp char *tagmsg_path = NULL, *editor = NULL;
3609 8e7bd50a 2019-08-22 stsp int fd = -1;
3611 8e7bd50a 2019-08-22 stsp if (asprintf(&template, "/tmp/got-tagmsg") == -1) {
3612 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("asprintf");
3613 8e7bd50a 2019-08-22 stsp goto done;
3616 62870f63 2019-08-22 stsp if (asprintf(&initial_content, "\n# tagging commit %s as %s\n",
3617 62870f63 2019-08-22 stsp commit_id_str, tag_name) == -1) {
3618 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("asprintf");
3619 8e7bd50a 2019-08-22 stsp goto done;
3622 8e7bd50a 2019-08-22 stsp err = got_opentemp_named_fd(&tagmsg_path, &fd, template);
3624 8e7bd50a 2019-08-22 stsp goto done;
3626 8e7bd50a 2019-08-22 stsp dprintf(fd, initial_content);
3627 8e7bd50a 2019-08-22 stsp close(fd);
3629 8e7bd50a 2019-08-22 stsp err = get_editor(&editor);
3631 8e7bd50a 2019-08-22 stsp goto done;
3632 8e7bd50a 2019-08-22 stsp err = edit_logmsg(tagmsg, editor, tagmsg_path, initial_content);
3634 8e7bd50a 2019-08-22 stsp if (err == NULL || err->code == GOT_ERR_COMMIT_MSG_EMPTY) {
3635 8e7bd50a 2019-08-22 stsp unlink(tagmsg_path);
3636 8e7bd50a 2019-08-22 stsp free(tagmsg_path);
3637 8e7bd50a 2019-08-22 stsp tagmsg_path = NULL;
3639 8e7bd50a 2019-08-22 stsp free(initial_content);
3640 8e7bd50a 2019-08-22 stsp free(template);
3641 8e7bd50a 2019-08-22 stsp free(editor);
3643 8e7bd50a 2019-08-22 stsp /* Editor is done; we can now apply unveil(2) */
3644 8e7bd50a 2019-08-22 stsp if (err == NULL) {
3645 8e7bd50a 2019-08-22 stsp err = apply_unveil(repo_path, 0, NULL);
3646 8e7bd50a 2019-08-22 stsp if (err) {
3647 8e7bd50a 2019-08-22 stsp free(*tagmsg);
3648 8e7bd50a 2019-08-22 stsp *tagmsg = NULL;
3651 8e7bd50a 2019-08-22 stsp return err;
3654 8e7bd50a 2019-08-22 stsp static const struct got_error *
3655 8e7bd50a 2019-08-22 stsp add_tag(struct got_repository *repo, const char *tag_name,
3656 8e7bd50a 2019-08-22 stsp const char *commit_arg, const char *tagmsg_arg)
3658 8e7bd50a 2019-08-22 stsp const struct got_error *err = NULL;
3659 8e7bd50a 2019-08-22 stsp struct got_object_id *commit_id = NULL, *tag_id = NULL;
3660 8e7bd50a 2019-08-22 stsp char *label = NULL, *commit_id_str = NULL;
3661 8e7bd50a 2019-08-22 stsp struct got_reference *ref = NULL;
3662 8e7bd50a 2019-08-22 stsp char *refname = NULL, *tagmsg = NULL;
3663 8e7bd50a 2019-08-22 stsp const char *tagger;
3666 8e7bd50a 2019-08-22 stsp * Don't let the user create a tag named '-'.
3667 8e7bd50a 2019-08-22 stsp * While technically a valid reference name, this case is usually
3668 8e7bd50a 2019-08-22 stsp * an unintended typo.
3670 8e7bd50a 2019-08-22 stsp if (tag_name[0] == '-' && tag_name[1] == '\0')
3671 8e7bd50a 2019-08-22 stsp return got_error(GOT_ERR_BAD_REF_NAME);
3673 8e7bd50a 2019-08-22 stsp err = get_author(&tagger);
3675 8e7bd50a 2019-08-22 stsp return err;
3677 8e7bd50a 2019-08-22 stsp err = match_object_id(&commit_id, &label, commit_arg,
3678 8e7bd50a 2019-08-22 stsp GOT_OBJ_TYPE_COMMIT, 1, repo);
3680 8e7bd50a 2019-08-22 stsp goto done;
3682 8e7bd50a 2019-08-22 stsp err = got_object_id_str(&commit_id_str, commit_id);
3684 8e7bd50a 2019-08-22 stsp goto done;
3686 8e7bd50a 2019-08-22 stsp if (strncmp("refs/tags/", tag_name, 10) == 0) {
3687 8e7bd50a 2019-08-22 stsp refname = strdup(tag_name);
3688 8e7bd50a 2019-08-22 stsp if (refname == NULL) {
3689 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("strdup");
3690 8e7bd50a 2019-08-22 stsp goto done;
3692 8e7bd50a 2019-08-22 stsp tag_name += 10;
3693 8e7bd50a 2019-08-22 stsp } else if (asprintf(&refname, "refs/tags/%s", tag_name) == -1) {
3694 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("asprintf");
3695 8e7bd50a 2019-08-22 stsp goto done;
3698 8e7bd50a 2019-08-22 stsp err = got_ref_open(&ref, repo, refname, 0);
3699 8e7bd50a 2019-08-22 stsp if (err == NULL) {
3700 8e7bd50a 2019-08-22 stsp err = got_error(GOT_ERR_TAG_EXISTS);
3701 8e7bd50a 2019-08-22 stsp goto done;
3702 8e7bd50a 2019-08-22 stsp } else if (err->code != GOT_ERR_NOT_REF)
3703 8e7bd50a 2019-08-22 stsp goto done;
3705 8e7bd50a 2019-08-22 stsp if (tagmsg_arg == NULL) {
3706 8e7bd50a 2019-08-22 stsp err = get_tag_message(&tagmsg, commit_id_str,
3707 62870f63 2019-08-22 stsp tag_name, got_repo_get_path(repo));
3709 8e7bd50a 2019-08-22 stsp goto done;
3712 8e7bd50a 2019-08-22 stsp err = got_object_tag_create(&tag_id, tag_name, commit_id,
3713 8e7bd50a 2019-08-22 stsp tagger, time(NULL), tagmsg ? tagmsg : tagmsg_arg, repo);
3715 8e7bd50a 2019-08-22 stsp goto done;
3717 8e7bd50a 2019-08-22 stsp err = got_ref_alloc(&ref, refname, tag_id);
3719 8e7bd50a 2019-08-22 stsp goto done;
3721 8e7bd50a 2019-08-22 stsp err = got_ref_write(ref, repo);
3723 8e7bd50a 2019-08-22 stsp if (err == NULL) {
3724 8e7bd50a 2019-08-22 stsp char *tag_id_str;
3725 8e7bd50a 2019-08-22 stsp err = got_object_id_str(&tag_id_str, tag_id);
3726 8e7bd50a 2019-08-22 stsp printf("Created tag %s\n", tag_id_str);
3727 8e7bd50a 2019-08-22 stsp free(tag_id_str);
3731 8e7bd50a 2019-08-22 stsp got_ref_close(ref);
3732 8e7bd50a 2019-08-22 stsp free(commit_id);
3733 8e7bd50a 2019-08-22 stsp free(commit_id_str);
3734 8e7bd50a 2019-08-22 stsp free(refname);
3735 8e7bd50a 2019-08-22 stsp free(tagmsg);
3736 8e7bd50a 2019-08-22 stsp return err;
3739 8e7bd50a 2019-08-22 stsp static const struct got_error *
3740 8e7bd50a 2019-08-22 stsp cmd_tag(int argc, char *argv[])
3742 8e7bd50a 2019-08-22 stsp const struct got_error *error = NULL;
3743 8e7bd50a 2019-08-22 stsp struct got_repository *repo = NULL;
3744 8e7bd50a 2019-08-22 stsp struct got_worktree *worktree = NULL;
3745 8e7bd50a 2019-08-22 stsp char *cwd = NULL, *repo_path = NULL, *commit_id_str = NULL;
3746 8e7bd50a 2019-08-22 stsp const char *tag_name, *commit_id_arg = NULL, *tagmsg = NULL;
3747 8e7bd50a 2019-08-22 stsp int ch, do_list = 0;
3749 8e7bd50a 2019-08-22 stsp while ((ch = getopt(argc, argv, "m:r:l")) != -1) {
3750 8e7bd50a 2019-08-22 stsp switch (ch) {
3752 8e7bd50a 2019-08-22 stsp tagmsg = optarg;
3755 8e7bd50a 2019-08-22 stsp repo_path = realpath(optarg, NULL);
3756 8e7bd50a 2019-08-22 stsp if (repo_path == NULL)
3757 8e7bd50a 2019-08-22 stsp err(1, "-r option");
3758 8e7bd50a 2019-08-22 stsp got_path_strip_trailing_slashes(repo_path);
3761 8e7bd50a 2019-08-22 stsp do_list = 1;
3764 8e7bd50a 2019-08-22 stsp usage_tag();
3765 8e7bd50a 2019-08-22 stsp /* NOTREACHED */
3769 8e7bd50a 2019-08-22 stsp argc -= optind;
3770 8e7bd50a 2019-08-22 stsp argv += optind;
3772 8e7bd50a 2019-08-22 stsp if (do_list) {
3773 8e7bd50a 2019-08-22 stsp if (tagmsg)
3774 8e7bd50a 2019-08-22 stsp errx(1, "-l and -m options are mutually exclusive\n");
3775 8e7bd50a 2019-08-22 stsp if (argc > 0)
3776 8e7bd50a 2019-08-22 stsp usage_tag();
3777 8e7bd50a 2019-08-22 stsp } else if (argc < 1 || argc > 2)
3778 8e7bd50a 2019-08-22 stsp usage_tag();
3779 a2887370 2019-08-23 stsp else if (argc > 1)
3780 a2887370 2019-08-23 stsp commit_id_arg = argv[1];
3781 a2887370 2019-08-23 stsp tag_name = argv[0];
3783 8e7bd50a 2019-08-22 stsp #ifndef PROFILE
3784 8e7bd50a 2019-08-22 stsp if (do_list) {
3785 8e7bd50a 2019-08-22 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3786 8e7bd50a 2019-08-22 stsp NULL) == -1)
3787 8e7bd50a 2019-08-22 stsp err(1, "pledge");
3789 8e7bd50a 2019-08-22 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3790 8e7bd50a 2019-08-22 stsp "sendfd unveil", NULL) == -1)
3791 8e7bd50a 2019-08-22 stsp err(1, "pledge");
3794 8e7bd50a 2019-08-22 stsp cwd = getcwd(NULL, 0);
3795 8e7bd50a 2019-08-22 stsp if (cwd == NULL) {
3796 8e7bd50a 2019-08-22 stsp error = got_error_from_errno("getcwd");
3797 8e7bd50a 2019-08-22 stsp goto done;
3800 8e7bd50a 2019-08-22 stsp if (repo_path == NULL) {
3801 8e7bd50a 2019-08-22 stsp error = got_worktree_open(&worktree, cwd);
3802 8e7bd50a 2019-08-22 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
3803 8e7bd50a 2019-08-22 stsp goto done;
3805 8e7bd50a 2019-08-22 stsp error = NULL;
3806 8e7bd50a 2019-08-22 stsp if (worktree) {
3807 8e7bd50a 2019-08-22 stsp repo_path =
3808 8e7bd50a 2019-08-22 stsp strdup(got_worktree_get_repo_path(worktree));
3809 8e7bd50a 2019-08-22 stsp if (repo_path == NULL)
3810 8e7bd50a 2019-08-22 stsp error = got_error_from_errno("strdup");
3811 8e7bd50a 2019-08-22 stsp if (error)
3812 8e7bd50a 2019-08-22 stsp goto done;
3814 8e7bd50a 2019-08-22 stsp repo_path = strdup(cwd);
3815 8e7bd50a 2019-08-22 stsp if (repo_path == NULL) {
3816 8e7bd50a 2019-08-22 stsp error = got_error_from_errno("strdup");
3817 8e7bd50a 2019-08-22 stsp goto done;
3822 8e7bd50a 2019-08-22 stsp error = got_repo_open(&repo, repo_path);
3823 8e7bd50a 2019-08-22 stsp if (error != NULL)
3824 8e7bd50a 2019-08-22 stsp goto done;
3827 8e7bd50a 2019-08-22 stsp if (do_list) {
3828 8e7bd50a 2019-08-22 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
3829 8e7bd50a 2019-08-22 stsp if (error)
3830 8e7bd50a 2019-08-22 stsp goto done;
3831 8e7bd50a 2019-08-22 stsp error = list_tags(repo, worktree);
3833 8e7bd50a 2019-08-22 stsp if (tagmsg) {
3834 8e7bd50a 2019-08-22 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
3835 8e7bd50a 2019-08-22 stsp if (error)
3836 8e7bd50a 2019-08-22 stsp goto done;
3839 8e7bd50a 2019-08-22 stsp if (commit_id_arg == NULL) {
3840 8e7bd50a 2019-08-22 stsp struct got_reference *head_ref;
3841 8e7bd50a 2019-08-22 stsp struct got_object_id *commit_id;
3842 8e7bd50a 2019-08-22 stsp error = got_ref_open(&head_ref, repo,
3843 8e7bd50a 2019-08-22 stsp worktree ? got_worktree_get_head_ref_name(worktree)
3844 8e7bd50a 2019-08-22 stsp : GOT_REF_HEAD, 0);
3845 8e7bd50a 2019-08-22 stsp if (error)
3846 8e7bd50a 2019-08-22 stsp goto done;
3847 8e7bd50a 2019-08-22 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
3848 8e7bd50a 2019-08-22 stsp got_ref_close(head_ref);
3849 8e7bd50a 2019-08-22 stsp if (error)
3850 8e7bd50a 2019-08-22 stsp goto done;
3851 8e7bd50a 2019-08-22 stsp error = got_object_id_str(&commit_id_str, commit_id);
3852 8e7bd50a 2019-08-22 stsp free(commit_id);
3853 8e7bd50a 2019-08-22 stsp if (error)
3854 8e7bd50a 2019-08-22 stsp goto done;
3857 8e7bd50a 2019-08-22 stsp error = add_tag(repo, tag_name,
3858 8e7bd50a 2019-08-22 stsp commit_id_str ? commit_id_str : commit_id_arg, tagmsg);
3862 8e7bd50a 2019-08-22 stsp got_repo_close(repo);
3863 8e7bd50a 2019-08-22 stsp if (worktree)
3864 8e7bd50a 2019-08-22 stsp got_worktree_close(worktree);
3865 8e7bd50a 2019-08-22 stsp free(cwd);
3866 8e7bd50a 2019-08-22 stsp free(repo_path);
3867 8e7bd50a 2019-08-22 stsp free(commit_id_str);
3868 8e7bd50a 2019-08-22 stsp return error;
3871 8e7bd50a 2019-08-22 stsp __dead static void
3872 d00136be 2019-03-26 stsp usage_add(void)
3874 fbb7e5c7 2019-05-11 stsp fprintf(stderr, "usage: %s add file-path ...\n", getprogname());
3878 d00136be 2019-03-26 stsp static const struct got_error *
3879 d00136be 2019-03-26 stsp cmd_add(int argc, char *argv[])
3881 d00136be 2019-03-26 stsp const struct got_error *error = NULL;
3882 031a5338 2019-03-26 stsp struct got_repository *repo = NULL;
3883 d00136be 2019-03-26 stsp struct got_worktree *worktree = NULL;
3884 1dd54920 2019-05-11 stsp char *cwd = NULL;
3885 1dd54920 2019-05-11 stsp struct got_pathlist_head paths;
3886 1dd54920 2019-05-11 stsp struct got_pathlist_entry *pe;
3889 1dd54920 2019-05-11 stsp TAILQ_INIT(&paths);
3891 d00136be 2019-03-26 stsp while ((ch = getopt(argc, argv, "")) != -1) {
3892 d00136be 2019-03-26 stsp switch (ch) {
3894 d00136be 2019-03-26 stsp usage_add();
3895 d00136be 2019-03-26 stsp /* NOTREACHED */
3899 d00136be 2019-03-26 stsp argc -= optind;
3900 d00136be 2019-03-26 stsp argv += optind;
3902 43012d58 2019-07-14 stsp #ifndef PROFILE
3903 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3904 43012d58 2019-07-14 stsp NULL) == -1)
3905 43012d58 2019-07-14 stsp err(1, "pledge");
3907 723c305c 2019-05-11 jcs if (argc < 1)
3908 d00136be 2019-03-26 stsp usage_add();
3910 d00136be 2019-03-26 stsp cwd = getcwd(NULL, 0);
3911 d00136be 2019-03-26 stsp if (cwd == NULL) {
3912 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
3913 d00136be 2019-03-26 stsp goto done;
3916 d00136be 2019-03-26 stsp error = got_worktree_open(&worktree, cwd);
3917 d00136be 2019-03-26 stsp if (error)
3918 d00136be 2019-03-26 stsp goto done;
3920 031a5338 2019-03-26 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3921 031a5338 2019-03-26 stsp if (error != NULL)
3922 031a5338 2019-03-26 stsp goto done;
3924 031a5338 2019-03-26 stsp error = apply_unveil(got_repo_get_path(repo), 1,
3925 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
3926 d00136be 2019-03-26 stsp if (error)
3927 d00136be 2019-03-26 stsp goto done;
3929 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3930 6d022e97 2019-08-04 stsp if (error)
3931 6d022e97 2019-08-04 stsp goto done;
3933 1dd54920 2019-05-11 stsp error = got_worktree_schedule_add(worktree, &paths, print_status,
3934 1dd54920 2019-05-11 stsp NULL, repo);
3937 031a5338 2019-03-26 stsp got_repo_close(repo);
3938 d00136be 2019-03-26 stsp if (worktree)
3939 d00136be 2019-03-26 stsp got_worktree_close(worktree);
3940 1dd54920 2019-05-11 stsp TAILQ_FOREACH(pe, &paths, entry)
3941 1dd54920 2019-05-11 stsp free((char *)pe->path);
3942 1dd54920 2019-05-11 stsp got_pathlist_free(&paths);
3943 2ec1f75b 2019-03-26 stsp free(cwd);
3944 2ec1f75b 2019-03-26 stsp return error;
3947 2ec1f75b 2019-03-26 stsp __dead static void
3948 648e4ef7 2019-07-09 stsp usage_remove(void)
3950 648e4ef7 2019-07-09 stsp fprintf(stderr, "usage: %s remove [-f] file-path ...\n", getprogname());
3954 2a06fe5f 2019-08-24 stsp static const struct got_error *
3955 2a06fe5f 2019-08-24 stsp print_remove_status(void *arg, unsigned char status,
3956 2a06fe5f 2019-08-24 stsp unsigned char staged_status, const char *path,
3957 2a06fe5f 2019-08-24 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
3958 2a06fe5f 2019-08-24 stsp struct got_object_id *commit_id)
3960 2a06fe5f 2019-08-24 stsp if (status == GOT_STATUS_NONEXISTENT)
3961 2a06fe5f 2019-08-24 stsp return NULL;
3962 2a06fe5f 2019-08-24 stsp if (status == staged_status && (status == GOT_STATUS_DELETE))
3963 2a06fe5f 2019-08-24 stsp status = GOT_STATUS_NO_CHANGE;
3964 2a06fe5f 2019-08-24 stsp printf("%c%c %s\n", status, staged_status, path);
3965 2a06fe5f 2019-08-24 stsp return NULL;
3968 2ec1f75b 2019-03-26 stsp static const struct got_error *
3969 648e4ef7 2019-07-09 stsp cmd_remove(int argc, char *argv[])
3971 2ec1f75b 2019-03-26 stsp const struct got_error *error = NULL;
3972 2ec1f75b 2019-03-26 stsp struct got_worktree *worktree = NULL;
3973 2ec1f75b 2019-03-26 stsp struct got_repository *repo = NULL;
3974 17ed4618 2019-06-02 stsp char *cwd = NULL;
3975 17ed4618 2019-06-02 stsp struct got_pathlist_head paths;
3976 17ed4618 2019-06-02 stsp struct got_pathlist_entry *pe;
3977 6d022e97 2019-08-04 stsp int ch, delete_local_mods = 0;
3979 17ed4618 2019-06-02 stsp TAILQ_INIT(&paths);
3981 2ec1f75b 2019-03-26 stsp while ((ch = getopt(argc, argv, "f")) != -1) {
3982 2ec1f75b 2019-03-26 stsp switch (ch) {
3984 2ec1f75b 2019-03-26 stsp delete_local_mods = 1;
3987 2ec1f75b 2019-03-26 stsp usage_add();
3988 2ec1f75b 2019-03-26 stsp /* NOTREACHED */
3992 2ec1f75b 2019-03-26 stsp argc -= optind;
3993 2ec1f75b 2019-03-26 stsp argv += optind;
3995 43012d58 2019-07-14 stsp #ifndef PROFILE
3996 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3997 43012d58 2019-07-14 stsp NULL) == -1)
3998 43012d58 2019-07-14 stsp err(1, "pledge");
4000 17ed4618 2019-06-02 stsp if (argc < 1)
4001 648e4ef7 2019-07-09 stsp usage_remove();
4003 2ec1f75b 2019-03-26 stsp cwd = getcwd(NULL, 0);
4004 2ec1f75b 2019-03-26 stsp if (cwd == NULL) {
4005 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
4006 2ec1f75b 2019-03-26 stsp goto done;
4008 2ec1f75b 2019-03-26 stsp error = got_worktree_open(&worktree, cwd);
4009 2ec1f75b 2019-03-26 stsp if (error)
4010 2ec1f75b 2019-03-26 stsp goto done;
4012 2ec1f75b 2019-03-26 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
4014 2ec1f75b 2019-03-26 stsp goto done;
4016 c2253644 2019-03-26 stsp error = apply_unveil(got_repo_get_path(repo), 1,
4017 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
4018 2ec1f75b 2019-03-26 stsp if (error)
4019 2ec1f75b 2019-03-26 stsp goto done;
4021 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4022 6d022e97 2019-08-04 stsp if (error)
4023 6d022e97 2019-08-04 stsp goto done;
4025 17ed4618 2019-06-02 stsp error = got_worktree_schedule_delete(worktree, &paths,
4026 2a06fe5f 2019-08-24 stsp delete_local_mods, print_remove_status, NULL, repo);
4027 a129376b 2019-03-28 stsp if (error)
4028 a129376b 2019-03-28 stsp goto done;
4031 a129376b 2019-03-28 stsp got_repo_close(repo);
4032 a129376b 2019-03-28 stsp if (worktree)
4033 a129376b 2019-03-28 stsp got_worktree_close(worktree);
4034 17ed4618 2019-06-02 stsp TAILQ_FOREACH(pe, &paths, entry)
4035 17ed4618 2019-06-02 stsp free((char *)pe->path);
4036 17ed4618 2019-06-02 stsp got_pathlist_free(&paths);
4037 a129376b 2019-03-28 stsp free(cwd);
4038 a129376b 2019-03-28 stsp return error;
4041 a129376b 2019-03-28 stsp __dead static void
4042 a129376b 2019-03-28 stsp usage_revert(void)
4044 33aa809d 2019-08-08 stsp fprintf(stderr, "usage: %s revert [-p] [-F response-script] [-R] "
4045 33aa809d 2019-08-08 stsp "path ...\n", getprogname());
4049 1ee397ad 2019-07-12 stsp static const struct got_error *
4050 a129376b 2019-03-28 stsp revert_progress(void *arg, unsigned char status, const char *path)
4052 a129376b 2019-03-28 stsp while (path[0] == '/')
4054 a129376b 2019-03-28 stsp printf("%c %s\n", status, path);
4055 33aa809d 2019-08-08 stsp return NULL;
4058 33aa809d 2019-08-08 stsp struct choose_patch_arg {
4059 33aa809d 2019-08-08 stsp FILE *patch_script_file;
4060 33aa809d 2019-08-08 stsp const char *action;
4063 33aa809d 2019-08-08 stsp static const struct got_error *
4064 33aa809d 2019-08-08 stsp show_change(unsigned char status, const char *path, FILE *patch_file, int n,
4065 33aa809d 2019-08-08 stsp int nchanges, const char *action)
4067 33aa809d 2019-08-08 stsp char *line = NULL;
4068 33aa809d 2019-08-08 stsp size_t linesize = 0;
4069 33aa809d 2019-08-08 stsp ssize_t linelen;
4071 33aa809d 2019-08-08 stsp switch (status) {
4072 33aa809d 2019-08-08 stsp case GOT_STATUS_ADD:
4073 33aa809d 2019-08-08 stsp printf("A %s\n%s this addition? [y/n] ", path, action);
4075 33aa809d 2019-08-08 stsp case GOT_STATUS_DELETE:
4076 33aa809d 2019-08-08 stsp printf("D %s\n%s this deletion? [y/n] ", path, action);
4078 33aa809d 2019-08-08 stsp case GOT_STATUS_MODIFY:
4079 33aa809d 2019-08-08 stsp if (fseek(patch_file, 0L, SEEK_SET) == -1)
4080 33aa809d 2019-08-08 stsp return got_error_from_errno("fseek");
4081 33aa809d 2019-08-08 stsp printf(GOT_COMMIT_SEP_STR);
4082 9516e7cb 2019-08-11 stsp while ((linelen = getline(&line, &linesize, patch_file)) != -1)
4083 33aa809d 2019-08-08 stsp printf("%s", line);
4084 33aa809d 2019-08-08 stsp if (ferror(patch_file))
4085 33aa809d 2019-08-08 stsp return got_error_from_errno("getline");
4086 33aa809d 2019-08-08 stsp printf(GOT_COMMIT_SEP_STR);
4087 33aa809d 2019-08-08 stsp printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
4088 33aa809d 2019-08-08 stsp path, n, nchanges, action);
4091 33aa809d 2019-08-08 stsp return got_error_path(path, GOT_ERR_FILE_STATUS);
4094 33aa809d 2019-08-08 stsp return NULL;
4097 33aa809d 2019-08-08 stsp static const struct got_error *
4098 33aa809d 2019-08-08 stsp choose_patch(int *choice, void *arg, unsigned char status, const char *path,
4099 33aa809d 2019-08-08 stsp FILE *patch_file, int n, int nchanges)
4101 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
4102 33aa809d 2019-08-08 stsp char *line = NULL;
4103 33aa809d 2019-08-08 stsp size_t linesize = 0;
4104 33aa809d 2019-08-08 stsp ssize_t linelen;
4105 33aa809d 2019-08-08 stsp int resp = ' ';
4106 33aa809d 2019-08-08 stsp struct choose_patch_arg *a = arg;
4108 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NONE;
4110 33aa809d 2019-08-08 stsp if (a->patch_script_file) {
4112 33aa809d 2019-08-08 stsp err = show_change(status, path, patch_file, n, nchanges,
4113 33aa809d 2019-08-08 stsp a->action);
4115 33aa809d 2019-08-08 stsp return err;
4116 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, a->patch_script_file);
4117 33aa809d 2019-08-08 stsp if (linelen == -1) {
4118 33aa809d 2019-08-08 stsp if (ferror(a->patch_script_file))
4119 33aa809d 2019-08-08 stsp return got_error_from_errno("getline");
4120 33aa809d 2019-08-08 stsp return NULL;
4122 33aa809d 2019-08-08 stsp nl = strchr(line, '\n');
4124 33aa809d 2019-08-08 stsp *nl = '\0';
4125 33aa809d 2019-08-08 stsp if (strcmp(line, "y") == 0) {
4126 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_YES;
4127 33aa809d 2019-08-08 stsp printf("y\n");
4128 33aa809d 2019-08-08 stsp } else if (strcmp(line, "n") == 0) {
4129 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NO;
4130 33aa809d 2019-08-08 stsp printf("n\n");
4131 33aa809d 2019-08-08 stsp } else if (strcmp(line, "q") == 0 &&
4132 33aa809d 2019-08-08 stsp status == GOT_STATUS_MODIFY) {
4133 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_QUIT;
4134 33aa809d 2019-08-08 stsp printf("q\n");
4136 33aa809d 2019-08-08 stsp printf("invalid response '%s'\n", line);
4137 33aa809d 2019-08-08 stsp free(line);
4138 33aa809d 2019-08-08 stsp return NULL;
4141 33aa809d 2019-08-08 stsp while (resp != 'y' && resp != 'n' && resp != 'q') {
4142 33aa809d 2019-08-08 stsp err = show_change(status, path, patch_file, n, nchanges,
4143 33aa809d 2019-08-08 stsp a->action);
4145 33aa809d 2019-08-08 stsp return err;
4146 33aa809d 2019-08-08 stsp resp = getchar();
4147 33aa809d 2019-08-08 stsp if (resp == '\n')
4148 33aa809d 2019-08-08 stsp resp = getchar();
4149 33aa809d 2019-08-08 stsp if (status == GOT_STATUS_MODIFY) {
4150 33aa809d 2019-08-08 stsp if (resp != 'y' && resp != 'n' && resp != 'q') {
4151 33aa809d 2019-08-08 stsp printf("invalid response '%c'\n", resp);
4152 33aa809d 2019-08-08 stsp resp = ' ';
4154 33aa809d 2019-08-08 stsp } else if (resp != 'y' && resp != 'n') {
4155 33aa809d 2019-08-08 stsp printf("invalid response '%c'\n", resp);
4156 33aa809d 2019-08-08 stsp resp = ' ';
4160 33aa809d 2019-08-08 stsp if (resp == 'y')
4161 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_YES;
4162 33aa809d 2019-08-08 stsp else if (resp == 'n')
4163 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NO;
4164 33aa809d 2019-08-08 stsp else if (resp == 'q' && status == GOT_STATUS_MODIFY)
4165 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_QUIT;
4167 1ee397ad 2019-07-12 stsp return NULL;
4171 a129376b 2019-03-28 stsp static const struct got_error *
4172 a129376b 2019-03-28 stsp cmd_revert(int argc, char *argv[])
4174 a129376b 2019-03-28 stsp const struct got_error *error = NULL;
4175 a129376b 2019-03-28 stsp struct got_worktree *worktree = NULL;
4176 a129376b 2019-03-28 stsp struct got_repository *repo = NULL;
4177 a129376b 2019-03-28 stsp char *cwd = NULL, *path = NULL;
4178 e20a8b6f 2019-06-04 stsp struct got_pathlist_head paths;
4179 0f6d7415 2019-08-08 stsp struct got_pathlist_entry *pe;
4180 33aa809d 2019-08-08 stsp int ch, can_recurse = 0, pflag = 0;
4181 33aa809d 2019-08-08 stsp FILE *patch_script_file = NULL;
4182 33aa809d 2019-08-08 stsp const char *patch_script_path = NULL;
4183 33aa809d 2019-08-08 stsp struct choose_patch_arg cpa;
4185 e20a8b6f 2019-06-04 stsp TAILQ_INIT(&paths);
4187 33aa809d 2019-08-08 stsp while ((ch = getopt(argc, argv, "pF:R")) != -1) {
4188 a129376b 2019-03-28 stsp switch (ch) {
4190 33aa809d 2019-08-08 stsp pflag = 1;
4193 33aa809d 2019-08-08 stsp patch_script_path = optarg;
4196 0f6d7415 2019-08-08 stsp can_recurse = 1;
4199 a129376b 2019-03-28 stsp usage_revert();
4200 a129376b 2019-03-28 stsp /* NOTREACHED */
4204 a129376b 2019-03-28 stsp argc -= optind;
4205 a129376b 2019-03-28 stsp argv += optind;
4207 43012d58 2019-07-14 stsp #ifndef PROFILE
4208 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4209 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
4210 43012d58 2019-07-14 stsp err(1, "pledge");
4212 e20a8b6f 2019-06-04 stsp if (argc < 1)
4213 a129376b 2019-03-28 stsp usage_revert();
4214 33aa809d 2019-08-08 stsp if (patch_script_path && !pflag)
4215 33aa809d 2019-08-08 stsp errx(1, "-F option can only be used together with -p option");
4217 a129376b 2019-03-28 stsp cwd = getcwd(NULL, 0);
4218 a129376b 2019-03-28 stsp if (cwd == NULL) {
4219 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
4220 a129376b 2019-03-28 stsp goto done;
4222 a129376b 2019-03-28 stsp error = got_worktree_open(&worktree, cwd);
4223 2ec1f75b 2019-03-26 stsp if (error)
4224 2ec1f75b 2019-03-26 stsp goto done;
4226 a129376b 2019-03-28 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
4227 a129376b 2019-03-28 stsp if (error != NULL)
4228 a129376b 2019-03-28 stsp goto done;
4230 33aa809d 2019-08-08 stsp if (patch_script_path) {
4231 33aa809d 2019-08-08 stsp patch_script_file = fopen(patch_script_path, "r");
4232 33aa809d 2019-08-08 stsp if (patch_script_file == NULL) {
4233 33aa809d 2019-08-08 stsp error = got_error_from_errno2("fopen",
4234 33aa809d 2019-08-08 stsp patch_script_path);
4235 33aa809d 2019-08-08 stsp goto done;
4238 a129376b 2019-03-28 stsp error = apply_unveil(got_repo_get_path(repo), 1,
4239 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
4240 a129376b 2019-03-28 stsp if (error)
4241 a129376b 2019-03-28 stsp goto done;
4243 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4244 6d022e97 2019-08-04 stsp if (error)
4245 6d022e97 2019-08-04 stsp goto done;
4247 0f6d7415 2019-08-08 stsp if (!can_recurse) {
4248 0f6d7415 2019-08-08 stsp char *ondisk_path;
4249 0f6d7415 2019-08-08 stsp struct stat sb;
4250 0f6d7415 2019-08-08 stsp TAILQ_FOREACH(pe, &paths, entry) {
4251 0f6d7415 2019-08-08 stsp if (asprintf(&ondisk_path, "%s/%s",
4252 0f6d7415 2019-08-08 stsp got_worktree_get_root_path(worktree),
4253 0f6d7415 2019-08-08 stsp pe->path) == -1) {
4254 0f6d7415 2019-08-08 stsp error = got_error_from_errno("asprintf");
4255 0f6d7415 2019-08-08 stsp goto done;
4257 0f6d7415 2019-08-08 stsp if (lstat(ondisk_path, &sb) == -1) {
4258 0f6d7415 2019-08-08 stsp if (errno == ENOENT) {
4259 0f6d7415 2019-08-08 stsp free(ondisk_path);
4262 0f6d7415 2019-08-08 stsp error = got_error_from_errno2("lstat",
4263 0f6d7415 2019-08-08 stsp ondisk_path);
4264 0f6d7415 2019-08-08 stsp free(ondisk_path);
4265 0f6d7415 2019-08-08 stsp goto done;
4267 0f6d7415 2019-08-08 stsp free(ondisk_path);
4268 0f6d7415 2019-08-08 stsp if (S_ISDIR(sb.st_mode)) {
4269 0f6d7415 2019-08-08 stsp error = got_error_msg(GOT_ERR_BAD_PATH,
4270 0f6d7415 2019-08-08 stsp "reverting directories requires -R option");
4271 0f6d7415 2019-08-08 stsp goto done;
4276 33aa809d 2019-08-08 stsp cpa.patch_script_file = patch_script_file;
4277 33aa809d 2019-08-08 stsp cpa.action = "revert";
4278 33aa809d 2019-08-08 stsp error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
4279 33aa809d 2019-08-08 stsp pflag ? choose_patch : NULL, &cpa, repo);
4280 a129376b 2019-03-28 stsp if (error)
4281 a129376b 2019-03-28 stsp goto done;
4283 33aa809d 2019-08-08 stsp if (patch_script_file && fclose(patch_script_file) == EOF &&
4284 33aa809d 2019-08-08 stsp error == NULL)
4285 33aa809d 2019-08-08 stsp error = got_error_from_errno2("fclose", patch_script_path);
4287 2ec1f75b 2019-03-26 stsp got_repo_close(repo);
4288 2ec1f75b 2019-03-26 stsp if (worktree)
4289 2ec1f75b 2019-03-26 stsp got_worktree_close(worktree);
4290 2ec1f75b 2019-03-26 stsp free(path);
4291 d00136be 2019-03-26 stsp free(cwd);
4292 6bad629b 2019-02-04 stsp return error;
4295 c4296144 2019-05-09 stsp __dead static void
4296 c4296144 2019-05-09 stsp usage_commit(void)
4298 5c1e53bc 2019-07-28 stsp fprintf(stderr, "usage: %s commit [-m msg] [path ...]\n",
4299 5c1e53bc 2019-07-28 stsp getprogname());
4303 e2ba3d07 2019-05-13 stsp struct collect_commit_logmsg_arg {
4304 e2ba3d07 2019-05-13 stsp const char *cmdline_log;
4305 e2ba3d07 2019-05-13 stsp const char *editor;
4306 e0870e44 2019-05-13 stsp const char *worktree_path;
4307 76d98825 2019-06-03 stsp const char *branch_name;
4308 314a6357 2019-05-13 stsp const char *repo_path;
4309 e0870e44 2019-05-13 stsp char *logmsg_path;
4313 33ad4cbe 2019-05-12 jcs static const struct got_error *
4314 33ad4cbe 2019-05-12 jcs collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
4317 76d98825 2019-06-03 stsp char *initial_content = NULL;
4318 33ad4cbe 2019-05-12 jcs struct got_pathlist_entry *pe;
4319 33ad4cbe 2019-05-12 jcs const struct got_error *err = NULL;
4320 e0870e44 2019-05-13 stsp char *template = NULL;
4321 e2ba3d07 2019-05-13 stsp struct collect_commit_logmsg_arg *a = arg;
4323 33ad4cbe 2019-05-12 jcs size_t len;
4325 33ad4cbe 2019-05-12 jcs /* if a message was specified on the command line, just use it */
4326 e2ba3d07 2019-05-13 stsp if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
4327 e2ba3d07 2019-05-13 stsp len = strlen(a->cmdline_log) + 1;
4328 33ad4cbe 2019-05-12 jcs *logmsg = malloc(len + 1);
4329 9f42ff69 2019-05-13 stsp if (*logmsg == NULL)
4330 638f9024 2019-05-13 stsp return got_error_from_errno("malloc");
4331 e2ba3d07 2019-05-13 stsp strlcpy(*logmsg, a->cmdline_log, len);
4332 33ad4cbe 2019-05-12 jcs return NULL;
4335 e0870e44 2019-05-13 stsp if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
4336 76d98825 2019-06-03 stsp return got_error_from_errno("asprintf");
4338 76d98825 2019-06-03 stsp if (asprintf(&initial_content,
4339 76d98825 2019-06-03 stsp "\n# changes to be committed on branch %s:\n",
4340 76d98825 2019-06-03 stsp a->branch_name) == -1)
4341 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
4343 e0870e44 2019-05-13 stsp err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
4345 e0870e44 2019-05-13 stsp goto done;
4347 e0870e44 2019-05-13 stsp dprintf(fd, initial_content);
4349 33ad4cbe 2019-05-12 jcs TAILQ_FOREACH(pe, commitable_paths, entry) {
4350 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
4351 8656d6c4 2019-05-20 stsp dprintf(fd, "# %c %s\n",
4352 8656d6c4 2019-05-20 stsp got_commitable_get_status(ct),
4353 8656d6c4 2019-05-20 stsp got_commitable_get_path(ct));
4357 3ce1b845 2019-07-15 stsp err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content);
4359 e2af0fd1 2019-08-08 stsp if (err == NULL || err->code == GOT_ERR_COMMIT_MSG_EMPTY) {
4360 e2af0fd1 2019-08-08 stsp unlink(a->logmsg_path);
4361 e2af0fd1 2019-08-08 stsp free(a->logmsg_path);
4362 e2af0fd1 2019-08-08 stsp a->logmsg_path = NULL;
4364 76d98825 2019-06-03 stsp free(initial_content);
4365 e0870e44 2019-05-13 stsp free(template);
4367 314a6357 2019-05-13 stsp /* Editor is done; we can now apply unveil(2) */
4368 3ce1b845 2019-07-15 stsp if (err == NULL) {
4369 c530dc23 2019-07-23 stsp err = apply_unveil(a->repo_path, 0, a->worktree_path);
4370 3ce1b845 2019-07-15 stsp if (err) {
4371 3ce1b845 2019-07-15 stsp free(*logmsg);
4372 3ce1b845 2019-07-15 stsp *logmsg = NULL;
4375 33ad4cbe 2019-05-12 jcs return err;
4378 c4296144 2019-05-09 stsp static const struct got_error *
4379 c4296144 2019-05-09 stsp cmd_commit(int argc, char *argv[])
4381 c4296144 2019-05-09 stsp const struct got_error *error = NULL;
4382 c4296144 2019-05-09 stsp struct got_worktree *worktree = NULL;
4383 c4296144 2019-05-09 stsp struct got_repository *repo = NULL;
4384 5c1e53bc 2019-07-28 stsp char *cwd = NULL, *id_str = NULL;
4385 c4296144 2019-05-09 stsp struct got_object_id *id = NULL;
4386 33ad4cbe 2019-05-12 jcs const char *logmsg = NULL;
4387 84792843 2019-08-09 stsp const char *author;
4388 e2ba3d07 2019-05-13 stsp struct collect_commit_logmsg_arg cl_arg;
4389 e2ba3d07 2019-05-13 stsp char *editor = NULL;
4390 916f288c 2019-07-30 stsp int ch, rebase_in_progress, histedit_in_progress;
4391 5c1e53bc 2019-07-28 stsp struct got_pathlist_head paths;
4393 5c1e53bc 2019-07-28 stsp TAILQ_INIT(&paths);
4394 6ac5a73c 2019-08-08 stsp cl_arg.logmsg_path = NULL;
4396 c4296144 2019-05-09 stsp while ((ch = getopt(argc, argv, "m:")) != -1) {
4397 c4296144 2019-05-09 stsp switch (ch) {
4399 c4296144 2019-05-09 stsp logmsg = optarg;
4402 c4296144 2019-05-09 stsp usage_commit();
4403 c4296144 2019-05-09 stsp /* NOTREACHED */
4407 c4296144 2019-05-09 stsp argc -= optind;
4408 c4296144 2019-05-09 stsp argv += optind;
4410 43012d58 2019-07-14 stsp #ifndef PROFILE
4411 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4412 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
4413 43012d58 2019-07-14 stsp err(1, "pledge");
4415 84792843 2019-08-09 stsp error = get_author(&author);
4416 84792843 2019-08-09 stsp if (error)
4417 84792843 2019-08-09 stsp return error;
4419 c4296144 2019-05-09 stsp cwd = getcwd(NULL, 0);
4420 c4296144 2019-05-09 stsp if (cwd == NULL) {
4421 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
4422 c4296144 2019-05-09 stsp goto done;
4424 c4296144 2019-05-09 stsp error = got_worktree_open(&worktree, cwd);
4425 7d5807f4 2019-07-11 stsp if (error)
4426 7d5807f4 2019-07-11 stsp goto done;
4428 a698f62e 2019-07-25 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
4429 c4296144 2019-05-09 stsp if (error)
4430 a698f62e 2019-07-25 stsp goto done;
4431 a698f62e 2019-07-25 stsp if (rebase_in_progress) {
4432 a698f62e 2019-07-25 stsp error = got_error(GOT_ERR_REBASING);
4433 c4296144 2019-05-09 stsp goto done;
4436 916f288c 2019-07-30 stsp error = got_worktree_histedit_in_progress(&histedit_in_progress,
4437 916f288c 2019-07-30 stsp worktree);
4438 5c1e53bc 2019-07-28 stsp if (error)
4439 5c1e53bc 2019-07-28 stsp goto done;
4441 c4296144 2019-05-09 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
4442 c4296144 2019-05-09 stsp if (error != NULL)
4443 c4296144 2019-05-09 stsp goto done;
4446 314a6357 2019-05-13 stsp * unveil(2) traverses exec(2); if an editor is used we have
4447 314a6357 2019-05-13 stsp * to apply unveil after the log message has been written.
4449 314a6357 2019-05-13 stsp if (logmsg == NULL || strlen(logmsg) == 0)
4450 314a6357 2019-05-13 stsp error = get_editor(&editor);
4452 314a6357 2019-05-13 stsp error = apply_unveil(got_repo_get_path(repo), 0,
4453 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
4454 c4296144 2019-05-09 stsp if (error)
4455 c4296144 2019-05-09 stsp goto done;
4457 087fb88c 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4458 087fb88c 2019-08-04 stsp if (error)
4459 087fb88c 2019-08-04 stsp goto done;
4461 e2ba3d07 2019-05-13 stsp cl_arg.editor = editor;
4462 e2ba3d07 2019-05-13 stsp cl_arg.cmdline_log = logmsg;
4463 e0870e44 2019-05-13 stsp cl_arg.worktree_path = got_worktree_get_root_path(worktree);
4464 76d98825 2019-06-03 stsp cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
4465 916f288c 2019-07-30 stsp if (!histedit_in_progress) {
4466 916f288c 2019-07-30 stsp if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
4467 916f288c 2019-07-30 stsp error = got_error(GOT_ERR_COMMIT_BRANCH);
4468 916f288c 2019-07-30 stsp goto done;
4470 916f288c 2019-07-30 stsp cl_arg.branch_name += 11;
4472 314a6357 2019-05-13 stsp cl_arg.repo_path = got_repo_get_path(repo);
4473 84792843 2019-08-09 stsp error = got_worktree_commit(&id, worktree, &paths, author, NULL,
4474 e2ba3d07 2019-05-13 stsp collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
4475 e0870e44 2019-05-13 stsp if (error) {
4476 e0870e44 2019-05-13 stsp if (cl_arg.logmsg_path)
4477 e0870e44 2019-05-13 stsp fprintf(stderr, "%s: log message preserved in %s\n",
4478 e0870e44 2019-05-13 stsp getprogname(), cl_arg.logmsg_path);
4479 c4296144 2019-05-09 stsp goto done;
4482 e0870e44 2019-05-13 stsp if (cl_arg.logmsg_path)
4483 e0870e44 2019-05-13 stsp unlink(cl_arg.logmsg_path);
4485 c4296144 2019-05-09 stsp error = got_object_id_str(&id_str, id);
4486 c4296144 2019-05-09 stsp if (error)
4487 c4296144 2019-05-09 stsp goto done;
4488 a7648d7a 2019-06-02 stsp printf("Created commit %s\n", id_str);
4490 6ac5a73c 2019-08-08 stsp free(cl_arg.logmsg_path);
4492 c4296144 2019-05-09 stsp got_repo_close(repo);
4493 c4296144 2019-05-09 stsp if (worktree)
4494 c4296144 2019-05-09 stsp got_worktree_close(worktree);
4495 c4296144 2019-05-09 stsp free(cwd);
4496 c4296144 2019-05-09 stsp free(id_str);
4497 e2ba3d07 2019-05-13 stsp free(editor);
4498 234035bc 2019-06-01 stsp return error;
4501 234035bc 2019-06-01 stsp __dead static void
4502 234035bc 2019-06-01 stsp usage_cherrypick(void)
4504 234035bc 2019-06-01 stsp fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
4508 234035bc 2019-06-01 stsp static const struct got_error *
4509 234035bc 2019-06-01 stsp cmd_cherrypick(int argc, char *argv[])
4511 234035bc 2019-06-01 stsp const struct got_error *error = NULL;
4512 234035bc 2019-06-01 stsp struct got_worktree *worktree = NULL;
4513 234035bc 2019-06-01 stsp struct got_repository *repo = NULL;
4514 234035bc 2019-06-01 stsp char *cwd = NULL, *commit_id_str = NULL;
4515 234035bc 2019-06-01 stsp struct got_object_id *commit_id = NULL;
4516 234035bc 2019-06-01 stsp struct got_commit_object *commit = NULL;
4517 234035bc 2019-06-01 stsp struct got_object_qid *pid;
4518 234035bc 2019-06-01 stsp struct got_reference *head_ref = NULL;
4519 234035bc 2019-06-01 stsp int ch, did_something = 0;
4521 234035bc 2019-06-01 stsp while ((ch = getopt(argc, argv, "")) != -1) {
4522 234035bc 2019-06-01 stsp switch (ch) {
4524 234035bc 2019-06-01 stsp usage_cherrypick();
4525 234035bc 2019-06-01 stsp /* NOTREACHED */
4529 234035bc 2019-06-01 stsp argc -= optind;
4530 234035bc 2019-06-01 stsp argv += optind;
4532 43012d58 2019-07-14 stsp #ifndef PROFILE
4533 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4534 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
4535 43012d58 2019-07-14 stsp err(1, "pledge");
4537 234035bc 2019-06-01 stsp if (argc != 1)
4538 234035bc 2019-06-01 stsp usage_cherrypick();
4540 234035bc 2019-06-01 stsp cwd = getcwd(NULL, 0);
4541 234035bc 2019-06-01 stsp if (cwd == NULL) {
4542 234035bc 2019-06-01 stsp error = got_error_from_errno("getcwd");
4543 234035bc 2019-06-01 stsp goto done;
4545 234035bc 2019-06-01 stsp error = got_worktree_open(&worktree, cwd);
4546 234035bc 2019-06-01 stsp if (error)
4547 234035bc 2019-06-01 stsp goto done;
4549 234035bc 2019-06-01 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
4550 234035bc 2019-06-01 stsp if (error != NULL)
4551 234035bc 2019-06-01 stsp goto done;
4553 234035bc 2019-06-01 stsp error = apply_unveil(got_repo_get_path(repo), 0,
4554 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
4555 234035bc 2019-06-01 stsp if (error)
4556 234035bc 2019-06-01 stsp goto done;
4558 dd88155e 2019-06-29 stsp error = got_repo_match_object_id_prefix(&commit_id, argv[0],
4559 dd88155e 2019-06-29 stsp GOT_OBJ_TYPE_COMMIT, repo);
4560 234035bc 2019-06-01 stsp if (error != NULL) {
4561 234035bc 2019-06-01 stsp struct got_reference *ref;
4562 234035bc 2019-06-01 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
4563 234035bc 2019-06-01 stsp goto done;
4564 234035bc 2019-06-01 stsp error = got_ref_open(&ref, repo, argv[0], 0);
4565 234035bc 2019-06-01 stsp if (error != NULL)
4566 234035bc 2019-06-01 stsp goto done;
4567 234035bc 2019-06-01 stsp error = got_ref_resolve(&commit_id, repo, ref);
4568 234035bc 2019-06-01 stsp got_ref_close(ref);
4569 234035bc 2019-06-01 stsp if (error != NULL)
4570 234035bc 2019-06-01 stsp goto done;
4572 234035bc 2019-06-01 stsp error = got_object_id_str(&commit_id_str, commit_id);
4573 234035bc 2019-06-01 stsp if (error)
4574 234035bc 2019-06-01 stsp goto done;
4576 234035bc 2019-06-01 stsp error = got_ref_open(&head_ref, repo,
4577 234035bc 2019-06-01 stsp got_worktree_get_head_ref_name(worktree), 0);
4578 234035bc 2019-06-01 stsp if (error != NULL)
4579 234035bc 2019-06-01 stsp goto done;
4581 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
4582 234035bc 2019-06-01 stsp if (error) {
4583 234035bc 2019-06-01 stsp if (error->code != GOT_ERR_ANCESTRY)
4584 234035bc 2019-06-01 stsp goto done;
4585 234035bc 2019-06-01 stsp error = NULL;
4587 234035bc 2019-06-01 stsp error = got_error(GOT_ERR_SAME_BRANCH);
4588 234035bc 2019-06-01 stsp goto done;
4591 234035bc 2019-06-01 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
4592 234035bc 2019-06-01 stsp if (error)
4593 234035bc 2019-06-01 stsp goto done;
4594 234035bc 2019-06-01 stsp pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
4595 03415a1a 2019-06-02 stsp error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
4596 03415a1a 2019-06-02 stsp commit_id, repo, update_progress, &did_something, check_cancelled,
4598 234035bc 2019-06-01 stsp if (error != NULL)
4599 234035bc 2019-06-01 stsp goto done;
4601 234035bc 2019-06-01 stsp if (did_something)
4602 a7648d7a 2019-06-02 stsp printf("Merged commit %s\n", commit_id_str);
4604 234035bc 2019-06-01 stsp if (commit)
4605 234035bc 2019-06-01 stsp got_object_commit_close(commit);
4606 234035bc 2019-06-01 stsp free(commit_id_str);
4607 234035bc 2019-06-01 stsp if (head_ref)
4608 234035bc 2019-06-01 stsp got_ref_close(head_ref);
4609 234035bc 2019-06-01 stsp if (worktree)
4610 234035bc 2019-06-01 stsp got_worktree_close(worktree);
4612 234035bc 2019-06-01 stsp got_repo_close(repo);
4613 c4296144 2019-05-09 stsp return error;
4616 5ef14e63 2019-06-02 stsp __dead static void
4617 5ef14e63 2019-06-02 stsp usage_backout(void)
4619 5ef14e63 2019-06-02 stsp fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
4623 5ef14e63 2019-06-02 stsp static const struct got_error *
4624 5ef14e63 2019-06-02 stsp cmd_backout(int argc, char *argv[])
4626 5ef14e63 2019-06-02 stsp const struct got_error *error = NULL;
4627 5ef14e63 2019-06-02 stsp struct got_worktree *worktree = NULL;
4628 5ef14e63 2019-06-02 stsp struct got_repository *repo = NULL;
4629 5ef14e63 2019-06-02 stsp char *cwd = NULL, *commit_id_str = NULL;
4630 5ef14e63 2019-06-02 stsp struct got_object_id *commit_id = NULL;
4631 5ef14e63 2019-06-02 stsp struct got_commit_object *commit = NULL;
4632 5ef14e63 2019-06-02 stsp struct got_object_qid *pid;
4633 5ef14e63 2019-06-02 stsp struct got_reference *head_ref = NULL;
4634 5ef14e63 2019-06-02 stsp int ch, did_something = 0;
4636 5ef14e63 2019-06-02 stsp while ((ch = getopt(argc, argv, "")) != -1) {
4637 5ef14e63 2019-06-02 stsp switch (ch) {
4639 5ef14e63 2019-06-02 stsp usage_backout();
4640 5ef14e63 2019-06-02 stsp /* NOTREACHED */
4644 5ef14e63 2019-06-02 stsp argc -= optind;
4645 5ef14e63 2019-06-02 stsp argv += optind;
4647 43012d58 2019-07-14 stsp #ifndef PROFILE
4648 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4649 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
4650 43012d58 2019-07-14 stsp err(1, "pledge");
4652 5ef14e63 2019-06-02 stsp if (argc != 1)
4653 5ef14e63 2019-06-02 stsp usage_backout();
4655 5ef14e63 2019-06-02 stsp cwd = getcwd(NULL, 0);
4656 5ef14e63 2019-06-02 stsp if (cwd == NULL) {
4657 5ef14e63 2019-06-02 stsp error = got_error_from_errno("getcwd");
4658 5ef14e63 2019-06-02 stsp goto done;
4660 5ef14e63 2019-06-02 stsp error = got_worktree_open(&worktree, cwd);
4661 5ef14e63 2019-06-02 stsp if (error)
4662 5ef14e63 2019-06-02 stsp goto done;
4664 5ef14e63 2019-06-02 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
4665 5ef14e63 2019-06-02 stsp if (error != NULL)
4666 5ef14e63 2019-06-02 stsp goto done;
4668 5ef14e63 2019-06-02 stsp error = apply_unveil(got_repo_get_path(repo), 0,
4669 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
4670 5ef14e63 2019-06-02 stsp if (error)
4671 5ef14e63 2019-06-02 stsp goto done;
4673 dd88155e 2019-06-29 stsp error = got_repo_match_object_id_prefix(&commit_id, argv[0],
4674 dd88155e 2019-06-29 stsp GOT_OBJ_TYPE_COMMIT, repo);
4675 5ef14e63 2019-06-02 stsp if (error != NULL) {
4676 5ef14e63 2019-06-02 stsp struct got_reference *ref;
4677 5ef14e63 2019-06-02 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
4678 5ef14e63 2019-06-02 stsp goto done;
4679 5ef14e63 2019-06-02 stsp error = got_ref_open(&ref, repo, argv[0], 0);
4680 5ef14e63 2019-06-02 stsp if (error != NULL)
4681 5ef14e63 2019-06-02 stsp goto done;
4682 5ef14e63 2019-06-02 stsp error = got_ref_resolve(&commit_id, repo, ref);
4683 5ef14e63 2019-06-02 stsp got_ref_close(ref);
4684 5ef14e63 2019-06-02 stsp if (error != NULL)
4685 5ef14e63 2019-06-02 stsp goto done;
4687 5ef14e63 2019-06-02 stsp error = got_object_id_str(&commit_id_str, commit_id);
4688 5ef14e63 2019-06-02 stsp if (error)
4689 5ef14e63 2019-06-02 stsp goto done;
4691 5ef14e63 2019-06-02 stsp error = got_ref_open(&head_ref, repo,
4692 5ef14e63 2019-06-02 stsp got_worktree_get_head_ref_name(worktree), 0);
4693 5ef14e63 2019-06-02 stsp if (error != NULL)
4694 5ef14e63 2019-06-02 stsp goto done;
4696 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
4697 5ef14e63 2019-06-02 stsp if (error)
4698 5ef14e63 2019-06-02 stsp goto done;
4700 5ef14e63 2019-06-02 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
4701 5ef14e63 2019-06-02 stsp if (error)
4702 5ef14e63 2019-06-02 stsp goto done;
4703 5ef14e63 2019-06-02 stsp pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
4704 5ef14e63 2019-06-02 stsp if (pid == NULL) {
4705 5ef14e63 2019-06-02 stsp error = got_error(GOT_ERR_ROOT_COMMIT);
4706 5ef14e63 2019-06-02 stsp goto done;
4709 5ef14e63 2019-06-02 stsp error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
4710 5ef14e63 2019-06-02 stsp update_progress, &did_something, check_cancelled, NULL);
4711 5ef14e63 2019-06-02 stsp if (error != NULL)
4712 5ef14e63 2019-06-02 stsp goto done;
4714 5ef14e63 2019-06-02 stsp if (did_something)
4715 a7648d7a 2019-06-02 stsp printf("Backed out commit %s\n", commit_id_str);
4717 5ef14e63 2019-06-02 stsp if (commit)
4718 5ef14e63 2019-06-02 stsp got_object_commit_close(commit);
4719 5ef14e63 2019-06-02 stsp free(commit_id_str);
4720 5ef14e63 2019-06-02 stsp if (head_ref)
4721 5ef14e63 2019-06-02 stsp got_ref_close(head_ref);
4722 818c7501 2019-07-11 stsp if (worktree)
4723 818c7501 2019-07-11 stsp got_worktree_close(worktree);
4725 818c7501 2019-07-11 stsp got_repo_close(repo);
4726 818c7501 2019-07-11 stsp return error;
4729 818c7501 2019-07-11 stsp __dead static void
4730 818c7501 2019-07-11 stsp usage_rebase(void)
4732 af54c8f8 2019-07-11 stsp fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
4733 af54c8f8 2019-07-11 stsp getprogname());
4738 0ebf8283 2019-07-24 stsp trim_logmsg(char *logmsg, int limit)
4741 0ebf8283 2019-07-24 stsp size_t len;
4743 0ebf8283 2019-07-24 stsp len = strlen(logmsg);
4744 0ebf8283 2019-07-24 stsp if (len > limit)
4745 0ebf8283 2019-07-24 stsp len = limit;
4746 0ebf8283 2019-07-24 stsp logmsg[len] = '\0';
4747 0ebf8283 2019-07-24 stsp nl = strchr(logmsg, '\n');
4749 0ebf8283 2019-07-24 stsp *nl = '\0';
4752 0ebf8283 2019-07-24 stsp static const struct got_error *
4753 0ebf8283 2019-07-24 stsp get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
4755 5943eee2 2019-08-13 stsp const struct got_error *err;
4756 5943eee2 2019-08-13 stsp char *logmsg0 = NULL;
4757 5943eee2 2019-08-13 stsp const char *s;
4759 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
4761 5943eee2 2019-08-13 stsp return err;
4763 5943eee2 2019-08-13 stsp s = logmsg0;
4764 5943eee2 2019-08-13 stsp while (isspace((unsigned char)s[0]))
4767 5943eee2 2019-08-13 stsp *logmsg = strdup(s);
4768 5943eee2 2019-08-13 stsp if (*logmsg == NULL) {
4769 5943eee2 2019-08-13 stsp err = got_error_from_errno("strdup");
4770 5943eee2 2019-08-13 stsp goto done;
4773 0ebf8283 2019-07-24 stsp trim_logmsg(*logmsg, limit);
4775 5943eee2 2019-08-13 stsp free(logmsg0);
4776 5943eee2 2019-08-13 stsp return err;
4779 818c7501 2019-07-11 stsp static const struct got_error *
4780 818c7501 2019-07-11 stsp show_rebase_progress(struct got_commit_object *commit,
4781 818c7501 2019-07-11 stsp struct got_object_id *old_id, struct got_object_id *new_id)
4783 818c7501 2019-07-11 stsp const struct got_error *err;
4784 0ebf8283 2019-07-24 stsp char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
4786 818c7501 2019-07-11 stsp err = got_object_id_str(&old_id_str, old_id);
4788 818c7501 2019-07-11 stsp goto done;
4790 ff0d2220 2019-07-11 stsp if (new_id) {
4791 ff0d2220 2019-07-11 stsp err = got_object_id_str(&new_id_str, new_id);
4793 ff0d2220 2019-07-11 stsp goto done;
4796 818c7501 2019-07-11 stsp old_id_str[12] = '\0';
4797 ff0d2220 2019-07-11 stsp if (new_id_str)
4798 ff0d2220 2019-07-11 stsp new_id_str[12] = '\0';
4800 0ebf8283 2019-07-24 stsp err = get_short_logmsg(&logmsg, 42, commit);
4802 0ebf8283 2019-07-24 stsp goto done;
4804 ff0d2220 2019-07-11 stsp printf("%s -> %s: %s\n", old_id_str,
4805 ff0d2220 2019-07-11 stsp new_id_str ? new_id_str : "no-op change", logmsg);
4807 818c7501 2019-07-11 stsp free(old_id_str);
4808 818c7501 2019-07-11 stsp free(new_id_str);
4809 818c7501 2019-07-11 stsp return err;
4812 1ee397ad 2019-07-12 stsp static const struct got_error *
4813 818c7501 2019-07-11 stsp rebase_progress(void *arg, unsigned char status, const char *path)
4815 818c7501 2019-07-11 stsp unsigned char *rebase_status = arg;
4817 818c7501 2019-07-11 stsp while (path[0] == '/')
4819 818c7501 2019-07-11 stsp printf("%c %s\n", status, path);
4821 818c7501 2019-07-11 stsp if (*rebase_status == GOT_STATUS_CONFLICT)
4822 1ee397ad 2019-07-12 stsp return NULL;
4823 818c7501 2019-07-11 stsp if (status == GOT_STATUS_CONFLICT || status == GOT_STATUS_MERGE)
4824 818c7501 2019-07-11 stsp *rebase_status = status;
4825 1ee397ad 2019-07-12 stsp return NULL;
4828 818c7501 2019-07-11 stsp static const struct got_error *
4829 3e3a69f1 2019-07-25 stsp rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
4830 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_reference *new_base_branch,
4831 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_repository *repo)
4833 818c7501 2019-07-11 stsp printf("Switching work tree to %s\n", got_ref_get_name(branch));
4834 3e3a69f1 2019-07-25 stsp return got_worktree_rebase_complete(worktree, fileindex,
4835 818c7501 2019-07-11 stsp new_base_branch, tmp_branch, branch, repo);
4838 818c7501 2019-07-11 stsp static const struct got_error *
4839 01757395 2019-07-12 stsp rebase_commit(struct got_pathlist_head *merged_paths,
4840 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
4841 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch,
4842 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
4844 ff0d2220 2019-07-11 stsp const struct got_error *error;
4845 ff0d2220 2019-07-11 stsp struct got_commit_object *commit;
4846 ff0d2220 2019-07-11 stsp struct got_object_id *new_commit_id;
4848 ff0d2220 2019-07-11 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
4849 ff0d2220 2019-07-11 stsp if (error)
4850 ff0d2220 2019-07-11 stsp return error;
4852 01757395 2019-07-12 stsp error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
4853 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, commit, commit_id, repo);
4854 ff0d2220 2019-07-11 stsp if (error) {
4855 ff0d2220 2019-07-11 stsp if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
4856 ff0d2220 2019-07-11 stsp goto done;
4857 ff0d2220 2019-07-11 stsp error = show_rebase_progress(commit, commit_id, NULL);
4859 ff0d2220 2019-07-11 stsp error = show_rebase_progress(commit, commit_id, new_commit_id);
4860 ff0d2220 2019-07-11 stsp free(new_commit_id);
4863 ff0d2220 2019-07-11 stsp got_object_commit_close(commit);
4864 ff0d2220 2019-07-11 stsp return error;
4867 64c6d990 2019-07-11 stsp struct check_path_prefix_arg {
4868 64c6d990 2019-07-11 stsp const char *path_prefix;
4869 64c6d990 2019-07-11 stsp size_t len;
4870 8ca9bd68 2019-07-25 stsp int errcode;
4873 64c6d990 2019-07-11 stsp static const struct got_error *
4874 8ca9bd68 2019-07-25 stsp check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
4875 64c6d990 2019-07-11 stsp struct got_blob_object *blob2, struct got_object_id *id1,
4876 64c6d990 2019-07-11 stsp struct got_object_id *id2, const char *path1, const char *path2,
4877 64c6d990 2019-07-11 stsp struct got_repository *repo)
4879 64c6d990 2019-07-11 stsp struct check_path_prefix_arg *a = arg;
4881 64c6d990 2019-07-11 stsp if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
4882 64c6d990 2019-07-11 stsp (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
4883 8ca9bd68 2019-07-25 stsp return got_error(a->errcode);
4885 64c6d990 2019-07-11 stsp return NULL;
4888 64c6d990 2019-07-11 stsp static const struct got_error *
4889 8ca9bd68 2019-07-25 stsp check_path_prefix(struct got_object_id *parent_id,
4890 64c6d990 2019-07-11 stsp struct got_object_id *commit_id, const char *path_prefix,
4891 8ca9bd68 2019-07-25 stsp int errcode, struct got_repository *repo)
4893 64c6d990 2019-07-11 stsp const struct got_error *err;
4894 64c6d990 2019-07-11 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
4895 64c6d990 2019-07-11 stsp struct got_commit_object *commit = NULL, *parent_commit = NULL;
4896 64c6d990 2019-07-11 stsp struct check_path_prefix_arg cpp_arg;
4898 64c6d990 2019-07-11 stsp if (got_path_is_root_dir(path_prefix))
4899 64c6d990 2019-07-11 stsp return NULL;
4901 64c6d990 2019-07-11 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
4903 64c6d990 2019-07-11 stsp goto done;
4905 64c6d990 2019-07-11 stsp err = got_object_open_as_commit(&parent_commit, repo, parent_id);
4907 64c6d990 2019-07-11 stsp goto done;
4909 64c6d990 2019-07-11 stsp err = got_object_open_as_tree(&tree1, repo,
4910 64c6d990 2019-07-11 stsp got_object_commit_get_tree_id(parent_commit));
4912 64c6d990 2019-07-11 stsp goto done;
4914 64c6d990 2019-07-11 stsp err = got_object_open_as_tree(&tree2, repo,
4915 64c6d990 2019-07-11 stsp got_object_commit_get_tree_id(commit));
4917 64c6d990 2019-07-11 stsp goto done;
4919 64c6d990 2019-07-11 stsp cpp_arg.path_prefix = path_prefix;
4920 d23ace97 2019-07-25 stsp while (cpp_arg.path_prefix[0] == '/')
4921 d23ace97 2019-07-25 stsp cpp_arg.path_prefix++;
4922 d23ace97 2019-07-25 stsp cpp_arg.len = strlen(cpp_arg.path_prefix);
4923 8ca9bd68 2019-07-25 stsp cpp_arg.errcode = errcode;
4924 8ca9bd68 2019-07-25 stsp err = got_diff_tree(tree1, tree2, "", "", repo,
4925 31b4484f 2019-07-27 stsp check_path_prefix_in_diff, &cpp_arg, 0);
4927 64c6d990 2019-07-11 stsp if (tree1)
4928 64c6d990 2019-07-11 stsp got_object_tree_close(tree1);
4929 64c6d990 2019-07-11 stsp if (tree2)
4930 64c6d990 2019-07-11 stsp got_object_tree_close(tree2);
4931 64c6d990 2019-07-11 stsp if (commit)
4932 64c6d990 2019-07-11 stsp got_object_commit_close(commit);
4933 64c6d990 2019-07-11 stsp if (parent_commit)
4934 64c6d990 2019-07-11 stsp got_object_commit_close(parent_commit);
4935 64c6d990 2019-07-11 stsp return err;
4938 ff0d2220 2019-07-11 stsp static const struct got_error *
4939 8ca9bd68 2019-07-25 stsp collect_commits(struct got_object_id_queue *commits,
4940 af61c510 2019-07-19 stsp struct got_object_id *initial_commit_id,
4941 af61c510 2019-07-19 stsp struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
4942 8ca9bd68 2019-07-25 stsp const char *path_prefix, int path_prefix_errcode,
4943 8ca9bd68 2019-07-25 stsp struct got_repository *repo)
4945 af61c510 2019-07-19 stsp const struct got_error *err = NULL;
4946 af61c510 2019-07-19 stsp struct got_commit_graph *graph = NULL;
4947 af61c510 2019-07-19 stsp struct got_object_id *parent_id = NULL;
4948 af61c510 2019-07-19 stsp struct got_object_qid *qid;
4949 af61c510 2019-07-19 stsp struct got_object_id *commit_id = initial_commit_id;
4951 af61c510 2019-07-19 stsp err = got_commit_graph_open(&graph, initial_commit_id, "/", 1, repo);
4953 af61c510 2019-07-19 stsp return err;
4955 6fb7cd11 2019-08-22 stsp err = got_commit_graph_iter_start(graph, iter_start_id, repo,
4956 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
4958 af61c510 2019-07-19 stsp goto done;
4959 af61c510 2019-07-19 stsp while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
4960 af61c510 2019-07-19 stsp err = got_commit_graph_iter_next(&parent_id, graph);
4961 af61c510 2019-07-19 stsp if (err) {
4962 af61c510 2019-07-19 stsp if (err->code == GOT_ERR_ITER_COMPLETED) {
4963 af61c510 2019-07-19 stsp err = got_error_msg(GOT_ERR_ANCESTRY,
4964 af61c510 2019-07-19 stsp "ran out of commits to rebase before "
4965 af61c510 2019-07-19 stsp "youngest common ancestor commit has "
4966 af61c510 2019-07-19 stsp "been reached?!?");
4967 af61c510 2019-07-19 stsp goto done;
4968 af61c510 2019-07-19 stsp } else if (err->code != GOT_ERR_ITER_NEED_MORE)
4969 af61c510 2019-07-19 stsp goto done;
4970 6fb7cd11 2019-08-22 stsp err = got_commit_graph_fetch_commits(graph, 1, repo,
4971 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
4973 af61c510 2019-07-19 stsp goto done;
4975 8ca9bd68 2019-07-25 stsp err = check_path_prefix(parent_id, commit_id,
4976 8ca9bd68 2019-07-25 stsp path_prefix, path_prefix_errcode, repo);
4978 af61c510 2019-07-19 stsp goto done;
4980 af61c510 2019-07-19 stsp err = got_object_qid_alloc(&qid, commit_id);
4982 af61c510 2019-07-19 stsp goto done;
4983 af61c510 2019-07-19 stsp SIMPLEQ_INSERT_HEAD(commits, qid, entry);
4984 af61c510 2019-07-19 stsp commit_id = parent_id;
4988 af61c510 2019-07-19 stsp got_commit_graph_close(graph);
4989 af61c510 2019-07-19 stsp return err;
4992 af61c510 2019-07-19 stsp static const struct got_error *
4993 818c7501 2019-07-11 stsp cmd_rebase(int argc, char *argv[])
4995 818c7501 2019-07-11 stsp const struct got_error *error = NULL;
4996 818c7501 2019-07-11 stsp struct got_worktree *worktree = NULL;
4997 818c7501 2019-07-11 stsp struct got_repository *repo = NULL;
4998 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex = NULL;
4999 818c7501 2019-07-11 stsp char *cwd = NULL;
5000 818c7501 2019-07-11 stsp struct got_reference *branch = NULL;
5001 818c7501 2019-07-11 stsp struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
5002 818c7501 2019-07-11 stsp struct got_object_id *commit_id = NULL, *parent_id = NULL;
5003 818c7501 2019-07-11 stsp struct got_object_id *resume_commit_id = NULL;
5004 818c7501 2019-07-11 stsp struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
5005 818c7501 2019-07-11 stsp struct got_commit_object *commit = NULL;
5006 818c7501 2019-07-11 stsp int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
5007 818c7501 2019-07-11 stsp unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
5008 818c7501 2019-07-11 stsp struct got_object_id_queue commits;
5009 01757395 2019-07-12 stsp struct got_pathlist_head merged_paths;
5010 818c7501 2019-07-11 stsp const struct got_object_id_queue *parent_ids;
5011 818c7501 2019-07-11 stsp struct got_object_qid *qid, *pid;
5013 818c7501 2019-07-11 stsp SIMPLEQ_INIT(&commits);
5014 01757395 2019-07-12 stsp TAILQ_INIT(&merged_paths);
5016 818c7501 2019-07-11 stsp while ((ch = getopt(argc, argv, "ac")) != -1) {
5017 818c7501 2019-07-11 stsp switch (ch) {
5019 818c7501 2019-07-11 stsp abort_rebase = 1;
5022 818c7501 2019-07-11 stsp continue_rebase = 1;
5025 818c7501 2019-07-11 stsp usage_rebase();
5026 818c7501 2019-07-11 stsp /* NOTREACHED */
5030 818c7501 2019-07-11 stsp argc -= optind;
5031 818c7501 2019-07-11 stsp argv += optind;
5033 43012d58 2019-07-14 stsp #ifndef PROFILE
5034 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5035 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
5036 43012d58 2019-07-14 stsp err(1, "pledge");
5038 818c7501 2019-07-11 stsp if (abort_rebase && continue_rebase)
5039 818c7501 2019-07-11 stsp usage_rebase();
5040 818c7501 2019-07-11 stsp else if (abort_rebase || continue_rebase) {
5041 818c7501 2019-07-11 stsp if (argc != 0)
5042 818c7501 2019-07-11 stsp usage_rebase();
5043 818c7501 2019-07-11 stsp } else if (argc != 1)
5044 818c7501 2019-07-11 stsp usage_rebase();
5046 818c7501 2019-07-11 stsp cwd = getcwd(NULL, 0);
5047 818c7501 2019-07-11 stsp if (cwd == NULL) {
5048 818c7501 2019-07-11 stsp error = got_error_from_errno("getcwd");
5049 818c7501 2019-07-11 stsp goto done;
5051 818c7501 2019-07-11 stsp error = got_worktree_open(&worktree, cwd);
5052 818c7501 2019-07-11 stsp if (error)
5053 818c7501 2019-07-11 stsp goto done;
5055 818c7501 2019-07-11 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
5056 818c7501 2019-07-11 stsp if (error != NULL)
5057 818c7501 2019-07-11 stsp goto done;
5059 818c7501 2019-07-11 stsp error = apply_unveil(got_repo_get_path(repo), 0,
5060 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
5061 818c7501 2019-07-11 stsp if (error)
5062 818c7501 2019-07-11 stsp goto done;
5064 818c7501 2019-07-11 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
5065 818c7501 2019-07-11 stsp if (error)
5066 818c7501 2019-07-11 stsp goto done;
5068 f6794adc 2019-07-23 stsp if (abort_rebase) {
5069 818c7501 2019-07-11 stsp int did_something;
5070 f6794adc 2019-07-23 stsp if (!rebase_in_progress) {
5071 f6794adc 2019-07-23 stsp error = got_error(GOT_ERR_NOT_REBASING);
5072 f6794adc 2019-07-23 stsp goto done;
5074 818c7501 2019-07-11 stsp error = got_worktree_rebase_continue(&resume_commit_id,
5075 3e3a69f1 2019-07-25 stsp &new_base_branch, &tmp_branch, &branch, &fileindex,
5076 3e3a69f1 2019-07-25 stsp worktree, repo);
5077 818c7501 2019-07-11 stsp if (error)
5078 818c7501 2019-07-11 stsp goto done;
5079 818c7501 2019-07-11 stsp printf("Switching work tree to %s\n",
5080 818c7501 2019-07-11 stsp got_ref_get_symref_target(new_base_branch));
5081 3e3a69f1 2019-07-25 stsp error = got_worktree_rebase_abort(worktree, fileindex, repo,
5082 818c7501 2019-07-11 stsp new_base_branch, update_progress, &did_something);
5083 818c7501 2019-07-11 stsp if (error)
5084 818c7501 2019-07-11 stsp goto done;
5085 818c7501 2019-07-11 stsp printf("Rebase of %s aborted\n", got_ref_get_name(branch));
5086 818c7501 2019-07-11 stsp goto done; /* nothing else to do */
5089 818c7501 2019-07-11 stsp if (continue_rebase) {
5090 f6794adc 2019-07-23 stsp if (!rebase_in_progress) {
5091 f6794adc 2019-07-23 stsp error = got_error(GOT_ERR_NOT_REBASING);
5092 f6794adc 2019-07-23 stsp goto done;
5094 818c7501 2019-07-11 stsp error = got_worktree_rebase_continue(&resume_commit_id,
5095 3e3a69f1 2019-07-25 stsp &new_base_branch, &tmp_branch, &branch, &fileindex,
5096 3e3a69f1 2019-07-25 stsp worktree, repo);
5097 818c7501 2019-07-11 stsp if (error)
5098 818c7501 2019-07-11 stsp goto done;
5100 3e3a69f1 2019-07-25 stsp error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
5101 01757395 2019-07-12 stsp resume_commit_id, repo);
5102 818c7501 2019-07-11 stsp if (error)
5103 818c7501 2019-07-11 stsp goto done;
5105 ff0d2220 2019-07-11 stsp yca_id = got_object_id_dup(resume_commit_id);
5106 ff0d2220 2019-07-11 stsp if (yca_id == NULL) {
5107 818c7501 2019-07-11 stsp error = got_error_from_errno("got_object_id_dup");
5108 818c7501 2019-07-11 stsp goto done;
5111 818c7501 2019-07-11 stsp error = got_ref_open(&branch, repo, argv[0], 0);
5112 818c7501 2019-07-11 stsp if (error != NULL)
5113 818c7501 2019-07-11 stsp goto done;
5116 ff0d2220 2019-07-11 stsp error = got_ref_resolve(&branch_head_commit_id, repo, branch);
5117 ff0d2220 2019-07-11 stsp if (error)
5118 ff0d2220 2019-07-11 stsp goto done;
5120 ff0d2220 2019-07-11 stsp if (!continue_rebase) {
5121 a51a74b3 2019-07-27 stsp struct got_object_id *base_commit_id;
5123 a51a74b3 2019-07-27 stsp base_commit_id = got_worktree_get_base_commit_id(worktree);
5124 818c7501 2019-07-11 stsp error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
5125 6fb7cd11 2019-08-22 stsp base_commit_id, branch_head_commit_id, repo,
5126 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
5127 818c7501 2019-07-11 stsp if (error)
5128 818c7501 2019-07-11 stsp goto done;
5129 818c7501 2019-07-11 stsp if (yca_id == NULL) {
5130 818c7501 2019-07-11 stsp error = got_error_msg(GOT_ERR_ANCESTRY,
5131 818c7501 2019-07-11 stsp "specified branch shares no common ancestry "
5132 818c7501 2019-07-11 stsp "with work tree's branch");
5133 818c7501 2019-07-11 stsp goto done;
5136 a51a74b3 2019-07-27 stsp error = check_same_branch(base_commit_id, branch, yca_id, repo);
5137 a51a74b3 2019-07-27 stsp if (error) {
5138 a51a74b3 2019-07-27 stsp if (error->code != GOT_ERR_ANCESTRY)
5139 a51a74b3 2019-07-27 stsp goto done;
5140 a51a74b3 2019-07-27 stsp error = NULL;
5142 a51a74b3 2019-07-27 stsp error = got_error_msg(GOT_ERR_SAME_BRANCH,
5143 a51a74b3 2019-07-27 stsp "specified branch resolves to a commit which "
5144 a51a74b3 2019-07-27 stsp "is already contained in work tree's branch");
5145 a51a74b3 2019-07-27 stsp goto done;
5147 818c7501 2019-07-11 stsp error = got_worktree_rebase_prepare(&new_base_branch,
5148 3e3a69f1 2019-07-25 stsp &tmp_branch, &fileindex, worktree, branch, repo);
5149 818c7501 2019-07-11 stsp if (error)
5150 818c7501 2019-07-11 stsp goto done;
5153 ff0d2220 2019-07-11 stsp commit_id = branch_head_commit_id;
5154 818c7501 2019-07-11 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
5155 818c7501 2019-07-11 stsp if (error)
5156 818c7501 2019-07-11 stsp goto done;
5158 818c7501 2019-07-11 stsp parent_ids = got_object_commit_get_parent_ids(commit);
5159 818c7501 2019-07-11 stsp pid = SIMPLEQ_FIRST(parent_ids);
5160 fc66b545 2019-08-12 stsp if (pid == NULL) {
5161 fc66b545 2019-08-12 stsp if (!continue_rebase) {
5162 fc66b545 2019-08-12 stsp int did_something;
5163 fc66b545 2019-08-12 stsp error = got_worktree_rebase_abort(worktree, fileindex,
5164 fc66b545 2019-08-12 stsp repo, new_base_branch, update_progress,
5165 fc66b545 2019-08-12 stsp &did_something);
5166 fc66b545 2019-08-12 stsp if (error)
5167 fc66b545 2019-08-12 stsp goto done;
5168 fc66b545 2019-08-12 stsp printf("Rebase of %s aborted\n",
5169 fc66b545 2019-08-12 stsp got_ref_get_name(branch));
5171 fc66b545 2019-08-12 stsp error = got_error(GOT_ERR_EMPTY_REBASE);
5172 fc66b545 2019-08-12 stsp goto done;
5174 8ca9bd68 2019-07-25 stsp error = collect_commits(&commits, commit_id, pid->id,
5175 8ca9bd68 2019-07-25 stsp yca_id, got_worktree_get_path_prefix(worktree),
5176 8ca9bd68 2019-07-25 stsp GOT_ERR_REBASE_PATH, repo);
5177 818c7501 2019-07-11 stsp got_object_commit_close(commit);
5178 818c7501 2019-07-11 stsp commit = NULL;
5179 818c7501 2019-07-11 stsp if (error)
5180 818c7501 2019-07-11 stsp goto done;
5182 818c7501 2019-07-11 stsp if (SIMPLEQ_EMPTY(&commits)) {
5183 ff0d2220 2019-07-11 stsp if (continue_rebase)
5184 3e3a69f1 2019-07-25 stsp error = rebase_complete(worktree, fileindex,
5185 3e3a69f1 2019-07-25 stsp branch, new_base_branch, tmp_branch, repo);
5187 ff0d2220 2019-07-11 stsp error = got_error(GOT_ERR_EMPTY_REBASE);
5188 818c7501 2019-07-11 stsp goto done;
5191 818c7501 2019-07-11 stsp pid = NULL;
5192 818c7501 2019-07-11 stsp SIMPLEQ_FOREACH(qid, &commits, entry) {
5193 818c7501 2019-07-11 stsp commit_id = qid->id;
5194 818c7501 2019-07-11 stsp parent_id = pid ? pid->id : yca_id;
5195 818c7501 2019-07-11 stsp pid = qid;
5197 01757395 2019-07-12 stsp error = got_worktree_rebase_merge_files(&merged_paths,
5198 3e3a69f1 2019-07-25 stsp worktree, fileindex, parent_id, commit_id, repo,
5199 3e3a69f1 2019-07-25 stsp rebase_progress, &rebase_status, check_cancelled, NULL);
5200 818c7501 2019-07-11 stsp if (error)
5201 818c7501 2019-07-11 stsp goto done;
5203 01757395 2019-07-12 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
5204 01757395 2019-07-12 stsp got_worktree_rebase_pathlist_free(&merged_paths);
5208 3e3a69f1 2019-07-25 stsp error = rebase_commit(&merged_paths, worktree, fileindex,
5209 3e3a69f1 2019-07-25 stsp tmp_branch, commit_id, repo);
5210 01757395 2019-07-12 stsp got_worktree_rebase_pathlist_free(&merged_paths);
5211 818c7501 2019-07-11 stsp if (error)
5212 818c7501 2019-07-11 stsp goto done;
5215 818c7501 2019-07-11 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
5216 3e3a69f1 2019-07-25 stsp error = got_worktree_rebase_postpone(worktree, fileindex);
5217 818c7501 2019-07-11 stsp if (error)
5218 818c7501 2019-07-11 stsp goto done;
5219 818c7501 2019-07-11 stsp error = got_error_msg(GOT_ERR_CONFLICTS,
5220 11495e04 2019-07-12 stsp "conflicts must be resolved before rebasing can continue");
5222 3e3a69f1 2019-07-25 stsp error = rebase_complete(worktree, fileindex, branch,
5223 3e3a69f1 2019-07-25 stsp new_base_branch, tmp_branch, repo);
5225 818c7501 2019-07-11 stsp got_object_id_queue_free(&commits);
5226 818c7501 2019-07-11 stsp free(branch_head_commit_id);
5227 818c7501 2019-07-11 stsp free(resume_commit_id);
5228 818c7501 2019-07-11 stsp free(yca_id);
5229 818c7501 2019-07-11 stsp if (commit)
5230 818c7501 2019-07-11 stsp got_object_commit_close(commit);
5231 818c7501 2019-07-11 stsp if (branch)
5232 818c7501 2019-07-11 stsp got_ref_close(branch);
5233 818c7501 2019-07-11 stsp if (new_base_branch)
5234 818c7501 2019-07-11 stsp got_ref_close(new_base_branch);
5235 818c7501 2019-07-11 stsp if (tmp_branch)
5236 818c7501 2019-07-11 stsp got_ref_close(tmp_branch);
5237 5ef14e63 2019-06-02 stsp if (worktree)
5238 5ef14e63 2019-06-02 stsp got_worktree_close(worktree);
5240 5ef14e63 2019-06-02 stsp got_repo_close(repo);
5241 5ef14e63 2019-06-02 stsp return error;
5244 0ebf8283 2019-07-24 stsp __dead static void
5245 0ebf8283 2019-07-24 stsp usage_histedit(void)
5247 f3055044 2019-08-07 stsp fprintf(stderr, "usage: %s histedit [-a] [-c] [-F histedit-script]\n",
5248 0ebf8283 2019-07-24 stsp getprogname());
5252 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_PICK 'p'
5253 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_EDIT 'e'
5254 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_FOLD 'f'
5255 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_DROP 'd'
5256 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_MESG 'm'
5258 0ebf8283 2019-07-24 stsp static struct got_histedit_cmd {
5259 0ebf8283 2019-07-24 stsp unsigned char code;
5260 0ebf8283 2019-07-24 stsp const char *name;
5261 0ebf8283 2019-07-24 stsp const char *desc;
5262 0ebf8283 2019-07-24 stsp } got_histedit_cmds[] = {
5263 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_PICK, "pick", "use commit" },
5264 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
5265 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_FOLD, "fold", "combine with commit below" },
5266 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
5267 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_MESG, "mesg",
5268 0ebf8283 2019-07-24 stsp "single-line log message for commit above (open editor if empty)" },
5271 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry {
5272 0ebf8283 2019-07-24 stsp TAILQ_ENTRY(got_histedit_list_entry) entry;
5273 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id;
5274 0ebf8283 2019-07-24 stsp const struct got_histedit_cmd *cmd;
5275 0ebf8283 2019-07-24 stsp char *logmsg;
5277 0ebf8283 2019-07-24 stsp TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
5279 0ebf8283 2019-07-24 stsp static const struct got_error *
5280 0ebf8283 2019-07-24 stsp histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
5281 0ebf8283 2019-07-24 stsp FILE *f, struct got_repository *repo)
5283 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
5284 0ebf8283 2019-07-24 stsp char *logmsg = NULL, *id_str = NULL;
5285 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
5288 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
5290 0ebf8283 2019-07-24 stsp goto done;
5292 0ebf8283 2019-07-24 stsp err = get_short_logmsg(&logmsg, 34, commit);
5294 0ebf8283 2019-07-24 stsp goto done;
5296 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, commit_id);
5298 0ebf8283 2019-07-24 stsp goto done;
5300 0ebf8283 2019-07-24 stsp n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
5301 0ebf8283 2019-07-24 stsp if (n < 0)
5302 0ebf8283 2019-07-24 stsp err = got_ferror(f, GOT_ERR_IO);
5304 0ebf8283 2019-07-24 stsp if (commit)
5305 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
5306 0ebf8283 2019-07-24 stsp free(id_str);
5307 0ebf8283 2019-07-24 stsp free(logmsg);
5308 0ebf8283 2019-07-24 stsp return err;
5311 0ebf8283 2019-07-24 stsp static const struct got_error *
5312 0ebf8283 2019-07-24 stsp histedit_write_commit_list(struct got_object_id_queue *commits, FILE *f,
5313 0ebf8283 2019-07-24 stsp struct got_repository *repo)
5315 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
5316 0ebf8283 2019-07-24 stsp struct got_object_qid *qid;
5318 0ebf8283 2019-07-24 stsp if (SIMPLEQ_EMPTY(commits))
5319 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_EMPTY_HISTEDIT);
5321 0ebf8283 2019-07-24 stsp SIMPLEQ_FOREACH(qid, commits, entry) {
5322 0ebf8283 2019-07-24 stsp err = histedit_write_commit(qid->id, got_histedit_cmds[0].name,
5328 0ebf8283 2019-07-24 stsp return err;
5331 0ebf8283 2019-07-24 stsp static const struct got_error *
5332 0ebf8283 2019-07-24 stsp write_cmd_list(FILE *f)
5334 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
5337 0ebf8283 2019-07-24 stsp n = fprintf(f, "# Available histedit commands:\n");
5338 0ebf8283 2019-07-24 stsp if (n < 0)
5339 0ebf8283 2019-07-24 stsp return got_ferror(f, GOT_ERR_IO);
5341 0ebf8283 2019-07-24 stsp for (i = 0; i < nitems(got_histedit_cmds); i++) {
5342 0ebf8283 2019-07-24 stsp struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
5343 0ebf8283 2019-07-24 stsp n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
5344 0ebf8283 2019-07-24 stsp cmd->desc);
5345 0ebf8283 2019-07-24 stsp if (n < 0) {
5346 0ebf8283 2019-07-24 stsp err = got_ferror(f, GOT_ERR_IO);
5350 0ebf8283 2019-07-24 stsp n = fprintf(f, "# Commits will be processed in order from top to "
5351 0ebf8283 2019-07-24 stsp "bottom of this file.\n");
5352 0ebf8283 2019-07-24 stsp if (n < 0)
5353 0ebf8283 2019-07-24 stsp return got_ferror(f, GOT_ERR_IO);
5354 0ebf8283 2019-07-24 stsp return err;
5357 0ebf8283 2019-07-24 stsp static const struct got_error *
5358 0ebf8283 2019-07-24 stsp histedit_syntax_error(int lineno)
5360 0ebf8283 2019-07-24 stsp static char msg[42];
5363 0ebf8283 2019-07-24 stsp ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
5365 0ebf8283 2019-07-24 stsp if (ret == -1 || ret >= sizeof(msg))
5366 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_HISTEDIT_SYNTAX);
5368 0ebf8283 2019-07-24 stsp return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
5371 0ebf8283 2019-07-24 stsp static const struct got_error *
5372 0ebf8283 2019-07-24 stsp append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
5373 0ebf8283 2019-07-24 stsp char *logmsg, struct got_repository *repo)
5375 0ebf8283 2019-07-24 stsp const struct got_error *err;
5376 0ebf8283 2019-07-24 stsp struct got_commit_object *folded_commit = NULL;
5377 5943eee2 2019-08-13 stsp char *id_str, *folded_logmsg = NULL;
5379 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, hle->commit_id);
5381 0ebf8283 2019-07-24 stsp return err;
5383 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
5385 0ebf8283 2019-07-24 stsp goto done;
5387 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
5389 5943eee2 2019-08-13 stsp goto done;
5390 0ebf8283 2019-07-24 stsp if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
5391 0ebf8283 2019-07-24 stsp logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
5392 5943eee2 2019-08-13 stsp folded_logmsg) == -1) {
5393 0ebf8283 2019-07-24 stsp err = got_error_from_errno("asprintf");
5394 0ebf8283 2019-07-24 stsp goto done;
5397 0ebf8283 2019-07-24 stsp if (folded_commit)
5398 0ebf8283 2019-07-24 stsp got_object_commit_close(folded_commit);
5399 0ebf8283 2019-07-24 stsp free(id_str);
5400 5943eee2 2019-08-13 stsp free(folded_logmsg);
5401 0ebf8283 2019-07-24 stsp return err;
5404 0ebf8283 2019-07-24 stsp static struct got_histedit_list_entry *
5405 0ebf8283 2019-07-24 stsp get_folded_commits(struct got_histedit_list_entry *hle)
5407 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *prev, *folded = NULL;
5409 0ebf8283 2019-07-24 stsp prev = TAILQ_PREV(hle, got_histedit_list, entry);
5410 3f9de99f 2019-07-24 stsp while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
5411 3f9de99f 2019-07-24 stsp prev->cmd->code == GOT_HISTEDIT_DROP)) {
5412 3f9de99f 2019-07-24 stsp if (prev->cmd->code == GOT_HISTEDIT_FOLD)
5413 3f9de99f 2019-07-24 stsp folded = prev;
5414 0ebf8283 2019-07-24 stsp prev = TAILQ_PREV(prev, got_histedit_list, entry);
5417 0ebf8283 2019-07-24 stsp return folded;
5420 0ebf8283 2019-07-24 stsp static const struct got_error *
5421 0ebf8283 2019-07-24 stsp histedit_edit_logmsg(struct got_histedit_list_entry *hle,
5422 0ebf8283 2019-07-24 stsp struct got_repository *repo)
5424 5943eee2 2019-08-13 stsp char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
5425 0ebf8283 2019-07-24 stsp char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
5426 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
5427 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
5429 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *folded = NULL;
5431 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&commit, repo, hle->commit_id);
5433 0ebf8283 2019-07-24 stsp return err;
5435 0ebf8283 2019-07-24 stsp folded = get_folded_commits(hle);
5436 0ebf8283 2019-07-24 stsp if (folded) {
5437 0ebf8283 2019-07-24 stsp while (folded != hle) {
5438 3f9de99f 2019-07-24 stsp if (folded->cmd->code == GOT_HISTEDIT_DROP) {
5439 3f9de99f 2019-07-24 stsp folded = TAILQ_NEXT(folded, entry);
5442 0ebf8283 2019-07-24 stsp err = append_folded_commit_msg(&new_msg, folded,
5443 0ebf8283 2019-07-24 stsp logmsg, repo);
5445 0ebf8283 2019-07-24 stsp goto done;
5446 0ebf8283 2019-07-24 stsp free(logmsg);
5447 0ebf8283 2019-07-24 stsp logmsg = new_msg;
5448 0ebf8283 2019-07-24 stsp folded = TAILQ_NEXT(folded, entry);
5452 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, hle->commit_id);
5454 0ebf8283 2019-07-24 stsp goto done;
5455 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&orig_logmsg, commit);
5457 5943eee2 2019-08-13 stsp goto done;
5458 0ebf8283 2019-07-24 stsp if (asprintf(&new_msg,
5459 0ebf8283 2019-07-24 stsp "%s\n# original log message of commit %s: %s",
5460 5943eee2 2019-08-13 stsp logmsg ? logmsg : "", id_str, orig_logmsg) == -1) {
5461 0ebf8283 2019-07-24 stsp err = got_error_from_errno("asprintf");
5462 0ebf8283 2019-07-24 stsp goto done;
5464 0ebf8283 2019-07-24 stsp free(logmsg);
5465 0ebf8283 2019-07-24 stsp logmsg = new_msg;
5467 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, hle->commit_id);
5469 0ebf8283 2019-07-24 stsp goto done;
5471 0ebf8283 2019-07-24 stsp err = got_opentemp_named_fd(&logmsg_path, &fd, "/tmp/got-logmsg");
5473 0ebf8283 2019-07-24 stsp goto done;
5475 0ebf8283 2019-07-24 stsp dprintf(fd, logmsg);
5476 0ebf8283 2019-07-24 stsp close(fd);
5478 0ebf8283 2019-07-24 stsp err = get_editor(&editor);
5480 0ebf8283 2019-07-24 stsp goto done;
5482 0ebf8283 2019-07-24 stsp err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg);
5483 0ebf8283 2019-07-24 stsp if (err) {
5484 0ebf8283 2019-07-24 stsp if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
5485 0ebf8283 2019-07-24 stsp goto done;
5486 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&hle->logmsg, commit);
5489 0ebf8283 2019-07-24 stsp if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
5490 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("unlink", logmsg_path);
5491 0ebf8283 2019-07-24 stsp free(logmsg_path);
5492 0ebf8283 2019-07-24 stsp free(logmsg);
5493 5943eee2 2019-08-13 stsp free(orig_logmsg);
5494 0ebf8283 2019-07-24 stsp free(editor);
5495 0ebf8283 2019-07-24 stsp if (commit)
5496 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
5497 0ebf8283 2019-07-24 stsp return err;
5500 0ebf8283 2019-07-24 stsp static const struct got_error *
5501 0ebf8283 2019-07-24 stsp histedit_parse_list(struct got_histedit_list *histedit_cmds,
5502 0ebf8283 2019-07-24 stsp FILE *f, struct got_repository *repo)
5504 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
5505 0ebf8283 2019-07-24 stsp char *line = NULL, *p, *end;
5506 0ebf8283 2019-07-24 stsp size_t size;
5507 0ebf8283 2019-07-24 stsp ssize_t len;
5508 0ebf8283 2019-07-24 stsp int lineno = 0, i;
5509 0ebf8283 2019-07-24 stsp const struct got_histedit_cmd *cmd;
5510 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id = NULL;
5511 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle = NULL;
5513 0ebf8283 2019-07-24 stsp for (;;) {
5514 0ebf8283 2019-07-24 stsp len = getline(&line, &size, f);
5515 0ebf8283 2019-07-24 stsp if (len == -1) {
5516 0ebf8283 2019-07-24 stsp const struct got_error *getline_err;
5517 0ebf8283 2019-07-24 stsp if (feof(f))
5519 0ebf8283 2019-07-24 stsp getline_err = got_error_from_errno("getline");
5520 0ebf8283 2019-07-24 stsp err = got_ferror(f, getline_err->code);
5525 0ebf8283 2019-07-24 stsp while (isspace((unsigned char)p[0]))
5527 0ebf8283 2019-07-24 stsp if (p[0] == '#' || p[0] == '\0') {
5528 0ebf8283 2019-07-24 stsp free(line);
5529 0ebf8283 2019-07-24 stsp line = NULL;
5532 0ebf8283 2019-07-24 stsp cmd = NULL;
5533 0ebf8283 2019-07-24 stsp for (i = 0; i < nitems(got_histedit_cmds); i++) {
5534 0ebf8283 2019-07-24 stsp cmd = &got_histedit_cmds[i];
5535 0ebf8283 2019-07-24 stsp if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
5536 0ebf8283 2019-07-24 stsp isspace((unsigned char)p[strlen(cmd->name)])) {
5537 0ebf8283 2019-07-24 stsp p += strlen(cmd->name);
5540 0ebf8283 2019-07-24 stsp if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
5545 6c1844f6 2019-07-25 stsp if (i == nitems(got_histedit_cmds)) {
5546 0ebf8283 2019-07-24 stsp err = histedit_syntax_error(lineno);
5549 0ebf8283 2019-07-24 stsp while (isspace((unsigned char)p[0]))
5551 0ebf8283 2019-07-24 stsp if (cmd->code == GOT_HISTEDIT_MESG) {
5552 0ebf8283 2019-07-24 stsp if (hle == NULL || hle->logmsg != NULL) {
5553 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_HISTEDIT_CMD);
5556 0ebf8283 2019-07-24 stsp if (p[0] == '\0') {
5557 0ebf8283 2019-07-24 stsp err = histedit_edit_logmsg(hle, repo);
5561 0ebf8283 2019-07-24 stsp hle->logmsg = strdup(p);
5562 0ebf8283 2019-07-24 stsp if (hle->logmsg == NULL) {
5563 0ebf8283 2019-07-24 stsp err = got_error_from_errno("strdup");
5567 0ebf8283 2019-07-24 stsp free(line);
5568 0ebf8283 2019-07-24 stsp line = NULL;
5572 0ebf8283 2019-07-24 stsp while (end[0] && !isspace((unsigned char)end[0]))
5574 0ebf8283 2019-07-24 stsp *end = '\0';
5576 0ebf8283 2019-07-24 stsp err = got_object_resolve_id_str(&commit_id, repo, p);
5577 0ebf8283 2019-07-24 stsp if (err) {
5578 0ebf8283 2019-07-24 stsp /* override error code */
5579 0ebf8283 2019-07-24 stsp err = histedit_syntax_error(lineno);
5583 0ebf8283 2019-07-24 stsp hle = malloc(sizeof(*hle));
5584 0ebf8283 2019-07-24 stsp if (hle == NULL) {
5585 0ebf8283 2019-07-24 stsp err = got_error_from_errno("malloc");
5588 0ebf8283 2019-07-24 stsp hle->cmd = cmd;
5589 0ebf8283 2019-07-24 stsp hle->commit_id = commit_id;
5590 0ebf8283 2019-07-24 stsp hle->logmsg = NULL;
5591 0ebf8283 2019-07-24 stsp commit_id = NULL;
5592 0ebf8283 2019-07-24 stsp free(line);
5593 0ebf8283 2019-07-24 stsp line = NULL;
5594 0ebf8283 2019-07-24 stsp TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
5597 0ebf8283 2019-07-24 stsp free(line);
5598 0ebf8283 2019-07-24 stsp free(commit_id);
5599 0ebf8283 2019-07-24 stsp return err;
5602 0ebf8283 2019-07-24 stsp static const struct got_error *
5603 bfce7f83 2019-07-27 stsp histedit_check_script(struct got_histedit_list *histedit_cmds,
5604 bfce7f83 2019-07-27 stsp struct got_object_id_queue *commits, struct got_repository *repo)
5606 bfce7f83 2019-07-27 stsp const struct got_error *err = NULL;
5607 bfce7f83 2019-07-27 stsp struct got_object_qid *qid;
5608 bfce7f83 2019-07-27 stsp struct got_histedit_list_entry *hle;
5609 bfce7f83 2019-07-27 stsp static char msg[80];
5610 bfce7f83 2019-07-27 stsp char *id_str;
5612 bfce7f83 2019-07-27 stsp if (TAILQ_EMPTY(histedit_cmds))
5613 bfce7f83 2019-07-27 stsp return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
5614 bfce7f83 2019-07-27 stsp "histedit script contains no commands");
5615 a0de39f3 2019-08-09 stsp if (SIMPLEQ_EMPTY(commits))
5616 a0de39f3 2019-08-09 stsp return got_error(GOT_ERR_EMPTY_HISTEDIT);
5618 bfce7f83 2019-07-27 stsp SIMPLEQ_FOREACH(qid, commits, entry) {
5619 bfce7f83 2019-07-27 stsp TAILQ_FOREACH(hle, histedit_cmds, entry) {
5620 bfce7f83 2019-07-27 stsp if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
5623 bfce7f83 2019-07-27 stsp if (hle == NULL) {
5624 bfce7f83 2019-07-27 stsp err = got_object_id_str(&id_str, qid->id);
5626 bfce7f83 2019-07-27 stsp return err;
5627 bfce7f83 2019-07-27 stsp snprintf(msg, sizeof(msg),
5628 bfce7f83 2019-07-27 stsp "commit %s missing from histedit script", id_str);
5629 bfce7f83 2019-07-27 stsp free(id_str);
5630 bfce7f83 2019-07-27 stsp return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
5634 0def28b1 2019-08-17 stsp hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
5635 0def28b1 2019-08-17 stsp if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
5636 bfce7f83 2019-07-27 stsp return got_error_msg(GOT_ERR_HISTEDIT_CMD,
5637 bfce7f83 2019-07-27 stsp "last commit in histedit script cannot be folded");
5639 bfce7f83 2019-07-27 stsp return NULL;
5642 bfce7f83 2019-07-27 stsp static const struct got_error *
5643 0ebf8283 2019-07-24 stsp histedit_run_editor(struct got_histedit_list *histedit_cmds,
5644 bfce7f83 2019-07-27 stsp const char *path, struct got_object_id_queue *commits,
5645 bfce7f83 2019-07-27 stsp struct got_repository *repo)
5647 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
5648 0ebf8283 2019-07-24 stsp char *editor;
5649 0ebf8283 2019-07-24 stsp FILE *f = NULL;
5651 0ebf8283 2019-07-24 stsp err = get_editor(&editor);
5653 0ebf8283 2019-07-24 stsp return err;
5655 0ebf8283 2019-07-24 stsp if (spawn_editor(editor, path) == -1) {
5656 0ebf8283 2019-07-24 stsp err = got_error_from_errno("failed spawning editor");
5657 0ebf8283 2019-07-24 stsp goto done;
5660 0ebf8283 2019-07-24 stsp f = fopen(path, "r");
5661 0ebf8283 2019-07-24 stsp if (f == NULL) {
5662 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fopen");
5663 0ebf8283 2019-07-24 stsp goto done;
5665 0ebf8283 2019-07-24 stsp err = histedit_parse_list(histedit_cmds, f, repo);
5667 bfce7f83 2019-07-27 stsp goto done;
5669 bfce7f83 2019-07-27 stsp err = histedit_check_script(histedit_cmds, commits, repo);
5671 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
5672 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
5673 0ebf8283 2019-07-24 stsp free(editor);
5674 0ebf8283 2019-07-24 stsp return err;
5677 0ebf8283 2019-07-24 stsp static const struct got_error *
5678 bfce7f83 2019-07-27 stsp histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
5679 0ebf8283 2019-07-24 stsp struct got_object_id_queue *, const char *, struct got_repository *);
5681 0ebf8283 2019-07-24 stsp static const struct got_error *
5682 0ebf8283 2019-07-24 stsp histedit_edit_script(struct got_histedit_list *histedit_cmds,
5683 0ebf8283 2019-07-24 stsp struct got_object_id_queue *commits, struct got_repository *repo)
5685 0ebf8283 2019-07-24 stsp const struct got_error *err;
5686 0ebf8283 2019-07-24 stsp FILE *f = NULL;
5687 0ebf8283 2019-07-24 stsp char *path = NULL;
5689 0ebf8283 2019-07-24 stsp err = got_opentemp_named(&path, &f, "got-histedit");
5691 0ebf8283 2019-07-24 stsp return err;
5693 0ebf8283 2019-07-24 stsp err = write_cmd_list(f);
5695 0ebf8283 2019-07-24 stsp goto done;
5697 0ebf8283 2019-07-24 stsp err = histedit_write_commit_list(commits, f, repo);
5699 0ebf8283 2019-07-24 stsp goto done;
5701 0ebf8283 2019-07-24 stsp if (fclose(f) != 0) {
5702 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
5703 0ebf8283 2019-07-24 stsp goto done;
5707 bfce7f83 2019-07-27 stsp err = histedit_run_editor(histedit_cmds, path, commits, repo);
5708 0ebf8283 2019-07-24 stsp if (err) {
5709 bfce7f83 2019-07-27 stsp if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
5710 bfce7f83 2019-07-27 stsp err->code != GOT_ERR_HISTEDIT_CMD)
5711 0ebf8283 2019-07-24 stsp goto done;
5712 bfce7f83 2019-07-27 stsp err = histedit_edit_list_retry(histedit_cmds, err,
5713 0ebf8283 2019-07-24 stsp commits, path, repo);
5716 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
5717 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
5718 0ebf8283 2019-07-24 stsp if (path && unlink(path) != 0 && err == NULL)
5719 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("unlink", path);
5720 0ebf8283 2019-07-24 stsp free(path);
5721 0ebf8283 2019-07-24 stsp return err;
5724 0ebf8283 2019-07-24 stsp static const struct got_error *
5725 0ebf8283 2019-07-24 stsp histedit_save_list(struct got_histedit_list *histedit_cmds,
5726 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
5728 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
5729 0ebf8283 2019-07-24 stsp char *path = NULL;
5730 0ebf8283 2019-07-24 stsp FILE *f = NULL;
5731 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle;
5732 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
5734 c3022ba5 2019-07-27 stsp err = got_worktree_get_histedit_script_path(&path, worktree);
5736 0ebf8283 2019-07-24 stsp return err;
5738 0ebf8283 2019-07-24 stsp f = fopen(path, "w");
5739 0ebf8283 2019-07-24 stsp if (f == NULL) {
5740 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("fopen", path);
5741 0ebf8283 2019-07-24 stsp goto done;
5743 0ebf8283 2019-07-24 stsp TAILQ_FOREACH(hle, histedit_cmds, entry) {
5744 0ebf8283 2019-07-24 stsp err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
5749 0ebf8283 2019-07-24 stsp if (hle->logmsg) {
5750 0ebf8283 2019-07-24 stsp int n = fprintf(f, "%c %s\n",
5751 0ebf8283 2019-07-24 stsp GOT_HISTEDIT_MESG, hle->logmsg);
5752 0ebf8283 2019-07-24 stsp if (n < 0) {
5753 0ebf8283 2019-07-24 stsp err = got_ferror(f, GOT_ERR_IO);
5759 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
5760 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
5761 0ebf8283 2019-07-24 stsp free(path);
5762 0ebf8283 2019-07-24 stsp if (commit)
5763 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
5764 0ebf8283 2019-07-24 stsp return err;
5768 bfce7f83 2019-07-27 stsp histedit_free_list(struct got_histedit_list *histedit_cmds)
5770 bfce7f83 2019-07-27 stsp struct got_histedit_list_entry *hle;
5772 bfce7f83 2019-07-27 stsp while ((hle = TAILQ_FIRST(histedit_cmds))) {
5773 bfce7f83 2019-07-27 stsp TAILQ_REMOVE(histedit_cmds, hle, entry);
5774 bfce7f83 2019-07-27 stsp free(hle);
5778 0ebf8283 2019-07-24 stsp static const struct got_error *
5779 0ebf8283 2019-07-24 stsp histedit_load_list(struct got_histedit_list *histedit_cmds,
5780 0ebf8283 2019-07-24 stsp const char *path, struct got_repository *repo)
5782 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
5783 0ebf8283 2019-07-24 stsp FILE *f = NULL;
5785 0ebf8283 2019-07-24 stsp f = fopen(path, "r");
5786 0ebf8283 2019-07-24 stsp if (f == NULL) {
5787 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("fopen", path);
5788 0ebf8283 2019-07-24 stsp goto done;
5791 0ebf8283 2019-07-24 stsp err = histedit_parse_list(histedit_cmds, f, repo);
5793 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
5794 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
5795 0ebf8283 2019-07-24 stsp return err;
5798 0ebf8283 2019-07-24 stsp static const struct got_error *
5799 0ebf8283 2019-07-24 stsp histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
5800 bfce7f83 2019-07-27 stsp const struct got_error *edit_err, struct got_object_id_queue *commits,
5801 0ebf8283 2019-07-24 stsp const char *path, struct got_repository *repo)
5803 bfce7f83 2019-07-27 stsp const struct got_error *err = NULL, *prev_err = edit_err;
5804 0ebf8283 2019-07-24 stsp int resp = ' ';
5806 b006047e 2019-07-25 stsp while (resp != 'c' && resp != 'r' && resp != 'a') {
5807 0ebf8283 2019-07-24 stsp printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
5808 bfce7f83 2019-07-27 stsp "or (a)bort: ", getprogname(), prev_err->msg);
5809 0ebf8283 2019-07-24 stsp resp = getchar();
5810 a61a4414 2019-08-07 stsp if (resp == '\n')
5811 a61a4414 2019-08-07 stsp resp = getchar();
5812 426ebf2e 2019-07-25 stsp if (resp == 'c') {
5813 bfce7f83 2019-07-27 stsp histedit_free_list(histedit_cmds);
5814 bfce7f83 2019-07-27 stsp err = histedit_run_editor(histedit_cmds, path, commits,
5816 426ebf2e 2019-07-25 stsp if (err) {
5817 bfce7f83 2019-07-27 stsp if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
5818 bfce7f83 2019-07-27 stsp err->code != GOT_ERR_HISTEDIT_CMD)
5820 bfce7f83 2019-07-27 stsp prev_err = err;
5821 426ebf2e 2019-07-25 stsp resp = ' ';
5825 426ebf2e 2019-07-25 stsp } else if (resp == 'r') {
5826 bfce7f83 2019-07-27 stsp histedit_free_list(histedit_cmds);
5827 0ebf8283 2019-07-24 stsp err = histedit_edit_script(histedit_cmds,
5828 0ebf8283 2019-07-24 stsp commits, repo);
5829 426ebf2e 2019-07-25 stsp if (err) {
5830 bfce7f83 2019-07-27 stsp if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
5831 bfce7f83 2019-07-27 stsp err->code != GOT_ERR_HISTEDIT_CMD)
5833 bfce7f83 2019-07-27 stsp prev_err = err;
5834 426ebf2e 2019-07-25 stsp resp = ' ';
5838 426ebf2e 2019-07-25 stsp } else if (resp == 'a') {
5839 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_HISTEDIT_CANCEL);
5842 0ebf8283 2019-07-24 stsp printf("invalid response '%c'\n", resp);
5845 0ebf8283 2019-07-24 stsp return err;
5848 0ebf8283 2019-07-24 stsp static const struct got_error *
5849 0ebf8283 2019-07-24 stsp histedit_complete(struct got_worktree *worktree,
5850 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
5851 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_repository *repo)
5853 0ebf8283 2019-07-24 stsp printf("Switching work tree to %s\n",
5854 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch));
5855 3e3a69f1 2019-07-25 stsp return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
5856 3e3a69f1 2019-07-25 stsp branch, repo);
5859 0ebf8283 2019-07-24 stsp static const struct got_error *
5860 0ebf8283 2019-07-24 stsp show_histedit_progress(struct got_commit_object *commit,
5861 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle, struct got_object_id *new_id)
5863 0ebf8283 2019-07-24 stsp const struct got_error *err;
5864 0ebf8283 2019-07-24 stsp char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
5866 0ebf8283 2019-07-24 stsp err = got_object_id_str(&old_id_str, hle->commit_id);
5868 0ebf8283 2019-07-24 stsp goto done;
5870 0ebf8283 2019-07-24 stsp if (new_id) {
5871 0ebf8283 2019-07-24 stsp err = got_object_id_str(&new_id_str, new_id);
5873 0ebf8283 2019-07-24 stsp goto done;
5876 0ebf8283 2019-07-24 stsp old_id_str[12] = '\0';
5877 0ebf8283 2019-07-24 stsp if (new_id_str)
5878 0ebf8283 2019-07-24 stsp new_id_str[12] = '\0';
5880 0ebf8283 2019-07-24 stsp if (hle->logmsg) {
5881 0ebf8283 2019-07-24 stsp logmsg = strdup(hle->logmsg);
5882 0ebf8283 2019-07-24 stsp if (logmsg == NULL) {
5883 0ebf8283 2019-07-24 stsp err = got_error_from_errno("strdup");
5884 0ebf8283 2019-07-24 stsp goto done;
5886 0ebf8283 2019-07-24 stsp trim_logmsg(logmsg, 42);
5888 0ebf8283 2019-07-24 stsp err = get_short_logmsg(&logmsg, 42, commit);
5890 0ebf8283 2019-07-24 stsp goto done;
5893 0ebf8283 2019-07-24 stsp switch (hle->cmd->code) {
5894 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_PICK:
5895 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_EDIT:
5896 0ebf8283 2019-07-24 stsp printf("%s -> %s: %s\n", old_id_str,
5897 0ebf8283 2019-07-24 stsp new_id_str ? new_id_str : "no-op change", logmsg);
5899 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_DROP:
5900 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_FOLD:
5901 0ebf8283 2019-07-24 stsp printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
5909 0ebf8283 2019-07-24 stsp free(old_id_str);
5910 0ebf8283 2019-07-24 stsp free(new_id_str);
5911 0ebf8283 2019-07-24 stsp return err;
5914 0ebf8283 2019-07-24 stsp static const struct got_error *
5915 0ebf8283 2019-07-24 stsp histedit_commit(struct got_pathlist_head *merged_paths,
5916 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
5917 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
5918 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
5920 0ebf8283 2019-07-24 stsp const struct got_error *err;
5921 0ebf8283 2019-07-24 stsp struct got_commit_object *commit;
5922 0ebf8283 2019-07-24 stsp struct got_object_id *new_commit_id;
5924 0ebf8283 2019-07-24 stsp if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
5925 0ebf8283 2019-07-24 stsp && hle->logmsg == NULL) {
5926 0ebf8283 2019-07-24 stsp err = histedit_edit_logmsg(hle, repo);
5928 0ebf8283 2019-07-24 stsp return err;
5931 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&commit, repo, hle->commit_id);
5933 0ebf8283 2019-07-24 stsp return err;
5935 0ebf8283 2019-07-24 stsp err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
5936 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, commit, hle->commit_id,
5937 3e3a69f1 2019-07-25 stsp hle->logmsg, repo);
5938 0ebf8283 2019-07-24 stsp if (err) {
5939 0ebf8283 2019-07-24 stsp if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
5940 0ebf8283 2019-07-24 stsp goto done;
5941 0ebf8283 2019-07-24 stsp err = show_histedit_progress(commit, hle, NULL);
5943 0ebf8283 2019-07-24 stsp err = show_histedit_progress(commit, hle, new_commit_id);
5944 0ebf8283 2019-07-24 stsp free(new_commit_id);
5947 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
5948 0ebf8283 2019-07-24 stsp return err;
5951 0ebf8283 2019-07-24 stsp static const struct got_error *
5952 0ebf8283 2019-07-24 stsp histedit_skip_commit(struct got_histedit_list_entry *hle,
5953 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
5955 0ebf8283 2019-07-24 stsp const struct got_error *error;
5956 0ebf8283 2019-07-24 stsp struct got_commit_object *commit;
5958 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
5960 0ebf8283 2019-07-24 stsp if (error)
5961 0ebf8283 2019-07-24 stsp return error;
5963 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo, hle->commit_id);
5964 0ebf8283 2019-07-24 stsp if (error)
5965 0ebf8283 2019-07-24 stsp return error;
5967 0ebf8283 2019-07-24 stsp error = show_histedit_progress(commit, hle, NULL);
5968 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
5969 0ebf8283 2019-07-24 stsp return error;
5972 0ebf8283 2019-07-24 stsp static const struct got_error *
5973 0ebf8283 2019-07-24 stsp cmd_histedit(int argc, char *argv[])
5975 0ebf8283 2019-07-24 stsp const struct got_error *error = NULL;
5976 0ebf8283 2019-07-24 stsp struct got_worktree *worktree = NULL;
5977 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex = NULL;
5978 0ebf8283 2019-07-24 stsp struct got_repository *repo = NULL;
5979 0ebf8283 2019-07-24 stsp char *cwd = NULL;
5980 0ebf8283 2019-07-24 stsp struct got_reference *branch = NULL;
5981 0ebf8283 2019-07-24 stsp struct got_reference *tmp_branch = NULL;
5982 0ebf8283 2019-07-24 stsp struct got_object_id *resume_commit_id = NULL;
5983 0ebf8283 2019-07-24 stsp struct got_object_id *base_commit_id = NULL;
5984 0ebf8283 2019-07-24 stsp struct got_object_id *head_commit_id = NULL;
5985 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
5986 6ba2bea2 2019-07-27 stsp int ch, rebase_in_progress = 0, did_something;
5987 0ebf8283 2019-07-24 stsp int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
5988 0ebf8283 2019-07-24 stsp const char *edit_script_path = NULL;
5989 0ebf8283 2019-07-24 stsp unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
5990 0ebf8283 2019-07-24 stsp struct got_object_id_queue commits;
5991 0ebf8283 2019-07-24 stsp struct got_pathlist_head merged_paths;
5992 0ebf8283 2019-07-24 stsp const struct got_object_id_queue *parent_ids;
5993 0ebf8283 2019-07-24 stsp struct got_object_qid *pid;
5994 0ebf8283 2019-07-24 stsp struct got_histedit_list histedit_cmds;
5995 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle;
5997 0ebf8283 2019-07-24 stsp SIMPLEQ_INIT(&commits);
5998 0ebf8283 2019-07-24 stsp TAILQ_INIT(&histedit_cmds);
5999 0ebf8283 2019-07-24 stsp TAILQ_INIT(&merged_paths);
6001 0ebf8283 2019-07-24 stsp while ((ch = getopt(argc, argv, "acF:")) != -1) {
6002 0ebf8283 2019-07-24 stsp switch (ch) {
6004 0ebf8283 2019-07-24 stsp abort_edit = 1;
6007 0ebf8283 2019-07-24 stsp continue_edit = 1;
6010 0ebf8283 2019-07-24 stsp edit_script_path = optarg;
6013 0ebf8283 2019-07-24 stsp usage_histedit();
6014 0ebf8283 2019-07-24 stsp /* NOTREACHED */
6018 0ebf8283 2019-07-24 stsp argc -= optind;
6019 0ebf8283 2019-07-24 stsp argv += optind;
6021 0ebf8283 2019-07-24 stsp #ifndef PROFILE
6022 0ebf8283 2019-07-24 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6023 0ebf8283 2019-07-24 stsp "unveil", NULL) == -1)
6024 0ebf8283 2019-07-24 stsp err(1, "pledge");
6026 0ebf8283 2019-07-24 stsp if (abort_edit && continue_edit)
6027 0ebf8283 2019-07-24 stsp usage_histedit();
6028 0ebf8283 2019-07-24 stsp if (argc != 0)
6029 0ebf8283 2019-07-24 stsp usage_histedit();
6032 0ebf8283 2019-07-24 stsp * This command cannot apply unveil(2) in all cases because the
6033 0ebf8283 2019-07-24 stsp * user may choose to run an editor to edit the histedit script
6034 0ebf8283 2019-07-24 stsp * and to edit individual commit log messages.
6035 0ebf8283 2019-07-24 stsp * unveil(2) traverses exec(2); if an editor is used we have to
6036 0ebf8283 2019-07-24 stsp * apply unveil after edit script and log messages have been written.
6037 0ebf8283 2019-07-24 stsp * XXX TODO: Make use of unveil(2) where possible.
6040 0ebf8283 2019-07-24 stsp cwd = getcwd(NULL, 0);
6041 0ebf8283 2019-07-24 stsp if (cwd == NULL) {
6042 0ebf8283 2019-07-24 stsp error = got_error_from_errno("getcwd");
6043 0ebf8283 2019-07-24 stsp goto done;
6045 0ebf8283 2019-07-24 stsp error = got_worktree_open(&worktree, cwd);
6046 0ebf8283 2019-07-24 stsp if (error)
6047 0ebf8283 2019-07-24 stsp goto done;
6049 0ebf8283 2019-07-24 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
6050 0ebf8283 2019-07-24 stsp if (error != NULL)
6051 0ebf8283 2019-07-24 stsp goto done;
6053 0ebf8283 2019-07-24 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
6054 0ebf8283 2019-07-24 stsp if (error)
6055 0ebf8283 2019-07-24 stsp goto done;
6056 0ebf8283 2019-07-24 stsp if (rebase_in_progress) {
6057 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_REBASING);
6058 0ebf8283 2019-07-24 stsp goto done;
6061 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
6062 0ebf8283 2019-07-24 stsp if (error)
6063 0ebf8283 2019-07-24 stsp goto done;
6065 0ebf8283 2019-07-24 stsp if (edit_in_progress && abort_edit) {
6066 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_continue(&resume_commit_id,
6067 3e3a69f1 2019-07-25 stsp &tmp_branch, &branch, &base_commit_id, &fileindex,
6068 3e3a69f1 2019-07-25 stsp worktree, repo);
6069 0ebf8283 2019-07-24 stsp if (error)
6070 0ebf8283 2019-07-24 stsp goto done;
6071 0ebf8283 2019-07-24 stsp printf("Switching work tree to %s\n",
6072 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch));
6073 3e3a69f1 2019-07-25 stsp error = got_worktree_histedit_abort(worktree, fileindex, repo,
6074 0ebf8283 2019-07-24 stsp branch, base_commit_id, update_progress, &did_something);
6075 0ebf8283 2019-07-24 stsp if (error)
6076 0ebf8283 2019-07-24 stsp goto done;
6077 0ebf8283 2019-07-24 stsp printf("Histedit of %s aborted\n",
6078 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch));
6079 0ebf8283 2019-07-24 stsp goto done; /* nothing else to do */
6080 0ebf8283 2019-07-24 stsp } else if (abort_edit) {
6081 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_NOT_HISTEDIT);
6082 0ebf8283 2019-07-24 stsp goto done;
6085 0ebf8283 2019-07-24 stsp if (continue_edit) {
6086 0ebf8283 2019-07-24 stsp char *path;
6088 0ebf8283 2019-07-24 stsp if (!edit_in_progress) {
6089 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_NOT_HISTEDIT);
6090 0ebf8283 2019-07-24 stsp goto done;
6093 c3022ba5 2019-07-27 stsp error = got_worktree_get_histedit_script_path(&path, worktree);
6094 0ebf8283 2019-07-24 stsp if (error)
6095 0ebf8283 2019-07-24 stsp goto done;
6097 0ebf8283 2019-07-24 stsp error = histedit_load_list(&histedit_cmds, path, repo);
6098 0ebf8283 2019-07-24 stsp free(path);
6099 0ebf8283 2019-07-24 stsp if (error)
6100 0ebf8283 2019-07-24 stsp goto done;
6102 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_continue(&resume_commit_id,
6103 3e3a69f1 2019-07-25 stsp &tmp_branch, &branch, &base_commit_id, &fileindex,
6104 3e3a69f1 2019-07-25 stsp worktree, repo);
6105 0ebf8283 2019-07-24 stsp if (error)
6106 0ebf8283 2019-07-24 stsp goto done;
6108 0ebf8283 2019-07-24 stsp error = got_ref_resolve(&head_commit_id, repo, branch);
6109 0ebf8283 2019-07-24 stsp if (error)
6110 0ebf8283 2019-07-24 stsp goto done;
6112 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo,
6113 0ebf8283 2019-07-24 stsp head_commit_id);
6114 0ebf8283 2019-07-24 stsp if (error)
6115 0ebf8283 2019-07-24 stsp goto done;
6116 0ebf8283 2019-07-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
6117 0ebf8283 2019-07-24 stsp pid = SIMPLEQ_FIRST(parent_ids);
6118 3aac7cf7 2019-07-25 stsp if (pid == NULL) {
6119 3aac7cf7 2019-07-25 stsp error = got_error(GOT_ERR_EMPTY_HISTEDIT);
6120 3aac7cf7 2019-07-25 stsp goto done;
6122 8ca9bd68 2019-07-25 stsp error = collect_commits(&commits, head_commit_id, pid->id,
6123 8ca9bd68 2019-07-25 stsp base_commit_id, got_worktree_get_path_prefix(worktree),
6124 8ca9bd68 2019-07-25 stsp GOT_ERR_HISTEDIT_PATH, repo);
6125 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
6126 0ebf8283 2019-07-24 stsp commit = NULL;
6127 0ebf8283 2019-07-24 stsp if (error)
6128 0ebf8283 2019-07-24 stsp goto done;
6130 0ebf8283 2019-07-24 stsp if (edit_in_progress) {
6131 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_HISTEDIT_BUSY);
6132 0ebf8283 2019-07-24 stsp goto done;
6135 0ebf8283 2019-07-24 stsp error = got_ref_open(&branch, repo,
6136 0ebf8283 2019-07-24 stsp got_worktree_get_head_ref_name(worktree), 0);
6137 0ebf8283 2019-07-24 stsp if (error != NULL)
6138 0ebf8283 2019-07-24 stsp goto done;
6140 c7d20a3f 2019-07-30 stsp if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
6141 c7d20a3f 2019-07-30 stsp error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
6142 c7d20a3f 2019-07-30 stsp "will not edit commit history of a branch outside "
6143 c7d20a3f 2019-07-30 stsp "the \"refs/heads/\" reference namespace");
6144 c7d20a3f 2019-07-30 stsp goto done;
6147 0ebf8283 2019-07-24 stsp error = got_ref_resolve(&head_commit_id, repo, branch);
6148 bfce7f83 2019-07-27 stsp got_ref_close(branch);
6149 bfce7f83 2019-07-27 stsp branch = NULL;
6150 0ebf8283 2019-07-24 stsp if (error)
6151 0ebf8283 2019-07-24 stsp goto done;
6153 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo,
6154 0ebf8283 2019-07-24 stsp head_commit_id);
6155 0ebf8283 2019-07-24 stsp if (error)
6156 0ebf8283 2019-07-24 stsp goto done;
6157 0ebf8283 2019-07-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
6158 0ebf8283 2019-07-24 stsp pid = SIMPLEQ_FIRST(parent_ids);
6159 3aac7cf7 2019-07-25 stsp if (pid == NULL) {
6160 3aac7cf7 2019-07-25 stsp error = got_error(GOT_ERR_EMPTY_HISTEDIT);
6161 3aac7cf7 2019-07-25 stsp goto done;
6163 8ca9bd68 2019-07-25 stsp error = collect_commits(&commits, head_commit_id, pid->id,
6164 8ca9bd68 2019-07-25 stsp got_worktree_get_base_commit_id(worktree),
6165 8ca9bd68 2019-07-25 stsp got_worktree_get_path_prefix(worktree),
6166 8ca9bd68 2019-07-25 stsp GOT_ERR_HISTEDIT_PATH, repo);
6167 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
6168 0ebf8283 2019-07-24 stsp commit = NULL;
6169 0ebf8283 2019-07-24 stsp if (error)
6170 0ebf8283 2019-07-24 stsp goto done;
6172 bfce7f83 2019-07-27 stsp error = got_worktree_histedit_prepare(&tmp_branch, &branch,
6173 bfce7f83 2019-07-27 stsp &base_commit_id, &fileindex, worktree, repo);
6174 bfce7f83 2019-07-27 stsp if (error)
6175 bfce7f83 2019-07-27 stsp goto done;
6177 0ebf8283 2019-07-24 stsp if (edit_script_path) {
6178 0ebf8283 2019-07-24 stsp error = histedit_load_list(&histedit_cmds,
6179 0ebf8283 2019-07-24 stsp edit_script_path, repo);
6180 6ba2bea2 2019-07-27 stsp if (error) {
6181 6ba2bea2 2019-07-27 stsp got_worktree_histedit_abort(worktree, fileindex,
6182 6ba2bea2 2019-07-27 stsp repo, branch, base_commit_id,
6183 6ba2bea2 2019-07-27 stsp update_progress, &did_something);
6184 0ebf8283 2019-07-24 stsp goto done;
6187 0ebf8283 2019-07-24 stsp error = histedit_edit_script(&histedit_cmds, &commits,
6189 6ba2bea2 2019-07-27 stsp if (error) {
6190 6ba2bea2 2019-07-27 stsp got_worktree_histedit_abort(worktree, fileindex,
6191 6ba2bea2 2019-07-27 stsp repo, branch, base_commit_id,
6192 6ba2bea2 2019-07-27 stsp update_progress, &did_something);
6193 0ebf8283 2019-07-24 stsp goto done;
6198 0ebf8283 2019-07-24 stsp error = histedit_save_list(&histedit_cmds, worktree,
6200 6ba2bea2 2019-07-27 stsp if (error) {
6201 6ba2bea2 2019-07-27 stsp got_worktree_histedit_abort(worktree, fileindex,
6202 6ba2bea2 2019-07-27 stsp repo, branch, base_commit_id,
6203 6ba2bea2 2019-07-27 stsp update_progress, &did_something);
6204 0ebf8283 2019-07-24 stsp goto done;
6209 4ec14e60 2019-08-28 hiltjo error = histedit_check_script(&histedit_cmds, &commits, repo);
6210 4ec14e60 2019-08-28 hiltjo if (error)
6211 0ebf8283 2019-07-24 stsp goto done;
6213 0ebf8283 2019-07-24 stsp TAILQ_FOREACH(hle, &histedit_cmds, entry) {
6214 0ebf8283 2019-07-24 stsp if (resume_commit_id) {
6215 0ebf8283 2019-07-24 stsp if (got_object_id_cmp(hle->commit_id,
6216 0ebf8283 2019-07-24 stsp resume_commit_id) != 0)
6219 0ebf8283 2019-07-24 stsp resume_commit_id = NULL;
6220 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_DROP ||
6221 0ebf8283 2019-07-24 stsp hle->cmd->code == GOT_HISTEDIT_FOLD) {
6222 0ebf8283 2019-07-24 stsp error = histedit_skip_commit(hle, worktree,
6225 0ebf8283 2019-07-24 stsp error = histedit_commit(NULL, worktree,
6226 3e3a69f1 2019-07-25 stsp fileindex, tmp_branch, hle, repo);
6228 0ebf8283 2019-07-24 stsp if (error)
6229 0ebf8283 2019-07-24 stsp goto done;
6233 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_DROP) {
6234 0ebf8283 2019-07-24 stsp error = histedit_skip_commit(hle, worktree, repo);
6235 0ebf8283 2019-07-24 stsp if (error)
6236 0ebf8283 2019-07-24 stsp goto done;
6240 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo,
6241 0ebf8283 2019-07-24 stsp hle->commit_id);
6242 0ebf8283 2019-07-24 stsp if (error)
6243 0ebf8283 2019-07-24 stsp goto done;
6244 0ebf8283 2019-07-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
6245 0ebf8283 2019-07-24 stsp pid = SIMPLEQ_FIRST(parent_ids);
6247 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_merge_files(&merged_paths,
6248 3e3a69f1 2019-07-25 stsp worktree, fileindex, pid->id, hle->commit_id, repo,
6249 3e3a69f1 2019-07-25 stsp rebase_progress, &rebase_status, check_cancelled, NULL);
6250 0ebf8283 2019-07-24 stsp if (error)
6251 0ebf8283 2019-07-24 stsp goto done;
6252 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
6253 0ebf8283 2019-07-24 stsp commit = NULL;
6255 0ebf8283 2019-07-24 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
6256 0ebf8283 2019-07-24 stsp got_worktree_rebase_pathlist_free(&merged_paths);
6260 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
6261 0ebf8283 2019-07-24 stsp char *id_str;
6262 0ebf8283 2019-07-24 stsp error = got_object_id_str(&id_str, hle->commit_id);
6263 0ebf8283 2019-07-24 stsp if (error)
6264 0ebf8283 2019-07-24 stsp goto done;
6265 0ebf8283 2019-07-24 stsp printf("Stopping histedit for amending commit %s\n",
6267 0ebf8283 2019-07-24 stsp free(id_str);
6268 0ebf8283 2019-07-24 stsp got_worktree_rebase_pathlist_free(&merged_paths);
6269 3e3a69f1 2019-07-25 stsp error = got_worktree_histedit_postpone(worktree,
6270 3e3a69f1 2019-07-25 stsp fileindex);
6271 0ebf8283 2019-07-24 stsp goto done;
6274 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
6275 0ebf8283 2019-07-24 stsp error = histedit_skip_commit(hle, worktree, repo);
6276 0ebf8283 2019-07-24 stsp if (error)
6277 0ebf8283 2019-07-24 stsp goto done;
6281 3e3a69f1 2019-07-25 stsp error = histedit_commit(&merged_paths, worktree, fileindex,
6282 3e3a69f1 2019-07-25 stsp tmp_branch, hle, repo);
6283 0ebf8283 2019-07-24 stsp got_worktree_rebase_pathlist_free(&merged_paths);
6284 0ebf8283 2019-07-24 stsp if (error)
6285 0ebf8283 2019-07-24 stsp goto done;
6288 0ebf8283 2019-07-24 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
6289 3e3a69f1 2019-07-25 stsp error = got_worktree_histedit_postpone(worktree, fileindex);
6290 0ebf8283 2019-07-24 stsp if (error)
6291 0ebf8283 2019-07-24 stsp goto done;
6292 0ebf8283 2019-07-24 stsp error = got_error_msg(GOT_ERR_CONFLICTS,
6293 0ebf8283 2019-07-24 stsp "conflicts must be resolved before rebasing can continue");
6295 3e3a69f1 2019-07-25 stsp error = histedit_complete(worktree, fileindex, tmp_branch,
6296 3e3a69f1 2019-07-25 stsp branch, repo);
6298 0ebf8283 2019-07-24 stsp got_object_id_queue_free(&commits);
6299 bfce7f83 2019-07-27 stsp histedit_free_list(&histedit_cmds);
6300 0ebf8283 2019-07-24 stsp free(head_commit_id);
6301 0ebf8283 2019-07-24 stsp free(base_commit_id);
6302 0ebf8283 2019-07-24 stsp free(resume_commit_id);
6303 0ebf8283 2019-07-24 stsp if (commit)
6304 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
6305 0ebf8283 2019-07-24 stsp if (branch)
6306 0ebf8283 2019-07-24 stsp got_ref_close(branch);
6307 0ebf8283 2019-07-24 stsp if (tmp_branch)
6308 0ebf8283 2019-07-24 stsp got_ref_close(tmp_branch);
6309 0ebf8283 2019-07-24 stsp if (worktree)
6310 0ebf8283 2019-07-24 stsp got_worktree_close(worktree);
6312 715dc77e 2019-08-03 stsp got_repo_close(repo);
6313 715dc77e 2019-08-03 stsp return error;
6316 715dc77e 2019-08-03 stsp __dead static void
6317 715dc77e 2019-08-03 stsp usage_stage(void)
6319 f3055044 2019-08-07 stsp fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
6320 f3055044 2019-08-07 stsp "[file-path ...]\n",
6321 715dc77e 2019-08-03 stsp getprogname());
6325 715dc77e 2019-08-03 stsp static const struct got_error *
6326 408b4ebc 2019-08-03 stsp print_stage(void *arg, unsigned char status, unsigned char staged_status,
6327 408b4ebc 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
6328 408b4ebc 2019-08-03 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
6330 408b4ebc 2019-08-03 stsp const struct got_error *err = NULL;
6331 408b4ebc 2019-08-03 stsp char *id_str = NULL;
6333 408b4ebc 2019-08-03 stsp if (staged_status != GOT_STATUS_ADD &&
6334 408b4ebc 2019-08-03 stsp staged_status != GOT_STATUS_MODIFY &&
6335 408b4ebc 2019-08-03 stsp staged_status != GOT_STATUS_DELETE)
6336 408b4ebc 2019-08-03 stsp return NULL;
6338 408b4ebc 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
6339 408b4ebc 2019-08-03 stsp staged_status == GOT_STATUS_MODIFY)
6340 408b4ebc 2019-08-03 stsp err = got_object_id_str(&id_str, staged_blob_id);
6342 408b4ebc 2019-08-03 stsp err = got_object_id_str(&id_str, blob_id);
6344 408b4ebc 2019-08-03 stsp return err;
6346 408b4ebc 2019-08-03 stsp printf("%s %c %s\n", id_str, staged_status, path);
6347 408b4ebc 2019-08-03 stsp free(id_str);
6348 dc424a06 2019-08-07 stsp return NULL;
6351 dc424a06 2019-08-07 stsp static const struct got_error *
6352 715dc77e 2019-08-03 stsp cmd_stage(int argc, char *argv[])
6354 715dc77e 2019-08-03 stsp const struct got_error *error = NULL;
6355 715dc77e 2019-08-03 stsp struct got_repository *repo = NULL;
6356 715dc77e 2019-08-03 stsp struct got_worktree *worktree = NULL;
6357 715dc77e 2019-08-03 stsp char *cwd = NULL;
6358 715dc77e 2019-08-03 stsp struct got_pathlist_head paths;
6359 715dc77e 2019-08-03 stsp struct got_pathlist_entry *pe;
6360 dc424a06 2019-08-07 stsp int ch, list_stage = 0, pflag = 0;
6361 dc424a06 2019-08-07 stsp FILE *patch_script_file = NULL;
6362 2e1f37b0 2019-08-08 stsp const char *patch_script_path = NULL;
6363 2e1f37b0 2019-08-08 stsp struct choose_patch_arg cpa;
6365 715dc77e 2019-08-03 stsp TAILQ_INIT(&paths);
6367 dc424a06 2019-08-07 stsp while ((ch = getopt(argc, argv, "lpF:")) != -1) {
6368 715dc77e 2019-08-03 stsp switch (ch) {
6370 408b4ebc 2019-08-03 stsp list_stage = 1;
6373 dc424a06 2019-08-07 stsp pflag = 1;
6376 dc424a06 2019-08-07 stsp patch_script_path = optarg;
6379 715dc77e 2019-08-03 stsp usage_stage();
6380 715dc77e 2019-08-03 stsp /* NOTREACHED */
6384 715dc77e 2019-08-03 stsp argc -= optind;
6385 715dc77e 2019-08-03 stsp argv += optind;
6387 715dc77e 2019-08-03 stsp #ifndef PROFILE
6388 715dc77e 2019-08-03 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6389 715dc77e 2019-08-03 stsp "unveil", NULL) == -1)
6390 715dc77e 2019-08-03 stsp err(1, "pledge");
6392 2db2652d 2019-08-07 stsp if (list_stage && (pflag || patch_script_path))
6393 2db2652d 2019-08-07 stsp errx(1, "-l option cannot be used with other options");
6394 dc424a06 2019-08-07 stsp if (patch_script_path && !pflag)
6395 dc424a06 2019-08-07 stsp errx(1, "-F option can only be used together with -p option");
6397 715dc77e 2019-08-03 stsp cwd = getcwd(NULL, 0);
6398 715dc77e 2019-08-03 stsp if (cwd == NULL) {
6399 715dc77e 2019-08-03 stsp error = got_error_from_errno("getcwd");
6400 715dc77e 2019-08-03 stsp goto done;
6403 715dc77e 2019-08-03 stsp error = got_worktree_open(&worktree, cwd);
6404 715dc77e 2019-08-03 stsp if (error)
6405 715dc77e 2019-08-03 stsp goto done;
6407 715dc77e 2019-08-03 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
6408 715dc77e 2019-08-03 stsp if (error != NULL)
6409 715dc77e 2019-08-03 stsp goto done;
6411 dc424a06 2019-08-07 stsp if (patch_script_path) {
6412 dc424a06 2019-08-07 stsp patch_script_file = fopen(patch_script_path, "r");
6413 dc424a06 2019-08-07 stsp if (patch_script_file == NULL) {
6414 dc424a06 2019-08-07 stsp error = got_error_from_errno2("fopen",
6415 dc424a06 2019-08-07 stsp patch_script_path);
6416 dc424a06 2019-08-07 stsp goto done;
6419 715dc77e 2019-08-03 stsp error = apply_unveil(got_repo_get_path(repo), 1,
6420 715dc77e 2019-08-03 stsp got_worktree_get_root_path(worktree));
6421 715dc77e 2019-08-03 stsp if (error)
6422 715dc77e 2019-08-03 stsp goto done;
6424 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6425 6d022e97 2019-08-04 stsp if (error)
6426 6d022e97 2019-08-04 stsp goto done;
6428 6d022e97 2019-08-04 stsp if (list_stage)
6429 408b4ebc 2019-08-03 stsp error = got_worktree_status(worktree, &paths, repo,
6430 408b4ebc 2019-08-03 stsp print_stage, NULL, check_cancelled, NULL);
6432 2e1f37b0 2019-08-08 stsp cpa.patch_script_file = patch_script_file;
6433 2e1f37b0 2019-08-08 stsp cpa.action = "stage";
6434 dc424a06 2019-08-07 stsp error = got_worktree_stage(worktree, &paths,
6435 dc424a06 2019-08-07 stsp pflag ? NULL : print_status, NULL,
6436 2e1f37b0 2019-08-08 stsp pflag ? choose_patch : NULL, &cpa, repo);
6439 2e1f37b0 2019-08-08 stsp if (patch_script_file && fclose(patch_script_file) == EOF &&
6440 2e1f37b0 2019-08-08 stsp error == NULL)
6441 2e1f37b0 2019-08-08 stsp error = got_error_from_errno2("fclose", patch_script_path);
6443 ad493afc 2019-08-03 stsp got_repo_close(repo);
6444 ad493afc 2019-08-03 stsp if (worktree)
6445 ad493afc 2019-08-03 stsp got_worktree_close(worktree);
6446 ad493afc 2019-08-03 stsp TAILQ_FOREACH(pe, &paths, entry)
6447 ad493afc 2019-08-03 stsp free((char *)pe->path);
6448 ad493afc 2019-08-03 stsp got_pathlist_free(&paths);
6449 ad493afc 2019-08-03 stsp free(cwd);
6450 ad493afc 2019-08-03 stsp return error;
6453 ad493afc 2019-08-03 stsp __dead static void
6454 ad493afc 2019-08-03 stsp usage_unstage(void)
6456 2e1f37b0 2019-08-08 stsp fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
6457 2e1f37b0 2019-08-08 stsp "[file-path ...]\n",
6458 ad493afc 2019-08-03 stsp getprogname());
6463 ad493afc 2019-08-03 stsp static const struct got_error *
6464 ad493afc 2019-08-03 stsp cmd_unstage(int argc, char *argv[])
6466 ad493afc 2019-08-03 stsp const struct got_error *error = NULL;
6467 ad493afc 2019-08-03 stsp struct got_repository *repo = NULL;
6468 ad493afc 2019-08-03 stsp struct got_worktree *worktree = NULL;
6469 ad493afc 2019-08-03 stsp char *cwd = NULL;
6470 ad493afc 2019-08-03 stsp struct got_pathlist_head paths;
6471 ad493afc 2019-08-03 stsp struct got_pathlist_entry *pe;
6472 2e1f37b0 2019-08-08 stsp int ch, did_something = 0, pflag = 0;
6473 2e1f37b0 2019-08-08 stsp FILE *patch_script_file = NULL;
6474 2e1f37b0 2019-08-08 stsp const char *patch_script_path = NULL;
6475 2e1f37b0 2019-08-08 stsp struct choose_patch_arg cpa;
6477 ad493afc 2019-08-03 stsp TAILQ_INIT(&paths);
6479 2e1f37b0 2019-08-08 stsp while ((ch = getopt(argc, argv, "pF:")) != -1) {
6480 ad493afc 2019-08-03 stsp switch (ch) {
6482 2e1f37b0 2019-08-08 stsp pflag = 1;
6485 2e1f37b0 2019-08-08 stsp patch_script_path = optarg;
6488 ad493afc 2019-08-03 stsp usage_unstage();
6489 ad493afc 2019-08-03 stsp /* NOTREACHED */
6493 ad493afc 2019-08-03 stsp argc -= optind;
6494 ad493afc 2019-08-03 stsp argv += optind;
6496 ad493afc 2019-08-03 stsp #ifndef PROFILE
6497 ad493afc 2019-08-03 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6498 ad493afc 2019-08-03 stsp "unveil", NULL) == -1)
6499 ad493afc 2019-08-03 stsp err(1, "pledge");
6501 2e1f37b0 2019-08-08 stsp if (patch_script_path && !pflag)
6502 2e1f37b0 2019-08-08 stsp errx(1, "-F option can only be used together with -p option");
6504 ad493afc 2019-08-03 stsp cwd = getcwd(NULL, 0);
6505 ad493afc 2019-08-03 stsp if (cwd == NULL) {
6506 ad493afc 2019-08-03 stsp error = got_error_from_errno("getcwd");
6507 ad493afc 2019-08-03 stsp goto done;
6510 ad493afc 2019-08-03 stsp error = got_worktree_open(&worktree, cwd);
6511 ad493afc 2019-08-03 stsp if (error)
6512 ad493afc 2019-08-03 stsp goto done;
6514 ad493afc 2019-08-03 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
6515 ad493afc 2019-08-03 stsp if (error != NULL)
6516 ad493afc 2019-08-03 stsp goto done;
6518 2e1f37b0 2019-08-08 stsp if (patch_script_path) {
6519 2e1f37b0 2019-08-08 stsp patch_script_file = fopen(patch_script_path, "r");
6520 2e1f37b0 2019-08-08 stsp if (patch_script_file == NULL) {
6521 2e1f37b0 2019-08-08 stsp error = got_error_from_errno2("fopen",
6522 2e1f37b0 2019-08-08 stsp patch_script_path);
6523 2e1f37b0 2019-08-08 stsp goto done;
6527 ad493afc 2019-08-03 stsp error = apply_unveil(got_repo_get_path(repo), 1,
6528 ad493afc 2019-08-03 stsp got_worktree_get_root_path(worktree));
6529 ad493afc 2019-08-03 stsp if (error)
6530 ad493afc 2019-08-03 stsp goto done;
6532 ad493afc 2019-08-03 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6533 ad493afc 2019-08-03 stsp if (error)
6534 ad493afc 2019-08-03 stsp goto done;
6536 2e1f37b0 2019-08-08 stsp cpa.patch_script_file = patch_script_file;
6537 2e1f37b0 2019-08-08 stsp cpa.action = "unstage";
6538 ad493afc 2019-08-03 stsp error = got_worktree_unstage(worktree, &paths, update_progress,
6539 2e1f37b0 2019-08-08 stsp &did_something, pflag ? choose_patch : NULL, &cpa, repo);
6541 2e1f37b0 2019-08-08 stsp if (patch_script_file && fclose(patch_script_file) == EOF &&
6542 2e1f37b0 2019-08-08 stsp error == NULL)
6543 2e1f37b0 2019-08-08 stsp error = got_error_from_errno2("fclose", patch_script_path);
6545 0ebf8283 2019-07-24 stsp got_repo_close(repo);
6546 715dc77e 2019-08-03 stsp if (worktree)
6547 715dc77e 2019-08-03 stsp got_worktree_close(worktree);
6548 715dc77e 2019-08-03 stsp TAILQ_FOREACH(pe, &paths, entry)
6549 715dc77e 2019-08-03 stsp free((char *)pe->path);
6550 715dc77e 2019-08-03 stsp got_pathlist_free(&paths);
6551 715dc77e 2019-08-03 stsp free(cwd);
6552 01073a5d 2019-08-22 stsp return error;
6555 01073a5d 2019-08-22 stsp __dead static void
6556 01073a5d 2019-08-22 stsp usage_cat(void)
6558 896e9b6f 2019-08-26 stsp fprintf(stderr, "usage: %s cat [-r repository ] [ -c commit ] [ -P ] "
6559 896e9b6f 2019-08-26 stsp "arg1 [arg2 ...]\n", getprogname());
6563 01073a5d 2019-08-22 stsp static const struct got_error *
6564 01073a5d 2019-08-22 stsp cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
6566 01073a5d 2019-08-22 stsp const struct got_error *err;
6567 01073a5d 2019-08-22 stsp struct got_blob_object *blob;
6569 01073a5d 2019-08-22 stsp err = got_object_open_as_blob(&blob, repo, id, 8192);
6571 01073a5d 2019-08-22 stsp return err;
6573 01073a5d 2019-08-22 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
6574 01073a5d 2019-08-22 stsp got_object_blob_close(blob);
6575 01073a5d 2019-08-22 stsp return err;
6578 01073a5d 2019-08-22 stsp static const struct got_error *
6579 01073a5d 2019-08-22 stsp cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
6581 01073a5d 2019-08-22 stsp const struct got_error *err;
6582 01073a5d 2019-08-22 stsp struct got_tree_object *tree;
6583 01073a5d 2019-08-22 stsp const struct got_tree_entries *entries;
6584 01073a5d 2019-08-22 stsp struct got_tree_entry *te;
6586 01073a5d 2019-08-22 stsp err = got_object_open_as_tree(&tree, repo, id);
6588 01073a5d 2019-08-22 stsp return err;
6590 01073a5d 2019-08-22 stsp entries = got_object_tree_get_entries(tree);
6591 01073a5d 2019-08-22 stsp te = SIMPLEQ_FIRST(&entries->head);
6592 01073a5d 2019-08-22 stsp while (te) {
6593 01073a5d 2019-08-22 stsp char *id_str;
6594 01073a5d 2019-08-22 stsp if (sigint_received || sigpipe_received)
6596 01073a5d 2019-08-22 stsp err = got_object_id_str(&id_str, te->id);
6599 01073a5d 2019-08-22 stsp fprintf(outfile, "%s %.7o %s\n", id_str, te->mode, te->name);
6600 01073a5d 2019-08-22 stsp free(id_str);
6601 01073a5d 2019-08-22 stsp te = SIMPLEQ_NEXT(te, entry);
6604 01073a5d 2019-08-22 stsp got_object_tree_close(tree);
6605 01073a5d 2019-08-22 stsp return err;
6608 01073a5d 2019-08-22 stsp static const struct got_error *
6609 01073a5d 2019-08-22 stsp cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
6611 01073a5d 2019-08-22 stsp const struct got_error *err;
6612 01073a5d 2019-08-22 stsp struct got_commit_object *commit;
6613 01073a5d 2019-08-22 stsp const struct got_object_id_queue *parent_ids;
6614 01073a5d 2019-08-22 stsp struct got_object_qid *pid;
6615 01073a5d 2019-08-22 stsp char *id_str = NULL;
6616 24ea5512 2019-08-22 stsp const char *logmsg = NULL;
6618 01073a5d 2019-08-22 stsp err = got_object_open_as_commit(&commit, repo, id);
6620 01073a5d 2019-08-22 stsp return err;
6622 01073a5d 2019-08-22 stsp err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
6624 01073a5d 2019-08-22 stsp goto done;
6626 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str);
6627 01073a5d 2019-08-22 stsp parent_ids = got_object_commit_get_parent_ids(commit);
6628 8aa93786 2019-08-22 stsp fprintf(outfile, "numparents %d\n",
6629 01073a5d 2019-08-22 stsp got_object_commit_get_nparents(commit));
6630 01073a5d 2019-08-22 stsp SIMPLEQ_FOREACH(pid, parent_ids, entry) {
6631 01073a5d 2019-08-22 stsp char *pid_str;
6632 01073a5d 2019-08-22 stsp err = got_object_id_str(&pid_str, pid->id);
6634 01073a5d 2019-08-22 stsp goto done;
6635 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_PARENT, pid_str);
6636 01073a5d 2019-08-22 stsp free(pid_str);
6638 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_AUTHOR,
6639 8aa93786 2019-08-22 stsp got_object_commit_get_author(commit),
6640 01073a5d 2019-08-22 stsp got_object_commit_get_author_time(commit));
6642 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_COMMITTER,
6643 8aa93786 2019-08-22 stsp got_object_commit_get_author(commit),
6644 01073a5d 2019-08-22 stsp got_object_commit_get_committer_time(commit));
6646 24ea5512 2019-08-22 stsp logmsg = got_object_commit_get_logmsg_raw(commit);
6647 8aa93786 2019-08-22 stsp fprintf(outfile, "messagelen %zd\n", strlen(logmsg));
6648 01073a5d 2019-08-22 stsp fprintf(outfile, "%s", logmsg);
6650 01073a5d 2019-08-22 stsp free(id_str);
6651 01073a5d 2019-08-22 stsp got_object_commit_close(commit);
6652 01073a5d 2019-08-22 stsp return err;
6655 01073a5d 2019-08-22 stsp static const struct got_error *
6656 01073a5d 2019-08-22 stsp cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
6658 01073a5d 2019-08-22 stsp const struct got_error *err;
6659 01073a5d 2019-08-22 stsp struct got_tag_object *tag;
6660 01073a5d 2019-08-22 stsp char *id_str = NULL;
6661 01073a5d 2019-08-22 stsp const char *tagmsg = NULL;
6663 01073a5d 2019-08-22 stsp err = got_object_open_as_tag(&tag, repo, id);
6665 01073a5d 2019-08-22 stsp return err;
6667 01073a5d 2019-08-22 stsp err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
6669 01073a5d 2019-08-22 stsp goto done;
6671 e15d5241 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str);
6673 01073a5d 2019-08-22 stsp switch (got_object_tag_get_object_type(tag)) {
6674 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_BLOB:
6675 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
6676 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_BLOB);
6678 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TREE:
6679 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
6680 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_TREE);
6682 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_COMMIT:
6683 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
6684 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_COMMIT);
6686 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TAG:
6687 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
6688 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_TAG);
6694 e15d5241 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TAG,
6695 e15d5241 2019-08-22 stsp got_object_tag_get_name(tag));
6697 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s %lld +0000\n", GOT_TAG_LABEL_TAGGER,
6698 8aa93786 2019-08-22 stsp got_object_tag_get_tagger(tag),
6699 8aa93786 2019-08-22 stsp got_object_tag_get_tagger_time(tag));
6701 01073a5d 2019-08-22 stsp tagmsg = got_object_tag_get_message(tag);
6702 8aa93786 2019-08-22 stsp fprintf(outfile, "messagelen %zd\n", strlen(tagmsg));
6703 01073a5d 2019-08-22 stsp fprintf(outfile, "%s", tagmsg);
6705 01073a5d 2019-08-22 stsp free(id_str);
6706 01073a5d 2019-08-22 stsp got_object_tag_close(tag);
6707 01073a5d 2019-08-22 stsp return err;
6710 01073a5d 2019-08-22 stsp static const struct got_error *
6711 01073a5d 2019-08-22 stsp cmd_cat(int argc, char *argv[])
6713 01073a5d 2019-08-22 stsp const struct got_error *error;
6714 01073a5d 2019-08-22 stsp struct got_repository *repo = NULL;
6715 01073a5d 2019-08-22 stsp struct got_worktree *worktree = NULL;
6716 01073a5d 2019-08-22 stsp char *cwd = NULL, *repo_path = NULL, *label = NULL;
6717 896e9b6f 2019-08-26 stsp const char *commit_id_str = NULL;
6718 896e9b6f 2019-08-26 stsp struct got_object_id *id = NULL, *commit_id = NULL;
6719 896e9b6f 2019-08-26 stsp int ch, obj_type, i, force_path = 0;
6721 01073a5d 2019-08-22 stsp #ifndef PROFILE
6722 01073a5d 2019-08-22 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
6723 01073a5d 2019-08-22 stsp NULL) == -1)
6724 01073a5d 2019-08-22 stsp err(1, "pledge");
6727 896e9b6f 2019-08-26 stsp while ((ch = getopt(argc, argv, "c:r:P")) != -1) {
6728 01073a5d 2019-08-22 stsp switch (ch) {
6730 896e9b6f 2019-08-26 stsp commit_id_str = optarg;
6733 01073a5d 2019-08-22 stsp repo_path = realpath(optarg, NULL);
6734 01073a5d 2019-08-22 stsp if (repo_path == NULL)
6735 01073a5d 2019-08-22 stsp err(1, "-r option");
6736 01073a5d 2019-08-22 stsp got_path_strip_trailing_slashes(repo_path);
6739 896e9b6f 2019-08-26 stsp force_path = 1;
6742 01073a5d 2019-08-22 stsp usage_cat();
6743 01073a5d 2019-08-22 stsp /* NOTREACHED */
6747 01073a5d 2019-08-22 stsp argc -= optind;
6748 01073a5d 2019-08-22 stsp argv += optind;
6750 01073a5d 2019-08-22 stsp cwd = getcwd(NULL, 0);
6751 01073a5d 2019-08-22 stsp if (cwd == NULL) {
6752 01073a5d 2019-08-22 stsp error = got_error_from_errno("getcwd");
6753 01073a5d 2019-08-22 stsp goto done;
6755 01073a5d 2019-08-22 stsp error = got_worktree_open(&worktree, cwd);
6756 01073a5d 2019-08-22 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
6757 01073a5d 2019-08-22 stsp goto done;
6758 01073a5d 2019-08-22 stsp if (worktree) {
6759 01073a5d 2019-08-22 stsp if (repo_path == NULL) {
6760 01073a5d 2019-08-22 stsp repo_path = strdup(
6761 01073a5d 2019-08-22 stsp got_worktree_get_repo_path(worktree));
6762 01073a5d 2019-08-22 stsp if (repo_path == NULL) {
6763 01073a5d 2019-08-22 stsp error = got_error_from_errno("strdup");
6764 01073a5d 2019-08-22 stsp goto done;
6769 01073a5d 2019-08-22 stsp if (repo_path == NULL) {
6770 01073a5d 2019-08-22 stsp repo_path = getcwd(NULL, 0);
6771 01073a5d 2019-08-22 stsp if (repo_path == NULL)
6772 01073a5d 2019-08-22 stsp return got_error_from_errno("getcwd");
6775 01073a5d 2019-08-22 stsp error = got_repo_open(&repo, repo_path);
6776 01073a5d 2019-08-22 stsp free(repo_path);
6777 01073a5d 2019-08-22 stsp if (error != NULL)
6778 01073a5d 2019-08-22 stsp goto done;
6780 01073a5d 2019-08-22 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
6781 896e9b6f 2019-08-26 stsp if (error)
6782 896e9b6f 2019-08-26 stsp goto done;
6784 896e9b6f 2019-08-26 stsp if (commit_id_str == NULL)
6785 896e9b6f 2019-08-26 stsp commit_id_str = GOT_REF_HEAD;
6786 896e9b6f 2019-08-26 stsp error = resolve_commit_arg(&commit_id, commit_id_str, repo);
6787 01073a5d 2019-08-22 stsp if (error)
6788 01073a5d 2019-08-22 stsp goto done;
6790 01073a5d 2019-08-22 stsp for (i = 0; i < argc; i++) {
6791 896e9b6f 2019-08-26 stsp if (force_path) {
6792 896e9b6f 2019-08-26 stsp error = got_object_id_by_path(&id, repo, commit_id,
6794 896e9b6f 2019-08-26 stsp if (error)
6797 896e9b6f 2019-08-26 stsp error = match_object_id(&id, &label, argv[i],
6798 896e9b6f 2019-08-26 stsp GOT_OBJ_TYPE_ANY, 0, repo);
6799 896e9b6f 2019-08-26 stsp if (error) {
6800 896e9b6f 2019-08-26 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR &&
6801 896e9b6f 2019-08-26 stsp error->code != GOT_ERR_NOT_REF)
6803 896e9b6f 2019-08-26 stsp error = got_object_id_by_path(&id, repo,
6804 896e9b6f 2019-08-26 stsp commit_id, argv[i]);
6805 896e9b6f 2019-08-26 stsp if (error)
6810 01073a5d 2019-08-22 stsp error = got_object_get_type(&obj_type, repo, id);
6811 01073a5d 2019-08-22 stsp if (error)
6814 01073a5d 2019-08-22 stsp switch (obj_type) {
6815 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_BLOB:
6816 01073a5d 2019-08-22 stsp error = cat_blob(id, repo, stdout);
6818 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TREE:
6819 01073a5d 2019-08-22 stsp error = cat_tree(id, repo, stdout);
6821 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_COMMIT:
6822 01073a5d 2019-08-22 stsp error = cat_commit(id, repo, stdout);
6824 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TAG:
6825 01073a5d 2019-08-22 stsp error = cat_tag(id, repo, stdout);
6828 01073a5d 2019-08-22 stsp error = got_error(GOT_ERR_OBJ_TYPE);
6831 01073a5d 2019-08-22 stsp if (error)
6833 01073a5d 2019-08-22 stsp free(label);
6834 01073a5d 2019-08-22 stsp label = NULL;
6836 01073a5d 2019-08-22 stsp id = NULL;
6840 01073a5d 2019-08-22 stsp free(label);
6842 896e9b6f 2019-08-26 stsp free(commit_id);
6843 01073a5d 2019-08-22 stsp if (worktree)
6844 01073a5d 2019-08-22 stsp got_worktree_close(worktree);
6845 01073a5d 2019-08-22 stsp if (repo) {
6846 01073a5d 2019-08-22 stsp const struct got_error *repo_error;
6847 01073a5d 2019-08-22 stsp repo_error = got_repo_close(repo);
6848 01073a5d 2019-08-22 stsp if (error == NULL)
6849 01073a5d 2019-08-22 stsp error = repo_error;
6851 0ebf8283 2019-07-24 stsp return error;