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 c09a553d 2018-03-12 stsp #include "got_worktree.h"
45 79109fed 2018-03-27 stsp #include "got_diff.h"
46 372ccdbb 2018-06-10 stsp #include "got_commit_graph.h"
47 404c43c4 2018-06-21 stsp #include "got_blame.h"
48 63219cd2 2019-01-04 stsp #include "got_privsep.h"
49 793c30b5 2019-05-13 stsp #include "got_opentemp.h"
51 5c860e29 2018-03-12 stsp #ifndef nitems
52 5c860e29 2018-03-12 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
55 99437157 2018-11-11 stsp static volatile sig_atomic_t sigint_received;
56 99437157 2018-11-11 stsp static volatile sig_atomic_t sigpipe_received;
59 99437157 2018-11-11 stsp catch_sigint(int signo)
61 99437157 2018-11-11 stsp sigint_received = 1;
65 99437157 2018-11-11 stsp catch_sigpipe(int signo)
67 99437157 2018-11-11 stsp sigpipe_received = 1;
71 8cfb4057 2019-07-09 stsp struct got_cmd {
72 5c860e29 2018-03-12 stsp const char *cmd_name;
73 d7d4f210 2018-03-12 stsp const struct got_error *(*cmd_main)(int, char *[]);
74 1b6b95a8 2018-03-12 stsp void (*cmd_usage)(void);
75 97b3a7be 2019-07-09 stsp const char *cmd_alias;
78 ce5b7c56 2019-07-09 stsp __dead static void usage(int);
79 2c7829a4 2019-06-17 stsp __dead static void usage_init(void);
80 3ce1b845 2019-07-15 stsp __dead static void usage_import(void);
81 4ed7e80c 2018-05-20 stsp __dead static void usage_checkout(void);
82 507dc3bb 2018-12-29 stsp __dead static void usage_update(void);
83 4ed7e80c 2018-05-20 stsp __dead static void usage_log(void);
84 4ed7e80c 2018-05-20 stsp __dead static void usage_diff(void);
85 404c43c4 2018-06-21 stsp __dead static void usage_blame(void);
86 5de5890b 2018-10-18 stsp __dead static void usage_tree(void);
87 6bad629b 2019-02-04 stsp __dead static void usage_status(void);
88 d0eebce4 2019-03-11 stsp __dead static void usage_ref(void);
89 4e759de4 2019-06-26 stsp __dead static void usage_branch(void);
90 d00136be 2019-03-26 stsp __dead static void usage_add(void);
91 648e4ef7 2019-07-09 stsp __dead static void usage_remove(void);
92 a129376b 2019-03-28 stsp __dead static void usage_revert(void);
93 c4296144 2019-05-09 stsp __dead static void usage_commit(void);
94 234035bc 2019-06-01 stsp __dead static void usage_cherrypick(void);
95 5ef14e63 2019-06-02 stsp __dead static void usage_backout(void);
96 818c7501 2019-07-11 stsp __dead static void usage_rebase(void);
97 0ebf8283 2019-07-24 stsp __dead static void usage_histedit(void);
98 715dc77e 2019-08-03 stsp __dead static void usage_stage(void);
99 ad493afc 2019-08-03 stsp __dead static void usage_unstage(void);
101 2c7829a4 2019-06-17 stsp static const struct got_error* cmd_init(int, char *[]);
102 3ce1b845 2019-07-15 stsp static const struct got_error* cmd_import(int, char *[]);
103 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_checkout(int, char *[]);
104 507dc3bb 2018-12-29 stsp static const struct got_error* cmd_update(int, char *[]);
105 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_log(int, char *[]);
106 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_diff(int, char *[]);
107 404c43c4 2018-06-21 stsp static const struct got_error* cmd_blame(int, char *[]);
108 5de5890b 2018-10-18 stsp static const struct got_error* cmd_tree(int, char *[]);
109 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_status(int, char *[]);
110 d0eebce4 2019-03-11 stsp static const struct got_error* cmd_ref(int, char *[]);
111 4e759de4 2019-06-26 stsp static const struct got_error* cmd_branch(int, char *[]);
112 d00136be 2019-03-26 stsp static const struct got_error* cmd_add(int, char *[]);
113 648e4ef7 2019-07-09 stsp static const struct got_error* cmd_remove(int, char *[]);
114 a129376b 2019-03-28 stsp static const struct got_error* cmd_revert(int, char *[]);
115 c4296144 2019-05-09 stsp static const struct got_error* cmd_commit(int, char *[]);
116 234035bc 2019-06-01 stsp static const struct got_error* cmd_cherrypick(int, char *[]);
117 5ef14e63 2019-06-02 stsp static const struct got_error* cmd_backout(int, char *[]);
118 818c7501 2019-07-11 stsp static const struct got_error* cmd_rebase(int, char *[]);
119 0ebf8283 2019-07-24 stsp static const struct got_error* cmd_histedit(int, char *[]);
120 715dc77e 2019-08-03 stsp static const struct got_error* cmd_stage(int, char *[]);
121 ad493afc 2019-08-03 stsp static const struct got_error* cmd_unstage(int, char *[]);
123 8cfb4057 2019-07-09 stsp static struct got_cmd got_commands[] = {
124 bc26cce8 2019-08-04 stsp { "init", cmd_init, usage_init, "in" },
125 bc26cce8 2019-08-04 stsp { "import", cmd_import, usage_import, "im" },
126 97b3a7be 2019-07-09 stsp { "checkout", cmd_checkout, usage_checkout, "co" },
127 97b3a7be 2019-07-09 stsp { "update", cmd_update, usage_update, "up" },
128 97b3a7be 2019-07-09 stsp { "log", cmd_log, usage_log, "" },
129 bc26cce8 2019-08-04 stsp { "diff", cmd_diff, usage_diff, "di" },
130 bc26cce8 2019-08-04 stsp { "blame", cmd_blame, usage_blame, "bl" },
131 bc26cce8 2019-08-04 stsp { "tree", cmd_tree, usage_tree, "tr" },
132 97b3a7be 2019-07-09 stsp { "status", cmd_status, usage_status, "st" },
133 97b3a7be 2019-07-09 stsp { "ref", cmd_ref, usage_ref, "" },
134 97b3a7be 2019-07-09 stsp { "branch", cmd_branch, usage_branch, "br" },
135 97b3a7be 2019-07-09 stsp { "add", cmd_add, usage_add, "" },
136 648e4ef7 2019-07-09 stsp { "remove", cmd_remove, usage_remove, "rm" },
137 97b3a7be 2019-07-09 stsp { "revert", cmd_revert, usage_revert, "rv" },
138 97b3a7be 2019-07-09 stsp { "commit", cmd_commit, usage_commit, "ci" },
139 016477fd 2019-07-09 stsp { "cherrypick", cmd_cherrypick, usage_cherrypick, "cy" },
140 97b3a7be 2019-07-09 stsp { "backout", cmd_backout, usage_backout, "bo" },
141 818c7501 2019-07-11 stsp { "rebase", cmd_rebase, usage_rebase, "rb" },
142 0ebf8283 2019-07-24 stsp { "histedit", cmd_histedit, usage_histedit, "he" },
143 715dc77e 2019-08-03 stsp { "stage", cmd_stage, usage_stage, "sg" },
144 ad493afc 2019-08-03 stsp { "unstage", cmd_unstage, usage_unstage, "ug" },
147 ce5b7c56 2019-07-09 stsp static void
148 ce5b7c56 2019-07-09 stsp list_commands(void)
152 ce5b7c56 2019-07-09 stsp fprintf(stderr, "commands:");
153 ce5b7c56 2019-07-09 stsp for (i = 0; i < nitems(got_commands); i++) {
154 ce5b7c56 2019-07-09 stsp struct got_cmd *cmd = &got_commands[i];
155 ce5b7c56 2019-07-09 stsp fprintf(stderr, " %s", cmd->cmd_name);
157 ce5b7c56 2019-07-09 stsp fputc('\n', stderr);
161 5c860e29 2018-03-12 stsp main(int argc, char *argv[])
163 8cfb4057 2019-07-09 stsp struct got_cmd *cmd;
164 5c860e29 2018-03-12 stsp unsigned int i;
166 53ccebc2 2019-07-30 stsp int hflag = 0, Vflag = 0;
168 289e3cbf 2019-02-04 stsp setlocale(LC_CTYPE, "");
170 53ccebc2 2019-07-30 stsp while ((ch = getopt(argc, argv, "hV")) != -1) {
171 5c860e29 2018-03-12 stsp switch (ch) {
179 ce5b7c56 2019-07-09 stsp usage(hflag);
180 5c860e29 2018-03-12 stsp /* NOTREACHED */
184 5c860e29 2018-03-12 stsp argc -= optind;
185 5c860e29 2018-03-12 stsp argv += optind;
186 1e70621d 2018-03-27 stsp optind = 0;
188 53ccebc2 2019-07-30 stsp if (Vflag) {
189 53ccebc2 2019-07-30 stsp got_version_print_str();
193 5c860e29 2018-03-12 stsp if (argc <= 0)
194 ce5b7c56 2019-07-09 stsp usage(hflag);
196 99437157 2018-11-11 stsp signal(SIGINT, catch_sigint);
197 99437157 2018-11-11 stsp signal(SIGPIPE, catch_sigpipe);
199 5c860e29 2018-03-12 stsp for (i = 0; i < nitems(got_commands); i++) {
200 d7d4f210 2018-03-12 stsp const struct got_error *error;
202 5c860e29 2018-03-12 stsp cmd = &got_commands[i];
204 97b3a7be 2019-07-09 stsp if (strcmp(cmd->cmd_name, argv[0]) != 0 &&
205 97b3a7be 2019-07-09 stsp strcmp(cmd->cmd_alias, argv[0]) != 0)
209 1b6b95a8 2018-03-12 stsp got_commands[i].cmd_usage();
211 d7d4f210 2018-03-12 stsp error = got_commands[i].cmd_main(argc, argv);
212 80d5f134 2018-11-11 stsp if (error && !(sigint_received || sigpipe_received)) {
213 d7d4f210 2018-03-12 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
220 20ecf764 2018-03-12 stsp fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
221 ce5b7c56 2019-07-09 stsp list_commands();
225 4ed7e80c 2018-05-20 stsp __dead static void
226 ce5b7c56 2019-07-09 stsp usage(int hflag)
228 53ccebc2 2019-07-30 stsp fprintf(stderr, "usage: %s [-h] [-V] command [arg ...]\n",
229 53ccebc2 2019-07-30 stsp getprogname());
231 ce5b7c56 2019-07-09 stsp list_commands();
235 0266afb7 2019-01-04 stsp static const struct got_error *
236 0ee7065d 2019-05-13 stsp get_editor(char **abspath)
238 0ee7065d 2019-05-13 stsp const struct got_error *err = NULL;
239 e2ba3d07 2019-05-13 stsp const char *editor;
241 8920fa04 2019-08-18 stsp *abspath = NULL;
243 e2ba3d07 2019-05-13 stsp editor = getenv("VISUAL");
244 e2ba3d07 2019-05-13 stsp if (editor == NULL)
245 e2ba3d07 2019-05-13 stsp editor = getenv("EDITOR");
247 0ee7065d 2019-05-13 stsp if (editor) {
248 0ee7065d 2019-05-13 stsp err = got_path_find_prog(abspath, editor);
250 0ee7065d 2019-05-13 stsp return err;
253 0ee7065d 2019-05-13 stsp if (*abspath == NULL) {
254 0ee7065d 2019-05-13 stsp *abspath = strdup("/bin/ed");
255 0ee7065d 2019-05-13 stsp if (*abspath == NULL)
256 0ee7065d 2019-05-13 stsp return got_error_from_errno("strdup");
259 e2ba3d07 2019-05-13 stsp return NULL;
262 e2ba3d07 2019-05-13 stsp static const struct got_error *
263 d0eebce4 2019-03-11 stsp apply_unveil(const char *repo_path, int repo_read_only,
264 c530dc23 2019-07-23 stsp const char *worktree_path)
266 163ce85a 2019-05-13 stsp const struct got_error *err;
268 37c06ea4 2019-07-15 stsp #ifdef PROFILE
269 37c06ea4 2019-07-15 stsp if (unveil("gmon.out", "rwc") != 0)
270 37c06ea4 2019-07-15 stsp return got_error_from_errno2("unveil", "gmon.out");
272 d0eebce4 2019-03-11 stsp if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
273 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", repo_path);
275 0266afb7 2019-01-04 stsp if (worktree_path && unveil(worktree_path, "rwc") != 0)
276 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", worktree_path);
278 f12d0dbe 2019-01-04 stsp if (unveil("/tmp", "rwc") != 0)
279 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", "/tmp");
281 163ce85a 2019-05-13 stsp err = got_privsep_unveil_exec_helpers();
282 163ce85a 2019-05-13 stsp if (err != NULL)
283 163ce85a 2019-05-13 stsp return err;
285 0266afb7 2019-01-04 stsp if (unveil(NULL, NULL) != 0)
286 638f9024 2019-05-13 stsp return got_error_from_errno("unveil");
288 0266afb7 2019-01-04 stsp return NULL;
291 2c7829a4 2019-06-17 stsp __dead static void
292 2c7829a4 2019-06-17 stsp usage_init(void)
294 09ea71ba 2019-07-27 stsp fprintf(stderr, "usage: %s init repository-path\n", getprogname());
298 2c7829a4 2019-06-17 stsp static const struct got_error *
299 2c7829a4 2019-06-17 stsp cmd_init(int argc, char *argv[])
301 2c7829a4 2019-06-17 stsp const struct got_error *error = NULL;
302 2c7829a4 2019-06-17 stsp char *repo_path = NULL;
305 2c7829a4 2019-06-17 stsp while ((ch = getopt(argc, argv, "")) != -1) {
306 2c7829a4 2019-06-17 stsp switch (ch) {
308 2c7829a4 2019-06-17 stsp usage_init();
309 2c7829a4 2019-06-17 stsp /* NOTREACHED */
313 2c7829a4 2019-06-17 stsp argc -= optind;
314 2c7829a4 2019-06-17 stsp argv += optind;
316 2c7829a4 2019-06-17 stsp #ifndef PROFILE
317 2c7829a4 2019-06-17 stsp if (pledge("stdio rpath wpath cpath unveil", NULL) == -1)
318 2c7829a4 2019-06-17 stsp err(1, "pledge");
320 2c7829a4 2019-06-17 stsp if (argc != 1)
321 bc20e173 2019-06-17 stsp usage_init();
323 2c7829a4 2019-06-17 stsp repo_path = strdup(argv[0]);
324 2c7829a4 2019-06-17 stsp if (repo_path == NULL)
325 2c7829a4 2019-06-17 stsp return got_error_from_errno("strdup");
327 2c7829a4 2019-06-17 stsp got_path_strip_trailing_slashes(repo_path);
329 2c7829a4 2019-06-17 stsp error = got_path_mkdir(repo_path);
330 2c7829a4 2019-06-17 stsp if (error &&
331 2c7829a4 2019-06-17 stsp !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
334 c530dc23 2019-07-23 stsp error = apply_unveil(repo_path, 0, NULL);
338 2c7829a4 2019-06-17 stsp error = got_repo_init(repo_path);
339 2c7829a4 2019-06-17 stsp if (error != NULL)
343 3ce1b845 2019-07-15 stsp free(repo_path);
344 3ce1b845 2019-07-15 stsp return error;
347 3ce1b845 2019-07-15 stsp __dead static void
348 3ce1b845 2019-07-15 stsp usage_import(void)
350 3ce1b845 2019-07-15 stsp fprintf(stderr, "usage: %s import [-b branch] [-m message] "
351 3ce1b845 2019-07-15 stsp "[-r repository-path] [-I pattern] path\n", getprogname());
356 3ce1b845 2019-07-15 stsp spawn_editor(const char *editor, const char *file)
359 3ce1b845 2019-07-15 stsp sig_t sighup, sigint, sigquit;
360 3ce1b845 2019-07-15 stsp int st = -1;
362 3ce1b845 2019-07-15 stsp sighup = signal(SIGHUP, SIG_IGN);
363 3ce1b845 2019-07-15 stsp sigint = signal(SIGINT, SIG_IGN);
364 3ce1b845 2019-07-15 stsp sigquit = signal(SIGQUIT, SIG_IGN);
366 3ce1b845 2019-07-15 stsp switch (pid = fork()) {
368 3ce1b845 2019-07-15 stsp goto doneediting;
370 3ce1b845 2019-07-15 stsp execl(editor, editor, file, (char *)NULL);
371 3ce1b845 2019-07-15 stsp _exit(127);
374 3ce1b845 2019-07-15 stsp while (waitpid(pid, &st, 0) == -1)
375 3ce1b845 2019-07-15 stsp if (errno != EINTR)
378 3ce1b845 2019-07-15 stsp doneediting:
379 3ce1b845 2019-07-15 stsp (void)signal(SIGHUP, sighup);
380 3ce1b845 2019-07-15 stsp (void)signal(SIGINT, sigint);
381 3ce1b845 2019-07-15 stsp (void)signal(SIGQUIT, sigquit);
383 3ce1b845 2019-07-15 stsp if (!WIFEXITED(st)) {
384 3ce1b845 2019-07-15 stsp errno = EINTR;
388 3ce1b845 2019-07-15 stsp return WEXITSTATUS(st);
391 3ce1b845 2019-07-15 stsp static const struct got_error *
392 3ce1b845 2019-07-15 stsp edit_logmsg(char **logmsg, const char *editor, const char *logmsg_path,
393 3ce1b845 2019-07-15 stsp const char *initial_content)
395 3ce1b845 2019-07-15 stsp const struct got_error *err = NULL;
396 3ce1b845 2019-07-15 stsp char buf[1024];
397 3ce1b845 2019-07-15 stsp struct stat st, st2;
399 3ce1b845 2019-07-15 stsp int content_changed = 0;
400 3ce1b845 2019-07-15 stsp size_t len;
402 3ce1b845 2019-07-15 stsp *logmsg = NULL;
404 3ce1b845 2019-07-15 stsp if (stat(logmsg_path, &st) == -1)
405 3ce1b845 2019-07-15 stsp return got_error_from_errno2("stat", logmsg_path);
407 3ce1b845 2019-07-15 stsp if (spawn_editor(editor, logmsg_path) == -1)
408 3ce1b845 2019-07-15 stsp return got_error_from_errno("failed spawning editor");
410 3ce1b845 2019-07-15 stsp if (stat(logmsg_path, &st2) == -1)
411 3ce1b845 2019-07-15 stsp return got_error_from_errno("stat");
413 3ce1b845 2019-07-15 stsp if (st.st_mtime == st2.st_mtime && st.st_size == st2.st_size)
414 3ce1b845 2019-07-15 stsp return got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
415 3ce1b845 2019-07-15 stsp "no changes made to commit message, aborting");
417 3ce1b845 2019-07-15 stsp *logmsg = malloc(st2.st_size + 1);
418 3ce1b845 2019-07-15 stsp if (*logmsg == NULL)
419 3ce1b845 2019-07-15 stsp return got_error_from_errno("malloc");
420 3ce1b845 2019-07-15 stsp (*logmsg)[0] = '\0';
423 3ce1b845 2019-07-15 stsp fp = fopen(logmsg_path, "r");
424 3ce1b845 2019-07-15 stsp if (fp == NULL) {
425 3ce1b845 2019-07-15 stsp err = got_error_from_errno("fopen");
428 3ce1b845 2019-07-15 stsp while (fgets(buf, sizeof(buf), fp) != NULL) {
429 3ce1b845 2019-07-15 stsp if (!content_changed && strcmp(buf, initial_content) != 0)
430 3ce1b845 2019-07-15 stsp content_changed = 1;
431 3ce1b845 2019-07-15 stsp if (buf[0] == '#' || (len == 0 && buf[0] == '\n'))
432 3ce1b845 2019-07-15 stsp continue; /* remove comments and leading empty lines */
433 3ce1b845 2019-07-15 stsp len = strlcat(*logmsg, buf, st2.st_size);
435 3ce1b845 2019-07-15 stsp fclose(fp);
437 3ce1b845 2019-07-15 stsp while (len > 0 && (*logmsg)[len - 1] == '\n') {
438 3ce1b845 2019-07-15 stsp (*logmsg)[len - 1] = '\0';
442 3ce1b845 2019-07-15 stsp if (len == 0 || !content_changed)
443 3ce1b845 2019-07-15 stsp err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
444 3ce1b845 2019-07-15 stsp "commit message cannot be empty, aborting");
447 3ce1b845 2019-07-15 stsp free(*logmsg);
448 3ce1b845 2019-07-15 stsp *logmsg = NULL;
450 3ce1b845 2019-07-15 stsp return err;
453 3ce1b845 2019-07-15 stsp static const struct got_error *
454 3ce1b845 2019-07-15 stsp collect_import_msg(char **logmsg, const char *editor, const char *path_dir,
455 3ce1b845 2019-07-15 stsp const char *branch_name)
457 3ce1b845 2019-07-15 stsp char *initial_content = NULL, *logmsg_path = NULL;
458 3ce1b845 2019-07-15 stsp const struct got_error *err = NULL;
461 3ce1b845 2019-07-15 stsp if (asprintf(&initial_content,
462 3ce1b845 2019-07-15 stsp "\n# %s to be imported to branch %s\n", path_dir,
463 3ce1b845 2019-07-15 stsp branch_name) == -1)
464 3ce1b845 2019-07-15 stsp return got_error_from_errno("asprintf");
466 3ce1b845 2019-07-15 stsp err = got_opentemp_named_fd(&logmsg_path, &fd, "/tmp/got-importmsg");
470 3ce1b845 2019-07-15 stsp dprintf(fd, initial_content);
473 3ce1b845 2019-07-15 stsp err = edit_logmsg(logmsg, editor, logmsg_path, initial_content);
475 3ce1b845 2019-07-15 stsp free(initial_content);
476 3ce1b845 2019-07-15 stsp free(logmsg_path);
477 3ce1b845 2019-07-15 stsp return err;
480 3ce1b845 2019-07-15 stsp static const struct got_error *
481 3ce1b845 2019-07-15 stsp import_progress(void *arg, const char *path)
483 3ce1b845 2019-07-15 stsp printf("A %s\n", path);
484 3ce1b845 2019-07-15 stsp return NULL;
487 3ce1b845 2019-07-15 stsp static const struct got_error *
488 84792843 2019-08-09 stsp get_author(const char **author)
490 84792843 2019-08-09 stsp const char *got_author;
492 84792843 2019-08-09 stsp *author = NULL;
494 84792843 2019-08-09 stsp got_author = getenv("GOT_AUTHOR");
495 84792843 2019-08-09 stsp if (got_author == NULL) {
496 84792843 2019-08-09 stsp /* TODO: Look up user in password database? */
497 84792843 2019-08-09 stsp return got_error(GOT_ERR_COMMIT_NO_AUTHOR);
500 84792843 2019-08-09 stsp *author = got_author;
503 84792843 2019-08-09 stsp * Really dumb email address check; we're only doing this to
504 84792843 2019-08-09 stsp * avoid git's object parser breaking on commits we create.
506 84792843 2019-08-09 stsp while (*got_author && *got_author != '<')
507 84792843 2019-08-09 stsp got_author++;
508 84792843 2019-08-09 stsp if (*got_author != '<')
509 84792843 2019-08-09 stsp return got_error(GOT_ERR_COMMIT_NO_EMAIL);
510 84792843 2019-08-09 stsp while (*got_author && *got_author != '@')
511 84792843 2019-08-09 stsp got_author++;
512 84792843 2019-08-09 stsp if (*got_author != '@')
513 84792843 2019-08-09 stsp return got_error(GOT_ERR_COMMIT_NO_EMAIL);
514 84792843 2019-08-09 stsp while (*got_author && *got_author != '>')
515 84792843 2019-08-09 stsp got_author++;
516 84792843 2019-08-09 stsp if (*got_author != '>')
517 84792843 2019-08-09 stsp return got_error(GOT_ERR_COMMIT_NO_EMAIL);
519 84792843 2019-08-09 stsp return NULL;
522 84792843 2019-08-09 stsp static const struct got_error *
523 3ce1b845 2019-07-15 stsp cmd_import(int argc, char *argv[])
525 3ce1b845 2019-07-15 stsp const struct got_error *error = NULL;
526 3ce1b845 2019-07-15 stsp char *path_dir = NULL, *repo_path = NULL, *logmsg = NULL;
527 3ce1b845 2019-07-15 stsp char *editor = NULL;
528 84792843 2019-08-09 stsp const char *author;
529 3ce1b845 2019-07-15 stsp const char *branch_name = "master";
530 3ce1b845 2019-07-15 stsp char *refname = NULL, *id_str = NULL;
531 3ce1b845 2019-07-15 stsp struct got_repository *repo = NULL;
532 3ce1b845 2019-07-15 stsp struct got_reference *branch_ref = NULL, *head_ref = NULL;
533 3ce1b845 2019-07-15 stsp struct got_object_id *new_commit_id = NULL;
535 3ce1b845 2019-07-15 stsp struct got_pathlist_head ignores;
536 3ce1b845 2019-07-15 stsp struct got_pathlist_entry *pe;
538 3ce1b845 2019-07-15 stsp TAILQ_INIT(&ignores);
540 3ce1b845 2019-07-15 stsp while ((ch = getopt(argc, argv, "b:m:r:I:")) != -1) {
541 3ce1b845 2019-07-15 stsp switch (ch) {
543 3ce1b845 2019-07-15 stsp branch_name = optarg;
546 3ce1b845 2019-07-15 stsp logmsg = strdup(optarg);
547 3ce1b845 2019-07-15 stsp if (logmsg == NULL) {
548 3ce1b845 2019-07-15 stsp error = got_error_from_errno("strdup");
553 3ce1b845 2019-07-15 stsp repo_path = realpath(optarg, NULL);
554 3ce1b845 2019-07-15 stsp if (repo_path == NULL) {
555 3ce1b845 2019-07-15 stsp error = got_error_from_errno("realpath");
560 3ce1b845 2019-07-15 stsp if (optarg[0] == '\0')
562 3ce1b845 2019-07-15 stsp error = got_pathlist_insert(&pe, &ignores, optarg,
568 3ce1b845 2019-07-15 stsp usage_init();
569 3ce1b845 2019-07-15 stsp /* NOTREACHED */
573 3ce1b845 2019-07-15 stsp argc -= optind;
574 3ce1b845 2019-07-15 stsp argv += optind;
576 3ce1b845 2019-07-15 stsp #ifndef PROFILE
577 3ce1b845 2019-07-15 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec unveil",
578 3ce1b845 2019-07-15 stsp NULL) == -1)
579 3ce1b845 2019-07-15 stsp err(1, "pledge");
581 3ce1b845 2019-07-15 stsp if (argc != 1)
582 3ce1b845 2019-07-15 stsp usage_import();
584 84792843 2019-08-09 stsp error = get_author(&author);
586 84792843 2019-08-09 stsp return error;
588 3ce1b845 2019-07-15 stsp if (repo_path == NULL) {
589 3ce1b845 2019-07-15 stsp repo_path = getcwd(NULL, 0);
590 3ce1b845 2019-07-15 stsp if (repo_path == NULL)
591 3ce1b845 2019-07-15 stsp return got_error_from_errno("getcwd");
593 3ce1b845 2019-07-15 stsp got_path_strip_trailing_slashes(repo_path);
594 3ce1b845 2019-07-15 stsp error = got_repo_open(&repo, repo_path);
598 3ce1b845 2019-07-15 stsp if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
599 3ce1b845 2019-07-15 stsp error = got_error_from_errno("asprintf");
603 3ce1b845 2019-07-15 stsp error = got_ref_open(&branch_ref, repo, refname, 0);
604 3ce1b845 2019-07-15 stsp if (error) {
605 3ce1b845 2019-07-15 stsp if (error->code != GOT_ERR_NOT_REF)
608 3ce1b845 2019-07-15 stsp error = got_error_msg(GOT_ERR_BRANCH_EXISTS,
609 3ce1b845 2019-07-15 stsp "import target branch already exists");
613 3ce1b845 2019-07-15 stsp path_dir = realpath(argv[0], NULL);
614 3ce1b845 2019-07-15 stsp if (path_dir == NULL) {
615 3ce1b845 2019-07-15 stsp error = got_error_from_errno("realpath");
618 3ce1b845 2019-07-15 stsp got_path_strip_trailing_slashes(path_dir);
621 3ce1b845 2019-07-15 stsp * unveil(2) traverses exec(2); if an editor is used we have
622 3ce1b845 2019-07-15 stsp * to apply unveil after the log message has been written.
624 3ce1b845 2019-07-15 stsp if (logmsg == NULL || strlen(logmsg) == 0) {
625 3ce1b845 2019-07-15 stsp error = get_editor(&editor);
628 3ce1b845 2019-07-15 stsp error = collect_import_msg(&logmsg, editor, path_dir, refname);
633 3ce1b845 2019-07-15 stsp if (unveil(path_dir, "r") != 0)
634 3ce1b845 2019-07-15 stsp return got_error_from_errno2("unveil", path_dir);
636 c530dc23 2019-07-23 stsp error = apply_unveil(got_repo_get_path(repo), 0, NULL);
640 3ce1b845 2019-07-15 stsp error = got_repo_import(&new_commit_id, path_dir, logmsg,
641 84792843 2019-08-09 stsp author, &ignores, repo, import_progress, NULL);
645 3ce1b845 2019-07-15 stsp error = got_ref_alloc(&branch_ref, refname, new_commit_id);
649 3ce1b845 2019-07-15 stsp error = got_ref_write(branch_ref, repo);
653 3ce1b845 2019-07-15 stsp error = got_object_id_str(&id_str, new_commit_id);
657 3ce1b845 2019-07-15 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
658 3ce1b845 2019-07-15 stsp if (error) {
659 3ce1b845 2019-07-15 stsp if (error->code != GOT_ERR_NOT_REF)
662 3ce1b845 2019-07-15 stsp error = got_ref_alloc_symref(&head_ref, GOT_REF_HEAD,
663 3ce1b845 2019-07-15 stsp branch_ref);
667 3ce1b845 2019-07-15 stsp error = got_ref_write(head_ref, repo);
672 3ce1b845 2019-07-15 stsp printf("Created branch %s with commit %s\n",
673 3ce1b845 2019-07-15 stsp got_ref_get_name(branch_ref), id_str);
675 2c7829a4 2019-06-17 stsp free(repo_path);
676 3ce1b845 2019-07-15 stsp free(editor);
677 3ce1b845 2019-07-15 stsp free(refname);
678 3ce1b845 2019-07-15 stsp free(new_commit_id);
679 3ce1b845 2019-07-15 stsp free(id_str);
680 3ce1b845 2019-07-15 stsp if (branch_ref)
681 3ce1b845 2019-07-15 stsp got_ref_close(branch_ref);
682 3ce1b845 2019-07-15 stsp if (head_ref)
683 3ce1b845 2019-07-15 stsp got_ref_close(head_ref);
684 2c7829a4 2019-06-17 stsp return error;
687 4ed7e80c 2018-05-20 stsp __dead static void
688 c09a553d 2018-03-12 stsp usage_checkout(void)
690 08573d5b 2019-05-14 stsp fprintf(stderr, "usage: %s checkout [-b branch] [-c commit] "
691 08573d5b 2019-05-14 stsp "[-p prefix] repository-path [worktree-path]\n", getprogname());
695 1ee397ad 2019-07-12 stsp static const struct got_error *
696 a0eb853d 2018-12-29 stsp checkout_progress(void *arg, unsigned char status, const char *path)
698 92a684f4 2018-03-12 stsp char *worktree_path = arg;
700 a484d721 2019-06-10 stsp /* Base commit bump happens silently. */
701 a484d721 2019-06-10 stsp if (status == GOT_STATUS_BUMP_BASE)
702 1ee397ad 2019-07-12 stsp return NULL;
704 92a684f4 2018-03-12 stsp while (path[0] == '/')
707 d7b62c98 2018-12-27 stsp printf("%c %s/%s\n", status, worktree_path, path);
708 1ee397ad 2019-07-12 stsp return NULL;
711 99437157 2018-11-11 stsp static const struct got_error *
712 6bad629b 2019-02-04 stsp check_cancelled(void *arg)
714 99437157 2018-11-11 stsp if (sigint_received || sigpipe_received)
715 99437157 2018-11-11 stsp return got_error(GOT_ERR_CANCELLED);
716 99437157 2018-11-11 stsp return NULL;
719 8069f636 2019-01-12 stsp static const struct got_error *
720 024e9686 2019-05-14 stsp check_linear_ancestry(struct got_object_id *commit_id,
721 024e9686 2019-05-14 stsp struct got_object_id *base_commit_id, struct got_repository *repo)
723 d5bea539 2019-05-13 stsp const struct got_error *err = NULL;
724 024e9686 2019-05-14 stsp struct got_object_id *yca_id;
726 d5bea539 2019-05-13 stsp err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
727 d5bea539 2019-05-13 stsp commit_id, base_commit_id, repo);
729 36a38700 2019-05-10 stsp return err;
731 d5bea539 2019-05-13 stsp if (yca_id == NULL)
732 d5bea539 2019-05-13 stsp return got_error(GOT_ERR_ANCESTRY);
735 d5bea539 2019-05-13 stsp * Require a straight line of history between the target commit
736 d5bea539 2019-05-13 stsp * and the work tree's base commit.
738 d5751d49 2019-05-14 stsp * Non-linear situations such as this require a rebase:
740 d5bea539 2019-05-13 stsp * (commit) D F (base_commit)
748 d5bea539 2019-05-13 stsp * 'got update' only handles linear cases:
749 d5bea539 2019-05-13 stsp * Update forwards in time: A (base/yca) - B - C - D (commit)
750 efa2b6f7 2019-05-14 stsp * Update backwards in time: D (base) - C - B - A (commit/yca)
752 d5bea539 2019-05-13 stsp if (got_object_id_cmp(commit_id, yca_id) != 0 &&
753 d5bea539 2019-05-13 stsp got_object_id_cmp(base_commit_id, yca_id) != 0)
754 d5bea539 2019-05-13 stsp return got_error(GOT_ERR_ANCESTRY);
756 d5bea539 2019-05-13 stsp free(yca_id);
757 d5bea539 2019-05-13 stsp return NULL;
760 a367ff0f 2019-05-14 stsp static const struct got_error *
761 a367ff0f 2019-05-14 stsp check_same_branch(struct got_object_id *commit_id,
762 a51a74b3 2019-07-27 stsp struct got_reference *head_ref, struct got_object_id *yca_id,
763 a51a74b3 2019-07-27 stsp struct got_repository *repo)
765 a367ff0f 2019-05-14 stsp const struct got_error *err = NULL;
766 a367ff0f 2019-05-14 stsp struct got_commit_graph *graph = NULL;
767 a367ff0f 2019-05-14 stsp struct got_object_id *head_commit_id = NULL;
768 a367ff0f 2019-05-14 stsp int is_same_branch = 0;
770 a367ff0f 2019-05-14 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
774 a51a74b3 2019-07-27 stsp if (got_object_id_cmp(head_commit_id, commit_id) == 0) {
775 a51a74b3 2019-07-27 stsp is_same_branch = 1;
778 a51a74b3 2019-07-27 stsp if (yca_id && got_object_id_cmp(commit_id, yca_id) == 0) {
779 a51a74b3 2019-07-27 stsp is_same_branch = 1;
783 a367ff0f 2019-05-14 stsp err = got_commit_graph_open(&graph, head_commit_id, "/", 1, repo);
787 a367ff0f 2019-05-14 stsp err = got_commit_graph_iter_start(graph, head_commit_id, repo);
792 a367ff0f 2019-05-14 stsp struct got_object_id *id;
793 a367ff0f 2019-05-14 stsp err = got_commit_graph_iter_next(&id, graph);
795 a367ff0f 2019-05-14 stsp if (err->code == GOT_ERR_ITER_COMPLETED) {
796 a367ff0f 2019-05-14 stsp err = NULL;
798 a14c42fa 2019-07-15 stsp } else if (err->code != GOT_ERR_ITER_NEED_MORE)
800 a367ff0f 2019-05-14 stsp err = got_commit_graph_fetch_commits(graph, 1,
807 a51a74b3 2019-07-27 stsp if (yca_id && got_object_id_cmp(id, yca_id) == 0)
809 a367ff0f 2019-05-14 stsp if (got_object_id_cmp(id, commit_id) == 0) {
810 a367ff0f 2019-05-14 stsp is_same_branch = 1;
817 a367ff0f 2019-05-14 stsp got_commit_graph_close(graph);
818 a367ff0f 2019-05-14 stsp free(head_commit_id);
819 a367ff0f 2019-05-14 stsp if (!err && !is_same_branch)
820 a367ff0f 2019-05-14 stsp err = got_error(GOT_ERR_ANCESTRY);
821 04f57cb3 2019-07-25 stsp return err;
824 04f57cb3 2019-07-25 stsp static const struct got_error *
825 04f57cb3 2019-07-25 stsp resolve_commit_arg(struct got_object_id **commit_id,
826 04f57cb3 2019-07-25 stsp const char *commit_id_arg, struct got_repository *repo)
828 04f57cb3 2019-07-25 stsp const struct got_error *err;
829 04f57cb3 2019-07-25 stsp struct got_reference *ref;
830 303e2782 2019-08-09 stsp struct got_tag_object *tag;
832 303e2782 2019-08-09 stsp err = got_repo_object_match_tag(&tag, commit_id_arg,
833 303e2782 2019-08-09 stsp GOT_OBJ_TYPE_COMMIT, repo);
834 303e2782 2019-08-09 stsp if (err == NULL) {
835 303e2782 2019-08-09 stsp *commit_id = got_object_id_dup(
836 303e2782 2019-08-09 stsp got_object_tag_get_object_id(tag));
837 303e2782 2019-08-09 stsp if (*commit_id == NULL)
838 303e2782 2019-08-09 stsp err = got_error_from_errno("got_object_id_dup");
839 303e2782 2019-08-09 stsp got_object_tag_close(tag);
840 303e2782 2019-08-09 stsp return err;
841 303e2782 2019-08-09 stsp } else if (err->code != GOT_ERR_NO_OBJ)
842 303e2782 2019-08-09 stsp return err;
844 04f57cb3 2019-07-25 stsp err = got_ref_open(&ref, repo, commit_id_arg, 0);
845 04f57cb3 2019-07-25 stsp if (err == NULL) {
846 04f57cb3 2019-07-25 stsp err = got_ref_resolve(commit_id, repo, ref);
847 04f57cb3 2019-07-25 stsp got_ref_close(ref);
849 04f57cb3 2019-07-25 stsp if (err->code != GOT_ERR_NOT_REF)
850 04f57cb3 2019-07-25 stsp return err;
851 04f57cb3 2019-07-25 stsp err = got_repo_match_object_id_prefix(commit_id,
852 04f57cb3 2019-07-25 stsp commit_id_arg, GOT_OBJ_TYPE_COMMIT, repo);
854 a367ff0f 2019-05-14 stsp return err;
857 4ed7e80c 2018-05-20 stsp static const struct got_error *
858 c09a553d 2018-03-12 stsp cmd_checkout(int argc, char *argv[])
860 c09a553d 2018-03-12 stsp const struct got_error *error = NULL;
861 c09a553d 2018-03-12 stsp struct got_repository *repo = NULL;
862 c09a553d 2018-03-12 stsp struct got_reference *head_ref = NULL;
863 c09a553d 2018-03-12 stsp struct got_worktree *worktree = NULL;
864 c09a553d 2018-03-12 stsp char *repo_path = NULL;
865 c09a553d 2018-03-12 stsp char *worktree_path = NULL;
866 0bb8a95e 2018-03-12 stsp const char *path_prefix = "";
867 08573d5b 2019-05-14 stsp const char *branch_name = GOT_REF_HEAD;
868 8069f636 2019-01-12 stsp char *commit_id_str = NULL;
869 72151b04 2019-05-11 stsp int ch, same_path_prefix;
870 f2ea84fa 2019-07-27 stsp struct got_pathlist_head paths;
872 f2ea84fa 2019-07-27 stsp TAILQ_INIT(&paths);
874 08573d5b 2019-05-14 stsp while ((ch = getopt(argc, argv, "b:c:p:")) != -1) {
875 0bb8a95e 2018-03-12 stsp switch (ch) {
877 08573d5b 2019-05-14 stsp branch_name = optarg;
880 8069f636 2019-01-12 stsp commit_id_str = strdup(optarg);
881 8069f636 2019-01-12 stsp if (commit_id_str == NULL)
882 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
885 0bb8a95e 2018-03-12 stsp path_prefix = optarg;
888 2deda0b9 2019-03-07 stsp usage_checkout();
889 0bb8a95e 2018-03-12 stsp /* NOTREACHED */
893 0bb8a95e 2018-03-12 stsp argc -= optind;
894 0bb8a95e 2018-03-12 stsp argv += optind;
896 6715a751 2018-03-16 stsp #ifndef PROFILE
897 68ed9ba5 2019-02-10 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
898 68ed9ba5 2019-02-10 stsp "unveil", NULL) == -1)
899 c09a553d 2018-03-12 stsp err(1, "pledge");
901 0bb8a95e 2018-03-12 stsp if (argc == 1) {
902 c09a553d 2018-03-12 stsp char *cwd, *base, *dotgit;
903 76089277 2018-04-01 stsp repo_path = realpath(argv[0], NULL);
904 76089277 2018-04-01 stsp if (repo_path == NULL)
905 638f9024 2019-05-13 stsp return got_error_from_errno2("realpath", argv[0]);
906 c09a553d 2018-03-12 stsp cwd = getcwd(NULL, 0);
907 76089277 2018-04-01 stsp if (cwd == NULL) {
908 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
911 230a42bd 2019-05-11 jcs if (path_prefix[0]) {
912 230a42bd 2019-05-11 jcs base = basename(path_prefix);
913 230a42bd 2019-05-11 jcs if (base == NULL) {
914 638f9024 2019-05-13 stsp error = got_error_from_errno2("basename",
915 230a42bd 2019-05-11 jcs path_prefix);
919 5d7c1dab 2018-04-01 stsp base = basename(repo_path);
920 230a42bd 2019-05-11 jcs if (base == NULL) {
921 638f9024 2019-05-13 stsp error = got_error_from_errno2("basename",
926 c09a553d 2018-03-12 stsp dotgit = strstr(base, ".git");
927 c09a553d 2018-03-12 stsp if (dotgit)
928 c09a553d 2018-03-12 stsp *dotgit = '\0';
929 c09a553d 2018-03-12 stsp if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
930 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
935 0bb8a95e 2018-03-12 stsp } else if (argc == 2) {
936 76089277 2018-04-01 stsp repo_path = realpath(argv[0], NULL);
937 76089277 2018-04-01 stsp if (repo_path == NULL) {
938 638f9024 2019-05-13 stsp error = got_error_from_errno2("realpath", argv[0]);
941 f7b38925 2018-04-01 stsp worktree_path = realpath(argv[1], NULL);
942 76089277 2018-04-01 stsp if (worktree_path == NULL) {
943 b4b3a7dd 2019-07-22 stsp if (errno != ENOENT) {
944 b4b3a7dd 2019-07-22 stsp error = got_error_from_errno2("realpath",
948 b4b3a7dd 2019-07-22 stsp worktree_path = strdup(argv[1]);
949 b4b3a7dd 2019-07-22 stsp if (worktree_path == NULL) {
950 b4b3a7dd 2019-07-22 stsp error = got_error_from_errno("strdup");
955 c09a553d 2018-03-12 stsp usage_checkout();
957 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
958 72151b04 2019-05-11 stsp got_path_strip_trailing_slashes(worktree_path);
960 c09a553d 2018-03-12 stsp error = got_repo_open(&repo, repo_path);
961 c09a553d 2018-03-12 stsp if (error != NULL)
964 c530dc23 2019-07-23 stsp /* Pre-create work tree path for unveil(2) */
965 c530dc23 2019-07-23 stsp error = got_path_mkdir(worktree_path);
966 c530dc23 2019-07-23 stsp if (error) {
967 80c1b583 2019-08-07 stsp if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
968 80c1b583 2019-08-07 stsp !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
970 c530dc23 2019-07-23 stsp if (!got_path_dir_is_empty(worktree_path)) {
971 c530dc23 2019-07-23 stsp error = got_error_path(worktree_path,
972 c530dc23 2019-07-23 stsp GOT_ERR_DIR_NOT_EMPTY);
977 c530dc23 2019-07-23 stsp error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
981 08573d5b 2019-05-14 stsp error = got_ref_open(&head_ref, repo, branch_name, 0);
982 c09a553d 2018-03-12 stsp if (error != NULL)
985 0bb8a95e 2018-03-12 stsp error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
986 d70b8e30 2018-12-27 stsp if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
989 c09a553d 2018-03-12 stsp error = got_worktree_open(&worktree, worktree_path);
990 c09a553d 2018-03-12 stsp if (error != NULL)
993 e5dc7198 2018-12-29 stsp error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
994 e5dc7198 2018-12-29 stsp path_prefix);
995 e5dc7198 2018-12-29 stsp if (error != NULL)
997 e5dc7198 2018-12-29 stsp if (!same_path_prefix) {
998 49520a32 2018-12-29 stsp error = got_error(GOT_ERR_PATH_PREFIX);
1002 8069f636 2019-01-12 stsp if (commit_id_str) {
1003 04f57cb3 2019-07-25 stsp struct got_object_id *commit_id;
1004 04f57cb3 2019-07-25 stsp error = resolve_commit_arg(&commit_id, commit_id_str, repo);
1005 30837e32 2019-07-25 stsp if (error)
1006 8069f636 2019-01-12 stsp goto done;
1007 024e9686 2019-05-14 stsp error = check_linear_ancestry(commit_id,
1008 024e9686 2019-05-14 stsp got_worktree_get_base_commit_id(worktree), repo);
1009 8069f636 2019-01-12 stsp if (error != NULL) {
1010 8069f636 2019-01-12 stsp free(commit_id);
1011 8069f636 2019-01-12 stsp goto done;
1013 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
1014 45d344f6 2019-05-14 stsp if (error)
1015 45d344f6 2019-05-14 stsp goto done;
1016 8069f636 2019-01-12 stsp error = got_worktree_set_base_commit_id(worktree, repo,
1017 8069f636 2019-01-12 stsp commit_id);
1018 8069f636 2019-01-12 stsp free(commit_id);
1019 8069f636 2019-01-12 stsp if (error)
1020 8069f636 2019-01-12 stsp goto done;
1023 adc19d55 2019-07-28 stsp error = got_pathlist_append(&paths, "", NULL);
1024 f2ea84fa 2019-07-27 stsp if (error)
1025 f2ea84fa 2019-07-27 stsp goto done;
1026 f2ea84fa 2019-07-27 stsp error = got_worktree_checkout_files(worktree, &paths, repo,
1027 6bad629b 2019-02-04 stsp checkout_progress, worktree_path, check_cancelled, NULL);
1028 c09a553d 2018-03-12 stsp if (error != NULL)
1029 c09a553d 2018-03-12 stsp goto done;
1031 b65ae19a 2018-04-24 stsp printf("Now shut up and hack\n");
1034 f2ea84fa 2019-07-27 stsp got_pathlist_free(&paths);
1035 8069f636 2019-01-12 stsp free(commit_id_str);
1036 76089277 2018-04-01 stsp free(repo_path);
1037 507dc3bb 2018-12-29 stsp free(worktree_path);
1038 507dc3bb 2018-12-29 stsp return error;
1041 507dc3bb 2018-12-29 stsp __dead static void
1042 507dc3bb 2018-12-29 stsp usage_update(void)
1044 f2ea84fa 2019-07-27 stsp fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path ...]\n",
1045 507dc3bb 2018-12-29 stsp getprogname());
1049 1ee397ad 2019-07-12 stsp static const struct got_error *
1050 507dc3bb 2018-12-29 stsp update_progress(void *arg, unsigned char status, const char *path)
1052 784955db 2019-01-12 stsp int *did_something = arg;
1054 507dc3bb 2018-12-29 stsp if (status == GOT_STATUS_EXISTS)
1055 1ee397ad 2019-07-12 stsp return NULL;
1057 1545c615 2019-02-10 stsp *did_something = 1;
1059 a484d721 2019-06-10 stsp /* Base commit bump happens silently. */
1060 a484d721 2019-06-10 stsp if (status == GOT_STATUS_BUMP_BASE)
1061 1ee397ad 2019-07-12 stsp return NULL;
1063 507dc3bb 2018-12-29 stsp while (path[0] == '/')
1065 507dc3bb 2018-12-29 stsp printf("%c %s\n", status, path);
1066 1ee397ad 2019-07-12 stsp return NULL;
1069 be7061eb 2018-12-30 stsp static const struct got_error *
1070 a1fb16d8 2019-05-24 stsp switch_head_ref(struct got_reference *head_ref,
1071 a1fb16d8 2019-05-24 stsp struct got_object_id *commit_id, struct got_worktree *worktree,
1072 a1fb16d8 2019-05-24 stsp struct got_repository *repo)
1074 a1fb16d8 2019-05-24 stsp const struct got_error *err = NULL;
1075 a1fb16d8 2019-05-24 stsp char *base_id_str;
1076 a1fb16d8 2019-05-24 stsp int ref_has_moved = 0;
1078 a1fb16d8 2019-05-24 stsp /* Trivial case: switching between two different references. */
1079 a1fb16d8 2019-05-24 stsp if (strcmp(got_ref_get_name(head_ref),
1080 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree)) != 0) {
1081 a1fb16d8 2019-05-24 stsp printf("Switching work tree from %s to %s\n",
1082 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree),
1083 a1fb16d8 2019-05-24 stsp got_ref_get_name(head_ref));
1084 a1fb16d8 2019-05-24 stsp return got_worktree_set_head_ref(worktree, head_ref);
1087 a1fb16d8 2019-05-24 stsp err = check_linear_ancestry(commit_id,
1088 a1fb16d8 2019-05-24 stsp got_worktree_get_base_commit_id(worktree), repo);
1089 a1fb16d8 2019-05-24 stsp if (err) {
1090 a1fb16d8 2019-05-24 stsp if (err->code != GOT_ERR_ANCESTRY)
1091 a1fb16d8 2019-05-24 stsp return err;
1092 a1fb16d8 2019-05-24 stsp ref_has_moved = 1;
1094 a1fb16d8 2019-05-24 stsp if (!ref_has_moved)
1095 a1fb16d8 2019-05-24 stsp return NULL;
1097 a1fb16d8 2019-05-24 stsp /* Switching to a rebased branch with the same reference name. */
1098 a1fb16d8 2019-05-24 stsp err = got_object_id_str(&base_id_str,
1099 a1fb16d8 2019-05-24 stsp got_worktree_get_base_commit_id(worktree));
1101 a1fb16d8 2019-05-24 stsp return err;
1102 a1fb16d8 2019-05-24 stsp printf("Reference %s now points at a different branch\n",
1103 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree));
1104 a1fb16d8 2019-05-24 stsp printf("Switching work tree from %s to %s\n", base_id_str,
1105 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree));
1106 0ebf8283 2019-07-24 stsp return NULL;
1109 0ebf8283 2019-07-24 stsp static const struct got_error *
1110 0ebf8283 2019-07-24 stsp check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
1112 0ebf8283 2019-07-24 stsp const struct got_error *err;
1113 0ebf8283 2019-07-24 stsp int in_progress;
1115 0ebf8283 2019-07-24 stsp err = got_worktree_rebase_in_progress(&in_progress, worktree);
1117 0ebf8283 2019-07-24 stsp return err;
1118 0ebf8283 2019-07-24 stsp if (in_progress)
1119 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_REBASING);
1121 0ebf8283 2019-07-24 stsp err = got_worktree_histedit_in_progress(&in_progress, worktree);
1123 0ebf8283 2019-07-24 stsp return err;
1124 0ebf8283 2019-07-24 stsp if (in_progress)
1125 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_HISTEDIT_BUSY);
1127 a1fb16d8 2019-05-24 stsp return NULL;
1130 a5edda0a 2019-07-27 stsp static const struct got_error *
1131 a5edda0a 2019-07-27 stsp get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
1132 a5edda0a 2019-07-27 stsp char *argv[], struct got_worktree *worktree)
1134 a0de39f3 2019-08-09 stsp const struct got_error *err = NULL;
1135 a5edda0a 2019-07-27 stsp char *path;
1138 a5edda0a 2019-07-27 stsp if (argc == 0) {
1139 a5edda0a 2019-07-27 stsp path = strdup("");
1140 a5edda0a 2019-07-27 stsp if (path == NULL)
1141 a5edda0a 2019-07-27 stsp return got_error_from_errno("strdup");
1142 adc19d55 2019-07-28 stsp return got_pathlist_append(paths, path, NULL);
1145 a5edda0a 2019-07-27 stsp for (i = 0; i < argc; i++) {
1146 a5edda0a 2019-07-27 stsp err = got_worktree_resolve_path(&path, worktree, argv[i]);
1149 adc19d55 2019-07-28 stsp err = got_pathlist_append(paths, path, NULL);
1150 a5edda0a 2019-07-27 stsp if (err) {
1151 a5edda0a 2019-07-27 stsp free(path);
1156 a5edda0a 2019-07-27 stsp return err;
1159 a1fb16d8 2019-05-24 stsp static const struct got_error *
1160 507dc3bb 2018-12-29 stsp cmd_update(int argc, char *argv[])
1162 507dc3bb 2018-12-29 stsp const struct got_error *error = NULL;
1163 507dc3bb 2018-12-29 stsp struct got_repository *repo = NULL;
1164 507dc3bb 2018-12-29 stsp struct got_worktree *worktree = NULL;
1165 f2ea84fa 2019-07-27 stsp char *worktree_path = NULL;
1166 507dc3bb 2018-12-29 stsp struct got_object_id *commit_id = NULL;
1167 9c4b8182 2019-01-02 stsp char *commit_id_str = NULL;
1168 024e9686 2019-05-14 stsp const char *branch_name = NULL;
1169 024e9686 2019-05-14 stsp struct got_reference *head_ref = NULL;
1170 f2ea84fa 2019-07-27 stsp struct got_pathlist_head paths;
1171 f2ea84fa 2019-07-27 stsp struct got_pathlist_entry *pe;
1172 0ebf8283 2019-07-24 stsp int ch, did_something = 0;
1174 f2ea84fa 2019-07-27 stsp TAILQ_INIT(&paths);
1176 024e9686 2019-05-14 stsp while ((ch = getopt(argc, argv, "b:c:")) != -1) {
1177 507dc3bb 2018-12-29 stsp switch (ch) {
1179 024e9686 2019-05-14 stsp branch_name = optarg;
1182 9c4b8182 2019-01-02 stsp commit_id_str = strdup(optarg);
1183 9c4b8182 2019-01-02 stsp if (commit_id_str == NULL)
1184 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
1187 2deda0b9 2019-03-07 stsp usage_update();
1188 507dc3bb 2018-12-29 stsp /* NOTREACHED */
1192 507dc3bb 2018-12-29 stsp argc -= optind;
1193 507dc3bb 2018-12-29 stsp argv += optind;
1195 507dc3bb 2018-12-29 stsp #ifndef PROFILE
1196 68ed9ba5 2019-02-10 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1197 68ed9ba5 2019-02-10 stsp "unveil", NULL) == -1)
1198 507dc3bb 2018-12-29 stsp err(1, "pledge");
1200 c4cdcb68 2019-04-03 stsp worktree_path = getcwd(NULL, 0);
1201 c4cdcb68 2019-04-03 stsp if (worktree_path == NULL) {
1202 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
1203 c4cdcb68 2019-04-03 stsp goto done;
1205 c4cdcb68 2019-04-03 stsp error = got_worktree_open(&worktree, worktree_path);
1206 7d5807f4 2019-07-11 stsp if (error)
1207 7d5807f4 2019-07-11 stsp goto done;
1209 0ebf8283 2019-07-24 stsp error = check_rebase_or_histedit_in_progress(worktree);
1210 f2ea84fa 2019-07-27 stsp if (error)
1211 f2ea84fa 2019-07-27 stsp goto done;
1213 507dc3bb 2018-12-29 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
1214 507dc3bb 2018-12-29 stsp if (error != NULL)
1215 507dc3bb 2018-12-29 stsp goto done;
1217 97430839 2019-03-11 stsp error = apply_unveil(got_repo_get_path(repo), 0,
1218 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
1219 087fb88c 2019-08-04 stsp if (error)
1220 087fb88c 2019-08-04 stsp goto done;
1222 087fb88c 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
1223 0266afb7 2019-01-04 stsp if (error)
1224 0266afb7 2019-01-04 stsp goto done;
1226 a1fb16d8 2019-05-24 stsp error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
1227 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree), 0);
1228 024e9686 2019-05-14 stsp if (error != NULL)
1229 024e9686 2019-05-14 stsp goto done;
1230 507dc3bb 2018-12-29 stsp if (commit_id_str == NULL) {
1231 507dc3bb 2018-12-29 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
1232 9c4b8182 2019-01-02 stsp if (error != NULL)
1233 9c4b8182 2019-01-02 stsp goto done;
1234 9c4b8182 2019-01-02 stsp error = got_object_id_str(&commit_id_str, commit_id);
1235 507dc3bb 2018-12-29 stsp if (error != NULL)
1236 507dc3bb 2018-12-29 stsp goto done;
1238 04f57cb3 2019-07-25 stsp error = resolve_commit_arg(&commit_id, commit_id_str, repo);
1239 04f57cb3 2019-07-25 stsp free(commit_id_str);
1240 bb787f09 2019-07-25 stsp commit_id_str = NULL;
1241 30837e32 2019-07-25 stsp if (error)
1242 a297e751 2019-07-11 stsp goto done;
1243 a297e751 2019-07-11 stsp error = got_object_id_str(&commit_id_str, commit_id);
1244 a297e751 2019-07-11 stsp if (error)
1245 507dc3bb 2018-12-29 stsp goto done;
1248 a1fb16d8 2019-05-24 stsp if (branch_name) {
1249 024e9686 2019-05-14 stsp struct got_object_id *head_commit_id;
1250 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, &paths, entry) {
1251 f2b16ada 2019-08-02 stsp if (pe->path_len == 0)
1253 f2ea84fa 2019-07-27 stsp error = got_error_msg(GOT_ERR_BAD_PATH,
1254 f2ea84fa 2019-07-27 stsp "switching between branches requires that "
1255 f2ea84fa 2019-07-27 stsp "the entire work tree gets updated");
1256 024e9686 2019-05-14 stsp goto done;
1258 024e9686 2019-05-14 stsp error = got_ref_resolve(&head_commit_id, repo, head_ref);
1259 024e9686 2019-05-14 stsp if (error)
1260 024e9686 2019-05-14 stsp goto done;
1261 024e9686 2019-05-14 stsp error = check_linear_ancestry(commit_id, head_commit_id, repo);
1262 a367ff0f 2019-05-14 stsp free(head_commit_id);
1263 024e9686 2019-05-14 stsp if (error != NULL)
1264 024e9686 2019-05-14 stsp goto done;
1265 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
1266 a367ff0f 2019-05-14 stsp if (error)
1267 a367ff0f 2019-05-14 stsp goto done;
1268 a1fb16d8 2019-05-24 stsp error = switch_head_ref(head_ref, commit_id, worktree, repo);
1269 024e9686 2019-05-14 stsp if (error)
1270 024e9686 2019-05-14 stsp goto done;
1272 024e9686 2019-05-14 stsp error = check_linear_ancestry(commit_id,
1273 024e9686 2019-05-14 stsp got_worktree_get_base_commit_id(worktree), repo);
1274 a367ff0f 2019-05-14 stsp if (error != NULL) {
1275 a367ff0f 2019-05-14 stsp if (error->code == GOT_ERR_ANCESTRY)
1276 a367ff0f 2019-05-14 stsp error = got_error(GOT_ERR_BRANCH_MOVED);
1277 024e9686 2019-05-14 stsp goto done;
1279 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
1280 a367ff0f 2019-05-14 stsp if (error)
1281 a367ff0f 2019-05-14 stsp goto done;
1284 507dc3bb 2018-12-29 stsp if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
1285 507dc3bb 2018-12-29 stsp commit_id) != 0) {
1286 507dc3bb 2018-12-29 stsp error = got_worktree_set_base_commit_id(worktree, repo,
1287 507dc3bb 2018-12-29 stsp commit_id);
1288 507dc3bb 2018-12-29 stsp if (error)
1289 507dc3bb 2018-12-29 stsp goto done;
1292 f2ea84fa 2019-07-27 stsp error = got_worktree_checkout_files(worktree, &paths, repo,
1293 6bad629b 2019-02-04 stsp update_progress, &did_something, check_cancelled, NULL);
1294 507dc3bb 2018-12-29 stsp if (error != NULL)
1295 507dc3bb 2018-12-29 stsp goto done;
1297 784955db 2019-01-12 stsp if (did_something)
1298 784955db 2019-01-12 stsp printf("Updated to commit %s\n", commit_id_str);
1300 784955db 2019-01-12 stsp printf("Already up-to-date\n");
1302 c09a553d 2018-03-12 stsp free(worktree_path);
1303 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, &paths, entry)
1304 f2ea84fa 2019-07-27 stsp free((char *)pe->path);
1305 f2ea84fa 2019-07-27 stsp got_pathlist_free(&paths);
1306 507dc3bb 2018-12-29 stsp free(commit_id);
1307 9c4b8182 2019-01-02 stsp free(commit_id_str);
1308 c09a553d 2018-03-12 stsp return error;
1311 f42b1b34 2018-03-12 stsp static const struct got_error *
1312 79109fed 2018-03-27 stsp print_patch(struct got_commit_object *commit, struct got_object_id *id,
1313 c0cc5c62 2018-10-18 stsp int diff_context, struct got_repository *repo)
1315 f42b1b34 2018-03-12 stsp const struct got_error *err = NULL;
1316 79109fed 2018-03-27 stsp struct got_tree_object *tree1 = NULL, *tree2;
1317 79f35eb3 2018-06-11 stsp struct got_object_qid *qid;
1318 0f2b3dca 2018-12-22 stsp char *id_str1 = NULL, *id_str2;
1319 aaa13589 2019-06-01 stsp struct got_diff_blob_output_unidiff_arg arg;
1321 45d799e2 2018-12-23 stsp err = got_object_open_as_tree(&tree2, repo,
1322 45d799e2 2018-12-23 stsp got_object_commit_get_tree_id(commit));
1324 79109fed 2018-03-27 stsp return err;
1326 45d799e2 2018-12-23 stsp qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1327 79f35eb3 2018-06-11 stsp if (qid != NULL) {
1328 79109fed 2018-03-27 stsp struct got_commit_object *pcommit;
1330 117e771c 2018-07-23 stsp err = got_object_open_as_commit(&pcommit, repo, qid->id);
1332 79109fed 2018-03-27 stsp return err;
1334 45d799e2 2018-12-23 stsp err = got_object_open_as_tree(&tree1, repo,
1335 45d799e2 2018-12-23 stsp got_object_commit_get_tree_id(pcommit));
1336 79109fed 2018-03-27 stsp got_object_commit_close(pcommit);
1338 0f2b3dca 2018-12-22 stsp return err;
1340 0f2b3dca 2018-12-22 stsp err = got_object_id_str(&id_str1, qid->id);
1342 79109fed 2018-03-27 stsp return err;
1345 0f2b3dca 2018-12-22 stsp err = got_object_id_str(&id_str2, id);
1347 0f2b3dca 2018-12-22 stsp goto done;
1349 56765ebb 2018-12-23 stsp printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1350 aaa13589 2019-06-01 stsp arg.diff_context = diff_context;
1351 aaa13589 2019-06-01 stsp arg.outfile = stdout;
1352 aaa13589 2019-06-01 stsp err = got_diff_tree(tree1, tree2, "", "", repo,
1353 31b4484f 2019-07-27 stsp got_diff_blob_output_unidiff, &arg, 1);
1355 79109fed 2018-03-27 stsp if (tree1)
1356 79109fed 2018-03-27 stsp got_object_tree_close(tree1);
1357 79109fed 2018-03-27 stsp got_object_tree_close(tree2);
1358 0f2b3dca 2018-12-22 stsp free(id_str1);
1359 0f2b3dca 2018-12-22 stsp free(id_str2);
1360 79109fed 2018-03-27 stsp return err;
1363 4bb494d5 2018-06-16 stsp static char *
1364 6c281f94 2018-06-11 stsp get_datestr(time_t *time, char *datebuf)
1366 09867e48 2019-08-13 stsp struct tm mytm, *tm;
1367 09867e48 2019-08-13 stsp char *p, *s;
1369 09867e48 2019-08-13 stsp tm = gmtime_r(time, &mytm);
1370 09867e48 2019-08-13 stsp if (tm == NULL)
1371 09867e48 2019-08-13 stsp return NULL;
1372 09867e48 2019-08-13 stsp s = asctime_r(tm, datebuf);
1373 09867e48 2019-08-13 stsp if (s == NULL)
1374 09867e48 2019-08-13 stsp return NULL;
1375 6c281f94 2018-06-11 stsp p = strchr(s, '\n');
1377 6c281f94 2018-06-11 stsp *p = '\0';
1381 dc424a06 2019-08-07 stsp #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
1383 79109fed 2018-03-27 stsp static const struct got_error *
1384 79109fed 2018-03-27 stsp print_commit(struct got_commit_object *commit, struct got_object_id *id,
1385 199a4027 2019-02-02 stsp struct got_repository *repo, int show_patch, int diff_context,
1386 199a4027 2019-02-02 stsp struct got_reflist_head *refs)
1388 79109fed 2018-03-27 stsp const struct got_error *err = NULL;
1389 621015ac 2018-07-23 stsp char *id_str, *datestr, *logmsg0, *logmsg, *line;
1390 6c281f94 2018-06-11 stsp char datebuf[26];
1391 45d799e2 2018-12-23 stsp time_t committer_time;
1392 45d799e2 2018-12-23 stsp const char *author, *committer;
1393 199a4027 2019-02-02 stsp char *refs_str = NULL;
1394 199a4027 2019-02-02 stsp struct got_reflist_entry *re;
1396 199a4027 2019-02-02 stsp SIMPLEQ_FOREACH(re, refs, entry) {
1398 199a4027 2019-02-02 stsp const char *name;
1399 a436ad14 2019-08-13 stsp struct got_tag_object *tag = NULL;
1402 199a4027 2019-02-02 stsp name = got_ref_get_name(re->ref);
1403 d9498b20 2019-02-05 stsp if (strcmp(name, GOT_REF_HEAD) == 0)
1405 199a4027 2019-02-02 stsp if (strncmp(name, "refs/", 5) == 0)
1406 199a4027 2019-02-02 stsp name += 5;
1407 7143d404 2019-03-12 stsp if (strncmp(name, "got/", 4) == 0)
1409 e34f9ed6 2019-02-02 stsp if (strncmp(name, "heads/", 6) == 0)
1410 e34f9ed6 2019-02-02 stsp name += 6;
1411 141c2bff 2019-02-04 stsp if (strncmp(name, "remotes/", 8) == 0)
1412 141c2bff 2019-02-04 stsp name += 8;
1413 a436ad14 2019-08-13 stsp if (strncmp(name, "tags/", 5) == 0) {
1414 a436ad14 2019-08-13 stsp err = got_object_open_as_tag(&tag, repo, re->id);
1415 5d844a1e 2019-08-13 stsp if (err) {
1416 5d844a1e 2019-08-13 stsp if (err->code != GOT_ERR_OBJ_TYPE)
1417 5d844a1e 2019-08-13 stsp return err;
1418 5d844a1e 2019-08-13 stsp /* Ref points at something other than a tag. */
1419 5d844a1e 2019-08-13 stsp err = NULL;
1420 5d844a1e 2019-08-13 stsp tag = NULL;
1423 a436ad14 2019-08-13 stsp cmp = got_object_id_cmp(tag ?
1424 a436ad14 2019-08-13 stsp got_object_tag_get_object_id(tag) : re->id, id);
1426 a436ad14 2019-08-13 stsp got_object_tag_close(tag);
1427 a436ad14 2019-08-13 stsp if (cmp != 0)
1429 199a4027 2019-02-02 stsp s = refs_str;
1430 199a4027 2019-02-02 stsp if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
1431 199a4027 2019-02-02 stsp name) == -1) {
1432 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1434 34ca4898 2019-08-13 stsp return err;
1438 832c249c 2018-06-10 stsp err = got_object_id_str(&id_str, id);
1440 8bf5b3c9 2018-03-17 stsp return err;
1442 dc424a06 2019-08-07 stsp printf(GOT_COMMIT_SEP_STR);
1443 199a4027 2019-02-02 stsp printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
1444 199a4027 2019-02-02 stsp refs_str ? refs_str : "", refs_str ? ")" : "");
1445 832c249c 2018-06-10 stsp free(id_str);
1446 d3d493d7 2019-02-21 stsp id_str = NULL;
1447 d3d493d7 2019-02-21 stsp free(refs_str);
1448 d3d493d7 2019-02-21 stsp refs_str = NULL;
1449 45d799e2 2018-12-23 stsp printf("from: %s\n", got_object_commit_get_author(commit));
1450 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
1451 45d799e2 2018-12-23 stsp datestr = get_datestr(&committer_time, datebuf);
1452 09867e48 2019-08-13 stsp if (datestr)
1453 09867e48 2019-08-13 stsp printf("date: %s UTC\n", datestr);
1454 45d799e2 2018-12-23 stsp author = got_object_commit_get_author(commit);
1455 45d799e2 2018-12-23 stsp committer = got_object_commit_get_committer(commit);
1456 45d799e2 2018-12-23 stsp if (strcmp(author, committer) != 0)
1457 45d799e2 2018-12-23 stsp printf("via: %s\n", committer);
1458 45d799e2 2018-12-23 stsp if (got_object_commit_get_nparents(commit) > 1) {
1459 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
1460 79f35eb3 2018-06-11 stsp struct got_object_qid *qid;
1461 3fe1abad 2018-06-10 stsp int n = 1;
1462 45d799e2 2018-12-23 stsp parent_ids = got_object_commit_get_parent_ids(commit);
1463 45d799e2 2018-12-23 stsp SIMPLEQ_FOREACH(qid, parent_ids, entry) {
1464 79f35eb3 2018-06-11 stsp err = got_object_id_str(&id_str, qid->id);
1466 a0603db2 2018-06-10 stsp return err;
1467 3fe1abad 2018-06-10 stsp printf("parent %d: %s\n", n++, id_str);
1468 a0603db2 2018-06-10 stsp free(id_str);
1472 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
1474 5943eee2 2019-08-13 stsp return err;
1476 621015ac 2018-07-23 stsp logmsg = logmsg0;
1478 832c249c 2018-06-10 stsp line = strsep(&logmsg, "\n");
1480 832c249c 2018-06-10 stsp printf(" %s\n", line);
1481 832c249c 2018-06-10 stsp } while (line);
1482 621015ac 2018-07-23 stsp free(logmsg0);
1484 971751ac 2018-03-27 stsp if (show_patch) {
1485 c0cc5c62 2018-10-18 stsp err = print_patch(commit, id, diff_context, repo);
1486 971751ac 2018-03-27 stsp if (err == 0)
1487 971751ac 2018-03-27 stsp printf("\n");
1490 cbe7f848 2019-02-11 stsp if (fflush(stdout) != 0 && err == NULL)
1491 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
1492 07862c20 2018-09-15 stsp return err;
1495 f42b1b34 2018-03-12 stsp static const struct got_error *
1496 15a94983 2018-12-23 stsp print_commits(struct got_object_id *root_id, struct got_repository *repo,
1497 15a94983 2018-12-23 stsp char *path, int show_patch, int diff_context, int limit,
1498 199a4027 2019-02-02 stsp int first_parent_traversal, struct got_reflist_head *refs)
1500 f42b1b34 2018-03-12 stsp const struct got_error *err;
1501 372ccdbb 2018-06-10 stsp struct got_commit_graph *graph;
1503 31cedeaf 2018-09-15 stsp err = got_commit_graph_open(&graph, root_id, path,
1504 31cedeaf 2018-09-15 stsp first_parent_traversal, repo);
1506 f42b1b34 2018-03-12 stsp return err;
1507 31cedeaf 2018-09-15 stsp err = got_commit_graph_iter_start(graph, root_id, repo);
1509 fcc85cad 2018-07-22 stsp goto done;
1511 372ccdbb 2018-06-10 stsp struct got_commit_object *commit;
1512 372ccdbb 2018-06-10 stsp struct got_object_id *id;
1514 84453469 2018-11-11 stsp if (sigint_received || sigpipe_received)
1517 b43fbaa0 2018-06-11 stsp err = got_commit_graph_iter_next(&id, graph);
1518 372ccdbb 2018-06-10 stsp if (err) {
1519 9ba79e04 2018-06-11 stsp if (err->code == GOT_ERR_ITER_COMPLETED) {
1520 9ba79e04 2018-06-11 stsp err = NULL;
1523 372ccdbb 2018-06-10 stsp if (err->code != GOT_ERR_ITER_NEED_MORE)
1525 31cedeaf 2018-09-15 stsp err = got_commit_graph_fetch_commits(graph, 1, repo);
1531 b43fbaa0 2018-06-11 stsp if (id == NULL)
1534 f8e900f3 2018-06-11 stsp err = got_object_open_as_commit(&commit, repo, id);
1537 199a4027 2019-02-02 stsp err = print_commit(commit, id, repo, show_patch, diff_context,
1539 b43fbaa0 2018-06-11 stsp got_object_commit_close(commit);
1540 372ccdbb 2018-06-10 stsp if (err || (limit && --limit == 0))
1544 372ccdbb 2018-06-10 stsp got_commit_graph_close(graph);
1545 f42b1b34 2018-03-12 stsp return err;
1548 4ed7e80c 2018-05-20 stsp __dead static void
1549 6f3d1eb0 2018-03-12 stsp usage_log(void)
1551 cc54c501 2019-07-15 stsp fprintf(stderr, "usage: %s log [-c commit] [-C number] [-f] [ -l N ] [-p] "
1552 04ca23f4 2018-07-16 stsp "[-r repository-path] [path]\n", getprogname());
1556 b1ebc001 2019-08-13 stsp static int
1557 b1ebc001 2019-08-13 stsp get_default_log_limit(void)
1559 b1ebc001 2019-08-13 stsp const char *got_default_log_limit;
1560 b1ebc001 2019-08-13 stsp long long n;
1561 b1ebc001 2019-08-13 stsp const char *errstr;
1563 b1ebc001 2019-08-13 stsp got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
1564 b1ebc001 2019-08-13 stsp if (got_default_log_limit == NULL)
1566 b1ebc001 2019-08-13 stsp n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
1567 b1ebc001 2019-08-13 stsp if (errstr != NULL)
1572 4ed7e80c 2018-05-20 stsp static const struct got_error *
1573 f42b1b34 2018-03-12 stsp cmd_log(int argc, char *argv[])
1575 f42b1b34 2018-03-12 stsp const struct got_error *error;
1576 04ca23f4 2018-07-16 stsp struct got_repository *repo = NULL;
1577 cffc0aa4 2019-02-05 stsp struct got_worktree *worktree = NULL;
1578 15a94983 2018-12-23 stsp struct got_commit_object *commit = NULL;
1579 3235492e 2018-04-01 stsp struct got_object_id *id = NULL;
1580 04ca23f4 2018-07-16 stsp char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
1581 d142fc45 2018-04-01 stsp char *start_commit = NULL;
1582 c0cc5c62 2018-10-18 stsp int diff_context = 3, ch;
1583 1fd6d7ea 2018-06-13 stsp int show_patch = 0, limit = 0, first_parent_traversal = 0;
1584 64a96a6d 2018-04-01 stsp const char *errstr;
1585 199a4027 2019-02-02 stsp struct got_reflist_head refs;
1587 1b3893a2 2019-03-18 stsp SIMPLEQ_INIT(&refs);
1589 6715a751 2018-03-16 stsp #ifndef PROFILE
1590 6098196c 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1593 f42b1b34 2018-03-12 stsp err(1, "pledge");
1596 b1ebc001 2019-08-13 stsp limit = get_default_log_limit();
1598 cc54c501 2019-07-15 stsp while ((ch = getopt(argc, argv, "b:pc:C:l:fr:")) != -1) {
1599 79109fed 2018-03-27 stsp switch (ch) {
1601 79109fed 2018-03-27 stsp show_patch = 1;
1604 d142fc45 2018-04-01 stsp start_commit = optarg;
1607 4a8520aa 2018-10-18 stsp diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
1609 c0cc5c62 2018-10-18 stsp if (errstr != NULL)
1610 c0cc5c62 2018-10-18 stsp err(1, "-C option %s", errstr);
1613 b1ebc001 2019-08-13 stsp limit = strtonum(optarg, 0, INT_MAX, &errstr);
1614 64a96a6d 2018-04-01 stsp if (errstr != NULL)
1615 64a96a6d 2018-04-01 stsp err(1, "-l option %s", errstr);
1618 cc54c501 2019-07-15 stsp first_parent_traversal = 1;
1621 04ca23f4 2018-07-16 stsp repo_path = realpath(optarg, NULL);
1622 04ca23f4 2018-07-16 stsp if (repo_path == NULL)
1623 04ca23f4 2018-07-16 stsp err(1, "-r option");
1624 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
1627 2deda0b9 2019-03-07 stsp usage_log();
1628 79109fed 2018-03-27 stsp /* NOTREACHED */
1632 79109fed 2018-03-27 stsp argc -= optind;
1633 79109fed 2018-03-27 stsp argv += optind;
1635 04ca23f4 2018-07-16 stsp cwd = getcwd(NULL, 0);
1636 04ca23f4 2018-07-16 stsp if (cwd == NULL) {
1637 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
1638 04ca23f4 2018-07-16 stsp goto done;
1641 cffc0aa4 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
1642 cffc0aa4 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
1643 cffc0aa4 2019-02-05 stsp goto done;
1644 cffc0aa4 2019-02-05 stsp error = NULL;
1646 e7301579 2019-03-18 stsp if (argc == 0) {
1647 cbd1af7a 2019-03-18 stsp path = strdup("");
1648 e7301579 2019-03-18 stsp if (path == NULL) {
1649 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
1650 cbd1af7a 2019-03-18 stsp goto done;
1652 e7301579 2019-03-18 stsp } else if (argc == 1) {
1653 e7301579 2019-03-18 stsp if (worktree) {
1654 e7301579 2019-03-18 stsp error = got_worktree_resolve_path(&path, worktree,
1656 e7301579 2019-03-18 stsp if (error)
1657 e7301579 2019-03-18 stsp goto done;
1659 e7301579 2019-03-18 stsp path = strdup(argv[0]);
1660 e7301579 2019-03-18 stsp if (path == NULL) {
1661 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
1662 e7301579 2019-03-18 stsp goto done;
1666 cbd1af7a 2019-03-18 stsp usage_log();
1668 5486daa2 2019-05-11 stsp if (repo_path == NULL) {
1669 5486daa2 2019-05-11 stsp repo_path = worktree ?
1670 5486daa2 2019-05-11 stsp strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
1672 04ca23f4 2018-07-16 stsp if (repo_path == NULL) {
1673 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
1674 cffc0aa4 2019-02-05 stsp goto done;
1677 f42b1b34 2018-03-12 stsp error = got_repo_open(&repo, repo_path);
1678 d7d4f210 2018-03-12 stsp if (error != NULL)
1679 04ca23f4 2018-07-16 stsp goto done;
1681 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), 1,
1682 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
1683 c02c541e 2019-03-29 stsp if (error)
1684 c02c541e 2019-03-29 stsp goto done;
1686 d142fc45 2018-04-01 stsp if (start_commit == NULL) {
1687 3235492e 2018-04-01 stsp struct got_reference *head_ref;
1688 1cc14b9f 2019-05-14 stsp error = got_ref_open(&head_ref, repo,
1689 1cc14b9f 2019-05-14 stsp worktree ? got_worktree_get_head_ref_name(worktree)
1690 1cc14b9f 2019-05-14 stsp : GOT_REF_HEAD, 0);
1691 3235492e 2018-04-01 stsp if (error != NULL)
1692 3235492e 2018-04-01 stsp return error;
1693 3235492e 2018-04-01 stsp error = got_ref_resolve(&id, repo, head_ref);
1694 3235492e 2018-04-01 stsp got_ref_close(head_ref);
1695 3235492e 2018-04-01 stsp if (error != NULL)
1696 3235492e 2018-04-01 stsp return error;
1697 15a94983 2018-12-23 stsp error = got_object_open_as_commit(&commit, repo, id);
1699 d1f2edc9 2018-06-13 stsp struct got_reference *ref;
1700 2f17228e 2019-05-12 stsp error = got_ref_open(&ref, repo, start_commit, 0);
1701 3235492e 2018-04-01 stsp if (error == NULL) {
1702 1caad61b 2019-02-01 stsp int obj_type;
1703 d1f2edc9 2018-06-13 stsp error = got_ref_resolve(&id, repo, ref);
1704 d1f2edc9 2018-06-13 stsp got_ref_close(ref);
1705 d1f2edc9 2018-06-13 stsp if (error != NULL)
1706 1caad61b 2019-02-01 stsp goto done;
1707 1caad61b 2019-02-01 stsp error = got_object_get_type(&obj_type, repo, id);
1708 1caad61b 2019-02-01 stsp if (error != NULL)
1709 1caad61b 2019-02-01 stsp goto done;
1710 1caad61b 2019-02-01 stsp if (obj_type == GOT_OBJ_TYPE_TAG) {
1711 1caad61b 2019-02-01 stsp struct got_tag_object *tag;
1712 1caad61b 2019-02-01 stsp error = got_object_open_as_tag(&tag, repo, id);
1713 1caad61b 2019-02-01 stsp if (error != NULL)
1714 1caad61b 2019-02-01 stsp goto done;
1715 1caad61b 2019-02-01 stsp if (got_object_tag_get_object_type(tag) !=
1716 1caad61b 2019-02-01 stsp GOT_OBJ_TYPE_COMMIT) {
1717 1caad61b 2019-02-01 stsp got_object_tag_close(tag);
1718 1caad61b 2019-02-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
1719 1caad61b 2019-02-01 stsp goto done;
1722 1caad61b 2019-02-01 stsp id = got_object_id_dup(
1723 1caad61b 2019-02-01 stsp got_object_tag_get_object_id(tag));
1724 1caad61b 2019-02-01 stsp if (id == NULL)
1725 638f9024 2019-05-13 stsp error = got_error_from_errno(
1726 230a42bd 2019-05-11 jcs "got_object_id_dup");
1727 1caad61b 2019-02-01 stsp got_object_tag_close(tag);
1728 1caad61b 2019-02-01 stsp if (error)
1729 1caad61b 2019-02-01 stsp goto done;
1730 1caad61b 2019-02-01 stsp } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
1731 1caad61b 2019-02-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
1732 1caad61b 2019-02-01 stsp goto done;
1734 15a94983 2018-12-23 stsp error = got_object_open_as_commit(&commit, repo, id);
1735 d1f2edc9 2018-06-13 stsp if (error != NULL)
1736 1caad61b 2019-02-01 stsp goto done;
1738 15a94983 2018-12-23 stsp if (commit == NULL) {
1739 e09a504c 2019-06-28 stsp error = got_repo_match_object_id_prefix(&id,
1740 dd88155e 2019-06-29 stsp start_commit, GOT_OBJ_TYPE_COMMIT, repo);
1741 d1f2edc9 2018-06-13 stsp if (error != NULL)
1742 d1f2edc9 2018-06-13 stsp return error;
1745 d7d4f210 2018-03-12 stsp if (error != NULL)
1746 04ca23f4 2018-07-16 stsp goto done;
1748 23721109 2018-10-22 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
1749 04ca23f4 2018-07-16 stsp if (error != NULL)
1750 04ca23f4 2018-07-16 stsp goto done;
1751 04ca23f4 2018-07-16 stsp if (in_repo_path) {
1752 04ca23f4 2018-07-16 stsp free(path);
1753 04ca23f4 2018-07-16 stsp path = in_repo_path;
1756 199a4027 2019-02-02 stsp error = got_ref_list(&refs, repo);
1757 199a4027 2019-02-02 stsp if (error)
1758 199a4027 2019-02-02 stsp goto done;
1760 15a94983 2018-12-23 stsp error = print_commits(id, repo, path, show_patch,
1761 199a4027 2019-02-02 stsp diff_context, limit, first_parent_traversal, &refs);
1763 04ca23f4 2018-07-16 stsp free(path);
1764 04ca23f4 2018-07-16 stsp free(repo_path);
1765 04ca23f4 2018-07-16 stsp free(cwd);
1767 cffc0aa4 2019-02-05 stsp if (worktree)
1768 cffc0aa4 2019-02-05 stsp got_worktree_close(worktree);
1769 ad242220 2018-09-08 stsp if (repo) {
1770 ad242220 2018-09-08 stsp const struct got_error *repo_error;
1771 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
1772 ad242220 2018-09-08 stsp if (error == NULL)
1773 ad242220 2018-09-08 stsp error = repo_error;
1775 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
1776 8bf5b3c9 2018-03-17 stsp return error;
1779 4ed7e80c 2018-05-20 stsp __dead static void
1780 3f8b7d6a 2018-04-01 stsp usage_diff(void)
1782 98eaaa12 2019-08-03 stsp fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] [-s] "
1783 927df6b7 2019-02-10 stsp "[object1 object2 | path]\n", getprogname());
1787 b72f483a 2019-02-05 stsp struct print_diff_arg {
1788 b72f483a 2019-02-05 stsp struct got_repository *repo;
1789 b72f483a 2019-02-05 stsp struct got_worktree *worktree;
1790 b72f483a 2019-02-05 stsp int diff_context;
1791 3fc0c068 2019-02-10 stsp const char *id_str;
1792 3fc0c068 2019-02-10 stsp int header_shown;
1793 98eaaa12 2019-08-03 stsp int diff_staged;
1796 4ed7e80c 2018-05-20 stsp static const struct got_error *
1797 88d0e355 2019-08-03 stsp print_diff(void *arg, unsigned char status, unsigned char staged_status,
1798 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
1799 537ac44b 2019-08-03 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
1801 b72f483a 2019-02-05 stsp struct print_diff_arg *a = arg;
1802 b72f483a 2019-02-05 stsp const struct got_error *err = NULL;
1803 b72f483a 2019-02-05 stsp struct got_blob_object *blob1 = NULL;
1804 b72f483a 2019-02-05 stsp FILE *f2 = NULL;
1805 4ce46740 2019-08-08 stsp char *abspath = NULL, *label1 = NULL;
1806 b72f483a 2019-02-05 stsp struct stat sb;
1808 98eaaa12 2019-08-03 stsp if (a->diff_staged) {
1809 98eaaa12 2019-08-03 stsp if (staged_status != GOT_STATUS_MODIFY &&
1810 98eaaa12 2019-08-03 stsp staged_status != GOT_STATUS_ADD &&
1811 98eaaa12 2019-08-03 stsp staged_status != GOT_STATUS_DELETE)
1812 98eaaa12 2019-08-03 stsp return NULL;
1814 98eaaa12 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
1815 98eaaa12 2019-08-03 stsp return NULL;
1816 98eaaa12 2019-08-03 stsp if (status != GOT_STATUS_MODIFY &&
1817 98eaaa12 2019-08-03 stsp status != GOT_STATUS_ADD &&
1818 98eaaa12 2019-08-03 stsp status != GOT_STATUS_DELETE &&
1819 98eaaa12 2019-08-03 stsp status != GOT_STATUS_CONFLICT)
1820 98eaaa12 2019-08-03 stsp return NULL;
1823 3fc0c068 2019-02-10 stsp if (!a->header_shown) {
1824 98eaaa12 2019-08-03 stsp printf("diff %s %s%s\n", a->id_str,
1825 98eaaa12 2019-08-03 stsp got_worktree_get_root_path(a->worktree),
1826 98eaaa12 2019-08-03 stsp a->diff_staged ? " (staged changes)" : "");
1827 3fc0c068 2019-02-10 stsp a->header_shown = 1;
1830 98eaaa12 2019-08-03 stsp if (a->diff_staged) {
1831 98eaaa12 2019-08-03 stsp const char *label1 = NULL, *label2 = NULL;
1832 98eaaa12 2019-08-03 stsp switch (staged_status) {
1833 98eaaa12 2019-08-03 stsp case GOT_STATUS_MODIFY:
1834 98eaaa12 2019-08-03 stsp label1 = path;
1835 98eaaa12 2019-08-03 stsp label2 = path;
1837 98eaaa12 2019-08-03 stsp case GOT_STATUS_ADD:
1838 98eaaa12 2019-08-03 stsp label2 = path;
1840 98eaaa12 2019-08-03 stsp case GOT_STATUS_DELETE:
1841 98eaaa12 2019-08-03 stsp label1 = path;
1844 98eaaa12 2019-08-03 stsp return got_error(GOT_ERR_FILE_STATUS);
1846 98eaaa12 2019-08-03 stsp return got_diff_objects_as_blobs(blob_id, staged_blob_id,
1847 98eaaa12 2019-08-03 stsp label1, label2, a->diff_context, a->repo, stdout);
1850 408b4ebc 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
1851 4ce46740 2019-08-08 stsp staged_status == GOT_STATUS_MODIFY) {
1852 4ce46740 2019-08-08 stsp char *id_str;
1853 408b4ebc 2019-08-03 stsp err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
1856 4ce46740 2019-08-08 stsp goto done;
1857 4ce46740 2019-08-08 stsp err = got_object_id_str(&id_str, staged_blob_id);
1859 4ce46740 2019-08-08 stsp goto done;
1860 4ce46740 2019-08-08 stsp if (asprintf(&label1, "%s (staged)", id_str) == -1) {
1861 4ce46740 2019-08-08 stsp err = got_error_from_errno("asprintf");
1862 4ce46740 2019-08-08 stsp free(id_str);
1863 4ce46740 2019-08-08 stsp goto done;
1865 4ce46740 2019-08-08 stsp free(id_str);
1866 4ce46740 2019-08-08 stsp } else if (status != GOT_STATUS_ADD) {
1867 016a88dd 2019-05-13 stsp err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
1869 4ce46740 2019-08-08 stsp goto done;
1872 7154f6ce 2019-03-27 stsp if (status != GOT_STATUS_DELETE) {
1873 2ec1f75b 2019-03-26 stsp if (asprintf(&abspath, "%s/%s",
1874 2ec1f75b 2019-03-26 stsp got_worktree_get_root_path(a->worktree), path) == -1) {
1875 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1876 2ec1f75b 2019-03-26 stsp goto done;
1879 2ec1f75b 2019-03-26 stsp f2 = fopen(abspath, "r");
1880 2ec1f75b 2019-03-26 stsp if (f2 == NULL) {
1881 638f9024 2019-05-13 stsp err = got_error_from_errno2("fopen", abspath);
1882 2ec1f75b 2019-03-26 stsp goto done;
1884 2ec1f75b 2019-03-26 stsp if (lstat(abspath, &sb) == -1) {
1885 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", abspath);
1886 2ec1f75b 2019-03-26 stsp goto done;
1889 2ec1f75b 2019-03-26 stsp sb.st_size = 0;
1891 4ce46740 2019-08-08 stsp err = got_diff_blob_file(blob1, label1, f2, sb.st_size, path,
1892 4ce46740 2019-08-08 stsp a->diff_context, stdout);
1894 b72f483a 2019-02-05 stsp if (blob1)
1895 b72f483a 2019-02-05 stsp got_object_blob_close(blob1);
1896 fb43ecf1 2019-02-11 stsp if (f2 && fclose(f2) != 0 && err == NULL)
1897 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
1898 b72f483a 2019-02-05 stsp free(abspath);
1899 927df6b7 2019-02-10 stsp return err;
1902 927df6b7 2019-02-10 stsp static const struct got_error *
1903 0adb7196 2019-08-11 stsp match_object_id(struct got_object_id **id, char **label,
1904 0adb7196 2019-08-11 stsp const char *id_str, int obj_type, struct got_repository *repo)
1906 0adb7196 2019-08-11 stsp const struct got_error *err;
1907 d24820bf 2019-08-11 stsp struct got_tag_object *tag;
1908 0adb7196 2019-08-11 stsp struct got_reference *ref = NULL;
1910 0adb7196 2019-08-11 stsp *id = NULL;
1911 0adb7196 2019-08-11 stsp *label = NULL;
1913 d24820bf 2019-08-11 stsp err = got_repo_object_match_tag(&tag, id_str, GOT_OBJ_TYPE_ANY, repo);
1914 d24820bf 2019-08-11 stsp if (err == NULL) {
1915 d24820bf 2019-08-11 stsp *id = got_object_id_dup(got_object_tag_get_object_id(tag));
1916 d24820bf 2019-08-11 stsp if (*id == NULL)
1917 d24820bf 2019-08-11 stsp err = got_error_from_errno("got_object_id_dup");
1918 d24820bf 2019-08-11 stsp if (asprintf(label, "refs/tags/%s",
1919 1b7a69d3 2019-08-11 stsp got_object_tag_get_name(tag)) == -1)
1920 d24820bf 2019-08-11 stsp err = got_error_from_errno("asprintf");
1921 d24820bf 2019-08-11 stsp got_object_tag_close(tag);
1922 d24820bf 2019-08-11 stsp return err;
1923 d24820bf 2019-08-11 stsp } else if (err->code != GOT_ERR_NO_OBJ)
1924 d24820bf 2019-08-11 stsp return err;
1926 0adb7196 2019-08-11 stsp err = got_repo_match_object_id_prefix(id, id_str, obj_type, repo);
1927 0adb7196 2019-08-11 stsp if (err) {
1928 0adb7196 2019-08-11 stsp if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
1929 0adb7196 2019-08-11 stsp return err;
1930 0adb7196 2019-08-11 stsp err = got_ref_open(&ref, repo, id_str, 0);
1931 0adb7196 2019-08-11 stsp if (err != NULL)
1932 0adb7196 2019-08-11 stsp goto done;
1933 0adb7196 2019-08-11 stsp *label = strdup(got_ref_get_name(ref));
1934 0adb7196 2019-08-11 stsp if (*label == NULL) {
1935 0adb7196 2019-08-11 stsp err = got_error_from_errno("strdup");
1936 0adb7196 2019-08-11 stsp goto done;
1938 0adb7196 2019-08-11 stsp err = got_ref_resolve(id, repo, ref);
1940 0adb7196 2019-08-11 stsp err = got_object_id_str(label, *id);
1941 0adb7196 2019-08-11 stsp if (*label == NULL) {
1942 0adb7196 2019-08-11 stsp err = got_error_from_errno("strdup");
1943 0adb7196 2019-08-11 stsp goto done;
1948 0adb7196 2019-08-11 stsp got_ref_close(ref);
1949 0adb7196 2019-08-11 stsp return err;
1953 0adb7196 2019-08-11 stsp static const struct got_error *
1954 b00d56cd 2018-04-01 stsp cmd_diff(int argc, char *argv[])
1956 b00d56cd 2018-04-01 stsp const struct got_error *error;
1957 b00d56cd 2018-04-01 stsp struct got_repository *repo = NULL;
1958 b72f483a 2019-02-05 stsp struct got_worktree *worktree = NULL;
1959 b72f483a 2019-02-05 stsp char *cwd = NULL, *repo_path = NULL;
1960 15a94983 2018-12-23 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
1961 cd628e99 2019-05-28 stsp const char *id_str1 = NULL, *id_str2 = NULL;
1962 5e70831e 2019-05-28 stsp char *label1 = NULL, *label2 = NULL;
1963 15a94983 2018-12-23 stsp int type1, type2;
1964 98eaaa12 2019-08-03 stsp int diff_context = 3, diff_staged = 0, ch;
1965 c0cc5c62 2018-10-18 stsp const char *errstr;
1966 927df6b7 2019-02-10 stsp char *path = NULL;
1968 b00d56cd 2018-04-01 stsp #ifndef PROFILE
1969 25eccc22 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1970 25eccc22 2019-01-04 stsp NULL) == -1)
1971 b00d56cd 2018-04-01 stsp err(1, "pledge");
1974 98eaaa12 2019-08-03 stsp while ((ch = getopt(argc, argv, "C:r:s")) != -1) {
1975 b00d56cd 2018-04-01 stsp switch (ch) {
1977 c0cc5c62 2018-10-18 stsp diff_context = strtonum(optarg, 1, INT_MAX, &errstr);
1978 c0cc5c62 2018-10-18 stsp if (errstr != NULL)
1979 c0cc5c62 2018-10-18 stsp err(1, "-C option %s", errstr);
1982 b72f483a 2019-02-05 stsp repo_path = realpath(optarg, NULL);
1983 b72f483a 2019-02-05 stsp if (repo_path == NULL)
1984 b72f483a 2019-02-05 stsp err(1, "-r option");
1985 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
1988 98eaaa12 2019-08-03 stsp diff_staged = 1;
1991 2deda0b9 2019-03-07 stsp usage_diff();
1992 b00d56cd 2018-04-01 stsp /* NOTREACHED */
1996 b00d56cd 2018-04-01 stsp argc -= optind;
1997 b00d56cd 2018-04-01 stsp argv += optind;
1999 b72f483a 2019-02-05 stsp cwd = getcwd(NULL, 0);
2000 b72f483a 2019-02-05 stsp if (cwd == NULL) {
2001 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2002 b72f483a 2019-02-05 stsp goto done;
2004 927df6b7 2019-02-10 stsp error = got_worktree_open(&worktree, cwd);
2005 927df6b7 2019-02-10 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
2006 927df6b7 2019-02-10 stsp goto done;
2007 927df6b7 2019-02-10 stsp if (argc <= 1) {
2008 927df6b7 2019-02-10 stsp if (worktree == NULL) {
2009 927df6b7 2019-02-10 stsp error = got_error(GOT_ERR_NOT_WORKTREE);
2010 927df6b7 2019-02-10 stsp goto done;
2012 b72f483a 2019-02-05 stsp if (repo_path)
2014 b72f483a 2019-02-05 stsp "-r option can't be used when diffing a work tree");
2015 b72f483a 2019-02-05 stsp repo_path = strdup(got_worktree_get_repo_path(worktree));
2016 927df6b7 2019-02-10 stsp if (repo_path == NULL) {
2017 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2018 927df6b7 2019-02-10 stsp goto done;
2020 927df6b7 2019-02-10 stsp if (argc == 1) {
2021 6c7ab921 2019-03-18 stsp error = got_worktree_resolve_path(&path, worktree,
2023 927df6b7 2019-02-10 stsp if (error)
2024 927df6b7 2019-02-10 stsp goto done;
2026 927df6b7 2019-02-10 stsp path = strdup("");
2027 927df6b7 2019-02-10 stsp if (path == NULL) {
2028 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2029 927df6b7 2019-02-10 stsp goto done;
2032 b72f483a 2019-02-05 stsp } else if (argc == 2) {
2033 98eaaa12 2019-08-03 stsp if (diff_staged)
2034 98eaaa12 2019-08-03 stsp errx(1, "-s option can't be used when diffing "
2035 98eaaa12 2019-08-03 stsp "objects in repository");
2036 15a94983 2018-12-23 stsp id_str1 = argv[0];
2037 15a94983 2018-12-23 stsp id_str2 = argv[1];
2038 30db809c 2019-06-05 stsp if (worktree && repo_path == NULL) {
2039 30db809c 2019-06-05 stsp repo_path =
2040 30db809c 2019-06-05 stsp strdup(got_worktree_get_repo_path(worktree));
2041 30db809c 2019-06-05 stsp if (repo_path == NULL) {
2042 30db809c 2019-06-05 stsp error = got_error_from_errno("strdup");
2043 30db809c 2019-06-05 stsp goto done;
2047 b00d56cd 2018-04-01 stsp usage_diff();
2049 b72f483a 2019-02-05 stsp if (repo_path == NULL) {
2050 b72f483a 2019-02-05 stsp repo_path = getcwd(NULL, 0);
2051 b72f483a 2019-02-05 stsp if (repo_path == NULL)
2052 638f9024 2019-05-13 stsp return got_error_from_errno("getcwd");
2055 b00d56cd 2018-04-01 stsp error = got_repo_open(&repo, repo_path);
2056 76089277 2018-04-01 stsp free(repo_path);
2057 b00d56cd 2018-04-01 stsp if (error != NULL)
2058 b00d56cd 2018-04-01 stsp goto done;
2060 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), 1,
2061 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
2062 c02c541e 2019-03-29 stsp if (error)
2063 c02c541e 2019-03-29 stsp goto done;
2065 30db809c 2019-06-05 stsp if (argc <= 1) {
2066 b72f483a 2019-02-05 stsp struct print_diff_arg arg;
2067 72ea6654 2019-07-27 stsp struct got_pathlist_head paths;
2068 b72f483a 2019-02-05 stsp char *id_str;
2070 72ea6654 2019-07-27 stsp TAILQ_INIT(&paths);
2072 b72f483a 2019-02-05 stsp error = got_object_id_str(&id_str,
2073 b72f483a 2019-02-05 stsp got_worktree_get_base_commit_id(worktree));
2074 b72f483a 2019-02-05 stsp if (error)
2075 b72f483a 2019-02-05 stsp goto done;
2076 b72f483a 2019-02-05 stsp arg.repo = repo;
2077 b72f483a 2019-02-05 stsp arg.worktree = worktree;
2078 b72f483a 2019-02-05 stsp arg.diff_context = diff_context;
2079 3fc0c068 2019-02-10 stsp arg.id_str = id_str;
2080 3fc0c068 2019-02-10 stsp arg.header_shown = 0;
2081 98eaaa12 2019-08-03 stsp arg.diff_staged = diff_staged;
2083 adc19d55 2019-07-28 stsp error = got_pathlist_append(&paths, path, NULL);
2084 72ea6654 2019-07-27 stsp if (error)
2085 72ea6654 2019-07-27 stsp goto done;
2087 72ea6654 2019-07-27 stsp error = got_worktree_status(worktree, &paths, repo, print_diff,
2088 b72f483a 2019-02-05 stsp &arg, check_cancelled, NULL);
2089 b72f483a 2019-02-05 stsp free(id_str);
2090 72ea6654 2019-07-27 stsp got_pathlist_free(&paths);
2091 b72f483a 2019-02-05 stsp goto done;
2094 0adb7196 2019-08-11 stsp error = match_object_id(&id1, &label1, id_str1, GOT_OBJ_TYPE_ANY, repo);
2095 0adb7196 2019-08-11 stsp if (error)
2096 0adb7196 2019-08-11 stsp goto done;
2098 0adb7196 2019-08-11 stsp error = match_object_id(&id2, &label2, id_str2, GOT_OBJ_TYPE_ANY, repo);
2099 0adb7196 2019-08-11 stsp if (error)
2100 0adb7196 2019-08-11 stsp goto done;
2102 15a94983 2018-12-23 stsp error = got_object_get_type(&type1, repo, id1);
2103 15a94983 2018-12-23 stsp if (error)
2104 15a94983 2018-12-23 stsp goto done;
2106 15a94983 2018-12-23 stsp error = got_object_get_type(&type2, repo, id2);
2107 15a94983 2018-12-23 stsp if (error)
2108 15a94983 2018-12-23 stsp goto done;
2110 15a94983 2018-12-23 stsp if (type1 != type2) {
2111 b00d56cd 2018-04-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
2112 b00d56cd 2018-04-01 stsp goto done;
2115 15a94983 2018-12-23 stsp switch (type1) {
2116 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_BLOB:
2117 15a94983 2018-12-23 stsp error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
2118 54156555 2018-12-24 stsp diff_context, repo, stdout);
2120 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_TREE:
2121 15a94983 2018-12-23 stsp error = got_diff_objects_as_trees(id1, id2, "", "",
2122 54156555 2018-12-24 stsp diff_context, repo, stdout);
2124 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_COMMIT:
2125 5e70831e 2019-05-28 stsp printf("diff %s %s\n", label1, label2);
2126 15a94983 2018-12-23 stsp error = got_diff_objects_as_commits(id1, id2, diff_context,
2127 c0cc5c62 2018-10-18 stsp repo, stdout);
2130 b00d56cd 2018-04-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
2134 5e70831e 2019-05-28 stsp free(label1);
2135 5e70831e 2019-05-28 stsp free(label2);
2136 15a94983 2018-12-23 stsp free(id1);
2137 15a94983 2018-12-23 stsp free(id2);
2138 927df6b7 2019-02-10 stsp free(path);
2139 b72f483a 2019-02-05 stsp if (worktree)
2140 b72f483a 2019-02-05 stsp got_worktree_close(worktree);
2141 ad242220 2018-09-08 stsp if (repo) {
2142 ad242220 2018-09-08 stsp const struct got_error *repo_error;
2143 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
2144 ad242220 2018-09-08 stsp if (error == NULL)
2145 ad242220 2018-09-08 stsp error = repo_error;
2147 b00d56cd 2018-04-01 stsp return error;
2150 404c43c4 2018-06-21 stsp __dead static void
2151 404c43c4 2018-06-21 stsp usage_blame(void)
2153 9270e621 2019-02-05 stsp fprintf(stderr,
2154 9270e621 2019-02-05 stsp "usage: %s blame [-c commit] [-r repository-path] path\n",
2155 404c43c4 2018-06-21 stsp getprogname());
2159 28315671 2019-08-14 stsp struct blame_line {
2160 28315671 2019-08-14 stsp int annotated;
2161 28315671 2019-08-14 stsp char *id_str;
2162 82f6abb8 2019-08-14 stsp char *committer;
2163 bcb49d15 2019-08-14 stsp char datebuf[9]; /* YY-MM-DD + NUL */
2166 28315671 2019-08-14 stsp struct blame_cb_args {
2167 28315671 2019-08-14 stsp struct blame_line *lines;
2168 28315671 2019-08-14 stsp int nlines;
2169 7ef28ff8 2019-08-14 stsp int nlines_prec;
2170 28315671 2019-08-14 stsp int lineno_cur;
2171 28315671 2019-08-14 stsp off_t *line_offsets;
2173 82f6abb8 2019-08-14 stsp struct got_repository *repo;
2176 404c43c4 2018-06-21 stsp static const struct got_error *
2177 28315671 2019-08-14 stsp blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2179 28315671 2019-08-14 stsp const struct got_error *err = NULL;
2180 28315671 2019-08-14 stsp struct blame_cb_args *a = arg;
2181 28315671 2019-08-14 stsp struct blame_line *bline;
2182 82f6abb8 2019-08-14 stsp char *line = NULL;
2183 28315671 2019-08-14 stsp size_t linesize = 0;
2184 82f6abb8 2019-08-14 stsp struct got_commit_object *commit = NULL;
2185 28315671 2019-08-14 stsp off_t offset;
2186 bcb49d15 2019-08-14 stsp struct tm tm;
2187 bcb49d15 2019-08-14 stsp time_t committer_time;
2189 28315671 2019-08-14 stsp if (nlines != a->nlines ||
2190 28315671 2019-08-14 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
2191 28315671 2019-08-14 stsp return got_error(GOT_ERR_RANGE);
2193 28315671 2019-08-14 stsp if (sigint_received)
2194 28315671 2019-08-14 stsp return got_error(GOT_ERR_ITER_COMPLETED);
2196 2839f8b9 2019-08-18 stsp if (lineno == -1)
2197 2839f8b9 2019-08-18 stsp return NULL; /* no change in this commit */
2199 28315671 2019-08-14 stsp /* Annotate this line. */
2200 28315671 2019-08-14 stsp bline = &a->lines[lineno - 1];
2201 28315671 2019-08-14 stsp if (bline->annotated)
2202 28315671 2019-08-14 stsp return NULL;
2203 28315671 2019-08-14 stsp err = got_object_id_str(&bline->id_str, id);
2205 28315671 2019-08-14 stsp return err;
2207 82f6abb8 2019-08-14 stsp err = got_object_open_as_commit(&commit, a->repo, id);
2209 82f6abb8 2019-08-14 stsp goto done;
2211 82f6abb8 2019-08-14 stsp bline->committer = strdup(got_object_commit_get_committer(commit));
2212 82f6abb8 2019-08-14 stsp if (bline->committer == NULL) {
2213 82f6abb8 2019-08-14 stsp err = got_error_from_errno("strdup");
2214 bcb49d15 2019-08-14 stsp goto done;
2217 bcb49d15 2019-08-14 stsp committer_time = got_object_commit_get_committer_time(commit);
2218 bcb49d15 2019-08-14 stsp if (localtime_r(&committer_time, &tm) == NULL)
2219 bcb49d15 2019-08-14 stsp return got_error_from_errno("localtime_r");
2220 bcb49d15 2019-08-14 stsp if (strftime(bline->datebuf, sizeof(bline->datebuf), "%g/%m/%d",
2221 bcb49d15 2019-08-14 stsp &tm) >= sizeof(bline->datebuf)) {
2222 bcb49d15 2019-08-14 stsp err = got_error(GOT_ERR_NO_SPACE);
2223 82f6abb8 2019-08-14 stsp goto done;
2225 28315671 2019-08-14 stsp bline->annotated = 1;
2227 28315671 2019-08-14 stsp /* Print lines annotated so far. */
2228 28315671 2019-08-14 stsp bline = &a->lines[a->lineno_cur - 1];
2229 28315671 2019-08-14 stsp if (!bline->annotated)
2230 82f6abb8 2019-08-14 stsp goto done;
2232 28315671 2019-08-14 stsp offset = a->line_offsets[a->lineno_cur - 1];
2233 82f6abb8 2019-08-14 stsp if (fseeko(a->f, offset, SEEK_SET) == -1) {
2234 82f6abb8 2019-08-14 stsp err = got_error_from_errno("fseeko");
2235 82f6abb8 2019-08-14 stsp goto done;
2238 28315671 2019-08-14 stsp while (bline->annotated) {
2239 82f6abb8 2019-08-14 stsp char *smallerthan, *at, *nl, *committer;
2240 82f6abb8 2019-08-14 stsp size_t len;
2242 28315671 2019-08-14 stsp if (getline(&line, &linesize, a->f) == (ssize_t)-1) {
2243 28315671 2019-08-14 stsp if (ferror(a->f))
2244 28315671 2019-08-14 stsp err = got_error_from_errno("getline");
2248 82f6abb8 2019-08-14 stsp committer = bline->committer;
2249 82f6abb8 2019-08-14 stsp smallerthan = strchr(committer, '<');
2250 82f6abb8 2019-08-14 stsp if (smallerthan && smallerthan[1] != '\0')
2251 82f6abb8 2019-08-14 stsp committer = smallerthan + 1;
2252 82f6abb8 2019-08-14 stsp at = strchr(committer, '@');
2254 82f6abb8 2019-08-14 stsp *at = '\0';
2255 82f6abb8 2019-08-14 stsp len = strlen(committer);
2256 82f6abb8 2019-08-14 stsp if (len >= 9)
2257 82f6abb8 2019-08-14 stsp committer[8] = '\0';
2259 28315671 2019-08-14 stsp nl = strchr(line, '\n');
2261 28315671 2019-08-14 stsp *nl = '\0';
2262 bcb49d15 2019-08-14 stsp printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
2263 bcb49d15 2019-08-14 stsp bline->id_str, bline->datebuf, committer, line);
2265 28315671 2019-08-14 stsp a->lineno_cur++;
2266 28315671 2019-08-14 stsp bline = &a->lines[a->lineno_cur - 1];
2269 82f6abb8 2019-08-14 stsp if (commit)
2270 82f6abb8 2019-08-14 stsp got_object_commit_close(commit);
2271 28315671 2019-08-14 stsp free(line);
2272 28315671 2019-08-14 stsp return err;
2275 28315671 2019-08-14 stsp static const struct got_error *
2276 404c43c4 2018-06-21 stsp cmd_blame(int argc, char *argv[])
2278 404c43c4 2018-06-21 stsp const struct got_error *error;
2279 404c43c4 2018-06-21 stsp struct got_repository *repo = NULL;
2280 0c06baac 2019-02-05 stsp struct got_worktree *worktree = NULL;
2281 66bea077 2018-08-02 stsp char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2282 28315671 2019-08-14 stsp struct got_object_id *obj_id = NULL;
2283 404c43c4 2018-06-21 stsp struct got_object_id *commit_id = NULL;
2284 28315671 2019-08-14 stsp struct got_blob_object *blob = NULL;
2285 404c43c4 2018-06-21 stsp char *commit_id_str = NULL;
2286 28315671 2019-08-14 stsp struct blame_cb_args bca;
2287 28315671 2019-08-14 stsp int ch, obj_type, i;
2288 b02560ec 2019-08-19 stsp size_t filesize;
2290 90f3c347 2019-08-19 stsp memset(&bca, 0, sizeof(bca));
2292 404c43c4 2018-06-21 stsp #ifndef PROFILE
2293 36e2fb66 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2294 36e2fb66 2019-01-04 stsp NULL) == -1)
2295 404c43c4 2018-06-21 stsp err(1, "pledge");
2298 66bea077 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2299 404c43c4 2018-06-21 stsp switch (ch) {
2301 404c43c4 2018-06-21 stsp commit_id_str = optarg;
2304 66bea077 2018-08-02 stsp repo_path = realpath(optarg, NULL);
2305 66bea077 2018-08-02 stsp if (repo_path == NULL)
2306 66bea077 2018-08-02 stsp err(1, "-r option");
2307 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
2310 2deda0b9 2019-03-07 stsp usage_blame();
2311 404c43c4 2018-06-21 stsp /* NOTREACHED */
2315 404c43c4 2018-06-21 stsp argc -= optind;
2316 404c43c4 2018-06-21 stsp argv += optind;
2318 a39318fd 2018-08-02 stsp if (argc == 1)
2319 404c43c4 2018-06-21 stsp path = argv[0];
2321 404c43c4 2018-06-21 stsp usage_blame();
2323 66bea077 2018-08-02 stsp cwd = getcwd(NULL, 0);
2324 66bea077 2018-08-02 stsp if (cwd == NULL) {
2325 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2326 66bea077 2018-08-02 stsp goto done;
2328 66bea077 2018-08-02 stsp if (repo_path == NULL) {
2329 0c06baac 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
2330 0c06baac 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
2331 66bea077 2018-08-02 stsp goto done;
2333 0c06baac 2019-02-05 stsp error = NULL;
2334 0c06baac 2019-02-05 stsp if (worktree) {
2335 0c06baac 2019-02-05 stsp repo_path =
2336 0c06baac 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
2337 0c06baac 2019-02-05 stsp if (repo_path == NULL)
2338 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2339 0c06baac 2019-02-05 stsp if (error)
2340 0c06baac 2019-02-05 stsp goto done;
2342 0c06baac 2019-02-05 stsp repo_path = strdup(cwd);
2343 0c06baac 2019-02-05 stsp if (repo_path == NULL) {
2344 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2345 0c06baac 2019-02-05 stsp goto done;
2350 c02c541e 2019-03-29 stsp error = got_repo_open(&repo, repo_path);
2351 c02c541e 2019-03-29 stsp if (error != NULL)
2352 36e2fb66 2019-01-04 stsp goto done;
2354 c530dc23 2019-07-23 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2355 c02c541e 2019-03-29 stsp if (error)
2356 404c43c4 2018-06-21 stsp goto done;
2358 0c06baac 2019-02-05 stsp if (worktree) {
2359 6efaaa2d 2019-02-05 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
2360 0c06baac 2019-02-05 stsp char *p, *worktree_subdir = cwd +
2361 0c06baac 2019-02-05 stsp strlen(got_worktree_get_root_path(worktree));
2362 6efaaa2d 2019-02-05 stsp if (asprintf(&p, "%s%s%s%s%s",
2363 6efaaa2d 2019-02-05 stsp prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
2364 6efaaa2d 2019-02-05 stsp worktree_subdir, worktree_subdir[0] ? "/" : "",
2365 6efaaa2d 2019-02-05 stsp path) == -1) {
2366 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
2367 0c06baac 2019-02-05 stsp goto done;
2369 6efaaa2d 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, p, 0);
2372 0c06baac 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
2374 0c06baac 2019-02-05 stsp if (error)
2375 66bea077 2018-08-02 stsp goto done;
2377 404c43c4 2018-06-21 stsp if (commit_id_str == NULL) {
2378 404c43c4 2018-06-21 stsp struct got_reference *head_ref;
2379 2f17228e 2019-05-12 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
2380 404c43c4 2018-06-21 stsp if (error != NULL)
2381 66bea077 2018-08-02 stsp goto done;
2382 404c43c4 2018-06-21 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
2383 404c43c4 2018-06-21 stsp got_ref_close(head_ref);
2384 404c43c4 2018-06-21 stsp if (error != NULL)
2385 66bea077 2018-08-02 stsp goto done;
2387 04f57cb3 2019-07-25 stsp error = resolve_commit_arg(&commit_id, commit_id_str, repo);
2388 30837e32 2019-07-25 stsp if (error)
2389 66bea077 2018-08-02 stsp goto done;
2392 28315671 2019-08-14 stsp error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
2393 28315671 2019-08-14 stsp if (error)
2394 28315671 2019-08-14 stsp goto done;
2395 28315671 2019-08-14 stsp if (obj_id == NULL) {
2396 28315671 2019-08-14 stsp error = got_error(GOT_ERR_NO_OBJ);
2397 28315671 2019-08-14 stsp goto done;
2400 28315671 2019-08-14 stsp error = got_object_get_type(&obj_type, repo, obj_id);
2401 28315671 2019-08-14 stsp if (error)
2402 28315671 2019-08-14 stsp goto done;
2404 28315671 2019-08-14 stsp if (obj_type != GOT_OBJ_TYPE_BLOB) {
2405 28315671 2019-08-14 stsp error = got_error(GOT_ERR_OBJ_TYPE);
2406 28315671 2019-08-14 stsp goto done;
2409 28315671 2019-08-14 stsp error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
2410 28315671 2019-08-14 stsp if (error)
2411 28315671 2019-08-14 stsp goto done;
2412 28315671 2019-08-14 stsp bca.f = got_opentemp();
2413 28315671 2019-08-14 stsp if (bca.f == NULL) {
2414 28315671 2019-08-14 stsp error = got_error_from_errno("got_opentemp");
2415 28315671 2019-08-14 stsp goto done;
2417 b02560ec 2019-08-19 stsp error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
2418 28315671 2019-08-14 stsp &bca.line_offsets, bca.f, blob);
2419 7ef28ff8 2019-08-14 stsp if (error || bca.nlines == 0)
2420 28315671 2019-08-14 stsp goto done;
2422 b02560ec 2019-08-19 stsp /* Don't include \n at EOF in the blame line count. */
2423 b02560ec 2019-08-19 stsp if (bca.line_offsets[bca.nlines - 1] == filesize)
2424 b02560ec 2019-08-19 stsp bca.nlines--;
2426 28315671 2019-08-14 stsp bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
2427 28315671 2019-08-14 stsp if (bca.lines == NULL) {
2428 28315671 2019-08-14 stsp error = got_error_from_errno("calloc");
2429 28315671 2019-08-14 stsp goto done;
2431 28315671 2019-08-14 stsp bca.lineno_cur = 1;
2432 7ef28ff8 2019-08-14 stsp bca.nlines_prec = 0;
2433 7ef28ff8 2019-08-14 stsp i = bca.nlines;
2434 7ef28ff8 2019-08-14 stsp while (i > 0) {
2436 7ef28ff8 2019-08-14 stsp bca.nlines_prec++;
2438 82f6abb8 2019-08-14 stsp bca.repo = repo;
2440 0d8ff7d5 2019-08-14 stsp error = got_blame(in_repo_path, commit_id, repo, blame_cb, &bca);
2441 28315671 2019-08-14 stsp if (error)
2442 28315671 2019-08-14 stsp goto done;
2444 66bea077 2018-08-02 stsp free(in_repo_path);
2445 66bea077 2018-08-02 stsp free(repo_path);
2446 66bea077 2018-08-02 stsp free(cwd);
2447 404c43c4 2018-06-21 stsp free(commit_id);
2448 28315671 2019-08-14 stsp free(obj_id);
2450 28315671 2019-08-14 stsp got_object_blob_close(blob);
2451 0c06baac 2019-02-05 stsp if (worktree)
2452 0c06baac 2019-02-05 stsp got_worktree_close(worktree);
2453 ad242220 2018-09-08 stsp if (repo) {
2454 ad242220 2018-09-08 stsp const struct got_error *repo_error;
2455 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
2456 ad242220 2018-09-08 stsp if (error == NULL)
2457 ad242220 2018-09-08 stsp error = repo_error;
2459 28315671 2019-08-14 stsp for (i = 0; i < bca.nlines; i++) {
2460 28315671 2019-08-14 stsp struct blame_line *bline = &bca.lines[i];
2461 28315671 2019-08-14 stsp free(bline->id_str);
2462 82f6abb8 2019-08-14 stsp free(bline->committer);
2464 28315671 2019-08-14 stsp free(bca.lines);
2465 28315671 2019-08-14 stsp free(bca.line_offsets);
2466 28315671 2019-08-14 stsp if (bca.f && fclose(bca.f) == EOF && error == NULL)
2467 28315671 2019-08-14 stsp error = got_error_from_errno("fclose");
2468 404c43c4 2018-06-21 stsp return error;
2471 5de5890b 2018-10-18 stsp __dead static void
2472 5de5890b 2018-10-18 stsp usage_tree(void)
2474 c1669e2e 2019-01-09 stsp fprintf(stderr,
2475 c1669e2e 2019-01-09 stsp "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
2476 5de5890b 2018-10-18 stsp getprogname());
2480 c1669e2e 2019-01-09 stsp static void
2481 c1669e2e 2019-01-09 stsp print_entry(struct got_tree_entry *te, const char *id, const char *path,
2482 c1669e2e 2019-01-09 stsp const char *root_path)
2484 c1669e2e 2019-01-09 stsp int is_root_path = (strcmp(path, root_path) == 0);
2485 848d6979 2019-08-12 stsp const char *modestr = "";
2487 c1669e2e 2019-01-09 stsp path += strlen(root_path);
2488 c1669e2e 2019-01-09 stsp while (path[0] == '/')
2491 848d6979 2019-08-12 stsp if (S_ISLNK(te->mode))
2492 848d6979 2019-08-12 stsp modestr = "@";
2493 848d6979 2019-08-12 stsp else if (S_ISDIR(te->mode))
2494 848d6979 2019-08-12 stsp modestr = "/";
2495 848d6979 2019-08-12 stsp else if (te->mode & S_IXUSR)
2496 848d6979 2019-08-12 stsp modestr = "*";
2498 c1669e2e 2019-01-09 stsp printf("%s%s%s%s%s\n", id ? id : "", path,
2499 848d6979 2019-08-12 stsp is_root_path ? "" : "/", te->name, modestr);
2502 5de5890b 2018-10-18 stsp static const struct got_error *
2503 5de5890b 2018-10-18 stsp print_tree(const char *path, struct got_object_id *commit_id,
2504 c1669e2e 2019-01-09 stsp int show_ids, int recurse, const char *root_path,
2505 c1669e2e 2019-01-09 stsp struct got_repository *repo)
2507 5de5890b 2018-10-18 stsp const struct got_error *err = NULL;
2508 5de5890b 2018-10-18 stsp struct got_object_id *tree_id = NULL;
2509 5de5890b 2018-10-18 stsp struct got_tree_object *tree = NULL;
2510 5de5890b 2018-10-18 stsp const struct got_tree_entries *entries;
2511 5de5890b 2018-10-18 stsp struct got_tree_entry *te;
2513 5de5890b 2018-10-18 stsp err = got_object_id_by_path(&tree_id, repo, commit_id, path);
2515 5de5890b 2018-10-18 stsp goto done;
2517 5de5890b 2018-10-18 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
2519 5de5890b 2018-10-18 stsp goto done;
2520 5de5890b 2018-10-18 stsp entries = got_object_tree_get_entries(tree);
2521 5de5890b 2018-10-18 stsp te = SIMPLEQ_FIRST(&entries->head);
2522 5de5890b 2018-10-18 stsp while (te) {
2523 5de5890b 2018-10-18 stsp char *id = NULL;
2525 84453469 2018-11-11 stsp if (sigint_received || sigpipe_received)
2528 5de5890b 2018-10-18 stsp if (show_ids) {
2529 5de5890b 2018-10-18 stsp char *id_str;
2530 5de5890b 2018-10-18 stsp err = got_object_id_str(&id_str, te->id);
2532 5de5890b 2018-10-18 stsp goto done;
2533 5de5890b 2018-10-18 stsp if (asprintf(&id, "%s ", id_str) == -1) {
2534 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2535 5de5890b 2018-10-18 stsp free(id_str);
2536 5de5890b 2018-10-18 stsp goto done;
2538 5de5890b 2018-10-18 stsp free(id_str);
2540 c1669e2e 2019-01-09 stsp print_entry(te, id, path, root_path);
2543 c1669e2e 2019-01-09 stsp if (recurse && S_ISDIR(te->mode)) {
2544 c1669e2e 2019-01-09 stsp char *child_path;
2545 c1669e2e 2019-01-09 stsp if (asprintf(&child_path, "%s%s%s", path,
2546 c1669e2e 2019-01-09 stsp path[0] == '/' && path[1] == '\0' ? "" : "/",
2547 c1669e2e 2019-01-09 stsp te->name) == -1) {
2548 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2549 c1669e2e 2019-01-09 stsp goto done;
2551 c1669e2e 2019-01-09 stsp err = print_tree(child_path, commit_id, show_ids, 1,
2552 c1669e2e 2019-01-09 stsp root_path, repo);
2553 c1669e2e 2019-01-09 stsp free(child_path);
2555 c1669e2e 2019-01-09 stsp goto done;
2558 c1669e2e 2019-01-09 stsp te = SIMPLEQ_NEXT(te, entry);
2562 5de5890b 2018-10-18 stsp got_object_tree_close(tree);
2563 5de5890b 2018-10-18 stsp free(tree_id);
2564 5de5890b 2018-10-18 stsp return err;
2567 5de5890b 2018-10-18 stsp static const struct got_error *
2568 5de5890b 2018-10-18 stsp cmd_tree(int argc, char *argv[])
2570 5de5890b 2018-10-18 stsp const struct got_error *error;
2571 5de5890b 2018-10-18 stsp struct got_repository *repo = NULL;
2572 7a2c19d6 2019-02-05 stsp struct got_worktree *worktree = NULL;
2573 7a2c19d6 2019-02-05 stsp const char *path;
2574 7a2c19d6 2019-02-05 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2575 5de5890b 2018-10-18 stsp struct got_object_id *commit_id = NULL;
2576 5de5890b 2018-10-18 stsp char *commit_id_str = NULL;
2577 c1669e2e 2019-01-09 stsp int show_ids = 0, recurse = 0;
2580 5de5890b 2018-10-18 stsp #ifndef PROFILE
2581 0f8d269b 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2582 0f8d269b 2019-01-04 stsp NULL) == -1)
2583 5de5890b 2018-10-18 stsp err(1, "pledge");
2586 c1669e2e 2019-01-09 stsp while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
2587 5de5890b 2018-10-18 stsp switch (ch) {
2589 5de5890b 2018-10-18 stsp commit_id_str = optarg;
2592 5de5890b 2018-10-18 stsp repo_path = realpath(optarg, NULL);
2593 5de5890b 2018-10-18 stsp if (repo_path == NULL)
2594 5de5890b 2018-10-18 stsp err(1, "-r option");
2595 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
2598 5de5890b 2018-10-18 stsp show_ids = 1;
2601 c1669e2e 2019-01-09 stsp recurse = 1;
2604 2deda0b9 2019-03-07 stsp usage_tree();
2605 5de5890b 2018-10-18 stsp /* NOTREACHED */
2609 5de5890b 2018-10-18 stsp argc -= optind;
2610 5de5890b 2018-10-18 stsp argv += optind;
2612 5de5890b 2018-10-18 stsp if (argc == 1)
2613 5de5890b 2018-10-18 stsp path = argv[0];
2614 5de5890b 2018-10-18 stsp else if (argc > 1)
2615 5de5890b 2018-10-18 stsp usage_tree();
2617 7a2c19d6 2019-02-05 stsp path = NULL;
2619 9bf7a39b 2019-02-05 stsp cwd = getcwd(NULL, 0);
2620 9bf7a39b 2019-02-05 stsp if (cwd == NULL) {
2621 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2622 9bf7a39b 2019-02-05 stsp goto done;
2624 5de5890b 2018-10-18 stsp if (repo_path == NULL) {
2625 7a2c19d6 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
2626 8994de28 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
2627 7a2c19d6 2019-02-05 stsp goto done;
2629 7a2c19d6 2019-02-05 stsp error = NULL;
2630 7a2c19d6 2019-02-05 stsp if (worktree) {
2631 7a2c19d6 2019-02-05 stsp repo_path =
2632 7a2c19d6 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
2633 7a2c19d6 2019-02-05 stsp if (repo_path == NULL)
2634 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2635 7a2c19d6 2019-02-05 stsp if (error)
2636 7a2c19d6 2019-02-05 stsp goto done;
2638 7a2c19d6 2019-02-05 stsp repo_path = strdup(cwd);
2639 7a2c19d6 2019-02-05 stsp if (repo_path == NULL) {
2640 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2641 7a2c19d6 2019-02-05 stsp goto done;
2646 5de5890b 2018-10-18 stsp error = got_repo_open(&repo, repo_path);
2647 5de5890b 2018-10-18 stsp if (error != NULL)
2648 c02c541e 2019-03-29 stsp goto done;
2650 c530dc23 2019-07-23 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2651 c02c541e 2019-03-29 stsp if (error)
2652 5de5890b 2018-10-18 stsp goto done;
2654 9bf7a39b 2019-02-05 stsp if (path == NULL) {
2655 9bf7a39b 2019-02-05 stsp if (worktree) {
2656 9bf7a39b 2019-02-05 stsp char *p, *worktree_subdir = cwd +
2657 9bf7a39b 2019-02-05 stsp strlen(got_worktree_get_root_path(worktree));
2658 9bf7a39b 2019-02-05 stsp if (asprintf(&p, "%s/%s",
2659 9bf7a39b 2019-02-05 stsp got_worktree_get_path_prefix(worktree),
2660 9bf7a39b 2019-02-05 stsp worktree_subdir) == -1) {
2661 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
2662 9bf7a39b 2019-02-05 stsp goto done;
2664 9bf7a39b 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, p, 1);
2666 9bf7a39b 2019-02-05 stsp if (error)
2667 9bf7a39b 2019-02-05 stsp goto done;
2669 9bf7a39b 2019-02-05 stsp path = "/";
2671 9bf7a39b 2019-02-05 stsp if (in_repo_path == NULL) {
2672 9bf7a39b 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
2673 9bf7a39b 2019-02-05 stsp if (error != NULL)
2674 9bf7a39b 2019-02-05 stsp goto done;
2677 5de5890b 2018-10-18 stsp if (commit_id_str == NULL) {
2678 5de5890b 2018-10-18 stsp struct got_reference *head_ref;
2679 2f17228e 2019-05-12 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
2680 5de5890b 2018-10-18 stsp if (error != NULL)
2681 5de5890b 2018-10-18 stsp goto done;
2682 5de5890b 2018-10-18 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
2683 5de5890b 2018-10-18 stsp got_ref_close(head_ref);
2684 5de5890b 2018-10-18 stsp if (error != NULL)
2685 5de5890b 2018-10-18 stsp goto done;
2687 04f57cb3 2019-07-25 stsp error = resolve_commit_arg(&commit_id, commit_id_str, repo);
2688 30837e32 2019-07-25 stsp if (error)
2689 5de5890b 2018-10-18 stsp goto done;
2692 c1669e2e 2019-01-09 stsp error = print_tree(in_repo_path, commit_id, show_ids, recurse,
2693 c1669e2e 2019-01-09 stsp in_repo_path, repo);
2695 5de5890b 2018-10-18 stsp free(in_repo_path);
2696 5de5890b 2018-10-18 stsp free(repo_path);
2697 5de5890b 2018-10-18 stsp free(cwd);
2698 5de5890b 2018-10-18 stsp free(commit_id);
2699 7a2c19d6 2019-02-05 stsp if (worktree)
2700 7a2c19d6 2019-02-05 stsp got_worktree_close(worktree);
2701 5de5890b 2018-10-18 stsp if (repo) {
2702 5de5890b 2018-10-18 stsp const struct got_error *repo_error;
2703 5de5890b 2018-10-18 stsp repo_error = got_repo_close(repo);
2704 5de5890b 2018-10-18 stsp if (error == NULL)
2705 5de5890b 2018-10-18 stsp error = repo_error;
2707 5de5890b 2018-10-18 stsp return error;
2710 6bad629b 2019-02-04 stsp __dead static void
2711 6bad629b 2019-02-04 stsp usage_status(void)
2713 72ea6654 2019-07-27 stsp fprintf(stderr, "usage: %s status [path ...]\n", getprogname());
2717 b72f483a 2019-02-05 stsp static const struct got_error *
2718 88d0e355 2019-08-03 stsp print_status(void *arg, unsigned char status, unsigned char staged_status,
2719 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
2720 537ac44b 2019-08-03 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
2722 244725f2 2019-08-03 stsp if (status == staged_status && (status == GOT_STATUS_DELETE))
2723 c363b2c1 2019-08-03 stsp status = GOT_STATUS_NO_CHANGE;
2724 88d0e355 2019-08-03 stsp printf("%c%c %s\n", status, staged_status, path);
2725 b72f483a 2019-02-05 stsp return NULL;
2728 6bad629b 2019-02-04 stsp static const struct got_error *
2729 6bad629b 2019-02-04 stsp cmd_status(int argc, char *argv[])
2731 6bad629b 2019-02-04 stsp const struct got_error *error = NULL;
2732 6bad629b 2019-02-04 stsp struct got_repository *repo = NULL;
2733 6bad629b 2019-02-04 stsp struct got_worktree *worktree = NULL;
2734 f86a1bf5 2019-07-27 stsp char *cwd = NULL;
2735 72ea6654 2019-07-27 stsp struct got_pathlist_head paths;
2736 a5edda0a 2019-07-27 stsp struct got_pathlist_entry *pe;
2739 72ea6654 2019-07-27 stsp TAILQ_INIT(&paths);
2741 6bad629b 2019-02-04 stsp while ((ch = getopt(argc, argv, "")) != -1) {
2742 6bad629b 2019-02-04 stsp switch (ch) {
2744 2deda0b9 2019-03-07 stsp usage_status();
2745 6bad629b 2019-02-04 stsp /* NOTREACHED */
2749 6bad629b 2019-02-04 stsp argc -= optind;
2750 6bad629b 2019-02-04 stsp argv += optind;
2752 6bad629b 2019-02-04 stsp #ifndef PROFILE
2753 6bad629b 2019-02-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2754 6bad629b 2019-02-04 stsp NULL) == -1)
2755 6bad629b 2019-02-04 stsp err(1, "pledge");
2757 927df6b7 2019-02-10 stsp cwd = getcwd(NULL, 0);
2758 927df6b7 2019-02-10 stsp if (cwd == NULL) {
2759 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2760 927df6b7 2019-02-10 stsp goto done;
2763 927df6b7 2019-02-10 stsp error = got_worktree_open(&worktree, cwd);
2764 927df6b7 2019-02-10 stsp if (error != NULL)
2765 a5edda0a 2019-07-27 stsp goto done;
2767 6bad629b 2019-02-04 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2768 6bad629b 2019-02-04 stsp if (error != NULL)
2769 6bad629b 2019-02-04 stsp goto done;
2771 d0eebce4 2019-03-11 stsp error = apply_unveil(got_repo_get_path(repo), 1,
2772 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
2773 087fb88c 2019-08-04 stsp if (error)
2774 087fb88c 2019-08-04 stsp goto done;
2776 087fb88c 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
2777 6bad629b 2019-02-04 stsp if (error)
2778 6bad629b 2019-02-04 stsp goto done;
2780 72ea6654 2019-07-27 stsp error = got_worktree_status(worktree, &paths, repo, print_status, NULL,
2781 6bad629b 2019-02-04 stsp check_cancelled, NULL);
2783 a5edda0a 2019-07-27 stsp TAILQ_FOREACH(pe, &paths, entry)
2784 a5edda0a 2019-07-27 stsp free((char *)pe->path);
2785 72ea6654 2019-07-27 stsp got_pathlist_free(&paths);
2786 927df6b7 2019-02-10 stsp free(cwd);
2787 d0eebce4 2019-03-11 stsp return error;
2790 d0eebce4 2019-03-11 stsp __dead static void
2791 d0eebce4 2019-03-11 stsp usage_ref(void)
2793 d0eebce4 2019-03-11 stsp fprintf(stderr,
2794 d1c1ae5f 2019-08-12 stsp "usage: %s ref [-r repository] -l | -d name | [-s] name target\n",
2795 d0eebce4 2019-03-11 stsp getprogname());
2799 d0eebce4 2019-03-11 stsp static const struct got_error *
2800 d0eebce4 2019-03-11 stsp list_refs(struct got_repository *repo)
2802 d0eebce4 2019-03-11 stsp static const struct got_error *err = NULL;
2803 d0eebce4 2019-03-11 stsp struct got_reflist_head refs;
2804 d0eebce4 2019-03-11 stsp struct got_reflist_entry *re;
2806 d0eebce4 2019-03-11 stsp SIMPLEQ_INIT(&refs);
2807 d0eebce4 2019-03-11 stsp err = got_ref_list(&refs, repo);
2809 d0eebce4 2019-03-11 stsp return err;
2811 d0eebce4 2019-03-11 stsp SIMPLEQ_FOREACH(re, &refs, entry) {
2812 d0eebce4 2019-03-11 stsp char *refstr;
2813 d0eebce4 2019-03-11 stsp refstr = got_ref_to_str(re->ref);
2814 d0eebce4 2019-03-11 stsp if (refstr == NULL)
2815 638f9024 2019-05-13 stsp return got_error_from_errno("got_ref_to_str");
2816 d0eebce4 2019-03-11 stsp printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
2817 d0eebce4 2019-03-11 stsp free(refstr);
2820 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
2821 d0eebce4 2019-03-11 stsp return NULL;
2824 d0eebce4 2019-03-11 stsp static const struct got_error *
2825 d0eebce4 2019-03-11 stsp delete_ref(struct got_repository *repo, const char *refname)
2827 d0eebce4 2019-03-11 stsp const struct got_error *err = NULL;
2828 d0eebce4 2019-03-11 stsp struct got_reference *ref;
2830 2f17228e 2019-05-12 stsp err = got_ref_open(&ref, repo, refname, 0);
2832 d0eebce4 2019-03-11 stsp return err;
2834 d0eebce4 2019-03-11 stsp err = got_ref_delete(ref, repo);
2835 d0eebce4 2019-03-11 stsp got_ref_close(ref);
2836 d0eebce4 2019-03-11 stsp return err;
2839 d0eebce4 2019-03-11 stsp static const struct got_error *
2840 d83d9d5c 2019-05-13 stsp add_ref(struct got_repository *repo, const char *refname, const char *target)
2842 d0eebce4 2019-03-11 stsp const struct got_error *err = NULL;
2843 d0eebce4 2019-03-11 stsp struct got_object_id *id;
2844 d0eebce4 2019-03-11 stsp struct got_reference *ref = NULL;
2847 d1644381 2019-07-14 stsp * Don't let the user create a reference named '-'.
2848 d1644381 2019-07-14 stsp * While technically a valid reference name, this case is usually
2849 d1644381 2019-07-14 stsp * an unintended typo.
2851 d1644381 2019-07-14 stsp if (refname[0] == '-' && refname[1] == '\0')
2852 d1644381 2019-07-14 stsp return got_error(GOT_ERR_BAD_REF_NAME);
2854 dd88155e 2019-06-29 stsp err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
2856 d83d9d5c 2019-05-13 stsp if (err) {
2857 d83d9d5c 2019-05-13 stsp struct got_reference *target_ref;
2859 d83d9d5c 2019-05-13 stsp if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
2860 d83d9d5c 2019-05-13 stsp return err;
2861 d83d9d5c 2019-05-13 stsp err = got_ref_open(&target_ref, repo, target, 0);
2863 d83d9d5c 2019-05-13 stsp return err;
2864 d83d9d5c 2019-05-13 stsp err = got_ref_resolve(&id, repo, target_ref);
2865 d83d9d5c 2019-05-13 stsp got_ref_close(target_ref);
2867 d83d9d5c 2019-05-13 stsp return err;
2870 d0eebce4 2019-03-11 stsp err = got_ref_alloc(&ref, refname, id);
2872 d0eebce4 2019-03-11 stsp goto done;
2874 d0eebce4 2019-03-11 stsp err = got_ref_write(ref, repo);
2877 d0eebce4 2019-03-11 stsp got_ref_close(ref);
2879 d1c1ae5f 2019-08-12 stsp return err;
2882 d1c1ae5f 2019-08-12 stsp static const struct got_error *
2883 d1c1ae5f 2019-08-12 stsp add_symref(struct got_repository *repo, const char *refname, const char *target)
2885 d1c1ae5f 2019-08-12 stsp const struct got_error *err = NULL;
2886 d1c1ae5f 2019-08-12 stsp struct got_reference *ref = NULL;
2887 d1c1ae5f 2019-08-12 stsp struct got_reference *target_ref = NULL;
2890 d1c1ae5f 2019-08-12 stsp * Don't let the user create a reference named '-'.
2891 d1c1ae5f 2019-08-12 stsp * While technically a valid reference name, this case is usually
2892 d1c1ae5f 2019-08-12 stsp * an unintended typo.
2894 d1c1ae5f 2019-08-12 stsp if (refname[0] == '-' && refname[1] == '\0')
2895 d1c1ae5f 2019-08-12 stsp return got_error(GOT_ERR_BAD_REF_NAME);
2897 d1c1ae5f 2019-08-12 stsp err = got_ref_open(&target_ref, repo, target, 0);
2899 d1c1ae5f 2019-08-12 stsp return err;
2901 d1c1ae5f 2019-08-12 stsp err = got_ref_alloc_symref(&ref, refname, target_ref);
2903 d1c1ae5f 2019-08-12 stsp goto done;
2905 d1c1ae5f 2019-08-12 stsp err = got_ref_write(ref, repo);
2907 d1c1ae5f 2019-08-12 stsp if (target_ref)
2908 d1c1ae5f 2019-08-12 stsp got_ref_close(target_ref);
2910 d1c1ae5f 2019-08-12 stsp got_ref_close(ref);
2911 d0eebce4 2019-03-11 stsp return err;
2914 d0eebce4 2019-03-11 stsp static const struct got_error *
2915 d0eebce4 2019-03-11 stsp cmd_ref(int argc, char *argv[])
2917 d0eebce4 2019-03-11 stsp const struct got_error *error = NULL;
2918 d0eebce4 2019-03-11 stsp struct got_repository *repo = NULL;
2919 d0eebce4 2019-03-11 stsp struct got_worktree *worktree = NULL;
2920 d0eebce4 2019-03-11 stsp char *cwd = NULL, *repo_path = NULL;
2921 d1c1ae5f 2019-08-12 stsp int ch, do_list = 0, create_symref = 0;
2922 d0eebce4 2019-03-11 stsp const char *delref = NULL;
2924 d0eebce4 2019-03-11 stsp /* TODO: Add -s option for adding symbolic references. */
2925 d1c1ae5f 2019-08-12 stsp while ((ch = getopt(argc, argv, "d:r:ls")) != -1) {
2926 d0eebce4 2019-03-11 stsp switch (ch) {
2928 d0eebce4 2019-03-11 stsp delref = optarg;
2931 d0eebce4 2019-03-11 stsp repo_path = realpath(optarg, NULL);
2932 d0eebce4 2019-03-11 stsp if (repo_path == NULL)
2933 d0eebce4 2019-03-11 stsp err(1, "-r option");
2934 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
2937 d0eebce4 2019-03-11 stsp do_list = 1;
2940 d1c1ae5f 2019-08-12 stsp create_symref = 1;
2943 d0eebce4 2019-03-11 stsp usage_ref();
2944 d0eebce4 2019-03-11 stsp /* NOTREACHED */
2948 d0eebce4 2019-03-11 stsp if (do_list && delref)
2949 d0eebce4 2019-03-11 stsp errx(1, "-l and -d options are mutually exclusive\n");
2951 d0eebce4 2019-03-11 stsp argc -= optind;
2952 d0eebce4 2019-03-11 stsp argv += optind;
2954 d0eebce4 2019-03-11 stsp if (do_list || delref) {
2955 d1c1ae5f 2019-08-12 stsp if (create_symref)
2956 d1c1ae5f 2019-08-12 stsp errx(1, "-s option cannot be used together with the "
2957 d1c1ae5f 2019-08-12 stsp "-l or -d options");
2958 d0eebce4 2019-03-11 stsp if (argc > 0)
2959 d0eebce4 2019-03-11 stsp usage_ref();
2960 d0eebce4 2019-03-11 stsp } else if (argc != 2)
2961 d0eebce4 2019-03-11 stsp usage_ref();
2963 d0eebce4 2019-03-11 stsp #ifndef PROFILE
2964 e0b57350 2019-03-12 stsp if (do_list) {
2965 e0b57350 2019-03-12 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
2966 e0b57350 2019-03-12 stsp NULL) == -1)
2967 e0b57350 2019-03-12 stsp err(1, "pledge");
2969 e0b57350 2019-03-12 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2970 e0b57350 2019-03-12 stsp "sendfd unveil", NULL) == -1)
2971 e0b57350 2019-03-12 stsp err(1, "pledge");
2974 d0eebce4 2019-03-11 stsp cwd = getcwd(NULL, 0);
2975 d0eebce4 2019-03-11 stsp if (cwd == NULL) {
2976 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2977 d0eebce4 2019-03-11 stsp goto done;
2980 d0eebce4 2019-03-11 stsp if (repo_path == NULL) {
2981 d0eebce4 2019-03-11 stsp error = got_worktree_open(&worktree, cwd);
2982 d0eebce4 2019-03-11 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
2983 d0eebce4 2019-03-11 stsp goto done;
2985 d0eebce4 2019-03-11 stsp error = NULL;
2986 d0eebce4 2019-03-11 stsp if (worktree) {
2987 d0eebce4 2019-03-11 stsp repo_path =
2988 d0eebce4 2019-03-11 stsp strdup(got_worktree_get_repo_path(worktree));
2989 d0eebce4 2019-03-11 stsp if (repo_path == NULL)
2990 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2991 d0eebce4 2019-03-11 stsp if (error)
2992 d0eebce4 2019-03-11 stsp goto done;
2994 d0eebce4 2019-03-11 stsp repo_path = strdup(cwd);
2995 d0eebce4 2019-03-11 stsp if (repo_path == NULL) {
2996 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2997 d0eebce4 2019-03-11 stsp goto done;
3002 d0eebce4 2019-03-11 stsp error = got_repo_open(&repo, repo_path);
3003 d0eebce4 2019-03-11 stsp if (error != NULL)
3004 d0eebce4 2019-03-11 stsp goto done;
3006 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), do_list,
3007 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
3008 c02c541e 2019-03-29 stsp if (error)
3009 c02c541e 2019-03-29 stsp goto done;
3011 d0eebce4 2019-03-11 stsp if (do_list)
3012 d0eebce4 2019-03-11 stsp error = list_refs(repo);
3013 d0eebce4 2019-03-11 stsp else if (delref)
3014 d0eebce4 2019-03-11 stsp error = delete_ref(repo, delref);
3015 d1c1ae5f 2019-08-12 stsp else if (create_symref)
3016 d1c1ae5f 2019-08-12 stsp error = add_symref(repo, argv[0], argv[1]);
3018 d0eebce4 2019-03-11 stsp error = add_ref(repo, argv[0], argv[1]);
3021 4e759de4 2019-06-26 stsp got_repo_close(repo);
3022 4e759de4 2019-06-26 stsp if (worktree)
3023 4e759de4 2019-06-26 stsp got_worktree_close(worktree);
3024 4e759de4 2019-06-26 stsp free(cwd);
3025 4e759de4 2019-06-26 stsp free(repo_path);
3026 4e759de4 2019-06-26 stsp return error;
3029 4e759de4 2019-06-26 stsp __dead static void
3030 4e759de4 2019-06-26 stsp usage_branch(void)
3032 4e759de4 2019-06-26 stsp fprintf(stderr,
3033 4e759de4 2019-06-26 stsp "usage: %s branch [-r repository] -l | -d name | "
3034 4e759de4 2019-06-26 stsp "name [base-branch]\n", getprogname());
3038 4e759de4 2019-06-26 stsp static const struct got_error *
3039 ba882ee3 2019-07-11 stsp list_branches(struct got_repository *repo, struct got_worktree *worktree)
3041 4e759de4 2019-06-26 stsp static const struct got_error *err = NULL;
3042 4e759de4 2019-06-26 stsp struct got_reflist_head refs;
3043 4e759de4 2019-06-26 stsp struct got_reflist_entry *re;
3045 4e759de4 2019-06-26 stsp SIMPLEQ_INIT(&refs);
3047 4e759de4 2019-06-26 stsp err = got_ref_list(&refs, repo);
3049 4e759de4 2019-06-26 stsp return err;
3051 4e759de4 2019-06-26 stsp SIMPLEQ_FOREACH(re, &refs, entry) {
3052 ba882ee3 2019-07-11 stsp const char *refname, *marker = " ";
3053 4e759de4 2019-06-26 stsp char *refstr;
3054 4e759de4 2019-06-26 stsp refname = got_ref_get_name(re->ref);
3055 4e759de4 2019-06-26 stsp if (strncmp(refname, "refs/heads/", 11) != 0)
3057 ba882ee3 2019-07-11 stsp if (worktree && strcmp(refname,
3058 ba882ee3 2019-07-11 stsp got_worktree_get_head_ref_name(worktree)) == 0) {
3059 ba882ee3 2019-07-11 stsp struct got_object_id *id = NULL;
3060 ba882ee3 2019-07-11 stsp err = got_ref_resolve(&id, repo, re->ref);
3062 ba882ee3 2019-07-11 stsp return err;
3063 ba882ee3 2019-07-11 stsp if (got_object_id_cmp(id,
3064 ba882ee3 2019-07-11 stsp got_worktree_get_base_commit_id(worktree)) == 0)
3065 ba882ee3 2019-07-11 stsp marker = "* ";
3067 ba882ee3 2019-07-11 stsp marker = "~ ";
3070 4e759de4 2019-06-26 stsp refname += 11;
3071 4e759de4 2019-06-26 stsp refstr = got_ref_to_str(re->ref);
3072 4e759de4 2019-06-26 stsp if (refstr == NULL)
3073 4e759de4 2019-06-26 stsp return got_error_from_errno("got_ref_to_str");
3074 ba882ee3 2019-07-11 stsp printf("%s%s: %s\n", marker, refname, refstr);
3075 4e759de4 2019-06-26 stsp free(refstr);
3078 4e759de4 2019-06-26 stsp got_ref_list_free(&refs);
3079 4e759de4 2019-06-26 stsp return NULL;
3082 4e759de4 2019-06-26 stsp static const struct got_error *
3083 4e759de4 2019-06-26 stsp delete_branch(struct got_repository *repo, const char *branch_name)
3085 4e759de4 2019-06-26 stsp const struct got_error *err = NULL;
3086 4e759de4 2019-06-26 stsp struct got_reference *ref;
3087 4e759de4 2019-06-26 stsp char *refname;
3089 4e759de4 2019-06-26 stsp if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
3090 4e759de4 2019-06-26 stsp return got_error_from_errno("asprintf");
3092 4e759de4 2019-06-26 stsp err = got_ref_open(&ref, repo, refname, 0);
3094 4e759de4 2019-06-26 stsp goto done;
3096 4e759de4 2019-06-26 stsp err = got_ref_delete(ref, repo);
3097 4e759de4 2019-06-26 stsp got_ref_close(ref);
3099 4e759de4 2019-06-26 stsp free(refname);
3100 4e759de4 2019-06-26 stsp return err;
3103 4e759de4 2019-06-26 stsp static const struct got_error *
3104 4e759de4 2019-06-26 stsp add_branch(struct got_repository *repo, const char *branch_name,
3105 4e759de4 2019-06-26 stsp const char *base_branch)
3107 4e759de4 2019-06-26 stsp const struct got_error *err = NULL;
3108 4e759de4 2019-06-26 stsp struct got_object_id *id = NULL;
3109 4e759de4 2019-06-26 stsp struct got_reference *ref = NULL;
3110 4e759de4 2019-06-26 stsp char *base_refname = NULL, *refname = NULL;
3111 4e759de4 2019-06-26 stsp struct got_reference *base_ref;
3114 d3f84d51 2019-07-11 stsp * Don't let the user create a branch named '-'.
3115 d3f84d51 2019-07-11 stsp * While technically a valid reference name, this case is usually
3116 d3f84d51 2019-07-11 stsp * an unintended typo.
3118 d3f84d51 2019-07-11 stsp if (branch_name[0] == '-' && branch_name[1] == '\0')
3119 d3f84d51 2019-07-11 stsp return got_error(GOT_ERR_BAD_REF_NAME);
3121 4e759de4 2019-06-26 stsp if (strcmp(GOT_REF_HEAD, base_branch) == 0) {
3122 4e759de4 2019-06-26 stsp base_refname = strdup(GOT_REF_HEAD);
3123 4e759de4 2019-06-26 stsp if (base_refname == NULL)
3124 4e759de4 2019-06-26 stsp return got_error_from_errno("strdup");
3125 4e759de4 2019-06-26 stsp } else if (asprintf(&base_refname, "refs/heads/%s", base_branch) == -1)
3126 4e759de4 2019-06-26 stsp return got_error_from_errno("asprintf");
3128 4e759de4 2019-06-26 stsp err = got_ref_open(&base_ref, repo, base_refname, 0);
3130 4e759de4 2019-06-26 stsp goto done;
3131 4e759de4 2019-06-26 stsp err = got_ref_resolve(&id, repo, base_ref);
3132 4e759de4 2019-06-26 stsp got_ref_close(base_ref);
3134 4e759de4 2019-06-26 stsp goto done;
3136 4e759de4 2019-06-26 stsp if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
3137 4e759de4 2019-06-26 stsp err = got_error_from_errno("asprintf");
3138 4e759de4 2019-06-26 stsp goto done;
3141 4e759de4 2019-06-26 stsp err = got_ref_open(&ref, repo, refname, 0);
3142 4e759de4 2019-06-26 stsp if (err == NULL) {
3143 4e759de4 2019-06-26 stsp err = got_error(GOT_ERR_BRANCH_EXISTS);
3144 4e759de4 2019-06-26 stsp goto done;
3145 4e759de4 2019-06-26 stsp } else if (err->code != GOT_ERR_NOT_REF)
3146 4e759de4 2019-06-26 stsp goto done;
3148 4e759de4 2019-06-26 stsp err = got_ref_alloc(&ref, refname, id);
3150 4e759de4 2019-06-26 stsp goto done;
3152 4e759de4 2019-06-26 stsp err = got_ref_write(ref, repo);
3155 4e759de4 2019-06-26 stsp got_ref_close(ref);
3157 4e759de4 2019-06-26 stsp free(base_refname);
3158 4e759de4 2019-06-26 stsp free(refname);
3159 4e759de4 2019-06-26 stsp return err;
3162 4e759de4 2019-06-26 stsp static const struct got_error *
3163 4e759de4 2019-06-26 stsp cmd_branch(int argc, char *argv[])
3165 4e759de4 2019-06-26 stsp const struct got_error *error = NULL;
3166 4e759de4 2019-06-26 stsp struct got_repository *repo = NULL;
3167 4e759de4 2019-06-26 stsp struct got_worktree *worktree = NULL;
3168 4e759de4 2019-06-26 stsp char *cwd = NULL, *repo_path = NULL;
3169 4e759de4 2019-06-26 stsp int ch, do_list = 0;
3170 4e759de4 2019-06-26 stsp const char *delref = NULL;
3172 4e759de4 2019-06-26 stsp while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
3173 4e759de4 2019-06-26 stsp switch (ch) {
3175 4e759de4 2019-06-26 stsp delref = optarg;
3178 4e759de4 2019-06-26 stsp repo_path = realpath(optarg, NULL);
3179 4e759de4 2019-06-26 stsp if (repo_path == NULL)
3180 4e759de4 2019-06-26 stsp err(1, "-r option");
3181 4e759de4 2019-06-26 stsp got_path_strip_trailing_slashes(repo_path);
3184 4e759de4 2019-06-26 stsp do_list = 1;
3187 4e759de4 2019-06-26 stsp usage_branch();
3188 4e759de4 2019-06-26 stsp /* NOTREACHED */
3192 4e759de4 2019-06-26 stsp if (do_list && delref)
3193 4e759de4 2019-06-26 stsp errx(1, "-l and -d options are mutually exclusive\n");
3195 4e759de4 2019-06-26 stsp argc -= optind;
3196 4e759de4 2019-06-26 stsp argv += optind;
3198 4e759de4 2019-06-26 stsp if (do_list || delref) {
3199 4e759de4 2019-06-26 stsp if (argc > 0)
3200 4e759de4 2019-06-26 stsp usage_branch();
3201 4e759de4 2019-06-26 stsp } else if (argc < 1 || argc > 2)
3202 4e759de4 2019-06-26 stsp usage_branch();
3204 4e759de4 2019-06-26 stsp #ifndef PROFILE
3205 4e759de4 2019-06-26 stsp if (do_list) {
3206 4e759de4 2019-06-26 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3207 4e759de4 2019-06-26 stsp NULL) == -1)
3208 4e759de4 2019-06-26 stsp err(1, "pledge");
3210 4e759de4 2019-06-26 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3211 4e759de4 2019-06-26 stsp "sendfd unveil", NULL) == -1)
3212 4e759de4 2019-06-26 stsp err(1, "pledge");
3215 4e759de4 2019-06-26 stsp cwd = getcwd(NULL, 0);
3216 4e759de4 2019-06-26 stsp if (cwd == NULL) {
3217 4e759de4 2019-06-26 stsp error = got_error_from_errno("getcwd");
3218 4e759de4 2019-06-26 stsp goto done;
3221 4e759de4 2019-06-26 stsp if (repo_path == NULL) {
3222 4e759de4 2019-06-26 stsp error = got_worktree_open(&worktree, cwd);
3223 4e759de4 2019-06-26 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
3224 4e759de4 2019-06-26 stsp goto done;
3226 4e759de4 2019-06-26 stsp error = NULL;
3227 4e759de4 2019-06-26 stsp if (worktree) {
3228 4e759de4 2019-06-26 stsp repo_path =
3229 4e759de4 2019-06-26 stsp strdup(got_worktree_get_repo_path(worktree));
3230 4e759de4 2019-06-26 stsp if (repo_path == NULL)
3231 4e759de4 2019-06-26 stsp error = got_error_from_errno("strdup");
3232 4e759de4 2019-06-26 stsp if (error)
3233 4e759de4 2019-06-26 stsp goto done;
3235 4e759de4 2019-06-26 stsp repo_path = strdup(cwd);
3236 4e759de4 2019-06-26 stsp if (repo_path == NULL) {
3237 4e759de4 2019-06-26 stsp error = got_error_from_errno("strdup");
3238 4e759de4 2019-06-26 stsp goto done;
3243 4e759de4 2019-06-26 stsp error = got_repo_open(&repo, repo_path);
3244 4e759de4 2019-06-26 stsp if (error != NULL)
3245 4e759de4 2019-06-26 stsp goto done;
3247 4e759de4 2019-06-26 stsp error = apply_unveil(got_repo_get_path(repo), do_list,
3248 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
3249 4e759de4 2019-06-26 stsp if (error)
3250 4e759de4 2019-06-26 stsp goto done;
3252 4e759de4 2019-06-26 stsp if (do_list)
3253 ba882ee3 2019-07-11 stsp error = list_branches(repo, worktree);
3254 4e759de4 2019-06-26 stsp else if (delref)
3255 4e759de4 2019-06-26 stsp error = delete_branch(repo, delref);
3257 4e759de4 2019-06-26 stsp const char *base_branch;
3258 4e759de4 2019-06-26 stsp if (argc == 1) {
3259 4e759de4 2019-06-26 stsp base_branch = worktree ?
3260 4e759de4 2019-06-26 stsp got_worktree_get_head_ref_name(worktree) :
3261 4e759de4 2019-06-26 stsp GOT_REF_HEAD;
3262 dc5351b4 2019-07-30 stsp if (strncmp(base_branch, "refs/heads/", 11) == 0)
3263 dc5351b4 2019-07-30 stsp base_branch += 11;
3265 4e759de4 2019-06-26 stsp base_branch = argv[1];
3266 4e759de4 2019-06-26 stsp error = add_branch(repo, argv[0], base_branch);
3270 d0eebce4 2019-03-11 stsp got_repo_close(repo);
3271 d0eebce4 2019-03-11 stsp if (worktree)
3272 d0eebce4 2019-03-11 stsp got_worktree_close(worktree);
3273 d0eebce4 2019-03-11 stsp free(cwd);
3274 d0eebce4 2019-03-11 stsp free(repo_path);
3275 d00136be 2019-03-26 stsp return error;
3278 d00136be 2019-03-26 stsp __dead static void
3279 d00136be 2019-03-26 stsp usage_add(void)
3281 fbb7e5c7 2019-05-11 stsp fprintf(stderr, "usage: %s add file-path ...\n", getprogname());
3285 d00136be 2019-03-26 stsp static const struct got_error *
3286 d00136be 2019-03-26 stsp cmd_add(int argc, char *argv[])
3288 d00136be 2019-03-26 stsp const struct got_error *error = NULL;
3289 031a5338 2019-03-26 stsp struct got_repository *repo = NULL;
3290 d00136be 2019-03-26 stsp struct got_worktree *worktree = NULL;
3291 1dd54920 2019-05-11 stsp char *cwd = NULL;
3292 1dd54920 2019-05-11 stsp struct got_pathlist_head paths;
3293 1dd54920 2019-05-11 stsp struct got_pathlist_entry *pe;
3296 1dd54920 2019-05-11 stsp TAILQ_INIT(&paths);
3298 d00136be 2019-03-26 stsp while ((ch = getopt(argc, argv, "")) != -1) {
3299 d00136be 2019-03-26 stsp switch (ch) {
3301 d00136be 2019-03-26 stsp usage_add();
3302 d00136be 2019-03-26 stsp /* NOTREACHED */
3306 d00136be 2019-03-26 stsp argc -= optind;
3307 d00136be 2019-03-26 stsp argv += optind;
3309 43012d58 2019-07-14 stsp #ifndef PROFILE
3310 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3311 43012d58 2019-07-14 stsp NULL) == -1)
3312 43012d58 2019-07-14 stsp err(1, "pledge");
3314 723c305c 2019-05-11 jcs if (argc < 1)
3315 d00136be 2019-03-26 stsp usage_add();
3317 d00136be 2019-03-26 stsp cwd = getcwd(NULL, 0);
3318 d00136be 2019-03-26 stsp if (cwd == NULL) {
3319 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
3320 d00136be 2019-03-26 stsp goto done;
3323 d00136be 2019-03-26 stsp error = got_worktree_open(&worktree, cwd);
3324 d00136be 2019-03-26 stsp if (error)
3325 d00136be 2019-03-26 stsp goto done;
3327 031a5338 2019-03-26 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3328 031a5338 2019-03-26 stsp if (error != NULL)
3329 031a5338 2019-03-26 stsp goto done;
3331 031a5338 2019-03-26 stsp error = apply_unveil(got_repo_get_path(repo), 1,
3332 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
3333 d00136be 2019-03-26 stsp if (error)
3334 d00136be 2019-03-26 stsp goto done;
3336 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3337 6d022e97 2019-08-04 stsp if (error)
3338 6d022e97 2019-08-04 stsp goto done;
3340 1dd54920 2019-05-11 stsp error = got_worktree_schedule_add(worktree, &paths, print_status,
3341 1dd54920 2019-05-11 stsp NULL, repo);
3344 031a5338 2019-03-26 stsp got_repo_close(repo);
3345 d00136be 2019-03-26 stsp if (worktree)
3346 d00136be 2019-03-26 stsp got_worktree_close(worktree);
3347 1dd54920 2019-05-11 stsp TAILQ_FOREACH(pe, &paths, entry)
3348 1dd54920 2019-05-11 stsp free((char *)pe->path);
3349 1dd54920 2019-05-11 stsp got_pathlist_free(&paths);
3350 2ec1f75b 2019-03-26 stsp free(cwd);
3351 2ec1f75b 2019-03-26 stsp return error;
3354 2ec1f75b 2019-03-26 stsp __dead static void
3355 648e4ef7 2019-07-09 stsp usage_remove(void)
3357 648e4ef7 2019-07-09 stsp fprintf(stderr, "usage: %s remove [-f] file-path ...\n", getprogname());
3361 2ec1f75b 2019-03-26 stsp static const struct got_error *
3362 648e4ef7 2019-07-09 stsp cmd_remove(int argc, char *argv[])
3364 2ec1f75b 2019-03-26 stsp const struct got_error *error = NULL;
3365 2ec1f75b 2019-03-26 stsp struct got_worktree *worktree = NULL;
3366 2ec1f75b 2019-03-26 stsp struct got_repository *repo = NULL;
3367 17ed4618 2019-06-02 stsp char *cwd = NULL;
3368 17ed4618 2019-06-02 stsp struct got_pathlist_head paths;
3369 17ed4618 2019-06-02 stsp struct got_pathlist_entry *pe;
3370 6d022e97 2019-08-04 stsp int ch, delete_local_mods = 0;
3372 17ed4618 2019-06-02 stsp TAILQ_INIT(&paths);
3374 2ec1f75b 2019-03-26 stsp while ((ch = getopt(argc, argv, "f")) != -1) {
3375 2ec1f75b 2019-03-26 stsp switch (ch) {
3377 2ec1f75b 2019-03-26 stsp delete_local_mods = 1;
3380 2ec1f75b 2019-03-26 stsp usage_add();
3381 2ec1f75b 2019-03-26 stsp /* NOTREACHED */
3385 2ec1f75b 2019-03-26 stsp argc -= optind;
3386 2ec1f75b 2019-03-26 stsp argv += optind;
3388 43012d58 2019-07-14 stsp #ifndef PROFILE
3389 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3390 43012d58 2019-07-14 stsp NULL) == -1)
3391 43012d58 2019-07-14 stsp err(1, "pledge");
3393 17ed4618 2019-06-02 stsp if (argc < 1)
3394 648e4ef7 2019-07-09 stsp usage_remove();
3396 2ec1f75b 2019-03-26 stsp cwd = getcwd(NULL, 0);
3397 2ec1f75b 2019-03-26 stsp if (cwd == NULL) {
3398 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
3399 2ec1f75b 2019-03-26 stsp goto done;
3401 2ec1f75b 2019-03-26 stsp error = got_worktree_open(&worktree, cwd);
3402 2ec1f75b 2019-03-26 stsp if (error)
3403 2ec1f75b 2019-03-26 stsp goto done;
3405 2ec1f75b 2019-03-26 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3407 2ec1f75b 2019-03-26 stsp goto done;
3409 c2253644 2019-03-26 stsp error = apply_unveil(got_repo_get_path(repo), 1,
3410 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
3411 2ec1f75b 2019-03-26 stsp if (error)
3412 2ec1f75b 2019-03-26 stsp goto done;
3414 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3415 6d022e97 2019-08-04 stsp if (error)
3416 6d022e97 2019-08-04 stsp goto done;
3418 17ed4618 2019-06-02 stsp error = got_worktree_schedule_delete(worktree, &paths,
3419 17ed4618 2019-06-02 stsp delete_local_mods, print_status, NULL, repo);
3420 a129376b 2019-03-28 stsp if (error)
3421 a129376b 2019-03-28 stsp goto done;
3424 a129376b 2019-03-28 stsp got_repo_close(repo);
3425 a129376b 2019-03-28 stsp if (worktree)
3426 a129376b 2019-03-28 stsp got_worktree_close(worktree);
3427 17ed4618 2019-06-02 stsp TAILQ_FOREACH(pe, &paths, entry)
3428 17ed4618 2019-06-02 stsp free((char *)pe->path);
3429 17ed4618 2019-06-02 stsp got_pathlist_free(&paths);
3430 a129376b 2019-03-28 stsp free(cwd);
3431 a129376b 2019-03-28 stsp return error;
3434 a129376b 2019-03-28 stsp __dead static void
3435 a129376b 2019-03-28 stsp usage_revert(void)
3437 33aa809d 2019-08-08 stsp fprintf(stderr, "usage: %s revert [-p] [-F response-script] [-R] "
3438 33aa809d 2019-08-08 stsp "path ...\n", getprogname());
3442 1ee397ad 2019-07-12 stsp static const struct got_error *
3443 a129376b 2019-03-28 stsp revert_progress(void *arg, unsigned char status, const char *path)
3445 a129376b 2019-03-28 stsp while (path[0] == '/')
3447 a129376b 2019-03-28 stsp printf("%c %s\n", status, path);
3448 33aa809d 2019-08-08 stsp return NULL;
3451 33aa809d 2019-08-08 stsp struct choose_patch_arg {
3452 33aa809d 2019-08-08 stsp FILE *patch_script_file;
3453 33aa809d 2019-08-08 stsp const char *action;
3456 33aa809d 2019-08-08 stsp static const struct got_error *
3457 33aa809d 2019-08-08 stsp show_change(unsigned char status, const char *path, FILE *patch_file, int n,
3458 33aa809d 2019-08-08 stsp int nchanges, const char *action)
3460 33aa809d 2019-08-08 stsp char *line = NULL;
3461 33aa809d 2019-08-08 stsp size_t linesize = 0;
3462 33aa809d 2019-08-08 stsp ssize_t linelen;
3464 33aa809d 2019-08-08 stsp switch (status) {
3465 33aa809d 2019-08-08 stsp case GOT_STATUS_ADD:
3466 33aa809d 2019-08-08 stsp printf("A %s\n%s this addition? [y/n] ", path, action);
3468 33aa809d 2019-08-08 stsp case GOT_STATUS_DELETE:
3469 33aa809d 2019-08-08 stsp printf("D %s\n%s this deletion? [y/n] ", path, action);
3471 33aa809d 2019-08-08 stsp case GOT_STATUS_MODIFY:
3472 33aa809d 2019-08-08 stsp if (fseek(patch_file, 0L, SEEK_SET) == -1)
3473 33aa809d 2019-08-08 stsp return got_error_from_errno("fseek");
3474 33aa809d 2019-08-08 stsp printf(GOT_COMMIT_SEP_STR);
3475 9516e7cb 2019-08-11 stsp while ((linelen = getline(&line, &linesize, patch_file)) != -1)
3476 33aa809d 2019-08-08 stsp printf("%s", line);
3477 33aa809d 2019-08-08 stsp if (ferror(patch_file))
3478 33aa809d 2019-08-08 stsp return got_error_from_errno("getline");
3479 33aa809d 2019-08-08 stsp printf(GOT_COMMIT_SEP_STR);
3480 33aa809d 2019-08-08 stsp printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
3481 33aa809d 2019-08-08 stsp path, n, nchanges, action);
3484 33aa809d 2019-08-08 stsp return got_error_path(path, GOT_ERR_FILE_STATUS);
3487 33aa809d 2019-08-08 stsp return NULL;
3490 33aa809d 2019-08-08 stsp static const struct got_error *
3491 33aa809d 2019-08-08 stsp choose_patch(int *choice, void *arg, unsigned char status, const char *path,
3492 33aa809d 2019-08-08 stsp FILE *patch_file, int n, int nchanges)
3494 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
3495 33aa809d 2019-08-08 stsp char *line = NULL;
3496 33aa809d 2019-08-08 stsp size_t linesize = 0;
3497 33aa809d 2019-08-08 stsp ssize_t linelen;
3498 33aa809d 2019-08-08 stsp int resp = ' ';
3499 33aa809d 2019-08-08 stsp struct choose_patch_arg *a = arg;
3501 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NONE;
3503 33aa809d 2019-08-08 stsp if (a->patch_script_file) {
3505 33aa809d 2019-08-08 stsp err = show_change(status, path, patch_file, n, nchanges,
3506 33aa809d 2019-08-08 stsp a->action);
3508 33aa809d 2019-08-08 stsp return err;
3509 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, a->patch_script_file);
3510 33aa809d 2019-08-08 stsp if (linelen == -1) {
3511 33aa809d 2019-08-08 stsp if (ferror(a->patch_script_file))
3512 33aa809d 2019-08-08 stsp return got_error_from_errno("getline");
3513 33aa809d 2019-08-08 stsp return NULL;
3515 33aa809d 2019-08-08 stsp nl = strchr(line, '\n');
3517 33aa809d 2019-08-08 stsp *nl = '\0';
3518 33aa809d 2019-08-08 stsp if (strcmp(line, "y") == 0) {
3519 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_YES;
3520 33aa809d 2019-08-08 stsp printf("y\n");
3521 33aa809d 2019-08-08 stsp } else if (strcmp(line, "n") == 0) {
3522 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NO;
3523 33aa809d 2019-08-08 stsp printf("n\n");
3524 33aa809d 2019-08-08 stsp } else if (strcmp(line, "q") == 0 &&
3525 33aa809d 2019-08-08 stsp status == GOT_STATUS_MODIFY) {
3526 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_QUIT;
3527 33aa809d 2019-08-08 stsp printf("q\n");
3529 33aa809d 2019-08-08 stsp printf("invalid response '%s'\n", line);
3530 33aa809d 2019-08-08 stsp free(line);
3531 33aa809d 2019-08-08 stsp return NULL;
3534 33aa809d 2019-08-08 stsp while (resp != 'y' && resp != 'n' && resp != 'q') {
3535 33aa809d 2019-08-08 stsp err = show_change(status, path, patch_file, n, nchanges,
3536 33aa809d 2019-08-08 stsp a->action);
3538 33aa809d 2019-08-08 stsp return err;
3539 33aa809d 2019-08-08 stsp resp = getchar();
3540 33aa809d 2019-08-08 stsp if (resp == '\n')
3541 33aa809d 2019-08-08 stsp resp = getchar();
3542 33aa809d 2019-08-08 stsp if (status == GOT_STATUS_MODIFY) {
3543 33aa809d 2019-08-08 stsp if (resp != 'y' && resp != 'n' && resp != 'q') {
3544 33aa809d 2019-08-08 stsp printf("invalid response '%c'\n", resp);
3545 33aa809d 2019-08-08 stsp resp = ' ';
3547 33aa809d 2019-08-08 stsp } else if (resp != 'y' && resp != 'n') {
3548 33aa809d 2019-08-08 stsp printf("invalid response '%c'\n", resp);
3549 33aa809d 2019-08-08 stsp resp = ' ';
3553 33aa809d 2019-08-08 stsp if (resp == 'y')
3554 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_YES;
3555 33aa809d 2019-08-08 stsp else if (resp == 'n')
3556 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NO;
3557 33aa809d 2019-08-08 stsp else if (resp == 'q' && status == GOT_STATUS_MODIFY)
3558 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_QUIT;
3560 1ee397ad 2019-07-12 stsp return NULL;
3564 a129376b 2019-03-28 stsp static const struct got_error *
3565 a129376b 2019-03-28 stsp cmd_revert(int argc, char *argv[])
3567 a129376b 2019-03-28 stsp const struct got_error *error = NULL;
3568 a129376b 2019-03-28 stsp struct got_worktree *worktree = NULL;
3569 a129376b 2019-03-28 stsp struct got_repository *repo = NULL;
3570 a129376b 2019-03-28 stsp char *cwd = NULL, *path = NULL;
3571 e20a8b6f 2019-06-04 stsp struct got_pathlist_head paths;
3572 0f6d7415 2019-08-08 stsp struct got_pathlist_entry *pe;
3573 33aa809d 2019-08-08 stsp int ch, can_recurse = 0, pflag = 0;
3574 33aa809d 2019-08-08 stsp FILE *patch_script_file = NULL;
3575 33aa809d 2019-08-08 stsp const char *patch_script_path = NULL;
3576 33aa809d 2019-08-08 stsp struct choose_patch_arg cpa;
3578 e20a8b6f 2019-06-04 stsp TAILQ_INIT(&paths);
3580 33aa809d 2019-08-08 stsp while ((ch = getopt(argc, argv, "pF:R")) != -1) {
3581 a129376b 2019-03-28 stsp switch (ch) {
3583 33aa809d 2019-08-08 stsp pflag = 1;
3586 33aa809d 2019-08-08 stsp patch_script_path = optarg;
3589 0f6d7415 2019-08-08 stsp can_recurse = 1;
3592 a129376b 2019-03-28 stsp usage_revert();
3593 a129376b 2019-03-28 stsp /* NOTREACHED */
3597 a129376b 2019-03-28 stsp argc -= optind;
3598 a129376b 2019-03-28 stsp argv += optind;
3600 43012d58 2019-07-14 stsp #ifndef PROFILE
3601 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3602 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
3603 43012d58 2019-07-14 stsp err(1, "pledge");
3605 e20a8b6f 2019-06-04 stsp if (argc < 1)
3606 a129376b 2019-03-28 stsp usage_revert();
3607 33aa809d 2019-08-08 stsp if (patch_script_path && !pflag)
3608 33aa809d 2019-08-08 stsp errx(1, "-F option can only be used together with -p option");
3610 a129376b 2019-03-28 stsp cwd = getcwd(NULL, 0);
3611 a129376b 2019-03-28 stsp if (cwd == NULL) {
3612 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
3613 a129376b 2019-03-28 stsp goto done;
3615 a129376b 2019-03-28 stsp error = got_worktree_open(&worktree, cwd);
3616 2ec1f75b 2019-03-26 stsp if (error)
3617 2ec1f75b 2019-03-26 stsp goto done;
3619 a129376b 2019-03-28 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3620 a129376b 2019-03-28 stsp if (error != NULL)
3621 a129376b 2019-03-28 stsp goto done;
3623 33aa809d 2019-08-08 stsp if (patch_script_path) {
3624 33aa809d 2019-08-08 stsp patch_script_file = fopen(patch_script_path, "r");
3625 33aa809d 2019-08-08 stsp if (patch_script_file == NULL) {
3626 33aa809d 2019-08-08 stsp error = got_error_from_errno2("fopen",
3627 33aa809d 2019-08-08 stsp patch_script_path);
3628 33aa809d 2019-08-08 stsp goto done;
3631 a129376b 2019-03-28 stsp error = apply_unveil(got_repo_get_path(repo), 1,
3632 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
3633 a129376b 2019-03-28 stsp if (error)
3634 a129376b 2019-03-28 stsp goto done;
3636 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3637 6d022e97 2019-08-04 stsp if (error)
3638 6d022e97 2019-08-04 stsp goto done;
3640 0f6d7415 2019-08-08 stsp if (!can_recurse) {
3641 0f6d7415 2019-08-08 stsp char *ondisk_path;
3642 0f6d7415 2019-08-08 stsp struct stat sb;
3643 0f6d7415 2019-08-08 stsp TAILQ_FOREACH(pe, &paths, entry) {
3644 0f6d7415 2019-08-08 stsp if (asprintf(&ondisk_path, "%s/%s",
3645 0f6d7415 2019-08-08 stsp got_worktree_get_root_path(worktree),
3646 0f6d7415 2019-08-08 stsp pe->path) == -1) {
3647 0f6d7415 2019-08-08 stsp error = got_error_from_errno("asprintf");
3648 0f6d7415 2019-08-08 stsp goto done;
3650 0f6d7415 2019-08-08 stsp if (lstat(ondisk_path, &sb) == -1) {
3651 0f6d7415 2019-08-08 stsp if (errno == ENOENT) {
3652 0f6d7415 2019-08-08 stsp free(ondisk_path);
3655 0f6d7415 2019-08-08 stsp error = got_error_from_errno2("lstat",
3656 0f6d7415 2019-08-08 stsp ondisk_path);
3657 0f6d7415 2019-08-08 stsp free(ondisk_path);
3658 0f6d7415 2019-08-08 stsp goto done;
3660 0f6d7415 2019-08-08 stsp free(ondisk_path);
3661 0f6d7415 2019-08-08 stsp if (S_ISDIR(sb.st_mode)) {
3662 0f6d7415 2019-08-08 stsp error = got_error_msg(GOT_ERR_BAD_PATH,
3663 0f6d7415 2019-08-08 stsp "reverting directories requires -R option");
3664 0f6d7415 2019-08-08 stsp goto done;
3669 33aa809d 2019-08-08 stsp cpa.patch_script_file = patch_script_file;
3670 33aa809d 2019-08-08 stsp cpa.action = "revert";
3671 33aa809d 2019-08-08 stsp error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
3672 33aa809d 2019-08-08 stsp pflag ? choose_patch : NULL, &cpa, repo);
3673 a129376b 2019-03-28 stsp if (error)
3674 a129376b 2019-03-28 stsp goto done;
3676 33aa809d 2019-08-08 stsp if (patch_script_file && fclose(patch_script_file) == EOF &&
3677 33aa809d 2019-08-08 stsp error == NULL)
3678 33aa809d 2019-08-08 stsp error = got_error_from_errno2("fclose", patch_script_path);
3680 2ec1f75b 2019-03-26 stsp got_repo_close(repo);
3681 2ec1f75b 2019-03-26 stsp if (worktree)
3682 2ec1f75b 2019-03-26 stsp got_worktree_close(worktree);
3683 2ec1f75b 2019-03-26 stsp free(path);
3684 d00136be 2019-03-26 stsp free(cwd);
3685 6bad629b 2019-02-04 stsp return error;
3688 c4296144 2019-05-09 stsp __dead static void
3689 c4296144 2019-05-09 stsp usage_commit(void)
3691 5c1e53bc 2019-07-28 stsp fprintf(stderr, "usage: %s commit [-m msg] [path ...]\n",
3692 5c1e53bc 2019-07-28 stsp getprogname());
3696 e2ba3d07 2019-05-13 stsp struct collect_commit_logmsg_arg {
3697 e2ba3d07 2019-05-13 stsp const char *cmdline_log;
3698 e2ba3d07 2019-05-13 stsp const char *editor;
3699 e0870e44 2019-05-13 stsp const char *worktree_path;
3700 76d98825 2019-06-03 stsp const char *branch_name;
3701 314a6357 2019-05-13 stsp const char *repo_path;
3702 e0870e44 2019-05-13 stsp char *logmsg_path;
3706 33ad4cbe 2019-05-12 jcs static const struct got_error *
3707 33ad4cbe 2019-05-12 jcs collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
3710 76d98825 2019-06-03 stsp char *initial_content = NULL;
3711 33ad4cbe 2019-05-12 jcs struct got_pathlist_entry *pe;
3712 33ad4cbe 2019-05-12 jcs const struct got_error *err = NULL;
3713 e0870e44 2019-05-13 stsp char *template = NULL;
3714 e2ba3d07 2019-05-13 stsp struct collect_commit_logmsg_arg *a = arg;
3716 33ad4cbe 2019-05-12 jcs size_t len;
3718 33ad4cbe 2019-05-12 jcs /* if a message was specified on the command line, just use it */
3719 e2ba3d07 2019-05-13 stsp if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
3720 e2ba3d07 2019-05-13 stsp len = strlen(a->cmdline_log) + 1;
3721 33ad4cbe 2019-05-12 jcs *logmsg = malloc(len + 1);
3722 9f42ff69 2019-05-13 stsp if (*logmsg == NULL)
3723 638f9024 2019-05-13 stsp return got_error_from_errno("malloc");
3724 e2ba3d07 2019-05-13 stsp strlcpy(*logmsg, a->cmdline_log, len);
3725 33ad4cbe 2019-05-12 jcs return NULL;
3728 e0870e44 2019-05-13 stsp if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
3729 76d98825 2019-06-03 stsp return got_error_from_errno("asprintf");
3731 76d98825 2019-06-03 stsp if (asprintf(&initial_content,
3732 76d98825 2019-06-03 stsp "\n# changes to be committed on branch %s:\n",
3733 76d98825 2019-06-03 stsp a->branch_name) == -1)
3734 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3736 e0870e44 2019-05-13 stsp err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
3738 e0870e44 2019-05-13 stsp goto done;
3740 e0870e44 2019-05-13 stsp dprintf(fd, initial_content);
3742 33ad4cbe 2019-05-12 jcs TAILQ_FOREACH(pe, commitable_paths, entry) {
3743 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
3744 8656d6c4 2019-05-20 stsp dprintf(fd, "# %c %s\n",
3745 8656d6c4 2019-05-20 stsp got_commitable_get_status(ct),
3746 8656d6c4 2019-05-20 stsp got_commitable_get_path(ct));
3750 3ce1b845 2019-07-15 stsp err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content);
3752 e2af0fd1 2019-08-08 stsp if (err == NULL || err->code == GOT_ERR_COMMIT_MSG_EMPTY) {
3753 e2af0fd1 2019-08-08 stsp unlink(a->logmsg_path);
3754 e2af0fd1 2019-08-08 stsp free(a->logmsg_path);
3755 e2af0fd1 2019-08-08 stsp a->logmsg_path = NULL;
3757 76d98825 2019-06-03 stsp free(initial_content);
3758 e0870e44 2019-05-13 stsp free(template);
3760 314a6357 2019-05-13 stsp /* Editor is done; we can now apply unveil(2) */
3761 3ce1b845 2019-07-15 stsp if (err == NULL) {
3762 c530dc23 2019-07-23 stsp err = apply_unveil(a->repo_path, 0, a->worktree_path);
3763 3ce1b845 2019-07-15 stsp if (err) {
3764 3ce1b845 2019-07-15 stsp free(*logmsg);
3765 3ce1b845 2019-07-15 stsp *logmsg = NULL;
3768 33ad4cbe 2019-05-12 jcs return err;
3771 c4296144 2019-05-09 stsp static const struct got_error *
3772 c4296144 2019-05-09 stsp cmd_commit(int argc, char *argv[])
3774 c4296144 2019-05-09 stsp const struct got_error *error = NULL;
3775 c4296144 2019-05-09 stsp struct got_worktree *worktree = NULL;
3776 c4296144 2019-05-09 stsp struct got_repository *repo = NULL;
3777 5c1e53bc 2019-07-28 stsp char *cwd = NULL, *id_str = NULL;
3778 c4296144 2019-05-09 stsp struct got_object_id *id = NULL;
3779 33ad4cbe 2019-05-12 jcs const char *logmsg = NULL;
3780 84792843 2019-08-09 stsp const char *author;
3781 e2ba3d07 2019-05-13 stsp struct collect_commit_logmsg_arg cl_arg;
3782 e2ba3d07 2019-05-13 stsp char *editor = NULL;
3783 916f288c 2019-07-30 stsp int ch, rebase_in_progress, histedit_in_progress;
3784 5c1e53bc 2019-07-28 stsp struct got_pathlist_head paths;
3786 5c1e53bc 2019-07-28 stsp TAILQ_INIT(&paths);
3787 6ac5a73c 2019-08-08 stsp cl_arg.logmsg_path = NULL;
3789 c4296144 2019-05-09 stsp while ((ch = getopt(argc, argv, "m:")) != -1) {
3790 c4296144 2019-05-09 stsp switch (ch) {
3792 c4296144 2019-05-09 stsp logmsg = optarg;
3795 c4296144 2019-05-09 stsp usage_commit();
3796 c4296144 2019-05-09 stsp /* NOTREACHED */
3800 c4296144 2019-05-09 stsp argc -= optind;
3801 c4296144 2019-05-09 stsp argv += optind;
3803 43012d58 2019-07-14 stsp #ifndef PROFILE
3804 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3805 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
3806 43012d58 2019-07-14 stsp err(1, "pledge");
3808 84792843 2019-08-09 stsp error = get_author(&author);
3809 84792843 2019-08-09 stsp if (error)
3810 84792843 2019-08-09 stsp return error;
3812 c4296144 2019-05-09 stsp cwd = getcwd(NULL, 0);
3813 c4296144 2019-05-09 stsp if (cwd == NULL) {
3814 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
3815 c4296144 2019-05-09 stsp goto done;
3817 c4296144 2019-05-09 stsp error = got_worktree_open(&worktree, cwd);
3818 7d5807f4 2019-07-11 stsp if (error)
3819 7d5807f4 2019-07-11 stsp goto done;
3821 a698f62e 2019-07-25 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
3822 c4296144 2019-05-09 stsp if (error)
3823 a698f62e 2019-07-25 stsp goto done;
3824 a698f62e 2019-07-25 stsp if (rebase_in_progress) {
3825 a698f62e 2019-07-25 stsp error = got_error(GOT_ERR_REBASING);
3826 c4296144 2019-05-09 stsp goto done;
3829 916f288c 2019-07-30 stsp error = got_worktree_histedit_in_progress(&histedit_in_progress,
3830 916f288c 2019-07-30 stsp worktree);
3831 5c1e53bc 2019-07-28 stsp if (error)
3832 5c1e53bc 2019-07-28 stsp goto done;
3834 c4296144 2019-05-09 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3835 c4296144 2019-05-09 stsp if (error != NULL)
3836 c4296144 2019-05-09 stsp goto done;
3839 314a6357 2019-05-13 stsp * unveil(2) traverses exec(2); if an editor is used we have
3840 314a6357 2019-05-13 stsp * to apply unveil after the log message has been written.
3842 314a6357 2019-05-13 stsp if (logmsg == NULL || strlen(logmsg) == 0)
3843 314a6357 2019-05-13 stsp error = get_editor(&editor);
3845 314a6357 2019-05-13 stsp error = apply_unveil(got_repo_get_path(repo), 0,
3846 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
3847 c4296144 2019-05-09 stsp if (error)
3848 c4296144 2019-05-09 stsp goto done;
3850 087fb88c 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3851 087fb88c 2019-08-04 stsp if (error)
3852 087fb88c 2019-08-04 stsp goto done;
3854 e2ba3d07 2019-05-13 stsp cl_arg.editor = editor;
3855 e2ba3d07 2019-05-13 stsp cl_arg.cmdline_log = logmsg;
3856 e0870e44 2019-05-13 stsp cl_arg.worktree_path = got_worktree_get_root_path(worktree);
3857 76d98825 2019-06-03 stsp cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
3858 916f288c 2019-07-30 stsp if (!histedit_in_progress) {
3859 916f288c 2019-07-30 stsp if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
3860 916f288c 2019-07-30 stsp error = got_error(GOT_ERR_COMMIT_BRANCH);
3861 916f288c 2019-07-30 stsp goto done;
3863 916f288c 2019-07-30 stsp cl_arg.branch_name += 11;
3865 314a6357 2019-05-13 stsp cl_arg.repo_path = got_repo_get_path(repo);
3866 84792843 2019-08-09 stsp error = got_worktree_commit(&id, worktree, &paths, author, NULL,
3867 e2ba3d07 2019-05-13 stsp collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
3868 e0870e44 2019-05-13 stsp if (error) {
3869 e0870e44 2019-05-13 stsp if (cl_arg.logmsg_path)
3870 e0870e44 2019-05-13 stsp fprintf(stderr, "%s: log message preserved in %s\n",
3871 e0870e44 2019-05-13 stsp getprogname(), cl_arg.logmsg_path);
3872 c4296144 2019-05-09 stsp goto done;
3875 e0870e44 2019-05-13 stsp if (cl_arg.logmsg_path)
3876 e0870e44 2019-05-13 stsp unlink(cl_arg.logmsg_path);
3878 c4296144 2019-05-09 stsp error = got_object_id_str(&id_str, id);
3879 c4296144 2019-05-09 stsp if (error)
3880 c4296144 2019-05-09 stsp goto done;
3881 a7648d7a 2019-06-02 stsp printf("Created commit %s\n", id_str);
3883 6ac5a73c 2019-08-08 stsp free(cl_arg.logmsg_path);
3885 c4296144 2019-05-09 stsp got_repo_close(repo);
3886 c4296144 2019-05-09 stsp if (worktree)
3887 c4296144 2019-05-09 stsp got_worktree_close(worktree);
3888 c4296144 2019-05-09 stsp free(cwd);
3889 c4296144 2019-05-09 stsp free(id_str);
3890 e2ba3d07 2019-05-13 stsp free(editor);
3891 234035bc 2019-06-01 stsp return error;
3894 234035bc 2019-06-01 stsp __dead static void
3895 234035bc 2019-06-01 stsp usage_cherrypick(void)
3897 234035bc 2019-06-01 stsp fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
3901 234035bc 2019-06-01 stsp static const struct got_error *
3902 234035bc 2019-06-01 stsp cmd_cherrypick(int argc, char *argv[])
3904 234035bc 2019-06-01 stsp const struct got_error *error = NULL;
3905 234035bc 2019-06-01 stsp struct got_worktree *worktree = NULL;
3906 234035bc 2019-06-01 stsp struct got_repository *repo = NULL;
3907 234035bc 2019-06-01 stsp char *cwd = NULL, *commit_id_str = NULL;
3908 234035bc 2019-06-01 stsp struct got_object_id *commit_id = NULL;
3909 234035bc 2019-06-01 stsp struct got_commit_object *commit = NULL;
3910 234035bc 2019-06-01 stsp struct got_object_qid *pid;
3911 234035bc 2019-06-01 stsp struct got_reference *head_ref = NULL;
3912 234035bc 2019-06-01 stsp int ch, did_something = 0;
3914 234035bc 2019-06-01 stsp while ((ch = getopt(argc, argv, "")) != -1) {
3915 234035bc 2019-06-01 stsp switch (ch) {
3917 234035bc 2019-06-01 stsp usage_cherrypick();
3918 234035bc 2019-06-01 stsp /* NOTREACHED */
3922 234035bc 2019-06-01 stsp argc -= optind;
3923 234035bc 2019-06-01 stsp argv += optind;
3925 43012d58 2019-07-14 stsp #ifndef PROFILE
3926 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3927 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
3928 43012d58 2019-07-14 stsp err(1, "pledge");
3930 234035bc 2019-06-01 stsp if (argc != 1)
3931 234035bc 2019-06-01 stsp usage_cherrypick();
3933 234035bc 2019-06-01 stsp cwd = getcwd(NULL, 0);
3934 234035bc 2019-06-01 stsp if (cwd == NULL) {
3935 234035bc 2019-06-01 stsp error = got_error_from_errno("getcwd");
3936 234035bc 2019-06-01 stsp goto done;
3938 234035bc 2019-06-01 stsp error = got_worktree_open(&worktree, cwd);
3939 234035bc 2019-06-01 stsp if (error)
3940 234035bc 2019-06-01 stsp goto done;
3942 234035bc 2019-06-01 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3943 234035bc 2019-06-01 stsp if (error != NULL)
3944 234035bc 2019-06-01 stsp goto done;
3946 234035bc 2019-06-01 stsp error = apply_unveil(got_repo_get_path(repo), 0,
3947 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
3948 234035bc 2019-06-01 stsp if (error)
3949 234035bc 2019-06-01 stsp goto done;
3951 dd88155e 2019-06-29 stsp error = got_repo_match_object_id_prefix(&commit_id, argv[0],
3952 dd88155e 2019-06-29 stsp GOT_OBJ_TYPE_COMMIT, repo);
3953 234035bc 2019-06-01 stsp if (error != NULL) {
3954 234035bc 2019-06-01 stsp struct got_reference *ref;
3955 234035bc 2019-06-01 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
3956 234035bc 2019-06-01 stsp goto done;
3957 234035bc 2019-06-01 stsp error = got_ref_open(&ref, repo, argv[0], 0);
3958 234035bc 2019-06-01 stsp if (error != NULL)
3959 234035bc 2019-06-01 stsp goto done;
3960 234035bc 2019-06-01 stsp error = got_ref_resolve(&commit_id, repo, ref);
3961 234035bc 2019-06-01 stsp got_ref_close(ref);
3962 234035bc 2019-06-01 stsp if (error != NULL)
3963 234035bc 2019-06-01 stsp goto done;
3965 234035bc 2019-06-01 stsp error = got_object_id_str(&commit_id_str, commit_id);
3966 234035bc 2019-06-01 stsp if (error)
3967 234035bc 2019-06-01 stsp goto done;
3969 234035bc 2019-06-01 stsp error = got_ref_open(&head_ref, repo,
3970 234035bc 2019-06-01 stsp got_worktree_get_head_ref_name(worktree), 0);
3971 234035bc 2019-06-01 stsp if (error != NULL)
3972 234035bc 2019-06-01 stsp goto done;
3974 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
3975 234035bc 2019-06-01 stsp if (error) {
3976 234035bc 2019-06-01 stsp if (error->code != GOT_ERR_ANCESTRY)
3977 234035bc 2019-06-01 stsp goto done;
3978 234035bc 2019-06-01 stsp error = NULL;
3980 234035bc 2019-06-01 stsp error = got_error(GOT_ERR_SAME_BRANCH);
3981 234035bc 2019-06-01 stsp goto done;
3984 234035bc 2019-06-01 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
3985 234035bc 2019-06-01 stsp if (error)
3986 234035bc 2019-06-01 stsp goto done;
3987 234035bc 2019-06-01 stsp pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3988 03415a1a 2019-06-02 stsp error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
3989 03415a1a 2019-06-02 stsp commit_id, repo, update_progress, &did_something, check_cancelled,
3991 234035bc 2019-06-01 stsp if (error != NULL)
3992 234035bc 2019-06-01 stsp goto done;
3994 234035bc 2019-06-01 stsp if (did_something)
3995 a7648d7a 2019-06-02 stsp printf("Merged commit %s\n", commit_id_str);
3997 234035bc 2019-06-01 stsp if (commit)
3998 234035bc 2019-06-01 stsp got_object_commit_close(commit);
3999 234035bc 2019-06-01 stsp free(commit_id_str);
4000 234035bc 2019-06-01 stsp if (head_ref)
4001 234035bc 2019-06-01 stsp got_ref_close(head_ref);
4002 234035bc 2019-06-01 stsp if (worktree)
4003 234035bc 2019-06-01 stsp got_worktree_close(worktree);
4005 234035bc 2019-06-01 stsp got_repo_close(repo);
4006 c4296144 2019-05-09 stsp return error;
4009 5ef14e63 2019-06-02 stsp __dead static void
4010 5ef14e63 2019-06-02 stsp usage_backout(void)
4012 5ef14e63 2019-06-02 stsp fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
4016 5ef14e63 2019-06-02 stsp static const struct got_error *
4017 5ef14e63 2019-06-02 stsp cmd_backout(int argc, char *argv[])
4019 5ef14e63 2019-06-02 stsp const struct got_error *error = NULL;
4020 5ef14e63 2019-06-02 stsp struct got_worktree *worktree = NULL;
4021 5ef14e63 2019-06-02 stsp struct got_repository *repo = NULL;
4022 5ef14e63 2019-06-02 stsp char *cwd = NULL, *commit_id_str = NULL;
4023 5ef14e63 2019-06-02 stsp struct got_object_id *commit_id = NULL;
4024 5ef14e63 2019-06-02 stsp struct got_commit_object *commit = NULL;
4025 5ef14e63 2019-06-02 stsp struct got_object_qid *pid;
4026 5ef14e63 2019-06-02 stsp struct got_reference *head_ref = NULL;
4027 5ef14e63 2019-06-02 stsp int ch, did_something = 0;
4029 5ef14e63 2019-06-02 stsp while ((ch = getopt(argc, argv, "")) != -1) {
4030 5ef14e63 2019-06-02 stsp switch (ch) {
4032 5ef14e63 2019-06-02 stsp usage_backout();
4033 5ef14e63 2019-06-02 stsp /* NOTREACHED */
4037 5ef14e63 2019-06-02 stsp argc -= optind;
4038 5ef14e63 2019-06-02 stsp argv += optind;
4040 43012d58 2019-07-14 stsp #ifndef PROFILE
4041 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4042 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
4043 43012d58 2019-07-14 stsp err(1, "pledge");
4045 5ef14e63 2019-06-02 stsp if (argc != 1)
4046 5ef14e63 2019-06-02 stsp usage_backout();
4048 5ef14e63 2019-06-02 stsp cwd = getcwd(NULL, 0);
4049 5ef14e63 2019-06-02 stsp if (cwd == NULL) {
4050 5ef14e63 2019-06-02 stsp error = got_error_from_errno("getcwd");
4051 5ef14e63 2019-06-02 stsp goto done;
4053 5ef14e63 2019-06-02 stsp error = got_worktree_open(&worktree, cwd);
4054 5ef14e63 2019-06-02 stsp if (error)
4055 5ef14e63 2019-06-02 stsp goto done;
4057 5ef14e63 2019-06-02 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
4058 5ef14e63 2019-06-02 stsp if (error != NULL)
4059 5ef14e63 2019-06-02 stsp goto done;
4061 5ef14e63 2019-06-02 stsp error = apply_unveil(got_repo_get_path(repo), 0,
4062 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
4063 5ef14e63 2019-06-02 stsp if (error)
4064 5ef14e63 2019-06-02 stsp goto done;
4066 dd88155e 2019-06-29 stsp error = got_repo_match_object_id_prefix(&commit_id, argv[0],
4067 dd88155e 2019-06-29 stsp GOT_OBJ_TYPE_COMMIT, repo);
4068 5ef14e63 2019-06-02 stsp if (error != NULL) {
4069 5ef14e63 2019-06-02 stsp struct got_reference *ref;
4070 5ef14e63 2019-06-02 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
4071 5ef14e63 2019-06-02 stsp goto done;
4072 5ef14e63 2019-06-02 stsp error = got_ref_open(&ref, repo, argv[0], 0);
4073 5ef14e63 2019-06-02 stsp if (error != NULL)
4074 5ef14e63 2019-06-02 stsp goto done;
4075 5ef14e63 2019-06-02 stsp error = got_ref_resolve(&commit_id, repo, ref);
4076 5ef14e63 2019-06-02 stsp got_ref_close(ref);
4077 5ef14e63 2019-06-02 stsp if (error != NULL)
4078 5ef14e63 2019-06-02 stsp goto done;
4080 5ef14e63 2019-06-02 stsp error = got_object_id_str(&commit_id_str, commit_id);
4081 5ef14e63 2019-06-02 stsp if (error)
4082 5ef14e63 2019-06-02 stsp goto done;
4084 5ef14e63 2019-06-02 stsp error = got_ref_open(&head_ref, repo,
4085 5ef14e63 2019-06-02 stsp got_worktree_get_head_ref_name(worktree), 0);
4086 5ef14e63 2019-06-02 stsp if (error != NULL)
4087 5ef14e63 2019-06-02 stsp goto done;
4089 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
4090 5ef14e63 2019-06-02 stsp if (error)
4091 5ef14e63 2019-06-02 stsp goto done;
4093 5ef14e63 2019-06-02 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
4094 5ef14e63 2019-06-02 stsp if (error)
4095 5ef14e63 2019-06-02 stsp goto done;
4096 5ef14e63 2019-06-02 stsp pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
4097 5ef14e63 2019-06-02 stsp if (pid == NULL) {
4098 5ef14e63 2019-06-02 stsp error = got_error(GOT_ERR_ROOT_COMMIT);
4099 5ef14e63 2019-06-02 stsp goto done;
4102 5ef14e63 2019-06-02 stsp error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
4103 5ef14e63 2019-06-02 stsp update_progress, &did_something, check_cancelled, NULL);
4104 5ef14e63 2019-06-02 stsp if (error != NULL)
4105 5ef14e63 2019-06-02 stsp goto done;
4107 5ef14e63 2019-06-02 stsp if (did_something)
4108 a7648d7a 2019-06-02 stsp printf("Backed out commit %s\n", commit_id_str);
4110 5ef14e63 2019-06-02 stsp if (commit)
4111 5ef14e63 2019-06-02 stsp got_object_commit_close(commit);
4112 5ef14e63 2019-06-02 stsp free(commit_id_str);
4113 5ef14e63 2019-06-02 stsp if (head_ref)
4114 5ef14e63 2019-06-02 stsp got_ref_close(head_ref);
4115 818c7501 2019-07-11 stsp if (worktree)
4116 818c7501 2019-07-11 stsp got_worktree_close(worktree);
4118 818c7501 2019-07-11 stsp got_repo_close(repo);
4119 818c7501 2019-07-11 stsp return error;
4122 818c7501 2019-07-11 stsp __dead static void
4123 818c7501 2019-07-11 stsp usage_rebase(void)
4125 af54c8f8 2019-07-11 stsp fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
4126 af54c8f8 2019-07-11 stsp getprogname());
4131 0ebf8283 2019-07-24 stsp trim_logmsg(char *logmsg, int limit)
4134 0ebf8283 2019-07-24 stsp size_t len;
4136 0ebf8283 2019-07-24 stsp len = strlen(logmsg);
4137 0ebf8283 2019-07-24 stsp if (len > limit)
4138 0ebf8283 2019-07-24 stsp len = limit;
4139 0ebf8283 2019-07-24 stsp logmsg[len] = '\0';
4140 0ebf8283 2019-07-24 stsp nl = strchr(logmsg, '\n');
4142 0ebf8283 2019-07-24 stsp *nl = '\0';
4145 0ebf8283 2019-07-24 stsp static const struct got_error *
4146 0ebf8283 2019-07-24 stsp get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
4148 5943eee2 2019-08-13 stsp const struct got_error *err;
4149 5943eee2 2019-08-13 stsp char *logmsg0 = NULL;
4150 5943eee2 2019-08-13 stsp const char *s;
4152 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
4154 5943eee2 2019-08-13 stsp return err;
4156 5943eee2 2019-08-13 stsp s = logmsg0;
4157 5943eee2 2019-08-13 stsp while (isspace((unsigned char)s[0]))
4160 5943eee2 2019-08-13 stsp *logmsg = strdup(s);
4161 5943eee2 2019-08-13 stsp if (*logmsg == NULL) {
4162 5943eee2 2019-08-13 stsp err = got_error_from_errno("strdup");
4163 5943eee2 2019-08-13 stsp goto done;
4166 0ebf8283 2019-07-24 stsp trim_logmsg(*logmsg, limit);
4168 5943eee2 2019-08-13 stsp free(logmsg0);
4169 5943eee2 2019-08-13 stsp return err;
4172 818c7501 2019-07-11 stsp static const struct got_error *
4173 818c7501 2019-07-11 stsp show_rebase_progress(struct got_commit_object *commit,
4174 818c7501 2019-07-11 stsp struct got_object_id *old_id, struct got_object_id *new_id)
4176 818c7501 2019-07-11 stsp const struct got_error *err;
4177 0ebf8283 2019-07-24 stsp char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
4179 818c7501 2019-07-11 stsp err = got_object_id_str(&old_id_str, old_id);
4181 818c7501 2019-07-11 stsp goto done;
4183 ff0d2220 2019-07-11 stsp if (new_id) {
4184 ff0d2220 2019-07-11 stsp err = got_object_id_str(&new_id_str, new_id);
4186 ff0d2220 2019-07-11 stsp goto done;
4189 818c7501 2019-07-11 stsp old_id_str[12] = '\0';
4190 ff0d2220 2019-07-11 stsp if (new_id_str)
4191 ff0d2220 2019-07-11 stsp new_id_str[12] = '\0';
4193 0ebf8283 2019-07-24 stsp err = get_short_logmsg(&logmsg, 42, commit);
4195 0ebf8283 2019-07-24 stsp goto done;
4197 ff0d2220 2019-07-11 stsp printf("%s -> %s: %s\n", old_id_str,
4198 ff0d2220 2019-07-11 stsp new_id_str ? new_id_str : "no-op change", logmsg);
4200 818c7501 2019-07-11 stsp free(old_id_str);
4201 818c7501 2019-07-11 stsp free(new_id_str);
4202 818c7501 2019-07-11 stsp return err;
4205 1ee397ad 2019-07-12 stsp static const struct got_error *
4206 818c7501 2019-07-11 stsp rebase_progress(void *arg, unsigned char status, const char *path)
4208 818c7501 2019-07-11 stsp unsigned char *rebase_status = arg;
4210 818c7501 2019-07-11 stsp while (path[0] == '/')
4212 818c7501 2019-07-11 stsp printf("%c %s\n", status, path);
4214 818c7501 2019-07-11 stsp if (*rebase_status == GOT_STATUS_CONFLICT)
4215 1ee397ad 2019-07-12 stsp return NULL;
4216 818c7501 2019-07-11 stsp if (status == GOT_STATUS_CONFLICT || status == GOT_STATUS_MERGE)
4217 818c7501 2019-07-11 stsp *rebase_status = status;
4218 1ee397ad 2019-07-12 stsp return NULL;
4221 818c7501 2019-07-11 stsp static const struct got_error *
4222 3e3a69f1 2019-07-25 stsp rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
4223 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_reference *new_base_branch,
4224 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_repository *repo)
4226 818c7501 2019-07-11 stsp printf("Switching work tree to %s\n", got_ref_get_name(branch));
4227 3e3a69f1 2019-07-25 stsp return got_worktree_rebase_complete(worktree, fileindex,
4228 818c7501 2019-07-11 stsp new_base_branch, tmp_branch, branch, repo);
4231 818c7501 2019-07-11 stsp static const struct got_error *
4232 01757395 2019-07-12 stsp rebase_commit(struct got_pathlist_head *merged_paths,
4233 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
4234 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch,
4235 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
4237 ff0d2220 2019-07-11 stsp const struct got_error *error;
4238 ff0d2220 2019-07-11 stsp struct got_commit_object *commit;
4239 ff0d2220 2019-07-11 stsp struct got_object_id *new_commit_id;
4241 ff0d2220 2019-07-11 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
4242 ff0d2220 2019-07-11 stsp if (error)
4243 ff0d2220 2019-07-11 stsp return error;
4245 01757395 2019-07-12 stsp error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
4246 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, commit, commit_id, repo);
4247 ff0d2220 2019-07-11 stsp if (error) {
4248 ff0d2220 2019-07-11 stsp if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
4249 ff0d2220 2019-07-11 stsp goto done;
4250 ff0d2220 2019-07-11 stsp error = show_rebase_progress(commit, commit_id, NULL);
4252 ff0d2220 2019-07-11 stsp error = show_rebase_progress(commit, commit_id, new_commit_id);
4253 ff0d2220 2019-07-11 stsp free(new_commit_id);
4256 ff0d2220 2019-07-11 stsp got_object_commit_close(commit);
4257 ff0d2220 2019-07-11 stsp return error;
4260 64c6d990 2019-07-11 stsp struct check_path_prefix_arg {
4261 64c6d990 2019-07-11 stsp const char *path_prefix;
4262 64c6d990 2019-07-11 stsp size_t len;
4263 8ca9bd68 2019-07-25 stsp int errcode;
4266 64c6d990 2019-07-11 stsp static const struct got_error *
4267 8ca9bd68 2019-07-25 stsp check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
4268 64c6d990 2019-07-11 stsp struct got_blob_object *blob2, struct got_object_id *id1,
4269 64c6d990 2019-07-11 stsp struct got_object_id *id2, const char *path1, const char *path2,
4270 64c6d990 2019-07-11 stsp struct got_repository *repo)
4272 64c6d990 2019-07-11 stsp struct check_path_prefix_arg *a = arg;
4274 64c6d990 2019-07-11 stsp if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
4275 64c6d990 2019-07-11 stsp (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
4276 8ca9bd68 2019-07-25 stsp return got_error(a->errcode);
4278 64c6d990 2019-07-11 stsp return NULL;
4281 64c6d990 2019-07-11 stsp static const struct got_error *
4282 8ca9bd68 2019-07-25 stsp check_path_prefix(struct got_object_id *parent_id,
4283 64c6d990 2019-07-11 stsp struct got_object_id *commit_id, const char *path_prefix,
4284 8ca9bd68 2019-07-25 stsp int errcode, struct got_repository *repo)
4286 64c6d990 2019-07-11 stsp const struct got_error *err;
4287 64c6d990 2019-07-11 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
4288 64c6d990 2019-07-11 stsp struct got_commit_object *commit = NULL, *parent_commit = NULL;
4289 64c6d990 2019-07-11 stsp struct check_path_prefix_arg cpp_arg;
4291 64c6d990 2019-07-11 stsp if (got_path_is_root_dir(path_prefix))
4292 64c6d990 2019-07-11 stsp return NULL;
4294 64c6d990 2019-07-11 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
4296 64c6d990 2019-07-11 stsp goto done;
4298 64c6d990 2019-07-11 stsp err = got_object_open_as_commit(&parent_commit, repo, parent_id);
4300 64c6d990 2019-07-11 stsp goto done;
4302 64c6d990 2019-07-11 stsp err = got_object_open_as_tree(&tree1, repo,
4303 64c6d990 2019-07-11 stsp got_object_commit_get_tree_id(parent_commit));
4305 64c6d990 2019-07-11 stsp goto done;
4307 64c6d990 2019-07-11 stsp err = got_object_open_as_tree(&tree2, repo,
4308 64c6d990 2019-07-11 stsp got_object_commit_get_tree_id(commit));
4310 64c6d990 2019-07-11 stsp goto done;
4312 64c6d990 2019-07-11 stsp cpp_arg.path_prefix = path_prefix;
4313 d23ace97 2019-07-25 stsp while (cpp_arg.path_prefix[0] == '/')
4314 d23ace97 2019-07-25 stsp cpp_arg.path_prefix++;
4315 d23ace97 2019-07-25 stsp cpp_arg.len = strlen(cpp_arg.path_prefix);
4316 8ca9bd68 2019-07-25 stsp cpp_arg.errcode = errcode;
4317 8ca9bd68 2019-07-25 stsp err = got_diff_tree(tree1, tree2, "", "", repo,
4318 31b4484f 2019-07-27 stsp check_path_prefix_in_diff, &cpp_arg, 0);
4320 64c6d990 2019-07-11 stsp if (tree1)
4321 64c6d990 2019-07-11 stsp got_object_tree_close(tree1);
4322 64c6d990 2019-07-11 stsp if (tree2)
4323 64c6d990 2019-07-11 stsp got_object_tree_close(tree2);
4324 64c6d990 2019-07-11 stsp if (commit)
4325 64c6d990 2019-07-11 stsp got_object_commit_close(commit);
4326 64c6d990 2019-07-11 stsp if (parent_commit)
4327 64c6d990 2019-07-11 stsp got_object_commit_close(parent_commit);
4328 64c6d990 2019-07-11 stsp return err;
4331 ff0d2220 2019-07-11 stsp static const struct got_error *
4332 8ca9bd68 2019-07-25 stsp collect_commits(struct got_object_id_queue *commits,
4333 af61c510 2019-07-19 stsp struct got_object_id *initial_commit_id,
4334 af61c510 2019-07-19 stsp struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
4335 8ca9bd68 2019-07-25 stsp const char *path_prefix, int path_prefix_errcode,
4336 8ca9bd68 2019-07-25 stsp struct got_repository *repo)
4338 af61c510 2019-07-19 stsp const struct got_error *err = NULL;
4339 af61c510 2019-07-19 stsp struct got_commit_graph *graph = NULL;
4340 af61c510 2019-07-19 stsp struct got_object_id *parent_id = NULL;
4341 af61c510 2019-07-19 stsp struct got_object_qid *qid;
4342 af61c510 2019-07-19 stsp struct got_object_id *commit_id = initial_commit_id;
4344 af61c510 2019-07-19 stsp err = got_commit_graph_open(&graph, initial_commit_id, "/", 1, repo);
4346 af61c510 2019-07-19 stsp return err;
4348 af61c510 2019-07-19 stsp err = got_commit_graph_iter_start(graph, iter_start_id, repo);
4350 af61c510 2019-07-19 stsp goto done;
4351 af61c510 2019-07-19 stsp while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
4352 af61c510 2019-07-19 stsp err = got_commit_graph_iter_next(&parent_id, graph);
4353 af61c510 2019-07-19 stsp if (err) {
4354 af61c510 2019-07-19 stsp if (err->code == GOT_ERR_ITER_COMPLETED) {
4355 af61c510 2019-07-19 stsp err = got_error_msg(GOT_ERR_ANCESTRY,
4356 af61c510 2019-07-19 stsp "ran out of commits to rebase before "
4357 af61c510 2019-07-19 stsp "youngest common ancestor commit has "
4358 af61c510 2019-07-19 stsp "been reached?!?");
4359 af61c510 2019-07-19 stsp goto done;
4360 af61c510 2019-07-19 stsp } else if (err->code != GOT_ERR_ITER_NEED_MORE)
4361 af61c510 2019-07-19 stsp goto done;
4362 af61c510 2019-07-19 stsp err = got_commit_graph_fetch_commits(graph, 1, repo);
4364 af61c510 2019-07-19 stsp goto done;
4366 8ca9bd68 2019-07-25 stsp err = check_path_prefix(parent_id, commit_id,
4367 8ca9bd68 2019-07-25 stsp path_prefix, path_prefix_errcode, repo);
4369 af61c510 2019-07-19 stsp goto done;
4371 af61c510 2019-07-19 stsp err = got_object_qid_alloc(&qid, commit_id);
4373 af61c510 2019-07-19 stsp goto done;
4374 af61c510 2019-07-19 stsp SIMPLEQ_INSERT_HEAD(commits, qid, entry);
4375 af61c510 2019-07-19 stsp commit_id = parent_id;
4379 af61c510 2019-07-19 stsp got_commit_graph_close(graph);
4380 af61c510 2019-07-19 stsp return err;
4383 af61c510 2019-07-19 stsp static const struct got_error *
4384 818c7501 2019-07-11 stsp cmd_rebase(int argc, char *argv[])
4386 818c7501 2019-07-11 stsp const struct got_error *error = NULL;
4387 818c7501 2019-07-11 stsp struct got_worktree *worktree = NULL;
4388 818c7501 2019-07-11 stsp struct got_repository *repo = NULL;
4389 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex = NULL;
4390 818c7501 2019-07-11 stsp char *cwd = NULL;
4391 818c7501 2019-07-11 stsp struct got_reference *branch = NULL;
4392 818c7501 2019-07-11 stsp struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
4393 818c7501 2019-07-11 stsp struct got_object_id *commit_id = NULL, *parent_id = NULL;
4394 818c7501 2019-07-11 stsp struct got_object_id *resume_commit_id = NULL;
4395 818c7501 2019-07-11 stsp struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
4396 818c7501 2019-07-11 stsp struct got_commit_object *commit = NULL;
4397 818c7501 2019-07-11 stsp int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
4398 818c7501 2019-07-11 stsp unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
4399 818c7501 2019-07-11 stsp struct got_object_id_queue commits;
4400 01757395 2019-07-12 stsp struct got_pathlist_head merged_paths;
4401 818c7501 2019-07-11 stsp const struct got_object_id_queue *parent_ids;
4402 818c7501 2019-07-11 stsp struct got_object_qid *qid, *pid;
4404 818c7501 2019-07-11 stsp SIMPLEQ_INIT(&commits);
4405 01757395 2019-07-12 stsp TAILQ_INIT(&merged_paths);
4407 818c7501 2019-07-11 stsp while ((ch = getopt(argc, argv, "ac")) != -1) {
4408 818c7501 2019-07-11 stsp switch (ch) {
4410 818c7501 2019-07-11 stsp abort_rebase = 1;
4413 818c7501 2019-07-11 stsp continue_rebase = 1;
4416 818c7501 2019-07-11 stsp usage_rebase();
4417 818c7501 2019-07-11 stsp /* NOTREACHED */
4421 818c7501 2019-07-11 stsp argc -= optind;
4422 818c7501 2019-07-11 stsp argv += optind;
4424 43012d58 2019-07-14 stsp #ifndef PROFILE
4425 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4426 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
4427 43012d58 2019-07-14 stsp err(1, "pledge");
4429 818c7501 2019-07-11 stsp if (abort_rebase && continue_rebase)
4430 818c7501 2019-07-11 stsp usage_rebase();
4431 818c7501 2019-07-11 stsp else if (abort_rebase || continue_rebase) {
4432 818c7501 2019-07-11 stsp if (argc != 0)
4433 818c7501 2019-07-11 stsp usage_rebase();
4434 818c7501 2019-07-11 stsp } else if (argc != 1)
4435 818c7501 2019-07-11 stsp usage_rebase();
4437 818c7501 2019-07-11 stsp cwd = getcwd(NULL, 0);
4438 818c7501 2019-07-11 stsp if (cwd == NULL) {
4439 818c7501 2019-07-11 stsp error = got_error_from_errno("getcwd");
4440 818c7501 2019-07-11 stsp goto done;
4442 818c7501 2019-07-11 stsp error = got_worktree_open(&worktree, cwd);
4443 818c7501 2019-07-11 stsp if (error)
4444 818c7501 2019-07-11 stsp goto done;
4446 818c7501 2019-07-11 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
4447 818c7501 2019-07-11 stsp if (error != NULL)
4448 818c7501 2019-07-11 stsp goto done;
4450 818c7501 2019-07-11 stsp error = apply_unveil(got_repo_get_path(repo), 0,
4451 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
4452 818c7501 2019-07-11 stsp if (error)
4453 818c7501 2019-07-11 stsp goto done;
4455 818c7501 2019-07-11 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
4456 818c7501 2019-07-11 stsp if (error)
4457 818c7501 2019-07-11 stsp goto done;
4459 f6794adc 2019-07-23 stsp if (abort_rebase) {
4460 818c7501 2019-07-11 stsp int did_something;
4461 f6794adc 2019-07-23 stsp if (!rebase_in_progress) {
4462 f6794adc 2019-07-23 stsp error = got_error(GOT_ERR_NOT_REBASING);
4463 f6794adc 2019-07-23 stsp goto done;
4465 818c7501 2019-07-11 stsp error = got_worktree_rebase_continue(&resume_commit_id,
4466 3e3a69f1 2019-07-25 stsp &new_base_branch, &tmp_branch, &branch, &fileindex,
4467 3e3a69f1 2019-07-25 stsp worktree, repo);
4468 818c7501 2019-07-11 stsp if (error)
4469 818c7501 2019-07-11 stsp goto done;
4470 818c7501 2019-07-11 stsp printf("Switching work tree to %s\n",
4471 818c7501 2019-07-11 stsp got_ref_get_symref_target(new_base_branch));
4472 3e3a69f1 2019-07-25 stsp error = got_worktree_rebase_abort(worktree, fileindex, repo,
4473 818c7501 2019-07-11 stsp new_base_branch, update_progress, &did_something);
4474 818c7501 2019-07-11 stsp if (error)
4475 818c7501 2019-07-11 stsp goto done;
4476 818c7501 2019-07-11 stsp printf("Rebase of %s aborted\n", got_ref_get_name(branch));
4477 818c7501 2019-07-11 stsp goto done; /* nothing else to do */
4480 818c7501 2019-07-11 stsp if (continue_rebase) {
4481 f6794adc 2019-07-23 stsp if (!rebase_in_progress) {
4482 f6794adc 2019-07-23 stsp error = got_error(GOT_ERR_NOT_REBASING);
4483 f6794adc 2019-07-23 stsp goto done;
4485 818c7501 2019-07-11 stsp error = got_worktree_rebase_continue(&resume_commit_id,
4486 3e3a69f1 2019-07-25 stsp &new_base_branch, &tmp_branch, &branch, &fileindex,
4487 3e3a69f1 2019-07-25 stsp worktree, repo);
4488 818c7501 2019-07-11 stsp if (error)
4489 818c7501 2019-07-11 stsp goto done;
4491 3e3a69f1 2019-07-25 stsp error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
4492 01757395 2019-07-12 stsp resume_commit_id, repo);
4493 818c7501 2019-07-11 stsp if (error)
4494 818c7501 2019-07-11 stsp goto done;
4496 ff0d2220 2019-07-11 stsp yca_id = got_object_id_dup(resume_commit_id);
4497 ff0d2220 2019-07-11 stsp if (yca_id == NULL) {
4498 818c7501 2019-07-11 stsp error = got_error_from_errno("got_object_id_dup");
4499 818c7501 2019-07-11 stsp goto done;
4502 818c7501 2019-07-11 stsp error = got_ref_open(&branch, repo, argv[0], 0);
4503 818c7501 2019-07-11 stsp if (error != NULL)
4504 818c7501 2019-07-11 stsp goto done;
4507 ff0d2220 2019-07-11 stsp error = got_ref_resolve(&branch_head_commit_id, repo, branch);
4508 ff0d2220 2019-07-11 stsp if (error)
4509 ff0d2220 2019-07-11 stsp goto done;
4511 ff0d2220 2019-07-11 stsp if (!continue_rebase) {
4512 a51a74b3 2019-07-27 stsp struct got_object_id *base_commit_id;
4514 a51a74b3 2019-07-27 stsp base_commit_id = got_worktree_get_base_commit_id(worktree);
4515 818c7501 2019-07-11 stsp error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
4516 a51a74b3 2019-07-27 stsp base_commit_id, branch_head_commit_id, repo);
4517 818c7501 2019-07-11 stsp if (error)
4518 818c7501 2019-07-11 stsp goto done;
4519 818c7501 2019-07-11 stsp if (yca_id == NULL) {
4520 818c7501 2019-07-11 stsp error = got_error_msg(GOT_ERR_ANCESTRY,
4521 818c7501 2019-07-11 stsp "specified branch shares no common ancestry "
4522 818c7501 2019-07-11 stsp "with work tree's branch");
4523 818c7501 2019-07-11 stsp goto done;
4526 a51a74b3 2019-07-27 stsp error = check_same_branch(base_commit_id, branch, yca_id, repo);
4527 a51a74b3 2019-07-27 stsp if (error) {
4528 a51a74b3 2019-07-27 stsp if (error->code != GOT_ERR_ANCESTRY)
4529 a51a74b3 2019-07-27 stsp goto done;
4530 a51a74b3 2019-07-27 stsp error = NULL;
4532 a51a74b3 2019-07-27 stsp error = got_error_msg(GOT_ERR_SAME_BRANCH,
4533 a51a74b3 2019-07-27 stsp "specified branch resolves to a commit which "
4534 a51a74b3 2019-07-27 stsp "is already contained in work tree's branch");
4535 a51a74b3 2019-07-27 stsp goto done;
4537 818c7501 2019-07-11 stsp error = got_worktree_rebase_prepare(&new_base_branch,
4538 3e3a69f1 2019-07-25 stsp &tmp_branch, &fileindex, worktree, branch, repo);
4539 818c7501 2019-07-11 stsp if (error)
4540 818c7501 2019-07-11 stsp goto done;
4543 ff0d2220 2019-07-11 stsp commit_id = branch_head_commit_id;
4544 818c7501 2019-07-11 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
4545 818c7501 2019-07-11 stsp if (error)
4546 818c7501 2019-07-11 stsp goto done;
4548 818c7501 2019-07-11 stsp parent_ids = got_object_commit_get_parent_ids(commit);
4549 818c7501 2019-07-11 stsp pid = SIMPLEQ_FIRST(parent_ids);
4550 fc66b545 2019-08-12 stsp if (pid == NULL) {
4551 fc66b545 2019-08-12 stsp if (!continue_rebase) {
4552 fc66b545 2019-08-12 stsp int did_something;
4553 fc66b545 2019-08-12 stsp error = got_worktree_rebase_abort(worktree, fileindex,
4554 fc66b545 2019-08-12 stsp repo, new_base_branch, update_progress,
4555 fc66b545 2019-08-12 stsp &did_something);
4556 fc66b545 2019-08-12 stsp if (error)
4557 fc66b545 2019-08-12 stsp goto done;
4558 fc66b545 2019-08-12 stsp printf("Rebase of %s aborted\n",
4559 fc66b545 2019-08-12 stsp got_ref_get_name(branch));
4561 fc66b545 2019-08-12 stsp error = got_error(GOT_ERR_EMPTY_REBASE);
4562 fc66b545 2019-08-12 stsp goto done;
4564 8ca9bd68 2019-07-25 stsp error = collect_commits(&commits, commit_id, pid->id,
4565 8ca9bd68 2019-07-25 stsp yca_id, got_worktree_get_path_prefix(worktree),
4566 8ca9bd68 2019-07-25 stsp GOT_ERR_REBASE_PATH, repo);
4567 818c7501 2019-07-11 stsp got_object_commit_close(commit);
4568 818c7501 2019-07-11 stsp commit = NULL;
4569 818c7501 2019-07-11 stsp if (error)
4570 818c7501 2019-07-11 stsp goto done;
4572 818c7501 2019-07-11 stsp if (SIMPLEQ_EMPTY(&commits)) {
4573 ff0d2220 2019-07-11 stsp if (continue_rebase)
4574 3e3a69f1 2019-07-25 stsp error = rebase_complete(worktree, fileindex,
4575 3e3a69f1 2019-07-25 stsp branch, new_base_branch, tmp_branch, repo);
4577 ff0d2220 2019-07-11 stsp error = got_error(GOT_ERR_EMPTY_REBASE);
4578 818c7501 2019-07-11 stsp goto done;
4581 818c7501 2019-07-11 stsp pid = NULL;
4582 818c7501 2019-07-11 stsp SIMPLEQ_FOREACH(qid, &commits, entry) {
4583 818c7501 2019-07-11 stsp commit_id = qid->id;
4584 818c7501 2019-07-11 stsp parent_id = pid ? pid->id : yca_id;
4585 818c7501 2019-07-11 stsp pid = qid;
4587 01757395 2019-07-12 stsp error = got_worktree_rebase_merge_files(&merged_paths,
4588 3e3a69f1 2019-07-25 stsp worktree, fileindex, parent_id, commit_id, repo,
4589 3e3a69f1 2019-07-25 stsp rebase_progress, &rebase_status, check_cancelled, NULL);
4590 818c7501 2019-07-11 stsp if (error)
4591 818c7501 2019-07-11 stsp goto done;
4593 01757395 2019-07-12 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
4594 01757395 2019-07-12 stsp got_worktree_rebase_pathlist_free(&merged_paths);
4598 3e3a69f1 2019-07-25 stsp error = rebase_commit(&merged_paths, worktree, fileindex,
4599 3e3a69f1 2019-07-25 stsp tmp_branch, commit_id, repo);
4600 01757395 2019-07-12 stsp got_worktree_rebase_pathlist_free(&merged_paths);
4601 818c7501 2019-07-11 stsp if (error)
4602 818c7501 2019-07-11 stsp goto done;
4605 818c7501 2019-07-11 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
4606 3e3a69f1 2019-07-25 stsp error = got_worktree_rebase_postpone(worktree, fileindex);
4607 818c7501 2019-07-11 stsp if (error)
4608 818c7501 2019-07-11 stsp goto done;
4609 818c7501 2019-07-11 stsp error = got_error_msg(GOT_ERR_CONFLICTS,
4610 11495e04 2019-07-12 stsp "conflicts must be resolved before rebasing can continue");
4612 3e3a69f1 2019-07-25 stsp error = rebase_complete(worktree, fileindex, branch,
4613 3e3a69f1 2019-07-25 stsp new_base_branch, tmp_branch, repo);
4615 818c7501 2019-07-11 stsp got_object_id_queue_free(&commits);
4616 818c7501 2019-07-11 stsp free(branch_head_commit_id);
4617 818c7501 2019-07-11 stsp free(resume_commit_id);
4618 818c7501 2019-07-11 stsp free(yca_id);
4619 818c7501 2019-07-11 stsp if (commit)
4620 818c7501 2019-07-11 stsp got_object_commit_close(commit);
4621 818c7501 2019-07-11 stsp if (branch)
4622 818c7501 2019-07-11 stsp got_ref_close(branch);
4623 818c7501 2019-07-11 stsp if (new_base_branch)
4624 818c7501 2019-07-11 stsp got_ref_close(new_base_branch);
4625 818c7501 2019-07-11 stsp if (tmp_branch)
4626 818c7501 2019-07-11 stsp got_ref_close(tmp_branch);
4627 5ef14e63 2019-06-02 stsp if (worktree)
4628 5ef14e63 2019-06-02 stsp got_worktree_close(worktree);
4630 5ef14e63 2019-06-02 stsp got_repo_close(repo);
4631 5ef14e63 2019-06-02 stsp return error;
4634 0ebf8283 2019-07-24 stsp __dead static void
4635 0ebf8283 2019-07-24 stsp usage_histedit(void)
4637 f3055044 2019-08-07 stsp fprintf(stderr, "usage: %s histedit [-a] [-c] [-F histedit-script]\n",
4638 0ebf8283 2019-07-24 stsp getprogname());
4642 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_PICK 'p'
4643 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_EDIT 'e'
4644 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_FOLD 'f'
4645 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_DROP 'd'
4646 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_MESG 'm'
4648 0ebf8283 2019-07-24 stsp static struct got_histedit_cmd {
4649 0ebf8283 2019-07-24 stsp unsigned char code;
4650 0ebf8283 2019-07-24 stsp const char *name;
4651 0ebf8283 2019-07-24 stsp const char *desc;
4652 0ebf8283 2019-07-24 stsp } got_histedit_cmds[] = {
4653 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_PICK, "pick", "use commit" },
4654 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
4655 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_FOLD, "fold", "combine with commit below" },
4656 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
4657 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_MESG, "mesg",
4658 0ebf8283 2019-07-24 stsp "single-line log message for commit above (open editor if empty)" },
4661 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry {
4662 0ebf8283 2019-07-24 stsp TAILQ_ENTRY(got_histedit_list_entry) entry;
4663 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id;
4664 0ebf8283 2019-07-24 stsp const struct got_histedit_cmd *cmd;
4665 0ebf8283 2019-07-24 stsp char *logmsg;
4667 0ebf8283 2019-07-24 stsp TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
4669 0ebf8283 2019-07-24 stsp static const struct got_error *
4670 0ebf8283 2019-07-24 stsp histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
4671 0ebf8283 2019-07-24 stsp FILE *f, struct got_repository *repo)
4673 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
4674 0ebf8283 2019-07-24 stsp char *logmsg = NULL, *id_str = NULL;
4675 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
4678 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
4680 0ebf8283 2019-07-24 stsp goto done;
4682 0ebf8283 2019-07-24 stsp err = get_short_logmsg(&logmsg, 34, commit);
4684 0ebf8283 2019-07-24 stsp goto done;
4686 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, commit_id);
4688 0ebf8283 2019-07-24 stsp goto done;
4690 0ebf8283 2019-07-24 stsp n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
4691 0ebf8283 2019-07-24 stsp if (n < 0)
4692 0ebf8283 2019-07-24 stsp err = got_ferror(f, GOT_ERR_IO);
4694 0ebf8283 2019-07-24 stsp if (commit)
4695 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
4696 0ebf8283 2019-07-24 stsp free(id_str);
4697 0ebf8283 2019-07-24 stsp free(logmsg);
4698 0ebf8283 2019-07-24 stsp return err;
4701 0ebf8283 2019-07-24 stsp static const struct got_error *
4702 0ebf8283 2019-07-24 stsp histedit_write_commit_list(struct got_object_id_queue *commits, FILE *f,
4703 0ebf8283 2019-07-24 stsp struct got_repository *repo)
4705 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
4706 0ebf8283 2019-07-24 stsp struct got_object_qid *qid;
4708 0ebf8283 2019-07-24 stsp if (SIMPLEQ_EMPTY(commits))
4709 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_EMPTY_HISTEDIT);
4711 0ebf8283 2019-07-24 stsp SIMPLEQ_FOREACH(qid, commits, entry) {
4712 0ebf8283 2019-07-24 stsp err = histedit_write_commit(qid->id, got_histedit_cmds[0].name,
4718 0ebf8283 2019-07-24 stsp return err;
4721 0ebf8283 2019-07-24 stsp static const struct got_error *
4722 0ebf8283 2019-07-24 stsp write_cmd_list(FILE *f)
4724 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
4727 0ebf8283 2019-07-24 stsp n = fprintf(f, "# Available histedit commands:\n");
4728 0ebf8283 2019-07-24 stsp if (n < 0)
4729 0ebf8283 2019-07-24 stsp return got_ferror(f, GOT_ERR_IO);
4731 0ebf8283 2019-07-24 stsp for (i = 0; i < nitems(got_histedit_cmds); i++) {
4732 0ebf8283 2019-07-24 stsp struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
4733 0ebf8283 2019-07-24 stsp n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
4734 0ebf8283 2019-07-24 stsp cmd->desc);
4735 0ebf8283 2019-07-24 stsp if (n < 0) {
4736 0ebf8283 2019-07-24 stsp err = got_ferror(f, GOT_ERR_IO);
4740 0ebf8283 2019-07-24 stsp n = fprintf(f, "# Commits will be processed in order from top to "
4741 0ebf8283 2019-07-24 stsp "bottom of this file.\n");
4742 0ebf8283 2019-07-24 stsp if (n < 0)
4743 0ebf8283 2019-07-24 stsp return got_ferror(f, GOT_ERR_IO);
4744 0ebf8283 2019-07-24 stsp return err;
4747 0ebf8283 2019-07-24 stsp static const struct got_error *
4748 0ebf8283 2019-07-24 stsp histedit_syntax_error(int lineno)
4750 0ebf8283 2019-07-24 stsp static char msg[42];
4753 0ebf8283 2019-07-24 stsp ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
4755 0ebf8283 2019-07-24 stsp if (ret == -1 || ret >= sizeof(msg))
4756 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_HISTEDIT_SYNTAX);
4758 0ebf8283 2019-07-24 stsp return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
4761 0ebf8283 2019-07-24 stsp static const struct got_error *
4762 0ebf8283 2019-07-24 stsp append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
4763 0ebf8283 2019-07-24 stsp char *logmsg, struct got_repository *repo)
4765 0ebf8283 2019-07-24 stsp const struct got_error *err;
4766 0ebf8283 2019-07-24 stsp struct got_commit_object *folded_commit = NULL;
4767 5943eee2 2019-08-13 stsp char *id_str, *folded_logmsg = NULL;
4769 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, hle->commit_id);
4771 0ebf8283 2019-07-24 stsp return err;
4773 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
4775 0ebf8283 2019-07-24 stsp goto done;
4777 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
4779 5943eee2 2019-08-13 stsp goto done;
4780 0ebf8283 2019-07-24 stsp if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
4781 0ebf8283 2019-07-24 stsp logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
4782 5943eee2 2019-08-13 stsp folded_logmsg) == -1) {
4783 0ebf8283 2019-07-24 stsp err = got_error_from_errno("asprintf");
4784 0ebf8283 2019-07-24 stsp goto done;
4787 0ebf8283 2019-07-24 stsp if (folded_commit)
4788 0ebf8283 2019-07-24 stsp got_object_commit_close(folded_commit);
4789 0ebf8283 2019-07-24 stsp free(id_str);
4790 5943eee2 2019-08-13 stsp free(folded_logmsg);
4791 0ebf8283 2019-07-24 stsp return err;
4794 0ebf8283 2019-07-24 stsp static struct got_histedit_list_entry *
4795 0ebf8283 2019-07-24 stsp get_folded_commits(struct got_histedit_list_entry *hle)
4797 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *prev, *folded = NULL;
4799 0ebf8283 2019-07-24 stsp prev = TAILQ_PREV(hle, got_histedit_list, entry);
4800 3f9de99f 2019-07-24 stsp while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
4801 3f9de99f 2019-07-24 stsp prev->cmd->code == GOT_HISTEDIT_DROP)) {
4802 3f9de99f 2019-07-24 stsp if (prev->cmd->code == GOT_HISTEDIT_FOLD)
4803 3f9de99f 2019-07-24 stsp folded = prev;
4804 0ebf8283 2019-07-24 stsp prev = TAILQ_PREV(prev, got_histedit_list, entry);
4807 0ebf8283 2019-07-24 stsp return folded;
4810 0ebf8283 2019-07-24 stsp static const struct got_error *
4811 0ebf8283 2019-07-24 stsp histedit_edit_logmsg(struct got_histedit_list_entry *hle,
4812 0ebf8283 2019-07-24 stsp struct got_repository *repo)
4814 5943eee2 2019-08-13 stsp char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
4815 0ebf8283 2019-07-24 stsp char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
4816 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
4817 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
4819 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *folded = NULL;
4821 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&commit, repo, hle->commit_id);
4823 0ebf8283 2019-07-24 stsp return err;
4825 0ebf8283 2019-07-24 stsp folded = get_folded_commits(hle);
4826 0ebf8283 2019-07-24 stsp if (folded) {
4827 0ebf8283 2019-07-24 stsp while (folded != hle) {
4828 3f9de99f 2019-07-24 stsp if (folded->cmd->code == GOT_HISTEDIT_DROP) {
4829 3f9de99f 2019-07-24 stsp folded = TAILQ_NEXT(folded, entry);
4832 0ebf8283 2019-07-24 stsp err = append_folded_commit_msg(&new_msg, folded,
4833 0ebf8283 2019-07-24 stsp logmsg, repo);
4835 0ebf8283 2019-07-24 stsp goto done;
4836 0ebf8283 2019-07-24 stsp free(logmsg);
4837 0ebf8283 2019-07-24 stsp logmsg = new_msg;
4838 0ebf8283 2019-07-24 stsp folded = TAILQ_NEXT(folded, entry);
4842 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, hle->commit_id);
4844 0ebf8283 2019-07-24 stsp goto done;
4845 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&orig_logmsg, commit);
4847 5943eee2 2019-08-13 stsp goto done;
4848 0ebf8283 2019-07-24 stsp if (asprintf(&new_msg,
4849 0ebf8283 2019-07-24 stsp "%s\n# original log message of commit %s: %s",
4850 5943eee2 2019-08-13 stsp logmsg ? logmsg : "", id_str, orig_logmsg) == -1) {
4851 0ebf8283 2019-07-24 stsp err = got_error_from_errno("asprintf");
4852 0ebf8283 2019-07-24 stsp goto done;
4854 0ebf8283 2019-07-24 stsp free(logmsg);
4855 0ebf8283 2019-07-24 stsp logmsg = new_msg;
4857 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, hle->commit_id);
4859 0ebf8283 2019-07-24 stsp goto done;
4861 0ebf8283 2019-07-24 stsp err = got_opentemp_named_fd(&logmsg_path, &fd, "/tmp/got-logmsg");
4863 0ebf8283 2019-07-24 stsp goto done;
4865 0ebf8283 2019-07-24 stsp dprintf(fd, logmsg);
4866 0ebf8283 2019-07-24 stsp close(fd);
4868 0ebf8283 2019-07-24 stsp err = get_editor(&editor);
4870 0ebf8283 2019-07-24 stsp goto done;
4872 0ebf8283 2019-07-24 stsp err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg);
4873 0ebf8283 2019-07-24 stsp if (err) {
4874 0ebf8283 2019-07-24 stsp if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
4875 0ebf8283 2019-07-24 stsp goto done;
4876 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&hle->logmsg, commit);
4879 0ebf8283 2019-07-24 stsp if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
4880 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("unlink", logmsg_path);
4881 0ebf8283 2019-07-24 stsp free(logmsg_path);
4882 0ebf8283 2019-07-24 stsp free(logmsg);
4883 5943eee2 2019-08-13 stsp free(orig_logmsg);
4884 0ebf8283 2019-07-24 stsp free(editor);
4885 0ebf8283 2019-07-24 stsp if (commit)
4886 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
4887 0ebf8283 2019-07-24 stsp return err;
4890 0ebf8283 2019-07-24 stsp static const struct got_error *
4891 0ebf8283 2019-07-24 stsp histedit_parse_list(struct got_histedit_list *histedit_cmds,
4892 0ebf8283 2019-07-24 stsp FILE *f, struct got_repository *repo)
4894 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
4895 0ebf8283 2019-07-24 stsp char *line = NULL, *p, *end;
4896 0ebf8283 2019-07-24 stsp size_t size;
4897 0ebf8283 2019-07-24 stsp ssize_t len;
4898 0ebf8283 2019-07-24 stsp int lineno = 0, i;
4899 0ebf8283 2019-07-24 stsp const struct got_histedit_cmd *cmd;
4900 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id = NULL;
4901 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle = NULL;
4903 0ebf8283 2019-07-24 stsp for (;;) {
4904 0ebf8283 2019-07-24 stsp len = getline(&line, &size, f);
4905 0ebf8283 2019-07-24 stsp if (len == -1) {
4906 0ebf8283 2019-07-24 stsp const struct got_error *getline_err;
4907 0ebf8283 2019-07-24 stsp if (feof(f))
4909 0ebf8283 2019-07-24 stsp getline_err = got_error_from_errno("getline");
4910 0ebf8283 2019-07-24 stsp err = got_ferror(f, getline_err->code);
4915 0ebf8283 2019-07-24 stsp while (isspace((unsigned char)p[0]))
4917 0ebf8283 2019-07-24 stsp if (p[0] == '#' || p[0] == '\0') {
4918 0ebf8283 2019-07-24 stsp free(line);
4919 0ebf8283 2019-07-24 stsp line = NULL;
4922 0ebf8283 2019-07-24 stsp cmd = NULL;
4923 0ebf8283 2019-07-24 stsp for (i = 0; i < nitems(got_histedit_cmds); i++) {
4924 0ebf8283 2019-07-24 stsp cmd = &got_histedit_cmds[i];
4925 0ebf8283 2019-07-24 stsp if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
4926 0ebf8283 2019-07-24 stsp isspace((unsigned char)p[strlen(cmd->name)])) {
4927 0ebf8283 2019-07-24 stsp p += strlen(cmd->name);
4930 0ebf8283 2019-07-24 stsp if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
4935 6c1844f6 2019-07-25 stsp if (i == nitems(got_histedit_cmds)) {
4936 0ebf8283 2019-07-24 stsp err = histedit_syntax_error(lineno);
4939 0ebf8283 2019-07-24 stsp while (isspace((unsigned char)p[0]))
4941 0ebf8283 2019-07-24 stsp if (cmd->code == GOT_HISTEDIT_MESG) {
4942 0ebf8283 2019-07-24 stsp if (hle == NULL || hle->logmsg != NULL) {
4943 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_HISTEDIT_CMD);
4946 0ebf8283 2019-07-24 stsp if (p[0] == '\0') {
4947 0ebf8283 2019-07-24 stsp err = histedit_edit_logmsg(hle, repo);
4951 0ebf8283 2019-07-24 stsp hle->logmsg = strdup(p);
4952 0ebf8283 2019-07-24 stsp if (hle->logmsg == NULL) {
4953 0ebf8283 2019-07-24 stsp err = got_error_from_errno("strdup");
4957 0ebf8283 2019-07-24 stsp free(line);
4958 0ebf8283 2019-07-24 stsp line = NULL;
4962 0ebf8283 2019-07-24 stsp while (end[0] && !isspace((unsigned char)end[0]))
4964 0ebf8283 2019-07-24 stsp *end = '\0';
4966 0ebf8283 2019-07-24 stsp err = got_object_resolve_id_str(&commit_id, repo, p);
4967 0ebf8283 2019-07-24 stsp if (err) {
4968 0ebf8283 2019-07-24 stsp /* override error code */
4969 0ebf8283 2019-07-24 stsp err = histedit_syntax_error(lineno);
4973 0ebf8283 2019-07-24 stsp hle = malloc(sizeof(*hle));
4974 0ebf8283 2019-07-24 stsp if (hle == NULL) {
4975 0ebf8283 2019-07-24 stsp err = got_error_from_errno("malloc");
4978 0ebf8283 2019-07-24 stsp hle->cmd = cmd;
4979 0ebf8283 2019-07-24 stsp hle->commit_id = commit_id;
4980 0ebf8283 2019-07-24 stsp hle->logmsg = NULL;
4981 0ebf8283 2019-07-24 stsp commit_id = NULL;
4982 0ebf8283 2019-07-24 stsp free(line);
4983 0ebf8283 2019-07-24 stsp line = NULL;
4984 0ebf8283 2019-07-24 stsp TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
4987 0ebf8283 2019-07-24 stsp free(line);
4988 0ebf8283 2019-07-24 stsp free(commit_id);
4989 0ebf8283 2019-07-24 stsp return err;
4992 0ebf8283 2019-07-24 stsp static const struct got_error *
4993 bfce7f83 2019-07-27 stsp histedit_check_script(struct got_histedit_list *histedit_cmds,
4994 bfce7f83 2019-07-27 stsp struct got_object_id_queue *commits, struct got_repository *repo)
4996 bfce7f83 2019-07-27 stsp const struct got_error *err = NULL;
4997 bfce7f83 2019-07-27 stsp struct got_object_qid *qid;
4998 bfce7f83 2019-07-27 stsp struct got_histedit_list_entry *hle;
4999 bfce7f83 2019-07-27 stsp static char msg[80];
5000 bfce7f83 2019-07-27 stsp char *id_str;
5002 bfce7f83 2019-07-27 stsp if (TAILQ_EMPTY(histedit_cmds))
5003 bfce7f83 2019-07-27 stsp return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
5004 bfce7f83 2019-07-27 stsp "histedit script contains no commands");
5005 a0de39f3 2019-08-09 stsp if (SIMPLEQ_EMPTY(commits))
5006 a0de39f3 2019-08-09 stsp return got_error(GOT_ERR_EMPTY_HISTEDIT);
5008 bfce7f83 2019-07-27 stsp SIMPLEQ_FOREACH(qid, commits, entry) {
5009 bfce7f83 2019-07-27 stsp TAILQ_FOREACH(hle, histedit_cmds, entry) {
5010 bfce7f83 2019-07-27 stsp if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
5013 bfce7f83 2019-07-27 stsp if (hle == NULL) {
5014 bfce7f83 2019-07-27 stsp err = got_object_id_str(&id_str, qid->id);
5016 bfce7f83 2019-07-27 stsp return err;
5017 bfce7f83 2019-07-27 stsp snprintf(msg, sizeof(msg),
5018 bfce7f83 2019-07-27 stsp "commit %s missing from histedit script", id_str);
5019 bfce7f83 2019-07-27 stsp free(id_str);
5020 bfce7f83 2019-07-27 stsp return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
5024 0def28b1 2019-08-17 stsp hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
5025 0def28b1 2019-08-17 stsp if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
5026 bfce7f83 2019-07-27 stsp return got_error_msg(GOT_ERR_HISTEDIT_CMD,
5027 bfce7f83 2019-07-27 stsp "last commit in histedit script cannot be folded");
5029 bfce7f83 2019-07-27 stsp return NULL;
5032 bfce7f83 2019-07-27 stsp static const struct got_error *
5033 0ebf8283 2019-07-24 stsp histedit_run_editor(struct got_histedit_list *histedit_cmds,
5034 bfce7f83 2019-07-27 stsp const char *path, struct got_object_id_queue *commits,
5035 bfce7f83 2019-07-27 stsp struct got_repository *repo)
5037 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
5038 0ebf8283 2019-07-24 stsp char *editor;
5039 0ebf8283 2019-07-24 stsp FILE *f = NULL;
5041 0ebf8283 2019-07-24 stsp err = get_editor(&editor);
5043 0ebf8283 2019-07-24 stsp return err;
5045 0ebf8283 2019-07-24 stsp if (spawn_editor(editor, path) == -1) {
5046 0ebf8283 2019-07-24 stsp err = got_error_from_errno("failed spawning editor");
5047 0ebf8283 2019-07-24 stsp goto done;
5050 0ebf8283 2019-07-24 stsp f = fopen(path, "r");
5051 0ebf8283 2019-07-24 stsp if (f == NULL) {
5052 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fopen");
5053 0ebf8283 2019-07-24 stsp goto done;
5055 0ebf8283 2019-07-24 stsp err = histedit_parse_list(histedit_cmds, f, repo);
5057 bfce7f83 2019-07-27 stsp goto done;
5059 bfce7f83 2019-07-27 stsp err = histedit_check_script(histedit_cmds, commits, repo);
5061 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
5062 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
5063 0ebf8283 2019-07-24 stsp free(editor);
5064 0ebf8283 2019-07-24 stsp return err;
5067 0ebf8283 2019-07-24 stsp static const struct got_error *
5068 bfce7f83 2019-07-27 stsp histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
5069 0ebf8283 2019-07-24 stsp struct got_object_id_queue *, const char *, struct got_repository *);
5071 0ebf8283 2019-07-24 stsp static const struct got_error *
5072 0ebf8283 2019-07-24 stsp histedit_edit_script(struct got_histedit_list *histedit_cmds,
5073 0ebf8283 2019-07-24 stsp struct got_object_id_queue *commits, struct got_repository *repo)
5075 0ebf8283 2019-07-24 stsp const struct got_error *err;
5076 0ebf8283 2019-07-24 stsp FILE *f = NULL;
5077 0ebf8283 2019-07-24 stsp char *path = NULL;
5079 0ebf8283 2019-07-24 stsp err = got_opentemp_named(&path, &f, "got-histedit");
5081 0ebf8283 2019-07-24 stsp return err;
5083 0ebf8283 2019-07-24 stsp err = write_cmd_list(f);
5085 0ebf8283 2019-07-24 stsp goto done;
5087 0ebf8283 2019-07-24 stsp err = histedit_write_commit_list(commits, f, repo);
5089 0ebf8283 2019-07-24 stsp goto done;
5091 0ebf8283 2019-07-24 stsp if (fclose(f) != 0) {
5092 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
5093 0ebf8283 2019-07-24 stsp goto done;
5097 bfce7f83 2019-07-27 stsp err = histedit_run_editor(histedit_cmds, path, commits, repo);
5098 0ebf8283 2019-07-24 stsp if (err) {
5099 bfce7f83 2019-07-27 stsp if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
5100 bfce7f83 2019-07-27 stsp err->code != GOT_ERR_HISTEDIT_CMD)
5101 0ebf8283 2019-07-24 stsp goto done;
5102 bfce7f83 2019-07-27 stsp err = histedit_edit_list_retry(histedit_cmds, err,
5103 0ebf8283 2019-07-24 stsp commits, path, repo);
5106 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
5107 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
5108 0ebf8283 2019-07-24 stsp if (path && unlink(path) != 0 && err == NULL)
5109 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("unlink", path);
5110 0ebf8283 2019-07-24 stsp free(path);
5111 0ebf8283 2019-07-24 stsp return err;
5114 0ebf8283 2019-07-24 stsp static const struct got_error *
5115 0ebf8283 2019-07-24 stsp histedit_save_list(struct got_histedit_list *histedit_cmds,
5116 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
5118 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
5119 0ebf8283 2019-07-24 stsp char *path = NULL;
5120 0ebf8283 2019-07-24 stsp FILE *f = NULL;
5121 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle;
5122 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
5124 c3022ba5 2019-07-27 stsp err = got_worktree_get_histedit_script_path(&path, worktree);
5126 0ebf8283 2019-07-24 stsp return err;
5128 0ebf8283 2019-07-24 stsp f = fopen(path, "w");
5129 0ebf8283 2019-07-24 stsp if (f == NULL) {
5130 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("fopen", path);
5131 0ebf8283 2019-07-24 stsp goto done;
5133 0ebf8283 2019-07-24 stsp TAILQ_FOREACH(hle, histedit_cmds, entry) {
5134 0ebf8283 2019-07-24 stsp err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
5139 0ebf8283 2019-07-24 stsp if (hle->logmsg) {
5140 0ebf8283 2019-07-24 stsp int n = fprintf(f, "%c %s\n",
5141 0ebf8283 2019-07-24 stsp GOT_HISTEDIT_MESG, hle->logmsg);
5142 0ebf8283 2019-07-24 stsp if (n < 0) {
5143 0ebf8283 2019-07-24 stsp err = got_ferror(f, GOT_ERR_IO);
5149 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
5150 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
5151 0ebf8283 2019-07-24 stsp free(path);
5152 0ebf8283 2019-07-24 stsp if (commit)
5153 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
5154 0ebf8283 2019-07-24 stsp return err;
5158 bfce7f83 2019-07-27 stsp histedit_free_list(struct got_histedit_list *histedit_cmds)
5160 bfce7f83 2019-07-27 stsp struct got_histedit_list_entry *hle;
5162 bfce7f83 2019-07-27 stsp while ((hle = TAILQ_FIRST(histedit_cmds))) {
5163 bfce7f83 2019-07-27 stsp TAILQ_REMOVE(histedit_cmds, hle, entry);
5164 bfce7f83 2019-07-27 stsp free(hle);
5168 0ebf8283 2019-07-24 stsp static const struct got_error *
5169 0ebf8283 2019-07-24 stsp histedit_load_list(struct got_histedit_list *histedit_cmds,
5170 0ebf8283 2019-07-24 stsp const char *path, struct got_repository *repo)
5172 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
5173 0ebf8283 2019-07-24 stsp FILE *f = NULL;
5175 0ebf8283 2019-07-24 stsp f = fopen(path, "r");
5176 0ebf8283 2019-07-24 stsp if (f == NULL) {
5177 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("fopen", path);
5178 0ebf8283 2019-07-24 stsp goto done;
5181 0ebf8283 2019-07-24 stsp err = histedit_parse_list(histedit_cmds, f, repo);
5183 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
5184 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
5185 0ebf8283 2019-07-24 stsp return err;
5188 0ebf8283 2019-07-24 stsp static const struct got_error *
5189 0ebf8283 2019-07-24 stsp histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
5190 bfce7f83 2019-07-27 stsp const struct got_error *edit_err, struct got_object_id_queue *commits,
5191 0ebf8283 2019-07-24 stsp const char *path, struct got_repository *repo)
5193 bfce7f83 2019-07-27 stsp const struct got_error *err = NULL, *prev_err = edit_err;
5194 0ebf8283 2019-07-24 stsp int resp = ' ';
5196 b006047e 2019-07-25 stsp while (resp != 'c' && resp != 'r' && resp != 'a') {
5197 0ebf8283 2019-07-24 stsp printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
5198 bfce7f83 2019-07-27 stsp "or (a)bort: ", getprogname(), prev_err->msg);
5199 0ebf8283 2019-07-24 stsp resp = getchar();
5200 a61a4414 2019-08-07 stsp if (resp == '\n')
5201 a61a4414 2019-08-07 stsp resp = getchar();
5202 426ebf2e 2019-07-25 stsp if (resp == 'c') {
5203 bfce7f83 2019-07-27 stsp histedit_free_list(histedit_cmds);
5204 bfce7f83 2019-07-27 stsp err = histedit_run_editor(histedit_cmds, path, commits,
5206 426ebf2e 2019-07-25 stsp if (err) {
5207 bfce7f83 2019-07-27 stsp if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
5208 bfce7f83 2019-07-27 stsp err->code != GOT_ERR_HISTEDIT_CMD)
5210 bfce7f83 2019-07-27 stsp prev_err = err;
5211 426ebf2e 2019-07-25 stsp resp = ' ';
5215 426ebf2e 2019-07-25 stsp } else if (resp == 'r') {
5216 bfce7f83 2019-07-27 stsp histedit_free_list(histedit_cmds);
5217 0ebf8283 2019-07-24 stsp err = histedit_edit_script(histedit_cmds,
5218 0ebf8283 2019-07-24 stsp commits, repo);
5219 426ebf2e 2019-07-25 stsp if (err) {
5220 bfce7f83 2019-07-27 stsp if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
5221 bfce7f83 2019-07-27 stsp err->code != GOT_ERR_HISTEDIT_CMD)
5223 bfce7f83 2019-07-27 stsp prev_err = err;
5224 426ebf2e 2019-07-25 stsp resp = ' ';
5228 426ebf2e 2019-07-25 stsp } else if (resp == 'a') {
5229 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_HISTEDIT_CANCEL);
5232 0ebf8283 2019-07-24 stsp printf("invalid response '%c'\n", resp);
5235 0ebf8283 2019-07-24 stsp return err;
5238 0ebf8283 2019-07-24 stsp static const struct got_error *
5239 0ebf8283 2019-07-24 stsp histedit_complete(struct got_worktree *worktree,
5240 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
5241 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_repository *repo)
5243 0ebf8283 2019-07-24 stsp printf("Switching work tree to %s\n",
5244 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch));
5245 3e3a69f1 2019-07-25 stsp return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
5246 3e3a69f1 2019-07-25 stsp branch, repo);
5249 0ebf8283 2019-07-24 stsp static const struct got_error *
5250 0ebf8283 2019-07-24 stsp show_histedit_progress(struct got_commit_object *commit,
5251 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle, struct got_object_id *new_id)
5253 0ebf8283 2019-07-24 stsp const struct got_error *err;
5254 0ebf8283 2019-07-24 stsp char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
5256 0ebf8283 2019-07-24 stsp err = got_object_id_str(&old_id_str, hle->commit_id);
5258 0ebf8283 2019-07-24 stsp goto done;
5260 0ebf8283 2019-07-24 stsp if (new_id) {
5261 0ebf8283 2019-07-24 stsp err = got_object_id_str(&new_id_str, new_id);
5263 0ebf8283 2019-07-24 stsp goto done;
5266 0ebf8283 2019-07-24 stsp old_id_str[12] = '\0';
5267 0ebf8283 2019-07-24 stsp if (new_id_str)
5268 0ebf8283 2019-07-24 stsp new_id_str[12] = '\0';
5270 0ebf8283 2019-07-24 stsp if (hle->logmsg) {
5271 0ebf8283 2019-07-24 stsp logmsg = strdup(hle->logmsg);
5272 0ebf8283 2019-07-24 stsp if (logmsg == NULL) {
5273 0ebf8283 2019-07-24 stsp err = got_error_from_errno("strdup");
5274 0ebf8283 2019-07-24 stsp goto done;
5276 0ebf8283 2019-07-24 stsp trim_logmsg(logmsg, 42);
5278 0ebf8283 2019-07-24 stsp err = get_short_logmsg(&logmsg, 42, commit);
5280 0ebf8283 2019-07-24 stsp goto done;
5283 0ebf8283 2019-07-24 stsp switch (hle->cmd->code) {
5284 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_PICK:
5285 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_EDIT:
5286 0ebf8283 2019-07-24 stsp printf("%s -> %s: %s\n", old_id_str,
5287 0ebf8283 2019-07-24 stsp new_id_str ? new_id_str : "no-op change", logmsg);
5289 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_DROP:
5290 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_FOLD:
5291 0ebf8283 2019-07-24 stsp printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
5299 0ebf8283 2019-07-24 stsp free(old_id_str);
5300 0ebf8283 2019-07-24 stsp free(new_id_str);
5301 0ebf8283 2019-07-24 stsp return err;
5304 0ebf8283 2019-07-24 stsp static const struct got_error *
5305 0ebf8283 2019-07-24 stsp histedit_commit(struct got_pathlist_head *merged_paths,
5306 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
5307 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
5308 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
5310 0ebf8283 2019-07-24 stsp const struct got_error *err;
5311 0ebf8283 2019-07-24 stsp struct got_commit_object *commit;
5312 0ebf8283 2019-07-24 stsp struct got_object_id *new_commit_id;
5314 0ebf8283 2019-07-24 stsp if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
5315 0ebf8283 2019-07-24 stsp && hle->logmsg == NULL) {
5316 0ebf8283 2019-07-24 stsp err = histedit_edit_logmsg(hle, repo);
5318 0ebf8283 2019-07-24 stsp return err;
5321 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&commit, repo, hle->commit_id);
5323 0ebf8283 2019-07-24 stsp return err;
5325 0ebf8283 2019-07-24 stsp err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
5326 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, commit, hle->commit_id,
5327 3e3a69f1 2019-07-25 stsp hle->logmsg, repo);
5328 0ebf8283 2019-07-24 stsp if (err) {
5329 0ebf8283 2019-07-24 stsp if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
5330 0ebf8283 2019-07-24 stsp goto done;
5331 0ebf8283 2019-07-24 stsp err = show_histedit_progress(commit, hle, NULL);
5333 0ebf8283 2019-07-24 stsp err = show_histedit_progress(commit, hle, new_commit_id);
5334 0ebf8283 2019-07-24 stsp free(new_commit_id);
5337 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
5338 0ebf8283 2019-07-24 stsp return err;
5341 0ebf8283 2019-07-24 stsp static const struct got_error *
5342 0ebf8283 2019-07-24 stsp histedit_skip_commit(struct got_histedit_list_entry *hle,
5343 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
5345 0ebf8283 2019-07-24 stsp const struct got_error *error;
5346 0ebf8283 2019-07-24 stsp struct got_commit_object *commit;
5348 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
5350 0ebf8283 2019-07-24 stsp if (error)
5351 0ebf8283 2019-07-24 stsp return error;
5353 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo, hle->commit_id);
5354 0ebf8283 2019-07-24 stsp if (error)
5355 0ebf8283 2019-07-24 stsp return error;
5357 0ebf8283 2019-07-24 stsp error = show_histedit_progress(commit, hle, NULL);
5358 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
5359 0ebf8283 2019-07-24 stsp return error;
5362 0ebf8283 2019-07-24 stsp static const struct got_error *
5363 0ebf8283 2019-07-24 stsp cmd_histedit(int argc, char *argv[])
5365 0ebf8283 2019-07-24 stsp const struct got_error *error = NULL;
5366 0ebf8283 2019-07-24 stsp struct got_worktree *worktree = NULL;
5367 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex = NULL;
5368 0ebf8283 2019-07-24 stsp struct got_repository *repo = NULL;
5369 0ebf8283 2019-07-24 stsp char *cwd = NULL;
5370 0ebf8283 2019-07-24 stsp struct got_reference *branch = NULL;
5371 0ebf8283 2019-07-24 stsp struct got_reference *tmp_branch = NULL;
5372 0ebf8283 2019-07-24 stsp struct got_object_id *resume_commit_id = NULL;
5373 0ebf8283 2019-07-24 stsp struct got_object_id *base_commit_id = NULL;
5374 0ebf8283 2019-07-24 stsp struct got_object_id *head_commit_id = NULL;
5375 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
5376 6ba2bea2 2019-07-27 stsp int ch, rebase_in_progress = 0, did_something;
5377 0ebf8283 2019-07-24 stsp int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
5378 0ebf8283 2019-07-24 stsp const char *edit_script_path = NULL;
5379 0ebf8283 2019-07-24 stsp unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
5380 0ebf8283 2019-07-24 stsp struct got_object_id_queue commits;
5381 0ebf8283 2019-07-24 stsp struct got_pathlist_head merged_paths;
5382 0ebf8283 2019-07-24 stsp const struct got_object_id_queue *parent_ids;
5383 0ebf8283 2019-07-24 stsp struct got_object_qid *pid;
5384 0ebf8283 2019-07-24 stsp struct got_histedit_list histedit_cmds;
5385 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle;
5387 0ebf8283 2019-07-24 stsp SIMPLEQ_INIT(&commits);
5388 0ebf8283 2019-07-24 stsp TAILQ_INIT(&histedit_cmds);
5389 0ebf8283 2019-07-24 stsp TAILQ_INIT(&merged_paths);
5391 0ebf8283 2019-07-24 stsp while ((ch = getopt(argc, argv, "acF:")) != -1) {
5392 0ebf8283 2019-07-24 stsp switch (ch) {
5394 0ebf8283 2019-07-24 stsp abort_edit = 1;
5397 0ebf8283 2019-07-24 stsp continue_edit = 1;
5400 0ebf8283 2019-07-24 stsp edit_script_path = optarg;
5403 0ebf8283 2019-07-24 stsp usage_histedit();
5404 0ebf8283 2019-07-24 stsp /* NOTREACHED */
5408 0ebf8283 2019-07-24 stsp argc -= optind;
5409 0ebf8283 2019-07-24 stsp argv += optind;
5411 0ebf8283 2019-07-24 stsp #ifndef PROFILE
5412 0ebf8283 2019-07-24 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5413 0ebf8283 2019-07-24 stsp "unveil", NULL) == -1)
5414 0ebf8283 2019-07-24 stsp err(1, "pledge");
5416 0ebf8283 2019-07-24 stsp if (abort_edit && continue_edit)
5417 0ebf8283 2019-07-24 stsp usage_histedit();
5418 0ebf8283 2019-07-24 stsp if (argc != 0)
5419 0ebf8283 2019-07-24 stsp usage_histedit();
5422 0ebf8283 2019-07-24 stsp * This command cannot apply unveil(2) in all cases because the
5423 0ebf8283 2019-07-24 stsp * user may choose to run an editor to edit the histedit script
5424 0ebf8283 2019-07-24 stsp * and to edit individual commit log messages.
5425 0ebf8283 2019-07-24 stsp * unveil(2) traverses exec(2); if an editor is used we have to
5426 0ebf8283 2019-07-24 stsp * apply unveil after edit script and log messages have been written.
5427 0ebf8283 2019-07-24 stsp * XXX TODO: Make use of unveil(2) where possible.
5430 0ebf8283 2019-07-24 stsp cwd = getcwd(NULL, 0);
5431 0ebf8283 2019-07-24 stsp if (cwd == NULL) {
5432 0ebf8283 2019-07-24 stsp error = got_error_from_errno("getcwd");
5433 0ebf8283 2019-07-24 stsp goto done;
5435 0ebf8283 2019-07-24 stsp error = got_worktree_open(&worktree, cwd);
5436 0ebf8283 2019-07-24 stsp if (error)
5437 0ebf8283 2019-07-24 stsp goto done;
5439 0ebf8283 2019-07-24 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
5440 0ebf8283 2019-07-24 stsp if (error != NULL)
5441 0ebf8283 2019-07-24 stsp goto done;
5443 0ebf8283 2019-07-24 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
5444 0ebf8283 2019-07-24 stsp if (error)
5445 0ebf8283 2019-07-24 stsp goto done;
5446 0ebf8283 2019-07-24 stsp if (rebase_in_progress) {
5447 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_REBASING);
5448 0ebf8283 2019-07-24 stsp goto done;
5451 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
5452 0ebf8283 2019-07-24 stsp if (error)
5453 0ebf8283 2019-07-24 stsp goto done;
5455 0ebf8283 2019-07-24 stsp if (edit_in_progress && abort_edit) {
5456 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_continue(&resume_commit_id,
5457 3e3a69f1 2019-07-25 stsp &tmp_branch, &branch, &base_commit_id, &fileindex,
5458 3e3a69f1 2019-07-25 stsp worktree, repo);
5459 0ebf8283 2019-07-24 stsp if (error)
5460 0ebf8283 2019-07-24 stsp goto done;
5461 0ebf8283 2019-07-24 stsp printf("Switching work tree to %s\n",
5462 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch));
5463 3e3a69f1 2019-07-25 stsp error = got_worktree_histedit_abort(worktree, fileindex, repo,
5464 0ebf8283 2019-07-24 stsp branch, base_commit_id, update_progress, &did_something);
5465 0ebf8283 2019-07-24 stsp if (error)
5466 0ebf8283 2019-07-24 stsp goto done;
5467 0ebf8283 2019-07-24 stsp printf("Histedit of %s aborted\n",
5468 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch));
5469 0ebf8283 2019-07-24 stsp goto done; /* nothing else to do */
5470 0ebf8283 2019-07-24 stsp } else if (abort_edit) {
5471 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_NOT_HISTEDIT);
5472 0ebf8283 2019-07-24 stsp goto done;
5475 0ebf8283 2019-07-24 stsp if (continue_edit) {
5476 0ebf8283 2019-07-24 stsp char *path;
5478 0ebf8283 2019-07-24 stsp if (!edit_in_progress) {
5479 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_NOT_HISTEDIT);
5480 0ebf8283 2019-07-24 stsp goto done;
5483 c3022ba5 2019-07-27 stsp error = got_worktree_get_histedit_script_path(&path, worktree);
5484 0ebf8283 2019-07-24 stsp if (error)
5485 0ebf8283 2019-07-24 stsp goto done;
5487 0ebf8283 2019-07-24 stsp error = histedit_load_list(&histedit_cmds, path, repo);
5488 0ebf8283 2019-07-24 stsp free(path);
5489 0ebf8283 2019-07-24 stsp if (error)
5490 0ebf8283 2019-07-24 stsp goto done;
5492 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_continue(&resume_commit_id,
5493 3e3a69f1 2019-07-25 stsp &tmp_branch, &branch, &base_commit_id, &fileindex,
5494 3e3a69f1 2019-07-25 stsp worktree, repo);
5495 0ebf8283 2019-07-24 stsp if (error)
5496 0ebf8283 2019-07-24 stsp goto done;
5498 0ebf8283 2019-07-24 stsp error = got_ref_resolve(&head_commit_id, repo, branch);
5499 0ebf8283 2019-07-24 stsp if (error)
5500 0ebf8283 2019-07-24 stsp goto done;
5502 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo,
5503 0ebf8283 2019-07-24 stsp head_commit_id);
5504 0ebf8283 2019-07-24 stsp if (error)
5505 0ebf8283 2019-07-24 stsp goto done;
5506 0ebf8283 2019-07-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
5507 0ebf8283 2019-07-24 stsp pid = SIMPLEQ_FIRST(parent_ids);
5508 3aac7cf7 2019-07-25 stsp if (pid == NULL) {
5509 3aac7cf7 2019-07-25 stsp error = got_error(GOT_ERR_EMPTY_HISTEDIT);
5510 3aac7cf7 2019-07-25 stsp goto done;
5512 8ca9bd68 2019-07-25 stsp error = collect_commits(&commits, head_commit_id, pid->id,
5513 8ca9bd68 2019-07-25 stsp base_commit_id, got_worktree_get_path_prefix(worktree),
5514 8ca9bd68 2019-07-25 stsp GOT_ERR_HISTEDIT_PATH, repo);
5515 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
5516 0ebf8283 2019-07-24 stsp commit = NULL;
5517 0ebf8283 2019-07-24 stsp if (error)
5518 0ebf8283 2019-07-24 stsp goto done;
5520 0ebf8283 2019-07-24 stsp if (edit_in_progress) {
5521 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_HISTEDIT_BUSY);
5522 0ebf8283 2019-07-24 stsp goto done;
5525 0ebf8283 2019-07-24 stsp error = got_ref_open(&branch, repo,
5526 0ebf8283 2019-07-24 stsp got_worktree_get_head_ref_name(worktree), 0);
5527 0ebf8283 2019-07-24 stsp if (error != NULL)
5528 0ebf8283 2019-07-24 stsp goto done;
5530 c7d20a3f 2019-07-30 stsp if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
5531 c7d20a3f 2019-07-30 stsp error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
5532 c7d20a3f 2019-07-30 stsp "will not edit commit history of a branch outside "
5533 c7d20a3f 2019-07-30 stsp "the \"refs/heads/\" reference namespace");
5534 c7d20a3f 2019-07-30 stsp goto done;
5537 0ebf8283 2019-07-24 stsp error = got_ref_resolve(&head_commit_id, repo, branch);
5538 bfce7f83 2019-07-27 stsp got_ref_close(branch);
5539 bfce7f83 2019-07-27 stsp branch = NULL;
5540 0ebf8283 2019-07-24 stsp if (error)
5541 0ebf8283 2019-07-24 stsp goto done;
5543 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo,
5544 0ebf8283 2019-07-24 stsp head_commit_id);
5545 0ebf8283 2019-07-24 stsp if (error)
5546 0ebf8283 2019-07-24 stsp goto done;
5547 0ebf8283 2019-07-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
5548 0ebf8283 2019-07-24 stsp pid = SIMPLEQ_FIRST(parent_ids);
5549 3aac7cf7 2019-07-25 stsp if (pid == NULL) {
5550 3aac7cf7 2019-07-25 stsp error = got_error(GOT_ERR_EMPTY_HISTEDIT);
5551 3aac7cf7 2019-07-25 stsp goto done;
5553 8ca9bd68 2019-07-25 stsp error = collect_commits(&commits, head_commit_id, pid->id,
5554 8ca9bd68 2019-07-25 stsp got_worktree_get_base_commit_id(worktree),
5555 8ca9bd68 2019-07-25 stsp got_worktree_get_path_prefix(worktree),
5556 8ca9bd68 2019-07-25 stsp GOT_ERR_HISTEDIT_PATH, repo);
5557 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
5558 0ebf8283 2019-07-24 stsp commit = NULL;
5559 0ebf8283 2019-07-24 stsp if (error)
5560 0ebf8283 2019-07-24 stsp goto done;
5562 bfce7f83 2019-07-27 stsp error = got_worktree_histedit_prepare(&tmp_branch, &branch,
5563 bfce7f83 2019-07-27 stsp &base_commit_id, &fileindex, worktree, repo);
5564 bfce7f83 2019-07-27 stsp if (error)
5565 bfce7f83 2019-07-27 stsp goto done;
5567 0ebf8283 2019-07-24 stsp if (edit_script_path) {
5568 0ebf8283 2019-07-24 stsp error = histedit_load_list(&histedit_cmds,
5569 0ebf8283 2019-07-24 stsp edit_script_path, repo);
5570 6ba2bea2 2019-07-27 stsp if (error) {
5571 6ba2bea2 2019-07-27 stsp got_worktree_histedit_abort(worktree, fileindex,
5572 6ba2bea2 2019-07-27 stsp repo, branch, base_commit_id,
5573 6ba2bea2 2019-07-27 stsp update_progress, &did_something);
5574 0ebf8283 2019-07-24 stsp goto done;
5577 0ebf8283 2019-07-24 stsp error = histedit_edit_script(&histedit_cmds, &commits,
5579 6ba2bea2 2019-07-27 stsp if (error) {
5580 6ba2bea2 2019-07-27 stsp got_worktree_histedit_abort(worktree, fileindex,
5581 6ba2bea2 2019-07-27 stsp repo, branch, base_commit_id,
5582 6ba2bea2 2019-07-27 stsp update_progress, &did_something);
5583 0ebf8283 2019-07-24 stsp goto done;
5588 0ebf8283 2019-07-24 stsp error = histedit_save_list(&histedit_cmds, worktree,
5590 6ba2bea2 2019-07-27 stsp if (error) {
5591 6ba2bea2 2019-07-27 stsp got_worktree_histedit_abort(worktree, fileindex,
5592 6ba2bea2 2019-07-27 stsp repo, branch, base_commit_id,
5593 6ba2bea2 2019-07-27 stsp update_progress, &did_something);
5594 0ebf8283 2019-07-24 stsp goto done;
5599 0ebf8283 2019-07-24 stsp error = histedit_check_script(&histedit_cmds, &commits, repo);
5600 0ebf8283 2019-07-24 stsp if (error)
5601 0ebf8283 2019-07-24 stsp goto done;
5603 0ebf8283 2019-07-24 stsp TAILQ_FOREACH(hle, &histedit_cmds, entry) {
5604 0ebf8283 2019-07-24 stsp if (resume_commit_id) {
5605 0ebf8283 2019-07-24 stsp if (got_object_id_cmp(hle->commit_id,
5606 0ebf8283 2019-07-24 stsp resume_commit_id) != 0)
5609 0ebf8283 2019-07-24 stsp resume_commit_id = NULL;
5610 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_DROP ||
5611 0ebf8283 2019-07-24 stsp hle->cmd->code == GOT_HISTEDIT_FOLD) {
5612 0ebf8283 2019-07-24 stsp error = histedit_skip_commit(hle, worktree,
5615 0ebf8283 2019-07-24 stsp error = histedit_commit(NULL, worktree,
5616 3e3a69f1 2019-07-25 stsp fileindex, tmp_branch, hle, repo);
5618 0ebf8283 2019-07-24 stsp if (error)
5619 0ebf8283 2019-07-24 stsp goto done;
5623 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_DROP) {
5624 0ebf8283 2019-07-24 stsp error = histedit_skip_commit(hle, worktree, repo);
5625 0ebf8283 2019-07-24 stsp if (error)
5626 0ebf8283 2019-07-24 stsp goto done;
5630 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo,
5631 0ebf8283 2019-07-24 stsp hle->commit_id);
5632 0ebf8283 2019-07-24 stsp if (error)
5633 0ebf8283 2019-07-24 stsp goto done;
5634 0ebf8283 2019-07-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
5635 0ebf8283 2019-07-24 stsp pid = SIMPLEQ_FIRST(parent_ids);
5637 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_merge_files(&merged_paths,
5638 3e3a69f1 2019-07-25 stsp worktree, fileindex, pid->id, hle->commit_id, repo,
5639 3e3a69f1 2019-07-25 stsp rebase_progress, &rebase_status, check_cancelled, NULL);
5640 0ebf8283 2019-07-24 stsp if (error)
5641 0ebf8283 2019-07-24 stsp goto done;
5642 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
5643 0ebf8283 2019-07-24 stsp commit = NULL;
5645 0ebf8283 2019-07-24 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
5646 0ebf8283 2019-07-24 stsp got_worktree_rebase_pathlist_free(&merged_paths);
5650 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
5651 0ebf8283 2019-07-24 stsp char *id_str;
5652 0ebf8283 2019-07-24 stsp error = got_object_id_str(&id_str, hle->commit_id);
5653 0ebf8283 2019-07-24 stsp if (error)
5654 0ebf8283 2019-07-24 stsp goto done;
5655 0ebf8283 2019-07-24 stsp printf("Stopping histedit for amending commit %s\n",
5657 0ebf8283 2019-07-24 stsp free(id_str);
5658 0ebf8283 2019-07-24 stsp got_worktree_rebase_pathlist_free(&merged_paths);
5659 3e3a69f1 2019-07-25 stsp error = got_worktree_histedit_postpone(worktree,
5660 3e3a69f1 2019-07-25 stsp fileindex);
5661 0ebf8283 2019-07-24 stsp goto done;
5664 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
5665 0ebf8283 2019-07-24 stsp error = histedit_skip_commit(hle, worktree, repo);
5666 0ebf8283 2019-07-24 stsp if (error)
5667 0ebf8283 2019-07-24 stsp goto done;
5671 3e3a69f1 2019-07-25 stsp error = histedit_commit(&merged_paths, worktree, fileindex,
5672 3e3a69f1 2019-07-25 stsp tmp_branch, hle, repo);
5673 0ebf8283 2019-07-24 stsp got_worktree_rebase_pathlist_free(&merged_paths);
5674 0ebf8283 2019-07-24 stsp if (error)
5675 0ebf8283 2019-07-24 stsp goto done;
5678 0ebf8283 2019-07-24 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
5679 3e3a69f1 2019-07-25 stsp error = got_worktree_histedit_postpone(worktree, fileindex);
5680 0ebf8283 2019-07-24 stsp if (error)
5681 0ebf8283 2019-07-24 stsp goto done;
5682 0ebf8283 2019-07-24 stsp error = got_error_msg(GOT_ERR_CONFLICTS,
5683 0ebf8283 2019-07-24 stsp "conflicts must be resolved before rebasing can continue");
5685 3e3a69f1 2019-07-25 stsp error = histedit_complete(worktree, fileindex, tmp_branch,
5686 3e3a69f1 2019-07-25 stsp branch, repo);
5688 0ebf8283 2019-07-24 stsp got_object_id_queue_free(&commits);
5689 bfce7f83 2019-07-27 stsp histedit_free_list(&histedit_cmds);
5690 0ebf8283 2019-07-24 stsp free(head_commit_id);
5691 0ebf8283 2019-07-24 stsp free(base_commit_id);
5692 0ebf8283 2019-07-24 stsp free(resume_commit_id);
5693 0ebf8283 2019-07-24 stsp if (commit)
5694 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
5695 0ebf8283 2019-07-24 stsp if (branch)
5696 0ebf8283 2019-07-24 stsp got_ref_close(branch);
5697 0ebf8283 2019-07-24 stsp if (tmp_branch)
5698 0ebf8283 2019-07-24 stsp got_ref_close(tmp_branch);
5699 0ebf8283 2019-07-24 stsp if (worktree)
5700 0ebf8283 2019-07-24 stsp got_worktree_close(worktree);
5702 715dc77e 2019-08-03 stsp got_repo_close(repo);
5703 715dc77e 2019-08-03 stsp return error;
5706 715dc77e 2019-08-03 stsp __dead static void
5707 715dc77e 2019-08-03 stsp usage_stage(void)
5709 f3055044 2019-08-07 stsp fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
5710 f3055044 2019-08-07 stsp "[file-path ...]\n",
5711 715dc77e 2019-08-03 stsp getprogname());
5715 715dc77e 2019-08-03 stsp static const struct got_error *
5716 408b4ebc 2019-08-03 stsp print_stage(void *arg, unsigned char status, unsigned char staged_status,
5717 408b4ebc 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
5718 408b4ebc 2019-08-03 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
5720 408b4ebc 2019-08-03 stsp const struct got_error *err = NULL;
5721 408b4ebc 2019-08-03 stsp char *id_str = NULL;
5723 408b4ebc 2019-08-03 stsp if (staged_status != GOT_STATUS_ADD &&
5724 408b4ebc 2019-08-03 stsp staged_status != GOT_STATUS_MODIFY &&
5725 408b4ebc 2019-08-03 stsp staged_status != GOT_STATUS_DELETE)
5726 408b4ebc 2019-08-03 stsp return NULL;
5728 408b4ebc 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
5729 408b4ebc 2019-08-03 stsp staged_status == GOT_STATUS_MODIFY)
5730 408b4ebc 2019-08-03 stsp err = got_object_id_str(&id_str, staged_blob_id);
5732 408b4ebc 2019-08-03 stsp err = got_object_id_str(&id_str, blob_id);
5734 408b4ebc 2019-08-03 stsp return err;
5736 408b4ebc 2019-08-03 stsp printf("%s %c %s\n", id_str, staged_status, path);
5737 408b4ebc 2019-08-03 stsp free(id_str);
5738 dc424a06 2019-08-07 stsp return NULL;
5741 dc424a06 2019-08-07 stsp static const struct got_error *
5742 715dc77e 2019-08-03 stsp cmd_stage(int argc, char *argv[])
5744 715dc77e 2019-08-03 stsp const struct got_error *error = NULL;
5745 715dc77e 2019-08-03 stsp struct got_repository *repo = NULL;
5746 715dc77e 2019-08-03 stsp struct got_worktree *worktree = NULL;
5747 715dc77e 2019-08-03 stsp char *cwd = NULL;
5748 715dc77e 2019-08-03 stsp struct got_pathlist_head paths;
5749 715dc77e 2019-08-03 stsp struct got_pathlist_entry *pe;
5750 dc424a06 2019-08-07 stsp int ch, list_stage = 0, pflag = 0;
5751 dc424a06 2019-08-07 stsp FILE *patch_script_file = NULL;
5752 2e1f37b0 2019-08-08 stsp const char *patch_script_path = NULL;
5753 2e1f37b0 2019-08-08 stsp struct choose_patch_arg cpa;
5755 715dc77e 2019-08-03 stsp TAILQ_INIT(&paths);
5757 dc424a06 2019-08-07 stsp while ((ch = getopt(argc, argv, "lpF:")) != -1) {
5758 715dc77e 2019-08-03 stsp switch (ch) {
5760 408b4ebc 2019-08-03 stsp list_stage = 1;
5763 dc424a06 2019-08-07 stsp pflag = 1;
5766 dc424a06 2019-08-07 stsp patch_script_path = optarg;
5769 715dc77e 2019-08-03 stsp usage_stage();
5770 715dc77e 2019-08-03 stsp /* NOTREACHED */
5774 715dc77e 2019-08-03 stsp argc -= optind;
5775 715dc77e 2019-08-03 stsp argv += optind;
5777 715dc77e 2019-08-03 stsp #ifndef PROFILE
5778 715dc77e 2019-08-03 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5779 715dc77e 2019-08-03 stsp "unveil", NULL) == -1)
5780 715dc77e 2019-08-03 stsp err(1, "pledge");
5782 2db2652d 2019-08-07 stsp if (list_stage && (pflag || patch_script_path))
5783 2db2652d 2019-08-07 stsp errx(1, "-l option cannot be used with other options");
5784 dc424a06 2019-08-07 stsp if (patch_script_path && !pflag)
5785 dc424a06 2019-08-07 stsp errx(1, "-F option can only be used together with -p option");
5787 715dc77e 2019-08-03 stsp cwd = getcwd(NULL, 0);
5788 715dc77e 2019-08-03 stsp if (cwd == NULL) {
5789 715dc77e 2019-08-03 stsp error = got_error_from_errno("getcwd");
5790 715dc77e 2019-08-03 stsp goto done;
5793 715dc77e 2019-08-03 stsp error = got_worktree_open(&worktree, cwd);
5794 715dc77e 2019-08-03 stsp if (error)
5795 715dc77e 2019-08-03 stsp goto done;
5797 715dc77e 2019-08-03 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
5798 715dc77e 2019-08-03 stsp if (error != NULL)
5799 715dc77e 2019-08-03 stsp goto done;
5801 dc424a06 2019-08-07 stsp if (patch_script_path) {
5802 dc424a06 2019-08-07 stsp patch_script_file = fopen(patch_script_path, "r");
5803 dc424a06 2019-08-07 stsp if (patch_script_file == NULL) {
5804 dc424a06 2019-08-07 stsp error = got_error_from_errno2("fopen",
5805 dc424a06 2019-08-07 stsp patch_script_path);
5806 dc424a06 2019-08-07 stsp goto done;
5809 715dc77e 2019-08-03 stsp error = apply_unveil(got_repo_get_path(repo), 1,
5810 715dc77e 2019-08-03 stsp got_worktree_get_root_path(worktree));
5811 715dc77e 2019-08-03 stsp if (error)
5812 715dc77e 2019-08-03 stsp goto done;
5814 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5815 6d022e97 2019-08-04 stsp if (error)
5816 6d022e97 2019-08-04 stsp goto done;
5818 6d022e97 2019-08-04 stsp if (list_stage)
5819 408b4ebc 2019-08-03 stsp error = got_worktree_status(worktree, &paths, repo,
5820 408b4ebc 2019-08-03 stsp print_stage, NULL, check_cancelled, NULL);
5822 2e1f37b0 2019-08-08 stsp cpa.patch_script_file = patch_script_file;
5823 2e1f37b0 2019-08-08 stsp cpa.action = "stage";
5824 dc424a06 2019-08-07 stsp error = got_worktree_stage(worktree, &paths,
5825 dc424a06 2019-08-07 stsp pflag ? NULL : print_status, NULL,
5826 2e1f37b0 2019-08-08 stsp pflag ? choose_patch : NULL, &cpa, repo);
5829 2e1f37b0 2019-08-08 stsp if (patch_script_file && fclose(patch_script_file) == EOF &&
5830 2e1f37b0 2019-08-08 stsp error == NULL)
5831 2e1f37b0 2019-08-08 stsp error = got_error_from_errno2("fclose", patch_script_path);
5833 ad493afc 2019-08-03 stsp got_repo_close(repo);
5834 ad493afc 2019-08-03 stsp if (worktree)
5835 ad493afc 2019-08-03 stsp got_worktree_close(worktree);
5836 ad493afc 2019-08-03 stsp TAILQ_FOREACH(pe, &paths, entry)
5837 ad493afc 2019-08-03 stsp free((char *)pe->path);
5838 ad493afc 2019-08-03 stsp got_pathlist_free(&paths);
5839 ad493afc 2019-08-03 stsp free(cwd);
5840 ad493afc 2019-08-03 stsp return error;
5843 ad493afc 2019-08-03 stsp __dead static void
5844 ad493afc 2019-08-03 stsp usage_unstage(void)
5846 2e1f37b0 2019-08-08 stsp fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
5847 2e1f37b0 2019-08-08 stsp "[file-path ...]\n",
5848 ad493afc 2019-08-03 stsp getprogname());
5853 ad493afc 2019-08-03 stsp static const struct got_error *
5854 ad493afc 2019-08-03 stsp cmd_unstage(int argc, char *argv[])
5856 ad493afc 2019-08-03 stsp const struct got_error *error = NULL;
5857 ad493afc 2019-08-03 stsp struct got_repository *repo = NULL;
5858 ad493afc 2019-08-03 stsp struct got_worktree *worktree = NULL;
5859 ad493afc 2019-08-03 stsp char *cwd = NULL;
5860 ad493afc 2019-08-03 stsp struct got_pathlist_head paths;
5861 ad493afc 2019-08-03 stsp struct got_pathlist_entry *pe;
5862 2e1f37b0 2019-08-08 stsp int ch, did_something = 0, pflag = 0;
5863 2e1f37b0 2019-08-08 stsp FILE *patch_script_file = NULL;
5864 2e1f37b0 2019-08-08 stsp const char *patch_script_path = NULL;
5865 2e1f37b0 2019-08-08 stsp struct choose_patch_arg cpa;
5867 ad493afc 2019-08-03 stsp TAILQ_INIT(&paths);
5869 2e1f37b0 2019-08-08 stsp while ((ch = getopt(argc, argv, "pF:")) != -1) {
5870 ad493afc 2019-08-03 stsp switch (ch) {
5872 2e1f37b0 2019-08-08 stsp pflag = 1;
5875 2e1f37b0 2019-08-08 stsp patch_script_path = optarg;
5878 ad493afc 2019-08-03 stsp usage_unstage();
5879 ad493afc 2019-08-03 stsp /* NOTREACHED */
5883 ad493afc 2019-08-03 stsp argc -= optind;
5884 ad493afc 2019-08-03 stsp argv += optind;
5886 ad493afc 2019-08-03 stsp #ifndef PROFILE
5887 ad493afc 2019-08-03 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5888 ad493afc 2019-08-03 stsp "unveil", NULL) == -1)
5889 ad493afc 2019-08-03 stsp err(1, "pledge");
5891 2e1f37b0 2019-08-08 stsp if (patch_script_path && !pflag)
5892 2e1f37b0 2019-08-08 stsp errx(1, "-F option can only be used together with -p option");
5894 ad493afc 2019-08-03 stsp cwd = getcwd(NULL, 0);
5895 ad493afc 2019-08-03 stsp if (cwd == NULL) {
5896 ad493afc 2019-08-03 stsp error = got_error_from_errno("getcwd");
5897 ad493afc 2019-08-03 stsp goto done;
5900 ad493afc 2019-08-03 stsp error = got_worktree_open(&worktree, cwd);
5901 ad493afc 2019-08-03 stsp if (error)
5902 ad493afc 2019-08-03 stsp goto done;
5904 ad493afc 2019-08-03 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
5905 ad493afc 2019-08-03 stsp if (error != NULL)
5906 ad493afc 2019-08-03 stsp goto done;
5908 2e1f37b0 2019-08-08 stsp if (patch_script_path) {
5909 2e1f37b0 2019-08-08 stsp patch_script_file = fopen(patch_script_path, "r");
5910 2e1f37b0 2019-08-08 stsp if (patch_script_file == NULL) {
5911 2e1f37b0 2019-08-08 stsp error = got_error_from_errno2("fopen",
5912 2e1f37b0 2019-08-08 stsp patch_script_path);
5913 2e1f37b0 2019-08-08 stsp goto done;
5917 ad493afc 2019-08-03 stsp error = apply_unveil(got_repo_get_path(repo), 1,
5918 ad493afc 2019-08-03 stsp got_worktree_get_root_path(worktree));
5919 ad493afc 2019-08-03 stsp if (error)
5920 ad493afc 2019-08-03 stsp goto done;
5922 ad493afc 2019-08-03 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5923 ad493afc 2019-08-03 stsp if (error)
5924 ad493afc 2019-08-03 stsp goto done;
5926 2e1f37b0 2019-08-08 stsp cpa.patch_script_file = patch_script_file;
5927 2e1f37b0 2019-08-08 stsp cpa.action = "unstage";
5928 ad493afc 2019-08-03 stsp error = got_worktree_unstage(worktree, &paths, update_progress,
5929 2e1f37b0 2019-08-08 stsp &did_something, pflag ? choose_patch : NULL, &cpa, repo);
5931 2e1f37b0 2019-08-08 stsp if (patch_script_file && fclose(patch_script_file) == EOF &&
5932 2e1f37b0 2019-08-08 stsp error == NULL)
5933 2e1f37b0 2019-08-08 stsp error = got_error_from_errno2("fclose", patch_script_path);
5935 0ebf8283 2019-07-24 stsp got_repo_close(repo);
5936 715dc77e 2019-08-03 stsp if (worktree)
5937 715dc77e 2019-08-03 stsp got_worktree_close(worktree);
5938 715dc77e 2019-08-03 stsp TAILQ_FOREACH(pe, &paths, entry)
5939 715dc77e 2019-08-03 stsp free((char *)pe->path);
5940 715dc77e 2019-08-03 stsp got_pathlist_free(&paths);
5941 715dc77e 2019-08-03 stsp free(cwd);
5942 0ebf8283 2019-07-24 stsp return error;