2 f42b1b34 2018-03-12 stsp * Copyright (c) 2017 Martin Pieuchot <mpi@openbsd.org>
3 5aa81393 2020-01-06 stsp * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
4 93658fb9 2020-03-18 stsp * Copyright (c) 2020 Ori Bernstein <ori@openbsd.org>
6 5c860e29 2018-03-12 stsp * Permission to use, copy, modify, and distribute this software for any
7 5c860e29 2018-03-12 stsp * purpose with or without fee is hereby granted, provided that the above
8 5c860e29 2018-03-12 stsp * copyright notice and this permission notice appear in all copies.
10 5c860e29 2018-03-12 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 5c860e29 2018-03-12 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 5c860e29 2018-03-12 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 5c860e29 2018-03-12 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 5c860e29 2018-03-12 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 5c860e29 2018-03-12 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 5c860e29 2018-03-12 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 f42b1b34 2018-03-12 stsp #include <sys/queue.h>
20 c0768b0f 2018-06-10 stsp #include <sys/types.h>
21 5de5890b 2018-10-18 stsp #include <sys/stat.h>
22 3c45a30a 2019-05-12 jcs #include <sys/param.h>
23 33ad4cbe 2019-05-12 jcs #include <sys/wait.h>
25 5c860e29 2018-03-12 stsp #include <err.h>
26 5c860e29 2018-03-12 stsp #include <errno.h>
27 12463d8b 2019-12-13 stsp #include <fcntl.h>
28 12ce7a6c 2019-08-12 stsp #include <limits.h>
29 5c860e29 2018-03-12 stsp #include <locale.h>
30 818c7501 2019-07-11 stsp #include <ctype.h>
31 99437157 2018-11-11 stsp #include <signal.h>
32 5c860e29 2018-03-12 stsp #include <stdio.h>
33 5c860e29 2018-03-12 stsp #include <stdlib.h>
34 5c860e29 2018-03-12 stsp #include <string.h>
35 5c860e29 2018-03-12 stsp #include <unistd.h>
36 c09a553d 2018-03-12 stsp #include <libgen.h>
37 c0768b0f 2018-06-10 stsp #include <time.h>
38 33ad4cbe 2019-05-12 jcs #include <paths.h>
39 6841bf13 2019-11-29 kn #include <regex.h>
40 83cd27f8 2020-01-13 stsp #include <getopt.h>
42 53ccebc2 2019-07-30 stsp #include "got_version.h"
43 f42b1b34 2018-03-12 stsp #include "got_error.h"
44 f42b1b34 2018-03-12 stsp #include "got_object.h"
45 5261c201 2018-04-01 stsp #include "got_reference.h"
46 f42b1b34 2018-03-12 stsp #include "got_repository.h"
47 1dd54920 2019-05-11 stsp #include "got_path.h"
48 e6209546 2019-08-22 stsp #include "got_cancel.h"
49 c09a553d 2018-03-12 stsp #include "got_worktree.h"
50 79109fed 2018-03-27 stsp #include "got_diff.h"
51 372ccdbb 2018-06-10 stsp #include "got_commit_graph.h"
52 6f23baec 2020-03-18 stsp #include "got_fetch.h"
53 404c43c4 2018-06-21 stsp #include "got_blame.h"
54 63219cd2 2019-01-04 stsp #include "got_privsep.h"
55 793c30b5 2019-05-13 stsp #include "got_opentemp.h"
57 5c860e29 2018-03-12 stsp #ifndef nitems
58 5c860e29 2018-03-12 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
61 99437157 2018-11-11 stsp static volatile sig_atomic_t sigint_received;
62 99437157 2018-11-11 stsp static volatile sig_atomic_t sigpipe_received;
65 99437157 2018-11-11 stsp catch_sigint(int signo)
67 99437157 2018-11-11 stsp sigint_received = 1;
71 99437157 2018-11-11 stsp catch_sigpipe(int signo)
73 99437157 2018-11-11 stsp sigpipe_received = 1;
77 8cfb4057 2019-07-09 stsp struct got_cmd {
78 5c860e29 2018-03-12 stsp const char *cmd_name;
79 d7d4f210 2018-03-12 stsp const struct got_error *(*cmd_main)(int, char *[]);
80 1b6b95a8 2018-03-12 stsp void (*cmd_usage)(void);
81 97b3a7be 2019-07-09 stsp const char *cmd_alias;
84 ce5b7c56 2019-07-09 stsp __dead static void usage(int);
85 2c7829a4 2019-06-17 stsp __dead static void usage_init(void);
86 3ce1b845 2019-07-15 stsp __dead static void usage_import(void);
87 4ed7e80c 2018-05-20 stsp __dead static void usage_checkout(void);
88 93658fb9 2020-03-18 stsp __dead static void usage_clone(void);
89 507dc3bb 2018-12-29 stsp __dead static void usage_update(void);
90 4ed7e80c 2018-05-20 stsp __dead static void usage_log(void);
91 4ed7e80c 2018-05-20 stsp __dead static void usage_diff(void);
92 404c43c4 2018-06-21 stsp __dead static void usage_blame(void);
93 5de5890b 2018-10-18 stsp __dead static void usage_tree(void);
94 6bad629b 2019-02-04 stsp __dead static void usage_status(void);
95 d0eebce4 2019-03-11 stsp __dead static void usage_ref(void);
96 4e759de4 2019-06-26 stsp __dead static void usage_branch(void);
97 8e7bd50a 2019-08-22 stsp __dead static void usage_tag(void);
98 d00136be 2019-03-26 stsp __dead static void usage_add(void);
99 648e4ef7 2019-07-09 stsp __dead static void usage_remove(void);
100 a129376b 2019-03-28 stsp __dead static void usage_revert(void);
101 c4296144 2019-05-09 stsp __dead static void usage_commit(void);
102 234035bc 2019-06-01 stsp __dead static void usage_cherrypick(void);
103 5ef14e63 2019-06-02 stsp __dead static void usage_backout(void);
104 818c7501 2019-07-11 stsp __dead static void usage_rebase(void);
105 0ebf8283 2019-07-24 stsp __dead static void usage_histedit(void);
106 2822a352 2019-10-15 stsp __dead static void usage_integrate(void);
107 715dc77e 2019-08-03 stsp __dead static void usage_stage(void);
108 ad493afc 2019-08-03 stsp __dead static void usage_unstage(void);
109 01073a5d 2019-08-22 stsp __dead static void usage_cat(void);
111 2c7829a4 2019-06-17 stsp static const struct got_error* cmd_init(int, char *[]);
112 3ce1b845 2019-07-15 stsp static const struct got_error* cmd_import(int, char *[]);
113 93658fb9 2020-03-18 stsp static const struct got_error* cmd_clone(int, char *[]);
114 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_checkout(int, char *[]);
115 507dc3bb 2018-12-29 stsp static const struct got_error* cmd_update(int, char *[]);
116 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_log(int, char *[]);
117 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_diff(int, char *[]);
118 404c43c4 2018-06-21 stsp static const struct got_error* cmd_blame(int, char *[]);
119 5de5890b 2018-10-18 stsp static const struct got_error* cmd_tree(int, char *[]);
120 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_status(int, char *[]);
121 d0eebce4 2019-03-11 stsp static const struct got_error* cmd_ref(int, char *[]);
122 4e759de4 2019-06-26 stsp static const struct got_error* cmd_branch(int, char *[]);
123 8e7bd50a 2019-08-22 stsp static const struct got_error* cmd_tag(int, char *[]);
124 d00136be 2019-03-26 stsp static const struct got_error* cmd_add(int, char *[]);
125 648e4ef7 2019-07-09 stsp static const struct got_error* cmd_remove(int, char *[]);
126 a129376b 2019-03-28 stsp static const struct got_error* cmd_revert(int, char *[]);
127 c4296144 2019-05-09 stsp static const struct got_error* cmd_commit(int, char *[]);
128 234035bc 2019-06-01 stsp static const struct got_error* cmd_cherrypick(int, char *[]);
129 5ef14e63 2019-06-02 stsp static const struct got_error* cmd_backout(int, char *[]);
130 818c7501 2019-07-11 stsp static const struct got_error* cmd_rebase(int, char *[]);
131 0ebf8283 2019-07-24 stsp static const struct got_error* cmd_histedit(int, char *[]);
132 2822a352 2019-10-15 stsp static const struct got_error* cmd_integrate(int, char *[]);
133 715dc77e 2019-08-03 stsp static const struct got_error* cmd_stage(int, char *[]);
134 ad493afc 2019-08-03 stsp static const struct got_error* cmd_unstage(int, char *[]);
135 01073a5d 2019-08-22 stsp static const struct got_error* cmd_cat(int, char *[]);
137 8cfb4057 2019-07-09 stsp static struct got_cmd got_commands[] = {
138 bc26cce8 2019-08-04 stsp { "init", cmd_init, usage_init, "in" },
139 bc26cce8 2019-08-04 stsp { "import", cmd_import, usage_import, "im" },
140 97b3a7be 2019-07-09 stsp { "checkout", cmd_checkout, usage_checkout, "co" },
141 93658fb9 2020-03-18 stsp { "clone", cmd_clone, usage_clone, "cl" },
142 97b3a7be 2019-07-09 stsp { "update", cmd_update, usage_update, "up" },
143 97b3a7be 2019-07-09 stsp { "log", cmd_log, usage_log, "" },
144 bc26cce8 2019-08-04 stsp { "diff", cmd_diff, usage_diff, "di" },
145 bc26cce8 2019-08-04 stsp { "blame", cmd_blame, usage_blame, "bl" },
146 bc26cce8 2019-08-04 stsp { "tree", cmd_tree, usage_tree, "tr" },
147 97b3a7be 2019-07-09 stsp { "status", cmd_status, usage_status, "st" },
148 97b3a7be 2019-07-09 stsp { "ref", cmd_ref, usage_ref, "" },
149 97b3a7be 2019-07-09 stsp { "branch", cmd_branch, usage_branch, "br" },
150 8e7bd50a 2019-08-22 stsp { "tag", cmd_tag, usage_tag, "" },
151 97b3a7be 2019-07-09 stsp { "add", cmd_add, usage_add, "" },
152 648e4ef7 2019-07-09 stsp { "remove", cmd_remove, usage_remove, "rm" },
153 97b3a7be 2019-07-09 stsp { "revert", cmd_revert, usage_revert, "rv" },
154 97b3a7be 2019-07-09 stsp { "commit", cmd_commit, usage_commit, "ci" },
155 016477fd 2019-07-09 stsp { "cherrypick", cmd_cherrypick, usage_cherrypick, "cy" },
156 97b3a7be 2019-07-09 stsp { "backout", cmd_backout, usage_backout, "bo" },
157 818c7501 2019-07-11 stsp { "rebase", cmd_rebase, usage_rebase, "rb" },
158 0ebf8283 2019-07-24 stsp { "histedit", cmd_histedit, usage_histedit, "he" },
159 2822a352 2019-10-15 stsp { "integrate", cmd_integrate, usage_integrate,"ig" },
160 715dc77e 2019-08-03 stsp { "stage", cmd_stage, usage_stage, "sg" },
161 ad493afc 2019-08-03 stsp { "unstage", cmd_unstage, usage_unstage, "ug" },
162 01073a5d 2019-08-22 stsp { "cat", cmd_cat, usage_cat, "" },
165 ce5b7c56 2019-07-09 stsp static void
166 ce5b7c56 2019-07-09 stsp list_commands(void)
170 ce5b7c56 2019-07-09 stsp fprintf(stderr, "commands:");
171 ce5b7c56 2019-07-09 stsp for (i = 0; i < nitems(got_commands); i++) {
172 ce5b7c56 2019-07-09 stsp struct got_cmd *cmd = &got_commands[i];
173 ce5b7c56 2019-07-09 stsp fprintf(stderr, " %s", cmd->cmd_name);
175 ce5b7c56 2019-07-09 stsp fputc('\n', stderr);
179 5c860e29 2018-03-12 stsp main(int argc, char *argv[])
181 8cfb4057 2019-07-09 stsp struct got_cmd *cmd;
182 5c860e29 2018-03-12 stsp unsigned int i;
184 53ccebc2 2019-07-30 stsp int hflag = 0, Vflag = 0;
185 83cd27f8 2020-01-13 stsp static struct option longopts[] = {
186 83cd27f8 2020-01-13 stsp { "version", no_argument, NULL, 'V' },
187 83cd27f8 2020-01-13 stsp { NULL, 0, NULL, 0}
190 289e3cbf 2019-02-04 stsp setlocale(LC_CTYPE, "");
192 6586ea88 2020-01-13 stsp while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
193 5c860e29 2018-03-12 stsp switch (ch) {
201 ce5b7c56 2019-07-09 stsp usage(hflag);
202 5c860e29 2018-03-12 stsp /* NOTREACHED */
206 5c860e29 2018-03-12 stsp argc -= optind;
207 5c860e29 2018-03-12 stsp argv += optind;
208 1e70621d 2018-03-27 stsp optind = 0;
210 53ccebc2 2019-07-30 stsp if (Vflag) {
211 53ccebc2 2019-07-30 stsp got_version_print_str();
215 5c860e29 2018-03-12 stsp if (argc <= 0)
216 ce5b7c56 2019-07-09 stsp usage(hflag);
218 99437157 2018-11-11 stsp signal(SIGINT, catch_sigint);
219 99437157 2018-11-11 stsp signal(SIGPIPE, catch_sigpipe);
221 5c860e29 2018-03-12 stsp for (i = 0; i < nitems(got_commands); i++) {
222 d7d4f210 2018-03-12 stsp const struct got_error *error;
224 5c860e29 2018-03-12 stsp cmd = &got_commands[i];
226 97b3a7be 2019-07-09 stsp if (strcmp(cmd->cmd_name, argv[0]) != 0 &&
227 97b3a7be 2019-07-09 stsp strcmp(cmd->cmd_alias, argv[0]) != 0)
231 1b6b95a8 2018-03-12 stsp got_commands[i].cmd_usage();
233 d7d4f210 2018-03-12 stsp error = got_commands[i].cmd_main(argc, argv);
234 f8afbdc8 2019-11-08 stsp if (error && error->code != GOT_ERR_CANCELLED &&
235 f8afbdc8 2019-11-08 stsp error->code != GOT_ERR_PRIVSEP_EXIT &&
236 f8afbdc8 2019-11-08 stsp !(sigpipe_received &&
237 70015d7a 2019-11-08 stsp error->code == GOT_ERR_ERRNO && errno == EPIPE) &&
238 70015d7a 2019-11-08 stsp !(sigint_received &&
239 70015d7a 2019-11-08 stsp error->code == GOT_ERR_ERRNO && errno == EINTR)) {
240 d7d4f210 2018-03-12 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
247 20ecf764 2018-03-12 stsp fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
248 ce5b7c56 2019-07-09 stsp list_commands();
252 4ed7e80c 2018-05-20 stsp __dead static void
253 ce5b7c56 2019-07-09 stsp usage(int hflag)
255 e801a566 2020-01-13 stsp fprintf(stderr, "usage: %s [-h] [-V | --version] command [arg ...]\n",
256 53ccebc2 2019-07-30 stsp getprogname());
258 ce5b7c56 2019-07-09 stsp list_commands();
262 0266afb7 2019-01-04 stsp static const struct got_error *
263 0ee7065d 2019-05-13 stsp get_editor(char **abspath)
265 0ee7065d 2019-05-13 stsp const struct got_error *err = NULL;
266 e2ba3d07 2019-05-13 stsp const char *editor;
268 8920fa04 2019-08-18 stsp *abspath = NULL;
270 e2ba3d07 2019-05-13 stsp editor = getenv("VISUAL");
271 e2ba3d07 2019-05-13 stsp if (editor == NULL)
272 e2ba3d07 2019-05-13 stsp editor = getenv("EDITOR");
274 0ee7065d 2019-05-13 stsp if (editor) {
275 0ee7065d 2019-05-13 stsp err = got_path_find_prog(abspath, editor);
277 0ee7065d 2019-05-13 stsp return err;
280 0ee7065d 2019-05-13 stsp if (*abspath == NULL) {
281 0ee7065d 2019-05-13 stsp *abspath = strdup("/bin/ed");
282 0ee7065d 2019-05-13 stsp if (*abspath == NULL)
283 0ee7065d 2019-05-13 stsp return got_error_from_errno("strdup");
286 e2ba3d07 2019-05-13 stsp return NULL;
289 e2ba3d07 2019-05-13 stsp static const struct got_error *
290 d0eebce4 2019-03-11 stsp apply_unveil(const char *repo_path, int repo_read_only,
291 c530dc23 2019-07-23 stsp const char *worktree_path)
293 163ce85a 2019-05-13 stsp const struct got_error *err;
295 37c06ea4 2019-07-15 stsp #ifdef PROFILE
296 37c06ea4 2019-07-15 stsp if (unveil("gmon.out", "rwc") != 0)
297 37c06ea4 2019-07-15 stsp return got_error_from_errno2("unveil", "gmon.out");
299 d0eebce4 2019-03-11 stsp if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
300 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", repo_path);
302 0266afb7 2019-01-04 stsp if (worktree_path && unveil(worktree_path, "rwc") != 0)
303 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", worktree_path);
305 bb63914a 2020-02-17 stsp if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
306 bb63914a 2020-02-17 stsp return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
308 163ce85a 2019-05-13 stsp err = got_privsep_unveil_exec_helpers();
309 163ce85a 2019-05-13 stsp if (err != NULL)
310 163ce85a 2019-05-13 stsp return err;
312 0266afb7 2019-01-04 stsp if (unveil(NULL, NULL) != 0)
313 638f9024 2019-05-13 stsp return got_error_from_errno("unveil");
315 0266afb7 2019-01-04 stsp return NULL;
318 2c7829a4 2019-06-17 stsp __dead static void
319 2c7829a4 2019-06-17 stsp usage_init(void)
321 09ea71ba 2019-07-27 stsp fprintf(stderr, "usage: %s init repository-path\n", getprogname());
325 2c7829a4 2019-06-17 stsp static const struct got_error *
326 2c7829a4 2019-06-17 stsp cmd_init(int argc, char *argv[])
328 2c7829a4 2019-06-17 stsp const struct got_error *error = NULL;
329 2c7829a4 2019-06-17 stsp char *repo_path = NULL;
332 2c7829a4 2019-06-17 stsp while ((ch = getopt(argc, argv, "")) != -1) {
333 2c7829a4 2019-06-17 stsp switch (ch) {
335 2c7829a4 2019-06-17 stsp usage_init();
336 2c7829a4 2019-06-17 stsp /* NOTREACHED */
340 2c7829a4 2019-06-17 stsp argc -= optind;
341 2c7829a4 2019-06-17 stsp argv += optind;
343 2c7829a4 2019-06-17 stsp #ifndef PROFILE
344 2c7829a4 2019-06-17 stsp if (pledge("stdio rpath wpath cpath unveil", NULL) == -1)
345 2c7829a4 2019-06-17 stsp err(1, "pledge");
347 2c7829a4 2019-06-17 stsp if (argc != 1)
348 bc20e173 2019-06-17 stsp usage_init();
350 2c7829a4 2019-06-17 stsp repo_path = strdup(argv[0]);
351 2c7829a4 2019-06-17 stsp if (repo_path == NULL)
352 2c7829a4 2019-06-17 stsp return got_error_from_errno("strdup");
354 2c7829a4 2019-06-17 stsp got_path_strip_trailing_slashes(repo_path);
356 2c7829a4 2019-06-17 stsp error = got_path_mkdir(repo_path);
357 2c7829a4 2019-06-17 stsp if (error &&
358 2c7829a4 2019-06-17 stsp !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
361 c530dc23 2019-07-23 stsp error = apply_unveil(repo_path, 0, NULL);
365 2c7829a4 2019-06-17 stsp error = got_repo_init(repo_path);
367 3ce1b845 2019-07-15 stsp free(repo_path);
368 3ce1b845 2019-07-15 stsp return error;
371 3ce1b845 2019-07-15 stsp __dead static void
372 3ce1b845 2019-07-15 stsp usage_import(void)
374 3ce1b845 2019-07-15 stsp fprintf(stderr, "usage: %s import [-b branch] [-m message] "
375 3ce1b845 2019-07-15 stsp "[-r repository-path] [-I pattern] path\n", getprogname());
380 3ce1b845 2019-07-15 stsp spawn_editor(const char *editor, const char *file)
383 3ce1b845 2019-07-15 stsp sig_t sighup, sigint, sigquit;
384 3ce1b845 2019-07-15 stsp int st = -1;
386 3ce1b845 2019-07-15 stsp sighup = signal(SIGHUP, SIG_IGN);
387 3ce1b845 2019-07-15 stsp sigint = signal(SIGINT, SIG_IGN);
388 3ce1b845 2019-07-15 stsp sigquit = signal(SIGQUIT, SIG_IGN);
390 3ce1b845 2019-07-15 stsp switch (pid = fork()) {
392 3ce1b845 2019-07-15 stsp goto doneediting;
394 3ce1b845 2019-07-15 stsp execl(editor, editor, file, (char *)NULL);
395 3ce1b845 2019-07-15 stsp _exit(127);
398 3ce1b845 2019-07-15 stsp while (waitpid(pid, &st, 0) == -1)
399 3ce1b845 2019-07-15 stsp if (errno != EINTR)
402 3ce1b845 2019-07-15 stsp doneediting:
403 3ce1b845 2019-07-15 stsp (void)signal(SIGHUP, sighup);
404 3ce1b845 2019-07-15 stsp (void)signal(SIGINT, sigint);
405 3ce1b845 2019-07-15 stsp (void)signal(SIGQUIT, sigquit);
407 3ce1b845 2019-07-15 stsp if (!WIFEXITED(st)) {
408 3ce1b845 2019-07-15 stsp errno = EINTR;
412 3ce1b845 2019-07-15 stsp return WEXITSTATUS(st);
415 3ce1b845 2019-07-15 stsp static const struct got_error *
416 3ce1b845 2019-07-15 stsp edit_logmsg(char **logmsg, const char *editor, const char *logmsg_path,
417 3ce1b845 2019-07-15 stsp const char *initial_content)
419 3ce1b845 2019-07-15 stsp const struct got_error *err = NULL;
420 3ce1b845 2019-07-15 stsp char buf[1024];
421 3ce1b845 2019-07-15 stsp struct stat st, st2;
423 3ce1b845 2019-07-15 stsp int content_changed = 0;
424 3ce1b845 2019-07-15 stsp size_t len;
426 3ce1b845 2019-07-15 stsp *logmsg = NULL;
428 3ce1b845 2019-07-15 stsp if (stat(logmsg_path, &st) == -1)
429 3ce1b845 2019-07-15 stsp return got_error_from_errno2("stat", logmsg_path);
431 3ce1b845 2019-07-15 stsp if (spawn_editor(editor, logmsg_path) == -1)
432 3ce1b845 2019-07-15 stsp return got_error_from_errno("failed spawning editor");
434 3ce1b845 2019-07-15 stsp if (stat(logmsg_path, &st2) == -1)
435 3ce1b845 2019-07-15 stsp return got_error_from_errno("stat");
437 3ce1b845 2019-07-15 stsp if (st.st_mtime == st2.st_mtime && st.st_size == st2.st_size)
438 3ce1b845 2019-07-15 stsp return got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
439 3ce1b845 2019-07-15 stsp "no changes made to commit message, aborting");
441 3ce1b845 2019-07-15 stsp *logmsg = malloc(st2.st_size + 1);
442 3ce1b845 2019-07-15 stsp if (*logmsg == NULL)
443 3ce1b845 2019-07-15 stsp return got_error_from_errno("malloc");
444 3ce1b845 2019-07-15 stsp (*logmsg)[0] = '\0';
447 3ce1b845 2019-07-15 stsp fp = fopen(logmsg_path, "r");
448 3ce1b845 2019-07-15 stsp if (fp == NULL) {
449 3ce1b845 2019-07-15 stsp err = got_error_from_errno("fopen");
452 3ce1b845 2019-07-15 stsp while (fgets(buf, sizeof(buf), fp) != NULL) {
453 3ce1b845 2019-07-15 stsp if (!content_changed && strcmp(buf, initial_content) != 0)
454 3ce1b845 2019-07-15 stsp content_changed = 1;
455 3ce1b845 2019-07-15 stsp if (buf[0] == '#' || (len == 0 && buf[0] == '\n'))
456 3ce1b845 2019-07-15 stsp continue; /* remove comments and leading empty lines */
457 3ce1b845 2019-07-15 stsp len = strlcat(*logmsg, buf, st2.st_size);
459 3ce1b845 2019-07-15 stsp fclose(fp);
461 3ce1b845 2019-07-15 stsp while (len > 0 && (*logmsg)[len - 1] == '\n') {
462 3ce1b845 2019-07-15 stsp (*logmsg)[len - 1] = '\0';
466 3ce1b845 2019-07-15 stsp if (len == 0 || !content_changed)
467 3ce1b845 2019-07-15 stsp err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
468 3ce1b845 2019-07-15 stsp "commit message cannot be empty, aborting");
471 3ce1b845 2019-07-15 stsp free(*logmsg);
472 3ce1b845 2019-07-15 stsp *logmsg = NULL;
474 3ce1b845 2019-07-15 stsp return err;
477 3ce1b845 2019-07-15 stsp static const struct got_error *
478 ef293bdd 2019-10-21 stsp collect_import_msg(char **logmsg, char **logmsg_path, const char *editor,
479 ef293bdd 2019-10-21 stsp const char *path_dir, const char *branch_name)
481 ef293bdd 2019-10-21 stsp char *initial_content = NULL;
482 3ce1b845 2019-07-15 stsp const struct got_error *err = NULL;
485 3ce1b845 2019-07-15 stsp if (asprintf(&initial_content,
486 3ce1b845 2019-07-15 stsp "\n# %s to be imported to branch %s\n", path_dir,
487 3ce1b845 2019-07-15 stsp branch_name) == -1)
488 3ce1b845 2019-07-15 stsp return got_error_from_errno("asprintf");
490 bb63914a 2020-02-17 stsp err = got_opentemp_named_fd(logmsg_path, &fd,
491 bb63914a 2020-02-17 stsp GOT_TMPDIR_STR "/got-importmsg");
495 3ce1b845 2019-07-15 stsp dprintf(fd, initial_content);
498 ef293bdd 2019-10-21 stsp err = edit_logmsg(logmsg, editor, *logmsg_path, initial_content);
500 3ce1b845 2019-07-15 stsp free(initial_content);
501 3ce1b845 2019-07-15 stsp return err;
504 3ce1b845 2019-07-15 stsp static const struct got_error *
505 3ce1b845 2019-07-15 stsp import_progress(void *arg, const char *path)
507 3ce1b845 2019-07-15 stsp printf("A %s\n", path);
508 3ce1b845 2019-07-15 stsp return NULL;
511 3ce1b845 2019-07-15 stsp static const struct got_error *
512 aba9c984 2019-09-08 stsp get_author(char **author, struct got_repository *repo)
514 aba9c984 2019-09-08 stsp const struct got_error *err = NULL;
515 c9956ddf 2019-09-08 stsp const char *got_author, *name, *email;
517 84792843 2019-08-09 stsp *author = NULL;
519 c9956ddf 2019-09-08 stsp name = got_repo_get_gitconfig_author_name(repo);
520 c9956ddf 2019-09-08 stsp email = got_repo_get_gitconfig_author_email(repo);
521 c9956ddf 2019-09-08 stsp if (name && email) {
522 c9956ddf 2019-09-08 stsp if (asprintf(author, "%s <%s>", name, email) == -1)
523 aba9c984 2019-09-08 stsp return got_error_from_errno("asprintf");
524 aba9c984 2019-09-08 stsp return NULL;
527 84792843 2019-08-09 stsp got_author = getenv("GOT_AUTHOR");
528 84792843 2019-08-09 stsp if (got_author == NULL) {
529 c9956ddf 2019-09-08 stsp name = got_repo_get_global_gitconfig_author_name(repo);
530 c9956ddf 2019-09-08 stsp email = got_repo_get_global_gitconfig_author_email(repo);
531 c9956ddf 2019-09-08 stsp if (name && email) {
532 c9956ddf 2019-09-08 stsp if (asprintf(author, "%s <%s>", name, email) == -1)
533 c9956ddf 2019-09-08 stsp return got_error_from_errno("asprintf");
534 c9956ddf 2019-09-08 stsp return NULL;
536 84792843 2019-08-09 stsp /* TODO: Look up user in password database? */
537 84792843 2019-08-09 stsp return got_error(GOT_ERR_COMMIT_NO_AUTHOR);
540 aba9c984 2019-09-08 stsp *author = strdup(got_author);
541 aba9c984 2019-09-08 stsp if (*author == NULL)
542 aba9c984 2019-09-08 stsp return got_error_from_errno("strdup");
545 84792843 2019-08-09 stsp * Really dumb email address check; we're only doing this to
546 84792843 2019-08-09 stsp * avoid git's object parser breaking on commits we create.
548 84792843 2019-08-09 stsp while (*got_author && *got_author != '<')
549 84792843 2019-08-09 stsp got_author++;
550 aba9c984 2019-09-08 stsp if (*got_author != '<') {
551 aba9c984 2019-09-08 stsp err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
554 84792843 2019-08-09 stsp while (*got_author && *got_author != '@')
555 84792843 2019-08-09 stsp got_author++;
556 aba9c984 2019-09-08 stsp if (*got_author != '@') {
557 aba9c984 2019-09-08 stsp err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
560 84792843 2019-08-09 stsp while (*got_author && *got_author != '>')
561 84792843 2019-08-09 stsp got_author++;
562 84792843 2019-08-09 stsp if (*got_author != '>')
563 aba9c984 2019-09-08 stsp err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
566 aba9c984 2019-09-08 stsp free(*author);
567 aba9c984 2019-09-08 stsp *author = NULL;
569 aba9c984 2019-09-08 stsp return err;
572 c9956ddf 2019-09-08 stsp static const struct got_error *
573 c9956ddf 2019-09-08 stsp get_gitconfig_path(char **gitconfig_path)
575 c9956ddf 2019-09-08 stsp const char *homedir = getenv("HOME");
577 c9956ddf 2019-09-08 stsp *gitconfig_path = NULL;
578 c9956ddf 2019-09-08 stsp if (homedir) {
579 c9956ddf 2019-09-08 stsp if (asprintf(gitconfig_path, "%s/.gitconfig", homedir) == -1)
580 c9956ddf 2019-09-08 stsp return got_error_from_errno("asprintf");
583 c9956ddf 2019-09-08 stsp return NULL;
586 84792843 2019-08-09 stsp static const struct got_error *
587 3ce1b845 2019-07-15 stsp cmd_import(int argc, char *argv[])
589 3ce1b845 2019-07-15 stsp const struct got_error *error = NULL;
590 3ce1b845 2019-07-15 stsp char *path_dir = NULL, *repo_path = NULL, *logmsg = NULL;
591 c9956ddf 2019-09-08 stsp char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
592 5d67f40d 2019-11-08 stsp const char *branch_name = "main";
593 ef293bdd 2019-10-21 stsp char *refname = NULL, *id_str = NULL, *logmsg_path = NULL;
594 3ce1b845 2019-07-15 stsp struct got_repository *repo = NULL;
595 3ce1b845 2019-07-15 stsp struct got_reference *branch_ref = NULL, *head_ref = NULL;
596 3ce1b845 2019-07-15 stsp struct got_object_id *new_commit_id = NULL;
598 3ce1b845 2019-07-15 stsp struct got_pathlist_head ignores;
599 3ce1b845 2019-07-15 stsp struct got_pathlist_entry *pe;
600 ef293bdd 2019-10-21 stsp int preserve_logmsg = 0;
602 3ce1b845 2019-07-15 stsp TAILQ_INIT(&ignores);
604 3ce1b845 2019-07-15 stsp while ((ch = getopt(argc, argv, "b:m:r:I:")) != -1) {
605 3ce1b845 2019-07-15 stsp switch (ch) {
607 3ce1b845 2019-07-15 stsp branch_name = optarg;
610 3ce1b845 2019-07-15 stsp logmsg = strdup(optarg);
611 3ce1b845 2019-07-15 stsp if (logmsg == NULL) {
612 3ce1b845 2019-07-15 stsp error = got_error_from_errno("strdup");
617 3ce1b845 2019-07-15 stsp repo_path = realpath(optarg, NULL);
618 3ce1b845 2019-07-15 stsp if (repo_path == NULL) {
619 9ba1d308 2019-10-21 stsp error = got_error_from_errno2("realpath",
625 3ce1b845 2019-07-15 stsp if (optarg[0] == '\0')
627 3ce1b845 2019-07-15 stsp error = got_pathlist_insert(&pe, &ignores, optarg,
633 b2b65d18 2019-08-22 stsp usage_import();
634 3ce1b845 2019-07-15 stsp /* NOTREACHED */
638 3ce1b845 2019-07-15 stsp argc -= optind;
639 3ce1b845 2019-07-15 stsp argv += optind;
641 3ce1b845 2019-07-15 stsp #ifndef PROFILE
642 aba9c984 2019-09-08 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
644 3ce1b845 2019-07-15 stsp NULL) == -1)
645 3ce1b845 2019-07-15 stsp err(1, "pledge");
647 3ce1b845 2019-07-15 stsp if (argc != 1)
648 3ce1b845 2019-07-15 stsp usage_import();
650 3ce1b845 2019-07-15 stsp if (repo_path == NULL) {
651 3ce1b845 2019-07-15 stsp repo_path = getcwd(NULL, 0);
652 3ce1b845 2019-07-15 stsp if (repo_path == NULL)
653 3ce1b845 2019-07-15 stsp return got_error_from_errno("getcwd");
655 3ce1b845 2019-07-15 stsp got_path_strip_trailing_slashes(repo_path);
656 c9956ddf 2019-09-08 stsp error = get_gitconfig_path(&gitconfig_path);
659 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, gitconfig_path);
663 aba9c984 2019-09-08 stsp error = get_author(&author, repo);
665 aba9c984 2019-09-08 stsp return error;
668 bd5895f3 2019-11-28 stsp * Don't let the user create a branch name with a leading '-'.
669 e560b7e0 2019-11-28 stsp * While technically a valid reference name, this case is usually
670 e560b7e0 2019-11-28 stsp * an unintended typo.
672 bd5895f3 2019-11-28 stsp if (branch_name[0] == '-')
673 bd5895f3 2019-11-28 stsp return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
675 3ce1b845 2019-07-15 stsp if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
676 3ce1b845 2019-07-15 stsp error = got_error_from_errno("asprintf");
680 3ce1b845 2019-07-15 stsp error = got_ref_open(&branch_ref, repo, refname, 0);
681 3ce1b845 2019-07-15 stsp if (error) {
682 3ce1b845 2019-07-15 stsp if (error->code != GOT_ERR_NOT_REF)
685 3ce1b845 2019-07-15 stsp error = got_error_msg(GOT_ERR_BRANCH_EXISTS,
686 3ce1b845 2019-07-15 stsp "import target branch already exists");
690 3ce1b845 2019-07-15 stsp path_dir = realpath(argv[0], NULL);
691 3ce1b845 2019-07-15 stsp if (path_dir == NULL) {
692 9ba1d308 2019-10-21 stsp error = got_error_from_errno2("realpath", argv[0]);
695 3ce1b845 2019-07-15 stsp got_path_strip_trailing_slashes(path_dir);
698 3ce1b845 2019-07-15 stsp * unveil(2) traverses exec(2); if an editor is used we have
699 3ce1b845 2019-07-15 stsp * to apply unveil after the log message has been written.
701 3ce1b845 2019-07-15 stsp if (logmsg == NULL || strlen(logmsg) == 0) {
702 3ce1b845 2019-07-15 stsp error = get_editor(&editor);
705 8e158b01 2019-09-22 stsp free(logmsg);
706 ef293bdd 2019-10-21 stsp error = collect_import_msg(&logmsg, &logmsg_path, editor,
707 ef293bdd 2019-10-21 stsp path_dir, refname);
708 ef293bdd 2019-10-21 stsp if (error) {
709 ef293bdd 2019-10-21 stsp if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
710 ef293bdd 2019-10-21 stsp logmsg_path != NULL)
711 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
716 ef293bdd 2019-10-21 stsp if (unveil(path_dir, "r") != 0) {
717 ef293bdd 2019-10-21 stsp error = got_error_from_errno2("unveil", path_dir);
718 ef293bdd 2019-10-21 stsp if (logmsg_path)
719 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
723 ef293bdd 2019-10-21 stsp error = apply_unveil(got_repo_get_path(repo), 0, NULL);
724 ef293bdd 2019-10-21 stsp if (error) {
725 ef293bdd 2019-10-21 stsp if (logmsg_path)
726 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
730 3ce1b845 2019-07-15 stsp error = got_repo_import(&new_commit_id, path_dir, logmsg,
731 84792843 2019-08-09 stsp author, &ignores, repo, import_progress, NULL);
732 ef293bdd 2019-10-21 stsp if (error) {
733 ef293bdd 2019-10-21 stsp if (logmsg_path)
734 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
738 3ce1b845 2019-07-15 stsp error = got_ref_alloc(&branch_ref, refname, new_commit_id);
739 ef293bdd 2019-10-21 stsp if (error) {
740 ef293bdd 2019-10-21 stsp if (logmsg_path)
741 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
745 3ce1b845 2019-07-15 stsp error = got_ref_write(branch_ref, repo);
746 ef293bdd 2019-10-21 stsp if (error) {
747 ef293bdd 2019-10-21 stsp if (logmsg_path)
748 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
752 3ce1b845 2019-07-15 stsp error = got_object_id_str(&id_str, new_commit_id);
753 ef293bdd 2019-10-21 stsp if (error) {
754 ef293bdd 2019-10-21 stsp if (logmsg_path)
755 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
759 3ce1b845 2019-07-15 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
760 3ce1b845 2019-07-15 stsp if (error) {
761 ef293bdd 2019-10-21 stsp if (error->code != GOT_ERR_NOT_REF) {
762 ef293bdd 2019-10-21 stsp if (logmsg_path)
763 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
767 3ce1b845 2019-07-15 stsp error = got_ref_alloc_symref(&head_ref, GOT_REF_HEAD,
768 3ce1b845 2019-07-15 stsp branch_ref);
769 ef293bdd 2019-10-21 stsp if (error) {
770 ef293bdd 2019-10-21 stsp if (logmsg_path)
771 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
775 3ce1b845 2019-07-15 stsp error = got_ref_write(head_ref, repo);
776 ef293bdd 2019-10-21 stsp if (error) {
777 ef293bdd 2019-10-21 stsp if (logmsg_path)
778 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
783 3ce1b845 2019-07-15 stsp printf("Created branch %s with commit %s\n",
784 3ce1b845 2019-07-15 stsp got_ref_get_name(branch_ref), id_str);
786 ef293bdd 2019-10-21 stsp if (preserve_logmsg) {
787 ef293bdd 2019-10-21 stsp fprintf(stderr, "%s: log message preserved in %s\n",
788 ef293bdd 2019-10-21 stsp getprogname(), logmsg_path);
789 ef293bdd 2019-10-21 stsp } else if (logmsg_path && unlink(logmsg_path) == -1 && error == NULL)
790 ef293bdd 2019-10-21 stsp error = got_error_from_errno2("unlink", logmsg_path);
791 8e158b01 2019-09-22 stsp free(logmsg);
792 ef293bdd 2019-10-21 stsp free(logmsg_path);
793 2c7829a4 2019-06-17 stsp free(repo_path);
794 3ce1b845 2019-07-15 stsp free(editor);
795 3ce1b845 2019-07-15 stsp free(refname);
796 3ce1b845 2019-07-15 stsp free(new_commit_id);
797 3ce1b845 2019-07-15 stsp free(id_str);
798 aba9c984 2019-09-08 stsp free(author);
799 c9956ddf 2019-09-08 stsp free(gitconfig_path);
800 3ce1b845 2019-07-15 stsp if (branch_ref)
801 3ce1b845 2019-07-15 stsp got_ref_close(branch_ref);
802 3ce1b845 2019-07-15 stsp if (head_ref)
803 3ce1b845 2019-07-15 stsp got_ref_close(head_ref);
804 2c7829a4 2019-06-17 stsp return error;
807 93658fb9 2020-03-18 stsp __dead static void
808 93658fb9 2020-03-18 stsp usage_clone(void)
810 93658fb9 2020-03-18 stsp fprintf(stderr, "usage: %s clone repo-url\n", getprogname());
814 4ed7e80c 2018-05-20 stsp __dead static void
815 c09a553d 2018-03-12 stsp usage_checkout(void)
817 bb51a5b4 2020-01-13 stsp fprintf(stderr, "usage: %s checkout [-E] [-b branch] [-c commit] "
818 08573d5b 2019-05-14 stsp "[-p prefix] repository-path [worktree-path]\n", getprogname());
822 7f47418f 2019-12-20 stsp static void
823 7f47418f 2019-12-20 stsp show_worktree_base_ref_warning(void)
825 7f47418f 2019-12-20 stsp fprintf(stderr, "%s: warning: could not create a reference "
826 7f47418f 2019-12-20 stsp "to the work tree's base commit; the commit could be "
827 7f47418f 2019-12-20 stsp "garbage-collected by Git; making the repository "
828 7f47418f 2019-12-20 stsp "writable and running 'got update' will prevent this\n",
829 7f47418f 2019-12-20 stsp getprogname());
832 7f47418f 2019-12-20 stsp struct got_checkout_progress_arg {
833 7f47418f 2019-12-20 stsp const char *worktree_path;
834 7f47418f 2019-12-20 stsp int had_base_commit_ref_error;
837 1ee397ad 2019-07-12 stsp static const struct got_error *
838 a0eb853d 2018-12-29 stsp checkout_progress(void *arg, unsigned char status, const char *path)
840 7f47418f 2019-12-20 stsp struct got_checkout_progress_arg *a = arg;
842 a484d721 2019-06-10 stsp /* Base commit bump happens silently. */
843 a484d721 2019-06-10 stsp if (status == GOT_STATUS_BUMP_BASE)
844 7f47418f 2019-12-20 stsp return NULL;
846 7f47418f 2019-12-20 stsp if (status == GOT_STATUS_BASE_REF_ERR) {
847 7f47418f 2019-12-20 stsp a->had_base_commit_ref_error = 1;
848 1ee397ad 2019-07-12 stsp return NULL;
851 92a684f4 2018-03-12 stsp while (path[0] == '/')
854 7f47418f 2019-12-20 stsp printf("%c %s/%s\n", status, a->worktree_path, path);
855 1ee397ad 2019-07-12 stsp return NULL;
858 99437157 2018-11-11 stsp static const struct got_error *
859 6bad629b 2019-02-04 stsp check_cancelled(void *arg)
861 99437157 2018-11-11 stsp if (sigint_received || sigpipe_received)
862 99437157 2018-11-11 stsp return got_error(GOT_ERR_CANCELLED);
863 99437157 2018-11-11 stsp return NULL;
866 8069f636 2019-01-12 stsp static const struct got_error *
867 024e9686 2019-05-14 stsp check_linear_ancestry(struct got_object_id *commit_id,
868 3aef623b 2019-10-15 stsp struct got_object_id *base_commit_id, int allow_forwards_in_time_only,
869 3aef623b 2019-10-15 stsp struct got_repository *repo)
871 d5bea539 2019-05-13 stsp const struct got_error *err = NULL;
872 024e9686 2019-05-14 stsp struct got_object_id *yca_id;
874 d5bea539 2019-05-13 stsp err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
875 6fb7cd11 2019-08-22 stsp commit_id, base_commit_id, repo, check_cancelled, NULL);
877 36a38700 2019-05-10 stsp return err;
879 d5bea539 2019-05-13 stsp if (yca_id == NULL)
880 d5bea539 2019-05-13 stsp return got_error(GOT_ERR_ANCESTRY);
883 d5bea539 2019-05-13 stsp * Require a straight line of history between the target commit
884 d5bea539 2019-05-13 stsp * and the work tree's base commit.
886 d5751d49 2019-05-14 stsp * Non-linear situations such as this require a rebase:
888 d5bea539 2019-05-13 stsp * (commit) D F (base_commit)
896 d5bea539 2019-05-13 stsp * 'got update' only handles linear cases:
897 d5bea539 2019-05-13 stsp * Update forwards in time: A (base/yca) - B - C - D (commit)
898 efa2b6f7 2019-05-14 stsp * Update backwards in time: D (base) - C - B - A (commit/yca)
900 3aef623b 2019-10-15 stsp if (allow_forwards_in_time_only) {
901 3aef623b 2019-10-15 stsp if (got_object_id_cmp(base_commit_id, yca_id) != 0)
902 3aef623b 2019-10-15 stsp return got_error(GOT_ERR_ANCESTRY);
903 3aef623b 2019-10-15 stsp } else if (got_object_id_cmp(commit_id, yca_id) != 0 &&
904 d5bea539 2019-05-13 stsp got_object_id_cmp(base_commit_id, yca_id) != 0)
905 d5bea539 2019-05-13 stsp return got_error(GOT_ERR_ANCESTRY);
907 d5bea539 2019-05-13 stsp free(yca_id);
908 d5bea539 2019-05-13 stsp return NULL;
911 a367ff0f 2019-05-14 stsp static const struct got_error *
912 a367ff0f 2019-05-14 stsp check_same_branch(struct got_object_id *commit_id,
913 a51a74b3 2019-07-27 stsp struct got_reference *head_ref, struct got_object_id *yca_id,
914 a51a74b3 2019-07-27 stsp struct got_repository *repo)
916 a367ff0f 2019-05-14 stsp const struct got_error *err = NULL;
917 a367ff0f 2019-05-14 stsp struct got_commit_graph *graph = NULL;
918 a367ff0f 2019-05-14 stsp struct got_object_id *head_commit_id = NULL;
919 a367ff0f 2019-05-14 stsp int is_same_branch = 0;
921 a367ff0f 2019-05-14 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
925 a51a74b3 2019-07-27 stsp if (got_object_id_cmp(head_commit_id, commit_id) == 0) {
926 a51a74b3 2019-07-27 stsp is_same_branch = 1;
929 a51a74b3 2019-07-27 stsp if (yca_id && got_object_id_cmp(commit_id, yca_id) == 0) {
930 a51a74b3 2019-07-27 stsp is_same_branch = 1;
934 3d509237 2020-01-04 stsp err = got_commit_graph_open(&graph, "/", 1);
938 6fb7cd11 2019-08-22 stsp err = got_commit_graph_iter_start(graph, head_commit_id, repo,
939 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
944 a367ff0f 2019-05-14 stsp struct got_object_id *id;
945 ee780d5c 2020-01-04 stsp err = got_commit_graph_iter_next(&id, graph, repo,
946 ee780d5c 2020-01-04 stsp check_cancelled, NULL);
948 ee780d5c 2020-01-04 stsp if (err->code == GOT_ERR_ITER_COMPLETED)
949 a367ff0f 2019-05-14 stsp err = NULL;
954 a51a74b3 2019-07-27 stsp if (yca_id && got_object_id_cmp(id, yca_id) == 0)
956 a367ff0f 2019-05-14 stsp if (got_object_id_cmp(id, commit_id) == 0) {
957 a367ff0f 2019-05-14 stsp is_same_branch = 1;
964 a367ff0f 2019-05-14 stsp got_commit_graph_close(graph);
965 a367ff0f 2019-05-14 stsp free(head_commit_id);
966 a367ff0f 2019-05-14 stsp if (!err && !is_same_branch)
967 a367ff0f 2019-05-14 stsp err = got_error(GOT_ERR_ANCESTRY);
968 a367ff0f 2019-05-14 stsp return err;
971 93658fb9 2020-03-18 stsp static const struct got_error *
972 531c3985 2020-03-18 stsp fetch_progress(void *arg, const char *message)
974 531c3985 2020-03-18 stsp char *servername = arg;
975 531c3985 2020-03-18 stsp printf("\rserver %s: %s", servername, message);
976 531c3985 2020-03-18 stsp fflush(stdout);
977 531c3985 2020-03-18 stsp return NULL;
980 531c3985 2020-03-18 stsp static const struct got_error *
981 93658fb9 2020-03-18 stsp cmd_clone(int argc, char *argv[])
983 09838ffc 2020-03-18 stsp const struct got_error *err = NULL;
984 09838ffc 2020-03-18 stsp const char *uri, *branch_filter, *dirname;
985 09838ffc 2020-03-18 stsp char *proto, *host, *port, *repo_name, *server_path;
986 d9b4d0c0 2020-03-18 stsp char *default_destdir = NULL, *id_str = NULL;
987 bb64b798 2020-03-18 stsp const char *repo_path;
988 bb64b798 2020-03-18 stsp struct got_repository *repo = NULL;
989 d9b4d0c0 2020-03-18 stsp struct got_pathlist_head refs, symrefs;
990 d9b4d0c0 2020-03-18 stsp struct got_pathlist_entry *pe;
991 d9b4d0c0 2020-03-18 stsp struct got_object_id *pack_hash = NULL;
992 20eb36d0 2020-03-18 stsp int ch, fetchfd = -1;
994 d9b4d0c0 2020-03-18 stsp TAILQ_INIT(&refs);
995 d9b4d0c0 2020-03-18 stsp TAILQ_INIT(&symrefs);
997 93658fb9 2020-03-18 stsp while ((ch = getopt(argc, argv, "b:")) != -1) {
998 93658fb9 2020-03-18 stsp switch (ch) {
1000 93658fb9 2020-03-18 stsp branch_filter = optarg;
1003 93658fb9 2020-03-18 stsp usage_clone();
1007 93658fb9 2020-03-18 stsp argc -= optind;
1008 93658fb9 2020-03-18 stsp argv += optind;
1009 93658fb9 2020-03-18 stsp uri = argv[0];
1010 93658fb9 2020-03-18 stsp if(argc == 1)
1011 93658fb9 2020-03-18 stsp dirname = NULL;
1012 93658fb9 2020-03-18 stsp else if(argc == 2)
1013 93658fb9 2020-03-18 stsp dirname = argv[1];
1015 93658fb9 2020-03-18 stsp usage_clone();
1017 09838ffc 2020-03-18 stsp err = got_fetch_parse_uri(&proto, &host, &port, &server_path,
1018 09838ffc 2020-03-18 stsp &repo_name, argv[0]);
1020 20eb36d0 2020-03-18 stsp goto done;
1022 20eb36d0 2020-03-18 stsp err = got_fetch_connect(&fetchfd, proto, host, port, server_path);
1024 09838ffc 2020-03-18 stsp goto done;
1026 bb64b798 2020-03-18 stsp if (dirname == NULL) {
1027 bb64b798 2020-03-18 stsp if (asprintf(&default_destdir, "%s.git", repo_name) == -1) {
1028 bb64b798 2020-03-18 stsp err = got_error_from_errno("asprintf");
1029 bb64b798 2020-03-18 stsp goto done;
1031 bb64b798 2020-03-18 stsp repo_path = default_destdir;
1033 bb64b798 2020-03-18 stsp repo_path = dirname;
1035 bb64b798 2020-03-18 stsp err = got_path_mkdir(repo_path);
1037 bb64b798 2020-03-18 stsp goto done;
1039 bb64b798 2020-03-18 stsp err = got_repo_init(repo_path);
1040 bb64b798 2020-03-18 stsp if (err != NULL)
1041 bb64b798 2020-03-18 stsp goto done;
1043 bb64b798 2020-03-18 stsp err = got_repo_open(&repo, repo_path, NULL);
1045 bb64b798 2020-03-18 stsp goto done;
1047 07e52fce 2020-03-18 stsp err = got_fetch_pack(&pack_hash, &refs, &symrefs, fetchfd,
1048 531c3985 2020-03-18 stsp repo, fetch_progress, host);
1050 d9b4d0c0 2020-03-18 stsp goto done;
1052 d9b4d0c0 2020-03-18 stsp err = got_object_id_str(&id_str, pack_hash);
1054 d9b4d0c0 2020-03-18 stsp goto done;
1055 d9b4d0c0 2020-03-18 stsp printf("Fetched %s.pack\n", id_str);
1056 d9b4d0c0 2020-03-18 stsp free(id_str);
1058 d9b4d0c0 2020-03-18 stsp /* Set up references provided with the pack file. */
1059 d9b4d0c0 2020-03-18 stsp TAILQ_FOREACH(pe, &refs, entry) {
1060 d9b4d0c0 2020-03-18 stsp const char *refname = pe->path;
1061 d9b4d0c0 2020-03-18 stsp struct got_object_id *id = pe->data;
1062 d9b4d0c0 2020-03-18 stsp struct got_reference *ref;
1064 d9b4d0c0 2020-03-18 stsp err = got_object_id_str(&id_str, id);
1066 d9b4d0c0 2020-03-18 stsp goto done;
1068 d9b4d0c0 2020-03-18 stsp err = got_ref_alloc(&ref, refname, id);
1069 d9b4d0c0 2020-03-18 stsp if (err) {
1070 d9b4d0c0 2020-03-18 stsp free(id_str);
1071 d9b4d0c0 2020-03-18 stsp goto done;
1074 d9b4d0c0 2020-03-18 stsp printf("%s: %s\n", got_ref_get_name(ref), id_str);
1075 d9b4d0c0 2020-03-18 stsp free(id_str);
1076 d9b4d0c0 2020-03-18 stsp err = got_ref_write(ref, repo);
1077 d9b4d0c0 2020-03-18 stsp got_ref_close(ref);
1079 d9b4d0c0 2020-03-18 stsp goto done;
1082 d9b4d0c0 2020-03-18 stsp /* Set the HEAD reference if the server provided one. */
1083 d9b4d0c0 2020-03-18 stsp TAILQ_FOREACH(pe, &symrefs, entry) {
1084 d9b4d0c0 2020-03-18 stsp struct got_reference *symref, *target_ref;
1085 d9b4d0c0 2020-03-18 stsp const char *refname = pe->path;
1086 d9b4d0c0 2020-03-18 stsp const char *target = pe->data;
1088 d9b4d0c0 2020-03-18 stsp if (strcmp(refname, GOT_REF_HEAD) != 0)
1091 d9b4d0c0 2020-03-18 stsp err = got_ref_open(&target_ref, repo, target, 0);
1092 d9b4d0c0 2020-03-18 stsp if (err) {
1093 d9b4d0c0 2020-03-18 stsp if (err->code == GOT_ERR_NOT_REF)
1095 d9b4d0c0 2020-03-18 stsp goto done;
1098 d9b4d0c0 2020-03-18 stsp err = got_ref_alloc_symref(&symref, GOT_REF_HEAD, target_ref);
1099 d9b4d0c0 2020-03-18 stsp got_ref_close(target_ref);
1101 d9b4d0c0 2020-03-18 stsp goto done;
1103 d9b4d0c0 2020-03-18 stsp printf("Setting %s to %s\n", GOT_REF_HEAD,
1104 d9b4d0c0 2020-03-18 stsp got_ref_get_symref_target(symref));
1106 d9b4d0c0 2020-03-18 stsp err = got_ref_write(symref, repo);
1107 d9b4d0c0 2020-03-18 stsp got_ref_close(symref);
1112 20eb36d0 2020-03-18 stsp if (fetchfd != -1 && close(fetchfd) == -1 && err == NULL)
1113 20eb36d0 2020-03-18 stsp err = got_error_from_errno("close");
1115 bb64b798 2020-03-18 stsp got_repo_close(repo);
1116 d9b4d0c0 2020-03-18 stsp TAILQ_FOREACH(pe, &refs, entry) {
1117 d9b4d0c0 2020-03-18 stsp free((void *)pe->path);
1118 d9b4d0c0 2020-03-18 stsp free(pe->data);
1120 d9b4d0c0 2020-03-18 stsp got_pathlist_free(&refs);
1121 d9b4d0c0 2020-03-18 stsp TAILQ_FOREACH(pe, &symrefs, entry) {
1122 d9b4d0c0 2020-03-18 stsp free((void *)pe->path);
1123 d9b4d0c0 2020-03-18 stsp free(pe->data);
1125 d9b4d0c0 2020-03-18 stsp got_pathlist_free(&symrefs);
1126 d9b4d0c0 2020-03-18 stsp free(pack_hash);
1127 09838ffc 2020-03-18 stsp free(proto);
1128 09838ffc 2020-03-18 stsp free(host);
1129 09838ffc 2020-03-18 stsp free(port);
1130 09838ffc 2020-03-18 stsp free(server_path);
1131 09838ffc 2020-03-18 stsp free(repo_name);
1132 bb64b798 2020-03-18 stsp free(default_destdir);
1133 09838ffc 2020-03-18 stsp return err;
1136 4ed7e80c 2018-05-20 stsp static const struct got_error *
1137 4b6c9460 2020-03-05 stsp checkout_ancestry_error(struct got_reference *ref, const char *commit_id_str)
1139 4b6c9460 2020-03-05 stsp static char msg[512];
1140 4b6c9460 2020-03-05 stsp const char *branch_name;
1142 4b6c9460 2020-03-05 stsp if (got_ref_is_symbolic(ref))
1143 4b6c9460 2020-03-05 stsp branch_name = got_ref_get_symref_target(ref);
1145 4b6c9460 2020-03-05 stsp branch_name = got_ref_get_name(ref);
1147 4b6c9460 2020-03-05 stsp if (strncmp("refs/heads/", branch_name, 11) == 0)
1148 4b6c9460 2020-03-05 stsp branch_name += 11;
1150 4b6c9460 2020-03-05 stsp snprintf(msg, sizeof(msg),
1151 4b6c9460 2020-03-05 stsp "target commit is not contained in branch '%s'; "
1152 4b6c9460 2020-03-05 stsp "the branch to use must be specified with -b; "
1153 4b6c9460 2020-03-05 stsp "if necessary a new branch can be created for "
1154 4b6c9460 2020-03-05 stsp "this commit with 'got branch -c %s BRANCH_NAME'",
1155 4b6c9460 2020-03-05 stsp branch_name, commit_id_str);
1157 4b6c9460 2020-03-05 stsp return got_error_msg(GOT_ERR_ANCESTRY, msg);
1160 4b6c9460 2020-03-05 stsp static const struct got_error *
1161 c09a553d 2018-03-12 stsp cmd_checkout(int argc, char *argv[])
1163 c09a553d 2018-03-12 stsp const struct got_error *error = NULL;
1164 c09a553d 2018-03-12 stsp struct got_repository *repo = NULL;
1165 c09a553d 2018-03-12 stsp struct got_reference *head_ref = NULL;
1166 c09a553d 2018-03-12 stsp struct got_worktree *worktree = NULL;
1167 c09a553d 2018-03-12 stsp char *repo_path = NULL;
1168 c09a553d 2018-03-12 stsp char *worktree_path = NULL;
1169 0bb8a95e 2018-03-12 stsp const char *path_prefix = "";
1170 08573d5b 2019-05-14 stsp const char *branch_name = GOT_REF_HEAD;
1171 8069f636 2019-01-12 stsp char *commit_id_str = NULL;
1172 bb51a5b4 2020-01-13 stsp int ch, same_path_prefix, allow_nonempty = 0;
1173 f2ea84fa 2019-07-27 stsp struct got_pathlist_head paths;
1174 7f47418f 2019-12-20 stsp struct got_checkout_progress_arg cpa;
1176 f2ea84fa 2019-07-27 stsp TAILQ_INIT(&paths);
1178 bb51a5b4 2020-01-13 stsp while ((ch = getopt(argc, argv, "b:c:Ep:")) != -1) {
1179 0bb8a95e 2018-03-12 stsp switch (ch) {
1181 08573d5b 2019-05-14 stsp branch_name = optarg;
1184 8069f636 2019-01-12 stsp commit_id_str = strdup(optarg);
1185 8069f636 2019-01-12 stsp if (commit_id_str == NULL)
1186 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
1189 bb51a5b4 2020-01-13 stsp allow_nonempty = 1;
1192 0bb8a95e 2018-03-12 stsp path_prefix = optarg;
1195 2deda0b9 2019-03-07 stsp usage_checkout();
1196 0bb8a95e 2018-03-12 stsp /* NOTREACHED */
1200 0bb8a95e 2018-03-12 stsp argc -= optind;
1201 0bb8a95e 2018-03-12 stsp argv += optind;
1203 6715a751 2018-03-16 stsp #ifndef PROFILE
1204 68ed9ba5 2019-02-10 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1205 68ed9ba5 2019-02-10 stsp "unveil", NULL) == -1)
1206 c09a553d 2018-03-12 stsp err(1, "pledge");
1208 0bb8a95e 2018-03-12 stsp if (argc == 1) {
1209 c09a553d 2018-03-12 stsp char *cwd, *base, *dotgit;
1210 76089277 2018-04-01 stsp repo_path = realpath(argv[0], NULL);
1211 76089277 2018-04-01 stsp if (repo_path == NULL)
1212 638f9024 2019-05-13 stsp return got_error_from_errno2("realpath", argv[0]);
1213 c09a553d 2018-03-12 stsp cwd = getcwd(NULL, 0);
1214 76089277 2018-04-01 stsp if (cwd == NULL) {
1215 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
1216 76089277 2018-04-01 stsp goto done;
1218 230a42bd 2019-05-11 jcs if (path_prefix[0]) {
1219 230a42bd 2019-05-11 jcs base = basename(path_prefix);
1220 230a42bd 2019-05-11 jcs if (base == NULL) {
1221 638f9024 2019-05-13 stsp error = got_error_from_errno2("basename",
1222 230a42bd 2019-05-11 jcs path_prefix);
1226 5d7c1dab 2018-04-01 stsp base = basename(repo_path);
1227 230a42bd 2019-05-11 jcs if (base == NULL) {
1228 638f9024 2019-05-13 stsp error = got_error_from_errno2("basename",
1229 230a42bd 2019-05-11 jcs repo_path);
1233 c09a553d 2018-03-12 stsp dotgit = strstr(base, ".git");
1234 c09a553d 2018-03-12 stsp if (dotgit)
1235 c09a553d 2018-03-12 stsp *dotgit = '\0';
1236 c09a553d 2018-03-12 stsp if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
1237 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
1238 c09a553d 2018-03-12 stsp free(cwd);
1239 76089277 2018-04-01 stsp goto done;
1241 c09a553d 2018-03-12 stsp free(cwd);
1242 0bb8a95e 2018-03-12 stsp } else if (argc == 2) {
1243 76089277 2018-04-01 stsp repo_path = realpath(argv[0], NULL);
1244 76089277 2018-04-01 stsp if (repo_path == NULL) {
1245 638f9024 2019-05-13 stsp error = got_error_from_errno2("realpath", argv[0]);
1246 76089277 2018-04-01 stsp goto done;
1248 f7b38925 2018-04-01 stsp worktree_path = realpath(argv[1], NULL);
1249 76089277 2018-04-01 stsp if (worktree_path == NULL) {
1250 b4b3a7dd 2019-07-22 stsp if (errno != ENOENT) {
1251 b4b3a7dd 2019-07-22 stsp error = got_error_from_errno2("realpath",
1253 b4b3a7dd 2019-07-22 stsp goto done;
1255 b4b3a7dd 2019-07-22 stsp worktree_path = strdup(argv[1]);
1256 b4b3a7dd 2019-07-22 stsp if (worktree_path == NULL) {
1257 b4b3a7dd 2019-07-22 stsp error = got_error_from_errno("strdup");
1258 b4b3a7dd 2019-07-22 stsp goto done;
1262 c09a553d 2018-03-12 stsp usage_checkout();
1264 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
1265 72151b04 2019-05-11 stsp got_path_strip_trailing_slashes(worktree_path);
1267 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
1268 c09a553d 2018-03-12 stsp if (error != NULL)
1269 c02c541e 2019-03-29 stsp goto done;
1271 c530dc23 2019-07-23 stsp /* Pre-create work tree path for unveil(2) */
1272 c530dc23 2019-07-23 stsp error = got_path_mkdir(worktree_path);
1273 c530dc23 2019-07-23 stsp if (error) {
1274 80c1b583 2019-08-07 stsp if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
1275 80c1b583 2019-08-07 stsp !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
1276 c530dc23 2019-07-23 stsp goto done;
1277 bb51a5b4 2020-01-13 stsp if (!allow_nonempty &&
1278 bb51a5b4 2020-01-13 stsp !got_path_dir_is_empty(worktree_path)) {
1279 c530dc23 2019-07-23 stsp error = got_error_path(worktree_path,
1280 c530dc23 2019-07-23 stsp GOT_ERR_DIR_NOT_EMPTY);
1281 c530dc23 2019-07-23 stsp goto done;
1285 c530dc23 2019-07-23 stsp error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
1286 c02c541e 2019-03-29 stsp if (error)
1287 c09a553d 2018-03-12 stsp goto done;
1289 08573d5b 2019-05-14 stsp error = got_ref_open(&head_ref, repo, branch_name, 0);
1290 c09a553d 2018-03-12 stsp if (error != NULL)
1291 c09a553d 2018-03-12 stsp goto done;
1293 0bb8a95e 2018-03-12 stsp error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
1294 d70b8e30 2018-12-27 stsp if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
1295 c09a553d 2018-03-12 stsp goto done;
1297 c09a553d 2018-03-12 stsp error = got_worktree_open(&worktree, worktree_path);
1298 c09a553d 2018-03-12 stsp if (error != NULL)
1299 c09a553d 2018-03-12 stsp goto done;
1301 e5dc7198 2018-12-29 stsp error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
1302 e5dc7198 2018-12-29 stsp path_prefix);
1303 e5dc7198 2018-12-29 stsp if (error != NULL)
1304 e5dc7198 2018-12-29 stsp goto done;
1305 e5dc7198 2018-12-29 stsp if (!same_path_prefix) {
1306 49520a32 2018-12-29 stsp error = got_error(GOT_ERR_PATH_PREFIX);
1307 49520a32 2018-12-29 stsp goto done;
1310 8069f636 2019-01-12 stsp if (commit_id_str) {
1311 04f57cb3 2019-07-25 stsp struct got_object_id *commit_id;
1312 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
1313 71a27632 2020-01-15 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
1314 30837e32 2019-07-25 stsp if (error)
1315 8069f636 2019-01-12 stsp goto done;
1316 024e9686 2019-05-14 stsp error = check_linear_ancestry(commit_id,
1317 3aef623b 2019-10-15 stsp got_worktree_get_base_commit_id(worktree), 0, repo);
1318 8069f636 2019-01-12 stsp if (error != NULL) {
1319 8069f636 2019-01-12 stsp free(commit_id);
1320 4b6c9460 2020-03-05 stsp if (error->code == GOT_ERR_ANCESTRY) {
1321 4b6c9460 2020-03-05 stsp error = checkout_ancestry_error(
1322 4b6c9460 2020-03-05 stsp head_ref, commit_id_str);
1324 8069f636 2019-01-12 stsp goto done;
1326 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
1327 4b6c9460 2020-03-05 stsp if (error) {
1328 4b6c9460 2020-03-05 stsp if (error->code == GOT_ERR_ANCESTRY) {
1329 4b6c9460 2020-03-05 stsp error = checkout_ancestry_error(
1330 4b6c9460 2020-03-05 stsp head_ref, commit_id_str);
1332 45d344f6 2019-05-14 stsp goto done;
1334 8069f636 2019-01-12 stsp error = got_worktree_set_base_commit_id(worktree, repo,
1335 8069f636 2019-01-12 stsp commit_id);
1336 8069f636 2019-01-12 stsp free(commit_id);
1337 8069f636 2019-01-12 stsp if (error)
1338 8069f636 2019-01-12 stsp goto done;
1341 adc19d55 2019-07-28 stsp error = got_pathlist_append(&paths, "", NULL);
1342 f2ea84fa 2019-07-27 stsp if (error)
1343 f2ea84fa 2019-07-27 stsp goto done;
1344 7f47418f 2019-12-20 stsp cpa.worktree_path = worktree_path;
1345 7f47418f 2019-12-20 stsp cpa.had_base_commit_ref_error = 0;
1346 f2ea84fa 2019-07-27 stsp error = got_worktree_checkout_files(worktree, &paths, repo,
1347 7f47418f 2019-12-20 stsp checkout_progress, &cpa, check_cancelled, NULL);
1348 c09a553d 2018-03-12 stsp if (error != NULL)
1349 c09a553d 2018-03-12 stsp goto done;
1351 b65ae19a 2018-04-24 stsp printf("Now shut up and hack\n");
1352 7f47418f 2019-12-20 stsp if (cpa.had_base_commit_ref_error)
1353 7f47418f 2019-12-20 stsp show_worktree_base_ref_warning();
1355 f2ea84fa 2019-07-27 stsp got_pathlist_free(&paths);
1356 8069f636 2019-01-12 stsp free(commit_id_str);
1357 76089277 2018-04-01 stsp free(repo_path);
1358 507dc3bb 2018-12-29 stsp free(worktree_path);
1359 507dc3bb 2018-12-29 stsp return error;
1362 507dc3bb 2018-12-29 stsp __dead static void
1363 507dc3bb 2018-12-29 stsp usage_update(void)
1365 f2ea84fa 2019-07-27 stsp fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path ...]\n",
1366 507dc3bb 2018-12-29 stsp getprogname());
1370 1ee397ad 2019-07-12 stsp static const struct got_error *
1371 507dc3bb 2018-12-29 stsp update_progress(void *arg, unsigned char status, const char *path)
1373 784955db 2019-01-12 stsp int *did_something = arg;
1375 7f47418f 2019-12-20 stsp if (status == GOT_STATUS_EXISTS ||
1376 7f47418f 2019-12-20 stsp status == GOT_STATUS_BASE_REF_ERR)
1377 1ee397ad 2019-07-12 stsp return NULL;
1379 1545c615 2019-02-10 stsp *did_something = 1;
1381 a484d721 2019-06-10 stsp /* Base commit bump happens silently. */
1382 a484d721 2019-06-10 stsp if (status == GOT_STATUS_BUMP_BASE)
1383 1ee397ad 2019-07-12 stsp return NULL;
1385 507dc3bb 2018-12-29 stsp while (path[0] == '/')
1387 507dc3bb 2018-12-29 stsp printf("%c %s\n", status, path);
1388 1ee397ad 2019-07-12 stsp return NULL;
1391 be7061eb 2018-12-30 stsp static const struct got_error *
1392 a1fb16d8 2019-05-24 stsp switch_head_ref(struct got_reference *head_ref,
1393 a1fb16d8 2019-05-24 stsp struct got_object_id *commit_id, struct got_worktree *worktree,
1394 a1fb16d8 2019-05-24 stsp struct got_repository *repo)
1396 a1fb16d8 2019-05-24 stsp const struct got_error *err = NULL;
1397 a1fb16d8 2019-05-24 stsp char *base_id_str;
1398 a1fb16d8 2019-05-24 stsp int ref_has_moved = 0;
1400 a1fb16d8 2019-05-24 stsp /* Trivial case: switching between two different references. */
1401 a1fb16d8 2019-05-24 stsp if (strcmp(got_ref_get_name(head_ref),
1402 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree)) != 0) {
1403 a1fb16d8 2019-05-24 stsp printf("Switching work tree from %s to %s\n",
1404 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree),
1405 a1fb16d8 2019-05-24 stsp got_ref_get_name(head_ref));
1406 a1fb16d8 2019-05-24 stsp return got_worktree_set_head_ref(worktree, head_ref);
1409 a1fb16d8 2019-05-24 stsp err = check_linear_ancestry(commit_id,
1410 3aef623b 2019-10-15 stsp got_worktree_get_base_commit_id(worktree), 0, repo);
1411 a1fb16d8 2019-05-24 stsp if (err) {
1412 a1fb16d8 2019-05-24 stsp if (err->code != GOT_ERR_ANCESTRY)
1413 a1fb16d8 2019-05-24 stsp return err;
1414 a1fb16d8 2019-05-24 stsp ref_has_moved = 1;
1416 a1fb16d8 2019-05-24 stsp if (!ref_has_moved)
1417 a1fb16d8 2019-05-24 stsp return NULL;
1419 a1fb16d8 2019-05-24 stsp /* Switching to a rebased branch with the same reference name. */
1420 a1fb16d8 2019-05-24 stsp err = got_object_id_str(&base_id_str,
1421 a1fb16d8 2019-05-24 stsp got_worktree_get_base_commit_id(worktree));
1423 a1fb16d8 2019-05-24 stsp return err;
1424 a1fb16d8 2019-05-24 stsp printf("Reference %s now points at a different branch\n",
1425 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree));
1426 a1fb16d8 2019-05-24 stsp printf("Switching work tree from %s to %s\n", base_id_str,
1427 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree));
1428 0ebf8283 2019-07-24 stsp return NULL;
1431 0ebf8283 2019-07-24 stsp static const struct got_error *
1432 0ebf8283 2019-07-24 stsp check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
1434 0ebf8283 2019-07-24 stsp const struct got_error *err;
1435 0ebf8283 2019-07-24 stsp int in_progress;
1437 0ebf8283 2019-07-24 stsp err = got_worktree_rebase_in_progress(&in_progress, worktree);
1439 0ebf8283 2019-07-24 stsp return err;
1440 0ebf8283 2019-07-24 stsp if (in_progress)
1441 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_REBASING);
1443 0ebf8283 2019-07-24 stsp err = got_worktree_histedit_in_progress(&in_progress, worktree);
1445 0ebf8283 2019-07-24 stsp return err;
1446 0ebf8283 2019-07-24 stsp if (in_progress)
1447 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_HISTEDIT_BUSY);
1449 a1fb16d8 2019-05-24 stsp return NULL;
1452 a5edda0a 2019-07-27 stsp static const struct got_error *
1453 a5edda0a 2019-07-27 stsp get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
1454 a5edda0a 2019-07-27 stsp char *argv[], struct got_worktree *worktree)
1456 a0de39f3 2019-08-09 stsp const struct got_error *err = NULL;
1457 a5edda0a 2019-07-27 stsp char *path;
1460 a5edda0a 2019-07-27 stsp if (argc == 0) {
1461 a5edda0a 2019-07-27 stsp path = strdup("");
1462 a5edda0a 2019-07-27 stsp if (path == NULL)
1463 a5edda0a 2019-07-27 stsp return got_error_from_errno("strdup");
1464 adc19d55 2019-07-28 stsp return got_pathlist_append(paths, path, NULL);
1467 a5edda0a 2019-07-27 stsp for (i = 0; i < argc; i++) {
1468 a5edda0a 2019-07-27 stsp err = got_worktree_resolve_path(&path, worktree, argv[i]);
1471 adc19d55 2019-07-28 stsp err = got_pathlist_append(paths, path, NULL);
1472 a5edda0a 2019-07-27 stsp if (err) {
1473 a5edda0a 2019-07-27 stsp free(path);
1478 a5edda0a 2019-07-27 stsp return err;
1481 a1fb16d8 2019-05-24 stsp static const struct got_error *
1482 507dc3bb 2018-12-29 stsp cmd_update(int argc, char *argv[])
1484 507dc3bb 2018-12-29 stsp const struct got_error *error = NULL;
1485 507dc3bb 2018-12-29 stsp struct got_repository *repo = NULL;
1486 507dc3bb 2018-12-29 stsp struct got_worktree *worktree = NULL;
1487 f2ea84fa 2019-07-27 stsp char *worktree_path = NULL;
1488 507dc3bb 2018-12-29 stsp struct got_object_id *commit_id = NULL;
1489 9c4b8182 2019-01-02 stsp char *commit_id_str = NULL;
1490 024e9686 2019-05-14 stsp const char *branch_name = NULL;
1491 024e9686 2019-05-14 stsp struct got_reference *head_ref = NULL;
1492 f2ea84fa 2019-07-27 stsp struct got_pathlist_head paths;
1493 f2ea84fa 2019-07-27 stsp struct got_pathlist_entry *pe;
1494 0ebf8283 2019-07-24 stsp int ch, did_something = 0;
1496 f2ea84fa 2019-07-27 stsp TAILQ_INIT(&paths);
1498 024e9686 2019-05-14 stsp while ((ch = getopt(argc, argv, "b:c:")) != -1) {
1499 507dc3bb 2018-12-29 stsp switch (ch) {
1501 024e9686 2019-05-14 stsp branch_name = optarg;
1504 9c4b8182 2019-01-02 stsp commit_id_str = strdup(optarg);
1505 9c4b8182 2019-01-02 stsp if (commit_id_str == NULL)
1506 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
1509 2deda0b9 2019-03-07 stsp usage_update();
1510 507dc3bb 2018-12-29 stsp /* NOTREACHED */
1514 507dc3bb 2018-12-29 stsp argc -= optind;
1515 507dc3bb 2018-12-29 stsp argv += optind;
1517 507dc3bb 2018-12-29 stsp #ifndef PROFILE
1518 68ed9ba5 2019-02-10 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1519 68ed9ba5 2019-02-10 stsp "unveil", NULL) == -1)
1520 507dc3bb 2018-12-29 stsp err(1, "pledge");
1522 c4cdcb68 2019-04-03 stsp worktree_path = getcwd(NULL, 0);
1523 c4cdcb68 2019-04-03 stsp if (worktree_path == NULL) {
1524 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
1525 c4cdcb68 2019-04-03 stsp goto done;
1527 c4cdcb68 2019-04-03 stsp error = got_worktree_open(&worktree, worktree_path);
1528 7d5807f4 2019-07-11 stsp if (error)
1529 7d5807f4 2019-07-11 stsp goto done;
1531 0ebf8283 2019-07-24 stsp error = check_rebase_or_histedit_in_progress(worktree);
1532 f2ea84fa 2019-07-27 stsp if (error)
1533 f2ea84fa 2019-07-27 stsp goto done;
1535 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
1537 507dc3bb 2018-12-29 stsp if (error != NULL)
1538 507dc3bb 2018-12-29 stsp goto done;
1540 97430839 2019-03-11 stsp error = apply_unveil(got_repo_get_path(repo), 0,
1541 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
1542 087fb88c 2019-08-04 stsp if (error)
1543 087fb88c 2019-08-04 stsp goto done;
1545 087fb88c 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
1546 0266afb7 2019-01-04 stsp if (error)
1547 0266afb7 2019-01-04 stsp goto done;
1549 a1fb16d8 2019-05-24 stsp error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
1550 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree), 0);
1551 024e9686 2019-05-14 stsp if (error != NULL)
1552 024e9686 2019-05-14 stsp goto done;
1553 507dc3bb 2018-12-29 stsp if (commit_id_str == NULL) {
1554 507dc3bb 2018-12-29 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
1555 9c4b8182 2019-01-02 stsp if (error != NULL)
1556 9c4b8182 2019-01-02 stsp goto done;
1557 9c4b8182 2019-01-02 stsp error = got_object_id_str(&commit_id_str, commit_id);
1558 507dc3bb 2018-12-29 stsp if (error != NULL)
1559 507dc3bb 2018-12-29 stsp goto done;
1561 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
1562 71a27632 2020-01-15 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
1563 04f57cb3 2019-07-25 stsp free(commit_id_str);
1564 bb787f09 2019-07-25 stsp commit_id_str = NULL;
1565 30837e32 2019-07-25 stsp if (error)
1566 a297e751 2019-07-11 stsp goto done;
1567 a297e751 2019-07-11 stsp error = got_object_id_str(&commit_id_str, commit_id);
1568 a297e751 2019-07-11 stsp if (error)
1569 507dc3bb 2018-12-29 stsp goto done;
1572 a1fb16d8 2019-05-24 stsp if (branch_name) {
1573 024e9686 2019-05-14 stsp struct got_object_id *head_commit_id;
1574 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, &paths, entry) {
1575 f2b16ada 2019-08-02 stsp if (pe->path_len == 0)
1577 f2ea84fa 2019-07-27 stsp error = got_error_msg(GOT_ERR_BAD_PATH,
1578 f2ea84fa 2019-07-27 stsp "switching between branches requires that "
1579 f2ea84fa 2019-07-27 stsp "the entire work tree gets updated");
1580 024e9686 2019-05-14 stsp goto done;
1582 024e9686 2019-05-14 stsp error = got_ref_resolve(&head_commit_id, repo, head_ref);
1583 024e9686 2019-05-14 stsp if (error)
1584 024e9686 2019-05-14 stsp goto done;
1585 3aef623b 2019-10-15 stsp error = check_linear_ancestry(commit_id, head_commit_id, 0,
1587 a367ff0f 2019-05-14 stsp free(head_commit_id);
1588 024e9686 2019-05-14 stsp if (error != NULL)
1589 024e9686 2019-05-14 stsp goto done;
1590 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
1591 a367ff0f 2019-05-14 stsp if (error)
1592 a367ff0f 2019-05-14 stsp goto done;
1593 a1fb16d8 2019-05-24 stsp error = switch_head_ref(head_ref, commit_id, worktree, repo);
1594 024e9686 2019-05-14 stsp if (error)
1595 024e9686 2019-05-14 stsp goto done;
1597 024e9686 2019-05-14 stsp error = check_linear_ancestry(commit_id,
1598 3aef623b 2019-10-15 stsp got_worktree_get_base_commit_id(worktree), 0, repo);
1599 a367ff0f 2019-05-14 stsp if (error != NULL) {
1600 a367ff0f 2019-05-14 stsp if (error->code == GOT_ERR_ANCESTRY)
1601 a367ff0f 2019-05-14 stsp error = got_error(GOT_ERR_BRANCH_MOVED);
1602 024e9686 2019-05-14 stsp goto done;
1604 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
1605 a367ff0f 2019-05-14 stsp if (error)
1606 a367ff0f 2019-05-14 stsp goto done;
1609 507dc3bb 2018-12-29 stsp if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
1610 507dc3bb 2018-12-29 stsp commit_id) != 0) {
1611 507dc3bb 2018-12-29 stsp error = got_worktree_set_base_commit_id(worktree, repo,
1612 507dc3bb 2018-12-29 stsp commit_id);
1613 507dc3bb 2018-12-29 stsp if (error)
1614 507dc3bb 2018-12-29 stsp goto done;
1617 f2ea84fa 2019-07-27 stsp error = got_worktree_checkout_files(worktree, &paths, repo,
1618 6bad629b 2019-02-04 stsp update_progress, &did_something, check_cancelled, NULL);
1619 507dc3bb 2018-12-29 stsp if (error != NULL)
1620 507dc3bb 2018-12-29 stsp goto done;
1622 784955db 2019-01-12 stsp if (did_something)
1623 784955db 2019-01-12 stsp printf("Updated to commit %s\n", commit_id_str);
1625 784955db 2019-01-12 stsp printf("Already up-to-date\n");
1627 c09a553d 2018-03-12 stsp free(worktree_path);
1628 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, &paths, entry)
1629 f2ea84fa 2019-07-27 stsp free((char *)pe->path);
1630 f2ea84fa 2019-07-27 stsp got_pathlist_free(&paths);
1631 507dc3bb 2018-12-29 stsp free(commit_id);
1632 9c4b8182 2019-01-02 stsp free(commit_id_str);
1633 c09a553d 2018-03-12 stsp return error;
1636 f42b1b34 2018-03-12 stsp static const struct got_error *
1637 44392932 2019-08-25 stsp diff_blobs(struct got_object_id *blob_id1, struct got_object_id *blob_id2,
1638 63035f9f 2019-10-06 stsp const char *path, int diff_context, int ignore_whitespace,
1639 63035f9f 2019-10-06 stsp struct got_repository *repo)
1641 f42b1b34 2018-03-12 stsp const struct got_error *err = NULL;
1642 44392932 2019-08-25 stsp struct got_blob_object *blob1 = NULL, *blob2 = NULL;
1644 44392932 2019-08-25 stsp if (blob_id1) {
1645 44392932 2019-08-25 stsp err = got_object_open_as_blob(&blob1, repo, blob_id1, 8192);
1647 44392932 2019-08-25 stsp goto done;
1650 44392932 2019-08-25 stsp err = got_object_open_as_blob(&blob2, repo, blob_id2, 8192);
1652 44392932 2019-08-25 stsp goto done;
1654 44392932 2019-08-25 stsp while (path[0] == '/')
1656 44392932 2019-08-25 stsp err = got_diff_blob(blob1, blob2, path, path, diff_context,
1657 63035f9f 2019-10-06 stsp ignore_whitespace, stdout);
1659 44392932 2019-08-25 stsp if (blob1)
1660 44392932 2019-08-25 stsp got_object_blob_close(blob1);
1661 44392932 2019-08-25 stsp got_object_blob_close(blob2);
1662 44392932 2019-08-25 stsp return err;
1665 44392932 2019-08-25 stsp static const struct got_error *
1666 44392932 2019-08-25 stsp diff_trees(struct got_object_id *tree_id1, struct got_object_id *tree_id2,
1667 63035f9f 2019-10-06 stsp const char *path, int diff_context, int ignore_whitespace,
1668 63035f9f 2019-10-06 stsp struct got_repository *repo)
1670 44392932 2019-08-25 stsp const struct got_error *err = NULL;
1671 44392932 2019-08-25 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
1672 44392932 2019-08-25 stsp struct got_diff_blob_output_unidiff_arg arg;
1674 44392932 2019-08-25 stsp if (tree_id1) {
1675 44392932 2019-08-25 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
1677 44392932 2019-08-25 stsp goto done;
1680 44392932 2019-08-25 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
1682 0f2b3dca 2018-12-22 stsp goto done;
1684 aaa13589 2019-06-01 stsp arg.diff_context = diff_context;
1685 63035f9f 2019-10-06 stsp arg.ignore_whitespace = ignore_whitespace;
1686 aaa13589 2019-06-01 stsp arg.outfile = stdout;
1687 44392932 2019-08-25 stsp while (path[0] == '/')
1689 44392932 2019-08-25 stsp err = got_diff_tree(tree1, tree2, path, path, repo,
1690 31b4484f 2019-07-27 stsp got_diff_blob_output_unidiff, &arg, 1);
1692 79109fed 2018-03-27 stsp if (tree1)
1693 79109fed 2018-03-27 stsp got_object_tree_close(tree1);
1694 366e0a5f 2019-10-10 stsp if (tree2)
1695 366e0a5f 2019-10-10 stsp got_object_tree_close(tree2);
1696 44392932 2019-08-25 stsp return err;
1699 44392932 2019-08-25 stsp static const struct got_error *
1700 44392932 2019-08-25 stsp print_patch(struct got_commit_object *commit, struct got_object_id *id,
1701 44392932 2019-08-25 stsp const char *path, int diff_context, struct got_repository *repo)
1703 44392932 2019-08-25 stsp const struct got_error *err = NULL;
1704 44392932 2019-08-25 stsp struct got_commit_object *pcommit = NULL;
1705 44392932 2019-08-25 stsp char *id_str1 = NULL, *id_str2 = NULL;
1706 44392932 2019-08-25 stsp struct got_object_id *obj_id1 = NULL, *obj_id2 = NULL;
1707 44392932 2019-08-25 stsp struct got_object_qid *qid;
1709 44392932 2019-08-25 stsp qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1710 44392932 2019-08-25 stsp if (qid != NULL) {
1711 44392932 2019-08-25 stsp err = got_object_open_as_commit(&pcommit, repo,
1714 44392932 2019-08-25 stsp return err;
1717 44392932 2019-08-25 stsp if (path && path[0] != '\0') {
1718 44392932 2019-08-25 stsp int obj_type;
1719 44392932 2019-08-25 stsp err = got_object_id_by_path(&obj_id2, repo, id, path);
1721 44392932 2019-08-25 stsp goto done;
1722 44392932 2019-08-25 stsp err = got_object_id_str(&id_str2, obj_id2);
1723 44392932 2019-08-25 stsp if (err) {
1724 44392932 2019-08-25 stsp free(obj_id2);
1725 44392932 2019-08-25 stsp goto done;
1727 44392932 2019-08-25 stsp if (pcommit) {
1728 44392932 2019-08-25 stsp err = got_object_id_by_path(&obj_id1, repo,
1729 44392932 2019-08-25 stsp qid->id, path);
1730 44392932 2019-08-25 stsp if (err) {
1731 44392932 2019-08-25 stsp free(obj_id2);
1732 44392932 2019-08-25 stsp goto done;
1734 44392932 2019-08-25 stsp err = got_object_id_str(&id_str1, obj_id1);
1735 44392932 2019-08-25 stsp if (err) {
1736 44392932 2019-08-25 stsp free(obj_id2);
1737 44392932 2019-08-25 stsp goto done;
1740 44392932 2019-08-25 stsp err = got_object_get_type(&obj_type, repo, obj_id2);
1741 44392932 2019-08-25 stsp if (err) {
1742 44392932 2019-08-25 stsp free(obj_id2);
1743 44392932 2019-08-25 stsp goto done;
1745 44392932 2019-08-25 stsp printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1746 44392932 2019-08-25 stsp switch (obj_type) {
1747 44392932 2019-08-25 stsp case GOT_OBJ_TYPE_BLOB:
1748 44392932 2019-08-25 stsp err = diff_blobs(obj_id1, obj_id2, path, diff_context,
1751 44392932 2019-08-25 stsp case GOT_OBJ_TYPE_TREE:
1752 44392932 2019-08-25 stsp err = diff_trees(obj_id1, obj_id2, path, diff_context,
1756 44392932 2019-08-25 stsp err = got_error(GOT_ERR_OBJ_TYPE);
1759 44392932 2019-08-25 stsp free(obj_id1);
1760 44392932 2019-08-25 stsp free(obj_id2);
1762 44392932 2019-08-25 stsp obj_id2 = got_object_commit_get_tree_id(commit);
1763 44392932 2019-08-25 stsp err = got_object_id_str(&id_str2, obj_id2);
1765 44392932 2019-08-25 stsp goto done;
1766 44392932 2019-08-25 stsp obj_id1 = got_object_commit_get_tree_id(pcommit);
1767 44392932 2019-08-25 stsp err = got_object_id_str(&id_str1, obj_id1);
1769 44392932 2019-08-25 stsp goto done;
1770 44392932 2019-08-25 stsp printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1771 63035f9f 2019-10-06 stsp err = diff_trees(obj_id1, obj_id2, "", diff_context, 0, repo);
1774 0f2b3dca 2018-12-22 stsp free(id_str1);
1775 0f2b3dca 2018-12-22 stsp free(id_str2);
1776 44392932 2019-08-25 stsp if (pcommit)
1777 44392932 2019-08-25 stsp got_object_commit_close(pcommit);
1778 79109fed 2018-03-27 stsp return err;
1781 4bb494d5 2018-06-16 stsp static char *
1782 6c281f94 2018-06-11 stsp get_datestr(time_t *time, char *datebuf)
1784 09867e48 2019-08-13 stsp struct tm mytm, *tm;
1785 09867e48 2019-08-13 stsp char *p, *s;
1787 09867e48 2019-08-13 stsp tm = gmtime_r(time, &mytm);
1788 09867e48 2019-08-13 stsp if (tm == NULL)
1789 09867e48 2019-08-13 stsp return NULL;
1790 09867e48 2019-08-13 stsp s = asctime_r(tm, datebuf);
1791 09867e48 2019-08-13 stsp if (s == NULL)
1792 09867e48 2019-08-13 stsp return NULL;
1793 6c281f94 2018-06-11 stsp p = strchr(s, '\n');
1795 6c281f94 2018-06-11 stsp *p = '\0';
1799 6841bf13 2019-11-29 kn static const struct got_error *
1800 6841bf13 2019-11-29 kn match_logmsg(int *have_match, struct got_object_id *id,
1801 6841bf13 2019-11-29 kn struct got_commit_object *commit, regex_t *regex)
1803 6841bf13 2019-11-29 kn const struct got_error *err = NULL;
1804 6841bf13 2019-11-29 kn regmatch_t regmatch;
1805 6841bf13 2019-11-29 kn char *id_str = NULL, *logmsg = NULL;
1807 6841bf13 2019-11-29 kn *have_match = 0;
1809 6841bf13 2019-11-29 kn err = got_object_id_str(&id_str, id);
1813 6841bf13 2019-11-29 kn err = got_object_commit_get_logmsg(&logmsg, commit);
1817 6841bf13 2019-11-29 kn if (regexec(regex, logmsg, 1, ®match, 0) == 0)
1818 6841bf13 2019-11-29 kn *have_match = 1;
1820 6841bf13 2019-11-29 kn free(id_str);
1821 6841bf13 2019-11-29 kn free(logmsg);
1825 dc424a06 2019-08-07 stsp #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
1827 79109fed 2018-03-27 stsp static const struct got_error *
1828 79109fed 2018-03-27 stsp print_commit(struct got_commit_object *commit, struct got_object_id *id,
1829 44392932 2019-08-25 stsp struct got_repository *repo, const char *path, int show_patch,
1830 44392932 2019-08-25 stsp int diff_context, struct got_reflist_head *refs)
1832 79109fed 2018-03-27 stsp const struct got_error *err = NULL;
1833 621015ac 2018-07-23 stsp char *id_str, *datestr, *logmsg0, *logmsg, *line;
1834 6c281f94 2018-06-11 stsp char datebuf[26];
1835 45d799e2 2018-12-23 stsp time_t committer_time;
1836 45d799e2 2018-12-23 stsp const char *author, *committer;
1837 199a4027 2019-02-02 stsp char *refs_str = NULL;
1838 199a4027 2019-02-02 stsp struct got_reflist_entry *re;
1840 199a4027 2019-02-02 stsp SIMPLEQ_FOREACH(re, refs, entry) {
1842 199a4027 2019-02-02 stsp const char *name;
1843 a436ad14 2019-08-13 stsp struct got_tag_object *tag = NULL;
1846 199a4027 2019-02-02 stsp name = got_ref_get_name(re->ref);
1847 d9498b20 2019-02-05 stsp if (strcmp(name, GOT_REF_HEAD) == 0)
1849 199a4027 2019-02-02 stsp if (strncmp(name, "refs/", 5) == 0)
1850 199a4027 2019-02-02 stsp name += 5;
1851 7143d404 2019-03-12 stsp if (strncmp(name, "got/", 4) == 0)
1853 e34f9ed6 2019-02-02 stsp if (strncmp(name, "heads/", 6) == 0)
1854 e34f9ed6 2019-02-02 stsp name += 6;
1855 141c2bff 2019-02-04 stsp if (strncmp(name, "remotes/", 8) == 0)
1856 141c2bff 2019-02-04 stsp name += 8;
1857 a436ad14 2019-08-13 stsp if (strncmp(name, "tags/", 5) == 0) {
1858 a436ad14 2019-08-13 stsp err = got_object_open_as_tag(&tag, repo, re->id);
1859 5d844a1e 2019-08-13 stsp if (err) {
1860 5d844a1e 2019-08-13 stsp if (err->code != GOT_ERR_OBJ_TYPE)
1861 5d844a1e 2019-08-13 stsp return err;
1862 5d844a1e 2019-08-13 stsp /* Ref points at something other than a tag. */
1863 5d844a1e 2019-08-13 stsp err = NULL;
1864 5d844a1e 2019-08-13 stsp tag = NULL;
1867 a436ad14 2019-08-13 stsp cmp = got_object_id_cmp(tag ?
1868 a436ad14 2019-08-13 stsp got_object_tag_get_object_id(tag) : re->id, id);
1870 a436ad14 2019-08-13 stsp got_object_tag_close(tag);
1871 a436ad14 2019-08-13 stsp if (cmp != 0)
1873 199a4027 2019-02-02 stsp s = refs_str;
1874 199a4027 2019-02-02 stsp if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
1875 199a4027 2019-02-02 stsp name) == -1) {
1876 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1878 34ca4898 2019-08-13 stsp return err;
1882 832c249c 2018-06-10 stsp err = got_object_id_str(&id_str, id);
1884 8bf5b3c9 2018-03-17 stsp return err;
1886 dc424a06 2019-08-07 stsp printf(GOT_COMMIT_SEP_STR);
1887 199a4027 2019-02-02 stsp printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
1888 199a4027 2019-02-02 stsp refs_str ? refs_str : "", refs_str ? ")" : "");
1889 832c249c 2018-06-10 stsp free(id_str);
1890 d3d493d7 2019-02-21 stsp id_str = NULL;
1891 d3d493d7 2019-02-21 stsp free(refs_str);
1892 d3d493d7 2019-02-21 stsp refs_str = NULL;
1893 45d799e2 2018-12-23 stsp printf("from: %s\n", got_object_commit_get_author(commit));
1894 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
1895 45d799e2 2018-12-23 stsp datestr = get_datestr(&committer_time, datebuf);
1896 09867e48 2019-08-13 stsp if (datestr)
1897 09867e48 2019-08-13 stsp printf("date: %s UTC\n", datestr);
1898 45d799e2 2018-12-23 stsp author = got_object_commit_get_author(commit);
1899 45d799e2 2018-12-23 stsp committer = got_object_commit_get_committer(commit);
1900 45d799e2 2018-12-23 stsp if (strcmp(author, committer) != 0)
1901 45d799e2 2018-12-23 stsp printf("via: %s\n", committer);
1902 45d799e2 2018-12-23 stsp if (got_object_commit_get_nparents(commit) > 1) {
1903 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
1904 79f35eb3 2018-06-11 stsp struct got_object_qid *qid;
1905 3fe1abad 2018-06-10 stsp int n = 1;
1906 45d799e2 2018-12-23 stsp parent_ids = got_object_commit_get_parent_ids(commit);
1907 45d799e2 2018-12-23 stsp SIMPLEQ_FOREACH(qid, parent_ids, entry) {
1908 79f35eb3 2018-06-11 stsp err = got_object_id_str(&id_str, qid->id);
1910 a0603db2 2018-06-10 stsp return err;
1911 3fe1abad 2018-06-10 stsp printf("parent %d: %s\n", n++, id_str);
1912 a0603db2 2018-06-10 stsp free(id_str);
1916 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
1918 5943eee2 2019-08-13 stsp return err;
1920 621015ac 2018-07-23 stsp logmsg = logmsg0;
1922 832c249c 2018-06-10 stsp line = strsep(&logmsg, "\n");
1924 832c249c 2018-06-10 stsp printf(" %s\n", line);
1925 832c249c 2018-06-10 stsp } while (line);
1926 621015ac 2018-07-23 stsp free(logmsg0);
1928 971751ac 2018-03-27 stsp if (show_patch) {
1929 44392932 2019-08-25 stsp err = print_patch(commit, id, path, diff_context, repo);
1930 971751ac 2018-03-27 stsp if (err == 0)
1931 971751ac 2018-03-27 stsp printf("\n");
1934 cbe7f848 2019-02-11 stsp if (fflush(stdout) != 0 && err == NULL)
1935 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
1936 07862c20 2018-09-15 stsp return err;
1939 f42b1b34 2018-03-12 stsp static const struct got_error *
1940 15a94983 2018-12-23 stsp print_commits(struct got_object_id *root_id, struct got_repository *repo,
1941 463a997a 2019-12-08 kn const char *path, int show_patch, const char *search_pattern,
1942 48c8c60d 2020-01-27 stsp int diff_context, int limit, int log_branches,
1943 463a997a 2019-12-08 kn struct got_reflist_head *refs)
1945 f42b1b34 2018-03-12 stsp const struct got_error *err;
1946 372ccdbb 2018-06-10 stsp struct got_commit_graph *graph;
1947 6841bf13 2019-11-29 kn regex_t regex;
1948 6841bf13 2019-11-29 kn int have_match;
1950 6841bf13 2019-11-29 kn if (search_pattern &&
1951 6841bf13 2019-11-29 kn regcomp(®ex, search_pattern, REG_EXTENDED | REG_NOSUB | REG_NEWLINE))
1952 6841bf13 2019-11-29 kn return got_error_msg(GOT_ERR_REGEX, search_pattern);
1954 48c8c60d 2020-01-27 stsp err = got_commit_graph_open(&graph, path, !log_branches);
1956 f42b1b34 2018-03-12 stsp return err;
1957 6fb7cd11 2019-08-22 stsp err = got_commit_graph_iter_start(graph, root_id, repo,
1958 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
1960 fcc85cad 2018-07-22 stsp goto done;
1962 372ccdbb 2018-06-10 stsp struct got_commit_object *commit;
1963 372ccdbb 2018-06-10 stsp struct got_object_id *id;
1965 84453469 2018-11-11 stsp if (sigint_received || sigpipe_received)
1968 ee780d5c 2020-01-04 stsp err = got_commit_graph_iter_next(&id, graph, repo,
1969 ee780d5c 2020-01-04 stsp check_cancelled, NULL);
1970 372ccdbb 2018-06-10 stsp if (err) {
1971 ee780d5c 2020-01-04 stsp if (err->code == GOT_ERR_ITER_COMPLETED)
1972 9ba79e04 2018-06-11 stsp err = NULL;
1975 b43fbaa0 2018-06-11 stsp if (id == NULL)
1978 f8e900f3 2018-06-11 stsp err = got_object_open_as_commit(&commit, repo, id);
1982 6841bf13 2019-11-29 kn if (search_pattern) {
1983 6841bf13 2019-11-29 kn err = match_logmsg(&have_match, id, commit, ®ex);
1985 6841bf13 2019-11-29 kn got_object_commit_close(commit);
1988 6841bf13 2019-11-29 kn if (have_match == 0) {
1989 6841bf13 2019-11-29 kn got_object_commit_close(commit);
1994 44392932 2019-08-25 stsp err = print_commit(commit, id, repo, path, show_patch,
1995 44392932 2019-08-25 stsp diff_context, refs);
1996 b43fbaa0 2018-06-11 stsp got_object_commit_close(commit);
1997 372ccdbb 2018-06-10 stsp if (err || (limit && --limit == 0))
2001 6841bf13 2019-11-29 kn if (search_pattern)
2002 6841bf13 2019-11-29 kn regfree(®ex);
2003 372ccdbb 2018-06-10 stsp got_commit_graph_close(graph);
2004 f42b1b34 2018-03-12 stsp return err;
2007 4ed7e80c 2018-05-20 stsp __dead static void
2008 6f3d1eb0 2018-03-12 stsp usage_log(void)
2010 48c8c60d 2020-01-27 stsp fprintf(stderr, "usage: %s log [-b] [-c commit] [-C number] [ -l N ] [-p] "
2011 6841bf13 2019-11-29 kn "[-s search-pattern] [-r repository-path] [path]\n", getprogname());
2015 b1ebc001 2019-08-13 stsp static int
2016 b1ebc001 2019-08-13 stsp get_default_log_limit(void)
2018 b1ebc001 2019-08-13 stsp const char *got_default_log_limit;
2019 b1ebc001 2019-08-13 stsp long long n;
2020 b1ebc001 2019-08-13 stsp const char *errstr;
2022 b1ebc001 2019-08-13 stsp got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
2023 b1ebc001 2019-08-13 stsp if (got_default_log_limit == NULL)
2025 b1ebc001 2019-08-13 stsp n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
2026 b1ebc001 2019-08-13 stsp if (errstr != NULL)
2031 4ed7e80c 2018-05-20 stsp static const struct got_error *
2032 f42b1b34 2018-03-12 stsp cmd_log(int argc, char *argv[])
2034 f42b1b34 2018-03-12 stsp const struct got_error *error;
2035 04ca23f4 2018-07-16 stsp struct got_repository *repo = NULL;
2036 cffc0aa4 2019-02-05 stsp struct got_worktree *worktree = NULL;
2037 15a94983 2018-12-23 stsp struct got_commit_object *commit = NULL;
2038 3235492e 2018-04-01 stsp struct got_object_id *id = NULL;
2039 04ca23f4 2018-07-16 stsp char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
2040 463a997a 2019-12-08 kn const char *start_commit = NULL, *search_pattern = NULL;
2041 dc1edbfa 2019-11-29 kn int diff_context = -1, ch;
2042 48c8c60d 2020-01-27 stsp int show_patch = 0, limit = 0, log_branches = 0;
2043 64a96a6d 2018-04-01 stsp const char *errstr;
2044 199a4027 2019-02-02 stsp struct got_reflist_head refs;
2046 1b3893a2 2019-03-18 stsp SIMPLEQ_INIT(&refs);
2048 6715a751 2018-03-16 stsp #ifndef PROFILE
2049 6098196c 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2052 f42b1b34 2018-03-12 stsp err(1, "pledge");
2055 b1ebc001 2019-08-13 stsp limit = get_default_log_limit();
2057 48c8c60d 2020-01-27 stsp while ((ch = getopt(argc, argv, "bpc:C:l:r:s:")) != -1) {
2058 79109fed 2018-03-27 stsp switch (ch) {
2060 79109fed 2018-03-27 stsp show_patch = 1;
2063 d142fc45 2018-04-01 stsp start_commit = optarg;
2066 4a8520aa 2018-10-18 stsp diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
2068 c0cc5c62 2018-10-18 stsp if (errstr != NULL)
2069 c0cc5c62 2018-10-18 stsp err(1, "-C option %s", errstr);
2072 b1ebc001 2019-08-13 stsp limit = strtonum(optarg, 0, INT_MAX, &errstr);
2073 64a96a6d 2018-04-01 stsp if (errstr != NULL)
2074 64a96a6d 2018-04-01 stsp err(1, "-l option %s", errstr);
2077 48c8c60d 2020-01-27 stsp log_branches = 1;
2080 04ca23f4 2018-07-16 stsp repo_path = realpath(optarg, NULL);
2081 04ca23f4 2018-07-16 stsp if (repo_path == NULL)
2082 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
2084 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
2087 6841bf13 2019-11-29 kn search_pattern = optarg;
2090 2deda0b9 2019-03-07 stsp usage_log();
2091 79109fed 2018-03-27 stsp /* NOTREACHED */
2095 79109fed 2018-03-27 stsp argc -= optind;
2096 79109fed 2018-03-27 stsp argv += optind;
2098 dc1edbfa 2019-11-29 kn if (diff_context == -1)
2099 dc1edbfa 2019-11-29 kn diff_context = 3;
2100 dc1edbfa 2019-11-29 kn else if (!show_patch)
2101 dc1edbfa 2019-11-29 kn errx(1, "-C reguires -p");
2103 04ca23f4 2018-07-16 stsp cwd = getcwd(NULL, 0);
2104 04ca23f4 2018-07-16 stsp if (cwd == NULL) {
2105 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2106 04ca23f4 2018-07-16 stsp goto done;
2109 cffc0aa4 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
2110 cffc0aa4 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
2111 cffc0aa4 2019-02-05 stsp goto done;
2112 cffc0aa4 2019-02-05 stsp error = NULL;
2114 e7301579 2019-03-18 stsp if (argc == 0) {
2115 cbd1af7a 2019-03-18 stsp path = strdup("");
2116 e7301579 2019-03-18 stsp if (path == NULL) {
2117 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2118 cbd1af7a 2019-03-18 stsp goto done;
2120 e7301579 2019-03-18 stsp } else if (argc == 1) {
2121 e7301579 2019-03-18 stsp if (worktree) {
2122 e7301579 2019-03-18 stsp error = got_worktree_resolve_path(&path, worktree,
2124 e7301579 2019-03-18 stsp if (error)
2125 e7301579 2019-03-18 stsp goto done;
2127 e7301579 2019-03-18 stsp path = strdup(argv[0]);
2128 e7301579 2019-03-18 stsp if (path == NULL) {
2129 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2130 e7301579 2019-03-18 stsp goto done;
2134 cbd1af7a 2019-03-18 stsp usage_log();
2136 5486daa2 2019-05-11 stsp if (repo_path == NULL) {
2137 5486daa2 2019-05-11 stsp repo_path = worktree ?
2138 5486daa2 2019-05-11 stsp strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
2140 04ca23f4 2018-07-16 stsp if (repo_path == NULL) {
2141 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2142 cffc0aa4 2019-02-05 stsp goto done;
2145 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
2146 d7d4f210 2018-03-12 stsp if (error != NULL)
2147 04ca23f4 2018-07-16 stsp goto done;
2149 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), 1,
2150 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
2151 c02c541e 2019-03-29 stsp if (error)
2152 c02c541e 2019-03-29 stsp goto done;
2154 d142fc45 2018-04-01 stsp if (start_commit == NULL) {
2155 3235492e 2018-04-01 stsp struct got_reference *head_ref;
2156 1cc14b9f 2019-05-14 stsp error = got_ref_open(&head_ref, repo,
2157 1cc14b9f 2019-05-14 stsp worktree ? got_worktree_get_head_ref_name(worktree)
2158 1cc14b9f 2019-05-14 stsp : GOT_REF_HEAD, 0);
2159 3235492e 2018-04-01 stsp if (error != NULL)
2160 3235492e 2018-04-01 stsp return error;
2161 3235492e 2018-04-01 stsp error = got_ref_resolve(&id, repo, head_ref);
2162 3235492e 2018-04-01 stsp got_ref_close(head_ref);
2163 3235492e 2018-04-01 stsp if (error != NULL)
2164 3235492e 2018-04-01 stsp return error;
2165 15a94983 2018-12-23 stsp error = got_object_open_as_commit(&commit, repo, id);
2167 d1f2edc9 2018-06-13 stsp struct got_reference *ref;
2168 2f17228e 2019-05-12 stsp error = got_ref_open(&ref, repo, start_commit, 0);
2169 3235492e 2018-04-01 stsp if (error == NULL) {
2170 1caad61b 2019-02-01 stsp int obj_type;
2171 d1f2edc9 2018-06-13 stsp error = got_ref_resolve(&id, repo, ref);
2172 d1f2edc9 2018-06-13 stsp got_ref_close(ref);
2173 d1f2edc9 2018-06-13 stsp if (error != NULL)
2174 1caad61b 2019-02-01 stsp goto done;
2175 1caad61b 2019-02-01 stsp error = got_object_get_type(&obj_type, repo, id);
2176 1caad61b 2019-02-01 stsp if (error != NULL)
2177 1caad61b 2019-02-01 stsp goto done;
2178 1caad61b 2019-02-01 stsp if (obj_type == GOT_OBJ_TYPE_TAG) {
2179 1caad61b 2019-02-01 stsp struct got_tag_object *tag;
2180 1caad61b 2019-02-01 stsp error = got_object_open_as_tag(&tag, repo, id);
2181 1caad61b 2019-02-01 stsp if (error != NULL)
2182 1caad61b 2019-02-01 stsp goto done;
2183 1caad61b 2019-02-01 stsp if (got_object_tag_get_object_type(tag) !=
2184 1caad61b 2019-02-01 stsp GOT_OBJ_TYPE_COMMIT) {
2185 1caad61b 2019-02-01 stsp got_object_tag_close(tag);
2186 1caad61b 2019-02-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
2187 1caad61b 2019-02-01 stsp goto done;
2190 1caad61b 2019-02-01 stsp id = got_object_id_dup(
2191 1caad61b 2019-02-01 stsp got_object_tag_get_object_id(tag));
2192 1caad61b 2019-02-01 stsp if (id == NULL)
2193 638f9024 2019-05-13 stsp error = got_error_from_errno(
2194 230a42bd 2019-05-11 jcs "got_object_id_dup");
2195 1caad61b 2019-02-01 stsp got_object_tag_close(tag);
2196 1caad61b 2019-02-01 stsp if (error)
2197 1caad61b 2019-02-01 stsp goto done;
2198 1caad61b 2019-02-01 stsp } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
2199 1caad61b 2019-02-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
2200 1caad61b 2019-02-01 stsp goto done;
2202 15a94983 2018-12-23 stsp error = got_object_open_as_commit(&commit, repo, id);
2203 d1f2edc9 2018-06-13 stsp if (error != NULL)
2204 1caad61b 2019-02-01 stsp goto done;
2206 15a94983 2018-12-23 stsp if (commit == NULL) {
2207 e09a504c 2019-06-28 stsp error = got_repo_match_object_id_prefix(&id,
2208 dd88155e 2019-06-29 stsp start_commit, GOT_OBJ_TYPE_COMMIT, repo);
2209 d1f2edc9 2018-06-13 stsp if (error != NULL)
2210 d1f2edc9 2018-06-13 stsp return error;
2213 d7d4f210 2018-03-12 stsp if (error != NULL)
2214 04ca23f4 2018-07-16 stsp goto done;
2216 deeabeae 2019-08-27 stsp if (worktree) {
2217 deeabeae 2019-08-27 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
2219 deeabeae 2019-08-27 stsp if (asprintf(&p, "%s%s%s", prefix,
2220 deeabeae 2019-08-27 stsp (strcmp(prefix, "/") != 0) ? "/" : "", path) == -1) {
2221 deeabeae 2019-08-27 stsp error = got_error_from_errno("asprintf");
2222 deeabeae 2019-08-27 stsp goto done;
2224 f43793a4 2020-01-27 stsp error = got_repo_map_path(&in_repo_path, repo, p, 0);
2227 deeabeae 2019-08-27 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
2228 04ca23f4 2018-07-16 stsp if (error != NULL)
2229 04ca23f4 2018-07-16 stsp goto done;
2230 04ca23f4 2018-07-16 stsp if (in_repo_path) {
2231 04ca23f4 2018-07-16 stsp free(path);
2232 04ca23f4 2018-07-16 stsp path = in_repo_path;
2235 b8bad2ba 2019-08-23 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
2236 199a4027 2019-02-02 stsp if (error)
2237 199a4027 2019-02-02 stsp goto done;
2239 6841bf13 2019-11-29 kn error = print_commits(id, repo, path, show_patch, search_pattern,
2240 48c8c60d 2020-01-27 stsp diff_context, limit, log_branches, &refs);
2242 04ca23f4 2018-07-16 stsp free(path);
2243 04ca23f4 2018-07-16 stsp free(repo_path);
2244 04ca23f4 2018-07-16 stsp free(cwd);
2246 cffc0aa4 2019-02-05 stsp if (worktree)
2247 cffc0aa4 2019-02-05 stsp got_worktree_close(worktree);
2248 ad242220 2018-09-08 stsp if (repo) {
2249 ad242220 2018-09-08 stsp const struct got_error *repo_error;
2250 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
2251 ad242220 2018-09-08 stsp if (error == NULL)
2252 ad242220 2018-09-08 stsp error = repo_error;
2254 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
2255 8bf5b3c9 2018-03-17 stsp return error;
2258 4ed7e80c 2018-05-20 stsp __dead static void
2259 3f8b7d6a 2018-04-01 stsp usage_diff(void)
2261 98eaaa12 2019-08-03 stsp fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] [-s] "
2262 63035f9f 2019-10-06 stsp "[-w] [object1 object2 | path]\n", getprogname());
2266 b72f483a 2019-02-05 stsp struct print_diff_arg {
2267 b72f483a 2019-02-05 stsp struct got_repository *repo;
2268 b72f483a 2019-02-05 stsp struct got_worktree *worktree;
2269 b72f483a 2019-02-05 stsp int diff_context;
2270 3fc0c068 2019-02-10 stsp const char *id_str;
2271 3fc0c068 2019-02-10 stsp int header_shown;
2272 98eaaa12 2019-08-03 stsp int diff_staged;
2273 63035f9f 2019-10-06 stsp int ignore_whitespace;
2276 4ed7e80c 2018-05-20 stsp static const struct got_error *
2277 88d0e355 2019-08-03 stsp print_diff(void *arg, unsigned char status, unsigned char staged_status,
2278 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
2279 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
2280 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
2282 b72f483a 2019-02-05 stsp struct print_diff_arg *a = arg;
2283 b72f483a 2019-02-05 stsp const struct got_error *err = NULL;
2284 b72f483a 2019-02-05 stsp struct got_blob_object *blob1 = NULL;
2285 12463d8b 2019-12-13 stsp int fd = -1;
2286 b72f483a 2019-02-05 stsp FILE *f2 = NULL;
2287 4ce46740 2019-08-08 stsp char *abspath = NULL, *label1 = NULL;
2288 b72f483a 2019-02-05 stsp struct stat sb;
2290 98eaaa12 2019-08-03 stsp if (a->diff_staged) {
2291 98eaaa12 2019-08-03 stsp if (staged_status != GOT_STATUS_MODIFY &&
2292 98eaaa12 2019-08-03 stsp staged_status != GOT_STATUS_ADD &&
2293 98eaaa12 2019-08-03 stsp staged_status != GOT_STATUS_DELETE)
2294 98eaaa12 2019-08-03 stsp return NULL;
2296 98eaaa12 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
2297 98eaaa12 2019-08-03 stsp return NULL;
2298 2a06fe5f 2019-08-24 stsp if (status == GOT_STATUS_NONEXISTENT)
2299 2a06fe5f 2019-08-24 stsp return got_error_set_errno(ENOENT, path);
2300 98eaaa12 2019-08-03 stsp if (status != GOT_STATUS_MODIFY &&
2301 98eaaa12 2019-08-03 stsp status != GOT_STATUS_ADD &&
2302 98eaaa12 2019-08-03 stsp status != GOT_STATUS_DELETE &&
2303 98eaaa12 2019-08-03 stsp status != GOT_STATUS_CONFLICT)
2304 98eaaa12 2019-08-03 stsp return NULL;
2307 3fc0c068 2019-02-10 stsp if (!a->header_shown) {
2308 98eaaa12 2019-08-03 stsp printf("diff %s %s%s\n", a->id_str,
2309 98eaaa12 2019-08-03 stsp got_worktree_get_root_path(a->worktree),
2310 98eaaa12 2019-08-03 stsp a->diff_staged ? " (staged changes)" : "");
2311 3fc0c068 2019-02-10 stsp a->header_shown = 1;
2314 98eaaa12 2019-08-03 stsp if (a->diff_staged) {
2315 98eaaa12 2019-08-03 stsp const char *label1 = NULL, *label2 = NULL;
2316 98eaaa12 2019-08-03 stsp switch (staged_status) {
2317 98eaaa12 2019-08-03 stsp case GOT_STATUS_MODIFY:
2318 98eaaa12 2019-08-03 stsp label1 = path;
2319 98eaaa12 2019-08-03 stsp label2 = path;
2321 98eaaa12 2019-08-03 stsp case GOT_STATUS_ADD:
2322 98eaaa12 2019-08-03 stsp label2 = path;
2324 98eaaa12 2019-08-03 stsp case GOT_STATUS_DELETE:
2325 98eaaa12 2019-08-03 stsp label1 = path;
2328 98eaaa12 2019-08-03 stsp return got_error(GOT_ERR_FILE_STATUS);
2330 98eaaa12 2019-08-03 stsp return got_diff_objects_as_blobs(blob_id, staged_blob_id,
2331 63035f9f 2019-10-06 stsp label1, label2, a->diff_context, a->ignore_whitespace,
2332 63035f9f 2019-10-06 stsp a->repo, stdout);
2335 408b4ebc 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
2336 4ce46740 2019-08-08 stsp staged_status == GOT_STATUS_MODIFY) {
2337 4ce46740 2019-08-08 stsp char *id_str;
2338 408b4ebc 2019-08-03 stsp err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
2341 4ce46740 2019-08-08 stsp goto done;
2342 4ce46740 2019-08-08 stsp err = got_object_id_str(&id_str, staged_blob_id);
2344 4ce46740 2019-08-08 stsp goto done;
2345 4ce46740 2019-08-08 stsp if (asprintf(&label1, "%s (staged)", id_str) == -1) {
2346 4ce46740 2019-08-08 stsp err = got_error_from_errno("asprintf");
2347 4ce46740 2019-08-08 stsp free(id_str);
2348 4ce46740 2019-08-08 stsp goto done;
2350 4ce46740 2019-08-08 stsp free(id_str);
2351 4ce46740 2019-08-08 stsp } else if (status != GOT_STATUS_ADD) {
2352 016a88dd 2019-05-13 stsp err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
2354 4ce46740 2019-08-08 stsp goto done;
2357 7154f6ce 2019-03-27 stsp if (status != GOT_STATUS_DELETE) {
2358 2ec1f75b 2019-03-26 stsp if (asprintf(&abspath, "%s/%s",
2359 2ec1f75b 2019-03-26 stsp got_worktree_get_root_path(a->worktree), path) == -1) {
2360 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2361 2ec1f75b 2019-03-26 stsp goto done;
2364 12463d8b 2019-12-13 stsp if (dirfd != -1) {
2365 12463d8b 2019-12-13 stsp fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
2366 12463d8b 2019-12-13 stsp if (fd == -1) {
2367 12463d8b 2019-12-13 stsp err = got_error_from_errno2("openat", abspath);
2368 12463d8b 2019-12-13 stsp goto done;
2371 12463d8b 2019-12-13 stsp fd = open(abspath, O_RDONLY | O_NOFOLLOW);
2372 12463d8b 2019-12-13 stsp if (fd == -1) {
2373 12463d8b 2019-12-13 stsp err = got_error_from_errno2("open", abspath);
2374 12463d8b 2019-12-13 stsp goto done;
2377 12463d8b 2019-12-13 stsp if (fstat(fd, &sb) == -1) {
2378 12463d8b 2019-12-13 stsp err = got_error_from_errno2("fstat", abspath);
2379 2ec1f75b 2019-03-26 stsp goto done;
2381 12463d8b 2019-12-13 stsp f2 = fdopen(fd, "r");
2382 12463d8b 2019-12-13 stsp if (f2 == NULL) {
2383 12463d8b 2019-12-13 stsp err = got_error_from_errno2("fdopen", abspath);
2384 2ec1f75b 2019-03-26 stsp goto done;
2388 2ec1f75b 2019-03-26 stsp sb.st_size = 0;
2390 4ce46740 2019-08-08 stsp err = got_diff_blob_file(blob1, label1, f2, sb.st_size, path,
2391 63035f9f 2019-10-06 stsp a->diff_context, a->ignore_whitespace, stdout);
2393 b72f483a 2019-02-05 stsp if (blob1)
2394 b72f483a 2019-02-05 stsp got_object_blob_close(blob1);
2395 12463d8b 2019-12-13 stsp if (f2 && fclose(f2) == EOF && err == NULL)
2396 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
2397 12463d8b 2019-12-13 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
2398 12463d8b 2019-12-13 stsp err = got_error_from_errno("close");
2399 b72f483a 2019-02-05 stsp free(abspath);
2400 927df6b7 2019-02-10 stsp return err;
2403 927df6b7 2019-02-10 stsp static const struct got_error *
2404 b00d56cd 2018-04-01 stsp cmd_diff(int argc, char *argv[])
2406 b00d56cd 2018-04-01 stsp const struct got_error *error;
2407 b00d56cd 2018-04-01 stsp struct got_repository *repo = NULL;
2408 b72f483a 2019-02-05 stsp struct got_worktree *worktree = NULL;
2409 b72f483a 2019-02-05 stsp char *cwd = NULL, *repo_path = NULL;
2410 15a94983 2018-12-23 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
2411 cd628e99 2019-05-28 stsp const char *id_str1 = NULL, *id_str2 = NULL;
2412 5e70831e 2019-05-28 stsp char *label1 = NULL, *label2 = NULL;
2413 15a94983 2018-12-23 stsp int type1, type2;
2414 63035f9f 2019-10-06 stsp int diff_context = 3, diff_staged = 0, ignore_whitespace = 0, ch;
2415 c0cc5c62 2018-10-18 stsp const char *errstr;
2416 927df6b7 2019-02-10 stsp char *path = NULL;
2418 b00d56cd 2018-04-01 stsp #ifndef PROFILE
2419 25eccc22 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2420 25eccc22 2019-01-04 stsp NULL) == -1)
2421 b00d56cd 2018-04-01 stsp err(1, "pledge");
2424 63035f9f 2019-10-06 stsp while ((ch = getopt(argc, argv, "C:r:sw")) != -1) {
2425 b00d56cd 2018-04-01 stsp switch (ch) {
2427 dfcab68b 2019-11-29 kn diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
2429 c0cc5c62 2018-10-18 stsp if (errstr != NULL)
2430 c0cc5c62 2018-10-18 stsp err(1, "-C option %s", errstr);
2433 b72f483a 2019-02-05 stsp repo_path = realpath(optarg, NULL);
2434 b72f483a 2019-02-05 stsp if (repo_path == NULL)
2435 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
2437 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
2440 98eaaa12 2019-08-03 stsp diff_staged = 1;
2443 63035f9f 2019-10-06 stsp ignore_whitespace = 1;
2446 2deda0b9 2019-03-07 stsp usage_diff();
2447 b00d56cd 2018-04-01 stsp /* NOTREACHED */
2451 b00d56cd 2018-04-01 stsp argc -= optind;
2452 b00d56cd 2018-04-01 stsp argv += optind;
2454 b72f483a 2019-02-05 stsp cwd = getcwd(NULL, 0);
2455 b72f483a 2019-02-05 stsp if (cwd == NULL) {
2456 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2457 b72f483a 2019-02-05 stsp goto done;
2459 927df6b7 2019-02-10 stsp error = got_worktree_open(&worktree, cwd);
2460 927df6b7 2019-02-10 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
2461 927df6b7 2019-02-10 stsp goto done;
2462 927df6b7 2019-02-10 stsp if (argc <= 1) {
2463 927df6b7 2019-02-10 stsp if (worktree == NULL) {
2464 927df6b7 2019-02-10 stsp error = got_error(GOT_ERR_NOT_WORKTREE);
2465 927df6b7 2019-02-10 stsp goto done;
2467 b72f483a 2019-02-05 stsp if (repo_path)
2469 b72f483a 2019-02-05 stsp "-r option can't be used when diffing a work tree");
2470 b72f483a 2019-02-05 stsp repo_path = strdup(got_worktree_get_repo_path(worktree));
2471 927df6b7 2019-02-10 stsp if (repo_path == NULL) {
2472 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2473 927df6b7 2019-02-10 stsp goto done;
2475 927df6b7 2019-02-10 stsp if (argc == 1) {
2476 6c7ab921 2019-03-18 stsp error = got_worktree_resolve_path(&path, worktree,
2478 927df6b7 2019-02-10 stsp if (error)
2479 927df6b7 2019-02-10 stsp goto done;
2481 927df6b7 2019-02-10 stsp path = strdup("");
2482 927df6b7 2019-02-10 stsp if (path == NULL) {
2483 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2484 927df6b7 2019-02-10 stsp goto done;
2487 b72f483a 2019-02-05 stsp } else if (argc == 2) {
2488 98eaaa12 2019-08-03 stsp if (diff_staged)
2489 98eaaa12 2019-08-03 stsp errx(1, "-s option can't be used when diffing "
2490 98eaaa12 2019-08-03 stsp "objects in repository");
2491 15a94983 2018-12-23 stsp id_str1 = argv[0];
2492 15a94983 2018-12-23 stsp id_str2 = argv[1];
2493 30db809c 2019-06-05 stsp if (worktree && repo_path == NULL) {
2494 30db809c 2019-06-05 stsp repo_path =
2495 30db809c 2019-06-05 stsp strdup(got_worktree_get_repo_path(worktree));
2496 30db809c 2019-06-05 stsp if (repo_path == NULL) {
2497 30db809c 2019-06-05 stsp error = got_error_from_errno("strdup");
2498 30db809c 2019-06-05 stsp goto done;
2502 b00d56cd 2018-04-01 stsp usage_diff();
2504 b72f483a 2019-02-05 stsp if (repo_path == NULL) {
2505 b72f483a 2019-02-05 stsp repo_path = getcwd(NULL, 0);
2506 b72f483a 2019-02-05 stsp if (repo_path == NULL)
2507 638f9024 2019-05-13 stsp return got_error_from_errno("getcwd");
2510 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
2511 76089277 2018-04-01 stsp free(repo_path);
2512 b00d56cd 2018-04-01 stsp if (error != NULL)
2513 b00d56cd 2018-04-01 stsp goto done;
2515 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), 1,
2516 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
2517 c02c541e 2019-03-29 stsp if (error)
2518 c02c541e 2019-03-29 stsp goto done;
2520 30db809c 2019-06-05 stsp if (argc <= 1) {
2521 b72f483a 2019-02-05 stsp struct print_diff_arg arg;
2522 72ea6654 2019-07-27 stsp struct got_pathlist_head paths;
2523 b72f483a 2019-02-05 stsp char *id_str;
2525 72ea6654 2019-07-27 stsp TAILQ_INIT(&paths);
2527 b72f483a 2019-02-05 stsp error = got_object_id_str(&id_str,
2528 b72f483a 2019-02-05 stsp got_worktree_get_base_commit_id(worktree));
2529 b72f483a 2019-02-05 stsp if (error)
2530 b72f483a 2019-02-05 stsp goto done;
2531 b72f483a 2019-02-05 stsp arg.repo = repo;
2532 b72f483a 2019-02-05 stsp arg.worktree = worktree;
2533 b72f483a 2019-02-05 stsp arg.diff_context = diff_context;
2534 3fc0c068 2019-02-10 stsp arg.id_str = id_str;
2535 3fc0c068 2019-02-10 stsp arg.header_shown = 0;
2536 98eaaa12 2019-08-03 stsp arg.diff_staged = diff_staged;
2537 63035f9f 2019-10-06 stsp arg.ignore_whitespace = ignore_whitespace;
2539 adc19d55 2019-07-28 stsp error = got_pathlist_append(&paths, path, NULL);
2540 72ea6654 2019-07-27 stsp if (error)
2541 72ea6654 2019-07-27 stsp goto done;
2543 72ea6654 2019-07-27 stsp error = got_worktree_status(worktree, &paths, repo, print_diff,
2544 b72f483a 2019-02-05 stsp &arg, check_cancelled, NULL);
2545 b72f483a 2019-02-05 stsp free(id_str);
2546 72ea6654 2019-07-27 stsp got_pathlist_free(&paths);
2547 b72f483a 2019-02-05 stsp goto done;
2550 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&id1, &label1, id_str1,
2551 71a27632 2020-01-15 stsp GOT_OBJ_TYPE_ANY, 1, repo);
2552 0adb7196 2019-08-11 stsp if (error)
2553 0adb7196 2019-08-11 stsp goto done;
2555 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&id2, &label2, id_str2,
2556 71a27632 2020-01-15 stsp GOT_OBJ_TYPE_ANY, 1, repo);
2557 0adb7196 2019-08-11 stsp if (error)
2558 0adb7196 2019-08-11 stsp goto done;
2560 15a94983 2018-12-23 stsp error = got_object_get_type(&type1, repo, id1);
2561 15a94983 2018-12-23 stsp if (error)
2562 15a94983 2018-12-23 stsp goto done;
2564 15a94983 2018-12-23 stsp error = got_object_get_type(&type2, repo, id2);
2565 15a94983 2018-12-23 stsp if (error)
2566 15a94983 2018-12-23 stsp goto done;
2568 15a94983 2018-12-23 stsp if (type1 != type2) {
2569 b00d56cd 2018-04-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
2570 b00d56cd 2018-04-01 stsp goto done;
2573 15a94983 2018-12-23 stsp switch (type1) {
2574 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_BLOB:
2575 15a94983 2018-12-23 stsp error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
2576 63035f9f 2019-10-06 stsp diff_context, ignore_whitespace, repo, stdout);
2578 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_TREE:
2579 15a94983 2018-12-23 stsp error = got_diff_objects_as_trees(id1, id2, "", "",
2580 63035f9f 2019-10-06 stsp diff_context, ignore_whitespace, repo, stdout);
2582 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_COMMIT:
2583 5e70831e 2019-05-28 stsp printf("diff %s %s\n", label1, label2);
2584 15a94983 2018-12-23 stsp error = got_diff_objects_as_commits(id1, id2, diff_context,
2585 63035f9f 2019-10-06 stsp ignore_whitespace, repo, stdout);
2588 b00d56cd 2018-04-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
2591 5e70831e 2019-05-28 stsp free(label1);
2592 5e70831e 2019-05-28 stsp free(label2);
2593 15a94983 2018-12-23 stsp free(id1);
2594 15a94983 2018-12-23 stsp free(id2);
2595 927df6b7 2019-02-10 stsp free(path);
2596 b72f483a 2019-02-05 stsp if (worktree)
2597 b72f483a 2019-02-05 stsp got_worktree_close(worktree);
2598 ad242220 2018-09-08 stsp if (repo) {
2599 ad242220 2018-09-08 stsp const struct got_error *repo_error;
2600 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
2601 ad242220 2018-09-08 stsp if (error == NULL)
2602 ad242220 2018-09-08 stsp error = repo_error;
2604 b00d56cd 2018-04-01 stsp return error;
2607 404c43c4 2018-06-21 stsp __dead static void
2608 404c43c4 2018-06-21 stsp usage_blame(void)
2610 9270e621 2019-02-05 stsp fprintf(stderr,
2611 9270e621 2019-02-05 stsp "usage: %s blame [-c commit] [-r repository-path] path\n",
2612 404c43c4 2018-06-21 stsp getprogname());
2616 28315671 2019-08-14 stsp struct blame_line {
2617 28315671 2019-08-14 stsp int annotated;
2618 28315671 2019-08-14 stsp char *id_str;
2619 82f6abb8 2019-08-14 stsp char *committer;
2620 11db6024 2019-10-21 stsp char datebuf[11]; /* YYYY-MM-DD + NUL */
2623 28315671 2019-08-14 stsp struct blame_cb_args {
2624 28315671 2019-08-14 stsp struct blame_line *lines;
2625 28315671 2019-08-14 stsp int nlines;
2626 7ef28ff8 2019-08-14 stsp int nlines_prec;
2627 28315671 2019-08-14 stsp int lineno_cur;
2628 28315671 2019-08-14 stsp off_t *line_offsets;
2630 82f6abb8 2019-08-14 stsp struct got_repository *repo;
2633 404c43c4 2018-06-21 stsp static const struct got_error *
2634 28315671 2019-08-14 stsp blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2636 28315671 2019-08-14 stsp const struct got_error *err = NULL;
2637 28315671 2019-08-14 stsp struct blame_cb_args *a = arg;
2638 28315671 2019-08-14 stsp struct blame_line *bline;
2639 82f6abb8 2019-08-14 stsp char *line = NULL;
2640 28315671 2019-08-14 stsp size_t linesize = 0;
2641 82f6abb8 2019-08-14 stsp struct got_commit_object *commit = NULL;
2642 28315671 2019-08-14 stsp off_t offset;
2643 bcb49d15 2019-08-14 stsp struct tm tm;
2644 bcb49d15 2019-08-14 stsp time_t committer_time;
2646 28315671 2019-08-14 stsp if (nlines != a->nlines ||
2647 28315671 2019-08-14 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
2648 28315671 2019-08-14 stsp return got_error(GOT_ERR_RANGE);
2650 28315671 2019-08-14 stsp if (sigint_received)
2651 28315671 2019-08-14 stsp return got_error(GOT_ERR_ITER_COMPLETED);
2653 2839f8b9 2019-08-18 stsp if (lineno == -1)
2654 2839f8b9 2019-08-18 stsp return NULL; /* no change in this commit */
2656 28315671 2019-08-14 stsp /* Annotate this line. */
2657 28315671 2019-08-14 stsp bline = &a->lines[lineno - 1];
2658 28315671 2019-08-14 stsp if (bline->annotated)
2659 28315671 2019-08-14 stsp return NULL;
2660 28315671 2019-08-14 stsp err = got_object_id_str(&bline->id_str, id);
2662 28315671 2019-08-14 stsp return err;
2664 82f6abb8 2019-08-14 stsp err = got_object_open_as_commit(&commit, a->repo, id);
2666 82f6abb8 2019-08-14 stsp goto done;
2668 82f6abb8 2019-08-14 stsp bline->committer = strdup(got_object_commit_get_committer(commit));
2669 82f6abb8 2019-08-14 stsp if (bline->committer == NULL) {
2670 82f6abb8 2019-08-14 stsp err = got_error_from_errno("strdup");
2671 bcb49d15 2019-08-14 stsp goto done;
2674 bcb49d15 2019-08-14 stsp committer_time = got_object_commit_get_committer_time(commit);
2675 bcb49d15 2019-08-14 stsp if (localtime_r(&committer_time, &tm) == NULL)
2676 bcb49d15 2019-08-14 stsp return got_error_from_errno("localtime_r");
2677 6db9f7f6 2019-12-10 stsp if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
2678 bcb49d15 2019-08-14 stsp &tm) >= sizeof(bline->datebuf)) {
2679 bcb49d15 2019-08-14 stsp err = got_error(GOT_ERR_NO_SPACE);
2680 82f6abb8 2019-08-14 stsp goto done;
2682 28315671 2019-08-14 stsp bline->annotated = 1;
2684 28315671 2019-08-14 stsp /* Print lines annotated so far. */
2685 28315671 2019-08-14 stsp bline = &a->lines[a->lineno_cur - 1];
2686 28315671 2019-08-14 stsp if (!bline->annotated)
2687 82f6abb8 2019-08-14 stsp goto done;
2689 28315671 2019-08-14 stsp offset = a->line_offsets[a->lineno_cur - 1];
2690 82f6abb8 2019-08-14 stsp if (fseeko(a->f, offset, SEEK_SET) == -1) {
2691 82f6abb8 2019-08-14 stsp err = got_error_from_errno("fseeko");
2692 82f6abb8 2019-08-14 stsp goto done;
2695 28315671 2019-08-14 stsp while (bline->annotated) {
2696 82f6abb8 2019-08-14 stsp char *smallerthan, *at, *nl, *committer;
2697 82f6abb8 2019-08-14 stsp size_t len;
2699 500467ff 2019-09-25 hiltjo if (getline(&line, &linesize, a->f) == -1) {
2700 28315671 2019-08-14 stsp if (ferror(a->f))
2701 28315671 2019-08-14 stsp err = got_error_from_errno("getline");
2705 82f6abb8 2019-08-14 stsp committer = bline->committer;
2706 82f6abb8 2019-08-14 stsp smallerthan = strchr(committer, '<');
2707 82f6abb8 2019-08-14 stsp if (smallerthan && smallerthan[1] != '\0')
2708 82f6abb8 2019-08-14 stsp committer = smallerthan + 1;
2709 82f6abb8 2019-08-14 stsp at = strchr(committer, '@');
2711 82f6abb8 2019-08-14 stsp *at = '\0';
2712 82f6abb8 2019-08-14 stsp len = strlen(committer);
2713 82f6abb8 2019-08-14 stsp if (len >= 9)
2714 82f6abb8 2019-08-14 stsp committer[8] = '\0';
2716 28315671 2019-08-14 stsp nl = strchr(line, '\n');
2718 28315671 2019-08-14 stsp *nl = '\0';
2719 bcb49d15 2019-08-14 stsp printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
2720 bcb49d15 2019-08-14 stsp bline->id_str, bline->datebuf, committer, line);
2722 28315671 2019-08-14 stsp a->lineno_cur++;
2723 28315671 2019-08-14 stsp bline = &a->lines[a->lineno_cur - 1];
2726 82f6abb8 2019-08-14 stsp if (commit)
2727 82f6abb8 2019-08-14 stsp got_object_commit_close(commit);
2728 28315671 2019-08-14 stsp free(line);
2729 28315671 2019-08-14 stsp return err;
2732 28315671 2019-08-14 stsp static const struct got_error *
2733 404c43c4 2018-06-21 stsp cmd_blame(int argc, char *argv[])
2735 404c43c4 2018-06-21 stsp const struct got_error *error;
2736 404c43c4 2018-06-21 stsp struct got_repository *repo = NULL;
2737 0c06baac 2019-02-05 stsp struct got_worktree *worktree = NULL;
2738 66bea077 2018-08-02 stsp char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2739 28315671 2019-08-14 stsp struct got_object_id *obj_id = NULL;
2740 404c43c4 2018-06-21 stsp struct got_object_id *commit_id = NULL;
2741 28315671 2019-08-14 stsp struct got_blob_object *blob = NULL;
2742 404c43c4 2018-06-21 stsp char *commit_id_str = NULL;
2743 28315671 2019-08-14 stsp struct blame_cb_args bca;
2744 28315671 2019-08-14 stsp int ch, obj_type, i;
2745 b02560ec 2019-08-19 stsp size_t filesize;
2747 90f3c347 2019-08-19 stsp memset(&bca, 0, sizeof(bca));
2749 404c43c4 2018-06-21 stsp #ifndef PROFILE
2750 36e2fb66 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2751 36e2fb66 2019-01-04 stsp NULL) == -1)
2752 404c43c4 2018-06-21 stsp err(1, "pledge");
2755 66bea077 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2756 404c43c4 2018-06-21 stsp switch (ch) {
2758 404c43c4 2018-06-21 stsp commit_id_str = optarg;
2761 66bea077 2018-08-02 stsp repo_path = realpath(optarg, NULL);
2762 66bea077 2018-08-02 stsp if (repo_path == NULL)
2763 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
2765 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
2768 2deda0b9 2019-03-07 stsp usage_blame();
2769 404c43c4 2018-06-21 stsp /* NOTREACHED */
2773 404c43c4 2018-06-21 stsp argc -= optind;
2774 404c43c4 2018-06-21 stsp argv += optind;
2776 a39318fd 2018-08-02 stsp if (argc == 1)
2777 404c43c4 2018-06-21 stsp path = argv[0];
2779 404c43c4 2018-06-21 stsp usage_blame();
2781 66bea077 2018-08-02 stsp cwd = getcwd(NULL, 0);
2782 66bea077 2018-08-02 stsp if (cwd == NULL) {
2783 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2784 66bea077 2018-08-02 stsp goto done;
2786 66bea077 2018-08-02 stsp if (repo_path == NULL) {
2787 0c06baac 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
2788 0c06baac 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
2789 66bea077 2018-08-02 stsp goto done;
2791 0c06baac 2019-02-05 stsp error = NULL;
2792 0c06baac 2019-02-05 stsp if (worktree) {
2793 0c06baac 2019-02-05 stsp repo_path =
2794 0c06baac 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
2795 0c06baac 2019-02-05 stsp if (repo_path == NULL)
2796 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2797 0c06baac 2019-02-05 stsp if (error)
2798 0c06baac 2019-02-05 stsp goto done;
2800 0c06baac 2019-02-05 stsp repo_path = strdup(cwd);
2801 0c06baac 2019-02-05 stsp if (repo_path == NULL) {
2802 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2803 0c06baac 2019-02-05 stsp goto done;
2808 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
2809 c02c541e 2019-03-29 stsp if (error != NULL)
2810 36e2fb66 2019-01-04 stsp goto done;
2812 c530dc23 2019-07-23 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2813 c02c541e 2019-03-29 stsp if (error)
2814 404c43c4 2018-06-21 stsp goto done;
2816 0c06baac 2019-02-05 stsp if (worktree) {
2817 6efaaa2d 2019-02-05 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
2818 0c06baac 2019-02-05 stsp char *p, *worktree_subdir = cwd +
2819 0c06baac 2019-02-05 stsp strlen(got_worktree_get_root_path(worktree));
2820 6efaaa2d 2019-02-05 stsp if (asprintf(&p, "%s%s%s%s%s",
2821 6efaaa2d 2019-02-05 stsp prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
2822 6efaaa2d 2019-02-05 stsp worktree_subdir, worktree_subdir[0] ? "/" : "",
2823 6efaaa2d 2019-02-05 stsp path) == -1) {
2824 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
2825 0c06baac 2019-02-05 stsp goto done;
2827 6efaaa2d 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, p, 0);
2830 0c06baac 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
2832 0c06baac 2019-02-05 stsp if (error)
2833 66bea077 2018-08-02 stsp goto done;
2835 404c43c4 2018-06-21 stsp if (commit_id_str == NULL) {
2836 404c43c4 2018-06-21 stsp struct got_reference *head_ref;
2837 a0975128 2020-02-07 stsp error = got_ref_open(&head_ref, repo, worktree ?
2838 a0975128 2020-02-07 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
2839 404c43c4 2018-06-21 stsp if (error != NULL)
2840 66bea077 2018-08-02 stsp goto done;
2841 404c43c4 2018-06-21 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
2842 404c43c4 2018-06-21 stsp got_ref_close(head_ref);
2843 404c43c4 2018-06-21 stsp if (error != NULL)
2844 66bea077 2018-08-02 stsp goto done;
2846 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
2847 71a27632 2020-01-15 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
2848 30837e32 2019-07-25 stsp if (error)
2849 66bea077 2018-08-02 stsp goto done;
2852 28315671 2019-08-14 stsp error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
2853 28315671 2019-08-14 stsp if (error)
2854 28315671 2019-08-14 stsp goto done;
2856 28315671 2019-08-14 stsp error = got_object_get_type(&obj_type, repo, obj_id);
2857 28315671 2019-08-14 stsp if (error)
2858 28315671 2019-08-14 stsp goto done;
2860 28315671 2019-08-14 stsp if (obj_type != GOT_OBJ_TYPE_BLOB) {
2861 28315671 2019-08-14 stsp error = got_error(GOT_ERR_OBJ_TYPE);
2862 28315671 2019-08-14 stsp goto done;
2865 28315671 2019-08-14 stsp error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
2866 28315671 2019-08-14 stsp if (error)
2867 28315671 2019-08-14 stsp goto done;
2868 28315671 2019-08-14 stsp bca.f = got_opentemp();
2869 28315671 2019-08-14 stsp if (bca.f == NULL) {
2870 28315671 2019-08-14 stsp error = got_error_from_errno("got_opentemp");
2871 28315671 2019-08-14 stsp goto done;
2873 b02560ec 2019-08-19 stsp error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
2874 28315671 2019-08-14 stsp &bca.line_offsets, bca.f, blob);
2875 7ef28ff8 2019-08-14 stsp if (error || bca.nlines == 0)
2876 28315671 2019-08-14 stsp goto done;
2878 b02560ec 2019-08-19 stsp /* Don't include \n at EOF in the blame line count. */
2879 b02560ec 2019-08-19 stsp if (bca.line_offsets[bca.nlines - 1] == filesize)
2880 b02560ec 2019-08-19 stsp bca.nlines--;
2882 28315671 2019-08-14 stsp bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
2883 28315671 2019-08-14 stsp if (bca.lines == NULL) {
2884 28315671 2019-08-14 stsp error = got_error_from_errno("calloc");
2885 28315671 2019-08-14 stsp goto done;
2887 28315671 2019-08-14 stsp bca.lineno_cur = 1;
2888 7ef28ff8 2019-08-14 stsp bca.nlines_prec = 0;
2889 7ef28ff8 2019-08-14 stsp i = bca.nlines;
2890 7ef28ff8 2019-08-14 stsp while (i > 0) {
2892 7ef28ff8 2019-08-14 stsp bca.nlines_prec++;
2894 82f6abb8 2019-08-14 stsp bca.repo = repo;
2896 6fb7cd11 2019-08-22 stsp error = got_blame(in_repo_path, commit_id, repo, blame_cb, &bca,
2897 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
2899 66bea077 2018-08-02 stsp free(in_repo_path);
2900 66bea077 2018-08-02 stsp free(repo_path);
2901 66bea077 2018-08-02 stsp free(cwd);
2902 404c43c4 2018-06-21 stsp free(commit_id);
2903 28315671 2019-08-14 stsp free(obj_id);
2905 28315671 2019-08-14 stsp got_object_blob_close(blob);
2906 0c06baac 2019-02-05 stsp if (worktree)
2907 0c06baac 2019-02-05 stsp got_worktree_close(worktree);
2908 ad242220 2018-09-08 stsp if (repo) {
2909 ad242220 2018-09-08 stsp const struct got_error *repo_error;
2910 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
2911 ad242220 2018-09-08 stsp if (error == NULL)
2912 ad242220 2018-09-08 stsp error = repo_error;
2914 3affba96 2019-09-22 stsp if (bca.lines) {
2915 3affba96 2019-09-22 stsp for (i = 0; i < bca.nlines; i++) {
2916 3affba96 2019-09-22 stsp struct blame_line *bline = &bca.lines[i];
2917 3affba96 2019-09-22 stsp free(bline->id_str);
2918 3affba96 2019-09-22 stsp free(bline->committer);
2920 3affba96 2019-09-22 stsp free(bca.lines);
2922 28315671 2019-08-14 stsp free(bca.line_offsets);
2923 28315671 2019-08-14 stsp if (bca.f && fclose(bca.f) == EOF && error == NULL)
2924 28315671 2019-08-14 stsp error = got_error_from_errno("fclose");
2925 404c43c4 2018-06-21 stsp return error;
2928 5de5890b 2018-10-18 stsp __dead static void
2929 5de5890b 2018-10-18 stsp usage_tree(void)
2931 c1669e2e 2019-01-09 stsp fprintf(stderr,
2932 c1669e2e 2019-01-09 stsp "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
2933 5de5890b 2018-10-18 stsp getprogname());
2937 c1669e2e 2019-01-09 stsp static void
2938 c1669e2e 2019-01-09 stsp print_entry(struct got_tree_entry *te, const char *id, const char *path,
2939 c1669e2e 2019-01-09 stsp const char *root_path)
2941 c1669e2e 2019-01-09 stsp int is_root_path = (strcmp(path, root_path) == 0);
2942 848d6979 2019-08-12 stsp const char *modestr = "";
2943 56e0773d 2019-11-28 stsp mode_t mode = got_tree_entry_get_mode(te);
2945 c1669e2e 2019-01-09 stsp path += strlen(root_path);
2946 c1669e2e 2019-01-09 stsp while (path[0] == '/')
2949 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
2950 63c5ca5d 2019-08-24 stsp modestr = "$";
2951 56e0773d 2019-11-28 stsp else if (S_ISLNK(mode))
2952 848d6979 2019-08-12 stsp modestr = "@";
2953 56e0773d 2019-11-28 stsp else if (S_ISDIR(mode))
2954 848d6979 2019-08-12 stsp modestr = "/";
2955 56e0773d 2019-11-28 stsp else if (mode & S_IXUSR)
2956 848d6979 2019-08-12 stsp modestr = "*";
2958 c1669e2e 2019-01-09 stsp printf("%s%s%s%s%s\n", id ? id : "", path,
2959 56e0773d 2019-11-28 stsp is_root_path ? "" : "/", got_tree_entry_get_name(te), modestr);
2962 5de5890b 2018-10-18 stsp static const struct got_error *
2963 5de5890b 2018-10-18 stsp print_tree(const char *path, struct got_object_id *commit_id,
2964 c1669e2e 2019-01-09 stsp int show_ids, int recurse, const char *root_path,
2965 c1669e2e 2019-01-09 stsp struct got_repository *repo)
2967 5de5890b 2018-10-18 stsp const struct got_error *err = NULL;
2968 5de5890b 2018-10-18 stsp struct got_object_id *tree_id = NULL;
2969 5de5890b 2018-10-18 stsp struct got_tree_object *tree = NULL;
2970 56e0773d 2019-11-28 stsp int nentries, i;
2972 5de5890b 2018-10-18 stsp err = got_object_id_by_path(&tree_id, repo, commit_id, path);
2974 5de5890b 2018-10-18 stsp goto done;
2976 5de5890b 2018-10-18 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
2978 5de5890b 2018-10-18 stsp goto done;
2979 56e0773d 2019-11-28 stsp nentries = got_object_tree_get_nentries(tree);
2980 56e0773d 2019-11-28 stsp for (i = 0; i < nentries; i++) {
2981 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
2982 5de5890b 2018-10-18 stsp char *id = NULL;
2984 84453469 2018-11-11 stsp if (sigint_received || sigpipe_received)
2987 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(tree, i);
2988 5de5890b 2018-10-18 stsp if (show_ids) {
2989 5de5890b 2018-10-18 stsp char *id_str;
2990 56e0773d 2019-11-28 stsp err = got_object_id_str(&id_str,
2991 56e0773d 2019-11-28 stsp got_tree_entry_get_id(te));
2993 5de5890b 2018-10-18 stsp goto done;
2994 5de5890b 2018-10-18 stsp if (asprintf(&id, "%s ", id_str) == -1) {
2995 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2996 5de5890b 2018-10-18 stsp free(id_str);
2997 5de5890b 2018-10-18 stsp goto done;
2999 5de5890b 2018-10-18 stsp free(id_str);
3001 c1669e2e 2019-01-09 stsp print_entry(te, id, path, root_path);
3004 56e0773d 2019-11-28 stsp if (recurse && S_ISDIR(got_tree_entry_get_mode(te))) {
3005 c1669e2e 2019-01-09 stsp char *child_path;
3006 c1669e2e 2019-01-09 stsp if (asprintf(&child_path, "%s%s%s", path,
3007 c1669e2e 2019-01-09 stsp path[0] == '/' && path[1] == '\0' ? "" : "/",
3008 56e0773d 2019-11-28 stsp got_tree_entry_get_name(te)) == -1) {
3009 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
3010 c1669e2e 2019-01-09 stsp goto done;
3012 c1669e2e 2019-01-09 stsp err = print_tree(child_path, commit_id, show_ids, 1,
3013 c1669e2e 2019-01-09 stsp root_path, repo);
3014 c1669e2e 2019-01-09 stsp free(child_path);
3016 c1669e2e 2019-01-09 stsp goto done;
3021 5de5890b 2018-10-18 stsp got_object_tree_close(tree);
3022 5de5890b 2018-10-18 stsp free(tree_id);
3023 5de5890b 2018-10-18 stsp return err;
3026 5de5890b 2018-10-18 stsp static const struct got_error *
3027 5de5890b 2018-10-18 stsp cmd_tree(int argc, char *argv[])
3029 5de5890b 2018-10-18 stsp const struct got_error *error;
3030 5de5890b 2018-10-18 stsp struct got_repository *repo = NULL;
3031 7a2c19d6 2019-02-05 stsp struct got_worktree *worktree = NULL;
3032 7a2c19d6 2019-02-05 stsp const char *path;
3033 7a2c19d6 2019-02-05 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
3034 5de5890b 2018-10-18 stsp struct got_object_id *commit_id = NULL;
3035 5de5890b 2018-10-18 stsp char *commit_id_str = NULL;
3036 c1669e2e 2019-01-09 stsp int show_ids = 0, recurse = 0;
3039 5de5890b 2018-10-18 stsp #ifndef PROFILE
3040 0f8d269b 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3041 0f8d269b 2019-01-04 stsp NULL) == -1)
3042 5de5890b 2018-10-18 stsp err(1, "pledge");
3045 c1669e2e 2019-01-09 stsp while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
3046 5de5890b 2018-10-18 stsp switch (ch) {
3048 5de5890b 2018-10-18 stsp commit_id_str = optarg;
3051 5de5890b 2018-10-18 stsp repo_path = realpath(optarg, NULL);
3052 5de5890b 2018-10-18 stsp if (repo_path == NULL)
3053 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
3055 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
3058 5de5890b 2018-10-18 stsp show_ids = 1;
3061 c1669e2e 2019-01-09 stsp recurse = 1;
3064 2deda0b9 2019-03-07 stsp usage_tree();
3065 5de5890b 2018-10-18 stsp /* NOTREACHED */
3069 5de5890b 2018-10-18 stsp argc -= optind;
3070 5de5890b 2018-10-18 stsp argv += optind;
3072 5de5890b 2018-10-18 stsp if (argc == 1)
3073 5de5890b 2018-10-18 stsp path = argv[0];
3074 5de5890b 2018-10-18 stsp else if (argc > 1)
3075 5de5890b 2018-10-18 stsp usage_tree();
3077 7a2c19d6 2019-02-05 stsp path = NULL;
3079 9bf7a39b 2019-02-05 stsp cwd = getcwd(NULL, 0);
3080 9bf7a39b 2019-02-05 stsp if (cwd == NULL) {
3081 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
3082 9bf7a39b 2019-02-05 stsp goto done;
3084 5de5890b 2018-10-18 stsp if (repo_path == NULL) {
3085 7a2c19d6 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
3086 8994de28 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
3087 7a2c19d6 2019-02-05 stsp goto done;
3089 7a2c19d6 2019-02-05 stsp error = NULL;
3090 7a2c19d6 2019-02-05 stsp if (worktree) {
3091 7a2c19d6 2019-02-05 stsp repo_path =
3092 7a2c19d6 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
3093 7a2c19d6 2019-02-05 stsp if (repo_path == NULL)
3094 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
3095 7a2c19d6 2019-02-05 stsp if (error)
3096 7a2c19d6 2019-02-05 stsp goto done;
3098 7a2c19d6 2019-02-05 stsp repo_path = strdup(cwd);
3099 7a2c19d6 2019-02-05 stsp if (repo_path == NULL) {
3100 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
3101 7a2c19d6 2019-02-05 stsp goto done;
3106 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
3107 5de5890b 2018-10-18 stsp if (error != NULL)
3108 c02c541e 2019-03-29 stsp goto done;
3110 c530dc23 2019-07-23 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
3111 c02c541e 2019-03-29 stsp if (error)
3112 5de5890b 2018-10-18 stsp goto done;
3114 9bf7a39b 2019-02-05 stsp if (path == NULL) {
3115 9bf7a39b 2019-02-05 stsp if (worktree) {
3116 9bf7a39b 2019-02-05 stsp char *p, *worktree_subdir = cwd +
3117 9bf7a39b 2019-02-05 stsp strlen(got_worktree_get_root_path(worktree));
3118 9bf7a39b 2019-02-05 stsp if (asprintf(&p, "%s/%s",
3119 9bf7a39b 2019-02-05 stsp got_worktree_get_path_prefix(worktree),
3120 9bf7a39b 2019-02-05 stsp worktree_subdir) == -1) {
3121 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
3122 9bf7a39b 2019-02-05 stsp goto done;
3124 f43793a4 2020-01-27 stsp error = got_repo_map_path(&in_repo_path, repo, p, 0);
3126 9bf7a39b 2019-02-05 stsp if (error)
3127 9bf7a39b 2019-02-05 stsp goto done;
3129 9bf7a39b 2019-02-05 stsp path = "/";
3131 9bf7a39b 2019-02-05 stsp if (in_repo_path == NULL) {
3132 9bf7a39b 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
3133 9bf7a39b 2019-02-05 stsp if (error != NULL)
3134 9bf7a39b 2019-02-05 stsp goto done;
3137 5de5890b 2018-10-18 stsp if (commit_id_str == NULL) {
3138 5de5890b 2018-10-18 stsp struct got_reference *head_ref;
3139 2f17228e 2019-05-12 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
3140 5de5890b 2018-10-18 stsp if (error != NULL)
3141 5de5890b 2018-10-18 stsp goto done;
3142 5de5890b 2018-10-18 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
3143 5de5890b 2018-10-18 stsp got_ref_close(head_ref);
3144 5de5890b 2018-10-18 stsp if (error != NULL)
3145 5de5890b 2018-10-18 stsp goto done;
3147 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
3148 71a27632 2020-01-15 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
3149 30837e32 2019-07-25 stsp if (error)
3150 5de5890b 2018-10-18 stsp goto done;
3153 c1669e2e 2019-01-09 stsp error = print_tree(in_repo_path, commit_id, show_ids, recurse,
3154 c1669e2e 2019-01-09 stsp in_repo_path, repo);
3156 5de5890b 2018-10-18 stsp free(in_repo_path);
3157 5de5890b 2018-10-18 stsp free(repo_path);
3158 5de5890b 2018-10-18 stsp free(cwd);
3159 5de5890b 2018-10-18 stsp free(commit_id);
3160 7a2c19d6 2019-02-05 stsp if (worktree)
3161 7a2c19d6 2019-02-05 stsp got_worktree_close(worktree);
3162 5de5890b 2018-10-18 stsp if (repo) {
3163 5de5890b 2018-10-18 stsp const struct got_error *repo_error;
3164 5de5890b 2018-10-18 stsp repo_error = got_repo_close(repo);
3165 5de5890b 2018-10-18 stsp if (error == NULL)
3166 5de5890b 2018-10-18 stsp error = repo_error;
3168 5de5890b 2018-10-18 stsp return error;
3171 6bad629b 2019-02-04 stsp __dead static void
3172 6bad629b 2019-02-04 stsp usage_status(void)
3174 72ea6654 2019-07-27 stsp fprintf(stderr, "usage: %s status [path ...]\n", getprogname());
3178 b72f483a 2019-02-05 stsp static const struct got_error *
3179 88d0e355 2019-08-03 stsp print_status(void *arg, unsigned char status, unsigned char staged_status,
3180 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
3181 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3182 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
3184 244725f2 2019-08-03 stsp if (status == staged_status && (status == GOT_STATUS_DELETE))
3185 c363b2c1 2019-08-03 stsp status = GOT_STATUS_NO_CHANGE;
3186 88d0e355 2019-08-03 stsp printf("%c%c %s\n", status, staged_status, path);
3187 b72f483a 2019-02-05 stsp return NULL;
3190 6bad629b 2019-02-04 stsp static const struct got_error *
3191 6bad629b 2019-02-04 stsp cmd_status(int argc, char *argv[])
3193 6bad629b 2019-02-04 stsp const struct got_error *error = NULL;
3194 6bad629b 2019-02-04 stsp struct got_repository *repo = NULL;
3195 6bad629b 2019-02-04 stsp struct got_worktree *worktree = NULL;
3196 f86a1bf5 2019-07-27 stsp char *cwd = NULL;
3197 72ea6654 2019-07-27 stsp struct got_pathlist_head paths;
3198 a5edda0a 2019-07-27 stsp struct got_pathlist_entry *pe;
3201 72ea6654 2019-07-27 stsp TAILQ_INIT(&paths);
3203 6bad629b 2019-02-04 stsp while ((ch = getopt(argc, argv, "")) != -1) {
3204 6bad629b 2019-02-04 stsp switch (ch) {
3206 2deda0b9 2019-03-07 stsp usage_status();
3207 6bad629b 2019-02-04 stsp /* NOTREACHED */
3211 6bad629b 2019-02-04 stsp argc -= optind;
3212 6bad629b 2019-02-04 stsp argv += optind;
3214 6bad629b 2019-02-04 stsp #ifndef PROFILE
3215 6bad629b 2019-02-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3216 6bad629b 2019-02-04 stsp NULL) == -1)
3217 6bad629b 2019-02-04 stsp err(1, "pledge");
3219 927df6b7 2019-02-10 stsp cwd = getcwd(NULL, 0);
3220 927df6b7 2019-02-10 stsp if (cwd == NULL) {
3221 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
3222 927df6b7 2019-02-10 stsp goto done;
3225 927df6b7 2019-02-10 stsp error = got_worktree_open(&worktree, cwd);
3226 927df6b7 2019-02-10 stsp if (error != NULL)
3227 a5edda0a 2019-07-27 stsp goto done;
3229 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
3231 6bad629b 2019-02-04 stsp if (error != NULL)
3232 6bad629b 2019-02-04 stsp goto done;
3234 d0eebce4 2019-03-11 stsp error = apply_unveil(got_repo_get_path(repo), 1,
3235 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
3236 087fb88c 2019-08-04 stsp if (error)
3237 087fb88c 2019-08-04 stsp goto done;
3239 087fb88c 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3240 6bad629b 2019-02-04 stsp if (error)
3241 6bad629b 2019-02-04 stsp goto done;
3243 72ea6654 2019-07-27 stsp error = got_worktree_status(worktree, &paths, repo, print_status, NULL,
3244 6bad629b 2019-02-04 stsp check_cancelled, NULL);
3246 a5edda0a 2019-07-27 stsp TAILQ_FOREACH(pe, &paths, entry)
3247 a5edda0a 2019-07-27 stsp free((char *)pe->path);
3248 72ea6654 2019-07-27 stsp got_pathlist_free(&paths);
3249 927df6b7 2019-02-10 stsp free(cwd);
3250 d0eebce4 2019-03-11 stsp return error;
3253 d0eebce4 2019-03-11 stsp __dead static void
3254 d0eebce4 2019-03-11 stsp usage_ref(void)
3256 d0eebce4 2019-03-11 stsp fprintf(stderr,
3257 d1c1ae5f 2019-08-12 stsp "usage: %s ref [-r repository] -l | -d name | [-s] name target\n",
3258 d0eebce4 2019-03-11 stsp getprogname());
3262 d0eebce4 2019-03-11 stsp static const struct got_error *
3263 d0eebce4 2019-03-11 stsp list_refs(struct got_repository *repo)
3265 d0eebce4 2019-03-11 stsp static const struct got_error *err = NULL;
3266 d0eebce4 2019-03-11 stsp struct got_reflist_head refs;
3267 d0eebce4 2019-03-11 stsp struct got_reflist_entry *re;
3269 d0eebce4 2019-03-11 stsp SIMPLEQ_INIT(&refs);
3270 b8bad2ba 2019-08-23 stsp err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
3272 d0eebce4 2019-03-11 stsp return err;
3274 d0eebce4 2019-03-11 stsp SIMPLEQ_FOREACH(re, &refs, entry) {
3275 d0eebce4 2019-03-11 stsp char *refstr;
3276 d0eebce4 2019-03-11 stsp refstr = got_ref_to_str(re->ref);
3277 d0eebce4 2019-03-11 stsp if (refstr == NULL)
3278 638f9024 2019-05-13 stsp return got_error_from_errno("got_ref_to_str");
3279 d0eebce4 2019-03-11 stsp printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
3280 d0eebce4 2019-03-11 stsp free(refstr);
3283 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
3284 d0eebce4 2019-03-11 stsp return NULL;
3287 d0eebce4 2019-03-11 stsp static const struct got_error *
3288 d0eebce4 2019-03-11 stsp delete_ref(struct got_repository *repo, const char *refname)
3290 d0eebce4 2019-03-11 stsp const struct got_error *err = NULL;
3291 d0eebce4 2019-03-11 stsp struct got_reference *ref;
3293 2f17228e 2019-05-12 stsp err = got_ref_open(&ref, repo, refname, 0);
3295 d0eebce4 2019-03-11 stsp return err;
3297 d0eebce4 2019-03-11 stsp err = got_ref_delete(ref, repo);
3298 d0eebce4 2019-03-11 stsp got_ref_close(ref);
3299 d0eebce4 2019-03-11 stsp return err;
3302 d0eebce4 2019-03-11 stsp static const struct got_error *
3303 d83d9d5c 2019-05-13 stsp add_ref(struct got_repository *repo, const char *refname, const char *target)
3305 d0eebce4 2019-03-11 stsp const struct got_error *err = NULL;
3306 d0eebce4 2019-03-11 stsp struct got_object_id *id;
3307 d0eebce4 2019-03-11 stsp struct got_reference *ref = NULL;
3310 bd5895f3 2019-11-28 stsp * Don't let the user create a reference name with a leading '-'.
3311 d1644381 2019-07-14 stsp * While technically a valid reference name, this case is usually
3312 d1644381 2019-07-14 stsp * an unintended typo.
3314 bd5895f3 2019-11-28 stsp if (refname[0] == '-')
3315 bd5895f3 2019-11-28 stsp return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
3317 dd88155e 2019-06-29 stsp err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
3319 d83d9d5c 2019-05-13 stsp if (err) {
3320 d83d9d5c 2019-05-13 stsp struct got_reference *target_ref;
3322 d83d9d5c 2019-05-13 stsp if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
3323 d83d9d5c 2019-05-13 stsp return err;
3324 d83d9d5c 2019-05-13 stsp err = got_ref_open(&target_ref, repo, target, 0);
3326 d83d9d5c 2019-05-13 stsp return err;
3327 d83d9d5c 2019-05-13 stsp err = got_ref_resolve(&id, repo, target_ref);
3328 d83d9d5c 2019-05-13 stsp got_ref_close(target_ref);
3330 d83d9d5c 2019-05-13 stsp return err;
3333 d0eebce4 2019-03-11 stsp err = got_ref_alloc(&ref, refname, id);
3335 d0eebce4 2019-03-11 stsp goto done;
3337 d0eebce4 2019-03-11 stsp err = got_ref_write(ref, repo);
3340 d0eebce4 2019-03-11 stsp got_ref_close(ref);
3342 d1c1ae5f 2019-08-12 stsp return err;
3345 d1c1ae5f 2019-08-12 stsp static const struct got_error *
3346 d1c1ae5f 2019-08-12 stsp add_symref(struct got_repository *repo, const char *refname, const char *target)
3348 d1c1ae5f 2019-08-12 stsp const struct got_error *err = NULL;
3349 d1c1ae5f 2019-08-12 stsp struct got_reference *ref = NULL;
3350 d1c1ae5f 2019-08-12 stsp struct got_reference *target_ref = NULL;
3353 bd5895f3 2019-11-28 stsp * Don't let the user create a reference name with a leading '-'.
3354 d1c1ae5f 2019-08-12 stsp * While technically a valid reference name, this case is usually
3355 d1c1ae5f 2019-08-12 stsp * an unintended typo.
3357 bd5895f3 2019-11-28 stsp if (refname[0] == '-')
3358 bd5895f3 2019-11-28 stsp return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
3360 d1c1ae5f 2019-08-12 stsp err = got_ref_open(&target_ref, repo, target, 0);
3362 d1c1ae5f 2019-08-12 stsp return err;
3364 d1c1ae5f 2019-08-12 stsp err = got_ref_alloc_symref(&ref, refname, target_ref);
3366 d1c1ae5f 2019-08-12 stsp goto done;
3368 d1c1ae5f 2019-08-12 stsp err = got_ref_write(ref, repo);
3370 d1c1ae5f 2019-08-12 stsp if (target_ref)
3371 d1c1ae5f 2019-08-12 stsp got_ref_close(target_ref);
3373 d1c1ae5f 2019-08-12 stsp got_ref_close(ref);
3374 d0eebce4 2019-03-11 stsp return err;
3377 d0eebce4 2019-03-11 stsp static const struct got_error *
3378 d0eebce4 2019-03-11 stsp cmd_ref(int argc, char *argv[])
3380 d0eebce4 2019-03-11 stsp const struct got_error *error = NULL;
3381 d0eebce4 2019-03-11 stsp struct got_repository *repo = NULL;
3382 d0eebce4 2019-03-11 stsp struct got_worktree *worktree = NULL;
3383 d0eebce4 2019-03-11 stsp char *cwd = NULL, *repo_path = NULL;
3384 d1c1ae5f 2019-08-12 stsp int ch, do_list = 0, create_symref = 0;
3385 d0eebce4 2019-03-11 stsp const char *delref = NULL;
3387 d0eebce4 2019-03-11 stsp /* TODO: Add -s option for adding symbolic references. */
3388 d1c1ae5f 2019-08-12 stsp while ((ch = getopt(argc, argv, "d:r:ls")) != -1) {
3389 d0eebce4 2019-03-11 stsp switch (ch) {
3391 d0eebce4 2019-03-11 stsp delref = optarg;
3394 d0eebce4 2019-03-11 stsp repo_path = realpath(optarg, NULL);
3395 d0eebce4 2019-03-11 stsp if (repo_path == NULL)
3396 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
3398 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
3401 d0eebce4 2019-03-11 stsp do_list = 1;
3404 d1c1ae5f 2019-08-12 stsp create_symref = 1;
3407 d0eebce4 2019-03-11 stsp usage_ref();
3408 d0eebce4 2019-03-11 stsp /* NOTREACHED */
3412 d0eebce4 2019-03-11 stsp if (do_list && delref)
3413 d0eebce4 2019-03-11 stsp errx(1, "-l and -d options are mutually exclusive\n");
3415 d0eebce4 2019-03-11 stsp argc -= optind;
3416 d0eebce4 2019-03-11 stsp argv += optind;
3418 d0eebce4 2019-03-11 stsp if (do_list || delref) {
3419 d1c1ae5f 2019-08-12 stsp if (create_symref)
3420 d1c1ae5f 2019-08-12 stsp errx(1, "-s option cannot be used together with the "
3421 d1c1ae5f 2019-08-12 stsp "-l or -d options");
3422 d0eebce4 2019-03-11 stsp if (argc > 0)
3423 d0eebce4 2019-03-11 stsp usage_ref();
3424 d0eebce4 2019-03-11 stsp } else if (argc != 2)
3425 d0eebce4 2019-03-11 stsp usage_ref();
3427 d0eebce4 2019-03-11 stsp #ifndef PROFILE
3428 e0b57350 2019-03-12 stsp if (do_list) {
3429 e0b57350 2019-03-12 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3430 e0b57350 2019-03-12 stsp NULL) == -1)
3431 e0b57350 2019-03-12 stsp err(1, "pledge");
3433 e0b57350 2019-03-12 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3434 e0b57350 2019-03-12 stsp "sendfd unveil", NULL) == -1)
3435 e0b57350 2019-03-12 stsp err(1, "pledge");
3438 d0eebce4 2019-03-11 stsp cwd = getcwd(NULL, 0);
3439 d0eebce4 2019-03-11 stsp if (cwd == NULL) {
3440 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
3441 d0eebce4 2019-03-11 stsp goto done;
3444 d0eebce4 2019-03-11 stsp if (repo_path == NULL) {
3445 d0eebce4 2019-03-11 stsp error = got_worktree_open(&worktree, cwd);
3446 d0eebce4 2019-03-11 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
3447 d0eebce4 2019-03-11 stsp goto done;
3449 d0eebce4 2019-03-11 stsp error = NULL;
3450 d0eebce4 2019-03-11 stsp if (worktree) {
3451 d0eebce4 2019-03-11 stsp repo_path =
3452 d0eebce4 2019-03-11 stsp strdup(got_worktree_get_repo_path(worktree));
3453 d0eebce4 2019-03-11 stsp if (repo_path == NULL)
3454 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
3455 d0eebce4 2019-03-11 stsp if (error)
3456 d0eebce4 2019-03-11 stsp goto done;
3458 d0eebce4 2019-03-11 stsp repo_path = strdup(cwd);
3459 d0eebce4 2019-03-11 stsp if (repo_path == NULL) {
3460 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
3461 d0eebce4 2019-03-11 stsp goto done;
3466 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
3467 d0eebce4 2019-03-11 stsp if (error != NULL)
3468 d0eebce4 2019-03-11 stsp goto done;
3470 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), do_list,
3471 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
3472 c02c541e 2019-03-29 stsp if (error)
3473 c02c541e 2019-03-29 stsp goto done;
3475 d0eebce4 2019-03-11 stsp if (do_list)
3476 d0eebce4 2019-03-11 stsp error = list_refs(repo);
3477 d0eebce4 2019-03-11 stsp else if (delref)
3478 d0eebce4 2019-03-11 stsp error = delete_ref(repo, delref);
3479 d1c1ae5f 2019-08-12 stsp else if (create_symref)
3480 d1c1ae5f 2019-08-12 stsp error = add_symref(repo, argv[0], argv[1]);
3482 d0eebce4 2019-03-11 stsp error = add_ref(repo, argv[0], argv[1]);
3485 4e759de4 2019-06-26 stsp got_repo_close(repo);
3486 4e759de4 2019-06-26 stsp if (worktree)
3487 4e759de4 2019-06-26 stsp got_worktree_close(worktree);
3488 4e759de4 2019-06-26 stsp free(cwd);
3489 4e759de4 2019-06-26 stsp free(repo_path);
3490 4e759de4 2019-06-26 stsp return error;
3493 4e759de4 2019-06-26 stsp __dead static void
3494 4e759de4 2019-06-26 stsp usage_branch(void)
3496 4e759de4 2019-06-26 stsp fprintf(stderr,
3497 da76fce2 2020-02-24 stsp "usage: %s branch [-c commit] [-d] [-r repository] [-l] [-n] "
3498 da76fce2 2020-02-24 stsp "[name]\n", getprogname());
3502 4e759de4 2019-06-26 stsp static const struct got_error *
3503 4e99b47e 2019-10-04 stsp list_branch(struct got_repository *repo, struct got_worktree *worktree,
3504 4e99b47e 2019-10-04 stsp struct got_reference *ref)
3506 4e99b47e 2019-10-04 stsp const struct got_error *err = NULL;
3507 4e99b47e 2019-10-04 stsp const char *refname, *marker = " ";
3508 4e99b47e 2019-10-04 stsp char *refstr;
3510 4e99b47e 2019-10-04 stsp refname = got_ref_get_name(ref);
3511 4e99b47e 2019-10-04 stsp if (worktree && strcmp(refname,
3512 4e99b47e 2019-10-04 stsp got_worktree_get_head_ref_name(worktree)) == 0) {
3513 4e99b47e 2019-10-04 stsp struct got_object_id *id = NULL;
3515 4e99b47e 2019-10-04 stsp err = got_ref_resolve(&id, repo, ref);
3517 4e99b47e 2019-10-04 stsp return err;
3518 4e99b47e 2019-10-04 stsp if (got_object_id_cmp(id,
3519 4e99b47e 2019-10-04 stsp got_worktree_get_base_commit_id(worktree)) == 0)
3520 4e99b47e 2019-10-04 stsp marker = "* ";
3522 4e99b47e 2019-10-04 stsp marker = "~ ";
3526 4e99b47e 2019-10-04 stsp if (strncmp(refname, "refs/heads/", 11) == 0)
3527 4e99b47e 2019-10-04 stsp refname += 11;
3528 4e99b47e 2019-10-04 stsp if (strncmp(refname, "refs/got/worktree/", 18) == 0)
3529 4e99b47e 2019-10-04 stsp refname += 18;
3531 4e99b47e 2019-10-04 stsp refstr = got_ref_to_str(ref);
3532 4e99b47e 2019-10-04 stsp if (refstr == NULL)
3533 4e99b47e 2019-10-04 stsp return got_error_from_errno("got_ref_to_str");
3535 4e99b47e 2019-10-04 stsp printf("%s%s: %s\n", marker, refname, refstr);
3536 4e99b47e 2019-10-04 stsp free(refstr);
3537 4e99b47e 2019-10-04 stsp return NULL;
3540 4e99b47e 2019-10-04 stsp static const struct got_error *
3541 ad89fa31 2019-10-04 stsp show_current_branch(struct got_repository *repo, struct got_worktree *worktree)
3543 ad89fa31 2019-10-04 stsp const char *refname;
3545 ad89fa31 2019-10-04 stsp if (worktree == NULL)
3546 ad89fa31 2019-10-04 stsp return got_error(GOT_ERR_NOT_WORKTREE);
3548 ad89fa31 2019-10-04 stsp refname = got_worktree_get_head_ref_name(worktree);
3550 ad89fa31 2019-10-04 stsp if (strncmp(refname, "refs/heads/", 11) == 0)
3551 ad89fa31 2019-10-04 stsp refname += 11;
3552 ad89fa31 2019-10-04 stsp if (strncmp(refname, "refs/got/worktree/", 18) == 0)
3553 ad89fa31 2019-10-04 stsp refname += 18;
3555 ad89fa31 2019-10-04 stsp printf("%s\n", refname);
3557 ad89fa31 2019-10-04 stsp return NULL;
3560 ad89fa31 2019-10-04 stsp static const struct got_error *
3561 ba882ee3 2019-07-11 stsp list_branches(struct got_repository *repo, struct got_worktree *worktree)
3563 4e759de4 2019-06-26 stsp static const struct got_error *err = NULL;
3564 4e759de4 2019-06-26 stsp struct got_reflist_head refs;
3565 4e759de4 2019-06-26 stsp struct got_reflist_entry *re;
3566 4e99b47e 2019-10-04 stsp struct got_reference *temp_ref = NULL;
3567 4e99b47e 2019-10-04 stsp int rebase_in_progress, histedit_in_progress;
3569 4e759de4 2019-06-26 stsp SIMPLEQ_INIT(&refs);
3571 4e99b47e 2019-10-04 stsp if (worktree) {
3572 4e99b47e 2019-10-04 stsp err = got_worktree_rebase_in_progress(&rebase_in_progress,
3573 4e99b47e 2019-10-04 stsp worktree);
3575 4e99b47e 2019-10-04 stsp return err;
3577 4e99b47e 2019-10-04 stsp err = got_worktree_histedit_in_progress(&histedit_in_progress,
3578 4e99b47e 2019-10-04 stsp worktree);
3580 4e99b47e 2019-10-04 stsp return err;
3582 4e99b47e 2019-10-04 stsp if (rebase_in_progress || histedit_in_progress) {
3583 4e99b47e 2019-10-04 stsp err = got_ref_open(&temp_ref, repo,
3584 4e99b47e 2019-10-04 stsp got_worktree_get_head_ref_name(worktree), 0);
3586 4e99b47e 2019-10-04 stsp return err;
3587 4e99b47e 2019-10-04 stsp list_branch(repo, worktree, temp_ref);
3588 4e99b47e 2019-10-04 stsp got_ref_close(temp_ref);
3592 b8bad2ba 2019-08-23 stsp err = got_ref_list(&refs, repo, "refs/heads",
3593 b8bad2ba 2019-08-23 stsp got_ref_cmp_by_name, NULL);
3595 4e759de4 2019-06-26 stsp return err;
3597 4e99b47e 2019-10-04 stsp SIMPLEQ_FOREACH(re, &refs, entry)
3598 4e99b47e 2019-10-04 stsp list_branch(repo, worktree, re->ref);
3600 4e759de4 2019-06-26 stsp got_ref_list_free(&refs);
3601 4e759de4 2019-06-26 stsp return NULL;
3604 4e759de4 2019-06-26 stsp static const struct got_error *
3605 45cd4e47 2019-08-25 stsp delete_branch(struct got_repository *repo, struct got_worktree *worktree,
3606 45cd4e47 2019-08-25 stsp const char *branch_name)
3608 4e759de4 2019-06-26 stsp const struct got_error *err = NULL;
3609 45cd4e47 2019-08-25 stsp struct got_reference *ref = NULL;
3610 4e759de4 2019-06-26 stsp char *refname;
3612 4e759de4 2019-06-26 stsp if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
3613 4e759de4 2019-06-26 stsp return got_error_from_errno("asprintf");
3615 4e759de4 2019-06-26 stsp err = got_ref_open(&ref, repo, refname, 0);
3617 4e759de4 2019-06-26 stsp goto done;
3619 45cd4e47 2019-08-25 stsp if (worktree &&
3620 45cd4e47 2019-08-25 stsp strcmp(got_worktree_get_head_ref_name(worktree),
3621 45cd4e47 2019-08-25 stsp got_ref_get_name(ref)) == 0) {
3622 45cd4e47 2019-08-25 stsp err = got_error_msg(GOT_ERR_SAME_BRANCH,
3623 45cd4e47 2019-08-25 stsp "will not delete this work tree's current branch");
3624 45cd4e47 2019-08-25 stsp goto done;
3627 45cd4e47 2019-08-25 stsp err = got_ref_delete(ref, repo);
3630 45cd4e47 2019-08-25 stsp got_ref_close(ref);
3631 4e759de4 2019-06-26 stsp free(refname);
3632 4e759de4 2019-06-26 stsp return err;
3635 4e759de4 2019-06-26 stsp static const struct got_error *
3636 4e759de4 2019-06-26 stsp add_branch(struct got_repository *repo, const char *branch_name,
3637 a74f7e83 2019-11-10 stsp struct got_object_id *base_commit_id)
3639 4e759de4 2019-06-26 stsp const struct got_error *err = NULL;
3640 4e759de4 2019-06-26 stsp struct got_reference *ref = NULL;
3641 4e759de4 2019-06-26 stsp char *base_refname = NULL, *refname = NULL;
3644 bd5895f3 2019-11-28 stsp * Don't let the user create a branch name with a leading '-'.
3645 d3f84d51 2019-07-11 stsp * While technically a valid reference name, this case is usually
3646 d3f84d51 2019-07-11 stsp * an unintended typo.
3648 bd5895f3 2019-11-28 stsp if (branch_name[0] == '-')
3649 bd5895f3 2019-11-28 stsp return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
3651 4e759de4 2019-06-26 stsp if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
3652 4e759de4 2019-06-26 stsp err = got_error_from_errno("asprintf");
3653 4e759de4 2019-06-26 stsp goto done;
3656 4e759de4 2019-06-26 stsp err = got_ref_open(&ref, repo, refname, 0);
3657 4e759de4 2019-06-26 stsp if (err == NULL) {
3658 4e759de4 2019-06-26 stsp err = got_error(GOT_ERR_BRANCH_EXISTS);
3659 4e759de4 2019-06-26 stsp goto done;
3660 4e759de4 2019-06-26 stsp } else if (err->code != GOT_ERR_NOT_REF)
3661 4e759de4 2019-06-26 stsp goto done;
3663 a74f7e83 2019-11-10 stsp err = got_ref_alloc(&ref, refname, base_commit_id);
3665 4e759de4 2019-06-26 stsp goto done;
3667 4e759de4 2019-06-26 stsp err = got_ref_write(ref, repo);
3670 4e759de4 2019-06-26 stsp got_ref_close(ref);
3671 4e759de4 2019-06-26 stsp free(base_refname);
3672 4e759de4 2019-06-26 stsp free(refname);
3673 4e759de4 2019-06-26 stsp return err;
3676 4e759de4 2019-06-26 stsp static const struct got_error *
3677 4e759de4 2019-06-26 stsp cmd_branch(int argc, char *argv[])
3679 4e759de4 2019-06-26 stsp const struct got_error *error = NULL;
3680 4e759de4 2019-06-26 stsp struct got_repository *repo = NULL;
3681 4e759de4 2019-06-26 stsp struct got_worktree *worktree = NULL;
3682 4e759de4 2019-06-26 stsp char *cwd = NULL, *repo_path = NULL;
3683 da76fce2 2020-02-24 stsp int ch, do_list = 0, do_show = 0, do_update = 1;
3684 a74f7e83 2019-11-10 stsp const char *delref = NULL, *commit_id_arg = NULL;
3685 da76fce2 2020-02-24 stsp struct got_reference *ref = NULL;
3686 da76fce2 2020-02-24 stsp struct got_pathlist_head paths;
3687 da76fce2 2020-02-24 stsp struct got_pathlist_entry *pe;
3688 da76fce2 2020-02-24 stsp struct got_object_id *commit_id = NULL;
3689 da76fce2 2020-02-24 stsp char *commit_id_str = NULL;
3691 da76fce2 2020-02-24 stsp TAILQ_INIT(&paths);
3693 da76fce2 2020-02-24 stsp while ((ch = getopt(argc, argv, "c:d:r:ln")) != -1) {
3694 4e759de4 2019-06-26 stsp switch (ch) {
3696 a74f7e83 2019-11-10 stsp commit_id_arg = optarg;
3699 4e759de4 2019-06-26 stsp delref = optarg;
3702 4e759de4 2019-06-26 stsp repo_path = realpath(optarg, NULL);
3703 4e759de4 2019-06-26 stsp if (repo_path == NULL)
3704 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
3706 4e759de4 2019-06-26 stsp got_path_strip_trailing_slashes(repo_path);
3709 4e759de4 2019-06-26 stsp do_list = 1;
3712 da76fce2 2020-02-24 stsp do_update = 0;
3715 4e759de4 2019-06-26 stsp usage_branch();
3716 4e759de4 2019-06-26 stsp /* NOTREACHED */
3720 4e759de4 2019-06-26 stsp if (do_list && delref)
3721 4e759de4 2019-06-26 stsp errx(1, "-l and -d options are mutually exclusive\n");
3723 4e759de4 2019-06-26 stsp argc -= optind;
3724 4e759de4 2019-06-26 stsp argv += optind;
3726 ad89fa31 2019-10-04 stsp if (!do_list && !delref && argc == 0)
3727 ad89fa31 2019-10-04 stsp do_show = 1;
3729 a74f7e83 2019-11-10 stsp if ((do_list || delref || do_show) && commit_id_arg != NULL)
3730 a74f7e83 2019-11-10 stsp errx(1, "-c option can only be used when creating a branch");
3732 4e759de4 2019-06-26 stsp if (do_list || delref) {
3733 4e759de4 2019-06-26 stsp if (argc > 0)
3734 4e759de4 2019-06-26 stsp usage_branch();
3735 a74f7e83 2019-11-10 stsp } else if (!do_show && argc != 1)
3736 4e759de4 2019-06-26 stsp usage_branch();
3738 4e759de4 2019-06-26 stsp #ifndef PROFILE
3739 ad89fa31 2019-10-04 stsp if (do_list || do_show) {
3740 4e759de4 2019-06-26 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3741 4e759de4 2019-06-26 stsp NULL) == -1)
3742 4e759de4 2019-06-26 stsp err(1, "pledge");
3744 4e759de4 2019-06-26 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3745 4e759de4 2019-06-26 stsp "sendfd unveil", NULL) == -1)
3746 4e759de4 2019-06-26 stsp err(1, "pledge");
3749 4e759de4 2019-06-26 stsp cwd = getcwd(NULL, 0);
3750 4e759de4 2019-06-26 stsp if (cwd == NULL) {
3751 4e759de4 2019-06-26 stsp error = got_error_from_errno("getcwd");
3752 4e759de4 2019-06-26 stsp goto done;
3755 4e759de4 2019-06-26 stsp if (repo_path == NULL) {
3756 4e759de4 2019-06-26 stsp error = got_worktree_open(&worktree, cwd);
3757 4e759de4 2019-06-26 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
3758 4e759de4 2019-06-26 stsp goto done;
3760 4e759de4 2019-06-26 stsp error = NULL;
3761 4e759de4 2019-06-26 stsp if (worktree) {
3762 4e759de4 2019-06-26 stsp repo_path =
3763 4e759de4 2019-06-26 stsp strdup(got_worktree_get_repo_path(worktree));
3764 4e759de4 2019-06-26 stsp if (repo_path == NULL)
3765 4e759de4 2019-06-26 stsp error = got_error_from_errno("strdup");
3766 4e759de4 2019-06-26 stsp if (error)
3767 4e759de4 2019-06-26 stsp goto done;
3769 4e759de4 2019-06-26 stsp repo_path = strdup(cwd);
3770 4e759de4 2019-06-26 stsp if (repo_path == NULL) {
3771 4e759de4 2019-06-26 stsp error = got_error_from_errno("strdup");
3772 4e759de4 2019-06-26 stsp goto done;
3777 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
3778 4e759de4 2019-06-26 stsp if (error != NULL)
3779 4e759de4 2019-06-26 stsp goto done;
3781 4e759de4 2019-06-26 stsp error = apply_unveil(got_repo_get_path(repo), do_list,
3782 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
3783 4e759de4 2019-06-26 stsp if (error)
3784 4e759de4 2019-06-26 stsp goto done;
3786 ad89fa31 2019-10-04 stsp if (do_show)
3787 ad89fa31 2019-10-04 stsp error = show_current_branch(repo, worktree);
3788 ad89fa31 2019-10-04 stsp else if (do_list)
3789 ba882ee3 2019-07-11 stsp error = list_branches(repo, worktree);
3790 4e759de4 2019-06-26 stsp else if (delref)
3791 45cd4e47 2019-08-25 stsp error = delete_branch(repo, worktree, delref);
3793 a74f7e83 2019-11-10 stsp if (commit_id_arg == NULL)
3794 a74f7e83 2019-11-10 stsp commit_id_arg = worktree ?
3795 4e759de4 2019-06-26 stsp got_worktree_get_head_ref_name(worktree) :
3796 4e759de4 2019-06-26 stsp GOT_REF_HEAD;
3797 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
3798 71a27632 2020-01-15 stsp commit_id_arg, GOT_OBJ_TYPE_COMMIT, 1, repo);
3799 a74f7e83 2019-11-10 stsp if (error)
3800 a74f7e83 2019-11-10 stsp goto done;
3801 a74f7e83 2019-11-10 stsp error = add_branch(repo, argv[0], commit_id);
3802 da76fce2 2020-02-24 stsp if (error)
3803 da76fce2 2020-02-24 stsp goto done;
3804 da76fce2 2020-02-24 stsp if (worktree && do_update) {
3805 da76fce2 2020-02-24 stsp int did_something = 0;
3806 da76fce2 2020-02-24 stsp char *branch_refname = NULL;
3808 da76fce2 2020-02-24 stsp error = got_object_id_str(&commit_id_str, commit_id);
3809 da76fce2 2020-02-24 stsp if (error)
3810 da76fce2 2020-02-24 stsp goto done;
3811 da76fce2 2020-02-24 stsp error = get_worktree_paths_from_argv(&paths, 0, NULL,
3812 da76fce2 2020-02-24 stsp worktree);
3813 da76fce2 2020-02-24 stsp if (error)
3814 da76fce2 2020-02-24 stsp goto done;
3815 da76fce2 2020-02-24 stsp if (asprintf(&branch_refname, "refs/heads/%s", argv[0])
3817 da76fce2 2020-02-24 stsp error = got_error_from_errno("asprintf");
3818 da76fce2 2020-02-24 stsp goto done;
3820 da76fce2 2020-02-24 stsp error = got_ref_open(&ref, repo, branch_refname, 0);
3821 da76fce2 2020-02-24 stsp free(branch_refname);
3822 da76fce2 2020-02-24 stsp if (error)
3823 da76fce2 2020-02-24 stsp goto done;
3824 da76fce2 2020-02-24 stsp error = switch_head_ref(ref, commit_id, worktree,
3826 da76fce2 2020-02-24 stsp if (error)
3827 da76fce2 2020-02-24 stsp goto done;
3828 da76fce2 2020-02-24 stsp error = got_worktree_set_base_commit_id(worktree, repo,
3829 da76fce2 2020-02-24 stsp commit_id);
3830 da76fce2 2020-02-24 stsp if (error)
3831 da76fce2 2020-02-24 stsp goto done;
3832 da76fce2 2020-02-24 stsp error = got_worktree_checkout_files(worktree, &paths,
3833 da76fce2 2020-02-24 stsp repo, update_progress, &did_something,
3834 da76fce2 2020-02-24 stsp check_cancelled, NULL);
3835 da76fce2 2020-02-24 stsp if (error)
3836 da76fce2 2020-02-24 stsp goto done;
3837 da76fce2 2020-02-24 stsp if (did_something)
3838 da76fce2 2020-02-24 stsp printf("Updated to commit %s\n", commit_id_str);
3843 da76fce2 2020-02-24 stsp got_ref_close(ref);
3845 d0eebce4 2019-03-11 stsp got_repo_close(repo);
3846 d0eebce4 2019-03-11 stsp if (worktree)
3847 d0eebce4 2019-03-11 stsp got_worktree_close(worktree);
3848 d0eebce4 2019-03-11 stsp free(cwd);
3849 d0eebce4 2019-03-11 stsp free(repo_path);
3850 da76fce2 2020-02-24 stsp free(commit_id);
3851 da76fce2 2020-02-24 stsp free(commit_id_str);
3852 da76fce2 2020-02-24 stsp TAILQ_FOREACH(pe, &paths, entry)
3853 da76fce2 2020-02-24 stsp free((char *)pe->path);
3854 da76fce2 2020-02-24 stsp got_pathlist_free(&paths);
3855 d00136be 2019-03-26 stsp return error;
3859 d00136be 2019-03-26 stsp __dead static void
3860 8e7bd50a 2019-08-22 stsp usage_tag(void)
3862 8e7bd50a 2019-08-22 stsp fprintf(stderr,
3863 80106605 2020-02-24 stsp "usage: %s tag [-c commit] [-r repository] [-l] "
3864 80106605 2020-02-24 stsp "[-m message] name\n", getprogname());
3869 b8bad2ba 2019-08-23 stsp static const struct got_error *
3870 b8bad2ba 2019-08-23 stsp sort_tags(struct got_reflist_head *sorted, struct got_reflist_head *tags)
3872 b8bad2ba 2019-08-23 stsp const struct got_error *err = NULL;
3873 b8bad2ba 2019-08-23 stsp struct got_reflist_entry *re, *se, *new;
3874 b8bad2ba 2019-08-23 stsp struct got_object_id *re_id, *se_id;
3875 b8bad2ba 2019-08-23 stsp struct got_tag_object *re_tag, *se_tag;
3876 b8bad2ba 2019-08-23 stsp time_t re_time, se_time;
3878 b8bad2ba 2019-08-23 stsp SIMPLEQ_FOREACH(re, tags, entry) {
3879 b8bad2ba 2019-08-23 stsp se = SIMPLEQ_FIRST(sorted);
3880 b8bad2ba 2019-08-23 stsp if (se == NULL) {
3881 b8bad2ba 2019-08-23 stsp err = got_reflist_entry_dup(&new, re);
3883 b8bad2ba 2019-08-23 stsp return err;
3884 b8bad2ba 2019-08-23 stsp SIMPLEQ_INSERT_HEAD(sorted, new, entry);
3887 b8bad2ba 2019-08-23 stsp err = got_ref_resolve(&re_id, repo, re->ref);
3890 b8bad2ba 2019-08-23 stsp err = got_object_open_as_tag(&re_tag, repo, re_id);
3891 b8bad2ba 2019-08-23 stsp free(re_id);
3894 b8bad2ba 2019-08-23 stsp re_time = got_object_tag_get_tagger_time(re_tag);
3895 b8bad2ba 2019-08-23 stsp got_object_tag_close(re_tag);
3898 b8bad2ba 2019-08-23 stsp while (se) {
3899 b8bad2ba 2019-08-23 stsp err = got_ref_resolve(&se_id, repo, re->ref);
3902 b8bad2ba 2019-08-23 stsp err = got_object_open_as_tag(&se_tag, repo, se_id);
3903 b8bad2ba 2019-08-23 stsp free(se_id);
3906 b8bad2ba 2019-08-23 stsp se_time = got_object_tag_get_tagger_time(se_tag);
3907 b8bad2ba 2019-08-23 stsp got_object_tag_close(se_tag);
3909 b8bad2ba 2019-08-23 stsp if (se_time > re_time) {
3910 b8bad2ba 2019-08-23 stsp err = got_reflist_entry_dup(&new, re);
3912 b8bad2ba 2019-08-23 stsp return err;
3913 b8bad2ba 2019-08-23 stsp SIMPLEQ_INSERT_AFTER(sorted, se, new, entry);
3916 b8bad2ba 2019-08-23 stsp se = SIMPLEQ_NEXT(se, entry);
3921 b8bad2ba 2019-08-23 stsp return err;
3925 b8bad2ba 2019-08-23 stsp static const struct got_error *
3926 8e7bd50a 2019-08-22 stsp list_tags(struct got_repository *repo, struct got_worktree *worktree)
3928 8e7bd50a 2019-08-22 stsp static const struct got_error *err = NULL;
3929 8e7bd50a 2019-08-22 stsp struct got_reflist_head refs;
3930 8e7bd50a 2019-08-22 stsp struct got_reflist_entry *re;
3932 8e7bd50a 2019-08-22 stsp SIMPLEQ_INIT(&refs);
3934 d1f16636 2020-01-15 stsp err = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags, repo);
3936 8e7bd50a 2019-08-22 stsp return err;
3938 8e7bd50a 2019-08-22 stsp SIMPLEQ_FOREACH(re, &refs, entry) {
3939 8e7bd50a 2019-08-22 stsp const char *refname;
3940 8e7bd50a 2019-08-22 stsp char *refstr, *tagmsg0, *tagmsg, *line, *id_str, *datestr;
3941 8e7bd50a 2019-08-22 stsp char datebuf[26];
3942 d4efa91b 2020-01-14 stsp const char *tagger;
3943 8e7bd50a 2019-08-22 stsp time_t tagger_time;
3944 8e7bd50a 2019-08-22 stsp struct got_object_id *id;
3945 8e7bd50a 2019-08-22 stsp struct got_tag_object *tag;
3946 d4efa91b 2020-01-14 stsp struct got_commit_object *commit = NULL;
3948 8e7bd50a 2019-08-22 stsp refname = got_ref_get_name(re->ref);
3949 8e7bd50a 2019-08-22 stsp if (strncmp(refname, "refs/tags/", 10) != 0)
3951 8e7bd50a 2019-08-22 stsp refname += 10;
3952 8e7bd50a 2019-08-22 stsp refstr = got_ref_to_str(re->ref);
3953 8e7bd50a 2019-08-22 stsp if (refstr == NULL) {
3954 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("got_ref_to_str");
3957 8e7bd50a 2019-08-22 stsp printf("%stag %s %s\n", GOT_COMMIT_SEP_STR, refname, refstr);
3958 8e7bd50a 2019-08-22 stsp free(refstr);
3960 8e7bd50a 2019-08-22 stsp err = got_ref_resolve(&id, repo, re->ref);
3963 8e7bd50a 2019-08-22 stsp err = got_object_open_as_tag(&tag, repo, id);
3964 d4efa91b 2020-01-14 stsp if (err) {
3965 d4efa91b 2020-01-14 stsp if (err->code != GOT_ERR_OBJ_TYPE) {
3969 d4efa91b 2020-01-14 stsp /* "lightweight" tag */
3970 d4efa91b 2020-01-14 stsp err = got_object_open_as_commit(&commit, repo, id);
3971 d4efa91b 2020-01-14 stsp if (err) {
3975 d4efa91b 2020-01-14 stsp tagger = got_object_commit_get_committer(commit);
3976 d4efa91b 2020-01-14 stsp tagger_time =
3977 d4efa91b 2020-01-14 stsp got_object_commit_get_committer_time(commit);
3978 d4efa91b 2020-01-14 stsp err = got_object_id_str(&id_str, id);
3984 d4efa91b 2020-01-14 stsp tagger = got_object_tag_get_tagger(tag);
3985 d4efa91b 2020-01-14 stsp tagger_time = got_object_tag_get_tagger_time(tag);
3986 d4efa91b 2020-01-14 stsp err = got_object_id_str(&id_str,
3987 d4efa91b 2020-01-14 stsp got_object_tag_get_object_id(tag));
3991 d4efa91b 2020-01-14 stsp printf("from: %s\n", tagger);
3992 2417344c 2019-08-23 stsp datestr = get_datestr(&tagger_time, datebuf);
3993 2417344c 2019-08-23 stsp if (datestr)
3994 2417344c 2019-08-23 stsp printf("date: %s UTC\n", datestr);
3995 d4efa91b 2020-01-14 stsp if (commit)
3996 2417344c 2019-08-23 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
3998 d4efa91b 2020-01-14 stsp switch (got_object_tag_get_object_type(tag)) {
3999 d4efa91b 2020-01-14 stsp case GOT_OBJ_TYPE_BLOB:
4000 d4efa91b 2020-01-14 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_BLOB,
4003 d4efa91b 2020-01-14 stsp case GOT_OBJ_TYPE_TREE:
4004 d4efa91b 2020-01-14 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_TREE,
4007 d4efa91b 2020-01-14 stsp case GOT_OBJ_TYPE_COMMIT:
4008 d4efa91b 2020-01-14 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT,
4011 d4efa91b 2020-01-14 stsp case GOT_OBJ_TYPE_TAG:
4012 d4efa91b 2020-01-14 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_TAG,
4019 8e7bd50a 2019-08-22 stsp free(id_str);
4020 d4efa91b 2020-01-14 stsp if (commit) {
4021 d4efa91b 2020-01-14 stsp err = got_object_commit_get_logmsg(&tagmsg0, commit);
4024 d4efa91b 2020-01-14 stsp got_object_commit_close(commit);
4026 d4efa91b 2020-01-14 stsp tagmsg0 = strdup(got_object_tag_get_message(tag));
4027 d4efa91b 2020-01-14 stsp got_object_tag_close(tag);
4028 d4efa91b 2020-01-14 stsp if (tagmsg0 == NULL) {
4029 d4efa91b 2020-01-14 stsp err = got_error_from_errno("strdup");
4034 8e7bd50a 2019-08-22 stsp tagmsg = tagmsg0;
4036 8e7bd50a 2019-08-22 stsp line = strsep(&tagmsg, "\n");
4038 8e7bd50a 2019-08-22 stsp printf(" %s\n", line);
4039 8e7bd50a 2019-08-22 stsp } while (line);
4040 8e7bd50a 2019-08-22 stsp free(tagmsg0);
4043 8e7bd50a 2019-08-22 stsp got_ref_list_free(&refs);
4044 8e7bd50a 2019-08-22 stsp return NULL;
4047 8e7bd50a 2019-08-22 stsp static const struct got_error *
4048 f372d5cd 2019-10-21 stsp get_tag_message(char **tagmsg, char **tagmsg_path, const char *commit_id_str,
4049 62870f63 2019-08-22 stsp const char *tag_name, const char *repo_path)
4051 8e7bd50a 2019-08-22 stsp const struct got_error *err = NULL;
4052 8e7bd50a 2019-08-22 stsp char *template = NULL, *initial_content = NULL;
4053 f372d5cd 2019-10-21 stsp char *editor = NULL;
4054 8e7bd50a 2019-08-22 stsp int fd = -1;
4056 bb63914a 2020-02-17 stsp if (asprintf(&template, GOT_TMPDIR_STR "/got-tagmsg") == -1) {
4057 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("asprintf");
4058 8e7bd50a 2019-08-22 stsp goto done;
4061 62870f63 2019-08-22 stsp if (asprintf(&initial_content, "\n# tagging commit %s as %s\n",
4062 62870f63 2019-08-22 stsp commit_id_str, tag_name) == -1) {
4063 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("asprintf");
4064 8e7bd50a 2019-08-22 stsp goto done;
4067 f372d5cd 2019-10-21 stsp err = got_opentemp_named_fd(tagmsg_path, &fd, template);
4069 8e7bd50a 2019-08-22 stsp goto done;
4071 8e7bd50a 2019-08-22 stsp dprintf(fd, initial_content);
4072 8e7bd50a 2019-08-22 stsp close(fd);
4074 8e7bd50a 2019-08-22 stsp err = get_editor(&editor);
4076 8e7bd50a 2019-08-22 stsp goto done;
4077 f372d5cd 2019-10-21 stsp err = edit_logmsg(tagmsg, editor, *tagmsg_path, initial_content);
4079 8e7bd50a 2019-08-22 stsp free(initial_content);
4080 8e7bd50a 2019-08-22 stsp free(template);
4081 8e7bd50a 2019-08-22 stsp free(editor);
4083 8e7bd50a 2019-08-22 stsp /* Editor is done; we can now apply unveil(2) */
4084 8e7bd50a 2019-08-22 stsp if (err == NULL) {
4085 8e7bd50a 2019-08-22 stsp err = apply_unveil(repo_path, 0, NULL);
4086 8e7bd50a 2019-08-22 stsp if (err) {
4087 8e7bd50a 2019-08-22 stsp free(*tagmsg);
4088 8e7bd50a 2019-08-22 stsp *tagmsg = NULL;
4091 8e7bd50a 2019-08-22 stsp return err;
4094 8e7bd50a 2019-08-22 stsp static const struct got_error *
4095 8e7bd50a 2019-08-22 stsp add_tag(struct got_repository *repo, const char *tag_name,
4096 8e7bd50a 2019-08-22 stsp const char *commit_arg, const char *tagmsg_arg)
4098 8e7bd50a 2019-08-22 stsp const struct got_error *err = NULL;
4099 8e7bd50a 2019-08-22 stsp struct got_object_id *commit_id = NULL, *tag_id = NULL;
4100 8e7bd50a 2019-08-22 stsp char *label = NULL, *commit_id_str = NULL;
4101 8e7bd50a 2019-08-22 stsp struct got_reference *ref = NULL;
4102 aba9c984 2019-09-08 stsp char *refname = NULL, *tagmsg = NULL, *tagger = NULL;
4103 f372d5cd 2019-10-21 stsp char *tagmsg_path = NULL, *tag_id_str = NULL;
4104 f372d5cd 2019-10-21 stsp int preserve_tagmsg = 0;
4107 bd5895f3 2019-11-28 stsp * Don't let the user create a tag name with a leading '-'.
4108 8e7bd50a 2019-08-22 stsp * While technically a valid reference name, this case is usually
4109 8e7bd50a 2019-08-22 stsp * an unintended typo.
4111 bd5895f3 2019-11-28 stsp if (tag_name[0] == '-')
4112 bd5895f3 2019-11-28 stsp return got_error_path(tag_name, GOT_ERR_REF_NAME_MINUS);
4114 aba9c984 2019-09-08 stsp err = get_author(&tagger, repo);
4116 8e7bd50a 2019-08-22 stsp return err;
4118 71a27632 2020-01-15 stsp err = got_repo_match_object_id(&commit_id, &label, commit_arg,
4119 8e7bd50a 2019-08-22 stsp GOT_OBJ_TYPE_COMMIT, 1, repo);
4121 8e7bd50a 2019-08-22 stsp goto done;
4123 8e7bd50a 2019-08-22 stsp err = got_object_id_str(&commit_id_str, commit_id);
4125 8e7bd50a 2019-08-22 stsp goto done;
4127 8e7bd50a 2019-08-22 stsp if (strncmp("refs/tags/", tag_name, 10) == 0) {
4128 8e7bd50a 2019-08-22 stsp refname = strdup(tag_name);
4129 8e7bd50a 2019-08-22 stsp if (refname == NULL) {
4130 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("strdup");
4131 8e7bd50a 2019-08-22 stsp goto done;
4133 8e7bd50a 2019-08-22 stsp tag_name += 10;
4134 8e7bd50a 2019-08-22 stsp } else if (asprintf(&refname, "refs/tags/%s", tag_name) == -1) {
4135 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("asprintf");
4136 8e7bd50a 2019-08-22 stsp goto done;
4139 8e7bd50a 2019-08-22 stsp err = got_ref_open(&ref, repo, refname, 0);
4140 8e7bd50a 2019-08-22 stsp if (err == NULL) {
4141 8e7bd50a 2019-08-22 stsp err = got_error(GOT_ERR_TAG_EXISTS);
4142 8e7bd50a 2019-08-22 stsp goto done;
4143 8e7bd50a 2019-08-22 stsp } else if (err->code != GOT_ERR_NOT_REF)
4144 8e7bd50a 2019-08-22 stsp goto done;
4146 8e7bd50a 2019-08-22 stsp if (tagmsg_arg == NULL) {
4147 f372d5cd 2019-10-21 stsp err = get_tag_message(&tagmsg, &tagmsg_path, commit_id_str,
4148 62870f63 2019-08-22 stsp tag_name, got_repo_get_path(repo));
4149 f372d5cd 2019-10-21 stsp if (err) {
4150 f372d5cd 2019-10-21 stsp if (err->code != GOT_ERR_COMMIT_MSG_EMPTY &&
4151 f372d5cd 2019-10-21 stsp tagmsg_path != NULL)
4152 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
4153 8e7bd50a 2019-08-22 stsp goto done;
4157 8e7bd50a 2019-08-22 stsp err = got_object_tag_create(&tag_id, tag_name, commit_id,
4158 8e7bd50a 2019-08-22 stsp tagger, time(NULL), tagmsg ? tagmsg : tagmsg_arg, repo);
4159 f372d5cd 2019-10-21 stsp if (err) {
4160 f372d5cd 2019-10-21 stsp if (tagmsg_path)
4161 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
4162 8e7bd50a 2019-08-22 stsp goto done;
4165 8e7bd50a 2019-08-22 stsp err = got_ref_alloc(&ref, refname, tag_id);
4166 f372d5cd 2019-10-21 stsp if (err) {
4167 f372d5cd 2019-10-21 stsp if (tagmsg_path)
4168 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
4169 8e7bd50a 2019-08-22 stsp goto done;
4172 8e7bd50a 2019-08-22 stsp err = got_ref_write(ref, repo);
4173 f372d5cd 2019-10-21 stsp if (err) {
4174 f372d5cd 2019-10-21 stsp if (tagmsg_path)
4175 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
4176 f372d5cd 2019-10-21 stsp goto done;
4179 f372d5cd 2019-10-21 stsp err = got_object_id_str(&tag_id_str, tag_id);
4180 f372d5cd 2019-10-21 stsp if (err) {
4181 f372d5cd 2019-10-21 stsp if (tagmsg_path)
4182 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
4183 f372d5cd 2019-10-21 stsp goto done;
4185 f372d5cd 2019-10-21 stsp printf("Created tag %s\n", tag_id_str);
4187 f372d5cd 2019-10-21 stsp if (preserve_tagmsg) {
4188 f372d5cd 2019-10-21 stsp fprintf(stderr, "%s: tag message preserved in %s\n",
4189 f372d5cd 2019-10-21 stsp getprogname(), tagmsg_path);
4190 f372d5cd 2019-10-21 stsp } else if (tagmsg_path && unlink(tagmsg_path) == -1 && err == NULL)
4191 f372d5cd 2019-10-21 stsp err = got_error_from_errno2("unlink", tagmsg_path);
4192 f372d5cd 2019-10-21 stsp free(tag_id_str);
4194 8e7bd50a 2019-08-22 stsp got_ref_close(ref);
4195 8e7bd50a 2019-08-22 stsp free(commit_id);
4196 8e7bd50a 2019-08-22 stsp free(commit_id_str);
4197 8e7bd50a 2019-08-22 stsp free(refname);
4198 8e7bd50a 2019-08-22 stsp free(tagmsg);
4199 f372d5cd 2019-10-21 stsp free(tagmsg_path);
4200 aba9c984 2019-09-08 stsp free(tagger);
4201 8e7bd50a 2019-08-22 stsp return err;
4204 8e7bd50a 2019-08-22 stsp static const struct got_error *
4205 8e7bd50a 2019-08-22 stsp cmd_tag(int argc, char *argv[])
4207 8e7bd50a 2019-08-22 stsp const struct got_error *error = NULL;
4208 8e7bd50a 2019-08-22 stsp struct got_repository *repo = NULL;
4209 8e7bd50a 2019-08-22 stsp struct got_worktree *worktree = NULL;
4210 8e7bd50a 2019-08-22 stsp char *cwd = NULL, *repo_path = NULL, *commit_id_str = NULL;
4211 c9956ddf 2019-09-08 stsp char *gitconfig_path = NULL;
4212 8e7bd50a 2019-08-22 stsp const char *tag_name, *commit_id_arg = NULL, *tagmsg = NULL;
4213 8e7bd50a 2019-08-22 stsp int ch, do_list = 0;
4215 80106605 2020-02-24 stsp while ((ch = getopt(argc, argv, "c:m:r:l")) != -1) {
4216 8e7bd50a 2019-08-22 stsp switch (ch) {
4218 80106605 2020-02-24 stsp commit_id_arg = optarg;
4221 8e7bd50a 2019-08-22 stsp tagmsg = optarg;
4224 8e7bd50a 2019-08-22 stsp repo_path = realpath(optarg, NULL);
4225 8e7bd50a 2019-08-22 stsp if (repo_path == NULL)
4226 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
4228 8e7bd50a 2019-08-22 stsp got_path_strip_trailing_slashes(repo_path);
4231 8e7bd50a 2019-08-22 stsp do_list = 1;
4234 8e7bd50a 2019-08-22 stsp usage_tag();
4235 8e7bd50a 2019-08-22 stsp /* NOTREACHED */
4239 8e7bd50a 2019-08-22 stsp argc -= optind;
4240 8e7bd50a 2019-08-22 stsp argv += optind;
4242 8e7bd50a 2019-08-22 stsp if (do_list) {
4243 80106605 2020-02-24 stsp if (commit_id_arg != NULL)
4244 80106605 2020-02-24 stsp errx(1, "-c option can only be used when creating a tag");
4245 8e7bd50a 2019-08-22 stsp if (tagmsg)
4246 80106605 2020-02-24 stsp errx(1, "-l and -m options are mutually exclusive");
4247 8e7bd50a 2019-08-22 stsp if (argc > 0)
4248 8e7bd50a 2019-08-22 stsp usage_tag();
4249 80106605 2020-02-24 stsp } else if (argc != 1)
4250 8e7bd50a 2019-08-22 stsp usage_tag();
4252 a2887370 2019-08-23 stsp tag_name = argv[0];
4254 8e7bd50a 2019-08-22 stsp #ifndef PROFILE
4255 8e7bd50a 2019-08-22 stsp if (do_list) {
4256 8e7bd50a 2019-08-22 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
4257 8e7bd50a 2019-08-22 stsp NULL) == -1)
4258 8e7bd50a 2019-08-22 stsp err(1, "pledge");
4260 8e7bd50a 2019-08-22 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
4261 8e7bd50a 2019-08-22 stsp "sendfd unveil", NULL) == -1)
4262 8e7bd50a 2019-08-22 stsp err(1, "pledge");
4265 8e7bd50a 2019-08-22 stsp cwd = getcwd(NULL, 0);
4266 8e7bd50a 2019-08-22 stsp if (cwd == NULL) {
4267 8e7bd50a 2019-08-22 stsp error = got_error_from_errno("getcwd");
4268 8e7bd50a 2019-08-22 stsp goto done;
4271 8e7bd50a 2019-08-22 stsp if (repo_path == NULL) {
4272 8e7bd50a 2019-08-22 stsp error = got_worktree_open(&worktree, cwd);
4273 8e7bd50a 2019-08-22 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4274 8e7bd50a 2019-08-22 stsp goto done;
4276 8e7bd50a 2019-08-22 stsp error = NULL;
4277 8e7bd50a 2019-08-22 stsp if (worktree) {
4278 8e7bd50a 2019-08-22 stsp repo_path =
4279 8e7bd50a 2019-08-22 stsp strdup(got_worktree_get_repo_path(worktree));
4280 8e7bd50a 2019-08-22 stsp if (repo_path == NULL)
4281 8e7bd50a 2019-08-22 stsp error = got_error_from_errno("strdup");
4282 8e7bd50a 2019-08-22 stsp if (error)
4283 8e7bd50a 2019-08-22 stsp goto done;
4285 8e7bd50a 2019-08-22 stsp repo_path = strdup(cwd);
4286 8e7bd50a 2019-08-22 stsp if (repo_path == NULL) {
4287 8e7bd50a 2019-08-22 stsp error = got_error_from_errno("strdup");
4288 8e7bd50a 2019-08-22 stsp goto done;
4293 8e7bd50a 2019-08-22 stsp if (do_list) {
4294 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
4295 c9956ddf 2019-09-08 stsp if (error != NULL)
4296 c9956ddf 2019-09-08 stsp goto done;
4297 8e7bd50a 2019-08-22 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4298 8e7bd50a 2019-08-22 stsp if (error)
4299 8e7bd50a 2019-08-22 stsp goto done;
4300 8e7bd50a 2019-08-22 stsp error = list_tags(repo, worktree);
4302 c9956ddf 2019-09-08 stsp error = get_gitconfig_path(&gitconfig_path);
4303 c9956ddf 2019-09-08 stsp if (error)
4304 c9956ddf 2019-09-08 stsp goto done;
4305 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, gitconfig_path);
4306 c9956ddf 2019-09-08 stsp if (error != NULL)
4307 c9956ddf 2019-09-08 stsp goto done;
4309 8e7bd50a 2019-08-22 stsp if (tagmsg) {
4310 8e7bd50a 2019-08-22 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4311 8e7bd50a 2019-08-22 stsp if (error)
4312 8e7bd50a 2019-08-22 stsp goto done;
4315 8e7bd50a 2019-08-22 stsp if (commit_id_arg == NULL) {
4316 8e7bd50a 2019-08-22 stsp struct got_reference *head_ref;
4317 8e7bd50a 2019-08-22 stsp struct got_object_id *commit_id;
4318 8e7bd50a 2019-08-22 stsp error = got_ref_open(&head_ref, repo,
4319 8e7bd50a 2019-08-22 stsp worktree ? got_worktree_get_head_ref_name(worktree)
4320 8e7bd50a 2019-08-22 stsp : GOT_REF_HEAD, 0);
4321 8e7bd50a 2019-08-22 stsp if (error)
4322 8e7bd50a 2019-08-22 stsp goto done;
4323 8e7bd50a 2019-08-22 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
4324 8e7bd50a 2019-08-22 stsp got_ref_close(head_ref);
4325 8e7bd50a 2019-08-22 stsp if (error)
4326 8e7bd50a 2019-08-22 stsp goto done;
4327 8e7bd50a 2019-08-22 stsp error = got_object_id_str(&commit_id_str, commit_id);
4328 8e7bd50a 2019-08-22 stsp free(commit_id);
4329 8e7bd50a 2019-08-22 stsp if (error)
4330 8e7bd50a 2019-08-22 stsp goto done;
4333 8e7bd50a 2019-08-22 stsp error = add_tag(repo, tag_name,
4334 8e7bd50a 2019-08-22 stsp commit_id_str ? commit_id_str : commit_id_arg, tagmsg);
4338 8e7bd50a 2019-08-22 stsp got_repo_close(repo);
4339 8e7bd50a 2019-08-22 stsp if (worktree)
4340 8e7bd50a 2019-08-22 stsp got_worktree_close(worktree);
4341 8e7bd50a 2019-08-22 stsp free(cwd);
4342 8e7bd50a 2019-08-22 stsp free(repo_path);
4343 c9956ddf 2019-09-08 stsp free(gitconfig_path);
4344 8e7bd50a 2019-08-22 stsp free(commit_id_str);
4345 8e7bd50a 2019-08-22 stsp return error;
4348 8e7bd50a 2019-08-22 stsp __dead static void
4349 d00136be 2019-03-26 stsp usage_add(void)
4351 c29c428a 2019-12-16 stsp fprintf(stderr, "usage: %s add [-R] [-I] path ...\n",
4352 022fae89 2019-12-06 tracey getprogname());
4356 d00136be 2019-03-26 stsp static const struct got_error *
4357 4e68cba3 2019-11-23 stsp add_progress(void *arg, unsigned char status, const char *path)
4359 4e68cba3 2019-11-23 stsp while (path[0] == '/')
4361 4e68cba3 2019-11-23 stsp printf("%c %s\n", status, path);
4362 4e68cba3 2019-11-23 stsp return NULL;
4365 4e68cba3 2019-11-23 stsp static const struct got_error *
4366 d00136be 2019-03-26 stsp cmd_add(int argc, char *argv[])
4368 d00136be 2019-03-26 stsp const struct got_error *error = NULL;
4369 031a5338 2019-03-26 stsp struct got_repository *repo = NULL;
4370 d00136be 2019-03-26 stsp struct got_worktree *worktree = NULL;
4371 1dd54920 2019-05-11 stsp char *cwd = NULL;
4372 1dd54920 2019-05-11 stsp struct got_pathlist_head paths;
4373 1dd54920 2019-05-11 stsp struct got_pathlist_entry *pe;
4374 022fae89 2019-12-06 tracey int ch, can_recurse = 0, no_ignores = 0;
4376 1dd54920 2019-05-11 stsp TAILQ_INIT(&paths);
4378 022fae89 2019-12-06 tracey while ((ch = getopt(argc, argv, "IR")) != -1) {
4379 d00136be 2019-03-26 stsp switch (ch) {
4380 022fae89 2019-12-06 tracey case 'I':
4381 022fae89 2019-12-06 tracey no_ignores = 1;
4384 4e68cba3 2019-11-23 stsp can_recurse = 1;
4387 d00136be 2019-03-26 stsp usage_add();
4388 d00136be 2019-03-26 stsp /* NOTREACHED */
4392 d00136be 2019-03-26 stsp argc -= optind;
4393 d00136be 2019-03-26 stsp argv += optind;
4395 43012d58 2019-07-14 stsp #ifndef PROFILE
4396 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4397 43012d58 2019-07-14 stsp NULL) == -1)
4398 43012d58 2019-07-14 stsp err(1, "pledge");
4400 723c305c 2019-05-11 jcs if (argc < 1)
4401 d00136be 2019-03-26 stsp usage_add();
4403 d00136be 2019-03-26 stsp cwd = getcwd(NULL, 0);
4404 d00136be 2019-03-26 stsp if (cwd == NULL) {
4405 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
4406 d00136be 2019-03-26 stsp goto done;
4409 d00136be 2019-03-26 stsp error = got_worktree_open(&worktree, cwd);
4410 d00136be 2019-03-26 stsp if (error)
4411 d00136be 2019-03-26 stsp goto done;
4413 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4415 031a5338 2019-03-26 stsp if (error != NULL)
4416 031a5338 2019-03-26 stsp goto done;
4418 031a5338 2019-03-26 stsp error = apply_unveil(got_repo_get_path(repo), 1,
4419 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
4420 d00136be 2019-03-26 stsp if (error)
4421 d00136be 2019-03-26 stsp goto done;
4423 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4424 6d022e97 2019-08-04 stsp if (error)
4425 022fae89 2019-12-06 tracey goto done;
4427 022fae89 2019-12-06 tracey if (!can_recurse && no_ignores) {
4428 022fae89 2019-12-06 tracey error = got_error_msg(GOT_ERR_BAD_PATH,
4429 022fae89 2019-12-06 tracey "disregarding ignores requires -R option");
4430 6d022e97 2019-08-04 stsp goto done;
4434 4e68cba3 2019-11-23 stsp if (!can_recurse) {
4435 4e68cba3 2019-11-23 stsp char *ondisk_path;
4436 4e68cba3 2019-11-23 stsp struct stat sb;
4437 4e68cba3 2019-11-23 stsp TAILQ_FOREACH(pe, &paths, entry) {
4438 4e68cba3 2019-11-23 stsp if (asprintf(&ondisk_path, "%s/%s",
4439 4e68cba3 2019-11-23 stsp got_worktree_get_root_path(worktree),
4440 4e68cba3 2019-11-23 stsp pe->path) == -1) {
4441 4e68cba3 2019-11-23 stsp error = got_error_from_errno("asprintf");
4442 4e68cba3 2019-11-23 stsp goto done;
4444 4e68cba3 2019-11-23 stsp if (lstat(ondisk_path, &sb) == -1) {
4445 4e68cba3 2019-11-23 stsp if (errno == ENOENT) {
4446 4e68cba3 2019-11-23 stsp free(ondisk_path);
4449 4e68cba3 2019-11-23 stsp error = got_error_from_errno2("lstat",
4450 4e68cba3 2019-11-23 stsp ondisk_path);
4451 4e68cba3 2019-11-23 stsp free(ondisk_path);
4452 4e68cba3 2019-11-23 stsp goto done;
4454 4e68cba3 2019-11-23 stsp free(ondisk_path);
4455 4e68cba3 2019-11-23 stsp if (S_ISDIR(sb.st_mode)) {
4456 4e68cba3 2019-11-23 stsp error = got_error_msg(GOT_ERR_BAD_PATH,
4457 4e68cba3 2019-11-23 stsp "adding directories requires -R option");
4458 4e68cba3 2019-11-23 stsp goto done;
4463 4e68cba3 2019-11-23 stsp error = got_worktree_schedule_add(worktree, &paths, add_progress,
4464 022fae89 2019-12-06 tracey NULL, repo, no_ignores);
4467 031a5338 2019-03-26 stsp got_repo_close(repo);
4468 d00136be 2019-03-26 stsp if (worktree)
4469 d00136be 2019-03-26 stsp got_worktree_close(worktree);
4470 1dd54920 2019-05-11 stsp TAILQ_FOREACH(pe, &paths, entry)
4471 1dd54920 2019-05-11 stsp free((char *)pe->path);
4472 1dd54920 2019-05-11 stsp got_pathlist_free(&paths);
4473 2ec1f75b 2019-03-26 stsp free(cwd);
4474 2ec1f75b 2019-03-26 stsp return error;
4477 2ec1f75b 2019-03-26 stsp __dead static void
4478 648e4ef7 2019-07-09 stsp usage_remove(void)
4480 c29c428a 2019-12-16 stsp fprintf(stderr, "usage: %s remove [-f] [-k] [-R] path ...\n",
4481 f2a9dc41 2019-12-13 tracey getprogname());
4485 2a06fe5f 2019-08-24 stsp static const struct got_error *
4486 2a06fe5f 2019-08-24 stsp print_remove_status(void *arg, unsigned char status,
4487 f2a9dc41 2019-12-13 tracey unsigned char staged_status, const char *path)
4489 f2a9dc41 2019-12-13 tracey while (path[0] == '/')
4491 2a06fe5f 2019-08-24 stsp if (status == GOT_STATUS_NONEXISTENT)
4492 2a06fe5f 2019-08-24 stsp return NULL;
4493 2a06fe5f 2019-08-24 stsp if (status == staged_status && (status == GOT_STATUS_DELETE))
4494 2a06fe5f 2019-08-24 stsp status = GOT_STATUS_NO_CHANGE;
4495 2a06fe5f 2019-08-24 stsp printf("%c%c %s\n", status, staged_status, path);
4496 2a06fe5f 2019-08-24 stsp return NULL;
4499 2ec1f75b 2019-03-26 stsp static const struct got_error *
4500 648e4ef7 2019-07-09 stsp cmd_remove(int argc, char *argv[])
4502 2ec1f75b 2019-03-26 stsp const struct got_error *error = NULL;
4503 2ec1f75b 2019-03-26 stsp struct got_worktree *worktree = NULL;
4504 2ec1f75b 2019-03-26 stsp struct got_repository *repo = NULL;
4505 17ed4618 2019-06-02 stsp char *cwd = NULL;
4506 17ed4618 2019-06-02 stsp struct got_pathlist_head paths;
4507 17ed4618 2019-06-02 stsp struct got_pathlist_entry *pe;
4508 70e3e7f5 2019-12-13 tracey int ch, delete_local_mods = 0, can_recurse = 0, keep_on_disk = 0;
4510 17ed4618 2019-06-02 stsp TAILQ_INIT(&paths);
4512 70e3e7f5 2019-12-13 tracey while ((ch = getopt(argc, argv, "fkR")) != -1) {
4513 2ec1f75b 2019-03-26 stsp switch (ch) {
4515 2ec1f75b 2019-03-26 stsp delete_local_mods = 1;
4517 70e3e7f5 2019-12-13 tracey case 'k':
4518 70e3e7f5 2019-12-13 tracey keep_on_disk = 1;
4520 f2a9dc41 2019-12-13 tracey case 'R':
4521 f2a9dc41 2019-12-13 tracey can_recurse = 1;
4524 f2a9dc41 2019-12-13 tracey usage_remove();
4525 2ec1f75b 2019-03-26 stsp /* NOTREACHED */
4529 2ec1f75b 2019-03-26 stsp argc -= optind;
4530 2ec1f75b 2019-03-26 stsp argv += optind;
4532 43012d58 2019-07-14 stsp #ifndef PROFILE
4533 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4534 43012d58 2019-07-14 stsp NULL) == -1)
4535 43012d58 2019-07-14 stsp err(1, "pledge");
4537 17ed4618 2019-06-02 stsp if (argc < 1)
4538 648e4ef7 2019-07-09 stsp usage_remove();
4540 2ec1f75b 2019-03-26 stsp cwd = getcwd(NULL, 0);
4541 2ec1f75b 2019-03-26 stsp if (cwd == NULL) {
4542 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
4543 2ec1f75b 2019-03-26 stsp goto done;
4545 2ec1f75b 2019-03-26 stsp error = got_worktree_open(&worktree, cwd);
4546 2ec1f75b 2019-03-26 stsp if (error)
4547 2ec1f75b 2019-03-26 stsp goto done;
4549 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4552 2ec1f75b 2019-03-26 stsp goto done;
4554 c2253644 2019-03-26 stsp error = apply_unveil(got_repo_get_path(repo), 1,
4555 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
4556 2ec1f75b 2019-03-26 stsp if (error)
4557 2ec1f75b 2019-03-26 stsp goto done;
4559 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4560 6d022e97 2019-08-04 stsp if (error)
4561 6d022e97 2019-08-04 stsp goto done;
4563 f2a9dc41 2019-12-13 tracey if (!can_recurse) {
4564 f2a9dc41 2019-12-13 tracey char *ondisk_path;
4565 f2a9dc41 2019-12-13 tracey struct stat sb;
4566 f2a9dc41 2019-12-13 tracey TAILQ_FOREACH(pe, &paths, entry) {
4567 f2a9dc41 2019-12-13 tracey if (asprintf(&ondisk_path, "%s/%s",
4568 f2a9dc41 2019-12-13 tracey got_worktree_get_root_path(worktree),
4569 f2a9dc41 2019-12-13 tracey pe->path) == -1) {
4570 f2a9dc41 2019-12-13 tracey error = got_error_from_errno("asprintf");
4571 f2a9dc41 2019-12-13 tracey goto done;
4573 f2a9dc41 2019-12-13 tracey if (lstat(ondisk_path, &sb) == -1) {
4574 f2a9dc41 2019-12-13 tracey if (errno == ENOENT) {
4575 f2a9dc41 2019-12-13 tracey free(ondisk_path);
4576 f2a9dc41 2019-12-13 tracey continue;
4578 f2a9dc41 2019-12-13 tracey error = got_error_from_errno2("lstat",
4579 f2a9dc41 2019-12-13 tracey ondisk_path);
4580 f2a9dc41 2019-12-13 tracey free(ondisk_path);
4581 f2a9dc41 2019-12-13 tracey goto done;
4583 f2a9dc41 2019-12-13 tracey free(ondisk_path);
4584 f2a9dc41 2019-12-13 tracey if (S_ISDIR(sb.st_mode)) {
4585 f2a9dc41 2019-12-13 tracey error = got_error_msg(GOT_ERR_BAD_PATH,
4586 f2a9dc41 2019-12-13 tracey "removing directories requires -R option");
4587 f2a9dc41 2019-12-13 tracey goto done;
4592 17ed4618 2019-06-02 stsp error = got_worktree_schedule_delete(worktree, &paths,
4593 70e3e7f5 2019-12-13 tracey delete_local_mods, print_remove_status, NULL, repo, keep_on_disk);
4596 a129376b 2019-03-28 stsp got_repo_close(repo);
4597 a129376b 2019-03-28 stsp if (worktree)
4598 a129376b 2019-03-28 stsp got_worktree_close(worktree);
4599 17ed4618 2019-06-02 stsp TAILQ_FOREACH(pe, &paths, entry)
4600 17ed4618 2019-06-02 stsp free((char *)pe->path);
4601 17ed4618 2019-06-02 stsp got_pathlist_free(&paths);
4602 a129376b 2019-03-28 stsp free(cwd);
4603 a129376b 2019-03-28 stsp return error;
4606 a129376b 2019-03-28 stsp __dead static void
4607 a129376b 2019-03-28 stsp usage_revert(void)
4609 33aa809d 2019-08-08 stsp fprintf(stderr, "usage: %s revert [-p] [-F response-script] [-R] "
4610 33aa809d 2019-08-08 stsp "path ...\n", getprogname());
4614 1ee397ad 2019-07-12 stsp static const struct got_error *
4615 a129376b 2019-03-28 stsp revert_progress(void *arg, unsigned char status, const char *path)
4617 fb9704af 2020-01-27 stsp if (status == GOT_STATUS_UNVERSIONED)
4618 fb9704af 2020-01-27 stsp return NULL;
4620 a129376b 2019-03-28 stsp while (path[0] == '/')
4622 a129376b 2019-03-28 stsp printf("%c %s\n", status, path);
4623 33aa809d 2019-08-08 stsp return NULL;
4626 33aa809d 2019-08-08 stsp struct choose_patch_arg {
4627 33aa809d 2019-08-08 stsp FILE *patch_script_file;
4628 33aa809d 2019-08-08 stsp const char *action;
4631 33aa809d 2019-08-08 stsp static const struct got_error *
4632 33aa809d 2019-08-08 stsp show_change(unsigned char status, const char *path, FILE *patch_file, int n,
4633 33aa809d 2019-08-08 stsp int nchanges, const char *action)
4635 33aa809d 2019-08-08 stsp char *line = NULL;
4636 33aa809d 2019-08-08 stsp size_t linesize = 0;
4637 33aa809d 2019-08-08 stsp ssize_t linelen;
4639 33aa809d 2019-08-08 stsp switch (status) {
4640 33aa809d 2019-08-08 stsp case GOT_STATUS_ADD:
4641 33aa809d 2019-08-08 stsp printf("A %s\n%s this addition? [y/n] ", path, action);
4643 33aa809d 2019-08-08 stsp case GOT_STATUS_DELETE:
4644 33aa809d 2019-08-08 stsp printf("D %s\n%s this deletion? [y/n] ", path, action);
4646 33aa809d 2019-08-08 stsp case GOT_STATUS_MODIFY:
4647 33aa809d 2019-08-08 stsp if (fseek(patch_file, 0L, SEEK_SET) == -1)
4648 33aa809d 2019-08-08 stsp return got_error_from_errno("fseek");
4649 33aa809d 2019-08-08 stsp printf(GOT_COMMIT_SEP_STR);
4650 9516e7cb 2019-08-11 stsp while ((linelen = getline(&line, &linesize, patch_file)) != -1)
4651 33aa809d 2019-08-08 stsp printf("%s", line);
4652 33aa809d 2019-08-08 stsp if (ferror(patch_file))
4653 33aa809d 2019-08-08 stsp return got_error_from_errno("getline");
4654 33aa809d 2019-08-08 stsp printf(GOT_COMMIT_SEP_STR);
4655 33aa809d 2019-08-08 stsp printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
4656 33aa809d 2019-08-08 stsp path, n, nchanges, action);
4659 33aa809d 2019-08-08 stsp return got_error_path(path, GOT_ERR_FILE_STATUS);
4662 33aa809d 2019-08-08 stsp return NULL;
4665 33aa809d 2019-08-08 stsp static const struct got_error *
4666 33aa809d 2019-08-08 stsp choose_patch(int *choice, void *arg, unsigned char status, const char *path,
4667 33aa809d 2019-08-08 stsp FILE *patch_file, int n, int nchanges)
4669 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
4670 33aa809d 2019-08-08 stsp char *line = NULL;
4671 33aa809d 2019-08-08 stsp size_t linesize = 0;
4672 33aa809d 2019-08-08 stsp ssize_t linelen;
4673 33aa809d 2019-08-08 stsp int resp = ' ';
4674 33aa809d 2019-08-08 stsp struct choose_patch_arg *a = arg;
4676 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NONE;
4678 33aa809d 2019-08-08 stsp if (a->patch_script_file) {
4680 33aa809d 2019-08-08 stsp err = show_change(status, path, patch_file, n, nchanges,
4681 33aa809d 2019-08-08 stsp a->action);
4683 33aa809d 2019-08-08 stsp return err;
4684 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, a->patch_script_file);
4685 33aa809d 2019-08-08 stsp if (linelen == -1) {
4686 33aa809d 2019-08-08 stsp if (ferror(a->patch_script_file))
4687 33aa809d 2019-08-08 stsp return got_error_from_errno("getline");
4688 33aa809d 2019-08-08 stsp return NULL;
4690 33aa809d 2019-08-08 stsp nl = strchr(line, '\n');
4692 33aa809d 2019-08-08 stsp *nl = '\0';
4693 33aa809d 2019-08-08 stsp if (strcmp(line, "y") == 0) {
4694 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_YES;
4695 33aa809d 2019-08-08 stsp printf("y\n");
4696 33aa809d 2019-08-08 stsp } else if (strcmp(line, "n") == 0) {
4697 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NO;
4698 33aa809d 2019-08-08 stsp printf("n\n");
4699 33aa809d 2019-08-08 stsp } else if (strcmp(line, "q") == 0 &&
4700 33aa809d 2019-08-08 stsp status == GOT_STATUS_MODIFY) {
4701 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_QUIT;
4702 33aa809d 2019-08-08 stsp printf("q\n");
4704 33aa809d 2019-08-08 stsp printf("invalid response '%s'\n", line);
4705 33aa809d 2019-08-08 stsp free(line);
4706 33aa809d 2019-08-08 stsp return NULL;
4709 33aa809d 2019-08-08 stsp while (resp != 'y' && resp != 'n' && resp != 'q') {
4710 33aa809d 2019-08-08 stsp err = show_change(status, path, patch_file, n, nchanges,
4711 33aa809d 2019-08-08 stsp a->action);
4713 33aa809d 2019-08-08 stsp return err;
4714 33aa809d 2019-08-08 stsp resp = getchar();
4715 33aa809d 2019-08-08 stsp if (resp == '\n')
4716 33aa809d 2019-08-08 stsp resp = getchar();
4717 33aa809d 2019-08-08 stsp if (status == GOT_STATUS_MODIFY) {
4718 33aa809d 2019-08-08 stsp if (resp != 'y' && resp != 'n' && resp != 'q') {
4719 33aa809d 2019-08-08 stsp printf("invalid response '%c'\n", resp);
4720 33aa809d 2019-08-08 stsp resp = ' ';
4722 33aa809d 2019-08-08 stsp } else if (resp != 'y' && resp != 'n') {
4723 33aa809d 2019-08-08 stsp printf("invalid response '%c'\n", resp);
4724 33aa809d 2019-08-08 stsp resp = ' ';
4728 33aa809d 2019-08-08 stsp if (resp == 'y')
4729 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_YES;
4730 33aa809d 2019-08-08 stsp else if (resp == 'n')
4731 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NO;
4732 33aa809d 2019-08-08 stsp else if (resp == 'q' && status == GOT_STATUS_MODIFY)
4733 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_QUIT;
4735 1ee397ad 2019-07-12 stsp return NULL;
4739 a129376b 2019-03-28 stsp static const struct got_error *
4740 a129376b 2019-03-28 stsp cmd_revert(int argc, char *argv[])
4742 a129376b 2019-03-28 stsp const struct got_error *error = NULL;
4743 a129376b 2019-03-28 stsp struct got_worktree *worktree = NULL;
4744 a129376b 2019-03-28 stsp struct got_repository *repo = NULL;
4745 a129376b 2019-03-28 stsp char *cwd = NULL, *path = NULL;
4746 e20a8b6f 2019-06-04 stsp struct got_pathlist_head paths;
4747 0f6d7415 2019-08-08 stsp struct got_pathlist_entry *pe;
4748 33aa809d 2019-08-08 stsp int ch, can_recurse = 0, pflag = 0;
4749 33aa809d 2019-08-08 stsp FILE *patch_script_file = NULL;
4750 33aa809d 2019-08-08 stsp const char *patch_script_path = NULL;
4751 33aa809d 2019-08-08 stsp struct choose_patch_arg cpa;
4753 e20a8b6f 2019-06-04 stsp TAILQ_INIT(&paths);
4755 33aa809d 2019-08-08 stsp while ((ch = getopt(argc, argv, "pF:R")) != -1) {
4756 a129376b 2019-03-28 stsp switch (ch) {
4758 33aa809d 2019-08-08 stsp pflag = 1;
4761 33aa809d 2019-08-08 stsp patch_script_path = optarg;
4764 0f6d7415 2019-08-08 stsp can_recurse = 1;
4767 a129376b 2019-03-28 stsp usage_revert();
4768 a129376b 2019-03-28 stsp /* NOTREACHED */
4772 a129376b 2019-03-28 stsp argc -= optind;
4773 a129376b 2019-03-28 stsp argv += optind;
4775 43012d58 2019-07-14 stsp #ifndef PROFILE
4776 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4777 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
4778 43012d58 2019-07-14 stsp err(1, "pledge");
4780 e20a8b6f 2019-06-04 stsp if (argc < 1)
4781 a129376b 2019-03-28 stsp usage_revert();
4782 33aa809d 2019-08-08 stsp if (patch_script_path && !pflag)
4783 33aa809d 2019-08-08 stsp errx(1, "-F option can only be used together with -p option");
4785 a129376b 2019-03-28 stsp cwd = getcwd(NULL, 0);
4786 a129376b 2019-03-28 stsp if (cwd == NULL) {
4787 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
4788 a129376b 2019-03-28 stsp goto done;
4790 a129376b 2019-03-28 stsp error = got_worktree_open(&worktree, cwd);
4791 2ec1f75b 2019-03-26 stsp if (error)
4792 2ec1f75b 2019-03-26 stsp goto done;
4794 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4796 a129376b 2019-03-28 stsp if (error != NULL)
4797 a129376b 2019-03-28 stsp goto done;
4799 33aa809d 2019-08-08 stsp if (patch_script_path) {
4800 33aa809d 2019-08-08 stsp patch_script_file = fopen(patch_script_path, "r");
4801 33aa809d 2019-08-08 stsp if (patch_script_file == NULL) {
4802 33aa809d 2019-08-08 stsp error = got_error_from_errno2("fopen",
4803 33aa809d 2019-08-08 stsp patch_script_path);
4804 33aa809d 2019-08-08 stsp goto done;
4807 a129376b 2019-03-28 stsp error = apply_unveil(got_repo_get_path(repo), 1,
4808 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
4809 a129376b 2019-03-28 stsp if (error)
4810 a129376b 2019-03-28 stsp goto done;
4812 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4813 6d022e97 2019-08-04 stsp if (error)
4814 6d022e97 2019-08-04 stsp goto done;
4816 0f6d7415 2019-08-08 stsp if (!can_recurse) {
4817 0f6d7415 2019-08-08 stsp char *ondisk_path;
4818 0f6d7415 2019-08-08 stsp struct stat sb;
4819 0f6d7415 2019-08-08 stsp TAILQ_FOREACH(pe, &paths, entry) {
4820 0f6d7415 2019-08-08 stsp if (asprintf(&ondisk_path, "%s/%s",
4821 0f6d7415 2019-08-08 stsp got_worktree_get_root_path(worktree),
4822 0f6d7415 2019-08-08 stsp pe->path) == -1) {
4823 0f6d7415 2019-08-08 stsp error = got_error_from_errno("asprintf");
4824 0f6d7415 2019-08-08 stsp goto done;
4826 0f6d7415 2019-08-08 stsp if (lstat(ondisk_path, &sb) == -1) {
4827 0f6d7415 2019-08-08 stsp if (errno == ENOENT) {
4828 0f6d7415 2019-08-08 stsp free(ondisk_path);
4831 0f6d7415 2019-08-08 stsp error = got_error_from_errno2("lstat",
4832 0f6d7415 2019-08-08 stsp ondisk_path);
4833 0f6d7415 2019-08-08 stsp free(ondisk_path);
4834 0f6d7415 2019-08-08 stsp goto done;
4836 0f6d7415 2019-08-08 stsp free(ondisk_path);
4837 0f6d7415 2019-08-08 stsp if (S_ISDIR(sb.st_mode)) {
4838 0f6d7415 2019-08-08 stsp error = got_error_msg(GOT_ERR_BAD_PATH,
4839 0f6d7415 2019-08-08 stsp "reverting directories requires -R option");
4840 0f6d7415 2019-08-08 stsp goto done;
4845 33aa809d 2019-08-08 stsp cpa.patch_script_file = patch_script_file;
4846 33aa809d 2019-08-08 stsp cpa.action = "revert";
4847 33aa809d 2019-08-08 stsp error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
4848 33aa809d 2019-08-08 stsp pflag ? choose_patch : NULL, &cpa, repo);
4850 33aa809d 2019-08-08 stsp if (patch_script_file && fclose(patch_script_file) == EOF &&
4851 33aa809d 2019-08-08 stsp error == NULL)
4852 33aa809d 2019-08-08 stsp error = got_error_from_errno2("fclose", patch_script_path);
4854 2ec1f75b 2019-03-26 stsp got_repo_close(repo);
4855 2ec1f75b 2019-03-26 stsp if (worktree)
4856 2ec1f75b 2019-03-26 stsp got_worktree_close(worktree);
4857 2ec1f75b 2019-03-26 stsp free(path);
4858 d00136be 2019-03-26 stsp free(cwd);
4859 6bad629b 2019-02-04 stsp return error;
4862 c4296144 2019-05-09 stsp __dead static void
4863 c4296144 2019-05-09 stsp usage_commit(void)
4865 5c1e53bc 2019-07-28 stsp fprintf(stderr, "usage: %s commit [-m msg] [path ...]\n",
4866 5c1e53bc 2019-07-28 stsp getprogname());
4870 e2ba3d07 2019-05-13 stsp struct collect_commit_logmsg_arg {
4871 e2ba3d07 2019-05-13 stsp const char *cmdline_log;
4872 e2ba3d07 2019-05-13 stsp const char *editor;
4873 e0870e44 2019-05-13 stsp const char *worktree_path;
4874 76d98825 2019-06-03 stsp const char *branch_name;
4875 314a6357 2019-05-13 stsp const char *repo_path;
4876 e0870e44 2019-05-13 stsp char *logmsg_path;
4880 33ad4cbe 2019-05-12 jcs static const struct got_error *
4881 33ad4cbe 2019-05-12 jcs collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
4884 76d98825 2019-06-03 stsp char *initial_content = NULL;
4885 33ad4cbe 2019-05-12 jcs struct got_pathlist_entry *pe;
4886 33ad4cbe 2019-05-12 jcs const struct got_error *err = NULL;
4887 e0870e44 2019-05-13 stsp char *template = NULL;
4888 e2ba3d07 2019-05-13 stsp struct collect_commit_logmsg_arg *a = arg;
4890 33ad4cbe 2019-05-12 jcs size_t len;
4892 33ad4cbe 2019-05-12 jcs /* if a message was specified on the command line, just use it */
4893 e2ba3d07 2019-05-13 stsp if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
4894 e2ba3d07 2019-05-13 stsp len = strlen(a->cmdline_log) + 1;
4895 33ad4cbe 2019-05-12 jcs *logmsg = malloc(len + 1);
4896 9f42ff69 2019-05-13 stsp if (*logmsg == NULL)
4897 638f9024 2019-05-13 stsp return got_error_from_errno("malloc");
4898 e2ba3d07 2019-05-13 stsp strlcpy(*logmsg, a->cmdline_log, len);
4899 33ad4cbe 2019-05-12 jcs return NULL;
4902 e0870e44 2019-05-13 stsp if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
4903 76d98825 2019-06-03 stsp return got_error_from_errno("asprintf");
4905 76d98825 2019-06-03 stsp if (asprintf(&initial_content,
4906 76d98825 2019-06-03 stsp "\n# changes to be committed on branch %s:\n",
4907 76d98825 2019-06-03 stsp a->branch_name) == -1)
4908 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
4910 e0870e44 2019-05-13 stsp err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
4912 e0870e44 2019-05-13 stsp goto done;
4914 e0870e44 2019-05-13 stsp dprintf(fd, initial_content);
4916 33ad4cbe 2019-05-12 jcs TAILQ_FOREACH(pe, commitable_paths, entry) {
4917 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
4918 8656d6c4 2019-05-20 stsp dprintf(fd, "# %c %s\n",
4919 8656d6c4 2019-05-20 stsp got_commitable_get_status(ct),
4920 8656d6c4 2019-05-20 stsp got_commitable_get_path(ct));
4924 3ce1b845 2019-07-15 stsp err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content);
4926 76d98825 2019-06-03 stsp free(initial_content);
4927 e0870e44 2019-05-13 stsp free(template);
4929 314a6357 2019-05-13 stsp /* Editor is done; we can now apply unveil(2) */
4930 3ce1b845 2019-07-15 stsp if (err == NULL) {
4931 c530dc23 2019-07-23 stsp err = apply_unveil(a->repo_path, 0, a->worktree_path);
4932 3ce1b845 2019-07-15 stsp if (err) {
4933 3ce1b845 2019-07-15 stsp free(*logmsg);
4934 3ce1b845 2019-07-15 stsp *logmsg = NULL;
4937 33ad4cbe 2019-05-12 jcs return err;
4940 c4296144 2019-05-09 stsp static const struct got_error *
4941 c4296144 2019-05-09 stsp cmd_commit(int argc, char *argv[])
4943 c4296144 2019-05-09 stsp const struct got_error *error = NULL;
4944 c4296144 2019-05-09 stsp struct got_worktree *worktree = NULL;
4945 c4296144 2019-05-09 stsp struct got_repository *repo = NULL;
4946 5c1e53bc 2019-07-28 stsp char *cwd = NULL, *id_str = NULL;
4947 c4296144 2019-05-09 stsp struct got_object_id *id = NULL;
4948 33ad4cbe 2019-05-12 jcs const char *logmsg = NULL;
4949 aba9c984 2019-09-08 stsp struct collect_commit_logmsg_arg cl_arg;
4950 c9956ddf 2019-09-08 stsp char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
4951 7266f21f 2019-10-21 stsp int ch, rebase_in_progress, histedit_in_progress, preserve_logmsg = 0;
4952 5c1e53bc 2019-07-28 stsp struct got_pathlist_head paths;
4954 5c1e53bc 2019-07-28 stsp TAILQ_INIT(&paths);
4955 6ac5a73c 2019-08-08 stsp cl_arg.logmsg_path = NULL;
4957 c4296144 2019-05-09 stsp while ((ch = getopt(argc, argv, "m:")) != -1) {
4958 c4296144 2019-05-09 stsp switch (ch) {
4960 c4296144 2019-05-09 stsp logmsg = optarg;
4963 c4296144 2019-05-09 stsp usage_commit();
4964 c4296144 2019-05-09 stsp /* NOTREACHED */
4968 c4296144 2019-05-09 stsp argc -= optind;
4969 c4296144 2019-05-09 stsp argv += optind;
4971 43012d58 2019-07-14 stsp #ifndef PROFILE
4972 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4973 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
4974 43012d58 2019-07-14 stsp err(1, "pledge");
4976 c4296144 2019-05-09 stsp cwd = getcwd(NULL, 0);
4977 c4296144 2019-05-09 stsp if (cwd == NULL) {
4978 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
4979 c4296144 2019-05-09 stsp goto done;
4981 c4296144 2019-05-09 stsp error = got_worktree_open(&worktree, cwd);
4982 7d5807f4 2019-07-11 stsp if (error)
4983 7d5807f4 2019-07-11 stsp goto done;
4985 a698f62e 2019-07-25 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
4986 c4296144 2019-05-09 stsp if (error)
4987 a698f62e 2019-07-25 stsp goto done;
4988 a698f62e 2019-07-25 stsp if (rebase_in_progress) {
4989 a698f62e 2019-07-25 stsp error = got_error(GOT_ERR_REBASING);
4990 c4296144 2019-05-09 stsp goto done;
4993 916f288c 2019-07-30 stsp error = got_worktree_histedit_in_progress(&histedit_in_progress,
4994 916f288c 2019-07-30 stsp worktree);
4995 5c1e53bc 2019-07-28 stsp if (error)
4996 5c1e53bc 2019-07-28 stsp goto done;
4998 c9956ddf 2019-09-08 stsp error = get_gitconfig_path(&gitconfig_path);
4999 c9956ddf 2019-09-08 stsp if (error)
5000 c9956ddf 2019-09-08 stsp goto done;
5001 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5002 c9956ddf 2019-09-08 stsp gitconfig_path);
5003 c4296144 2019-05-09 stsp if (error != NULL)
5004 c4296144 2019-05-09 stsp goto done;
5006 aba9c984 2019-09-08 stsp error = get_author(&author, repo);
5007 aba9c984 2019-09-08 stsp if (error)
5008 aba9c984 2019-09-08 stsp return error;
5011 314a6357 2019-05-13 stsp * unveil(2) traverses exec(2); if an editor is used we have
5012 314a6357 2019-05-13 stsp * to apply unveil after the log message has been written.
5014 314a6357 2019-05-13 stsp if (logmsg == NULL || strlen(logmsg) == 0)
5015 314a6357 2019-05-13 stsp error = get_editor(&editor);
5017 314a6357 2019-05-13 stsp error = apply_unveil(got_repo_get_path(repo), 0,
5018 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
5019 c4296144 2019-05-09 stsp if (error)
5020 c4296144 2019-05-09 stsp goto done;
5022 087fb88c 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5023 087fb88c 2019-08-04 stsp if (error)
5024 087fb88c 2019-08-04 stsp goto done;
5026 e2ba3d07 2019-05-13 stsp cl_arg.editor = editor;
5027 e2ba3d07 2019-05-13 stsp cl_arg.cmdline_log = logmsg;
5028 e0870e44 2019-05-13 stsp cl_arg.worktree_path = got_worktree_get_root_path(worktree);
5029 76d98825 2019-06-03 stsp cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
5030 916f288c 2019-07-30 stsp if (!histedit_in_progress) {
5031 916f288c 2019-07-30 stsp if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
5032 916f288c 2019-07-30 stsp error = got_error(GOT_ERR_COMMIT_BRANCH);
5033 916f288c 2019-07-30 stsp goto done;
5035 916f288c 2019-07-30 stsp cl_arg.branch_name += 11;
5037 314a6357 2019-05-13 stsp cl_arg.repo_path = got_repo_get_path(repo);
5038 84792843 2019-08-09 stsp error = got_worktree_commit(&id, worktree, &paths, author, NULL,
5039 e2ba3d07 2019-05-13 stsp collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
5040 e0870e44 2019-05-13 stsp if (error) {
5041 7266f21f 2019-10-21 stsp if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
5042 7266f21f 2019-10-21 stsp cl_arg.logmsg_path != NULL)
5043 7266f21f 2019-10-21 stsp preserve_logmsg = 1;
5044 c4296144 2019-05-09 stsp goto done;
5047 c4296144 2019-05-09 stsp error = got_object_id_str(&id_str, id);
5048 c4296144 2019-05-09 stsp if (error)
5049 c4296144 2019-05-09 stsp goto done;
5050 a7648d7a 2019-06-02 stsp printf("Created commit %s\n", id_str);
5052 7266f21f 2019-10-21 stsp if (preserve_logmsg) {
5053 7266f21f 2019-10-21 stsp fprintf(stderr, "%s: log message preserved in %s\n",
5054 7266f21f 2019-10-21 stsp getprogname(), cl_arg.logmsg_path);
5055 7266f21f 2019-10-21 stsp } else if (cl_arg.logmsg_path && unlink(cl_arg.logmsg_path) == -1 &&
5056 7266f21f 2019-10-21 stsp error == NULL)
5057 7266f21f 2019-10-21 stsp error = got_error_from_errno2("unlink", cl_arg.logmsg_path);
5058 6ac5a73c 2019-08-08 stsp free(cl_arg.logmsg_path);
5060 c4296144 2019-05-09 stsp got_repo_close(repo);
5061 c4296144 2019-05-09 stsp if (worktree)
5062 c4296144 2019-05-09 stsp got_worktree_close(worktree);
5063 c4296144 2019-05-09 stsp free(cwd);
5064 c4296144 2019-05-09 stsp free(id_str);
5065 c9956ddf 2019-09-08 stsp free(gitconfig_path);
5066 e2ba3d07 2019-05-13 stsp free(editor);
5067 aba9c984 2019-09-08 stsp free(author);
5068 234035bc 2019-06-01 stsp return error;
5071 234035bc 2019-06-01 stsp __dead static void
5072 234035bc 2019-06-01 stsp usage_cherrypick(void)
5074 234035bc 2019-06-01 stsp fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
5078 234035bc 2019-06-01 stsp static const struct got_error *
5079 234035bc 2019-06-01 stsp cmd_cherrypick(int argc, char *argv[])
5081 234035bc 2019-06-01 stsp const struct got_error *error = NULL;
5082 234035bc 2019-06-01 stsp struct got_worktree *worktree = NULL;
5083 234035bc 2019-06-01 stsp struct got_repository *repo = NULL;
5084 234035bc 2019-06-01 stsp char *cwd = NULL, *commit_id_str = NULL;
5085 234035bc 2019-06-01 stsp struct got_object_id *commit_id = NULL;
5086 234035bc 2019-06-01 stsp struct got_commit_object *commit = NULL;
5087 234035bc 2019-06-01 stsp struct got_object_qid *pid;
5088 234035bc 2019-06-01 stsp struct got_reference *head_ref = NULL;
5089 234035bc 2019-06-01 stsp int ch, did_something = 0;
5091 234035bc 2019-06-01 stsp while ((ch = getopt(argc, argv, "")) != -1) {
5092 234035bc 2019-06-01 stsp switch (ch) {
5094 234035bc 2019-06-01 stsp usage_cherrypick();
5095 234035bc 2019-06-01 stsp /* NOTREACHED */
5099 234035bc 2019-06-01 stsp argc -= optind;
5100 234035bc 2019-06-01 stsp argv += optind;
5102 43012d58 2019-07-14 stsp #ifndef PROFILE
5103 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5104 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
5105 43012d58 2019-07-14 stsp err(1, "pledge");
5107 234035bc 2019-06-01 stsp if (argc != 1)
5108 234035bc 2019-06-01 stsp usage_cherrypick();
5110 234035bc 2019-06-01 stsp cwd = getcwd(NULL, 0);
5111 234035bc 2019-06-01 stsp if (cwd == NULL) {
5112 234035bc 2019-06-01 stsp error = got_error_from_errno("getcwd");
5113 234035bc 2019-06-01 stsp goto done;
5115 234035bc 2019-06-01 stsp error = got_worktree_open(&worktree, cwd);
5116 234035bc 2019-06-01 stsp if (error)
5117 234035bc 2019-06-01 stsp goto done;
5119 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5121 234035bc 2019-06-01 stsp if (error != NULL)
5122 234035bc 2019-06-01 stsp goto done;
5124 234035bc 2019-06-01 stsp error = apply_unveil(got_repo_get_path(repo), 0,
5125 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
5126 234035bc 2019-06-01 stsp if (error)
5127 234035bc 2019-06-01 stsp goto done;
5129 dd88155e 2019-06-29 stsp error = got_repo_match_object_id_prefix(&commit_id, argv[0],
5130 dd88155e 2019-06-29 stsp GOT_OBJ_TYPE_COMMIT, repo);
5131 234035bc 2019-06-01 stsp if (error != NULL) {
5132 234035bc 2019-06-01 stsp struct got_reference *ref;
5133 234035bc 2019-06-01 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
5134 234035bc 2019-06-01 stsp goto done;
5135 234035bc 2019-06-01 stsp error = got_ref_open(&ref, repo, argv[0], 0);
5136 234035bc 2019-06-01 stsp if (error != NULL)
5137 234035bc 2019-06-01 stsp goto done;
5138 234035bc 2019-06-01 stsp error = got_ref_resolve(&commit_id, repo, ref);
5139 234035bc 2019-06-01 stsp got_ref_close(ref);
5140 234035bc 2019-06-01 stsp if (error != NULL)
5141 234035bc 2019-06-01 stsp goto done;
5143 234035bc 2019-06-01 stsp error = got_object_id_str(&commit_id_str, commit_id);
5144 234035bc 2019-06-01 stsp if (error)
5145 234035bc 2019-06-01 stsp goto done;
5147 234035bc 2019-06-01 stsp error = got_ref_open(&head_ref, repo,
5148 234035bc 2019-06-01 stsp got_worktree_get_head_ref_name(worktree), 0);
5149 234035bc 2019-06-01 stsp if (error != NULL)
5150 234035bc 2019-06-01 stsp goto done;
5152 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
5153 234035bc 2019-06-01 stsp if (error) {
5154 234035bc 2019-06-01 stsp if (error->code != GOT_ERR_ANCESTRY)
5155 234035bc 2019-06-01 stsp goto done;
5156 234035bc 2019-06-01 stsp error = NULL;
5158 234035bc 2019-06-01 stsp error = got_error(GOT_ERR_SAME_BRANCH);
5159 234035bc 2019-06-01 stsp goto done;
5162 234035bc 2019-06-01 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
5163 234035bc 2019-06-01 stsp if (error)
5164 234035bc 2019-06-01 stsp goto done;
5165 234035bc 2019-06-01 stsp pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
5166 03415a1a 2019-06-02 stsp error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
5167 03415a1a 2019-06-02 stsp commit_id, repo, update_progress, &did_something, check_cancelled,
5169 234035bc 2019-06-01 stsp if (error != NULL)
5170 234035bc 2019-06-01 stsp goto done;
5172 234035bc 2019-06-01 stsp if (did_something)
5173 a7648d7a 2019-06-02 stsp printf("Merged commit %s\n", commit_id_str);
5175 234035bc 2019-06-01 stsp if (commit)
5176 234035bc 2019-06-01 stsp got_object_commit_close(commit);
5177 234035bc 2019-06-01 stsp free(commit_id_str);
5178 234035bc 2019-06-01 stsp if (head_ref)
5179 234035bc 2019-06-01 stsp got_ref_close(head_ref);
5180 234035bc 2019-06-01 stsp if (worktree)
5181 234035bc 2019-06-01 stsp got_worktree_close(worktree);
5183 234035bc 2019-06-01 stsp got_repo_close(repo);
5184 c4296144 2019-05-09 stsp return error;
5187 5ef14e63 2019-06-02 stsp __dead static void
5188 5ef14e63 2019-06-02 stsp usage_backout(void)
5190 5ef14e63 2019-06-02 stsp fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
5194 5ef14e63 2019-06-02 stsp static const struct got_error *
5195 5ef14e63 2019-06-02 stsp cmd_backout(int argc, char *argv[])
5197 5ef14e63 2019-06-02 stsp const struct got_error *error = NULL;
5198 5ef14e63 2019-06-02 stsp struct got_worktree *worktree = NULL;
5199 5ef14e63 2019-06-02 stsp struct got_repository *repo = NULL;
5200 5ef14e63 2019-06-02 stsp char *cwd = NULL, *commit_id_str = NULL;
5201 5ef14e63 2019-06-02 stsp struct got_object_id *commit_id = NULL;
5202 5ef14e63 2019-06-02 stsp struct got_commit_object *commit = NULL;
5203 5ef14e63 2019-06-02 stsp struct got_object_qid *pid;
5204 5ef14e63 2019-06-02 stsp struct got_reference *head_ref = NULL;
5205 5ef14e63 2019-06-02 stsp int ch, did_something = 0;
5207 5ef14e63 2019-06-02 stsp while ((ch = getopt(argc, argv, "")) != -1) {
5208 5ef14e63 2019-06-02 stsp switch (ch) {
5210 5ef14e63 2019-06-02 stsp usage_backout();
5211 5ef14e63 2019-06-02 stsp /* NOTREACHED */
5215 5ef14e63 2019-06-02 stsp argc -= optind;
5216 5ef14e63 2019-06-02 stsp argv += optind;
5218 43012d58 2019-07-14 stsp #ifndef PROFILE
5219 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5220 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
5221 43012d58 2019-07-14 stsp err(1, "pledge");
5223 5ef14e63 2019-06-02 stsp if (argc != 1)
5224 5ef14e63 2019-06-02 stsp usage_backout();
5226 5ef14e63 2019-06-02 stsp cwd = getcwd(NULL, 0);
5227 5ef14e63 2019-06-02 stsp if (cwd == NULL) {
5228 5ef14e63 2019-06-02 stsp error = got_error_from_errno("getcwd");
5229 5ef14e63 2019-06-02 stsp goto done;
5231 5ef14e63 2019-06-02 stsp error = got_worktree_open(&worktree, cwd);
5232 5ef14e63 2019-06-02 stsp if (error)
5233 5ef14e63 2019-06-02 stsp goto done;
5235 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5237 5ef14e63 2019-06-02 stsp if (error != NULL)
5238 5ef14e63 2019-06-02 stsp goto done;
5240 5ef14e63 2019-06-02 stsp error = apply_unveil(got_repo_get_path(repo), 0,
5241 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
5242 5ef14e63 2019-06-02 stsp if (error)
5243 5ef14e63 2019-06-02 stsp goto done;
5245 dd88155e 2019-06-29 stsp error = got_repo_match_object_id_prefix(&commit_id, argv[0],
5246 dd88155e 2019-06-29 stsp GOT_OBJ_TYPE_COMMIT, repo);
5247 5ef14e63 2019-06-02 stsp if (error != NULL) {
5248 5ef14e63 2019-06-02 stsp struct got_reference *ref;
5249 5ef14e63 2019-06-02 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
5250 5ef14e63 2019-06-02 stsp goto done;
5251 5ef14e63 2019-06-02 stsp error = got_ref_open(&ref, repo, argv[0], 0);
5252 5ef14e63 2019-06-02 stsp if (error != NULL)
5253 5ef14e63 2019-06-02 stsp goto done;
5254 5ef14e63 2019-06-02 stsp error = got_ref_resolve(&commit_id, repo, ref);
5255 5ef14e63 2019-06-02 stsp got_ref_close(ref);
5256 5ef14e63 2019-06-02 stsp if (error != NULL)
5257 5ef14e63 2019-06-02 stsp goto done;
5259 5ef14e63 2019-06-02 stsp error = got_object_id_str(&commit_id_str, commit_id);
5260 5ef14e63 2019-06-02 stsp if (error)
5261 5ef14e63 2019-06-02 stsp goto done;
5263 5ef14e63 2019-06-02 stsp error = got_ref_open(&head_ref, repo,
5264 5ef14e63 2019-06-02 stsp got_worktree_get_head_ref_name(worktree), 0);
5265 5ef14e63 2019-06-02 stsp if (error != NULL)
5266 5ef14e63 2019-06-02 stsp goto done;
5268 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
5269 5ef14e63 2019-06-02 stsp if (error)
5270 5ef14e63 2019-06-02 stsp goto done;
5272 5ef14e63 2019-06-02 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
5273 5ef14e63 2019-06-02 stsp if (error)
5274 5ef14e63 2019-06-02 stsp goto done;
5275 5ef14e63 2019-06-02 stsp pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
5276 5ef14e63 2019-06-02 stsp if (pid == NULL) {
5277 5ef14e63 2019-06-02 stsp error = got_error(GOT_ERR_ROOT_COMMIT);
5278 5ef14e63 2019-06-02 stsp goto done;
5281 5ef14e63 2019-06-02 stsp error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
5282 5ef14e63 2019-06-02 stsp update_progress, &did_something, check_cancelled, NULL);
5283 5ef14e63 2019-06-02 stsp if (error != NULL)
5284 5ef14e63 2019-06-02 stsp goto done;
5286 5ef14e63 2019-06-02 stsp if (did_something)
5287 a7648d7a 2019-06-02 stsp printf("Backed out commit %s\n", commit_id_str);
5289 5ef14e63 2019-06-02 stsp if (commit)
5290 5ef14e63 2019-06-02 stsp got_object_commit_close(commit);
5291 5ef14e63 2019-06-02 stsp free(commit_id_str);
5292 5ef14e63 2019-06-02 stsp if (head_ref)
5293 5ef14e63 2019-06-02 stsp got_ref_close(head_ref);
5294 818c7501 2019-07-11 stsp if (worktree)
5295 818c7501 2019-07-11 stsp got_worktree_close(worktree);
5297 818c7501 2019-07-11 stsp got_repo_close(repo);
5298 818c7501 2019-07-11 stsp return error;
5301 818c7501 2019-07-11 stsp __dead static void
5302 818c7501 2019-07-11 stsp usage_rebase(void)
5304 af54c8f8 2019-07-11 stsp fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
5305 af54c8f8 2019-07-11 stsp getprogname());
5310 0ebf8283 2019-07-24 stsp trim_logmsg(char *logmsg, int limit)
5313 0ebf8283 2019-07-24 stsp size_t len;
5315 0ebf8283 2019-07-24 stsp len = strlen(logmsg);
5316 0ebf8283 2019-07-24 stsp if (len > limit)
5317 0ebf8283 2019-07-24 stsp len = limit;
5318 0ebf8283 2019-07-24 stsp logmsg[len] = '\0';
5319 0ebf8283 2019-07-24 stsp nl = strchr(logmsg, '\n');
5321 0ebf8283 2019-07-24 stsp *nl = '\0';
5324 0ebf8283 2019-07-24 stsp static const struct got_error *
5325 0ebf8283 2019-07-24 stsp get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
5327 5943eee2 2019-08-13 stsp const struct got_error *err;
5328 5943eee2 2019-08-13 stsp char *logmsg0 = NULL;
5329 5943eee2 2019-08-13 stsp const char *s;
5331 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
5333 5943eee2 2019-08-13 stsp return err;
5335 5943eee2 2019-08-13 stsp s = logmsg0;
5336 5943eee2 2019-08-13 stsp while (isspace((unsigned char)s[0]))
5339 5943eee2 2019-08-13 stsp *logmsg = strdup(s);
5340 5943eee2 2019-08-13 stsp if (*logmsg == NULL) {
5341 5943eee2 2019-08-13 stsp err = got_error_from_errno("strdup");
5342 5943eee2 2019-08-13 stsp goto done;
5345 0ebf8283 2019-07-24 stsp trim_logmsg(*logmsg, limit);
5347 5943eee2 2019-08-13 stsp free(logmsg0);
5348 a0ea4fc0 2020-02-28 stsp return err;
5351 a0ea4fc0 2020-02-28 stsp static const struct got_error *
5352 a0ea4fc0 2020-02-28 stsp show_rebase_merge_conflict(struct got_object_id *id, struct got_repository *repo)
5354 a0ea4fc0 2020-02-28 stsp const struct got_error *err;
5355 a0ea4fc0 2020-02-28 stsp struct got_commit_object *commit = NULL;
5356 a0ea4fc0 2020-02-28 stsp char *id_str = NULL, *logmsg = NULL;
5358 a0ea4fc0 2020-02-28 stsp err = got_object_open_as_commit(&commit, repo, id);
5360 a0ea4fc0 2020-02-28 stsp return err;
5362 a0ea4fc0 2020-02-28 stsp err = got_object_id_str(&id_str, id);
5364 a0ea4fc0 2020-02-28 stsp goto done;
5366 a0ea4fc0 2020-02-28 stsp id_str[12] = '\0';
5368 a0ea4fc0 2020-02-28 stsp err = get_short_logmsg(&logmsg, 42, commit);
5370 a0ea4fc0 2020-02-28 stsp goto done;
5372 a0ea4fc0 2020-02-28 stsp printf("%s -> merge conflict: %s\n", id_str, logmsg);
5374 a0ea4fc0 2020-02-28 stsp free(id_str);
5375 a0ea4fc0 2020-02-28 stsp got_object_commit_close(commit);
5376 a0ea4fc0 2020-02-28 stsp free(logmsg);
5377 5943eee2 2019-08-13 stsp return err;
5380 818c7501 2019-07-11 stsp static const struct got_error *
5381 818c7501 2019-07-11 stsp show_rebase_progress(struct got_commit_object *commit,
5382 818c7501 2019-07-11 stsp struct got_object_id *old_id, struct got_object_id *new_id)
5384 818c7501 2019-07-11 stsp const struct got_error *err;
5385 0ebf8283 2019-07-24 stsp char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
5387 818c7501 2019-07-11 stsp err = got_object_id_str(&old_id_str, old_id);
5389 818c7501 2019-07-11 stsp goto done;
5391 ff0d2220 2019-07-11 stsp if (new_id) {
5392 ff0d2220 2019-07-11 stsp err = got_object_id_str(&new_id_str, new_id);
5394 ff0d2220 2019-07-11 stsp goto done;
5397 818c7501 2019-07-11 stsp old_id_str[12] = '\0';
5398 ff0d2220 2019-07-11 stsp if (new_id_str)
5399 ff0d2220 2019-07-11 stsp new_id_str[12] = '\0';
5401 0ebf8283 2019-07-24 stsp err = get_short_logmsg(&logmsg, 42, commit);
5403 0ebf8283 2019-07-24 stsp goto done;
5405 ff0d2220 2019-07-11 stsp printf("%s -> %s: %s\n", old_id_str,
5406 ff0d2220 2019-07-11 stsp new_id_str ? new_id_str : "no-op change", logmsg);
5408 818c7501 2019-07-11 stsp free(old_id_str);
5409 818c7501 2019-07-11 stsp free(new_id_str);
5410 272a1371 2020-02-28 stsp free(logmsg);
5411 818c7501 2019-07-11 stsp return err;
5414 1ee397ad 2019-07-12 stsp static const struct got_error *
5415 818c7501 2019-07-11 stsp rebase_progress(void *arg, unsigned char status, const char *path)
5417 818c7501 2019-07-11 stsp unsigned char *rebase_status = arg;
5419 818c7501 2019-07-11 stsp while (path[0] == '/')
5421 818c7501 2019-07-11 stsp printf("%c %s\n", status, path);
5423 818c7501 2019-07-11 stsp if (*rebase_status == GOT_STATUS_CONFLICT)
5424 1ee397ad 2019-07-12 stsp return NULL;
5425 818c7501 2019-07-11 stsp if (status == GOT_STATUS_CONFLICT || status == GOT_STATUS_MERGE)
5426 818c7501 2019-07-11 stsp *rebase_status = status;
5427 1ee397ad 2019-07-12 stsp return NULL;
5430 818c7501 2019-07-11 stsp static const struct got_error *
5431 3e3a69f1 2019-07-25 stsp rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
5432 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_reference *new_base_branch,
5433 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_repository *repo)
5435 818c7501 2019-07-11 stsp printf("Switching work tree to %s\n", got_ref_get_name(branch));
5436 3e3a69f1 2019-07-25 stsp return got_worktree_rebase_complete(worktree, fileindex,
5437 818c7501 2019-07-11 stsp new_base_branch, tmp_branch, branch, repo);
5440 818c7501 2019-07-11 stsp static const struct got_error *
5441 01757395 2019-07-12 stsp rebase_commit(struct got_pathlist_head *merged_paths,
5442 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
5443 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch,
5444 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
5446 ff0d2220 2019-07-11 stsp const struct got_error *error;
5447 ff0d2220 2019-07-11 stsp struct got_commit_object *commit;
5448 ff0d2220 2019-07-11 stsp struct got_object_id *new_commit_id;
5450 ff0d2220 2019-07-11 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
5451 ff0d2220 2019-07-11 stsp if (error)
5452 ff0d2220 2019-07-11 stsp return error;
5454 01757395 2019-07-12 stsp error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
5455 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, commit, commit_id, repo);
5456 ff0d2220 2019-07-11 stsp if (error) {
5457 ff0d2220 2019-07-11 stsp if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
5458 ff0d2220 2019-07-11 stsp goto done;
5459 ff0d2220 2019-07-11 stsp error = show_rebase_progress(commit, commit_id, NULL);
5461 ff0d2220 2019-07-11 stsp error = show_rebase_progress(commit, commit_id, new_commit_id);
5462 ff0d2220 2019-07-11 stsp free(new_commit_id);
5465 ff0d2220 2019-07-11 stsp got_object_commit_close(commit);
5466 ff0d2220 2019-07-11 stsp return error;
5469 64c6d990 2019-07-11 stsp struct check_path_prefix_arg {
5470 64c6d990 2019-07-11 stsp const char *path_prefix;
5471 64c6d990 2019-07-11 stsp size_t len;
5472 8ca9bd68 2019-07-25 stsp int errcode;
5475 64c6d990 2019-07-11 stsp static const struct got_error *
5476 8ca9bd68 2019-07-25 stsp check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
5477 64c6d990 2019-07-11 stsp struct got_blob_object *blob2, struct got_object_id *id1,
5478 64c6d990 2019-07-11 stsp struct got_object_id *id2, const char *path1, const char *path2,
5479 46f68b20 2019-10-19 stsp mode_t mode1, mode_t mode2, struct got_repository *repo)
5481 64c6d990 2019-07-11 stsp struct check_path_prefix_arg *a = arg;
5483 64c6d990 2019-07-11 stsp if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
5484 64c6d990 2019-07-11 stsp (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
5485 8ca9bd68 2019-07-25 stsp return got_error(a->errcode);
5487 64c6d990 2019-07-11 stsp return NULL;
5490 64c6d990 2019-07-11 stsp static const struct got_error *
5491 8ca9bd68 2019-07-25 stsp check_path_prefix(struct got_object_id *parent_id,
5492 64c6d990 2019-07-11 stsp struct got_object_id *commit_id, const char *path_prefix,
5493 8ca9bd68 2019-07-25 stsp int errcode, struct got_repository *repo)
5495 64c6d990 2019-07-11 stsp const struct got_error *err;
5496 64c6d990 2019-07-11 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
5497 64c6d990 2019-07-11 stsp struct got_commit_object *commit = NULL, *parent_commit = NULL;
5498 64c6d990 2019-07-11 stsp struct check_path_prefix_arg cpp_arg;
5500 64c6d990 2019-07-11 stsp if (got_path_is_root_dir(path_prefix))
5501 64c6d990 2019-07-11 stsp return NULL;
5503 64c6d990 2019-07-11 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
5505 64c6d990 2019-07-11 stsp goto done;
5507 64c6d990 2019-07-11 stsp err = got_object_open_as_commit(&parent_commit, repo, parent_id);
5509 64c6d990 2019-07-11 stsp goto done;
5511 64c6d990 2019-07-11 stsp err = got_object_open_as_tree(&tree1, repo,
5512 64c6d990 2019-07-11 stsp got_object_commit_get_tree_id(parent_commit));
5514 64c6d990 2019-07-11 stsp goto done;
5516 64c6d990 2019-07-11 stsp err = got_object_open_as_tree(&tree2, repo,
5517 64c6d990 2019-07-11 stsp got_object_commit_get_tree_id(commit));
5519 64c6d990 2019-07-11 stsp goto done;
5521 64c6d990 2019-07-11 stsp cpp_arg.path_prefix = path_prefix;
5522 d23ace97 2019-07-25 stsp while (cpp_arg.path_prefix[0] == '/')
5523 d23ace97 2019-07-25 stsp cpp_arg.path_prefix++;
5524 d23ace97 2019-07-25 stsp cpp_arg.len = strlen(cpp_arg.path_prefix);
5525 8ca9bd68 2019-07-25 stsp cpp_arg.errcode = errcode;
5526 8ca9bd68 2019-07-25 stsp err = got_diff_tree(tree1, tree2, "", "", repo,
5527 31b4484f 2019-07-27 stsp check_path_prefix_in_diff, &cpp_arg, 0);
5529 64c6d990 2019-07-11 stsp if (tree1)
5530 64c6d990 2019-07-11 stsp got_object_tree_close(tree1);
5531 64c6d990 2019-07-11 stsp if (tree2)
5532 64c6d990 2019-07-11 stsp got_object_tree_close(tree2);
5533 64c6d990 2019-07-11 stsp if (commit)
5534 64c6d990 2019-07-11 stsp got_object_commit_close(commit);
5535 64c6d990 2019-07-11 stsp if (parent_commit)
5536 64c6d990 2019-07-11 stsp got_object_commit_close(parent_commit);
5537 64c6d990 2019-07-11 stsp return err;
5540 ff0d2220 2019-07-11 stsp static const struct got_error *
5541 8ca9bd68 2019-07-25 stsp collect_commits(struct got_object_id_queue *commits,
5542 af61c510 2019-07-19 stsp struct got_object_id *initial_commit_id,
5543 af61c510 2019-07-19 stsp struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
5544 8ca9bd68 2019-07-25 stsp const char *path_prefix, int path_prefix_errcode,
5545 8ca9bd68 2019-07-25 stsp struct got_repository *repo)
5547 af61c510 2019-07-19 stsp const struct got_error *err = NULL;
5548 af61c510 2019-07-19 stsp struct got_commit_graph *graph = NULL;
5549 af61c510 2019-07-19 stsp struct got_object_id *parent_id = NULL;
5550 af61c510 2019-07-19 stsp struct got_object_qid *qid;
5551 af61c510 2019-07-19 stsp struct got_object_id *commit_id = initial_commit_id;
5553 3d509237 2020-01-04 stsp err = got_commit_graph_open(&graph, "/", 1);
5555 af61c510 2019-07-19 stsp return err;
5557 6fb7cd11 2019-08-22 stsp err = got_commit_graph_iter_start(graph, iter_start_id, repo,
5558 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
5560 af61c510 2019-07-19 stsp goto done;
5561 af61c510 2019-07-19 stsp while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
5562 ee780d5c 2020-01-04 stsp err = got_commit_graph_iter_next(&parent_id, graph, repo,
5563 ee780d5c 2020-01-04 stsp check_cancelled, NULL);
5564 af61c510 2019-07-19 stsp if (err) {
5565 af61c510 2019-07-19 stsp if (err->code == GOT_ERR_ITER_COMPLETED) {
5566 af61c510 2019-07-19 stsp err = got_error_msg(GOT_ERR_ANCESTRY,
5567 af61c510 2019-07-19 stsp "ran out of commits to rebase before "
5568 af61c510 2019-07-19 stsp "youngest common ancestor commit has "
5569 af61c510 2019-07-19 stsp "been reached?!?");
5571 ee780d5c 2020-01-04 stsp goto done;
5573 8ca9bd68 2019-07-25 stsp err = check_path_prefix(parent_id, commit_id,
5574 8ca9bd68 2019-07-25 stsp path_prefix, path_prefix_errcode, repo);
5576 af61c510 2019-07-19 stsp goto done;
5578 af61c510 2019-07-19 stsp err = got_object_qid_alloc(&qid, commit_id);
5580 af61c510 2019-07-19 stsp goto done;
5581 af61c510 2019-07-19 stsp SIMPLEQ_INSERT_HEAD(commits, qid, entry);
5582 af61c510 2019-07-19 stsp commit_id = parent_id;
5586 af61c510 2019-07-19 stsp got_commit_graph_close(graph);
5587 af61c510 2019-07-19 stsp return err;
5590 af61c510 2019-07-19 stsp static const struct got_error *
5591 818c7501 2019-07-11 stsp cmd_rebase(int argc, char *argv[])
5593 818c7501 2019-07-11 stsp const struct got_error *error = NULL;
5594 818c7501 2019-07-11 stsp struct got_worktree *worktree = NULL;
5595 818c7501 2019-07-11 stsp struct got_repository *repo = NULL;
5596 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex = NULL;
5597 818c7501 2019-07-11 stsp char *cwd = NULL;
5598 818c7501 2019-07-11 stsp struct got_reference *branch = NULL;
5599 818c7501 2019-07-11 stsp struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
5600 818c7501 2019-07-11 stsp struct got_object_id *commit_id = NULL, *parent_id = NULL;
5601 818c7501 2019-07-11 stsp struct got_object_id *resume_commit_id = NULL;
5602 818c7501 2019-07-11 stsp struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
5603 818c7501 2019-07-11 stsp struct got_commit_object *commit = NULL;
5604 818c7501 2019-07-11 stsp int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
5605 7ef62c4e 2020-02-24 stsp int histedit_in_progress = 0;
5606 818c7501 2019-07-11 stsp unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
5607 818c7501 2019-07-11 stsp struct got_object_id_queue commits;
5608 01757395 2019-07-12 stsp struct got_pathlist_head merged_paths;
5609 818c7501 2019-07-11 stsp const struct got_object_id_queue *parent_ids;
5610 818c7501 2019-07-11 stsp struct got_object_qid *qid, *pid;
5612 818c7501 2019-07-11 stsp SIMPLEQ_INIT(&commits);
5613 01757395 2019-07-12 stsp TAILQ_INIT(&merged_paths);
5615 818c7501 2019-07-11 stsp while ((ch = getopt(argc, argv, "ac")) != -1) {
5616 818c7501 2019-07-11 stsp switch (ch) {
5618 818c7501 2019-07-11 stsp abort_rebase = 1;
5621 818c7501 2019-07-11 stsp continue_rebase = 1;
5624 818c7501 2019-07-11 stsp usage_rebase();
5625 818c7501 2019-07-11 stsp /* NOTREACHED */
5629 818c7501 2019-07-11 stsp argc -= optind;
5630 818c7501 2019-07-11 stsp argv += optind;
5632 43012d58 2019-07-14 stsp #ifndef PROFILE
5633 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5634 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
5635 43012d58 2019-07-14 stsp err(1, "pledge");
5637 818c7501 2019-07-11 stsp if (abort_rebase && continue_rebase)
5638 818c7501 2019-07-11 stsp usage_rebase();
5639 818c7501 2019-07-11 stsp else if (abort_rebase || continue_rebase) {
5640 818c7501 2019-07-11 stsp if (argc != 0)
5641 818c7501 2019-07-11 stsp usage_rebase();
5642 818c7501 2019-07-11 stsp } else if (argc != 1)
5643 818c7501 2019-07-11 stsp usage_rebase();
5645 818c7501 2019-07-11 stsp cwd = getcwd(NULL, 0);
5646 818c7501 2019-07-11 stsp if (cwd == NULL) {
5647 818c7501 2019-07-11 stsp error = got_error_from_errno("getcwd");
5648 818c7501 2019-07-11 stsp goto done;
5650 818c7501 2019-07-11 stsp error = got_worktree_open(&worktree, cwd);
5651 818c7501 2019-07-11 stsp if (error)
5652 818c7501 2019-07-11 stsp goto done;
5654 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5656 818c7501 2019-07-11 stsp if (error != NULL)
5657 818c7501 2019-07-11 stsp goto done;
5659 818c7501 2019-07-11 stsp error = apply_unveil(got_repo_get_path(repo), 0,
5660 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
5661 7ef62c4e 2020-02-24 stsp if (error)
5662 7ef62c4e 2020-02-24 stsp goto done;
5664 7ef62c4e 2020-02-24 stsp error = got_worktree_histedit_in_progress(&histedit_in_progress,
5665 7ef62c4e 2020-02-24 stsp worktree);
5666 818c7501 2019-07-11 stsp if (error)
5667 7ef62c4e 2020-02-24 stsp goto done;
5668 7ef62c4e 2020-02-24 stsp if (histedit_in_progress) {
5669 7ef62c4e 2020-02-24 stsp error = got_error(GOT_ERR_HISTEDIT_BUSY);
5670 818c7501 2019-07-11 stsp goto done;
5673 818c7501 2019-07-11 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
5674 818c7501 2019-07-11 stsp if (error)
5675 818c7501 2019-07-11 stsp goto done;
5677 f6794adc 2019-07-23 stsp if (abort_rebase) {
5678 818c7501 2019-07-11 stsp int did_something;
5679 f6794adc 2019-07-23 stsp if (!rebase_in_progress) {
5680 f6794adc 2019-07-23 stsp error = got_error(GOT_ERR_NOT_REBASING);
5681 f6794adc 2019-07-23 stsp goto done;
5683 818c7501 2019-07-11 stsp error = got_worktree_rebase_continue(&resume_commit_id,
5684 3e3a69f1 2019-07-25 stsp &new_base_branch, &tmp_branch, &branch, &fileindex,
5685 3e3a69f1 2019-07-25 stsp worktree, repo);
5686 818c7501 2019-07-11 stsp if (error)
5687 818c7501 2019-07-11 stsp goto done;
5688 818c7501 2019-07-11 stsp printf("Switching work tree to %s\n",
5689 818c7501 2019-07-11 stsp got_ref_get_symref_target(new_base_branch));
5690 3e3a69f1 2019-07-25 stsp error = got_worktree_rebase_abort(worktree, fileindex, repo,
5691 818c7501 2019-07-11 stsp new_base_branch, update_progress, &did_something);
5692 818c7501 2019-07-11 stsp if (error)
5693 818c7501 2019-07-11 stsp goto done;
5694 818c7501 2019-07-11 stsp printf("Rebase of %s aborted\n", got_ref_get_name(branch));
5695 818c7501 2019-07-11 stsp goto done; /* nothing else to do */
5698 818c7501 2019-07-11 stsp if (continue_rebase) {
5699 f6794adc 2019-07-23 stsp if (!rebase_in_progress) {
5700 f6794adc 2019-07-23 stsp error = got_error(GOT_ERR_NOT_REBASING);
5701 f6794adc 2019-07-23 stsp goto done;
5703 818c7501 2019-07-11 stsp error = got_worktree_rebase_continue(&resume_commit_id,
5704 3e3a69f1 2019-07-25 stsp &new_base_branch, &tmp_branch, &branch, &fileindex,
5705 3e3a69f1 2019-07-25 stsp worktree, repo);
5706 818c7501 2019-07-11 stsp if (error)
5707 818c7501 2019-07-11 stsp goto done;
5709 3e3a69f1 2019-07-25 stsp error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
5710 01757395 2019-07-12 stsp resume_commit_id, repo);
5711 818c7501 2019-07-11 stsp if (error)
5712 818c7501 2019-07-11 stsp goto done;
5714 ff0d2220 2019-07-11 stsp yca_id = got_object_id_dup(resume_commit_id);
5715 ff0d2220 2019-07-11 stsp if (yca_id == NULL) {
5716 818c7501 2019-07-11 stsp error = got_error_from_errno("got_object_id_dup");
5717 818c7501 2019-07-11 stsp goto done;
5720 818c7501 2019-07-11 stsp error = got_ref_open(&branch, repo, argv[0], 0);
5721 818c7501 2019-07-11 stsp if (error != NULL)
5722 818c7501 2019-07-11 stsp goto done;
5725 ff0d2220 2019-07-11 stsp error = got_ref_resolve(&branch_head_commit_id, repo, branch);
5726 ff0d2220 2019-07-11 stsp if (error)
5727 ff0d2220 2019-07-11 stsp goto done;
5729 ff0d2220 2019-07-11 stsp if (!continue_rebase) {
5730 a51a74b3 2019-07-27 stsp struct got_object_id *base_commit_id;
5732 a51a74b3 2019-07-27 stsp base_commit_id = got_worktree_get_base_commit_id(worktree);
5733 818c7501 2019-07-11 stsp error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
5734 6fb7cd11 2019-08-22 stsp base_commit_id, branch_head_commit_id, repo,
5735 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
5736 818c7501 2019-07-11 stsp if (error)
5737 818c7501 2019-07-11 stsp goto done;
5738 818c7501 2019-07-11 stsp if (yca_id == NULL) {
5739 818c7501 2019-07-11 stsp error = got_error_msg(GOT_ERR_ANCESTRY,
5740 818c7501 2019-07-11 stsp "specified branch shares no common ancestry "
5741 818c7501 2019-07-11 stsp "with work tree's branch");
5742 818c7501 2019-07-11 stsp goto done;
5745 a51a74b3 2019-07-27 stsp error = check_same_branch(base_commit_id, branch, yca_id, repo);
5746 a51a74b3 2019-07-27 stsp if (error) {
5747 a51a74b3 2019-07-27 stsp if (error->code != GOT_ERR_ANCESTRY)
5748 a51a74b3 2019-07-27 stsp goto done;
5749 a51a74b3 2019-07-27 stsp error = NULL;
5751 a51a74b3 2019-07-27 stsp error = got_error_msg(GOT_ERR_SAME_BRANCH,
5752 a51a74b3 2019-07-27 stsp "specified branch resolves to a commit which "
5753 a51a74b3 2019-07-27 stsp "is already contained in work tree's branch");
5754 a51a74b3 2019-07-27 stsp goto done;
5756 818c7501 2019-07-11 stsp error = got_worktree_rebase_prepare(&new_base_branch,
5757 3e3a69f1 2019-07-25 stsp &tmp_branch, &fileindex, worktree, branch, repo);
5758 818c7501 2019-07-11 stsp if (error)
5759 818c7501 2019-07-11 stsp goto done;
5762 ff0d2220 2019-07-11 stsp commit_id = branch_head_commit_id;
5763 818c7501 2019-07-11 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
5764 818c7501 2019-07-11 stsp if (error)
5765 818c7501 2019-07-11 stsp goto done;
5767 818c7501 2019-07-11 stsp parent_ids = got_object_commit_get_parent_ids(commit);
5768 818c7501 2019-07-11 stsp pid = SIMPLEQ_FIRST(parent_ids);
5769 fc66b545 2019-08-12 stsp if (pid == NULL) {
5770 fc66b545 2019-08-12 stsp if (!continue_rebase) {
5771 fc66b545 2019-08-12 stsp int did_something;
5772 fc66b545 2019-08-12 stsp error = got_worktree_rebase_abort(worktree, fileindex,
5773 fc66b545 2019-08-12 stsp repo, new_base_branch, update_progress,
5774 fc66b545 2019-08-12 stsp &did_something);
5775 fc66b545 2019-08-12 stsp if (error)
5776 fc66b545 2019-08-12 stsp goto done;
5777 fc66b545 2019-08-12 stsp printf("Rebase of %s aborted\n",
5778 fc66b545 2019-08-12 stsp got_ref_get_name(branch));
5780 fc66b545 2019-08-12 stsp error = got_error(GOT_ERR_EMPTY_REBASE);
5781 fc66b545 2019-08-12 stsp goto done;
5783 8ca9bd68 2019-07-25 stsp error = collect_commits(&commits, commit_id, pid->id,
5784 8ca9bd68 2019-07-25 stsp yca_id, got_worktree_get_path_prefix(worktree),
5785 8ca9bd68 2019-07-25 stsp GOT_ERR_REBASE_PATH, repo);
5786 818c7501 2019-07-11 stsp got_object_commit_close(commit);
5787 818c7501 2019-07-11 stsp commit = NULL;
5788 818c7501 2019-07-11 stsp if (error)
5789 818c7501 2019-07-11 stsp goto done;
5791 818c7501 2019-07-11 stsp if (SIMPLEQ_EMPTY(&commits)) {
5792 38b0338b 2019-11-29 stsp if (continue_rebase) {
5793 3e3a69f1 2019-07-25 stsp error = rebase_complete(worktree, fileindex,
5794 3e3a69f1 2019-07-25 stsp branch, new_base_branch, tmp_branch, repo);
5795 38b0338b 2019-11-29 stsp goto done;
5797 38b0338b 2019-11-29 stsp /* Fast-forward the reference of the branch. */
5798 38b0338b 2019-11-29 stsp struct got_object_id *new_head_commit_id;
5799 38b0338b 2019-11-29 stsp char *id_str;
5800 38b0338b 2019-11-29 stsp error = got_ref_resolve(&new_head_commit_id, repo,
5801 38b0338b 2019-11-29 stsp new_base_branch);
5802 38b0338b 2019-11-29 stsp if (error)
5803 38b0338b 2019-11-29 stsp goto done;
5804 38b0338b 2019-11-29 stsp error = got_object_id_str(&id_str, new_head_commit_id);
5805 38b0338b 2019-11-29 stsp printf("Forwarding %s to commit %s\n",
5806 38b0338b 2019-11-29 stsp got_ref_get_name(branch), id_str);
5807 38b0338b 2019-11-29 stsp free(id_str);
5808 38b0338b 2019-11-29 stsp error = got_ref_change_ref(branch,
5809 38b0338b 2019-11-29 stsp new_head_commit_id);
5810 38b0338b 2019-11-29 stsp if (error)
5811 38b0338b 2019-11-29 stsp goto done;
5815 818c7501 2019-07-11 stsp pid = NULL;
5816 818c7501 2019-07-11 stsp SIMPLEQ_FOREACH(qid, &commits, entry) {
5817 818c7501 2019-07-11 stsp commit_id = qid->id;
5818 818c7501 2019-07-11 stsp parent_id = pid ? pid->id : yca_id;
5819 818c7501 2019-07-11 stsp pid = qid;
5821 01757395 2019-07-12 stsp error = got_worktree_rebase_merge_files(&merged_paths,
5822 3e3a69f1 2019-07-25 stsp worktree, fileindex, parent_id, commit_id, repo,
5823 3e3a69f1 2019-07-25 stsp rebase_progress, &rebase_status, check_cancelled, NULL);
5824 818c7501 2019-07-11 stsp if (error)
5825 818c7501 2019-07-11 stsp goto done;
5827 01757395 2019-07-12 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
5828 a0ea4fc0 2020-02-28 stsp error = show_rebase_merge_conflict(qid->id, repo);
5829 a0ea4fc0 2020-02-28 stsp if (error)
5830 a0ea4fc0 2020-02-28 stsp goto done;
5831 01757395 2019-07-12 stsp got_worktree_rebase_pathlist_free(&merged_paths);
5835 3e3a69f1 2019-07-25 stsp error = rebase_commit(&merged_paths, worktree, fileindex,
5836 3e3a69f1 2019-07-25 stsp tmp_branch, commit_id, repo);
5837 01757395 2019-07-12 stsp got_worktree_rebase_pathlist_free(&merged_paths);
5838 818c7501 2019-07-11 stsp if (error)
5839 818c7501 2019-07-11 stsp goto done;
5842 818c7501 2019-07-11 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
5843 3e3a69f1 2019-07-25 stsp error = got_worktree_rebase_postpone(worktree, fileindex);
5844 818c7501 2019-07-11 stsp if (error)
5845 818c7501 2019-07-11 stsp goto done;
5846 818c7501 2019-07-11 stsp error = got_error_msg(GOT_ERR_CONFLICTS,
5847 11495e04 2019-07-12 stsp "conflicts must be resolved before rebasing can continue");
5849 3e3a69f1 2019-07-25 stsp error = rebase_complete(worktree, fileindex, branch,
5850 3e3a69f1 2019-07-25 stsp new_base_branch, tmp_branch, repo);
5852 818c7501 2019-07-11 stsp got_object_id_queue_free(&commits);
5853 818c7501 2019-07-11 stsp free(branch_head_commit_id);
5854 818c7501 2019-07-11 stsp free(resume_commit_id);
5855 818c7501 2019-07-11 stsp free(yca_id);
5856 818c7501 2019-07-11 stsp if (commit)
5857 818c7501 2019-07-11 stsp got_object_commit_close(commit);
5858 818c7501 2019-07-11 stsp if (branch)
5859 818c7501 2019-07-11 stsp got_ref_close(branch);
5860 818c7501 2019-07-11 stsp if (new_base_branch)
5861 818c7501 2019-07-11 stsp got_ref_close(new_base_branch);
5862 818c7501 2019-07-11 stsp if (tmp_branch)
5863 818c7501 2019-07-11 stsp got_ref_close(tmp_branch);
5864 5ef14e63 2019-06-02 stsp if (worktree)
5865 5ef14e63 2019-06-02 stsp got_worktree_close(worktree);
5867 5ef14e63 2019-06-02 stsp got_repo_close(repo);
5868 5ef14e63 2019-06-02 stsp return error;
5871 0ebf8283 2019-07-24 stsp __dead static void
5872 0ebf8283 2019-07-24 stsp usage_histedit(void)
5874 083957f4 2020-02-24 stsp fprintf(stderr, "usage: %s histedit [-a] [-c] [-F histedit-script] [-m]\n",
5875 0ebf8283 2019-07-24 stsp getprogname());
5879 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_PICK 'p'
5880 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_EDIT 'e'
5881 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_FOLD 'f'
5882 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_DROP 'd'
5883 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_MESG 'm'
5885 0ebf8283 2019-07-24 stsp static struct got_histedit_cmd {
5886 0ebf8283 2019-07-24 stsp unsigned char code;
5887 0ebf8283 2019-07-24 stsp const char *name;
5888 0ebf8283 2019-07-24 stsp const char *desc;
5889 0ebf8283 2019-07-24 stsp } got_histedit_cmds[] = {
5890 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_PICK, "pick", "use commit" },
5891 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
5892 82997472 2020-01-29 stsp { GOT_HISTEDIT_FOLD, "fold", "combine with next commit that will "
5893 82997472 2020-01-29 stsp "be used" },
5894 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
5895 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_MESG, "mesg",
5896 0ebf8283 2019-07-24 stsp "single-line log message for commit above (open editor if empty)" },
5899 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry {
5900 0ebf8283 2019-07-24 stsp TAILQ_ENTRY(got_histedit_list_entry) entry;
5901 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id;
5902 0ebf8283 2019-07-24 stsp const struct got_histedit_cmd *cmd;
5903 0ebf8283 2019-07-24 stsp char *logmsg;
5905 0ebf8283 2019-07-24 stsp TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
5907 0ebf8283 2019-07-24 stsp static const struct got_error *
5908 0ebf8283 2019-07-24 stsp histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
5909 0ebf8283 2019-07-24 stsp FILE *f, struct got_repository *repo)
5911 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
5912 0ebf8283 2019-07-24 stsp char *logmsg = NULL, *id_str = NULL;
5913 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
5916 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
5918 0ebf8283 2019-07-24 stsp goto done;
5920 0ebf8283 2019-07-24 stsp err = get_short_logmsg(&logmsg, 34, commit);
5922 0ebf8283 2019-07-24 stsp goto done;
5924 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, commit_id);
5926 0ebf8283 2019-07-24 stsp goto done;
5928 0ebf8283 2019-07-24 stsp n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
5929 0ebf8283 2019-07-24 stsp if (n < 0)
5930 0ebf8283 2019-07-24 stsp err = got_ferror(f, GOT_ERR_IO);
5932 0ebf8283 2019-07-24 stsp if (commit)
5933 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
5934 0ebf8283 2019-07-24 stsp free(id_str);
5935 0ebf8283 2019-07-24 stsp free(logmsg);
5936 0ebf8283 2019-07-24 stsp return err;
5939 0ebf8283 2019-07-24 stsp static const struct got_error *
5940 083957f4 2020-02-24 stsp histedit_write_commit_list(struct got_object_id_queue *commits,
5941 083957f4 2020-02-24 stsp FILE *f, int edit_logmsg_only, struct got_repository *repo)
5943 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
5944 0ebf8283 2019-07-24 stsp struct got_object_qid *qid;
5946 0ebf8283 2019-07-24 stsp if (SIMPLEQ_EMPTY(commits))
5947 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_EMPTY_HISTEDIT);
5949 0ebf8283 2019-07-24 stsp SIMPLEQ_FOREACH(qid, commits, entry) {
5950 0ebf8283 2019-07-24 stsp err = histedit_write_commit(qid->id, got_histedit_cmds[0].name,
5954 083957f4 2020-02-24 stsp if (edit_logmsg_only) {
5955 083957f4 2020-02-24 stsp int n = fprintf(f, "%c\n", GOT_HISTEDIT_MESG);
5956 083957f4 2020-02-24 stsp if (n < 0) {
5957 083957f4 2020-02-24 stsp err = got_ferror(f, GOT_ERR_IO);
5963 0ebf8283 2019-07-24 stsp return err;
5966 0ebf8283 2019-07-24 stsp static const struct got_error *
5967 514f2ffe 2020-01-29 stsp write_cmd_list(FILE *f, const char *branch_name,
5968 514f2ffe 2020-01-29 stsp struct got_object_id_queue *commits)
5970 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
5972 514f2ffe 2020-01-29 stsp char *id_str;
5973 514f2ffe 2020-01-29 stsp struct got_object_qid *qid;
5975 514f2ffe 2020-01-29 stsp qid = SIMPLEQ_FIRST(commits);
5976 514f2ffe 2020-01-29 stsp err = got_object_id_str(&id_str, qid->id);
5978 514f2ffe 2020-01-29 stsp return err;
5980 514f2ffe 2020-01-29 stsp n = fprintf(f,
5981 514f2ffe 2020-01-29 stsp "# Editing the history of branch '%s' starting at\n"
5982 514f2ffe 2020-01-29 stsp "# commit %s\n"
5983 514f2ffe 2020-01-29 stsp "# Commits will be processed in order from top to "
5984 514f2ffe 2020-01-29 stsp "bottom of this file.\n", branch_name, id_str);
5985 514f2ffe 2020-01-29 stsp if (n < 0) {
5986 514f2ffe 2020-01-29 stsp err = got_ferror(f, GOT_ERR_IO);
5987 514f2ffe 2020-01-29 stsp goto done;
5990 0ebf8283 2019-07-24 stsp n = fprintf(f, "# Available histedit commands:\n");
5991 514f2ffe 2020-01-29 stsp if (n < 0) {
5992 514f2ffe 2020-01-29 stsp err = got_ferror(f, GOT_ERR_IO);
5993 514f2ffe 2020-01-29 stsp goto done;
5996 0ebf8283 2019-07-24 stsp for (i = 0; i < nitems(got_histedit_cmds); i++) {
5997 0ebf8283 2019-07-24 stsp struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
5998 0ebf8283 2019-07-24 stsp n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
5999 0ebf8283 2019-07-24 stsp cmd->desc);
6000 0ebf8283 2019-07-24 stsp if (n < 0) {
6001 0ebf8283 2019-07-24 stsp err = got_ferror(f, GOT_ERR_IO);
6006 514f2ffe 2020-01-29 stsp free(id_str);
6007 0ebf8283 2019-07-24 stsp return err;
6010 0ebf8283 2019-07-24 stsp static const struct got_error *
6011 0ebf8283 2019-07-24 stsp histedit_syntax_error(int lineno)
6013 0ebf8283 2019-07-24 stsp static char msg[42];
6016 0ebf8283 2019-07-24 stsp ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
6018 0ebf8283 2019-07-24 stsp if (ret == -1 || ret >= sizeof(msg))
6019 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_HISTEDIT_SYNTAX);
6021 0ebf8283 2019-07-24 stsp return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
6024 0ebf8283 2019-07-24 stsp static const struct got_error *
6025 0ebf8283 2019-07-24 stsp append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
6026 0ebf8283 2019-07-24 stsp char *logmsg, struct got_repository *repo)
6028 0ebf8283 2019-07-24 stsp const struct got_error *err;
6029 0ebf8283 2019-07-24 stsp struct got_commit_object *folded_commit = NULL;
6030 5943eee2 2019-08-13 stsp char *id_str, *folded_logmsg = NULL;
6032 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, hle->commit_id);
6034 0ebf8283 2019-07-24 stsp return err;
6036 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
6038 0ebf8283 2019-07-24 stsp goto done;
6040 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
6042 5943eee2 2019-08-13 stsp goto done;
6043 0ebf8283 2019-07-24 stsp if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
6044 0ebf8283 2019-07-24 stsp logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
6045 5943eee2 2019-08-13 stsp folded_logmsg) == -1) {
6046 0ebf8283 2019-07-24 stsp err = got_error_from_errno("asprintf");
6049 0ebf8283 2019-07-24 stsp if (folded_commit)
6050 0ebf8283 2019-07-24 stsp got_object_commit_close(folded_commit);
6051 0ebf8283 2019-07-24 stsp free(id_str);
6052 5943eee2 2019-08-13 stsp free(folded_logmsg);
6053 0ebf8283 2019-07-24 stsp return err;
6056 0ebf8283 2019-07-24 stsp static struct got_histedit_list_entry *
6057 0ebf8283 2019-07-24 stsp get_folded_commits(struct got_histedit_list_entry *hle)
6059 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *prev, *folded = NULL;
6061 0ebf8283 2019-07-24 stsp prev = TAILQ_PREV(hle, got_histedit_list, entry);
6062 3f9de99f 2019-07-24 stsp while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
6063 3f9de99f 2019-07-24 stsp prev->cmd->code == GOT_HISTEDIT_DROP)) {
6064 3f9de99f 2019-07-24 stsp if (prev->cmd->code == GOT_HISTEDIT_FOLD)
6065 3f9de99f 2019-07-24 stsp folded = prev;
6066 0ebf8283 2019-07-24 stsp prev = TAILQ_PREV(prev, got_histedit_list, entry);
6069 0ebf8283 2019-07-24 stsp return folded;
6072 0ebf8283 2019-07-24 stsp static const struct got_error *
6073 0ebf8283 2019-07-24 stsp histedit_edit_logmsg(struct got_histedit_list_entry *hle,
6074 0ebf8283 2019-07-24 stsp struct got_repository *repo)
6076 5943eee2 2019-08-13 stsp char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
6077 0ebf8283 2019-07-24 stsp char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
6078 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
6079 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
6081 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *folded = NULL;
6083 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&commit, repo, hle->commit_id);
6085 0ebf8283 2019-07-24 stsp return err;
6087 0ebf8283 2019-07-24 stsp folded = get_folded_commits(hle);
6088 0ebf8283 2019-07-24 stsp if (folded) {
6089 0ebf8283 2019-07-24 stsp while (folded != hle) {
6090 3f9de99f 2019-07-24 stsp if (folded->cmd->code == GOT_HISTEDIT_DROP) {
6091 3f9de99f 2019-07-24 stsp folded = TAILQ_NEXT(folded, entry);
6094 0ebf8283 2019-07-24 stsp err = append_folded_commit_msg(&new_msg, folded,
6095 0ebf8283 2019-07-24 stsp logmsg, repo);
6097 0ebf8283 2019-07-24 stsp goto done;
6098 0ebf8283 2019-07-24 stsp free(logmsg);
6099 0ebf8283 2019-07-24 stsp logmsg = new_msg;
6100 0ebf8283 2019-07-24 stsp folded = TAILQ_NEXT(folded, entry);
6104 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, hle->commit_id);
6106 0ebf8283 2019-07-24 stsp goto done;
6107 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&orig_logmsg, commit);
6109 5943eee2 2019-08-13 stsp goto done;
6110 0ebf8283 2019-07-24 stsp if (asprintf(&new_msg,
6111 0ebf8283 2019-07-24 stsp "%s\n# original log message of commit %s: %s",
6112 5943eee2 2019-08-13 stsp logmsg ? logmsg : "", id_str, orig_logmsg) == -1) {
6113 0ebf8283 2019-07-24 stsp err = got_error_from_errno("asprintf");
6114 0ebf8283 2019-07-24 stsp goto done;
6116 0ebf8283 2019-07-24 stsp free(logmsg);
6117 0ebf8283 2019-07-24 stsp logmsg = new_msg;
6119 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, hle->commit_id);
6121 0ebf8283 2019-07-24 stsp goto done;
6123 bb63914a 2020-02-17 stsp err = got_opentemp_named_fd(&logmsg_path, &fd,
6124 bb63914a 2020-02-17 stsp GOT_TMPDIR_STR "/got-logmsg");
6126 0ebf8283 2019-07-24 stsp goto done;
6128 0ebf8283 2019-07-24 stsp dprintf(fd, logmsg);
6129 0ebf8283 2019-07-24 stsp close(fd);
6131 0ebf8283 2019-07-24 stsp err = get_editor(&editor);
6133 0ebf8283 2019-07-24 stsp goto done;
6135 0ebf8283 2019-07-24 stsp err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg);
6136 0ebf8283 2019-07-24 stsp if (err) {
6137 0ebf8283 2019-07-24 stsp if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
6138 0ebf8283 2019-07-24 stsp goto done;
6139 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&hle->logmsg, commit);
6142 0ebf8283 2019-07-24 stsp if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
6143 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("unlink", logmsg_path);
6144 0ebf8283 2019-07-24 stsp free(logmsg_path);
6145 0ebf8283 2019-07-24 stsp free(logmsg);
6146 5943eee2 2019-08-13 stsp free(orig_logmsg);
6147 0ebf8283 2019-07-24 stsp free(editor);
6148 0ebf8283 2019-07-24 stsp if (commit)
6149 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
6150 0ebf8283 2019-07-24 stsp return err;
6153 0ebf8283 2019-07-24 stsp static const struct got_error *
6154 0ebf8283 2019-07-24 stsp histedit_parse_list(struct got_histedit_list *histedit_cmds,
6155 0ebf8283 2019-07-24 stsp FILE *f, struct got_repository *repo)
6157 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
6158 0ebf8283 2019-07-24 stsp char *line = NULL, *p, *end;
6159 0ebf8283 2019-07-24 stsp size_t size;
6160 0ebf8283 2019-07-24 stsp ssize_t len;
6161 0ebf8283 2019-07-24 stsp int lineno = 0, i;
6162 0ebf8283 2019-07-24 stsp const struct got_histedit_cmd *cmd;
6163 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id = NULL;
6164 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle = NULL;
6166 0ebf8283 2019-07-24 stsp for (;;) {
6167 0ebf8283 2019-07-24 stsp len = getline(&line, &size, f);
6168 0ebf8283 2019-07-24 stsp if (len == -1) {
6169 0ebf8283 2019-07-24 stsp const struct got_error *getline_err;
6170 0ebf8283 2019-07-24 stsp if (feof(f))
6172 0ebf8283 2019-07-24 stsp getline_err = got_error_from_errno("getline");
6173 0ebf8283 2019-07-24 stsp err = got_ferror(f, getline_err->code);
6178 0ebf8283 2019-07-24 stsp while (isspace((unsigned char)p[0]))
6180 0ebf8283 2019-07-24 stsp if (p[0] == '#' || p[0] == '\0') {
6181 0ebf8283 2019-07-24 stsp free(line);
6182 0ebf8283 2019-07-24 stsp line = NULL;
6185 0ebf8283 2019-07-24 stsp cmd = NULL;
6186 0ebf8283 2019-07-24 stsp for (i = 0; i < nitems(got_histedit_cmds); i++) {
6187 0ebf8283 2019-07-24 stsp cmd = &got_histedit_cmds[i];
6188 0ebf8283 2019-07-24 stsp if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
6189 0ebf8283 2019-07-24 stsp isspace((unsigned char)p[strlen(cmd->name)])) {
6190 0ebf8283 2019-07-24 stsp p += strlen(cmd->name);
6193 0ebf8283 2019-07-24 stsp if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
6198 6c1844f6 2019-07-25 stsp if (i == nitems(got_histedit_cmds)) {
6199 0ebf8283 2019-07-24 stsp err = histedit_syntax_error(lineno);
6202 0ebf8283 2019-07-24 stsp while (isspace((unsigned char)p[0]))
6204 0ebf8283 2019-07-24 stsp if (cmd->code == GOT_HISTEDIT_MESG) {
6205 0ebf8283 2019-07-24 stsp if (hle == NULL || hle->logmsg != NULL) {
6206 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_HISTEDIT_CMD);
6209 0ebf8283 2019-07-24 stsp if (p[0] == '\0') {
6210 0ebf8283 2019-07-24 stsp err = histedit_edit_logmsg(hle, repo);
6214 0ebf8283 2019-07-24 stsp hle->logmsg = strdup(p);
6215 0ebf8283 2019-07-24 stsp if (hle->logmsg == NULL) {
6216 0ebf8283 2019-07-24 stsp err = got_error_from_errno("strdup");
6220 0ebf8283 2019-07-24 stsp free(line);
6221 0ebf8283 2019-07-24 stsp line = NULL;
6225 0ebf8283 2019-07-24 stsp while (end[0] && !isspace((unsigned char)end[0]))
6227 0ebf8283 2019-07-24 stsp *end = '\0';
6229 0ebf8283 2019-07-24 stsp err = got_object_resolve_id_str(&commit_id, repo, p);
6230 0ebf8283 2019-07-24 stsp if (err) {
6231 0ebf8283 2019-07-24 stsp /* override error code */
6232 0ebf8283 2019-07-24 stsp err = histedit_syntax_error(lineno);
6236 0ebf8283 2019-07-24 stsp hle = malloc(sizeof(*hle));
6237 0ebf8283 2019-07-24 stsp if (hle == NULL) {
6238 0ebf8283 2019-07-24 stsp err = got_error_from_errno("malloc");
6241 0ebf8283 2019-07-24 stsp hle->cmd = cmd;
6242 0ebf8283 2019-07-24 stsp hle->commit_id = commit_id;
6243 0ebf8283 2019-07-24 stsp hle->logmsg = NULL;
6244 0ebf8283 2019-07-24 stsp commit_id = NULL;
6245 0ebf8283 2019-07-24 stsp free(line);
6246 0ebf8283 2019-07-24 stsp line = NULL;
6247 0ebf8283 2019-07-24 stsp TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
6250 0ebf8283 2019-07-24 stsp free(line);
6251 0ebf8283 2019-07-24 stsp free(commit_id);
6252 0ebf8283 2019-07-24 stsp return err;
6255 0ebf8283 2019-07-24 stsp static const struct got_error *
6256 bfce7f83 2019-07-27 stsp histedit_check_script(struct got_histedit_list *histedit_cmds,
6257 bfce7f83 2019-07-27 stsp struct got_object_id_queue *commits, struct got_repository *repo)
6259 bfce7f83 2019-07-27 stsp const struct got_error *err = NULL;
6260 bfce7f83 2019-07-27 stsp struct got_object_qid *qid;
6261 bfce7f83 2019-07-27 stsp struct got_histedit_list_entry *hle;
6262 5b87815e 2020-03-05 stsp static char msg[92];
6263 bfce7f83 2019-07-27 stsp char *id_str;
6265 bfce7f83 2019-07-27 stsp if (TAILQ_EMPTY(histedit_cmds))
6266 bfce7f83 2019-07-27 stsp return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
6267 bfce7f83 2019-07-27 stsp "histedit script contains no commands");
6268 a0de39f3 2019-08-09 stsp if (SIMPLEQ_EMPTY(commits))
6269 a0de39f3 2019-08-09 stsp return got_error(GOT_ERR_EMPTY_HISTEDIT);
6271 5b87815e 2020-03-05 stsp TAILQ_FOREACH(hle, histedit_cmds, entry) {
6272 5b87815e 2020-03-05 stsp struct got_histedit_list_entry *hle2;
6273 5b87815e 2020-03-05 stsp TAILQ_FOREACH(hle2, histedit_cmds, entry) {
6274 5b87815e 2020-03-05 stsp if (hle == hle2)
6276 5b87815e 2020-03-05 stsp if (got_object_id_cmp(hle->commit_id,
6277 5b87815e 2020-03-05 stsp hle2->commit_id) != 0)
6279 5b87815e 2020-03-05 stsp err = got_object_id_str(&id_str, hle->commit_id);
6281 5b87815e 2020-03-05 stsp return err;
6282 5b87815e 2020-03-05 stsp snprintf(msg, sizeof(msg), "commit %s is listed "
6283 5b87815e 2020-03-05 stsp "more than once in histedit script", id_str);
6284 5b87815e 2020-03-05 stsp free(id_str);
6285 5b87815e 2020-03-05 stsp return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
6289 bfce7f83 2019-07-27 stsp SIMPLEQ_FOREACH(qid, commits, entry) {
6290 bfce7f83 2019-07-27 stsp TAILQ_FOREACH(hle, histedit_cmds, entry) {
6291 bfce7f83 2019-07-27 stsp if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
6294 bfce7f83 2019-07-27 stsp if (hle == NULL) {
6295 bfce7f83 2019-07-27 stsp err = got_object_id_str(&id_str, qid->id);
6297 bfce7f83 2019-07-27 stsp return err;
6298 bfce7f83 2019-07-27 stsp snprintf(msg, sizeof(msg),
6299 bfce7f83 2019-07-27 stsp "commit %s missing from histedit script", id_str);
6300 bfce7f83 2019-07-27 stsp free(id_str);
6301 bfce7f83 2019-07-27 stsp return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
6305 0def28b1 2019-08-17 stsp hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
6306 0def28b1 2019-08-17 stsp if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
6307 bfce7f83 2019-07-27 stsp return got_error_msg(GOT_ERR_HISTEDIT_CMD,
6308 bfce7f83 2019-07-27 stsp "last commit in histedit script cannot be folded");
6310 bfce7f83 2019-07-27 stsp return NULL;
6313 bfce7f83 2019-07-27 stsp static const struct got_error *
6314 0ebf8283 2019-07-24 stsp histedit_run_editor(struct got_histedit_list *histedit_cmds,
6315 bfce7f83 2019-07-27 stsp const char *path, struct got_object_id_queue *commits,
6316 bfce7f83 2019-07-27 stsp struct got_repository *repo)
6318 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
6319 0ebf8283 2019-07-24 stsp char *editor;
6320 0ebf8283 2019-07-24 stsp FILE *f = NULL;
6322 0ebf8283 2019-07-24 stsp err = get_editor(&editor);
6324 0ebf8283 2019-07-24 stsp return err;
6326 0ebf8283 2019-07-24 stsp if (spawn_editor(editor, path) == -1) {
6327 0ebf8283 2019-07-24 stsp err = got_error_from_errno("failed spawning editor");
6328 0ebf8283 2019-07-24 stsp goto done;
6331 0ebf8283 2019-07-24 stsp f = fopen(path, "r");
6332 0ebf8283 2019-07-24 stsp if (f == NULL) {
6333 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fopen");
6334 0ebf8283 2019-07-24 stsp goto done;
6336 0ebf8283 2019-07-24 stsp err = histedit_parse_list(histedit_cmds, f, repo);
6338 bfce7f83 2019-07-27 stsp goto done;
6340 bfce7f83 2019-07-27 stsp err = histedit_check_script(histedit_cmds, commits, repo);
6342 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
6343 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
6344 0ebf8283 2019-07-24 stsp free(editor);
6345 0ebf8283 2019-07-24 stsp return err;
6348 0ebf8283 2019-07-24 stsp static const struct got_error *
6349 bfce7f83 2019-07-27 stsp histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
6350 514f2ffe 2020-01-29 stsp struct got_object_id_queue *, const char *, const char *,
6351 514f2ffe 2020-01-29 stsp struct got_repository *);
6353 0ebf8283 2019-07-24 stsp static const struct got_error *
6354 0ebf8283 2019-07-24 stsp histedit_edit_script(struct got_histedit_list *histedit_cmds,
6355 514f2ffe 2020-01-29 stsp struct got_object_id_queue *commits, const char *branch_name,
6356 083957f4 2020-02-24 stsp int edit_logmsg_only, struct got_repository *repo)
6358 0ebf8283 2019-07-24 stsp const struct got_error *err;
6359 0ebf8283 2019-07-24 stsp FILE *f = NULL;
6360 0ebf8283 2019-07-24 stsp char *path = NULL;
6362 0ebf8283 2019-07-24 stsp err = got_opentemp_named(&path, &f, "got-histedit");
6364 0ebf8283 2019-07-24 stsp return err;
6366 514f2ffe 2020-01-29 stsp err = write_cmd_list(f, branch_name, commits);
6368 0ebf8283 2019-07-24 stsp goto done;
6370 083957f4 2020-02-24 stsp err = histedit_write_commit_list(commits, f, edit_logmsg_only, repo);
6372 0ebf8283 2019-07-24 stsp goto done;
6374 083957f4 2020-02-24 stsp if (edit_logmsg_only) {
6375 083957f4 2020-02-24 stsp rewind(f);
6376 083957f4 2020-02-24 stsp err = histedit_parse_list(histedit_cmds, f, repo);
6378 083957f4 2020-02-24 stsp if (fclose(f) != 0) {
6379 083957f4 2020-02-24 stsp err = got_error_from_errno("fclose");
6380 0ebf8283 2019-07-24 stsp goto done;
6383 083957f4 2020-02-24 stsp err = histedit_run_editor(histedit_cmds, path, commits, repo);
6384 083957f4 2020-02-24 stsp if (err) {
6385 083957f4 2020-02-24 stsp if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6386 083957f4 2020-02-24 stsp err->code != GOT_ERR_HISTEDIT_CMD)
6387 083957f4 2020-02-24 stsp goto done;
6388 083957f4 2020-02-24 stsp err = histedit_edit_list_retry(histedit_cmds, err,
6389 083957f4 2020-02-24 stsp commits, path, branch_name, repo);
6393 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
6394 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
6395 0ebf8283 2019-07-24 stsp if (path && unlink(path) != 0 && err == NULL)
6396 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("unlink", path);
6397 0ebf8283 2019-07-24 stsp free(path);
6398 0ebf8283 2019-07-24 stsp return err;
6401 0ebf8283 2019-07-24 stsp static const struct got_error *
6402 0ebf8283 2019-07-24 stsp histedit_save_list(struct got_histedit_list *histedit_cmds,
6403 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
6405 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
6406 0ebf8283 2019-07-24 stsp char *path = NULL;
6407 0ebf8283 2019-07-24 stsp FILE *f = NULL;
6408 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle;
6409 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
6411 c3022ba5 2019-07-27 stsp err = got_worktree_get_histedit_script_path(&path, worktree);
6413 0ebf8283 2019-07-24 stsp return err;
6415 0ebf8283 2019-07-24 stsp f = fopen(path, "w");
6416 0ebf8283 2019-07-24 stsp if (f == NULL) {
6417 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("fopen", path);
6418 0ebf8283 2019-07-24 stsp goto done;
6420 0ebf8283 2019-07-24 stsp TAILQ_FOREACH(hle, histedit_cmds, entry) {
6421 0ebf8283 2019-07-24 stsp err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
6426 0ebf8283 2019-07-24 stsp if (hle->logmsg) {
6427 0ebf8283 2019-07-24 stsp int n = fprintf(f, "%c %s\n",
6428 0ebf8283 2019-07-24 stsp GOT_HISTEDIT_MESG, hle->logmsg);
6429 0ebf8283 2019-07-24 stsp if (n < 0) {
6430 0ebf8283 2019-07-24 stsp err = got_ferror(f, GOT_ERR_IO);
6436 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
6437 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
6438 0ebf8283 2019-07-24 stsp free(path);
6439 0ebf8283 2019-07-24 stsp if (commit)
6440 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
6441 0ebf8283 2019-07-24 stsp return err;
6445 bfce7f83 2019-07-27 stsp histedit_free_list(struct got_histedit_list *histedit_cmds)
6447 bfce7f83 2019-07-27 stsp struct got_histedit_list_entry *hle;
6449 bfce7f83 2019-07-27 stsp while ((hle = TAILQ_FIRST(histedit_cmds))) {
6450 bfce7f83 2019-07-27 stsp TAILQ_REMOVE(histedit_cmds, hle, entry);
6451 bfce7f83 2019-07-27 stsp free(hle);
6455 0ebf8283 2019-07-24 stsp static const struct got_error *
6456 0ebf8283 2019-07-24 stsp histedit_load_list(struct got_histedit_list *histedit_cmds,
6457 0ebf8283 2019-07-24 stsp const char *path, struct got_repository *repo)
6459 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
6460 0ebf8283 2019-07-24 stsp FILE *f = NULL;
6462 0ebf8283 2019-07-24 stsp f = fopen(path, "r");
6463 0ebf8283 2019-07-24 stsp if (f == NULL) {
6464 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("fopen", path);
6465 0ebf8283 2019-07-24 stsp goto done;
6468 0ebf8283 2019-07-24 stsp err = histedit_parse_list(histedit_cmds, f, repo);
6470 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
6471 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
6472 0ebf8283 2019-07-24 stsp return err;
6475 0ebf8283 2019-07-24 stsp static const struct got_error *
6476 0ebf8283 2019-07-24 stsp histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
6477 bfce7f83 2019-07-27 stsp const struct got_error *edit_err, struct got_object_id_queue *commits,
6478 514f2ffe 2020-01-29 stsp const char *path, const char *branch_name, struct got_repository *repo)
6480 bfce7f83 2019-07-27 stsp const struct got_error *err = NULL, *prev_err = edit_err;
6481 0ebf8283 2019-07-24 stsp int resp = ' ';
6483 b006047e 2019-07-25 stsp while (resp != 'c' && resp != 'r' && resp != 'a') {
6484 0ebf8283 2019-07-24 stsp printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
6485 bfce7f83 2019-07-27 stsp "or (a)bort: ", getprogname(), prev_err->msg);
6486 0ebf8283 2019-07-24 stsp resp = getchar();
6487 a61a4414 2019-08-07 stsp if (resp == '\n')
6488 a61a4414 2019-08-07 stsp resp = getchar();
6489 426ebf2e 2019-07-25 stsp if (resp == 'c') {
6490 bfce7f83 2019-07-27 stsp histedit_free_list(histedit_cmds);
6491 bfce7f83 2019-07-27 stsp err = histedit_run_editor(histedit_cmds, path, commits,
6493 426ebf2e 2019-07-25 stsp if (err) {
6494 bfce7f83 2019-07-27 stsp if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6495 bfce7f83 2019-07-27 stsp err->code != GOT_ERR_HISTEDIT_CMD)
6497 bfce7f83 2019-07-27 stsp prev_err = err;
6498 426ebf2e 2019-07-25 stsp resp = ' ';
6502 426ebf2e 2019-07-25 stsp } else if (resp == 'r') {
6503 bfce7f83 2019-07-27 stsp histedit_free_list(histedit_cmds);
6504 0ebf8283 2019-07-24 stsp err = histedit_edit_script(histedit_cmds,
6505 083957f4 2020-02-24 stsp commits, branch_name, 0, repo);
6506 426ebf2e 2019-07-25 stsp if (err) {
6507 bfce7f83 2019-07-27 stsp if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6508 bfce7f83 2019-07-27 stsp err->code != GOT_ERR_HISTEDIT_CMD)
6510 bfce7f83 2019-07-27 stsp prev_err = err;
6511 426ebf2e 2019-07-25 stsp resp = ' ';
6515 426ebf2e 2019-07-25 stsp } else if (resp == 'a') {
6516 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_HISTEDIT_CANCEL);
6519 0ebf8283 2019-07-24 stsp printf("invalid response '%c'\n", resp);
6522 0ebf8283 2019-07-24 stsp return err;
6525 0ebf8283 2019-07-24 stsp static const struct got_error *
6526 0ebf8283 2019-07-24 stsp histedit_complete(struct got_worktree *worktree,
6527 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6528 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_repository *repo)
6530 0ebf8283 2019-07-24 stsp printf("Switching work tree to %s\n",
6531 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch));
6532 3e3a69f1 2019-07-25 stsp return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
6533 3e3a69f1 2019-07-25 stsp branch, repo);
6536 0ebf8283 2019-07-24 stsp static const struct got_error *
6537 0ebf8283 2019-07-24 stsp show_histedit_progress(struct got_commit_object *commit,
6538 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle, struct got_object_id *new_id)
6540 0ebf8283 2019-07-24 stsp const struct got_error *err;
6541 0ebf8283 2019-07-24 stsp char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
6543 0ebf8283 2019-07-24 stsp err = got_object_id_str(&old_id_str, hle->commit_id);
6545 0ebf8283 2019-07-24 stsp goto done;
6547 0ebf8283 2019-07-24 stsp if (new_id) {
6548 0ebf8283 2019-07-24 stsp err = got_object_id_str(&new_id_str, new_id);
6550 0ebf8283 2019-07-24 stsp goto done;
6553 0ebf8283 2019-07-24 stsp old_id_str[12] = '\0';
6554 0ebf8283 2019-07-24 stsp if (new_id_str)
6555 0ebf8283 2019-07-24 stsp new_id_str[12] = '\0';
6557 0ebf8283 2019-07-24 stsp if (hle->logmsg) {
6558 0ebf8283 2019-07-24 stsp logmsg = strdup(hle->logmsg);
6559 0ebf8283 2019-07-24 stsp if (logmsg == NULL) {
6560 0ebf8283 2019-07-24 stsp err = got_error_from_errno("strdup");
6561 0ebf8283 2019-07-24 stsp goto done;
6563 0ebf8283 2019-07-24 stsp trim_logmsg(logmsg, 42);
6565 0ebf8283 2019-07-24 stsp err = get_short_logmsg(&logmsg, 42, commit);
6567 0ebf8283 2019-07-24 stsp goto done;
6570 0ebf8283 2019-07-24 stsp switch (hle->cmd->code) {
6571 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_PICK:
6572 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_EDIT:
6573 0ebf8283 2019-07-24 stsp printf("%s -> %s: %s\n", old_id_str,
6574 0ebf8283 2019-07-24 stsp new_id_str ? new_id_str : "no-op change", logmsg);
6576 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_DROP:
6577 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_FOLD:
6578 0ebf8283 2019-07-24 stsp printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
6585 0ebf8283 2019-07-24 stsp free(old_id_str);
6586 0ebf8283 2019-07-24 stsp free(new_id_str);
6587 0ebf8283 2019-07-24 stsp return err;
6590 0ebf8283 2019-07-24 stsp static const struct got_error *
6591 0ebf8283 2019-07-24 stsp histedit_commit(struct got_pathlist_head *merged_paths,
6592 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
6593 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
6594 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
6596 0ebf8283 2019-07-24 stsp const struct got_error *err;
6597 0ebf8283 2019-07-24 stsp struct got_commit_object *commit;
6598 0ebf8283 2019-07-24 stsp struct got_object_id *new_commit_id;
6600 0ebf8283 2019-07-24 stsp if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
6601 0ebf8283 2019-07-24 stsp && hle->logmsg == NULL) {
6602 0ebf8283 2019-07-24 stsp err = histedit_edit_logmsg(hle, repo);
6604 0ebf8283 2019-07-24 stsp return err;
6607 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&commit, repo, hle->commit_id);
6609 0ebf8283 2019-07-24 stsp return err;
6611 0ebf8283 2019-07-24 stsp err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
6612 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, commit, hle->commit_id,
6613 3e3a69f1 2019-07-25 stsp hle->logmsg, repo);
6614 0ebf8283 2019-07-24 stsp if (err) {
6615 0ebf8283 2019-07-24 stsp if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
6616 0ebf8283 2019-07-24 stsp goto done;
6617 0ebf8283 2019-07-24 stsp err = show_histedit_progress(commit, hle, NULL);
6619 0ebf8283 2019-07-24 stsp err = show_histedit_progress(commit, hle, new_commit_id);
6620 0ebf8283 2019-07-24 stsp free(new_commit_id);
6623 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
6624 0ebf8283 2019-07-24 stsp return err;
6627 0ebf8283 2019-07-24 stsp static const struct got_error *
6628 0ebf8283 2019-07-24 stsp histedit_skip_commit(struct got_histedit_list_entry *hle,
6629 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
6631 0ebf8283 2019-07-24 stsp const struct got_error *error;
6632 0ebf8283 2019-07-24 stsp struct got_commit_object *commit;
6634 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
6636 0ebf8283 2019-07-24 stsp if (error)
6637 0ebf8283 2019-07-24 stsp return error;
6639 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo, hle->commit_id);
6640 0ebf8283 2019-07-24 stsp if (error)
6641 0ebf8283 2019-07-24 stsp return error;
6643 0ebf8283 2019-07-24 stsp error = show_histedit_progress(commit, hle, NULL);
6644 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
6645 0ebf8283 2019-07-24 stsp return error;
6648 ab20a43a 2020-01-29 stsp static const struct got_error *
6649 ab20a43a 2020-01-29 stsp check_local_changes(void *arg, unsigned char status,
6650 ab20a43a 2020-01-29 stsp unsigned char staged_status, const char *path,
6651 ab20a43a 2020-01-29 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
6652 ab20a43a 2020-01-29 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
6654 ab20a43a 2020-01-29 stsp int *have_local_changes = arg;
6656 ab20a43a 2020-01-29 stsp switch (status) {
6657 ab20a43a 2020-01-29 stsp case GOT_STATUS_ADD:
6658 ab20a43a 2020-01-29 stsp case GOT_STATUS_DELETE:
6659 ab20a43a 2020-01-29 stsp case GOT_STATUS_MODIFY:
6660 ab20a43a 2020-01-29 stsp case GOT_STATUS_CONFLICT:
6661 ab20a43a 2020-01-29 stsp *have_local_changes = 1;
6662 ab20a43a 2020-01-29 stsp return got_error(GOT_ERR_CANCELLED);
6667 ab20a43a 2020-01-29 stsp switch (staged_status) {
6668 ab20a43a 2020-01-29 stsp case GOT_STATUS_ADD:
6669 ab20a43a 2020-01-29 stsp case GOT_STATUS_DELETE:
6670 ab20a43a 2020-01-29 stsp case GOT_STATUS_MODIFY:
6671 ab20a43a 2020-01-29 stsp *have_local_changes = 1;
6672 ab20a43a 2020-01-29 stsp return got_error(GOT_ERR_CANCELLED);
6677 ab20a43a 2020-01-29 stsp return NULL;
6680 0ebf8283 2019-07-24 stsp static const struct got_error *
6681 0ebf8283 2019-07-24 stsp cmd_histedit(int argc, char *argv[])
6683 0ebf8283 2019-07-24 stsp const struct got_error *error = NULL;
6684 0ebf8283 2019-07-24 stsp struct got_worktree *worktree = NULL;
6685 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex = NULL;
6686 0ebf8283 2019-07-24 stsp struct got_repository *repo = NULL;
6687 0ebf8283 2019-07-24 stsp char *cwd = NULL;
6688 0ebf8283 2019-07-24 stsp struct got_reference *branch = NULL;
6689 0ebf8283 2019-07-24 stsp struct got_reference *tmp_branch = NULL;
6690 0ebf8283 2019-07-24 stsp struct got_object_id *resume_commit_id = NULL;
6691 0ebf8283 2019-07-24 stsp struct got_object_id *base_commit_id = NULL;
6692 0ebf8283 2019-07-24 stsp struct got_object_id *head_commit_id = NULL;
6693 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
6694 6ba2bea2 2019-07-27 stsp int ch, rebase_in_progress = 0, did_something;
6695 0ebf8283 2019-07-24 stsp int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
6696 083957f4 2020-02-24 stsp int edit_logmsg_only = 0;
6697 0ebf8283 2019-07-24 stsp const char *edit_script_path = NULL;
6698 0ebf8283 2019-07-24 stsp unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
6699 0ebf8283 2019-07-24 stsp struct got_object_id_queue commits;
6700 0ebf8283 2019-07-24 stsp struct got_pathlist_head merged_paths;
6701 0ebf8283 2019-07-24 stsp const struct got_object_id_queue *parent_ids;
6702 0ebf8283 2019-07-24 stsp struct got_object_qid *pid;
6703 0ebf8283 2019-07-24 stsp struct got_histedit_list histedit_cmds;
6704 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle;
6706 0ebf8283 2019-07-24 stsp SIMPLEQ_INIT(&commits);
6707 0ebf8283 2019-07-24 stsp TAILQ_INIT(&histedit_cmds);
6708 0ebf8283 2019-07-24 stsp TAILQ_INIT(&merged_paths);
6710 083957f4 2020-02-24 stsp while ((ch = getopt(argc, argv, "acF:m")) != -1) {
6711 0ebf8283 2019-07-24 stsp switch (ch) {
6713 0ebf8283 2019-07-24 stsp abort_edit = 1;
6716 0ebf8283 2019-07-24 stsp continue_edit = 1;
6719 0ebf8283 2019-07-24 stsp edit_script_path = optarg;
6722 083957f4 2020-02-24 stsp edit_logmsg_only = 1;
6725 0ebf8283 2019-07-24 stsp usage_histedit();
6726 0ebf8283 2019-07-24 stsp /* NOTREACHED */
6730 0ebf8283 2019-07-24 stsp argc -= optind;
6731 0ebf8283 2019-07-24 stsp argv += optind;
6733 0ebf8283 2019-07-24 stsp #ifndef PROFILE
6734 0ebf8283 2019-07-24 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6735 0ebf8283 2019-07-24 stsp "unveil", NULL) == -1)
6736 0ebf8283 2019-07-24 stsp err(1, "pledge");
6738 0ebf8283 2019-07-24 stsp if (abort_edit && continue_edit)
6739 083957f4 2020-02-24 stsp errx(1, "histedit's -a and -c options are mutually exclusive");
6740 083957f4 2020-02-24 stsp if (edit_script_path && edit_logmsg_only)
6741 083957f4 2020-02-24 stsp errx(1, "histedit's -F and -m options are mutually exclusive");
6742 083957f4 2020-02-24 stsp if (abort_edit && edit_logmsg_only)
6743 083957f4 2020-02-24 stsp errx(1, "histedit's -a and -m options are mutually exclusive");
6744 083957f4 2020-02-24 stsp if (continue_edit && edit_logmsg_only)
6745 083957f4 2020-02-24 stsp errx(1, "histedit's -c and -m options are mutually exclusive");
6746 0ebf8283 2019-07-24 stsp if (argc != 0)
6747 0ebf8283 2019-07-24 stsp usage_histedit();
6750 0ebf8283 2019-07-24 stsp * This command cannot apply unveil(2) in all cases because the
6751 0ebf8283 2019-07-24 stsp * user may choose to run an editor to edit the histedit script
6752 0ebf8283 2019-07-24 stsp * and to edit individual commit log messages.
6753 0ebf8283 2019-07-24 stsp * unveil(2) traverses exec(2); if an editor is used we have to
6754 0ebf8283 2019-07-24 stsp * apply unveil after edit script and log messages have been written.
6755 0ebf8283 2019-07-24 stsp * XXX TODO: Make use of unveil(2) where possible.
6758 0ebf8283 2019-07-24 stsp cwd = getcwd(NULL, 0);
6759 0ebf8283 2019-07-24 stsp if (cwd == NULL) {
6760 0ebf8283 2019-07-24 stsp error = got_error_from_errno("getcwd");
6761 0ebf8283 2019-07-24 stsp goto done;
6763 0ebf8283 2019-07-24 stsp error = got_worktree_open(&worktree, cwd);
6764 0ebf8283 2019-07-24 stsp if (error)
6765 0ebf8283 2019-07-24 stsp goto done;
6767 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6769 0ebf8283 2019-07-24 stsp if (error != NULL)
6770 0ebf8283 2019-07-24 stsp goto done;
6772 0ebf8283 2019-07-24 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
6773 0ebf8283 2019-07-24 stsp if (error)
6774 0ebf8283 2019-07-24 stsp goto done;
6775 0ebf8283 2019-07-24 stsp if (rebase_in_progress) {
6776 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_REBASING);
6777 0ebf8283 2019-07-24 stsp goto done;
6780 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
6781 0ebf8283 2019-07-24 stsp if (error)
6782 0ebf8283 2019-07-24 stsp goto done;
6784 083957f4 2020-02-24 stsp if (edit_in_progress && edit_logmsg_only) {
6785 083957f4 2020-02-24 stsp error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
6786 083957f4 2020-02-24 stsp "histedit operation is in progress in this "
6787 083957f4 2020-02-24 stsp "work tree and must be continued or aborted "
6788 083957f4 2020-02-24 stsp "before the -m option can be used");
6789 083957f4 2020-02-24 stsp goto done;
6792 0ebf8283 2019-07-24 stsp if (edit_in_progress && abort_edit) {
6793 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_continue(&resume_commit_id,
6794 3e3a69f1 2019-07-25 stsp &tmp_branch, &branch, &base_commit_id, &fileindex,
6795 3e3a69f1 2019-07-25 stsp worktree, repo);
6796 0ebf8283 2019-07-24 stsp if (error)
6797 0ebf8283 2019-07-24 stsp goto done;
6798 0ebf8283 2019-07-24 stsp printf("Switching work tree to %s\n",
6799 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch));
6800 3e3a69f1 2019-07-25 stsp error = got_worktree_histedit_abort(worktree, fileindex, repo,
6801 0ebf8283 2019-07-24 stsp branch, base_commit_id, update_progress, &did_something);
6802 0ebf8283 2019-07-24 stsp if (error)
6803 0ebf8283 2019-07-24 stsp goto done;
6804 0ebf8283 2019-07-24 stsp printf("Histedit of %s aborted\n",
6805 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch));
6806 0ebf8283 2019-07-24 stsp goto done; /* nothing else to do */
6807 0ebf8283 2019-07-24 stsp } else if (abort_edit) {
6808 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_NOT_HISTEDIT);
6809 0ebf8283 2019-07-24 stsp goto done;
6812 0ebf8283 2019-07-24 stsp if (continue_edit) {
6813 0ebf8283 2019-07-24 stsp char *path;
6815 0ebf8283 2019-07-24 stsp if (!edit_in_progress) {
6816 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_NOT_HISTEDIT);
6817 0ebf8283 2019-07-24 stsp goto done;
6820 c3022ba5 2019-07-27 stsp error = got_worktree_get_histedit_script_path(&path, worktree);
6821 0ebf8283 2019-07-24 stsp if (error)
6822 0ebf8283 2019-07-24 stsp goto done;
6824 0ebf8283 2019-07-24 stsp error = histedit_load_list(&histedit_cmds, path, repo);
6825 0ebf8283 2019-07-24 stsp free(path);
6826 0ebf8283 2019-07-24 stsp if (error)
6827 0ebf8283 2019-07-24 stsp goto done;
6829 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_continue(&resume_commit_id,
6830 3e3a69f1 2019-07-25 stsp &tmp_branch, &branch, &base_commit_id, &fileindex,
6831 3e3a69f1 2019-07-25 stsp worktree, repo);
6832 0ebf8283 2019-07-24 stsp if (error)
6833 0ebf8283 2019-07-24 stsp goto done;
6835 0ebf8283 2019-07-24 stsp error = got_ref_resolve(&head_commit_id, repo, branch);
6836 0ebf8283 2019-07-24 stsp if (error)
6837 0ebf8283 2019-07-24 stsp goto done;
6839 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo,
6840 0ebf8283 2019-07-24 stsp head_commit_id);
6841 0ebf8283 2019-07-24 stsp if (error)
6842 0ebf8283 2019-07-24 stsp goto done;
6843 0ebf8283 2019-07-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
6844 0ebf8283 2019-07-24 stsp pid = SIMPLEQ_FIRST(parent_ids);
6845 3aac7cf7 2019-07-25 stsp if (pid == NULL) {
6846 3aac7cf7 2019-07-25 stsp error = got_error(GOT_ERR_EMPTY_HISTEDIT);
6847 3aac7cf7 2019-07-25 stsp goto done;
6849 8ca9bd68 2019-07-25 stsp error = collect_commits(&commits, head_commit_id, pid->id,
6850 8ca9bd68 2019-07-25 stsp base_commit_id, got_worktree_get_path_prefix(worktree),
6851 8ca9bd68 2019-07-25 stsp GOT_ERR_HISTEDIT_PATH, repo);
6852 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
6853 0ebf8283 2019-07-24 stsp commit = NULL;
6854 0ebf8283 2019-07-24 stsp if (error)
6855 0ebf8283 2019-07-24 stsp goto done;
6857 0ebf8283 2019-07-24 stsp if (edit_in_progress) {
6858 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_HISTEDIT_BUSY);
6859 0ebf8283 2019-07-24 stsp goto done;
6862 0ebf8283 2019-07-24 stsp error = got_ref_open(&branch, repo,
6863 0ebf8283 2019-07-24 stsp got_worktree_get_head_ref_name(worktree), 0);
6864 0ebf8283 2019-07-24 stsp if (error != NULL)
6865 0ebf8283 2019-07-24 stsp goto done;
6867 c7d20a3f 2019-07-30 stsp if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
6868 c7d20a3f 2019-07-30 stsp error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
6869 c7d20a3f 2019-07-30 stsp "will not edit commit history of a branch outside "
6870 c7d20a3f 2019-07-30 stsp "the \"refs/heads/\" reference namespace");
6871 c7d20a3f 2019-07-30 stsp goto done;
6874 0ebf8283 2019-07-24 stsp error = got_ref_resolve(&head_commit_id, repo, branch);
6875 bfce7f83 2019-07-27 stsp got_ref_close(branch);
6876 bfce7f83 2019-07-27 stsp branch = NULL;
6877 0ebf8283 2019-07-24 stsp if (error)
6878 0ebf8283 2019-07-24 stsp goto done;
6880 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo,
6881 0ebf8283 2019-07-24 stsp head_commit_id);
6882 0ebf8283 2019-07-24 stsp if (error)
6883 0ebf8283 2019-07-24 stsp goto done;
6884 0ebf8283 2019-07-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
6885 0ebf8283 2019-07-24 stsp pid = SIMPLEQ_FIRST(parent_ids);
6886 3aac7cf7 2019-07-25 stsp if (pid == NULL) {
6887 3aac7cf7 2019-07-25 stsp error = got_error(GOT_ERR_EMPTY_HISTEDIT);
6888 3aac7cf7 2019-07-25 stsp goto done;
6890 8ca9bd68 2019-07-25 stsp error = collect_commits(&commits, head_commit_id, pid->id,
6891 8ca9bd68 2019-07-25 stsp got_worktree_get_base_commit_id(worktree),
6892 8ca9bd68 2019-07-25 stsp got_worktree_get_path_prefix(worktree),
6893 8ca9bd68 2019-07-25 stsp GOT_ERR_HISTEDIT_PATH, repo);
6894 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
6895 0ebf8283 2019-07-24 stsp commit = NULL;
6896 0ebf8283 2019-07-24 stsp if (error)
6897 0ebf8283 2019-07-24 stsp goto done;
6899 c5996fff 2020-01-29 stsp if (SIMPLEQ_EMPTY(&commits)) {
6900 c5996fff 2020-01-29 stsp error = got_error(GOT_ERR_EMPTY_HISTEDIT);
6901 c5996fff 2020-01-29 stsp goto done;
6904 bfce7f83 2019-07-27 stsp error = got_worktree_histedit_prepare(&tmp_branch, &branch,
6905 bfce7f83 2019-07-27 stsp &base_commit_id, &fileindex, worktree, repo);
6906 bfce7f83 2019-07-27 stsp if (error)
6907 bfce7f83 2019-07-27 stsp goto done;
6909 0ebf8283 2019-07-24 stsp if (edit_script_path) {
6910 0ebf8283 2019-07-24 stsp error = histedit_load_list(&histedit_cmds,
6911 0ebf8283 2019-07-24 stsp edit_script_path, repo);
6912 6ba2bea2 2019-07-27 stsp if (error) {
6913 6ba2bea2 2019-07-27 stsp got_worktree_histedit_abort(worktree, fileindex,
6914 6ba2bea2 2019-07-27 stsp repo, branch, base_commit_id,
6915 6ba2bea2 2019-07-27 stsp update_progress, &did_something);
6916 0ebf8283 2019-07-24 stsp goto done;
6919 514f2ffe 2020-01-29 stsp const char *branch_name;
6920 514f2ffe 2020-01-29 stsp branch_name = got_ref_get_symref_target(branch);
6921 514f2ffe 2020-01-29 stsp if (strncmp(branch_name, "refs/heads/", 11) == 0)
6922 514f2ffe 2020-01-29 stsp branch_name += 11;
6923 0ebf8283 2019-07-24 stsp error = histedit_edit_script(&histedit_cmds, &commits,
6924 083957f4 2020-02-24 stsp branch_name, edit_logmsg_only, repo);
6925 6ba2bea2 2019-07-27 stsp if (error) {
6926 6ba2bea2 2019-07-27 stsp got_worktree_histedit_abort(worktree, fileindex,
6927 6ba2bea2 2019-07-27 stsp repo, branch, base_commit_id,
6928 6ba2bea2 2019-07-27 stsp update_progress, &did_something);
6929 0ebf8283 2019-07-24 stsp goto done;
6934 0ebf8283 2019-07-24 stsp error = histedit_save_list(&histedit_cmds, worktree,
6936 6ba2bea2 2019-07-27 stsp if (error) {
6937 6ba2bea2 2019-07-27 stsp got_worktree_histedit_abort(worktree, fileindex,
6938 6ba2bea2 2019-07-27 stsp repo, branch, base_commit_id,
6939 6ba2bea2 2019-07-27 stsp update_progress, &did_something);
6940 0ebf8283 2019-07-24 stsp goto done;
6945 4ec14e60 2019-08-28 hiltjo error = histedit_check_script(&histedit_cmds, &commits, repo);
6946 4ec14e60 2019-08-28 hiltjo if (error)
6947 0ebf8283 2019-07-24 stsp goto done;
6949 0ebf8283 2019-07-24 stsp TAILQ_FOREACH(hle, &histedit_cmds, entry) {
6950 0ebf8283 2019-07-24 stsp if (resume_commit_id) {
6951 0ebf8283 2019-07-24 stsp if (got_object_id_cmp(hle->commit_id,
6952 0ebf8283 2019-07-24 stsp resume_commit_id) != 0)
6955 0ebf8283 2019-07-24 stsp resume_commit_id = NULL;
6956 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_DROP ||
6957 0ebf8283 2019-07-24 stsp hle->cmd->code == GOT_HISTEDIT_FOLD) {
6958 0ebf8283 2019-07-24 stsp error = histedit_skip_commit(hle, worktree,
6960 ab20a43a 2020-01-29 stsp if (error)
6961 ab20a43a 2020-01-29 stsp goto done;
6963 ab20a43a 2020-01-29 stsp struct got_pathlist_head paths;
6964 ab20a43a 2020-01-29 stsp int have_changes = 0;
6966 ab20a43a 2020-01-29 stsp TAILQ_INIT(&paths);
6967 ab20a43a 2020-01-29 stsp error = got_pathlist_append(&paths, "", NULL);
6968 ab20a43a 2020-01-29 stsp if (error)
6969 ab20a43a 2020-01-29 stsp goto done;
6970 ab20a43a 2020-01-29 stsp error = got_worktree_status(worktree, &paths,
6971 ab20a43a 2020-01-29 stsp repo, check_local_changes, &have_changes,
6972 ab20a43a 2020-01-29 stsp check_cancelled, NULL);
6973 ab20a43a 2020-01-29 stsp got_pathlist_free(&paths);
6974 ab20a43a 2020-01-29 stsp if (error) {
6975 ab20a43a 2020-01-29 stsp if (error->code != GOT_ERR_CANCELLED)
6976 ab20a43a 2020-01-29 stsp goto done;
6977 ab20a43a 2020-01-29 stsp if (sigint_received || sigpipe_received)
6978 ab20a43a 2020-01-29 stsp goto done;
6980 ab20a43a 2020-01-29 stsp if (have_changes) {
6981 ab20a43a 2020-01-29 stsp error = histedit_commit(NULL, worktree,
6982 ab20a43a 2020-01-29 stsp fileindex, tmp_branch, hle, repo);
6983 ab20a43a 2020-01-29 stsp if (error)
6984 ab20a43a 2020-01-29 stsp goto done;
6986 ab20a43a 2020-01-29 stsp error = got_object_open_as_commit(
6987 ab20a43a 2020-01-29 stsp &commit, repo, hle->commit_id);
6988 ab20a43a 2020-01-29 stsp if (error)
6989 ab20a43a 2020-01-29 stsp goto done;
6990 ab20a43a 2020-01-29 stsp error = show_histedit_progress(commit,
6991 ab20a43a 2020-01-29 stsp hle, NULL);
6992 ab20a43a 2020-01-29 stsp got_object_commit_close(commit);
6993 ab20a43a 2020-01-29 stsp commit = NULL;
6994 ab20a43a 2020-01-29 stsp if (error)
6995 ab20a43a 2020-01-29 stsp goto done;
7001 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_DROP) {
7002 0ebf8283 2019-07-24 stsp error = histedit_skip_commit(hle, worktree, repo);
7003 0ebf8283 2019-07-24 stsp if (error)
7004 0ebf8283 2019-07-24 stsp goto done;
7008 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo,
7009 0ebf8283 2019-07-24 stsp hle->commit_id);
7010 0ebf8283 2019-07-24 stsp if (error)
7011 0ebf8283 2019-07-24 stsp goto done;
7012 0ebf8283 2019-07-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
7013 0ebf8283 2019-07-24 stsp pid = SIMPLEQ_FIRST(parent_ids);
7015 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_merge_files(&merged_paths,
7016 3e3a69f1 2019-07-25 stsp worktree, fileindex, pid->id, hle->commit_id, repo,
7017 3e3a69f1 2019-07-25 stsp rebase_progress, &rebase_status, check_cancelled, NULL);
7018 0ebf8283 2019-07-24 stsp if (error)
7019 0ebf8283 2019-07-24 stsp goto done;
7020 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
7021 0ebf8283 2019-07-24 stsp commit = NULL;
7023 0ebf8283 2019-07-24 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
7024 a0ea4fc0 2020-02-28 stsp error = show_rebase_merge_conflict(hle->commit_id,
7026 a0ea4fc0 2020-02-28 stsp if (error)
7027 a0ea4fc0 2020-02-28 stsp goto done;
7028 0ebf8283 2019-07-24 stsp got_worktree_rebase_pathlist_free(&merged_paths);
7032 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
7033 0ebf8283 2019-07-24 stsp char *id_str;
7034 0ebf8283 2019-07-24 stsp error = got_object_id_str(&id_str, hle->commit_id);
7035 0ebf8283 2019-07-24 stsp if (error)
7036 0ebf8283 2019-07-24 stsp goto done;
7037 0ebf8283 2019-07-24 stsp printf("Stopping histedit for amending commit %s\n",
7039 0ebf8283 2019-07-24 stsp free(id_str);
7040 0ebf8283 2019-07-24 stsp got_worktree_rebase_pathlist_free(&merged_paths);
7041 3e3a69f1 2019-07-25 stsp error = got_worktree_histedit_postpone(worktree,
7042 3e3a69f1 2019-07-25 stsp fileindex);
7043 0ebf8283 2019-07-24 stsp goto done;
7046 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
7047 0ebf8283 2019-07-24 stsp error = histedit_skip_commit(hle, worktree, repo);
7048 0ebf8283 2019-07-24 stsp if (error)
7049 0ebf8283 2019-07-24 stsp goto done;
7053 3e3a69f1 2019-07-25 stsp error = histedit_commit(&merged_paths, worktree, fileindex,
7054 3e3a69f1 2019-07-25 stsp tmp_branch, hle, repo);
7055 0ebf8283 2019-07-24 stsp got_worktree_rebase_pathlist_free(&merged_paths);
7056 0ebf8283 2019-07-24 stsp if (error)
7057 0ebf8283 2019-07-24 stsp goto done;
7060 0ebf8283 2019-07-24 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
7061 3e3a69f1 2019-07-25 stsp error = got_worktree_histedit_postpone(worktree, fileindex);
7062 0ebf8283 2019-07-24 stsp if (error)
7063 0ebf8283 2019-07-24 stsp goto done;
7064 0ebf8283 2019-07-24 stsp error = got_error_msg(GOT_ERR_CONFLICTS,
7065 4cb8f8f3 2020-03-05 stsp "conflicts must be resolved before histedit can continue");
7067 3e3a69f1 2019-07-25 stsp error = histedit_complete(worktree, fileindex, tmp_branch,
7068 3e3a69f1 2019-07-25 stsp branch, repo);
7070 0ebf8283 2019-07-24 stsp got_object_id_queue_free(&commits);
7071 bfce7f83 2019-07-27 stsp histedit_free_list(&histedit_cmds);
7072 0ebf8283 2019-07-24 stsp free(head_commit_id);
7073 0ebf8283 2019-07-24 stsp free(base_commit_id);
7074 0ebf8283 2019-07-24 stsp free(resume_commit_id);
7075 0ebf8283 2019-07-24 stsp if (commit)
7076 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
7077 0ebf8283 2019-07-24 stsp if (branch)
7078 0ebf8283 2019-07-24 stsp got_ref_close(branch);
7079 0ebf8283 2019-07-24 stsp if (tmp_branch)
7080 0ebf8283 2019-07-24 stsp got_ref_close(tmp_branch);
7081 0ebf8283 2019-07-24 stsp if (worktree)
7082 0ebf8283 2019-07-24 stsp got_worktree_close(worktree);
7084 2822a352 2019-10-15 stsp got_repo_close(repo);
7085 2822a352 2019-10-15 stsp return error;
7088 2822a352 2019-10-15 stsp __dead static void
7089 2822a352 2019-10-15 stsp usage_integrate(void)
7091 2822a352 2019-10-15 stsp fprintf(stderr, "usage: %s integrate branch\n", getprogname());
7095 2822a352 2019-10-15 stsp static const struct got_error *
7096 2822a352 2019-10-15 stsp cmd_integrate(int argc, char *argv[])
7098 2822a352 2019-10-15 stsp const struct got_error *error = NULL;
7099 2822a352 2019-10-15 stsp struct got_repository *repo = NULL;
7100 2822a352 2019-10-15 stsp struct got_worktree *worktree = NULL;
7101 2822a352 2019-10-15 stsp char *cwd = NULL, *refname = NULL, *base_refname = NULL;
7102 2822a352 2019-10-15 stsp const char *branch_arg = NULL;
7103 2822a352 2019-10-15 stsp struct got_reference *branch_ref = NULL, *base_branch_ref = NULL;
7104 2822a352 2019-10-15 stsp struct got_fileindex *fileindex = NULL;
7105 2822a352 2019-10-15 stsp struct got_object_id *commit_id = NULL, *base_commit_id = NULL;
7106 2822a352 2019-10-15 stsp int ch, did_something = 0;
7108 2822a352 2019-10-15 stsp while ((ch = getopt(argc, argv, "")) != -1) {
7109 2822a352 2019-10-15 stsp switch (ch) {
7111 2822a352 2019-10-15 stsp usage_integrate();
7112 2822a352 2019-10-15 stsp /* NOTREACHED */
7116 2822a352 2019-10-15 stsp argc -= optind;
7117 2822a352 2019-10-15 stsp argv += optind;
7119 2822a352 2019-10-15 stsp if (argc != 1)
7120 2822a352 2019-10-15 stsp usage_integrate();
7121 2822a352 2019-10-15 stsp branch_arg = argv[0];
7123 2822a352 2019-10-15 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7124 2822a352 2019-10-15 stsp "unveil", NULL) == -1)
7125 2822a352 2019-10-15 stsp err(1, "pledge");
7127 2822a352 2019-10-15 stsp cwd = getcwd(NULL, 0);
7128 2822a352 2019-10-15 stsp if (cwd == NULL) {
7129 2822a352 2019-10-15 stsp error = got_error_from_errno("getcwd");
7130 2822a352 2019-10-15 stsp goto done;
7133 2822a352 2019-10-15 stsp error = got_worktree_open(&worktree, cwd);
7134 2822a352 2019-10-15 stsp if (error)
7135 2822a352 2019-10-15 stsp goto done;
7137 2822a352 2019-10-15 stsp error = check_rebase_or_histedit_in_progress(worktree);
7138 2822a352 2019-10-15 stsp if (error)
7139 2822a352 2019-10-15 stsp goto done;
7141 2822a352 2019-10-15 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7143 2822a352 2019-10-15 stsp if (error != NULL)
7144 2822a352 2019-10-15 stsp goto done;
7146 2822a352 2019-10-15 stsp error = apply_unveil(got_repo_get_path(repo), 0,
7147 2822a352 2019-10-15 stsp got_worktree_get_root_path(worktree));
7148 2822a352 2019-10-15 stsp if (error)
7149 2822a352 2019-10-15 stsp goto done;
7151 2822a352 2019-10-15 stsp if (asprintf(&refname, "refs/heads/%s", branch_arg) == -1) {
7152 2822a352 2019-10-15 stsp error = got_error_from_errno("asprintf");
7153 2822a352 2019-10-15 stsp goto done;
7156 2822a352 2019-10-15 stsp error = got_worktree_integrate_prepare(&fileindex, &branch_ref,
7157 2822a352 2019-10-15 stsp &base_branch_ref, worktree, refname, repo);
7158 2822a352 2019-10-15 stsp if (error)
7159 2822a352 2019-10-15 stsp goto done;
7161 2822a352 2019-10-15 stsp refname = strdup(got_ref_get_name(branch_ref));
7162 2822a352 2019-10-15 stsp if (refname == NULL) {
7163 2822a352 2019-10-15 stsp error = got_error_from_errno("strdup");
7164 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
7165 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
7166 2822a352 2019-10-15 stsp goto done;
7168 2822a352 2019-10-15 stsp base_refname = strdup(got_ref_get_name(base_branch_ref));
7169 2822a352 2019-10-15 stsp if (base_refname == NULL) {
7170 2822a352 2019-10-15 stsp error = got_error_from_errno("strdup");
7171 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
7172 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
7173 2822a352 2019-10-15 stsp goto done;
7176 2822a352 2019-10-15 stsp error = got_ref_resolve(&commit_id, repo, branch_ref);
7177 2822a352 2019-10-15 stsp if (error)
7178 2822a352 2019-10-15 stsp goto done;
7180 2822a352 2019-10-15 stsp error = got_ref_resolve(&base_commit_id, repo, base_branch_ref);
7181 2822a352 2019-10-15 stsp if (error)
7182 2822a352 2019-10-15 stsp goto done;
7184 2822a352 2019-10-15 stsp if (got_object_id_cmp(commit_id, base_commit_id) == 0) {
7185 2822a352 2019-10-15 stsp error = got_error_msg(GOT_ERR_SAME_BRANCH,
7186 2822a352 2019-10-15 stsp "specified branch has already been integrated");
7187 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
7188 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
7189 2822a352 2019-10-15 stsp goto done;
7192 3aef623b 2019-10-15 stsp error = check_linear_ancestry(commit_id, base_commit_id, 1, repo);
7193 2822a352 2019-10-15 stsp if (error) {
7194 2822a352 2019-10-15 stsp if (error->code == GOT_ERR_ANCESTRY)
7195 2822a352 2019-10-15 stsp error = got_error(GOT_ERR_REBASE_REQUIRED);
7196 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
7197 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
7198 2822a352 2019-10-15 stsp goto done;
7201 2822a352 2019-10-15 stsp error = got_worktree_integrate_continue(worktree, fileindex, repo,
7202 2822a352 2019-10-15 stsp branch_ref, base_branch_ref, update_progress, &did_something,
7203 2822a352 2019-10-15 stsp check_cancelled, NULL);
7204 2822a352 2019-10-15 stsp if (error)
7205 2822a352 2019-10-15 stsp goto done;
7207 2822a352 2019-10-15 stsp printf("Integrated %s into %s\n", refname, base_refname);
7210 715dc77e 2019-08-03 stsp got_repo_close(repo);
7211 2822a352 2019-10-15 stsp if (worktree)
7212 2822a352 2019-10-15 stsp got_worktree_close(worktree);
7213 2822a352 2019-10-15 stsp free(cwd);
7214 2822a352 2019-10-15 stsp free(base_commit_id);
7215 2822a352 2019-10-15 stsp free(commit_id);
7216 2822a352 2019-10-15 stsp free(refname);
7217 2822a352 2019-10-15 stsp free(base_refname);
7218 715dc77e 2019-08-03 stsp return error;
7221 715dc77e 2019-08-03 stsp __dead static void
7222 715dc77e 2019-08-03 stsp usage_stage(void)
7224 f3055044 2019-08-07 stsp fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
7225 f3055044 2019-08-07 stsp "[file-path ...]\n",
7226 715dc77e 2019-08-03 stsp getprogname());
7230 715dc77e 2019-08-03 stsp static const struct got_error *
7231 408b4ebc 2019-08-03 stsp print_stage(void *arg, unsigned char status, unsigned char staged_status,
7232 408b4ebc 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
7233 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
7234 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
7236 408b4ebc 2019-08-03 stsp const struct got_error *err = NULL;
7237 408b4ebc 2019-08-03 stsp char *id_str = NULL;
7239 408b4ebc 2019-08-03 stsp if (staged_status != GOT_STATUS_ADD &&
7240 408b4ebc 2019-08-03 stsp staged_status != GOT_STATUS_MODIFY &&
7241 408b4ebc 2019-08-03 stsp staged_status != GOT_STATUS_DELETE)
7242 408b4ebc 2019-08-03 stsp return NULL;
7244 408b4ebc 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
7245 408b4ebc 2019-08-03 stsp staged_status == GOT_STATUS_MODIFY)
7246 408b4ebc 2019-08-03 stsp err = got_object_id_str(&id_str, staged_blob_id);
7248 408b4ebc 2019-08-03 stsp err = got_object_id_str(&id_str, blob_id);
7250 408b4ebc 2019-08-03 stsp return err;
7252 408b4ebc 2019-08-03 stsp printf("%s %c %s\n", id_str, staged_status, path);
7253 408b4ebc 2019-08-03 stsp free(id_str);
7254 dc424a06 2019-08-07 stsp return NULL;
7257 dc424a06 2019-08-07 stsp static const struct got_error *
7258 715dc77e 2019-08-03 stsp cmd_stage(int argc, char *argv[])
7260 715dc77e 2019-08-03 stsp const struct got_error *error = NULL;
7261 715dc77e 2019-08-03 stsp struct got_repository *repo = NULL;
7262 715dc77e 2019-08-03 stsp struct got_worktree *worktree = NULL;
7263 715dc77e 2019-08-03 stsp char *cwd = NULL;
7264 715dc77e 2019-08-03 stsp struct got_pathlist_head paths;
7265 715dc77e 2019-08-03 stsp struct got_pathlist_entry *pe;
7266 dc424a06 2019-08-07 stsp int ch, list_stage = 0, pflag = 0;
7267 dc424a06 2019-08-07 stsp FILE *patch_script_file = NULL;
7268 2e1f37b0 2019-08-08 stsp const char *patch_script_path = NULL;
7269 2e1f37b0 2019-08-08 stsp struct choose_patch_arg cpa;
7271 715dc77e 2019-08-03 stsp TAILQ_INIT(&paths);
7273 dc424a06 2019-08-07 stsp while ((ch = getopt(argc, argv, "lpF:")) != -1) {
7274 715dc77e 2019-08-03 stsp switch (ch) {
7276 408b4ebc 2019-08-03 stsp list_stage = 1;
7279 dc424a06 2019-08-07 stsp pflag = 1;
7282 dc424a06 2019-08-07 stsp patch_script_path = optarg;
7285 715dc77e 2019-08-03 stsp usage_stage();
7286 715dc77e 2019-08-03 stsp /* NOTREACHED */
7290 715dc77e 2019-08-03 stsp argc -= optind;
7291 715dc77e 2019-08-03 stsp argv += optind;
7293 715dc77e 2019-08-03 stsp #ifndef PROFILE
7294 715dc77e 2019-08-03 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7295 715dc77e 2019-08-03 stsp "unveil", NULL) == -1)
7296 715dc77e 2019-08-03 stsp err(1, "pledge");
7298 2db2652d 2019-08-07 stsp if (list_stage && (pflag || patch_script_path))
7299 2db2652d 2019-08-07 stsp errx(1, "-l option cannot be used with other options");
7300 dc424a06 2019-08-07 stsp if (patch_script_path && !pflag)
7301 dc424a06 2019-08-07 stsp errx(1, "-F option can only be used together with -p option");
7303 715dc77e 2019-08-03 stsp cwd = getcwd(NULL, 0);
7304 715dc77e 2019-08-03 stsp if (cwd == NULL) {
7305 715dc77e 2019-08-03 stsp error = got_error_from_errno("getcwd");
7306 715dc77e 2019-08-03 stsp goto done;
7309 715dc77e 2019-08-03 stsp error = got_worktree_open(&worktree, cwd);
7310 715dc77e 2019-08-03 stsp if (error)
7311 715dc77e 2019-08-03 stsp goto done;
7313 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7315 715dc77e 2019-08-03 stsp if (error != NULL)
7316 715dc77e 2019-08-03 stsp goto done;
7318 dc424a06 2019-08-07 stsp if (patch_script_path) {
7319 dc424a06 2019-08-07 stsp patch_script_file = fopen(patch_script_path, "r");
7320 dc424a06 2019-08-07 stsp if (patch_script_file == NULL) {
7321 dc424a06 2019-08-07 stsp error = got_error_from_errno2("fopen",
7322 dc424a06 2019-08-07 stsp patch_script_path);
7323 dc424a06 2019-08-07 stsp goto done;
7326 9fd7cd22 2019-08-30 stsp error = apply_unveil(got_repo_get_path(repo), 0,
7327 715dc77e 2019-08-03 stsp got_worktree_get_root_path(worktree));
7328 715dc77e 2019-08-03 stsp if (error)
7329 715dc77e 2019-08-03 stsp goto done;
7331 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7332 6d022e97 2019-08-04 stsp if (error)
7333 6d022e97 2019-08-04 stsp goto done;
7335 6d022e97 2019-08-04 stsp if (list_stage)
7336 408b4ebc 2019-08-03 stsp error = got_worktree_status(worktree, &paths, repo,
7337 408b4ebc 2019-08-03 stsp print_stage, NULL, check_cancelled, NULL);
7339 2e1f37b0 2019-08-08 stsp cpa.patch_script_file = patch_script_file;
7340 2e1f37b0 2019-08-08 stsp cpa.action = "stage";
7341 dc424a06 2019-08-07 stsp error = got_worktree_stage(worktree, &paths,
7342 dc424a06 2019-08-07 stsp pflag ? NULL : print_status, NULL,
7343 2e1f37b0 2019-08-08 stsp pflag ? choose_patch : NULL, &cpa, repo);
7346 2e1f37b0 2019-08-08 stsp if (patch_script_file && fclose(patch_script_file) == EOF &&
7347 2e1f37b0 2019-08-08 stsp error == NULL)
7348 2e1f37b0 2019-08-08 stsp error = got_error_from_errno2("fclose", patch_script_path);
7350 ad493afc 2019-08-03 stsp got_repo_close(repo);
7351 ad493afc 2019-08-03 stsp if (worktree)
7352 ad493afc 2019-08-03 stsp got_worktree_close(worktree);
7353 ad493afc 2019-08-03 stsp TAILQ_FOREACH(pe, &paths, entry)
7354 ad493afc 2019-08-03 stsp free((char *)pe->path);
7355 ad493afc 2019-08-03 stsp got_pathlist_free(&paths);
7356 ad493afc 2019-08-03 stsp free(cwd);
7357 ad493afc 2019-08-03 stsp return error;
7360 ad493afc 2019-08-03 stsp __dead static void
7361 ad493afc 2019-08-03 stsp usage_unstage(void)
7363 2e1f37b0 2019-08-08 stsp fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
7364 2e1f37b0 2019-08-08 stsp "[file-path ...]\n",
7365 ad493afc 2019-08-03 stsp getprogname());
7370 ad493afc 2019-08-03 stsp static const struct got_error *
7371 ad493afc 2019-08-03 stsp cmd_unstage(int argc, char *argv[])
7373 ad493afc 2019-08-03 stsp const struct got_error *error = NULL;
7374 ad493afc 2019-08-03 stsp struct got_repository *repo = NULL;
7375 ad493afc 2019-08-03 stsp struct got_worktree *worktree = NULL;
7376 ad493afc 2019-08-03 stsp char *cwd = NULL;
7377 ad493afc 2019-08-03 stsp struct got_pathlist_head paths;
7378 ad493afc 2019-08-03 stsp struct got_pathlist_entry *pe;
7379 2e1f37b0 2019-08-08 stsp int ch, did_something = 0, pflag = 0;
7380 2e1f37b0 2019-08-08 stsp FILE *patch_script_file = NULL;
7381 2e1f37b0 2019-08-08 stsp const char *patch_script_path = NULL;
7382 2e1f37b0 2019-08-08 stsp struct choose_patch_arg cpa;
7384 ad493afc 2019-08-03 stsp TAILQ_INIT(&paths);
7386 2e1f37b0 2019-08-08 stsp while ((ch = getopt(argc, argv, "pF:")) != -1) {
7387 ad493afc 2019-08-03 stsp switch (ch) {
7389 2e1f37b0 2019-08-08 stsp pflag = 1;
7392 2e1f37b0 2019-08-08 stsp patch_script_path = optarg;
7395 ad493afc 2019-08-03 stsp usage_unstage();
7396 ad493afc 2019-08-03 stsp /* NOTREACHED */
7400 ad493afc 2019-08-03 stsp argc -= optind;
7401 ad493afc 2019-08-03 stsp argv += optind;
7403 ad493afc 2019-08-03 stsp #ifndef PROFILE
7404 ad493afc 2019-08-03 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7405 ad493afc 2019-08-03 stsp "unveil", NULL) == -1)
7406 ad493afc 2019-08-03 stsp err(1, "pledge");
7408 2e1f37b0 2019-08-08 stsp if (patch_script_path && !pflag)
7409 2e1f37b0 2019-08-08 stsp errx(1, "-F option can only be used together with -p option");
7411 ad493afc 2019-08-03 stsp cwd = getcwd(NULL, 0);
7412 ad493afc 2019-08-03 stsp if (cwd == NULL) {
7413 ad493afc 2019-08-03 stsp error = got_error_from_errno("getcwd");
7414 ad493afc 2019-08-03 stsp goto done;
7417 ad493afc 2019-08-03 stsp error = got_worktree_open(&worktree, cwd);
7418 ad493afc 2019-08-03 stsp if (error)
7419 ad493afc 2019-08-03 stsp goto done;
7421 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7423 ad493afc 2019-08-03 stsp if (error != NULL)
7424 ad493afc 2019-08-03 stsp goto done;
7426 2e1f37b0 2019-08-08 stsp if (patch_script_path) {
7427 2e1f37b0 2019-08-08 stsp patch_script_file = fopen(patch_script_path, "r");
7428 2e1f37b0 2019-08-08 stsp if (patch_script_file == NULL) {
7429 2e1f37b0 2019-08-08 stsp error = got_error_from_errno2("fopen",
7430 2e1f37b0 2019-08-08 stsp patch_script_path);
7431 2e1f37b0 2019-08-08 stsp goto done;
7435 00f36e47 2019-09-06 stsp error = apply_unveil(got_repo_get_path(repo), 0,
7436 ad493afc 2019-08-03 stsp got_worktree_get_root_path(worktree));
7437 ad493afc 2019-08-03 stsp if (error)
7438 ad493afc 2019-08-03 stsp goto done;
7440 ad493afc 2019-08-03 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7441 ad493afc 2019-08-03 stsp if (error)
7442 ad493afc 2019-08-03 stsp goto done;
7444 2e1f37b0 2019-08-08 stsp cpa.patch_script_file = patch_script_file;
7445 2e1f37b0 2019-08-08 stsp cpa.action = "unstage";
7446 ad493afc 2019-08-03 stsp error = got_worktree_unstage(worktree, &paths, update_progress,
7447 2e1f37b0 2019-08-08 stsp &did_something, pflag ? choose_patch : NULL, &cpa, repo);
7449 2e1f37b0 2019-08-08 stsp if (patch_script_file && fclose(patch_script_file) == EOF &&
7450 2e1f37b0 2019-08-08 stsp error == NULL)
7451 2e1f37b0 2019-08-08 stsp error = got_error_from_errno2("fclose", patch_script_path);
7453 0ebf8283 2019-07-24 stsp got_repo_close(repo);
7454 715dc77e 2019-08-03 stsp if (worktree)
7455 715dc77e 2019-08-03 stsp got_worktree_close(worktree);
7456 715dc77e 2019-08-03 stsp TAILQ_FOREACH(pe, &paths, entry)
7457 715dc77e 2019-08-03 stsp free((char *)pe->path);
7458 715dc77e 2019-08-03 stsp got_pathlist_free(&paths);
7459 715dc77e 2019-08-03 stsp free(cwd);
7460 01073a5d 2019-08-22 stsp return error;
7463 01073a5d 2019-08-22 stsp __dead static void
7464 01073a5d 2019-08-22 stsp usage_cat(void)
7466 896e9b6f 2019-08-26 stsp fprintf(stderr, "usage: %s cat [-r repository ] [ -c commit ] [ -P ] "
7467 896e9b6f 2019-08-26 stsp "arg1 [arg2 ...]\n", getprogname());
7471 01073a5d 2019-08-22 stsp static const struct got_error *
7472 01073a5d 2019-08-22 stsp cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7474 01073a5d 2019-08-22 stsp const struct got_error *err;
7475 01073a5d 2019-08-22 stsp struct got_blob_object *blob;
7477 01073a5d 2019-08-22 stsp err = got_object_open_as_blob(&blob, repo, id, 8192);
7479 01073a5d 2019-08-22 stsp return err;
7481 01073a5d 2019-08-22 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
7482 01073a5d 2019-08-22 stsp got_object_blob_close(blob);
7483 01073a5d 2019-08-22 stsp return err;
7486 01073a5d 2019-08-22 stsp static const struct got_error *
7487 01073a5d 2019-08-22 stsp cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7489 01073a5d 2019-08-22 stsp const struct got_error *err;
7490 01073a5d 2019-08-22 stsp struct got_tree_object *tree;
7491 56e0773d 2019-11-28 stsp int nentries, i;
7493 01073a5d 2019-08-22 stsp err = got_object_open_as_tree(&tree, repo, id);
7495 01073a5d 2019-08-22 stsp return err;
7497 56e0773d 2019-11-28 stsp nentries = got_object_tree_get_nentries(tree);
7498 56e0773d 2019-11-28 stsp for (i = 0; i < nentries; i++) {
7499 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
7500 01073a5d 2019-08-22 stsp char *id_str;
7501 01073a5d 2019-08-22 stsp if (sigint_received || sigpipe_received)
7503 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(tree, i);
7504 56e0773d 2019-11-28 stsp err = got_object_id_str(&id_str, got_tree_entry_get_id(te));
7507 56e0773d 2019-11-28 stsp fprintf(outfile, "%s %.7o %s\n", id_str,
7508 56e0773d 2019-11-28 stsp got_tree_entry_get_mode(te),
7509 56e0773d 2019-11-28 stsp got_tree_entry_get_name(te));
7510 01073a5d 2019-08-22 stsp free(id_str);
7513 01073a5d 2019-08-22 stsp got_object_tree_close(tree);
7514 01073a5d 2019-08-22 stsp return err;
7517 01073a5d 2019-08-22 stsp static const struct got_error *
7518 01073a5d 2019-08-22 stsp cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7520 01073a5d 2019-08-22 stsp const struct got_error *err;
7521 01073a5d 2019-08-22 stsp struct got_commit_object *commit;
7522 01073a5d 2019-08-22 stsp const struct got_object_id_queue *parent_ids;
7523 01073a5d 2019-08-22 stsp struct got_object_qid *pid;
7524 01073a5d 2019-08-22 stsp char *id_str = NULL;
7525 24ea5512 2019-08-22 stsp const char *logmsg = NULL;
7527 01073a5d 2019-08-22 stsp err = got_object_open_as_commit(&commit, repo, id);
7529 01073a5d 2019-08-22 stsp return err;
7531 01073a5d 2019-08-22 stsp err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
7533 01073a5d 2019-08-22 stsp goto done;
7535 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str);
7536 01073a5d 2019-08-22 stsp parent_ids = got_object_commit_get_parent_ids(commit);
7537 8aa93786 2019-08-22 stsp fprintf(outfile, "numparents %d\n",
7538 01073a5d 2019-08-22 stsp got_object_commit_get_nparents(commit));
7539 01073a5d 2019-08-22 stsp SIMPLEQ_FOREACH(pid, parent_ids, entry) {
7540 01073a5d 2019-08-22 stsp char *pid_str;
7541 01073a5d 2019-08-22 stsp err = got_object_id_str(&pid_str, pid->id);
7543 01073a5d 2019-08-22 stsp goto done;
7544 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_PARENT, pid_str);
7545 01073a5d 2019-08-22 stsp free(pid_str);
7547 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_AUTHOR,
7548 8aa93786 2019-08-22 stsp got_object_commit_get_author(commit),
7549 01073a5d 2019-08-22 stsp got_object_commit_get_author_time(commit));
7551 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_COMMITTER,
7552 8aa93786 2019-08-22 stsp got_object_commit_get_author(commit),
7553 01073a5d 2019-08-22 stsp got_object_commit_get_committer_time(commit));
7555 24ea5512 2019-08-22 stsp logmsg = got_object_commit_get_logmsg_raw(commit);
7556 8aa93786 2019-08-22 stsp fprintf(outfile, "messagelen %zd\n", strlen(logmsg));
7557 01073a5d 2019-08-22 stsp fprintf(outfile, "%s", logmsg);
7559 01073a5d 2019-08-22 stsp free(id_str);
7560 01073a5d 2019-08-22 stsp got_object_commit_close(commit);
7561 01073a5d 2019-08-22 stsp return err;
7564 01073a5d 2019-08-22 stsp static const struct got_error *
7565 01073a5d 2019-08-22 stsp cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7567 01073a5d 2019-08-22 stsp const struct got_error *err;
7568 01073a5d 2019-08-22 stsp struct got_tag_object *tag;
7569 01073a5d 2019-08-22 stsp char *id_str = NULL;
7570 01073a5d 2019-08-22 stsp const char *tagmsg = NULL;
7572 01073a5d 2019-08-22 stsp err = got_object_open_as_tag(&tag, repo, id);
7574 01073a5d 2019-08-22 stsp return err;
7576 01073a5d 2019-08-22 stsp err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
7578 01073a5d 2019-08-22 stsp goto done;
7580 e15d5241 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str);
7582 01073a5d 2019-08-22 stsp switch (got_object_tag_get_object_type(tag)) {
7583 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_BLOB:
7584 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7585 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_BLOB);
7587 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TREE:
7588 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7589 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_TREE);
7591 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_COMMIT:
7592 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7593 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_COMMIT);
7595 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TAG:
7596 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7597 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_TAG);
7603 e15d5241 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TAG,
7604 e15d5241 2019-08-22 stsp got_object_tag_get_name(tag));
7606 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s %lld +0000\n", GOT_TAG_LABEL_TAGGER,
7607 8aa93786 2019-08-22 stsp got_object_tag_get_tagger(tag),
7608 8aa93786 2019-08-22 stsp got_object_tag_get_tagger_time(tag));
7610 01073a5d 2019-08-22 stsp tagmsg = got_object_tag_get_message(tag);
7611 8aa93786 2019-08-22 stsp fprintf(outfile, "messagelen %zd\n", strlen(tagmsg));
7612 01073a5d 2019-08-22 stsp fprintf(outfile, "%s", tagmsg);
7614 01073a5d 2019-08-22 stsp free(id_str);
7615 01073a5d 2019-08-22 stsp got_object_tag_close(tag);
7616 01073a5d 2019-08-22 stsp return err;
7619 01073a5d 2019-08-22 stsp static const struct got_error *
7620 01073a5d 2019-08-22 stsp cmd_cat(int argc, char *argv[])
7622 01073a5d 2019-08-22 stsp const struct got_error *error;
7623 01073a5d 2019-08-22 stsp struct got_repository *repo = NULL;
7624 01073a5d 2019-08-22 stsp struct got_worktree *worktree = NULL;
7625 01073a5d 2019-08-22 stsp char *cwd = NULL, *repo_path = NULL, *label = NULL;
7626 896e9b6f 2019-08-26 stsp const char *commit_id_str = NULL;
7627 896e9b6f 2019-08-26 stsp struct got_object_id *id = NULL, *commit_id = NULL;
7628 896e9b6f 2019-08-26 stsp int ch, obj_type, i, force_path = 0;
7630 01073a5d 2019-08-22 stsp #ifndef PROFILE
7631 01073a5d 2019-08-22 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
7632 01073a5d 2019-08-22 stsp NULL) == -1)
7633 01073a5d 2019-08-22 stsp err(1, "pledge");
7636 896e9b6f 2019-08-26 stsp while ((ch = getopt(argc, argv, "c:r:P")) != -1) {
7637 01073a5d 2019-08-22 stsp switch (ch) {
7639 896e9b6f 2019-08-26 stsp commit_id_str = optarg;
7642 01073a5d 2019-08-22 stsp repo_path = realpath(optarg, NULL);
7643 01073a5d 2019-08-22 stsp if (repo_path == NULL)
7644 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
7646 01073a5d 2019-08-22 stsp got_path_strip_trailing_slashes(repo_path);
7649 896e9b6f 2019-08-26 stsp force_path = 1;
7652 01073a5d 2019-08-22 stsp usage_cat();
7653 01073a5d 2019-08-22 stsp /* NOTREACHED */
7657 01073a5d 2019-08-22 stsp argc -= optind;
7658 01073a5d 2019-08-22 stsp argv += optind;
7660 01073a5d 2019-08-22 stsp cwd = getcwd(NULL, 0);
7661 01073a5d 2019-08-22 stsp if (cwd == NULL) {
7662 01073a5d 2019-08-22 stsp error = got_error_from_errno("getcwd");
7663 01073a5d 2019-08-22 stsp goto done;
7665 01073a5d 2019-08-22 stsp error = got_worktree_open(&worktree, cwd);
7666 01073a5d 2019-08-22 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
7667 01073a5d 2019-08-22 stsp goto done;
7668 01073a5d 2019-08-22 stsp if (worktree) {
7669 01073a5d 2019-08-22 stsp if (repo_path == NULL) {
7670 01073a5d 2019-08-22 stsp repo_path = strdup(
7671 01073a5d 2019-08-22 stsp got_worktree_get_repo_path(worktree));
7672 01073a5d 2019-08-22 stsp if (repo_path == NULL) {
7673 01073a5d 2019-08-22 stsp error = got_error_from_errno("strdup");
7674 01073a5d 2019-08-22 stsp goto done;
7679 01073a5d 2019-08-22 stsp if (repo_path == NULL) {
7680 01073a5d 2019-08-22 stsp repo_path = getcwd(NULL, 0);
7681 01073a5d 2019-08-22 stsp if (repo_path == NULL)
7682 01073a5d 2019-08-22 stsp return got_error_from_errno("getcwd");
7685 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
7686 01073a5d 2019-08-22 stsp free(repo_path);
7687 01073a5d 2019-08-22 stsp if (error != NULL)
7688 01073a5d 2019-08-22 stsp goto done;
7690 01073a5d 2019-08-22 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
7691 896e9b6f 2019-08-26 stsp if (error)
7692 896e9b6f 2019-08-26 stsp goto done;
7694 896e9b6f 2019-08-26 stsp if (commit_id_str == NULL)
7695 896e9b6f 2019-08-26 stsp commit_id_str = GOT_REF_HEAD;
7696 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
7697 71a27632 2020-01-15 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
7698 01073a5d 2019-08-22 stsp if (error)
7699 01073a5d 2019-08-22 stsp goto done;
7701 01073a5d 2019-08-22 stsp for (i = 0; i < argc; i++) {
7702 896e9b6f 2019-08-26 stsp if (force_path) {
7703 896e9b6f 2019-08-26 stsp error = got_object_id_by_path(&id, repo, commit_id,
7705 896e9b6f 2019-08-26 stsp if (error)
7708 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&id, &label, argv[i],
7709 896e9b6f 2019-08-26 stsp GOT_OBJ_TYPE_ANY, 0, repo);
7710 896e9b6f 2019-08-26 stsp if (error) {
7711 896e9b6f 2019-08-26 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR &&
7712 896e9b6f 2019-08-26 stsp error->code != GOT_ERR_NOT_REF)
7714 896e9b6f 2019-08-26 stsp error = got_object_id_by_path(&id, repo,
7715 896e9b6f 2019-08-26 stsp commit_id, argv[i]);
7716 896e9b6f 2019-08-26 stsp if (error)
7721 01073a5d 2019-08-22 stsp error = got_object_get_type(&obj_type, repo, id);
7722 01073a5d 2019-08-22 stsp if (error)
7725 01073a5d 2019-08-22 stsp switch (obj_type) {
7726 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_BLOB:
7727 01073a5d 2019-08-22 stsp error = cat_blob(id, repo, stdout);
7729 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TREE:
7730 01073a5d 2019-08-22 stsp error = cat_tree(id, repo, stdout);
7732 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_COMMIT:
7733 01073a5d 2019-08-22 stsp error = cat_commit(id, repo, stdout);
7735 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TAG:
7736 01073a5d 2019-08-22 stsp error = cat_tag(id, repo, stdout);
7739 01073a5d 2019-08-22 stsp error = got_error(GOT_ERR_OBJ_TYPE);
7742 01073a5d 2019-08-22 stsp if (error)
7744 01073a5d 2019-08-22 stsp free(label);
7745 01073a5d 2019-08-22 stsp label = NULL;
7747 01073a5d 2019-08-22 stsp id = NULL;
7750 01073a5d 2019-08-22 stsp free(label);
7752 896e9b6f 2019-08-26 stsp free(commit_id);
7753 01073a5d 2019-08-22 stsp if (worktree)
7754 01073a5d 2019-08-22 stsp got_worktree_close(worktree);
7755 01073a5d 2019-08-22 stsp if (repo) {
7756 01073a5d 2019-08-22 stsp const struct got_error *repo_error;
7757 01073a5d 2019-08-22 stsp repo_error = got_repo_close(repo);
7758 01073a5d 2019-08-22 stsp if (error == NULL)
7759 01073a5d 2019-08-22 stsp error = repo_error;
7761 0ebf8283 2019-07-24 stsp return error;