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);
102 2c7829a4 2019-06-17 stsp static const struct got_error* cmd_init(int, char *[]);
103 3ce1b845 2019-07-15 stsp static const struct got_error* cmd_import(int, char *[]);
104 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_checkout(int, char *[]);
105 507dc3bb 2018-12-29 stsp static const struct got_error* cmd_update(int, char *[]);
106 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_log(int, char *[]);
107 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_diff(int, char *[]);
108 404c43c4 2018-06-21 stsp static const struct got_error* cmd_blame(int, char *[]);
109 5de5890b 2018-10-18 stsp static const struct got_error* cmd_tree(int, char *[]);
110 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_status(int, char *[]);
111 d0eebce4 2019-03-11 stsp static const struct got_error* cmd_ref(int, char *[]);
112 4e759de4 2019-06-26 stsp static const struct got_error* cmd_branch(int, char *[]);
113 d00136be 2019-03-26 stsp static const struct got_error* cmd_add(int, char *[]);
114 648e4ef7 2019-07-09 stsp static const struct got_error* cmd_remove(int, char *[]);
115 a129376b 2019-03-28 stsp static const struct got_error* cmd_revert(int, char *[]);
116 c4296144 2019-05-09 stsp static const struct got_error* cmd_commit(int, char *[]);
117 234035bc 2019-06-01 stsp static const struct got_error* cmd_cherrypick(int, char *[]);
118 5ef14e63 2019-06-02 stsp static const struct got_error* cmd_backout(int, char *[]);
119 818c7501 2019-07-11 stsp static const struct got_error* cmd_rebase(int, char *[]);
120 0ebf8283 2019-07-24 stsp static const struct got_error* cmd_histedit(int, char *[]);
121 715dc77e 2019-08-03 stsp static const struct got_error* cmd_stage(int, char *[]);
122 ad493afc 2019-08-03 stsp static const struct got_error* cmd_unstage(int, char *[]);
124 8cfb4057 2019-07-09 stsp static struct got_cmd got_commands[] = {
125 bc26cce8 2019-08-04 stsp { "init", cmd_init, usage_init, "in" },
126 bc26cce8 2019-08-04 stsp { "import", cmd_import, usage_import, "im" },
127 97b3a7be 2019-07-09 stsp { "checkout", cmd_checkout, usage_checkout, "co" },
128 97b3a7be 2019-07-09 stsp { "update", cmd_update, usage_update, "up" },
129 97b3a7be 2019-07-09 stsp { "log", cmd_log, usage_log, "" },
130 bc26cce8 2019-08-04 stsp { "diff", cmd_diff, usage_diff, "di" },
131 bc26cce8 2019-08-04 stsp { "blame", cmd_blame, usage_blame, "bl" },
132 bc26cce8 2019-08-04 stsp { "tree", cmd_tree, usage_tree, "tr" },
133 97b3a7be 2019-07-09 stsp { "status", cmd_status, usage_status, "st" },
134 97b3a7be 2019-07-09 stsp { "ref", cmd_ref, usage_ref, "" },
135 97b3a7be 2019-07-09 stsp { "branch", cmd_branch, usage_branch, "br" },
136 97b3a7be 2019-07-09 stsp { "add", cmd_add, usage_add, "" },
137 648e4ef7 2019-07-09 stsp { "remove", cmd_remove, usage_remove, "rm" },
138 97b3a7be 2019-07-09 stsp { "revert", cmd_revert, usage_revert, "rv" },
139 97b3a7be 2019-07-09 stsp { "commit", cmd_commit, usage_commit, "ci" },
140 016477fd 2019-07-09 stsp { "cherrypick", cmd_cherrypick, usage_cherrypick, "cy" },
141 97b3a7be 2019-07-09 stsp { "backout", cmd_backout, usage_backout, "bo" },
142 818c7501 2019-07-11 stsp { "rebase", cmd_rebase, usage_rebase, "rb" },
143 0ebf8283 2019-07-24 stsp { "histedit", cmd_histedit, usage_histedit, "he" },
144 715dc77e 2019-08-03 stsp { "stage", cmd_stage, usage_stage, "sg" },
145 ad493afc 2019-08-03 stsp { "unstage", cmd_unstage, usage_unstage, "ug" },
148 ce5b7c56 2019-07-09 stsp static void
149 ce5b7c56 2019-07-09 stsp list_commands(void)
153 ce5b7c56 2019-07-09 stsp fprintf(stderr, "commands:");
154 ce5b7c56 2019-07-09 stsp for (i = 0; i < nitems(got_commands); i++) {
155 ce5b7c56 2019-07-09 stsp struct got_cmd *cmd = &got_commands[i];
156 ce5b7c56 2019-07-09 stsp fprintf(stderr, " %s", cmd->cmd_name);
158 ce5b7c56 2019-07-09 stsp fputc('\n', stderr);
162 5c860e29 2018-03-12 stsp main(int argc, char *argv[])
164 8cfb4057 2019-07-09 stsp struct got_cmd *cmd;
165 5c860e29 2018-03-12 stsp unsigned int i;
167 53ccebc2 2019-07-30 stsp int hflag = 0, Vflag = 0;
169 289e3cbf 2019-02-04 stsp setlocale(LC_CTYPE, "");
171 53ccebc2 2019-07-30 stsp while ((ch = getopt(argc, argv, "hV")) != -1) {
172 5c860e29 2018-03-12 stsp switch (ch) {
180 ce5b7c56 2019-07-09 stsp usage(hflag);
181 5c860e29 2018-03-12 stsp /* NOTREACHED */
185 5c860e29 2018-03-12 stsp argc -= optind;
186 5c860e29 2018-03-12 stsp argv += optind;
187 1e70621d 2018-03-27 stsp optind = 0;
189 53ccebc2 2019-07-30 stsp if (Vflag) {
190 53ccebc2 2019-07-30 stsp got_version_print_str();
194 5c860e29 2018-03-12 stsp if (argc <= 0)
195 ce5b7c56 2019-07-09 stsp usage(hflag);
197 99437157 2018-11-11 stsp signal(SIGINT, catch_sigint);
198 99437157 2018-11-11 stsp signal(SIGPIPE, catch_sigpipe);
200 5c860e29 2018-03-12 stsp for (i = 0; i < nitems(got_commands); i++) {
201 d7d4f210 2018-03-12 stsp const struct got_error *error;
203 5c860e29 2018-03-12 stsp cmd = &got_commands[i];
205 97b3a7be 2019-07-09 stsp if (strcmp(cmd->cmd_name, argv[0]) != 0 &&
206 97b3a7be 2019-07-09 stsp strcmp(cmd->cmd_alias, argv[0]) != 0)
210 1b6b95a8 2018-03-12 stsp got_commands[i].cmd_usage();
212 d7d4f210 2018-03-12 stsp error = got_commands[i].cmd_main(argc, argv);
213 80d5f134 2018-11-11 stsp if (error && !(sigint_received || sigpipe_received)) {
214 d7d4f210 2018-03-12 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
221 20ecf764 2018-03-12 stsp fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
222 ce5b7c56 2019-07-09 stsp list_commands();
226 4ed7e80c 2018-05-20 stsp __dead static void
227 ce5b7c56 2019-07-09 stsp usage(int hflag)
229 53ccebc2 2019-07-30 stsp fprintf(stderr, "usage: %s [-h] [-V] command [arg ...]\n",
230 53ccebc2 2019-07-30 stsp getprogname());
232 ce5b7c56 2019-07-09 stsp list_commands();
236 0266afb7 2019-01-04 stsp static const struct got_error *
237 0ee7065d 2019-05-13 stsp get_editor(char **abspath)
239 0ee7065d 2019-05-13 stsp const struct got_error *err = NULL;
240 e2ba3d07 2019-05-13 stsp const char *editor;
242 8920fa04 2019-08-18 stsp *abspath = NULL;
244 e2ba3d07 2019-05-13 stsp editor = getenv("VISUAL");
245 e2ba3d07 2019-05-13 stsp if (editor == NULL)
246 e2ba3d07 2019-05-13 stsp editor = getenv("EDITOR");
248 0ee7065d 2019-05-13 stsp if (editor) {
249 0ee7065d 2019-05-13 stsp err = got_path_find_prog(abspath, editor);
251 0ee7065d 2019-05-13 stsp return err;
254 0ee7065d 2019-05-13 stsp if (*abspath == NULL) {
255 0ee7065d 2019-05-13 stsp *abspath = strdup("/bin/ed");
256 0ee7065d 2019-05-13 stsp if (*abspath == NULL)
257 0ee7065d 2019-05-13 stsp return got_error_from_errno("strdup");
260 e2ba3d07 2019-05-13 stsp return NULL;
263 e2ba3d07 2019-05-13 stsp static const struct got_error *
264 d0eebce4 2019-03-11 stsp apply_unveil(const char *repo_path, int repo_read_only,
265 c530dc23 2019-07-23 stsp const char *worktree_path)
267 163ce85a 2019-05-13 stsp const struct got_error *err;
269 37c06ea4 2019-07-15 stsp #ifdef PROFILE
270 37c06ea4 2019-07-15 stsp if (unveil("gmon.out", "rwc") != 0)
271 37c06ea4 2019-07-15 stsp return got_error_from_errno2("unveil", "gmon.out");
273 d0eebce4 2019-03-11 stsp if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
274 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", repo_path);
276 0266afb7 2019-01-04 stsp if (worktree_path && unveil(worktree_path, "rwc") != 0)
277 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", worktree_path);
279 f12d0dbe 2019-01-04 stsp if (unveil("/tmp", "rwc") != 0)
280 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", "/tmp");
282 163ce85a 2019-05-13 stsp err = got_privsep_unveil_exec_helpers();
283 163ce85a 2019-05-13 stsp if (err != NULL)
284 163ce85a 2019-05-13 stsp return err;
286 0266afb7 2019-01-04 stsp if (unveil(NULL, NULL) != 0)
287 638f9024 2019-05-13 stsp return got_error_from_errno("unveil");
289 0266afb7 2019-01-04 stsp return NULL;
292 2c7829a4 2019-06-17 stsp __dead static void
293 2c7829a4 2019-06-17 stsp usage_init(void)
295 09ea71ba 2019-07-27 stsp fprintf(stderr, "usage: %s init repository-path\n", getprogname());
299 2c7829a4 2019-06-17 stsp static const struct got_error *
300 2c7829a4 2019-06-17 stsp cmd_init(int argc, char *argv[])
302 2c7829a4 2019-06-17 stsp const struct got_error *error = NULL;
303 2c7829a4 2019-06-17 stsp char *repo_path = NULL;
306 2c7829a4 2019-06-17 stsp while ((ch = getopt(argc, argv, "")) != -1) {
307 2c7829a4 2019-06-17 stsp switch (ch) {
309 2c7829a4 2019-06-17 stsp usage_init();
310 2c7829a4 2019-06-17 stsp /* NOTREACHED */
314 2c7829a4 2019-06-17 stsp argc -= optind;
315 2c7829a4 2019-06-17 stsp argv += optind;
317 2c7829a4 2019-06-17 stsp #ifndef PROFILE
318 2c7829a4 2019-06-17 stsp if (pledge("stdio rpath wpath cpath unveil", NULL) == -1)
319 2c7829a4 2019-06-17 stsp err(1, "pledge");
321 2c7829a4 2019-06-17 stsp if (argc != 1)
322 bc20e173 2019-06-17 stsp usage_init();
324 2c7829a4 2019-06-17 stsp repo_path = strdup(argv[0]);
325 2c7829a4 2019-06-17 stsp if (repo_path == NULL)
326 2c7829a4 2019-06-17 stsp return got_error_from_errno("strdup");
328 2c7829a4 2019-06-17 stsp got_path_strip_trailing_slashes(repo_path);
330 2c7829a4 2019-06-17 stsp error = got_path_mkdir(repo_path);
331 2c7829a4 2019-06-17 stsp if (error &&
332 2c7829a4 2019-06-17 stsp !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
335 c530dc23 2019-07-23 stsp error = apply_unveil(repo_path, 0, NULL);
339 2c7829a4 2019-06-17 stsp error = got_repo_init(repo_path);
340 2c7829a4 2019-06-17 stsp if (error != NULL)
344 3ce1b845 2019-07-15 stsp free(repo_path);
345 3ce1b845 2019-07-15 stsp return error;
348 3ce1b845 2019-07-15 stsp __dead static void
349 3ce1b845 2019-07-15 stsp usage_import(void)
351 3ce1b845 2019-07-15 stsp fprintf(stderr, "usage: %s import [-b branch] [-m message] "
352 3ce1b845 2019-07-15 stsp "[-r repository-path] [-I pattern] path\n", getprogname());
357 3ce1b845 2019-07-15 stsp spawn_editor(const char *editor, const char *file)
360 3ce1b845 2019-07-15 stsp sig_t sighup, sigint, sigquit;
361 3ce1b845 2019-07-15 stsp int st = -1;
363 3ce1b845 2019-07-15 stsp sighup = signal(SIGHUP, SIG_IGN);
364 3ce1b845 2019-07-15 stsp sigint = signal(SIGINT, SIG_IGN);
365 3ce1b845 2019-07-15 stsp sigquit = signal(SIGQUIT, SIG_IGN);
367 3ce1b845 2019-07-15 stsp switch (pid = fork()) {
369 3ce1b845 2019-07-15 stsp goto doneediting;
371 3ce1b845 2019-07-15 stsp execl(editor, editor, file, (char *)NULL);
372 3ce1b845 2019-07-15 stsp _exit(127);
375 3ce1b845 2019-07-15 stsp while (waitpid(pid, &st, 0) == -1)
376 3ce1b845 2019-07-15 stsp if (errno != EINTR)
379 3ce1b845 2019-07-15 stsp doneediting:
380 3ce1b845 2019-07-15 stsp (void)signal(SIGHUP, sighup);
381 3ce1b845 2019-07-15 stsp (void)signal(SIGINT, sigint);
382 3ce1b845 2019-07-15 stsp (void)signal(SIGQUIT, sigquit);
384 3ce1b845 2019-07-15 stsp if (!WIFEXITED(st)) {
385 3ce1b845 2019-07-15 stsp errno = EINTR;
389 3ce1b845 2019-07-15 stsp return WEXITSTATUS(st);
392 3ce1b845 2019-07-15 stsp static const struct got_error *
393 3ce1b845 2019-07-15 stsp edit_logmsg(char **logmsg, const char *editor, const char *logmsg_path,
394 3ce1b845 2019-07-15 stsp const char *initial_content)
396 3ce1b845 2019-07-15 stsp const struct got_error *err = NULL;
397 3ce1b845 2019-07-15 stsp char buf[1024];
398 3ce1b845 2019-07-15 stsp struct stat st, st2;
400 3ce1b845 2019-07-15 stsp int content_changed = 0;
401 3ce1b845 2019-07-15 stsp size_t len;
403 3ce1b845 2019-07-15 stsp *logmsg = NULL;
405 3ce1b845 2019-07-15 stsp if (stat(logmsg_path, &st) == -1)
406 3ce1b845 2019-07-15 stsp return got_error_from_errno2("stat", logmsg_path);
408 3ce1b845 2019-07-15 stsp if (spawn_editor(editor, logmsg_path) == -1)
409 3ce1b845 2019-07-15 stsp return got_error_from_errno("failed spawning editor");
411 3ce1b845 2019-07-15 stsp if (stat(logmsg_path, &st2) == -1)
412 3ce1b845 2019-07-15 stsp return got_error_from_errno("stat");
414 3ce1b845 2019-07-15 stsp if (st.st_mtime == st2.st_mtime && st.st_size == st2.st_size)
415 3ce1b845 2019-07-15 stsp return got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
416 3ce1b845 2019-07-15 stsp "no changes made to commit message, aborting");
418 3ce1b845 2019-07-15 stsp *logmsg = malloc(st2.st_size + 1);
419 3ce1b845 2019-07-15 stsp if (*logmsg == NULL)
420 3ce1b845 2019-07-15 stsp return got_error_from_errno("malloc");
421 3ce1b845 2019-07-15 stsp (*logmsg)[0] = '\0';
424 3ce1b845 2019-07-15 stsp fp = fopen(logmsg_path, "r");
425 3ce1b845 2019-07-15 stsp if (fp == NULL) {
426 3ce1b845 2019-07-15 stsp err = got_error_from_errno("fopen");
429 3ce1b845 2019-07-15 stsp while (fgets(buf, sizeof(buf), fp) != NULL) {
430 3ce1b845 2019-07-15 stsp if (!content_changed && strcmp(buf, initial_content) != 0)
431 3ce1b845 2019-07-15 stsp content_changed = 1;
432 3ce1b845 2019-07-15 stsp if (buf[0] == '#' || (len == 0 && buf[0] == '\n'))
433 3ce1b845 2019-07-15 stsp continue; /* remove comments and leading empty lines */
434 3ce1b845 2019-07-15 stsp len = strlcat(*logmsg, buf, st2.st_size);
436 3ce1b845 2019-07-15 stsp fclose(fp);
438 3ce1b845 2019-07-15 stsp while (len > 0 && (*logmsg)[len - 1] == '\n') {
439 3ce1b845 2019-07-15 stsp (*logmsg)[len - 1] = '\0';
443 3ce1b845 2019-07-15 stsp if (len == 0 || !content_changed)
444 3ce1b845 2019-07-15 stsp err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
445 3ce1b845 2019-07-15 stsp "commit message cannot be empty, aborting");
448 3ce1b845 2019-07-15 stsp free(*logmsg);
449 3ce1b845 2019-07-15 stsp *logmsg = NULL;
451 3ce1b845 2019-07-15 stsp return err;
454 3ce1b845 2019-07-15 stsp static const struct got_error *
455 3ce1b845 2019-07-15 stsp collect_import_msg(char **logmsg, const char *editor, const char *path_dir,
456 3ce1b845 2019-07-15 stsp const char *branch_name)
458 3ce1b845 2019-07-15 stsp char *initial_content = NULL, *logmsg_path = NULL;
459 3ce1b845 2019-07-15 stsp const struct got_error *err = NULL;
462 3ce1b845 2019-07-15 stsp if (asprintf(&initial_content,
463 3ce1b845 2019-07-15 stsp "\n# %s to be imported to branch %s\n", path_dir,
464 3ce1b845 2019-07-15 stsp branch_name) == -1)
465 3ce1b845 2019-07-15 stsp return got_error_from_errno("asprintf");
467 3ce1b845 2019-07-15 stsp err = got_opentemp_named_fd(&logmsg_path, &fd, "/tmp/got-importmsg");
471 3ce1b845 2019-07-15 stsp dprintf(fd, initial_content);
474 3ce1b845 2019-07-15 stsp err = edit_logmsg(logmsg, editor, logmsg_path, initial_content);
476 3ce1b845 2019-07-15 stsp free(initial_content);
477 3ce1b845 2019-07-15 stsp free(logmsg_path);
478 3ce1b845 2019-07-15 stsp return err;
481 3ce1b845 2019-07-15 stsp static const struct got_error *
482 3ce1b845 2019-07-15 stsp import_progress(void *arg, const char *path)
484 3ce1b845 2019-07-15 stsp printf("A %s\n", path);
485 3ce1b845 2019-07-15 stsp return NULL;
488 3ce1b845 2019-07-15 stsp static const struct got_error *
489 84792843 2019-08-09 stsp get_author(const char **author)
491 84792843 2019-08-09 stsp const char *got_author;
493 84792843 2019-08-09 stsp *author = NULL;
495 84792843 2019-08-09 stsp got_author = getenv("GOT_AUTHOR");
496 84792843 2019-08-09 stsp if (got_author == NULL) {
497 84792843 2019-08-09 stsp /* TODO: Look up user in password database? */
498 84792843 2019-08-09 stsp return got_error(GOT_ERR_COMMIT_NO_AUTHOR);
501 84792843 2019-08-09 stsp *author = got_author;
504 84792843 2019-08-09 stsp * Really dumb email address check; we're only doing this to
505 84792843 2019-08-09 stsp * avoid git's object parser breaking on commits we create.
507 84792843 2019-08-09 stsp while (*got_author && *got_author != '<')
508 84792843 2019-08-09 stsp got_author++;
509 84792843 2019-08-09 stsp if (*got_author != '<')
510 84792843 2019-08-09 stsp return got_error(GOT_ERR_COMMIT_NO_EMAIL);
511 84792843 2019-08-09 stsp while (*got_author && *got_author != '@')
512 84792843 2019-08-09 stsp got_author++;
513 84792843 2019-08-09 stsp if (*got_author != '@')
514 84792843 2019-08-09 stsp return got_error(GOT_ERR_COMMIT_NO_EMAIL);
515 84792843 2019-08-09 stsp while (*got_author && *got_author != '>')
516 84792843 2019-08-09 stsp got_author++;
517 84792843 2019-08-09 stsp if (*got_author != '>')
518 84792843 2019-08-09 stsp return got_error(GOT_ERR_COMMIT_NO_EMAIL);
520 84792843 2019-08-09 stsp return NULL;
523 84792843 2019-08-09 stsp static const struct got_error *
524 3ce1b845 2019-07-15 stsp cmd_import(int argc, char *argv[])
526 3ce1b845 2019-07-15 stsp const struct got_error *error = NULL;
527 3ce1b845 2019-07-15 stsp char *path_dir = NULL, *repo_path = NULL, *logmsg = NULL;
528 3ce1b845 2019-07-15 stsp char *editor = NULL;
529 84792843 2019-08-09 stsp const char *author;
530 3ce1b845 2019-07-15 stsp const char *branch_name = "master";
531 3ce1b845 2019-07-15 stsp char *refname = NULL, *id_str = NULL;
532 3ce1b845 2019-07-15 stsp struct got_repository *repo = NULL;
533 3ce1b845 2019-07-15 stsp struct got_reference *branch_ref = NULL, *head_ref = NULL;
534 3ce1b845 2019-07-15 stsp struct got_object_id *new_commit_id = NULL;
536 3ce1b845 2019-07-15 stsp struct got_pathlist_head ignores;
537 3ce1b845 2019-07-15 stsp struct got_pathlist_entry *pe;
539 3ce1b845 2019-07-15 stsp TAILQ_INIT(&ignores);
541 3ce1b845 2019-07-15 stsp while ((ch = getopt(argc, argv, "b:m:r:I:")) != -1) {
542 3ce1b845 2019-07-15 stsp switch (ch) {
544 3ce1b845 2019-07-15 stsp branch_name = optarg;
547 3ce1b845 2019-07-15 stsp logmsg = strdup(optarg);
548 3ce1b845 2019-07-15 stsp if (logmsg == NULL) {
549 3ce1b845 2019-07-15 stsp error = got_error_from_errno("strdup");
554 3ce1b845 2019-07-15 stsp repo_path = realpath(optarg, NULL);
555 3ce1b845 2019-07-15 stsp if (repo_path == NULL) {
556 3ce1b845 2019-07-15 stsp error = got_error_from_errno("realpath");
561 3ce1b845 2019-07-15 stsp if (optarg[0] == '\0')
563 3ce1b845 2019-07-15 stsp error = got_pathlist_insert(&pe, &ignores, optarg,
569 3ce1b845 2019-07-15 stsp usage_init();
570 3ce1b845 2019-07-15 stsp /* NOTREACHED */
574 3ce1b845 2019-07-15 stsp argc -= optind;
575 3ce1b845 2019-07-15 stsp argv += optind;
577 3ce1b845 2019-07-15 stsp #ifndef PROFILE
578 3ce1b845 2019-07-15 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec unveil",
579 3ce1b845 2019-07-15 stsp NULL) == -1)
580 3ce1b845 2019-07-15 stsp err(1, "pledge");
582 3ce1b845 2019-07-15 stsp if (argc != 1)
583 3ce1b845 2019-07-15 stsp usage_import();
585 84792843 2019-08-09 stsp error = get_author(&author);
587 84792843 2019-08-09 stsp return error;
589 3ce1b845 2019-07-15 stsp if (repo_path == NULL) {
590 3ce1b845 2019-07-15 stsp repo_path = getcwd(NULL, 0);
591 3ce1b845 2019-07-15 stsp if (repo_path == NULL)
592 3ce1b845 2019-07-15 stsp return got_error_from_errno("getcwd");
594 3ce1b845 2019-07-15 stsp got_path_strip_trailing_slashes(repo_path);
595 3ce1b845 2019-07-15 stsp error = got_repo_open(&repo, repo_path);
599 3ce1b845 2019-07-15 stsp if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
600 3ce1b845 2019-07-15 stsp error = got_error_from_errno("asprintf");
604 3ce1b845 2019-07-15 stsp error = got_ref_open(&branch_ref, repo, refname, 0);
605 3ce1b845 2019-07-15 stsp if (error) {
606 3ce1b845 2019-07-15 stsp if (error->code != GOT_ERR_NOT_REF)
609 3ce1b845 2019-07-15 stsp error = got_error_msg(GOT_ERR_BRANCH_EXISTS,
610 3ce1b845 2019-07-15 stsp "import target branch already exists");
614 3ce1b845 2019-07-15 stsp path_dir = realpath(argv[0], NULL);
615 3ce1b845 2019-07-15 stsp if (path_dir == NULL) {
616 3ce1b845 2019-07-15 stsp error = got_error_from_errno("realpath");
619 3ce1b845 2019-07-15 stsp got_path_strip_trailing_slashes(path_dir);
622 3ce1b845 2019-07-15 stsp * unveil(2) traverses exec(2); if an editor is used we have
623 3ce1b845 2019-07-15 stsp * to apply unveil after the log message has been written.
625 3ce1b845 2019-07-15 stsp if (logmsg == NULL || strlen(logmsg) == 0) {
626 3ce1b845 2019-07-15 stsp error = get_editor(&editor);
629 3ce1b845 2019-07-15 stsp error = collect_import_msg(&logmsg, editor, path_dir, refname);
634 3ce1b845 2019-07-15 stsp if (unveil(path_dir, "r") != 0)
635 3ce1b845 2019-07-15 stsp return got_error_from_errno2("unveil", path_dir);
637 c530dc23 2019-07-23 stsp error = apply_unveil(got_repo_get_path(repo), 0, NULL);
641 3ce1b845 2019-07-15 stsp error = got_repo_import(&new_commit_id, path_dir, logmsg,
642 84792843 2019-08-09 stsp author, &ignores, repo, import_progress, NULL);
646 3ce1b845 2019-07-15 stsp error = got_ref_alloc(&branch_ref, refname, new_commit_id);
650 3ce1b845 2019-07-15 stsp error = got_ref_write(branch_ref, repo);
654 3ce1b845 2019-07-15 stsp error = got_object_id_str(&id_str, new_commit_id);
658 3ce1b845 2019-07-15 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
659 3ce1b845 2019-07-15 stsp if (error) {
660 3ce1b845 2019-07-15 stsp if (error->code != GOT_ERR_NOT_REF)
663 3ce1b845 2019-07-15 stsp error = got_ref_alloc_symref(&head_ref, GOT_REF_HEAD,
664 3ce1b845 2019-07-15 stsp branch_ref);
668 3ce1b845 2019-07-15 stsp error = got_ref_write(head_ref, repo);
673 3ce1b845 2019-07-15 stsp printf("Created branch %s with commit %s\n",
674 3ce1b845 2019-07-15 stsp got_ref_get_name(branch_ref), id_str);
676 2c7829a4 2019-06-17 stsp free(repo_path);
677 3ce1b845 2019-07-15 stsp free(editor);
678 3ce1b845 2019-07-15 stsp free(refname);
679 3ce1b845 2019-07-15 stsp free(new_commit_id);
680 3ce1b845 2019-07-15 stsp free(id_str);
681 3ce1b845 2019-07-15 stsp if (branch_ref)
682 3ce1b845 2019-07-15 stsp got_ref_close(branch_ref);
683 3ce1b845 2019-07-15 stsp if (head_ref)
684 3ce1b845 2019-07-15 stsp got_ref_close(head_ref);
685 2c7829a4 2019-06-17 stsp return error;
688 4ed7e80c 2018-05-20 stsp __dead static void
689 c09a553d 2018-03-12 stsp usage_checkout(void)
691 08573d5b 2019-05-14 stsp fprintf(stderr, "usage: %s checkout [-b branch] [-c commit] "
692 08573d5b 2019-05-14 stsp "[-p prefix] repository-path [worktree-path]\n", getprogname());
696 1ee397ad 2019-07-12 stsp static const struct got_error *
697 a0eb853d 2018-12-29 stsp checkout_progress(void *arg, unsigned char status, const char *path)
699 92a684f4 2018-03-12 stsp char *worktree_path = arg;
701 a484d721 2019-06-10 stsp /* Base commit bump happens silently. */
702 a484d721 2019-06-10 stsp if (status == GOT_STATUS_BUMP_BASE)
703 1ee397ad 2019-07-12 stsp return NULL;
705 92a684f4 2018-03-12 stsp while (path[0] == '/')
708 d7b62c98 2018-12-27 stsp printf("%c %s/%s\n", status, worktree_path, path);
709 1ee397ad 2019-07-12 stsp return NULL;
712 99437157 2018-11-11 stsp static const struct got_error *
713 6bad629b 2019-02-04 stsp check_cancelled(void *arg)
715 99437157 2018-11-11 stsp if (sigint_received || sigpipe_received)
716 99437157 2018-11-11 stsp return got_error(GOT_ERR_CANCELLED);
717 99437157 2018-11-11 stsp return NULL;
720 8069f636 2019-01-12 stsp static const struct got_error *
721 024e9686 2019-05-14 stsp check_linear_ancestry(struct got_object_id *commit_id,
722 024e9686 2019-05-14 stsp struct got_object_id *base_commit_id, struct got_repository *repo)
724 d5bea539 2019-05-13 stsp const struct got_error *err = NULL;
725 024e9686 2019-05-14 stsp struct got_object_id *yca_id;
727 d5bea539 2019-05-13 stsp err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
728 6fb7cd11 2019-08-22 stsp commit_id, base_commit_id, repo, check_cancelled, NULL);
730 36a38700 2019-05-10 stsp return err;
732 d5bea539 2019-05-13 stsp if (yca_id == NULL)
733 d5bea539 2019-05-13 stsp return got_error(GOT_ERR_ANCESTRY);
736 d5bea539 2019-05-13 stsp * Require a straight line of history between the target commit
737 d5bea539 2019-05-13 stsp * and the work tree's base commit.
739 d5751d49 2019-05-14 stsp * Non-linear situations such as this require a rebase:
741 d5bea539 2019-05-13 stsp * (commit) D F (base_commit)
749 d5bea539 2019-05-13 stsp * 'got update' only handles linear cases:
750 d5bea539 2019-05-13 stsp * Update forwards in time: A (base/yca) - B - C - D (commit)
751 efa2b6f7 2019-05-14 stsp * Update backwards in time: D (base) - C - B - A (commit/yca)
753 d5bea539 2019-05-13 stsp if (got_object_id_cmp(commit_id, yca_id) != 0 &&
754 d5bea539 2019-05-13 stsp got_object_id_cmp(base_commit_id, yca_id) != 0)
755 d5bea539 2019-05-13 stsp return got_error(GOT_ERR_ANCESTRY);
757 d5bea539 2019-05-13 stsp free(yca_id);
758 d5bea539 2019-05-13 stsp return NULL;
761 a367ff0f 2019-05-14 stsp static const struct got_error *
762 a367ff0f 2019-05-14 stsp check_same_branch(struct got_object_id *commit_id,
763 a51a74b3 2019-07-27 stsp struct got_reference *head_ref, struct got_object_id *yca_id,
764 a51a74b3 2019-07-27 stsp struct got_repository *repo)
766 a367ff0f 2019-05-14 stsp const struct got_error *err = NULL;
767 a367ff0f 2019-05-14 stsp struct got_commit_graph *graph = NULL;
768 a367ff0f 2019-05-14 stsp struct got_object_id *head_commit_id = NULL;
769 a367ff0f 2019-05-14 stsp int is_same_branch = 0;
771 a367ff0f 2019-05-14 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
775 a51a74b3 2019-07-27 stsp if (got_object_id_cmp(head_commit_id, commit_id) == 0) {
776 a51a74b3 2019-07-27 stsp is_same_branch = 1;
779 a51a74b3 2019-07-27 stsp if (yca_id && got_object_id_cmp(commit_id, yca_id) == 0) {
780 a51a74b3 2019-07-27 stsp is_same_branch = 1;
784 a367ff0f 2019-05-14 stsp err = got_commit_graph_open(&graph, head_commit_id, "/", 1, repo);
788 6fb7cd11 2019-08-22 stsp err = got_commit_graph_iter_start(graph, head_commit_id, repo,
789 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
794 a367ff0f 2019-05-14 stsp struct got_object_id *id;
795 a367ff0f 2019-05-14 stsp err = got_commit_graph_iter_next(&id, graph);
797 a367ff0f 2019-05-14 stsp if (err->code == GOT_ERR_ITER_COMPLETED) {
798 a367ff0f 2019-05-14 stsp err = NULL;
800 a14c42fa 2019-07-15 stsp } else if (err->code != GOT_ERR_ITER_NEED_MORE)
802 a367ff0f 2019-05-14 stsp err = got_commit_graph_fetch_commits(graph, 1,
803 6fb7cd11 2019-08-22 stsp repo, check_cancelled, NULL);
809 a51a74b3 2019-07-27 stsp if (yca_id && got_object_id_cmp(id, yca_id) == 0)
811 a367ff0f 2019-05-14 stsp if (got_object_id_cmp(id, commit_id) == 0) {
812 a367ff0f 2019-05-14 stsp is_same_branch = 1;
819 a367ff0f 2019-05-14 stsp got_commit_graph_close(graph);
820 a367ff0f 2019-05-14 stsp free(head_commit_id);
821 a367ff0f 2019-05-14 stsp if (!err && !is_same_branch)
822 a367ff0f 2019-05-14 stsp err = got_error(GOT_ERR_ANCESTRY);
823 04f57cb3 2019-07-25 stsp return err;
826 04f57cb3 2019-07-25 stsp static const struct got_error *
827 04f57cb3 2019-07-25 stsp resolve_commit_arg(struct got_object_id **commit_id,
828 04f57cb3 2019-07-25 stsp const char *commit_id_arg, struct got_repository *repo)
830 04f57cb3 2019-07-25 stsp const struct got_error *err;
831 04f57cb3 2019-07-25 stsp struct got_reference *ref;
832 303e2782 2019-08-09 stsp struct got_tag_object *tag;
834 303e2782 2019-08-09 stsp err = got_repo_object_match_tag(&tag, commit_id_arg,
835 303e2782 2019-08-09 stsp GOT_OBJ_TYPE_COMMIT, repo);
836 303e2782 2019-08-09 stsp if (err == NULL) {
837 303e2782 2019-08-09 stsp *commit_id = got_object_id_dup(
838 303e2782 2019-08-09 stsp got_object_tag_get_object_id(tag));
839 303e2782 2019-08-09 stsp if (*commit_id == NULL)
840 303e2782 2019-08-09 stsp err = got_error_from_errno("got_object_id_dup");
841 303e2782 2019-08-09 stsp got_object_tag_close(tag);
842 303e2782 2019-08-09 stsp return err;
843 303e2782 2019-08-09 stsp } else if (err->code != GOT_ERR_NO_OBJ)
844 303e2782 2019-08-09 stsp return err;
846 04f57cb3 2019-07-25 stsp err = got_ref_open(&ref, repo, commit_id_arg, 0);
847 04f57cb3 2019-07-25 stsp if (err == NULL) {
848 04f57cb3 2019-07-25 stsp err = got_ref_resolve(commit_id, repo, ref);
849 04f57cb3 2019-07-25 stsp got_ref_close(ref);
851 04f57cb3 2019-07-25 stsp if (err->code != GOT_ERR_NOT_REF)
852 04f57cb3 2019-07-25 stsp return err;
853 04f57cb3 2019-07-25 stsp err = got_repo_match_object_id_prefix(commit_id,
854 04f57cb3 2019-07-25 stsp commit_id_arg, GOT_OBJ_TYPE_COMMIT, repo);
856 a367ff0f 2019-05-14 stsp return err;
859 4ed7e80c 2018-05-20 stsp static const struct got_error *
860 c09a553d 2018-03-12 stsp cmd_checkout(int argc, char *argv[])
862 c09a553d 2018-03-12 stsp const struct got_error *error = NULL;
863 c09a553d 2018-03-12 stsp struct got_repository *repo = NULL;
864 c09a553d 2018-03-12 stsp struct got_reference *head_ref = NULL;
865 c09a553d 2018-03-12 stsp struct got_worktree *worktree = NULL;
866 c09a553d 2018-03-12 stsp char *repo_path = NULL;
867 c09a553d 2018-03-12 stsp char *worktree_path = NULL;
868 0bb8a95e 2018-03-12 stsp const char *path_prefix = "";
869 08573d5b 2019-05-14 stsp const char *branch_name = GOT_REF_HEAD;
870 8069f636 2019-01-12 stsp char *commit_id_str = NULL;
871 72151b04 2019-05-11 stsp int ch, same_path_prefix;
872 f2ea84fa 2019-07-27 stsp struct got_pathlist_head paths;
874 f2ea84fa 2019-07-27 stsp TAILQ_INIT(&paths);
876 08573d5b 2019-05-14 stsp while ((ch = getopt(argc, argv, "b:c:p:")) != -1) {
877 0bb8a95e 2018-03-12 stsp switch (ch) {
879 08573d5b 2019-05-14 stsp branch_name = optarg;
882 8069f636 2019-01-12 stsp commit_id_str = strdup(optarg);
883 8069f636 2019-01-12 stsp if (commit_id_str == NULL)
884 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
887 0bb8a95e 2018-03-12 stsp path_prefix = optarg;
890 2deda0b9 2019-03-07 stsp usage_checkout();
891 0bb8a95e 2018-03-12 stsp /* NOTREACHED */
895 0bb8a95e 2018-03-12 stsp argc -= optind;
896 0bb8a95e 2018-03-12 stsp argv += optind;
898 6715a751 2018-03-16 stsp #ifndef PROFILE
899 68ed9ba5 2019-02-10 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
900 68ed9ba5 2019-02-10 stsp "unveil", NULL) == -1)
901 c09a553d 2018-03-12 stsp err(1, "pledge");
903 0bb8a95e 2018-03-12 stsp if (argc == 1) {
904 c09a553d 2018-03-12 stsp char *cwd, *base, *dotgit;
905 76089277 2018-04-01 stsp repo_path = realpath(argv[0], NULL);
906 76089277 2018-04-01 stsp if (repo_path == NULL)
907 638f9024 2019-05-13 stsp return got_error_from_errno2("realpath", argv[0]);
908 c09a553d 2018-03-12 stsp cwd = getcwd(NULL, 0);
909 76089277 2018-04-01 stsp if (cwd == NULL) {
910 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
913 230a42bd 2019-05-11 jcs if (path_prefix[0]) {
914 230a42bd 2019-05-11 jcs base = basename(path_prefix);
915 230a42bd 2019-05-11 jcs if (base == NULL) {
916 638f9024 2019-05-13 stsp error = got_error_from_errno2("basename",
917 230a42bd 2019-05-11 jcs path_prefix);
921 5d7c1dab 2018-04-01 stsp base = basename(repo_path);
922 230a42bd 2019-05-11 jcs if (base == NULL) {
923 638f9024 2019-05-13 stsp error = got_error_from_errno2("basename",
928 c09a553d 2018-03-12 stsp dotgit = strstr(base, ".git");
929 c09a553d 2018-03-12 stsp if (dotgit)
930 c09a553d 2018-03-12 stsp *dotgit = '\0';
931 c09a553d 2018-03-12 stsp if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
932 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
937 0bb8a95e 2018-03-12 stsp } else if (argc == 2) {
938 76089277 2018-04-01 stsp repo_path = realpath(argv[0], NULL);
939 76089277 2018-04-01 stsp if (repo_path == NULL) {
940 638f9024 2019-05-13 stsp error = got_error_from_errno2("realpath", argv[0]);
943 f7b38925 2018-04-01 stsp worktree_path = realpath(argv[1], NULL);
944 76089277 2018-04-01 stsp if (worktree_path == NULL) {
945 b4b3a7dd 2019-07-22 stsp if (errno != ENOENT) {
946 b4b3a7dd 2019-07-22 stsp error = got_error_from_errno2("realpath",
950 b4b3a7dd 2019-07-22 stsp worktree_path = strdup(argv[1]);
951 b4b3a7dd 2019-07-22 stsp if (worktree_path == NULL) {
952 b4b3a7dd 2019-07-22 stsp error = got_error_from_errno("strdup");
957 c09a553d 2018-03-12 stsp usage_checkout();
959 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
960 72151b04 2019-05-11 stsp got_path_strip_trailing_slashes(worktree_path);
962 c09a553d 2018-03-12 stsp error = got_repo_open(&repo, repo_path);
963 c09a553d 2018-03-12 stsp if (error != NULL)
966 c530dc23 2019-07-23 stsp /* Pre-create work tree path for unveil(2) */
967 c530dc23 2019-07-23 stsp error = got_path_mkdir(worktree_path);
968 c530dc23 2019-07-23 stsp if (error) {
969 80c1b583 2019-08-07 stsp if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
970 80c1b583 2019-08-07 stsp !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
972 c530dc23 2019-07-23 stsp if (!got_path_dir_is_empty(worktree_path)) {
973 c530dc23 2019-07-23 stsp error = got_error_path(worktree_path,
974 c530dc23 2019-07-23 stsp GOT_ERR_DIR_NOT_EMPTY);
979 c530dc23 2019-07-23 stsp error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
983 08573d5b 2019-05-14 stsp error = got_ref_open(&head_ref, repo, branch_name, 0);
984 c09a553d 2018-03-12 stsp if (error != NULL)
987 0bb8a95e 2018-03-12 stsp error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
988 d70b8e30 2018-12-27 stsp if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
991 c09a553d 2018-03-12 stsp error = got_worktree_open(&worktree, worktree_path);
992 c09a553d 2018-03-12 stsp if (error != NULL)
995 e5dc7198 2018-12-29 stsp error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
996 e5dc7198 2018-12-29 stsp path_prefix);
997 e5dc7198 2018-12-29 stsp if (error != NULL)
999 e5dc7198 2018-12-29 stsp if (!same_path_prefix) {
1000 49520a32 2018-12-29 stsp error = got_error(GOT_ERR_PATH_PREFIX);
1001 49520a32 2018-12-29 stsp goto done;
1004 8069f636 2019-01-12 stsp if (commit_id_str) {
1005 04f57cb3 2019-07-25 stsp struct got_object_id *commit_id;
1006 04f57cb3 2019-07-25 stsp error = resolve_commit_arg(&commit_id, commit_id_str, repo);
1007 30837e32 2019-07-25 stsp if (error)
1008 8069f636 2019-01-12 stsp goto done;
1009 024e9686 2019-05-14 stsp error = check_linear_ancestry(commit_id,
1010 024e9686 2019-05-14 stsp got_worktree_get_base_commit_id(worktree), repo);
1011 8069f636 2019-01-12 stsp if (error != NULL) {
1012 8069f636 2019-01-12 stsp free(commit_id);
1013 8069f636 2019-01-12 stsp goto done;
1015 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
1016 45d344f6 2019-05-14 stsp if (error)
1017 45d344f6 2019-05-14 stsp goto done;
1018 8069f636 2019-01-12 stsp error = got_worktree_set_base_commit_id(worktree, repo,
1019 8069f636 2019-01-12 stsp commit_id);
1020 8069f636 2019-01-12 stsp free(commit_id);
1021 8069f636 2019-01-12 stsp if (error)
1022 8069f636 2019-01-12 stsp goto done;
1025 adc19d55 2019-07-28 stsp error = got_pathlist_append(&paths, "", NULL);
1026 f2ea84fa 2019-07-27 stsp if (error)
1027 f2ea84fa 2019-07-27 stsp goto done;
1028 f2ea84fa 2019-07-27 stsp error = got_worktree_checkout_files(worktree, &paths, repo,
1029 6bad629b 2019-02-04 stsp checkout_progress, worktree_path, check_cancelled, NULL);
1030 c09a553d 2018-03-12 stsp if (error != NULL)
1031 c09a553d 2018-03-12 stsp goto done;
1033 b65ae19a 2018-04-24 stsp printf("Now shut up and hack\n");
1036 f2ea84fa 2019-07-27 stsp got_pathlist_free(&paths);
1037 8069f636 2019-01-12 stsp free(commit_id_str);
1038 76089277 2018-04-01 stsp free(repo_path);
1039 507dc3bb 2018-12-29 stsp free(worktree_path);
1040 507dc3bb 2018-12-29 stsp return error;
1043 507dc3bb 2018-12-29 stsp __dead static void
1044 507dc3bb 2018-12-29 stsp usage_update(void)
1046 f2ea84fa 2019-07-27 stsp fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path ...]\n",
1047 507dc3bb 2018-12-29 stsp getprogname());
1051 1ee397ad 2019-07-12 stsp static const struct got_error *
1052 507dc3bb 2018-12-29 stsp update_progress(void *arg, unsigned char status, const char *path)
1054 784955db 2019-01-12 stsp int *did_something = arg;
1056 507dc3bb 2018-12-29 stsp if (status == GOT_STATUS_EXISTS)
1057 1ee397ad 2019-07-12 stsp return NULL;
1059 1545c615 2019-02-10 stsp *did_something = 1;
1061 a484d721 2019-06-10 stsp /* Base commit bump happens silently. */
1062 a484d721 2019-06-10 stsp if (status == GOT_STATUS_BUMP_BASE)
1063 1ee397ad 2019-07-12 stsp return NULL;
1065 507dc3bb 2018-12-29 stsp while (path[0] == '/')
1067 507dc3bb 2018-12-29 stsp printf("%c %s\n", status, path);
1068 1ee397ad 2019-07-12 stsp return NULL;
1071 be7061eb 2018-12-30 stsp static const struct got_error *
1072 a1fb16d8 2019-05-24 stsp switch_head_ref(struct got_reference *head_ref,
1073 a1fb16d8 2019-05-24 stsp struct got_object_id *commit_id, struct got_worktree *worktree,
1074 a1fb16d8 2019-05-24 stsp struct got_repository *repo)
1076 a1fb16d8 2019-05-24 stsp const struct got_error *err = NULL;
1077 a1fb16d8 2019-05-24 stsp char *base_id_str;
1078 a1fb16d8 2019-05-24 stsp int ref_has_moved = 0;
1080 a1fb16d8 2019-05-24 stsp /* Trivial case: switching between two different references. */
1081 a1fb16d8 2019-05-24 stsp if (strcmp(got_ref_get_name(head_ref),
1082 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree)) != 0) {
1083 a1fb16d8 2019-05-24 stsp printf("Switching work tree from %s to %s\n",
1084 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree),
1085 a1fb16d8 2019-05-24 stsp got_ref_get_name(head_ref));
1086 a1fb16d8 2019-05-24 stsp return got_worktree_set_head_ref(worktree, head_ref);
1089 a1fb16d8 2019-05-24 stsp err = check_linear_ancestry(commit_id,
1090 a1fb16d8 2019-05-24 stsp got_worktree_get_base_commit_id(worktree), repo);
1091 a1fb16d8 2019-05-24 stsp if (err) {
1092 a1fb16d8 2019-05-24 stsp if (err->code != GOT_ERR_ANCESTRY)
1093 a1fb16d8 2019-05-24 stsp return err;
1094 a1fb16d8 2019-05-24 stsp ref_has_moved = 1;
1096 a1fb16d8 2019-05-24 stsp if (!ref_has_moved)
1097 a1fb16d8 2019-05-24 stsp return NULL;
1099 a1fb16d8 2019-05-24 stsp /* Switching to a rebased branch with the same reference name. */
1100 a1fb16d8 2019-05-24 stsp err = got_object_id_str(&base_id_str,
1101 a1fb16d8 2019-05-24 stsp got_worktree_get_base_commit_id(worktree));
1103 a1fb16d8 2019-05-24 stsp return err;
1104 a1fb16d8 2019-05-24 stsp printf("Reference %s now points at a different branch\n",
1105 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree));
1106 a1fb16d8 2019-05-24 stsp printf("Switching work tree from %s to %s\n", base_id_str,
1107 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree));
1108 0ebf8283 2019-07-24 stsp return NULL;
1111 0ebf8283 2019-07-24 stsp static const struct got_error *
1112 0ebf8283 2019-07-24 stsp check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
1114 0ebf8283 2019-07-24 stsp const struct got_error *err;
1115 0ebf8283 2019-07-24 stsp int in_progress;
1117 0ebf8283 2019-07-24 stsp err = got_worktree_rebase_in_progress(&in_progress, worktree);
1119 0ebf8283 2019-07-24 stsp return err;
1120 0ebf8283 2019-07-24 stsp if (in_progress)
1121 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_REBASING);
1123 0ebf8283 2019-07-24 stsp err = got_worktree_histedit_in_progress(&in_progress, worktree);
1125 0ebf8283 2019-07-24 stsp return err;
1126 0ebf8283 2019-07-24 stsp if (in_progress)
1127 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_HISTEDIT_BUSY);
1129 a1fb16d8 2019-05-24 stsp return NULL;
1132 a5edda0a 2019-07-27 stsp static const struct got_error *
1133 a5edda0a 2019-07-27 stsp get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
1134 a5edda0a 2019-07-27 stsp char *argv[], struct got_worktree *worktree)
1136 a0de39f3 2019-08-09 stsp const struct got_error *err = NULL;
1137 a5edda0a 2019-07-27 stsp char *path;
1140 a5edda0a 2019-07-27 stsp if (argc == 0) {
1141 a5edda0a 2019-07-27 stsp path = strdup("");
1142 a5edda0a 2019-07-27 stsp if (path == NULL)
1143 a5edda0a 2019-07-27 stsp return got_error_from_errno("strdup");
1144 adc19d55 2019-07-28 stsp return got_pathlist_append(paths, path, NULL);
1147 a5edda0a 2019-07-27 stsp for (i = 0; i < argc; i++) {
1148 a5edda0a 2019-07-27 stsp err = got_worktree_resolve_path(&path, worktree, argv[i]);
1151 adc19d55 2019-07-28 stsp err = got_pathlist_append(paths, path, NULL);
1152 a5edda0a 2019-07-27 stsp if (err) {
1153 a5edda0a 2019-07-27 stsp free(path);
1158 a5edda0a 2019-07-27 stsp return err;
1161 a1fb16d8 2019-05-24 stsp static const struct got_error *
1162 507dc3bb 2018-12-29 stsp cmd_update(int argc, char *argv[])
1164 507dc3bb 2018-12-29 stsp const struct got_error *error = NULL;
1165 507dc3bb 2018-12-29 stsp struct got_repository *repo = NULL;
1166 507dc3bb 2018-12-29 stsp struct got_worktree *worktree = NULL;
1167 f2ea84fa 2019-07-27 stsp char *worktree_path = NULL;
1168 507dc3bb 2018-12-29 stsp struct got_object_id *commit_id = NULL;
1169 9c4b8182 2019-01-02 stsp char *commit_id_str = NULL;
1170 024e9686 2019-05-14 stsp const char *branch_name = NULL;
1171 024e9686 2019-05-14 stsp struct got_reference *head_ref = NULL;
1172 f2ea84fa 2019-07-27 stsp struct got_pathlist_head paths;
1173 f2ea84fa 2019-07-27 stsp struct got_pathlist_entry *pe;
1174 0ebf8283 2019-07-24 stsp int ch, did_something = 0;
1176 f2ea84fa 2019-07-27 stsp TAILQ_INIT(&paths);
1178 024e9686 2019-05-14 stsp while ((ch = getopt(argc, argv, "b:c:")) != -1) {
1179 507dc3bb 2018-12-29 stsp switch (ch) {
1181 024e9686 2019-05-14 stsp branch_name = optarg;
1184 9c4b8182 2019-01-02 stsp commit_id_str = strdup(optarg);
1185 9c4b8182 2019-01-02 stsp if (commit_id_str == NULL)
1186 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
1189 2deda0b9 2019-03-07 stsp usage_update();
1190 507dc3bb 2018-12-29 stsp /* NOTREACHED */
1194 507dc3bb 2018-12-29 stsp argc -= optind;
1195 507dc3bb 2018-12-29 stsp argv += optind;
1197 507dc3bb 2018-12-29 stsp #ifndef PROFILE
1198 68ed9ba5 2019-02-10 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1199 68ed9ba5 2019-02-10 stsp "unveil", NULL) == -1)
1200 507dc3bb 2018-12-29 stsp err(1, "pledge");
1202 c4cdcb68 2019-04-03 stsp worktree_path = getcwd(NULL, 0);
1203 c4cdcb68 2019-04-03 stsp if (worktree_path == NULL) {
1204 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
1205 c4cdcb68 2019-04-03 stsp goto done;
1207 c4cdcb68 2019-04-03 stsp error = got_worktree_open(&worktree, worktree_path);
1208 7d5807f4 2019-07-11 stsp if (error)
1209 7d5807f4 2019-07-11 stsp goto done;
1211 0ebf8283 2019-07-24 stsp error = check_rebase_or_histedit_in_progress(worktree);
1212 f2ea84fa 2019-07-27 stsp if (error)
1213 f2ea84fa 2019-07-27 stsp goto done;
1215 507dc3bb 2018-12-29 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
1216 507dc3bb 2018-12-29 stsp if (error != NULL)
1217 507dc3bb 2018-12-29 stsp goto done;
1219 97430839 2019-03-11 stsp error = apply_unveil(got_repo_get_path(repo), 0,
1220 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
1221 087fb88c 2019-08-04 stsp if (error)
1222 087fb88c 2019-08-04 stsp goto done;
1224 087fb88c 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
1225 0266afb7 2019-01-04 stsp if (error)
1226 0266afb7 2019-01-04 stsp goto done;
1228 a1fb16d8 2019-05-24 stsp error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
1229 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree), 0);
1230 024e9686 2019-05-14 stsp if (error != NULL)
1231 024e9686 2019-05-14 stsp goto done;
1232 507dc3bb 2018-12-29 stsp if (commit_id_str == NULL) {
1233 507dc3bb 2018-12-29 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
1234 9c4b8182 2019-01-02 stsp if (error != NULL)
1235 9c4b8182 2019-01-02 stsp goto done;
1236 9c4b8182 2019-01-02 stsp error = got_object_id_str(&commit_id_str, commit_id);
1237 507dc3bb 2018-12-29 stsp if (error != NULL)
1238 507dc3bb 2018-12-29 stsp goto done;
1240 04f57cb3 2019-07-25 stsp error = resolve_commit_arg(&commit_id, commit_id_str, repo);
1241 04f57cb3 2019-07-25 stsp free(commit_id_str);
1242 bb787f09 2019-07-25 stsp commit_id_str = NULL;
1243 30837e32 2019-07-25 stsp if (error)
1244 a297e751 2019-07-11 stsp goto done;
1245 a297e751 2019-07-11 stsp error = got_object_id_str(&commit_id_str, commit_id);
1246 a297e751 2019-07-11 stsp if (error)
1247 507dc3bb 2018-12-29 stsp goto done;
1250 a1fb16d8 2019-05-24 stsp if (branch_name) {
1251 024e9686 2019-05-14 stsp struct got_object_id *head_commit_id;
1252 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, &paths, entry) {
1253 f2b16ada 2019-08-02 stsp if (pe->path_len == 0)
1255 f2ea84fa 2019-07-27 stsp error = got_error_msg(GOT_ERR_BAD_PATH,
1256 f2ea84fa 2019-07-27 stsp "switching between branches requires that "
1257 f2ea84fa 2019-07-27 stsp "the entire work tree gets updated");
1258 024e9686 2019-05-14 stsp goto done;
1260 024e9686 2019-05-14 stsp error = got_ref_resolve(&head_commit_id, repo, head_ref);
1261 024e9686 2019-05-14 stsp if (error)
1262 024e9686 2019-05-14 stsp goto done;
1263 024e9686 2019-05-14 stsp error = check_linear_ancestry(commit_id, head_commit_id, repo);
1264 a367ff0f 2019-05-14 stsp free(head_commit_id);
1265 024e9686 2019-05-14 stsp if (error != NULL)
1266 024e9686 2019-05-14 stsp goto done;
1267 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
1268 a367ff0f 2019-05-14 stsp if (error)
1269 a367ff0f 2019-05-14 stsp goto done;
1270 a1fb16d8 2019-05-24 stsp error = switch_head_ref(head_ref, commit_id, worktree, repo);
1271 024e9686 2019-05-14 stsp if (error)
1272 024e9686 2019-05-14 stsp goto done;
1274 024e9686 2019-05-14 stsp error = check_linear_ancestry(commit_id,
1275 024e9686 2019-05-14 stsp got_worktree_get_base_commit_id(worktree), repo);
1276 a367ff0f 2019-05-14 stsp if (error != NULL) {
1277 a367ff0f 2019-05-14 stsp if (error->code == GOT_ERR_ANCESTRY)
1278 a367ff0f 2019-05-14 stsp error = got_error(GOT_ERR_BRANCH_MOVED);
1279 024e9686 2019-05-14 stsp goto done;
1281 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
1282 a367ff0f 2019-05-14 stsp if (error)
1283 a367ff0f 2019-05-14 stsp goto done;
1286 507dc3bb 2018-12-29 stsp if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
1287 507dc3bb 2018-12-29 stsp commit_id) != 0) {
1288 507dc3bb 2018-12-29 stsp error = got_worktree_set_base_commit_id(worktree, repo,
1289 507dc3bb 2018-12-29 stsp commit_id);
1290 507dc3bb 2018-12-29 stsp if (error)
1291 507dc3bb 2018-12-29 stsp goto done;
1294 f2ea84fa 2019-07-27 stsp error = got_worktree_checkout_files(worktree, &paths, repo,
1295 6bad629b 2019-02-04 stsp update_progress, &did_something, check_cancelled, NULL);
1296 507dc3bb 2018-12-29 stsp if (error != NULL)
1297 507dc3bb 2018-12-29 stsp goto done;
1299 784955db 2019-01-12 stsp if (did_something)
1300 784955db 2019-01-12 stsp printf("Updated to commit %s\n", commit_id_str);
1302 784955db 2019-01-12 stsp printf("Already up-to-date\n");
1304 c09a553d 2018-03-12 stsp free(worktree_path);
1305 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, &paths, entry)
1306 f2ea84fa 2019-07-27 stsp free((char *)pe->path);
1307 f2ea84fa 2019-07-27 stsp got_pathlist_free(&paths);
1308 507dc3bb 2018-12-29 stsp free(commit_id);
1309 9c4b8182 2019-01-02 stsp free(commit_id_str);
1310 c09a553d 2018-03-12 stsp return error;
1313 f42b1b34 2018-03-12 stsp static const struct got_error *
1314 79109fed 2018-03-27 stsp print_patch(struct got_commit_object *commit, struct got_object_id *id,
1315 c0cc5c62 2018-10-18 stsp int diff_context, struct got_repository *repo)
1317 f42b1b34 2018-03-12 stsp const struct got_error *err = NULL;
1318 79109fed 2018-03-27 stsp struct got_tree_object *tree1 = NULL, *tree2;
1319 79f35eb3 2018-06-11 stsp struct got_object_qid *qid;
1320 0f2b3dca 2018-12-22 stsp char *id_str1 = NULL, *id_str2;
1321 aaa13589 2019-06-01 stsp struct got_diff_blob_output_unidiff_arg arg;
1323 45d799e2 2018-12-23 stsp err = got_object_open_as_tree(&tree2, repo,
1324 45d799e2 2018-12-23 stsp got_object_commit_get_tree_id(commit));
1326 79109fed 2018-03-27 stsp return err;
1328 45d799e2 2018-12-23 stsp qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1329 79f35eb3 2018-06-11 stsp if (qid != NULL) {
1330 79109fed 2018-03-27 stsp struct got_commit_object *pcommit;
1332 117e771c 2018-07-23 stsp err = got_object_open_as_commit(&pcommit, repo, qid->id);
1334 79109fed 2018-03-27 stsp return err;
1336 45d799e2 2018-12-23 stsp err = got_object_open_as_tree(&tree1, repo,
1337 45d799e2 2018-12-23 stsp got_object_commit_get_tree_id(pcommit));
1338 79109fed 2018-03-27 stsp got_object_commit_close(pcommit);
1340 0f2b3dca 2018-12-22 stsp return err;
1342 0f2b3dca 2018-12-22 stsp err = got_object_id_str(&id_str1, qid->id);
1344 79109fed 2018-03-27 stsp return err;
1347 0f2b3dca 2018-12-22 stsp err = got_object_id_str(&id_str2, id);
1349 0f2b3dca 2018-12-22 stsp goto done;
1351 56765ebb 2018-12-23 stsp printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1352 aaa13589 2019-06-01 stsp arg.diff_context = diff_context;
1353 aaa13589 2019-06-01 stsp arg.outfile = stdout;
1354 aaa13589 2019-06-01 stsp err = got_diff_tree(tree1, tree2, "", "", repo,
1355 31b4484f 2019-07-27 stsp got_diff_blob_output_unidiff, &arg, 1);
1357 79109fed 2018-03-27 stsp if (tree1)
1358 79109fed 2018-03-27 stsp got_object_tree_close(tree1);
1359 79109fed 2018-03-27 stsp got_object_tree_close(tree2);
1360 0f2b3dca 2018-12-22 stsp free(id_str1);
1361 0f2b3dca 2018-12-22 stsp free(id_str2);
1362 79109fed 2018-03-27 stsp return err;
1365 4bb494d5 2018-06-16 stsp static char *
1366 6c281f94 2018-06-11 stsp get_datestr(time_t *time, char *datebuf)
1368 09867e48 2019-08-13 stsp struct tm mytm, *tm;
1369 09867e48 2019-08-13 stsp char *p, *s;
1371 09867e48 2019-08-13 stsp tm = gmtime_r(time, &mytm);
1372 09867e48 2019-08-13 stsp if (tm == NULL)
1373 09867e48 2019-08-13 stsp return NULL;
1374 09867e48 2019-08-13 stsp s = asctime_r(tm, datebuf);
1375 09867e48 2019-08-13 stsp if (s == NULL)
1376 09867e48 2019-08-13 stsp return NULL;
1377 6c281f94 2018-06-11 stsp p = strchr(s, '\n');
1379 6c281f94 2018-06-11 stsp *p = '\0';
1383 dc424a06 2019-08-07 stsp #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
1385 79109fed 2018-03-27 stsp static const struct got_error *
1386 79109fed 2018-03-27 stsp print_commit(struct got_commit_object *commit, struct got_object_id *id,
1387 199a4027 2019-02-02 stsp struct got_repository *repo, int show_patch, int diff_context,
1388 199a4027 2019-02-02 stsp struct got_reflist_head *refs)
1390 79109fed 2018-03-27 stsp const struct got_error *err = NULL;
1391 621015ac 2018-07-23 stsp char *id_str, *datestr, *logmsg0, *logmsg, *line;
1392 6c281f94 2018-06-11 stsp char datebuf[26];
1393 45d799e2 2018-12-23 stsp time_t committer_time;
1394 45d799e2 2018-12-23 stsp const char *author, *committer;
1395 199a4027 2019-02-02 stsp char *refs_str = NULL;
1396 199a4027 2019-02-02 stsp struct got_reflist_entry *re;
1398 199a4027 2019-02-02 stsp SIMPLEQ_FOREACH(re, refs, entry) {
1400 199a4027 2019-02-02 stsp const char *name;
1401 a436ad14 2019-08-13 stsp struct got_tag_object *tag = NULL;
1404 199a4027 2019-02-02 stsp name = got_ref_get_name(re->ref);
1405 d9498b20 2019-02-05 stsp if (strcmp(name, GOT_REF_HEAD) == 0)
1407 199a4027 2019-02-02 stsp if (strncmp(name, "refs/", 5) == 0)
1408 199a4027 2019-02-02 stsp name += 5;
1409 7143d404 2019-03-12 stsp if (strncmp(name, "got/", 4) == 0)
1411 e34f9ed6 2019-02-02 stsp if (strncmp(name, "heads/", 6) == 0)
1412 e34f9ed6 2019-02-02 stsp name += 6;
1413 141c2bff 2019-02-04 stsp if (strncmp(name, "remotes/", 8) == 0)
1414 141c2bff 2019-02-04 stsp name += 8;
1415 a436ad14 2019-08-13 stsp if (strncmp(name, "tags/", 5) == 0) {
1416 a436ad14 2019-08-13 stsp err = got_object_open_as_tag(&tag, repo, re->id);
1417 5d844a1e 2019-08-13 stsp if (err) {
1418 5d844a1e 2019-08-13 stsp if (err->code != GOT_ERR_OBJ_TYPE)
1419 5d844a1e 2019-08-13 stsp return err;
1420 5d844a1e 2019-08-13 stsp /* Ref points at something other than a tag. */
1421 5d844a1e 2019-08-13 stsp err = NULL;
1422 5d844a1e 2019-08-13 stsp tag = NULL;
1425 a436ad14 2019-08-13 stsp cmp = got_object_id_cmp(tag ?
1426 a436ad14 2019-08-13 stsp got_object_tag_get_object_id(tag) : re->id, id);
1428 a436ad14 2019-08-13 stsp got_object_tag_close(tag);
1429 a436ad14 2019-08-13 stsp if (cmp != 0)
1431 199a4027 2019-02-02 stsp s = refs_str;
1432 199a4027 2019-02-02 stsp if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
1433 199a4027 2019-02-02 stsp name) == -1) {
1434 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1436 34ca4898 2019-08-13 stsp return err;
1440 832c249c 2018-06-10 stsp err = got_object_id_str(&id_str, id);
1442 8bf5b3c9 2018-03-17 stsp return err;
1444 dc424a06 2019-08-07 stsp printf(GOT_COMMIT_SEP_STR);
1445 199a4027 2019-02-02 stsp printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
1446 199a4027 2019-02-02 stsp refs_str ? refs_str : "", refs_str ? ")" : "");
1447 832c249c 2018-06-10 stsp free(id_str);
1448 d3d493d7 2019-02-21 stsp id_str = NULL;
1449 d3d493d7 2019-02-21 stsp free(refs_str);
1450 d3d493d7 2019-02-21 stsp refs_str = NULL;
1451 45d799e2 2018-12-23 stsp printf("from: %s\n", got_object_commit_get_author(commit));
1452 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
1453 45d799e2 2018-12-23 stsp datestr = get_datestr(&committer_time, datebuf);
1454 09867e48 2019-08-13 stsp if (datestr)
1455 09867e48 2019-08-13 stsp printf("date: %s UTC\n", datestr);
1456 45d799e2 2018-12-23 stsp author = got_object_commit_get_author(commit);
1457 45d799e2 2018-12-23 stsp committer = got_object_commit_get_committer(commit);
1458 45d799e2 2018-12-23 stsp if (strcmp(author, committer) != 0)
1459 45d799e2 2018-12-23 stsp printf("via: %s\n", committer);
1460 45d799e2 2018-12-23 stsp if (got_object_commit_get_nparents(commit) > 1) {
1461 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
1462 79f35eb3 2018-06-11 stsp struct got_object_qid *qid;
1463 3fe1abad 2018-06-10 stsp int n = 1;
1464 45d799e2 2018-12-23 stsp parent_ids = got_object_commit_get_parent_ids(commit);
1465 45d799e2 2018-12-23 stsp SIMPLEQ_FOREACH(qid, parent_ids, entry) {
1466 79f35eb3 2018-06-11 stsp err = got_object_id_str(&id_str, qid->id);
1468 a0603db2 2018-06-10 stsp return err;
1469 3fe1abad 2018-06-10 stsp printf("parent %d: %s\n", n++, id_str);
1470 a0603db2 2018-06-10 stsp free(id_str);
1474 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
1476 5943eee2 2019-08-13 stsp return err;
1478 621015ac 2018-07-23 stsp logmsg = logmsg0;
1480 832c249c 2018-06-10 stsp line = strsep(&logmsg, "\n");
1482 832c249c 2018-06-10 stsp printf(" %s\n", line);
1483 832c249c 2018-06-10 stsp } while (line);
1484 621015ac 2018-07-23 stsp free(logmsg0);
1486 971751ac 2018-03-27 stsp if (show_patch) {
1487 c0cc5c62 2018-10-18 stsp err = print_patch(commit, id, diff_context, repo);
1488 971751ac 2018-03-27 stsp if (err == 0)
1489 971751ac 2018-03-27 stsp printf("\n");
1492 cbe7f848 2019-02-11 stsp if (fflush(stdout) != 0 && err == NULL)
1493 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
1494 07862c20 2018-09-15 stsp return err;
1497 f42b1b34 2018-03-12 stsp static const struct got_error *
1498 15a94983 2018-12-23 stsp print_commits(struct got_object_id *root_id, struct got_repository *repo,
1499 15a94983 2018-12-23 stsp char *path, int show_patch, int diff_context, int limit,
1500 199a4027 2019-02-02 stsp int first_parent_traversal, struct got_reflist_head *refs)
1502 f42b1b34 2018-03-12 stsp const struct got_error *err;
1503 372ccdbb 2018-06-10 stsp struct got_commit_graph *graph;
1505 31cedeaf 2018-09-15 stsp err = got_commit_graph_open(&graph, root_id, path,
1506 31cedeaf 2018-09-15 stsp first_parent_traversal, repo);
1508 f42b1b34 2018-03-12 stsp return err;
1509 6fb7cd11 2019-08-22 stsp err = got_commit_graph_iter_start(graph, root_id, repo,
1510 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
1512 fcc85cad 2018-07-22 stsp goto done;
1514 372ccdbb 2018-06-10 stsp struct got_commit_object *commit;
1515 372ccdbb 2018-06-10 stsp struct got_object_id *id;
1517 84453469 2018-11-11 stsp if (sigint_received || sigpipe_received)
1520 b43fbaa0 2018-06-11 stsp err = got_commit_graph_iter_next(&id, graph);
1521 372ccdbb 2018-06-10 stsp if (err) {
1522 9ba79e04 2018-06-11 stsp if (err->code == GOT_ERR_ITER_COMPLETED) {
1523 9ba79e04 2018-06-11 stsp err = NULL;
1526 372ccdbb 2018-06-10 stsp if (err->code != GOT_ERR_ITER_NEED_MORE)
1528 6fb7cd11 2019-08-22 stsp err = got_commit_graph_fetch_commits(graph, 1, repo,
1529 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
1535 b43fbaa0 2018-06-11 stsp if (id == NULL)
1538 f8e900f3 2018-06-11 stsp err = got_object_open_as_commit(&commit, repo, id);
1541 199a4027 2019-02-02 stsp err = print_commit(commit, id, repo, show_patch, diff_context,
1543 b43fbaa0 2018-06-11 stsp got_object_commit_close(commit);
1544 372ccdbb 2018-06-10 stsp if (err || (limit && --limit == 0))
1548 372ccdbb 2018-06-10 stsp got_commit_graph_close(graph);
1549 f42b1b34 2018-03-12 stsp return err;
1552 4ed7e80c 2018-05-20 stsp __dead static void
1553 6f3d1eb0 2018-03-12 stsp usage_log(void)
1555 cc54c501 2019-07-15 stsp fprintf(stderr, "usage: %s log [-c commit] [-C number] [-f] [ -l N ] [-p] "
1556 04ca23f4 2018-07-16 stsp "[-r repository-path] [path]\n", getprogname());
1560 b1ebc001 2019-08-13 stsp static int
1561 b1ebc001 2019-08-13 stsp get_default_log_limit(void)
1563 b1ebc001 2019-08-13 stsp const char *got_default_log_limit;
1564 b1ebc001 2019-08-13 stsp long long n;
1565 b1ebc001 2019-08-13 stsp const char *errstr;
1567 b1ebc001 2019-08-13 stsp got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
1568 b1ebc001 2019-08-13 stsp if (got_default_log_limit == NULL)
1570 b1ebc001 2019-08-13 stsp n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
1571 b1ebc001 2019-08-13 stsp if (errstr != NULL)
1576 4ed7e80c 2018-05-20 stsp static const struct got_error *
1577 f42b1b34 2018-03-12 stsp cmd_log(int argc, char *argv[])
1579 f42b1b34 2018-03-12 stsp const struct got_error *error;
1580 04ca23f4 2018-07-16 stsp struct got_repository *repo = NULL;
1581 cffc0aa4 2019-02-05 stsp struct got_worktree *worktree = NULL;
1582 15a94983 2018-12-23 stsp struct got_commit_object *commit = NULL;
1583 3235492e 2018-04-01 stsp struct got_object_id *id = NULL;
1584 04ca23f4 2018-07-16 stsp char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
1585 d142fc45 2018-04-01 stsp char *start_commit = NULL;
1586 c0cc5c62 2018-10-18 stsp int diff_context = 3, ch;
1587 1fd6d7ea 2018-06-13 stsp int show_patch = 0, limit = 0, first_parent_traversal = 0;
1588 64a96a6d 2018-04-01 stsp const char *errstr;
1589 199a4027 2019-02-02 stsp struct got_reflist_head refs;
1591 1b3893a2 2019-03-18 stsp SIMPLEQ_INIT(&refs);
1593 6715a751 2018-03-16 stsp #ifndef PROFILE
1594 6098196c 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1597 f42b1b34 2018-03-12 stsp err(1, "pledge");
1600 b1ebc001 2019-08-13 stsp limit = get_default_log_limit();
1602 cc54c501 2019-07-15 stsp while ((ch = getopt(argc, argv, "b:pc:C:l:fr:")) != -1) {
1603 79109fed 2018-03-27 stsp switch (ch) {
1605 79109fed 2018-03-27 stsp show_patch = 1;
1608 d142fc45 2018-04-01 stsp start_commit = optarg;
1611 4a8520aa 2018-10-18 stsp diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
1613 c0cc5c62 2018-10-18 stsp if (errstr != NULL)
1614 c0cc5c62 2018-10-18 stsp err(1, "-C option %s", errstr);
1617 b1ebc001 2019-08-13 stsp limit = strtonum(optarg, 0, INT_MAX, &errstr);
1618 64a96a6d 2018-04-01 stsp if (errstr != NULL)
1619 64a96a6d 2018-04-01 stsp err(1, "-l option %s", errstr);
1622 cc54c501 2019-07-15 stsp first_parent_traversal = 1;
1625 04ca23f4 2018-07-16 stsp repo_path = realpath(optarg, NULL);
1626 04ca23f4 2018-07-16 stsp if (repo_path == NULL)
1627 04ca23f4 2018-07-16 stsp err(1, "-r option");
1628 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
1631 2deda0b9 2019-03-07 stsp usage_log();
1632 79109fed 2018-03-27 stsp /* NOTREACHED */
1636 79109fed 2018-03-27 stsp argc -= optind;
1637 79109fed 2018-03-27 stsp argv += optind;
1639 04ca23f4 2018-07-16 stsp cwd = getcwd(NULL, 0);
1640 04ca23f4 2018-07-16 stsp if (cwd == NULL) {
1641 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
1642 04ca23f4 2018-07-16 stsp goto done;
1645 cffc0aa4 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
1646 cffc0aa4 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
1647 cffc0aa4 2019-02-05 stsp goto done;
1648 cffc0aa4 2019-02-05 stsp error = NULL;
1650 e7301579 2019-03-18 stsp if (argc == 0) {
1651 cbd1af7a 2019-03-18 stsp path = strdup("");
1652 e7301579 2019-03-18 stsp if (path == NULL) {
1653 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
1654 cbd1af7a 2019-03-18 stsp goto done;
1656 e7301579 2019-03-18 stsp } else if (argc == 1) {
1657 e7301579 2019-03-18 stsp if (worktree) {
1658 e7301579 2019-03-18 stsp error = got_worktree_resolve_path(&path, worktree,
1660 e7301579 2019-03-18 stsp if (error)
1661 e7301579 2019-03-18 stsp goto done;
1663 e7301579 2019-03-18 stsp path = strdup(argv[0]);
1664 e7301579 2019-03-18 stsp if (path == NULL) {
1665 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
1666 e7301579 2019-03-18 stsp goto done;
1670 cbd1af7a 2019-03-18 stsp usage_log();
1672 5486daa2 2019-05-11 stsp if (repo_path == NULL) {
1673 5486daa2 2019-05-11 stsp repo_path = worktree ?
1674 5486daa2 2019-05-11 stsp strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
1676 04ca23f4 2018-07-16 stsp if (repo_path == NULL) {
1677 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
1678 cffc0aa4 2019-02-05 stsp goto done;
1681 f42b1b34 2018-03-12 stsp error = got_repo_open(&repo, repo_path);
1682 d7d4f210 2018-03-12 stsp if (error != NULL)
1683 04ca23f4 2018-07-16 stsp goto done;
1685 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), 1,
1686 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
1687 c02c541e 2019-03-29 stsp if (error)
1688 c02c541e 2019-03-29 stsp goto done;
1690 d142fc45 2018-04-01 stsp if (start_commit == NULL) {
1691 3235492e 2018-04-01 stsp struct got_reference *head_ref;
1692 1cc14b9f 2019-05-14 stsp error = got_ref_open(&head_ref, repo,
1693 1cc14b9f 2019-05-14 stsp worktree ? got_worktree_get_head_ref_name(worktree)
1694 1cc14b9f 2019-05-14 stsp : GOT_REF_HEAD, 0);
1695 3235492e 2018-04-01 stsp if (error != NULL)
1696 3235492e 2018-04-01 stsp return error;
1697 3235492e 2018-04-01 stsp error = got_ref_resolve(&id, repo, head_ref);
1698 3235492e 2018-04-01 stsp got_ref_close(head_ref);
1699 3235492e 2018-04-01 stsp if (error != NULL)
1700 3235492e 2018-04-01 stsp return error;
1701 15a94983 2018-12-23 stsp error = got_object_open_as_commit(&commit, repo, id);
1703 d1f2edc9 2018-06-13 stsp struct got_reference *ref;
1704 2f17228e 2019-05-12 stsp error = got_ref_open(&ref, repo, start_commit, 0);
1705 3235492e 2018-04-01 stsp if (error == NULL) {
1706 1caad61b 2019-02-01 stsp int obj_type;
1707 d1f2edc9 2018-06-13 stsp error = got_ref_resolve(&id, repo, ref);
1708 d1f2edc9 2018-06-13 stsp got_ref_close(ref);
1709 d1f2edc9 2018-06-13 stsp if (error != NULL)
1710 1caad61b 2019-02-01 stsp goto done;
1711 1caad61b 2019-02-01 stsp error = got_object_get_type(&obj_type, repo, id);
1712 1caad61b 2019-02-01 stsp if (error != NULL)
1713 1caad61b 2019-02-01 stsp goto done;
1714 1caad61b 2019-02-01 stsp if (obj_type == GOT_OBJ_TYPE_TAG) {
1715 1caad61b 2019-02-01 stsp struct got_tag_object *tag;
1716 1caad61b 2019-02-01 stsp error = got_object_open_as_tag(&tag, repo, id);
1717 1caad61b 2019-02-01 stsp if (error != NULL)
1718 1caad61b 2019-02-01 stsp goto done;
1719 1caad61b 2019-02-01 stsp if (got_object_tag_get_object_type(tag) !=
1720 1caad61b 2019-02-01 stsp GOT_OBJ_TYPE_COMMIT) {
1721 1caad61b 2019-02-01 stsp got_object_tag_close(tag);
1722 1caad61b 2019-02-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
1723 1caad61b 2019-02-01 stsp goto done;
1726 1caad61b 2019-02-01 stsp id = got_object_id_dup(
1727 1caad61b 2019-02-01 stsp got_object_tag_get_object_id(tag));
1728 1caad61b 2019-02-01 stsp if (id == NULL)
1729 638f9024 2019-05-13 stsp error = got_error_from_errno(
1730 230a42bd 2019-05-11 jcs "got_object_id_dup");
1731 1caad61b 2019-02-01 stsp got_object_tag_close(tag);
1732 1caad61b 2019-02-01 stsp if (error)
1733 1caad61b 2019-02-01 stsp goto done;
1734 1caad61b 2019-02-01 stsp } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
1735 1caad61b 2019-02-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
1736 1caad61b 2019-02-01 stsp goto done;
1738 15a94983 2018-12-23 stsp error = got_object_open_as_commit(&commit, repo, id);
1739 d1f2edc9 2018-06-13 stsp if (error != NULL)
1740 1caad61b 2019-02-01 stsp goto done;
1742 15a94983 2018-12-23 stsp if (commit == NULL) {
1743 e09a504c 2019-06-28 stsp error = got_repo_match_object_id_prefix(&id,
1744 dd88155e 2019-06-29 stsp start_commit, GOT_OBJ_TYPE_COMMIT, repo);
1745 d1f2edc9 2018-06-13 stsp if (error != NULL)
1746 d1f2edc9 2018-06-13 stsp return error;
1749 d7d4f210 2018-03-12 stsp if (error != NULL)
1750 04ca23f4 2018-07-16 stsp goto done;
1752 23721109 2018-10-22 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
1753 04ca23f4 2018-07-16 stsp if (error != NULL)
1754 04ca23f4 2018-07-16 stsp goto done;
1755 04ca23f4 2018-07-16 stsp if (in_repo_path) {
1756 04ca23f4 2018-07-16 stsp free(path);
1757 04ca23f4 2018-07-16 stsp path = in_repo_path;
1760 199a4027 2019-02-02 stsp error = got_ref_list(&refs, repo);
1761 199a4027 2019-02-02 stsp if (error)
1762 199a4027 2019-02-02 stsp goto done;
1764 15a94983 2018-12-23 stsp error = print_commits(id, repo, path, show_patch,
1765 199a4027 2019-02-02 stsp diff_context, limit, first_parent_traversal, &refs);
1767 04ca23f4 2018-07-16 stsp free(path);
1768 04ca23f4 2018-07-16 stsp free(repo_path);
1769 04ca23f4 2018-07-16 stsp free(cwd);
1771 cffc0aa4 2019-02-05 stsp if (worktree)
1772 cffc0aa4 2019-02-05 stsp got_worktree_close(worktree);
1773 ad242220 2018-09-08 stsp if (repo) {
1774 ad242220 2018-09-08 stsp const struct got_error *repo_error;
1775 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
1776 ad242220 2018-09-08 stsp if (error == NULL)
1777 ad242220 2018-09-08 stsp error = repo_error;
1779 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
1780 8bf5b3c9 2018-03-17 stsp return error;
1783 4ed7e80c 2018-05-20 stsp __dead static void
1784 3f8b7d6a 2018-04-01 stsp usage_diff(void)
1786 98eaaa12 2019-08-03 stsp fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] [-s] "
1787 927df6b7 2019-02-10 stsp "[object1 object2 | path]\n", getprogname());
1791 b72f483a 2019-02-05 stsp struct print_diff_arg {
1792 b72f483a 2019-02-05 stsp struct got_repository *repo;
1793 b72f483a 2019-02-05 stsp struct got_worktree *worktree;
1794 b72f483a 2019-02-05 stsp int diff_context;
1795 3fc0c068 2019-02-10 stsp const char *id_str;
1796 3fc0c068 2019-02-10 stsp int header_shown;
1797 98eaaa12 2019-08-03 stsp int diff_staged;
1800 4ed7e80c 2018-05-20 stsp static const struct got_error *
1801 88d0e355 2019-08-03 stsp print_diff(void *arg, unsigned char status, unsigned char staged_status,
1802 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
1803 537ac44b 2019-08-03 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
1805 b72f483a 2019-02-05 stsp struct print_diff_arg *a = arg;
1806 b72f483a 2019-02-05 stsp const struct got_error *err = NULL;
1807 b72f483a 2019-02-05 stsp struct got_blob_object *blob1 = NULL;
1808 b72f483a 2019-02-05 stsp FILE *f2 = NULL;
1809 4ce46740 2019-08-08 stsp char *abspath = NULL, *label1 = NULL;
1810 b72f483a 2019-02-05 stsp struct stat sb;
1812 98eaaa12 2019-08-03 stsp if (a->diff_staged) {
1813 98eaaa12 2019-08-03 stsp if (staged_status != GOT_STATUS_MODIFY &&
1814 98eaaa12 2019-08-03 stsp staged_status != GOT_STATUS_ADD &&
1815 98eaaa12 2019-08-03 stsp staged_status != GOT_STATUS_DELETE)
1816 98eaaa12 2019-08-03 stsp return NULL;
1818 98eaaa12 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
1819 98eaaa12 2019-08-03 stsp return NULL;
1820 98eaaa12 2019-08-03 stsp if (status != GOT_STATUS_MODIFY &&
1821 98eaaa12 2019-08-03 stsp status != GOT_STATUS_ADD &&
1822 98eaaa12 2019-08-03 stsp status != GOT_STATUS_DELETE &&
1823 98eaaa12 2019-08-03 stsp status != GOT_STATUS_CONFLICT)
1824 98eaaa12 2019-08-03 stsp return NULL;
1827 3fc0c068 2019-02-10 stsp if (!a->header_shown) {
1828 98eaaa12 2019-08-03 stsp printf("diff %s %s%s\n", a->id_str,
1829 98eaaa12 2019-08-03 stsp got_worktree_get_root_path(a->worktree),
1830 98eaaa12 2019-08-03 stsp a->diff_staged ? " (staged changes)" : "");
1831 3fc0c068 2019-02-10 stsp a->header_shown = 1;
1834 98eaaa12 2019-08-03 stsp if (a->diff_staged) {
1835 98eaaa12 2019-08-03 stsp const char *label1 = NULL, *label2 = NULL;
1836 98eaaa12 2019-08-03 stsp switch (staged_status) {
1837 98eaaa12 2019-08-03 stsp case GOT_STATUS_MODIFY:
1838 98eaaa12 2019-08-03 stsp label1 = path;
1839 98eaaa12 2019-08-03 stsp label2 = path;
1841 98eaaa12 2019-08-03 stsp case GOT_STATUS_ADD:
1842 98eaaa12 2019-08-03 stsp label2 = path;
1844 98eaaa12 2019-08-03 stsp case GOT_STATUS_DELETE:
1845 98eaaa12 2019-08-03 stsp label1 = path;
1848 98eaaa12 2019-08-03 stsp return got_error(GOT_ERR_FILE_STATUS);
1850 98eaaa12 2019-08-03 stsp return got_diff_objects_as_blobs(blob_id, staged_blob_id,
1851 98eaaa12 2019-08-03 stsp label1, label2, a->diff_context, a->repo, stdout);
1854 408b4ebc 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
1855 4ce46740 2019-08-08 stsp staged_status == GOT_STATUS_MODIFY) {
1856 4ce46740 2019-08-08 stsp char *id_str;
1857 408b4ebc 2019-08-03 stsp err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
1860 4ce46740 2019-08-08 stsp goto done;
1861 4ce46740 2019-08-08 stsp err = got_object_id_str(&id_str, staged_blob_id);
1863 4ce46740 2019-08-08 stsp goto done;
1864 4ce46740 2019-08-08 stsp if (asprintf(&label1, "%s (staged)", id_str) == -1) {
1865 4ce46740 2019-08-08 stsp err = got_error_from_errno("asprintf");
1866 4ce46740 2019-08-08 stsp free(id_str);
1867 4ce46740 2019-08-08 stsp goto done;
1869 4ce46740 2019-08-08 stsp free(id_str);
1870 4ce46740 2019-08-08 stsp } else if (status != GOT_STATUS_ADD) {
1871 016a88dd 2019-05-13 stsp err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
1873 4ce46740 2019-08-08 stsp goto done;
1876 7154f6ce 2019-03-27 stsp if (status != GOT_STATUS_DELETE) {
1877 2ec1f75b 2019-03-26 stsp if (asprintf(&abspath, "%s/%s",
1878 2ec1f75b 2019-03-26 stsp got_worktree_get_root_path(a->worktree), path) == -1) {
1879 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1880 2ec1f75b 2019-03-26 stsp goto done;
1883 2ec1f75b 2019-03-26 stsp f2 = fopen(abspath, "r");
1884 2ec1f75b 2019-03-26 stsp if (f2 == NULL) {
1885 638f9024 2019-05-13 stsp err = got_error_from_errno2("fopen", abspath);
1886 2ec1f75b 2019-03-26 stsp goto done;
1888 2ec1f75b 2019-03-26 stsp if (lstat(abspath, &sb) == -1) {
1889 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", abspath);
1890 2ec1f75b 2019-03-26 stsp goto done;
1893 2ec1f75b 2019-03-26 stsp sb.st_size = 0;
1895 4ce46740 2019-08-08 stsp err = got_diff_blob_file(blob1, label1, f2, sb.st_size, path,
1896 4ce46740 2019-08-08 stsp a->diff_context, stdout);
1898 b72f483a 2019-02-05 stsp if (blob1)
1899 b72f483a 2019-02-05 stsp got_object_blob_close(blob1);
1900 fb43ecf1 2019-02-11 stsp if (f2 && fclose(f2) != 0 && err == NULL)
1901 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
1902 b72f483a 2019-02-05 stsp free(abspath);
1903 927df6b7 2019-02-10 stsp return err;
1906 927df6b7 2019-02-10 stsp static const struct got_error *
1907 0adb7196 2019-08-11 stsp match_object_id(struct got_object_id **id, char **label,
1908 0adb7196 2019-08-11 stsp const char *id_str, int obj_type, struct got_repository *repo)
1910 0adb7196 2019-08-11 stsp const struct got_error *err;
1911 d24820bf 2019-08-11 stsp struct got_tag_object *tag;
1912 0adb7196 2019-08-11 stsp struct got_reference *ref = NULL;
1914 0adb7196 2019-08-11 stsp *id = NULL;
1915 0adb7196 2019-08-11 stsp *label = NULL;
1917 d24820bf 2019-08-11 stsp err = got_repo_object_match_tag(&tag, id_str, GOT_OBJ_TYPE_ANY, repo);
1918 d24820bf 2019-08-11 stsp if (err == NULL) {
1919 d24820bf 2019-08-11 stsp *id = got_object_id_dup(got_object_tag_get_object_id(tag));
1920 d24820bf 2019-08-11 stsp if (*id == NULL)
1921 d24820bf 2019-08-11 stsp err = got_error_from_errno("got_object_id_dup");
1922 d24820bf 2019-08-11 stsp if (asprintf(label, "refs/tags/%s",
1923 1b7a69d3 2019-08-11 stsp got_object_tag_get_name(tag)) == -1)
1924 d24820bf 2019-08-11 stsp err = got_error_from_errno("asprintf");
1925 d24820bf 2019-08-11 stsp got_object_tag_close(tag);
1926 d24820bf 2019-08-11 stsp return err;
1927 d24820bf 2019-08-11 stsp } else if (err->code != GOT_ERR_NO_OBJ)
1928 d24820bf 2019-08-11 stsp return err;
1930 0adb7196 2019-08-11 stsp err = got_repo_match_object_id_prefix(id, id_str, obj_type, repo);
1931 0adb7196 2019-08-11 stsp if (err) {
1932 0adb7196 2019-08-11 stsp if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
1933 0adb7196 2019-08-11 stsp return err;
1934 0adb7196 2019-08-11 stsp err = got_ref_open(&ref, repo, id_str, 0);
1935 0adb7196 2019-08-11 stsp if (err != NULL)
1936 0adb7196 2019-08-11 stsp goto done;
1937 0adb7196 2019-08-11 stsp *label = strdup(got_ref_get_name(ref));
1938 0adb7196 2019-08-11 stsp if (*label == NULL) {
1939 0adb7196 2019-08-11 stsp err = got_error_from_errno("strdup");
1940 0adb7196 2019-08-11 stsp goto done;
1942 0adb7196 2019-08-11 stsp err = got_ref_resolve(id, repo, ref);
1944 0adb7196 2019-08-11 stsp err = got_object_id_str(label, *id);
1945 0adb7196 2019-08-11 stsp if (*label == NULL) {
1946 0adb7196 2019-08-11 stsp err = got_error_from_errno("strdup");
1947 0adb7196 2019-08-11 stsp goto done;
1952 0adb7196 2019-08-11 stsp got_ref_close(ref);
1953 0adb7196 2019-08-11 stsp return err;
1957 0adb7196 2019-08-11 stsp static const struct got_error *
1958 b00d56cd 2018-04-01 stsp cmd_diff(int argc, char *argv[])
1960 b00d56cd 2018-04-01 stsp const struct got_error *error;
1961 b00d56cd 2018-04-01 stsp struct got_repository *repo = NULL;
1962 b72f483a 2019-02-05 stsp struct got_worktree *worktree = NULL;
1963 b72f483a 2019-02-05 stsp char *cwd = NULL, *repo_path = NULL;
1964 15a94983 2018-12-23 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
1965 cd628e99 2019-05-28 stsp const char *id_str1 = NULL, *id_str2 = NULL;
1966 5e70831e 2019-05-28 stsp char *label1 = NULL, *label2 = NULL;
1967 15a94983 2018-12-23 stsp int type1, type2;
1968 98eaaa12 2019-08-03 stsp int diff_context = 3, diff_staged = 0, ch;
1969 c0cc5c62 2018-10-18 stsp const char *errstr;
1970 927df6b7 2019-02-10 stsp char *path = NULL;
1972 b00d56cd 2018-04-01 stsp #ifndef PROFILE
1973 25eccc22 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1974 25eccc22 2019-01-04 stsp NULL) == -1)
1975 b00d56cd 2018-04-01 stsp err(1, "pledge");
1978 98eaaa12 2019-08-03 stsp while ((ch = getopt(argc, argv, "C:r:s")) != -1) {
1979 b00d56cd 2018-04-01 stsp switch (ch) {
1981 c0cc5c62 2018-10-18 stsp diff_context = strtonum(optarg, 1, INT_MAX, &errstr);
1982 c0cc5c62 2018-10-18 stsp if (errstr != NULL)
1983 c0cc5c62 2018-10-18 stsp err(1, "-C option %s", errstr);
1986 b72f483a 2019-02-05 stsp repo_path = realpath(optarg, NULL);
1987 b72f483a 2019-02-05 stsp if (repo_path == NULL)
1988 b72f483a 2019-02-05 stsp err(1, "-r option");
1989 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
1992 98eaaa12 2019-08-03 stsp diff_staged = 1;
1995 2deda0b9 2019-03-07 stsp usage_diff();
1996 b00d56cd 2018-04-01 stsp /* NOTREACHED */
2000 b00d56cd 2018-04-01 stsp argc -= optind;
2001 b00d56cd 2018-04-01 stsp argv += optind;
2003 b72f483a 2019-02-05 stsp cwd = getcwd(NULL, 0);
2004 b72f483a 2019-02-05 stsp if (cwd == NULL) {
2005 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2006 b72f483a 2019-02-05 stsp goto done;
2008 927df6b7 2019-02-10 stsp error = got_worktree_open(&worktree, cwd);
2009 927df6b7 2019-02-10 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
2010 927df6b7 2019-02-10 stsp goto done;
2011 927df6b7 2019-02-10 stsp if (argc <= 1) {
2012 927df6b7 2019-02-10 stsp if (worktree == NULL) {
2013 927df6b7 2019-02-10 stsp error = got_error(GOT_ERR_NOT_WORKTREE);
2014 927df6b7 2019-02-10 stsp goto done;
2016 b72f483a 2019-02-05 stsp if (repo_path)
2018 b72f483a 2019-02-05 stsp "-r option can't be used when diffing a work tree");
2019 b72f483a 2019-02-05 stsp repo_path = strdup(got_worktree_get_repo_path(worktree));
2020 927df6b7 2019-02-10 stsp if (repo_path == NULL) {
2021 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2022 927df6b7 2019-02-10 stsp goto done;
2024 927df6b7 2019-02-10 stsp if (argc == 1) {
2025 6c7ab921 2019-03-18 stsp error = got_worktree_resolve_path(&path, worktree,
2027 927df6b7 2019-02-10 stsp if (error)
2028 927df6b7 2019-02-10 stsp goto done;
2030 927df6b7 2019-02-10 stsp path = strdup("");
2031 927df6b7 2019-02-10 stsp if (path == NULL) {
2032 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2033 927df6b7 2019-02-10 stsp goto done;
2036 b72f483a 2019-02-05 stsp } else if (argc == 2) {
2037 98eaaa12 2019-08-03 stsp if (diff_staged)
2038 98eaaa12 2019-08-03 stsp errx(1, "-s option can't be used when diffing "
2039 98eaaa12 2019-08-03 stsp "objects in repository");
2040 15a94983 2018-12-23 stsp id_str1 = argv[0];
2041 15a94983 2018-12-23 stsp id_str2 = argv[1];
2042 30db809c 2019-06-05 stsp if (worktree && repo_path == NULL) {
2043 30db809c 2019-06-05 stsp repo_path =
2044 30db809c 2019-06-05 stsp strdup(got_worktree_get_repo_path(worktree));
2045 30db809c 2019-06-05 stsp if (repo_path == NULL) {
2046 30db809c 2019-06-05 stsp error = got_error_from_errno("strdup");
2047 30db809c 2019-06-05 stsp goto done;
2051 b00d56cd 2018-04-01 stsp usage_diff();
2053 b72f483a 2019-02-05 stsp if (repo_path == NULL) {
2054 b72f483a 2019-02-05 stsp repo_path = getcwd(NULL, 0);
2055 b72f483a 2019-02-05 stsp if (repo_path == NULL)
2056 638f9024 2019-05-13 stsp return got_error_from_errno("getcwd");
2059 b00d56cd 2018-04-01 stsp error = got_repo_open(&repo, repo_path);
2060 76089277 2018-04-01 stsp free(repo_path);
2061 b00d56cd 2018-04-01 stsp if (error != NULL)
2062 b00d56cd 2018-04-01 stsp goto done;
2064 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), 1,
2065 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
2066 c02c541e 2019-03-29 stsp if (error)
2067 c02c541e 2019-03-29 stsp goto done;
2069 30db809c 2019-06-05 stsp if (argc <= 1) {
2070 b72f483a 2019-02-05 stsp struct print_diff_arg arg;
2071 72ea6654 2019-07-27 stsp struct got_pathlist_head paths;
2072 b72f483a 2019-02-05 stsp char *id_str;
2074 72ea6654 2019-07-27 stsp TAILQ_INIT(&paths);
2076 b72f483a 2019-02-05 stsp error = got_object_id_str(&id_str,
2077 b72f483a 2019-02-05 stsp got_worktree_get_base_commit_id(worktree));
2078 b72f483a 2019-02-05 stsp if (error)
2079 b72f483a 2019-02-05 stsp goto done;
2080 b72f483a 2019-02-05 stsp arg.repo = repo;
2081 b72f483a 2019-02-05 stsp arg.worktree = worktree;
2082 b72f483a 2019-02-05 stsp arg.diff_context = diff_context;
2083 3fc0c068 2019-02-10 stsp arg.id_str = id_str;
2084 3fc0c068 2019-02-10 stsp arg.header_shown = 0;
2085 98eaaa12 2019-08-03 stsp arg.diff_staged = diff_staged;
2087 adc19d55 2019-07-28 stsp error = got_pathlist_append(&paths, path, NULL);
2088 72ea6654 2019-07-27 stsp if (error)
2089 72ea6654 2019-07-27 stsp goto done;
2091 72ea6654 2019-07-27 stsp error = got_worktree_status(worktree, &paths, repo, print_diff,
2092 b72f483a 2019-02-05 stsp &arg, check_cancelled, NULL);
2093 b72f483a 2019-02-05 stsp free(id_str);
2094 72ea6654 2019-07-27 stsp got_pathlist_free(&paths);
2095 b72f483a 2019-02-05 stsp goto done;
2098 0adb7196 2019-08-11 stsp error = match_object_id(&id1, &label1, id_str1, GOT_OBJ_TYPE_ANY, repo);
2099 0adb7196 2019-08-11 stsp if (error)
2100 0adb7196 2019-08-11 stsp goto done;
2102 0adb7196 2019-08-11 stsp error = match_object_id(&id2, &label2, id_str2, GOT_OBJ_TYPE_ANY, repo);
2103 0adb7196 2019-08-11 stsp if (error)
2104 0adb7196 2019-08-11 stsp goto done;
2106 15a94983 2018-12-23 stsp error = got_object_get_type(&type1, repo, id1);
2107 15a94983 2018-12-23 stsp if (error)
2108 15a94983 2018-12-23 stsp goto done;
2110 15a94983 2018-12-23 stsp error = got_object_get_type(&type2, repo, id2);
2111 15a94983 2018-12-23 stsp if (error)
2112 15a94983 2018-12-23 stsp goto done;
2114 15a94983 2018-12-23 stsp if (type1 != type2) {
2115 b00d56cd 2018-04-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
2116 b00d56cd 2018-04-01 stsp goto done;
2119 15a94983 2018-12-23 stsp switch (type1) {
2120 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_BLOB:
2121 15a94983 2018-12-23 stsp error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
2122 54156555 2018-12-24 stsp diff_context, repo, stdout);
2124 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_TREE:
2125 15a94983 2018-12-23 stsp error = got_diff_objects_as_trees(id1, id2, "", "",
2126 54156555 2018-12-24 stsp diff_context, repo, stdout);
2128 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_COMMIT:
2129 5e70831e 2019-05-28 stsp printf("diff %s %s\n", label1, label2);
2130 15a94983 2018-12-23 stsp error = got_diff_objects_as_commits(id1, id2, diff_context,
2131 c0cc5c62 2018-10-18 stsp repo, stdout);
2134 b00d56cd 2018-04-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
2138 5e70831e 2019-05-28 stsp free(label1);
2139 5e70831e 2019-05-28 stsp free(label2);
2140 15a94983 2018-12-23 stsp free(id1);
2141 15a94983 2018-12-23 stsp free(id2);
2142 927df6b7 2019-02-10 stsp free(path);
2143 b72f483a 2019-02-05 stsp if (worktree)
2144 b72f483a 2019-02-05 stsp got_worktree_close(worktree);
2145 ad242220 2018-09-08 stsp if (repo) {
2146 ad242220 2018-09-08 stsp const struct got_error *repo_error;
2147 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
2148 ad242220 2018-09-08 stsp if (error == NULL)
2149 ad242220 2018-09-08 stsp error = repo_error;
2151 b00d56cd 2018-04-01 stsp return error;
2154 404c43c4 2018-06-21 stsp __dead static void
2155 404c43c4 2018-06-21 stsp usage_blame(void)
2157 9270e621 2019-02-05 stsp fprintf(stderr,
2158 9270e621 2019-02-05 stsp "usage: %s blame [-c commit] [-r repository-path] path\n",
2159 404c43c4 2018-06-21 stsp getprogname());
2163 28315671 2019-08-14 stsp struct blame_line {
2164 28315671 2019-08-14 stsp int annotated;
2165 28315671 2019-08-14 stsp char *id_str;
2166 82f6abb8 2019-08-14 stsp char *committer;
2167 bcb49d15 2019-08-14 stsp char datebuf[9]; /* YY-MM-DD + NUL */
2170 28315671 2019-08-14 stsp struct blame_cb_args {
2171 28315671 2019-08-14 stsp struct blame_line *lines;
2172 28315671 2019-08-14 stsp int nlines;
2173 7ef28ff8 2019-08-14 stsp int nlines_prec;
2174 28315671 2019-08-14 stsp int lineno_cur;
2175 28315671 2019-08-14 stsp off_t *line_offsets;
2177 82f6abb8 2019-08-14 stsp struct got_repository *repo;
2180 404c43c4 2018-06-21 stsp static const struct got_error *
2181 28315671 2019-08-14 stsp blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2183 28315671 2019-08-14 stsp const struct got_error *err = NULL;
2184 28315671 2019-08-14 stsp struct blame_cb_args *a = arg;
2185 28315671 2019-08-14 stsp struct blame_line *bline;
2186 82f6abb8 2019-08-14 stsp char *line = NULL;
2187 28315671 2019-08-14 stsp size_t linesize = 0;
2188 82f6abb8 2019-08-14 stsp struct got_commit_object *commit = NULL;
2189 28315671 2019-08-14 stsp off_t offset;
2190 bcb49d15 2019-08-14 stsp struct tm tm;
2191 bcb49d15 2019-08-14 stsp time_t committer_time;
2193 28315671 2019-08-14 stsp if (nlines != a->nlines ||
2194 28315671 2019-08-14 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
2195 28315671 2019-08-14 stsp return got_error(GOT_ERR_RANGE);
2197 28315671 2019-08-14 stsp if (sigint_received)
2198 28315671 2019-08-14 stsp return got_error(GOT_ERR_ITER_COMPLETED);
2200 2839f8b9 2019-08-18 stsp if (lineno == -1)
2201 2839f8b9 2019-08-18 stsp return NULL; /* no change in this commit */
2203 28315671 2019-08-14 stsp /* Annotate this line. */
2204 28315671 2019-08-14 stsp bline = &a->lines[lineno - 1];
2205 28315671 2019-08-14 stsp if (bline->annotated)
2206 28315671 2019-08-14 stsp return NULL;
2207 28315671 2019-08-14 stsp err = got_object_id_str(&bline->id_str, id);
2209 28315671 2019-08-14 stsp return err;
2211 82f6abb8 2019-08-14 stsp err = got_object_open_as_commit(&commit, a->repo, id);
2213 82f6abb8 2019-08-14 stsp goto done;
2215 82f6abb8 2019-08-14 stsp bline->committer = strdup(got_object_commit_get_committer(commit));
2216 82f6abb8 2019-08-14 stsp if (bline->committer == NULL) {
2217 82f6abb8 2019-08-14 stsp err = got_error_from_errno("strdup");
2218 bcb49d15 2019-08-14 stsp goto done;
2221 bcb49d15 2019-08-14 stsp committer_time = got_object_commit_get_committer_time(commit);
2222 bcb49d15 2019-08-14 stsp if (localtime_r(&committer_time, &tm) == NULL)
2223 bcb49d15 2019-08-14 stsp return got_error_from_errno("localtime_r");
2224 bcb49d15 2019-08-14 stsp if (strftime(bline->datebuf, sizeof(bline->datebuf), "%g/%m/%d",
2225 bcb49d15 2019-08-14 stsp &tm) >= sizeof(bline->datebuf)) {
2226 bcb49d15 2019-08-14 stsp err = got_error(GOT_ERR_NO_SPACE);
2227 82f6abb8 2019-08-14 stsp goto done;
2229 28315671 2019-08-14 stsp bline->annotated = 1;
2231 28315671 2019-08-14 stsp /* Print lines annotated so far. */
2232 28315671 2019-08-14 stsp bline = &a->lines[a->lineno_cur - 1];
2233 28315671 2019-08-14 stsp if (!bline->annotated)
2234 82f6abb8 2019-08-14 stsp goto done;
2236 28315671 2019-08-14 stsp offset = a->line_offsets[a->lineno_cur - 1];
2237 82f6abb8 2019-08-14 stsp if (fseeko(a->f, offset, SEEK_SET) == -1) {
2238 82f6abb8 2019-08-14 stsp err = got_error_from_errno("fseeko");
2239 82f6abb8 2019-08-14 stsp goto done;
2242 28315671 2019-08-14 stsp while (bline->annotated) {
2243 82f6abb8 2019-08-14 stsp char *smallerthan, *at, *nl, *committer;
2244 82f6abb8 2019-08-14 stsp size_t len;
2246 28315671 2019-08-14 stsp if (getline(&line, &linesize, a->f) == (ssize_t)-1) {
2247 28315671 2019-08-14 stsp if (ferror(a->f))
2248 28315671 2019-08-14 stsp err = got_error_from_errno("getline");
2252 82f6abb8 2019-08-14 stsp committer = bline->committer;
2253 82f6abb8 2019-08-14 stsp smallerthan = strchr(committer, '<');
2254 82f6abb8 2019-08-14 stsp if (smallerthan && smallerthan[1] != '\0')
2255 82f6abb8 2019-08-14 stsp committer = smallerthan + 1;
2256 82f6abb8 2019-08-14 stsp at = strchr(committer, '@');
2258 82f6abb8 2019-08-14 stsp *at = '\0';
2259 82f6abb8 2019-08-14 stsp len = strlen(committer);
2260 82f6abb8 2019-08-14 stsp if (len >= 9)
2261 82f6abb8 2019-08-14 stsp committer[8] = '\0';
2263 28315671 2019-08-14 stsp nl = strchr(line, '\n');
2265 28315671 2019-08-14 stsp *nl = '\0';
2266 bcb49d15 2019-08-14 stsp printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
2267 bcb49d15 2019-08-14 stsp bline->id_str, bline->datebuf, committer, line);
2269 28315671 2019-08-14 stsp a->lineno_cur++;
2270 28315671 2019-08-14 stsp bline = &a->lines[a->lineno_cur - 1];
2273 82f6abb8 2019-08-14 stsp if (commit)
2274 82f6abb8 2019-08-14 stsp got_object_commit_close(commit);
2275 28315671 2019-08-14 stsp free(line);
2276 28315671 2019-08-14 stsp return err;
2279 28315671 2019-08-14 stsp static const struct got_error *
2280 404c43c4 2018-06-21 stsp cmd_blame(int argc, char *argv[])
2282 404c43c4 2018-06-21 stsp const struct got_error *error;
2283 404c43c4 2018-06-21 stsp struct got_repository *repo = NULL;
2284 0c06baac 2019-02-05 stsp struct got_worktree *worktree = NULL;
2285 66bea077 2018-08-02 stsp char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2286 28315671 2019-08-14 stsp struct got_object_id *obj_id = NULL;
2287 404c43c4 2018-06-21 stsp struct got_object_id *commit_id = NULL;
2288 28315671 2019-08-14 stsp struct got_blob_object *blob = NULL;
2289 404c43c4 2018-06-21 stsp char *commit_id_str = NULL;
2290 28315671 2019-08-14 stsp struct blame_cb_args bca;
2291 28315671 2019-08-14 stsp int ch, obj_type, i;
2292 b02560ec 2019-08-19 stsp size_t filesize;
2294 90f3c347 2019-08-19 stsp memset(&bca, 0, sizeof(bca));
2296 404c43c4 2018-06-21 stsp #ifndef PROFILE
2297 36e2fb66 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2298 36e2fb66 2019-01-04 stsp NULL) == -1)
2299 404c43c4 2018-06-21 stsp err(1, "pledge");
2302 66bea077 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2303 404c43c4 2018-06-21 stsp switch (ch) {
2305 404c43c4 2018-06-21 stsp commit_id_str = optarg;
2308 66bea077 2018-08-02 stsp repo_path = realpath(optarg, NULL);
2309 66bea077 2018-08-02 stsp if (repo_path == NULL)
2310 66bea077 2018-08-02 stsp err(1, "-r option");
2311 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
2314 2deda0b9 2019-03-07 stsp usage_blame();
2315 404c43c4 2018-06-21 stsp /* NOTREACHED */
2319 404c43c4 2018-06-21 stsp argc -= optind;
2320 404c43c4 2018-06-21 stsp argv += optind;
2322 a39318fd 2018-08-02 stsp if (argc == 1)
2323 404c43c4 2018-06-21 stsp path = argv[0];
2325 404c43c4 2018-06-21 stsp usage_blame();
2327 66bea077 2018-08-02 stsp cwd = getcwd(NULL, 0);
2328 66bea077 2018-08-02 stsp if (cwd == NULL) {
2329 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2330 66bea077 2018-08-02 stsp goto done;
2332 66bea077 2018-08-02 stsp if (repo_path == NULL) {
2333 0c06baac 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
2334 0c06baac 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
2335 66bea077 2018-08-02 stsp goto done;
2337 0c06baac 2019-02-05 stsp error = NULL;
2338 0c06baac 2019-02-05 stsp if (worktree) {
2339 0c06baac 2019-02-05 stsp repo_path =
2340 0c06baac 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
2341 0c06baac 2019-02-05 stsp if (repo_path == NULL)
2342 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2343 0c06baac 2019-02-05 stsp if (error)
2344 0c06baac 2019-02-05 stsp goto done;
2346 0c06baac 2019-02-05 stsp repo_path = strdup(cwd);
2347 0c06baac 2019-02-05 stsp if (repo_path == NULL) {
2348 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2349 0c06baac 2019-02-05 stsp goto done;
2354 c02c541e 2019-03-29 stsp error = got_repo_open(&repo, repo_path);
2355 c02c541e 2019-03-29 stsp if (error != NULL)
2356 36e2fb66 2019-01-04 stsp goto done;
2358 c530dc23 2019-07-23 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2359 c02c541e 2019-03-29 stsp if (error)
2360 404c43c4 2018-06-21 stsp goto done;
2362 0c06baac 2019-02-05 stsp if (worktree) {
2363 6efaaa2d 2019-02-05 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
2364 0c06baac 2019-02-05 stsp char *p, *worktree_subdir = cwd +
2365 0c06baac 2019-02-05 stsp strlen(got_worktree_get_root_path(worktree));
2366 6efaaa2d 2019-02-05 stsp if (asprintf(&p, "%s%s%s%s%s",
2367 6efaaa2d 2019-02-05 stsp prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
2368 6efaaa2d 2019-02-05 stsp worktree_subdir, worktree_subdir[0] ? "/" : "",
2369 6efaaa2d 2019-02-05 stsp path) == -1) {
2370 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
2371 0c06baac 2019-02-05 stsp goto done;
2373 6efaaa2d 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, p, 0);
2376 0c06baac 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
2378 0c06baac 2019-02-05 stsp if (error)
2379 66bea077 2018-08-02 stsp goto done;
2381 404c43c4 2018-06-21 stsp if (commit_id_str == NULL) {
2382 404c43c4 2018-06-21 stsp struct got_reference *head_ref;
2383 2f17228e 2019-05-12 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
2384 404c43c4 2018-06-21 stsp if (error != NULL)
2385 66bea077 2018-08-02 stsp goto done;
2386 404c43c4 2018-06-21 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
2387 404c43c4 2018-06-21 stsp got_ref_close(head_ref);
2388 404c43c4 2018-06-21 stsp if (error != NULL)
2389 66bea077 2018-08-02 stsp goto done;
2391 04f57cb3 2019-07-25 stsp error = resolve_commit_arg(&commit_id, commit_id_str, repo);
2392 30837e32 2019-07-25 stsp if (error)
2393 66bea077 2018-08-02 stsp goto done;
2396 28315671 2019-08-14 stsp error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
2397 28315671 2019-08-14 stsp if (error)
2398 28315671 2019-08-14 stsp goto done;
2399 28315671 2019-08-14 stsp if (obj_id == NULL) {
2400 28315671 2019-08-14 stsp error = got_error(GOT_ERR_NO_OBJ);
2401 28315671 2019-08-14 stsp goto done;
2404 28315671 2019-08-14 stsp error = got_object_get_type(&obj_type, repo, obj_id);
2405 28315671 2019-08-14 stsp if (error)
2406 28315671 2019-08-14 stsp goto done;
2408 28315671 2019-08-14 stsp if (obj_type != GOT_OBJ_TYPE_BLOB) {
2409 28315671 2019-08-14 stsp error = got_error(GOT_ERR_OBJ_TYPE);
2410 28315671 2019-08-14 stsp goto done;
2413 28315671 2019-08-14 stsp error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
2414 28315671 2019-08-14 stsp if (error)
2415 28315671 2019-08-14 stsp goto done;
2416 28315671 2019-08-14 stsp bca.f = got_opentemp();
2417 28315671 2019-08-14 stsp if (bca.f == NULL) {
2418 28315671 2019-08-14 stsp error = got_error_from_errno("got_opentemp");
2419 28315671 2019-08-14 stsp goto done;
2421 b02560ec 2019-08-19 stsp error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
2422 28315671 2019-08-14 stsp &bca.line_offsets, bca.f, blob);
2423 7ef28ff8 2019-08-14 stsp if (error || bca.nlines == 0)
2424 28315671 2019-08-14 stsp goto done;
2426 b02560ec 2019-08-19 stsp /* Don't include \n at EOF in the blame line count. */
2427 b02560ec 2019-08-19 stsp if (bca.line_offsets[bca.nlines - 1] == filesize)
2428 b02560ec 2019-08-19 stsp bca.nlines--;
2430 28315671 2019-08-14 stsp bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
2431 28315671 2019-08-14 stsp if (bca.lines == NULL) {
2432 28315671 2019-08-14 stsp error = got_error_from_errno("calloc");
2433 28315671 2019-08-14 stsp goto done;
2435 28315671 2019-08-14 stsp bca.lineno_cur = 1;
2436 7ef28ff8 2019-08-14 stsp bca.nlines_prec = 0;
2437 7ef28ff8 2019-08-14 stsp i = bca.nlines;
2438 7ef28ff8 2019-08-14 stsp while (i > 0) {
2440 7ef28ff8 2019-08-14 stsp bca.nlines_prec++;
2442 82f6abb8 2019-08-14 stsp bca.repo = repo;
2444 6fb7cd11 2019-08-22 stsp error = got_blame(in_repo_path, commit_id, repo, blame_cb, &bca,
2445 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
2446 28315671 2019-08-14 stsp if (error)
2447 28315671 2019-08-14 stsp goto done;
2449 66bea077 2018-08-02 stsp free(in_repo_path);
2450 66bea077 2018-08-02 stsp free(repo_path);
2451 66bea077 2018-08-02 stsp free(cwd);
2452 404c43c4 2018-06-21 stsp free(commit_id);
2453 28315671 2019-08-14 stsp free(obj_id);
2455 28315671 2019-08-14 stsp got_object_blob_close(blob);
2456 0c06baac 2019-02-05 stsp if (worktree)
2457 0c06baac 2019-02-05 stsp got_worktree_close(worktree);
2458 ad242220 2018-09-08 stsp if (repo) {
2459 ad242220 2018-09-08 stsp const struct got_error *repo_error;
2460 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
2461 ad242220 2018-09-08 stsp if (error == NULL)
2462 ad242220 2018-09-08 stsp error = repo_error;
2464 28315671 2019-08-14 stsp for (i = 0; i < bca.nlines; i++) {
2465 28315671 2019-08-14 stsp struct blame_line *bline = &bca.lines[i];
2466 28315671 2019-08-14 stsp free(bline->id_str);
2467 82f6abb8 2019-08-14 stsp free(bline->committer);
2469 28315671 2019-08-14 stsp free(bca.lines);
2470 28315671 2019-08-14 stsp free(bca.line_offsets);
2471 28315671 2019-08-14 stsp if (bca.f && fclose(bca.f) == EOF && error == NULL)
2472 28315671 2019-08-14 stsp error = got_error_from_errno("fclose");
2473 404c43c4 2018-06-21 stsp return error;
2476 5de5890b 2018-10-18 stsp __dead static void
2477 5de5890b 2018-10-18 stsp usage_tree(void)
2479 c1669e2e 2019-01-09 stsp fprintf(stderr,
2480 c1669e2e 2019-01-09 stsp "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
2481 5de5890b 2018-10-18 stsp getprogname());
2485 c1669e2e 2019-01-09 stsp static void
2486 c1669e2e 2019-01-09 stsp print_entry(struct got_tree_entry *te, const char *id, const char *path,
2487 c1669e2e 2019-01-09 stsp const char *root_path)
2489 c1669e2e 2019-01-09 stsp int is_root_path = (strcmp(path, root_path) == 0);
2490 848d6979 2019-08-12 stsp const char *modestr = "";
2492 c1669e2e 2019-01-09 stsp path += strlen(root_path);
2493 c1669e2e 2019-01-09 stsp while (path[0] == '/')
2496 848d6979 2019-08-12 stsp if (S_ISLNK(te->mode))
2497 848d6979 2019-08-12 stsp modestr = "@";
2498 848d6979 2019-08-12 stsp else if (S_ISDIR(te->mode))
2499 848d6979 2019-08-12 stsp modestr = "/";
2500 848d6979 2019-08-12 stsp else if (te->mode & S_IXUSR)
2501 848d6979 2019-08-12 stsp modestr = "*";
2503 c1669e2e 2019-01-09 stsp printf("%s%s%s%s%s\n", id ? id : "", path,
2504 848d6979 2019-08-12 stsp is_root_path ? "" : "/", te->name, modestr);
2507 5de5890b 2018-10-18 stsp static const struct got_error *
2508 5de5890b 2018-10-18 stsp print_tree(const char *path, struct got_object_id *commit_id,
2509 c1669e2e 2019-01-09 stsp int show_ids, int recurse, const char *root_path,
2510 c1669e2e 2019-01-09 stsp struct got_repository *repo)
2512 5de5890b 2018-10-18 stsp const struct got_error *err = NULL;
2513 5de5890b 2018-10-18 stsp struct got_object_id *tree_id = NULL;
2514 5de5890b 2018-10-18 stsp struct got_tree_object *tree = NULL;
2515 5de5890b 2018-10-18 stsp const struct got_tree_entries *entries;
2516 5de5890b 2018-10-18 stsp struct got_tree_entry *te;
2518 5de5890b 2018-10-18 stsp err = got_object_id_by_path(&tree_id, repo, commit_id, path);
2520 5de5890b 2018-10-18 stsp goto done;
2522 5de5890b 2018-10-18 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
2524 5de5890b 2018-10-18 stsp goto done;
2525 5de5890b 2018-10-18 stsp entries = got_object_tree_get_entries(tree);
2526 5de5890b 2018-10-18 stsp te = SIMPLEQ_FIRST(&entries->head);
2527 5de5890b 2018-10-18 stsp while (te) {
2528 5de5890b 2018-10-18 stsp char *id = NULL;
2530 84453469 2018-11-11 stsp if (sigint_received || sigpipe_received)
2533 5de5890b 2018-10-18 stsp if (show_ids) {
2534 5de5890b 2018-10-18 stsp char *id_str;
2535 5de5890b 2018-10-18 stsp err = got_object_id_str(&id_str, te->id);
2537 5de5890b 2018-10-18 stsp goto done;
2538 5de5890b 2018-10-18 stsp if (asprintf(&id, "%s ", id_str) == -1) {
2539 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2540 5de5890b 2018-10-18 stsp free(id_str);
2541 5de5890b 2018-10-18 stsp goto done;
2543 5de5890b 2018-10-18 stsp free(id_str);
2545 c1669e2e 2019-01-09 stsp print_entry(te, id, path, root_path);
2548 c1669e2e 2019-01-09 stsp if (recurse && S_ISDIR(te->mode)) {
2549 c1669e2e 2019-01-09 stsp char *child_path;
2550 c1669e2e 2019-01-09 stsp if (asprintf(&child_path, "%s%s%s", path,
2551 c1669e2e 2019-01-09 stsp path[0] == '/' && path[1] == '\0' ? "" : "/",
2552 c1669e2e 2019-01-09 stsp te->name) == -1) {
2553 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2554 c1669e2e 2019-01-09 stsp goto done;
2556 c1669e2e 2019-01-09 stsp err = print_tree(child_path, commit_id, show_ids, 1,
2557 c1669e2e 2019-01-09 stsp root_path, repo);
2558 c1669e2e 2019-01-09 stsp free(child_path);
2560 c1669e2e 2019-01-09 stsp goto done;
2563 c1669e2e 2019-01-09 stsp te = SIMPLEQ_NEXT(te, entry);
2567 5de5890b 2018-10-18 stsp got_object_tree_close(tree);
2568 5de5890b 2018-10-18 stsp free(tree_id);
2569 5de5890b 2018-10-18 stsp return err;
2572 5de5890b 2018-10-18 stsp static const struct got_error *
2573 5de5890b 2018-10-18 stsp cmd_tree(int argc, char *argv[])
2575 5de5890b 2018-10-18 stsp const struct got_error *error;
2576 5de5890b 2018-10-18 stsp struct got_repository *repo = NULL;
2577 7a2c19d6 2019-02-05 stsp struct got_worktree *worktree = NULL;
2578 7a2c19d6 2019-02-05 stsp const char *path;
2579 7a2c19d6 2019-02-05 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2580 5de5890b 2018-10-18 stsp struct got_object_id *commit_id = NULL;
2581 5de5890b 2018-10-18 stsp char *commit_id_str = NULL;
2582 c1669e2e 2019-01-09 stsp int show_ids = 0, recurse = 0;
2585 5de5890b 2018-10-18 stsp #ifndef PROFILE
2586 0f8d269b 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2587 0f8d269b 2019-01-04 stsp NULL) == -1)
2588 5de5890b 2018-10-18 stsp err(1, "pledge");
2591 c1669e2e 2019-01-09 stsp while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
2592 5de5890b 2018-10-18 stsp switch (ch) {
2594 5de5890b 2018-10-18 stsp commit_id_str = optarg;
2597 5de5890b 2018-10-18 stsp repo_path = realpath(optarg, NULL);
2598 5de5890b 2018-10-18 stsp if (repo_path == NULL)
2599 5de5890b 2018-10-18 stsp err(1, "-r option");
2600 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
2603 5de5890b 2018-10-18 stsp show_ids = 1;
2606 c1669e2e 2019-01-09 stsp recurse = 1;
2609 2deda0b9 2019-03-07 stsp usage_tree();
2610 5de5890b 2018-10-18 stsp /* NOTREACHED */
2614 5de5890b 2018-10-18 stsp argc -= optind;
2615 5de5890b 2018-10-18 stsp argv += optind;
2617 5de5890b 2018-10-18 stsp if (argc == 1)
2618 5de5890b 2018-10-18 stsp path = argv[0];
2619 5de5890b 2018-10-18 stsp else if (argc > 1)
2620 5de5890b 2018-10-18 stsp usage_tree();
2622 7a2c19d6 2019-02-05 stsp path = NULL;
2624 9bf7a39b 2019-02-05 stsp cwd = getcwd(NULL, 0);
2625 9bf7a39b 2019-02-05 stsp if (cwd == NULL) {
2626 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2627 9bf7a39b 2019-02-05 stsp goto done;
2629 5de5890b 2018-10-18 stsp if (repo_path == NULL) {
2630 7a2c19d6 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
2631 8994de28 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
2632 7a2c19d6 2019-02-05 stsp goto done;
2634 7a2c19d6 2019-02-05 stsp error = NULL;
2635 7a2c19d6 2019-02-05 stsp if (worktree) {
2636 7a2c19d6 2019-02-05 stsp repo_path =
2637 7a2c19d6 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
2638 7a2c19d6 2019-02-05 stsp if (repo_path == NULL)
2639 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2640 7a2c19d6 2019-02-05 stsp if (error)
2641 7a2c19d6 2019-02-05 stsp goto done;
2643 7a2c19d6 2019-02-05 stsp repo_path = strdup(cwd);
2644 7a2c19d6 2019-02-05 stsp if (repo_path == NULL) {
2645 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2646 7a2c19d6 2019-02-05 stsp goto done;
2651 5de5890b 2018-10-18 stsp error = got_repo_open(&repo, repo_path);
2652 5de5890b 2018-10-18 stsp if (error != NULL)
2653 c02c541e 2019-03-29 stsp goto done;
2655 c530dc23 2019-07-23 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2656 c02c541e 2019-03-29 stsp if (error)
2657 5de5890b 2018-10-18 stsp goto done;
2659 9bf7a39b 2019-02-05 stsp if (path == NULL) {
2660 9bf7a39b 2019-02-05 stsp if (worktree) {
2661 9bf7a39b 2019-02-05 stsp char *p, *worktree_subdir = cwd +
2662 9bf7a39b 2019-02-05 stsp strlen(got_worktree_get_root_path(worktree));
2663 9bf7a39b 2019-02-05 stsp if (asprintf(&p, "%s/%s",
2664 9bf7a39b 2019-02-05 stsp got_worktree_get_path_prefix(worktree),
2665 9bf7a39b 2019-02-05 stsp worktree_subdir) == -1) {
2666 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
2667 9bf7a39b 2019-02-05 stsp goto done;
2669 9bf7a39b 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, p, 1);
2671 9bf7a39b 2019-02-05 stsp if (error)
2672 9bf7a39b 2019-02-05 stsp goto done;
2674 9bf7a39b 2019-02-05 stsp path = "/";
2676 9bf7a39b 2019-02-05 stsp if (in_repo_path == NULL) {
2677 9bf7a39b 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
2678 9bf7a39b 2019-02-05 stsp if (error != NULL)
2679 9bf7a39b 2019-02-05 stsp goto done;
2682 5de5890b 2018-10-18 stsp if (commit_id_str == NULL) {
2683 5de5890b 2018-10-18 stsp struct got_reference *head_ref;
2684 2f17228e 2019-05-12 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
2685 5de5890b 2018-10-18 stsp if (error != NULL)
2686 5de5890b 2018-10-18 stsp goto done;
2687 5de5890b 2018-10-18 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
2688 5de5890b 2018-10-18 stsp got_ref_close(head_ref);
2689 5de5890b 2018-10-18 stsp if (error != NULL)
2690 5de5890b 2018-10-18 stsp goto done;
2692 04f57cb3 2019-07-25 stsp error = resolve_commit_arg(&commit_id, commit_id_str, repo);
2693 30837e32 2019-07-25 stsp if (error)
2694 5de5890b 2018-10-18 stsp goto done;
2697 c1669e2e 2019-01-09 stsp error = print_tree(in_repo_path, commit_id, show_ids, recurse,
2698 c1669e2e 2019-01-09 stsp in_repo_path, repo);
2700 5de5890b 2018-10-18 stsp free(in_repo_path);
2701 5de5890b 2018-10-18 stsp free(repo_path);
2702 5de5890b 2018-10-18 stsp free(cwd);
2703 5de5890b 2018-10-18 stsp free(commit_id);
2704 7a2c19d6 2019-02-05 stsp if (worktree)
2705 7a2c19d6 2019-02-05 stsp got_worktree_close(worktree);
2706 5de5890b 2018-10-18 stsp if (repo) {
2707 5de5890b 2018-10-18 stsp const struct got_error *repo_error;
2708 5de5890b 2018-10-18 stsp repo_error = got_repo_close(repo);
2709 5de5890b 2018-10-18 stsp if (error == NULL)
2710 5de5890b 2018-10-18 stsp error = repo_error;
2712 5de5890b 2018-10-18 stsp return error;
2715 6bad629b 2019-02-04 stsp __dead static void
2716 6bad629b 2019-02-04 stsp usage_status(void)
2718 72ea6654 2019-07-27 stsp fprintf(stderr, "usage: %s status [path ...]\n", getprogname());
2722 b72f483a 2019-02-05 stsp static const struct got_error *
2723 88d0e355 2019-08-03 stsp print_status(void *arg, unsigned char status, unsigned char staged_status,
2724 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
2725 537ac44b 2019-08-03 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
2727 244725f2 2019-08-03 stsp if (status == staged_status && (status == GOT_STATUS_DELETE))
2728 c363b2c1 2019-08-03 stsp status = GOT_STATUS_NO_CHANGE;
2729 88d0e355 2019-08-03 stsp printf("%c%c %s\n", status, staged_status, path);
2730 b72f483a 2019-02-05 stsp return NULL;
2733 6bad629b 2019-02-04 stsp static const struct got_error *
2734 6bad629b 2019-02-04 stsp cmd_status(int argc, char *argv[])
2736 6bad629b 2019-02-04 stsp const struct got_error *error = NULL;
2737 6bad629b 2019-02-04 stsp struct got_repository *repo = NULL;
2738 6bad629b 2019-02-04 stsp struct got_worktree *worktree = NULL;
2739 f86a1bf5 2019-07-27 stsp char *cwd = NULL;
2740 72ea6654 2019-07-27 stsp struct got_pathlist_head paths;
2741 a5edda0a 2019-07-27 stsp struct got_pathlist_entry *pe;
2744 72ea6654 2019-07-27 stsp TAILQ_INIT(&paths);
2746 6bad629b 2019-02-04 stsp while ((ch = getopt(argc, argv, "")) != -1) {
2747 6bad629b 2019-02-04 stsp switch (ch) {
2749 2deda0b9 2019-03-07 stsp usage_status();
2750 6bad629b 2019-02-04 stsp /* NOTREACHED */
2754 6bad629b 2019-02-04 stsp argc -= optind;
2755 6bad629b 2019-02-04 stsp argv += optind;
2757 6bad629b 2019-02-04 stsp #ifndef PROFILE
2758 6bad629b 2019-02-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2759 6bad629b 2019-02-04 stsp NULL) == -1)
2760 6bad629b 2019-02-04 stsp err(1, "pledge");
2762 927df6b7 2019-02-10 stsp cwd = getcwd(NULL, 0);
2763 927df6b7 2019-02-10 stsp if (cwd == NULL) {
2764 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2765 927df6b7 2019-02-10 stsp goto done;
2768 927df6b7 2019-02-10 stsp error = got_worktree_open(&worktree, cwd);
2769 927df6b7 2019-02-10 stsp if (error != NULL)
2770 a5edda0a 2019-07-27 stsp goto done;
2772 6bad629b 2019-02-04 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2773 6bad629b 2019-02-04 stsp if (error != NULL)
2774 6bad629b 2019-02-04 stsp goto done;
2776 d0eebce4 2019-03-11 stsp error = apply_unveil(got_repo_get_path(repo), 1,
2777 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
2778 087fb88c 2019-08-04 stsp if (error)
2779 087fb88c 2019-08-04 stsp goto done;
2781 087fb88c 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
2782 6bad629b 2019-02-04 stsp if (error)
2783 6bad629b 2019-02-04 stsp goto done;
2785 72ea6654 2019-07-27 stsp error = got_worktree_status(worktree, &paths, repo, print_status, NULL,
2786 6bad629b 2019-02-04 stsp check_cancelled, NULL);
2788 a5edda0a 2019-07-27 stsp TAILQ_FOREACH(pe, &paths, entry)
2789 a5edda0a 2019-07-27 stsp free((char *)pe->path);
2790 72ea6654 2019-07-27 stsp got_pathlist_free(&paths);
2791 927df6b7 2019-02-10 stsp free(cwd);
2792 d0eebce4 2019-03-11 stsp return error;
2795 d0eebce4 2019-03-11 stsp __dead static void
2796 d0eebce4 2019-03-11 stsp usage_ref(void)
2798 d0eebce4 2019-03-11 stsp fprintf(stderr,
2799 d1c1ae5f 2019-08-12 stsp "usage: %s ref [-r repository] -l | -d name | [-s] name target\n",
2800 d0eebce4 2019-03-11 stsp getprogname());
2804 d0eebce4 2019-03-11 stsp static const struct got_error *
2805 d0eebce4 2019-03-11 stsp list_refs(struct got_repository *repo)
2807 d0eebce4 2019-03-11 stsp static const struct got_error *err = NULL;
2808 d0eebce4 2019-03-11 stsp struct got_reflist_head refs;
2809 d0eebce4 2019-03-11 stsp struct got_reflist_entry *re;
2811 d0eebce4 2019-03-11 stsp SIMPLEQ_INIT(&refs);
2812 d0eebce4 2019-03-11 stsp err = got_ref_list(&refs, repo);
2814 d0eebce4 2019-03-11 stsp return err;
2816 d0eebce4 2019-03-11 stsp SIMPLEQ_FOREACH(re, &refs, entry) {
2817 d0eebce4 2019-03-11 stsp char *refstr;
2818 d0eebce4 2019-03-11 stsp refstr = got_ref_to_str(re->ref);
2819 d0eebce4 2019-03-11 stsp if (refstr == NULL)
2820 638f9024 2019-05-13 stsp return got_error_from_errno("got_ref_to_str");
2821 d0eebce4 2019-03-11 stsp printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
2822 d0eebce4 2019-03-11 stsp free(refstr);
2825 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
2826 d0eebce4 2019-03-11 stsp return NULL;
2829 d0eebce4 2019-03-11 stsp static const struct got_error *
2830 d0eebce4 2019-03-11 stsp delete_ref(struct got_repository *repo, const char *refname)
2832 d0eebce4 2019-03-11 stsp const struct got_error *err = NULL;
2833 d0eebce4 2019-03-11 stsp struct got_reference *ref;
2835 2f17228e 2019-05-12 stsp err = got_ref_open(&ref, repo, refname, 0);
2837 d0eebce4 2019-03-11 stsp return err;
2839 d0eebce4 2019-03-11 stsp err = got_ref_delete(ref, repo);
2840 d0eebce4 2019-03-11 stsp got_ref_close(ref);
2841 d0eebce4 2019-03-11 stsp return err;
2844 d0eebce4 2019-03-11 stsp static const struct got_error *
2845 d83d9d5c 2019-05-13 stsp add_ref(struct got_repository *repo, const char *refname, const char *target)
2847 d0eebce4 2019-03-11 stsp const struct got_error *err = NULL;
2848 d0eebce4 2019-03-11 stsp struct got_object_id *id;
2849 d0eebce4 2019-03-11 stsp struct got_reference *ref = NULL;
2852 d1644381 2019-07-14 stsp * Don't let the user create a reference named '-'.
2853 d1644381 2019-07-14 stsp * While technically a valid reference name, this case is usually
2854 d1644381 2019-07-14 stsp * an unintended typo.
2856 d1644381 2019-07-14 stsp if (refname[0] == '-' && refname[1] == '\0')
2857 d1644381 2019-07-14 stsp return got_error(GOT_ERR_BAD_REF_NAME);
2859 dd88155e 2019-06-29 stsp err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
2861 d83d9d5c 2019-05-13 stsp if (err) {
2862 d83d9d5c 2019-05-13 stsp struct got_reference *target_ref;
2864 d83d9d5c 2019-05-13 stsp if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
2865 d83d9d5c 2019-05-13 stsp return err;
2866 d83d9d5c 2019-05-13 stsp err = got_ref_open(&target_ref, repo, target, 0);
2868 d83d9d5c 2019-05-13 stsp return err;
2869 d83d9d5c 2019-05-13 stsp err = got_ref_resolve(&id, repo, target_ref);
2870 d83d9d5c 2019-05-13 stsp got_ref_close(target_ref);
2872 d83d9d5c 2019-05-13 stsp return err;
2875 d0eebce4 2019-03-11 stsp err = got_ref_alloc(&ref, refname, id);
2877 d0eebce4 2019-03-11 stsp goto done;
2879 d0eebce4 2019-03-11 stsp err = got_ref_write(ref, repo);
2882 d0eebce4 2019-03-11 stsp got_ref_close(ref);
2884 d1c1ae5f 2019-08-12 stsp return err;
2887 d1c1ae5f 2019-08-12 stsp static const struct got_error *
2888 d1c1ae5f 2019-08-12 stsp add_symref(struct got_repository *repo, const char *refname, const char *target)
2890 d1c1ae5f 2019-08-12 stsp const struct got_error *err = NULL;
2891 d1c1ae5f 2019-08-12 stsp struct got_reference *ref = NULL;
2892 d1c1ae5f 2019-08-12 stsp struct got_reference *target_ref = NULL;
2895 d1c1ae5f 2019-08-12 stsp * Don't let the user create a reference named '-'.
2896 d1c1ae5f 2019-08-12 stsp * While technically a valid reference name, this case is usually
2897 d1c1ae5f 2019-08-12 stsp * an unintended typo.
2899 d1c1ae5f 2019-08-12 stsp if (refname[0] == '-' && refname[1] == '\0')
2900 d1c1ae5f 2019-08-12 stsp return got_error(GOT_ERR_BAD_REF_NAME);
2902 d1c1ae5f 2019-08-12 stsp err = got_ref_open(&target_ref, repo, target, 0);
2904 d1c1ae5f 2019-08-12 stsp return err;
2906 d1c1ae5f 2019-08-12 stsp err = got_ref_alloc_symref(&ref, refname, target_ref);
2908 d1c1ae5f 2019-08-12 stsp goto done;
2910 d1c1ae5f 2019-08-12 stsp err = got_ref_write(ref, repo);
2912 d1c1ae5f 2019-08-12 stsp if (target_ref)
2913 d1c1ae5f 2019-08-12 stsp got_ref_close(target_ref);
2915 d1c1ae5f 2019-08-12 stsp got_ref_close(ref);
2916 d0eebce4 2019-03-11 stsp return err;
2919 d0eebce4 2019-03-11 stsp static const struct got_error *
2920 d0eebce4 2019-03-11 stsp cmd_ref(int argc, char *argv[])
2922 d0eebce4 2019-03-11 stsp const struct got_error *error = NULL;
2923 d0eebce4 2019-03-11 stsp struct got_repository *repo = NULL;
2924 d0eebce4 2019-03-11 stsp struct got_worktree *worktree = NULL;
2925 d0eebce4 2019-03-11 stsp char *cwd = NULL, *repo_path = NULL;
2926 d1c1ae5f 2019-08-12 stsp int ch, do_list = 0, create_symref = 0;
2927 d0eebce4 2019-03-11 stsp const char *delref = NULL;
2929 d0eebce4 2019-03-11 stsp /* TODO: Add -s option for adding symbolic references. */
2930 d1c1ae5f 2019-08-12 stsp while ((ch = getopt(argc, argv, "d:r:ls")) != -1) {
2931 d0eebce4 2019-03-11 stsp switch (ch) {
2933 d0eebce4 2019-03-11 stsp delref = optarg;
2936 d0eebce4 2019-03-11 stsp repo_path = realpath(optarg, NULL);
2937 d0eebce4 2019-03-11 stsp if (repo_path == NULL)
2938 d0eebce4 2019-03-11 stsp err(1, "-r option");
2939 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
2942 d0eebce4 2019-03-11 stsp do_list = 1;
2945 d1c1ae5f 2019-08-12 stsp create_symref = 1;
2948 d0eebce4 2019-03-11 stsp usage_ref();
2949 d0eebce4 2019-03-11 stsp /* NOTREACHED */
2953 d0eebce4 2019-03-11 stsp if (do_list && delref)
2954 d0eebce4 2019-03-11 stsp errx(1, "-l and -d options are mutually exclusive\n");
2956 d0eebce4 2019-03-11 stsp argc -= optind;
2957 d0eebce4 2019-03-11 stsp argv += optind;
2959 d0eebce4 2019-03-11 stsp if (do_list || delref) {
2960 d1c1ae5f 2019-08-12 stsp if (create_symref)
2961 d1c1ae5f 2019-08-12 stsp errx(1, "-s option cannot be used together with the "
2962 d1c1ae5f 2019-08-12 stsp "-l or -d options");
2963 d0eebce4 2019-03-11 stsp if (argc > 0)
2964 d0eebce4 2019-03-11 stsp usage_ref();
2965 d0eebce4 2019-03-11 stsp } else if (argc != 2)
2966 d0eebce4 2019-03-11 stsp usage_ref();
2968 d0eebce4 2019-03-11 stsp #ifndef PROFILE
2969 e0b57350 2019-03-12 stsp if (do_list) {
2970 e0b57350 2019-03-12 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
2971 e0b57350 2019-03-12 stsp NULL) == -1)
2972 e0b57350 2019-03-12 stsp err(1, "pledge");
2974 e0b57350 2019-03-12 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2975 e0b57350 2019-03-12 stsp "sendfd unveil", NULL) == -1)
2976 e0b57350 2019-03-12 stsp err(1, "pledge");
2979 d0eebce4 2019-03-11 stsp cwd = getcwd(NULL, 0);
2980 d0eebce4 2019-03-11 stsp if (cwd == NULL) {
2981 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2982 d0eebce4 2019-03-11 stsp goto done;
2985 d0eebce4 2019-03-11 stsp if (repo_path == NULL) {
2986 d0eebce4 2019-03-11 stsp error = got_worktree_open(&worktree, cwd);
2987 d0eebce4 2019-03-11 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
2988 d0eebce4 2019-03-11 stsp goto done;
2990 d0eebce4 2019-03-11 stsp error = NULL;
2991 d0eebce4 2019-03-11 stsp if (worktree) {
2992 d0eebce4 2019-03-11 stsp repo_path =
2993 d0eebce4 2019-03-11 stsp strdup(got_worktree_get_repo_path(worktree));
2994 d0eebce4 2019-03-11 stsp if (repo_path == NULL)
2995 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2996 d0eebce4 2019-03-11 stsp if (error)
2997 d0eebce4 2019-03-11 stsp goto done;
2999 d0eebce4 2019-03-11 stsp repo_path = strdup(cwd);
3000 d0eebce4 2019-03-11 stsp if (repo_path == NULL) {
3001 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
3002 d0eebce4 2019-03-11 stsp goto done;
3007 d0eebce4 2019-03-11 stsp error = got_repo_open(&repo, repo_path);
3008 d0eebce4 2019-03-11 stsp if (error != NULL)
3009 d0eebce4 2019-03-11 stsp goto done;
3011 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), do_list,
3012 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
3013 c02c541e 2019-03-29 stsp if (error)
3014 c02c541e 2019-03-29 stsp goto done;
3016 d0eebce4 2019-03-11 stsp if (do_list)
3017 d0eebce4 2019-03-11 stsp error = list_refs(repo);
3018 d0eebce4 2019-03-11 stsp else if (delref)
3019 d0eebce4 2019-03-11 stsp error = delete_ref(repo, delref);
3020 d1c1ae5f 2019-08-12 stsp else if (create_symref)
3021 d1c1ae5f 2019-08-12 stsp error = add_symref(repo, argv[0], argv[1]);
3023 d0eebce4 2019-03-11 stsp error = add_ref(repo, argv[0], argv[1]);
3026 4e759de4 2019-06-26 stsp got_repo_close(repo);
3027 4e759de4 2019-06-26 stsp if (worktree)
3028 4e759de4 2019-06-26 stsp got_worktree_close(worktree);
3029 4e759de4 2019-06-26 stsp free(cwd);
3030 4e759de4 2019-06-26 stsp free(repo_path);
3031 4e759de4 2019-06-26 stsp return error;
3034 4e759de4 2019-06-26 stsp __dead static void
3035 4e759de4 2019-06-26 stsp usage_branch(void)
3037 4e759de4 2019-06-26 stsp fprintf(stderr,
3038 4e759de4 2019-06-26 stsp "usage: %s branch [-r repository] -l | -d name | "
3039 4e759de4 2019-06-26 stsp "name [base-branch]\n", getprogname());
3043 4e759de4 2019-06-26 stsp static const struct got_error *
3044 ba882ee3 2019-07-11 stsp list_branches(struct got_repository *repo, struct got_worktree *worktree)
3046 4e759de4 2019-06-26 stsp static const struct got_error *err = NULL;
3047 4e759de4 2019-06-26 stsp struct got_reflist_head refs;
3048 4e759de4 2019-06-26 stsp struct got_reflist_entry *re;
3050 4e759de4 2019-06-26 stsp SIMPLEQ_INIT(&refs);
3052 4e759de4 2019-06-26 stsp err = got_ref_list(&refs, repo);
3054 4e759de4 2019-06-26 stsp return err;
3056 4e759de4 2019-06-26 stsp SIMPLEQ_FOREACH(re, &refs, entry) {
3057 ba882ee3 2019-07-11 stsp const char *refname, *marker = " ";
3058 4e759de4 2019-06-26 stsp char *refstr;
3059 4e759de4 2019-06-26 stsp refname = got_ref_get_name(re->ref);
3060 4e759de4 2019-06-26 stsp if (strncmp(refname, "refs/heads/", 11) != 0)
3062 ba882ee3 2019-07-11 stsp if (worktree && strcmp(refname,
3063 ba882ee3 2019-07-11 stsp got_worktree_get_head_ref_name(worktree)) == 0) {
3064 ba882ee3 2019-07-11 stsp struct got_object_id *id = NULL;
3065 ba882ee3 2019-07-11 stsp err = got_ref_resolve(&id, repo, re->ref);
3067 ba882ee3 2019-07-11 stsp return err;
3068 ba882ee3 2019-07-11 stsp if (got_object_id_cmp(id,
3069 ba882ee3 2019-07-11 stsp got_worktree_get_base_commit_id(worktree)) == 0)
3070 ba882ee3 2019-07-11 stsp marker = "* ";
3072 ba882ee3 2019-07-11 stsp marker = "~ ";
3075 4e759de4 2019-06-26 stsp refname += 11;
3076 4e759de4 2019-06-26 stsp refstr = got_ref_to_str(re->ref);
3077 4e759de4 2019-06-26 stsp if (refstr == NULL)
3078 4e759de4 2019-06-26 stsp return got_error_from_errno("got_ref_to_str");
3079 ba882ee3 2019-07-11 stsp printf("%s%s: %s\n", marker, refname, refstr);
3080 4e759de4 2019-06-26 stsp free(refstr);
3083 4e759de4 2019-06-26 stsp got_ref_list_free(&refs);
3084 4e759de4 2019-06-26 stsp return NULL;
3087 4e759de4 2019-06-26 stsp static const struct got_error *
3088 4e759de4 2019-06-26 stsp delete_branch(struct got_repository *repo, const char *branch_name)
3090 4e759de4 2019-06-26 stsp const struct got_error *err = NULL;
3091 4e759de4 2019-06-26 stsp struct got_reference *ref;
3092 4e759de4 2019-06-26 stsp char *refname;
3094 4e759de4 2019-06-26 stsp if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
3095 4e759de4 2019-06-26 stsp return got_error_from_errno("asprintf");
3097 4e759de4 2019-06-26 stsp err = got_ref_open(&ref, repo, refname, 0);
3099 4e759de4 2019-06-26 stsp goto done;
3101 4e759de4 2019-06-26 stsp err = got_ref_delete(ref, repo);
3102 4e759de4 2019-06-26 stsp got_ref_close(ref);
3104 4e759de4 2019-06-26 stsp free(refname);
3105 4e759de4 2019-06-26 stsp return err;
3108 4e759de4 2019-06-26 stsp static const struct got_error *
3109 4e759de4 2019-06-26 stsp add_branch(struct got_repository *repo, const char *branch_name,
3110 4e759de4 2019-06-26 stsp const char *base_branch)
3112 4e759de4 2019-06-26 stsp const struct got_error *err = NULL;
3113 4e759de4 2019-06-26 stsp struct got_object_id *id = NULL;
3114 4e759de4 2019-06-26 stsp struct got_reference *ref = NULL;
3115 4e759de4 2019-06-26 stsp char *base_refname = NULL, *refname = NULL;
3116 4e759de4 2019-06-26 stsp struct got_reference *base_ref;
3119 d3f84d51 2019-07-11 stsp * Don't let the user create a branch named '-'.
3120 d3f84d51 2019-07-11 stsp * While technically a valid reference name, this case is usually
3121 d3f84d51 2019-07-11 stsp * an unintended typo.
3123 d3f84d51 2019-07-11 stsp if (branch_name[0] == '-' && branch_name[1] == '\0')
3124 d3f84d51 2019-07-11 stsp return got_error(GOT_ERR_BAD_REF_NAME);
3126 4e759de4 2019-06-26 stsp if (strcmp(GOT_REF_HEAD, base_branch) == 0) {
3127 4e759de4 2019-06-26 stsp base_refname = strdup(GOT_REF_HEAD);
3128 4e759de4 2019-06-26 stsp if (base_refname == NULL)
3129 4e759de4 2019-06-26 stsp return got_error_from_errno("strdup");
3130 4e759de4 2019-06-26 stsp } else if (asprintf(&base_refname, "refs/heads/%s", base_branch) == -1)
3131 4e759de4 2019-06-26 stsp return got_error_from_errno("asprintf");
3133 4e759de4 2019-06-26 stsp err = got_ref_open(&base_ref, repo, base_refname, 0);
3135 4e759de4 2019-06-26 stsp goto done;
3136 4e759de4 2019-06-26 stsp err = got_ref_resolve(&id, repo, base_ref);
3137 4e759de4 2019-06-26 stsp got_ref_close(base_ref);
3139 4e759de4 2019-06-26 stsp goto done;
3141 4e759de4 2019-06-26 stsp if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
3142 4e759de4 2019-06-26 stsp err = got_error_from_errno("asprintf");
3143 4e759de4 2019-06-26 stsp goto done;
3146 4e759de4 2019-06-26 stsp err = got_ref_open(&ref, repo, refname, 0);
3147 4e759de4 2019-06-26 stsp if (err == NULL) {
3148 4e759de4 2019-06-26 stsp err = got_error(GOT_ERR_BRANCH_EXISTS);
3149 4e759de4 2019-06-26 stsp goto done;
3150 4e759de4 2019-06-26 stsp } else if (err->code != GOT_ERR_NOT_REF)
3151 4e759de4 2019-06-26 stsp goto done;
3153 4e759de4 2019-06-26 stsp err = got_ref_alloc(&ref, refname, id);
3155 4e759de4 2019-06-26 stsp goto done;
3157 4e759de4 2019-06-26 stsp err = got_ref_write(ref, repo);
3160 4e759de4 2019-06-26 stsp got_ref_close(ref);
3162 4e759de4 2019-06-26 stsp free(base_refname);
3163 4e759de4 2019-06-26 stsp free(refname);
3164 4e759de4 2019-06-26 stsp return err;
3167 4e759de4 2019-06-26 stsp static const struct got_error *
3168 4e759de4 2019-06-26 stsp cmd_branch(int argc, char *argv[])
3170 4e759de4 2019-06-26 stsp const struct got_error *error = NULL;
3171 4e759de4 2019-06-26 stsp struct got_repository *repo = NULL;
3172 4e759de4 2019-06-26 stsp struct got_worktree *worktree = NULL;
3173 4e759de4 2019-06-26 stsp char *cwd = NULL, *repo_path = NULL;
3174 4e759de4 2019-06-26 stsp int ch, do_list = 0;
3175 4e759de4 2019-06-26 stsp const char *delref = NULL;
3177 4e759de4 2019-06-26 stsp while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
3178 4e759de4 2019-06-26 stsp switch (ch) {
3180 4e759de4 2019-06-26 stsp delref = optarg;
3183 4e759de4 2019-06-26 stsp repo_path = realpath(optarg, NULL);
3184 4e759de4 2019-06-26 stsp if (repo_path == NULL)
3185 4e759de4 2019-06-26 stsp err(1, "-r option");
3186 4e759de4 2019-06-26 stsp got_path_strip_trailing_slashes(repo_path);
3189 4e759de4 2019-06-26 stsp do_list = 1;
3192 4e759de4 2019-06-26 stsp usage_branch();
3193 4e759de4 2019-06-26 stsp /* NOTREACHED */
3197 4e759de4 2019-06-26 stsp if (do_list && delref)
3198 4e759de4 2019-06-26 stsp errx(1, "-l and -d options are mutually exclusive\n");
3200 4e759de4 2019-06-26 stsp argc -= optind;
3201 4e759de4 2019-06-26 stsp argv += optind;
3203 4e759de4 2019-06-26 stsp if (do_list || delref) {
3204 4e759de4 2019-06-26 stsp if (argc > 0)
3205 4e759de4 2019-06-26 stsp usage_branch();
3206 4e759de4 2019-06-26 stsp } else if (argc < 1 || argc > 2)
3207 4e759de4 2019-06-26 stsp usage_branch();
3209 4e759de4 2019-06-26 stsp #ifndef PROFILE
3210 4e759de4 2019-06-26 stsp if (do_list) {
3211 4e759de4 2019-06-26 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3212 4e759de4 2019-06-26 stsp NULL) == -1)
3213 4e759de4 2019-06-26 stsp err(1, "pledge");
3215 4e759de4 2019-06-26 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3216 4e759de4 2019-06-26 stsp "sendfd unveil", NULL) == -1)
3217 4e759de4 2019-06-26 stsp err(1, "pledge");
3220 4e759de4 2019-06-26 stsp cwd = getcwd(NULL, 0);
3221 4e759de4 2019-06-26 stsp if (cwd == NULL) {
3222 4e759de4 2019-06-26 stsp error = got_error_from_errno("getcwd");
3223 4e759de4 2019-06-26 stsp goto done;
3226 4e759de4 2019-06-26 stsp if (repo_path == NULL) {
3227 4e759de4 2019-06-26 stsp error = got_worktree_open(&worktree, cwd);
3228 4e759de4 2019-06-26 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
3229 4e759de4 2019-06-26 stsp goto done;
3231 4e759de4 2019-06-26 stsp error = NULL;
3232 4e759de4 2019-06-26 stsp if (worktree) {
3233 4e759de4 2019-06-26 stsp repo_path =
3234 4e759de4 2019-06-26 stsp strdup(got_worktree_get_repo_path(worktree));
3235 4e759de4 2019-06-26 stsp if (repo_path == NULL)
3236 4e759de4 2019-06-26 stsp error = got_error_from_errno("strdup");
3237 4e759de4 2019-06-26 stsp if (error)
3238 4e759de4 2019-06-26 stsp goto done;
3240 4e759de4 2019-06-26 stsp repo_path = strdup(cwd);
3241 4e759de4 2019-06-26 stsp if (repo_path == NULL) {
3242 4e759de4 2019-06-26 stsp error = got_error_from_errno("strdup");
3243 4e759de4 2019-06-26 stsp goto done;
3248 4e759de4 2019-06-26 stsp error = got_repo_open(&repo, repo_path);
3249 4e759de4 2019-06-26 stsp if (error != NULL)
3250 4e759de4 2019-06-26 stsp goto done;
3252 4e759de4 2019-06-26 stsp error = apply_unveil(got_repo_get_path(repo), do_list,
3253 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
3254 4e759de4 2019-06-26 stsp if (error)
3255 4e759de4 2019-06-26 stsp goto done;
3257 4e759de4 2019-06-26 stsp if (do_list)
3258 ba882ee3 2019-07-11 stsp error = list_branches(repo, worktree);
3259 4e759de4 2019-06-26 stsp else if (delref)
3260 4e759de4 2019-06-26 stsp error = delete_branch(repo, delref);
3262 4e759de4 2019-06-26 stsp const char *base_branch;
3263 4e759de4 2019-06-26 stsp if (argc == 1) {
3264 4e759de4 2019-06-26 stsp base_branch = worktree ?
3265 4e759de4 2019-06-26 stsp got_worktree_get_head_ref_name(worktree) :
3266 4e759de4 2019-06-26 stsp GOT_REF_HEAD;
3267 dc5351b4 2019-07-30 stsp if (strncmp(base_branch, "refs/heads/", 11) == 0)
3268 dc5351b4 2019-07-30 stsp base_branch += 11;
3270 4e759de4 2019-06-26 stsp base_branch = argv[1];
3271 4e759de4 2019-06-26 stsp error = add_branch(repo, argv[0], base_branch);
3275 d0eebce4 2019-03-11 stsp got_repo_close(repo);
3276 d0eebce4 2019-03-11 stsp if (worktree)
3277 d0eebce4 2019-03-11 stsp got_worktree_close(worktree);
3278 d0eebce4 2019-03-11 stsp free(cwd);
3279 d0eebce4 2019-03-11 stsp free(repo_path);
3280 d00136be 2019-03-26 stsp return error;
3283 d00136be 2019-03-26 stsp __dead static void
3284 d00136be 2019-03-26 stsp usage_add(void)
3286 fbb7e5c7 2019-05-11 stsp fprintf(stderr, "usage: %s add file-path ...\n", getprogname());
3290 d00136be 2019-03-26 stsp static const struct got_error *
3291 d00136be 2019-03-26 stsp cmd_add(int argc, char *argv[])
3293 d00136be 2019-03-26 stsp const struct got_error *error = NULL;
3294 031a5338 2019-03-26 stsp struct got_repository *repo = NULL;
3295 d00136be 2019-03-26 stsp struct got_worktree *worktree = NULL;
3296 1dd54920 2019-05-11 stsp char *cwd = NULL;
3297 1dd54920 2019-05-11 stsp struct got_pathlist_head paths;
3298 1dd54920 2019-05-11 stsp struct got_pathlist_entry *pe;
3301 1dd54920 2019-05-11 stsp TAILQ_INIT(&paths);
3303 d00136be 2019-03-26 stsp while ((ch = getopt(argc, argv, "")) != -1) {
3304 d00136be 2019-03-26 stsp switch (ch) {
3306 d00136be 2019-03-26 stsp usage_add();
3307 d00136be 2019-03-26 stsp /* NOTREACHED */
3311 d00136be 2019-03-26 stsp argc -= optind;
3312 d00136be 2019-03-26 stsp argv += optind;
3314 43012d58 2019-07-14 stsp #ifndef PROFILE
3315 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3316 43012d58 2019-07-14 stsp NULL) == -1)
3317 43012d58 2019-07-14 stsp err(1, "pledge");
3319 723c305c 2019-05-11 jcs if (argc < 1)
3320 d00136be 2019-03-26 stsp usage_add();
3322 d00136be 2019-03-26 stsp cwd = getcwd(NULL, 0);
3323 d00136be 2019-03-26 stsp if (cwd == NULL) {
3324 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
3325 d00136be 2019-03-26 stsp goto done;
3328 d00136be 2019-03-26 stsp error = got_worktree_open(&worktree, cwd);
3329 d00136be 2019-03-26 stsp if (error)
3330 d00136be 2019-03-26 stsp goto done;
3332 031a5338 2019-03-26 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3333 031a5338 2019-03-26 stsp if (error != NULL)
3334 031a5338 2019-03-26 stsp goto done;
3336 031a5338 2019-03-26 stsp error = apply_unveil(got_repo_get_path(repo), 1,
3337 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
3338 d00136be 2019-03-26 stsp if (error)
3339 d00136be 2019-03-26 stsp goto done;
3341 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3342 6d022e97 2019-08-04 stsp if (error)
3343 6d022e97 2019-08-04 stsp goto done;
3345 1dd54920 2019-05-11 stsp error = got_worktree_schedule_add(worktree, &paths, print_status,
3346 1dd54920 2019-05-11 stsp NULL, repo);
3349 031a5338 2019-03-26 stsp got_repo_close(repo);
3350 d00136be 2019-03-26 stsp if (worktree)
3351 d00136be 2019-03-26 stsp got_worktree_close(worktree);
3352 1dd54920 2019-05-11 stsp TAILQ_FOREACH(pe, &paths, entry)
3353 1dd54920 2019-05-11 stsp free((char *)pe->path);
3354 1dd54920 2019-05-11 stsp got_pathlist_free(&paths);
3355 2ec1f75b 2019-03-26 stsp free(cwd);
3356 2ec1f75b 2019-03-26 stsp return error;
3359 2ec1f75b 2019-03-26 stsp __dead static void
3360 648e4ef7 2019-07-09 stsp usage_remove(void)
3362 648e4ef7 2019-07-09 stsp fprintf(stderr, "usage: %s remove [-f] file-path ...\n", getprogname());
3366 2ec1f75b 2019-03-26 stsp static const struct got_error *
3367 648e4ef7 2019-07-09 stsp cmd_remove(int argc, char *argv[])
3369 2ec1f75b 2019-03-26 stsp const struct got_error *error = NULL;
3370 2ec1f75b 2019-03-26 stsp struct got_worktree *worktree = NULL;
3371 2ec1f75b 2019-03-26 stsp struct got_repository *repo = NULL;
3372 17ed4618 2019-06-02 stsp char *cwd = NULL;
3373 17ed4618 2019-06-02 stsp struct got_pathlist_head paths;
3374 17ed4618 2019-06-02 stsp struct got_pathlist_entry *pe;
3375 6d022e97 2019-08-04 stsp int ch, delete_local_mods = 0;
3377 17ed4618 2019-06-02 stsp TAILQ_INIT(&paths);
3379 2ec1f75b 2019-03-26 stsp while ((ch = getopt(argc, argv, "f")) != -1) {
3380 2ec1f75b 2019-03-26 stsp switch (ch) {
3382 2ec1f75b 2019-03-26 stsp delete_local_mods = 1;
3385 2ec1f75b 2019-03-26 stsp usage_add();
3386 2ec1f75b 2019-03-26 stsp /* NOTREACHED */
3390 2ec1f75b 2019-03-26 stsp argc -= optind;
3391 2ec1f75b 2019-03-26 stsp argv += optind;
3393 43012d58 2019-07-14 stsp #ifndef PROFILE
3394 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3395 43012d58 2019-07-14 stsp NULL) == -1)
3396 43012d58 2019-07-14 stsp err(1, "pledge");
3398 17ed4618 2019-06-02 stsp if (argc < 1)
3399 648e4ef7 2019-07-09 stsp usage_remove();
3401 2ec1f75b 2019-03-26 stsp cwd = getcwd(NULL, 0);
3402 2ec1f75b 2019-03-26 stsp if (cwd == NULL) {
3403 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
3404 2ec1f75b 2019-03-26 stsp goto done;
3406 2ec1f75b 2019-03-26 stsp error = got_worktree_open(&worktree, cwd);
3407 2ec1f75b 2019-03-26 stsp if (error)
3408 2ec1f75b 2019-03-26 stsp goto done;
3410 2ec1f75b 2019-03-26 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3412 2ec1f75b 2019-03-26 stsp goto done;
3414 c2253644 2019-03-26 stsp error = apply_unveil(got_repo_get_path(repo), 1,
3415 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
3416 2ec1f75b 2019-03-26 stsp if (error)
3417 2ec1f75b 2019-03-26 stsp goto done;
3419 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3420 6d022e97 2019-08-04 stsp if (error)
3421 6d022e97 2019-08-04 stsp goto done;
3423 17ed4618 2019-06-02 stsp error = got_worktree_schedule_delete(worktree, &paths,
3424 17ed4618 2019-06-02 stsp delete_local_mods, print_status, NULL, repo);
3425 a129376b 2019-03-28 stsp if (error)
3426 a129376b 2019-03-28 stsp goto done;
3429 a129376b 2019-03-28 stsp got_repo_close(repo);
3430 a129376b 2019-03-28 stsp if (worktree)
3431 a129376b 2019-03-28 stsp got_worktree_close(worktree);
3432 17ed4618 2019-06-02 stsp TAILQ_FOREACH(pe, &paths, entry)
3433 17ed4618 2019-06-02 stsp free((char *)pe->path);
3434 17ed4618 2019-06-02 stsp got_pathlist_free(&paths);
3435 a129376b 2019-03-28 stsp free(cwd);
3436 a129376b 2019-03-28 stsp return error;
3439 a129376b 2019-03-28 stsp __dead static void
3440 a129376b 2019-03-28 stsp usage_revert(void)
3442 33aa809d 2019-08-08 stsp fprintf(stderr, "usage: %s revert [-p] [-F response-script] [-R] "
3443 33aa809d 2019-08-08 stsp "path ...\n", getprogname());
3447 1ee397ad 2019-07-12 stsp static const struct got_error *
3448 a129376b 2019-03-28 stsp revert_progress(void *arg, unsigned char status, const char *path)
3450 a129376b 2019-03-28 stsp while (path[0] == '/')
3452 a129376b 2019-03-28 stsp printf("%c %s\n", status, path);
3453 33aa809d 2019-08-08 stsp return NULL;
3456 33aa809d 2019-08-08 stsp struct choose_patch_arg {
3457 33aa809d 2019-08-08 stsp FILE *patch_script_file;
3458 33aa809d 2019-08-08 stsp const char *action;
3461 33aa809d 2019-08-08 stsp static const struct got_error *
3462 33aa809d 2019-08-08 stsp show_change(unsigned char status, const char *path, FILE *patch_file, int n,
3463 33aa809d 2019-08-08 stsp int nchanges, const char *action)
3465 33aa809d 2019-08-08 stsp char *line = NULL;
3466 33aa809d 2019-08-08 stsp size_t linesize = 0;
3467 33aa809d 2019-08-08 stsp ssize_t linelen;
3469 33aa809d 2019-08-08 stsp switch (status) {
3470 33aa809d 2019-08-08 stsp case GOT_STATUS_ADD:
3471 33aa809d 2019-08-08 stsp printf("A %s\n%s this addition? [y/n] ", path, action);
3473 33aa809d 2019-08-08 stsp case GOT_STATUS_DELETE:
3474 33aa809d 2019-08-08 stsp printf("D %s\n%s this deletion? [y/n] ", path, action);
3476 33aa809d 2019-08-08 stsp case GOT_STATUS_MODIFY:
3477 33aa809d 2019-08-08 stsp if (fseek(patch_file, 0L, SEEK_SET) == -1)
3478 33aa809d 2019-08-08 stsp return got_error_from_errno("fseek");
3479 33aa809d 2019-08-08 stsp printf(GOT_COMMIT_SEP_STR);
3480 9516e7cb 2019-08-11 stsp while ((linelen = getline(&line, &linesize, patch_file)) != -1)
3481 33aa809d 2019-08-08 stsp printf("%s", line);
3482 33aa809d 2019-08-08 stsp if (ferror(patch_file))
3483 33aa809d 2019-08-08 stsp return got_error_from_errno("getline");
3484 33aa809d 2019-08-08 stsp printf(GOT_COMMIT_SEP_STR);
3485 33aa809d 2019-08-08 stsp printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
3486 33aa809d 2019-08-08 stsp path, n, nchanges, action);
3489 33aa809d 2019-08-08 stsp return got_error_path(path, GOT_ERR_FILE_STATUS);
3492 33aa809d 2019-08-08 stsp return NULL;
3495 33aa809d 2019-08-08 stsp static const struct got_error *
3496 33aa809d 2019-08-08 stsp choose_patch(int *choice, void *arg, unsigned char status, const char *path,
3497 33aa809d 2019-08-08 stsp FILE *patch_file, int n, int nchanges)
3499 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
3500 33aa809d 2019-08-08 stsp char *line = NULL;
3501 33aa809d 2019-08-08 stsp size_t linesize = 0;
3502 33aa809d 2019-08-08 stsp ssize_t linelen;
3503 33aa809d 2019-08-08 stsp int resp = ' ';
3504 33aa809d 2019-08-08 stsp struct choose_patch_arg *a = arg;
3506 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NONE;
3508 33aa809d 2019-08-08 stsp if (a->patch_script_file) {
3510 33aa809d 2019-08-08 stsp err = show_change(status, path, patch_file, n, nchanges,
3511 33aa809d 2019-08-08 stsp a->action);
3513 33aa809d 2019-08-08 stsp return err;
3514 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, a->patch_script_file);
3515 33aa809d 2019-08-08 stsp if (linelen == -1) {
3516 33aa809d 2019-08-08 stsp if (ferror(a->patch_script_file))
3517 33aa809d 2019-08-08 stsp return got_error_from_errno("getline");
3518 33aa809d 2019-08-08 stsp return NULL;
3520 33aa809d 2019-08-08 stsp nl = strchr(line, '\n');
3522 33aa809d 2019-08-08 stsp *nl = '\0';
3523 33aa809d 2019-08-08 stsp if (strcmp(line, "y") == 0) {
3524 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_YES;
3525 33aa809d 2019-08-08 stsp printf("y\n");
3526 33aa809d 2019-08-08 stsp } else if (strcmp(line, "n") == 0) {
3527 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NO;
3528 33aa809d 2019-08-08 stsp printf("n\n");
3529 33aa809d 2019-08-08 stsp } else if (strcmp(line, "q") == 0 &&
3530 33aa809d 2019-08-08 stsp status == GOT_STATUS_MODIFY) {
3531 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_QUIT;
3532 33aa809d 2019-08-08 stsp printf("q\n");
3534 33aa809d 2019-08-08 stsp printf("invalid response '%s'\n", line);
3535 33aa809d 2019-08-08 stsp free(line);
3536 33aa809d 2019-08-08 stsp return NULL;
3539 33aa809d 2019-08-08 stsp while (resp != 'y' && resp != 'n' && resp != 'q') {
3540 33aa809d 2019-08-08 stsp err = show_change(status, path, patch_file, n, nchanges,
3541 33aa809d 2019-08-08 stsp a->action);
3543 33aa809d 2019-08-08 stsp return err;
3544 33aa809d 2019-08-08 stsp resp = getchar();
3545 33aa809d 2019-08-08 stsp if (resp == '\n')
3546 33aa809d 2019-08-08 stsp resp = getchar();
3547 33aa809d 2019-08-08 stsp if (status == GOT_STATUS_MODIFY) {
3548 33aa809d 2019-08-08 stsp if (resp != 'y' && resp != 'n' && resp != 'q') {
3549 33aa809d 2019-08-08 stsp printf("invalid response '%c'\n", resp);
3550 33aa809d 2019-08-08 stsp resp = ' ';
3552 33aa809d 2019-08-08 stsp } else if (resp != 'y' && resp != 'n') {
3553 33aa809d 2019-08-08 stsp printf("invalid response '%c'\n", resp);
3554 33aa809d 2019-08-08 stsp resp = ' ';
3558 33aa809d 2019-08-08 stsp if (resp == 'y')
3559 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_YES;
3560 33aa809d 2019-08-08 stsp else if (resp == 'n')
3561 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NO;
3562 33aa809d 2019-08-08 stsp else if (resp == 'q' && status == GOT_STATUS_MODIFY)
3563 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_QUIT;
3565 1ee397ad 2019-07-12 stsp return NULL;
3569 a129376b 2019-03-28 stsp static const struct got_error *
3570 a129376b 2019-03-28 stsp cmd_revert(int argc, char *argv[])
3572 a129376b 2019-03-28 stsp const struct got_error *error = NULL;
3573 a129376b 2019-03-28 stsp struct got_worktree *worktree = NULL;
3574 a129376b 2019-03-28 stsp struct got_repository *repo = NULL;
3575 a129376b 2019-03-28 stsp char *cwd = NULL, *path = NULL;
3576 e20a8b6f 2019-06-04 stsp struct got_pathlist_head paths;
3577 0f6d7415 2019-08-08 stsp struct got_pathlist_entry *pe;
3578 33aa809d 2019-08-08 stsp int ch, can_recurse = 0, pflag = 0;
3579 33aa809d 2019-08-08 stsp FILE *patch_script_file = NULL;
3580 33aa809d 2019-08-08 stsp const char *patch_script_path = NULL;
3581 33aa809d 2019-08-08 stsp struct choose_patch_arg cpa;
3583 e20a8b6f 2019-06-04 stsp TAILQ_INIT(&paths);
3585 33aa809d 2019-08-08 stsp while ((ch = getopt(argc, argv, "pF:R")) != -1) {
3586 a129376b 2019-03-28 stsp switch (ch) {
3588 33aa809d 2019-08-08 stsp pflag = 1;
3591 33aa809d 2019-08-08 stsp patch_script_path = optarg;
3594 0f6d7415 2019-08-08 stsp can_recurse = 1;
3597 a129376b 2019-03-28 stsp usage_revert();
3598 a129376b 2019-03-28 stsp /* NOTREACHED */
3602 a129376b 2019-03-28 stsp argc -= optind;
3603 a129376b 2019-03-28 stsp argv += optind;
3605 43012d58 2019-07-14 stsp #ifndef PROFILE
3606 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3607 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
3608 43012d58 2019-07-14 stsp err(1, "pledge");
3610 e20a8b6f 2019-06-04 stsp if (argc < 1)
3611 a129376b 2019-03-28 stsp usage_revert();
3612 33aa809d 2019-08-08 stsp if (patch_script_path && !pflag)
3613 33aa809d 2019-08-08 stsp errx(1, "-F option can only be used together with -p option");
3615 a129376b 2019-03-28 stsp cwd = getcwd(NULL, 0);
3616 a129376b 2019-03-28 stsp if (cwd == NULL) {
3617 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
3618 a129376b 2019-03-28 stsp goto done;
3620 a129376b 2019-03-28 stsp error = got_worktree_open(&worktree, cwd);
3621 2ec1f75b 2019-03-26 stsp if (error)
3622 2ec1f75b 2019-03-26 stsp goto done;
3624 a129376b 2019-03-28 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3625 a129376b 2019-03-28 stsp if (error != NULL)
3626 a129376b 2019-03-28 stsp goto done;
3628 33aa809d 2019-08-08 stsp if (patch_script_path) {
3629 33aa809d 2019-08-08 stsp patch_script_file = fopen(patch_script_path, "r");
3630 33aa809d 2019-08-08 stsp if (patch_script_file == NULL) {
3631 33aa809d 2019-08-08 stsp error = got_error_from_errno2("fopen",
3632 33aa809d 2019-08-08 stsp patch_script_path);
3633 33aa809d 2019-08-08 stsp goto done;
3636 a129376b 2019-03-28 stsp error = apply_unveil(got_repo_get_path(repo), 1,
3637 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
3638 a129376b 2019-03-28 stsp if (error)
3639 a129376b 2019-03-28 stsp goto done;
3641 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3642 6d022e97 2019-08-04 stsp if (error)
3643 6d022e97 2019-08-04 stsp goto done;
3645 0f6d7415 2019-08-08 stsp if (!can_recurse) {
3646 0f6d7415 2019-08-08 stsp char *ondisk_path;
3647 0f6d7415 2019-08-08 stsp struct stat sb;
3648 0f6d7415 2019-08-08 stsp TAILQ_FOREACH(pe, &paths, entry) {
3649 0f6d7415 2019-08-08 stsp if (asprintf(&ondisk_path, "%s/%s",
3650 0f6d7415 2019-08-08 stsp got_worktree_get_root_path(worktree),
3651 0f6d7415 2019-08-08 stsp pe->path) == -1) {
3652 0f6d7415 2019-08-08 stsp error = got_error_from_errno("asprintf");
3653 0f6d7415 2019-08-08 stsp goto done;
3655 0f6d7415 2019-08-08 stsp if (lstat(ondisk_path, &sb) == -1) {
3656 0f6d7415 2019-08-08 stsp if (errno == ENOENT) {
3657 0f6d7415 2019-08-08 stsp free(ondisk_path);
3660 0f6d7415 2019-08-08 stsp error = got_error_from_errno2("lstat",
3661 0f6d7415 2019-08-08 stsp ondisk_path);
3662 0f6d7415 2019-08-08 stsp free(ondisk_path);
3663 0f6d7415 2019-08-08 stsp goto done;
3665 0f6d7415 2019-08-08 stsp free(ondisk_path);
3666 0f6d7415 2019-08-08 stsp if (S_ISDIR(sb.st_mode)) {
3667 0f6d7415 2019-08-08 stsp error = got_error_msg(GOT_ERR_BAD_PATH,
3668 0f6d7415 2019-08-08 stsp "reverting directories requires -R option");
3669 0f6d7415 2019-08-08 stsp goto done;
3674 33aa809d 2019-08-08 stsp cpa.patch_script_file = patch_script_file;
3675 33aa809d 2019-08-08 stsp cpa.action = "revert";
3676 33aa809d 2019-08-08 stsp error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
3677 33aa809d 2019-08-08 stsp pflag ? choose_patch : NULL, &cpa, repo);
3678 a129376b 2019-03-28 stsp if (error)
3679 a129376b 2019-03-28 stsp goto done;
3681 33aa809d 2019-08-08 stsp if (patch_script_file && fclose(patch_script_file) == EOF &&
3682 33aa809d 2019-08-08 stsp error == NULL)
3683 33aa809d 2019-08-08 stsp error = got_error_from_errno2("fclose", patch_script_path);
3685 2ec1f75b 2019-03-26 stsp got_repo_close(repo);
3686 2ec1f75b 2019-03-26 stsp if (worktree)
3687 2ec1f75b 2019-03-26 stsp got_worktree_close(worktree);
3688 2ec1f75b 2019-03-26 stsp free(path);
3689 d00136be 2019-03-26 stsp free(cwd);
3690 6bad629b 2019-02-04 stsp return error;
3693 c4296144 2019-05-09 stsp __dead static void
3694 c4296144 2019-05-09 stsp usage_commit(void)
3696 5c1e53bc 2019-07-28 stsp fprintf(stderr, "usage: %s commit [-m msg] [path ...]\n",
3697 5c1e53bc 2019-07-28 stsp getprogname());
3701 e2ba3d07 2019-05-13 stsp struct collect_commit_logmsg_arg {
3702 e2ba3d07 2019-05-13 stsp const char *cmdline_log;
3703 e2ba3d07 2019-05-13 stsp const char *editor;
3704 e0870e44 2019-05-13 stsp const char *worktree_path;
3705 76d98825 2019-06-03 stsp const char *branch_name;
3706 314a6357 2019-05-13 stsp const char *repo_path;
3707 e0870e44 2019-05-13 stsp char *logmsg_path;
3711 33ad4cbe 2019-05-12 jcs static const struct got_error *
3712 33ad4cbe 2019-05-12 jcs collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
3715 76d98825 2019-06-03 stsp char *initial_content = NULL;
3716 33ad4cbe 2019-05-12 jcs struct got_pathlist_entry *pe;
3717 33ad4cbe 2019-05-12 jcs const struct got_error *err = NULL;
3718 e0870e44 2019-05-13 stsp char *template = NULL;
3719 e2ba3d07 2019-05-13 stsp struct collect_commit_logmsg_arg *a = arg;
3721 33ad4cbe 2019-05-12 jcs size_t len;
3723 33ad4cbe 2019-05-12 jcs /* if a message was specified on the command line, just use it */
3724 e2ba3d07 2019-05-13 stsp if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
3725 e2ba3d07 2019-05-13 stsp len = strlen(a->cmdline_log) + 1;
3726 33ad4cbe 2019-05-12 jcs *logmsg = malloc(len + 1);
3727 9f42ff69 2019-05-13 stsp if (*logmsg == NULL)
3728 638f9024 2019-05-13 stsp return got_error_from_errno("malloc");
3729 e2ba3d07 2019-05-13 stsp strlcpy(*logmsg, a->cmdline_log, len);
3730 33ad4cbe 2019-05-12 jcs return NULL;
3733 e0870e44 2019-05-13 stsp if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
3734 76d98825 2019-06-03 stsp return got_error_from_errno("asprintf");
3736 76d98825 2019-06-03 stsp if (asprintf(&initial_content,
3737 76d98825 2019-06-03 stsp "\n# changes to be committed on branch %s:\n",
3738 76d98825 2019-06-03 stsp a->branch_name) == -1)
3739 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3741 e0870e44 2019-05-13 stsp err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
3743 e0870e44 2019-05-13 stsp goto done;
3745 e0870e44 2019-05-13 stsp dprintf(fd, initial_content);
3747 33ad4cbe 2019-05-12 jcs TAILQ_FOREACH(pe, commitable_paths, entry) {
3748 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
3749 8656d6c4 2019-05-20 stsp dprintf(fd, "# %c %s\n",
3750 8656d6c4 2019-05-20 stsp got_commitable_get_status(ct),
3751 8656d6c4 2019-05-20 stsp got_commitable_get_path(ct));
3755 3ce1b845 2019-07-15 stsp err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content);
3757 e2af0fd1 2019-08-08 stsp if (err == NULL || err->code == GOT_ERR_COMMIT_MSG_EMPTY) {
3758 e2af0fd1 2019-08-08 stsp unlink(a->logmsg_path);
3759 e2af0fd1 2019-08-08 stsp free(a->logmsg_path);
3760 e2af0fd1 2019-08-08 stsp a->logmsg_path = NULL;
3762 76d98825 2019-06-03 stsp free(initial_content);
3763 e0870e44 2019-05-13 stsp free(template);
3765 314a6357 2019-05-13 stsp /* Editor is done; we can now apply unveil(2) */
3766 3ce1b845 2019-07-15 stsp if (err == NULL) {
3767 c530dc23 2019-07-23 stsp err = apply_unveil(a->repo_path, 0, a->worktree_path);
3768 3ce1b845 2019-07-15 stsp if (err) {
3769 3ce1b845 2019-07-15 stsp free(*logmsg);
3770 3ce1b845 2019-07-15 stsp *logmsg = NULL;
3773 33ad4cbe 2019-05-12 jcs return err;
3776 c4296144 2019-05-09 stsp static const struct got_error *
3777 c4296144 2019-05-09 stsp cmd_commit(int argc, char *argv[])
3779 c4296144 2019-05-09 stsp const struct got_error *error = NULL;
3780 c4296144 2019-05-09 stsp struct got_worktree *worktree = NULL;
3781 c4296144 2019-05-09 stsp struct got_repository *repo = NULL;
3782 5c1e53bc 2019-07-28 stsp char *cwd = NULL, *id_str = NULL;
3783 c4296144 2019-05-09 stsp struct got_object_id *id = NULL;
3784 33ad4cbe 2019-05-12 jcs const char *logmsg = NULL;
3785 84792843 2019-08-09 stsp const char *author;
3786 e2ba3d07 2019-05-13 stsp struct collect_commit_logmsg_arg cl_arg;
3787 e2ba3d07 2019-05-13 stsp char *editor = NULL;
3788 916f288c 2019-07-30 stsp int ch, rebase_in_progress, histedit_in_progress;
3789 5c1e53bc 2019-07-28 stsp struct got_pathlist_head paths;
3791 5c1e53bc 2019-07-28 stsp TAILQ_INIT(&paths);
3792 6ac5a73c 2019-08-08 stsp cl_arg.logmsg_path = NULL;
3794 c4296144 2019-05-09 stsp while ((ch = getopt(argc, argv, "m:")) != -1) {
3795 c4296144 2019-05-09 stsp switch (ch) {
3797 c4296144 2019-05-09 stsp logmsg = optarg;
3800 c4296144 2019-05-09 stsp usage_commit();
3801 c4296144 2019-05-09 stsp /* NOTREACHED */
3805 c4296144 2019-05-09 stsp argc -= optind;
3806 c4296144 2019-05-09 stsp argv += optind;
3808 43012d58 2019-07-14 stsp #ifndef PROFILE
3809 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3810 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
3811 43012d58 2019-07-14 stsp err(1, "pledge");
3813 84792843 2019-08-09 stsp error = get_author(&author);
3814 84792843 2019-08-09 stsp if (error)
3815 84792843 2019-08-09 stsp return error;
3817 c4296144 2019-05-09 stsp cwd = getcwd(NULL, 0);
3818 c4296144 2019-05-09 stsp if (cwd == NULL) {
3819 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
3820 c4296144 2019-05-09 stsp goto done;
3822 c4296144 2019-05-09 stsp error = got_worktree_open(&worktree, cwd);
3823 7d5807f4 2019-07-11 stsp if (error)
3824 7d5807f4 2019-07-11 stsp goto done;
3826 a698f62e 2019-07-25 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
3827 c4296144 2019-05-09 stsp if (error)
3828 a698f62e 2019-07-25 stsp goto done;
3829 a698f62e 2019-07-25 stsp if (rebase_in_progress) {
3830 a698f62e 2019-07-25 stsp error = got_error(GOT_ERR_REBASING);
3831 c4296144 2019-05-09 stsp goto done;
3834 916f288c 2019-07-30 stsp error = got_worktree_histedit_in_progress(&histedit_in_progress,
3835 916f288c 2019-07-30 stsp worktree);
3836 5c1e53bc 2019-07-28 stsp if (error)
3837 5c1e53bc 2019-07-28 stsp goto done;
3839 c4296144 2019-05-09 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3840 c4296144 2019-05-09 stsp if (error != NULL)
3841 c4296144 2019-05-09 stsp goto done;
3844 314a6357 2019-05-13 stsp * unveil(2) traverses exec(2); if an editor is used we have
3845 314a6357 2019-05-13 stsp * to apply unveil after the log message has been written.
3847 314a6357 2019-05-13 stsp if (logmsg == NULL || strlen(logmsg) == 0)
3848 314a6357 2019-05-13 stsp error = get_editor(&editor);
3850 314a6357 2019-05-13 stsp error = apply_unveil(got_repo_get_path(repo), 0,
3851 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
3852 c4296144 2019-05-09 stsp if (error)
3853 c4296144 2019-05-09 stsp goto done;
3855 087fb88c 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3856 087fb88c 2019-08-04 stsp if (error)
3857 087fb88c 2019-08-04 stsp goto done;
3859 e2ba3d07 2019-05-13 stsp cl_arg.editor = editor;
3860 e2ba3d07 2019-05-13 stsp cl_arg.cmdline_log = logmsg;
3861 e0870e44 2019-05-13 stsp cl_arg.worktree_path = got_worktree_get_root_path(worktree);
3862 76d98825 2019-06-03 stsp cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
3863 916f288c 2019-07-30 stsp if (!histedit_in_progress) {
3864 916f288c 2019-07-30 stsp if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
3865 916f288c 2019-07-30 stsp error = got_error(GOT_ERR_COMMIT_BRANCH);
3866 916f288c 2019-07-30 stsp goto done;
3868 916f288c 2019-07-30 stsp cl_arg.branch_name += 11;
3870 314a6357 2019-05-13 stsp cl_arg.repo_path = got_repo_get_path(repo);
3871 84792843 2019-08-09 stsp error = got_worktree_commit(&id, worktree, &paths, author, NULL,
3872 e2ba3d07 2019-05-13 stsp collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
3873 e0870e44 2019-05-13 stsp if (error) {
3874 e0870e44 2019-05-13 stsp if (cl_arg.logmsg_path)
3875 e0870e44 2019-05-13 stsp fprintf(stderr, "%s: log message preserved in %s\n",
3876 e0870e44 2019-05-13 stsp getprogname(), cl_arg.logmsg_path);
3877 c4296144 2019-05-09 stsp goto done;
3880 e0870e44 2019-05-13 stsp if (cl_arg.logmsg_path)
3881 e0870e44 2019-05-13 stsp unlink(cl_arg.logmsg_path);
3883 c4296144 2019-05-09 stsp error = got_object_id_str(&id_str, id);
3884 c4296144 2019-05-09 stsp if (error)
3885 c4296144 2019-05-09 stsp goto done;
3886 a7648d7a 2019-06-02 stsp printf("Created commit %s\n", id_str);
3888 6ac5a73c 2019-08-08 stsp free(cl_arg.logmsg_path);
3890 c4296144 2019-05-09 stsp got_repo_close(repo);
3891 c4296144 2019-05-09 stsp if (worktree)
3892 c4296144 2019-05-09 stsp got_worktree_close(worktree);
3893 c4296144 2019-05-09 stsp free(cwd);
3894 c4296144 2019-05-09 stsp free(id_str);
3895 e2ba3d07 2019-05-13 stsp free(editor);
3896 234035bc 2019-06-01 stsp return error;
3899 234035bc 2019-06-01 stsp __dead static void
3900 234035bc 2019-06-01 stsp usage_cherrypick(void)
3902 234035bc 2019-06-01 stsp fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
3906 234035bc 2019-06-01 stsp static const struct got_error *
3907 234035bc 2019-06-01 stsp cmd_cherrypick(int argc, char *argv[])
3909 234035bc 2019-06-01 stsp const struct got_error *error = NULL;
3910 234035bc 2019-06-01 stsp struct got_worktree *worktree = NULL;
3911 234035bc 2019-06-01 stsp struct got_repository *repo = NULL;
3912 234035bc 2019-06-01 stsp char *cwd = NULL, *commit_id_str = NULL;
3913 234035bc 2019-06-01 stsp struct got_object_id *commit_id = NULL;
3914 234035bc 2019-06-01 stsp struct got_commit_object *commit = NULL;
3915 234035bc 2019-06-01 stsp struct got_object_qid *pid;
3916 234035bc 2019-06-01 stsp struct got_reference *head_ref = NULL;
3917 234035bc 2019-06-01 stsp int ch, did_something = 0;
3919 234035bc 2019-06-01 stsp while ((ch = getopt(argc, argv, "")) != -1) {
3920 234035bc 2019-06-01 stsp switch (ch) {
3922 234035bc 2019-06-01 stsp usage_cherrypick();
3923 234035bc 2019-06-01 stsp /* NOTREACHED */
3927 234035bc 2019-06-01 stsp argc -= optind;
3928 234035bc 2019-06-01 stsp argv += optind;
3930 43012d58 2019-07-14 stsp #ifndef PROFILE
3931 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3932 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
3933 43012d58 2019-07-14 stsp err(1, "pledge");
3935 234035bc 2019-06-01 stsp if (argc != 1)
3936 234035bc 2019-06-01 stsp usage_cherrypick();
3938 234035bc 2019-06-01 stsp cwd = getcwd(NULL, 0);
3939 234035bc 2019-06-01 stsp if (cwd == NULL) {
3940 234035bc 2019-06-01 stsp error = got_error_from_errno("getcwd");
3941 234035bc 2019-06-01 stsp goto done;
3943 234035bc 2019-06-01 stsp error = got_worktree_open(&worktree, cwd);
3944 234035bc 2019-06-01 stsp if (error)
3945 234035bc 2019-06-01 stsp goto done;
3947 234035bc 2019-06-01 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3948 234035bc 2019-06-01 stsp if (error != NULL)
3949 234035bc 2019-06-01 stsp goto done;
3951 234035bc 2019-06-01 stsp error = apply_unveil(got_repo_get_path(repo), 0,
3952 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
3953 234035bc 2019-06-01 stsp if (error)
3954 234035bc 2019-06-01 stsp goto done;
3956 dd88155e 2019-06-29 stsp error = got_repo_match_object_id_prefix(&commit_id, argv[0],
3957 dd88155e 2019-06-29 stsp GOT_OBJ_TYPE_COMMIT, repo);
3958 234035bc 2019-06-01 stsp if (error != NULL) {
3959 234035bc 2019-06-01 stsp struct got_reference *ref;
3960 234035bc 2019-06-01 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
3961 234035bc 2019-06-01 stsp goto done;
3962 234035bc 2019-06-01 stsp error = got_ref_open(&ref, repo, argv[0], 0);
3963 234035bc 2019-06-01 stsp if (error != NULL)
3964 234035bc 2019-06-01 stsp goto done;
3965 234035bc 2019-06-01 stsp error = got_ref_resolve(&commit_id, repo, ref);
3966 234035bc 2019-06-01 stsp got_ref_close(ref);
3967 234035bc 2019-06-01 stsp if (error != NULL)
3968 234035bc 2019-06-01 stsp goto done;
3970 234035bc 2019-06-01 stsp error = got_object_id_str(&commit_id_str, commit_id);
3971 234035bc 2019-06-01 stsp if (error)
3972 234035bc 2019-06-01 stsp goto done;
3974 234035bc 2019-06-01 stsp error = got_ref_open(&head_ref, repo,
3975 234035bc 2019-06-01 stsp got_worktree_get_head_ref_name(worktree), 0);
3976 234035bc 2019-06-01 stsp if (error != NULL)
3977 234035bc 2019-06-01 stsp goto done;
3979 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
3980 234035bc 2019-06-01 stsp if (error) {
3981 234035bc 2019-06-01 stsp if (error->code != GOT_ERR_ANCESTRY)
3982 234035bc 2019-06-01 stsp goto done;
3983 234035bc 2019-06-01 stsp error = NULL;
3985 234035bc 2019-06-01 stsp error = got_error(GOT_ERR_SAME_BRANCH);
3986 234035bc 2019-06-01 stsp goto done;
3989 234035bc 2019-06-01 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
3990 234035bc 2019-06-01 stsp if (error)
3991 234035bc 2019-06-01 stsp goto done;
3992 234035bc 2019-06-01 stsp pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3993 03415a1a 2019-06-02 stsp error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
3994 03415a1a 2019-06-02 stsp commit_id, repo, update_progress, &did_something, check_cancelled,
3996 234035bc 2019-06-01 stsp if (error != NULL)
3997 234035bc 2019-06-01 stsp goto done;
3999 234035bc 2019-06-01 stsp if (did_something)
4000 a7648d7a 2019-06-02 stsp printf("Merged commit %s\n", commit_id_str);
4002 234035bc 2019-06-01 stsp if (commit)
4003 234035bc 2019-06-01 stsp got_object_commit_close(commit);
4004 234035bc 2019-06-01 stsp free(commit_id_str);
4005 234035bc 2019-06-01 stsp if (head_ref)
4006 234035bc 2019-06-01 stsp got_ref_close(head_ref);
4007 234035bc 2019-06-01 stsp if (worktree)
4008 234035bc 2019-06-01 stsp got_worktree_close(worktree);
4010 234035bc 2019-06-01 stsp got_repo_close(repo);
4011 c4296144 2019-05-09 stsp return error;
4014 5ef14e63 2019-06-02 stsp __dead static void
4015 5ef14e63 2019-06-02 stsp usage_backout(void)
4017 5ef14e63 2019-06-02 stsp fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
4021 5ef14e63 2019-06-02 stsp static const struct got_error *
4022 5ef14e63 2019-06-02 stsp cmd_backout(int argc, char *argv[])
4024 5ef14e63 2019-06-02 stsp const struct got_error *error = NULL;
4025 5ef14e63 2019-06-02 stsp struct got_worktree *worktree = NULL;
4026 5ef14e63 2019-06-02 stsp struct got_repository *repo = NULL;
4027 5ef14e63 2019-06-02 stsp char *cwd = NULL, *commit_id_str = NULL;
4028 5ef14e63 2019-06-02 stsp struct got_object_id *commit_id = NULL;
4029 5ef14e63 2019-06-02 stsp struct got_commit_object *commit = NULL;
4030 5ef14e63 2019-06-02 stsp struct got_object_qid *pid;
4031 5ef14e63 2019-06-02 stsp struct got_reference *head_ref = NULL;
4032 5ef14e63 2019-06-02 stsp int ch, did_something = 0;
4034 5ef14e63 2019-06-02 stsp while ((ch = getopt(argc, argv, "")) != -1) {
4035 5ef14e63 2019-06-02 stsp switch (ch) {
4037 5ef14e63 2019-06-02 stsp usage_backout();
4038 5ef14e63 2019-06-02 stsp /* NOTREACHED */
4042 5ef14e63 2019-06-02 stsp argc -= optind;
4043 5ef14e63 2019-06-02 stsp argv += optind;
4045 43012d58 2019-07-14 stsp #ifndef PROFILE
4046 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4047 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
4048 43012d58 2019-07-14 stsp err(1, "pledge");
4050 5ef14e63 2019-06-02 stsp if (argc != 1)
4051 5ef14e63 2019-06-02 stsp usage_backout();
4053 5ef14e63 2019-06-02 stsp cwd = getcwd(NULL, 0);
4054 5ef14e63 2019-06-02 stsp if (cwd == NULL) {
4055 5ef14e63 2019-06-02 stsp error = got_error_from_errno("getcwd");
4056 5ef14e63 2019-06-02 stsp goto done;
4058 5ef14e63 2019-06-02 stsp error = got_worktree_open(&worktree, cwd);
4059 5ef14e63 2019-06-02 stsp if (error)
4060 5ef14e63 2019-06-02 stsp goto done;
4062 5ef14e63 2019-06-02 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
4063 5ef14e63 2019-06-02 stsp if (error != NULL)
4064 5ef14e63 2019-06-02 stsp goto done;
4066 5ef14e63 2019-06-02 stsp error = apply_unveil(got_repo_get_path(repo), 0,
4067 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
4068 5ef14e63 2019-06-02 stsp if (error)
4069 5ef14e63 2019-06-02 stsp goto done;
4071 dd88155e 2019-06-29 stsp error = got_repo_match_object_id_prefix(&commit_id, argv[0],
4072 dd88155e 2019-06-29 stsp GOT_OBJ_TYPE_COMMIT, repo);
4073 5ef14e63 2019-06-02 stsp if (error != NULL) {
4074 5ef14e63 2019-06-02 stsp struct got_reference *ref;
4075 5ef14e63 2019-06-02 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
4076 5ef14e63 2019-06-02 stsp goto done;
4077 5ef14e63 2019-06-02 stsp error = got_ref_open(&ref, repo, argv[0], 0);
4078 5ef14e63 2019-06-02 stsp if (error != NULL)
4079 5ef14e63 2019-06-02 stsp goto done;
4080 5ef14e63 2019-06-02 stsp error = got_ref_resolve(&commit_id, repo, ref);
4081 5ef14e63 2019-06-02 stsp got_ref_close(ref);
4082 5ef14e63 2019-06-02 stsp if (error != NULL)
4083 5ef14e63 2019-06-02 stsp goto done;
4085 5ef14e63 2019-06-02 stsp error = got_object_id_str(&commit_id_str, commit_id);
4086 5ef14e63 2019-06-02 stsp if (error)
4087 5ef14e63 2019-06-02 stsp goto done;
4089 5ef14e63 2019-06-02 stsp error = got_ref_open(&head_ref, repo,
4090 5ef14e63 2019-06-02 stsp got_worktree_get_head_ref_name(worktree), 0);
4091 5ef14e63 2019-06-02 stsp if (error != NULL)
4092 5ef14e63 2019-06-02 stsp goto done;
4094 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
4095 5ef14e63 2019-06-02 stsp if (error)
4096 5ef14e63 2019-06-02 stsp goto done;
4098 5ef14e63 2019-06-02 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
4099 5ef14e63 2019-06-02 stsp if (error)
4100 5ef14e63 2019-06-02 stsp goto done;
4101 5ef14e63 2019-06-02 stsp pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
4102 5ef14e63 2019-06-02 stsp if (pid == NULL) {
4103 5ef14e63 2019-06-02 stsp error = got_error(GOT_ERR_ROOT_COMMIT);
4104 5ef14e63 2019-06-02 stsp goto done;
4107 5ef14e63 2019-06-02 stsp error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
4108 5ef14e63 2019-06-02 stsp update_progress, &did_something, check_cancelled, NULL);
4109 5ef14e63 2019-06-02 stsp if (error != NULL)
4110 5ef14e63 2019-06-02 stsp goto done;
4112 5ef14e63 2019-06-02 stsp if (did_something)
4113 a7648d7a 2019-06-02 stsp printf("Backed out commit %s\n", commit_id_str);
4115 5ef14e63 2019-06-02 stsp if (commit)
4116 5ef14e63 2019-06-02 stsp got_object_commit_close(commit);
4117 5ef14e63 2019-06-02 stsp free(commit_id_str);
4118 5ef14e63 2019-06-02 stsp if (head_ref)
4119 5ef14e63 2019-06-02 stsp got_ref_close(head_ref);
4120 818c7501 2019-07-11 stsp if (worktree)
4121 818c7501 2019-07-11 stsp got_worktree_close(worktree);
4123 818c7501 2019-07-11 stsp got_repo_close(repo);
4124 818c7501 2019-07-11 stsp return error;
4127 818c7501 2019-07-11 stsp __dead static void
4128 818c7501 2019-07-11 stsp usage_rebase(void)
4130 af54c8f8 2019-07-11 stsp fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
4131 af54c8f8 2019-07-11 stsp getprogname());
4136 0ebf8283 2019-07-24 stsp trim_logmsg(char *logmsg, int limit)
4139 0ebf8283 2019-07-24 stsp size_t len;
4141 0ebf8283 2019-07-24 stsp len = strlen(logmsg);
4142 0ebf8283 2019-07-24 stsp if (len > limit)
4143 0ebf8283 2019-07-24 stsp len = limit;
4144 0ebf8283 2019-07-24 stsp logmsg[len] = '\0';
4145 0ebf8283 2019-07-24 stsp nl = strchr(logmsg, '\n');
4147 0ebf8283 2019-07-24 stsp *nl = '\0';
4150 0ebf8283 2019-07-24 stsp static const struct got_error *
4151 0ebf8283 2019-07-24 stsp get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
4153 5943eee2 2019-08-13 stsp const struct got_error *err;
4154 5943eee2 2019-08-13 stsp char *logmsg0 = NULL;
4155 5943eee2 2019-08-13 stsp const char *s;
4157 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
4159 5943eee2 2019-08-13 stsp return err;
4161 5943eee2 2019-08-13 stsp s = logmsg0;
4162 5943eee2 2019-08-13 stsp while (isspace((unsigned char)s[0]))
4165 5943eee2 2019-08-13 stsp *logmsg = strdup(s);
4166 5943eee2 2019-08-13 stsp if (*logmsg == NULL) {
4167 5943eee2 2019-08-13 stsp err = got_error_from_errno("strdup");
4168 5943eee2 2019-08-13 stsp goto done;
4171 0ebf8283 2019-07-24 stsp trim_logmsg(*logmsg, limit);
4173 5943eee2 2019-08-13 stsp free(logmsg0);
4174 5943eee2 2019-08-13 stsp return err;
4177 818c7501 2019-07-11 stsp static const struct got_error *
4178 818c7501 2019-07-11 stsp show_rebase_progress(struct got_commit_object *commit,
4179 818c7501 2019-07-11 stsp struct got_object_id *old_id, struct got_object_id *new_id)
4181 818c7501 2019-07-11 stsp const struct got_error *err;
4182 0ebf8283 2019-07-24 stsp char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
4184 818c7501 2019-07-11 stsp err = got_object_id_str(&old_id_str, old_id);
4186 818c7501 2019-07-11 stsp goto done;
4188 ff0d2220 2019-07-11 stsp if (new_id) {
4189 ff0d2220 2019-07-11 stsp err = got_object_id_str(&new_id_str, new_id);
4191 ff0d2220 2019-07-11 stsp goto done;
4194 818c7501 2019-07-11 stsp old_id_str[12] = '\0';
4195 ff0d2220 2019-07-11 stsp if (new_id_str)
4196 ff0d2220 2019-07-11 stsp new_id_str[12] = '\0';
4198 0ebf8283 2019-07-24 stsp err = get_short_logmsg(&logmsg, 42, commit);
4200 0ebf8283 2019-07-24 stsp goto done;
4202 ff0d2220 2019-07-11 stsp printf("%s -> %s: %s\n", old_id_str,
4203 ff0d2220 2019-07-11 stsp new_id_str ? new_id_str : "no-op change", logmsg);
4205 818c7501 2019-07-11 stsp free(old_id_str);
4206 818c7501 2019-07-11 stsp free(new_id_str);
4207 818c7501 2019-07-11 stsp return err;
4210 1ee397ad 2019-07-12 stsp static const struct got_error *
4211 818c7501 2019-07-11 stsp rebase_progress(void *arg, unsigned char status, const char *path)
4213 818c7501 2019-07-11 stsp unsigned char *rebase_status = arg;
4215 818c7501 2019-07-11 stsp while (path[0] == '/')
4217 818c7501 2019-07-11 stsp printf("%c %s\n", status, path);
4219 818c7501 2019-07-11 stsp if (*rebase_status == GOT_STATUS_CONFLICT)
4220 1ee397ad 2019-07-12 stsp return NULL;
4221 818c7501 2019-07-11 stsp if (status == GOT_STATUS_CONFLICT || status == GOT_STATUS_MERGE)
4222 818c7501 2019-07-11 stsp *rebase_status = status;
4223 1ee397ad 2019-07-12 stsp return NULL;
4226 818c7501 2019-07-11 stsp static const struct got_error *
4227 3e3a69f1 2019-07-25 stsp rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
4228 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_reference *new_base_branch,
4229 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_repository *repo)
4231 818c7501 2019-07-11 stsp printf("Switching work tree to %s\n", got_ref_get_name(branch));
4232 3e3a69f1 2019-07-25 stsp return got_worktree_rebase_complete(worktree, fileindex,
4233 818c7501 2019-07-11 stsp new_base_branch, tmp_branch, branch, repo);
4236 818c7501 2019-07-11 stsp static const struct got_error *
4237 01757395 2019-07-12 stsp rebase_commit(struct got_pathlist_head *merged_paths,
4238 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
4239 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch,
4240 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
4242 ff0d2220 2019-07-11 stsp const struct got_error *error;
4243 ff0d2220 2019-07-11 stsp struct got_commit_object *commit;
4244 ff0d2220 2019-07-11 stsp struct got_object_id *new_commit_id;
4246 ff0d2220 2019-07-11 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
4247 ff0d2220 2019-07-11 stsp if (error)
4248 ff0d2220 2019-07-11 stsp return error;
4250 01757395 2019-07-12 stsp error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
4251 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, commit, commit_id, repo);
4252 ff0d2220 2019-07-11 stsp if (error) {
4253 ff0d2220 2019-07-11 stsp if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
4254 ff0d2220 2019-07-11 stsp goto done;
4255 ff0d2220 2019-07-11 stsp error = show_rebase_progress(commit, commit_id, NULL);
4257 ff0d2220 2019-07-11 stsp error = show_rebase_progress(commit, commit_id, new_commit_id);
4258 ff0d2220 2019-07-11 stsp free(new_commit_id);
4261 ff0d2220 2019-07-11 stsp got_object_commit_close(commit);
4262 ff0d2220 2019-07-11 stsp return error;
4265 64c6d990 2019-07-11 stsp struct check_path_prefix_arg {
4266 64c6d990 2019-07-11 stsp const char *path_prefix;
4267 64c6d990 2019-07-11 stsp size_t len;
4268 8ca9bd68 2019-07-25 stsp int errcode;
4271 64c6d990 2019-07-11 stsp static const struct got_error *
4272 8ca9bd68 2019-07-25 stsp check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
4273 64c6d990 2019-07-11 stsp struct got_blob_object *blob2, struct got_object_id *id1,
4274 64c6d990 2019-07-11 stsp struct got_object_id *id2, const char *path1, const char *path2,
4275 64c6d990 2019-07-11 stsp struct got_repository *repo)
4277 64c6d990 2019-07-11 stsp struct check_path_prefix_arg *a = arg;
4279 64c6d990 2019-07-11 stsp if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
4280 64c6d990 2019-07-11 stsp (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
4281 8ca9bd68 2019-07-25 stsp return got_error(a->errcode);
4283 64c6d990 2019-07-11 stsp return NULL;
4286 64c6d990 2019-07-11 stsp static const struct got_error *
4287 8ca9bd68 2019-07-25 stsp check_path_prefix(struct got_object_id *parent_id,
4288 64c6d990 2019-07-11 stsp struct got_object_id *commit_id, const char *path_prefix,
4289 8ca9bd68 2019-07-25 stsp int errcode, struct got_repository *repo)
4291 64c6d990 2019-07-11 stsp const struct got_error *err;
4292 64c6d990 2019-07-11 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
4293 64c6d990 2019-07-11 stsp struct got_commit_object *commit = NULL, *parent_commit = NULL;
4294 64c6d990 2019-07-11 stsp struct check_path_prefix_arg cpp_arg;
4296 64c6d990 2019-07-11 stsp if (got_path_is_root_dir(path_prefix))
4297 64c6d990 2019-07-11 stsp return NULL;
4299 64c6d990 2019-07-11 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
4301 64c6d990 2019-07-11 stsp goto done;
4303 64c6d990 2019-07-11 stsp err = got_object_open_as_commit(&parent_commit, repo, parent_id);
4305 64c6d990 2019-07-11 stsp goto done;
4307 64c6d990 2019-07-11 stsp err = got_object_open_as_tree(&tree1, repo,
4308 64c6d990 2019-07-11 stsp got_object_commit_get_tree_id(parent_commit));
4310 64c6d990 2019-07-11 stsp goto done;
4312 64c6d990 2019-07-11 stsp err = got_object_open_as_tree(&tree2, repo,
4313 64c6d990 2019-07-11 stsp got_object_commit_get_tree_id(commit));
4315 64c6d990 2019-07-11 stsp goto done;
4317 64c6d990 2019-07-11 stsp cpp_arg.path_prefix = path_prefix;
4318 d23ace97 2019-07-25 stsp while (cpp_arg.path_prefix[0] == '/')
4319 d23ace97 2019-07-25 stsp cpp_arg.path_prefix++;
4320 d23ace97 2019-07-25 stsp cpp_arg.len = strlen(cpp_arg.path_prefix);
4321 8ca9bd68 2019-07-25 stsp cpp_arg.errcode = errcode;
4322 8ca9bd68 2019-07-25 stsp err = got_diff_tree(tree1, tree2, "", "", repo,
4323 31b4484f 2019-07-27 stsp check_path_prefix_in_diff, &cpp_arg, 0);
4325 64c6d990 2019-07-11 stsp if (tree1)
4326 64c6d990 2019-07-11 stsp got_object_tree_close(tree1);
4327 64c6d990 2019-07-11 stsp if (tree2)
4328 64c6d990 2019-07-11 stsp got_object_tree_close(tree2);
4329 64c6d990 2019-07-11 stsp if (commit)
4330 64c6d990 2019-07-11 stsp got_object_commit_close(commit);
4331 64c6d990 2019-07-11 stsp if (parent_commit)
4332 64c6d990 2019-07-11 stsp got_object_commit_close(parent_commit);
4333 64c6d990 2019-07-11 stsp return err;
4336 ff0d2220 2019-07-11 stsp static const struct got_error *
4337 8ca9bd68 2019-07-25 stsp collect_commits(struct got_object_id_queue *commits,
4338 af61c510 2019-07-19 stsp struct got_object_id *initial_commit_id,
4339 af61c510 2019-07-19 stsp struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
4340 8ca9bd68 2019-07-25 stsp const char *path_prefix, int path_prefix_errcode,
4341 8ca9bd68 2019-07-25 stsp struct got_repository *repo)
4343 af61c510 2019-07-19 stsp const struct got_error *err = NULL;
4344 af61c510 2019-07-19 stsp struct got_commit_graph *graph = NULL;
4345 af61c510 2019-07-19 stsp struct got_object_id *parent_id = NULL;
4346 af61c510 2019-07-19 stsp struct got_object_qid *qid;
4347 af61c510 2019-07-19 stsp struct got_object_id *commit_id = initial_commit_id;
4349 af61c510 2019-07-19 stsp err = got_commit_graph_open(&graph, initial_commit_id, "/", 1, repo);
4351 af61c510 2019-07-19 stsp return err;
4353 6fb7cd11 2019-08-22 stsp err = got_commit_graph_iter_start(graph, iter_start_id, repo,
4354 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
4356 af61c510 2019-07-19 stsp goto done;
4357 af61c510 2019-07-19 stsp while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
4358 af61c510 2019-07-19 stsp err = got_commit_graph_iter_next(&parent_id, graph);
4359 af61c510 2019-07-19 stsp if (err) {
4360 af61c510 2019-07-19 stsp if (err->code == GOT_ERR_ITER_COMPLETED) {
4361 af61c510 2019-07-19 stsp err = got_error_msg(GOT_ERR_ANCESTRY,
4362 af61c510 2019-07-19 stsp "ran out of commits to rebase before "
4363 af61c510 2019-07-19 stsp "youngest common ancestor commit has "
4364 af61c510 2019-07-19 stsp "been reached?!?");
4365 af61c510 2019-07-19 stsp goto done;
4366 af61c510 2019-07-19 stsp } else if (err->code != GOT_ERR_ITER_NEED_MORE)
4367 af61c510 2019-07-19 stsp goto done;
4368 6fb7cd11 2019-08-22 stsp err = got_commit_graph_fetch_commits(graph, 1, repo,
4369 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
4371 af61c510 2019-07-19 stsp goto done;
4373 8ca9bd68 2019-07-25 stsp err = check_path_prefix(parent_id, commit_id,
4374 8ca9bd68 2019-07-25 stsp path_prefix, path_prefix_errcode, repo);
4376 af61c510 2019-07-19 stsp goto done;
4378 af61c510 2019-07-19 stsp err = got_object_qid_alloc(&qid, commit_id);
4380 af61c510 2019-07-19 stsp goto done;
4381 af61c510 2019-07-19 stsp SIMPLEQ_INSERT_HEAD(commits, qid, entry);
4382 af61c510 2019-07-19 stsp commit_id = parent_id;
4386 af61c510 2019-07-19 stsp got_commit_graph_close(graph);
4387 af61c510 2019-07-19 stsp return err;
4390 af61c510 2019-07-19 stsp static const struct got_error *
4391 818c7501 2019-07-11 stsp cmd_rebase(int argc, char *argv[])
4393 818c7501 2019-07-11 stsp const struct got_error *error = NULL;
4394 818c7501 2019-07-11 stsp struct got_worktree *worktree = NULL;
4395 818c7501 2019-07-11 stsp struct got_repository *repo = NULL;
4396 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex = NULL;
4397 818c7501 2019-07-11 stsp char *cwd = NULL;
4398 818c7501 2019-07-11 stsp struct got_reference *branch = NULL;
4399 818c7501 2019-07-11 stsp struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
4400 818c7501 2019-07-11 stsp struct got_object_id *commit_id = NULL, *parent_id = NULL;
4401 818c7501 2019-07-11 stsp struct got_object_id *resume_commit_id = NULL;
4402 818c7501 2019-07-11 stsp struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
4403 818c7501 2019-07-11 stsp struct got_commit_object *commit = NULL;
4404 818c7501 2019-07-11 stsp int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
4405 818c7501 2019-07-11 stsp unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
4406 818c7501 2019-07-11 stsp struct got_object_id_queue commits;
4407 01757395 2019-07-12 stsp struct got_pathlist_head merged_paths;
4408 818c7501 2019-07-11 stsp const struct got_object_id_queue *parent_ids;
4409 818c7501 2019-07-11 stsp struct got_object_qid *qid, *pid;
4411 818c7501 2019-07-11 stsp SIMPLEQ_INIT(&commits);
4412 01757395 2019-07-12 stsp TAILQ_INIT(&merged_paths);
4414 818c7501 2019-07-11 stsp while ((ch = getopt(argc, argv, "ac")) != -1) {
4415 818c7501 2019-07-11 stsp switch (ch) {
4417 818c7501 2019-07-11 stsp abort_rebase = 1;
4420 818c7501 2019-07-11 stsp continue_rebase = 1;
4423 818c7501 2019-07-11 stsp usage_rebase();
4424 818c7501 2019-07-11 stsp /* NOTREACHED */
4428 818c7501 2019-07-11 stsp argc -= optind;
4429 818c7501 2019-07-11 stsp argv += optind;
4431 43012d58 2019-07-14 stsp #ifndef PROFILE
4432 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4433 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
4434 43012d58 2019-07-14 stsp err(1, "pledge");
4436 818c7501 2019-07-11 stsp if (abort_rebase && continue_rebase)
4437 818c7501 2019-07-11 stsp usage_rebase();
4438 818c7501 2019-07-11 stsp else if (abort_rebase || continue_rebase) {
4439 818c7501 2019-07-11 stsp if (argc != 0)
4440 818c7501 2019-07-11 stsp usage_rebase();
4441 818c7501 2019-07-11 stsp } else if (argc != 1)
4442 818c7501 2019-07-11 stsp usage_rebase();
4444 818c7501 2019-07-11 stsp cwd = getcwd(NULL, 0);
4445 818c7501 2019-07-11 stsp if (cwd == NULL) {
4446 818c7501 2019-07-11 stsp error = got_error_from_errno("getcwd");
4447 818c7501 2019-07-11 stsp goto done;
4449 818c7501 2019-07-11 stsp error = got_worktree_open(&worktree, cwd);
4450 818c7501 2019-07-11 stsp if (error)
4451 818c7501 2019-07-11 stsp goto done;
4453 818c7501 2019-07-11 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
4454 818c7501 2019-07-11 stsp if (error != NULL)
4455 818c7501 2019-07-11 stsp goto done;
4457 818c7501 2019-07-11 stsp error = apply_unveil(got_repo_get_path(repo), 0,
4458 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
4459 818c7501 2019-07-11 stsp if (error)
4460 818c7501 2019-07-11 stsp goto done;
4462 818c7501 2019-07-11 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
4463 818c7501 2019-07-11 stsp if (error)
4464 818c7501 2019-07-11 stsp goto done;
4466 f6794adc 2019-07-23 stsp if (abort_rebase) {
4467 818c7501 2019-07-11 stsp int did_something;
4468 f6794adc 2019-07-23 stsp if (!rebase_in_progress) {
4469 f6794adc 2019-07-23 stsp error = got_error(GOT_ERR_NOT_REBASING);
4470 f6794adc 2019-07-23 stsp goto done;
4472 818c7501 2019-07-11 stsp error = got_worktree_rebase_continue(&resume_commit_id,
4473 3e3a69f1 2019-07-25 stsp &new_base_branch, &tmp_branch, &branch, &fileindex,
4474 3e3a69f1 2019-07-25 stsp worktree, repo);
4475 818c7501 2019-07-11 stsp if (error)
4476 818c7501 2019-07-11 stsp goto done;
4477 818c7501 2019-07-11 stsp printf("Switching work tree to %s\n",
4478 818c7501 2019-07-11 stsp got_ref_get_symref_target(new_base_branch));
4479 3e3a69f1 2019-07-25 stsp error = got_worktree_rebase_abort(worktree, fileindex, repo,
4480 818c7501 2019-07-11 stsp new_base_branch, update_progress, &did_something);
4481 818c7501 2019-07-11 stsp if (error)
4482 818c7501 2019-07-11 stsp goto done;
4483 818c7501 2019-07-11 stsp printf("Rebase of %s aborted\n", got_ref_get_name(branch));
4484 818c7501 2019-07-11 stsp goto done; /* nothing else to do */
4487 818c7501 2019-07-11 stsp if (continue_rebase) {
4488 f6794adc 2019-07-23 stsp if (!rebase_in_progress) {
4489 f6794adc 2019-07-23 stsp error = got_error(GOT_ERR_NOT_REBASING);
4490 f6794adc 2019-07-23 stsp goto done;
4492 818c7501 2019-07-11 stsp error = got_worktree_rebase_continue(&resume_commit_id,
4493 3e3a69f1 2019-07-25 stsp &new_base_branch, &tmp_branch, &branch, &fileindex,
4494 3e3a69f1 2019-07-25 stsp worktree, repo);
4495 818c7501 2019-07-11 stsp if (error)
4496 818c7501 2019-07-11 stsp goto done;
4498 3e3a69f1 2019-07-25 stsp error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
4499 01757395 2019-07-12 stsp resume_commit_id, repo);
4500 818c7501 2019-07-11 stsp if (error)
4501 818c7501 2019-07-11 stsp goto done;
4503 ff0d2220 2019-07-11 stsp yca_id = got_object_id_dup(resume_commit_id);
4504 ff0d2220 2019-07-11 stsp if (yca_id == NULL) {
4505 818c7501 2019-07-11 stsp error = got_error_from_errno("got_object_id_dup");
4506 818c7501 2019-07-11 stsp goto done;
4509 818c7501 2019-07-11 stsp error = got_ref_open(&branch, repo, argv[0], 0);
4510 818c7501 2019-07-11 stsp if (error != NULL)
4511 818c7501 2019-07-11 stsp goto done;
4514 ff0d2220 2019-07-11 stsp error = got_ref_resolve(&branch_head_commit_id, repo, branch);
4515 ff0d2220 2019-07-11 stsp if (error)
4516 ff0d2220 2019-07-11 stsp goto done;
4518 ff0d2220 2019-07-11 stsp if (!continue_rebase) {
4519 a51a74b3 2019-07-27 stsp struct got_object_id *base_commit_id;
4521 a51a74b3 2019-07-27 stsp base_commit_id = got_worktree_get_base_commit_id(worktree);
4522 818c7501 2019-07-11 stsp error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
4523 6fb7cd11 2019-08-22 stsp base_commit_id, branch_head_commit_id, repo,
4524 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
4525 818c7501 2019-07-11 stsp if (error)
4526 818c7501 2019-07-11 stsp goto done;
4527 818c7501 2019-07-11 stsp if (yca_id == NULL) {
4528 818c7501 2019-07-11 stsp error = got_error_msg(GOT_ERR_ANCESTRY,
4529 818c7501 2019-07-11 stsp "specified branch shares no common ancestry "
4530 818c7501 2019-07-11 stsp "with work tree's branch");
4531 818c7501 2019-07-11 stsp goto done;
4534 a51a74b3 2019-07-27 stsp error = check_same_branch(base_commit_id, branch, yca_id, repo);
4535 a51a74b3 2019-07-27 stsp if (error) {
4536 a51a74b3 2019-07-27 stsp if (error->code != GOT_ERR_ANCESTRY)
4537 a51a74b3 2019-07-27 stsp goto done;
4538 a51a74b3 2019-07-27 stsp error = NULL;
4540 a51a74b3 2019-07-27 stsp error = got_error_msg(GOT_ERR_SAME_BRANCH,
4541 a51a74b3 2019-07-27 stsp "specified branch resolves to a commit which "
4542 a51a74b3 2019-07-27 stsp "is already contained in work tree's branch");
4543 a51a74b3 2019-07-27 stsp goto done;
4545 818c7501 2019-07-11 stsp error = got_worktree_rebase_prepare(&new_base_branch,
4546 3e3a69f1 2019-07-25 stsp &tmp_branch, &fileindex, worktree, branch, repo);
4547 818c7501 2019-07-11 stsp if (error)
4548 818c7501 2019-07-11 stsp goto done;
4551 ff0d2220 2019-07-11 stsp commit_id = branch_head_commit_id;
4552 818c7501 2019-07-11 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
4553 818c7501 2019-07-11 stsp if (error)
4554 818c7501 2019-07-11 stsp goto done;
4556 818c7501 2019-07-11 stsp parent_ids = got_object_commit_get_parent_ids(commit);
4557 818c7501 2019-07-11 stsp pid = SIMPLEQ_FIRST(parent_ids);
4558 fc66b545 2019-08-12 stsp if (pid == NULL) {
4559 fc66b545 2019-08-12 stsp if (!continue_rebase) {
4560 fc66b545 2019-08-12 stsp int did_something;
4561 fc66b545 2019-08-12 stsp error = got_worktree_rebase_abort(worktree, fileindex,
4562 fc66b545 2019-08-12 stsp repo, new_base_branch, update_progress,
4563 fc66b545 2019-08-12 stsp &did_something);
4564 fc66b545 2019-08-12 stsp if (error)
4565 fc66b545 2019-08-12 stsp goto done;
4566 fc66b545 2019-08-12 stsp printf("Rebase of %s aborted\n",
4567 fc66b545 2019-08-12 stsp got_ref_get_name(branch));
4569 fc66b545 2019-08-12 stsp error = got_error(GOT_ERR_EMPTY_REBASE);
4570 fc66b545 2019-08-12 stsp goto done;
4572 8ca9bd68 2019-07-25 stsp error = collect_commits(&commits, commit_id, pid->id,
4573 8ca9bd68 2019-07-25 stsp yca_id, got_worktree_get_path_prefix(worktree),
4574 8ca9bd68 2019-07-25 stsp GOT_ERR_REBASE_PATH, repo);
4575 818c7501 2019-07-11 stsp got_object_commit_close(commit);
4576 818c7501 2019-07-11 stsp commit = NULL;
4577 818c7501 2019-07-11 stsp if (error)
4578 818c7501 2019-07-11 stsp goto done;
4580 818c7501 2019-07-11 stsp if (SIMPLEQ_EMPTY(&commits)) {
4581 ff0d2220 2019-07-11 stsp if (continue_rebase)
4582 3e3a69f1 2019-07-25 stsp error = rebase_complete(worktree, fileindex,
4583 3e3a69f1 2019-07-25 stsp branch, new_base_branch, tmp_branch, repo);
4585 ff0d2220 2019-07-11 stsp error = got_error(GOT_ERR_EMPTY_REBASE);
4586 818c7501 2019-07-11 stsp goto done;
4589 818c7501 2019-07-11 stsp pid = NULL;
4590 818c7501 2019-07-11 stsp SIMPLEQ_FOREACH(qid, &commits, entry) {
4591 818c7501 2019-07-11 stsp commit_id = qid->id;
4592 818c7501 2019-07-11 stsp parent_id = pid ? pid->id : yca_id;
4593 818c7501 2019-07-11 stsp pid = qid;
4595 01757395 2019-07-12 stsp error = got_worktree_rebase_merge_files(&merged_paths,
4596 3e3a69f1 2019-07-25 stsp worktree, fileindex, parent_id, commit_id, repo,
4597 3e3a69f1 2019-07-25 stsp rebase_progress, &rebase_status, check_cancelled, NULL);
4598 818c7501 2019-07-11 stsp if (error)
4599 818c7501 2019-07-11 stsp goto done;
4601 01757395 2019-07-12 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
4602 01757395 2019-07-12 stsp got_worktree_rebase_pathlist_free(&merged_paths);
4606 3e3a69f1 2019-07-25 stsp error = rebase_commit(&merged_paths, worktree, fileindex,
4607 3e3a69f1 2019-07-25 stsp tmp_branch, commit_id, repo);
4608 01757395 2019-07-12 stsp got_worktree_rebase_pathlist_free(&merged_paths);
4609 818c7501 2019-07-11 stsp if (error)
4610 818c7501 2019-07-11 stsp goto done;
4613 818c7501 2019-07-11 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
4614 3e3a69f1 2019-07-25 stsp error = got_worktree_rebase_postpone(worktree, fileindex);
4615 818c7501 2019-07-11 stsp if (error)
4616 818c7501 2019-07-11 stsp goto done;
4617 818c7501 2019-07-11 stsp error = got_error_msg(GOT_ERR_CONFLICTS,
4618 11495e04 2019-07-12 stsp "conflicts must be resolved before rebasing can continue");
4620 3e3a69f1 2019-07-25 stsp error = rebase_complete(worktree, fileindex, branch,
4621 3e3a69f1 2019-07-25 stsp new_base_branch, tmp_branch, repo);
4623 818c7501 2019-07-11 stsp got_object_id_queue_free(&commits);
4624 818c7501 2019-07-11 stsp free(branch_head_commit_id);
4625 818c7501 2019-07-11 stsp free(resume_commit_id);
4626 818c7501 2019-07-11 stsp free(yca_id);
4627 818c7501 2019-07-11 stsp if (commit)
4628 818c7501 2019-07-11 stsp got_object_commit_close(commit);
4629 818c7501 2019-07-11 stsp if (branch)
4630 818c7501 2019-07-11 stsp got_ref_close(branch);
4631 818c7501 2019-07-11 stsp if (new_base_branch)
4632 818c7501 2019-07-11 stsp got_ref_close(new_base_branch);
4633 818c7501 2019-07-11 stsp if (tmp_branch)
4634 818c7501 2019-07-11 stsp got_ref_close(tmp_branch);
4635 5ef14e63 2019-06-02 stsp if (worktree)
4636 5ef14e63 2019-06-02 stsp got_worktree_close(worktree);
4638 5ef14e63 2019-06-02 stsp got_repo_close(repo);
4639 5ef14e63 2019-06-02 stsp return error;
4642 0ebf8283 2019-07-24 stsp __dead static void
4643 0ebf8283 2019-07-24 stsp usage_histedit(void)
4645 f3055044 2019-08-07 stsp fprintf(stderr, "usage: %s histedit [-a] [-c] [-F histedit-script]\n",
4646 0ebf8283 2019-07-24 stsp getprogname());
4650 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_PICK 'p'
4651 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_EDIT 'e'
4652 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_FOLD 'f'
4653 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_DROP 'd'
4654 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_MESG 'm'
4656 0ebf8283 2019-07-24 stsp static struct got_histedit_cmd {
4657 0ebf8283 2019-07-24 stsp unsigned char code;
4658 0ebf8283 2019-07-24 stsp const char *name;
4659 0ebf8283 2019-07-24 stsp const char *desc;
4660 0ebf8283 2019-07-24 stsp } got_histedit_cmds[] = {
4661 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_PICK, "pick", "use commit" },
4662 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
4663 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_FOLD, "fold", "combine with commit below" },
4664 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
4665 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_MESG, "mesg",
4666 0ebf8283 2019-07-24 stsp "single-line log message for commit above (open editor if empty)" },
4669 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry {
4670 0ebf8283 2019-07-24 stsp TAILQ_ENTRY(got_histedit_list_entry) entry;
4671 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id;
4672 0ebf8283 2019-07-24 stsp const struct got_histedit_cmd *cmd;
4673 0ebf8283 2019-07-24 stsp char *logmsg;
4675 0ebf8283 2019-07-24 stsp TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
4677 0ebf8283 2019-07-24 stsp static const struct got_error *
4678 0ebf8283 2019-07-24 stsp histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
4679 0ebf8283 2019-07-24 stsp FILE *f, struct got_repository *repo)
4681 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
4682 0ebf8283 2019-07-24 stsp char *logmsg = NULL, *id_str = NULL;
4683 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
4686 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
4688 0ebf8283 2019-07-24 stsp goto done;
4690 0ebf8283 2019-07-24 stsp err = get_short_logmsg(&logmsg, 34, commit);
4692 0ebf8283 2019-07-24 stsp goto done;
4694 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, commit_id);
4696 0ebf8283 2019-07-24 stsp goto done;
4698 0ebf8283 2019-07-24 stsp n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
4699 0ebf8283 2019-07-24 stsp if (n < 0)
4700 0ebf8283 2019-07-24 stsp err = got_ferror(f, GOT_ERR_IO);
4702 0ebf8283 2019-07-24 stsp if (commit)
4703 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
4704 0ebf8283 2019-07-24 stsp free(id_str);
4705 0ebf8283 2019-07-24 stsp free(logmsg);
4706 0ebf8283 2019-07-24 stsp return err;
4709 0ebf8283 2019-07-24 stsp static const struct got_error *
4710 0ebf8283 2019-07-24 stsp histedit_write_commit_list(struct got_object_id_queue *commits, FILE *f,
4711 0ebf8283 2019-07-24 stsp struct got_repository *repo)
4713 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
4714 0ebf8283 2019-07-24 stsp struct got_object_qid *qid;
4716 0ebf8283 2019-07-24 stsp if (SIMPLEQ_EMPTY(commits))
4717 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_EMPTY_HISTEDIT);
4719 0ebf8283 2019-07-24 stsp SIMPLEQ_FOREACH(qid, commits, entry) {
4720 0ebf8283 2019-07-24 stsp err = histedit_write_commit(qid->id, got_histedit_cmds[0].name,
4726 0ebf8283 2019-07-24 stsp return err;
4729 0ebf8283 2019-07-24 stsp static const struct got_error *
4730 0ebf8283 2019-07-24 stsp write_cmd_list(FILE *f)
4732 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
4735 0ebf8283 2019-07-24 stsp n = fprintf(f, "# Available histedit commands:\n");
4736 0ebf8283 2019-07-24 stsp if (n < 0)
4737 0ebf8283 2019-07-24 stsp return got_ferror(f, GOT_ERR_IO);
4739 0ebf8283 2019-07-24 stsp for (i = 0; i < nitems(got_histedit_cmds); i++) {
4740 0ebf8283 2019-07-24 stsp struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
4741 0ebf8283 2019-07-24 stsp n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
4742 0ebf8283 2019-07-24 stsp cmd->desc);
4743 0ebf8283 2019-07-24 stsp if (n < 0) {
4744 0ebf8283 2019-07-24 stsp err = got_ferror(f, GOT_ERR_IO);
4748 0ebf8283 2019-07-24 stsp n = fprintf(f, "# Commits will be processed in order from top to "
4749 0ebf8283 2019-07-24 stsp "bottom of this file.\n");
4750 0ebf8283 2019-07-24 stsp if (n < 0)
4751 0ebf8283 2019-07-24 stsp return got_ferror(f, GOT_ERR_IO);
4752 0ebf8283 2019-07-24 stsp return err;
4755 0ebf8283 2019-07-24 stsp static const struct got_error *
4756 0ebf8283 2019-07-24 stsp histedit_syntax_error(int lineno)
4758 0ebf8283 2019-07-24 stsp static char msg[42];
4761 0ebf8283 2019-07-24 stsp ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
4763 0ebf8283 2019-07-24 stsp if (ret == -1 || ret >= sizeof(msg))
4764 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_HISTEDIT_SYNTAX);
4766 0ebf8283 2019-07-24 stsp return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
4769 0ebf8283 2019-07-24 stsp static const struct got_error *
4770 0ebf8283 2019-07-24 stsp append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
4771 0ebf8283 2019-07-24 stsp char *logmsg, struct got_repository *repo)
4773 0ebf8283 2019-07-24 stsp const struct got_error *err;
4774 0ebf8283 2019-07-24 stsp struct got_commit_object *folded_commit = NULL;
4775 5943eee2 2019-08-13 stsp char *id_str, *folded_logmsg = NULL;
4777 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, hle->commit_id);
4779 0ebf8283 2019-07-24 stsp return err;
4781 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
4783 0ebf8283 2019-07-24 stsp goto done;
4785 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
4787 5943eee2 2019-08-13 stsp goto done;
4788 0ebf8283 2019-07-24 stsp if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
4789 0ebf8283 2019-07-24 stsp logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
4790 5943eee2 2019-08-13 stsp folded_logmsg) == -1) {
4791 0ebf8283 2019-07-24 stsp err = got_error_from_errno("asprintf");
4792 0ebf8283 2019-07-24 stsp goto done;
4795 0ebf8283 2019-07-24 stsp if (folded_commit)
4796 0ebf8283 2019-07-24 stsp got_object_commit_close(folded_commit);
4797 0ebf8283 2019-07-24 stsp free(id_str);
4798 5943eee2 2019-08-13 stsp free(folded_logmsg);
4799 0ebf8283 2019-07-24 stsp return err;
4802 0ebf8283 2019-07-24 stsp static struct got_histedit_list_entry *
4803 0ebf8283 2019-07-24 stsp get_folded_commits(struct got_histedit_list_entry *hle)
4805 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *prev, *folded = NULL;
4807 0ebf8283 2019-07-24 stsp prev = TAILQ_PREV(hle, got_histedit_list, entry);
4808 3f9de99f 2019-07-24 stsp while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
4809 3f9de99f 2019-07-24 stsp prev->cmd->code == GOT_HISTEDIT_DROP)) {
4810 3f9de99f 2019-07-24 stsp if (prev->cmd->code == GOT_HISTEDIT_FOLD)
4811 3f9de99f 2019-07-24 stsp folded = prev;
4812 0ebf8283 2019-07-24 stsp prev = TAILQ_PREV(prev, got_histedit_list, entry);
4815 0ebf8283 2019-07-24 stsp return folded;
4818 0ebf8283 2019-07-24 stsp static const struct got_error *
4819 0ebf8283 2019-07-24 stsp histedit_edit_logmsg(struct got_histedit_list_entry *hle,
4820 0ebf8283 2019-07-24 stsp struct got_repository *repo)
4822 5943eee2 2019-08-13 stsp char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
4823 0ebf8283 2019-07-24 stsp char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
4824 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
4825 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
4827 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *folded = NULL;
4829 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&commit, repo, hle->commit_id);
4831 0ebf8283 2019-07-24 stsp return err;
4833 0ebf8283 2019-07-24 stsp folded = get_folded_commits(hle);
4834 0ebf8283 2019-07-24 stsp if (folded) {
4835 0ebf8283 2019-07-24 stsp while (folded != hle) {
4836 3f9de99f 2019-07-24 stsp if (folded->cmd->code == GOT_HISTEDIT_DROP) {
4837 3f9de99f 2019-07-24 stsp folded = TAILQ_NEXT(folded, entry);
4840 0ebf8283 2019-07-24 stsp err = append_folded_commit_msg(&new_msg, folded,
4841 0ebf8283 2019-07-24 stsp logmsg, repo);
4843 0ebf8283 2019-07-24 stsp goto done;
4844 0ebf8283 2019-07-24 stsp free(logmsg);
4845 0ebf8283 2019-07-24 stsp logmsg = new_msg;
4846 0ebf8283 2019-07-24 stsp folded = TAILQ_NEXT(folded, entry);
4850 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, hle->commit_id);
4852 0ebf8283 2019-07-24 stsp goto done;
4853 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&orig_logmsg, commit);
4855 5943eee2 2019-08-13 stsp goto done;
4856 0ebf8283 2019-07-24 stsp if (asprintf(&new_msg,
4857 0ebf8283 2019-07-24 stsp "%s\n# original log message of commit %s: %s",
4858 5943eee2 2019-08-13 stsp logmsg ? logmsg : "", id_str, orig_logmsg) == -1) {
4859 0ebf8283 2019-07-24 stsp err = got_error_from_errno("asprintf");
4860 0ebf8283 2019-07-24 stsp goto done;
4862 0ebf8283 2019-07-24 stsp free(logmsg);
4863 0ebf8283 2019-07-24 stsp logmsg = new_msg;
4865 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, hle->commit_id);
4867 0ebf8283 2019-07-24 stsp goto done;
4869 0ebf8283 2019-07-24 stsp err = got_opentemp_named_fd(&logmsg_path, &fd, "/tmp/got-logmsg");
4871 0ebf8283 2019-07-24 stsp goto done;
4873 0ebf8283 2019-07-24 stsp dprintf(fd, logmsg);
4874 0ebf8283 2019-07-24 stsp close(fd);
4876 0ebf8283 2019-07-24 stsp err = get_editor(&editor);
4878 0ebf8283 2019-07-24 stsp goto done;
4880 0ebf8283 2019-07-24 stsp err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg);
4881 0ebf8283 2019-07-24 stsp if (err) {
4882 0ebf8283 2019-07-24 stsp if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
4883 0ebf8283 2019-07-24 stsp goto done;
4884 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&hle->logmsg, commit);
4887 0ebf8283 2019-07-24 stsp if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
4888 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("unlink", logmsg_path);
4889 0ebf8283 2019-07-24 stsp free(logmsg_path);
4890 0ebf8283 2019-07-24 stsp free(logmsg);
4891 5943eee2 2019-08-13 stsp free(orig_logmsg);
4892 0ebf8283 2019-07-24 stsp free(editor);
4893 0ebf8283 2019-07-24 stsp if (commit)
4894 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
4895 0ebf8283 2019-07-24 stsp return err;
4898 0ebf8283 2019-07-24 stsp static const struct got_error *
4899 0ebf8283 2019-07-24 stsp histedit_parse_list(struct got_histedit_list *histedit_cmds,
4900 0ebf8283 2019-07-24 stsp FILE *f, struct got_repository *repo)
4902 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
4903 0ebf8283 2019-07-24 stsp char *line = NULL, *p, *end;
4904 0ebf8283 2019-07-24 stsp size_t size;
4905 0ebf8283 2019-07-24 stsp ssize_t len;
4906 0ebf8283 2019-07-24 stsp int lineno = 0, i;
4907 0ebf8283 2019-07-24 stsp const struct got_histedit_cmd *cmd;
4908 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id = NULL;
4909 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle = NULL;
4911 0ebf8283 2019-07-24 stsp for (;;) {
4912 0ebf8283 2019-07-24 stsp len = getline(&line, &size, f);
4913 0ebf8283 2019-07-24 stsp if (len == -1) {
4914 0ebf8283 2019-07-24 stsp const struct got_error *getline_err;
4915 0ebf8283 2019-07-24 stsp if (feof(f))
4917 0ebf8283 2019-07-24 stsp getline_err = got_error_from_errno("getline");
4918 0ebf8283 2019-07-24 stsp err = got_ferror(f, getline_err->code);
4923 0ebf8283 2019-07-24 stsp while (isspace((unsigned char)p[0]))
4925 0ebf8283 2019-07-24 stsp if (p[0] == '#' || p[0] == '\0') {
4926 0ebf8283 2019-07-24 stsp free(line);
4927 0ebf8283 2019-07-24 stsp line = NULL;
4930 0ebf8283 2019-07-24 stsp cmd = NULL;
4931 0ebf8283 2019-07-24 stsp for (i = 0; i < nitems(got_histedit_cmds); i++) {
4932 0ebf8283 2019-07-24 stsp cmd = &got_histedit_cmds[i];
4933 0ebf8283 2019-07-24 stsp if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
4934 0ebf8283 2019-07-24 stsp isspace((unsigned char)p[strlen(cmd->name)])) {
4935 0ebf8283 2019-07-24 stsp p += strlen(cmd->name);
4938 0ebf8283 2019-07-24 stsp if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
4943 6c1844f6 2019-07-25 stsp if (i == nitems(got_histedit_cmds)) {
4944 0ebf8283 2019-07-24 stsp err = histedit_syntax_error(lineno);
4947 0ebf8283 2019-07-24 stsp while (isspace((unsigned char)p[0]))
4949 0ebf8283 2019-07-24 stsp if (cmd->code == GOT_HISTEDIT_MESG) {
4950 0ebf8283 2019-07-24 stsp if (hle == NULL || hle->logmsg != NULL) {
4951 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_HISTEDIT_CMD);
4954 0ebf8283 2019-07-24 stsp if (p[0] == '\0') {
4955 0ebf8283 2019-07-24 stsp err = histedit_edit_logmsg(hle, repo);
4959 0ebf8283 2019-07-24 stsp hle->logmsg = strdup(p);
4960 0ebf8283 2019-07-24 stsp if (hle->logmsg == NULL) {
4961 0ebf8283 2019-07-24 stsp err = got_error_from_errno("strdup");
4965 0ebf8283 2019-07-24 stsp free(line);
4966 0ebf8283 2019-07-24 stsp line = NULL;
4970 0ebf8283 2019-07-24 stsp while (end[0] && !isspace((unsigned char)end[0]))
4972 0ebf8283 2019-07-24 stsp *end = '\0';
4974 0ebf8283 2019-07-24 stsp err = got_object_resolve_id_str(&commit_id, repo, p);
4975 0ebf8283 2019-07-24 stsp if (err) {
4976 0ebf8283 2019-07-24 stsp /* override error code */
4977 0ebf8283 2019-07-24 stsp err = histedit_syntax_error(lineno);
4981 0ebf8283 2019-07-24 stsp hle = malloc(sizeof(*hle));
4982 0ebf8283 2019-07-24 stsp if (hle == NULL) {
4983 0ebf8283 2019-07-24 stsp err = got_error_from_errno("malloc");
4986 0ebf8283 2019-07-24 stsp hle->cmd = cmd;
4987 0ebf8283 2019-07-24 stsp hle->commit_id = commit_id;
4988 0ebf8283 2019-07-24 stsp hle->logmsg = NULL;
4989 0ebf8283 2019-07-24 stsp commit_id = NULL;
4990 0ebf8283 2019-07-24 stsp free(line);
4991 0ebf8283 2019-07-24 stsp line = NULL;
4992 0ebf8283 2019-07-24 stsp TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
4995 0ebf8283 2019-07-24 stsp free(line);
4996 0ebf8283 2019-07-24 stsp free(commit_id);
4997 0ebf8283 2019-07-24 stsp return err;
5000 0ebf8283 2019-07-24 stsp static const struct got_error *
5001 bfce7f83 2019-07-27 stsp histedit_check_script(struct got_histedit_list *histedit_cmds,
5002 bfce7f83 2019-07-27 stsp struct got_object_id_queue *commits, struct got_repository *repo)
5004 bfce7f83 2019-07-27 stsp const struct got_error *err = NULL;
5005 bfce7f83 2019-07-27 stsp struct got_object_qid *qid;
5006 bfce7f83 2019-07-27 stsp struct got_histedit_list_entry *hle;
5007 bfce7f83 2019-07-27 stsp static char msg[80];
5008 bfce7f83 2019-07-27 stsp char *id_str;
5010 bfce7f83 2019-07-27 stsp if (TAILQ_EMPTY(histedit_cmds))
5011 bfce7f83 2019-07-27 stsp return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
5012 bfce7f83 2019-07-27 stsp "histedit script contains no commands");
5013 a0de39f3 2019-08-09 stsp if (SIMPLEQ_EMPTY(commits))
5014 a0de39f3 2019-08-09 stsp return got_error(GOT_ERR_EMPTY_HISTEDIT);
5016 bfce7f83 2019-07-27 stsp SIMPLEQ_FOREACH(qid, commits, entry) {
5017 bfce7f83 2019-07-27 stsp TAILQ_FOREACH(hle, histedit_cmds, entry) {
5018 bfce7f83 2019-07-27 stsp if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
5021 bfce7f83 2019-07-27 stsp if (hle == NULL) {
5022 bfce7f83 2019-07-27 stsp err = got_object_id_str(&id_str, qid->id);
5024 bfce7f83 2019-07-27 stsp return err;
5025 bfce7f83 2019-07-27 stsp snprintf(msg, sizeof(msg),
5026 bfce7f83 2019-07-27 stsp "commit %s missing from histedit script", id_str);
5027 bfce7f83 2019-07-27 stsp free(id_str);
5028 bfce7f83 2019-07-27 stsp return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
5032 0def28b1 2019-08-17 stsp hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
5033 0def28b1 2019-08-17 stsp if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
5034 bfce7f83 2019-07-27 stsp return got_error_msg(GOT_ERR_HISTEDIT_CMD,
5035 bfce7f83 2019-07-27 stsp "last commit in histedit script cannot be folded");
5037 bfce7f83 2019-07-27 stsp return NULL;
5040 bfce7f83 2019-07-27 stsp static const struct got_error *
5041 0ebf8283 2019-07-24 stsp histedit_run_editor(struct got_histedit_list *histedit_cmds,
5042 bfce7f83 2019-07-27 stsp const char *path, struct got_object_id_queue *commits,
5043 bfce7f83 2019-07-27 stsp struct got_repository *repo)
5045 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
5046 0ebf8283 2019-07-24 stsp char *editor;
5047 0ebf8283 2019-07-24 stsp FILE *f = NULL;
5049 0ebf8283 2019-07-24 stsp err = get_editor(&editor);
5051 0ebf8283 2019-07-24 stsp return err;
5053 0ebf8283 2019-07-24 stsp if (spawn_editor(editor, path) == -1) {
5054 0ebf8283 2019-07-24 stsp err = got_error_from_errno("failed spawning editor");
5055 0ebf8283 2019-07-24 stsp goto done;
5058 0ebf8283 2019-07-24 stsp f = fopen(path, "r");
5059 0ebf8283 2019-07-24 stsp if (f == NULL) {
5060 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fopen");
5061 0ebf8283 2019-07-24 stsp goto done;
5063 0ebf8283 2019-07-24 stsp err = histedit_parse_list(histedit_cmds, f, repo);
5065 bfce7f83 2019-07-27 stsp goto done;
5067 bfce7f83 2019-07-27 stsp err = histedit_check_script(histedit_cmds, commits, repo);
5069 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
5070 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
5071 0ebf8283 2019-07-24 stsp free(editor);
5072 0ebf8283 2019-07-24 stsp return err;
5075 0ebf8283 2019-07-24 stsp static const struct got_error *
5076 bfce7f83 2019-07-27 stsp histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
5077 0ebf8283 2019-07-24 stsp struct got_object_id_queue *, const char *, struct got_repository *);
5079 0ebf8283 2019-07-24 stsp static const struct got_error *
5080 0ebf8283 2019-07-24 stsp histedit_edit_script(struct got_histedit_list *histedit_cmds,
5081 0ebf8283 2019-07-24 stsp struct got_object_id_queue *commits, struct got_repository *repo)
5083 0ebf8283 2019-07-24 stsp const struct got_error *err;
5084 0ebf8283 2019-07-24 stsp FILE *f = NULL;
5085 0ebf8283 2019-07-24 stsp char *path = NULL;
5087 0ebf8283 2019-07-24 stsp err = got_opentemp_named(&path, &f, "got-histedit");
5089 0ebf8283 2019-07-24 stsp return err;
5091 0ebf8283 2019-07-24 stsp err = write_cmd_list(f);
5093 0ebf8283 2019-07-24 stsp goto done;
5095 0ebf8283 2019-07-24 stsp err = histedit_write_commit_list(commits, f, repo);
5097 0ebf8283 2019-07-24 stsp goto done;
5099 0ebf8283 2019-07-24 stsp if (fclose(f) != 0) {
5100 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
5101 0ebf8283 2019-07-24 stsp goto done;
5105 bfce7f83 2019-07-27 stsp err = histedit_run_editor(histedit_cmds, path, commits, repo);
5106 0ebf8283 2019-07-24 stsp if (err) {
5107 bfce7f83 2019-07-27 stsp if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
5108 bfce7f83 2019-07-27 stsp err->code != GOT_ERR_HISTEDIT_CMD)
5109 0ebf8283 2019-07-24 stsp goto done;
5110 bfce7f83 2019-07-27 stsp err = histedit_edit_list_retry(histedit_cmds, err,
5111 0ebf8283 2019-07-24 stsp commits, path, repo);
5114 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
5115 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
5116 0ebf8283 2019-07-24 stsp if (path && unlink(path) != 0 && err == NULL)
5117 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("unlink", path);
5118 0ebf8283 2019-07-24 stsp free(path);
5119 0ebf8283 2019-07-24 stsp return err;
5122 0ebf8283 2019-07-24 stsp static const struct got_error *
5123 0ebf8283 2019-07-24 stsp histedit_save_list(struct got_histedit_list *histedit_cmds,
5124 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
5126 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
5127 0ebf8283 2019-07-24 stsp char *path = NULL;
5128 0ebf8283 2019-07-24 stsp FILE *f = NULL;
5129 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle;
5130 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
5132 c3022ba5 2019-07-27 stsp err = got_worktree_get_histedit_script_path(&path, worktree);
5134 0ebf8283 2019-07-24 stsp return err;
5136 0ebf8283 2019-07-24 stsp f = fopen(path, "w");
5137 0ebf8283 2019-07-24 stsp if (f == NULL) {
5138 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("fopen", path);
5139 0ebf8283 2019-07-24 stsp goto done;
5141 0ebf8283 2019-07-24 stsp TAILQ_FOREACH(hle, histedit_cmds, entry) {
5142 0ebf8283 2019-07-24 stsp err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
5147 0ebf8283 2019-07-24 stsp if (hle->logmsg) {
5148 0ebf8283 2019-07-24 stsp int n = fprintf(f, "%c %s\n",
5149 0ebf8283 2019-07-24 stsp GOT_HISTEDIT_MESG, hle->logmsg);
5150 0ebf8283 2019-07-24 stsp if (n < 0) {
5151 0ebf8283 2019-07-24 stsp err = got_ferror(f, GOT_ERR_IO);
5157 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
5158 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
5159 0ebf8283 2019-07-24 stsp free(path);
5160 0ebf8283 2019-07-24 stsp if (commit)
5161 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
5162 0ebf8283 2019-07-24 stsp return err;
5166 bfce7f83 2019-07-27 stsp histedit_free_list(struct got_histedit_list *histedit_cmds)
5168 bfce7f83 2019-07-27 stsp struct got_histedit_list_entry *hle;
5170 bfce7f83 2019-07-27 stsp while ((hle = TAILQ_FIRST(histedit_cmds))) {
5171 bfce7f83 2019-07-27 stsp TAILQ_REMOVE(histedit_cmds, hle, entry);
5172 bfce7f83 2019-07-27 stsp free(hle);
5176 0ebf8283 2019-07-24 stsp static const struct got_error *
5177 0ebf8283 2019-07-24 stsp histedit_load_list(struct got_histedit_list *histedit_cmds,
5178 0ebf8283 2019-07-24 stsp const char *path, struct got_repository *repo)
5180 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
5181 0ebf8283 2019-07-24 stsp FILE *f = NULL;
5183 0ebf8283 2019-07-24 stsp f = fopen(path, "r");
5184 0ebf8283 2019-07-24 stsp if (f == NULL) {
5185 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("fopen", path);
5186 0ebf8283 2019-07-24 stsp goto done;
5189 0ebf8283 2019-07-24 stsp err = histedit_parse_list(histedit_cmds, f, repo);
5191 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
5192 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
5193 0ebf8283 2019-07-24 stsp return err;
5196 0ebf8283 2019-07-24 stsp static const struct got_error *
5197 0ebf8283 2019-07-24 stsp histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
5198 bfce7f83 2019-07-27 stsp const struct got_error *edit_err, struct got_object_id_queue *commits,
5199 0ebf8283 2019-07-24 stsp const char *path, struct got_repository *repo)
5201 bfce7f83 2019-07-27 stsp const struct got_error *err = NULL, *prev_err = edit_err;
5202 0ebf8283 2019-07-24 stsp int resp = ' ';
5204 b006047e 2019-07-25 stsp while (resp != 'c' && resp != 'r' && resp != 'a') {
5205 0ebf8283 2019-07-24 stsp printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
5206 bfce7f83 2019-07-27 stsp "or (a)bort: ", getprogname(), prev_err->msg);
5207 0ebf8283 2019-07-24 stsp resp = getchar();
5208 a61a4414 2019-08-07 stsp if (resp == '\n')
5209 a61a4414 2019-08-07 stsp resp = getchar();
5210 426ebf2e 2019-07-25 stsp if (resp == 'c') {
5211 bfce7f83 2019-07-27 stsp histedit_free_list(histedit_cmds);
5212 bfce7f83 2019-07-27 stsp err = histedit_run_editor(histedit_cmds, path, commits,
5214 426ebf2e 2019-07-25 stsp if (err) {
5215 bfce7f83 2019-07-27 stsp if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
5216 bfce7f83 2019-07-27 stsp err->code != GOT_ERR_HISTEDIT_CMD)
5218 bfce7f83 2019-07-27 stsp prev_err = err;
5219 426ebf2e 2019-07-25 stsp resp = ' ';
5223 426ebf2e 2019-07-25 stsp } else if (resp == 'r') {
5224 bfce7f83 2019-07-27 stsp histedit_free_list(histedit_cmds);
5225 0ebf8283 2019-07-24 stsp err = histedit_edit_script(histedit_cmds,
5226 0ebf8283 2019-07-24 stsp commits, repo);
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 == 'a') {
5237 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_HISTEDIT_CANCEL);
5240 0ebf8283 2019-07-24 stsp printf("invalid response '%c'\n", resp);
5243 0ebf8283 2019-07-24 stsp return err;
5246 0ebf8283 2019-07-24 stsp static const struct got_error *
5247 0ebf8283 2019-07-24 stsp histedit_complete(struct got_worktree *worktree,
5248 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
5249 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_repository *repo)
5251 0ebf8283 2019-07-24 stsp printf("Switching work tree to %s\n",
5252 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch));
5253 3e3a69f1 2019-07-25 stsp return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
5254 3e3a69f1 2019-07-25 stsp branch, repo);
5257 0ebf8283 2019-07-24 stsp static const struct got_error *
5258 0ebf8283 2019-07-24 stsp show_histedit_progress(struct got_commit_object *commit,
5259 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle, struct got_object_id *new_id)
5261 0ebf8283 2019-07-24 stsp const struct got_error *err;
5262 0ebf8283 2019-07-24 stsp char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
5264 0ebf8283 2019-07-24 stsp err = got_object_id_str(&old_id_str, hle->commit_id);
5266 0ebf8283 2019-07-24 stsp goto done;
5268 0ebf8283 2019-07-24 stsp if (new_id) {
5269 0ebf8283 2019-07-24 stsp err = got_object_id_str(&new_id_str, new_id);
5271 0ebf8283 2019-07-24 stsp goto done;
5274 0ebf8283 2019-07-24 stsp old_id_str[12] = '\0';
5275 0ebf8283 2019-07-24 stsp if (new_id_str)
5276 0ebf8283 2019-07-24 stsp new_id_str[12] = '\0';
5278 0ebf8283 2019-07-24 stsp if (hle->logmsg) {
5279 0ebf8283 2019-07-24 stsp logmsg = strdup(hle->logmsg);
5280 0ebf8283 2019-07-24 stsp if (logmsg == NULL) {
5281 0ebf8283 2019-07-24 stsp err = got_error_from_errno("strdup");
5282 0ebf8283 2019-07-24 stsp goto done;
5284 0ebf8283 2019-07-24 stsp trim_logmsg(logmsg, 42);
5286 0ebf8283 2019-07-24 stsp err = get_short_logmsg(&logmsg, 42, commit);
5288 0ebf8283 2019-07-24 stsp goto done;
5291 0ebf8283 2019-07-24 stsp switch (hle->cmd->code) {
5292 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_PICK:
5293 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_EDIT:
5294 0ebf8283 2019-07-24 stsp printf("%s -> %s: %s\n", old_id_str,
5295 0ebf8283 2019-07-24 stsp new_id_str ? new_id_str : "no-op change", logmsg);
5297 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_DROP:
5298 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_FOLD:
5299 0ebf8283 2019-07-24 stsp printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
5307 0ebf8283 2019-07-24 stsp free(old_id_str);
5308 0ebf8283 2019-07-24 stsp free(new_id_str);
5309 0ebf8283 2019-07-24 stsp return err;
5312 0ebf8283 2019-07-24 stsp static const struct got_error *
5313 0ebf8283 2019-07-24 stsp histedit_commit(struct got_pathlist_head *merged_paths,
5314 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
5315 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
5316 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
5318 0ebf8283 2019-07-24 stsp const struct got_error *err;
5319 0ebf8283 2019-07-24 stsp struct got_commit_object *commit;
5320 0ebf8283 2019-07-24 stsp struct got_object_id *new_commit_id;
5322 0ebf8283 2019-07-24 stsp if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
5323 0ebf8283 2019-07-24 stsp && hle->logmsg == NULL) {
5324 0ebf8283 2019-07-24 stsp err = histedit_edit_logmsg(hle, repo);
5326 0ebf8283 2019-07-24 stsp return err;
5329 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&commit, repo, hle->commit_id);
5331 0ebf8283 2019-07-24 stsp return err;
5333 0ebf8283 2019-07-24 stsp err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
5334 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, commit, hle->commit_id,
5335 3e3a69f1 2019-07-25 stsp hle->logmsg, repo);
5336 0ebf8283 2019-07-24 stsp if (err) {
5337 0ebf8283 2019-07-24 stsp if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
5338 0ebf8283 2019-07-24 stsp goto done;
5339 0ebf8283 2019-07-24 stsp err = show_histedit_progress(commit, hle, NULL);
5341 0ebf8283 2019-07-24 stsp err = show_histedit_progress(commit, hle, new_commit_id);
5342 0ebf8283 2019-07-24 stsp free(new_commit_id);
5345 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
5346 0ebf8283 2019-07-24 stsp return err;
5349 0ebf8283 2019-07-24 stsp static const struct got_error *
5350 0ebf8283 2019-07-24 stsp histedit_skip_commit(struct got_histedit_list_entry *hle,
5351 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
5353 0ebf8283 2019-07-24 stsp const struct got_error *error;
5354 0ebf8283 2019-07-24 stsp struct got_commit_object *commit;
5356 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
5358 0ebf8283 2019-07-24 stsp if (error)
5359 0ebf8283 2019-07-24 stsp return error;
5361 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo, hle->commit_id);
5362 0ebf8283 2019-07-24 stsp if (error)
5363 0ebf8283 2019-07-24 stsp return error;
5365 0ebf8283 2019-07-24 stsp error = show_histedit_progress(commit, hle, NULL);
5366 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
5367 0ebf8283 2019-07-24 stsp return error;
5370 0ebf8283 2019-07-24 stsp static const struct got_error *
5371 0ebf8283 2019-07-24 stsp cmd_histedit(int argc, char *argv[])
5373 0ebf8283 2019-07-24 stsp const struct got_error *error = NULL;
5374 0ebf8283 2019-07-24 stsp struct got_worktree *worktree = NULL;
5375 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex = NULL;
5376 0ebf8283 2019-07-24 stsp struct got_repository *repo = NULL;
5377 0ebf8283 2019-07-24 stsp char *cwd = NULL;
5378 0ebf8283 2019-07-24 stsp struct got_reference *branch = NULL;
5379 0ebf8283 2019-07-24 stsp struct got_reference *tmp_branch = NULL;
5380 0ebf8283 2019-07-24 stsp struct got_object_id *resume_commit_id = NULL;
5381 0ebf8283 2019-07-24 stsp struct got_object_id *base_commit_id = NULL;
5382 0ebf8283 2019-07-24 stsp struct got_object_id *head_commit_id = NULL;
5383 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
5384 6ba2bea2 2019-07-27 stsp int ch, rebase_in_progress = 0, did_something;
5385 0ebf8283 2019-07-24 stsp int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
5386 0ebf8283 2019-07-24 stsp const char *edit_script_path = NULL;
5387 0ebf8283 2019-07-24 stsp unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
5388 0ebf8283 2019-07-24 stsp struct got_object_id_queue commits;
5389 0ebf8283 2019-07-24 stsp struct got_pathlist_head merged_paths;
5390 0ebf8283 2019-07-24 stsp const struct got_object_id_queue *parent_ids;
5391 0ebf8283 2019-07-24 stsp struct got_object_qid *pid;
5392 0ebf8283 2019-07-24 stsp struct got_histedit_list histedit_cmds;
5393 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle;
5395 0ebf8283 2019-07-24 stsp SIMPLEQ_INIT(&commits);
5396 0ebf8283 2019-07-24 stsp TAILQ_INIT(&histedit_cmds);
5397 0ebf8283 2019-07-24 stsp TAILQ_INIT(&merged_paths);
5399 0ebf8283 2019-07-24 stsp while ((ch = getopt(argc, argv, "acF:")) != -1) {
5400 0ebf8283 2019-07-24 stsp switch (ch) {
5402 0ebf8283 2019-07-24 stsp abort_edit = 1;
5405 0ebf8283 2019-07-24 stsp continue_edit = 1;
5408 0ebf8283 2019-07-24 stsp edit_script_path = optarg;
5411 0ebf8283 2019-07-24 stsp usage_histedit();
5412 0ebf8283 2019-07-24 stsp /* NOTREACHED */
5416 0ebf8283 2019-07-24 stsp argc -= optind;
5417 0ebf8283 2019-07-24 stsp argv += optind;
5419 0ebf8283 2019-07-24 stsp #ifndef PROFILE
5420 0ebf8283 2019-07-24 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5421 0ebf8283 2019-07-24 stsp "unveil", NULL) == -1)
5422 0ebf8283 2019-07-24 stsp err(1, "pledge");
5424 0ebf8283 2019-07-24 stsp if (abort_edit && continue_edit)
5425 0ebf8283 2019-07-24 stsp usage_histedit();
5426 0ebf8283 2019-07-24 stsp if (argc != 0)
5427 0ebf8283 2019-07-24 stsp usage_histedit();
5430 0ebf8283 2019-07-24 stsp * This command cannot apply unveil(2) in all cases because the
5431 0ebf8283 2019-07-24 stsp * user may choose to run an editor to edit the histedit script
5432 0ebf8283 2019-07-24 stsp * and to edit individual commit log messages.
5433 0ebf8283 2019-07-24 stsp * unveil(2) traverses exec(2); if an editor is used we have to
5434 0ebf8283 2019-07-24 stsp * apply unveil after edit script and log messages have been written.
5435 0ebf8283 2019-07-24 stsp * XXX TODO: Make use of unveil(2) where possible.
5438 0ebf8283 2019-07-24 stsp cwd = getcwd(NULL, 0);
5439 0ebf8283 2019-07-24 stsp if (cwd == NULL) {
5440 0ebf8283 2019-07-24 stsp error = got_error_from_errno("getcwd");
5441 0ebf8283 2019-07-24 stsp goto done;
5443 0ebf8283 2019-07-24 stsp error = got_worktree_open(&worktree, cwd);
5444 0ebf8283 2019-07-24 stsp if (error)
5445 0ebf8283 2019-07-24 stsp goto done;
5447 0ebf8283 2019-07-24 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
5448 0ebf8283 2019-07-24 stsp if (error != NULL)
5449 0ebf8283 2019-07-24 stsp goto done;
5451 0ebf8283 2019-07-24 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
5452 0ebf8283 2019-07-24 stsp if (error)
5453 0ebf8283 2019-07-24 stsp goto done;
5454 0ebf8283 2019-07-24 stsp if (rebase_in_progress) {
5455 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_REBASING);
5456 0ebf8283 2019-07-24 stsp goto done;
5459 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
5460 0ebf8283 2019-07-24 stsp if (error)
5461 0ebf8283 2019-07-24 stsp goto done;
5463 0ebf8283 2019-07-24 stsp if (edit_in_progress && abort_edit) {
5464 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_continue(&resume_commit_id,
5465 3e3a69f1 2019-07-25 stsp &tmp_branch, &branch, &base_commit_id, &fileindex,
5466 3e3a69f1 2019-07-25 stsp worktree, repo);
5467 0ebf8283 2019-07-24 stsp if (error)
5468 0ebf8283 2019-07-24 stsp goto done;
5469 0ebf8283 2019-07-24 stsp printf("Switching work tree to %s\n",
5470 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch));
5471 3e3a69f1 2019-07-25 stsp error = got_worktree_histedit_abort(worktree, fileindex, repo,
5472 0ebf8283 2019-07-24 stsp branch, base_commit_id, update_progress, &did_something);
5473 0ebf8283 2019-07-24 stsp if (error)
5474 0ebf8283 2019-07-24 stsp goto done;
5475 0ebf8283 2019-07-24 stsp printf("Histedit of %s aborted\n",
5476 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch));
5477 0ebf8283 2019-07-24 stsp goto done; /* nothing else to do */
5478 0ebf8283 2019-07-24 stsp } else if (abort_edit) {
5479 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_NOT_HISTEDIT);
5480 0ebf8283 2019-07-24 stsp goto done;
5483 0ebf8283 2019-07-24 stsp if (continue_edit) {
5484 0ebf8283 2019-07-24 stsp char *path;
5486 0ebf8283 2019-07-24 stsp if (!edit_in_progress) {
5487 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_NOT_HISTEDIT);
5488 0ebf8283 2019-07-24 stsp goto done;
5491 c3022ba5 2019-07-27 stsp error = got_worktree_get_histedit_script_path(&path, worktree);
5492 0ebf8283 2019-07-24 stsp if (error)
5493 0ebf8283 2019-07-24 stsp goto done;
5495 0ebf8283 2019-07-24 stsp error = histedit_load_list(&histedit_cmds, path, repo);
5496 0ebf8283 2019-07-24 stsp free(path);
5497 0ebf8283 2019-07-24 stsp if (error)
5498 0ebf8283 2019-07-24 stsp goto done;
5500 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_continue(&resume_commit_id,
5501 3e3a69f1 2019-07-25 stsp &tmp_branch, &branch, &base_commit_id, &fileindex,
5502 3e3a69f1 2019-07-25 stsp worktree, repo);
5503 0ebf8283 2019-07-24 stsp if (error)
5504 0ebf8283 2019-07-24 stsp goto done;
5506 0ebf8283 2019-07-24 stsp error = got_ref_resolve(&head_commit_id, repo, branch);
5507 0ebf8283 2019-07-24 stsp if (error)
5508 0ebf8283 2019-07-24 stsp goto done;
5510 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo,
5511 0ebf8283 2019-07-24 stsp head_commit_id);
5512 0ebf8283 2019-07-24 stsp if (error)
5513 0ebf8283 2019-07-24 stsp goto done;
5514 0ebf8283 2019-07-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
5515 0ebf8283 2019-07-24 stsp pid = SIMPLEQ_FIRST(parent_ids);
5516 3aac7cf7 2019-07-25 stsp if (pid == NULL) {
5517 3aac7cf7 2019-07-25 stsp error = got_error(GOT_ERR_EMPTY_HISTEDIT);
5518 3aac7cf7 2019-07-25 stsp goto done;
5520 8ca9bd68 2019-07-25 stsp error = collect_commits(&commits, head_commit_id, pid->id,
5521 8ca9bd68 2019-07-25 stsp base_commit_id, got_worktree_get_path_prefix(worktree),
5522 8ca9bd68 2019-07-25 stsp GOT_ERR_HISTEDIT_PATH, repo);
5523 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
5524 0ebf8283 2019-07-24 stsp commit = NULL;
5525 0ebf8283 2019-07-24 stsp if (error)
5526 0ebf8283 2019-07-24 stsp goto done;
5528 0ebf8283 2019-07-24 stsp if (edit_in_progress) {
5529 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_HISTEDIT_BUSY);
5530 0ebf8283 2019-07-24 stsp goto done;
5533 0ebf8283 2019-07-24 stsp error = got_ref_open(&branch, repo,
5534 0ebf8283 2019-07-24 stsp got_worktree_get_head_ref_name(worktree), 0);
5535 0ebf8283 2019-07-24 stsp if (error != NULL)
5536 0ebf8283 2019-07-24 stsp goto done;
5538 c7d20a3f 2019-07-30 stsp if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
5539 c7d20a3f 2019-07-30 stsp error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
5540 c7d20a3f 2019-07-30 stsp "will not edit commit history of a branch outside "
5541 c7d20a3f 2019-07-30 stsp "the \"refs/heads/\" reference namespace");
5542 c7d20a3f 2019-07-30 stsp goto done;
5545 0ebf8283 2019-07-24 stsp error = got_ref_resolve(&head_commit_id, repo, branch);
5546 bfce7f83 2019-07-27 stsp got_ref_close(branch);
5547 bfce7f83 2019-07-27 stsp branch = NULL;
5548 0ebf8283 2019-07-24 stsp if (error)
5549 0ebf8283 2019-07-24 stsp goto done;
5551 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo,
5552 0ebf8283 2019-07-24 stsp head_commit_id);
5553 0ebf8283 2019-07-24 stsp if (error)
5554 0ebf8283 2019-07-24 stsp goto done;
5555 0ebf8283 2019-07-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
5556 0ebf8283 2019-07-24 stsp pid = SIMPLEQ_FIRST(parent_ids);
5557 3aac7cf7 2019-07-25 stsp if (pid == NULL) {
5558 3aac7cf7 2019-07-25 stsp error = got_error(GOT_ERR_EMPTY_HISTEDIT);
5559 3aac7cf7 2019-07-25 stsp goto done;
5561 8ca9bd68 2019-07-25 stsp error = collect_commits(&commits, head_commit_id, pid->id,
5562 8ca9bd68 2019-07-25 stsp got_worktree_get_base_commit_id(worktree),
5563 8ca9bd68 2019-07-25 stsp got_worktree_get_path_prefix(worktree),
5564 8ca9bd68 2019-07-25 stsp GOT_ERR_HISTEDIT_PATH, repo);
5565 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
5566 0ebf8283 2019-07-24 stsp commit = NULL;
5567 0ebf8283 2019-07-24 stsp if (error)
5568 0ebf8283 2019-07-24 stsp goto done;
5570 bfce7f83 2019-07-27 stsp error = got_worktree_histedit_prepare(&tmp_branch, &branch,
5571 bfce7f83 2019-07-27 stsp &base_commit_id, &fileindex, worktree, repo);
5572 bfce7f83 2019-07-27 stsp if (error)
5573 bfce7f83 2019-07-27 stsp goto done;
5575 0ebf8283 2019-07-24 stsp if (edit_script_path) {
5576 0ebf8283 2019-07-24 stsp error = histedit_load_list(&histedit_cmds,
5577 0ebf8283 2019-07-24 stsp edit_script_path, repo);
5578 6ba2bea2 2019-07-27 stsp if (error) {
5579 6ba2bea2 2019-07-27 stsp got_worktree_histedit_abort(worktree, fileindex,
5580 6ba2bea2 2019-07-27 stsp repo, branch, base_commit_id,
5581 6ba2bea2 2019-07-27 stsp update_progress, &did_something);
5582 0ebf8283 2019-07-24 stsp goto done;
5585 0ebf8283 2019-07-24 stsp error = histedit_edit_script(&histedit_cmds, &commits,
5587 6ba2bea2 2019-07-27 stsp if (error) {
5588 6ba2bea2 2019-07-27 stsp got_worktree_histedit_abort(worktree, fileindex,
5589 6ba2bea2 2019-07-27 stsp repo, branch, base_commit_id,
5590 6ba2bea2 2019-07-27 stsp update_progress, &did_something);
5591 0ebf8283 2019-07-24 stsp goto done;
5596 0ebf8283 2019-07-24 stsp error = histedit_save_list(&histedit_cmds, worktree,
5598 6ba2bea2 2019-07-27 stsp if (error) {
5599 6ba2bea2 2019-07-27 stsp got_worktree_histedit_abort(worktree, fileindex,
5600 6ba2bea2 2019-07-27 stsp repo, branch, base_commit_id,
5601 6ba2bea2 2019-07-27 stsp update_progress, &did_something);
5602 0ebf8283 2019-07-24 stsp goto done;
5607 0ebf8283 2019-07-24 stsp error = histedit_check_script(&histedit_cmds, &commits, repo);
5608 0ebf8283 2019-07-24 stsp if (error)
5609 0ebf8283 2019-07-24 stsp goto done;
5611 0ebf8283 2019-07-24 stsp TAILQ_FOREACH(hle, &histedit_cmds, entry) {
5612 0ebf8283 2019-07-24 stsp if (resume_commit_id) {
5613 0ebf8283 2019-07-24 stsp if (got_object_id_cmp(hle->commit_id,
5614 0ebf8283 2019-07-24 stsp resume_commit_id) != 0)
5617 0ebf8283 2019-07-24 stsp resume_commit_id = NULL;
5618 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_DROP ||
5619 0ebf8283 2019-07-24 stsp hle->cmd->code == GOT_HISTEDIT_FOLD) {
5620 0ebf8283 2019-07-24 stsp error = histedit_skip_commit(hle, worktree,
5623 0ebf8283 2019-07-24 stsp error = histedit_commit(NULL, worktree,
5624 3e3a69f1 2019-07-25 stsp fileindex, tmp_branch, hle, repo);
5626 0ebf8283 2019-07-24 stsp if (error)
5627 0ebf8283 2019-07-24 stsp goto done;
5631 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_DROP) {
5632 0ebf8283 2019-07-24 stsp error = histedit_skip_commit(hle, worktree, repo);
5633 0ebf8283 2019-07-24 stsp if (error)
5634 0ebf8283 2019-07-24 stsp goto done;
5638 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo,
5639 0ebf8283 2019-07-24 stsp hle->commit_id);
5640 0ebf8283 2019-07-24 stsp if (error)
5641 0ebf8283 2019-07-24 stsp goto done;
5642 0ebf8283 2019-07-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
5643 0ebf8283 2019-07-24 stsp pid = SIMPLEQ_FIRST(parent_ids);
5645 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_merge_files(&merged_paths,
5646 3e3a69f1 2019-07-25 stsp worktree, fileindex, pid->id, hle->commit_id, repo,
5647 3e3a69f1 2019-07-25 stsp rebase_progress, &rebase_status, check_cancelled, NULL);
5648 0ebf8283 2019-07-24 stsp if (error)
5649 0ebf8283 2019-07-24 stsp goto done;
5650 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
5651 0ebf8283 2019-07-24 stsp commit = NULL;
5653 0ebf8283 2019-07-24 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
5654 0ebf8283 2019-07-24 stsp got_worktree_rebase_pathlist_free(&merged_paths);
5658 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
5659 0ebf8283 2019-07-24 stsp char *id_str;
5660 0ebf8283 2019-07-24 stsp error = got_object_id_str(&id_str, hle->commit_id);
5661 0ebf8283 2019-07-24 stsp if (error)
5662 0ebf8283 2019-07-24 stsp goto done;
5663 0ebf8283 2019-07-24 stsp printf("Stopping histedit for amending commit %s\n",
5665 0ebf8283 2019-07-24 stsp free(id_str);
5666 0ebf8283 2019-07-24 stsp got_worktree_rebase_pathlist_free(&merged_paths);
5667 3e3a69f1 2019-07-25 stsp error = got_worktree_histedit_postpone(worktree,
5668 3e3a69f1 2019-07-25 stsp fileindex);
5669 0ebf8283 2019-07-24 stsp goto done;
5672 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
5673 0ebf8283 2019-07-24 stsp error = histedit_skip_commit(hle, worktree, repo);
5674 0ebf8283 2019-07-24 stsp if (error)
5675 0ebf8283 2019-07-24 stsp goto done;
5679 3e3a69f1 2019-07-25 stsp error = histedit_commit(&merged_paths, worktree, fileindex,
5680 3e3a69f1 2019-07-25 stsp tmp_branch, hle, repo);
5681 0ebf8283 2019-07-24 stsp got_worktree_rebase_pathlist_free(&merged_paths);
5682 0ebf8283 2019-07-24 stsp if (error)
5683 0ebf8283 2019-07-24 stsp goto done;
5686 0ebf8283 2019-07-24 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
5687 3e3a69f1 2019-07-25 stsp error = got_worktree_histedit_postpone(worktree, fileindex);
5688 0ebf8283 2019-07-24 stsp if (error)
5689 0ebf8283 2019-07-24 stsp goto done;
5690 0ebf8283 2019-07-24 stsp error = got_error_msg(GOT_ERR_CONFLICTS,
5691 0ebf8283 2019-07-24 stsp "conflicts must be resolved before rebasing can continue");
5693 3e3a69f1 2019-07-25 stsp error = histedit_complete(worktree, fileindex, tmp_branch,
5694 3e3a69f1 2019-07-25 stsp branch, repo);
5696 0ebf8283 2019-07-24 stsp got_object_id_queue_free(&commits);
5697 bfce7f83 2019-07-27 stsp histedit_free_list(&histedit_cmds);
5698 0ebf8283 2019-07-24 stsp free(head_commit_id);
5699 0ebf8283 2019-07-24 stsp free(base_commit_id);
5700 0ebf8283 2019-07-24 stsp free(resume_commit_id);
5701 0ebf8283 2019-07-24 stsp if (commit)
5702 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
5703 0ebf8283 2019-07-24 stsp if (branch)
5704 0ebf8283 2019-07-24 stsp got_ref_close(branch);
5705 0ebf8283 2019-07-24 stsp if (tmp_branch)
5706 0ebf8283 2019-07-24 stsp got_ref_close(tmp_branch);
5707 0ebf8283 2019-07-24 stsp if (worktree)
5708 0ebf8283 2019-07-24 stsp got_worktree_close(worktree);
5710 715dc77e 2019-08-03 stsp got_repo_close(repo);
5711 715dc77e 2019-08-03 stsp return error;
5714 715dc77e 2019-08-03 stsp __dead static void
5715 715dc77e 2019-08-03 stsp usage_stage(void)
5717 f3055044 2019-08-07 stsp fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
5718 f3055044 2019-08-07 stsp "[file-path ...]\n",
5719 715dc77e 2019-08-03 stsp getprogname());
5723 715dc77e 2019-08-03 stsp static const struct got_error *
5724 408b4ebc 2019-08-03 stsp print_stage(void *arg, unsigned char status, unsigned char staged_status,
5725 408b4ebc 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
5726 408b4ebc 2019-08-03 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
5728 408b4ebc 2019-08-03 stsp const struct got_error *err = NULL;
5729 408b4ebc 2019-08-03 stsp char *id_str = NULL;
5731 408b4ebc 2019-08-03 stsp if (staged_status != GOT_STATUS_ADD &&
5732 408b4ebc 2019-08-03 stsp staged_status != GOT_STATUS_MODIFY &&
5733 408b4ebc 2019-08-03 stsp staged_status != GOT_STATUS_DELETE)
5734 408b4ebc 2019-08-03 stsp return NULL;
5736 408b4ebc 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
5737 408b4ebc 2019-08-03 stsp staged_status == GOT_STATUS_MODIFY)
5738 408b4ebc 2019-08-03 stsp err = got_object_id_str(&id_str, staged_blob_id);
5740 408b4ebc 2019-08-03 stsp err = got_object_id_str(&id_str, blob_id);
5742 408b4ebc 2019-08-03 stsp return err;
5744 408b4ebc 2019-08-03 stsp printf("%s %c %s\n", id_str, staged_status, path);
5745 408b4ebc 2019-08-03 stsp free(id_str);
5746 dc424a06 2019-08-07 stsp return NULL;
5749 dc424a06 2019-08-07 stsp static const struct got_error *
5750 715dc77e 2019-08-03 stsp cmd_stage(int argc, char *argv[])
5752 715dc77e 2019-08-03 stsp const struct got_error *error = NULL;
5753 715dc77e 2019-08-03 stsp struct got_repository *repo = NULL;
5754 715dc77e 2019-08-03 stsp struct got_worktree *worktree = NULL;
5755 715dc77e 2019-08-03 stsp char *cwd = NULL;
5756 715dc77e 2019-08-03 stsp struct got_pathlist_head paths;
5757 715dc77e 2019-08-03 stsp struct got_pathlist_entry *pe;
5758 dc424a06 2019-08-07 stsp int ch, list_stage = 0, pflag = 0;
5759 dc424a06 2019-08-07 stsp FILE *patch_script_file = NULL;
5760 2e1f37b0 2019-08-08 stsp const char *patch_script_path = NULL;
5761 2e1f37b0 2019-08-08 stsp struct choose_patch_arg cpa;
5763 715dc77e 2019-08-03 stsp TAILQ_INIT(&paths);
5765 dc424a06 2019-08-07 stsp while ((ch = getopt(argc, argv, "lpF:")) != -1) {
5766 715dc77e 2019-08-03 stsp switch (ch) {
5768 408b4ebc 2019-08-03 stsp list_stage = 1;
5771 dc424a06 2019-08-07 stsp pflag = 1;
5774 dc424a06 2019-08-07 stsp patch_script_path = optarg;
5777 715dc77e 2019-08-03 stsp usage_stage();
5778 715dc77e 2019-08-03 stsp /* NOTREACHED */
5782 715dc77e 2019-08-03 stsp argc -= optind;
5783 715dc77e 2019-08-03 stsp argv += optind;
5785 715dc77e 2019-08-03 stsp #ifndef PROFILE
5786 715dc77e 2019-08-03 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5787 715dc77e 2019-08-03 stsp "unveil", NULL) == -1)
5788 715dc77e 2019-08-03 stsp err(1, "pledge");
5790 2db2652d 2019-08-07 stsp if (list_stage && (pflag || patch_script_path))
5791 2db2652d 2019-08-07 stsp errx(1, "-l option cannot be used with other options");
5792 dc424a06 2019-08-07 stsp if (patch_script_path && !pflag)
5793 dc424a06 2019-08-07 stsp errx(1, "-F option can only be used together with -p option");
5795 715dc77e 2019-08-03 stsp cwd = getcwd(NULL, 0);
5796 715dc77e 2019-08-03 stsp if (cwd == NULL) {
5797 715dc77e 2019-08-03 stsp error = got_error_from_errno("getcwd");
5798 715dc77e 2019-08-03 stsp goto done;
5801 715dc77e 2019-08-03 stsp error = got_worktree_open(&worktree, cwd);
5802 715dc77e 2019-08-03 stsp if (error)
5803 715dc77e 2019-08-03 stsp goto done;
5805 715dc77e 2019-08-03 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
5806 715dc77e 2019-08-03 stsp if (error != NULL)
5807 715dc77e 2019-08-03 stsp goto done;
5809 dc424a06 2019-08-07 stsp if (patch_script_path) {
5810 dc424a06 2019-08-07 stsp patch_script_file = fopen(patch_script_path, "r");
5811 dc424a06 2019-08-07 stsp if (patch_script_file == NULL) {
5812 dc424a06 2019-08-07 stsp error = got_error_from_errno2("fopen",
5813 dc424a06 2019-08-07 stsp patch_script_path);
5814 dc424a06 2019-08-07 stsp goto done;
5817 715dc77e 2019-08-03 stsp error = apply_unveil(got_repo_get_path(repo), 1,
5818 715dc77e 2019-08-03 stsp got_worktree_get_root_path(worktree));
5819 715dc77e 2019-08-03 stsp if (error)
5820 715dc77e 2019-08-03 stsp goto done;
5822 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5823 6d022e97 2019-08-04 stsp if (error)
5824 6d022e97 2019-08-04 stsp goto done;
5826 6d022e97 2019-08-04 stsp if (list_stage)
5827 408b4ebc 2019-08-03 stsp error = got_worktree_status(worktree, &paths, repo,
5828 408b4ebc 2019-08-03 stsp print_stage, NULL, check_cancelled, NULL);
5830 2e1f37b0 2019-08-08 stsp cpa.patch_script_file = patch_script_file;
5831 2e1f37b0 2019-08-08 stsp cpa.action = "stage";
5832 dc424a06 2019-08-07 stsp error = got_worktree_stage(worktree, &paths,
5833 dc424a06 2019-08-07 stsp pflag ? NULL : print_status, NULL,
5834 2e1f37b0 2019-08-08 stsp pflag ? choose_patch : NULL, &cpa, repo);
5837 2e1f37b0 2019-08-08 stsp if (patch_script_file && fclose(patch_script_file) == EOF &&
5838 2e1f37b0 2019-08-08 stsp error == NULL)
5839 2e1f37b0 2019-08-08 stsp error = got_error_from_errno2("fclose", patch_script_path);
5841 ad493afc 2019-08-03 stsp got_repo_close(repo);
5842 ad493afc 2019-08-03 stsp if (worktree)
5843 ad493afc 2019-08-03 stsp got_worktree_close(worktree);
5844 ad493afc 2019-08-03 stsp TAILQ_FOREACH(pe, &paths, entry)
5845 ad493afc 2019-08-03 stsp free((char *)pe->path);
5846 ad493afc 2019-08-03 stsp got_pathlist_free(&paths);
5847 ad493afc 2019-08-03 stsp free(cwd);
5848 ad493afc 2019-08-03 stsp return error;
5851 ad493afc 2019-08-03 stsp __dead static void
5852 ad493afc 2019-08-03 stsp usage_unstage(void)
5854 2e1f37b0 2019-08-08 stsp fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
5855 2e1f37b0 2019-08-08 stsp "[file-path ...]\n",
5856 ad493afc 2019-08-03 stsp getprogname());
5861 ad493afc 2019-08-03 stsp static const struct got_error *
5862 ad493afc 2019-08-03 stsp cmd_unstage(int argc, char *argv[])
5864 ad493afc 2019-08-03 stsp const struct got_error *error = NULL;
5865 ad493afc 2019-08-03 stsp struct got_repository *repo = NULL;
5866 ad493afc 2019-08-03 stsp struct got_worktree *worktree = NULL;
5867 ad493afc 2019-08-03 stsp char *cwd = NULL;
5868 ad493afc 2019-08-03 stsp struct got_pathlist_head paths;
5869 ad493afc 2019-08-03 stsp struct got_pathlist_entry *pe;
5870 2e1f37b0 2019-08-08 stsp int ch, did_something = 0, pflag = 0;
5871 2e1f37b0 2019-08-08 stsp FILE *patch_script_file = NULL;
5872 2e1f37b0 2019-08-08 stsp const char *patch_script_path = NULL;
5873 2e1f37b0 2019-08-08 stsp struct choose_patch_arg cpa;
5875 ad493afc 2019-08-03 stsp TAILQ_INIT(&paths);
5877 2e1f37b0 2019-08-08 stsp while ((ch = getopt(argc, argv, "pF:")) != -1) {
5878 ad493afc 2019-08-03 stsp switch (ch) {
5880 2e1f37b0 2019-08-08 stsp pflag = 1;
5883 2e1f37b0 2019-08-08 stsp patch_script_path = optarg;
5886 ad493afc 2019-08-03 stsp usage_unstage();
5887 ad493afc 2019-08-03 stsp /* NOTREACHED */
5891 ad493afc 2019-08-03 stsp argc -= optind;
5892 ad493afc 2019-08-03 stsp argv += optind;
5894 ad493afc 2019-08-03 stsp #ifndef PROFILE
5895 ad493afc 2019-08-03 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5896 ad493afc 2019-08-03 stsp "unveil", NULL) == -1)
5897 ad493afc 2019-08-03 stsp err(1, "pledge");
5899 2e1f37b0 2019-08-08 stsp if (patch_script_path && !pflag)
5900 2e1f37b0 2019-08-08 stsp errx(1, "-F option can only be used together with -p option");
5902 ad493afc 2019-08-03 stsp cwd = getcwd(NULL, 0);
5903 ad493afc 2019-08-03 stsp if (cwd == NULL) {
5904 ad493afc 2019-08-03 stsp error = got_error_from_errno("getcwd");
5905 ad493afc 2019-08-03 stsp goto done;
5908 ad493afc 2019-08-03 stsp error = got_worktree_open(&worktree, cwd);
5909 ad493afc 2019-08-03 stsp if (error)
5910 ad493afc 2019-08-03 stsp goto done;
5912 ad493afc 2019-08-03 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
5913 ad493afc 2019-08-03 stsp if (error != NULL)
5914 ad493afc 2019-08-03 stsp goto done;
5916 2e1f37b0 2019-08-08 stsp if (patch_script_path) {
5917 2e1f37b0 2019-08-08 stsp patch_script_file = fopen(patch_script_path, "r");
5918 2e1f37b0 2019-08-08 stsp if (patch_script_file == NULL) {
5919 2e1f37b0 2019-08-08 stsp error = got_error_from_errno2("fopen",
5920 2e1f37b0 2019-08-08 stsp patch_script_path);
5921 2e1f37b0 2019-08-08 stsp goto done;
5925 ad493afc 2019-08-03 stsp error = apply_unveil(got_repo_get_path(repo), 1,
5926 ad493afc 2019-08-03 stsp got_worktree_get_root_path(worktree));
5927 ad493afc 2019-08-03 stsp if (error)
5928 ad493afc 2019-08-03 stsp goto done;
5930 ad493afc 2019-08-03 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5931 ad493afc 2019-08-03 stsp if (error)
5932 ad493afc 2019-08-03 stsp goto done;
5934 2e1f37b0 2019-08-08 stsp cpa.patch_script_file = patch_script_file;
5935 2e1f37b0 2019-08-08 stsp cpa.action = "unstage";
5936 ad493afc 2019-08-03 stsp error = got_worktree_unstage(worktree, &paths, update_progress,
5937 2e1f37b0 2019-08-08 stsp &did_something, pflag ? choose_patch : NULL, &cpa, repo);
5939 2e1f37b0 2019-08-08 stsp if (patch_script_file && fclose(patch_script_file) == EOF &&
5940 2e1f37b0 2019-08-08 stsp error == NULL)
5941 2e1f37b0 2019-08-08 stsp error = got_error_from_errno2("fclose", patch_script_path);
5943 0ebf8283 2019-07-24 stsp got_repo_close(repo);
5944 715dc77e 2019-08-03 stsp if (worktree)
5945 715dc77e 2019-08-03 stsp got_worktree_close(worktree);
5946 715dc77e 2019-08-03 stsp TAILQ_FOREACH(pe, &paths, entry)
5947 715dc77e 2019-08-03 stsp free((char *)pe->path);
5948 715dc77e 2019-08-03 stsp got_pathlist_free(&paths);
5949 715dc77e 2019-08-03 stsp free(cwd);
5950 0ebf8283 2019-07-24 stsp return error;