2 f42b1b34 2018-03-12 stsp * Copyright (c) 2017 Martin Pieuchot <mpi@openbsd.org>
3 5d56da81 2019-01-13 stsp * Copyright (c) 2018, 2019 Stefan Sperling <stsp@openbsd.org>
5 5c860e29 2018-03-12 stsp * Permission to use, copy, modify, and distribute this software for any
6 5c860e29 2018-03-12 stsp * purpose with or without fee is hereby granted, provided that the above
7 5c860e29 2018-03-12 stsp * copyright notice and this permission notice appear in all copies.
9 5c860e29 2018-03-12 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 5c860e29 2018-03-12 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 5c860e29 2018-03-12 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 5c860e29 2018-03-12 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 5c860e29 2018-03-12 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 5c860e29 2018-03-12 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 5c860e29 2018-03-12 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 f42b1b34 2018-03-12 stsp #include <sys/queue.h>
19 c0768b0f 2018-06-10 stsp #include <sys/types.h>
20 5de5890b 2018-10-18 stsp #include <sys/stat.h>
21 3c45a30a 2019-05-12 jcs #include <sys/param.h>
22 33ad4cbe 2019-05-12 jcs #include <sys/wait.h>
24 5c860e29 2018-03-12 stsp #include <err.h>
25 5c860e29 2018-03-12 stsp #include <errno.h>
26 12ce7a6c 2019-08-12 stsp #include <limits.h>
27 5c860e29 2018-03-12 stsp #include <locale.h>
28 818c7501 2019-07-11 stsp #include <ctype.h>
29 99437157 2018-11-11 stsp #include <signal.h>
30 5c860e29 2018-03-12 stsp #include <stdio.h>
31 5c860e29 2018-03-12 stsp #include <stdlib.h>
32 5c860e29 2018-03-12 stsp #include <string.h>
33 5c860e29 2018-03-12 stsp #include <unistd.h>
34 c09a553d 2018-03-12 stsp #include <libgen.h>
35 c0768b0f 2018-06-10 stsp #include <time.h>
36 33ad4cbe 2019-05-12 jcs #include <paths.h>
38 53ccebc2 2019-07-30 stsp #include "got_version.h"
39 f42b1b34 2018-03-12 stsp #include "got_error.h"
40 f42b1b34 2018-03-12 stsp #include "got_object.h"
41 5261c201 2018-04-01 stsp #include "got_reference.h"
42 f42b1b34 2018-03-12 stsp #include "got_repository.h"
43 1dd54920 2019-05-11 stsp #include "got_path.h"
44 e6209546 2019-08-22 stsp #include "got_cancel.h"
45 c09a553d 2018-03-12 stsp #include "got_worktree.h"
46 79109fed 2018-03-27 stsp #include "got_diff.h"
47 372ccdbb 2018-06-10 stsp #include "got_commit_graph.h"
48 404c43c4 2018-06-21 stsp #include "got_blame.h"
49 63219cd2 2019-01-04 stsp #include "got_privsep.h"
50 793c30b5 2019-05-13 stsp #include "got_opentemp.h"
52 5c860e29 2018-03-12 stsp #ifndef nitems
53 5c860e29 2018-03-12 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
56 99437157 2018-11-11 stsp static volatile sig_atomic_t sigint_received;
57 99437157 2018-11-11 stsp static volatile sig_atomic_t sigpipe_received;
60 99437157 2018-11-11 stsp catch_sigint(int signo)
62 99437157 2018-11-11 stsp sigint_received = 1;
66 99437157 2018-11-11 stsp catch_sigpipe(int signo)
68 99437157 2018-11-11 stsp sigpipe_received = 1;
72 8cfb4057 2019-07-09 stsp struct got_cmd {
73 5c860e29 2018-03-12 stsp const char *cmd_name;
74 d7d4f210 2018-03-12 stsp const struct got_error *(*cmd_main)(int, char *[]);
75 1b6b95a8 2018-03-12 stsp void (*cmd_usage)(void);
76 97b3a7be 2019-07-09 stsp const char *cmd_alias;
79 ce5b7c56 2019-07-09 stsp __dead static void usage(int);
80 2c7829a4 2019-06-17 stsp __dead static void usage_init(void);
81 3ce1b845 2019-07-15 stsp __dead static void usage_import(void);
82 4ed7e80c 2018-05-20 stsp __dead static void usage_checkout(void);
83 507dc3bb 2018-12-29 stsp __dead static void usage_update(void);
84 4ed7e80c 2018-05-20 stsp __dead static void usage_log(void);
85 4ed7e80c 2018-05-20 stsp __dead static void usage_diff(void);
86 404c43c4 2018-06-21 stsp __dead static void usage_blame(void);
87 5de5890b 2018-10-18 stsp __dead static void usage_tree(void);
88 6bad629b 2019-02-04 stsp __dead static void usage_status(void);
89 d0eebce4 2019-03-11 stsp __dead static void usage_ref(void);
90 4e759de4 2019-06-26 stsp __dead static void usage_branch(void);
91 d00136be 2019-03-26 stsp __dead static void usage_add(void);
92 648e4ef7 2019-07-09 stsp __dead static void usage_remove(void);
93 a129376b 2019-03-28 stsp __dead static void usage_revert(void);
94 c4296144 2019-05-09 stsp __dead static void usage_commit(void);
95 234035bc 2019-06-01 stsp __dead static void usage_cherrypick(void);
96 5ef14e63 2019-06-02 stsp __dead static void usage_backout(void);
97 818c7501 2019-07-11 stsp __dead static void usage_rebase(void);
98 0ebf8283 2019-07-24 stsp __dead static void usage_histedit(void);
99 715dc77e 2019-08-03 stsp __dead static void usage_stage(void);
100 ad493afc 2019-08-03 stsp __dead static void usage_unstage(void);
101 01073a5d 2019-08-22 stsp __dead static void usage_cat(void);
103 2c7829a4 2019-06-17 stsp static const struct got_error* cmd_init(int, char *[]);
104 3ce1b845 2019-07-15 stsp static const struct got_error* cmd_import(int, char *[]);
105 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_checkout(int, char *[]);
106 507dc3bb 2018-12-29 stsp static const struct got_error* cmd_update(int, char *[]);
107 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_log(int, char *[]);
108 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_diff(int, char *[]);
109 404c43c4 2018-06-21 stsp static const struct got_error* cmd_blame(int, char *[]);
110 5de5890b 2018-10-18 stsp static const struct got_error* cmd_tree(int, char *[]);
111 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_status(int, char *[]);
112 d0eebce4 2019-03-11 stsp static const struct got_error* cmd_ref(int, char *[]);
113 4e759de4 2019-06-26 stsp static const struct got_error* cmd_branch(int, char *[]);
114 d00136be 2019-03-26 stsp static const struct got_error* cmd_add(int, char *[]);
115 648e4ef7 2019-07-09 stsp static const struct got_error* cmd_remove(int, char *[]);
116 a129376b 2019-03-28 stsp static const struct got_error* cmd_revert(int, char *[]);
117 c4296144 2019-05-09 stsp static const struct got_error* cmd_commit(int, char *[]);
118 234035bc 2019-06-01 stsp static const struct got_error* cmd_cherrypick(int, char *[]);
119 5ef14e63 2019-06-02 stsp static const struct got_error* cmd_backout(int, char *[]);
120 818c7501 2019-07-11 stsp static const struct got_error* cmd_rebase(int, char *[]);
121 0ebf8283 2019-07-24 stsp static const struct got_error* cmd_histedit(int, char *[]);
122 715dc77e 2019-08-03 stsp static const struct got_error* cmd_stage(int, char *[]);
123 ad493afc 2019-08-03 stsp static const struct got_error* cmd_unstage(int, char *[]);
124 01073a5d 2019-08-22 stsp static const struct got_error* cmd_cat(int, char *[]);
126 8cfb4057 2019-07-09 stsp static struct got_cmd got_commands[] = {
127 bc26cce8 2019-08-04 stsp { "init", cmd_init, usage_init, "in" },
128 bc26cce8 2019-08-04 stsp { "import", cmd_import, usage_import, "im" },
129 97b3a7be 2019-07-09 stsp { "checkout", cmd_checkout, usage_checkout, "co" },
130 97b3a7be 2019-07-09 stsp { "update", cmd_update, usage_update, "up" },
131 97b3a7be 2019-07-09 stsp { "log", cmd_log, usage_log, "" },
132 bc26cce8 2019-08-04 stsp { "diff", cmd_diff, usage_diff, "di" },
133 bc26cce8 2019-08-04 stsp { "blame", cmd_blame, usage_blame, "bl" },
134 bc26cce8 2019-08-04 stsp { "tree", cmd_tree, usage_tree, "tr" },
135 97b3a7be 2019-07-09 stsp { "status", cmd_status, usage_status, "st" },
136 97b3a7be 2019-07-09 stsp { "ref", cmd_ref, usage_ref, "" },
137 97b3a7be 2019-07-09 stsp { "branch", cmd_branch, usage_branch, "br" },
138 97b3a7be 2019-07-09 stsp { "add", cmd_add, usage_add, "" },
139 648e4ef7 2019-07-09 stsp { "remove", cmd_remove, usage_remove, "rm" },
140 97b3a7be 2019-07-09 stsp { "revert", cmd_revert, usage_revert, "rv" },
141 97b3a7be 2019-07-09 stsp { "commit", cmd_commit, usage_commit, "ci" },
142 016477fd 2019-07-09 stsp { "cherrypick", cmd_cherrypick, usage_cherrypick, "cy" },
143 97b3a7be 2019-07-09 stsp { "backout", cmd_backout, usage_backout, "bo" },
144 818c7501 2019-07-11 stsp { "rebase", cmd_rebase, usage_rebase, "rb" },
145 0ebf8283 2019-07-24 stsp { "histedit", cmd_histedit, usage_histedit, "he" },
146 715dc77e 2019-08-03 stsp { "stage", cmd_stage, usage_stage, "sg" },
147 ad493afc 2019-08-03 stsp { "unstage", cmd_unstage, usage_unstage, "ug" },
148 01073a5d 2019-08-22 stsp { "cat", cmd_cat, usage_cat, "" },
151 ce5b7c56 2019-07-09 stsp static void
152 ce5b7c56 2019-07-09 stsp list_commands(void)
156 ce5b7c56 2019-07-09 stsp fprintf(stderr, "commands:");
157 ce5b7c56 2019-07-09 stsp for (i = 0; i < nitems(got_commands); i++) {
158 ce5b7c56 2019-07-09 stsp struct got_cmd *cmd = &got_commands[i];
159 ce5b7c56 2019-07-09 stsp fprintf(stderr, " %s", cmd->cmd_name);
161 ce5b7c56 2019-07-09 stsp fputc('\n', stderr);
165 5c860e29 2018-03-12 stsp main(int argc, char *argv[])
167 8cfb4057 2019-07-09 stsp struct got_cmd *cmd;
168 5c860e29 2018-03-12 stsp unsigned int i;
170 53ccebc2 2019-07-30 stsp int hflag = 0, Vflag = 0;
172 289e3cbf 2019-02-04 stsp setlocale(LC_CTYPE, "");
174 53ccebc2 2019-07-30 stsp while ((ch = getopt(argc, argv, "hV")) != -1) {
175 5c860e29 2018-03-12 stsp switch (ch) {
183 ce5b7c56 2019-07-09 stsp usage(hflag);
184 5c860e29 2018-03-12 stsp /* NOTREACHED */
188 5c860e29 2018-03-12 stsp argc -= optind;
189 5c860e29 2018-03-12 stsp argv += optind;
190 1e70621d 2018-03-27 stsp optind = 0;
192 53ccebc2 2019-07-30 stsp if (Vflag) {
193 53ccebc2 2019-07-30 stsp got_version_print_str();
197 5c860e29 2018-03-12 stsp if (argc <= 0)
198 ce5b7c56 2019-07-09 stsp usage(hflag);
200 99437157 2018-11-11 stsp signal(SIGINT, catch_sigint);
201 99437157 2018-11-11 stsp signal(SIGPIPE, catch_sigpipe);
203 5c860e29 2018-03-12 stsp for (i = 0; i < nitems(got_commands); i++) {
204 d7d4f210 2018-03-12 stsp const struct got_error *error;
206 5c860e29 2018-03-12 stsp cmd = &got_commands[i];
208 97b3a7be 2019-07-09 stsp if (strcmp(cmd->cmd_name, argv[0]) != 0 &&
209 97b3a7be 2019-07-09 stsp strcmp(cmd->cmd_alias, argv[0]) != 0)
213 1b6b95a8 2018-03-12 stsp got_commands[i].cmd_usage();
215 d7d4f210 2018-03-12 stsp error = got_commands[i].cmd_main(argc, argv);
216 80d5f134 2018-11-11 stsp if (error && !(sigint_received || sigpipe_received)) {
217 d7d4f210 2018-03-12 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
224 20ecf764 2018-03-12 stsp fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
225 ce5b7c56 2019-07-09 stsp list_commands();
229 4ed7e80c 2018-05-20 stsp __dead static void
230 ce5b7c56 2019-07-09 stsp usage(int hflag)
232 53ccebc2 2019-07-30 stsp fprintf(stderr, "usage: %s [-h] [-V] command [arg ...]\n",
233 53ccebc2 2019-07-30 stsp getprogname());
235 ce5b7c56 2019-07-09 stsp list_commands();
239 0266afb7 2019-01-04 stsp static const struct got_error *
240 0ee7065d 2019-05-13 stsp get_editor(char **abspath)
242 0ee7065d 2019-05-13 stsp const struct got_error *err = NULL;
243 e2ba3d07 2019-05-13 stsp const char *editor;
245 8920fa04 2019-08-18 stsp *abspath = NULL;
247 e2ba3d07 2019-05-13 stsp editor = getenv("VISUAL");
248 e2ba3d07 2019-05-13 stsp if (editor == NULL)
249 e2ba3d07 2019-05-13 stsp editor = getenv("EDITOR");
251 0ee7065d 2019-05-13 stsp if (editor) {
252 0ee7065d 2019-05-13 stsp err = got_path_find_prog(abspath, editor);
254 0ee7065d 2019-05-13 stsp return err;
257 0ee7065d 2019-05-13 stsp if (*abspath == NULL) {
258 0ee7065d 2019-05-13 stsp *abspath = strdup("/bin/ed");
259 0ee7065d 2019-05-13 stsp if (*abspath == NULL)
260 0ee7065d 2019-05-13 stsp return got_error_from_errno("strdup");
263 e2ba3d07 2019-05-13 stsp return NULL;
266 e2ba3d07 2019-05-13 stsp static const struct got_error *
267 d0eebce4 2019-03-11 stsp apply_unveil(const char *repo_path, int repo_read_only,
268 c530dc23 2019-07-23 stsp const char *worktree_path)
270 163ce85a 2019-05-13 stsp const struct got_error *err;
272 37c06ea4 2019-07-15 stsp #ifdef PROFILE
273 37c06ea4 2019-07-15 stsp if (unveil("gmon.out", "rwc") != 0)
274 37c06ea4 2019-07-15 stsp return got_error_from_errno2("unveil", "gmon.out");
276 d0eebce4 2019-03-11 stsp if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
277 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", repo_path);
279 0266afb7 2019-01-04 stsp if (worktree_path && unveil(worktree_path, "rwc") != 0)
280 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", worktree_path);
282 f12d0dbe 2019-01-04 stsp if (unveil("/tmp", "rwc") != 0)
283 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", "/tmp");
285 163ce85a 2019-05-13 stsp err = got_privsep_unveil_exec_helpers();
286 163ce85a 2019-05-13 stsp if (err != NULL)
287 163ce85a 2019-05-13 stsp return err;
289 0266afb7 2019-01-04 stsp if (unveil(NULL, NULL) != 0)
290 638f9024 2019-05-13 stsp return got_error_from_errno("unveil");
292 0266afb7 2019-01-04 stsp return NULL;
295 2c7829a4 2019-06-17 stsp __dead static void
296 2c7829a4 2019-06-17 stsp usage_init(void)
298 09ea71ba 2019-07-27 stsp fprintf(stderr, "usage: %s init repository-path\n", getprogname());
302 2c7829a4 2019-06-17 stsp static const struct got_error *
303 2c7829a4 2019-06-17 stsp cmd_init(int argc, char *argv[])
305 2c7829a4 2019-06-17 stsp const struct got_error *error = NULL;
306 2c7829a4 2019-06-17 stsp char *repo_path = NULL;
309 2c7829a4 2019-06-17 stsp while ((ch = getopt(argc, argv, "")) != -1) {
310 2c7829a4 2019-06-17 stsp switch (ch) {
312 2c7829a4 2019-06-17 stsp usage_init();
313 2c7829a4 2019-06-17 stsp /* NOTREACHED */
317 2c7829a4 2019-06-17 stsp argc -= optind;
318 2c7829a4 2019-06-17 stsp argv += optind;
320 2c7829a4 2019-06-17 stsp #ifndef PROFILE
321 2c7829a4 2019-06-17 stsp if (pledge("stdio rpath wpath cpath unveil", NULL) == -1)
322 2c7829a4 2019-06-17 stsp err(1, "pledge");
324 2c7829a4 2019-06-17 stsp if (argc != 1)
325 bc20e173 2019-06-17 stsp usage_init();
327 2c7829a4 2019-06-17 stsp repo_path = strdup(argv[0]);
328 2c7829a4 2019-06-17 stsp if (repo_path == NULL)
329 2c7829a4 2019-06-17 stsp return got_error_from_errno("strdup");
331 2c7829a4 2019-06-17 stsp got_path_strip_trailing_slashes(repo_path);
333 2c7829a4 2019-06-17 stsp error = got_path_mkdir(repo_path);
334 2c7829a4 2019-06-17 stsp if (error &&
335 2c7829a4 2019-06-17 stsp !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
338 c530dc23 2019-07-23 stsp error = apply_unveil(repo_path, 0, NULL);
342 2c7829a4 2019-06-17 stsp error = got_repo_init(repo_path);
343 2c7829a4 2019-06-17 stsp if (error != NULL)
347 3ce1b845 2019-07-15 stsp free(repo_path);
348 3ce1b845 2019-07-15 stsp return error;
351 3ce1b845 2019-07-15 stsp __dead static void
352 3ce1b845 2019-07-15 stsp usage_import(void)
354 3ce1b845 2019-07-15 stsp fprintf(stderr, "usage: %s import [-b branch] [-m message] "
355 3ce1b845 2019-07-15 stsp "[-r repository-path] [-I pattern] path\n", getprogname());
360 3ce1b845 2019-07-15 stsp spawn_editor(const char *editor, const char *file)
363 3ce1b845 2019-07-15 stsp sig_t sighup, sigint, sigquit;
364 3ce1b845 2019-07-15 stsp int st = -1;
366 3ce1b845 2019-07-15 stsp sighup = signal(SIGHUP, SIG_IGN);
367 3ce1b845 2019-07-15 stsp sigint = signal(SIGINT, SIG_IGN);
368 3ce1b845 2019-07-15 stsp sigquit = signal(SIGQUIT, SIG_IGN);
370 3ce1b845 2019-07-15 stsp switch (pid = fork()) {
372 3ce1b845 2019-07-15 stsp goto doneediting;
374 3ce1b845 2019-07-15 stsp execl(editor, editor, file, (char *)NULL);
375 3ce1b845 2019-07-15 stsp _exit(127);
378 3ce1b845 2019-07-15 stsp while (waitpid(pid, &st, 0) == -1)
379 3ce1b845 2019-07-15 stsp if (errno != EINTR)
382 3ce1b845 2019-07-15 stsp doneediting:
383 3ce1b845 2019-07-15 stsp (void)signal(SIGHUP, sighup);
384 3ce1b845 2019-07-15 stsp (void)signal(SIGINT, sigint);
385 3ce1b845 2019-07-15 stsp (void)signal(SIGQUIT, sigquit);
387 3ce1b845 2019-07-15 stsp if (!WIFEXITED(st)) {
388 3ce1b845 2019-07-15 stsp errno = EINTR;
392 3ce1b845 2019-07-15 stsp return WEXITSTATUS(st);
395 3ce1b845 2019-07-15 stsp static const struct got_error *
396 3ce1b845 2019-07-15 stsp edit_logmsg(char **logmsg, const char *editor, const char *logmsg_path,
397 3ce1b845 2019-07-15 stsp const char *initial_content)
399 3ce1b845 2019-07-15 stsp const struct got_error *err = NULL;
400 3ce1b845 2019-07-15 stsp char buf[1024];
401 3ce1b845 2019-07-15 stsp struct stat st, st2;
403 3ce1b845 2019-07-15 stsp int content_changed = 0;
404 3ce1b845 2019-07-15 stsp size_t len;
406 3ce1b845 2019-07-15 stsp *logmsg = NULL;
408 3ce1b845 2019-07-15 stsp if (stat(logmsg_path, &st) == -1)
409 3ce1b845 2019-07-15 stsp return got_error_from_errno2("stat", logmsg_path);
411 3ce1b845 2019-07-15 stsp if (spawn_editor(editor, logmsg_path) == -1)
412 3ce1b845 2019-07-15 stsp return got_error_from_errno("failed spawning editor");
414 3ce1b845 2019-07-15 stsp if (stat(logmsg_path, &st2) == -1)
415 3ce1b845 2019-07-15 stsp return got_error_from_errno("stat");
417 3ce1b845 2019-07-15 stsp if (st.st_mtime == st2.st_mtime && st.st_size == st2.st_size)
418 3ce1b845 2019-07-15 stsp return got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
419 3ce1b845 2019-07-15 stsp "no changes made to commit message, aborting");
421 3ce1b845 2019-07-15 stsp *logmsg = malloc(st2.st_size + 1);
422 3ce1b845 2019-07-15 stsp if (*logmsg == NULL)
423 3ce1b845 2019-07-15 stsp return got_error_from_errno("malloc");
424 3ce1b845 2019-07-15 stsp (*logmsg)[0] = '\0';
427 3ce1b845 2019-07-15 stsp fp = fopen(logmsg_path, "r");
428 3ce1b845 2019-07-15 stsp if (fp == NULL) {
429 3ce1b845 2019-07-15 stsp err = got_error_from_errno("fopen");
432 3ce1b845 2019-07-15 stsp while (fgets(buf, sizeof(buf), fp) != NULL) {
433 3ce1b845 2019-07-15 stsp if (!content_changed && strcmp(buf, initial_content) != 0)
434 3ce1b845 2019-07-15 stsp content_changed = 1;
435 3ce1b845 2019-07-15 stsp if (buf[0] == '#' || (len == 0 && buf[0] == '\n'))
436 3ce1b845 2019-07-15 stsp continue; /* remove comments and leading empty lines */
437 3ce1b845 2019-07-15 stsp len = strlcat(*logmsg, buf, st2.st_size);
439 3ce1b845 2019-07-15 stsp fclose(fp);
441 3ce1b845 2019-07-15 stsp while (len > 0 && (*logmsg)[len - 1] == '\n') {
442 3ce1b845 2019-07-15 stsp (*logmsg)[len - 1] = '\0';
446 3ce1b845 2019-07-15 stsp if (len == 0 || !content_changed)
447 3ce1b845 2019-07-15 stsp err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
448 3ce1b845 2019-07-15 stsp "commit message cannot be empty, aborting");
451 3ce1b845 2019-07-15 stsp free(*logmsg);
452 3ce1b845 2019-07-15 stsp *logmsg = NULL;
454 3ce1b845 2019-07-15 stsp return err;
457 3ce1b845 2019-07-15 stsp static const struct got_error *
458 3ce1b845 2019-07-15 stsp collect_import_msg(char **logmsg, const char *editor, const char *path_dir,
459 3ce1b845 2019-07-15 stsp const char *branch_name)
461 3ce1b845 2019-07-15 stsp char *initial_content = NULL, *logmsg_path = NULL;
462 3ce1b845 2019-07-15 stsp const struct got_error *err = NULL;
465 3ce1b845 2019-07-15 stsp if (asprintf(&initial_content,
466 3ce1b845 2019-07-15 stsp "\n# %s to be imported to branch %s\n", path_dir,
467 3ce1b845 2019-07-15 stsp branch_name) == -1)
468 3ce1b845 2019-07-15 stsp return got_error_from_errno("asprintf");
470 3ce1b845 2019-07-15 stsp err = got_opentemp_named_fd(&logmsg_path, &fd, "/tmp/got-importmsg");
474 3ce1b845 2019-07-15 stsp dprintf(fd, initial_content);
477 3ce1b845 2019-07-15 stsp err = edit_logmsg(logmsg, editor, logmsg_path, initial_content);
479 3ce1b845 2019-07-15 stsp free(initial_content);
480 3ce1b845 2019-07-15 stsp free(logmsg_path);
481 3ce1b845 2019-07-15 stsp return err;
484 3ce1b845 2019-07-15 stsp static const struct got_error *
485 3ce1b845 2019-07-15 stsp import_progress(void *arg, const char *path)
487 3ce1b845 2019-07-15 stsp printf("A %s\n", path);
488 3ce1b845 2019-07-15 stsp return NULL;
491 3ce1b845 2019-07-15 stsp static const struct got_error *
492 84792843 2019-08-09 stsp get_author(const char **author)
494 84792843 2019-08-09 stsp const char *got_author;
496 84792843 2019-08-09 stsp *author = NULL;
498 84792843 2019-08-09 stsp got_author = getenv("GOT_AUTHOR");
499 84792843 2019-08-09 stsp if (got_author == NULL) {
500 84792843 2019-08-09 stsp /* TODO: Look up user in password database? */
501 84792843 2019-08-09 stsp return got_error(GOT_ERR_COMMIT_NO_AUTHOR);
504 84792843 2019-08-09 stsp *author = got_author;
507 84792843 2019-08-09 stsp * Really dumb email address check; we're only doing this to
508 84792843 2019-08-09 stsp * avoid git's object parser breaking on commits we create.
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);
518 84792843 2019-08-09 stsp while (*got_author && *got_author != '>')
519 84792843 2019-08-09 stsp got_author++;
520 84792843 2019-08-09 stsp if (*got_author != '>')
521 84792843 2019-08-09 stsp return got_error(GOT_ERR_COMMIT_NO_EMAIL);
523 84792843 2019-08-09 stsp return NULL;
526 84792843 2019-08-09 stsp static const struct got_error *
527 3ce1b845 2019-07-15 stsp cmd_import(int argc, char *argv[])
529 3ce1b845 2019-07-15 stsp const struct got_error *error = NULL;
530 3ce1b845 2019-07-15 stsp char *path_dir = NULL, *repo_path = NULL, *logmsg = NULL;
531 3ce1b845 2019-07-15 stsp char *editor = NULL;
532 84792843 2019-08-09 stsp const char *author;
533 3ce1b845 2019-07-15 stsp const char *branch_name = "master";
534 3ce1b845 2019-07-15 stsp char *refname = NULL, *id_str = NULL;
535 3ce1b845 2019-07-15 stsp struct got_repository *repo = NULL;
536 3ce1b845 2019-07-15 stsp struct got_reference *branch_ref = NULL, *head_ref = NULL;
537 3ce1b845 2019-07-15 stsp struct got_object_id *new_commit_id = NULL;
539 3ce1b845 2019-07-15 stsp struct got_pathlist_head ignores;
540 3ce1b845 2019-07-15 stsp struct got_pathlist_entry *pe;
542 3ce1b845 2019-07-15 stsp TAILQ_INIT(&ignores);
544 3ce1b845 2019-07-15 stsp while ((ch = getopt(argc, argv, "b:m:r:I:")) != -1) {
545 3ce1b845 2019-07-15 stsp switch (ch) {
547 3ce1b845 2019-07-15 stsp branch_name = optarg;
550 3ce1b845 2019-07-15 stsp logmsg = strdup(optarg);
551 3ce1b845 2019-07-15 stsp if (logmsg == NULL) {
552 3ce1b845 2019-07-15 stsp error = got_error_from_errno("strdup");
557 3ce1b845 2019-07-15 stsp repo_path = realpath(optarg, NULL);
558 3ce1b845 2019-07-15 stsp if (repo_path == NULL) {
559 3ce1b845 2019-07-15 stsp error = got_error_from_errno("realpath");
564 3ce1b845 2019-07-15 stsp if (optarg[0] == '\0')
566 3ce1b845 2019-07-15 stsp error = got_pathlist_insert(&pe, &ignores, optarg,
572 3ce1b845 2019-07-15 stsp usage_init();
573 3ce1b845 2019-07-15 stsp /* NOTREACHED */
577 3ce1b845 2019-07-15 stsp argc -= optind;
578 3ce1b845 2019-07-15 stsp argv += optind;
580 3ce1b845 2019-07-15 stsp #ifndef PROFILE
581 3ce1b845 2019-07-15 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec unveil",
582 3ce1b845 2019-07-15 stsp NULL) == -1)
583 3ce1b845 2019-07-15 stsp err(1, "pledge");
585 3ce1b845 2019-07-15 stsp if (argc != 1)
586 3ce1b845 2019-07-15 stsp usage_import();
588 84792843 2019-08-09 stsp error = get_author(&author);
590 84792843 2019-08-09 stsp return error;
592 3ce1b845 2019-07-15 stsp if (repo_path == NULL) {
593 3ce1b845 2019-07-15 stsp repo_path = getcwd(NULL, 0);
594 3ce1b845 2019-07-15 stsp if (repo_path == NULL)
595 3ce1b845 2019-07-15 stsp return got_error_from_errno("getcwd");
597 3ce1b845 2019-07-15 stsp got_path_strip_trailing_slashes(repo_path);
598 3ce1b845 2019-07-15 stsp error = got_repo_open(&repo, repo_path);
602 3ce1b845 2019-07-15 stsp if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
603 3ce1b845 2019-07-15 stsp error = got_error_from_errno("asprintf");
607 3ce1b845 2019-07-15 stsp error = got_ref_open(&branch_ref, repo, refname, 0);
608 3ce1b845 2019-07-15 stsp if (error) {
609 3ce1b845 2019-07-15 stsp if (error->code != GOT_ERR_NOT_REF)
612 3ce1b845 2019-07-15 stsp error = got_error_msg(GOT_ERR_BRANCH_EXISTS,
613 3ce1b845 2019-07-15 stsp "import target branch already exists");
617 3ce1b845 2019-07-15 stsp path_dir = realpath(argv[0], NULL);
618 3ce1b845 2019-07-15 stsp if (path_dir == NULL) {
619 3ce1b845 2019-07-15 stsp error = got_error_from_errno("realpath");
622 3ce1b845 2019-07-15 stsp got_path_strip_trailing_slashes(path_dir);
625 3ce1b845 2019-07-15 stsp * unveil(2) traverses exec(2); if an editor is used we have
626 3ce1b845 2019-07-15 stsp * to apply unveil after the log message has been written.
628 3ce1b845 2019-07-15 stsp if (logmsg == NULL || strlen(logmsg) == 0) {
629 3ce1b845 2019-07-15 stsp error = get_editor(&editor);
632 3ce1b845 2019-07-15 stsp error = collect_import_msg(&logmsg, editor, path_dir, refname);
637 3ce1b845 2019-07-15 stsp if (unveil(path_dir, "r") != 0)
638 3ce1b845 2019-07-15 stsp return got_error_from_errno2("unveil", path_dir);
640 c530dc23 2019-07-23 stsp error = apply_unveil(got_repo_get_path(repo), 0, NULL);
644 3ce1b845 2019-07-15 stsp error = got_repo_import(&new_commit_id, path_dir, logmsg,
645 84792843 2019-08-09 stsp author, &ignores, repo, import_progress, NULL);
649 3ce1b845 2019-07-15 stsp error = got_ref_alloc(&branch_ref, refname, new_commit_id);
653 3ce1b845 2019-07-15 stsp error = got_ref_write(branch_ref, repo);
657 3ce1b845 2019-07-15 stsp error = got_object_id_str(&id_str, new_commit_id);
661 3ce1b845 2019-07-15 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
662 3ce1b845 2019-07-15 stsp if (error) {
663 3ce1b845 2019-07-15 stsp if (error->code != GOT_ERR_NOT_REF)
666 3ce1b845 2019-07-15 stsp error = got_ref_alloc_symref(&head_ref, GOT_REF_HEAD,
667 3ce1b845 2019-07-15 stsp branch_ref);
671 3ce1b845 2019-07-15 stsp error = got_ref_write(head_ref, repo);
676 3ce1b845 2019-07-15 stsp printf("Created branch %s with commit %s\n",
677 3ce1b845 2019-07-15 stsp got_ref_get_name(branch_ref), id_str);
679 2c7829a4 2019-06-17 stsp free(repo_path);
680 3ce1b845 2019-07-15 stsp free(editor);
681 3ce1b845 2019-07-15 stsp free(refname);
682 3ce1b845 2019-07-15 stsp free(new_commit_id);
683 3ce1b845 2019-07-15 stsp free(id_str);
684 3ce1b845 2019-07-15 stsp if (branch_ref)
685 3ce1b845 2019-07-15 stsp got_ref_close(branch_ref);
686 3ce1b845 2019-07-15 stsp if (head_ref)
687 3ce1b845 2019-07-15 stsp got_ref_close(head_ref);
688 2c7829a4 2019-06-17 stsp return error;
691 4ed7e80c 2018-05-20 stsp __dead static void
692 c09a553d 2018-03-12 stsp usage_checkout(void)
694 08573d5b 2019-05-14 stsp fprintf(stderr, "usage: %s checkout [-b branch] [-c commit] "
695 08573d5b 2019-05-14 stsp "[-p prefix] repository-path [worktree-path]\n", getprogname());
699 1ee397ad 2019-07-12 stsp static const struct got_error *
700 a0eb853d 2018-12-29 stsp checkout_progress(void *arg, unsigned char status, const char *path)
702 92a684f4 2018-03-12 stsp char *worktree_path = arg;
704 a484d721 2019-06-10 stsp /* Base commit bump happens silently. */
705 a484d721 2019-06-10 stsp if (status == GOT_STATUS_BUMP_BASE)
706 1ee397ad 2019-07-12 stsp return NULL;
708 92a684f4 2018-03-12 stsp while (path[0] == '/')
711 d7b62c98 2018-12-27 stsp printf("%c %s/%s\n", status, worktree_path, path);
712 1ee397ad 2019-07-12 stsp return NULL;
715 99437157 2018-11-11 stsp static const struct got_error *
716 6bad629b 2019-02-04 stsp check_cancelled(void *arg)
718 99437157 2018-11-11 stsp if (sigint_received || sigpipe_received)
719 99437157 2018-11-11 stsp return got_error(GOT_ERR_CANCELLED);
720 99437157 2018-11-11 stsp return NULL;
723 8069f636 2019-01-12 stsp static const struct got_error *
724 024e9686 2019-05-14 stsp check_linear_ancestry(struct got_object_id *commit_id,
725 024e9686 2019-05-14 stsp struct got_object_id *base_commit_id, struct got_repository *repo)
727 d5bea539 2019-05-13 stsp const struct got_error *err = NULL;
728 024e9686 2019-05-14 stsp struct got_object_id *yca_id;
730 d5bea539 2019-05-13 stsp err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
731 6fb7cd11 2019-08-22 stsp commit_id, base_commit_id, repo, check_cancelled, NULL);
733 36a38700 2019-05-10 stsp return err;
735 d5bea539 2019-05-13 stsp if (yca_id == NULL)
736 d5bea539 2019-05-13 stsp return got_error(GOT_ERR_ANCESTRY);
739 d5bea539 2019-05-13 stsp * Require a straight line of history between the target commit
740 d5bea539 2019-05-13 stsp * and the work tree's base commit.
742 d5751d49 2019-05-14 stsp * Non-linear situations such as this require a rebase:
744 d5bea539 2019-05-13 stsp * (commit) D F (base_commit)
752 d5bea539 2019-05-13 stsp * 'got update' only handles linear cases:
753 d5bea539 2019-05-13 stsp * Update forwards in time: A (base/yca) - B - C - D (commit)
754 efa2b6f7 2019-05-14 stsp * Update backwards in time: D (base) - C - B - A (commit/yca)
756 d5bea539 2019-05-13 stsp if (got_object_id_cmp(commit_id, yca_id) != 0 &&
757 d5bea539 2019-05-13 stsp got_object_id_cmp(base_commit_id, yca_id) != 0)
758 d5bea539 2019-05-13 stsp return got_error(GOT_ERR_ANCESTRY);
760 d5bea539 2019-05-13 stsp free(yca_id);
761 d5bea539 2019-05-13 stsp return NULL;
764 a367ff0f 2019-05-14 stsp static const struct got_error *
765 a367ff0f 2019-05-14 stsp check_same_branch(struct got_object_id *commit_id,
766 a51a74b3 2019-07-27 stsp struct got_reference *head_ref, struct got_object_id *yca_id,
767 a51a74b3 2019-07-27 stsp struct got_repository *repo)
769 a367ff0f 2019-05-14 stsp const struct got_error *err = NULL;
770 a367ff0f 2019-05-14 stsp struct got_commit_graph *graph = NULL;
771 a367ff0f 2019-05-14 stsp struct got_object_id *head_commit_id = NULL;
772 a367ff0f 2019-05-14 stsp int is_same_branch = 0;
774 a367ff0f 2019-05-14 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
778 a51a74b3 2019-07-27 stsp if (got_object_id_cmp(head_commit_id, commit_id) == 0) {
779 a51a74b3 2019-07-27 stsp is_same_branch = 1;
782 a51a74b3 2019-07-27 stsp if (yca_id && got_object_id_cmp(commit_id, yca_id) == 0) {
783 a51a74b3 2019-07-27 stsp is_same_branch = 1;
787 a367ff0f 2019-05-14 stsp err = got_commit_graph_open(&graph, head_commit_id, "/", 1, repo);
791 6fb7cd11 2019-08-22 stsp err = got_commit_graph_iter_start(graph, head_commit_id, repo,
792 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
797 a367ff0f 2019-05-14 stsp struct got_object_id *id;
798 a367ff0f 2019-05-14 stsp err = got_commit_graph_iter_next(&id, graph);
800 a367ff0f 2019-05-14 stsp if (err->code == GOT_ERR_ITER_COMPLETED) {
801 a367ff0f 2019-05-14 stsp err = NULL;
803 a14c42fa 2019-07-15 stsp } else if (err->code != GOT_ERR_ITER_NEED_MORE)
805 a367ff0f 2019-05-14 stsp err = got_commit_graph_fetch_commits(graph, 1,
806 6fb7cd11 2019-08-22 stsp repo, check_cancelled, NULL);
812 a51a74b3 2019-07-27 stsp if (yca_id && got_object_id_cmp(id, yca_id) == 0)
814 a367ff0f 2019-05-14 stsp if (got_object_id_cmp(id, commit_id) == 0) {
815 a367ff0f 2019-05-14 stsp is_same_branch = 1;
822 a367ff0f 2019-05-14 stsp got_commit_graph_close(graph);
823 a367ff0f 2019-05-14 stsp free(head_commit_id);
824 a367ff0f 2019-05-14 stsp if (!err && !is_same_branch)
825 a367ff0f 2019-05-14 stsp err = got_error(GOT_ERR_ANCESTRY);
826 04f57cb3 2019-07-25 stsp return err;
829 04f57cb3 2019-07-25 stsp static const struct got_error *
830 04f57cb3 2019-07-25 stsp resolve_commit_arg(struct got_object_id **commit_id,
831 04f57cb3 2019-07-25 stsp const char *commit_id_arg, struct got_repository *repo)
833 04f57cb3 2019-07-25 stsp const struct got_error *err;
834 04f57cb3 2019-07-25 stsp struct got_reference *ref;
835 303e2782 2019-08-09 stsp struct got_tag_object *tag;
837 303e2782 2019-08-09 stsp err = got_repo_object_match_tag(&tag, commit_id_arg,
838 303e2782 2019-08-09 stsp GOT_OBJ_TYPE_COMMIT, repo);
839 303e2782 2019-08-09 stsp if (err == NULL) {
840 303e2782 2019-08-09 stsp *commit_id = got_object_id_dup(
841 303e2782 2019-08-09 stsp got_object_tag_get_object_id(tag));
842 303e2782 2019-08-09 stsp if (*commit_id == NULL)
843 303e2782 2019-08-09 stsp err = got_error_from_errno("got_object_id_dup");
844 303e2782 2019-08-09 stsp got_object_tag_close(tag);
845 303e2782 2019-08-09 stsp return err;
846 303e2782 2019-08-09 stsp } else if (err->code != GOT_ERR_NO_OBJ)
847 303e2782 2019-08-09 stsp return err;
849 04f57cb3 2019-07-25 stsp err = got_ref_open(&ref, repo, commit_id_arg, 0);
850 04f57cb3 2019-07-25 stsp if (err == NULL) {
851 04f57cb3 2019-07-25 stsp err = got_ref_resolve(commit_id, repo, ref);
852 04f57cb3 2019-07-25 stsp got_ref_close(ref);
854 04f57cb3 2019-07-25 stsp if (err->code != GOT_ERR_NOT_REF)
855 04f57cb3 2019-07-25 stsp return err;
856 04f57cb3 2019-07-25 stsp err = got_repo_match_object_id_prefix(commit_id,
857 04f57cb3 2019-07-25 stsp commit_id_arg, GOT_OBJ_TYPE_COMMIT, repo);
859 a367ff0f 2019-05-14 stsp return err;
862 4ed7e80c 2018-05-20 stsp static const struct got_error *
863 c09a553d 2018-03-12 stsp cmd_checkout(int argc, char *argv[])
865 c09a553d 2018-03-12 stsp const struct got_error *error = NULL;
866 c09a553d 2018-03-12 stsp struct got_repository *repo = NULL;
867 c09a553d 2018-03-12 stsp struct got_reference *head_ref = NULL;
868 c09a553d 2018-03-12 stsp struct got_worktree *worktree = NULL;
869 c09a553d 2018-03-12 stsp char *repo_path = NULL;
870 c09a553d 2018-03-12 stsp char *worktree_path = NULL;
871 0bb8a95e 2018-03-12 stsp const char *path_prefix = "";
872 08573d5b 2019-05-14 stsp const char *branch_name = GOT_REF_HEAD;
873 8069f636 2019-01-12 stsp char *commit_id_str = NULL;
874 72151b04 2019-05-11 stsp int ch, same_path_prefix;
875 f2ea84fa 2019-07-27 stsp struct got_pathlist_head paths;
877 f2ea84fa 2019-07-27 stsp TAILQ_INIT(&paths);
879 08573d5b 2019-05-14 stsp while ((ch = getopt(argc, argv, "b:c:p:")) != -1) {
880 0bb8a95e 2018-03-12 stsp switch (ch) {
882 08573d5b 2019-05-14 stsp branch_name = optarg;
885 8069f636 2019-01-12 stsp commit_id_str = strdup(optarg);
886 8069f636 2019-01-12 stsp if (commit_id_str == NULL)
887 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
890 0bb8a95e 2018-03-12 stsp path_prefix = optarg;
893 2deda0b9 2019-03-07 stsp usage_checkout();
894 0bb8a95e 2018-03-12 stsp /* NOTREACHED */
898 0bb8a95e 2018-03-12 stsp argc -= optind;
899 0bb8a95e 2018-03-12 stsp argv += optind;
901 6715a751 2018-03-16 stsp #ifndef PROFILE
902 68ed9ba5 2019-02-10 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
903 68ed9ba5 2019-02-10 stsp "unveil", NULL) == -1)
904 c09a553d 2018-03-12 stsp err(1, "pledge");
906 0bb8a95e 2018-03-12 stsp if (argc == 1) {
907 c09a553d 2018-03-12 stsp char *cwd, *base, *dotgit;
908 76089277 2018-04-01 stsp repo_path = realpath(argv[0], NULL);
909 76089277 2018-04-01 stsp if (repo_path == NULL)
910 638f9024 2019-05-13 stsp return got_error_from_errno2("realpath", argv[0]);
911 c09a553d 2018-03-12 stsp cwd = getcwd(NULL, 0);
912 76089277 2018-04-01 stsp if (cwd == NULL) {
913 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
916 230a42bd 2019-05-11 jcs if (path_prefix[0]) {
917 230a42bd 2019-05-11 jcs base = basename(path_prefix);
918 230a42bd 2019-05-11 jcs if (base == NULL) {
919 638f9024 2019-05-13 stsp error = got_error_from_errno2("basename",
920 230a42bd 2019-05-11 jcs path_prefix);
924 5d7c1dab 2018-04-01 stsp base = basename(repo_path);
925 230a42bd 2019-05-11 jcs if (base == NULL) {
926 638f9024 2019-05-13 stsp error = got_error_from_errno2("basename",
931 c09a553d 2018-03-12 stsp dotgit = strstr(base, ".git");
932 c09a553d 2018-03-12 stsp if (dotgit)
933 c09a553d 2018-03-12 stsp *dotgit = '\0';
934 c09a553d 2018-03-12 stsp if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
935 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
940 0bb8a95e 2018-03-12 stsp } else if (argc == 2) {
941 76089277 2018-04-01 stsp repo_path = realpath(argv[0], NULL);
942 76089277 2018-04-01 stsp if (repo_path == NULL) {
943 638f9024 2019-05-13 stsp error = got_error_from_errno2("realpath", argv[0]);
946 f7b38925 2018-04-01 stsp worktree_path = realpath(argv[1], NULL);
947 76089277 2018-04-01 stsp if (worktree_path == NULL) {
948 b4b3a7dd 2019-07-22 stsp if (errno != ENOENT) {
949 b4b3a7dd 2019-07-22 stsp error = got_error_from_errno2("realpath",
953 b4b3a7dd 2019-07-22 stsp worktree_path = strdup(argv[1]);
954 b4b3a7dd 2019-07-22 stsp if (worktree_path == NULL) {
955 b4b3a7dd 2019-07-22 stsp error = got_error_from_errno("strdup");
960 c09a553d 2018-03-12 stsp usage_checkout();
962 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
963 72151b04 2019-05-11 stsp got_path_strip_trailing_slashes(worktree_path);
965 c09a553d 2018-03-12 stsp error = got_repo_open(&repo, repo_path);
966 c09a553d 2018-03-12 stsp if (error != NULL)
969 c530dc23 2019-07-23 stsp /* Pre-create work tree path for unveil(2) */
970 c530dc23 2019-07-23 stsp error = got_path_mkdir(worktree_path);
971 c530dc23 2019-07-23 stsp if (error) {
972 80c1b583 2019-08-07 stsp if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
973 80c1b583 2019-08-07 stsp !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
975 c530dc23 2019-07-23 stsp if (!got_path_dir_is_empty(worktree_path)) {
976 c530dc23 2019-07-23 stsp error = got_error_path(worktree_path,
977 c530dc23 2019-07-23 stsp GOT_ERR_DIR_NOT_EMPTY);
982 c530dc23 2019-07-23 stsp error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
986 08573d5b 2019-05-14 stsp error = got_ref_open(&head_ref, repo, branch_name, 0);
987 c09a553d 2018-03-12 stsp if (error != NULL)
990 0bb8a95e 2018-03-12 stsp error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
991 d70b8e30 2018-12-27 stsp if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
994 c09a553d 2018-03-12 stsp error = got_worktree_open(&worktree, worktree_path);
995 c09a553d 2018-03-12 stsp if (error != NULL)
998 e5dc7198 2018-12-29 stsp error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
999 e5dc7198 2018-12-29 stsp path_prefix);
1000 e5dc7198 2018-12-29 stsp if (error != NULL)
1001 e5dc7198 2018-12-29 stsp goto done;
1002 e5dc7198 2018-12-29 stsp if (!same_path_prefix) {
1003 49520a32 2018-12-29 stsp error = got_error(GOT_ERR_PATH_PREFIX);
1004 49520a32 2018-12-29 stsp goto done;
1007 8069f636 2019-01-12 stsp if (commit_id_str) {
1008 04f57cb3 2019-07-25 stsp struct got_object_id *commit_id;
1009 04f57cb3 2019-07-25 stsp error = resolve_commit_arg(&commit_id, commit_id_str, repo);
1010 30837e32 2019-07-25 stsp if (error)
1011 8069f636 2019-01-12 stsp goto done;
1012 024e9686 2019-05-14 stsp error = check_linear_ancestry(commit_id,
1013 024e9686 2019-05-14 stsp got_worktree_get_base_commit_id(worktree), repo);
1014 8069f636 2019-01-12 stsp if (error != NULL) {
1015 8069f636 2019-01-12 stsp free(commit_id);
1016 8069f636 2019-01-12 stsp goto done;
1018 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
1019 45d344f6 2019-05-14 stsp if (error)
1020 45d344f6 2019-05-14 stsp goto done;
1021 8069f636 2019-01-12 stsp error = got_worktree_set_base_commit_id(worktree, repo,
1022 8069f636 2019-01-12 stsp commit_id);
1023 8069f636 2019-01-12 stsp free(commit_id);
1024 8069f636 2019-01-12 stsp if (error)
1025 8069f636 2019-01-12 stsp goto done;
1028 adc19d55 2019-07-28 stsp error = got_pathlist_append(&paths, "", NULL);
1029 f2ea84fa 2019-07-27 stsp if (error)
1030 f2ea84fa 2019-07-27 stsp goto done;
1031 f2ea84fa 2019-07-27 stsp error = got_worktree_checkout_files(worktree, &paths, repo,
1032 6bad629b 2019-02-04 stsp checkout_progress, worktree_path, check_cancelled, NULL);
1033 c09a553d 2018-03-12 stsp if (error != NULL)
1034 c09a553d 2018-03-12 stsp goto done;
1036 b65ae19a 2018-04-24 stsp printf("Now shut up and hack\n");
1039 f2ea84fa 2019-07-27 stsp got_pathlist_free(&paths);
1040 8069f636 2019-01-12 stsp free(commit_id_str);
1041 76089277 2018-04-01 stsp free(repo_path);
1042 507dc3bb 2018-12-29 stsp free(worktree_path);
1043 507dc3bb 2018-12-29 stsp return error;
1046 507dc3bb 2018-12-29 stsp __dead static void
1047 507dc3bb 2018-12-29 stsp usage_update(void)
1049 f2ea84fa 2019-07-27 stsp fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path ...]\n",
1050 507dc3bb 2018-12-29 stsp getprogname());
1054 1ee397ad 2019-07-12 stsp static const struct got_error *
1055 507dc3bb 2018-12-29 stsp update_progress(void *arg, unsigned char status, const char *path)
1057 784955db 2019-01-12 stsp int *did_something = arg;
1059 507dc3bb 2018-12-29 stsp if (status == GOT_STATUS_EXISTS)
1060 1ee397ad 2019-07-12 stsp return NULL;
1062 1545c615 2019-02-10 stsp *did_something = 1;
1064 a484d721 2019-06-10 stsp /* Base commit bump happens silently. */
1065 a484d721 2019-06-10 stsp if (status == GOT_STATUS_BUMP_BASE)
1066 1ee397ad 2019-07-12 stsp return NULL;
1068 507dc3bb 2018-12-29 stsp while (path[0] == '/')
1070 507dc3bb 2018-12-29 stsp printf("%c %s\n", status, path);
1071 1ee397ad 2019-07-12 stsp return NULL;
1074 be7061eb 2018-12-30 stsp static const struct got_error *
1075 a1fb16d8 2019-05-24 stsp switch_head_ref(struct got_reference *head_ref,
1076 a1fb16d8 2019-05-24 stsp struct got_object_id *commit_id, struct got_worktree *worktree,
1077 a1fb16d8 2019-05-24 stsp struct got_repository *repo)
1079 a1fb16d8 2019-05-24 stsp const struct got_error *err = NULL;
1080 a1fb16d8 2019-05-24 stsp char *base_id_str;
1081 a1fb16d8 2019-05-24 stsp int ref_has_moved = 0;
1083 a1fb16d8 2019-05-24 stsp /* Trivial case: switching between two different references. */
1084 a1fb16d8 2019-05-24 stsp if (strcmp(got_ref_get_name(head_ref),
1085 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree)) != 0) {
1086 a1fb16d8 2019-05-24 stsp printf("Switching work tree from %s to %s\n",
1087 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree),
1088 a1fb16d8 2019-05-24 stsp got_ref_get_name(head_ref));
1089 a1fb16d8 2019-05-24 stsp return got_worktree_set_head_ref(worktree, head_ref);
1092 a1fb16d8 2019-05-24 stsp err = check_linear_ancestry(commit_id,
1093 a1fb16d8 2019-05-24 stsp got_worktree_get_base_commit_id(worktree), repo);
1094 a1fb16d8 2019-05-24 stsp if (err) {
1095 a1fb16d8 2019-05-24 stsp if (err->code != GOT_ERR_ANCESTRY)
1096 a1fb16d8 2019-05-24 stsp return err;
1097 a1fb16d8 2019-05-24 stsp ref_has_moved = 1;
1099 a1fb16d8 2019-05-24 stsp if (!ref_has_moved)
1100 a1fb16d8 2019-05-24 stsp return NULL;
1102 a1fb16d8 2019-05-24 stsp /* Switching to a rebased branch with the same reference name. */
1103 a1fb16d8 2019-05-24 stsp err = got_object_id_str(&base_id_str,
1104 a1fb16d8 2019-05-24 stsp got_worktree_get_base_commit_id(worktree));
1106 a1fb16d8 2019-05-24 stsp return err;
1107 a1fb16d8 2019-05-24 stsp printf("Reference %s now points at a different branch\n",
1108 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree));
1109 a1fb16d8 2019-05-24 stsp printf("Switching work tree from %s to %s\n", base_id_str,
1110 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree));
1111 0ebf8283 2019-07-24 stsp return NULL;
1114 0ebf8283 2019-07-24 stsp static const struct got_error *
1115 0ebf8283 2019-07-24 stsp check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
1117 0ebf8283 2019-07-24 stsp const struct got_error *err;
1118 0ebf8283 2019-07-24 stsp int in_progress;
1120 0ebf8283 2019-07-24 stsp err = got_worktree_rebase_in_progress(&in_progress, worktree);
1122 0ebf8283 2019-07-24 stsp return err;
1123 0ebf8283 2019-07-24 stsp if (in_progress)
1124 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_REBASING);
1126 0ebf8283 2019-07-24 stsp err = got_worktree_histedit_in_progress(&in_progress, worktree);
1128 0ebf8283 2019-07-24 stsp return err;
1129 0ebf8283 2019-07-24 stsp if (in_progress)
1130 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_HISTEDIT_BUSY);
1132 a1fb16d8 2019-05-24 stsp return NULL;
1135 a5edda0a 2019-07-27 stsp static const struct got_error *
1136 a5edda0a 2019-07-27 stsp get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
1137 a5edda0a 2019-07-27 stsp char *argv[], struct got_worktree *worktree)
1139 a0de39f3 2019-08-09 stsp const struct got_error *err = NULL;
1140 a5edda0a 2019-07-27 stsp char *path;
1143 a5edda0a 2019-07-27 stsp if (argc == 0) {
1144 a5edda0a 2019-07-27 stsp path = strdup("");
1145 a5edda0a 2019-07-27 stsp if (path == NULL)
1146 a5edda0a 2019-07-27 stsp return got_error_from_errno("strdup");
1147 adc19d55 2019-07-28 stsp return got_pathlist_append(paths, path, NULL);
1150 a5edda0a 2019-07-27 stsp for (i = 0; i < argc; i++) {
1151 a5edda0a 2019-07-27 stsp err = got_worktree_resolve_path(&path, worktree, argv[i]);
1154 adc19d55 2019-07-28 stsp err = got_pathlist_append(paths, path, NULL);
1155 a5edda0a 2019-07-27 stsp if (err) {
1156 a5edda0a 2019-07-27 stsp free(path);
1161 a5edda0a 2019-07-27 stsp return err;
1164 a1fb16d8 2019-05-24 stsp static const struct got_error *
1165 507dc3bb 2018-12-29 stsp cmd_update(int argc, char *argv[])
1167 507dc3bb 2018-12-29 stsp const struct got_error *error = NULL;
1168 507dc3bb 2018-12-29 stsp struct got_repository *repo = NULL;
1169 507dc3bb 2018-12-29 stsp struct got_worktree *worktree = NULL;
1170 f2ea84fa 2019-07-27 stsp char *worktree_path = NULL;
1171 507dc3bb 2018-12-29 stsp struct got_object_id *commit_id = NULL;
1172 9c4b8182 2019-01-02 stsp char *commit_id_str = NULL;
1173 024e9686 2019-05-14 stsp const char *branch_name = NULL;
1174 024e9686 2019-05-14 stsp struct got_reference *head_ref = NULL;
1175 f2ea84fa 2019-07-27 stsp struct got_pathlist_head paths;
1176 f2ea84fa 2019-07-27 stsp struct got_pathlist_entry *pe;
1177 0ebf8283 2019-07-24 stsp int ch, did_something = 0;
1179 f2ea84fa 2019-07-27 stsp TAILQ_INIT(&paths);
1181 024e9686 2019-05-14 stsp while ((ch = getopt(argc, argv, "b:c:")) != -1) {
1182 507dc3bb 2018-12-29 stsp switch (ch) {
1184 024e9686 2019-05-14 stsp branch_name = optarg;
1187 9c4b8182 2019-01-02 stsp commit_id_str = strdup(optarg);
1188 9c4b8182 2019-01-02 stsp if (commit_id_str == NULL)
1189 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
1192 2deda0b9 2019-03-07 stsp usage_update();
1193 507dc3bb 2018-12-29 stsp /* NOTREACHED */
1197 507dc3bb 2018-12-29 stsp argc -= optind;
1198 507dc3bb 2018-12-29 stsp argv += optind;
1200 507dc3bb 2018-12-29 stsp #ifndef PROFILE
1201 68ed9ba5 2019-02-10 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1202 68ed9ba5 2019-02-10 stsp "unveil", NULL) == -1)
1203 507dc3bb 2018-12-29 stsp err(1, "pledge");
1205 c4cdcb68 2019-04-03 stsp worktree_path = getcwd(NULL, 0);
1206 c4cdcb68 2019-04-03 stsp if (worktree_path == NULL) {
1207 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
1208 c4cdcb68 2019-04-03 stsp goto done;
1210 c4cdcb68 2019-04-03 stsp error = got_worktree_open(&worktree, worktree_path);
1211 7d5807f4 2019-07-11 stsp if (error)
1212 7d5807f4 2019-07-11 stsp goto done;
1214 0ebf8283 2019-07-24 stsp error = check_rebase_or_histedit_in_progress(worktree);
1215 f2ea84fa 2019-07-27 stsp if (error)
1216 f2ea84fa 2019-07-27 stsp goto done;
1218 507dc3bb 2018-12-29 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
1219 507dc3bb 2018-12-29 stsp if (error != NULL)
1220 507dc3bb 2018-12-29 stsp goto done;
1222 97430839 2019-03-11 stsp error = apply_unveil(got_repo_get_path(repo), 0,
1223 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
1224 087fb88c 2019-08-04 stsp if (error)
1225 087fb88c 2019-08-04 stsp goto done;
1227 087fb88c 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
1228 0266afb7 2019-01-04 stsp if (error)
1229 0266afb7 2019-01-04 stsp goto done;
1231 a1fb16d8 2019-05-24 stsp error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
1232 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree), 0);
1233 024e9686 2019-05-14 stsp if (error != NULL)
1234 024e9686 2019-05-14 stsp goto done;
1235 507dc3bb 2018-12-29 stsp if (commit_id_str == NULL) {
1236 507dc3bb 2018-12-29 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
1237 9c4b8182 2019-01-02 stsp if (error != NULL)
1238 9c4b8182 2019-01-02 stsp goto done;
1239 9c4b8182 2019-01-02 stsp error = got_object_id_str(&commit_id_str, commit_id);
1240 507dc3bb 2018-12-29 stsp if (error != NULL)
1241 507dc3bb 2018-12-29 stsp goto done;
1243 04f57cb3 2019-07-25 stsp error = resolve_commit_arg(&commit_id, commit_id_str, repo);
1244 04f57cb3 2019-07-25 stsp free(commit_id_str);
1245 bb787f09 2019-07-25 stsp commit_id_str = NULL;
1246 30837e32 2019-07-25 stsp if (error)
1247 a297e751 2019-07-11 stsp goto done;
1248 a297e751 2019-07-11 stsp error = got_object_id_str(&commit_id_str, commit_id);
1249 a297e751 2019-07-11 stsp if (error)
1250 507dc3bb 2018-12-29 stsp goto done;
1253 a1fb16d8 2019-05-24 stsp if (branch_name) {
1254 024e9686 2019-05-14 stsp struct got_object_id *head_commit_id;
1255 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, &paths, entry) {
1256 f2b16ada 2019-08-02 stsp if (pe->path_len == 0)
1258 f2ea84fa 2019-07-27 stsp error = got_error_msg(GOT_ERR_BAD_PATH,
1259 f2ea84fa 2019-07-27 stsp "switching between branches requires that "
1260 f2ea84fa 2019-07-27 stsp "the entire work tree gets updated");
1261 024e9686 2019-05-14 stsp goto done;
1263 024e9686 2019-05-14 stsp error = got_ref_resolve(&head_commit_id, repo, head_ref);
1264 024e9686 2019-05-14 stsp if (error)
1265 024e9686 2019-05-14 stsp goto done;
1266 024e9686 2019-05-14 stsp error = check_linear_ancestry(commit_id, head_commit_id, repo);
1267 a367ff0f 2019-05-14 stsp free(head_commit_id);
1268 024e9686 2019-05-14 stsp if (error != NULL)
1269 024e9686 2019-05-14 stsp goto done;
1270 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
1271 a367ff0f 2019-05-14 stsp if (error)
1272 a367ff0f 2019-05-14 stsp goto done;
1273 a1fb16d8 2019-05-24 stsp error = switch_head_ref(head_ref, commit_id, worktree, repo);
1274 024e9686 2019-05-14 stsp if (error)
1275 024e9686 2019-05-14 stsp goto done;
1277 024e9686 2019-05-14 stsp error = check_linear_ancestry(commit_id,
1278 024e9686 2019-05-14 stsp got_worktree_get_base_commit_id(worktree), repo);
1279 a367ff0f 2019-05-14 stsp if (error != NULL) {
1280 a367ff0f 2019-05-14 stsp if (error->code == GOT_ERR_ANCESTRY)
1281 a367ff0f 2019-05-14 stsp error = got_error(GOT_ERR_BRANCH_MOVED);
1282 024e9686 2019-05-14 stsp goto done;
1284 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
1285 a367ff0f 2019-05-14 stsp if (error)
1286 a367ff0f 2019-05-14 stsp goto done;
1289 507dc3bb 2018-12-29 stsp if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
1290 507dc3bb 2018-12-29 stsp commit_id) != 0) {
1291 507dc3bb 2018-12-29 stsp error = got_worktree_set_base_commit_id(worktree, repo,
1292 507dc3bb 2018-12-29 stsp commit_id);
1293 507dc3bb 2018-12-29 stsp if (error)
1294 507dc3bb 2018-12-29 stsp goto done;
1297 f2ea84fa 2019-07-27 stsp error = got_worktree_checkout_files(worktree, &paths, repo,
1298 6bad629b 2019-02-04 stsp update_progress, &did_something, check_cancelled, NULL);
1299 507dc3bb 2018-12-29 stsp if (error != NULL)
1300 507dc3bb 2018-12-29 stsp goto done;
1302 784955db 2019-01-12 stsp if (did_something)
1303 784955db 2019-01-12 stsp printf("Updated to commit %s\n", commit_id_str);
1305 784955db 2019-01-12 stsp printf("Already up-to-date\n");
1307 c09a553d 2018-03-12 stsp free(worktree_path);
1308 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, &paths, entry)
1309 f2ea84fa 2019-07-27 stsp free((char *)pe->path);
1310 f2ea84fa 2019-07-27 stsp got_pathlist_free(&paths);
1311 507dc3bb 2018-12-29 stsp free(commit_id);
1312 9c4b8182 2019-01-02 stsp free(commit_id_str);
1313 c09a553d 2018-03-12 stsp return error;
1316 f42b1b34 2018-03-12 stsp static const struct got_error *
1317 79109fed 2018-03-27 stsp print_patch(struct got_commit_object *commit, struct got_object_id *id,
1318 c0cc5c62 2018-10-18 stsp int diff_context, struct got_repository *repo)
1320 f42b1b34 2018-03-12 stsp const struct got_error *err = NULL;
1321 79109fed 2018-03-27 stsp struct got_tree_object *tree1 = NULL, *tree2;
1322 79f35eb3 2018-06-11 stsp struct got_object_qid *qid;
1323 0f2b3dca 2018-12-22 stsp char *id_str1 = NULL, *id_str2;
1324 aaa13589 2019-06-01 stsp struct got_diff_blob_output_unidiff_arg arg;
1326 45d799e2 2018-12-23 stsp err = got_object_open_as_tree(&tree2, repo,
1327 45d799e2 2018-12-23 stsp got_object_commit_get_tree_id(commit));
1329 79109fed 2018-03-27 stsp return err;
1331 45d799e2 2018-12-23 stsp qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1332 79f35eb3 2018-06-11 stsp if (qid != NULL) {
1333 79109fed 2018-03-27 stsp struct got_commit_object *pcommit;
1335 117e771c 2018-07-23 stsp err = got_object_open_as_commit(&pcommit, repo, qid->id);
1337 79109fed 2018-03-27 stsp return err;
1339 45d799e2 2018-12-23 stsp err = got_object_open_as_tree(&tree1, repo,
1340 45d799e2 2018-12-23 stsp got_object_commit_get_tree_id(pcommit));
1341 79109fed 2018-03-27 stsp got_object_commit_close(pcommit);
1343 0f2b3dca 2018-12-22 stsp return err;
1345 0f2b3dca 2018-12-22 stsp err = got_object_id_str(&id_str1, qid->id);
1347 79109fed 2018-03-27 stsp return err;
1350 0f2b3dca 2018-12-22 stsp err = got_object_id_str(&id_str2, id);
1352 0f2b3dca 2018-12-22 stsp goto done;
1354 56765ebb 2018-12-23 stsp printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1355 aaa13589 2019-06-01 stsp arg.diff_context = diff_context;
1356 aaa13589 2019-06-01 stsp arg.outfile = stdout;
1357 aaa13589 2019-06-01 stsp err = got_diff_tree(tree1, tree2, "", "", repo,
1358 31b4484f 2019-07-27 stsp got_diff_blob_output_unidiff, &arg, 1);
1360 79109fed 2018-03-27 stsp if (tree1)
1361 79109fed 2018-03-27 stsp got_object_tree_close(tree1);
1362 79109fed 2018-03-27 stsp got_object_tree_close(tree2);
1363 0f2b3dca 2018-12-22 stsp free(id_str1);
1364 0f2b3dca 2018-12-22 stsp free(id_str2);
1365 79109fed 2018-03-27 stsp return err;
1368 4bb494d5 2018-06-16 stsp static char *
1369 6c281f94 2018-06-11 stsp get_datestr(time_t *time, char *datebuf)
1371 09867e48 2019-08-13 stsp struct tm mytm, *tm;
1372 09867e48 2019-08-13 stsp char *p, *s;
1374 09867e48 2019-08-13 stsp tm = gmtime_r(time, &mytm);
1375 09867e48 2019-08-13 stsp if (tm == NULL)
1376 09867e48 2019-08-13 stsp return NULL;
1377 09867e48 2019-08-13 stsp s = asctime_r(tm, datebuf);
1378 09867e48 2019-08-13 stsp if (s == NULL)
1379 09867e48 2019-08-13 stsp return NULL;
1380 6c281f94 2018-06-11 stsp p = strchr(s, '\n');
1382 6c281f94 2018-06-11 stsp *p = '\0';
1386 dc424a06 2019-08-07 stsp #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
1388 79109fed 2018-03-27 stsp static const struct got_error *
1389 79109fed 2018-03-27 stsp print_commit(struct got_commit_object *commit, struct got_object_id *id,
1390 199a4027 2019-02-02 stsp struct got_repository *repo, int show_patch, int diff_context,
1391 199a4027 2019-02-02 stsp struct got_reflist_head *refs)
1393 79109fed 2018-03-27 stsp const struct got_error *err = NULL;
1394 621015ac 2018-07-23 stsp char *id_str, *datestr, *logmsg0, *logmsg, *line;
1395 6c281f94 2018-06-11 stsp char datebuf[26];
1396 45d799e2 2018-12-23 stsp time_t committer_time;
1397 45d799e2 2018-12-23 stsp const char *author, *committer;
1398 199a4027 2019-02-02 stsp char *refs_str = NULL;
1399 199a4027 2019-02-02 stsp struct got_reflist_entry *re;
1401 199a4027 2019-02-02 stsp SIMPLEQ_FOREACH(re, refs, entry) {
1403 199a4027 2019-02-02 stsp const char *name;
1404 a436ad14 2019-08-13 stsp struct got_tag_object *tag = NULL;
1407 199a4027 2019-02-02 stsp name = got_ref_get_name(re->ref);
1408 d9498b20 2019-02-05 stsp if (strcmp(name, GOT_REF_HEAD) == 0)
1410 199a4027 2019-02-02 stsp if (strncmp(name, "refs/", 5) == 0)
1411 199a4027 2019-02-02 stsp name += 5;
1412 7143d404 2019-03-12 stsp if (strncmp(name, "got/", 4) == 0)
1414 e34f9ed6 2019-02-02 stsp if (strncmp(name, "heads/", 6) == 0)
1415 e34f9ed6 2019-02-02 stsp name += 6;
1416 141c2bff 2019-02-04 stsp if (strncmp(name, "remotes/", 8) == 0)
1417 141c2bff 2019-02-04 stsp name += 8;
1418 a436ad14 2019-08-13 stsp if (strncmp(name, "tags/", 5) == 0) {
1419 a436ad14 2019-08-13 stsp err = got_object_open_as_tag(&tag, repo, re->id);
1420 5d844a1e 2019-08-13 stsp if (err) {
1421 5d844a1e 2019-08-13 stsp if (err->code != GOT_ERR_OBJ_TYPE)
1422 5d844a1e 2019-08-13 stsp return err;
1423 5d844a1e 2019-08-13 stsp /* Ref points at something other than a tag. */
1424 5d844a1e 2019-08-13 stsp err = NULL;
1425 5d844a1e 2019-08-13 stsp tag = NULL;
1428 a436ad14 2019-08-13 stsp cmp = got_object_id_cmp(tag ?
1429 a436ad14 2019-08-13 stsp got_object_tag_get_object_id(tag) : re->id, id);
1431 a436ad14 2019-08-13 stsp got_object_tag_close(tag);
1432 a436ad14 2019-08-13 stsp if (cmp != 0)
1434 199a4027 2019-02-02 stsp s = refs_str;
1435 199a4027 2019-02-02 stsp if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
1436 199a4027 2019-02-02 stsp name) == -1) {
1437 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1439 34ca4898 2019-08-13 stsp return err;
1443 832c249c 2018-06-10 stsp err = got_object_id_str(&id_str, id);
1445 8bf5b3c9 2018-03-17 stsp return err;
1447 dc424a06 2019-08-07 stsp printf(GOT_COMMIT_SEP_STR);
1448 199a4027 2019-02-02 stsp printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
1449 199a4027 2019-02-02 stsp refs_str ? refs_str : "", refs_str ? ")" : "");
1450 832c249c 2018-06-10 stsp free(id_str);
1451 d3d493d7 2019-02-21 stsp id_str = NULL;
1452 d3d493d7 2019-02-21 stsp free(refs_str);
1453 d3d493d7 2019-02-21 stsp refs_str = NULL;
1454 45d799e2 2018-12-23 stsp printf("from: %s\n", got_object_commit_get_author(commit));
1455 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
1456 45d799e2 2018-12-23 stsp datestr = get_datestr(&committer_time, datebuf);
1457 09867e48 2019-08-13 stsp if (datestr)
1458 09867e48 2019-08-13 stsp printf("date: %s UTC\n", datestr);
1459 45d799e2 2018-12-23 stsp author = got_object_commit_get_author(commit);
1460 45d799e2 2018-12-23 stsp committer = got_object_commit_get_committer(commit);
1461 45d799e2 2018-12-23 stsp if (strcmp(author, committer) != 0)
1462 45d799e2 2018-12-23 stsp printf("via: %s\n", committer);
1463 45d799e2 2018-12-23 stsp if (got_object_commit_get_nparents(commit) > 1) {
1464 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
1465 79f35eb3 2018-06-11 stsp struct got_object_qid *qid;
1466 3fe1abad 2018-06-10 stsp int n = 1;
1467 45d799e2 2018-12-23 stsp parent_ids = got_object_commit_get_parent_ids(commit);
1468 45d799e2 2018-12-23 stsp SIMPLEQ_FOREACH(qid, parent_ids, entry) {
1469 79f35eb3 2018-06-11 stsp err = got_object_id_str(&id_str, qid->id);
1471 a0603db2 2018-06-10 stsp return err;
1472 3fe1abad 2018-06-10 stsp printf("parent %d: %s\n", n++, id_str);
1473 a0603db2 2018-06-10 stsp free(id_str);
1477 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
1479 5943eee2 2019-08-13 stsp return err;
1481 621015ac 2018-07-23 stsp logmsg = logmsg0;
1483 832c249c 2018-06-10 stsp line = strsep(&logmsg, "\n");
1485 832c249c 2018-06-10 stsp printf(" %s\n", line);
1486 832c249c 2018-06-10 stsp } while (line);
1487 621015ac 2018-07-23 stsp free(logmsg0);
1489 971751ac 2018-03-27 stsp if (show_patch) {
1490 c0cc5c62 2018-10-18 stsp err = print_patch(commit, id, diff_context, repo);
1491 971751ac 2018-03-27 stsp if (err == 0)
1492 971751ac 2018-03-27 stsp printf("\n");
1495 cbe7f848 2019-02-11 stsp if (fflush(stdout) != 0 && err == NULL)
1496 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
1497 07862c20 2018-09-15 stsp return err;
1500 f42b1b34 2018-03-12 stsp static const struct got_error *
1501 15a94983 2018-12-23 stsp print_commits(struct got_object_id *root_id, struct got_repository *repo,
1502 15a94983 2018-12-23 stsp char *path, int show_patch, int diff_context, int limit,
1503 199a4027 2019-02-02 stsp int first_parent_traversal, struct got_reflist_head *refs)
1505 f42b1b34 2018-03-12 stsp const struct got_error *err;
1506 372ccdbb 2018-06-10 stsp struct got_commit_graph *graph;
1508 31cedeaf 2018-09-15 stsp err = got_commit_graph_open(&graph, root_id, path,
1509 31cedeaf 2018-09-15 stsp first_parent_traversal, repo);
1511 f42b1b34 2018-03-12 stsp return err;
1512 6fb7cd11 2019-08-22 stsp err = got_commit_graph_iter_start(graph, root_id, repo,
1513 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
1515 fcc85cad 2018-07-22 stsp goto done;
1517 372ccdbb 2018-06-10 stsp struct got_commit_object *commit;
1518 372ccdbb 2018-06-10 stsp struct got_object_id *id;
1520 84453469 2018-11-11 stsp if (sigint_received || sigpipe_received)
1523 b43fbaa0 2018-06-11 stsp err = got_commit_graph_iter_next(&id, graph);
1524 372ccdbb 2018-06-10 stsp if (err) {
1525 9ba79e04 2018-06-11 stsp if (err->code == GOT_ERR_ITER_COMPLETED) {
1526 9ba79e04 2018-06-11 stsp err = NULL;
1529 372ccdbb 2018-06-10 stsp if (err->code != GOT_ERR_ITER_NEED_MORE)
1531 6fb7cd11 2019-08-22 stsp err = got_commit_graph_fetch_commits(graph, 1, repo,
1532 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
1538 b43fbaa0 2018-06-11 stsp if (id == NULL)
1541 f8e900f3 2018-06-11 stsp err = got_object_open_as_commit(&commit, repo, id);
1544 199a4027 2019-02-02 stsp err = print_commit(commit, id, repo, show_patch, diff_context,
1546 b43fbaa0 2018-06-11 stsp got_object_commit_close(commit);
1547 372ccdbb 2018-06-10 stsp if (err || (limit && --limit == 0))
1551 372ccdbb 2018-06-10 stsp got_commit_graph_close(graph);
1552 f42b1b34 2018-03-12 stsp return err;
1555 4ed7e80c 2018-05-20 stsp __dead static void
1556 6f3d1eb0 2018-03-12 stsp usage_log(void)
1558 cc54c501 2019-07-15 stsp fprintf(stderr, "usage: %s log [-c commit] [-C number] [-f] [ -l N ] [-p] "
1559 04ca23f4 2018-07-16 stsp "[-r repository-path] [path]\n", getprogname());
1563 b1ebc001 2019-08-13 stsp static int
1564 b1ebc001 2019-08-13 stsp get_default_log_limit(void)
1566 b1ebc001 2019-08-13 stsp const char *got_default_log_limit;
1567 b1ebc001 2019-08-13 stsp long long n;
1568 b1ebc001 2019-08-13 stsp const char *errstr;
1570 b1ebc001 2019-08-13 stsp got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
1571 b1ebc001 2019-08-13 stsp if (got_default_log_limit == NULL)
1573 b1ebc001 2019-08-13 stsp n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
1574 b1ebc001 2019-08-13 stsp if (errstr != NULL)
1579 4ed7e80c 2018-05-20 stsp static const struct got_error *
1580 f42b1b34 2018-03-12 stsp cmd_log(int argc, char *argv[])
1582 f42b1b34 2018-03-12 stsp const struct got_error *error;
1583 04ca23f4 2018-07-16 stsp struct got_repository *repo = NULL;
1584 cffc0aa4 2019-02-05 stsp struct got_worktree *worktree = NULL;
1585 15a94983 2018-12-23 stsp struct got_commit_object *commit = NULL;
1586 3235492e 2018-04-01 stsp struct got_object_id *id = NULL;
1587 04ca23f4 2018-07-16 stsp char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
1588 d142fc45 2018-04-01 stsp char *start_commit = NULL;
1589 c0cc5c62 2018-10-18 stsp int diff_context = 3, ch;
1590 1fd6d7ea 2018-06-13 stsp int show_patch = 0, limit = 0, first_parent_traversal = 0;
1591 64a96a6d 2018-04-01 stsp const char *errstr;
1592 199a4027 2019-02-02 stsp struct got_reflist_head refs;
1594 1b3893a2 2019-03-18 stsp SIMPLEQ_INIT(&refs);
1596 6715a751 2018-03-16 stsp #ifndef PROFILE
1597 6098196c 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1600 f42b1b34 2018-03-12 stsp err(1, "pledge");
1603 b1ebc001 2019-08-13 stsp limit = get_default_log_limit();
1605 cc54c501 2019-07-15 stsp while ((ch = getopt(argc, argv, "b:pc:C:l:fr:")) != -1) {
1606 79109fed 2018-03-27 stsp switch (ch) {
1608 79109fed 2018-03-27 stsp show_patch = 1;
1611 d142fc45 2018-04-01 stsp start_commit = optarg;
1614 4a8520aa 2018-10-18 stsp diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
1616 c0cc5c62 2018-10-18 stsp if (errstr != NULL)
1617 c0cc5c62 2018-10-18 stsp err(1, "-C option %s", errstr);
1620 b1ebc001 2019-08-13 stsp limit = strtonum(optarg, 0, INT_MAX, &errstr);
1621 64a96a6d 2018-04-01 stsp if (errstr != NULL)
1622 64a96a6d 2018-04-01 stsp err(1, "-l option %s", errstr);
1625 cc54c501 2019-07-15 stsp first_parent_traversal = 1;
1628 04ca23f4 2018-07-16 stsp repo_path = realpath(optarg, NULL);
1629 04ca23f4 2018-07-16 stsp if (repo_path == NULL)
1630 04ca23f4 2018-07-16 stsp err(1, "-r option");
1631 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
1634 2deda0b9 2019-03-07 stsp usage_log();
1635 79109fed 2018-03-27 stsp /* NOTREACHED */
1639 79109fed 2018-03-27 stsp argc -= optind;
1640 79109fed 2018-03-27 stsp argv += optind;
1642 04ca23f4 2018-07-16 stsp cwd = getcwd(NULL, 0);
1643 04ca23f4 2018-07-16 stsp if (cwd == NULL) {
1644 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
1645 04ca23f4 2018-07-16 stsp goto done;
1648 cffc0aa4 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
1649 cffc0aa4 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
1650 cffc0aa4 2019-02-05 stsp goto done;
1651 cffc0aa4 2019-02-05 stsp error = NULL;
1653 e7301579 2019-03-18 stsp if (argc == 0) {
1654 cbd1af7a 2019-03-18 stsp path = strdup("");
1655 e7301579 2019-03-18 stsp if (path == NULL) {
1656 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
1657 cbd1af7a 2019-03-18 stsp goto done;
1659 e7301579 2019-03-18 stsp } else if (argc == 1) {
1660 e7301579 2019-03-18 stsp if (worktree) {
1661 e7301579 2019-03-18 stsp error = got_worktree_resolve_path(&path, worktree,
1663 e7301579 2019-03-18 stsp if (error)
1664 e7301579 2019-03-18 stsp goto done;
1666 e7301579 2019-03-18 stsp path = strdup(argv[0]);
1667 e7301579 2019-03-18 stsp if (path == NULL) {
1668 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
1669 e7301579 2019-03-18 stsp goto done;
1673 cbd1af7a 2019-03-18 stsp usage_log();
1675 5486daa2 2019-05-11 stsp if (repo_path == NULL) {
1676 5486daa2 2019-05-11 stsp repo_path = worktree ?
1677 5486daa2 2019-05-11 stsp strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
1679 04ca23f4 2018-07-16 stsp if (repo_path == NULL) {
1680 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
1681 cffc0aa4 2019-02-05 stsp goto done;
1684 f42b1b34 2018-03-12 stsp error = got_repo_open(&repo, repo_path);
1685 d7d4f210 2018-03-12 stsp if (error != NULL)
1686 04ca23f4 2018-07-16 stsp goto done;
1688 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), 1,
1689 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
1690 c02c541e 2019-03-29 stsp if (error)
1691 c02c541e 2019-03-29 stsp goto done;
1693 d142fc45 2018-04-01 stsp if (start_commit == NULL) {
1694 3235492e 2018-04-01 stsp struct got_reference *head_ref;
1695 1cc14b9f 2019-05-14 stsp error = got_ref_open(&head_ref, repo,
1696 1cc14b9f 2019-05-14 stsp worktree ? got_worktree_get_head_ref_name(worktree)
1697 1cc14b9f 2019-05-14 stsp : GOT_REF_HEAD, 0);
1698 3235492e 2018-04-01 stsp if (error != NULL)
1699 3235492e 2018-04-01 stsp return error;
1700 3235492e 2018-04-01 stsp error = got_ref_resolve(&id, repo, head_ref);
1701 3235492e 2018-04-01 stsp got_ref_close(head_ref);
1702 3235492e 2018-04-01 stsp if (error != NULL)
1703 3235492e 2018-04-01 stsp return error;
1704 15a94983 2018-12-23 stsp error = got_object_open_as_commit(&commit, repo, id);
1706 d1f2edc9 2018-06-13 stsp struct got_reference *ref;
1707 2f17228e 2019-05-12 stsp error = got_ref_open(&ref, repo, start_commit, 0);
1708 3235492e 2018-04-01 stsp if (error == NULL) {
1709 1caad61b 2019-02-01 stsp int obj_type;
1710 d1f2edc9 2018-06-13 stsp error = got_ref_resolve(&id, repo, ref);
1711 d1f2edc9 2018-06-13 stsp got_ref_close(ref);
1712 d1f2edc9 2018-06-13 stsp if (error != NULL)
1713 1caad61b 2019-02-01 stsp goto done;
1714 1caad61b 2019-02-01 stsp error = got_object_get_type(&obj_type, repo, id);
1715 1caad61b 2019-02-01 stsp if (error != NULL)
1716 1caad61b 2019-02-01 stsp goto done;
1717 1caad61b 2019-02-01 stsp if (obj_type == GOT_OBJ_TYPE_TAG) {
1718 1caad61b 2019-02-01 stsp struct got_tag_object *tag;
1719 1caad61b 2019-02-01 stsp error = got_object_open_as_tag(&tag, repo, id);
1720 1caad61b 2019-02-01 stsp if (error != NULL)
1721 1caad61b 2019-02-01 stsp goto done;
1722 1caad61b 2019-02-01 stsp if (got_object_tag_get_object_type(tag) !=
1723 1caad61b 2019-02-01 stsp GOT_OBJ_TYPE_COMMIT) {
1724 1caad61b 2019-02-01 stsp got_object_tag_close(tag);
1725 1caad61b 2019-02-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
1726 1caad61b 2019-02-01 stsp goto done;
1729 1caad61b 2019-02-01 stsp id = got_object_id_dup(
1730 1caad61b 2019-02-01 stsp got_object_tag_get_object_id(tag));
1731 1caad61b 2019-02-01 stsp if (id == NULL)
1732 638f9024 2019-05-13 stsp error = got_error_from_errno(
1733 230a42bd 2019-05-11 jcs "got_object_id_dup");
1734 1caad61b 2019-02-01 stsp got_object_tag_close(tag);
1735 1caad61b 2019-02-01 stsp if (error)
1736 1caad61b 2019-02-01 stsp goto done;
1737 1caad61b 2019-02-01 stsp } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
1738 1caad61b 2019-02-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
1739 1caad61b 2019-02-01 stsp goto done;
1741 15a94983 2018-12-23 stsp error = got_object_open_as_commit(&commit, repo, id);
1742 d1f2edc9 2018-06-13 stsp if (error != NULL)
1743 1caad61b 2019-02-01 stsp goto done;
1745 15a94983 2018-12-23 stsp if (commit == NULL) {
1746 e09a504c 2019-06-28 stsp error = got_repo_match_object_id_prefix(&id,
1747 dd88155e 2019-06-29 stsp start_commit, GOT_OBJ_TYPE_COMMIT, repo);
1748 d1f2edc9 2018-06-13 stsp if (error != NULL)
1749 d1f2edc9 2018-06-13 stsp return error;
1752 d7d4f210 2018-03-12 stsp if (error != NULL)
1753 04ca23f4 2018-07-16 stsp goto done;
1755 23721109 2018-10-22 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
1756 04ca23f4 2018-07-16 stsp if (error != NULL)
1757 04ca23f4 2018-07-16 stsp goto done;
1758 04ca23f4 2018-07-16 stsp if (in_repo_path) {
1759 04ca23f4 2018-07-16 stsp free(path);
1760 04ca23f4 2018-07-16 stsp path = in_repo_path;
1763 199a4027 2019-02-02 stsp error = got_ref_list(&refs, repo);
1764 199a4027 2019-02-02 stsp if (error)
1765 199a4027 2019-02-02 stsp goto done;
1767 15a94983 2018-12-23 stsp error = print_commits(id, repo, path, show_patch,
1768 199a4027 2019-02-02 stsp diff_context, limit, first_parent_traversal, &refs);
1770 04ca23f4 2018-07-16 stsp free(path);
1771 04ca23f4 2018-07-16 stsp free(repo_path);
1772 04ca23f4 2018-07-16 stsp free(cwd);
1774 cffc0aa4 2019-02-05 stsp if (worktree)
1775 cffc0aa4 2019-02-05 stsp got_worktree_close(worktree);
1776 ad242220 2018-09-08 stsp if (repo) {
1777 ad242220 2018-09-08 stsp const struct got_error *repo_error;
1778 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
1779 ad242220 2018-09-08 stsp if (error == NULL)
1780 ad242220 2018-09-08 stsp error = repo_error;
1782 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
1783 8bf5b3c9 2018-03-17 stsp return error;
1786 4ed7e80c 2018-05-20 stsp __dead static void
1787 3f8b7d6a 2018-04-01 stsp usage_diff(void)
1789 98eaaa12 2019-08-03 stsp fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] [-s] "
1790 927df6b7 2019-02-10 stsp "[object1 object2 | path]\n", getprogname());
1794 b72f483a 2019-02-05 stsp struct print_diff_arg {
1795 b72f483a 2019-02-05 stsp struct got_repository *repo;
1796 b72f483a 2019-02-05 stsp struct got_worktree *worktree;
1797 b72f483a 2019-02-05 stsp int diff_context;
1798 3fc0c068 2019-02-10 stsp const char *id_str;
1799 3fc0c068 2019-02-10 stsp int header_shown;
1800 98eaaa12 2019-08-03 stsp int diff_staged;
1803 4ed7e80c 2018-05-20 stsp static const struct got_error *
1804 88d0e355 2019-08-03 stsp print_diff(void *arg, unsigned char status, unsigned char staged_status,
1805 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
1806 537ac44b 2019-08-03 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
1808 b72f483a 2019-02-05 stsp struct print_diff_arg *a = arg;
1809 b72f483a 2019-02-05 stsp const struct got_error *err = NULL;
1810 b72f483a 2019-02-05 stsp struct got_blob_object *blob1 = NULL;
1811 b72f483a 2019-02-05 stsp FILE *f2 = NULL;
1812 4ce46740 2019-08-08 stsp char *abspath = NULL, *label1 = NULL;
1813 b72f483a 2019-02-05 stsp struct stat sb;
1815 98eaaa12 2019-08-03 stsp if (a->diff_staged) {
1816 98eaaa12 2019-08-03 stsp if (staged_status != GOT_STATUS_MODIFY &&
1817 98eaaa12 2019-08-03 stsp staged_status != GOT_STATUS_ADD &&
1818 98eaaa12 2019-08-03 stsp staged_status != GOT_STATUS_DELETE)
1819 98eaaa12 2019-08-03 stsp return NULL;
1821 98eaaa12 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
1822 98eaaa12 2019-08-03 stsp return NULL;
1823 98eaaa12 2019-08-03 stsp if (status != GOT_STATUS_MODIFY &&
1824 98eaaa12 2019-08-03 stsp status != GOT_STATUS_ADD &&
1825 98eaaa12 2019-08-03 stsp status != GOT_STATUS_DELETE &&
1826 98eaaa12 2019-08-03 stsp status != GOT_STATUS_CONFLICT)
1827 98eaaa12 2019-08-03 stsp return NULL;
1830 3fc0c068 2019-02-10 stsp if (!a->header_shown) {
1831 98eaaa12 2019-08-03 stsp printf("diff %s %s%s\n", a->id_str,
1832 98eaaa12 2019-08-03 stsp got_worktree_get_root_path(a->worktree),
1833 98eaaa12 2019-08-03 stsp a->diff_staged ? " (staged changes)" : "");
1834 3fc0c068 2019-02-10 stsp a->header_shown = 1;
1837 98eaaa12 2019-08-03 stsp if (a->diff_staged) {
1838 98eaaa12 2019-08-03 stsp const char *label1 = NULL, *label2 = NULL;
1839 98eaaa12 2019-08-03 stsp switch (staged_status) {
1840 98eaaa12 2019-08-03 stsp case GOT_STATUS_MODIFY:
1841 98eaaa12 2019-08-03 stsp label1 = path;
1842 98eaaa12 2019-08-03 stsp label2 = path;
1844 98eaaa12 2019-08-03 stsp case GOT_STATUS_ADD:
1845 98eaaa12 2019-08-03 stsp label2 = path;
1847 98eaaa12 2019-08-03 stsp case GOT_STATUS_DELETE:
1848 98eaaa12 2019-08-03 stsp label1 = path;
1851 98eaaa12 2019-08-03 stsp return got_error(GOT_ERR_FILE_STATUS);
1853 98eaaa12 2019-08-03 stsp return got_diff_objects_as_blobs(blob_id, staged_blob_id,
1854 98eaaa12 2019-08-03 stsp label1, label2, a->diff_context, a->repo, stdout);
1857 408b4ebc 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
1858 4ce46740 2019-08-08 stsp staged_status == GOT_STATUS_MODIFY) {
1859 4ce46740 2019-08-08 stsp char *id_str;
1860 408b4ebc 2019-08-03 stsp err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
1863 4ce46740 2019-08-08 stsp goto done;
1864 4ce46740 2019-08-08 stsp err = got_object_id_str(&id_str, staged_blob_id);
1866 4ce46740 2019-08-08 stsp goto done;
1867 4ce46740 2019-08-08 stsp if (asprintf(&label1, "%s (staged)", id_str) == -1) {
1868 4ce46740 2019-08-08 stsp err = got_error_from_errno("asprintf");
1869 4ce46740 2019-08-08 stsp free(id_str);
1870 4ce46740 2019-08-08 stsp goto done;
1872 4ce46740 2019-08-08 stsp free(id_str);
1873 4ce46740 2019-08-08 stsp } else if (status != GOT_STATUS_ADD) {
1874 016a88dd 2019-05-13 stsp err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
1876 4ce46740 2019-08-08 stsp goto done;
1879 7154f6ce 2019-03-27 stsp if (status != GOT_STATUS_DELETE) {
1880 2ec1f75b 2019-03-26 stsp if (asprintf(&abspath, "%s/%s",
1881 2ec1f75b 2019-03-26 stsp got_worktree_get_root_path(a->worktree), path) == -1) {
1882 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1883 2ec1f75b 2019-03-26 stsp goto done;
1886 2ec1f75b 2019-03-26 stsp f2 = fopen(abspath, "r");
1887 2ec1f75b 2019-03-26 stsp if (f2 == NULL) {
1888 638f9024 2019-05-13 stsp err = got_error_from_errno2("fopen", abspath);
1889 2ec1f75b 2019-03-26 stsp goto done;
1891 2ec1f75b 2019-03-26 stsp if (lstat(abspath, &sb) == -1) {
1892 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", abspath);
1893 2ec1f75b 2019-03-26 stsp goto done;
1896 2ec1f75b 2019-03-26 stsp sb.st_size = 0;
1898 4ce46740 2019-08-08 stsp err = got_diff_blob_file(blob1, label1, f2, sb.st_size, path,
1899 4ce46740 2019-08-08 stsp a->diff_context, stdout);
1901 b72f483a 2019-02-05 stsp if (blob1)
1902 b72f483a 2019-02-05 stsp got_object_blob_close(blob1);
1903 fb43ecf1 2019-02-11 stsp if (f2 && fclose(f2) != 0 && err == NULL)
1904 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
1905 b72f483a 2019-02-05 stsp free(abspath);
1906 927df6b7 2019-02-10 stsp return err;
1909 927df6b7 2019-02-10 stsp static const struct got_error *
1910 0adb7196 2019-08-11 stsp match_object_id(struct got_object_id **id, char **label,
1911 01073a5d 2019-08-22 stsp const char *id_str, int obj_type, int resolve_tags,
1912 01073a5d 2019-08-22 stsp struct got_repository *repo)
1914 0adb7196 2019-08-11 stsp const struct got_error *err;
1915 d24820bf 2019-08-11 stsp struct got_tag_object *tag;
1916 0adb7196 2019-08-11 stsp struct got_reference *ref = NULL;
1918 0adb7196 2019-08-11 stsp *id = NULL;
1919 0adb7196 2019-08-11 stsp *label = NULL;
1921 01073a5d 2019-08-22 stsp if (resolve_tags) {
1922 01073a5d 2019-08-22 stsp err = got_repo_object_match_tag(&tag, id_str, GOT_OBJ_TYPE_ANY,
1924 01073a5d 2019-08-22 stsp if (err == NULL) {
1925 01073a5d 2019-08-22 stsp *id = got_object_id_dup(
1926 01073a5d 2019-08-22 stsp got_object_tag_get_object_id(tag));
1927 01073a5d 2019-08-22 stsp if (*id == NULL)
1928 01073a5d 2019-08-22 stsp err = got_error_from_errno("got_object_id_dup");
1929 01073a5d 2019-08-22 stsp else if (asprintf(label, "refs/tags/%s",
1930 01073a5d 2019-08-22 stsp got_object_tag_get_name(tag)) == -1) {
1931 01073a5d 2019-08-22 stsp err = got_error_from_errno("asprintf");
1933 01073a5d 2019-08-22 stsp *id = NULL;
1935 01073a5d 2019-08-22 stsp got_object_tag_close(tag);
1936 01073a5d 2019-08-22 stsp return err;
1937 01073a5d 2019-08-22 stsp } else if (err->code != GOT_ERR_NO_OBJ)
1938 01073a5d 2019-08-22 stsp return err;
1941 0adb7196 2019-08-11 stsp err = got_repo_match_object_id_prefix(id, id_str, obj_type, repo);
1942 0adb7196 2019-08-11 stsp if (err) {
1943 0adb7196 2019-08-11 stsp if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
1944 0adb7196 2019-08-11 stsp return err;
1945 0adb7196 2019-08-11 stsp err = got_ref_open(&ref, repo, id_str, 0);
1946 0adb7196 2019-08-11 stsp if (err != NULL)
1947 0adb7196 2019-08-11 stsp goto done;
1948 0adb7196 2019-08-11 stsp *label = strdup(got_ref_get_name(ref));
1949 0adb7196 2019-08-11 stsp if (*label == NULL) {
1950 0adb7196 2019-08-11 stsp err = got_error_from_errno("strdup");
1951 0adb7196 2019-08-11 stsp goto done;
1953 0adb7196 2019-08-11 stsp err = got_ref_resolve(id, repo, ref);
1955 0adb7196 2019-08-11 stsp err = got_object_id_str(label, *id);
1956 0adb7196 2019-08-11 stsp if (*label == NULL) {
1957 0adb7196 2019-08-11 stsp err = got_error_from_errno("strdup");
1958 0adb7196 2019-08-11 stsp goto done;
1963 0adb7196 2019-08-11 stsp got_ref_close(ref);
1964 0adb7196 2019-08-11 stsp return err;
1968 0adb7196 2019-08-11 stsp static const struct got_error *
1969 b00d56cd 2018-04-01 stsp cmd_diff(int argc, char *argv[])
1971 b00d56cd 2018-04-01 stsp const struct got_error *error;
1972 b00d56cd 2018-04-01 stsp struct got_repository *repo = NULL;
1973 b72f483a 2019-02-05 stsp struct got_worktree *worktree = NULL;
1974 b72f483a 2019-02-05 stsp char *cwd = NULL, *repo_path = NULL;
1975 15a94983 2018-12-23 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
1976 cd628e99 2019-05-28 stsp const char *id_str1 = NULL, *id_str2 = NULL;
1977 5e70831e 2019-05-28 stsp char *label1 = NULL, *label2 = NULL;
1978 15a94983 2018-12-23 stsp int type1, type2;
1979 98eaaa12 2019-08-03 stsp int diff_context = 3, diff_staged = 0, ch;
1980 c0cc5c62 2018-10-18 stsp const char *errstr;
1981 927df6b7 2019-02-10 stsp char *path = NULL;
1983 b00d56cd 2018-04-01 stsp #ifndef PROFILE
1984 25eccc22 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1985 25eccc22 2019-01-04 stsp NULL) == -1)
1986 b00d56cd 2018-04-01 stsp err(1, "pledge");
1989 98eaaa12 2019-08-03 stsp while ((ch = getopt(argc, argv, "C:r:s")) != -1) {
1990 b00d56cd 2018-04-01 stsp switch (ch) {
1992 c0cc5c62 2018-10-18 stsp diff_context = strtonum(optarg, 1, INT_MAX, &errstr);
1993 c0cc5c62 2018-10-18 stsp if (errstr != NULL)
1994 c0cc5c62 2018-10-18 stsp err(1, "-C option %s", errstr);
1997 b72f483a 2019-02-05 stsp repo_path = realpath(optarg, NULL);
1998 b72f483a 2019-02-05 stsp if (repo_path == NULL)
1999 b72f483a 2019-02-05 stsp err(1, "-r option");
2000 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
2003 98eaaa12 2019-08-03 stsp diff_staged = 1;
2006 2deda0b9 2019-03-07 stsp usage_diff();
2007 b00d56cd 2018-04-01 stsp /* NOTREACHED */
2011 b00d56cd 2018-04-01 stsp argc -= optind;
2012 b00d56cd 2018-04-01 stsp argv += optind;
2014 b72f483a 2019-02-05 stsp cwd = getcwd(NULL, 0);
2015 b72f483a 2019-02-05 stsp if (cwd == NULL) {
2016 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2017 b72f483a 2019-02-05 stsp goto done;
2019 927df6b7 2019-02-10 stsp error = got_worktree_open(&worktree, cwd);
2020 927df6b7 2019-02-10 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
2021 927df6b7 2019-02-10 stsp goto done;
2022 927df6b7 2019-02-10 stsp if (argc <= 1) {
2023 927df6b7 2019-02-10 stsp if (worktree == NULL) {
2024 927df6b7 2019-02-10 stsp error = got_error(GOT_ERR_NOT_WORKTREE);
2025 927df6b7 2019-02-10 stsp goto done;
2027 b72f483a 2019-02-05 stsp if (repo_path)
2029 b72f483a 2019-02-05 stsp "-r option can't be used when diffing a work tree");
2030 b72f483a 2019-02-05 stsp repo_path = strdup(got_worktree_get_repo_path(worktree));
2031 927df6b7 2019-02-10 stsp if (repo_path == NULL) {
2032 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2033 927df6b7 2019-02-10 stsp goto done;
2035 927df6b7 2019-02-10 stsp if (argc == 1) {
2036 6c7ab921 2019-03-18 stsp error = got_worktree_resolve_path(&path, worktree,
2038 927df6b7 2019-02-10 stsp if (error)
2039 927df6b7 2019-02-10 stsp goto done;
2041 927df6b7 2019-02-10 stsp path = strdup("");
2042 927df6b7 2019-02-10 stsp if (path == NULL) {
2043 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2044 927df6b7 2019-02-10 stsp goto done;
2047 b72f483a 2019-02-05 stsp } else if (argc == 2) {
2048 98eaaa12 2019-08-03 stsp if (diff_staged)
2049 98eaaa12 2019-08-03 stsp errx(1, "-s option can't be used when diffing "
2050 98eaaa12 2019-08-03 stsp "objects in repository");
2051 15a94983 2018-12-23 stsp id_str1 = argv[0];
2052 15a94983 2018-12-23 stsp id_str2 = argv[1];
2053 30db809c 2019-06-05 stsp if (worktree && repo_path == NULL) {
2054 30db809c 2019-06-05 stsp repo_path =
2055 30db809c 2019-06-05 stsp strdup(got_worktree_get_repo_path(worktree));
2056 30db809c 2019-06-05 stsp if (repo_path == NULL) {
2057 30db809c 2019-06-05 stsp error = got_error_from_errno("strdup");
2058 30db809c 2019-06-05 stsp goto done;
2062 b00d56cd 2018-04-01 stsp usage_diff();
2064 b72f483a 2019-02-05 stsp if (repo_path == NULL) {
2065 b72f483a 2019-02-05 stsp repo_path = getcwd(NULL, 0);
2066 b72f483a 2019-02-05 stsp if (repo_path == NULL)
2067 638f9024 2019-05-13 stsp return got_error_from_errno("getcwd");
2070 b00d56cd 2018-04-01 stsp error = got_repo_open(&repo, repo_path);
2071 76089277 2018-04-01 stsp free(repo_path);
2072 b00d56cd 2018-04-01 stsp if (error != NULL)
2073 b00d56cd 2018-04-01 stsp goto done;
2075 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), 1,
2076 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
2077 c02c541e 2019-03-29 stsp if (error)
2078 c02c541e 2019-03-29 stsp goto done;
2080 30db809c 2019-06-05 stsp if (argc <= 1) {
2081 b72f483a 2019-02-05 stsp struct print_diff_arg arg;
2082 72ea6654 2019-07-27 stsp struct got_pathlist_head paths;
2083 b72f483a 2019-02-05 stsp char *id_str;
2085 72ea6654 2019-07-27 stsp TAILQ_INIT(&paths);
2087 b72f483a 2019-02-05 stsp error = got_object_id_str(&id_str,
2088 b72f483a 2019-02-05 stsp got_worktree_get_base_commit_id(worktree));
2089 b72f483a 2019-02-05 stsp if (error)
2090 b72f483a 2019-02-05 stsp goto done;
2091 b72f483a 2019-02-05 stsp arg.repo = repo;
2092 b72f483a 2019-02-05 stsp arg.worktree = worktree;
2093 b72f483a 2019-02-05 stsp arg.diff_context = diff_context;
2094 3fc0c068 2019-02-10 stsp arg.id_str = id_str;
2095 3fc0c068 2019-02-10 stsp arg.header_shown = 0;
2096 98eaaa12 2019-08-03 stsp arg.diff_staged = diff_staged;
2098 adc19d55 2019-07-28 stsp error = got_pathlist_append(&paths, path, NULL);
2099 72ea6654 2019-07-27 stsp if (error)
2100 72ea6654 2019-07-27 stsp goto done;
2102 72ea6654 2019-07-27 stsp error = got_worktree_status(worktree, &paths, repo, print_diff,
2103 b72f483a 2019-02-05 stsp &arg, check_cancelled, NULL);
2104 b72f483a 2019-02-05 stsp free(id_str);
2105 72ea6654 2019-07-27 stsp got_pathlist_free(&paths);
2106 b72f483a 2019-02-05 stsp goto done;
2109 01073a5d 2019-08-22 stsp error = match_object_id(&id1, &label1, id_str1, GOT_OBJ_TYPE_ANY, 1,
2111 0adb7196 2019-08-11 stsp if (error)
2112 0adb7196 2019-08-11 stsp goto done;
2114 01073a5d 2019-08-22 stsp error = match_object_id(&id2, &label2, id_str2, GOT_OBJ_TYPE_ANY, 1,
2116 0adb7196 2019-08-11 stsp if (error)
2117 0adb7196 2019-08-11 stsp goto done;
2119 15a94983 2018-12-23 stsp error = got_object_get_type(&type1, repo, id1);
2120 15a94983 2018-12-23 stsp if (error)
2121 15a94983 2018-12-23 stsp goto done;
2123 15a94983 2018-12-23 stsp error = got_object_get_type(&type2, repo, id2);
2124 15a94983 2018-12-23 stsp if (error)
2125 15a94983 2018-12-23 stsp goto done;
2127 15a94983 2018-12-23 stsp if (type1 != type2) {
2128 b00d56cd 2018-04-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
2129 b00d56cd 2018-04-01 stsp goto done;
2132 15a94983 2018-12-23 stsp switch (type1) {
2133 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_BLOB:
2134 15a94983 2018-12-23 stsp error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
2135 54156555 2018-12-24 stsp diff_context, repo, stdout);
2137 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_TREE:
2138 15a94983 2018-12-23 stsp error = got_diff_objects_as_trees(id1, id2, "", "",
2139 54156555 2018-12-24 stsp diff_context, repo, stdout);
2141 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_COMMIT:
2142 5e70831e 2019-05-28 stsp printf("diff %s %s\n", label1, label2);
2143 15a94983 2018-12-23 stsp error = got_diff_objects_as_commits(id1, id2, diff_context,
2144 c0cc5c62 2018-10-18 stsp repo, stdout);
2147 b00d56cd 2018-04-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
2151 5e70831e 2019-05-28 stsp free(label1);
2152 5e70831e 2019-05-28 stsp free(label2);
2153 15a94983 2018-12-23 stsp free(id1);
2154 15a94983 2018-12-23 stsp free(id2);
2155 927df6b7 2019-02-10 stsp free(path);
2156 b72f483a 2019-02-05 stsp if (worktree)
2157 b72f483a 2019-02-05 stsp got_worktree_close(worktree);
2158 ad242220 2018-09-08 stsp if (repo) {
2159 ad242220 2018-09-08 stsp const struct got_error *repo_error;
2160 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
2161 ad242220 2018-09-08 stsp if (error == NULL)
2162 ad242220 2018-09-08 stsp error = repo_error;
2164 b00d56cd 2018-04-01 stsp return error;
2167 404c43c4 2018-06-21 stsp __dead static void
2168 404c43c4 2018-06-21 stsp usage_blame(void)
2170 9270e621 2019-02-05 stsp fprintf(stderr,
2171 9270e621 2019-02-05 stsp "usage: %s blame [-c commit] [-r repository-path] path\n",
2172 404c43c4 2018-06-21 stsp getprogname());
2176 28315671 2019-08-14 stsp struct blame_line {
2177 28315671 2019-08-14 stsp int annotated;
2178 28315671 2019-08-14 stsp char *id_str;
2179 82f6abb8 2019-08-14 stsp char *committer;
2180 bcb49d15 2019-08-14 stsp char datebuf[9]; /* YY-MM-DD + NUL */
2183 28315671 2019-08-14 stsp struct blame_cb_args {
2184 28315671 2019-08-14 stsp struct blame_line *lines;
2185 28315671 2019-08-14 stsp int nlines;
2186 7ef28ff8 2019-08-14 stsp int nlines_prec;
2187 28315671 2019-08-14 stsp int lineno_cur;
2188 28315671 2019-08-14 stsp off_t *line_offsets;
2190 82f6abb8 2019-08-14 stsp struct got_repository *repo;
2193 404c43c4 2018-06-21 stsp static const struct got_error *
2194 28315671 2019-08-14 stsp blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2196 28315671 2019-08-14 stsp const struct got_error *err = NULL;
2197 28315671 2019-08-14 stsp struct blame_cb_args *a = arg;
2198 28315671 2019-08-14 stsp struct blame_line *bline;
2199 82f6abb8 2019-08-14 stsp char *line = NULL;
2200 28315671 2019-08-14 stsp size_t linesize = 0;
2201 82f6abb8 2019-08-14 stsp struct got_commit_object *commit = NULL;
2202 28315671 2019-08-14 stsp off_t offset;
2203 bcb49d15 2019-08-14 stsp struct tm tm;
2204 bcb49d15 2019-08-14 stsp time_t committer_time;
2206 28315671 2019-08-14 stsp if (nlines != a->nlines ||
2207 28315671 2019-08-14 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
2208 28315671 2019-08-14 stsp return got_error(GOT_ERR_RANGE);
2210 28315671 2019-08-14 stsp if (sigint_received)
2211 28315671 2019-08-14 stsp return got_error(GOT_ERR_ITER_COMPLETED);
2213 2839f8b9 2019-08-18 stsp if (lineno == -1)
2214 2839f8b9 2019-08-18 stsp return NULL; /* no change in this commit */
2216 28315671 2019-08-14 stsp /* Annotate this line. */
2217 28315671 2019-08-14 stsp bline = &a->lines[lineno - 1];
2218 28315671 2019-08-14 stsp if (bline->annotated)
2219 28315671 2019-08-14 stsp return NULL;
2220 28315671 2019-08-14 stsp err = got_object_id_str(&bline->id_str, id);
2222 28315671 2019-08-14 stsp return err;
2224 82f6abb8 2019-08-14 stsp err = got_object_open_as_commit(&commit, a->repo, id);
2226 82f6abb8 2019-08-14 stsp goto done;
2228 82f6abb8 2019-08-14 stsp bline->committer = strdup(got_object_commit_get_committer(commit));
2229 82f6abb8 2019-08-14 stsp if (bline->committer == NULL) {
2230 82f6abb8 2019-08-14 stsp err = got_error_from_errno("strdup");
2231 bcb49d15 2019-08-14 stsp goto done;
2234 bcb49d15 2019-08-14 stsp committer_time = got_object_commit_get_committer_time(commit);
2235 bcb49d15 2019-08-14 stsp if (localtime_r(&committer_time, &tm) == NULL)
2236 bcb49d15 2019-08-14 stsp return got_error_from_errno("localtime_r");
2237 bcb49d15 2019-08-14 stsp if (strftime(bline->datebuf, sizeof(bline->datebuf), "%g/%m/%d",
2238 bcb49d15 2019-08-14 stsp &tm) >= sizeof(bline->datebuf)) {
2239 bcb49d15 2019-08-14 stsp err = got_error(GOT_ERR_NO_SPACE);
2240 82f6abb8 2019-08-14 stsp goto done;
2242 28315671 2019-08-14 stsp bline->annotated = 1;
2244 28315671 2019-08-14 stsp /* Print lines annotated so far. */
2245 28315671 2019-08-14 stsp bline = &a->lines[a->lineno_cur - 1];
2246 28315671 2019-08-14 stsp if (!bline->annotated)
2247 82f6abb8 2019-08-14 stsp goto done;
2249 28315671 2019-08-14 stsp offset = a->line_offsets[a->lineno_cur - 1];
2250 82f6abb8 2019-08-14 stsp if (fseeko(a->f, offset, SEEK_SET) == -1) {
2251 82f6abb8 2019-08-14 stsp err = got_error_from_errno("fseeko");
2252 82f6abb8 2019-08-14 stsp goto done;
2255 28315671 2019-08-14 stsp while (bline->annotated) {
2256 82f6abb8 2019-08-14 stsp char *smallerthan, *at, *nl, *committer;
2257 82f6abb8 2019-08-14 stsp size_t len;
2259 28315671 2019-08-14 stsp if (getline(&line, &linesize, a->f) == (ssize_t)-1) {
2260 28315671 2019-08-14 stsp if (ferror(a->f))
2261 28315671 2019-08-14 stsp err = got_error_from_errno("getline");
2265 82f6abb8 2019-08-14 stsp committer = bline->committer;
2266 82f6abb8 2019-08-14 stsp smallerthan = strchr(committer, '<');
2267 82f6abb8 2019-08-14 stsp if (smallerthan && smallerthan[1] != '\0')
2268 82f6abb8 2019-08-14 stsp committer = smallerthan + 1;
2269 82f6abb8 2019-08-14 stsp at = strchr(committer, '@');
2271 82f6abb8 2019-08-14 stsp *at = '\0';
2272 82f6abb8 2019-08-14 stsp len = strlen(committer);
2273 82f6abb8 2019-08-14 stsp if (len >= 9)
2274 82f6abb8 2019-08-14 stsp committer[8] = '\0';
2276 28315671 2019-08-14 stsp nl = strchr(line, '\n');
2278 28315671 2019-08-14 stsp *nl = '\0';
2279 bcb49d15 2019-08-14 stsp printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
2280 bcb49d15 2019-08-14 stsp bline->id_str, bline->datebuf, committer, line);
2282 28315671 2019-08-14 stsp a->lineno_cur++;
2283 28315671 2019-08-14 stsp bline = &a->lines[a->lineno_cur - 1];
2286 82f6abb8 2019-08-14 stsp if (commit)
2287 82f6abb8 2019-08-14 stsp got_object_commit_close(commit);
2288 28315671 2019-08-14 stsp free(line);
2289 28315671 2019-08-14 stsp return err;
2292 28315671 2019-08-14 stsp static const struct got_error *
2293 404c43c4 2018-06-21 stsp cmd_blame(int argc, char *argv[])
2295 404c43c4 2018-06-21 stsp const struct got_error *error;
2296 404c43c4 2018-06-21 stsp struct got_repository *repo = NULL;
2297 0c06baac 2019-02-05 stsp struct got_worktree *worktree = NULL;
2298 66bea077 2018-08-02 stsp char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2299 28315671 2019-08-14 stsp struct got_object_id *obj_id = NULL;
2300 404c43c4 2018-06-21 stsp struct got_object_id *commit_id = NULL;
2301 28315671 2019-08-14 stsp struct got_blob_object *blob = NULL;
2302 404c43c4 2018-06-21 stsp char *commit_id_str = NULL;
2303 28315671 2019-08-14 stsp struct blame_cb_args bca;
2304 28315671 2019-08-14 stsp int ch, obj_type, i;
2305 b02560ec 2019-08-19 stsp size_t filesize;
2307 90f3c347 2019-08-19 stsp memset(&bca, 0, sizeof(bca));
2309 404c43c4 2018-06-21 stsp #ifndef PROFILE
2310 36e2fb66 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2311 36e2fb66 2019-01-04 stsp NULL) == -1)
2312 404c43c4 2018-06-21 stsp err(1, "pledge");
2315 66bea077 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2316 404c43c4 2018-06-21 stsp switch (ch) {
2318 404c43c4 2018-06-21 stsp commit_id_str = optarg;
2321 66bea077 2018-08-02 stsp repo_path = realpath(optarg, NULL);
2322 66bea077 2018-08-02 stsp if (repo_path == NULL)
2323 66bea077 2018-08-02 stsp err(1, "-r option");
2324 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
2327 2deda0b9 2019-03-07 stsp usage_blame();
2328 404c43c4 2018-06-21 stsp /* NOTREACHED */
2332 404c43c4 2018-06-21 stsp argc -= optind;
2333 404c43c4 2018-06-21 stsp argv += optind;
2335 a39318fd 2018-08-02 stsp if (argc == 1)
2336 404c43c4 2018-06-21 stsp path = argv[0];
2338 404c43c4 2018-06-21 stsp usage_blame();
2340 66bea077 2018-08-02 stsp cwd = getcwd(NULL, 0);
2341 66bea077 2018-08-02 stsp if (cwd == NULL) {
2342 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2343 66bea077 2018-08-02 stsp goto done;
2345 66bea077 2018-08-02 stsp if (repo_path == NULL) {
2346 0c06baac 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
2347 0c06baac 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
2348 66bea077 2018-08-02 stsp goto done;
2350 0c06baac 2019-02-05 stsp error = NULL;
2351 0c06baac 2019-02-05 stsp if (worktree) {
2352 0c06baac 2019-02-05 stsp repo_path =
2353 0c06baac 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
2354 0c06baac 2019-02-05 stsp if (repo_path == NULL)
2355 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2356 0c06baac 2019-02-05 stsp if (error)
2357 0c06baac 2019-02-05 stsp goto done;
2359 0c06baac 2019-02-05 stsp repo_path = strdup(cwd);
2360 0c06baac 2019-02-05 stsp if (repo_path == NULL) {
2361 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2362 0c06baac 2019-02-05 stsp goto done;
2367 c02c541e 2019-03-29 stsp error = got_repo_open(&repo, repo_path);
2368 c02c541e 2019-03-29 stsp if (error != NULL)
2369 36e2fb66 2019-01-04 stsp goto done;
2371 c530dc23 2019-07-23 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2372 c02c541e 2019-03-29 stsp if (error)
2373 404c43c4 2018-06-21 stsp goto done;
2375 0c06baac 2019-02-05 stsp if (worktree) {
2376 6efaaa2d 2019-02-05 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
2377 0c06baac 2019-02-05 stsp char *p, *worktree_subdir = cwd +
2378 0c06baac 2019-02-05 stsp strlen(got_worktree_get_root_path(worktree));
2379 6efaaa2d 2019-02-05 stsp if (asprintf(&p, "%s%s%s%s%s",
2380 6efaaa2d 2019-02-05 stsp prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
2381 6efaaa2d 2019-02-05 stsp worktree_subdir, worktree_subdir[0] ? "/" : "",
2382 6efaaa2d 2019-02-05 stsp path) == -1) {
2383 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
2384 0c06baac 2019-02-05 stsp goto done;
2386 6efaaa2d 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, p, 0);
2389 0c06baac 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
2391 0c06baac 2019-02-05 stsp if (error)
2392 66bea077 2018-08-02 stsp goto done;
2394 404c43c4 2018-06-21 stsp if (commit_id_str == NULL) {
2395 404c43c4 2018-06-21 stsp struct got_reference *head_ref;
2396 2f17228e 2019-05-12 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
2397 404c43c4 2018-06-21 stsp if (error != NULL)
2398 66bea077 2018-08-02 stsp goto done;
2399 404c43c4 2018-06-21 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
2400 404c43c4 2018-06-21 stsp got_ref_close(head_ref);
2401 404c43c4 2018-06-21 stsp if (error != NULL)
2402 66bea077 2018-08-02 stsp goto done;
2404 04f57cb3 2019-07-25 stsp error = resolve_commit_arg(&commit_id, commit_id_str, repo);
2405 30837e32 2019-07-25 stsp if (error)
2406 66bea077 2018-08-02 stsp goto done;
2409 28315671 2019-08-14 stsp error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
2410 28315671 2019-08-14 stsp if (error)
2411 28315671 2019-08-14 stsp goto done;
2412 28315671 2019-08-14 stsp if (obj_id == NULL) {
2413 28315671 2019-08-14 stsp error = got_error(GOT_ERR_NO_OBJ);
2414 28315671 2019-08-14 stsp goto done;
2417 28315671 2019-08-14 stsp error = got_object_get_type(&obj_type, repo, obj_id);
2418 28315671 2019-08-14 stsp if (error)
2419 28315671 2019-08-14 stsp goto done;
2421 28315671 2019-08-14 stsp if (obj_type != GOT_OBJ_TYPE_BLOB) {
2422 28315671 2019-08-14 stsp error = got_error(GOT_ERR_OBJ_TYPE);
2423 28315671 2019-08-14 stsp goto done;
2426 28315671 2019-08-14 stsp error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
2427 28315671 2019-08-14 stsp if (error)
2428 28315671 2019-08-14 stsp goto done;
2429 28315671 2019-08-14 stsp bca.f = got_opentemp();
2430 28315671 2019-08-14 stsp if (bca.f == NULL) {
2431 28315671 2019-08-14 stsp error = got_error_from_errno("got_opentemp");
2432 28315671 2019-08-14 stsp goto done;
2434 b02560ec 2019-08-19 stsp error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
2435 28315671 2019-08-14 stsp &bca.line_offsets, bca.f, blob);
2436 7ef28ff8 2019-08-14 stsp if (error || bca.nlines == 0)
2437 28315671 2019-08-14 stsp goto done;
2439 b02560ec 2019-08-19 stsp /* Don't include \n at EOF in the blame line count. */
2440 b02560ec 2019-08-19 stsp if (bca.line_offsets[bca.nlines - 1] == filesize)
2441 b02560ec 2019-08-19 stsp bca.nlines--;
2443 28315671 2019-08-14 stsp bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
2444 28315671 2019-08-14 stsp if (bca.lines == NULL) {
2445 28315671 2019-08-14 stsp error = got_error_from_errno("calloc");
2446 28315671 2019-08-14 stsp goto done;
2448 28315671 2019-08-14 stsp bca.lineno_cur = 1;
2449 7ef28ff8 2019-08-14 stsp bca.nlines_prec = 0;
2450 7ef28ff8 2019-08-14 stsp i = bca.nlines;
2451 7ef28ff8 2019-08-14 stsp while (i > 0) {
2453 7ef28ff8 2019-08-14 stsp bca.nlines_prec++;
2455 82f6abb8 2019-08-14 stsp bca.repo = repo;
2457 6fb7cd11 2019-08-22 stsp error = got_blame(in_repo_path, commit_id, repo, blame_cb, &bca,
2458 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
2459 28315671 2019-08-14 stsp if (error)
2460 28315671 2019-08-14 stsp goto done;
2462 66bea077 2018-08-02 stsp free(in_repo_path);
2463 66bea077 2018-08-02 stsp free(repo_path);
2464 66bea077 2018-08-02 stsp free(cwd);
2465 404c43c4 2018-06-21 stsp free(commit_id);
2466 28315671 2019-08-14 stsp free(obj_id);
2468 28315671 2019-08-14 stsp got_object_blob_close(blob);
2469 0c06baac 2019-02-05 stsp if (worktree)
2470 0c06baac 2019-02-05 stsp got_worktree_close(worktree);
2471 ad242220 2018-09-08 stsp if (repo) {
2472 ad242220 2018-09-08 stsp const struct got_error *repo_error;
2473 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
2474 ad242220 2018-09-08 stsp if (error == NULL)
2475 ad242220 2018-09-08 stsp error = repo_error;
2477 28315671 2019-08-14 stsp for (i = 0; i < bca.nlines; i++) {
2478 28315671 2019-08-14 stsp struct blame_line *bline = &bca.lines[i];
2479 28315671 2019-08-14 stsp free(bline->id_str);
2480 82f6abb8 2019-08-14 stsp free(bline->committer);
2482 28315671 2019-08-14 stsp free(bca.lines);
2483 28315671 2019-08-14 stsp free(bca.line_offsets);
2484 28315671 2019-08-14 stsp if (bca.f && fclose(bca.f) == EOF && error == NULL)
2485 28315671 2019-08-14 stsp error = got_error_from_errno("fclose");
2486 404c43c4 2018-06-21 stsp return error;
2489 5de5890b 2018-10-18 stsp __dead static void
2490 5de5890b 2018-10-18 stsp usage_tree(void)
2492 c1669e2e 2019-01-09 stsp fprintf(stderr,
2493 c1669e2e 2019-01-09 stsp "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
2494 5de5890b 2018-10-18 stsp getprogname());
2498 c1669e2e 2019-01-09 stsp static void
2499 c1669e2e 2019-01-09 stsp print_entry(struct got_tree_entry *te, const char *id, const char *path,
2500 c1669e2e 2019-01-09 stsp const char *root_path)
2502 c1669e2e 2019-01-09 stsp int is_root_path = (strcmp(path, root_path) == 0);
2503 848d6979 2019-08-12 stsp const char *modestr = "";
2505 c1669e2e 2019-01-09 stsp path += strlen(root_path);
2506 c1669e2e 2019-01-09 stsp while (path[0] == '/')
2509 848d6979 2019-08-12 stsp if (S_ISLNK(te->mode))
2510 848d6979 2019-08-12 stsp modestr = "@";
2511 848d6979 2019-08-12 stsp else if (S_ISDIR(te->mode))
2512 848d6979 2019-08-12 stsp modestr = "/";
2513 848d6979 2019-08-12 stsp else if (te->mode & S_IXUSR)
2514 848d6979 2019-08-12 stsp modestr = "*";
2516 c1669e2e 2019-01-09 stsp printf("%s%s%s%s%s\n", id ? id : "", path,
2517 848d6979 2019-08-12 stsp is_root_path ? "" : "/", te->name, modestr);
2520 5de5890b 2018-10-18 stsp static const struct got_error *
2521 5de5890b 2018-10-18 stsp print_tree(const char *path, struct got_object_id *commit_id,
2522 c1669e2e 2019-01-09 stsp int show_ids, int recurse, const char *root_path,
2523 c1669e2e 2019-01-09 stsp struct got_repository *repo)
2525 5de5890b 2018-10-18 stsp const struct got_error *err = NULL;
2526 5de5890b 2018-10-18 stsp struct got_object_id *tree_id = NULL;
2527 5de5890b 2018-10-18 stsp struct got_tree_object *tree = NULL;
2528 5de5890b 2018-10-18 stsp const struct got_tree_entries *entries;
2529 5de5890b 2018-10-18 stsp struct got_tree_entry *te;
2531 5de5890b 2018-10-18 stsp err = got_object_id_by_path(&tree_id, repo, commit_id, path);
2533 5de5890b 2018-10-18 stsp goto done;
2535 5de5890b 2018-10-18 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
2537 5de5890b 2018-10-18 stsp goto done;
2538 5de5890b 2018-10-18 stsp entries = got_object_tree_get_entries(tree);
2539 5de5890b 2018-10-18 stsp te = SIMPLEQ_FIRST(&entries->head);
2540 5de5890b 2018-10-18 stsp while (te) {
2541 5de5890b 2018-10-18 stsp char *id = NULL;
2543 84453469 2018-11-11 stsp if (sigint_received || sigpipe_received)
2546 5de5890b 2018-10-18 stsp if (show_ids) {
2547 5de5890b 2018-10-18 stsp char *id_str;
2548 5de5890b 2018-10-18 stsp err = got_object_id_str(&id_str, te->id);
2550 5de5890b 2018-10-18 stsp goto done;
2551 5de5890b 2018-10-18 stsp if (asprintf(&id, "%s ", id_str) == -1) {
2552 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2553 5de5890b 2018-10-18 stsp free(id_str);
2554 5de5890b 2018-10-18 stsp goto done;
2556 5de5890b 2018-10-18 stsp free(id_str);
2558 c1669e2e 2019-01-09 stsp print_entry(te, id, path, root_path);
2561 c1669e2e 2019-01-09 stsp if (recurse && S_ISDIR(te->mode)) {
2562 c1669e2e 2019-01-09 stsp char *child_path;
2563 c1669e2e 2019-01-09 stsp if (asprintf(&child_path, "%s%s%s", path,
2564 c1669e2e 2019-01-09 stsp path[0] == '/' && path[1] == '\0' ? "" : "/",
2565 c1669e2e 2019-01-09 stsp te->name) == -1) {
2566 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2567 c1669e2e 2019-01-09 stsp goto done;
2569 c1669e2e 2019-01-09 stsp err = print_tree(child_path, commit_id, show_ids, 1,
2570 c1669e2e 2019-01-09 stsp root_path, repo);
2571 c1669e2e 2019-01-09 stsp free(child_path);
2573 c1669e2e 2019-01-09 stsp goto done;
2576 c1669e2e 2019-01-09 stsp te = SIMPLEQ_NEXT(te, entry);
2580 5de5890b 2018-10-18 stsp got_object_tree_close(tree);
2581 5de5890b 2018-10-18 stsp free(tree_id);
2582 5de5890b 2018-10-18 stsp return err;
2585 5de5890b 2018-10-18 stsp static const struct got_error *
2586 5de5890b 2018-10-18 stsp cmd_tree(int argc, char *argv[])
2588 5de5890b 2018-10-18 stsp const struct got_error *error;
2589 5de5890b 2018-10-18 stsp struct got_repository *repo = NULL;
2590 7a2c19d6 2019-02-05 stsp struct got_worktree *worktree = NULL;
2591 7a2c19d6 2019-02-05 stsp const char *path;
2592 7a2c19d6 2019-02-05 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2593 5de5890b 2018-10-18 stsp struct got_object_id *commit_id = NULL;
2594 5de5890b 2018-10-18 stsp char *commit_id_str = NULL;
2595 c1669e2e 2019-01-09 stsp int show_ids = 0, recurse = 0;
2598 5de5890b 2018-10-18 stsp #ifndef PROFILE
2599 0f8d269b 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2600 0f8d269b 2019-01-04 stsp NULL) == -1)
2601 5de5890b 2018-10-18 stsp err(1, "pledge");
2604 c1669e2e 2019-01-09 stsp while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
2605 5de5890b 2018-10-18 stsp switch (ch) {
2607 5de5890b 2018-10-18 stsp commit_id_str = optarg;
2610 5de5890b 2018-10-18 stsp repo_path = realpath(optarg, NULL);
2611 5de5890b 2018-10-18 stsp if (repo_path == NULL)
2612 5de5890b 2018-10-18 stsp err(1, "-r option");
2613 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
2616 5de5890b 2018-10-18 stsp show_ids = 1;
2619 c1669e2e 2019-01-09 stsp recurse = 1;
2622 2deda0b9 2019-03-07 stsp usage_tree();
2623 5de5890b 2018-10-18 stsp /* NOTREACHED */
2627 5de5890b 2018-10-18 stsp argc -= optind;
2628 5de5890b 2018-10-18 stsp argv += optind;
2630 5de5890b 2018-10-18 stsp if (argc == 1)
2631 5de5890b 2018-10-18 stsp path = argv[0];
2632 5de5890b 2018-10-18 stsp else if (argc > 1)
2633 5de5890b 2018-10-18 stsp usage_tree();
2635 7a2c19d6 2019-02-05 stsp path = NULL;
2637 9bf7a39b 2019-02-05 stsp cwd = getcwd(NULL, 0);
2638 9bf7a39b 2019-02-05 stsp if (cwd == NULL) {
2639 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2640 9bf7a39b 2019-02-05 stsp goto done;
2642 5de5890b 2018-10-18 stsp if (repo_path == NULL) {
2643 7a2c19d6 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
2644 8994de28 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
2645 7a2c19d6 2019-02-05 stsp goto done;
2647 7a2c19d6 2019-02-05 stsp error = NULL;
2648 7a2c19d6 2019-02-05 stsp if (worktree) {
2649 7a2c19d6 2019-02-05 stsp repo_path =
2650 7a2c19d6 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
2651 7a2c19d6 2019-02-05 stsp if (repo_path == NULL)
2652 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2653 7a2c19d6 2019-02-05 stsp if (error)
2654 7a2c19d6 2019-02-05 stsp goto done;
2656 7a2c19d6 2019-02-05 stsp repo_path = strdup(cwd);
2657 7a2c19d6 2019-02-05 stsp if (repo_path == NULL) {
2658 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2659 7a2c19d6 2019-02-05 stsp goto done;
2664 5de5890b 2018-10-18 stsp error = got_repo_open(&repo, repo_path);
2665 5de5890b 2018-10-18 stsp if (error != NULL)
2666 c02c541e 2019-03-29 stsp goto done;
2668 c530dc23 2019-07-23 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2669 c02c541e 2019-03-29 stsp if (error)
2670 5de5890b 2018-10-18 stsp goto done;
2672 9bf7a39b 2019-02-05 stsp if (path == NULL) {
2673 9bf7a39b 2019-02-05 stsp if (worktree) {
2674 9bf7a39b 2019-02-05 stsp char *p, *worktree_subdir = cwd +
2675 9bf7a39b 2019-02-05 stsp strlen(got_worktree_get_root_path(worktree));
2676 9bf7a39b 2019-02-05 stsp if (asprintf(&p, "%s/%s",
2677 9bf7a39b 2019-02-05 stsp got_worktree_get_path_prefix(worktree),
2678 9bf7a39b 2019-02-05 stsp worktree_subdir) == -1) {
2679 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
2680 9bf7a39b 2019-02-05 stsp goto done;
2682 9bf7a39b 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, p, 1);
2684 9bf7a39b 2019-02-05 stsp if (error)
2685 9bf7a39b 2019-02-05 stsp goto done;
2687 9bf7a39b 2019-02-05 stsp path = "/";
2689 9bf7a39b 2019-02-05 stsp if (in_repo_path == NULL) {
2690 9bf7a39b 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
2691 9bf7a39b 2019-02-05 stsp if (error != NULL)
2692 9bf7a39b 2019-02-05 stsp goto done;
2695 5de5890b 2018-10-18 stsp if (commit_id_str == NULL) {
2696 5de5890b 2018-10-18 stsp struct got_reference *head_ref;
2697 2f17228e 2019-05-12 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
2698 5de5890b 2018-10-18 stsp if (error != NULL)
2699 5de5890b 2018-10-18 stsp goto done;
2700 5de5890b 2018-10-18 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
2701 5de5890b 2018-10-18 stsp got_ref_close(head_ref);
2702 5de5890b 2018-10-18 stsp if (error != NULL)
2703 5de5890b 2018-10-18 stsp goto done;
2705 04f57cb3 2019-07-25 stsp error = resolve_commit_arg(&commit_id, commit_id_str, repo);
2706 30837e32 2019-07-25 stsp if (error)
2707 5de5890b 2018-10-18 stsp goto done;
2710 c1669e2e 2019-01-09 stsp error = print_tree(in_repo_path, commit_id, show_ids, recurse,
2711 c1669e2e 2019-01-09 stsp in_repo_path, repo);
2713 5de5890b 2018-10-18 stsp free(in_repo_path);
2714 5de5890b 2018-10-18 stsp free(repo_path);
2715 5de5890b 2018-10-18 stsp free(cwd);
2716 5de5890b 2018-10-18 stsp free(commit_id);
2717 7a2c19d6 2019-02-05 stsp if (worktree)
2718 7a2c19d6 2019-02-05 stsp got_worktree_close(worktree);
2719 5de5890b 2018-10-18 stsp if (repo) {
2720 5de5890b 2018-10-18 stsp const struct got_error *repo_error;
2721 5de5890b 2018-10-18 stsp repo_error = got_repo_close(repo);
2722 5de5890b 2018-10-18 stsp if (error == NULL)
2723 5de5890b 2018-10-18 stsp error = repo_error;
2725 5de5890b 2018-10-18 stsp return error;
2728 6bad629b 2019-02-04 stsp __dead static void
2729 6bad629b 2019-02-04 stsp usage_status(void)
2731 72ea6654 2019-07-27 stsp fprintf(stderr, "usage: %s status [path ...]\n", getprogname());
2735 b72f483a 2019-02-05 stsp static const struct got_error *
2736 88d0e355 2019-08-03 stsp print_status(void *arg, unsigned char status, unsigned char staged_status,
2737 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
2738 537ac44b 2019-08-03 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
2740 244725f2 2019-08-03 stsp if (status == staged_status && (status == GOT_STATUS_DELETE))
2741 c363b2c1 2019-08-03 stsp status = GOT_STATUS_NO_CHANGE;
2742 88d0e355 2019-08-03 stsp printf("%c%c %s\n", status, staged_status, path);
2743 b72f483a 2019-02-05 stsp return NULL;
2746 6bad629b 2019-02-04 stsp static const struct got_error *
2747 6bad629b 2019-02-04 stsp cmd_status(int argc, char *argv[])
2749 6bad629b 2019-02-04 stsp const struct got_error *error = NULL;
2750 6bad629b 2019-02-04 stsp struct got_repository *repo = NULL;
2751 6bad629b 2019-02-04 stsp struct got_worktree *worktree = NULL;
2752 f86a1bf5 2019-07-27 stsp char *cwd = NULL;
2753 72ea6654 2019-07-27 stsp struct got_pathlist_head paths;
2754 a5edda0a 2019-07-27 stsp struct got_pathlist_entry *pe;
2757 72ea6654 2019-07-27 stsp TAILQ_INIT(&paths);
2759 6bad629b 2019-02-04 stsp while ((ch = getopt(argc, argv, "")) != -1) {
2760 6bad629b 2019-02-04 stsp switch (ch) {
2762 2deda0b9 2019-03-07 stsp usage_status();
2763 6bad629b 2019-02-04 stsp /* NOTREACHED */
2767 6bad629b 2019-02-04 stsp argc -= optind;
2768 6bad629b 2019-02-04 stsp argv += optind;
2770 6bad629b 2019-02-04 stsp #ifndef PROFILE
2771 6bad629b 2019-02-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2772 6bad629b 2019-02-04 stsp NULL) == -1)
2773 6bad629b 2019-02-04 stsp err(1, "pledge");
2775 927df6b7 2019-02-10 stsp cwd = getcwd(NULL, 0);
2776 927df6b7 2019-02-10 stsp if (cwd == NULL) {
2777 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2778 927df6b7 2019-02-10 stsp goto done;
2781 927df6b7 2019-02-10 stsp error = got_worktree_open(&worktree, cwd);
2782 927df6b7 2019-02-10 stsp if (error != NULL)
2783 a5edda0a 2019-07-27 stsp goto done;
2785 6bad629b 2019-02-04 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2786 6bad629b 2019-02-04 stsp if (error != NULL)
2787 6bad629b 2019-02-04 stsp goto done;
2789 d0eebce4 2019-03-11 stsp error = apply_unveil(got_repo_get_path(repo), 1,
2790 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
2791 087fb88c 2019-08-04 stsp if (error)
2792 087fb88c 2019-08-04 stsp goto done;
2794 087fb88c 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
2795 6bad629b 2019-02-04 stsp if (error)
2796 6bad629b 2019-02-04 stsp goto done;
2798 72ea6654 2019-07-27 stsp error = got_worktree_status(worktree, &paths, repo, print_status, NULL,
2799 6bad629b 2019-02-04 stsp check_cancelled, NULL);
2801 a5edda0a 2019-07-27 stsp TAILQ_FOREACH(pe, &paths, entry)
2802 a5edda0a 2019-07-27 stsp free((char *)pe->path);
2803 72ea6654 2019-07-27 stsp got_pathlist_free(&paths);
2804 927df6b7 2019-02-10 stsp free(cwd);
2805 d0eebce4 2019-03-11 stsp return error;
2808 d0eebce4 2019-03-11 stsp __dead static void
2809 d0eebce4 2019-03-11 stsp usage_ref(void)
2811 d0eebce4 2019-03-11 stsp fprintf(stderr,
2812 d1c1ae5f 2019-08-12 stsp "usage: %s ref [-r repository] -l | -d name | [-s] name target\n",
2813 d0eebce4 2019-03-11 stsp getprogname());
2817 d0eebce4 2019-03-11 stsp static const struct got_error *
2818 d0eebce4 2019-03-11 stsp list_refs(struct got_repository *repo)
2820 d0eebce4 2019-03-11 stsp static const struct got_error *err = NULL;
2821 d0eebce4 2019-03-11 stsp struct got_reflist_head refs;
2822 d0eebce4 2019-03-11 stsp struct got_reflist_entry *re;
2824 d0eebce4 2019-03-11 stsp SIMPLEQ_INIT(&refs);
2825 d0eebce4 2019-03-11 stsp err = got_ref_list(&refs, repo);
2827 d0eebce4 2019-03-11 stsp return err;
2829 d0eebce4 2019-03-11 stsp SIMPLEQ_FOREACH(re, &refs, entry) {
2830 d0eebce4 2019-03-11 stsp char *refstr;
2831 d0eebce4 2019-03-11 stsp refstr = got_ref_to_str(re->ref);
2832 d0eebce4 2019-03-11 stsp if (refstr == NULL)
2833 638f9024 2019-05-13 stsp return got_error_from_errno("got_ref_to_str");
2834 d0eebce4 2019-03-11 stsp printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
2835 d0eebce4 2019-03-11 stsp free(refstr);
2838 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
2839 d0eebce4 2019-03-11 stsp return NULL;
2842 d0eebce4 2019-03-11 stsp static const struct got_error *
2843 d0eebce4 2019-03-11 stsp delete_ref(struct got_repository *repo, const char *refname)
2845 d0eebce4 2019-03-11 stsp const struct got_error *err = NULL;
2846 d0eebce4 2019-03-11 stsp struct got_reference *ref;
2848 2f17228e 2019-05-12 stsp err = got_ref_open(&ref, repo, refname, 0);
2850 d0eebce4 2019-03-11 stsp return err;
2852 d0eebce4 2019-03-11 stsp err = got_ref_delete(ref, repo);
2853 d0eebce4 2019-03-11 stsp got_ref_close(ref);
2854 d0eebce4 2019-03-11 stsp return err;
2857 d0eebce4 2019-03-11 stsp static const struct got_error *
2858 d83d9d5c 2019-05-13 stsp add_ref(struct got_repository *repo, const char *refname, const char *target)
2860 d0eebce4 2019-03-11 stsp const struct got_error *err = NULL;
2861 d0eebce4 2019-03-11 stsp struct got_object_id *id;
2862 d0eebce4 2019-03-11 stsp struct got_reference *ref = NULL;
2865 d1644381 2019-07-14 stsp * Don't let the user create a reference named '-'.
2866 d1644381 2019-07-14 stsp * While technically a valid reference name, this case is usually
2867 d1644381 2019-07-14 stsp * an unintended typo.
2869 d1644381 2019-07-14 stsp if (refname[0] == '-' && refname[1] == '\0')
2870 d1644381 2019-07-14 stsp return got_error(GOT_ERR_BAD_REF_NAME);
2872 dd88155e 2019-06-29 stsp err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
2874 d83d9d5c 2019-05-13 stsp if (err) {
2875 d83d9d5c 2019-05-13 stsp struct got_reference *target_ref;
2877 d83d9d5c 2019-05-13 stsp if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
2878 d83d9d5c 2019-05-13 stsp return err;
2879 d83d9d5c 2019-05-13 stsp err = got_ref_open(&target_ref, repo, target, 0);
2881 d83d9d5c 2019-05-13 stsp return err;
2882 d83d9d5c 2019-05-13 stsp err = got_ref_resolve(&id, repo, target_ref);
2883 d83d9d5c 2019-05-13 stsp got_ref_close(target_ref);
2885 d83d9d5c 2019-05-13 stsp return err;
2888 d0eebce4 2019-03-11 stsp err = got_ref_alloc(&ref, refname, id);
2890 d0eebce4 2019-03-11 stsp goto done;
2892 d0eebce4 2019-03-11 stsp err = got_ref_write(ref, repo);
2895 d0eebce4 2019-03-11 stsp got_ref_close(ref);
2897 d1c1ae5f 2019-08-12 stsp return err;
2900 d1c1ae5f 2019-08-12 stsp static const struct got_error *
2901 d1c1ae5f 2019-08-12 stsp add_symref(struct got_repository *repo, const char *refname, const char *target)
2903 d1c1ae5f 2019-08-12 stsp const struct got_error *err = NULL;
2904 d1c1ae5f 2019-08-12 stsp struct got_reference *ref = NULL;
2905 d1c1ae5f 2019-08-12 stsp struct got_reference *target_ref = NULL;
2908 d1c1ae5f 2019-08-12 stsp * Don't let the user create a reference named '-'.
2909 d1c1ae5f 2019-08-12 stsp * While technically a valid reference name, this case is usually
2910 d1c1ae5f 2019-08-12 stsp * an unintended typo.
2912 d1c1ae5f 2019-08-12 stsp if (refname[0] == '-' && refname[1] == '\0')
2913 d1c1ae5f 2019-08-12 stsp return got_error(GOT_ERR_BAD_REF_NAME);
2915 d1c1ae5f 2019-08-12 stsp err = got_ref_open(&target_ref, repo, target, 0);
2917 d1c1ae5f 2019-08-12 stsp return err;
2919 d1c1ae5f 2019-08-12 stsp err = got_ref_alloc_symref(&ref, refname, target_ref);
2921 d1c1ae5f 2019-08-12 stsp goto done;
2923 d1c1ae5f 2019-08-12 stsp err = got_ref_write(ref, repo);
2925 d1c1ae5f 2019-08-12 stsp if (target_ref)
2926 d1c1ae5f 2019-08-12 stsp got_ref_close(target_ref);
2928 d1c1ae5f 2019-08-12 stsp got_ref_close(ref);
2929 d0eebce4 2019-03-11 stsp return err;
2932 d0eebce4 2019-03-11 stsp static const struct got_error *
2933 d0eebce4 2019-03-11 stsp cmd_ref(int argc, char *argv[])
2935 d0eebce4 2019-03-11 stsp const struct got_error *error = NULL;
2936 d0eebce4 2019-03-11 stsp struct got_repository *repo = NULL;
2937 d0eebce4 2019-03-11 stsp struct got_worktree *worktree = NULL;
2938 d0eebce4 2019-03-11 stsp char *cwd = NULL, *repo_path = NULL;
2939 d1c1ae5f 2019-08-12 stsp int ch, do_list = 0, create_symref = 0;
2940 d0eebce4 2019-03-11 stsp const char *delref = NULL;
2942 d0eebce4 2019-03-11 stsp /* TODO: Add -s option for adding symbolic references. */
2943 d1c1ae5f 2019-08-12 stsp while ((ch = getopt(argc, argv, "d:r:ls")) != -1) {
2944 d0eebce4 2019-03-11 stsp switch (ch) {
2946 d0eebce4 2019-03-11 stsp delref = optarg;
2949 d0eebce4 2019-03-11 stsp repo_path = realpath(optarg, NULL);
2950 d0eebce4 2019-03-11 stsp if (repo_path == NULL)
2951 d0eebce4 2019-03-11 stsp err(1, "-r option");
2952 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
2955 d0eebce4 2019-03-11 stsp do_list = 1;
2958 d1c1ae5f 2019-08-12 stsp create_symref = 1;
2961 d0eebce4 2019-03-11 stsp usage_ref();
2962 d0eebce4 2019-03-11 stsp /* NOTREACHED */
2966 d0eebce4 2019-03-11 stsp if (do_list && delref)
2967 d0eebce4 2019-03-11 stsp errx(1, "-l and -d options are mutually exclusive\n");
2969 d0eebce4 2019-03-11 stsp argc -= optind;
2970 d0eebce4 2019-03-11 stsp argv += optind;
2972 d0eebce4 2019-03-11 stsp if (do_list || delref) {
2973 d1c1ae5f 2019-08-12 stsp if (create_symref)
2974 d1c1ae5f 2019-08-12 stsp errx(1, "-s option cannot be used together with the "
2975 d1c1ae5f 2019-08-12 stsp "-l or -d options");
2976 d0eebce4 2019-03-11 stsp if (argc > 0)
2977 d0eebce4 2019-03-11 stsp usage_ref();
2978 d0eebce4 2019-03-11 stsp } else if (argc != 2)
2979 d0eebce4 2019-03-11 stsp usage_ref();
2981 d0eebce4 2019-03-11 stsp #ifndef PROFILE
2982 e0b57350 2019-03-12 stsp if (do_list) {
2983 e0b57350 2019-03-12 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
2984 e0b57350 2019-03-12 stsp NULL) == -1)
2985 e0b57350 2019-03-12 stsp err(1, "pledge");
2987 e0b57350 2019-03-12 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2988 e0b57350 2019-03-12 stsp "sendfd unveil", NULL) == -1)
2989 e0b57350 2019-03-12 stsp err(1, "pledge");
2992 d0eebce4 2019-03-11 stsp cwd = getcwd(NULL, 0);
2993 d0eebce4 2019-03-11 stsp if (cwd == NULL) {
2994 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2995 d0eebce4 2019-03-11 stsp goto done;
2998 d0eebce4 2019-03-11 stsp if (repo_path == NULL) {
2999 d0eebce4 2019-03-11 stsp error = got_worktree_open(&worktree, cwd);
3000 d0eebce4 2019-03-11 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
3001 d0eebce4 2019-03-11 stsp goto done;
3003 d0eebce4 2019-03-11 stsp error = NULL;
3004 d0eebce4 2019-03-11 stsp if (worktree) {
3005 d0eebce4 2019-03-11 stsp repo_path =
3006 d0eebce4 2019-03-11 stsp strdup(got_worktree_get_repo_path(worktree));
3007 d0eebce4 2019-03-11 stsp if (repo_path == NULL)
3008 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
3009 d0eebce4 2019-03-11 stsp if (error)
3010 d0eebce4 2019-03-11 stsp goto done;
3012 d0eebce4 2019-03-11 stsp repo_path = strdup(cwd);
3013 d0eebce4 2019-03-11 stsp if (repo_path == NULL) {
3014 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
3015 d0eebce4 2019-03-11 stsp goto done;
3020 d0eebce4 2019-03-11 stsp error = got_repo_open(&repo, repo_path);
3021 d0eebce4 2019-03-11 stsp if (error != NULL)
3022 d0eebce4 2019-03-11 stsp goto done;
3024 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), do_list,
3025 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
3026 c02c541e 2019-03-29 stsp if (error)
3027 c02c541e 2019-03-29 stsp goto done;
3029 d0eebce4 2019-03-11 stsp if (do_list)
3030 d0eebce4 2019-03-11 stsp error = list_refs(repo);
3031 d0eebce4 2019-03-11 stsp else if (delref)
3032 d0eebce4 2019-03-11 stsp error = delete_ref(repo, delref);
3033 d1c1ae5f 2019-08-12 stsp else if (create_symref)
3034 d1c1ae5f 2019-08-12 stsp error = add_symref(repo, argv[0], argv[1]);
3036 d0eebce4 2019-03-11 stsp error = add_ref(repo, argv[0], argv[1]);
3039 4e759de4 2019-06-26 stsp got_repo_close(repo);
3040 4e759de4 2019-06-26 stsp if (worktree)
3041 4e759de4 2019-06-26 stsp got_worktree_close(worktree);
3042 4e759de4 2019-06-26 stsp free(cwd);
3043 4e759de4 2019-06-26 stsp free(repo_path);
3044 4e759de4 2019-06-26 stsp return error;
3047 4e759de4 2019-06-26 stsp __dead static void
3048 4e759de4 2019-06-26 stsp usage_branch(void)
3050 4e759de4 2019-06-26 stsp fprintf(stderr,
3051 4e759de4 2019-06-26 stsp "usage: %s branch [-r repository] -l | -d name | "
3052 4e759de4 2019-06-26 stsp "name [base-branch]\n", getprogname());
3056 4e759de4 2019-06-26 stsp static const struct got_error *
3057 ba882ee3 2019-07-11 stsp list_branches(struct got_repository *repo, struct got_worktree *worktree)
3059 4e759de4 2019-06-26 stsp static const struct got_error *err = NULL;
3060 4e759de4 2019-06-26 stsp struct got_reflist_head refs;
3061 4e759de4 2019-06-26 stsp struct got_reflist_entry *re;
3063 4e759de4 2019-06-26 stsp SIMPLEQ_INIT(&refs);
3065 4e759de4 2019-06-26 stsp err = got_ref_list(&refs, repo);
3067 4e759de4 2019-06-26 stsp return err;
3069 4e759de4 2019-06-26 stsp SIMPLEQ_FOREACH(re, &refs, entry) {
3070 ba882ee3 2019-07-11 stsp const char *refname, *marker = " ";
3071 4e759de4 2019-06-26 stsp char *refstr;
3072 4e759de4 2019-06-26 stsp refname = got_ref_get_name(re->ref);
3073 4e759de4 2019-06-26 stsp if (strncmp(refname, "refs/heads/", 11) != 0)
3075 ba882ee3 2019-07-11 stsp if (worktree && strcmp(refname,
3076 ba882ee3 2019-07-11 stsp got_worktree_get_head_ref_name(worktree)) == 0) {
3077 ba882ee3 2019-07-11 stsp struct got_object_id *id = NULL;
3078 ba882ee3 2019-07-11 stsp err = got_ref_resolve(&id, repo, re->ref);
3080 ba882ee3 2019-07-11 stsp return err;
3081 ba882ee3 2019-07-11 stsp if (got_object_id_cmp(id,
3082 ba882ee3 2019-07-11 stsp got_worktree_get_base_commit_id(worktree)) == 0)
3083 ba882ee3 2019-07-11 stsp marker = "* ";
3085 ba882ee3 2019-07-11 stsp marker = "~ ";
3088 4e759de4 2019-06-26 stsp refname += 11;
3089 4e759de4 2019-06-26 stsp refstr = got_ref_to_str(re->ref);
3090 4e759de4 2019-06-26 stsp if (refstr == NULL)
3091 4e759de4 2019-06-26 stsp return got_error_from_errno("got_ref_to_str");
3092 ba882ee3 2019-07-11 stsp printf("%s%s: %s\n", marker, refname, refstr);
3093 4e759de4 2019-06-26 stsp free(refstr);
3096 4e759de4 2019-06-26 stsp got_ref_list_free(&refs);
3097 4e759de4 2019-06-26 stsp return NULL;
3100 4e759de4 2019-06-26 stsp static const struct got_error *
3101 4e759de4 2019-06-26 stsp delete_branch(struct got_repository *repo, const char *branch_name)
3103 4e759de4 2019-06-26 stsp const struct got_error *err = NULL;
3104 4e759de4 2019-06-26 stsp struct got_reference *ref;
3105 4e759de4 2019-06-26 stsp char *refname;
3107 4e759de4 2019-06-26 stsp if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
3108 4e759de4 2019-06-26 stsp return got_error_from_errno("asprintf");
3110 4e759de4 2019-06-26 stsp err = got_ref_open(&ref, repo, refname, 0);
3112 4e759de4 2019-06-26 stsp goto done;
3114 4e759de4 2019-06-26 stsp err = got_ref_delete(ref, repo);
3115 4e759de4 2019-06-26 stsp got_ref_close(ref);
3117 4e759de4 2019-06-26 stsp free(refname);
3118 4e759de4 2019-06-26 stsp return err;
3121 4e759de4 2019-06-26 stsp static const struct got_error *
3122 4e759de4 2019-06-26 stsp add_branch(struct got_repository *repo, const char *branch_name,
3123 4e759de4 2019-06-26 stsp const char *base_branch)
3125 4e759de4 2019-06-26 stsp const struct got_error *err = NULL;
3126 4e759de4 2019-06-26 stsp struct got_object_id *id = NULL;
3127 4e759de4 2019-06-26 stsp struct got_reference *ref = NULL;
3128 4e759de4 2019-06-26 stsp char *base_refname = NULL, *refname = NULL;
3129 4e759de4 2019-06-26 stsp struct got_reference *base_ref;
3132 d3f84d51 2019-07-11 stsp * Don't let the user create a branch named '-'.
3133 d3f84d51 2019-07-11 stsp * While technically a valid reference name, this case is usually
3134 d3f84d51 2019-07-11 stsp * an unintended typo.
3136 d3f84d51 2019-07-11 stsp if (branch_name[0] == '-' && branch_name[1] == '\0')
3137 d3f84d51 2019-07-11 stsp return got_error(GOT_ERR_BAD_REF_NAME);
3139 4e759de4 2019-06-26 stsp if (strcmp(GOT_REF_HEAD, base_branch) == 0) {
3140 4e759de4 2019-06-26 stsp base_refname = strdup(GOT_REF_HEAD);
3141 4e759de4 2019-06-26 stsp if (base_refname == NULL)
3142 4e759de4 2019-06-26 stsp return got_error_from_errno("strdup");
3143 4e759de4 2019-06-26 stsp } else if (asprintf(&base_refname, "refs/heads/%s", base_branch) == -1)
3144 4e759de4 2019-06-26 stsp return got_error_from_errno("asprintf");
3146 4e759de4 2019-06-26 stsp err = got_ref_open(&base_ref, repo, base_refname, 0);
3148 4e759de4 2019-06-26 stsp goto done;
3149 4e759de4 2019-06-26 stsp err = got_ref_resolve(&id, repo, base_ref);
3150 4e759de4 2019-06-26 stsp got_ref_close(base_ref);
3152 4e759de4 2019-06-26 stsp goto done;
3154 4e759de4 2019-06-26 stsp if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
3155 4e759de4 2019-06-26 stsp err = got_error_from_errno("asprintf");
3156 4e759de4 2019-06-26 stsp goto done;
3159 4e759de4 2019-06-26 stsp err = got_ref_open(&ref, repo, refname, 0);
3160 4e759de4 2019-06-26 stsp if (err == NULL) {
3161 4e759de4 2019-06-26 stsp err = got_error(GOT_ERR_BRANCH_EXISTS);
3162 4e759de4 2019-06-26 stsp goto done;
3163 4e759de4 2019-06-26 stsp } else if (err->code != GOT_ERR_NOT_REF)
3164 4e759de4 2019-06-26 stsp goto done;
3166 4e759de4 2019-06-26 stsp err = got_ref_alloc(&ref, refname, id);
3168 4e759de4 2019-06-26 stsp goto done;
3170 4e759de4 2019-06-26 stsp err = got_ref_write(ref, repo);
3173 4e759de4 2019-06-26 stsp got_ref_close(ref);
3175 4e759de4 2019-06-26 stsp free(base_refname);
3176 4e759de4 2019-06-26 stsp free(refname);
3177 4e759de4 2019-06-26 stsp return err;
3180 4e759de4 2019-06-26 stsp static const struct got_error *
3181 4e759de4 2019-06-26 stsp cmd_branch(int argc, char *argv[])
3183 4e759de4 2019-06-26 stsp const struct got_error *error = NULL;
3184 4e759de4 2019-06-26 stsp struct got_repository *repo = NULL;
3185 4e759de4 2019-06-26 stsp struct got_worktree *worktree = NULL;
3186 4e759de4 2019-06-26 stsp char *cwd = NULL, *repo_path = NULL;
3187 4e759de4 2019-06-26 stsp int ch, do_list = 0;
3188 4e759de4 2019-06-26 stsp const char *delref = NULL;
3190 4e759de4 2019-06-26 stsp while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
3191 4e759de4 2019-06-26 stsp switch (ch) {
3193 4e759de4 2019-06-26 stsp delref = optarg;
3196 4e759de4 2019-06-26 stsp repo_path = realpath(optarg, NULL);
3197 4e759de4 2019-06-26 stsp if (repo_path == NULL)
3198 4e759de4 2019-06-26 stsp err(1, "-r option");
3199 4e759de4 2019-06-26 stsp got_path_strip_trailing_slashes(repo_path);
3202 4e759de4 2019-06-26 stsp do_list = 1;
3205 4e759de4 2019-06-26 stsp usage_branch();
3206 4e759de4 2019-06-26 stsp /* NOTREACHED */
3210 4e759de4 2019-06-26 stsp if (do_list && delref)
3211 4e759de4 2019-06-26 stsp errx(1, "-l and -d options are mutually exclusive\n");
3213 4e759de4 2019-06-26 stsp argc -= optind;
3214 4e759de4 2019-06-26 stsp argv += optind;
3216 4e759de4 2019-06-26 stsp if (do_list || delref) {
3217 4e759de4 2019-06-26 stsp if (argc > 0)
3218 4e759de4 2019-06-26 stsp usage_branch();
3219 4e759de4 2019-06-26 stsp } else if (argc < 1 || argc > 2)
3220 4e759de4 2019-06-26 stsp usage_branch();
3222 4e759de4 2019-06-26 stsp #ifndef PROFILE
3223 4e759de4 2019-06-26 stsp if (do_list) {
3224 4e759de4 2019-06-26 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3225 4e759de4 2019-06-26 stsp NULL) == -1)
3226 4e759de4 2019-06-26 stsp err(1, "pledge");
3228 4e759de4 2019-06-26 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3229 4e759de4 2019-06-26 stsp "sendfd unveil", NULL) == -1)
3230 4e759de4 2019-06-26 stsp err(1, "pledge");
3233 4e759de4 2019-06-26 stsp cwd = getcwd(NULL, 0);
3234 4e759de4 2019-06-26 stsp if (cwd == NULL) {
3235 4e759de4 2019-06-26 stsp error = got_error_from_errno("getcwd");
3236 4e759de4 2019-06-26 stsp goto done;
3239 4e759de4 2019-06-26 stsp if (repo_path == NULL) {
3240 4e759de4 2019-06-26 stsp error = got_worktree_open(&worktree, cwd);
3241 4e759de4 2019-06-26 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
3242 4e759de4 2019-06-26 stsp goto done;
3244 4e759de4 2019-06-26 stsp error = NULL;
3245 4e759de4 2019-06-26 stsp if (worktree) {
3246 4e759de4 2019-06-26 stsp repo_path =
3247 4e759de4 2019-06-26 stsp strdup(got_worktree_get_repo_path(worktree));
3248 4e759de4 2019-06-26 stsp if (repo_path == NULL)
3249 4e759de4 2019-06-26 stsp error = got_error_from_errno("strdup");
3250 4e759de4 2019-06-26 stsp if (error)
3251 4e759de4 2019-06-26 stsp goto done;
3253 4e759de4 2019-06-26 stsp repo_path = strdup(cwd);
3254 4e759de4 2019-06-26 stsp if (repo_path == NULL) {
3255 4e759de4 2019-06-26 stsp error = got_error_from_errno("strdup");
3256 4e759de4 2019-06-26 stsp goto done;
3261 4e759de4 2019-06-26 stsp error = got_repo_open(&repo, repo_path);
3262 4e759de4 2019-06-26 stsp if (error != NULL)
3263 4e759de4 2019-06-26 stsp goto done;
3265 4e759de4 2019-06-26 stsp error = apply_unveil(got_repo_get_path(repo), do_list,
3266 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
3267 4e759de4 2019-06-26 stsp if (error)
3268 4e759de4 2019-06-26 stsp goto done;
3270 4e759de4 2019-06-26 stsp if (do_list)
3271 ba882ee3 2019-07-11 stsp error = list_branches(repo, worktree);
3272 4e759de4 2019-06-26 stsp else if (delref)
3273 4e759de4 2019-06-26 stsp error = delete_branch(repo, delref);
3275 4e759de4 2019-06-26 stsp const char *base_branch;
3276 4e759de4 2019-06-26 stsp if (argc == 1) {
3277 4e759de4 2019-06-26 stsp base_branch = worktree ?
3278 4e759de4 2019-06-26 stsp got_worktree_get_head_ref_name(worktree) :
3279 4e759de4 2019-06-26 stsp GOT_REF_HEAD;
3280 dc5351b4 2019-07-30 stsp if (strncmp(base_branch, "refs/heads/", 11) == 0)
3281 dc5351b4 2019-07-30 stsp base_branch += 11;
3283 4e759de4 2019-06-26 stsp base_branch = argv[1];
3284 4e759de4 2019-06-26 stsp error = add_branch(repo, argv[0], base_branch);
3288 d0eebce4 2019-03-11 stsp got_repo_close(repo);
3289 d0eebce4 2019-03-11 stsp if (worktree)
3290 d0eebce4 2019-03-11 stsp got_worktree_close(worktree);
3291 d0eebce4 2019-03-11 stsp free(cwd);
3292 d0eebce4 2019-03-11 stsp free(repo_path);
3293 d00136be 2019-03-26 stsp return error;
3296 d00136be 2019-03-26 stsp __dead static void
3297 d00136be 2019-03-26 stsp usage_add(void)
3299 fbb7e5c7 2019-05-11 stsp fprintf(stderr, "usage: %s add file-path ...\n", getprogname());
3303 d00136be 2019-03-26 stsp static const struct got_error *
3304 d00136be 2019-03-26 stsp cmd_add(int argc, char *argv[])
3306 d00136be 2019-03-26 stsp const struct got_error *error = NULL;
3307 031a5338 2019-03-26 stsp struct got_repository *repo = NULL;
3308 d00136be 2019-03-26 stsp struct got_worktree *worktree = NULL;
3309 1dd54920 2019-05-11 stsp char *cwd = NULL;
3310 1dd54920 2019-05-11 stsp struct got_pathlist_head paths;
3311 1dd54920 2019-05-11 stsp struct got_pathlist_entry *pe;
3314 1dd54920 2019-05-11 stsp TAILQ_INIT(&paths);
3316 d00136be 2019-03-26 stsp while ((ch = getopt(argc, argv, "")) != -1) {
3317 d00136be 2019-03-26 stsp switch (ch) {
3319 d00136be 2019-03-26 stsp usage_add();
3320 d00136be 2019-03-26 stsp /* NOTREACHED */
3324 d00136be 2019-03-26 stsp argc -= optind;
3325 d00136be 2019-03-26 stsp argv += optind;
3327 43012d58 2019-07-14 stsp #ifndef PROFILE
3328 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3329 43012d58 2019-07-14 stsp NULL) == -1)
3330 43012d58 2019-07-14 stsp err(1, "pledge");
3332 723c305c 2019-05-11 jcs if (argc < 1)
3333 d00136be 2019-03-26 stsp usage_add();
3335 d00136be 2019-03-26 stsp cwd = getcwd(NULL, 0);
3336 d00136be 2019-03-26 stsp if (cwd == NULL) {
3337 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
3338 d00136be 2019-03-26 stsp goto done;
3341 d00136be 2019-03-26 stsp error = got_worktree_open(&worktree, cwd);
3342 d00136be 2019-03-26 stsp if (error)
3343 d00136be 2019-03-26 stsp goto done;
3345 031a5338 2019-03-26 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3346 031a5338 2019-03-26 stsp if (error != NULL)
3347 031a5338 2019-03-26 stsp goto done;
3349 031a5338 2019-03-26 stsp error = apply_unveil(got_repo_get_path(repo), 1,
3350 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
3351 d00136be 2019-03-26 stsp if (error)
3352 d00136be 2019-03-26 stsp goto done;
3354 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3355 6d022e97 2019-08-04 stsp if (error)
3356 6d022e97 2019-08-04 stsp goto done;
3358 1dd54920 2019-05-11 stsp error = got_worktree_schedule_add(worktree, &paths, print_status,
3359 1dd54920 2019-05-11 stsp NULL, repo);
3362 031a5338 2019-03-26 stsp got_repo_close(repo);
3363 d00136be 2019-03-26 stsp if (worktree)
3364 d00136be 2019-03-26 stsp got_worktree_close(worktree);
3365 1dd54920 2019-05-11 stsp TAILQ_FOREACH(pe, &paths, entry)
3366 1dd54920 2019-05-11 stsp free((char *)pe->path);
3367 1dd54920 2019-05-11 stsp got_pathlist_free(&paths);
3368 2ec1f75b 2019-03-26 stsp free(cwd);
3369 2ec1f75b 2019-03-26 stsp return error;
3372 2ec1f75b 2019-03-26 stsp __dead static void
3373 648e4ef7 2019-07-09 stsp usage_remove(void)
3375 648e4ef7 2019-07-09 stsp fprintf(stderr, "usage: %s remove [-f] file-path ...\n", getprogname());
3379 2ec1f75b 2019-03-26 stsp static const struct got_error *
3380 648e4ef7 2019-07-09 stsp cmd_remove(int argc, char *argv[])
3382 2ec1f75b 2019-03-26 stsp const struct got_error *error = NULL;
3383 2ec1f75b 2019-03-26 stsp struct got_worktree *worktree = NULL;
3384 2ec1f75b 2019-03-26 stsp struct got_repository *repo = NULL;
3385 17ed4618 2019-06-02 stsp char *cwd = NULL;
3386 17ed4618 2019-06-02 stsp struct got_pathlist_head paths;
3387 17ed4618 2019-06-02 stsp struct got_pathlist_entry *pe;
3388 6d022e97 2019-08-04 stsp int ch, delete_local_mods = 0;
3390 17ed4618 2019-06-02 stsp TAILQ_INIT(&paths);
3392 2ec1f75b 2019-03-26 stsp while ((ch = getopt(argc, argv, "f")) != -1) {
3393 2ec1f75b 2019-03-26 stsp switch (ch) {
3395 2ec1f75b 2019-03-26 stsp delete_local_mods = 1;
3398 2ec1f75b 2019-03-26 stsp usage_add();
3399 2ec1f75b 2019-03-26 stsp /* NOTREACHED */
3403 2ec1f75b 2019-03-26 stsp argc -= optind;
3404 2ec1f75b 2019-03-26 stsp argv += optind;
3406 43012d58 2019-07-14 stsp #ifndef PROFILE
3407 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3408 43012d58 2019-07-14 stsp NULL) == -1)
3409 43012d58 2019-07-14 stsp err(1, "pledge");
3411 17ed4618 2019-06-02 stsp if (argc < 1)
3412 648e4ef7 2019-07-09 stsp usage_remove();
3414 2ec1f75b 2019-03-26 stsp cwd = getcwd(NULL, 0);
3415 2ec1f75b 2019-03-26 stsp if (cwd == NULL) {
3416 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
3417 2ec1f75b 2019-03-26 stsp goto done;
3419 2ec1f75b 2019-03-26 stsp error = got_worktree_open(&worktree, cwd);
3420 2ec1f75b 2019-03-26 stsp if (error)
3421 2ec1f75b 2019-03-26 stsp goto done;
3423 2ec1f75b 2019-03-26 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3425 2ec1f75b 2019-03-26 stsp goto done;
3427 c2253644 2019-03-26 stsp error = apply_unveil(got_repo_get_path(repo), 1,
3428 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
3429 2ec1f75b 2019-03-26 stsp if (error)
3430 2ec1f75b 2019-03-26 stsp goto done;
3432 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3433 6d022e97 2019-08-04 stsp if (error)
3434 6d022e97 2019-08-04 stsp goto done;
3436 17ed4618 2019-06-02 stsp error = got_worktree_schedule_delete(worktree, &paths,
3437 17ed4618 2019-06-02 stsp delete_local_mods, print_status, NULL, repo);
3438 a129376b 2019-03-28 stsp if (error)
3439 a129376b 2019-03-28 stsp goto done;
3442 a129376b 2019-03-28 stsp got_repo_close(repo);
3443 a129376b 2019-03-28 stsp if (worktree)
3444 a129376b 2019-03-28 stsp got_worktree_close(worktree);
3445 17ed4618 2019-06-02 stsp TAILQ_FOREACH(pe, &paths, entry)
3446 17ed4618 2019-06-02 stsp free((char *)pe->path);
3447 17ed4618 2019-06-02 stsp got_pathlist_free(&paths);
3448 a129376b 2019-03-28 stsp free(cwd);
3449 a129376b 2019-03-28 stsp return error;
3452 a129376b 2019-03-28 stsp __dead static void
3453 a129376b 2019-03-28 stsp usage_revert(void)
3455 33aa809d 2019-08-08 stsp fprintf(stderr, "usage: %s revert [-p] [-F response-script] [-R] "
3456 33aa809d 2019-08-08 stsp "path ...\n", getprogname());
3460 1ee397ad 2019-07-12 stsp static const struct got_error *
3461 a129376b 2019-03-28 stsp revert_progress(void *arg, unsigned char status, const char *path)
3463 a129376b 2019-03-28 stsp while (path[0] == '/')
3465 a129376b 2019-03-28 stsp printf("%c %s\n", status, path);
3466 33aa809d 2019-08-08 stsp return NULL;
3469 33aa809d 2019-08-08 stsp struct choose_patch_arg {
3470 33aa809d 2019-08-08 stsp FILE *patch_script_file;
3471 33aa809d 2019-08-08 stsp const char *action;
3474 33aa809d 2019-08-08 stsp static const struct got_error *
3475 33aa809d 2019-08-08 stsp show_change(unsigned char status, const char *path, FILE *patch_file, int n,
3476 33aa809d 2019-08-08 stsp int nchanges, const char *action)
3478 33aa809d 2019-08-08 stsp char *line = NULL;
3479 33aa809d 2019-08-08 stsp size_t linesize = 0;
3480 33aa809d 2019-08-08 stsp ssize_t linelen;
3482 33aa809d 2019-08-08 stsp switch (status) {
3483 33aa809d 2019-08-08 stsp case GOT_STATUS_ADD:
3484 33aa809d 2019-08-08 stsp printf("A %s\n%s this addition? [y/n] ", path, action);
3486 33aa809d 2019-08-08 stsp case GOT_STATUS_DELETE:
3487 33aa809d 2019-08-08 stsp printf("D %s\n%s this deletion? [y/n] ", path, action);
3489 33aa809d 2019-08-08 stsp case GOT_STATUS_MODIFY:
3490 33aa809d 2019-08-08 stsp if (fseek(patch_file, 0L, SEEK_SET) == -1)
3491 33aa809d 2019-08-08 stsp return got_error_from_errno("fseek");
3492 33aa809d 2019-08-08 stsp printf(GOT_COMMIT_SEP_STR);
3493 9516e7cb 2019-08-11 stsp while ((linelen = getline(&line, &linesize, patch_file)) != -1)
3494 33aa809d 2019-08-08 stsp printf("%s", line);
3495 33aa809d 2019-08-08 stsp if (ferror(patch_file))
3496 33aa809d 2019-08-08 stsp return got_error_from_errno("getline");
3497 33aa809d 2019-08-08 stsp printf(GOT_COMMIT_SEP_STR);
3498 33aa809d 2019-08-08 stsp printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
3499 33aa809d 2019-08-08 stsp path, n, nchanges, action);
3502 33aa809d 2019-08-08 stsp return got_error_path(path, GOT_ERR_FILE_STATUS);
3505 33aa809d 2019-08-08 stsp return NULL;
3508 33aa809d 2019-08-08 stsp static const struct got_error *
3509 33aa809d 2019-08-08 stsp choose_patch(int *choice, void *arg, unsigned char status, const char *path,
3510 33aa809d 2019-08-08 stsp FILE *patch_file, int n, int nchanges)
3512 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
3513 33aa809d 2019-08-08 stsp char *line = NULL;
3514 33aa809d 2019-08-08 stsp size_t linesize = 0;
3515 33aa809d 2019-08-08 stsp ssize_t linelen;
3516 33aa809d 2019-08-08 stsp int resp = ' ';
3517 33aa809d 2019-08-08 stsp struct choose_patch_arg *a = arg;
3519 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NONE;
3521 33aa809d 2019-08-08 stsp if (a->patch_script_file) {
3523 33aa809d 2019-08-08 stsp err = show_change(status, path, patch_file, n, nchanges,
3524 33aa809d 2019-08-08 stsp a->action);
3526 33aa809d 2019-08-08 stsp return err;
3527 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, a->patch_script_file);
3528 33aa809d 2019-08-08 stsp if (linelen == -1) {
3529 33aa809d 2019-08-08 stsp if (ferror(a->patch_script_file))
3530 33aa809d 2019-08-08 stsp return got_error_from_errno("getline");
3531 33aa809d 2019-08-08 stsp return NULL;
3533 33aa809d 2019-08-08 stsp nl = strchr(line, '\n');
3535 33aa809d 2019-08-08 stsp *nl = '\0';
3536 33aa809d 2019-08-08 stsp if (strcmp(line, "y") == 0) {
3537 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_YES;
3538 33aa809d 2019-08-08 stsp printf("y\n");
3539 33aa809d 2019-08-08 stsp } else if (strcmp(line, "n") == 0) {
3540 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NO;
3541 33aa809d 2019-08-08 stsp printf("n\n");
3542 33aa809d 2019-08-08 stsp } else if (strcmp(line, "q") == 0 &&
3543 33aa809d 2019-08-08 stsp status == GOT_STATUS_MODIFY) {
3544 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_QUIT;
3545 33aa809d 2019-08-08 stsp printf("q\n");
3547 33aa809d 2019-08-08 stsp printf("invalid response '%s'\n", line);
3548 33aa809d 2019-08-08 stsp free(line);
3549 33aa809d 2019-08-08 stsp return NULL;
3552 33aa809d 2019-08-08 stsp while (resp != 'y' && resp != 'n' && resp != 'q') {
3553 33aa809d 2019-08-08 stsp err = show_change(status, path, patch_file, n, nchanges,
3554 33aa809d 2019-08-08 stsp a->action);
3556 33aa809d 2019-08-08 stsp return err;
3557 33aa809d 2019-08-08 stsp resp = getchar();
3558 33aa809d 2019-08-08 stsp if (resp == '\n')
3559 33aa809d 2019-08-08 stsp resp = getchar();
3560 33aa809d 2019-08-08 stsp if (status == GOT_STATUS_MODIFY) {
3561 33aa809d 2019-08-08 stsp if (resp != 'y' && resp != 'n' && resp != 'q') {
3562 33aa809d 2019-08-08 stsp printf("invalid response '%c'\n", resp);
3563 33aa809d 2019-08-08 stsp resp = ' ';
3565 33aa809d 2019-08-08 stsp } else if (resp != 'y' && resp != 'n') {
3566 33aa809d 2019-08-08 stsp printf("invalid response '%c'\n", resp);
3567 33aa809d 2019-08-08 stsp resp = ' ';
3571 33aa809d 2019-08-08 stsp if (resp == 'y')
3572 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_YES;
3573 33aa809d 2019-08-08 stsp else if (resp == 'n')
3574 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NO;
3575 33aa809d 2019-08-08 stsp else if (resp == 'q' && status == GOT_STATUS_MODIFY)
3576 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_QUIT;
3578 1ee397ad 2019-07-12 stsp return NULL;
3582 a129376b 2019-03-28 stsp static const struct got_error *
3583 a129376b 2019-03-28 stsp cmd_revert(int argc, char *argv[])
3585 a129376b 2019-03-28 stsp const struct got_error *error = NULL;
3586 a129376b 2019-03-28 stsp struct got_worktree *worktree = NULL;
3587 a129376b 2019-03-28 stsp struct got_repository *repo = NULL;
3588 a129376b 2019-03-28 stsp char *cwd = NULL, *path = NULL;
3589 e20a8b6f 2019-06-04 stsp struct got_pathlist_head paths;
3590 0f6d7415 2019-08-08 stsp struct got_pathlist_entry *pe;
3591 33aa809d 2019-08-08 stsp int ch, can_recurse = 0, pflag = 0;
3592 33aa809d 2019-08-08 stsp FILE *patch_script_file = NULL;
3593 33aa809d 2019-08-08 stsp const char *patch_script_path = NULL;
3594 33aa809d 2019-08-08 stsp struct choose_patch_arg cpa;
3596 e20a8b6f 2019-06-04 stsp TAILQ_INIT(&paths);
3598 33aa809d 2019-08-08 stsp while ((ch = getopt(argc, argv, "pF:R")) != -1) {
3599 a129376b 2019-03-28 stsp switch (ch) {
3601 33aa809d 2019-08-08 stsp pflag = 1;
3604 33aa809d 2019-08-08 stsp patch_script_path = optarg;
3607 0f6d7415 2019-08-08 stsp can_recurse = 1;
3610 a129376b 2019-03-28 stsp usage_revert();
3611 a129376b 2019-03-28 stsp /* NOTREACHED */
3615 a129376b 2019-03-28 stsp argc -= optind;
3616 a129376b 2019-03-28 stsp argv += optind;
3618 43012d58 2019-07-14 stsp #ifndef PROFILE
3619 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3620 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
3621 43012d58 2019-07-14 stsp err(1, "pledge");
3623 e20a8b6f 2019-06-04 stsp if (argc < 1)
3624 a129376b 2019-03-28 stsp usage_revert();
3625 33aa809d 2019-08-08 stsp if (patch_script_path && !pflag)
3626 33aa809d 2019-08-08 stsp errx(1, "-F option can only be used together with -p option");
3628 a129376b 2019-03-28 stsp cwd = getcwd(NULL, 0);
3629 a129376b 2019-03-28 stsp if (cwd == NULL) {
3630 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
3631 a129376b 2019-03-28 stsp goto done;
3633 a129376b 2019-03-28 stsp error = got_worktree_open(&worktree, cwd);
3634 2ec1f75b 2019-03-26 stsp if (error)
3635 2ec1f75b 2019-03-26 stsp goto done;
3637 a129376b 2019-03-28 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3638 a129376b 2019-03-28 stsp if (error != NULL)
3639 a129376b 2019-03-28 stsp goto done;
3641 33aa809d 2019-08-08 stsp if (patch_script_path) {
3642 33aa809d 2019-08-08 stsp patch_script_file = fopen(patch_script_path, "r");
3643 33aa809d 2019-08-08 stsp if (patch_script_file == NULL) {
3644 33aa809d 2019-08-08 stsp error = got_error_from_errno2("fopen",
3645 33aa809d 2019-08-08 stsp patch_script_path);
3646 33aa809d 2019-08-08 stsp goto done;
3649 a129376b 2019-03-28 stsp error = apply_unveil(got_repo_get_path(repo), 1,
3650 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
3651 a129376b 2019-03-28 stsp if (error)
3652 a129376b 2019-03-28 stsp goto done;
3654 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3655 6d022e97 2019-08-04 stsp if (error)
3656 6d022e97 2019-08-04 stsp goto done;
3658 0f6d7415 2019-08-08 stsp if (!can_recurse) {
3659 0f6d7415 2019-08-08 stsp char *ondisk_path;
3660 0f6d7415 2019-08-08 stsp struct stat sb;
3661 0f6d7415 2019-08-08 stsp TAILQ_FOREACH(pe, &paths, entry) {
3662 0f6d7415 2019-08-08 stsp if (asprintf(&ondisk_path, "%s/%s",
3663 0f6d7415 2019-08-08 stsp got_worktree_get_root_path(worktree),
3664 0f6d7415 2019-08-08 stsp pe->path) == -1) {
3665 0f6d7415 2019-08-08 stsp error = got_error_from_errno("asprintf");
3666 0f6d7415 2019-08-08 stsp goto done;
3668 0f6d7415 2019-08-08 stsp if (lstat(ondisk_path, &sb) == -1) {
3669 0f6d7415 2019-08-08 stsp if (errno == ENOENT) {
3670 0f6d7415 2019-08-08 stsp free(ondisk_path);
3673 0f6d7415 2019-08-08 stsp error = got_error_from_errno2("lstat",
3674 0f6d7415 2019-08-08 stsp ondisk_path);
3675 0f6d7415 2019-08-08 stsp free(ondisk_path);
3676 0f6d7415 2019-08-08 stsp goto done;
3678 0f6d7415 2019-08-08 stsp free(ondisk_path);
3679 0f6d7415 2019-08-08 stsp if (S_ISDIR(sb.st_mode)) {
3680 0f6d7415 2019-08-08 stsp error = got_error_msg(GOT_ERR_BAD_PATH,
3681 0f6d7415 2019-08-08 stsp "reverting directories requires -R option");
3682 0f6d7415 2019-08-08 stsp goto done;
3687 33aa809d 2019-08-08 stsp cpa.patch_script_file = patch_script_file;
3688 33aa809d 2019-08-08 stsp cpa.action = "revert";
3689 33aa809d 2019-08-08 stsp error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
3690 33aa809d 2019-08-08 stsp pflag ? choose_patch : NULL, &cpa, repo);
3691 a129376b 2019-03-28 stsp if (error)
3692 a129376b 2019-03-28 stsp goto done;
3694 33aa809d 2019-08-08 stsp if (patch_script_file && fclose(patch_script_file) == EOF &&
3695 33aa809d 2019-08-08 stsp error == NULL)
3696 33aa809d 2019-08-08 stsp error = got_error_from_errno2("fclose", patch_script_path);
3698 2ec1f75b 2019-03-26 stsp got_repo_close(repo);
3699 2ec1f75b 2019-03-26 stsp if (worktree)
3700 2ec1f75b 2019-03-26 stsp got_worktree_close(worktree);
3701 2ec1f75b 2019-03-26 stsp free(path);
3702 d00136be 2019-03-26 stsp free(cwd);
3703 6bad629b 2019-02-04 stsp return error;
3706 c4296144 2019-05-09 stsp __dead static void
3707 c4296144 2019-05-09 stsp usage_commit(void)
3709 5c1e53bc 2019-07-28 stsp fprintf(stderr, "usage: %s commit [-m msg] [path ...]\n",
3710 5c1e53bc 2019-07-28 stsp getprogname());
3714 e2ba3d07 2019-05-13 stsp struct collect_commit_logmsg_arg {
3715 e2ba3d07 2019-05-13 stsp const char *cmdline_log;
3716 e2ba3d07 2019-05-13 stsp const char *editor;
3717 e0870e44 2019-05-13 stsp const char *worktree_path;
3718 76d98825 2019-06-03 stsp const char *branch_name;
3719 314a6357 2019-05-13 stsp const char *repo_path;
3720 e0870e44 2019-05-13 stsp char *logmsg_path;
3724 33ad4cbe 2019-05-12 jcs static const struct got_error *
3725 33ad4cbe 2019-05-12 jcs collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
3728 76d98825 2019-06-03 stsp char *initial_content = NULL;
3729 33ad4cbe 2019-05-12 jcs struct got_pathlist_entry *pe;
3730 33ad4cbe 2019-05-12 jcs const struct got_error *err = NULL;
3731 e0870e44 2019-05-13 stsp char *template = NULL;
3732 e2ba3d07 2019-05-13 stsp struct collect_commit_logmsg_arg *a = arg;
3734 33ad4cbe 2019-05-12 jcs size_t len;
3736 33ad4cbe 2019-05-12 jcs /* if a message was specified on the command line, just use it */
3737 e2ba3d07 2019-05-13 stsp if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
3738 e2ba3d07 2019-05-13 stsp len = strlen(a->cmdline_log) + 1;
3739 33ad4cbe 2019-05-12 jcs *logmsg = malloc(len + 1);
3740 9f42ff69 2019-05-13 stsp if (*logmsg == NULL)
3741 638f9024 2019-05-13 stsp return got_error_from_errno("malloc");
3742 e2ba3d07 2019-05-13 stsp strlcpy(*logmsg, a->cmdline_log, len);
3743 33ad4cbe 2019-05-12 jcs return NULL;
3746 e0870e44 2019-05-13 stsp if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
3747 76d98825 2019-06-03 stsp return got_error_from_errno("asprintf");
3749 76d98825 2019-06-03 stsp if (asprintf(&initial_content,
3750 76d98825 2019-06-03 stsp "\n# changes to be committed on branch %s:\n",
3751 76d98825 2019-06-03 stsp a->branch_name) == -1)
3752 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3754 e0870e44 2019-05-13 stsp err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
3756 e0870e44 2019-05-13 stsp goto done;
3758 e0870e44 2019-05-13 stsp dprintf(fd, initial_content);
3760 33ad4cbe 2019-05-12 jcs TAILQ_FOREACH(pe, commitable_paths, entry) {
3761 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
3762 8656d6c4 2019-05-20 stsp dprintf(fd, "# %c %s\n",
3763 8656d6c4 2019-05-20 stsp got_commitable_get_status(ct),
3764 8656d6c4 2019-05-20 stsp got_commitable_get_path(ct));
3768 3ce1b845 2019-07-15 stsp err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content);
3770 e2af0fd1 2019-08-08 stsp if (err == NULL || err->code == GOT_ERR_COMMIT_MSG_EMPTY) {
3771 e2af0fd1 2019-08-08 stsp unlink(a->logmsg_path);
3772 e2af0fd1 2019-08-08 stsp free(a->logmsg_path);
3773 e2af0fd1 2019-08-08 stsp a->logmsg_path = NULL;
3775 76d98825 2019-06-03 stsp free(initial_content);
3776 e0870e44 2019-05-13 stsp free(template);
3778 314a6357 2019-05-13 stsp /* Editor is done; we can now apply unveil(2) */
3779 3ce1b845 2019-07-15 stsp if (err == NULL) {
3780 c530dc23 2019-07-23 stsp err = apply_unveil(a->repo_path, 0, a->worktree_path);
3781 3ce1b845 2019-07-15 stsp if (err) {
3782 3ce1b845 2019-07-15 stsp free(*logmsg);
3783 3ce1b845 2019-07-15 stsp *logmsg = NULL;
3786 33ad4cbe 2019-05-12 jcs return err;
3789 c4296144 2019-05-09 stsp static const struct got_error *
3790 c4296144 2019-05-09 stsp cmd_commit(int argc, char *argv[])
3792 c4296144 2019-05-09 stsp const struct got_error *error = NULL;
3793 c4296144 2019-05-09 stsp struct got_worktree *worktree = NULL;
3794 c4296144 2019-05-09 stsp struct got_repository *repo = NULL;
3795 5c1e53bc 2019-07-28 stsp char *cwd = NULL, *id_str = NULL;
3796 c4296144 2019-05-09 stsp struct got_object_id *id = NULL;
3797 33ad4cbe 2019-05-12 jcs const char *logmsg = NULL;
3798 84792843 2019-08-09 stsp const char *author;
3799 e2ba3d07 2019-05-13 stsp struct collect_commit_logmsg_arg cl_arg;
3800 e2ba3d07 2019-05-13 stsp char *editor = NULL;
3801 916f288c 2019-07-30 stsp int ch, rebase_in_progress, histedit_in_progress;
3802 5c1e53bc 2019-07-28 stsp struct got_pathlist_head paths;
3804 5c1e53bc 2019-07-28 stsp TAILQ_INIT(&paths);
3805 6ac5a73c 2019-08-08 stsp cl_arg.logmsg_path = NULL;
3807 c4296144 2019-05-09 stsp while ((ch = getopt(argc, argv, "m:")) != -1) {
3808 c4296144 2019-05-09 stsp switch (ch) {
3810 c4296144 2019-05-09 stsp logmsg = optarg;
3813 c4296144 2019-05-09 stsp usage_commit();
3814 c4296144 2019-05-09 stsp /* NOTREACHED */
3818 c4296144 2019-05-09 stsp argc -= optind;
3819 c4296144 2019-05-09 stsp argv += optind;
3821 43012d58 2019-07-14 stsp #ifndef PROFILE
3822 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3823 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
3824 43012d58 2019-07-14 stsp err(1, "pledge");
3826 84792843 2019-08-09 stsp error = get_author(&author);
3827 84792843 2019-08-09 stsp if (error)
3828 84792843 2019-08-09 stsp return error;
3830 c4296144 2019-05-09 stsp cwd = getcwd(NULL, 0);
3831 c4296144 2019-05-09 stsp if (cwd == NULL) {
3832 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
3833 c4296144 2019-05-09 stsp goto done;
3835 c4296144 2019-05-09 stsp error = got_worktree_open(&worktree, cwd);
3836 7d5807f4 2019-07-11 stsp if (error)
3837 7d5807f4 2019-07-11 stsp goto done;
3839 a698f62e 2019-07-25 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
3840 c4296144 2019-05-09 stsp if (error)
3841 a698f62e 2019-07-25 stsp goto done;
3842 a698f62e 2019-07-25 stsp if (rebase_in_progress) {
3843 a698f62e 2019-07-25 stsp error = got_error(GOT_ERR_REBASING);
3844 c4296144 2019-05-09 stsp goto done;
3847 916f288c 2019-07-30 stsp error = got_worktree_histedit_in_progress(&histedit_in_progress,
3848 916f288c 2019-07-30 stsp worktree);
3849 5c1e53bc 2019-07-28 stsp if (error)
3850 5c1e53bc 2019-07-28 stsp goto done;
3852 c4296144 2019-05-09 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3853 c4296144 2019-05-09 stsp if (error != NULL)
3854 c4296144 2019-05-09 stsp goto done;
3857 314a6357 2019-05-13 stsp * unveil(2) traverses exec(2); if an editor is used we have
3858 314a6357 2019-05-13 stsp * to apply unveil after the log message has been written.
3860 314a6357 2019-05-13 stsp if (logmsg == NULL || strlen(logmsg) == 0)
3861 314a6357 2019-05-13 stsp error = get_editor(&editor);
3863 314a6357 2019-05-13 stsp error = apply_unveil(got_repo_get_path(repo), 0,
3864 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
3865 c4296144 2019-05-09 stsp if (error)
3866 c4296144 2019-05-09 stsp goto done;
3868 087fb88c 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3869 087fb88c 2019-08-04 stsp if (error)
3870 087fb88c 2019-08-04 stsp goto done;
3872 e2ba3d07 2019-05-13 stsp cl_arg.editor = editor;
3873 e2ba3d07 2019-05-13 stsp cl_arg.cmdline_log = logmsg;
3874 e0870e44 2019-05-13 stsp cl_arg.worktree_path = got_worktree_get_root_path(worktree);
3875 76d98825 2019-06-03 stsp cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
3876 916f288c 2019-07-30 stsp if (!histedit_in_progress) {
3877 916f288c 2019-07-30 stsp if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
3878 916f288c 2019-07-30 stsp error = got_error(GOT_ERR_COMMIT_BRANCH);
3879 916f288c 2019-07-30 stsp goto done;
3881 916f288c 2019-07-30 stsp cl_arg.branch_name += 11;
3883 314a6357 2019-05-13 stsp cl_arg.repo_path = got_repo_get_path(repo);
3884 84792843 2019-08-09 stsp error = got_worktree_commit(&id, worktree, &paths, author, NULL,
3885 e2ba3d07 2019-05-13 stsp collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
3886 e0870e44 2019-05-13 stsp if (error) {
3887 e0870e44 2019-05-13 stsp if (cl_arg.logmsg_path)
3888 e0870e44 2019-05-13 stsp fprintf(stderr, "%s: log message preserved in %s\n",
3889 e0870e44 2019-05-13 stsp getprogname(), cl_arg.logmsg_path);
3890 c4296144 2019-05-09 stsp goto done;
3893 e0870e44 2019-05-13 stsp if (cl_arg.logmsg_path)
3894 e0870e44 2019-05-13 stsp unlink(cl_arg.logmsg_path);
3896 c4296144 2019-05-09 stsp error = got_object_id_str(&id_str, id);
3897 c4296144 2019-05-09 stsp if (error)
3898 c4296144 2019-05-09 stsp goto done;
3899 a7648d7a 2019-06-02 stsp printf("Created commit %s\n", id_str);
3901 6ac5a73c 2019-08-08 stsp free(cl_arg.logmsg_path);
3903 c4296144 2019-05-09 stsp got_repo_close(repo);
3904 c4296144 2019-05-09 stsp if (worktree)
3905 c4296144 2019-05-09 stsp got_worktree_close(worktree);
3906 c4296144 2019-05-09 stsp free(cwd);
3907 c4296144 2019-05-09 stsp free(id_str);
3908 e2ba3d07 2019-05-13 stsp free(editor);
3909 234035bc 2019-06-01 stsp return error;
3912 234035bc 2019-06-01 stsp __dead static void
3913 234035bc 2019-06-01 stsp usage_cherrypick(void)
3915 234035bc 2019-06-01 stsp fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
3919 234035bc 2019-06-01 stsp static const struct got_error *
3920 234035bc 2019-06-01 stsp cmd_cherrypick(int argc, char *argv[])
3922 234035bc 2019-06-01 stsp const struct got_error *error = NULL;
3923 234035bc 2019-06-01 stsp struct got_worktree *worktree = NULL;
3924 234035bc 2019-06-01 stsp struct got_repository *repo = NULL;
3925 234035bc 2019-06-01 stsp char *cwd = NULL, *commit_id_str = NULL;
3926 234035bc 2019-06-01 stsp struct got_object_id *commit_id = NULL;
3927 234035bc 2019-06-01 stsp struct got_commit_object *commit = NULL;
3928 234035bc 2019-06-01 stsp struct got_object_qid *pid;
3929 234035bc 2019-06-01 stsp struct got_reference *head_ref = NULL;
3930 234035bc 2019-06-01 stsp int ch, did_something = 0;
3932 234035bc 2019-06-01 stsp while ((ch = getopt(argc, argv, "")) != -1) {
3933 234035bc 2019-06-01 stsp switch (ch) {
3935 234035bc 2019-06-01 stsp usage_cherrypick();
3936 234035bc 2019-06-01 stsp /* NOTREACHED */
3940 234035bc 2019-06-01 stsp argc -= optind;
3941 234035bc 2019-06-01 stsp argv += optind;
3943 43012d58 2019-07-14 stsp #ifndef PROFILE
3944 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3945 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
3946 43012d58 2019-07-14 stsp err(1, "pledge");
3948 234035bc 2019-06-01 stsp if (argc != 1)
3949 234035bc 2019-06-01 stsp usage_cherrypick();
3951 234035bc 2019-06-01 stsp cwd = getcwd(NULL, 0);
3952 234035bc 2019-06-01 stsp if (cwd == NULL) {
3953 234035bc 2019-06-01 stsp error = got_error_from_errno("getcwd");
3954 234035bc 2019-06-01 stsp goto done;
3956 234035bc 2019-06-01 stsp error = got_worktree_open(&worktree, cwd);
3957 234035bc 2019-06-01 stsp if (error)
3958 234035bc 2019-06-01 stsp goto done;
3960 234035bc 2019-06-01 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3961 234035bc 2019-06-01 stsp if (error != NULL)
3962 234035bc 2019-06-01 stsp goto done;
3964 234035bc 2019-06-01 stsp error = apply_unveil(got_repo_get_path(repo), 0,
3965 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
3966 234035bc 2019-06-01 stsp if (error)
3967 234035bc 2019-06-01 stsp goto done;
3969 dd88155e 2019-06-29 stsp error = got_repo_match_object_id_prefix(&commit_id, argv[0],
3970 dd88155e 2019-06-29 stsp GOT_OBJ_TYPE_COMMIT, repo);
3971 234035bc 2019-06-01 stsp if (error != NULL) {
3972 234035bc 2019-06-01 stsp struct got_reference *ref;
3973 234035bc 2019-06-01 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
3974 234035bc 2019-06-01 stsp goto done;
3975 234035bc 2019-06-01 stsp error = got_ref_open(&ref, repo, argv[0], 0);
3976 234035bc 2019-06-01 stsp if (error != NULL)
3977 234035bc 2019-06-01 stsp goto done;
3978 234035bc 2019-06-01 stsp error = got_ref_resolve(&commit_id, repo, ref);
3979 234035bc 2019-06-01 stsp got_ref_close(ref);
3980 234035bc 2019-06-01 stsp if (error != NULL)
3981 234035bc 2019-06-01 stsp goto done;
3983 234035bc 2019-06-01 stsp error = got_object_id_str(&commit_id_str, commit_id);
3984 234035bc 2019-06-01 stsp if (error)
3985 234035bc 2019-06-01 stsp goto done;
3987 234035bc 2019-06-01 stsp error = got_ref_open(&head_ref, repo,
3988 234035bc 2019-06-01 stsp got_worktree_get_head_ref_name(worktree), 0);
3989 234035bc 2019-06-01 stsp if (error != NULL)
3990 234035bc 2019-06-01 stsp goto done;
3992 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
3993 234035bc 2019-06-01 stsp if (error) {
3994 234035bc 2019-06-01 stsp if (error->code != GOT_ERR_ANCESTRY)
3995 234035bc 2019-06-01 stsp goto done;
3996 234035bc 2019-06-01 stsp error = NULL;
3998 234035bc 2019-06-01 stsp error = got_error(GOT_ERR_SAME_BRANCH);
3999 234035bc 2019-06-01 stsp goto done;
4002 234035bc 2019-06-01 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
4003 234035bc 2019-06-01 stsp if (error)
4004 234035bc 2019-06-01 stsp goto done;
4005 234035bc 2019-06-01 stsp pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
4006 03415a1a 2019-06-02 stsp error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
4007 03415a1a 2019-06-02 stsp commit_id, repo, update_progress, &did_something, check_cancelled,
4009 234035bc 2019-06-01 stsp if (error != NULL)
4010 234035bc 2019-06-01 stsp goto done;
4012 234035bc 2019-06-01 stsp if (did_something)
4013 a7648d7a 2019-06-02 stsp printf("Merged commit %s\n", commit_id_str);
4015 234035bc 2019-06-01 stsp if (commit)
4016 234035bc 2019-06-01 stsp got_object_commit_close(commit);
4017 234035bc 2019-06-01 stsp free(commit_id_str);
4018 234035bc 2019-06-01 stsp if (head_ref)
4019 234035bc 2019-06-01 stsp got_ref_close(head_ref);
4020 234035bc 2019-06-01 stsp if (worktree)
4021 234035bc 2019-06-01 stsp got_worktree_close(worktree);
4023 234035bc 2019-06-01 stsp got_repo_close(repo);
4024 c4296144 2019-05-09 stsp return error;
4027 5ef14e63 2019-06-02 stsp __dead static void
4028 5ef14e63 2019-06-02 stsp usage_backout(void)
4030 5ef14e63 2019-06-02 stsp fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
4034 5ef14e63 2019-06-02 stsp static const struct got_error *
4035 5ef14e63 2019-06-02 stsp cmd_backout(int argc, char *argv[])
4037 5ef14e63 2019-06-02 stsp const struct got_error *error = NULL;
4038 5ef14e63 2019-06-02 stsp struct got_worktree *worktree = NULL;
4039 5ef14e63 2019-06-02 stsp struct got_repository *repo = NULL;
4040 5ef14e63 2019-06-02 stsp char *cwd = NULL, *commit_id_str = NULL;
4041 5ef14e63 2019-06-02 stsp struct got_object_id *commit_id = NULL;
4042 5ef14e63 2019-06-02 stsp struct got_commit_object *commit = NULL;
4043 5ef14e63 2019-06-02 stsp struct got_object_qid *pid;
4044 5ef14e63 2019-06-02 stsp struct got_reference *head_ref = NULL;
4045 5ef14e63 2019-06-02 stsp int ch, did_something = 0;
4047 5ef14e63 2019-06-02 stsp while ((ch = getopt(argc, argv, "")) != -1) {
4048 5ef14e63 2019-06-02 stsp switch (ch) {
4050 5ef14e63 2019-06-02 stsp usage_backout();
4051 5ef14e63 2019-06-02 stsp /* NOTREACHED */
4055 5ef14e63 2019-06-02 stsp argc -= optind;
4056 5ef14e63 2019-06-02 stsp argv += optind;
4058 43012d58 2019-07-14 stsp #ifndef PROFILE
4059 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4060 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
4061 43012d58 2019-07-14 stsp err(1, "pledge");
4063 5ef14e63 2019-06-02 stsp if (argc != 1)
4064 5ef14e63 2019-06-02 stsp usage_backout();
4066 5ef14e63 2019-06-02 stsp cwd = getcwd(NULL, 0);
4067 5ef14e63 2019-06-02 stsp if (cwd == NULL) {
4068 5ef14e63 2019-06-02 stsp error = got_error_from_errno("getcwd");
4069 5ef14e63 2019-06-02 stsp goto done;
4071 5ef14e63 2019-06-02 stsp error = got_worktree_open(&worktree, cwd);
4072 5ef14e63 2019-06-02 stsp if (error)
4073 5ef14e63 2019-06-02 stsp goto done;
4075 5ef14e63 2019-06-02 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
4076 5ef14e63 2019-06-02 stsp if (error != NULL)
4077 5ef14e63 2019-06-02 stsp goto done;
4079 5ef14e63 2019-06-02 stsp error = apply_unveil(got_repo_get_path(repo), 0,
4080 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
4081 5ef14e63 2019-06-02 stsp if (error)
4082 5ef14e63 2019-06-02 stsp goto done;
4084 dd88155e 2019-06-29 stsp error = got_repo_match_object_id_prefix(&commit_id, argv[0],
4085 dd88155e 2019-06-29 stsp GOT_OBJ_TYPE_COMMIT, repo);
4086 5ef14e63 2019-06-02 stsp if (error != NULL) {
4087 5ef14e63 2019-06-02 stsp struct got_reference *ref;
4088 5ef14e63 2019-06-02 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
4089 5ef14e63 2019-06-02 stsp goto done;
4090 5ef14e63 2019-06-02 stsp error = got_ref_open(&ref, repo, argv[0], 0);
4091 5ef14e63 2019-06-02 stsp if (error != NULL)
4092 5ef14e63 2019-06-02 stsp goto done;
4093 5ef14e63 2019-06-02 stsp error = got_ref_resolve(&commit_id, repo, ref);
4094 5ef14e63 2019-06-02 stsp got_ref_close(ref);
4095 5ef14e63 2019-06-02 stsp if (error != NULL)
4096 5ef14e63 2019-06-02 stsp goto done;
4098 5ef14e63 2019-06-02 stsp error = got_object_id_str(&commit_id_str, commit_id);
4099 5ef14e63 2019-06-02 stsp if (error)
4100 5ef14e63 2019-06-02 stsp goto done;
4102 5ef14e63 2019-06-02 stsp error = got_ref_open(&head_ref, repo,
4103 5ef14e63 2019-06-02 stsp got_worktree_get_head_ref_name(worktree), 0);
4104 5ef14e63 2019-06-02 stsp if (error != NULL)
4105 5ef14e63 2019-06-02 stsp goto done;
4107 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
4108 5ef14e63 2019-06-02 stsp if (error)
4109 5ef14e63 2019-06-02 stsp goto done;
4111 5ef14e63 2019-06-02 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
4112 5ef14e63 2019-06-02 stsp if (error)
4113 5ef14e63 2019-06-02 stsp goto done;
4114 5ef14e63 2019-06-02 stsp pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
4115 5ef14e63 2019-06-02 stsp if (pid == NULL) {
4116 5ef14e63 2019-06-02 stsp error = got_error(GOT_ERR_ROOT_COMMIT);
4117 5ef14e63 2019-06-02 stsp goto done;
4120 5ef14e63 2019-06-02 stsp error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
4121 5ef14e63 2019-06-02 stsp update_progress, &did_something, check_cancelled, NULL);
4122 5ef14e63 2019-06-02 stsp if (error != NULL)
4123 5ef14e63 2019-06-02 stsp goto done;
4125 5ef14e63 2019-06-02 stsp if (did_something)
4126 a7648d7a 2019-06-02 stsp printf("Backed out commit %s\n", commit_id_str);
4128 5ef14e63 2019-06-02 stsp if (commit)
4129 5ef14e63 2019-06-02 stsp got_object_commit_close(commit);
4130 5ef14e63 2019-06-02 stsp free(commit_id_str);
4131 5ef14e63 2019-06-02 stsp if (head_ref)
4132 5ef14e63 2019-06-02 stsp got_ref_close(head_ref);
4133 818c7501 2019-07-11 stsp if (worktree)
4134 818c7501 2019-07-11 stsp got_worktree_close(worktree);
4136 818c7501 2019-07-11 stsp got_repo_close(repo);
4137 818c7501 2019-07-11 stsp return error;
4140 818c7501 2019-07-11 stsp __dead static void
4141 818c7501 2019-07-11 stsp usage_rebase(void)
4143 af54c8f8 2019-07-11 stsp fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
4144 af54c8f8 2019-07-11 stsp getprogname());
4149 0ebf8283 2019-07-24 stsp trim_logmsg(char *logmsg, int limit)
4152 0ebf8283 2019-07-24 stsp size_t len;
4154 0ebf8283 2019-07-24 stsp len = strlen(logmsg);
4155 0ebf8283 2019-07-24 stsp if (len > limit)
4156 0ebf8283 2019-07-24 stsp len = limit;
4157 0ebf8283 2019-07-24 stsp logmsg[len] = '\0';
4158 0ebf8283 2019-07-24 stsp nl = strchr(logmsg, '\n');
4160 0ebf8283 2019-07-24 stsp *nl = '\0';
4163 0ebf8283 2019-07-24 stsp static const struct got_error *
4164 0ebf8283 2019-07-24 stsp get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
4166 5943eee2 2019-08-13 stsp const struct got_error *err;
4167 5943eee2 2019-08-13 stsp char *logmsg0 = NULL;
4168 5943eee2 2019-08-13 stsp const char *s;
4170 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
4172 5943eee2 2019-08-13 stsp return err;
4174 5943eee2 2019-08-13 stsp s = logmsg0;
4175 5943eee2 2019-08-13 stsp while (isspace((unsigned char)s[0]))
4178 5943eee2 2019-08-13 stsp *logmsg = strdup(s);
4179 5943eee2 2019-08-13 stsp if (*logmsg == NULL) {
4180 5943eee2 2019-08-13 stsp err = got_error_from_errno("strdup");
4181 5943eee2 2019-08-13 stsp goto done;
4184 0ebf8283 2019-07-24 stsp trim_logmsg(*logmsg, limit);
4186 5943eee2 2019-08-13 stsp free(logmsg0);
4187 5943eee2 2019-08-13 stsp return err;
4190 818c7501 2019-07-11 stsp static const struct got_error *
4191 818c7501 2019-07-11 stsp show_rebase_progress(struct got_commit_object *commit,
4192 818c7501 2019-07-11 stsp struct got_object_id *old_id, struct got_object_id *new_id)
4194 818c7501 2019-07-11 stsp const struct got_error *err;
4195 0ebf8283 2019-07-24 stsp char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
4197 818c7501 2019-07-11 stsp err = got_object_id_str(&old_id_str, old_id);
4199 818c7501 2019-07-11 stsp goto done;
4201 ff0d2220 2019-07-11 stsp if (new_id) {
4202 ff0d2220 2019-07-11 stsp err = got_object_id_str(&new_id_str, new_id);
4204 ff0d2220 2019-07-11 stsp goto done;
4207 818c7501 2019-07-11 stsp old_id_str[12] = '\0';
4208 ff0d2220 2019-07-11 stsp if (new_id_str)
4209 ff0d2220 2019-07-11 stsp new_id_str[12] = '\0';
4211 0ebf8283 2019-07-24 stsp err = get_short_logmsg(&logmsg, 42, commit);
4213 0ebf8283 2019-07-24 stsp goto done;
4215 ff0d2220 2019-07-11 stsp printf("%s -> %s: %s\n", old_id_str,
4216 ff0d2220 2019-07-11 stsp new_id_str ? new_id_str : "no-op change", logmsg);
4218 818c7501 2019-07-11 stsp free(old_id_str);
4219 818c7501 2019-07-11 stsp free(new_id_str);
4220 818c7501 2019-07-11 stsp return err;
4223 1ee397ad 2019-07-12 stsp static const struct got_error *
4224 818c7501 2019-07-11 stsp rebase_progress(void *arg, unsigned char status, const char *path)
4226 818c7501 2019-07-11 stsp unsigned char *rebase_status = arg;
4228 818c7501 2019-07-11 stsp while (path[0] == '/')
4230 818c7501 2019-07-11 stsp printf("%c %s\n", status, path);
4232 818c7501 2019-07-11 stsp if (*rebase_status == GOT_STATUS_CONFLICT)
4233 1ee397ad 2019-07-12 stsp return NULL;
4234 818c7501 2019-07-11 stsp if (status == GOT_STATUS_CONFLICT || status == GOT_STATUS_MERGE)
4235 818c7501 2019-07-11 stsp *rebase_status = status;
4236 1ee397ad 2019-07-12 stsp return NULL;
4239 818c7501 2019-07-11 stsp static const struct got_error *
4240 3e3a69f1 2019-07-25 stsp rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
4241 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_reference *new_base_branch,
4242 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_repository *repo)
4244 818c7501 2019-07-11 stsp printf("Switching work tree to %s\n", got_ref_get_name(branch));
4245 3e3a69f1 2019-07-25 stsp return got_worktree_rebase_complete(worktree, fileindex,
4246 818c7501 2019-07-11 stsp new_base_branch, tmp_branch, branch, repo);
4249 818c7501 2019-07-11 stsp static const struct got_error *
4250 01757395 2019-07-12 stsp rebase_commit(struct got_pathlist_head *merged_paths,
4251 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
4252 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch,
4253 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
4255 ff0d2220 2019-07-11 stsp const struct got_error *error;
4256 ff0d2220 2019-07-11 stsp struct got_commit_object *commit;
4257 ff0d2220 2019-07-11 stsp struct got_object_id *new_commit_id;
4259 ff0d2220 2019-07-11 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
4260 ff0d2220 2019-07-11 stsp if (error)
4261 ff0d2220 2019-07-11 stsp return error;
4263 01757395 2019-07-12 stsp error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
4264 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, commit, commit_id, repo);
4265 ff0d2220 2019-07-11 stsp if (error) {
4266 ff0d2220 2019-07-11 stsp if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
4267 ff0d2220 2019-07-11 stsp goto done;
4268 ff0d2220 2019-07-11 stsp error = show_rebase_progress(commit, commit_id, NULL);
4270 ff0d2220 2019-07-11 stsp error = show_rebase_progress(commit, commit_id, new_commit_id);
4271 ff0d2220 2019-07-11 stsp free(new_commit_id);
4274 ff0d2220 2019-07-11 stsp got_object_commit_close(commit);
4275 ff0d2220 2019-07-11 stsp return error;
4278 64c6d990 2019-07-11 stsp struct check_path_prefix_arg {
4279 64c6d990 2019-07-11 stsp const char *path_prefix;
4280 64c6d990 2019-07-11 stsp size_t len;
4281 8ca9bd68 2019-07-25 stsp int errcode;
4284 64c6d990 2019-07-11 stsp static const struct got_error *
4285 8ca9bd68 2019-07-25 stsp check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
4286 64c6d990 2019-07-11 stsp struct got_blob_object *blob2, struct got_object_id *id1,
4287 64c6d990 2019-07-11 stsp struct got_object_id *id2, const char *path1, const char *path2,
4288 64c6d990 2019-07-11 stsp struct got_repository *repo)
4290 64c6d990 2019-07-11 stsp struct check_path_prefix_arg *a = arg;
4292 64c6d990 2019-07-11 stsp if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
4293 64c6d990 2019-07-11 stsp (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
4294 8ca9bd68 2019-07-25 stsp return got_error(a->errcode);
4296 64c6d990 2019-07-11 stsp return NULL;
4299 64c6d990 2019-07-11 stsp static const struct got_error *
4300 8ca9bd68 2019-07-25 stsp check_path_prefix(struct got_object_id *parent_id,
4301 64c6d990 2019-07-11 stsp struct got_object_id *commit_id, const char *path_prefix,
4302 8ca9bd68 2019-07-25 stsp int errcode, struct got_repository *repo)
4304 64c6d990 2019-07-11 stsp const struct got_error *err;
4305 64c6d990 2019-07-11 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
4306 64c6d990 2019-07-11 stsp struct got_commit_object *commit = NULL, *parent_commit = NULL;
4307 64c6d990 2019-07-11 stsp struct check_path_prefix_arg cpp_arg;
4309 64c6d990 2019-07-11 stsp if (got_path_is_root_dir(path_prefix))
4310 64c6d990 2019-07-11 stsp return NULL;
4312 64c6d990 2019-07-11 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
4314 64c6d990 2019-07-11 stsp goto done;
4316 64c6d990 2019-07-11 stsp err = got_object_open_as_commit(&parent_commit, repo, parent_id);
4318 64c6d990 2019-07-11 stsp goto done;
4320 64c6d990 2019-07-11 stsp err = got_object_open_as_tree(&tree1, repo,
4321 64c6d990 2019-07-11 stsp got_object_commit_get_tree_id(parent_commit));
4323 64c6d990 2019-07-11 stsp goto done;
4325 64c6d990 2019-07-11 stsp err = got_object_open_as_tree(&tree2, repo,
4326 64c6d990 2019-07-11 stsp got_object_commit_get_tree_id(commit));
4328 64c6d990 2019-07-11 stsp goto done;
4330 64c6d990 2019-07-11 stsp cpp_arg.path_prefix = path_prefix;
4331 d23ace97 2019-07-25 stsp while (cpp_arg.path_prefix[0] == '/')
4332 d23ace97 2019-07-25 stsp cpp_arg.path_prefix++;
4333 d23ace97 2019-07-25 stsp cpp_arg.len = strlen(cpp_arg.path_prefix);
4334 8ca9bd68 2019-07-25 stsp cpp_arg.errcode = errcode;
4335 8ca9bd68 2019-07-25 stsp err = got_diff_tree(tree1, tree2, "", "", repo,
4336 31b4484f 2019-07-27 stsp check_path_prefix_in_diff, &cpp_arg, 0);
4338 64c6d990 2019-07-11 stsp if (tree1)
4339 64c6d990 2019-07-11 stsp got_object_tree_close(tree1);
4340 64c6d990 2019-07-11 stsp if (tree2)
4341 64c6d990 2019-07-11 stsp got_object_tree_close(tree2);
4342 64c6d990 2019-07-11 stsp if (commit)
4343 64c6d990 2019-07-11 stsp got_object_commit_close(commit);
4344 64c6d990 2019-07-11 stsp if (parent_commit)
4345 64c6d990 2019-07-11 stsp got_object_commit_close(parent_commit);
4346 64c6d990 2019-07-11 stsp return err;
4349 ff0d2220 2019-07-11 stsp static const struct got_error *
4350 8ca9bd68 2019-07-25 stsp collect_commits(struct got_object_id_queue *commits,
4351 af61c510 2019-07-19 stsp struct got_object_id *initial_commit_id,
4352 af61c510 2019-07-19 stsp struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
4353 8ca9bd68 2019-07-25 stsp const char *path_prefix, int path_prefix_errcode,
4354 8ca9bd68 2019-07-25 stsp struct got_repository *repo)
4356 af61c510 2019-07-19 stsp const struct got_error *err = NULL;
4357 af61c510 2019-07-19 stsp struct got_commit_graph *graph = NULL;
4358 af61c510 2019-07-19 stsp struct got_object_id *parent_id = NULL;
4359 af61c510 2019-07-19 stsp struct got_object_qid *qid;
4360 af61c510 2019-07-19 stsp struct got_object_id *commit_id = initial_commit_id;
4362 af61c510 2019-07-19 stsp err = got_commit_graph_open(&graph, initial_commit_id, "/", 1, repo);
4364 af61c510 2019-07-19 stsp return err;
4366 6fb7cd11 2019-08-22 stsp err = got_commit_graph_iter_start(graph, iter_start_id, repo,
4367 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
4369 af61c510 2019-07-19 stsp goto done;
4370 af61c510 2019-07-19 stsp while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
4371 af61c510 2019-07-19 stsp err = got_commit_graph_iter_next(&parent_id, graph);
4372 af61c510 2019-07-19 stsp if (err) {
4373 af61c510 2019-07-19 stsp if (err->code == GOT_ERR_ITER_COMPLETED) {
4374 af61c510 2019-07-19 stsp err = got_error_msg(GOT_ERR_ANCESTRY,
4375 af61c510 2019-07-19 stsp "ran out of commits to rebase before "
4376 af61c510 2019-07-19 stsp "youngest common ancestor commit has "
4377 af61c510 2019-07-19 stsp "been reached?!?");
4378 af61c510 2019-07-19 stsp goto done;
4379 af61c510 2019-07-19 stsp } else if (err->code != GOT_ERR_ITER_NEED_MORE)
4380 af61c510 2019-07-19 stsp goto done;
4381 6fb7cd11 2019-08-22 stsp err = got_commit_graph_fetch_commits(graph, 1, repo,
4382 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
4384 af61c510 2019-07-19 stsp goto done;
4386 8ca9bd68 2019-07-25 stsp err = check_path_prefix(parent_id, commit_id,
4387 8ca9bd68 2019-07-25 stsp path_prefix, path_prefix_errcode, repo);
4389 af61c510 2019-07-19 stsp goto done;
4391 af61c510 2019-07-19 stsp err = got_object_qid_alloc(&qid, commit_id);
4393 af61c510 2019-07-19 stsp goto done;
4394 af61c510 2019-07-19 stsp SIMPLEQ_INSERT_HEAD(commits, qid, entry);
4395 af61c510 2019-07-19 stsp commit_id = parent_id;
4399 af61c510 2019-07-19 stsp got_commit_graph_close(graph);
4400 af61c510 2019-07-19 stsp return err;
4403 af61c510 2019-07-19 stsp static const struct got_error *
4404 818c7501 2019-07-11 stsp cmd_rebase(int argc, char *argv[])
4406 818c7501 2019-07-11 stsp const struct got_error *error = NULL;
4407 818c7501 2019-07-11 stsp struct got_worktree *worktree = NULL;
4408 818c7501 2019-07-11 stsp struct got_repository *repo = NULL;
4409 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex = NULL;
4410 818c7501 2019-07-11 stsp char *cwd = NULL;
4411 818c7501 2019-07-11 stsp struct got_reference *branch = NULL;
4412 818c7501 2019-07-11 stsp struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
4413 818c7501 2019-07-11 stsp struct got_object_id *commit_id = NULL, *parent_id = NULL;
4414 818c7501 2019-07-11 stsp struct got_object_id *resume_commit_id = NULL;
4415 818c7501 2019-07-11 stsp struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
4416 818c7501 2019-07-11 stsp struct got_commit_object *commit = NULL;
4417 818c7501 2019-07-11 stsp int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
4418 818c7501 2019-07-11 stsp unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
4419 818c7501 2019-07-11 stsp struct got_object_id_queue commits;
4420 01757395 2019-07-12 stsp struct got_pathlist_head merged_paths;
4421 818c7501 2019-07-11 stsp const struct got_object_id_queue *parent_ids;
4422 818c7501 2019-07-11 stsp struct got_object_qid *qid, *pid;
4424 818c7501 2019-07-11 stsp SIMPLEQ_INIT(&commits);
4425 01757395 2019-07-12 stsp TAILQ_INIT(&merged_paths);
4427 818c7501 2019-07-11 stsp while ((ch = getopt(argc, argv, "ac")) != -1) {
4428 818c7501 2019-07-11 stsp switch (ch) {
4430 818c7501 2019-07-11 stsp abort_rebase = 1;
4433 818c7501 2019-07-11 stsp continue_rebase = 1;
4436 818c7501 2019-07-11 stsp usage_rebase();
4437 818c7501 2019-07-11 stsp /* NOTREACHED */
4441 818c7501 2019-07-11 stsp argc -= optind;
4442 818c7501 2019-07-11 stsp argv += optind;
4444 43012d58 2019-07-14 stsp #ifndef PROFILE
4445 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4446 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
4447 43012d58 2019-07-14 stsp err(1, "pledge");
4449 818c7501 2019-07-11 stsp if (abort_rebase && continue_rebase)
4450 818c7501 2019-07-11 stsp usage_rebase();
4451 818c7501 2019-07-11 stsp else if (abort_rebase || continue_rebase) {
4452 818c7501 2019-07-11 stsp if (argc != 0)
4453 818c7501 2019-07-11 stsp usage_rebase();
4454 818c7501 2019-07-11 stsp } else if (argc != 1)
4455 818c7501 2019-07-11 stsp usage_rebase();
4457 818c7501 2019-07-11 stsp cwd = getcwd(NULL, 0);
4458 818c7501 2019-07-11 stsp if (cwd == NULL) {
4459 818c7501 2019-07-11 stsp error = got_error_from_errno("getcwd");
4460 818c7501 2019-07-11 stsp goto done;
4462 818c7501 2019-07-11 stsp error = got_worktree_open(&worktree, cwd);
4463 818c7501 2019-07-11 stsp if (error)
4464 818c7501 2019-07-11 stsp goto done;
4466 818c7501 2019-07-11 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
4467 818c7501 2019-07-11 stsp if (error != NULL)
4468 818c7501 2019-07-11 stsp goto done;
4470 818c7501 2019-07-11 stsp error = apply_unveil(got_repo_get_path(repo), 0,
4471 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
4472 818c7501 2019-07-11 stsp if (error)
4473 818c7501 2019-07-11 stsp goto done;
4475 818c7501 2019-07-11 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
4476 818c7501 2019-07-11 stsp if (error)
4477 818c7501 2019-07-11 stsp goto done;
4479 f6794adc 2019-07-23 stsp if (abort_rebase) {
4480 818c7501 2019-07-11 stsp int did_something;
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;
4490 818c7501 2019-07-11 stsp printf("Switching work tree to %s\n",
4491 818c7501 2019-07-11 stsp got_ref_get_symref_target(new_base_branch));
4492 3e3a69f1 2019-07-25 stsp error = got_worktree_rebase_abort(worktree, fileindex, repo,
4493 818c7501 2019-07-11 stsp new_base_branch, update_progress, &did_something);
4494 818c7501 2019-07-11 stsp if (error)
4495 818c7501 2019-07-11 stsp goto done;
4496 818c7501 2019-07-11 stsp printf("Rebase of %s aborted\n", got_ref_get_name(branch));
4497 818c7501 2019-07-11 stsp goto done; /* nothing else to do */
4500 818c7501 2019-07-11 stsp if (continue_rebase) {
4501 f6794adc 2019-07-23 stsp if (!rebase_in_progress) {
4502 f6794adc 2019-07-23 stsp error = got_error(GOT_ERR_NOT_REBASING);
4503 f6794adc 2019-07-23 stsp goto done;
4505 818c7501 2019-07-11 stsp error = got_worktree_rebase_continue(&resume_commit_id,
4506 3e3a69f1 2019-07-25 stsp &new_base_branch, &tmp_branch, &branch, &fileindex,
4507 3e3a69f1 2019-07-25 stsp worktree, repo);
4508 818c7501 2019-07-11 stsp if (error)
4509 818c7501 2019-07-11 stsp goto done;
4511 3e3a69f1 2019-07-25 stsp error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
4512 01757395 2019-07-12 stsp resume_commit_id, repo);
4513 818c7501 2019-07-11 stsp if (error)
4514 818c7501 2019-07-11 stsp goto done;
4516 ff0d2220 2019-07-11 stsp yca_id = got_object_id_dup(resume_commit_id);
4517 ff0d2220 2019-07-11 stsp if (yca_id == NULL) {
4518 818c7501 2019-07-11 stsp error = got_error_from_errno("got_object_id_dup");
4519 818c7501 2019-07-11 stsp goto done;
4522 818c7501 2019-07-11 stsp error = got_ref_open(&branch, repo, argv[0], 0);
4523 818c7501 2019-07-11 stsp if (error != NULL)
4524 818c7501 2019-07-11 stsp goto done;
4527 ff0d2220 2019-07-11 stsp error = got_ref_resolve(&branch_head_commit_id, repo, branch);
4528 ff0d2220 2019-07-11 stsp if (error)
4529 ff0d2220 2019-07-11 stsp goto done;
4531 ff0d2220 2019-07-11 stsp if (!continue_rebase) {
4532 a51a74b3 2019-07-27 stsp struct got_object_id *base_commit_id;
4534 a51a74b3 2019-07-27 stsp base_commit_id = got_worktree_get_base_commit_id(worktree);
4535 818c7501 2019-07-11 stsp error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
4536 6fb7cd11 2019-08-22 stsp base_commit_id, branch_head_commit_id, repo,
4537 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
4538 818c7501 2019-07-11 stsp if (error)
4539 818c7501 2019-07-11 stsp goto done;
4540 818c7501 2019-07-11 stsp if (yca_id == NULL) {
4541 818c7501 2019-07-11 stsp error = got_error_msg(GOT_ERR_ANCESTRY,
4542 818c7501 2019-07-11 stsp "specified branch shares no common ancestry "
4543 818c7501 2019-07-11 stsp "with work tree's branch");
4544 818c7501 2019-07-11 stsp goto done;
4547 a51a74b3 2019-07-27 stsp error = check_same_branch(base_commit_id, branch, yca_id, repo);
4548 a51a74b3 2019-07-27 stsp if (error) {
4549 a51a74b3 2019-07-27 stsp if (error->code != GOT_ERR_ANCESTRY)
4550 a51a74b3 2019-07-27 stsp goto done;
4551 a51a74b3 2019-07-27 stsp error = NULL;
4553 a51a74b3 2019-07-27 stsp error = got_error_msg(GOT_ERR_SAME_BRANCH,
4554 a51a74b3 2019-07-27 stsp "specified branch resolves to a commit which "
4555 a51a74b3 2019-07-27 stsp "is already contained in work tree's branch");
4556 a51a74b3 2019-07-27 stsp goto done;
4558 818c7501 2019-07-11 stsp error = got_worktree_rebase_prepare(&new_base_branch,
4559 3e3a69f1 2019-07-25 stsp &tmp_branch, &fileindex, worktree, branch, repo);
4560 818c7501 2019-07-11 stsp if (error)
4561 818c7501 2019-07-11 stsp goto done;
4564 ff0d2220 2019-07-11 stsp commit_id = branch_head_commit_id;
4565 818c7501 2019-07-11 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
4566 818c7501 2019-07-11 stsp if (error)
4567 818c7501 2019-07-11 stsp goto done;
4569 818c7501 2019-07-11 stsp parent_ids = got_object_commit_get_parent_ids(commit);
4570 818c7501 2019-07-11 stsp pid = SIMPLEQ_FIRST(parent_ids);
4571 fc66b545 2019-08-12 stsp if (pid == NULL) {
4572 fc66b545 2019-08-12 stsp if (!continue_rebase) {
4573 fc66b545 2019-08-12 stsp int did_something;
4574 fc66b545 2019-08-12 stsp error = got_worktree_rebase_abort(worktree, fileindex,
4575 fc66b545 2019-08-12 stsp repo, new_base_branch, update_progress,
4576 fc66b545 2019-08-12 stsp &did_something);
4577 fc66b545 2019-08-12 stsp if (error)
4578 fc66b545 2019-08-12 stsp goto done;
4579 fc66b545 2019-08-12 stsp printf("Rebase of %s aborted\n",
4580 fc66b545 2019-08-12 stsp got_ref_get_name(branch));
4582 fc66b545 2019-08-12 stsp error = got_error(GOT_ERR_EMPTY_REBASE);
4583 fc66b545 2019-08-12 stsp goto done;
4585 8ca9bd68 2019-07-25 stsp error = collect_commits(&commits, commit_id, pid->id,
4586 8ca9bd68 2019-07-25 stsp yca_id, got_worktree_get_path_prefix(worktree),
4587 8ca9bd68 2019-07-25 stsp GOT_ERR_REBASE_PATH, repo);
4588 818c7501 2019-07-11 stsp got_object_commit_close(commit);
4589 818c7501 2019-07-11 stsp commit = NULL;
4590 818c7501 2019-07-11 stsp if (error)
4591 818c7501 2019-07-11 stsp goto done;
4593 818c7501 2019-07-11 stsp if (SIMPLEQ_EMPTY(&commits)) {
4594 ff0d2220 2019-07-11 stsp if (continue_rebase)
4595 3e3a69f1 2019-07-25 stsp error = rebase_complete(worktree, fileindex,
4596 3e3a69f1 2019-07-25 stsp branch, new_base_branch, tmp_branch, repo);
4598 ff0d2220 2019-07-11 stsp error = got_error(GOT_ERR_EMPTY_REBASE);
4599 818c7501 2019-07-11 stsp goto done;
4602 818c7501 2019-07-11 stsp pid = NULL;
4603 818c7501 2019-07-11 stsp SIMPLEQ_FOREACH(qid, &commits, entry) {
4604 818c7501 2019-07-11 stsp commit_id = qid->id;
4605 818c7501 2019-07-11 stsp parent_id = pid ? pid->id : yca_id;
4606 818c7501 2019-07-11 stsp pid = qid;
4608 01757395 2019-07-12 stsp error = got_worktree_rebase_merge_files(&merged_paths,
4609 3e3a69f1 2019-07-25 stsp worktree, fileindex, parent_id, commit_id, repo,
4610 3e3a69f1 2019-07-25 stsp rebase_progress, &rebase_status, check_cancelled, NULL);
4611 818c7501 2019-07-11 stsp if (error)
4612 818c7501 2019-07-11 stsp goto done;
4614 01757395 2019-07-12 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
4615 01757395 2019-07-12 stsp got_worktree_rebase_pathlist_free(&merged_paths);
4619 3e3a69f1 2019-07-25 stsp error = rebase_commit(&merged_paths, worktree, fileindex,
4620 3e3a69f1 2019-07-25 stsp tmp_branch, commit_id, repo);
4621 01757395 2019-07-12 stsp got_worktree_rebase_pathlist_free(&merged_paths);
4622 818c7501 2019-07-11 stsp if (error)
4623 818c7501 2019-07-11 stsp goto done;
4626 818c7501 2019-07-11 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
4627 3e3a69f1 2019-07-25 stsp error = got_worktree_rebase_postpone(worktree, fileindex);
4628 818c7501 2019-07-11 stsp if (error)
4629 818c7501 2019-07-11 stsp goto done;
4630 818c7501 2019-07-11 stsp error = got_error_msg(GOT_ERR_CONFLICTS,
4631 11495e04 2019-07-12 stsp "conflicts must be resolved before rebasing can continue");
4633 3e3a69f1 2019-07-25 stsp error = rebase_complete(worktree, fileindex, branch,
4634 3e3a69f1 2019-07-25 stsp new_base_branch, tmp_branch, repo);
4636 818c7501 2019-07-11 stsp got_object_id_queue_free(&commits);
4637 818c7501 2019-07-11 stsp free(branch_head_commit_id);
4638 818c7501 2019-07-11 stsp free(resume_commit_id);
4639 818c7501 2019-07-11 stsp free(yca_id);
4640 818c7501 2019-07-11 stsp if (commit)
4641 818c7501 2019-07-11 stsp got_object_commit_close(commit);
4642 818c7501 2019-07-11 stsp if (branch)
4643 818c7501 2019-07-11 stsp got_ref_close(branch);
4644 818c7501 2019-07-11 stsp if (new_base_branch)
4645 818c7501 2019-07-11 stsp got_ref_close(new_base_branch);
4646 818c7501 2019-07-11 stsp if (tmp_branch)
4647 818c7501 2019-07-11 stsp got_ref_close(tmp_branch);
4648 5ef14e63 2019-06-02 stsp if (worktree)
4649 5ef14e63 2019-06-02 stsp got_worktree_close(worktree);
4651 5ef14e63 2019-06-02 stsp got_repo_close(repo);
4652 5ef14e63 2019-06-02 stsp return error;
4655 0ebf8283 2019-07-24 stsp __dead static void
4656 0ebf8283 2019-07-24 stsp usage_histedit(void)
4658 f3055044 2019-08-07 stsp fprintf(stderr, "usage: %s histedit [-a] [-c] [-F histedit-script]\n",
4659 0ebf8283 2019-07-24 stsp getprogname());
4663 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_PICK 'p'
4664 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_EDIT 'e'
4665 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_FOLD 'f'
4666 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_DROP 'd'
4667 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_MESG 'm'
4669 0ebf8283 2019-07-24 stsp static struct got_histedit_cmd {
4670 0ebf8283 2019-07-24 stsp unsigned char code;
4671 0ebf8283 2019-07-24 stsp const char *name;
4672 0ebf8283 2019-07-24 stsp const char *desc;
4673 0ebf8283 2019-07-24 stsp } got_histedit_cmds[] = {
4674 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_PICK, "pick", "use commit" },
4675 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
4676 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_FOLD, "fold", "combine with commit below" },
4677 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
4678 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_MESG, "mesg",
4679 0ebf8283 2019-07-24 stsp "single-line log message for commit above (open editor if empty)" },
4682 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry {
4683 0ebf8283 2019-07-24 stsp TAILQ_ENTRY(got_histedit_list_entry) entry;
4684 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id;
4685 0ebf8283 2019-07-24 stsp const struct got_histedit_cmd *cmd;
4686 0ebf8283 2019-07-24 stsp char *logmsg;
4688 0ebf8283 2019-07-24 stsp TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
4690 0ebf8283 2019-07-24 stsp static const struct got_error *
4691 0ebf8283 2019-07-24 stsp histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
4692 0ebf8283 2019-07-24 stsp FILE *f, struct got_repository *repo)
4694 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
4695 0ebf8283 2019-07-24 stsp char *logmsg = NULL, *id_str = NULL;
4696 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
4699 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
4701 0ebf8283 2019-07-24 stsp goto done;
4703 0ebf8283 2019-07-24 stsp err = get_short_logmsg(&logmsg, 34, commit);
4705 0ebf8283 2019-07-24 stsp goto done;
4707 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, commit_id);
4709 0ebf8283 2019-07-24 stsp goto done;
4711 0ebf8283 2019-07-24 stsp n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
4712 0ebf8283 2019-07-24 stsp if (n < 0)
4713 0ebf8283 2019-07-24 stsp err = got_ferror(f, GOT_ERR_IO);
4715 0ebf8283 2019-07-24 stsp if (commit)
4716 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
4717 0ebf8283 2019-07-24 stsp free(id_str);
4718 0ebf8283 2019-07-24 stsp free(logmsg);
4719 0ebf8283 2019-07-24 stsp return err;
4722 0ebf8283 2019-07-24 stsp static const struct got_error *
4723 0ebf8283 2019-07-24 stsp histedit_write_commit_list(struct got_object_id_queue *commits, FILE *f,
4724 0ebf8283 2019-07-24 stsp struct got_repository *repo)
4726 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
4727 0ebf8283 2019-07-24 stsp struct got_object_qid *qid;
4729 0ebf8283 2019-07-24 stsp if (SIMPLEQ_EMPTY(commits))
4730 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_EMPTY_HISTEDIT);
4732 0ebf8283 2019-07-24 stsp SIMPLEQ_FOREACH(qid, commits, entry) {
4733 0ebf8283 2019-07-24 stsp err = histedit_write_commit(qid->id, got_histedit_cmds[0].name,
4739 0ebf8283 2019-07-24 stsp return err;
4742 0ebf8283 2019-07-24 stsp static const struct got_error *
4743 0ebf8283 2019-07-24 stsp write_cmd_list(FILE *f)
4745 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
4748 0ebf8283 2019-07-24 stsp n = fprintf(f, "# Available histedit commands:\n");
4749 0ebf8283 2019-07-24 stsp if (n < 0)
4750 0ebf8283 2019-07-24 stsp return got_ferror(f, GOT_ERR_IO);
4752 0ebf8283 2019-07-24 stsp for (i = 0; i < nitems(got_histedit_cmds); i++) {
4753 0ebf8283 2019-07-24 stsp struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
4754 0ebf8283 2019-07-24 stsp n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
4755 0ebf8283 2019-07-24 stsp cmd->desc);
4756 0ebf8283 2019-07-24 stsp if (n < 0) {
4757 0ebf8283 2019-07-24 stsp err = got_ferror(f, GOT_ERR_IO);
4761 0ebf8283 2019-07-24 stsp n = fprintf(f, "# Commits will be processed in order from top to "
4762 0ebf8283 2019-07-24 stsp "bottom of this file.\n");
4763 0ebf8283 2019-07-24 stsp if (n < 0)
4764 0ebf8283 2019-07-24 stsp return got_ferror(f, GOT_ERR_IO);
4765 0ebf8283 2019-07-24 stsp return err;
4768 0ebf8283 2019-07-24 stsp static const struct got_error *
4769 0ebf8283 2019-07-24 stsp histedit_syntax_error(int lineno)
4771 0ebf8283 2019-07-24 stsp static char msg[42];
4774 0ebf8283 2019-07-24 stsp ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
4776 0ebf8283 2019-07-24 stsp if (ret == -1 || ret >= sizeof(msg))
4777 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_HISTEDIT_SYNTAX);
4779 0ebf8283 2019-07-24 stsp return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
4782 0ebf8283 2019-07-24 stsp static const struct got_error *
4783 0ebf8283 2019-07-24 stsp append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
4784 0ebf8283 2019-07-24 stsp char *logmsg, struct got_repository *repo)
4786 0ebf8283 2019-07-24 stsp const struct got_error *err;
4787 0ebf8283 2019-07-24 stsp struct got_commit_object *folded_commit = NULL;
4788 5943eee2 2019-08-13 stsp char *id_str, *folded_logmsg = NULL;
4790 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, hle->commit_id);
4792 0ebf8283 2019-07-24 stsp return err;
4794 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
4796 0ebf8283 2019-07-24 stsp goto done;
4798 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
4800 5943eee2 2019-08-13 stsp goto done;
4801 0ebf8283 2019-07-24 stsp if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
4802 0ebf8283 2019-07-24 stsp logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
4803 5943eee2 2019-08-13 stsp folded_logmsg) == -1) {
4804 0ebf8283 2019-07-24 stsp err = got_error_from_errno("asprintf");
4805 0ebf8283 2019-07-24 stsp goto done;
4808 0ebf8283 2019-07-24 stsp if (folded_commit)
4809 0ebf8283 2019-07-24 stsp got_object_commit_close(folded_commit);
4810 0ebf8283 2019-07-24 stsp free(id_str);
4811 5943eee2 2019-08-13 stsp free(folded_logmsg);
4812 0ebf8283 2019-07-24 stsp return err;
4815 0ebf8283 2019-07-24 stsp static struct got_histedit_list_entry *
4816 0ebf8283 2019-07-24 stsp get_folded_commits(struct got_histedit_list_entry *hle)
4818 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *prev, *folded = NULL;
4820 0ebf8283 2019-07-24 stsp prev = TAILQ_PREV(hle, got_histedit_list, entry);
4821 3f9de99f 2019-07-24 stsp while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
4822 3f9de99f 2019-07-24 stsp prev->cmd->code == GOT_HISTEDIT_DROP)) {
4823 3f9de99f 2019-07-24 stsp if (prev->cmd->code == GOT_HISTEDIT_FOLD)
4824 3f9de99f 2019-07-24 stsp folded = prev;
4825 0ebf8283 2019-07-24 stsp prev = TAILQ_PREV(prev, got_histedit_list, entry);
4828 0ebf8283 2019-07-24 stsp return folded;
4831 0ebf8283 2019-07-24 stsp static const struct got_error *
4832 0ebf8283 2019-07-24 stsp histedit_edit_logmsg(struct got_histedit_list_entry *hle,
4833 0ebf8283 2019-07-24 stsp struct got_repository *repo)
4835 5943eee2 2019-08-13 stsp char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
4836 0ebf8283 2019-07-24 stsp char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
4837 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
4838 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
4840 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *folded = NULL;
4842 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&commit, repo, hle->commit_id);
4844 0ebf8283 2019-07-24 stsp return err;
4846 0ebf8283 2019-07-24 stsp folded = get_folded_commits(hle);
4847 0ebf8283 2019-07-24 stsp if (folded) {
4848 0ebf8283 2019-07-24 stsp while (folded != hle) {
4849 3f9de99f 2019-07-24 stsp if (folded->cmd->code == GOT_HISTEDIT_DROP) {
4850 3f9de99f 2019-07-24 stsp folded = TAILQ_NEXT(folded, entry);
4853 0ebf8283 2019-07-24 stsp err = append_folded_commit_msg(&new_msg, folded,
4854 0ebf8283 2019-07-24 stsp logmsg, repo);
4856 0ebf8283 2019-07-24 stsp goto done;
4857 0ebf8283 2019-07-24 stsp free(logmsg);
4858 0ebf8283 2019-07-24 stsp logmsg = new_msg;
4859 0ebf8283 2019-07-24 stsp folded = TAILQ_NEXT(folded, entry);
4863 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, hle->commit_id);
4865 0ebf8283 2019-07-24 stsp goto done;
4866 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&orig_logmsg, commit);
4868 5943eee2 2019-08-13 stsp goto done;
4869 0ebf8283 2019-07-24 stsp if (asprintf(&new_msg,
4870 0ebf8283 2019-07-24 stsp "%s\n# original log message of commit %s: %s",
4871 5943eee2 2019-08-13 stsp logmsg ? logmsg : "", id_str, orig_logmsg) == -1) {
4872 0ebf8283 2019-07-24 stsp err = got_error_from_errno("asprintf");
4873 0ebf8283 2019-07-24 stsp goto done;
4875 0ebf8283 2019-07-24 stsp free(logmsg);
4876 0ebf8283 2019-07-24 stsp logmsg = new_msg;
4878 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, hle->commit_id);
4880 0ebf8283 2019-07-24 stsp goto done;
4882 0ebf8283 2019-07-24 stsp err = got_opentemp_named_fd(&logmsg_path, &fd, "/tmp/got-logmsg");
4884 0ebf8283 2019-07-24 stsp goto done;
4886 0ebf8283 2019-07-24 stsp dprintf(fd, logmsg);
4887 0ebf8283 2019-07-24 stsp close(fd);
4889 0ebf8283 2019-07-24 stsp err = get_editor(&editor);
4891 0ebf8283 2019-07-24 stsp goto done;
4893 0ebf8283 2019-07-24 stsp err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg);
4894 0ebf8283 2019-07-24 stsp if (err) {
4895 0ebf8283 2019-07-24 stsp if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
4896 0ebf8283 2019-07-24 stsp goto done;
4897 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&hle->logmsg, commit);
4900 0ebf8283 2019-07-24 stsp if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
4901 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("unlink", logmsg_path);
4902 0ebf8283 2019-07-24 stsp free(logmsg_path);
4903 0ebf8283 2019-07-24 stsp free(logmsg);
4904 5943eee2 2019-08-13 stsp free(orig_logmsg);
4905 0ebf8283 2019-07-24 stsp free(editor);
4906 0ebf8283 2019-07-24 stsp if (commit)
4907 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
4908 0ebf8283 2019-07-24 stsp return err;
4911 0ebf8283 2019-07-24 stsp static const struct got_error *
4912 0ebf8283 2019-07-24 stsp histedit_parse_list(struct got_histedit_list *histedit_cmds,
4913 0ebf8283 2019-07-24 stsp FILE *f, struct got_repository *repo)
4915 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
4916 0ebf8283 2019-07-24 stsp char *line = NULL, *p, *end;
4917 0ebf8283 2019-07-24 stsp size_t size;
4918 0ebf8283 2019-07-24 stsp ssize_t len;
4919 0ebf8283 2019-07-24 stsp int lineno = 0, i;
4920 0ebf8283 2019-07-24 stsp const struct got_histedit_cmd *cmd;
4921 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id = NULL;
4922 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle = NULL;
4924 0ebf8283 2019-07-24 stsp for (;;) {
4925 0ebf8283 2019-07-24 stsp len = getline(&line, &size, f);
4926 0ebf8283 2019-07-24 stsp if (len == -1) {
4927 0ebf8283 2019-07-24 stsp const struct got_error *getline_err;
4928 0ebf8283 2019-07-24 stsp if (feof(f))
4930 0ebf8283 2019-07-24 stsp getline_err = got_error_from_errno("getline");
4931 0ebf8283 2019-07-24 stsp err = got_ferror(f, getline_err->code);
4936 0ebf8283 2019-07-24 stsp while (isspace((unsigned char)p[0]))
4938 0ebf8283 2019-07-24 stsp if (p[0] == '#' || p[0] == '\0') {
4939 0ebf8283 2019-07-24 stsp free(line);
4940 0ebf8283 2019-07-24 stsp line = NULL;
4943 0ebf8283 2019-07-24 stsp cmd = NULL;
4944 0ebf8283 2019-07-24 stsp for (i = 0; i < nitems(got_histedit_cmds); i++) {
4945 0ebf8283 2019-07-24 stsp cmd = &got_histedit_cmds[i];
4946 0ebf8283 2019-07-24 stsp if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
4947 0ebf8283 2019-07-24 stsp isspace((unsigned char)p[strlen(cmd->name)])) {
4948 0ebf8283 2019-07-24 stsp p += strlen(cmd->name);
4951 0ebf8283 2019-07-24 stsp if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
4956 6c1844f6 2019-07-25 stsp if (i == nitems(got_histedit_cmds)) {
4957 0ebf8283 2019-07-24 stsp err = histedit_syntax_error(lineno);
4960 0ebf8283 2019-07-24 stsp while (isspace((unsigned char)p[0]))
4962 0ebf8283 2019-07-24 stsp if (cmd->code == GOT_HISTEDIT_MESG) {
4963 0ebf8283 2019-07-24 stsp if (hle == NULL || hle->logmsg != NULL) {
4964 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_HISTEDIT_CMD);
4967 0ebf8283 2019-07-24 stsp if (p[0] == '\0') {
4968 0ebf8283 2019-07-24 stsp err = histedit_edit_logmsg(hle, repo);
4972 0ebf8283 2019-07-24 stsp hle->logmsg = strdup(p);
4973 0ebf8283 2019-07-24 stsp if (hle->logmsg == NULL) {
4974 0ebf8283 2019-07-24 stsp err = got_error_from_errno("strdup");
4978 0ebf8283 2019-07-24 stsp free(line);
4979 0ebf8283 2019-07-24 stsp line = NULL;
4983 0ebf8283 2019-07-24 stsp while (end[0] && !isspace((unsigned char)end[0]))
4985 0ebf8283 2019-07-24 stsp *end = '\0';
4987 0ebf8283 2019-07-24 stsp err = got_object_resolve_id_str(&commit_id, repo, p);
4988 0ebf8283 2019-07-24 stsp if (err) {
4989 0ebf8283 2019-07-24 stsp /* override error code */
4990 0ebf8283 2019-07-24 stsp err = histedit_syntax_error(lineno);
4994 0ebf8283 2019-07-24 stsp hle = malloc(sizeof(*hle));
4995 0ebf8283 2019-07-24 stsp if (hle == NULL) {
4996 0ebf8283 2019-07-24 stsp err = got_error_from_errno("malloc");
4999 0ebf8283 2019-07-24 stsp hle->cmd = cmd;
5000 0ebf8283 2019-07-24 stsp hle->commit_id = commit_id;
5001 0ebf8283 2019-07-24 stsp hle->logmsg = NULL;
5002 0ebf8283 2019-07-24 stsp commit_id = NULL;
5003 0ebf8283 2019-07-24 stsp free(line);
5004 0ebf8283 2019-07-24 stsp line = NULL;
5005 0ebf8283 2019-07-24 stsp TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
5008 0ebf8283 2019-07-24 stsp free(line);
5009 0ebf8283 2019-07-24 stsp free(commit_id);
5010 0ebf8283 2019-07-24 stsp return err;
5013 0ebf8283 2019-07-24 stsp static const struct got_error *
5014 bfce7f83 2019-07-27 stsp histedit_check_script(struct got_histedit_list *histedit_cmds,
5015 bfce7f83 2019-07-27 stsp struct got_object_id_queue *commits, struct got_repository *repo)
5017 bfce7f83 2019-07-27 stsp const struct got_error *err = NULL;
5018 bfce7f83 2019-07-27 stsp struct got_object_qid *qid;
5019 bfce7f83 2019-07-27 stsp struct got_histedit_list_entry *hle;
5020 bfce7f83 2019-07-27 stsp static char msg[80];
5021 bfce7f83 2019-07-27 stsp char *id_str;
5023 bfce7f83 2019-07-27 stsp if (TAILQ_EMPTY(histedit_cmds))
5024 bfce7f83 2019-07-27 stsp return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
5025 bfce7f83 2019-07-27 stsp "histedit script contains no commands");
5026 a0de39f3 2019-08-09 stsp if (SIMPLEQ_EMPTY(commits))
5027 a0de39f3 2019-08-09 stsp return got_error(GOT_ERR_EMPTY_HISTEDIT);
5029 bfce7f83 2019-07-27 stsp SIMPLEQ_FOREACH(qid, commits, entry) {
5030 bfce7f83 2019-07-27 stsp TAILQ_FOREACH(hle, histedit_cmds, entry) {
5031 bfce7f83 2019-07-27 stsp if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
5034 bfce7f83 2019-07-27 stsp if (hle == NULL) {
5035 bfce7f83 2019-07-27 stsp err = got_object_id_str(&id_str, qid->id);
5037 bfce7f83 2019-07-27 stsp return err;
5038 bfce7f83 2019-07-27 stsp snprintf(msg, sizeof(msg),
5039 bfce7f83 2019-07-27 stsp "commit %s missing from histedit script", id_str);
5040 bfce7f83 2019-07-27 stsp free(id_str);
5041 bfce7f83 2019-07-27 stsp return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
5045 0def28b1 2019-08-17 stsp hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
5046 0def28b1 2019-08-17 stsp if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
5047 bfce7f83 2019-07-27 stsp return got_error_msg(GOT_ERR_HISTEDIT_CMD,
5048 bfce7f83 2019-07-27 stsp "last commit in histedit script cannot be folded");
5050 bfce7f83 2019-07-27 stsp return NULL;
5053 bfce7f83 2019-07-27 stsp static const struct got_error *
5054 0ebf8283 2019-07-24 stsp histedit_run_editor(struct got_histedit_list *histedit_cmds,
5055 bfce7f83 2019-07-27 stsp const char *path, struct got_object_id_queue *commits,
5056 bfce7f83 2019-07-27 stsp struct got_repository *repo)
5058 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
5059 0ebf8283 2019-07-24 stsp char *editor;
5060 0ebf8283 2019-07-24 stsp FILE *f = NULL;
5062 0ebf8283 2019-07-24 stsp err = get_editor(&editor);
5064 0ebf8283 2019-07-24 stsp return err;
5066 0ebf8283 2019-07-24 stsp if (spawn_editor(editor, path) == -1) {
5067 0ebf8283 2019-07-24 stsp err = got_error_from_errno("failed spawning editor");
5068 0ebf8283 2019-07-24 stsp goto done;
5071 0ebf8283 2019-07-24 stsp f = fopen(path, "r");
5072 0ebf8283 2019-07-24 stsp if (f == NULL) {
5073 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fopen");
5074 0ebf8283 2019-07-24 stsp goto done;
5076 0ebf8283 2019-07-24 stsp err = histedit_parse_list(histedit_cmds, f, repo);
5078 bfce7f83 2019-07-27 stsp goto done;
5080 bfce7f83 2019-07-27 stsp err = histedit_check_script(histedit_cmds, commits, repo);
5082 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
5083 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
5084 0ebf8283 2019-07-24 stsp free(editor);
5085 0ebf8283 2019-07-24 stsp return err;
5088 0ebf8283 2019-07-24 stsp static const struct got_error *
5089 bfce7f83 2019-07-27 stsp histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
5090 0ebf8283 2019-07-24 stsp struct got_object_id_queue *, const char *, struct got_repository *);
5092 0ebf8283 2019-07-24 stsp static const struct got_error *
5093 0ebf8283 2019-07-24 stsp histedit_edit_script(struct got_histedit_list *histedit_cmds,
5094 0ebf8283 2019-07-24 stsp struct got_object_id_queue *commits, struct got_repository *repo)
5096 0ebf8283 2019-07-24 stsp const struct got_error *err;
5097 0ebf8283 2019-07-24 stsp FILE *f = NULL;
5098 0ebf8283 2019-07-24 stsp char *path = NULL;
5100 0ebf8283 2019-07-24 stsp err = got_opentemp_named(&path, &f, "got-histedit");
5102 0ebf8283 2019-07-24 stsp return err;
5104 0ebf8283 2019-07-24 stsp err = write_cmd_list(f);
5106 0ebf8283 2019-07-24 stsp goto done;
5108 0ebf8283 2019-07-24 stsp err = histedit_write_commit_list(commits, f, repo);
5110 0ebf8283 2019-07-24 stsp goto done;
5112 0ebf8283 2019-07-24 stsp if (fclose(f) != 0) {
5113 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
5114 0ebf8283 2019-07-24 stsp goto done;
5118 bfce7f83 2019-07-27 stsp err = histedit_run_editor(histedit_cmds, path, commits, repo);
5119 0ebf8283 2019-07-24 stsp if (err) {
5120 bfce7f83 2019-07-27 stsp if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
5121 bfce7f83 2019-07-27 stsp err->code != GOT_ERR_HISTEDIT_CMD)
5122 0ebf8283 2019-07-24 stsp goto done;
5123 bfce7f83 2019-07-27 stsp err = histedit_edit_list_retry(histedit_cmds, err,
5124 0ebf8283 2019-07-24 stsp commits, path, repo);
5127 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
5128 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
5129 0ebf8283 2019-07-24 stsp if (path && unlink(path) != 0 && err == NULL)
5130 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("unlink", path);
5131 0ebf8283 2019-07-24 stsp free(path);
5132 0ebf8283 2019-07-24 stsp return err;
5135 0ebf8283 2019-07-24 stsp static const struct got_error *
5136 0ebf8283 2019-07-24 stsp histedit_save_list(struct got_histedit_list *histedit_cmds,
5137 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
5139 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
5140 0ebf8283 2019-07-24 stsp char *path = NULL;
5141 0ebf8283 2019-07-24 stsp FILE *f = NULL;
5142 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle;
5143 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
5145 c3022ba5 2019-07-27 stsp err = got_worktree_get_histedit_script_path(&path, worktree);
5147 0ebf8283 2019-07-24 stsp return err;
5149 0ebf8283 2019-07-24 stsp f = fopen(path, "w");
5150 0ebf8283 2019-07-24 stsp if (f == NULL) {
5151 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("fopen", path);
5152 0ebf8283 2019-07-24 stsp goto done;
5154 0ebf8283 2019-07-24 stsp TAILQ_FOREACH(hle, histedit_cmds, entry) {
5155 0ebf8283 2019-07-24 stsp err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
5160 0ebf8283 2019-07-24 stsp if (hle->logmsg) {
5161 0ebf8283 2019-07-24 stsp int n = fprintf(f, "%c %s\n",
5162 0ebf8283 2019-07-24 stsp GOT_HISTEDIT_MESG, hle->logmsg);
5163 0ebf8283 2019-07-24 stsp if (n < 0) {
5164 0ebf8283 2019-07-24 stsp err = got_ferror(f, GOT_ERR_IO);
5170 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
5171 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
5172 0ebf8283 2019-07-24 stsp free(path);
5173 0ebf8283 2019-07-24 stsp if (commit)
5174 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
5175 0ebf8283 2019-07-24 stsp return err;
5179 bfce7f83 2019-07-27 stsp histedit_free_list(struct got_histedit_list *histedit_cmds)
5181 bfce7f83 2019-07-27 stsp struct got_histedit_list_entry *hle;
5183 bfce7f83 2019-07-27 stsp while ((hle = TAILQ_FIRST(histedit_cmds))) {
5184 bfce7f83 2019-07-27 stsp TAILQ_REMOVE(histedit_cmds, hle, entry);
5185 bfce7f83 2019-07-27 stsp free(hle);
5189 0ebf8283 2019-07-24 stsp static const struct got_error *
5190 0ebf8283 2019-07-24 stsp histedit_load_list(struct got_histedit_list *histedit_cmds,
5191 0ebf8283 2019-07-24 stsp const char *path, struct got_repository *repo)
5193 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
5194 0ebf8283 2019-07-24 stsp FILE *f = NULL;
5196 0ebf8283 2019-07-24 stsp f = fopen(path, "r");
5197 0ebf8283 2019-07-24 stsp if (f == NULL) {
5198 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("fopen", path);
5199 0ebf8283 2019-07-24 stsp goto done;
5202 0ebf8283 2019-07-24 stsp err = histedit_parse_list(histedit_cmds, f, repo);
5204 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
5205 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
5206 0ebf8283 2019-07-24 stsp return err;
5209 0ebf8283 2019-07-24 stsp static const struct got_error *
5210 0ebf8283 2019-07-24 stsp histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
5211 bfce7f83 2019-07-27 stsp const struct got_error *edit_err, struct got_object_id_queue *commits,
5212 0ebf8283 2019-07-24 stsp const char *path, struct got_repository *repo)
5214 bfce7f83 2019-07-27 stsp const struct got_error *err = NULL, *prev_err = edit_err;
5215 0ebf8283 2019-07-24 stsp int resp = ' ';
5217 b006047e 2019-07-25 stsp while (resp != 'c' && resp != 'r' && resp != 'a') {
5218 0ebf8283 2019-07-24 stsp printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
5219 bfce7f83 2019-07-27 stsp "or (a)bort: ", getprogname(), prev_err->msg);
5220 0ebf8283 2019-07-24 stsp resp = getchar();
5221 a61a4414 2019-08-07 stsp if (resp == '\n')
5222 a61a4414 2019-08-07 stsp resp = getchar();
5223 426ebf2e 2019-07-25 stsp if (resp == 'c') {
5224 bfce7f83 2019-07-27 stsp histedit_free_list(histedit_cmds);
5225 bfce7f83 2019-07-27 stsp err = histedit_run_editor(histedit_cmds, path, commits,
5227 426ebf2e 2019-07-25 stsp if (err) {
5228 bfce7f83 2019-07-27 stsp if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
5229 bfce7f83 2019-07-27 stsp err->code != GOT_ERR_HISTEDIT_CMD)
5231 bfce7f83 2019-07-27 stsp prev_err = err;
5232 426ebf2e 2019-07-25 stsp resp = ' ';
5236 426ebf2e 2019-07-25 stsp } else if (resp == 'r') {
5237 bfce7f83 2019-07-27 stsp histedit_free_list(histedit_cmds);
5238 0ebf8283 2019-07-24 stsp err = histedit_edit_script(histedit_cmds,
5239 0ebf8283 2019-07-24 stsp commits, repo);
5240 426ebf2e 2019-07-25 stsp if (err) {
5241 bfce7f83 2019-07-27 stsp if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
5242 bfce7f83 2019-07-27 stsp err->code != GOT_ERR_HISTEDIT_CMD)
5244 bfce7f83 2019-07-27 stsp prev_err = err;
5245 426ebf2e 2019-07-25 stsp resp = ' ';
5249 426ebf2e 2019-07-25 stsp } else if (resp == 'a') {
5250 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_HISTEDIT_CANCEL);
5253 0ebf8283 2019-07-24 stsp printf("invalid response '%c'\n", resp);
5256 0ebf8283 2019-07-24 stsp return err;
5259 0ebf8283 2019-07-24 stsp static const struct got_error *
5260 0ebf8283 2019-07-24 stsp histedit_complete(struct got_worktree *worktree,
5261 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
5262 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_repository *repo)
5264 0ebf8283 2019-07-24 stsp printf("Switching work tree to %s\n",
5265 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch));
5266 3e3a69f1 2019-07-25 stsp return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
5267 3e3a69f1 2019-07-25 stsp branch, repo);
5270 0ebf8283 2019-07-24 stsp static const struct got_error *
5271 0ebf8283 2019-07-24 stsp show_histedit_progress(struct got_commit_object *commit,
5272 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle, struct got_object_id *new_id)
5274 0ebf8283 2019-07-24 stsp const struct got_error *err;
5275 0ebf8283 2019-07-24 stsp char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
5277 0ebf8283 2019-07-24 stsp err = got_object_id_str(&old_id_str, hle->commit_id);
5279 0ebf8283 2019-07-24 stsp goto done;
5281 0ebf8283 2019-07-24 stsp if (new_id) {
5282 0ebf8283 2019-07-24 stsp err = got_object_id_str(&new_id_str, new_id);
5284 0ebf8283 2019-07-24 stsp goto done;
5287 0ebf8283 2019-07-24 stsp old_id_str[12] = '\0';
5288 0ebf8283 2019-07-24 stsp if (new_id_str)
5289 0ebf8283 2019-07-24 stsp new_id_str[12] = '\0';
5291 0ebf8283 2019-07-24 stsp if (hle->logmsg) {
5292 0ebf8283 2019-07-24 stsp logmsg = strdup(hle->logmsg);
5293 0ebf8283 2019-07-24 stsp if (logmsg == NULL) {
5294 0ebf8283 2019-07-24 stsp err = got_error_from_errno("strdup");
5295 0ebf8283 2019-07-24 stsp goto done;
5297 0ebf8283 2019-07-24 stsp trim_logmsg(logmsg, 42);
5299 0ebf8283 2019-07-24 stsp err = get_short_logmsg(&logmsg, 42, commit);
5301 0ebf8283 2019-07-24 stsp goto done;
5304 0ebf8283 2019-07-24 stsp switch (hle->cmd->code) {
5305 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_PICK:
5306 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_EDIT:
5307 0ebf8283 2019-07-24 stsp printf("%s -> %s: %s\n", old_id_str,
5308 0ebf8283 2019-07-24 stsp new_id_str ? new_id_str : "no-op change", logmsg);
5310 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_DROP:
5311 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_FOLD:
5312 0ebf8283 2019-07-24 stsp printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
5320 0ebf8283 2019-07-24 stsp free(old_id_str);
5321 0ebf8283 2019-07-24 stsp free(new_id_str);
5322 0ebf8283 2019-07-24 stsp return err;
5325 0ebf8283 2019-07-24 stsp static const struct got_error *
5326 0ebf8283 2019-07-24 stsp histedit_commit(struct got_pathlist_head *merged_paths,
5327 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
5328 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
5329 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
5331 0ebf8283 2019-07-24 stsp const struct got_error *err;
5332 0ebf8283 2019-07-24 stsp struct got_commit_object *commit;
5333 0ebf8283 2019-07-24 stsp struct got_object_id *new_commit_id;
5335 0ebf8283 2019-07-24 stsp if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
5336 0ebf8283 2019-07-24 stsp && hle->logmsg == NULL) {
5337 0ebf8283 2019-07-24 stsp err = histedit_edit_logmsg(hle, repo);
5339 0ebf8283 2019-07-24 stsp return err;
5342 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&commit, repo, hle->commit_id);
5344 0ebf8283 2019-07-24 stsp return err;
5346 0ebf8283 2019-07-24 stsp err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
5347 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, commit, hle->commit_id,
5348 3e3a69f1 2019-07-25 stsp hle->logmsg, repo);
5349 0ebf8283 2019-07-24 stsp if (err) {
5350 0ebf8283 2019-07-24 stsp if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
5351 0ebf8283 2019-07-24 stsp goto done;
5352 0ebf8283 2019-07-24 stsp err = show_histedit_progress(commit, hle, NULL);
5354 0ebf8283 2019-07-24 stsp err = show_histedit_progress(commit, hle, new_commit_id);
5355 0ebf8283 2019-07-24 stsp free(new_commit_id);
5358 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
5359 0ebf8283 2019-07-24 stsp return err;
5362 0ebf8283 2019-07-24 stsp static const struct got_error *
5363 0ebf8283 2019-07-24 stsp histedit_skip_commit(struct got_histedit_list_entry *hle,
5364 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
5366 0ebf8283 2019-07-24 stsp const struct got_error *error;
5367 0ebf8283 2019-07-24 stsp struct got_commit_object *commit;
5369 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
5371 0ebf8283 2019-07-24 stsp if (error)
5372 0ebf8283 2019-07-24 stsp return error;
5374 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo, hle->commit_id);
5375 0ebf8283 2019-07-24 stsp if (error)
5376 0ebf8283 2019-07-24 stsp return error;
5378 0ebf8283 2019-07-24 stsp error = show_histedit_progress(commit, hle, NULL);
5379 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
5380 0ebf8283 2019-07-24 stsp return error;
5383 0ebf8283 2019-07-24 stsp static const struct got_error *
5384 0ebf8283 2019-07-24 stsp cmd_histedit(int argc, char *argv[])
5386 0ebf8283 2019-07-24 stsp const struct got_error *error = NULL;
5387 0ebf8283 2019-07-24 stsp struct got_worktree *worktree = NULL;
5388 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex = NULL;
5389 0ebf8283 2019-07-24 stsp struct got_repository *repo = NULL;
5390 0ebf8283 2019-07-24 stsp char *cwd = NULL;
5391 0ebf8283 2019-07-24 stsp struct got_reference *branch = NULL;
5392 0ebf8283 2019-07-24 stsp struct got_reference *tmp_branch = NULL;
5393 0ebf8283 2019-07-24 stsp struct got_object_id *resume_commit_id = NULL;
5394 0ebf8283 2019-07-24 stsp struct got_object_id *base_commit_id = NULL;
5395 0ebf8283 2019-07-24 stsp struct got_object_id *head_commit_id = NULL;
5396 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
5397 6ba2bea2 2019-07-27 stsp int ch, rebase_in_progress = 0, did_something;
5398 0ebf8283 2019-07-24 stsp int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
5399 0ebf8283 2019-07-24 stsp const char *edit_script_path = NULL;
5400 0ebf8283 2019-07-24 stsp unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
5401 0ebf8283 2019-07-24 stsp struct got_object_id_queue commits;
5402 0ebf8283 2019-07-24 stsp struct got_pathlist_head merged_paths;
5403 0ebf8283 2019-07-24 stsp const struct got_object_id_queue *parent_ids;
5404 0ebf8283 2019-07-24 stsp struct got_object_qid *pid;
5405 0ebf8283 2019-07-24 stsp struct got_histedit_list histedit_cmds;
5406 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle;
5408 0ebf8283 2019-07-24 stsp SIMPLEQ_INIT(&commits);
5409 0ebf8283 2019-07-24 stsp TAILQ_INIT(&histedit_cmds);
5410 0ebf8283 2019-07-24 stsp TAILQ_INIT(&merged_paths);
5412 0ebf8283 2019-07-24 stsp while ((ch = getopt(argc, argv, "acF:")) != -1) {
5413 0ebf8283 2019-07-24 stsp switch (ch) {
5415 0ebf8283 2019-07-24 stsp abort_edit = 1;
5418 0ebf8283 2019-07-24 stsp continue_edit = 1;
5421 0ebf8283 2019-07-24 stsp edit_script_path = optarg;
5424 0ebf8283 2019-07-24 stsp usage_histedit();
5425 0ebf8283 2019-07-24 stsp /* NOTREACHED */
5429 0ebf8283 2019-07-24 stsp argc -= optind;
5430 0ebf8283 2019-07-24 stsp argv += optind;
5432 0ebf8283 2019-07-24 stsp #ifndef PROFILE
5433 0ebf8283 2019-07-24 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5434 0ebf8283 2019-07-24 stsp "unveil", NULL) == -1)
5435 0ebf8283 2019-07-24 stsp err(1, "pledge");
5437 0ebf8283 2019-07-24 stsp if (abort_edit && continue_edit)
5438 0ebf8283 2019-07-24 stsp usage_histedit();
5439 0ebf8283 2019-07-24 stsp if (argc != 0)
5440 0ebf8283 2019-07-24 stsp usage_histedit();
5443 0ebf8283 2019-07-24 stsp * This command cannot apply unveil(2) in all cases because the
5444 0ebf8283 2019-07-24 stsp * user may choose to run an editor to edit the histedit script
5445 0ebf8283 2019-07-24 stsp * and to edit individual commit log messages.
5446 0ebf8283 2019-07-24 stsp * unveil(2) traverses exec(2); if an editor is used we have to
5447 0ebf8283 2019-07-24 stsp * apply unveil after edit script and log messages have been written.
5448 0ebf8283 2019-07-24 stsp * XXX TODO: Make use of unveil(2) where possible.
5451 0ebf8283 2019-07-24 stsp cwd = getcwd(NULL, 0);
5452 0ebf8283 2019-07-24 stsp if (cwd == NULL) {
5453 0ebf8283 2019-07-24 stsp error = got_error_from_errno("getcwd");
5454 0ebf8283 2019-07-24 stsp goto done;
5456 0ebf8283 2019-07-24 stsp error = got_worktree_open(&worktree, cwd);
5457 0ebf8283 2019-07-24 stsp if (error)
5458 0ebf8283 2019-07-24 stsp goto done;
5460 0ebf8283 2019-07-24 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
5461 0ebf8283 2019-07-24 stsp if (error != NULL)
5462 0ebf8283 2019-07-24 stsp goto done;
5464 0ebf8283 2019-07-24 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
5465 0ebf8283 2019-07-24 stsp if (error)
5466 0ebf8283 2019-07-24 stsp goto done;
5467 0ebf8283 2019-07-24 stsp if (rebase_in_progress) {
5468 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_REBASING);
5469 0ebf8283 2019-07-24 stsp goto done;
5472 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
5473 0ebf8283 2019-07-24 stsp if (error)
5474 0ebf8283 2019-07-24 stsp goto done;
5476 0ebf8283 2019-07-24 stsp if (edit_in_progress && abort_edit) {
5477 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_continue(&resume_commit_id,
5478 3e3a69f1 2019-07-25 stsp &tmp_branch, &branch, &base_commit_id, &fileindex,
5479 3e3a69f1 2019-07-25 stsp worktree, repo);
5480 0ebf8283 2019-07-24 stsp if (error)
5481 0ebf8283 2019-07-24 stsp goto done;
5482 0ebf8283 2019-07-24 stsp printf("Switching work tree to %s\n",
5483 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch));
5484 3e3a69f1 2019-07-25 stsp error = got_worktree_histedit_abort(worktree, fileindex, repo,
5485 0ebf8283 2019-07-24 stsp branch, base_commit_id, update_progress, &did_something);
5486 0ebf8283 2019-07-24 stsp if (error)
5487 0ebf8283 2019-07-24 stsp goto done;
5488 0ebf8283 2019-07-24 stsp printf("Histedit of %s aborted\n",
5489 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch));
5490 0ebf8283 2019-07-24 stsp goto done; /* nothing else to do */
5491 0ebf8283 2019-07-24 stsp } else if (abort_edit) {
5492 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_NOT_HISTEDIT);
5493 0ebf8283 2019-07-24 stsp goto done;
5496 0ebf8283 2019-07-24 stsp if (continue_edit) {
5497 0ebf8283 2019-07-24 stsp char *path;
5499 0ebf8283 2019-07-24 stsp if (!edit_in_progress) {
5500 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_NOT_HISTEDIT);
5501 0ebf8283 2019-07-24 stsp goto done;
5504 c3022ba5 2019-07-27 stsp error = got_worktree_get_histedit_script_path(&path, worktree);
5505 0ebf8283 2019-07-24 stsp if (error)
5506 0ebf8283 2019-07-24 stsp goto done;
5508 0ebf8283 2019-07-24 stsp error = histedit_load_list(&histedit_cmds, path, repo);
5509 0ebf8283 2019-07-24 stsp free(path);
5510 0ebf8283 2019-07-24 stsp if (error)
5511 0ebf8283 2019-07-24 stsp goto done;
5513 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_continue(&resume_commit_id,
5514 3e3a69f1 2019-07-25 stsp &tmp_branch, &branch, &base_commit_id, &fileindex,
5515 3e3a69f1 2019-07-25 stsp worktree, repo);
5516 0ebf8283 2019-07-24 stsp if (error)
5517 0ebf8283 2019-07-24 stsp goto done;
5519 0ebf8283 2019-07-24 stsp error = got_ref_resolve(&head_commit_id, repo, branch);
5520 0ebf8283 2019-07-24 stsp if (error)
5521 0ebf8283 2019-07-24 stsp goto done;
5523 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo,
5524 0ebf8283 2019-07-24 stsp head_commit_id);
5525 0ebf8283 2019-07-24 stsp if (error)
5526 0ebf8283 2019-07-24 stsp goto done;
5527 0ebf8283 2019-07-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
5528 0ebf8283 2019-07-24 stsp pid = SIMPLEQ_FIRST(parent_ids);
5529 3aac7cf7 2019-07-25 stsp if (pid == NULL) {
5530 3aac7cf7 2019-07-25 stsp error = got_error(GOT_ERR_EMPTY_HISTEDIT);
5531 3aac7cf7 2019-07-25 stsp goto done;
5533 8ca9bd68 2019-07-25 stsp error = collect_commits(&commits, head_commit_id, pid->id,
5534 8ca9bd68 2019-07-25 stsp base_commit_id, got_worktree_get_path_prefix(worktree),
5535 8ca9bd68 2019-07-25 stsp GOT_ERR_HISTEDIT_PATH, repo);
5536 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
5537 0ebf8283 2019-07-24 stsp commit = NULL;
5538 0ebf8283 2019-07-24 stsp if (error)
5539 0ebf8283 2019-07-24 stsp goto done;
5541 0ebf8283 2019-07-24 stsp if (edit_in_progress) {
5542 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_HISTEDIT_BUSY);
5543 0ebf8283 2019-07-24 stsp goto done;
5546 0ebf8283 2019-07-24 stsp error = got_ref_open(&branch, repo,
5547 0ebf8283 2019-07-24 stsp got_worktree_get_head_ref_name(worktree), 0);
5548 0ebf8283 2019-07-24 stsp if (error != NULL)
5549 0ebf8283 2019-07-24 stsp goto done;
5551 c7d20a3f 2019-07-30 stsp if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
5552 c7d20a3f 2019-07-30 stsp error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
5553 c7d20a3f 2019-07-30 stsp "will not edit commit history of a branch outside "
5554 c7d20a3f 2019-07-30 stsp "the \"refs/heads/\" reference namespace");
5555 c7d20a3f 2019-07-30 stsp goto done;
5558 0ebf8283 2019-07-24 stsp error = got_ref_resolve(&head_commit_id, repo, branch);
5559 bfce7f83 2019-07-27 stsp got_ref_close(branch);
5560 bfce7f83 2019-07-27 stsp branch = NULL;
5561 0ebf8283 2019-07-24 stsp if (error)
5562 0ebf8283 2019-07-24 stsp goto done;
5564 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo,
5565 0ebf8283 2019-07-24 stsp head_commit_id);
5566 0ebf8283 2019-07-24 stsp if (error)
5567 0ebf8283 2019-07-24 stsp goto done;
5568 0ebf8283 2019-07-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
5569 0ebf8283 2019-07-24 stsp pid = SIMPLEQ_FIRST(parent_ids);
5570 3aac7cf7 2019-07-25 stsp if (pid == NULL) {
5571 3aac7cf7 2019-07-25 stsp error = got_error(GOT_ERR_EMPTY_HISTEDIT);
5572 3aac7cf7 2019-07-25 stsp goto done;
5574 8ca9bd68 2019-07-25 stsp error = collect_commits(&commits, head_commit_id, pid->id,
5575 8ca9bd68 2019-07-25 stsp got_worktree_get_base_commit_id(worktree),
5576 8ca9bd68 2019-07-25 stsp got_worktree_get_path_prefix(worktree),
5577 8ca9bd68 2019-07-25 stsp GOT_ERR_HISTEDIT_PATH, repo);
5578 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
5579 0ebf8283 2019-07-24 stsp commit = NULL;
5580 0ebf8283 2019-07-24 stsp if (error)
5581 0ebf8283 2019-07-24 stsp goto done;
5583 bfce7f83 2019-07-27 stsp error = got_worktree_histedit_prepare(&tmp_branch, &branch,
5584 bfce7f83 2019-07-27 stsp &base_commit_id, &fileindex, worktree, repo);
5585 bfce7f83 2019-07-27 stsp if (error)
5586 bfce7f83 2019-07-27 stsp goto done;
5588 0ebf8283 2019-07-24 stsp if (edit_script_path) {
5589 0ebf8283 2019-07-24 stsp error = histedit_load_list(&histedit_cmds,
5590 0ebf8283 2019-07-24 stsp edit_script_path, repo);
5591 6ba2bea2 2019-07-27 stsp if (error) {
5592 6ba2bea2 2019-07-27 stsp got_worktree_histedit_abort(worktree, fileindex,
5593 6ba2bea2 2019-07-27 stsp repo, branch, base_commit_id,
5594 6ba2bea2 2019-07-27 stsp update_progress, &did_something);
5595 0ebf8283 2019-07-24 stsp goto done;
5598 0ebf8283 2019-07-24 stsp error = histedit_edit_script(&histedit_cmds, &commits,
5600 6ba2bea2 2019-07-27 stsp if (error) {
5601 6ba2bea2 2019-07-27 stsp got_worktree_histedit_abort(worktree, fileindex,
5602 6ba2bea2 2019-07-27 stsp repo, branch, base_commit_id,
5603 6ba2bea2 2019-07-27 stsp update_progress, &did_something);
5604 0ebf8283 2019-07-24 stsp goto done;
5609 0ebf8283 2019-07-24 stsp error = histedit_save_list(&histedit_cmds, worktree,
5611 6ba2bea2 2019-07-27 stsp if (error) {
5612 6ba2bea2 2019-07-27 stsp got_worktree_histedit_abort(worktree, fileindex,
5613 6ba2bea2 2019-07-27 stsp repo, branch, base_commit_id,
5614 6ba2bea2 2019-07-27 stsp update_progress, &did_something);
5615 0ebf8283 2019-07-24 stsp goto done;
5620 0ebf8283 2019-07-24 stsp error = histedit_check_script(&histedit_cmds, &commits, repo);
5621 0ebf8283 2019-07-24 stsp if (error)
5622 0ebf8283 2019-07-24 stsp goto done;
5624 0ebf8283 2019-07-24 stsp TAILQ_FOREACH(hle, &histedit_cmds, entry) {
5625 0ebf8283 2019-07-24 stsp if (resume_commit_id) {
5626 0ebf8283 2019-07-24 stsp if (got_object_id_cmp(hle->commit_id,
5627 0ebf8283 2019-07-24 stsp resume_commit_id) != 0)
5630 0ebf8283 2019-07-24 stsp resume_commit_id = NULL;
5631 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_DROP ||
5632 0ebf8283 2019-07-24 stsp hle->cmd->code == GOT_HISTEDIT_FOLD) {
5633 0ebf8283 2019-07-24 stsp error = histedit_skip_commit(hle, worktree,
5636 0ebf8283 2019-07-24 stsp error = histedit_commit(NULL, worktree,
5637 3e3a69f1 2019-07-25 stsp fileindex, tmp_branch, hle, repo);
5639 0ebf8283 2019-07-24 stsp if (error)
5640 0ebf8283 2019-07-24 stsp goto done;
5644 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_DROP) {
5645 0ebf8283 2019-07-24 stsp error = histedit_skip_commit(hle, worktree, repo);
5646 0ebf8283 2019-07-24 stsp if (error)
5647 0ebf8283 2019-07-24 stsp goto done;
5651 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo,
5652 0ebf8283 2019-07-24 stsp 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 parent_ids = got_object_commit_get_parent_ids(commit);
5656 0ebf8283 2019-07-24 stsp pid = SIMPLEQ_FIRST(parent_ids);
5658 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_merge_files(&merged_paths,
5659 3e3a69f1 2019-07-25 stsp worktree, fileindex, pid->id, hle->commit_id, repo,
5660 3e3a69f1 2019-07-25 stsp rebase_progress, &rebase_status, check_cancelled, NULL);
5661 0ebf8283 2019-07-24 stsp if (error)
5662 0ebf8283 2019-07-24 stsp goto done;
5663 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
5664 0ebf8283 2019-07-24 stsp commit = NULL;
5666 0ebf8283 2019-07-24 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
5667 0ebf8283 2019-07-24 stsp got_worktree_rebase_pathlist_free(&merged_paths);
5671 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
5672 0ebf8283 2019-07-24 stsp char *id_str;
5673 0ebf8283 2019-07-24 stsp error = got_object_id_str(&id_str, hle->commit_id);
5674 0ebf8283 2019-07-24 stsp if (error)
5675 0ebf8283 2019-07-24 stsp goto done;
5676 0ebf8283 2019-07-24 stsp printf("Stopping histedit for amending commit %s\n",
5678 0ebf8283 2019-07-24 stsp free(id_str);
5679 0ebf8283 2019-07-24 stsp got_worktree_rebase_pathlist_free(&merged_paths);
5680 3e3a69f1 2019-07-25 stsp error = got_worktree_histedit_postpone(worktree,
5681 3e3a69f1 2019-07-25 stsp fileindex);
5682 0ebf8283 2019-07-24 stsp goto done;
5685 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
5686 0ebf8283 2019-07-24 stsp error = histedit_skip_commit(hle, worktree, repo);
5687 0ebf8283 2019-07-24 stsp if (error)
5688 0ebf8283 2019-07-24 stsp goto done;
5692 3e3a69f1 2019-07-25 stsp error = histedit_commit(&merged_paths, worktree, fileindex,
5693 3e3a69f1 2019-07-25 stsp tmp_branch, hle, repo);
5694 0ebf8283 2019-07-24 stsp got_worktree_rebase_pathlist_free(&merged_paths);
5695 0ebf8283 2019-07-24 stsp if (error)
5696 0ebf8283 2019-07-24 stsp goto done;
5699 0ebf8283 2019-07-24 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
5700 3e3a69f1 2019-07-25 stsp error = got_worktree_histedit_postpone(worktree, fileindex);
5701 0ebf8283 2019-07-24 stsp if (error)
5702 0ebf8283 2019-07-24 stsp goto done;
5703 0ebf8283 2019-07-24 stsp error = got_error_msg(GOT_ERR_CONFLICTS,
5704 0ebf8283 2019-07-24 stsp "conflicts must be resolved before rebasing can continue");
5706 3e3a69f1 2019-07-25 stsp error = histedit_complete(worktree, fileindex, tmp_branch,
5707 3e3a69f1 2019-07-25 stsp branch, repo);
5709 0ebf8283 2019-07-24 stsp got_object_id_queue_free(&commits);
5710 bfce7f83 2019-07-27 stsp histedit_free_list(&histedit_cmds);
5711 0ebf8283 2019-07-24 stsp free(head_commit_id);
5712 0ebf8283 2019-07-24 stsp free(base_commit_id);
5713 0ebf8283 2019-07-24 stsp free(resume_commit_id);
5714 0ebf8283 2019-07-24 stsp if (commit)
5715 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
5716 0ebf8283 2019-07-24 stsp if (branch)
5717 0ebf8283 2019-07-24 stsp got_ref_close(branch);
5718 0ebf8283 2019-07-24 stsp if (tmp_branch)
5719 0ebf8283 2019-07-24 stsp got_ref_close(tmp_branch);
5720 0ebf8283 2019-07-24 stsp if (worktree)
5721 0ebf8283 2019-07-24 stsp got_worktree_close(worktree);
5723 715dc77e 2019-08-03 stsp got_repo_close(repo);
5724 715dc77e 2019-08-03 stsp return error;
5727 715dc77e 2019-08-03 stsp __dead static void
5728 715dc77e 2019-08-03 stsp usage_stage(void)
5730 f3055044 2019-08-07 stsp fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
5731 f3055044 2019-08-07 stsp "[file-path ...]\n",
5732 715dc77e 2019-08-03 stsp getprogname());
5736 715dc77e 2019-08-03 stsp static const struct got_error *
5737 408b4ebc 2019-08-03 stsp print_stage(void *arg, unsigned char status, unsigned char staged_status,
5738 408b4ebc 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
5739 408b4ebc 2019-08-03 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
5741 408b4ebc 2019-08-03 stsp const struct got_error *err = NULL;
5742 408b4ebc 2019-08-03 stsp char *id_str = NULL;
5744 408b4ebc 2019-08-03 stsp if (staged_status != GOT_STATUS_ADD &&
5745 408b4ebc 2019-08-03 stsp staged_status != GOT_STATUS_MODIFY &&
5746 408b4ebc 2019-08-03 stsp staged_status != GOT_STATUS_DELETE)
5747 408b4ebc 2019-08-03 stsp return NULL;
5749 408b4ebc 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
5750 408b4ebc 2019-08-03 stsp staged_status == GOT_STATUS_MODIFY)
5751 408b4ebc 2019-08-03 stsp err = got_object_id_str(&id_str, staged_blob_id);
5753 408b4ebc 2019-08-03 stsp err = got_object_id_str(&id_str, blob_id);
5755 408b4ebc 2019-08-03 stsp return err;
5757 408b4ebc 2019-08-03 stsp printf("%s %c %s\n", id_str, staged_status, path);
5758 408b4ebc 2019-08-03 stsp free(id_str);
5759 dc424a06 2019-08-07 stsp return NULL;
5762 dc424a06 2019-08-07 stsp static const struct got_error *
5763 715dc77e 2019-08-03 stsp cmd_stage(int argc, char *argv[])
5765 715dc77e 2019-08-03 stsp const struct got_error *error = NULL;
5766 715dc77e 2019-08-03 stsp struct got_repository *repo = NULL;
5767 715dc77e 2019-08-03 stsp struct got_worktree *worktree = NULL;
5768 715dc77e 2019-08-03 stsp char *cwd = NULL;
5769 715dc77e 2019-08-03 stsp struct got_pathlist_head paths;
5770 715dc77e 2019-08-03 stsp struct got_pathlist_entry *pe;
5771 dc424a06 2019-08-07 stsp int ch, list_stage = 0, pflag = 0;
5772 dc424a06 2019-08-07 stsp FILE *patch_script_file = NULL;
5773 2e1f37b0 2019-08-08 stsp const char *patch_script_path = NULL;
5774 2e1f37b0 2019-08-08 stsp struct choose_patch_arg cpa;
5776 715dc77e 2019-08-03 stsp TAILQ_INIT(&paths);
5778 dc424a06 2019-08-07 stsp while ((ch = getopt(argc, argv, "lpF:")) != -1) {
5779 715dc77e 2019-08-03 stsp switch (ch) {
5781 408b4ebc 2019-08-03 stsp list_stage = 1;
5784 dc424a06 2019-08-07 stsp pflag = 1;
5787 dc424a06 2019-08-07 stsp patch_script_path = optarg;
5790 715dc77e 2019-08-03 stsp usage_stage();
5791 715dc77e 2019-08-03 stsp /* NOTREACHED */
5795 715dc77e 2019-08-03 stsp argc -= optind;
5796 715dc77e 2019-08-03 stsp argv += optind;
5798 715dc77e 2019-08-03 stsp #ifndef PROFILE
5799 715dc77e 2019-08-03 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5800 715dc77e 2019-08-03 stsp "unveil", NULL) == -1)
5801 715dc77e 2019-08-03 stsp err(1, "pledge");
5803 2db2652d 2019-08-07 stsp if (list_stage && (pflag || patch_script_path))
5804 2db2652d 2019-08-07 stsp errx(1, "-l option cannot be used with other options");
5805 dc424a06 2019-08-07 stsp if (patch_script_path && !pflag)
5806 dc424a06 2019-08-07 stsp errx(1, "-F option can only be used together with -p option");
5808 715dc77e 2019-08-03 stsp cwd = getcwd(NULL, 0);
5809 715dc77e 2019-08-03 stsp if (cwd == NULL) {
5810 715dc77e 2019-08-03 stsp error = got_error_from_errno("getcwd");
5811 715dc77e 2019-08-03 stsp goto done;
5814 715dc77e 2019-08-03 stsp error = got_worktree_open(&worktree, cwd);
5815 715dc77e 2019-08-03 stsp if (error)
5816 715dc77e 2019-08-03 stsp goto done;
5818 715dc77e 2019-08-03 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
5819 715dc77e 2019-08-03 stsp if (error != NULL)
5820 715dc77e 2019-08-03 stsp goto done;
5822 dc424a06 2019-08-07 stsp if (patch_script_path) {
5823 dc424a06 2019-08-07 stsp patch_script_file = fopen(patch_script_path, "r");
5824 dc424a06 2019-08-07 stsp if (patch_script_file == NULL) {
5825 dc424a06 2019-08-07 stsp error = got_error_from_errno2("fopen",
5826 dc424a06 2019-08-07 stsp patch_script_path);
5827 dc424a06 2019-08-07 stsp goto done;
5830 715dc77e 2019-08-03 stsp error = apply_unveil(got_repo_get_path(repo), 1,
5831 715dc77e 2019-08-03 stsp got_worktree_get_root_path(worktree));
5832 715dc77e 2019-08-03 stsp if (error)
5833 715dc77e 2019-08-03 stsp goto done;
5835 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5836 6d022e97 2019-08-04 stsp if (error)
5837 6d022e97 2019-08-04 stsp goto done;
5839 6d022e97 2019-08-04 stsp if (list_stage)
5840 408b4ebc 2019-08-03 stsp error = got_worktree_status(worktree, &paths, repo,
5841 408b4ebc 2019-08-03 stsp print_stage, NULL, check_cancelled, NULL);
5843 2e1f37b0 2019-08-08 stsp cpa.patch_script_file = patch_script_file;
5844 2e1f37b0 2019-08-08 stsp cpa.action = "stage";
5845 dc424a06 2019-08-07 stsp error = got_worktree_stage(worktree, &paths,
5846 dc424a06 2019-08-07 stsp pflag ? NULL : print_status, NULL,
5847 2e1f37b0 2019-08-08 stsp pflag ? choose_patch : NULL, &cpa, repo);
5850 2e1f37b0 2019-08-08 stsp if (patch_script_file && fclose(patch_script_file) == EOF &&
5851 2e1f37b0 2019-08-08 stsp error == NULL)
5852 2e1f37b0 2019-08-08 stsp error = got_error_from_errno2("fclose", patch_script_path);
5854 ad493afc 2019-08-03 stsp got_repo_close(repo);
5855 ad493afc 2019-08-03 stsp if (worktree)
5856 ad493afc 2019-08-03 stsp got_worktree_close(worktree);
5857 ad493afc 2019-08-03 stsp TAILQ_FOREACH(pe, &paths, entry)
5858 ad493afc 2019-08-03 stsp free((char *)pe->path);
5859 ad493afc 2019-08-03 stsp got_pathlist_free(&paths);
5860 ad493afc 2019-08-03 stsp free(cwd);
5861 ad493afc 2019-08-03 stsp return error;
5864 ad493afc 2019-08-03 stsp __dead static void
5865 ad493afc 2019-08-03 stsp usage_unstage(void)
5867 2e1f37b0 2019-08-08 stsp fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
5868 2e1f37b0 2019-08-08 stsp "[file-path ...]\n",
5869 ad493afc 2019-08-03 stsp getprogname());
5874 ad493afc 2019-08-03 stsp static const struct got_error *
5875 ad493afc 2019-08-03 stsp cmd_unstage(int argc, char *argv[])
5877 ad493afc 2019-08-03 stsp const struct got_error *error = NULL;
5878 ad493afc 2019-08-03 stsp struct got_repository *repo = NULL;
5879 ad493afc 2019-08-03 stsp struct got_worktree *worktree = NULL;
5880 ad493afc 2019-08-03 stsp char *cwd = NULL;
5881 ad493afc 2019-08-03 stsp struct got_pathlist_head paths;
5882 ad493afc 2019-08-03 stsp struct got_pathlist_entry *pe;
5883 2e1f37b0 2019-08-08 stsp int ch, did_something = 0, pflag = 0;
5884 2e1f37b0 2019-08-08 stsp FILE *patch_script_file = NULL;
5885 2e1f37b0 2019-08-08 stsp const char *patch_script_path = NULL;
5886 2e1f37b0 2019-08-08 stsp struct choose_patch_arg cpa;
5888 ad493afc 2019-08-03 stsp TAILQ_INIT(&paths);
5890 2e1f37b0 2019-08-08 stsp while ((ch = getopt(argc, argv, "pF:")) != -1) {
5891 ad493afc 2019-08-03 stsp switch (ch) {
5893 2e1f37b0 2019-08-08 stsp pflag = 1;
5896 2e1f37b0 2019-08-08 stsp patch_script_path = optarg;
5899 ad493afc 2019-08-03 stsp usage_unstage();
5900 ad493afc 2019-08-03 stsp /* NOTREACHED */
5904 ad493afc 2019-08-03 stsp argc -= optind;
5905 ad493afc 2019-08-03 stsp argv += optind;
5907 ad493afc 2019-08-03 stsp #ifndef PROFILE
5908 ad493afc 2019-08-03 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5909 ad493afc 2019-08-03 stsp "unveil", NULL) == -1)
5910 ad493afc 2019-08-03 stsp err(1, "pledge");
5912 2e1f37b0 2019-08-08 stsp if (patch_script_path && !pflag)
5913 2e1f37b0 2019-08-08 stsp errx(1, "-F option can only be used together with -p option");
5915 ad493afc 2019-08-03 stsp cwd = getcwd(NULL, 0);
5916 ad493afc 2019-08-03 stsp if (cwd == NULL) {
5917 ad493afc 2019-08-03 stsp error = got_error_from_errno("getcwd");
5918 ad493afc 2019-08-03 stsp goto done;
5921 ad493afc 2019-08-03 stsp error = got_worktree_open(&worktree, cwd);
5922 ad493afc 2019-08-03 stsp if (error)
5923 ad493afc 2019-08-03 stsp goto done;
5925 ad493afc 2019-08-03 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
5926 ad493afc 2019-08-03 stsp if (error != NULL)
5927 ad493afc 2019-08-03 stsp goto done;
5929 2e1f37b0 2019-08-08 stsp if (patch_script_path) {
5930 2e1f37b0 2019-08-08 stsp patch_script_file = fopen(patch_script_path, "r");
5931 2e1f37b0 2019-08-08 stsp if (patch_script_file == NULL) {
5932 2e1f37b0 2019-08-08 stsp error = got_error_from_errno2("fopen",
5933 2e1f37b0 2019-08-08 stsp patch_script_path);
5934 2e1f37b0 2019-08-08 stsp goto done;
5938 ad493afc 2019-08-03 stsp error = apply_unveil(got_repo_get_path(repo), 1,
5939 ad493afc 2019-08-03 stsp got_worktree_get_root_path(worktree));
5940 ad493afc 2019-08-03 stsp if (error)
5941 ad493afc 2019-08-03 stsp goto done;
5943 ad493afc 2019-08-03 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5944 ad493afc 2019-08-03 stsp if (error)
5945 ad493afc 2019-08-03 stsp goto done;
5947 2e1f37b0 2019-08-08 stsp cpa.patch_script_file = patch_script_file;
5948 2e1f37b0 2019-08-08 stsp cpa.action = "unstage";
5949 ad493afc 2019-08-03 stsp error = got_worktree_unstage(worktree, &paths, update_progress,
5950 2e1f37b0 2019-08-08 stsp &did_something, pflag ? choose_patch : NULL, &cpa, repo);
5952 2e1f37b0 2019-08-08 stsp if (patch_script_file && fclose(patch_script_file) == EOF &&
5953 2e1f37b0 2019-08-08 stsp error == NULL)
5954 2e1f37b0 2019-08-08 stsp error = got_error_from_errno2("fclose", patch_script_path);
5956 0ebf8283 2019-07-24 stsp got_repo_close(repo);
5957 715dc77e 2019-08-03 stsp if (worktree)
5958 715dc77e 2019-08-03 stsp got_worktree_close(worktree);
5959 715dc77e 2019-08-03 stsp TAILQ_FOREACH(pe, &paths, entry)
5960 715dc77e 2019-08-03 stsp free((char *)pe->path);
5961 715dc77e 2019-08-03 stsp got_pathlist_free(&paths);
5962 715dc77e 2019-08-03 stsp free(cwd);
5963 01073a5d 2019-08-22 stsp return error;
5966 01073a5d 2019-08-22 stsp __dead static void
5967 01073a5d 2019-08-22 stsp usage_cat(void)
5969 01073a5d 2019-08-22 stsp fprintf(stderr, "usage: %s cat [-r repository ] object1 "
5970 01073a5d 2019-08-22 stsp "[object2 ...]\n", getprogname());
5974 01073a5d 2019-08-22 stsp static const struct got_error *
5975 01073a5d 2019-08-22 stsp cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
5977 01073a5d 2019-08-22 stsp const struct got_error *err;
5978 01073a5d 2019-08-22 stsp struct got_blob_object *blob;
5980 01073a5d 2019-08-22 stsp err = got_object_open_as_blob(&blob, repo, id, 8192);
5982 01073a5d 2019-08-22 stsp return err;
5984 01073a5d 2019-08-22 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
5985 01073a5d 2019-08-22 stsp got_object_blob_close(blob);
5986 01073a5d 2019-08-22 stsp return err;
5989 01073a5d 2019-08-22 stsp static const struct got_error *
5990 01073a5d 2019-08-22 stsp cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
5992 01073a5d 2019-08-22 stsp const struct got_error *err;
5993 01073a5d 2019-08-22 stsp struct got_tree_object *tree;
5994 01073a5d 2019-08-22 stsp const struct got_tree_entries *entries;
5995 01073a5d 2019-08-22 stsp struct got_tree_entry *te;
5997 01073a5d 2019-08-22 stsp err = got_object_open_as_tree(&tree, repo, id);
5999 01073a5d 2019-08-22 stsp return err;
6001 01073a5d 2019-08-22 stsp entries = got_object_tree_get_entries(tree);
6002 01073a5d 2019-08-22 stsp te = SIMPLEQ_FIRST(&entries->head);
6003 01073a5d 2019-08-22 stsp while (te) {
6004 01073a5d 2019-08-22 stsp char *id_str;
6005 01073a5d 2019-08-22 stsp if (sigint_received || sigpipe_received)
6007 01073a5d 2019-08-22 stsp err = got_object_id_str(&id_str, te->id);
6010 01073a5d 2019-08-22 stsp fprintf(outfile, "%s %.7o %s\n", id_str, te->mode, te->name);
6011 01073a5d 2019-08-22 stsp free(id_str);
6012 01073a5d 2019-08-22 stsp te = SIMPLEQ_NEXT(te, entry);
6015 01073a5d 2019-08-22 stsp got_object_tree_close(tree);
6016 01073a5d 2019-08-22 stsp return err;
6019 01073a5d 2019-08-22 stsp static const struct got_error *
6020 01073a5d 2019-08-22 stsp cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
6022 01073a5d 2019-08-22 stsp const struct got_error *err;
6023 01073a5d 2019-08-22 stsp struct got_commit_object *commit;
6024 01073a5d 2019-08-22 stsp const struct got_object_id_queue *parent_ids;
6025 01073a5d 2019-08-22 stsp struct got_object_qid *pid;
6026 01073a5d 2019-08-22 stsp char *id_str = NULL;
6027 24ea5512 2019-08-22 stsp const char *logmsg = NULL;
6030 01073a5d 2019-08-22 stsp err = got_object_open_as_commit(&commit, repo, id);
6032 01073a5d 2019-08-22 stsp return err;
6034 01073a5d 2019-08-22 stsp err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
6036 01073a5d 2019-08-22 stsp goto done;
6038 01073a5d 2019-08-22 stsp fprintf(outfile, "tree: %s\n", id_str);
6039 01073a5d 2019-08-22 stsp parent_ids = got_object_commit_get_parent_ids(commit);
6040 01073a5d 2019-08-22 stsp fprintf(outfile, "parents: %d\n",
6041 01073a5d 2019-08-22 stsp got_object_commit_get_nparents(commit));
6043 01073a5d 2019-08-22 stsp SIMPLEQ_FOREACH(pid, parent_ids, entry) {
6044 01073a5d 2019-08-22 stsp char *pid_str;
6045 01073a5d 2019-08-22 stsp err = got_object_id_str(&pid_str, pid->id);
6047 01073a5d 2019-08-22 stsp goto done;
6048 01073a5d 2019-08-22 stsp fprintf(outfile, "parent %d: %s\n", i++, pid_str);
6049 01073a5d 2019-08-22 stsp free(pid_str);
6051 01073a5d 2019-08-22 stsp fprintf(outfile, "author: %s\n",
6052 01073a5d 2019-08-22 stsp got_object_commit_get_author(commit));
6053 01073a5d 2019-08-22 stsp fprintf(outfile, "author-time: %lld\n",
6054 01073a5d 2019-08-22 stsp got_object_commit_get_author_time(commit));
6056 01073a5d 2019-08-22 stsp fprintf(outfile, "committer: %s\n",
6057 01073a5d 2019-08-22 stsp got_object_commit_get_author(commit));
6058 01073a5d 2019-08-22 stsp fprintf(outfile, "committer-time: %lld\n",
6059 01073a5d 2019-08-22 stsp got_object_commit_get_committer_time(commit));
6061 24ea5512 2019-08-22 stsp logmsg = got_object_commit_get_logmsg_raw(commit);
6062 01073a5d 2019-08-22 stsp fprintf(outfile, "log-message: %zd bytes\n", strlen(logmsg));
6063 01073a5d 2019-08-22 stsp fprintf(outfile, "%s", logmsg);
6065 01073a5d 2019-08-22 stsp free(id_str);
6066 01073a5d 2019-08-22 stsp got_object_commit_close(commit);
6067 01073a5d 2019-08-22 stsp return err;
6070 01073a5d 2019-08-22 stsp static const struct got_error *
6071 01073a5d 2019-08-22 stsp cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
6073 01073a5d 2019-08-22 stsp const struct got_error *err;
6074 01073a5d 2019-08-22 stsp struct got_tag_object *tag;
6075 01073a5d 2019-08-22 stsp char *id_str = NULL;
6076 01073a5d 2019-08-22 stsp const char *tagmsg = NULL;
6078 01073a5d 2019-08-22 stsp err = got_object_open_as_tag(&tag, repo, id);
6080 01073a5d 2019-08-22 stsp return err;
6082 01073a5d 2019-08-22 stsp err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
6084 01073a5d 2019-08-22 stsp goto done;
6086 01073a5d 2019-08-22 stsp fprintf(outfile, "tag-name: %s\n", got_object_tag_get_name(tag));
6087 01073a5d 2019-08-22 stsp switch (got_object_tag_get_object_type(tag)) {
6088 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_BLOB:
6089 01073a5d 2019-08-22 stsp fprintf(outfile, "tagged-object-type: blob\n");
6091 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TREE:
6092 01073a5d 2019-08-22 stsp fprintf(outfile, "tagged-object-type: tree\n");
6094 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_COMMIT:
6095 01073a5d 2019-08-22 stsp fprintf(outfile, "tagged-object-type: commit\n");
6097 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TAG:
6098 01073a5d 2019-08-22 stsp fprintf(outfile, "tagged-object-type: tag\n");
6103 01073a5d 2019-08-22 stsp fprintf(outfile, "tagged-object: %s\n", id_str);
6105 01073a5d 2019-08-22 stsp fprintf(outfile, "tagger: %s\n",
6106 01073a5d 2019-08-22 stsp got_object_tag_get_tagger(tag));
6107 01073a5d 2019-08-22 stsp fprintf(outfile, "tagger-time: %lld %lld\n",
6108 01073a5d 2019-08-22 stsp got_object_tag_get_tagger_time(tag),
6109 01073a5d 2019-08-22 stsp got_object_tag_get_tagger_gmtoff(tag));
6111 01073a5d 2019-08-22 stsp tagmsg = got_object_tag_get_message(tag);
6112 01073a5d 2019-08-22 stsp fprintf(outfile, "tag-message: %zd bytes\n", strlen(tagmsg));
6113 01073a5d 2019-08-22 stsp fprintf(outfile, "%s", tagmsg);
6115 01073a5d 2019-08-22 stsp free(id_str);
6116 01073a5d 2019-08-22 stsp got_object_tag_close(tag);
6117 01073a5d 2019-08-22 stsp return err;
6120 01073a5d 2019-08-22 stsp static const struct got_error *
6121 01073a5d 2019-08-22 stsp cmd_cat(int argc, char *argv[])
6123 01073a5d 2019-08-22 stsp const struct got_error *error;
6124 01073a5d 2019-08-22 stsp struct got_repository *repo = NULL;
6125 01073a5d 2019-08-22 stsp struct got_worktree *worktree = NULL;
6126 01073a5d 2019-08-22 stsp char *cwd = NULL, *repo_path = NULL, *label = NULL;
6127 01073a5d 2019-08-22 stsp struct got_object_id *id = NULL;
6128 01073a5d 2019-08-22 stsp int ch, obj_type, i;
6130 01073a5d 2019-08-22 stsp #ifndef PROFILE
6131 01073a5d 2019-08-22 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
6132 01073a5d 2019-08-22 stsp NULL) == -1)
6133 01073a5d 2019-08-22 stsp err(1, "pledge");
6136 01073a5d 2019-08-22 stsp while ((ch = getopt(argc, argv, "r:")) != -1) {
6137 01073a5d 2019-08-22 stsp switch (ch) {
6139 01073a5d 2019-08-22 stsp repo_path = realpath(optarg, NULL);
6140 01073a5d 2019-08-22 stsp if (repo_path == NULL)
6141 01073a5d 2019-08-22 stsp err(1, "-r option");
6142 01073a5d 2019-08-22 stsp got_path_strip_trailing_slashes(repo_path);
6145 01073a5d 2019-08-22 stsp usage_cat();
6146 01073a5d 2019-08-22 stsp /* NOTREACHED */
6150 01073a5d 2019-08-22 stsp argc -= optind;
6151 01073a5d 2019-08-22 stsp argv += optind;
6153 01073a5d 2019-08-22 stsp cwd = getcwd(NULL, 0);
6154 01073a5d 2019-08-22 stsp if (cwd == NULL) {
6155 01073a5d 2019-08-22 stsp error = got_error_from_errno("getcwd");
6156 01073a5d 2019-08-22 stsp goto done;
6158 01073a5d 2019-08-22 stsp error = got_worktree_open(&worktree, cwd);
6159 01073a5d 2019-08-22 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
6160 01073a5d 2019-08-22 stsp goto done;
6161 01073a5d 2019-08-22 stsp if (worktree) {
6162 01073a5d 2019-08-22 stsp if (repo_path == NULL) {
6163 01073a5d 2019-08-22 stsp repo_path = strdup(
6164 01073a5d 2019-08-22 stsp got_worktree_get_repo_path(worktree));
6165 01073a5d 2019-08-22 stsp if (repo_path == NULL) {
6166 01073a5d 2019-08-22 stsp error = got_error_from_errno("strdup");
6167 01073a5d 2019-08-22 stsp goto done;
6172 01073a5d 2019-08-22 stsp if (repo_path == NULL) {
6173 01073a5d 2019-08-22 stsp repo_path = getcwd(NULL, 0);
6174 01073a5d 2019-08-22 stsp if (repo_path == NULL)
6175 01073a5d 2019-08-22 stsp return got_error_from_errno("getcwd");
6178 01073a5d 2019-08-22 stsp error = got_repo_open(&repo, repo_path);
6179 01073a5d 2019-08-22 stsp free(repo_path);
6180 01073a5d 2019-08-22 stsp if (error != NULL)
6181 01073a5d 2019-08-22 stsp goto done;
6183 01073a5d 2019-08-22 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
6184 01073a5d 2019-08-22 stsp if (error)
6185 01073a5d 2019-08-22 stsp goto done;
6187 01073a5d 2019-08-22 stsp for (i = 0; i < argc; i++) {
6188 01073a5d 2019-08-22 stsp error = match_object_id(&id, &label, argv[i],
6189 01073a5d 2019-08-22 stsp GOT_OBJ_TYPE_ANY, 0, repo);
6190 01073a5d 2019-08-22 stsp if (error)
6193 01073a5d 2019-08-22 stsp error = got_object_get_type(&obj_type, repo, id);
6194 01073a5d 2019-08-22 stsp if (error)
6197 01073a5d 2019-08-22 stsp switch (obj_type) {
6198 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_BLOB:
6199 01073a5d 2019-08-22 stsp error = cat_blob(id, repo, stdout);
6201 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TREE:
6202 01073a5d 2019-08-22 stsp error = cat_tree(id, repo, stdout);
6204 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_COMMIT:
6205 01073a5d 2019-08-22 stsp error = cat_commit(id, repo, stdout);
6207 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TAG:
6208 01073a5d 2019-08-22 stsp error = cat_tag(id, repo, stdout);
6211 01073a5d 2019-08-22 stsp error = got_error(GOT_ERR_OBJ_TYPE);
6214 01073a5d 2019-08-22 stsp if (error)
6216 01073a5d 2019-08-22 stsp free(label);
6217 01073a5d 2019-08-22 stsp label = NULL;
6219 01073a5d 2019-08-22 stsp id = NULL;
6223 01073a5d 2019-08-22 stsp free(label);
6225 01073a5d 2019-08-22 stsp if (worktree)
6226 01073a5d 2019-08-22 stsp got_worktree_close(worktree);
6227 01073a5d 2019-08-22 stsp if (repo) {
6228 01073a5d 2019-08-22 stsp const struct got_error *repo_error;
6229 01073a5d 2019-08-22 stsp repo_error = got_repo_close(repo);
6230 01073a5d 2019-08-22 stsp if (error == NULL)
6231 01073a5d 2019-08-22 stsp error = repo_error;
6233 0ebf8283 2019-07-24 stsp return error;