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>
41 d2cdc636 2020-03-18 stsp #include <util.h>
43 53ccebc2 2019-07-30 stsp #include "got_version.h"
44 f42b1b34 2018-03-12 stsp #include "got_error.h"
45 f42b1b34 2018-03-12 stsp #include "got_object.h"
46 5261c201 2018-04-01 stsp #include "got_reference.h"
47 f42b1b34 2018-03-12 stsp #include "got_repository.h"
48 1dd54920 2019-05-11 stsp #include "got_path.h"
49 e6209546 2019-08-22 stsp #include "got_cancel.h"
50 c09a553d 2018-03-12 stsp #include "got_worktree.h"
51 79109fed 2018-03-27 stsp #include "got_diff.h"
52 372ccdbb 2018-06-10 stsp #include "got_commit_graph.h"
53 6f23baec 2020-03-18 stsp #include "got_fetch.h"
54 404c43c4 2018-06-21 stsp #include "got_blame.h"
55 63219cd2 2019-01-04 stsp #include "got_privsep.h"
56 793c30b5 2019-05-13 stsp #include "got_opentemp.h"
58 5c860e29 2018-03-12 stsp #ifndef nitems
59 5c860e29 2018-03-12 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
62 99437157 2018-11-11 stsp static volatile sig_atomic_t sigint_received;
63 99437157 2018-11-11 stsp static volatile sig_atomic_t sigpipe_received;
66 99437157 2018-11-11 stsp catch_sigint(int signo)
68 99437157 2018-11-11 stsp sigint_received = 1;
72 99437157 2018-11-11 stsp catch_sigpipe(int signo)
74 99437157 2018-11-11 stsp sigpipe_received = 1;
78 8cfb4057 2019-07-09 stsp struct got_cmd {
79 5c860e29 2018-03-12 stsp const char *cmd_name;
80 d7d4f210 2018-03-12 stsp const struct got_error *(*cmd_main)(int, char *[]);
81 1b6b95a8 2018-03-12 stsp void (*cmd_usage)(void);
82 97b3a7be 2019-07-09 stsp const char *cmd_alias;
85 ce5b7c56 2019-07-09 stsp __dead static void usage(int);
86 2c7829a4 2019-06-17 stsp __dead static void usage_init(void);
87 3ce1b845 2019-07-15 stsp __dead static void usage_import(void);
88 93658fb9 2020-03-18 stsp __dead static void usage_clone(void);
89 2ab43947 2020-03-18 stsp __dead static void usage_checkout(void);
90 507dc3bb 2018-12-29 stsp __dead static void usage_update(void);
91 4ed7e80c 2018-05-20 stsp __dead static void usage_log(void);
92 4ed7e80c 2018-05-20 stsp __dead static void usage_diff(void);
93 404c43c4 2018-06-21 stsp __dead static void usage_blame(void);
94 5de5890b 2018-10-18 stsp __dead static void usage_tree(void);
95 6bad629b 2019-02-04 stsp __dead static void usage_status(void);
96 d0eebce4 2019-03-11 stsp __dead static void usage_ref(void);
97 4e759de4 2019-06-26 stsp __dead static void usage_branch(void);
98 8e7bd50a 2019-08-22 stsp __dead static void usage_tag(void);
99 d00136be 2019-03-26 stsp __dead static void usage_add(void);
100 648e4ef7 2019-07-09 stsp __dead static void usage_remove(void);
101 a129376b 2019-03-28 stsp __dead static void usage_revert(void);
102 c4296144 2019-05-09 stsp __dead static void usage_commit(void);
103 234035bc 2019-06-01 stsp __dead static void usage_cherrypick(void);
104 5ef14e63 2019-06-02 stsp __dead static void usage_backout(void);
105 818c7501 2019-07-11 stsp __dead static void usage_rebase(void);
106 0ebf8283 2019-07-24 stsp __dead static void usage_histedit(void);
107 2822a352 2019-10-15 stsp __dead static void usage_integrate(void);
108 715dc77e 2019-08-03 stsp __dead static void usage_stage(void);
109 ad493afc 2019-08-03 stsp __dead static void usage_unstage(void);
110 01073a5d 2019-08-22 stsp __dead static void usage_cat(void);
112 2c7829a4 2019-06-17 stsp static const struct got_error* cmd_init(int, char *[]);
113 3ce1b845 2019-07-15 stsp static const struct got_error* cmd_import(int, char *[]);
114 93658fb9 2020-03-18 stsp static const struct got_error* cmd_clone(int, char *[]);
115 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_checkout(int, char *[]);
116 507dc3bb 2018-12-29 stsp static const struct got_error* cmd_update(int, char *[]);
117 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_log(int, char *[]);
118 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_diff(int, char *[]);
119 404c43c4 2018-06-21 stsp static const struct got_error* cmd_blame(int, char *[]);
120 5de5890b 2018-10-18 stsp static const struct got_error* cmd_tree(int, char *[]);
121 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_status(int, char *[]);
122 d0eebce4 2019-03-11 stsp static const struct got_error* cmd_ref(int, char *[]);
123 4e759de4 2019-06-26 stsp static const struct got_error* cmd_branch(int, char *[]);
124 8e7bd50a 2019-08-22 stsp static const struct got_error* cmd_tag(int, char *[]);
125 d00136be 2019-03-26 stsp static const struct got_error* cmd_add(int, char *[]);
126 648e4ef7 2019-07-09 stsp static const struct got_error* cmd_remove(int, char *[]);
127 a129376b 2019-03-28 stsp static const struct got_error* cmd_revert(int, char *[]);
128 c4296144 2019-05-09 stsp static const struct got_error* cmd_commit(int, char *[]);
129 234035bc 2019-06-01 stsp static const struct got_error* cmd_cherrypick(int, char *[]);
130 5ef14e63 2019-06-02 stsp static const struct got_error* cmd_backout(int, char *[]);
131 818c7501 2019-07-11 stsp static const struct got_error* cmd_rebase(int, char *[]);
132 0ebf8283 2019-07-24 stsp static const struct got_error* cmd_histedit(int, char *[]);
133 2822a352 2019-10-15 stsp static const struct got_error* cmd_integrate(int, char *[]);
134 715dc77e 2019-08-03 stsp static const struct got_error* cmd_stage(int, char *[]);
135 ad493afc 2019-08-03 stsp static const struct got_error* cmd_unstage(int, char *[]);
136 01073a5d 2019-08-22 stsp static const struct got_error* cmd_cat(int, char *[]);
138 8cfb4057 2019-07-09 stsp static struct got_cmd got_commands[] = {
139 bc26cce8 2019-08-04 stsp { "init", cmd_init, usage_init, "in" },
140 bc26cce8 2019-08-04 stsp { "import", cmd_import, usage_import, "im" },
141 93658fb9 2020-03-18 stsp { "clone", cmd_clone, usage_clone, "cl" },
142 2ab43947 2020-03-18 stsp { "checkout", cmd_checkout, usage_checkout, "co" },
143 97b3a7be 2019-07-09 stsp { "update", cmd_update, usage_update, "up" },
144 97b3a7be 2019-07-09 stsp { "log", cmd_log, usage_log, "" },
145 bc26cce8 2019-08-04 stsp { "diff", cmd_diff, usage_diff, "di" },
146 bc26cce8 2019-08-04 stsp { "blame", cmd_blame, usage_blame, "bl" },
147 bc26cce8 2019-08-04 stsp { "tree", cmd_tree, usage_tree, "tr" },
148 97b3a7be 2019-07-09 stsp { "status", cmd_status, usage_status, "st" },
149 97b3a7be 2019-07-09 stsp { "ref", cmd_ref, usage_ref, "" },
150 97b3a7be 2019-07-09 stsp { "branch", cmd_branch, usage_branch, "br" },
151 8e7bd50a 2019-08-22 stsp { "tag", cmd_tag, usage_tag, "" },
152 97b3a7be 2019-07-09 stsp { "add", cmd_add, usage_add, "" },
153 648e4ef7 2019-07-09 stsp { "remove", cmd_remove, usage_remove, "rm" },
154 97b3a7be 2019-07-09 stsp { "revert", cmd_revert, usage_revert, "rv" },
155 97b3a7be 2019-07-09 stsp { "commit", cmd_commit, usage_commit, "ci" },
156 016477fd 2019-07-09 stsp { "cherrypick", cmd_cherrypick, usage_cherrypick, "cy" },
157 97b3a7be 2019-07-09 stsp { "backout", cmd_backout, usage_backout, "bo" },
158 818c7501 2019-07-11 stsp { "rebase", cmd_rebase, usage_rebase, "rb" },
159 0ebf8283 2019-07-24 stsp { "histedit", cmd_histedit, usage_histedit, "he" },
160 2822a352 2019-10-15 stsp { "integrate", cmd_integrate, usage_integrate,"ig" },
161 715dc77e 2019-08-03 stsp { "stage", cmd_stage, usage_stage, "sg" },
162 ad493afc 2019-08-03 stsp { "unstage", cmd_unstage, usage_unstage, "ug" },
163 01073a5d 2019-08-22 stsp { "cat", cmd_cat, usage_cat, "" },
166 ce5b7c56 2019-07-09 stsp static void
167 ce5b7c56 2019-07-09 stsp list_commands(void)
171 ce5b7c56 2019-07-09 stsp fprintf(stderr, "commands:");
172 ce5b7c56 2019-07-09 stsp for (i = 0; i < nitems(got_commands); i++) {
173 ce5b7c56 2019-07-09 stsp struct got_cmd *cmd = &got_commands[i];
174 ce5b7c56 2019-07-09 stsp fprintf(stderr, " %s", cmd->cmd_name);
176 ce5b7c56 2019-07-09 stsp fputc('\n', stderr);
180 5c860e29 2018-03-12 stsp main(int argc, char *argv[])
182 8cfb4057 2019-07-09 stsp struct got_cmd *cmd;
183 5c860e29 2018-03-12 stsp unsigned int i;
185 53ccebc2 2019-07-30 stsp int hflag = 0, Vflag = 0;
186 83cd27f8 2020-01-13 stsp static struct option longopts[] = {
187 83cd27f8 2020-01-13 stsp { "version", no_argument, NULL, 'V' },
188 83cd27f8 2020-01-13 stsp { NULL, 0, NULL, 0}
191 289e3cbf 2019-02-04 stsp setlocale(LC_CTYPE, "");
193 6586ea88 2020-01-13 stsp while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
194 5c860e29 2018-03-12 stsp switch (ch) {
202 ce5b7c56 2019-07-09 stsp usage(hflag);
203 5c860e29 2018-03-12 stsp /* NOTREACHED */
207 5c860e29 2018-03-12 stsp argc -= optind;
208 5c860e29 2018-03-12 stsp argv += optind;
209 1e70621d 2018-03-27 stsp optind = 0;
211 53ccebc2 2019-07-30 stsp if (Vflag) {
212 53ccebc2 2019-07-30 stsp got_version_print_str();
216 5c860e29 2018-03-12 stsp if (argc <= 0)
217 ce5b7c56 2019-07-09 stsp usage(hflag);
219 99437157 2018-11-11 stsp signal(SIGINT, catch_sigint);
220 99437157 2018-11-11 stsp signal(SIGPIPE, catch_sigpipe);
222 5c860e29 2018-03-12 stsp for (i = 0; i < nitems(got_commands); i++) {
223 d7d4f210 2018-03-12 stsp const struct got_error *error;
225 5c860e29 2018-03-12 stsp cmd = &got_commands[i];
227 97b3a7be 2019-07-09 stsp if (strcmp(cmd->cmd_name, argv[0]) != 0 &&
228 97b3a7be 2019-07-09 stsp strcmp(cmd->cmd_alias, argv[0]) != 0)
232 1b6b95a8 2018-03-12 stsp got_commands[i].cmd_usage();
234 d7d4f210 2018-03-12 stsp error = got_commands[i].cmd_main(argc, argv);
235 f8afbdc8 2019-11-08 stsp if (error && error->code != GOT_ERR_CANCELLED &&
236 f8afbdc8 2019-11-08 stsp error->code != GOT_ERR_PRIVSEP_EXIT &&
237 f8afbdc8 2019-11-08 stsp !(sigpipe_received &&
238 70015d7a 2019-11-08 stsp error->code == GOT_ERR_ERRNO && errno == EPIPE) &&
239 70015d7a 2019-11-08 stsp !(sigint_received &&
240 70015d7a 2019-11-08 stsp error->code == GOT_ERR_ERRNO && errno == EINTR)) {
241 d7d4f210 2018-03-12 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
248 20ecf764 2018-03-12 stsp fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
249 ce5b7c56 2019-07-09 stsp list_commands();
253 4ed7e80c 2018-05-20 stsp __dead static void
254 ce5b7c56 2019-07-09 stsp usage(int hflag)
256 e801a566 2020-01-13 stsp fprintf(stderr, "usage: %s [-h] [-V | --version] command [arg ...]\n",
257 53ccebc2 2019-07-30 stsp getprogname());
259 ce5b7c56 2019-07-09 stsp list_commands();
263 0266afb7 2019-01-04 stsp static const struct got_error *
264 0ee7065d 2019-05-13 stsp get_editor(char **abspath)
266 0ee7065d 2019-05-13 stsp const struct got_error *err = NULL;
267 e2ba3d07 2019-05-13 stsp const char *editor;
269 8920fa04 2019-08-18 stsp *abspath = NULL;
271 e2ba3d07 2019-05-13 stsp editor = getenv("VISUAL");
272 e2ba3d07 2019-05-13 stsp if (editor == NULL)
273 e2ba3d07 2019-05-13 stsp editor = getenv("EDITOR");
275 0ee7065d 2019-05-13 stsp if (editor) {
276 0ee7065d 2019-05-13 stsp err = got_path_find_prog(abspath, editor);
278 0ee7065d 2019-05-13 stsp return err;
281 0ee7065d 2019-05-13 stsp if (*abspath == NULL) {
282 0ee7065d 2019-05-13 stsp *abspath = strdup("/bin/ed");
283 0ee7065d 2019-05-13 stsp if (*abspath == NULL)
284 0ee7065d 2019-05-13 stsp return got_error_from_errno("strdup");
287 e2ba3d07 2019-05-13 stsp return NULL;
290 e2ba3d07 2019-05-13 stsp static const struct got_error *
291 d0eebce4 2019-03-11 stsp apply_unveil(const char *repo_path, int repo_read_only,
292 c530dc23 2019-07-23 stsp const char *worktree_path)
294 163ce85a 2019-05-13 stsp const struct got_error *err;
296 37c06ea4 2019-07-15 stsp #ifdef PROFILE
297 37c06ea4 2019-07-15 stsp if (unveil("gmon.out", "rwc") != 0)
298 37c06ea4 2019-07-15 stsp return got_error_from_errno2("unveil", "gmon.out");
300 d0eebce4 2019-03-11 stsp if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
301 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", repo_path);
303 0266afb7 2019-01-04 stsp if (worktree_path && unveil(worktree_path, "rwc") != 0)
304 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", worktree_path);
306 bb63914a 2020-02-17 stsp if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
307 bb63914a 2020-02-17 stsp return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
309 163ce85a 2019-05-13 stsp err = got_privsep_unveil_exec_helpers();
310 163ce85a 2019-05-13 stsp if (err != NULL)
311 163ce85a 2019-05-13 stsp return err;
313 0266afb7 2019-01-04 stsp if (unveil(NULL, NULL) != 0)
314 638f9024 2019-05-13 stsp return got_error_from_errno("unveil");
316 0266afb7 2019-01-04 stsp return NULL;
319 2c7829a4 2019-06-17 stsp __dead static void
320 2c7829a4 2019-06-17 stsp usage_init(void)
322 09ea71ba 2019-07-27 stsp fprintf(stderr, "usage: %s init repository-path\n", getprogname());
326 2c7829a4 2019-06-17 stsp static const struct got_error *
327 2c7829a4 2019-06-17 stsp cmd_init(int argc, char *argv[])
329 2c7829a4 2019-06-17 stsp const struct got_error *error = NULL;
330 2c7829a4 2019-06-17 stsp char *repo_path = NULL;
333 2c7829a4 2019-06-17 stsp while ((ch = getopt(argc, argv, "")) != -1) {
334 2c7829a4 2019-06-17 stsp switch (ch) {
336 2c7829a4 2019-06-17 stsp usage_init();
337 2c7829a4 2019-06-17 stsp /* NOTREACHED */
341 2c7829a4 2019-06-17 stsp argc -= optind;
342 2c7829a4 2019-06-17 stsp argv += optind;
344 2c7829a4 2019-06-17 stsp #ifndef PROFILE
345 2c7829a4 2019-06-17 stsp if (pledge("stdio rpath wpath cpath unveil", NULL) == -1)
346 2c7829a4 2019-06-17 stsp err(1, "pledge");
348 2c7829a4 2019-06-17 stsp if (argc != 1)
349 bc20e173 2019-06-17 stsp usage_init();
351 2c7829a4 2019-06-17 stsp repo_path = strdup(argv[0]);
352 2c7829a4 2019-06-17 stsp if (repo_path == NULL)
353 2c7829a4 2019-06-17 stsp return got_error_from_errno("strdup");
355 2c7829a4 2019-06-17 stsp got_path_strip_trailing_slashes(repo_path);
357 2c7829a4 2019-06-17 stsp error = got_path_mkdir(repo_path);
358 2c7829a4 2019-06-17 stsp if (error &&
359 2c7829a4 2019-06-17 stsp !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
362 c530dc23 2019-07-23 stsp error = apply_unveil(repo_path, 0, NULL);
366 2c7829a4 2019-06-17 stsp error = got_repo_init(repo_path);
368 3ce1b845 2019-07-15 stsp free(repo_path);
369 3ce1b845 2019-07-15 stsp return error;
372 3ce1b845 2019-07-15 stsp __dead static void
373 3ce1b845 2019-07-15 stsp usage_import(void)
375 3ce1b845 2019-07-15 stsp fprintf(stderr, "usage: %s import [-b branch] [-m message] "
376 3ce1b845 2019-07-15 stsp "[-r repository-path] [-I pattern] path\n", getprogname());
381 3ce1b845 2019-07-15 stsp spawn_editor(const char *editor, const char *file)
384 3ce1b845 2019-07-15 stsp sig_t sighup, sigint, sigquit;
385 3ce1b845 2019-07-15 stsp int st = -1;
387 3ce1b845 2019-07-15 stsp sighup = signal(SIGHUP, SIG_IGN);
388 3ce1b845 2019-07-15 stsp sigint = signal(SIGINT, SIG_IGN);
389 3ce1b845 2019-07-15 stsp sigquit = signal(SIGQUIT, SIG_IGN);
391 3ce1b845 2019-07-15 stsp switch (pid = fork()) {
393 3ce1b845 2019-07-15 stsp goto doneediting;
395 3ce1b845 2019-07-15 stsp execl(editor, editor, file, (char *)NULL);
396 3ce1b845 2019-07-15 stsp _exit(127);
399 3ce1b845 2019-07-15 stsp while (waitpid(pid, &st, 0) == -1)
400 3ce1b845 2019-07-15 stsp if (errno != EINTR)
403 3ce1b845 2019-07-15 stsp doneediting:
404 3ce1b845 2019-07-15 stsp (void)signal(SIGHUP, sighup);
405 3ce1b845 2019-07-15 stsp (void)signal(SIGINT, sigint);
406 3ce1b845 2019-07-15 stsp (void)signal(SIGQUIT, sigquit);
408 3ce1b845 2019-07-15 stsp if (!WIFEXITED(st)) {
409 3ce1b845 2019-07-15 stsp errno = EINTR;
413 3ce1b845 2019-07-15 stsp return WEXITSTATUS(st);
416 3ce1b845 2019-07-15 stsp static const struct got_error *
417 3ce1b845 2019-07-15 stsp edit_logmsg(char **logmsg, const char *editor, const char *logmsg_path,
418 3ce1b845 2019-07-15 stsp const char *initial_content)
420 3ce1b845 2019-07-15 stsp const struct got_error *err = NULL;
421 3ce1b845 2019-07-15 stsp char buf[1024];
422 3ce1b845 2019-07-15 stsp struct stat st, st2;
424 3ce1b845 2019-07-15 stsp int content_changed = 0;
425 3ce1b845 2019-07-15 stsp size_t len;
427 3ce1b845 2019-07-15 stsp *logmsg = NULL;
429 3ce1b845 2019-07-15 stsp if (stat(logmsg_path, &st) == -1)
430 3ce1b845 2019-07-15 stsp return got_error_from_errno2("stat", logmsg_path);
432 3ce1b845 2019-07-15 stsp if (spawn_editor(editor, logmsg_path) == -1)
433 3ce1b845 2019-07-15 stsp return got_error_from_errno("failed spawning editor");
435 3ce1b845 2019-07-15 stsp if (stat(logmsg_path, &st2) == -1)
436 3ce1b845 2019-07-15 stsp return got_error_from_errno("stat");
438 3ce1b845 2019-07-15 stsp if (st.st_mtime == st2.st_mtime && st.st_size == st2.st_size)
439 3ce1b845 2019-07-15 stsp return got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
440 3ce1b845 2019-07-15 stsp "no changes made to commit message, aborting");
442 3ce1b845 2019-07-15 stsp *logmsg = malloc(st2.st_size + 1);
443 3ce1b845 2019-07-15 stsp if (*logmsg == NULL)
444 3ce1b845 2019-07-15 stsp return got_error_from_errno("malloc");
445 3ce1b845 2019-07-15 stsp (*logmsg)[0] = '\0';
448 3ce1b845 2019-07-15 stsp fp = fopen(logmsg_path, "r");
449 3ce1b845 2019-07-15 stsp if (fp == NULL) {
450 3ce1b845 2019-07-15 stsp err = got_error_from_errno("fopen");
453 3ce1b845 2019-07-15 stsp while (fgets(buf, sizeof(buf), fp) != NULL) {
454 3ce1b845 2019-07-15 stsp if (!content_changed && strcmp(buf, initial_content) != 0)
455 3ce1b845 2019-07-15 stsp content_changed = 1;
456 3ce1b845 2019-07-15 stsp if (buf[0] == '#' || (len == 0 && buf[0] == '\n'))
457 3ce1b845 2019-07-15 stsp continue; /* remove comments and leading empty lines */
458 3ce1b845 2019-07-15 stsp len = strlcat(*logmsg, buf, st2.st_size);
460 3ce1b845 2019-07-15 stsp fclose(fp);
462 3ce1b845 2019-07-15 stsp while (len > 0 && (*logmsg)[len - 1] == '\n') {
463 3ce1b845 2019-07-15 stsp (*logmsg)[len - 1] = '\0';
467 3ce1b845 2019-07-15 stsp if (len == 0 || !content_changed)
468 3ce1b845 2019-07-15 stsp err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
469 3ce1b845 2019-07-15 stsp "commit message cannot be empty, aborting");
472 3ce1b845 2019-07-15 stsp free(*logmsg);
473 3ce1b845 2019-07-15 stsp *logmsg = NULL;
475 3ce1b845 2019-07-15 stsp return err;
478 3ce1b845 2019-07-15 stsp static const struct got_error *
479 ef293bdd 2019-10-21 stsp collect_import_msg(char **logmsg, char **logmsg_path, const char *editor,
480 ef293bdd 2019-10-21 stsp const char *path_dir, const char *branch_name)
482 ef293bdd 2019-10-21 stsp char *initial_content = NULL;
483 3ce1b845 2019-07-15 stsp const struct got_error *err = NULL;
486 3ce1b845 2019-07-15 stsp if (asprintf(&initial_content,
487 3ce1b845 2019-07-15 stsp "\n# %s to be imported to branch %s\n", path_dir,
488 3ce1b845 2019-07-15 stsp branch_name) == -1)
489 3ce1b845 2019-07-15 stsp return got_error_from_errno("asprintf");
491 bb63914a 2020-02-17 stsp err = got_opentemp_named_fd(logmsg_path, &fd,
492 bb63914a 2020-02-17 stsp GOT_TMPDIR_STR "/got-importmsg");
496 3ce1b845 2019-07-15 stsp dprintf(fd, initial_content);
499 ef293bdd 2019-10-21 stsp err = edit_logmsg(logmsg, editor, *logmsg_path, initial_content);
501 3ce1b845 2019-07-15 stsp free(initial_content);
502 3ce1b845 2019-07-15 stsp return err;
505 3ce1b845 2019-07-15 stsp static const struct got_error *
506 3ce1b845 2019-07-15 stsp import_progress(void *arg, const char *path)
508 3ce1b845 2019-07-15 stsp printf("A %s\n", path);
509 3ce1b845 2019-07-15 stsp return NULL;
512 3ce1b845 2019-07-15 stsp static const struct got_error *
513 aba9c984 2019-09-08 stsp get_author(char **author, struct got_repository *repo)
515 aba9c984 2019-09-08 stsp const struct got_error *err = NULL;
516 c9956ddf 2019-09-08 stsp const char *got_author, *name, *email;
518 84792843 2019-08-09 stsp *author = NULL;
520 c9956ddf 2019-09-08 stsp name = got_repo_get_gitconfig_author_name(repo);
521 c9956ddf 2019-09-08 stsp email = got_repo_get_gitconfig_author_email(repo);
522 c9956ddf 2019-09-08 stsp if (name && email) {
523 c9956ddf 2019-09-08 stsp if (asprintf(author, "%s <%s>", name, email) == -1)
524 aba9c984 2019-09-08 stsp return got_error_from_errno("asprintf");
525 aba9c984 2019-09-08 stsp return NULL;
528 84792843 2019-08-09 stsp got_author = getenv("GOT_AUTHOR");
529 84792843 2019-08-09 stsp if (got_author == NULL) {
530 c9956ddf 2019-09-08 stsp name = got_repo_get_global_gitconfig_author_name(repo);
531 c9956ddf 2019-09-08 stsp email = got_repo_get_global_gitconfig_author_email(repo);
532 c9956ddf 2019-09-08 stsp if (name && email) {
533 c9956ddf 2019-09-08 stsp if (asprintf(author, "%s <%s>", name, email) == -1)
534 c9956ddf 2019-09-08 stsp return got_error_from_errno("asprintf");
535 c9956ddf 2019-09-08 stsp return NULL;
537 84792843 2019-08-09 stsp /* TODO: Look up user in password database? */
538 84792843 2019-08-09 stsp return got_error(GOT_ERR_COMMIT_NO_AUTHOR);
541 aba9c984 2019-09-08 stsp *author = strdup(got_author);
542 aba9c984 2019-09-08 stsp if (*author == NULL)
543 aba9c984 2019-09-08 stsp return got_error_from_errno("strdup");
546 84792843 2019-08-09 stsp * Really dumb email address check; we're only doing this to
547 84792843 2019-08-09 stsp * avoid git's object parser breaking on commits we create.
549 84792843 2019-08-09 stsp while (*got_author && *got_author != '<')
550 84792843 2019-08-09 stsp got_author++;
551 aba9c984 2019-09-08 stsp if (*got_author != '<') {
552 aba9c984 2019-09-08 stsp err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
555 84792843 2019-08-09 stsp while (*got_author && *got_author != '@')
556 84792843 2019-08-09 stsp got_author++;
557 aba9c984 2019-09-08 stsp if (*got_author != '@') {
558 aba9c984 2019-09-08 stsp err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
561 84792843 2019-08-09 stsp while (*got_author && *got_author != '>')
562 84792843 2019-08-09 stsp got_author++;
563 84792843 2019-08-09 stsp if (*got_author != '>')
564 aba9c984 2019-09-08 stsp err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
567 aba9c984 2019-09-08 stsp free(*author);
568 aba9c984 2019-09-08 stsp *author = NULL;
570 aba9c984 2019-09-08 stsp return err;
573 c9956ddf 2019-09-08 stsp static const struct got_error *
574 c9956ddf 2019-09-08 stsp get_gitconfig_path(char **gitconfig_path)
576 c9956ddf 2019-09-08 stsp const char *homedir = getenv("HOME");
578 c9956ddf 2019-09-08 stsp *gitconfig_path = NULL;
579 c9956ddf 2019-09-08 stsp if (homedir) {
580 c9956ddf 2019-09-08 stsp if (asprintf(gitconfig_path, "%s/.gitconfig", homedir) == -1)
581 c9956ddf 2019-09-08 stsp return got_error_from_errno("asprintf");
584 c9956ddf 2019-09-08 stsp return NULL;
587 84792843 2019-08-09 stsp static const struct got_error *
588 3ce1b845 2019-07-15 stsp cmd_import(int argc, char *argv[])
590 3ce1b845 2019-07-15 stsp const struct got_error *error = NULL;
591 3ce1b845 2019-07-15 stsp char *path_dir = NULL, *repo_path = NULL, *logmsg = NULL;
592 c9956ddf 2019-09-08 stsp char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
593 5d67f40d 2019-11-08 stsp const char *branch_name = "main";
594 ef293bdd 2019-10-21 stsp char *refname = NULL, *id_str = NULL, *logmsg_path = NULL;
595 3ce1b845 2019-07-15 stsp struct got_repository *repo = NULL;
596 3ce1b845 2019-07-15 stsp struct got_reference *branch_ref = NULL, *head_ref = NULL;
597 3ce1b845 2019-07-15 stsp struct got_object_id *new_commit_id = NULL;
599 3ce1b845 2019-07-15 stsp struct got_pathlist_head ignores;
600 3ce1b845 2019-07-15 stsp struct got_pathlist_entry *pe;
601 ef293bdd 2019-10-21 stsp int preserve_logmsg = 0;
603 3ce1b845 2019-07-15 stsp TAILQ_INIT(&ignores);
605 3ce1b845 2019-07-15 stsp while ((ch = getopt(argc, argv, "b:m:r:I:")) != -1) {
606 3ce1b845 2019-07-15 stsp switch (ch) {
608 3ce1b845 2019-07-15 stsp branch_name = optarg;
611 3ce1b845 2019-07-15 stsp logmsg = strdup(optarg);
612 3ce1b845 2019-07-15 stsp if (logmsg == NULL) {
613 3ce1b845 2019-07-15 stsp error = got_error_from_errno("strdup");
618 3ce1b845 2019-07-15 stsp repo_path = realpath(optarg, NULL);
619 3ce1b845 2019-07-15 stsp if (repo_path == NULL) {
620 9ba1d308 2019-10-21 stsp error = got_error_from_errno2("realpath",
626 3ce1b845 2019-07-15 stsp if (optarg[0] == '\0')
628 3ce1b845 2019-07-15 stsp error = got_pathlist_insert(&pe, &ignores, optarg,
634 b2b65d18 2019-08-22 stsp usage_import();
635 3ce1b845 2019-07-15 stsp /* NOTREACHED */
639 3ce1b845 2019-07-15 stsp argc -= optind;
640 3ce1b845 2019-07-15 stsp argv += optind;
642 3ce1b845 2019-07-15 stsp #ifndef PROFILE
643 aba9c984 2019-09-08 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
645 3ce1b845 2019-07-15 stsp NULL) == -1)
646 3ce1b845 2019-07-15 stsp err(1, "pledge");
648 3ce1b845 2019-07-15 stsp if (argc != 1)
649 3ce1b845 2019-07-15 stsp usage_import();
651 3ce1b845 2019-07-15 stsp if (repo_path == NULL) {
652 3ce1b845 2019-07-15 stsp repo_path = getcwd(NULL, 0);
653 3ce1b845 2019-07-15 stsp if (repo_path == NULL)
654 3ce1b845 2019-07-15 stsp return got_error_from_errno("getcwd");
656 3ce1b845 2019-07-15 stsp got_path_strip_trailing_slashes(repo_path);
657 c9956ddf 2019-09-08 stsp error = get_gitconfig_path(&gitconfig_path);
660 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, gitconfig_path);
664 aba9c984 2019-09-08 stsp error = get_author(&author, repo);
666 aba9c984 2019-09-08 stsp return error;
669 bd5895f3 2019-11-28 stsp * Don't let the user create a branch name with a leading '-'.
670 e560b7e0 2019-11-28 stsp * While technically a valid reference name, this case is usually
671 e560b7e0 2019-11-28 stsp * an unintended typo.
673 bd5895f3 2019-11-28 stsp if (branch_name[0] == '-')
674 bd5895f3 2019-11-28 stsp return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
676 3ce1b845 2019-07-15 stsp if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
677 3ce1b845 2019-07-15 stsp error = got_error_from_errno("asprintf");
681 3ce1b845 2019-07-15 stsp error = got_ref_open(&branch_ref, repo, refname, 0);
682 3ce1b845 2019-07-15 stsp if (error) {
683 3ce1b845 2019-07-15 stsp if (error->code != GOT_ERR_NOT_REF)
686 3ce1b845 2019-07-15 stsp error = got_error_msg(GOT_ERR_BRANCH_EXISTS,
687 3ce1b845 2019-07-15 stsp "import target branch already exists");
691 3ce1b845 2019-07-15 stsp path_dir = realpath(argv[0], NULL);
692 3ce1b845 2019-07-15 stsp if (path_dir == NULL) {
693 9ba1d308 2019-10-21 stsp error = got_error_from_errno2("realpath", argv[0]);
696 3ce1b845 2019-07-15 stsp got_path_strip_trailing_slashes(path_dir);
699 3ce1b845 2019-07-15 stsp * unveil(2) traverses exec(2); if an editor is used we have
700 3ce1b845 2019-07-15 stsp * to apply unveil after the log message has been written.
702 3ce1b845 2019-07-15 stsp if (logmsg == NULL || strlen(logmsg) == 0) {
703 3ce1b845 2019-07-15 stsp error = get_editor(&editor);
706 8e158b01 2019-09-22 stsp free(logmsg);
707 ef293bdd 2019-10-21 stsp error = collect_import_msg(&logmsg, &logmsg_path, editor,
708 ef293bdd 2019-10-21 stsp path_dir, refname);
709 ef293bdd 2019-10-21 stsp if (error) {
710 ef293bdd 2019-10-21 stsp if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
711 ef293bdd 2019-10-21 stsp logmsg_path != NULL)
712 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
717 ef293bdd 2019-10-21 stsp if (unveil(path_dir, "r") != 0) {
718 ef293bdd 2019-10-21 stsp error = got_error_from_errno2("unveil", path_dir);
719 ef293bdd 2019-10-21 stsp if (logmsg_path)
720 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
724 ef293bdd 2019-10-21 stsp error = apply_unveil(got_repo_get_path(repo), 0, NULL);
725 ef293bdd 2019-10-21 stsp if (error) {
726 ef293bdd 2019-10-21 stsp if (logmsg_path)
727 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
731 3ce1b845 2019-07-15 stsp error = got_repo_import(&new_commit_id, path_dir, logmsg,
732 84792843 2019-08-09 stsp author, &ignores, repo, import_progress, NULL);
733 ef293bdd 2019-10-21 stsp if (error) {
734 ef293bdd 2019-10-21 stsp if (logmsg_path)
735 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
739 3ce1b845 2019-07-15 stsp error = got_ref_alloc(&branch_ref, refname, new_commit_id);
740 ef293bdd 2019-10-21 stsp if (error) {
741 ef293bdd 2019-10-21 stsp if (logmsg_path)
742 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
746 3ce1b845 2019-07-15 stsp error = got_ref_write(branch_ref, repo);
747 ef293bdd 2019-10-21 stsp if (error) {
748 ef293bdd 2019-10-21 stsp if (logmsg_path)
749 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
753 3ce1b845 2019-07-15 stsp error = got_object_id_str(&id_str, new_commit_id);
754 ef293bdd 2019-10-21 stsp if (error) {
755 ef293bdd 2019-10-21 stsp if (logmsg_path)
756 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
760 3ce1b845 2019-07-15 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
761 3ce1b845 2019-07-15 stsp if (error) {
762 ef293bdd 2019-10-21 stsp if (error->code != GOT_ERR_NOT_REF) {
763 ef293bdd 2019-10-21 stsp if (logmsg_path)
764 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
768 3ce1b845 2019-07-15 stsp error = got_ref_alloc_symref(&head_ref, GOT_REF_HEAD,
769 3ce1b845 2019-07-15 stsp branch_ref);
770 ef293bdd 2019-10-21 stsp if (error) {
771 ef293bdd 2019-10-21 stsp if (logmsg_path)
772 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
776 3ce1b845 2019-07-15 stsp error = got_ref_write(head_ref, repo);
777 ef293bdd 2019-10-21 stsp if (error) {
778 ef293bdd 2019-10-21 stsp if (logmsg_path)
779 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
784 3ce1b845 2019-07-15 stsp printf("Created branch %s with commit %s\n",
785 3ce1b845 2019-07-15 stsp got_ref_get_name(branch_ref), id_str);
787 ef293bdd 2019-10-21 stsp if (preserve_logmsg) {
788 ef293bdd 2019-10-21 stsp fprintf(stderr, "%s: log message preserved in %s\n",
789 ef293bdd 2019-10-21 stsp getprogname(), logmsg_path);
790 ef293bdd 2019-10-21 stsp } else if (logmsg_path && unlink(logmsg_path) == -1 && error == NULL)
791 ef293bdd 2019-10-21 stsp error = got_error_from_errno2("unlink", logmsg_path);
792 8e158b01 2019-09-22 stsp free(logmsg);
793 ef293bdd 2019-10-21 stsp free(logmsg_path);
794 2c7829a4 2019-06-17 stsp free(repo_path);
795 3ce1b845 2019-07-15 stsp free(editor);
796 3ce1b845 2019-07-15 stsp free(refname);
797 3ce1b845 2019-07-15 stsp free(new_commit_id);
798 3ce1b845 2019-07-15 stsp free(id_str);
799 aba9c984 2019-09-08 stsp free(author);
800 c9956ddf 2019-09-08 stsp free(gitconfig_path);
801 3ce1b845 2019-07-15 stsp if (branch_ref)
802 3ce1b845 2019-07-15 stsp got_ref_close(branch_ref);
803 3ce1b845 2019-07-15 stsp if (head_ref)
804 3ce1b845 2019-07-15 stsp got_ref_close(head_ref);
805 2c7829a4 2019-06-17 stsp return error;
808 93658fb9 2020-03-18 stsp __dead static void
809 93658fb9 2020-03-18 stsp usage_clone(void)
811 68999b92 2020-03-18 stsp fprintf(stderr, "usage: %s clone [-q] [-v] repository-url [target-directory]\n",
812 9df6f38b 2020-03-18 stsp getprogname());
816 892ac3b6 2020-03-18 stsp struct got_fetch_progress_arg {
817 892ac3b6 2020-03-18 stsp char last_scaled_size[FMT_SCALED_STRSIZE];
818 892ac3b6 2020-03-18 stsp int last_p_indexed;
819 892ac3b6 2020-03-18 stsp int last_p_resolved;
820 68999b92 2020-03-18 stsp int verbosity;
823 93658fb9 2020-03-18 stsp static const struct got_error *
824 baa9fea0 2020-03-18 stsp fetch_progress(void *arg, const char *message, off_t packfile_size,
825 668a20f6 2020-03-18 stsp int nobj_total, int nobj_indexed, int nobj_loose, int nobj_resolved)
827 892ac3b6 2020-03-18 stsp struct got_fetch_progress_arg *a = arg;
828 892ac3b6 2020-03-18 stsp char scaled_size[FMT_SCALED_STRSIZE];
829 892ac3b6 2020-03-18 stsp int p_indexed, p_resolved;
830 892ac3b6 2020-03-18 stsp int print_size = 0, print_indexed = 0, print_resolved = 0;
832 68999b92 2020-03-18 stsp if (a->verbosity < 0)
833 68999b92 2020-03-18 stsp return NULL;
835 fd843b58 2020-03-18 stsp if (message && message[0] != '\0') {
836 d2cdc636 2020-03-18 stsp printf("\rserver: %s", message);
837 892ac3b6 2020-03-18 stsp fflush(stdout);
840 b2409d58 2020-03-18 stsp if (packfile_size > 0 || nobj_indexed > 0) {
841 892ac3b6 2020-03-18 stsp if (fmt_scaled(packfile_size, scaled_size) == 0 &&
842 892ac3b6 2020-03-18 stsp (a->last_scaled_size[0] == '\0' ||
843 892ac3b6 2020-03-18 stsp strcmp(scaled_size, a->last_scaled_size)) != 0) {
844 892ac3b6 2020-03-18 stsp print_size = 1;
845 892ac3b6 2020-03-18 stsp if (strlcpy(a->last_scaled_size, scaled_size,
846 892ac3b6 2020-03-18 stsp FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
847 892ac3b6 2020-03-18 stsp return got_error(GOT_ERR_NO_SPACE);
849 61cc1a7a 2020-03-18 stsp if (nobj_indexed > 0) {
850 892ac3b6 2020-03-18 stsp p_indexed = (nobj_indexed * 100) / nobj_total;
851 892ac3b6 2020-03-18 stsp if (p_indexed != a->last_p_indexed) {
852 892ac3b6 2020-03-18 stsp a->last_p_indexed = p_indexed;
853 892ac3b6 2020-03-18 stsp print_indexed = 1;
854 892ac3b6 2020-03-18 stsp print_size = 1;
857 61cc1a7a 2020-03-18 stsp if (nobj_resolved > 0) {
858 892ac3b6 2020-03-18 stsp p_resolved = (nobj_resolved * 100) /
859 892ac3b6 2020-03-18 stsp (nobj_total - nobj_loose);
860 892ac3b6 2020-03-18 stsp if (p_resolved != a->last_p_resolved) {
861 892ac3b6 2020-03-18 stsp a->last_p_resolved = p_resolved;
862 892ac3b6 2020-03-18 stsp print_resolved = 1;
863 892ac3b6 2020-03-18 stsp print_indexed = 1;
864 892ac3b6 2020-03-18 stsp print_size = 1;
869 892ac3b6 2020-03-18 stsp if (print_size || print_indexed || print_resolved)
870 892ac3b6 2020-03-18 stsp printf("\r");
871 892ac3b6 2020-03-18 stsp if (print_size)
872 892ac3b6 2020-03-18 stsp printf("%*s fetched", FMT_SCALED_STRSIZE, scaled_size);
873 68999b92 2020-03-18 stsp if (print_indexed) {
874 892ac3b6 2020-03-18 stsp printf("; indexing %d%%", p_indexed);
875 68999b92 2020-03-18 stsp if (a->verbosity > 0)
876 68999b92 2020-03-18 stsp printf(" (%d/%d)", nobj_indexed, nobj_total);
878 68999b92 2020-03-18 stsp if (print_resolved) {
879 892ac3b6 2020-03-18 stsp printf("; resolving deltas %d%%", p_resolved);
880 68999b92 2020-03-18 stsp if (a->verbosity > 0)
881 68999b92 2020-03-18 stsp printf(" (%d/%d)", nobj_resolved,
882 68999b92 2020-03-18 stsp nobj_total - nobj_loose);
884 892ac3b6 2020-03-18 stsp if (print_size || print_indexed || print_resolved)
885 892ac3b6 2020-03-18 stsp fflush(stdout);
887 892ac3b6 2020-03-18 stsp if (nobj_indexed > 0 && nobj_indexed == nobj_total &&
888 892ac3b6 2020-03-18 stsp nobj_resolved == nobj_total - nobj_loose)
889 892ac3b6 2020-03-18 stsp printf("\nWriting pack index...\n");
891 531c3985 2020-03-18 stsp return NULL;
894 531c3985 2020-03-18 stsp static const struct got_error *
895 93658fb9 2020-03-18 stsp cmd_clone(int argc, char *argv[])
897 39c64a6a 2020-03-18 stsp const struct got_error *error = NULL;
898 9df6f38b 2020-03-18 stsp const char *uri, *dirname;
899 09838ffc 2020-03-18 stsp char *proto, *host, *port, *repo_name, *server_path;
900 d9b4d0c0 2020-03-18 stsp char *default_destdir = NULL, *id_str = NULL;
901 bb64b798 2020-03-18 stsp const char *repo_path;
902 bb64b798 2020-03-18 stsp struct got_repository *repo = NULL;
903 d9b4d0c0 2020-03-18 stsp struct got_pathlist_head refs, symrefs;
904 d9b4d0c0 2020-03-18 stsp struct got_pathlist_entry *pe;
905 d9b4d0c0 2020-03-18 stsp struct got_object_id *pack_hash = NULL;
906 20eb36d0 2020-03-18 stsp int ch, fetchfd = -1;
907 892ac3b6 2020-03-18 stsp struct got_fetch_progress_arg fpa;
908 b46f3e71 2020-03-18 stsp char *git_url = NULL;
909 b46f3e71 2020-03-18 stsp char *gitconfig_path = NULL;
910 b46f3e71 2020-03-18 stsp char *gitconfig = NULL;
911 b46f3e71 2020-03-18 stsp FILE *gitconfig_file = NULL;
913 68999b92 2020-03-18 stsp int verbosity = 0;
915 d9b4d0c0 2020-03-18 stsp TAILQ_INIT(&refs);
916 d9b4d0c0 2020-03-18 stsp TAILQ_INIT(&symrefs);
918 68999b92 2020-03-18 stsp while ((ch = getopt(argc, argv, "vq")) != -1) {
919 93658fb9 2020-03-18 stsp switch (ch) {
921 68999b92 2020-03-18 stsp if (verbosity < 0)
922 68999b92 2020-03-18 stsp verbosity = 0;
923 68999b92 2020-03-18 stsp else if (verbosity < 3)
924 68999b92 2020-03-18 stsp verbosity++;
927 68999b92 2020-03-18 stsp verbosity = -1;
930 93658fb9 2020-03-18 stsp usage_clone();
934 93658fb9 2020-03-18 stsp argc -= optind;
935 93658fb9 2020-03-18 stsp argv += optind;
937 93658fb9 2020-03-18 stsp uri = argv[0];
939 9df6f38b 2020-03-18 stsp if (argc == 1)
940 93658fb9 2020-03-18 stsp dirname = NULL;
941 9df6f38b 2020-03-18 stsp else if (argc == 2)
942 93658fb9 2020-03-18 stsp dirname = argv[1];
944 93658fb9 2020-03-18 stsp usage_clone();
946 39c64a6a 2020-03-18 stsp error = got_fetch_parse_uri(&proto, &host, &port, &server_path,
947 09838ffc 2020-03-18 stsp &repo_name, argv[0]);
951 39c64a6a 2020-03-18 stsp if (strcmp(proto, "git") == 0) {
952 b46f3e71 2020-03-18 stsp #ifndef PROFILE
953 39c64a6a 2020-03-18 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
954 39c64a6a 2020-03-18 stsp "sendfd dns inet unveil", NULL) == -1)
955 39c64a6a 2020-03-18 stsp err(1, "pledge");
957 b46f3e71 2020-03-18 stsp git_url = strdup(argv[0]);
958 b46f3e71 2020-03-18 stsp if (git_url == NULL) {
959 b46f3e71 2020-03-18 stsp error = got_error_from_errno("strdup");
962 39c64a6a 2020-03-18 stsp } else if (strcmp(proto, "git+ssh") == 0 ||
963 39c64a6a 2020-03-18 stsp strcmp(proto, "ssh") == 0) {
964 b46f3e71 2020-03-18 stsp #ifndef PROFILE
965 39c64a6a 2020-03-18 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
966 39c64a6a 2020-03-18 stsp "sendfd unveil", NULL) == -1)
967 39c64a6a 2020-03-18 stsp err(1, "pledge");
969 b46f3e71 2020-03-18 stsp if (asprintf(&git_url, "%s:%s", host, server_path) == -1) {
970 b46f3e71 2020-03-18 stsp error = got_error_from_errno("asprintf");
973 39c64a6a 2020-03-18 stsp } else if (strcmp(proto, "http") == 0 ||
974 39c64a6a 2020-03-18 stsp strcmp(proto, "git+http") == 0) {
975 39c64a6a 2020-03-18 stsp error = got_error_path(proto, GOT_ERR_NOT_IMPL);
978 39c64a6a 2020-03-18 stsp error = got_error_path(proto, GOT_ERR_BAD_PROTO);
981 bb64b798 2020-03-18 stsp if (dirname == NULL) {
982 bb64b798 2020-03-18 stsp if (asprintf(&default_destdir, "%s.git", repo_name) == -1) {
983 39c64a6a 2020-03-18 stsp error = got_error_from_errno("asprintf");
986 bb64b798 2020-03-18 stsp repo_path = default_destdir;
988 bb64b798 2020-03-18 stsp repo_path = dirname;
990 39c64a6a 2020-03-18 stsp error = got_path_mkdir(repo_path);
994 39c64a6a 2020-03-18 stsp error = got_repo_init(repo_path);
998 39c64a6a 2020-03-18 stsp error = got_repo_open(&repo, repo_path, NULL);
1000 294dfefd 2020-03-18 stsp goto done;
1002 ee448f5f 2020-03-18 stsp if (strcmp(proto, "git+ssh") == 0 || strcmp(proto, "ssh") == 0) {
1003 ee448f5f 2020-03-18 stsp if (unveil(GOT_FETCH_PATH_SSH, "x") != 0) {
1004 ee448f5f 2020-03-18 stsp error = got_error_from_errno2("unveil",
1005 ee448f5f 2020-03-18 stsp GOT_FETCH_PATH_SSH);
1006 ee448f5f 2020-03-18 stsp goto done;
1009 ee448f5f 2020-03-18 stsp error = apply_unveil(got_repo_get_path(repo), 0, NULL);
1010 ee448f5f 2020-03-18 stsp if (error)
1011 ee448f5f 2020-03-18 stsp goto done;
1013 68999b92 2020-03-18 stsp error = got_fetch_connect(&fetchfd, proto, host, port, server_path,
1014 68999b92 2020-03-18 stsp verbosity);
1015 39c64a6a 2020-03-18 stsp if (error)
1016 bb64b798 2020-03-18 stsp goto done;
1018 68999b92 2020-03-18 stsp if (verbosity >= 0)
1019 68999b92 2020-03-18 stsp printf("Connected to %s:%s\n", host, port);
1021 892ac3b6 2020-03-18 stsp fpa.last_scaled_size[0] = '\0';
1022 892ac3b6 2020-03-18 stsp fpa.last_p_indexed = -1;
1023 892ac3b6 2020-03-18 stsp fpa.last_p_resolved = -1;
1024 68999b92 2020-03-18 stsp fpa.verbosity = verbosity;
1025 39c64a6a 2020-03-18 stsp error = got_fetch_pack(&pack_hash, &refs, &symrefs, fetchfd,
1026 892ac3b6 2020-03-18 stsp repo, fetch_progress, &fpa);
1027 39c64a6a 2020-03-18 stsp if (error)
1028 d9b4d0c0 2020-03-18 stsp goto done;
1030 39c64a6a 2020-03-18 stsp error = got_object_id_str(&id_str, pack_hash);
1031 39c64a6a 2020-03-18 stsp if (error)
1032 d9b4d0c0 2020-03-18 stsp goto done;
1033 68999b92 2020-03-18 stsp if (verbosity >= 0)
1034 68999b92 2020-03-18 stsp printf("Fetched %s.pack\n", id_str);
1035 d9b4d0c0 2020-03-18 stsp free(id_str);
1037 d9b4d0c0 2020-03-18 stsp /* Set up references provided with the pack file. */
1038 d9b4d0c0 2020-03-18 stsp TAILQ_FOREACH(pe, &refs, entry) {
1039 d9b4d0c0 2020-03-18 stsp const char *refname = pe->path;
1040 d9b4d0c0 2020-03-18 stsp struct got_object_id *id = pe->data;
1041 d9b4d0c0 2020-03-18 stsp struct got_reference *ref;
1042 7ebc0570 2020-03-18 stsp char *remote_refname;
1044 39c64a6a 2020-03-18 stsp error = got_ref_alloc(&ref, refname, id);
1045 39c64a6a 2020-03-18 stsp if (error)
1046 d9b4d0c0 2020-03-18 stsp goto done;
1047 7ebc0570 2020-03-18 stsp error = got_ref_write(ref, repo);
1048 7ebc0570 2020-03-18 stsp got_ref_close(ref);
1049 7ebc0570 2020-03-18 stsp if (error)
1050 7ebc0570 2020-03-18 stsp goto done;
1052 7ebc0570 2020-03-18 stsp if (strncmp("refs/heads/", refname, 11) != 0)
1055 7ebc0570 2020-03-18 stsp if (asprintf(&remote_refname,
1056 7ebc0570 2020-03-18 stsp "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1057 7ebc0570 2020-03-18 stsp refname + 11) == -1) {
1058 7ebc0570 2020-03-18 stsp error = got_error_from_errno("asprintf");
1059 7ebc0570 2020-03-18 stsp goto done;
1061 7ebc0570 2020-03-18 stsp error = got_ref_alloc(&ref, remote_refname, id);
1062 39c64a6a 2020-03-18 stsp if (error)
1063 d9b4d0c0 2020-03-18 stsp goto done;
1064 39c64a6a 2020-03-18 stsp error = got_ref_write(ref, repo);
1065 d9b4d0c0 2020-03-18 stsp got_ref_close(ref);
1066 39c64a6a 2020-03-18 stsp if (error)
1067 d9b4d0c0 2020-03-18 stsp goto done;
1070 d9b4d0c0 2020-03-18 stsp /* Set the HEAD reference if the server provided one. */
1071 d9b4d0c0 2020-03-18 stsp TAILQ_FOREACH(pe, &symrefs, entry) {
1072 d9b4d0c0 2020-03-18 stsp struct got_reference *symref, *target_ref;
1073 d9b4d0c0 2020-03-18 stsp const char *refname = pe->path;
1074 d9b4d0c0 2020-03-18 stsp const char *target = pe->data;
1076 d9b4d0c0 2020-03-18 stsp if (strcmp(refname, GOT_REF_HEAD) != 0)
1079 39c64a6a 2020-03-18 stsp error = got_ref_open(&target_ref, repo, target, 0);
1080 39c64a6a 2020-03-18 stsp if (error) {
1081 39c64a6a 2020-03-18 stsp if (error->code == GOT_ERR_NOT_REF)
1083 d9b4d0c0 2020-03-18 stsp goto done;
1086 39c64a6a 2020-03-18 stsp error = got_ref_alloc_symref(&symref, GOT_REF_HEAD, target_ref);
1087 d9b4d0c0 2020-03-18 stsp got_ref_close(target_ref);
1088 39c64a6a 2020-03-18 stsp if (error)
1089 d9b4d0c0 2020-03-18 stsp goto done;
1091 68999b92 2020-03-18 stsp if (verbosity > 1) {
1092 68999b92 2020-03-18 stsp printf("Setting %s to %s\n", GOT_REF_HEAD,
1093 68999b92 2020-03-18 stsp got_ref_get_symref_target(symref));
1095 d52aaa3d 2020-03-18 stsp if (verbosity >= 0)
1096 d52aaa3d 2020-03-18 stsp printf("Created cloned repository '%s'\n", repo_path);
1098 39c64a6a 2020-03-18 stsp error = got_ref_write(symref, repo);
1099 d9b4d0c0 2020-03-18 stsp got_ref_close(symref);
1103 b46f3e71 2020-03-18 stsp /* Create a config file so Git can understand this repository. */
1104 b46f3e71 2020-03-18 stsp gitconfig_path = got_repo_get_path_gitconfig(repo);
1105 b46f3e71 2020-03-18 stsp if (gitconfig_path == NULL) {
1106 b46f3e71 2020-03-18 stsp error = got_error_from_errno("got_repo_get_path_gitconfig");
1107 b46f3e71 2020-03-18 stsp goto done;
1109 b46f3e71 2020-03-18 stsp gitconfig_file = fopen(gitconfig_path, "w");
1110 b46f3e71 2020-03-18 stsp if (gitconfig_file == NULL) {
1111 b46f3e71 2020-03-18 stsp error = got_error_from_errno2("fopen", gitconfig_path);
1112 b46f3e71 2020-03-18 stsp goto done;
1114 b46f3e71 2020-03-18 stsp if (asprintf(&gitconfig,
1115 b46f3e71 2020-03-18 stsp "[core]\n"
1116 b46f3e71 2020-03-18 stsp "\trepositoryformatversion = 0\n"
1117 b46f3e71 2020-03-18 stsp "\tbare = true\n"
1118 7ebc0570 2020-03-18 stsp "[remote \"%s\"]\n"
1119 b46f3e71 2020-03-18 stsp "\turl = %s\n"
1120 7ebc0570 2020-03-18 stsp "\tfetch = +refs/heads/*:refs/remotes/%s/*\n",
1121 7ebc0570 2020-03-18 stsp GOT_FETCH_DEFAULT_REMOTE_NAME, git_url,
1122 7ebc0570 2020-03-18 stsp GOT_FETCH_DEFAULT_REMOTE_NAME) == -1) {
1123 b46f3e71 2020-03-18 stsp error = got_error_from_errno("asprintf");
1124 b46f3e71 2020-03-18 stsp goto done;
1126 b46f3e71 2020-03-18 stsp n = fwrite(gitconfig, 1, strlen(gitconfig), gitconfig_file);
1127 b46f3e71 2020-03-18 stsp if (n != strlen(gitconfig))
1128 b46f3e71 2020-03-18 stsp error = got_ferror(gitconfig_file, GOT_ERR_IO);
1130 39c64a6a 2020-03-18 stsp if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
1131 39c64a6a 2020-03-18 stsp error = got_error_from_errno("close");
1132 b46f3e71 2020-03-18 stsp if (gitconfig_file && fclose(gitconfig_file) == EOF && error == NULL)
1133 b46f3e71 2020-03-18 stsp error = got_error_from_errno("fclose");
1135 bb64b798 2020-03-18 stsp got_repo_close(repo);
1136 d9b4d0c0 2020-03-18 stsp TAILQ_FOREACH(pe, &refs, entry) {
1137 d9b4d0c0 2020-03-18 stsp free((void *)pe->path);
1138 d9b4d0c0 2020-03-18 stsp free(pe->data);
1140 d9b4d0c0 2020-03-18 stsp got_pathlist_free(&refs);
1141 d9b4d0c0 2020-03-18 stsp TAILQ_FOREACH(pe, &symrefs, entry) {
1142 d9b4d0c0 2020-03-18 stsp free((void *)pe->path);
1143 d9b4d0c0 2020-03-18 stsp free(pe->data);
1145 d9b4d0c0 2020-03-18 stsp got_pathlist_free(&symrefs);
1146 d9b4d0c0 2020-03-18 stsp free(pack_hash);
1147 09838ffc 2020-03-18 stsp free(proto);
1148 09838ffc 2020-03-18 stsp free(host);
1149 09838ffc 2020-03-18 stsp free(port);
1150 09838ffc 2020-03-18 stsp free(server_path);
1151 09838ffc 2020-03-18 stsp free(repo_name);
1152 bb64b798 2020-03-18 stsp free(default_destdir);
1153 b46f3e71 2020-03-18 stsp free(gitconfig_path);
1154 b46f3e71 2020-03-18 stsp free(git_url);
1155 39c64a6a 2020-03-18 stsp return error;
1158 2ab43947 2020-03-18 stsp __dead static void
1159 2ab43947 2020-03-18 stsp usage_checkout(void)
1161 2ab43947 2020-03-18 stsp fprintf(stderr, "usage: %s checkout [-E] [-b branch] [-c commit] "
1162 2ab43947 2020-03-18 stsp "[-p prefix] repository-path [worktree-path]\n", getprogname());
1166 2ab43947 2020-03-18 stsp static void
1167 2ab43947 2020-03-18 stsp show_worktree_base_ref_warning(void)
1169 2ab43947 2020-03-18 stsp fprintf(stderr, "%s: warning: could not create a reference "
1170 2ab43947 2020-03-18 stsp "to the work tree's base commit; the commit could be "
1171 2ab43947 2020-03-18 stsp "garbage-collected by Git; making the repository "
1172 2ab43947 2020-03-18 stsp "writable and running 'got update' will prevent this\n",
1173 2ab43947 2020-03-18 stsp getprogname());
1176 2ab43947 2020-03-18 stsp struct got_checkout_progress_arg {
1177 2ab43947 2020-03-18 stsp const char *worktree_path;
1178 2ab43947 2020-03-18 stsp int had_base_commit_ref_error;
1181 2ab43947 2020-03-18 stsp static const struct got_error *
1182 2ab43947 2020-03-18 stsp checkout_progress(void *arg, unsigned char status, const char *path)
1184 2ab43947 2020-03-18 stsp struct got_checkout_progress_arg *a = arg;
1186 2ab43947 2020-03-18 stsp /* Base commit bump happens silently. */
1187 2ab43947 2020-03-18 stsp if (status == GOT_STATUS_BUMP_BASE)
1188 2ab43947 2020-03-18 stsp return NULL;
1190 2ab43947 2020-03-18 stsp if (status == GOT_STATUS_BASE_REF_ERR) {
1191 2ab43947 2020-03-18 stsp a->had_base_commit_ref_error = 1;
1192 2ab43947 2020-03-18 stsp return NULL;
1195 2ab43947 2020-03-18 stsp while (path[0] == '/')
1198 2ab43947 2020-03-18 stsp printf("%c %s/%s\n", status, a->worktree_path, path);
1199 2ab43947 2020-03-18 stsp return NULL;
1202 2ab43947 2020-03-18 stsp static const struct got_error *
1203 2ab43947 2020-03-18 stsp check_cancelled(void *arg)
1205 2ab43947 2020-03-18 stsp if (sigint_received || sigpipe_received)
1206 2ab43947 2020-03-18 stsp return got_error(GOT_ERR_CANCELLED);
1207 2ab43947 2020-03-18 stsp return NULL;
1210 2ab43947 2020-03-18 stsp static const struct got_error *
1211 2ab43947 2020-03-18 stsp check_linear_ancestry(struct got_object_id *commit_id,
1212 2ab43947 2020-03-18 stsp struct got_object_id *base_commit_id, int allow_forwards_in_time_only,
1213 2ab43947 2020-03-18 stsp struct got_repository *repo)
1215 2ab43947 2020-03-18 stsp const struct got_error *err = NULL;
1216 2ab43947 2020-03-18 stsp struct got_object_id *yca_id;
1218 2ab43947 2020-03-18 stsp err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
1219 2ab43947 2020-03-18 stsp commit_id, base_commit_id, repo, check_cancelled, NULL);
1221 2ab43947 2020-03-18 stsp return err;
1223 2ab43947 2020-03-18 stsp if (yca_id == NULL)
1224 2ab43947 2020-03-18 stsp return got_error(GOT_ERR_ANCESTRY);
1227 2ab43947 2020-03-18 stsp * Require a straight line of history between the target commit
1228 2ab43947 2020-03-18 stsp * and the work tree's base commit.
1230 2ab43947 2020-03-18 stsp * Non-linear situations such as this require a rebase:
1232 2ab43947 2020-03-18 stsp * (commit) D F (base_commit)
1240 2ab43947 2020-03-18 stsp * 'got update' only handles linear cases:
1241 2ab43947 2020-03-18 stsp * Update forwards in time: A (base/yca) - B - C - D (commit)
1242 2ab43947 2020-03-18 stsp * Update backwards in time: D (base) - C - B - A (commit/yca)
1244 2ab43947 2020-03-18 stsp if (allow_forwards_in_time_only) {
1245 2ab43947 2020-03-18 stsp if (got_object_id_cmp(base_commit_id, yca_id) != 0)
1246 2ab43947 2020-03-18 stsp return got_error(GOT_ERR_ANCESTRY);
1247 2ab43947 2020-03-18 stsp } else if (got_object_id_cmp(commit_id, yca_id) != 0 &&
1248 2ab43947 2020-03-18 stsp got_object_id_cmp(base_commit_id, yca_id) != 0)
1249 2ab43947 2020-03-18 stsp return got_error(GOT_ERR_ANCESTRY);
1251 2ab43947 2020-03-18 stsp free(yca_id);
1252 2ab43947 2020-03-18 stsp return NULL;
1255 2ab43947 2020-03-18 stsp static const struct got_error *
1256 2ab43947 2020-03-18 stsp check_same_branch(struct got_object_id *commit_id,
1257 2ab43947 2020-03-18 stsp struct got_reference *head_ref, struct got_object_id *yca_id,
1258 2ab43947 2020-03-18 stsp struct got_repository *repo)
1260 2ab43947 2020-03-18 stsp const struct got_error *err = NULL;
1261 2ab43947 2020-03-18 stsp struct got_commit_graph *graph = NULL;
1262 2ab43947 2020-03-18 stsp struct got_object_id *head_commit_id = NULL;
1263 2ab43947 2020-03-18 stsp int is_same_branch = 0;
1265 2ab43947 2020-03-18 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
1267 2ab43947 2020-03-18 stsp goto done;
1269 2ab43947 2020-03-18 stsp if (got_object_id_cmp(head_commit_id, commit_id) == 0) {
1270 2ab43947 2020-03-18 stsp is_same_branch = 1;
1271 2ab43947 2020-03-18 stsp goto done;
1273 2ab43947 2020-03-18 stsp if (yca_id && got_object_id_cmp(commit_id, yca_id) == 0) {
1274 2ab43947 2020-03-18 stsp is_same_branch = 1;
1275 2ab43947 2020-03-18 stsp goto done;
1278 2ab43947 2020-03-18 stsp err = got_commit_graph_open(&graph, "/", 1);
1280 2ab43947 2020-03-18 stsp goto done;
1282 2ab43947 2020-03-18 stsp err = got_commit_graph_iter_start(graph, head_commit_id, repo,
1283 2ab43947 2020-03-18 stsp check_cancelled, NULL);
1285 2ab43947 2020-03-18 stsp goto done;
1287 2ab43947 2020-03-18 stsp for (;;) {
1288 2ab43947 2020-03-18 stsp struct got_object_id *id;
1289 2ab43947 2020-03-18 stsp err = got_commit_graph_iter_next(&id, graph, repo,
1290 2ab43947 2020-03-18 stsp check_cancelled, NULL);
1291 2ab43947 2020-03-18 stsp if (err) {
1292 2ab43947 2020-03-18 stsp if (err->code == GOT_ERR_ITER_COMPLETED)
1293 2ab43947 2020-03-18 stsp err = NULL;
1298 2ab43947 2020-03-18 stsp if (yca_id && got_object_id_cmp(id, yca_id) == 0)
1300 2ab43947 2020-03-18 stsp if (got_object_id_cmp(id, commit_id) == 0) {
1301 2ab43947 2020-03-18 stsp is_same_branch = 1;
1307 2ab43947 2020-03-18 stsp if (graph)
1308 2ab43947 2020-03-18 stsp got_commit_graph_close(graph);
1309 2ab43947 2020-03-18 stsp free(head_commit_id);
1310 2ab43947 2020-03-18 stsp if (!err && !is_same_branch)
1311 2ab43947 2020-03-18 stsp err = got_error(GOT_ERR_ANCESTRY);
1312 2ab43947 2020-03-18 stsp return err;
1315 4ed7e80c 2018-05-20 stsp static const struct got_error *
1316 4b6c9460 2020-03-05 stsp checkout_ancestry_error(struct got_reference *ref, const char *commit_id_str)
1318 4b6c9460 2020-03-05 stsp static char msg[512];
1319 4b6c9460 2020-03-05 stsp const char *branch_name;
1321 4b6c9460 2020-03-05 stsp if (got_ref_is_symbolic(ref))
1322 4b6c9460 2020-03-05 stsp branch_name = got_ref_get_symref_target(ref);
1324 4b6c9460 2020-03-05 stsp branch_name = got_ref_get_name(ref);
1326 4b6c9460 2020-03-05 stsp if (strncmp("refs/heads/", branch_name, 11) == 0)
1327 4b6c9460 2020-03-05 stsp branch_name += 11;
1329 4b6c9460 2020-03-05 stsp snprintf(msg, sizeof(msg),
1330 4b6c9460 2020-03-05 stsp "target commit is not contained in branch '%s'; "
1331 4b6c9460 2020-03-05 stsp "the branch to use must be specified with -b; "
1332 4b6c9460 2020-03-05 stsp "if necessary a new branch can be created for "
1333 4b6c9460 2020-03-05 stsp "this commit with 'got branch -c %s BRANCH_NAME'",
1334 4b6c9460 2020-03-05 stsp branch_name, commit_id_str);
1336 4b6c9460 2020-03-05 stsp return got_error_msg(GOT_ERR_ANCESTRY, msg);
1339 4b6c9460 2020-03-05 stsp static const struct got_error *
1340 c09a553d 2018-03-12 stsp cmd_checkout(int argc, char *argv[])
1342 c09a553d 2018-03-12 stsp const struct got_error *error = NULL;
1343 c09a553d 2018-03-12 stsp struct got_repository *repo = NULL;
1344 c09a553d 2018-03-12 stsp struct got_reference *head_ref = NULL;
1345 c09a553d 2018-03-12 stsp struct got_worktree *worktree = NULL;
1346 c09a553d 2018-03-12 stsp char *repo_path = NULL;
1347 c09a553d 2018-03-12 stsp char *worktree_path = NULL;
1348 0bb8a95e 2018-03-12 stsp const char *path_prefix = "";
1349 08573d5b 2019-05-14 stsp const char *branch_name = GOT_REF_HEAD;
1350 8069f636 2019-01-12 stsp char *commit_id_str = NULL;
1351 bb51a5b4 2020-01-13 stsp int ch, same_path_prefix, allow_nonempty = 0;
1352 f2ea84fa 2019-07-27 stsp struct got_pathlist_head paths;
1353 7f47418f 2019-12-20 stsp struct got_checkout_progress_arg cpa;
1355 f2ea84fa 2019-07-27 stsp TAILQ_INIT(&paths);
1357 bb51a5b4 2020-01-13 stsp while ((ch = getopt(argc, argv, "b:c:Ep:")) != -1) {
1358 0bb8a95e 2018-03-12 stsp switch (ch) {
1360 08573d5b 2019-05-14 stsp branch_name = optarg;
1363 8069f636 2019-01-12 stsp commit_id_str = strdup(optarg);
1364 8069f636 2019-01-12 stsp if (commit_id_str == NULL)
1365 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
1368 bb51a5b4 2020-01-13 stsp allow_nonempty = 1;
1371 0bb8a95e 2018-03-12 stsp path_prefix = optarg;
1374 2deda0b9 2019-03-07 stsp usage_checkout();
1375 0bb8a95e 2018-03-12 stsp /* NOTREACHED */
1379 0bb8a95e 2018-03-12 stsp argc -= optind;
1380 0bb8a95e 2018-03-12 stsp argv += optind;
1382 6715a751 2018-03-16 stsp #ifndef PROFILE
1383 68ed9ba5 2019-02-10 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1384 68ed9ba5 2019-02-10 stsp "unveil", NULL) == -1)
1385 c09a553d 2018-03-12 stsp err(1, "pledge");
1387 0bb8a95e 2018-03-12 stsp if (argc == 1) {
1388 c09a553d 2018-03-12 stsp char *cwd, *base, *dotgit;
1389 76089277 2018-04-01 stsp repo_path = realpath(argv[0], NULL);
1390 76089277 2018-04-01 stsp if (repo_path == NULL)
1391 638f9024 2019-05-13 stsp return got_error_from_errno2("realpath", argv[0]);
1392 c09a553d 2018-03-12 stsp cwd = getcwd(NULL, 0);
1393 76089277 2018-04-01 stsp if (cwd == NULL) {
1394 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
1395 76089277 2018-04-01 stsp goto done;
1397 230a42bd 2019-05-11 jcs if (path_prefix[0]) {
1398 230a42bd 2019-05-11 jcs base = basename(path_prefix);
1399 230a42bd 2019-05-11 jcs if (base == NULL) {
1400 638f9024 2019-05-13 stsp error = got_error_from_errno2("basename",
1401 230a42bd 2019-05-11 jcs path_prefix);
1405 5d7c1dab 2018-04-01 stsp base = basename(repo_path);
1406 230a42bd 2019-05-11 jcs if (base == NULL) {
1407 638f9024 2019-05-13 stsp error = got_error_from_errno2("basename",
1408 230a42bd 2019-05-11 jcs repo_path);
1412 c09a553d 2018-03-12 stsp dotgit = strstr(base, ".git");
1413 c09a553d 2018-03-12 stsp if (dotgit)
1414 c09a553d 2018-03-12 stsp *dotgit = '\0';
1415 c09a553d 2018-03-12 stsp if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
1416 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
1417 c09a553d 2018-03-12 stsp free(cwd);
1418 76089277 2018-04-01 stsp goto done;
1420 c09a553d 2018-03-12 stsp free(cwd);
1421 0bb8a95e 2018-03-12 stsp } else if (argc == 2) {
1422 76089277 2018-04-01 stsp repo_path = realpath(argv[0], NULL);
1423 76089277 2018-04-01 stsp if (repo_path == NULL) {
1424 638f9024 2019-05-13 stsp error = got_error_from_errno2("realpath", argv[0]);
1425 76089277 2018-04-01 stsp goto done;
1427 f7b38925 2018-04-01 stsp worktree_path = realpath(argv[1], NULL);
1428 76089277 2018-04-01 stsp if (worktree_path == NULL) {
1429 b4b3a7dd 2019-07-22 stsp if (errno != ENOENT) {
1430 b4b3a7dd 2019-07-22 stsp error = got_error_from_errno2("realpath",
1432 b4b3a7dd 2019-07-22 stsp goto done;
1434 b4b3a7dd 2019-07-22 stsp worktree_path = strdup(argv[1]);
1435 b4b3a7dd 2019-07-22 stsp if (worktree_path == NULL) {
1436 b4b3a7dd 2019-07-22 stsp error = got_error_from_errno("strdup");
1437 b4b3a7dd 2019-07-22 stsp goto done;
1441 c09a553d 2018-03-12 stsp usage_checkout();
1443 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
1444 72151b04 2019-05-11 stsp got_path_strip_trailing_slashes(worktree_path);
1446 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
1447 c09a553d 2018-03-12 stsp if (error != NULL)
1448 c02c541e 2019-03-29 stsp goto done;
1450 c530dc23 2019-07-23 stsp /* Pre-create work tree path for unveil(2) */
1451 c530dc23 2019-07-23 stsp error = got_path_mkdir(worktree_path);
1452 c530dc23 2019-07-23 stsp if (error) {
1453 80c1b583 2019-08-07 stsp if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
1454 80c1b583 2019-08-07 stsp !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
1455 c530dc23 2019-07-23 stsp goto done;
1456 bb51a5b4 2020-01-13 stsp if (!allow_nonempty &&
1457 bb51a5b4 2020-01-13 stsp !got_path_dir_is_empty(worktree_path)) {
1458 c530dc23 2019-07-23 stsp error = got_error_path(worktree_path,
1459 c530dc23 2019-07-23 stsp GOT_ERR_DIR_NOT_EMPTY);
1460 c530dc23 2019-07-23 stsp goto done;
1464 c530dc23 2019-07-23 stsp error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
1465 c02c541e 2019-03-29 stsp if (error)
1466 c09a553d 2018-03-12 stsp goto done;
1468 08573d5b 2019-05-14 stsp error = got_ref_open(&head_ref, repo, branch_name, 0);
1469 c09a553d 2018-03-12 stsp if (error != NULL)
1470 c09a553d 2018-03-12 stsp goto done;
1472 0bb8a95e 2018-03-12 stsp error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
1473 d70b8e30 2018-12-27 stsp if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
1474 c09a553d 2018-03-12 stsp goto done;
1476 c09a553d 2018-03-12 stsp error = got_worktree_open(&worktree, worktree_path);
1477 c09a553d 2018-03-12 stsp if (error != NULL)
1478 c09a553d 2018-03-12 stsp goto done;
1480 e5dc7198 2018-12-29 stsp error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
1481 e5dc7198 2018-12-29 stsp path_prefix);
1482 e5dc7198 2018-12-29 stsp if (error != NULL)
1483 e5dc7198 2018-12-29 stsp goto done;
1484 e5dc7198 2018-12-29 stsp if (!same_path_prefix) {
1485 49520a32 2018-12-29 stsp error = got_error(GOT_ERR_PATH_PREFIX);
1486 49520a32 2018-12-29 stsp goto done;
1489 8069f636 2019-01-12 stsp if (commit_id_str) {
1490 04f57cb3 2019-07-25 stsp struct got_object_id *commit_id;
1491 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
1492 71a27632 2020-01-15 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
1493 30837e32 2019-07-25 stsp if (error)
1494 8069f636 2019-01-12 stsp goto done;
1495 024e9686 2019-05-14 stsp error = check_linear_ancestry(commit_id,
1496 3aef623b 2019-10-15 stsp got_worktree_get_base_commit_id(worktree), 0, repo);
1497 8069f636 2019-01-12 stsp if (error != NULL) {
1498 8069f636 2019-01-12 stsp free(commit_id);
1499 4b6c9460 2020-03-05 stsp if (error->code == GOT_ERR_ANCESTRY) {
1500 4b6c9460 2020-03-05 stsp error = checkout_ancestry_error(
1501 4b6c9460 2020-03-05 stsp head_ref, commit_id_str);
1503 8069f636 2019-01-12 stsp goto done;
1505 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
1506 4b6c9460 2020-03-05 stsp if (error) {
1507 4b6c9460 2020-03-05 stsp if (error->code == GOT_ERR_ANCESTRY) {
1508 4b6c9460 2020-03-05 stsp error = checkout_ancestry_error(
1509 4b6c9460 2020-03-05 stsp head_ref, commit_id_str);
1511 45d344f6 2019-05-14 stsp goto done;
1513 8069f636 2019-01-12 stsp error = got_worktree_set_base_commit_id(worktree, repo,
1514 8069f636 2019-01-12 stsp commit_id);
1515 8069f636 2019-01-12 stsp free(commit_id);
1516 8069f636 2019-01-12 stsp if (error)
1517 8069f636 2019-01-12 stsp goto done;
1520 adc19d55 2019-07-28 stsp error = got_pathlist_append(&paths, "", NULL);
1521 f2ea84fa 2019-07-27 stsp if (error)
1522 f2ea84fa 2019-07-27 stsp goto done;
1523 7f47418f 2019-12-20 stsp cpa.worktree_path = worktree_path;
1524 7f47418f 2019-12-20 stsp cpa.had_base_commit_ref_error = 0;
1525 f2ea84fa 2019-07-27 stsp error = got_worktree_checkout_files(worktree, &paths, repo,
1526 7f47418f 2019-12-20 stsp checkout_progress, &cpa, check_cancelled, NULL);
1527 c09a553d 2018-03-12 stsp if (error != NULL)
1528 c09a553d 2018-03-12 stsp goto done;
1530 b65ae19a 2018-04-24 stsp printf("Now shut up and hack\n");
1531 7f47418f 2019-12-20 stsp if (cpa.had_base_commit_ref_error)
1532 7f47418f 2019-12-20 stsp show_worktree_base_ref_warning();
1534 f2ea84fa 2019-07-27 stsp got_pathlist_free(&paths);
1535 8069f636 2019-01-12 stsp free(commit_id_str);
1536 76089277 2018-04-01 stsp free(repo_path);
1537 507dc3bb 2018-12-29 stsp free(worktree_path);
1538 507dc3bb 2018-12-29 stsp return error;
1541 507dc3bb 2018-12-29 stsp __dead static void
1542 507dc3bb 2018-12-29 stsp usage_update(void)
1544 f2ea84fa 2019-07-27 stsp fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path ...]\n",
1545 507dc3bb 2018-12-29 stsp getprogname());
1549 1ee397ad 2019-07-12 stsp static const struct got_error *
1550 507dc3bb 2018-12-29 stsp update_progress(void *arg, unsigned char status, const char *path)
1552 784955db 2019-01-12 stsp int *did_something = arg;
1554 7f47418f 2019-12-20 stsp if (status == GOT_STATUS_EXISTS ||
1555 7f47418f 2019-12-20 stsp status == GOT_STATUS_BASE_REF_ERR)
1556 1ee397ad 2019-07-12 stsp return NULL;
1558 1545c615 2019-02-10 stsp *did_something = 1;
1560 a484d721 2019-06-10 stsp /* Base commit bump happens silently. */
1561 a484d721 2019-06-10 stsp if (status == GOT_STATUS_BUMP_BASE)
1562 1ee397ad 2019-07-12 stsp return NULL;
1564 507dc3bb 2018-12-29 stsp while (path[0] == '/')
1566 507dc3bb 2018-12-29 stsp printf("%c %s\n", status, path);
1567 1ee397ad 2019-07-12 stsp return NULL;
1570 be7061eb 2018-12-30 stsp static const struct got_error *
1571 a1fb16d8 2019-05-24 stsp switch_head_ref(struct got_reference *head_ref,
1572 a1fb16d8 2019-05-24 stsp struct got_object_id *commit_id, struct got_worktree *worktree,
1573 a1fb16d8 2019-05-24 stsp struct got_repository *repo)
1575 a1fb16d8 2019-05-24 stsp const struct got_error *err = NULL;
1576 a1fb16d8 2019-05-24 stsp char *base_id_str;
1577 a1fb16d8 2019-05-24 stsp int ref_has_moved = 0;
1579 a1fb16d8 2019-05-24 stsp /* Trivial case: switching between two different references. */
1580 a1fb16d8 2019-05-24 stsp if (strcmp(got_ref_get_name(head_ref),
1581 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree)) != 0) {
1582 a1fb16d8 2019-05-24 stsp printf("Switching work tree from %s to %s\n",
1583 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree),
1584 a1fb16d8 2019-05-24 stsp got_ref_get_name(head_ref));
1585 a1fb16d8 2019-05-24 stsp return got_worktree_set_head_ref(worktree, head_ref);
1588 a1fb16d8 2019-05-24 stsp err = check_linear_ancestry(commit_id,
1589 3aef623b 2019-10-15 stsp got_worktree_get_base_commit_id(worktree), 0, repo);
1590 a1fb16d8 2019-05-24 stsp if (err) {
1591 a1fb16d8 2019-05-24 stsp if (err->code != GOT_ERR_ANCESTRY)
1592 a1fb16d8 2019-05-24 stsp return err;
1593 a1fb16d8 2019-05-24 stsp ref_has_moved = 1;
1595 a1fb16d8 2019-05-24 stsp if (!ref_has_moved)
1596 a1fb16d8 2019-05-24 stsp return NULL;
1598 a1fb16d8 2019-05-24 stsp /* Switching to a rebased branch with the same reference name. */
1599 a1fb16d8 2019-05-24 stsp err = got_object_id_str(&base_id_str,
1600 a1fb16d8 2019-05-24 stsp got_worktree_get_base_commit_id(worktree));
1602 a1fb16d8 2019-05-24 stsp return err;
1603 a1fb16d8 2019-05-24 stsp printf("Reference %s now points at a different branch\n",
1604 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree));
1605 a1fb16d8 2019-05-24 stsp printf("Switching work tree from %s to %s\n", base_id_str,
1606 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree));
1607 0ebf8283 2019-07-24 stsp return NULL;
1610 0ebf8283 2019-07-24 stsp static const struct got_error *
1611 0ebf8283 2019-07-24 stsp check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
1613 0ebf8283 2019-07-24 stsp const struct got_error *err;
1614 0ebf8283 2019-07-24 stsp int in_progress;
1616 0ebf8283 2019-07-24 stsp err = got_worktree_rebase_in_progress(&in_progress, worktree);
1618 0ebf8283 2019-07-24 stsp return err;
1619 0ebf8283 2019-07-24 stsp if (in_progress)
1620 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_REBASING);
1622 0ebf8283 2019-07-24 stsp err = got_worktree_histedit_in_progress(&in_progress, worktree);
1624 0ebf8283 2019-07-24 stsp return err;
1625 0ebf8283 2019-07-24 stsp if (in_progress)
1626 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_HISTEDIT_BUSY);
1628 a1fb16d8 2019-05-24 stsp return NULL;
1631 a5edda0a 2019-07-27 stsp static const struct got_error *
1632 a5edda0a 2019-07-27 stsp get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
1633 a5edda0a 2019-07-27 stsp char *argv[], struct got_worktree *worktree)
1635 a0de39f3 2019-08-09 stsp const struct got_error *err = NULL;
1636 a5edda0a 2019-07-27 stsp char *path;
1639 a5edda0a 2019-07-27 stsp if (argc == 0) {
1640 a5edda0a 2019-07-27 stsp path = strdup("");
1641 a5edda0a 2019-07-27 stsp if (path == NULL)
1642 a5edda0a 2019-07-27 stsp return got_error_from_errno("strdup");
1643 adc19d55 2019-07-28 stsp return got_pathlist_append(paths, path, NULL);
1646 a5edda0a 2019-07-27 stsp for (i = 0; i < argc; i++) {
1647 a5edda0a 2019-07-27 stsp err = got_worktree_resolve_path(&path, worktree, argv[i]);
1650 adc19d55 2019-07-28 stsp err = got_pathlist_append(paths, path, NULL);
1651 a5edda0a 2019-07-27 stsp if (err) {
1652 a5edda0a 2019-07-27 stsp free(path);
1657 a5edda0a 2019-07-27 stsp return err;
1660 a1fb16d8 2019-05-24 stsp static const struct got_error *
1661 507dc3bb 2018-12-29 stsp cmd_update(int argc, char *argv[])
1663 507dc3bb 2018-12-29 stsp const struct got_error *error = NULL;
1664 507dc3bb 2018-12-29 stsp struct got_repository *repo = NULL;
1665 507dc3bb 2018-12-29 stsp struct got_worktree *worktree = NULL;
1666 f2ea84fa 2019-07-27 stsp char *worktree_path = NULL;
1667 507dc3bb 2018-12-29 stsp struct got_object_id *commit_id = NULL;
1668 9c4b8182 2019-01-02 stsp char *commit_id_str = NULL;
1669 024e9686 2019-05-14 stsp const char *branch_name = NULL;
1670 024e9686 2019-05-14 stsp struct got_reference *head_ref = NULL;
1671 f2ea84fa 2019-07-27 stsp struct got_pathlist_head paths;
1672 f2ea84fa 2019-07-27 stsp struct got_pathlist_entry *pe;
1673 0ebf8283 2019-07-24 stsp int ch, did_something = 0;
1675 f2ea84fa 2019-07-27 stsp TAILQ_INIT(&paths);
1677 024e9686 2019-05-14 stsp while ((ch = getopt(argc, argv, "b:c:")) != -1) {
1678 507dc3bb 2018-12-29 stsp switch (ch) {
1680 024e9686 2019-05-14 stsp branch_name = optarg;
1683 9c4b8182 2019-01-02 stsp commit_id_str = strdup(optarg);
1684 9c4b8182 2019-01-02 stsp if (commit_id_str == NULL)
1685 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
1688 2deda0b9 2019-03-07 stsp usage_update();
1689 507dc3bb 2018-12-29 stsp /* NOTREACHED */
1693 507dc3bb 2018-12-29 stsp argc -= optind;
1694 507dc3bb 2018-12-29 stsp argv += optind;
1696 507dc3bb 2018-12-29 stsp #ifndef PROFILE
1697 68ed9ba5 2019-02-10 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1698 68ed9ba5 2019-02-10 stsp "unveil", NULL) == -1)
1699 507dc3bb 2018-12-29 stsp err(1, "pledge");
1701 c4cdcb68 2019-04-03 stsp worktree_path = getcwd(NULL, 0);
1702 c4cdcb68 2019-04-03 stsp if (worktree_path == NULL) {
1703 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
1704 c4cdcb68 2019-04-03 stsp goto done;
1706 c4cdcb68 2019-04-03 stsp error = got_worktree_open(&worktree, worktree_path);
1707 7d5807f4 2019-07-11 stsp if (error)
1708 7d5807f4 2019-07-11 stsp goto done;
1710 0ebf8283 2019-07-24 stsp error = check_rebase_or_histedit_in_progress(worktree);
1711 f2ea84fa 2019-07-27 stsp if (error)
1712 f2ea84fa 2019-07-27 stsp goto done;
1714 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
1716 507dc3bb 2018-12-29 stsp if (error != NULL)
1717 507dc3bb 2018-12-29 stsp goto done;
1719 97430839 2019-03-11 stsp error = apply_unveil(got_repo_get_path(repo), 0,
1720 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
1721 087fb88c 2019-08-04 stsp if (error)
1722 087fb88c 2019-08-04 stsp goto done;
1724 087fb88c 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
1725 0266afb7 2019-01-04 stsp if (error)
1726 0266afb7 2019-01-04 stsp goto done;
1728 a1fb16d8 2019-05-24 stsp error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
1729 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree), 0);
1730 024e9686 2019-05-14 stsp if (error != NULL)
1731 024e9686 2019-05-14 stsp goto done;
1732 507dc3bb 2018-12-29 stsp if (commit_id_str == NULL) {
1733 507dc3bb 2018-12-29 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
1734 9c4b8182 2019-01-02 stsp if (error != NULL)
1735 9c4b8182 2019-01-02 stsp goto done;
1736 9c4b8182 2019-01-02 stsp error = got_object_id_str(&commit_id_str, commit_id);
1737 507dc3bb 2018-12-29 stsp if (error != NULL)
1738 507dc3bb 2018-12-29 stsp goto done;
1740 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
1741 71a27632 2020-01-15 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
1742 04f57cb3 2019-07-25 stsp free(commit_id_str);
1743 bb787f09 2019-07-25 stsp commit_id_str = NULL;
1744 30837e32 2019-07-25 stsp if (error)
1745 a297e751 2019-07-11 stsp goto done;
1746 a297e751 2019-07-11 stsp error = got_object_id_str(&commit_id_str, commit_id);
1747 a297e751 2019-07-11 stsp if (error)
1748 507dc3bb 2018-12-29 stsp goto done;
1751 a1fb16d8 2019-05-24 stsp if (branch_name) {
1752 024e9686 2019-05-14 stsp struct got_object_id *head_commit_id;
1753 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, &paths, entry) {
1754 f2b16ada 2019-08-02 stsp if (pe->path_len == 0)
1756 f2ea84fa 2019-07-27 stsp error = got_error_msg(GOT_ERR_BAD_PATH,
1757 f2ea84fa 2019-07-27 stsp "switching between branches requires that "
1758 f2ea84fa 2019-07-27 stsp "the entire work tree gets updated");
1759 024e9686 2019-05-14 stsp goto done;
1761 024e9686 2019-05-14 stsp error = got_ref_resolve(&head_commit_id, repo, head_ref);
1762 024e9686 2019-05-14 stsp if (error)
1763 024e9686 2019-05-14 stsp goto done;
1764 3aef623b 2019-10-15 stsp error = check_linear_ancestry(commit_id, head_commit_id, 0,
1766 a367ff0f 2019-05-14 stsp free(head_commit_id);
1767 024e9686 2019-05-14 stsp if (error != NULL)
1768 024e9686 2019-05-14 stsp goto done;
1769 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
1770 a367ff0f 2019-05-14 stsp if (error)
1771 a367ff0f 2019-05-14 stsp goto done;
1772 a1fb16d8 2019-05-24 stsp error = switch_head_ref(head_ref, commit_id, worktree, repo);
1773 024e9686 2019-05-14 stsp if (error)
1774 024e9686 2019-05-14 stsp goto done;
1776 024e9686 2019-05-14 stsp error = check_linear_ancestry(commit_id,
1777 3aef623b 2019-10-15 stsp got_worktree_get_base_commit_id(worktree), 0, repo);
1778 a367ff0f 2019-05-14 stsp if (error != NULL) {
1779 a367ff0f 2019-05-14 stsp if (error->code == GOT_ERR_ANCESTRY)
1780 a367ff0f 2019-05-14 stsp error = got_error(GOT_ERR_BRANCH_MOVED);
1781 024e9686 2019-05-14 stsp goto done;
1783 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
1784 a367ff0f 2019-05-14 stsp if (error)
1785 a367ff0f 2019-05-14 stsp goto done;
1788 507dc3bb 2018-12-29 stsp if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
1789 507dc3bb 2018-12-29 stsp commit_id) != 0) {
1790 507dc3bb 2018-12-29 stsp error = got_worktree_set_base_commit_id(worktree, repo,
1791 507dc3bb 2018-12-29 stsp commit_id);
1792 507dc3bb 2018-12-29 stsp if (error)
1793 507dc3bb 2018-12-29 stsp goto done;
1796 f2ea84fa 2019-07-27 stsp error = got_worktree_checkout_files(worktree, &paths, repo,
1797 6bad629b 2019-02-04 stsp update_progress, &did_something, check_cancelled, NULL);
1798 507dc3bb 2018-12-29 stsp if (error != NULL)
1799 507dc3bb 2018-12-29 stsp goto done;
1801 784955db 2019-01-12 stsp if (did_something)
1802 784955db 2019-01-12 stsp printf("Updated to commit %s\n", commit_id_str);
1804 784955db 2019-01-12 stsp printf("Already up-to-date\n");
1806 c09a553d 2018-03-12 stsp free(worktree_path);
1807 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, &paths, entry)
1808 f2ea84fa 2019-07-27 stsp free((char *)pe->path);
1809 f2ea84fa 2019-07-27 stsp got_pathlist_free(&paths);
1810 507dc3bb 2018-12-29 stsp free(commit_id);
1811 9c4b8182 2019-01-02 stsp free(commit_id_str);
1812 c09a553d 2018-03-12 stsp return error;
1815 f42b1b34 2018-03-12 stsp static const struct got_error *
1816 44392932 2019-08-25 stsp diff_blobs(struct got_object_id *blob_id1, struct got_object_id *blob_id2,
1817 63035f9f 2019-10-06 stsp const char *path, int diff_context, int ignore_whitespace,
1818 63035f9f 2019-10-06 stsp struct got_repository *repo)
1820 f42b1b34 2018-03-12 stsp const struct got_error *err = NULL;
1821 44392932 2019-08-25 stsp struct got_blob_object *blob1 = NULL, *blob2 = NULL;
1823 44392932 2019-08-25 stsp if (blob_id1) {
1824 44392932 2019-08-25 stsp err = got_object_open_as_blob(&blob1, repo, blob_id1, 8192);
1826 44392932 2019-08-25 stsp goto done;
1829 44392932 2019-08-25 stsp err = got_object_open_as_blob(&blob2, repo, blob_id2, 8192);
1831 44392932 2019-08-25 stsp goto done;
1833 44392932 2019-08-25 stsp while (path[0] == '/')
1835 44392932 2019-08-25 stsp err = got_diff_blob(blob1, blob2, path, path, diff_context,
1836 63035f9f 2019-10-06 stsp ignore_whitespace, stdout);
1838 44392932 2019-08-25 stsp if (blob1)
1839 44392932 2019-08-25 stsp got_object_blob_close(blob1);
1840 44392932 2019-08-25 stsp got_object_blob_close(blob2);
1841 44392932 2019-08-25 stsp return err;
1844 44392932 2019-08-25 stsp static const struct got_error *
1845 44392932 2019-08-25 stsp diff_trees(struct got_object_id *tree_id1, struct got_object_id *tree_id2,
1846 63035f9f 2019-10-06 stsp const char *path, int diff_context, int ignore_whitespace,
1847 63035f9f 2019-10-06 stsp struct got_repository *repo)
1849 44392932 2019-08-25 stsp const struct got_error *err = NULL;
1850 44392932 2019-08-25 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
1851 44392932 2019-08-25 stsp struct got_diff_blob_output_unidiff_arg arg;
1853 44392932 2019-08-25 stsp if (tree_id1) {
1854 44392932 2019-08-25 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
1856 44392932 2019-08-25 stsp goto done;
1859 44392932 2019-08-25 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
1861 0f2b3dca 2018-12-22 stsp goto done;
1863 aaa13589 2019-06-01 stsp arg.diff_context = diff_context;
1864 63035f9f 2019-10-06 stsp arg.ignore_whitespace = ignore_whitespace;
1865 aaa13589 2019-06-01 stsp arg.outfile = stdout;
1866 44392932 2019-08-25 stsp while (path[0] == '/')
1868 44392932 2019-08-25 stsp err = got_diff_tree(tree1, tree2, path, path, repo,
1869 31b4484f 2019-07-27 stsp got_diff_blob_output_unidiff, &arg, 1);
1871 79109fed 2018-03-27 stsp if (tree1)
1872 79109fed 2018-03-27 stsp got_object_tree_close(tree1);
1873 366e0a5f 2019-10-10 stsp if (tree2)
1874 366e0a5f 2019-10-10 stsp got_object_tree_close(tree2);
1875 44392932 2019-08-25 stsp return err;
1878 44392932 2019-08-25 stsp static const struct got_error *
1879 44392932 2019-08-25 stsp print_patch(struct got_commit_object *commit, struct got_object_id *id,
1880 44392932 2019-08-25 stsp const char *path, int diff_context, struct got_repository *repo)
1882 44392932 2019-08-25 stsp const struct got_error *err = NULL;
1883 44392932 2019-08-25 stsp struct got_commit_object *pcommit = NULL;
1884 44392932 2019-08-25 stsp char *id_str1 = NULL, *id_str2 = NULL;
1885 44392932 2019-08-25 stsp struct got_object_id *obj_id1 = NULL, *obj_id2 = NULL;
1886 44392932 2019-08-25 stsp struct got_object_qid *qid;
1888 44392932 2019-08-25 stsp qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1889 44392932 2019-08-25 stsp if (qid != NULL) {
1890 44392932 2019-08-25 stsp err = got_object_open_as_commit(&pcommit, repo,
1893 44392932 2019-08-25 stsp return err;
1896 44392932 2019-08-25 stsp if (path && path[0] != '\0') {
1897 44392932 2019-08-25 stsp int obj_type;
1898 44392932 2019-08-25 stsp err = got_object_id_by_path(&obj_id2, repo, id, path);
1900 44392932 2019-08-25 stsp goto done;
1901 44392932 2019-08-25 stsp err = got_object_id_str(&id_str2, obj_id2);
1902 44392932 2019-08-25 stsp if (err) {
1903 44392932 2019-08-25 stsp free(obj_id2);
1904 44392932 2019-08-25 stsp goto done;
1906 44392932 2019-08-25 stsp if (pcommit) {
1907 44392932 2019-08-25 stsp err = got_object_id_by_path(&obj_id1, repo,
1908 44392932 2019-08-25 stsp qid->id, path);
1909 44392932 2019-08-25 stsp if (err) {
1910 44392932 2019-08-25 stsp free(obj_id2);
1911 44392932 2019-08-25 stsp goto done;
1913 44392932 2019-08-25 stsp err = got_object_id_str(&id_str1, obj_id1);
1914 44392932 2019-08-25 stsp if (err) {
1915 44392932 2019-08-25 stsp free(obj_id2);
1916 44392932 2019-08-25 stsp goto done;
1919 44392932 2019-08-25 stsp err = got_object_get_type(&obj_type, repo, obj_id2);
1920 44392932 2019-08-25 stsp if (err) {
1921 44392932 2019-08-25 stsp free(obj_id2);
1922 44392932 2019-08-25 stsp goto done;
1924 44392932 2019-08-25 stsp printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1925 44392932 2019-08-25 stsp switch (obj_type) {
1926 44392932 2019-08-25 stsp case GOT_OBJ_TYPE_BLOB:
1927 44392932 2019-08-25 stsp err = diff_blobs(obj_id1, obj_id2, path, diff_context,
1930 44392932 2019-08-25 stsp case GOT_OBJ_TYPE_TREE:
1931 44392932 2019-08-25 stsp err = diff_trees(obj_id1, obj_id2, path, diff_context,
1935 44392932 2019-08-25 stsp err = got_error(GOT_ERR_OBJ_TYPE);
1938 44392932 2019-08-25 stsp free(obj_id1);
1939 44392932 2019-08-25 stsp free(obj_id2);
1941 44392932 2019-08-25 stsp obj_id2 = got_object_commit_get_tree_id(commit);
1942 44392932 2019-08-25 stsp err = got_object_id_str(&id_str2, obj_id2);
1944 44392932 2019-08-25 stsp goto done;
1945 44392932 2019-08-25 stsp obj_id1 = got_object_commit_get_tree_id(pcommit);
1946 44392932 2019-08-25 stsp err = got_object_id_str(&id_str1, obj_id1);
1948 44392932 2019-08-25 stsp goto done;
1949 44392932 2019-08-25 stsp printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1950 63035f9f 2019-10-06 stsp err = diff_trees(obj_id1, obj_id2, "", diff_context, 0, repo);
1953 0f2b3dca 2018-12-22 stsp free(id_str1);
1954 0f2b3dca 2018-12-22 stsp free(id_str2);
1955 44392932 2019-08-25 stsp if (pcommit)
1956 44392932 2019-08-25 stsp got_object_commit_close(pcommit);
1957 79109fed 2018-03-27 stsp return err;
1960 4bb494d5 2018-06-16 stsp static char *
1961 6c281f94 2018-06-11 stsp get_datestr(time_t *time, char *datebuf)
1963 09867e48 2019-08-13 stsp struct tm mytm, *tm;
1964 09867e48 2019-08-13 stsp char *p, *s;
1966 09867e48 2019-08-13 stsp tm = gmtime_r(time, &mytm);
1967 09867e48 2019-08-13 stsp if (tm == NULL)
1968 09867e48 2019-08-13 stsp return NULL;
1969 09867e48 2019-08-13 stsp s = asctime_r(tm, datebuf);
1970 09867e48 2019-08-13 stsp if (s == NULL)
1971 09867e48 2019-08-13 stsp return NULL;
1972 6c281f94 2018-06-11 stsp p = strchr(s, '\n');
1974 6c281f94 2018-06-11 stsp *p = '\0';
1978 6841bf13 2019-11-29 kn static const struct got_error *
1979 6841bf13 2019-11-29 kn match_logmsg(int *have_match, struct got_object_id *id,
1980 6841bf13 2019-11-29 kn struct got_commit_object *commit, regex_t *regex)
1982 6841bf13 2019-11-29 kn const struct got_error *err = NULL;
1983 6841bf13 2019-11-29 kn regmatch_t regmatch;
1984 6841bf13 2019-11-29 kn char *id_str = NULL, *logmsg = NULL;
1986 6841bf13 2019-11-29 kn *have_match = 0;
1988 6841bf13 2019-11-29 kn err = got_object_id_str(&id_str, id);
1992 6841bf13 2019-11-29 kn err = got_object_commit_get_logmsg(&logmsg, commit);
1996 6841bf13 2019-11-29 kn if (regexec(regex, logmsg, 1, ®match, 0) == 0)
1997 6841bf13 2019-11-29 kn *have_match = 1;
1999 6841bf13 2019-11-29 kn free(id_str);
2000 6841bf13 2019-11-29 kn free(logmsg);
2004 dc424a06 2019-08-07 stsp #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
2006 79109fed 2018-03-27 stsp static const struct got_error *
2007 79109fed 2018-03-27 stsp print_commit(struct got_commit_object *commit, struct got_object_id *id,
2008 44392932 2019-08-25 stsp struct got_repository *repo, const char *path, int show_patch,
2009 44392932 2019-08-25 stsp int diff_context, struct got_reflist_head *refs)
2011 79109fed 2018-03-27 stsp const struct got_error *err = NULL;
2012 621015ac 2018-07-23 stsp char *id_str, *datestr, *logmsg0, *logmsg, *line;
2013 6c281f94 2018-06-11 stsp char datebuf[26];
2014 45d799e2 2018-12-23 stsp time_t committer_time;
2015 45d799e2 2018-12-23 stsp const char *author, *committer;
2016 199a4027 2019-02-02 stsp char *refs_str = NULL;
2017 199a4027 2019-02-02 stsp struct got_reflist_entry *re;
2019 199a4027 2019-02-02 stsp SIMPLEQ_FOREACH(re, refs, entry) {
2021 199a4027 2019-02-02 stsp const char *name;
2022 a436ad14 2019-08-13 stsp struct got_tag_object *tag = NULL;
2025 199a4027 2019-02-02 stsp name = got_ref_get_name(re->ref);
2026 d9498b20 2019-02-05 stsp if (strcmp(name, GOT_REF_HEAD) == 0)
2028 199a4027 2019-02-02 stsp if (strncmp(name, "refs/", 5) == 0)
2029 199a4027 2019-02-02 stsp name += 5;
2030 7143d404 2019-03-12 stsp if (strncmp(name, "got/", 4) == 0)
2032 e34f9ed6 2019-02-02 stsp if (strncmp(name, "heads/", 6) == 0)
2033 e34f9ed6 2019-02-02 stsp name += 6;
2034 141c2bff 2019-02-04 stsp if (strncmp(name, "remotes/", 8) == 0)
2035 141c2bff 2019-02-04 stsp name += 8;
2036 a436ad14 2019-08-13 stsp if (strncmp(name, "tags/", 5) == 0) {
2037 a436ad14 2019-08-13 stsp err = got_object_open_as_tag(&tag, repo, re->id);
2038 5d844a1e 2019-08-13 stsp if (err) {
2039 5d844a1e 2019-08-13 stsp if (err->code != GOT_ERR_OBJ_TYPE)
2040 5d844a1e 2019-08-13 stsp return err;
2041 5d844a1e 2019-08-13 stsp /* Ref points at something other than a tag. */
2042 5d844a1e 2019-08-13 stsp err = NULL;
2043 5d844a1e 2019-08-13 stsp tag = NULL;
2046 a436ad14 2019-08-13 stsp cmp = got_object_id_cmp(tag ?
2047 a436ad14 2019-08-13 stsp got_object_tag_get_object_id(tag) : re->id, id);
2049 a436ad14 2019-08-13 stsp got_object_tag_close(tag);
2050 a436ad14 2019-08-13 stsp if (cmp != 0)
2052 199a4027 2019-02-02 stsp s = refs_str;
2053 199a4027 2019-02-02 stsp if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
2054 199a4027 2019-02-02 stsp name) == -1) {
2055 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2057 34ca4898 2019-08-13 stsp return err;
2061 832c249c 2018-06-10 stsp err = got_object_id_str(&id_str, id);
2063 8bf5b3c9 2018-03-17 stsp return err;
2065 dc424a06 2019-08-07 stsp printf(GOT_COMMIT_SEP_STR);
2066 199a4027 2019-02-02 stsp printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
2067 199a4027 2019-02-02 stsp refs_str ? refs_str : "", refs_str ? ")" : "");
2068 832c249c 2018-06-10 stsp free(id_str);
2069 d3d493d7 2019-02-21 stsp id_str = NULL;
2070 d3d493d7 2019-02-21 stsp free(refs_str);
2071 d3d493d7 2019-02-21 stsp refs_str = NULL;
2072 45d799e2 2018-12-23 stsp printf("from: %s\n", got_object_commit_get_author(commit));
2073 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
2074 45d799e2 2018-12-23 stsp datestr = get_datestr(&committer_time, datebuf);
2075 09867e48 2019-08-13 stsp if (datestr)
2076 09867e48 2019-08-13 stsp printf("date: %s UTC\n", datestr);
2077 45d799e2 2018-12-23 stsp author = got_object_commit_get_author(commit);
2078 45d799e2 2018-12-23 stsp committer = got_object_commit_get_committer(commit);
2079 45d799e2 2018-12-23 stsp if (strcmp(author, committer) != 0)
2080 45d799e2 2018-12-23 stsp printf("via: %s\n", committer);
2081 45d799e2 2018-12-23 stsp if (got_object_commit_get_nparents(commit) > 1) {
2082 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
2083 79f35eb3 2018-06-11 stsp struct got_object_qid *qid;
2084 3fe1abad 2018-06-10 stsp int n = 1;
2085 45d799e2 2018-12-23 stsp parent_ids = got_object_commit_get_parent_ids(commit);
2086 45d799e2 2018-12-23 stsp SIMPLEQ_FOREACH(qid, parent_ids, entry) {
2087 79f35eb3 2018-06-11 stsp err = got_object_id_str(&id_str, qid->id);
2089 a0603db2 2018-06-10 stsp return err;
2090 3fe1abad 2018-06-10 stsp printf("parent %d: %s\n", n++, id_str);
2091 a0603db2 2018-06-10 stsp free(id_str);
2095 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
2097 5943eee2 2019-08-13 stsp return err;
2099 621015ac 2018-07-23 stsp logmsg = logmsg0;
2101 832c249c 2018-06-10 stsp line = strsep(&logmsg, "\n");
2103 832c249c 2018-06-10 stsp printf(" %s\n", line);
2104 832c249c 2018-06-10 stsp } while (line);
2105 621015ac 2018-07-23 stsp free(logmsg0);
2107 971751ac 2018-03-27 stsp if (show_patch) {
2108 44392932 2019-08-25 stsp err = print_patch(commit, id, path, diff_context, repo);
2109 971751ac 2018-03-27 stsp if (err == 0)
2110 971751ac 2018-03-27 stsp printf("\n");
2113 cbe7f848 2019-02-11 stsp if (fflush(stdout) != 0 && err == NULL)
2114 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
2115 07862c20 2018-09-15 stsp return err;
2118 f42b1b34 2018-03-12 stsp static const struct got_error *
2119 15a94983 2018-12-23 stsp print_commits(struct got_object_id *root_id, struct got_repository *repo,
2120 463a997a 2019-12-08 kn const char *path, int show_patch, const char *search_pattern,
2121 48c8c60d 2020-01-27 stsp int diff_context, int limit, int log_branches,
2122 463a997a 2019-12-08 kn struct got_reflist_head *refs)
2124 f42b1b34 2018-03-12 stsp const struct got_error *err;
2125 372ccdbb 2018-06-10 stsp struct got_commit_graph *graph;
2126 6841bf13 2019-11-29 kn regex_t regex;
2127 6841bf13 2019-11-29 kn int have_match;
2129 6841bf13 2019-11-29 kn if (search_pattern &&
2130 6841bf13 2019-11-29 kn regcomp(®ex, search_pattern, REG_EXTENDED | REG_NOSUB | REG_NEWLINE))
2131 6841bf13 2019-11-29 kn return got_error_msg(GOT_ERR_REGEX, search_pattern);
2133 48c8c60d 2020-01-27 stsp err = got_commit_graph_open(&graph, path, !log_branches);
2135 f42b1b34 2018-03-12 stsp return err;
2136 6fb7cd11 2019-08-22 stsp err = got_commit_graph_iter_start(graph, root_id, repo,
2137 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
2139 fcc85cad 2018-07-22 stsp goto done;
2141 372ccdbb 2018-06-10 stsp struct got_commit_object *commit;
2142 372ccdbb 2018-06-10 stsp struct got_object_id *id;
2144 84453469 2018-11-11 stsp if (sigint_received || sigpipe_received)
2147 ee780d5c 2020-01-04 stsp err = got_commit_graph_iter_next(&id, graph, repo,
2148 ee780d5c 2020-01-04 stsp check_cancelled, NULL);
2149 372ccdbb 2018-06-10 stsp if (err) {
2150 ee780d5c 2020-01-04 stsp if (err->code == GOT_ERR_ITER_COMPLETED)
2151 9ba79e04 2018-06-11 stsp err = NULL;
2154 b43fbaa0 2018-06-11 stsp if (id == NULL)
2157 f8e900f3 2018-06-11 stsp err = got_object_open_as_commit(&commit, repo, id);
2161 6841bf13 2019-11-29 kn if (search_pattern) {
2162 6841bf13 2019-11-29 kn err = match_logmsg(&have_match, id, commit, ®ex);
2164 6841bf13 2019-11-29 kn got_object_commit_close(commit);
2167 6841bf13 2019-11-29 kn if (have_match == 0) {
2168 6841bf13 2019-11-29 kn got_object_commit_close(commit);
2173 44392932 2019-08-25 stsp err = print_commit(commit, id, repo, path, show_patch,
2174 44392932 2019-08-25 stsp diff_context, refs);
2175 b43fbaa0 2018-06-11 stsp got_object_commit_close(commit);
2176 372ccdbb 2018-06-10 stsp if (err || (limit && --limit == 0))
2180 6841bf13 2019-11-29 kn if (search_pattern)
2181 6841bf13 2019-11-29 kn regfree(®ex);
2182 372ccdbb 2018-06-10 stsp got_commit_graph_close(graph);
2183 f42b1b34 2018-03-12 stsp return err;
2186 4ed7e80c 2018-05-20 stsp __dead static void
2187 6f3d1eb0 2018-03-12 stsp usage_log(void)
2189 48c8c60d 2020-01-27 stsp fprintf(stderr, "usage: %s log [-b] [-c commit] [-C number] [ -l N ] [-p] "
2190 6841bf13 2019-11-29 kn "[-s search-pattern] [-r repository-path] [path]\n", getprogname());
2194 b1ebc001 2019-08-13 stsp static int
2195 b1ebc001 2019-08-13 stsp get_default_log_limit(void)
2197 b1ebc001 2019-08-13 stsp const char *got_default_log_limit;
2198 b1ebc001 2019-08-13 stsp long long n;
2199 b1ebc001 2019-08-13 stsp const char *errstr;
2201 b1ebc001 2019-08-13 stsp got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
2202 b1ebc001 2019-08-13 stsp if (got_default_log_limit == NULL)
2204 b1ebc001 2019-08-13 stsp n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
2205 b1ebc001 2019-08-13 stsp if (errstr != NULL)
2210 4ed7e80c 2018-05-20 stsp static const struct got_error *
2211 f42b1b34 2018-03-12 stsp cmd_log(int argc, char *argv[])
2213 f42b1b34 2018-03-12 stsp const struct got_error *error;
2214 04ca23f4 2018-07-16 stsp struct got_repository *repo = NULL;
2215 cffc0aa4 2019-02-05 stsp struct got_worktree *worktree = NULL;
2216 15a94983 2018-12-23 stsp struct got_commit_object *commit = NULL;
2217 3235492e 2018-04-01 stsp struct got_object_id *id = NULL;
2218 04ca23f4 2018-07-16 stsp char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
2219 463a997a 2019-12-08 kn const char *start_commit = NULL, *search_pattern = NULL;
2220 dc1edbfa 2019-11-29 kn int diff_context = -1, ch;
2221 48c8c60d 2020-01-27 stsp int show_patch = 0, limit = 0, log_branches = 0;
2222 64a96a6d 2018-04-01 stsp const char *errstr;
2223 199a4027 2019-02-02 stsp struct got_reflist_head refs;
2225 1b3893a2 2019-03-18 stsp SIMPLEQ_INIT(&refs);
2227 6715a751 2018-03-16 stsp #ifndef PROFILE
2228 6098196c 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2231 f42b1b34 2018-03-12 stsp err(1, "pledge");
2234 b1ebc001 2019-08-13 stsp limit = get_default_log_limit();
2236 48c8c60d 2020-01-27 stsp while ((ch = getopt(argc, argv, "bpc:C:l:r:s:")) != -1) {
2237 79109fed 2018-03-27 stsp switch (ch) {
2239 79109fed 2018-03-27 stsp show_patch = 1;
2242 d142fc45 2018-04-01 stsp start_commit = optarg;
2245 4a8520aa 2018-10-18 stsp diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
2247 c0cc5c62 2018-10-18 stsp if (errstr != NULL)
2248 c0cc5c62 2018-10-18 stsp err(1, "-C option %s", errstr);
2251 b1ebc001 2019-08-13 stsp limit = strtonum(optarg, 0, INT_MAX, &errstr);
2252 64a96a6d 2018-04-01 stsp if (errstr != NULL)
2253 64a96a6d 2018-04-01 stsp err(1, "-l option %s", errstr);
2256 48c8c60d 2020-01-27 stsp log_branches = 1;
2259 04ca23f4 2018-07-16 stsp repo_path = realpath(optarg, NULL);
2260 04ca23f4 2018-07-16 stsp if (repo_path == NULL)
2261 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
2263 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
2266 6841bf13 2019-11-29 kn search_pattern = optarg;
2269 2deda0b9 2019-03-07 stsp usage_log();
2270 79109fed 2018-03-27 stsp /* NOTREACHED */
2274 79109fed 2018-03-27 stsp argc -= optind;
2275 79109fed 2018-03-27 stsp argv += optind;
2277 dc1edbfa 2019-11-29 kn if (diff_context == -1)
2278 dc1edbfa 2019-11-29 kn diff_context = 3;
2279 dc1edbfa 2019-11-29 kn else if (!show_patch)
2280 dc1edbfa 2019-11-29 kn errx(1, "-C reguires -p");
2282 04ca23f4 2018-07-16 stsp cwd = getcwd(NULL, 0);
2283 04ca23f4 2018-07-16 stsp if (cwd == NULL) {
2284 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2285 04ca23f4 2018-07-16 stsp goto done;
2288 cffc0aa4 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
2289 cffc0aa4 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
2290 cffc0aa4 2019-02-05 stsp goto done;
2291 cffc0aa4 2019-02-05 stsp error = NULL;
2293 e7301579 2019-03-18 stsp if (argc == 0) {
2294 cbd1af7a 2019-03-18 stsp path = strdup("");
2295 e7301579 2019-03-18 stsp if (path == NULL) {
2296 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2297 cbd1af7a 2019-03-18 stsp goto done;
2299 e7301579 2019-03-18 stsp } else if (argc == 1) {
2300 e7301579 2019-03-18 stsp if (worktree) {
2301 e7301579 2019-03-18 stsp error = got_worktree_resolve_path(&path, worktree,
2303 e7301579 2019-03-18 stsp if (error)
2304 e7301579 2019-03-18 stsp goto done;
2306 e7301579 2019-03-18 stsp path = strdup(argv[0]);
2307 e7301579 2019-03-18 stsp if (path == NULL) {
2308 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2309 e7301579 2019-03-18 stsp goto done;
2313 cbd1af7a 2019-03-18 stsp usage_log();
2315 5486daa2 2019-05-11 stsp if (repo_path == NULL) {
2316 5486daa2 2019-05-11 stsp repo_path = worktree ?
2317 5486daa2 2019-05-11 stsp strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
2319 04ca23f4 2018-07-16 stsp if (repo_path == NULL) {
2320 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2321 cffc0aa4 2019-02-05 stsp goto done;
2324 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
2325 d7d4f210 2018-03-12 stsp if (error != NULL)
2326 04ca23f4 2018-07-16 stsp goto done;
2328 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), 1,
2329 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
2330 c02c541e 2019-03-29 stsp if (error)
2331 c02c541e 2019-03-29 stsp goto done;
2333 d142fc45 2018-04-01 stsp if (start_commit == NULL) {
2334 3235492e 2018-04-01 stsp struct got_reference *head_ref;
2335 1cc14b9f 2019-05-14 stsp error = got_ref_open(&head_ref, repo,
2336 1cc14b9f 2019-05-14 stsp worktree ? got_worktree_get_head_ref_name(worktree)
2337 1cc14b9f 2019-05-14 stsp : GOT_REF_HEAD, 0);
2338 3235492e 2018-04-01 stsp if (error != NULL)
2339 3235492e 2018-04-01 stsp return error;
2340 3235492e 2018-04-01 stsp error = got_ref_resolve(&id, repo, head_ref);
2341 3235492e 2018-04-01 stsp got_ref_close(head_ref);
2342 3235492e 2018-04-01 stsp if (error != NULL)
2343 3235492e 2018-04-01 stsp return error;
2344 15a94983 2018-12-23 stsp error = got_object_open_as_commit(&commit, repo, id);
2346 d1f2edc9 2018-06-13 stsp struct got_reference *ref;
2347 2f17228e 2019-05-12 stsp error = got_ref_open(&ref, repo, start_commit, 0);
2348 3235492e 2018-04-01 stsp if (error == NULL) {
2349 1caad61b 2019-02-01 stsp int obj_type;
2350 d1f2edc9 2018-06-13 stsp error = got_ref_resolve(&id, repo, ref);
2351 d1f2edc9 2018-06-13 stsp got_ref_close(ref);
2352 d1f2edc9 2018-06-13 stsp if (error != NULL)
2353 1caad61b 2019-02-01 stsp goto done;
2354 1caad61b 2019-02-01 stsp error = got_object_get_type(&obj_type, repo, id);
2355 1caad61b 2019-02-01 stsp if (error != NULL)
2356 1caad61b 2019-02-01 stsp goto done;
2357 1caad61b 2019-02-01 stsp if (obj_type == GOT_OBJ_TYPE_TAG) {
2358 1caad61b 2019-02-01 stsp struct got_tag_object *tag;
2359 1caad61b 2019-02-01 stsp error = got_object_open_as_tag(&tag, repo, id);
2360 1caad61b 2019-02-01 stsp if (error != NULL)
2361 1caad61b 2019-02-01 stsp goto done;
2362 1caad61b 2019-02-01 stsp if (got_object_tag_get_object_type(tag) !=
2363 1caad61b 2019-02-01 stsp GOT_OBJ_TYPE_COMMIT) {
2364 1caad61b 2019-02-01 stsp got_object_tag_close(tag);
2365 1caad61b 2019-02-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
2366 1caad61b 2019-02-01 stsp goto done;
2369 1caad61b 2019-02-01 stsp id = got_object_id_dup(
2370 1caad61b 2019-02-01 stsp got_object_tag_get_object_id(tag));
2371 1caad61b 2019-02-01 stsp if (id == NULL)
2372 638f9024 2019-05-13 stsp error = got_error_from_errno(
2373 230a42bd 2019-05-11 jcs "got_object_id_dup");
2374 1caad61b 2019-02-01 stsp got_object_tag_close(tag);
2375 1caad61b 2019-02-01 stsp if (error)
2376 1caad61b 2019-02-01 stsp goto done;
2377 1caad61b 2019-02-01 stsp } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
2378 1caad61b 2019-02-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
2379 1caad61b 2019-02-01 stsp goto done;
2381 15a94983 2018-12-23 stsp error = got_object_open_as_commit(&commit, repo, id);
2382 d1f2edc9 2018-06-13 stsp if (error != NULL)
2383 1caad61b 2019-02-01 stsp goto done;
2385 15a94983 2018-12-23 stsp if (commit == NULL) {
2386 e09a504c 2019-06-28 stsp error = got_repo_match_object_id_prefix(&id,
2387 dd88155e 2019-06-29 stsp start_commit, GOT_OBJ_TYPE_COMMIT, repo);
2388 d1f2edc9 2018-06-13 stsp if (error != NULL)
2389 d1f2edc9 2018-06-13 stsp return error;
2392 d7d4f210 2018-03-12 stsp if (error != NULL)
2393 04ca23f4 2018-07-16 stsp goto done;
2395 deeabeae 2019-08-27 stsp if (worktree) {
2396 deeabeae 2019-08-27 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
2398 deeabeae 2019-08-27 stsp if (asprintf(&p, "%s%s%s", prefix,
2399 deeabeae 2019-08-27 stsp (strcmp(prefix, "/") != 0) ? "/" : "", path) == -1) {
2400 deeabeae 2019-08-27 stsp error = got_error_from_errno("asprintf");
2401 deeabeae 2019-08-27 stsp goto done;
2403 f43793a4 2020-01-27 stsp error = got_repo_map_path(&in_repo_path, repo, p, 0);
2406 deeabeae 2019-08-27 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
2407 04ca23f4 2018-07-16 stsp if (error != NULL)
2408 04ca23f4 2018-07-16 stsp goto done;
2409 04ca23f4 2018-07-16 stsp if (in_repo_path) {
2410 04ca23f4 2018-07-16 stsp free(path);
2411 04ca23f4 2018-07-16 stsp path = in_repo_path;
2414 b8bad2ba 2019-08-23 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
2415 199a4027 2019-02-02 stsp if (error)
2416 199a4027 2019-02-02 stsp goto done;
2418 6841bf13 2019-11-29 kn error = print_commits(id, repo, path, show_patch, search_pattern,
2419 48c8c60d 2020-01-27 stsp diff_context, limit, log_branches, &refs);
2421 04ca23f4 2018-07-16 stsp free(path);
2422 04ca23f4 2018-07-16 stsp free(repo_path);
2423 04ca23f4 2018-07-16 stsp free(cwd);
2425 cffc0aa4 2019-02-05 stsp if (worktree)
2426 cffc0aa4 2019-02-05 stsp got_worktree_close(worktree);
2427 ad242220 2018-09-08 stsp if (repo) {
2428 ad242220 2018-09-08 stsp const struct got_error *repo_error;
2429 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
2430 ad242220 2018-09-08 stsp if (error == NULL)
2431 ad242220 2018-09-08 stsp error = repo_error;
2433 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
2434 8bf5b3c9 2018-03-17 stsp return error;
2437 4ed7e80c 2018-05-20 stsp __dead static void
2438 3f8b7d6a 2018-04-01 stsp usage_diff(void)
2440 98eaaa12 2019-08-03 stsp fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] [-s] "
2441 63035f9f 2019-10-06 stsp "[-w] [object1 object2 | path]\n", getprogname());
2445 b72f483a 2019-02-05 stsp struct print_diff_arg {
2446 b72f483a 2019-02-05 stsp struct got_repository *repo;
2447 b72f483a 2019-02-05 stsp struct got_worktree *worktree;
2448 b72f483a 2019-02-05 stsp int diff_context;
2449 3fc0c068 2019-02-10 stsp const char *id_str;
2450 3fc0c068 2019-02-10 stsp int header_shown;
2451 98eaaa12 2019-08-03 stsp int diff_staged;
2452 63035f9f 2019-10-06 stsp int ignore_whitespace;
2455 4ed7e80c 2018-05-20 stsp static const struct got_error *
2456 88d0e355 2019-08-03 stsp print_diff(void *arg, unsigned char status, unsigned char staged_status,
2457 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
2458 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
2459 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
2461 b72f483a 2019-02-05 stsp struct print_diff_arg *a = arg;
2462 b72f483a 2019-02-05 stsp const struct got_error *err = NULL;
2463 b72f483a 2019-02-05 stsp struct got_blob_object *blob1 = NULL;
2464 12463d8b 2019-12-13 stsp int fd = -1;
2465 b72f483a 2019-02-05 stsp FILE *f2 = NULL;
2466 4ce46740 2019-08-08 stsp char *abspath = NULL, *label1 = NULL;
2467 b72f483a 2019-02-05 stsp struct stat sb;
2469 98eaaa12 2019-08-03 stsp if (a->diff_staged) {
2470 98eaaa12 2019-08-03 stsp if (staged_status != GOT_STATUS_MODIFY &&
2471 98eaaa12 2019-08-03 stsp staged_status != GOT_STATUS_ADD &&
2472 98eaaa12 2019-08-03 stsp staged_status != GOT_STATUS_DELETE)
2473 98eaaa12 2019-08-03 stsp return NULL;
2475 98eaaa12 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
2476 98eaaa12 2019-08-03 stsp return NULL;
2477 2a06fe5f 2019-08-24 stsp if (status == GOT_STATUS_NONEXISTENT)
2478 2a06fe5f 2019-08-24 stsp return got_error_set_errno(ENOENT, path);
2479 98eaaa12 2019-08-03 stsp if (status != GOT_STATUS_MODIFY &&
2480 98eaaa12 2019-08-03 stsp status != GOT_STATUS_ADD &&
2481 98eaaa12 2019-08-03 stsp status != GOT_STATUS_DELETE &&
2482 98eaaa12 2019-08-03 stsp status != GOT_STATUS_CONFLICT)
2483 98eaaa12 2019-08-03 stsp return NULL;
2486 3fc0c068 2019-02-10 stsp if (!a->header_shown) {
2487 98eaaa12 2019-08-03 stsp printf("diff %s %s%s\n", a->id_str,
2488 98eaaa12 2019-08-03 stsp got_worktree_get_root_path(a->worktree),
2489 98eaaa12 2019-08-03 stsp a->diff_staged ? " (staged changes)" : "");
2490 3fc0c068 2019-02-10 stsp a->header_shown = 1;
2493 98eaaa12 2019-08-03 stsp if (a->diff_staged) {
2494 98eaaa12 2019-08-03 stsp const char *label1 = NULL, *label2 = NULL;
2495 98eaaa12 2019-08-03 stsp switch (staged_status) {
2496 98eaaa12 2019-08-03 stsp case GOT_STATUS_MODIFY:
2497 98eaaa12 2019-08-03 stsp label1 = path;
2498 98eaaa12 2019-08-03 stsp label2 = path;
2500 98eaaa12 2019-08-03 stsp case GOT_STATUS_ADD:
2501 98eaaa12 2019-08-03 stsp label2 = path;
2503 98eaaa12 2019-08-03 stsp case GOT_STATUS_DELETE:
2504 98eaaa12 2019-08-03 stsp label1 = path;
2507 98eaaa12 2019-08-03 stsp return got_error(GOT_ERR_FILE_STATUS);
2509 98eaaa12 2019-08-03 stsp return got_diff_objects_as_blobs(blob_id, staged_blob_id,
2510 63035f9f 2019-10-06 stsp label1, label2, a->diff_context, a->ignore_whitespace,
2511 63035f9f 2019-10-06 stsp a->repo, stdout);
2514 408b4ebc 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
2515 4ce46740 2019-08-08 stsp staged_status == GOT_STATUS_MODIFY) {
2516 4ce46740 2019-08-08 stsp char *id_str;
2517 408b4ebc 2019-08-03 stsp err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
2520 4ce46740 2019-08-08 stsp goto done;
2521 4ce46740 2019-08-08 stsp err = got_object_id_str(&id_str, staged_blob_id);
2523 4ce46740 2019-08-08 stsp goto done;
2524 4ce46740 2019-08-08 stsp if (asprintf(&label1, "%s (staged)", id_str) == -1) {
2525 4ce46740 2019-08-08 stsp err = got_error_from_errno("asprintf");
2526 4ce46740 2019-08-08 stsp free(id_str);
2527 4ce46740 2019-08-08 stsp goto done;
2529 4ce46740 2019-08-08 stsp free(id_str);
2530 4ce46740 2019-08-08 stsp } else if (status != GOT_STATUS_ADD) {
2531 016a88dd 2019-05-13 stsp err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
2533 4ce46740 2019-08-08 stsp goto done;
2536 7154f6ce 2019-03-27 stsp if (status != GOT_STATUS_DELETE) {
2537 2ec1f75b 2019-03-26 stsp if (asprintf(&abspath, "%s/%s",
2538 2ec1f75b 2019-03-26 stsp got_worktree_get_root_path(a->worktree), path) == -1) {
2539 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2540 2ec1f75b 2019-03-26 stsp goto done;
2543 12463d8b 2019-12-13 stsp if (dirfd != -1) {
2544 12463d8b 2019-12-13 stsp fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
2545 12463d8b 2019-12-13 stsp if (fd == -1) {
2546 12463d8b 2019-12-13 stsp err = got_error_from_errno2("openat", abspath);
2547 12463d8b 2019-12-13 stsp goto done;
2550 12463d8b 2019-12-13 stsp fd = open(abspath, O_RDONLY | O_NOFOLLOW);
2551 12463d8b 2019-12-13 stsp if (fd == -1) {
2552 12463d8b 2019-12-13 stsp err = got_error_from_errno2("open", abspath);
2553 12463d8b 2019-12-13 stsp goto done;
2556 12463d8b 2019-12-13 stsp if (fstat(fd, &sb) == -1) {
2557 12463d8b 2019-12-13 stsp err = got_error_from_errno2("fstat", abspath);
2558 2ec1f75b 2019-03-26 stsp goto done;
2560 12463d8b 2019-12-13 stsp f2 = fdopen(fd, "r");
2561 12463d8b 2019-12-13 stsp if (f2 == NULL) {
2562 12463d8b 2019-12-13 stsp err = got_error_from_errno2("fdopen", abspath);
2563 2ec1f75b 2019-03-26 stsp goto done;
2567 2ec1f75b 2019-03-26 stsp sb.st_size = 0;
2569 4ce46740 2019-08-08 stsp err = got_diff_blob_file(blob1, label1, f2, sb.st_size, path,
2570 63035f9f 2019-10-06 stsp a->diff_context, a->ignore_whitespace, stdout);
2572 b72f483a 2019-02-05 stsp if (blob1)
2573 b72f483a 2019-02-05 stsp got_object_blob_close(blob1);
2574 12463d8b 2019-12-13 stsp if (f2 && fclose(f2) == EOF && err == NULL)
2575 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
2576 12463d8b 2019-12-13 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
2577 12463d8b 2019-12-13 stsp err = got_error_from_errno("close");
2578 b72f483a 2019-02-05 stsp free(abspath);
2579 927df6b7 2019-02-10 stsp return err;
2582 927df6b7 2019-02-10 stsp static const struct got_error *
2583 b00d56cd 2018-04-01 stsp cmd_diff(int argc, char *argv[])
2585 b00d56cd 2018-04-01 stsp const struct got_error *error;
2586 b00d56cd 2018-04-01 stsp struct got_repository *repo = NULL;
2587 b72f483a 2019-02-05 stsp struct got_worktree *worktree = NULL;
2588 b72f483a 2019-02-05 stsp char *cwd = NULL, *repo_path = NULL;
2589 15a94983 2018-12-23 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
2590 cd628e99 2019-05-28 stsp const char *id_str1 = NULL, *id_str2 = NULL;
2591 5e70831e 2019-05-28 stsp char *label1 = NULL, *label2 = NULL;
2592 15a94983 2018-12-23 stsp int type1, type2;
2593 63035f9f 2019-10-06 stsp int diff_context = 3, diff_staged = 0, ignore_whitespace = 0, ch;
2594 c0cc5c62 2018-10-18 stsp const char *errstr;
2595 927df6b7 2019-02-10 stsp char *path = NULL;
2597 b00d56cd 2018-04-01 stsp #ifndef PROFILE
2598 25eccc22 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2599 25eccc22 2019-01-04 stsp NULL) == -1)
2600 b00d56cd 2018-04-01 stsp err(1, "pledge");
2603 63035f9f 2019-10-06 stsp while ((ch = getopt(argc, argv, "C:r:sw")) != -1) {
2604 b00d56cd 2018-04-01 stsp switch (ch) {
2606 dfcab68b 2019-11-29 kn diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
2608 c0cc5c62 2018-10-18 stsp if (errstr != NULL)
2609 c0cc5c62 2018-10-18 stsp err(1, "-C option %s", errstr);
2612 b72f483a 2019-02-05 stsp repo_path = realpath(optarg, NULL);
2613 b72f483a 2019-02-05 stsp if (repo_path == NULL)
2614 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
2616 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
2619 98eaaa12 2019-08-03 stsp diff_staged = 1;
2622 63035f9f 2019-10-06 stsp ignore_whitespace = 1;
2625 2deda0b9 2019-03-07 stsp usage_diff();
2626 b00d56cd 2018-04-01 stsp /* NOTREACHED */
2630 b00d56cd 2018-04-01 stsp argc -= optind;
2631 b00d56cd 2018-04-01 stsp argv += optind;
2633 b72f483a 2019-02-05 stsp cwd = getcwd(NULL, 0);
2634 b72f483a 2019-02-05 stsp if (cwd == NULL) {
2635 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2636 b72f483a 2019-02-05 stsp goto done;
2638 927df6b7 2019-02-10 stsp error = got_worktree_open(&worktree, cwd);
2639 927df6b7 2019-02-10 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
2640 927df6b7 2019-02-10 stsp goto done;
2641 927df6b7 2019-02-10 stsp if (argc <= 1) {
2642 927df6b7 2019-02-10 stsp if (worktree == NULL) {
2643 927df6b7 2019-02-10 stsp error = got_error(GOT_ERR_NOT_WORKTREE);
2644 927df6b7 2019-02-10 stsp goto done;
2646 b72f483a 2019-02-05 stsp if (repo_path)
2648 b72f483a 2019-02-05 stsp "-r option can't be used when diffing a work tree");
2649 b72f483a 2019-02-05 stsp repo_path = strdup(got_worktree_get_repo_path(worktree));
2650 927df6b7 2019-02-10 stsp if (repo_path == NULL) {
2651 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2652 927df6b7 2019-02-10 stsp goto done;
2654 927df6b7 2019-02-10 stsp if (argc == 1) {
2655 6c7ab921 2019-03-18 stsp error = got_worktree_resolve_path(&path, worktree,
2657 927df6b7 2019-02-10 stsp if (error)
2658 927df6b7 2019-02-10 stsp goto done;
2660 927df6b7 2019-02-10 stsp path = strdup("");
2661 927df6b7 2019-02-10 stsp if (path == NULL) {
2662 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2663 927df6b7 2019-02-10 stsp goto done;
2666 b72f483a 2019-02-05 stsp } else if (argc == 2) {
2667 98eaaa12 2019-08-03 stsp if (diff_staged)
2668 98eaaa12 2019-08-03 stsp errx(1, "-s option can't be used when diffing "
2669 98eaaa12 2019-08-03 stsp "objects in repository");
2670 15a94983 2018-12-23 stsp id_str1 = argv[0];
2671 15a94983 2018-12-23 stsp id_str2 = argv[1];
2672 30db809c 2019-06-05 stsp if (worktree && repo_path == NULL) {
2673 30db809c 2019-06-05 stsp repo_path =
2674 30db809c 2019-06-05 stsp strdup(got_worktree_get_repo_path(worktree));
2675 30db809c 2019-06-05 stsp if (repo_path == NULL) {
2676 30db809c 2019-06-05 stsp error = got_error_from_errno("strdup");
2677 30db809c 2019-06-05 stsp goto done;
2681 b00d56cd 2018-04-01 stsp usage_diff();
2683 b72f483a 2019-02-05 stsp if (repo_path == NULL) {
2684 b72f483a 2019-02-05 stsp repo_path = getcwd(NULL, 0);
2685 b72f483a 2019-02-05 stsp if (repo_path == NULL)
2686 638f9024 2019-05-13 stsp return got_error_from_errno("getcwd");
2689 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
2690 76089277 2018-04-01 stsp free(repo_path);
2691 b00d56cd 2018-04-01 stsp if (error != NULL)
2692 b00d56cd 2018-04-01 stsp goto done;
2694 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), 1,
2695 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
2696 c02c541e 2019-03-29 stsp if (error)
2697 c02c541e 2019-03-29 stsp goto done;
2699 30db809c 2019-06-05 stsp if (argc <= 1) {
2700 b72f483a 2019-02-05 stsp struct print_diff_arg arg;
2701 72ea6654 2019-07-27 stsp struct got_pathlist_head paths;
2702 b72f483a 2019-02-05 stsp char *id_str;
2704 72ea6654 2019-07-27 stsp TAILQ_INIT(&paths);
2706 b72f483a 2019-02-05 stsp error = got_object_id_str(&id_str,
2707 b72f483a 2019-02-05 stsp got_worktree_get_base_commit_id(worktree));
2708 b72f483a 2019-02-05 stsp if (error)
2709 b72f483a 2019-02-05 stsp goto done;
2710 b72f483a 2019-02-05 stsp arg.repo = repo;
2711 b72f483a 2019-02-05 stsp arg.worktree = worktree;
2712 b72f483a 2019-02-05 stsp arg.diff_context = diff_context;
2713 3fc0c068 2019-02-10 stsp arg.id_str = id_str;
2714 3fc0c068 2019-02-10 stsp arg.header_shown = 0;
2715 98eaaa12 2019-08-03 stsp arg.diff_staged = diff_staged;
2716 63035f9f 2019-10-06 stsp arg.ignore_whitespace = ignore_whitespace;
2718 adc19d55 2019-07-28 stsp error = got_pathlist_append(&paths, path, NULL);
2719 72ea6654 2019-07-27 stsp if (error)
2720 72ea6654 2019-07-27 stsp goto done;
2722 72ea6654 2019-07-27 stsp error = got_worktree_status(worktree, &paths, repo, print_diff,
2723 b72f483a 2019-02-05 stsp &arg, check_cancelled, NULL);
2724 b72f483a 2019-02-05 stsp free(id_str);
2725 72ea6654 2019-07-27 stsp got_pathlist_free(&paths);
2726 b72f483a 2019-02-05 stsp goto done;
2729 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&id1, &label1, id_str1,
2730 71a27632 2020-01-15 stsp GOT_OBJ_TYPE_ANY, 1, repo);
2731 0adb7196 2019-08-11 stsp if (error)
2732 0adb7196 2019-08-11 stsp goto done;
2734 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&id2, &label2, id_str2,
2735 71a27632 2020-01-15 stsp GOT_OBJ_TYPE_ANY, 1, repo);
2736 0adb7196 2019-08-11 stsp if (error)
2737 0adb7196 2019-08-11 stsp goto done;
2739 15a94983 2018-12-23 stsp error = got_object_get_type(&type1, repo, id1);
2740 15a94983 2018-12-23 stsp if (error)
2741 15a94983 2018-12-23 stsp goto done;
2743 15a94983 2018-12-23 stsp error = got_object_get_type(&type2, repo, id2);
2744 15a94983 2018-12-23 stsp if (error)
2745 15a94983 2018-12-23 stsp goto done;
2747 15a94983 2018-12-23 stsp if (type1 != type2) {
2748 b00d56cd 2018-04-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
2749 b00d56cd 2018-04-01 stsp goto done;
2752 15a94983 2018-12-23 stsp switch (type1) {
2753 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_BLOB:
2754 15a94983 2018-12-23 stsp error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
2755 63035f9f 2019-10-06 stsp diff_context, ignore_whitespace, repo, stdout);
2757 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_TREE:
2758 15a94983 2018-12-23 stsp error = got_diff_objects_as_trees(id1, id2, "", "",
2759 63035f9f 2019-10-06 stsp diff_context, ignore_whitespace, repo, stdout);
2761 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_COMMIT:
2762 5e70831e 2019-05-28 stsp printf("diff %s %s\n", label1, label2);
2763 15a94983 2018-12-23 stsp error = got_diff_objects_as_commits(id1, id2, diff_context,
2764 63035f9f 2019-10-06 stsp ignore_whitespace, repo, stdout);
2767 b00d56cd 2018-04-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
2770 5e70831e 2019-05-28 stsp free(label1);
2771 5e70831e 2019-05-28 stsp free(label2);
2772 15a94983 2018-12-23 stsp free(id1);
2773 15a94983 2018-12-23 stsp free(id2);
2774 927df6b7 2019-02-10 stsp free(path);
2775 b72f483a 2019-02-05 stsp if (worktree)
2776 b72f483a 2019-02-05 stsp got_worktree_close(worktree);
2777 ad242220 2018-09-08 stsp if (repo) {
2778 ad242220 2018-09-08 stsp const struct got_error *repo_error;
2779 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
2780 ad242220 2018-09-08 stsp if (error == NULL)
2781 ad242220 2018-09-08 stsp error = repo_error;
2783 b00d56cd 2018-04-01 stsp return error;
2786 404c43c4 2018-06-21 stsp __dead static void
2787 404c43c4 2018-06-21 stsp usage_blame(void)
2789 9270e621 2019-02-05 stsp fprintf(stderr,
2790 9270e621 2019-02-05 stsp "usage: %s blame [-c commit] [-r repository-path] path\n",
2791 404c43c4 2018-06-21 stsp getprogname());
2795 28315671 2019-08-14 stsp struct blame_line {
2796 28315671 2019-08-14 stsp int annotated;
2797 28315671 2019-08-14 stsp char *id_str;
2798 82f6abb8 2019-08-14 stsp char *committer;
2799 11db6024 2019-10-21 stsp char datebuf[11]; /* YYYY-MM-DD + NUL */
2802 28315671 2019-08-14 stsp struct blame_cb_args {
2803 28315671 2019-08-14 stsp struct blame_line *lines;
2804 28315671 2019-08-14 stsp int nlines;
2805 7ef28ff8 2019-08-14 stsp int nlines_prec;
2806 28315671 2019-08-14 stsp int lineno_cur;
2807 28315671 2019-08-14 stsp off_t *line_offsets;
2809 82f6abb8 2019-08-14 stsp struct got_repository *repo;
2812 404c43c4 2018-06-21 stsp static const struct got_error *
2813 28315671 2019-08-14 stsp blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2815 28315671 2019-08-14 stsp const struct got_error *err = NULL;
2816 28315671 2019-08-14 stsp struct blame_cb_args *a = arg;
2817 28315671 2019-08-14 stsp struct blame_line *bline;
2818 82f6abb8 2019-08-14 stsp char *line = NULL;
2819 28315671 2019-08-14 stsp size_t linesize = 0;
2820 82f6abb8 2019-08-14 stsp struct got_commit_object *commit = NULL;
2821 28315671 2019-08-14 stsp off_t offset;
2822 bcb49d15 2019-08-14 stsp struct tm tm;
2823 bcb49d15 2019-08-14 stsp time_t committer_time;
2825 28315671 2019-08-14 stsp if (nlines != a->nlines ||
2826 28315671 2019-08-14 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
2827 28315671 2019-08-14 stsp return got_error(GOT_ERR_RANGE);
2829 28315671 2019-08-14 stsp if (sigint_received)
2830 28315671 2019-08-14 stsp return got_error(GOT_ERR_ITER_COMPLETED);
2832 2839f8b9 2019-08-18 stsp if (lineno == -1)
2833 2839f8b9 2019-08-18 stsp return NULL; /* no change in this commit */
2835 28315671 2019-08-14 stsp /* Annotate this line. */
2836 28315671 2019-08-14 stsp bline = &a->lines[lineno - 1];
2837 28315671 2019-08-14 stsp if (bline->annotated)
2838 28315671 2019-08-14 stsp return NULL;
2839 28315671 2019-08-14 stsp err = got_object_id_str(&bline->id_str, id);
2841 28315671 2019-08-14 stsp return err;
2843 82f6abb8 2019-08-14 stsp err = got_object_open_as_commit(&commit, a->repo, id);
2845 82f6abb8 2019-08-14 stsp goto done;
2847 82f6abb8 2019-08-14 stsp bline->committer = strdup(got_object_commit_get_committer(commit));
2848 82f6abb8 2019-08-14 stsp if (bline->committer == NULL) {
2849 82f6abb8 2019-08-14 stsp err = got_error_from_errno("strdup");
2850 bcb49d15 2019-08-14 stsp goto done;
2853 bcb49d15 2019-08-14 stsp committer_time = got_object_commit_get_committer_time(commit);
2854 bcb49d15 2019-08-14 stsp if (localtime_r(&committer_time, &tm) == NULL)
2855 bcb49d15 2019-08-14 stsp return got_error_from_errno("localtime_r");
2856 6db9f7f6 2019-12-10 stsp if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
2857 bcb49d15 2019-08-14 stsp &tm) >= sizeof(bline->datebuf)) {
2858 bcb49d15 2019-08-14 stsp err = got_error(GOT_ERR_NO_SPACE);
2859 82f6abb8 2019-08-14 stsp goto done;
2861 28315671 2019-08-14 stsp bline->annotated = 1;
2863 28315671 2019-08-14 stsp /* Print lines annotated so far. */
2864 28315671 2019-08-14 stsp bline = &a->lines[a->lineno_cur - 1];
2865 28315671 2019-08-14 stsp if (!bline->annotated)
2866 82f6abb8 2019-08-14 stsp goto done;
2868 28315671 2019-08-14 stsp offset = a->line_offsets[a->lineno_cur - 1];
2869 82f6abb8 2019-08-14 stsp if (fseeko(a->f, offset, SEEK_SET) == -1) {
2870 82f6abb8 2019-08-14 stsp err = got_error_from_errno("fseeko");
2871 82f6abb8 2019-08-14 stsp goto done;
2874 28315671 2019-08-14 stsp while (bline->annotated) {
2875 82f6abb8 2019-08-14 stsp char *smallerthan, *at, *nl, *committer;
2876 82f6abb8 2019-08-14 stsp size_t len;
2878 500467ff 2019-09-25 hiltjo if (getline(&line, &linesize, a->f) == -1) {
2879 28315671 2019-08-14 stsp if (ferror(a->f))
2880 28315671 2019-08-14 stsp err = got_error_from_errno("getline");
2884 82f6abb8 2019-08-14 stsp committer = bline->committer;
2885 82f6abb8 2019-08-14 stsp smallerthan = strchr(committer, '<');
2886 82f6abb8 2019-08-14 stsp if (smallerthan && smallerthan[1] != '\0')
2887 82f6abb8 2019-08-14 stsp committer = smallerthan + 1;
2888 82f6abb8 2019-08-14 stsp at = strchr(committer, '@');
2890 82f6abb8 2019-08-14 stsp *at = '\0';
2891 82f6abb8 2019-08-14 stsp len = strlen(committer);
2892 82f6abb8 2019-08-14 stsp if (len >= 9)
2893 82f6abb8 2019-08-14 stsp committer[8] = '\0';
2895 28315671 2019-08-14 stsp nl = strchr(line, '\n');
2897 28315671 2019-08-14 stsp *nl = '\0';
2898 bcb49d15 2019-08-14 stsp printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
2899 bcb49d15 2019-08-14 stsp bline->id_str, bline->datebuf, committer, line);
2901 28315671 2019-08-14 stsp a->lineno_cur++;
2902 28315671 2019-08-14 stsp bline = &a->lines[a->lineno_cur - 1];
2905 82f6abb8 2019-08-14 stsp if (commit)
2906 82f6abb8 2019-08-14 stsp got_object_commit_close(commit);
2907 28315671 2019-08-14 stsp free(line);
2908 28315671 2019-08-14 stsp return err;
2911 28315671 2019-08-14 stsp static const struct got_error *
2912 404c43c4 2018-06-21 stsp cmd_blame(int argc, char *argv[])
2914 404c43c4 2018-06-21 stsp const struct got_error *error;
2915 404c43c4 2018-06-21 stsp struct got_repository *repo = NULL;
2916 0c06baac 2019-02-05 stsp struct got_worktree *worktree = NULL;
2917 66bea077 2018-08-02 stsp char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2918 28315671 2019-08-14 stsp struct got_object_id *obj_id = NULL;
2919 404c43c4 2018-06-21 stsp struct got_object_id *commit_id = NULL;
2920 28315671 2019-08-14 stsp struct got_blob_object *blob = NULL;
2921 404c43c4 2018-06-21 stsp char *commit_id_str = NULL;
2922 28315671 2019-08-14 stsp struct blame_cb_args bca;
2923 28315671 2019-08-14 stsp int ch, obj_type, i;
2924 b02560ec 2019-08-19 stsp size_t filesize;
2926 90f3c347 2019-08-19 stsp memset(&bca, 0, sizeof(bca));
2928 404c43c4 2018-06-21 stsp #ifndef PROFILE
2929 36e2fb66 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2930 36e2fb66 2019-01-04 stsp NULL) == -1)
2931 404c43c4 2018-06-21 stsp err(1, "pledge");
2934 66bea077 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2935 404c43c4 2018-06-21 stsp switch (ch) {
2937 404c43c4 2018-06-21 stsp commit_id_str = optarg;
2940 66bea077 2018-08-02 stsp repo_path = realpath(optarg, NULL);
2941 66bea077 2018-08-02 stsp if (repo_path == NULL)
2942 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
2944 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
2947 2deda0b9 2019-03-07 stsp usage_blame();
2948 404c43c4 2018-06-21 stsp /* NOTREACHED */
2952 404c43c4 2018-06-21 stsp argc -= optind;
2953 404c43c4 2018-06-21 stsp argv += optind;
2955 a39318fd 2018-08-02 stsp if (argc == 1)
2956 404c43c4 2018-06-21 stsp path = argv[0];
2958 404c43c4 2018-06-21 stsp usage_blame();
2960 66bea077 2018-08-02 stsp cwd = getcwd(NULL, 0);
2961 66bea077 2018-08-02 stsp if (cwd == NULL) {
2962 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2963 66bea077 2018-08-02 stsp goto done;
2965 66bea077 2018-08-02 stsp if (repo_path == NULL) {
2966 0c06baac 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
2967 0c06baac 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
2968 66bea077 2018-08-02 stsp goto done;
2970 0c06baac 2019-02-05 stsp error = NULL;
2971 0c06baac 2019-02-05 stsp if (worktree) {
2972 0c06baac 2019-02-05 stsp repo_path =
2973 0c06baac 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
2974 0c06baac 2019-02-05 stsp if (repo_path == NULL)
2975 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2976 0c06baac 2019-02-05 stsp if (error)
2977 0c06baac 2019-02-05 stsp goto done;
2979 0c06baac 2019-02-05 stsp repo_path = strdup(cwd);
2980 0c06baac 2019-02-05 stsp if (repo_path == NULL) {
2981 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2982 0c06baac 2019-02-05 stsp goto done;
2987 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
2988 c02c541e 2019-03-29 stsp if (error != NULL)
2989 36e2fb66 2019-01-04 stsp goto done;
2991 c530dc23 2019-07-23 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2992 c02c541e 2019-03-29 stsp if (error)
2993 404c43c4 2018-06-21 stsp goto done;
2995 0c06baac 2019-02-05 stsp if (worktree) {
2996 6efaaa2d 2019-02-05 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
2997 0c06baac 2019-02-05 stsp char *p, *worktree_subdir = cwd +
2998 0c06baac 2019-02-05 stsp strlen(got_worktree_get_root_path(worktree));
2999 6efaaa2d 2019-02-05 stsp if (asprintf(&p, "%s%s%s%s%s",
3000 6efaaa2d 2019-02-05 stsp prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
3001 6efaaa2d 2019-02-05 stsp worktree_subdir, worktree_subdir[0] ? "/" : "",
3002 6efaaa2d 2019-02-05 stsp path) == -1) {
3003 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
3004 0c06baac 2019-02-05 stsp goto done;
3006 6efaaa2d 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, p, 0);
3009 0c06baac 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
3011 0c06baac 2019-02-05 stsp if (error)
3012 66bea077 2018-08-02 stsp goto done;
3014 404c43c4 2018-06-21 stsp if (commit_id_str == NULL) {
3015 404c43c4 2018-06-21 stsp struct got_reference *head_ref;
3016 a0975128 2020-02-07 stsp error = got_ref_open(&head_ref, repo, worktree ?
3017 a0975128 2020-02-07 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
3018 404c43c4 2018-06-21 stsp if (error != NULL)
3019 66bea077 2018-08-02 stsp goto done;
3020 404c43c4 2018-06-21 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
3021 404c43c4 2018-06-21 stsp got_ref_close(head_ref);
3022 404c43c4 2018-06-21 stsp if (error != NULL)
3023 66bea077 2018-08-02 stsp goto done;
3025 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
3026 71a27632 2020-01-15 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
3027 30837e32 2019-07-25 stsp if (error)
3028 66bea077 2018-08-02 stsp goto done;
3031 28315671 2019-08-14 stsp error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
3032 28315671 2019-08-14 stsp if (error)
3033 28315671 2019-08-14 stsp goto done;
3035 28315671 2019-08-14 stsp error = got_object_get_type(&obj_type, repo, obj_id);
3036 28315671 2019-08-14 stsp if (error)
3037 28315671 2019-08-14 stsp goto done;
3039 28315671 2019-08-14 stsp if (obj_type != GOT_OBJ_TYPE_BLOB) {
3040 28315671 2019-08-14 stsp error = got_error(GOT_ERR_OBJ_TYPE);
3041 28315671 2019-08-14 stsp goto done;
3044 28315671 2019-08-14 stsp error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
3045 28315671 2019-08-14 stsp if (error)
3046 28315671 2019-08-14 stsp goto done;
3047 28315671 2019-08-14 stsp bca.f = got_opentemp();
3048 28315671 2019-08-14 stsp if (bca.f == NULL) {
3049 28315671 2019-08-14 stsp error = got_error_from_errno("got_opentemp");
3050 28315671 2019-08-14 stsp goto done;
3052 b02560ec 2019-08-19 stsp error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
3053 28315671 2019-08-14 stsp &bca.line_offsets, bca.f, blob);
3054 7ef28ff8 2019-08-14 stsp if (error || bca.nlines == 0)
3055 28315671 2019-08-14 stsp goto done;
3057 b02560ec 2019-08-19 stsp /* Don't include \n at EOF in the blame line count. */
3058 b02560ec 2019-08-19 stsp if (bca.line_offsets[bca.nlines - 1] == filesize)
3059 b02560ec 2019-08-19 stsp bca.nlines--;
3061 28315671 2019-08-14 stsp bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
3062 28315671 2019-08-14 stsp if (bca.lines == NULL) {
3063 28315671 2019-08-14 stsp error = got_error_from_errno("calloc");
3064 28315671 2019-08-14 stsp goto done;
3066 28315671 2019-08-14 stsp bca.lineno_cur = 1;
3067 7ef28ff8 2019-08-14 stsp bca.nlines_prec = 0;
3068 7ef28ff8 2019-08-14 stsp i = bca.nlines;
3069 7ef28ff8 2019-08-14 stsp while (i > 0) {
3071 7ef28ff8 2019-08-14 stsp bca.nlines_prec++;
3073 82f6abb8 2019-08-14 stsp bca.repo = repo;
3075 6fb7cd11 2019-08-22 stsp error = got_blame(in_repo_path, commit_id, repo, blame_cb, &bca,
3076 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
3078 66bea077 2018-08-02 stsp free(in_repo_path);
3079 66bea077 2018-08-02 stsp free(repo_path);
3080 66bea077 2018-08-02 stsp free(cwd);
3081 404c43c4 2018-06-21 stsp free(commit_id);
3082 28315671 2019-08-14 stsp free(obj_id);
3084 28315671 2019-08-14 stsp got_object_blob_close(blob);
3085 0c06baac 2019-02-05 stsp if (worktree)
3086 0c06baac 2019-02-05 stsp got_worktree_close(worktree);
3087 ad242220 2018-09-08 stsp if (repo) {
3088 ad242220 2018-09-08 stsp const struct got_error *repo_error;
3089 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
3090 ad242220 2018-09-08 stsp if (error == NULL)
3091 ad242220 2018-09-08 stsp error = repo_error;
3093 3affba96 2019-09-22 stsp if (bca.lines) {
3094 3affba96 2019-09-22 stsp for (i = 0; i < bca.nlines; i++) {
3095 3affba96 2019-09-22 stsp struct blame_line *bline = &bca.lines[i];
3096 3affba96 2019-09-22 stsp free(bline->id_str);
3097 3affba96 2019-09-22 stsp free(bline->committer);
3099 3affba96 2019-09-22 stsp free(bca.lines);
3101 28315671 2019-08-14 stsp free(bca.line_offsets);
3102 28315671 2019-08-14 stsp if (bca.f && fclose(bca.f) == EOF && error == NULL)
3103 28315671 2019-08-14 stsp error = got_error_from_errno("fclose");
3104 404c43c4 2018-06-21 stsp return error;
3107 5de5890b 2018-10-18 stsp __dead static void
3108 5de5890b 2018-10-18 stsp usage_tree(void)
3110 c1669e2e 2019-01-09 stsp fprintf(stderr,
3111 c1669e2e 2019-01-09 stsp "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
3112 5de5890b 2018-10-18 stsp getprogname());
3116 c1669e2e 2019-01-09 stsp static void
3117 c1669e2e 2019-01-09 stsp print_entry(struct got_tree_entry *te, const char *id, const char *path,
3118 c1669e2e 2019-01-09 stsp const char *root_path)
3120 c1669e2e 2019-01-09 stsp int is_root_path = (strcmp(path, root_path) == 0);
3121 848d6979 2019-08-12 stsp const char *modestr = "";
3122 56e0773d 2019-11-28 stsp mode_t mode = got_tree_entry_get_mode(te);
3124 c1669e2e 2019-01-09 stsp path += strlen(root_path);
3125 c1669e2e 2019-01-09 stsp while (path[0] == '/')
3128 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
3129 63c5ca5d 2019-08-24 stsp modestr = "$";
3130 56e0773d 2019-11-28 stsp else if (S_ISLNK(mode))
3131 848d6979 2019-08-12 stsp modestr = "@";
3132 56e0773d 2019-11-28 stsp else if (S_ISDIR(mode))
3133 848d6979 2019-08-12 stsp modestr = "/";
3134 56e0773d 2019-11-28 stsp else if (mode & S_IXUSR)
3135 848d6979 2019-08-12 stsp modestr = "*";
3137 c1669e2e 2019-01-09 stsp printf("%s%s%s%s%s\n", id ? id : "", path,
3138 56e0773d 2019-11-28 stsp is_root_path ? "" : "/", got_tree_entry_get_name(te), modestr);
3141 5de5890b 2018-10-18 stsp static const struct got_error *
3142 5de5890b 2018-10-18 stsp print_tree(const char *path, struct got_object_id *commit_id,
3143 c1669e2e 2019-01-09 stsp int show_ids, int recurse, const char *root_path,
3144 c1669e2e 2019-01-09 stsp struct got_repository *repo)
3146 5de5890b 2018-10-18 stsp const struct got_error *err = NULL;
3147 5de5890b 2018-10-18 stsp struct got_object_id *tree_id = NULL;
3148 5de5890b 2018-10-18 stsp struct got_tree_object *tree = NULL;
3149 56e0773d 2019-11-28 stsp int nentries, i;
3151 5de5890b 2018-10-18 stsp err = got_object_id_by_path(&tree_id, repo, commit_id, path);
3153 5de5890b 2018-10-18 stsp goto done;
3155 5de5890b 2018-10-18 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
3157 5de5890b 2018-10-18 stsp goto done;
3158 56e0773d 2019-11-28 stsp nentries = got_object_tree_get_nentries(tree);
3159 56e0773d 2019-11-28 stsp for (i = 0; i < nentries; i++) {
3160 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
3161 5de5890b 2018-10-18 stsp char *id = NULL;
3163 84453469 2018-11-11 stsp if (sigint_received || sigpipe_received)
3166 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(tree, i);
3167 5de5890b 2018-10-18 stsp if (show_ids) {
3168 5de5890b 2018-10-18 stsp char *id_str;
3169 56e0773d 2019-11-28 stsp err = got_object_id_str(&id_str,
3170 56e0773d 2019-11-28 stsp got_tree_entry_get_id(te));
3172 5de5890b 2018-10-18 stsp goto done;
3173 5de5890b 2018-10-18 stsp if (asprintf(&id, "%s ", id_str) == -1) {
3174 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
3175 5de5890b 2018-10-18 stsp free(id_str);
3176 5de5890b 2018-10-18 stsp goto done;
3178 5de5890b 2018-10-18 stsp free(id_str);
3180 c1669e2e 2019-01-09 stsp print_entry(te, id, path, root_path);
3183 56e0773d 2019-11-28 stsp if (recurse && S_ISDIR(got_tree_entry_get_mode(te))) {
3184 c1669e2e 2019-01-09 stsp char *child_path;
3185 c1669e2e 2019-01-09 stsp if (asprintf(&child_path, "%s%s%s", path,
3186 c1669e2e 2019-01-09 stsp path[0] == '/' && path[1] == '\0' ? "" : "/",
3187 56e0773d 2019-11-28 stsp got_tree_entry_get_name(te)) == -1) {
3188 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
3189 c1669e2e 2019-01-09 stsp goto done;
3191 c1669e2e 2019-01-09 stsp err = print_tree(child_path, commit_id, show_ids, 1,
3192 c1669e2e 2019-01-09 stsp root_path, repo);
3193 c1669e2e 2019-01-09 stsp free(child_path);
3195 c1669e2e 2019-01-09 stsp goto done;
3200 5de5890b 2018-10-18 stsp got_object_tree_close(tree);
3201 5de5890b 2018-10-18 stsp free(tree_id);
3202 5de5890b 2018-10-18 stsp return err;
3205 5de5890b 2018-10-18 stsp static const struct got_error *
3206 5de5890b 2018-10-18 stsp cmd_tree(int argc, char *argv[])
3208 5de5890b 2018-10-18 stsp const struct got_error *error;
3209 5de5890b 2018-10-18 stsp struct got_repository *repo = NULL;
3210 7a2c19d6 2019-02-05 stsp struct got_worktree *worktree = NULL;
3211 7a2c19d6 2019-02-05 stsp const char *path;
3212 7a2c19d6 2019-02-05 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
3213 5de5890b 2018-10-18 stsp struct got_object_id *commit_id = NULL;
3214 5de5890b 2018-10-18 stsp char *commit_id_str = NULL;
3215 c1669e2e 2019-01-09 stsp int show_ids = 0, recurse = 0;
3218 5de5890b 2018-10-18 stsp #ifndef PROFILE
3219 0f8d269b 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3220 0f8d269b 2019-01-04 stsp NULL) == -1)
3221 5de5890b 2018-10-18 stsp err(1, "pledge");
3224 c1669e2e 2019-01-09 stsp while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
3225 5de5890b 2018-10-18 stsp switch (ch) {
3227 5de5890b 2018-10-18 stsp commit_id_str = optarg;
3230 5de5890b 2018-10-18 stsp repo_path = realpath(optarg, NULL);
3231 5de5890b 2018-10-18 stsp if (repo_path == NULL)
3232 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
3234 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
3237 5de5890b 2018-10-18 stsp show_ids = 1;
3240 c1669e2e 2019-01-09 stsp recurse = 1;
3243 2deda0b9 2019-03-07 stsp usage_tree();
3244 5de5890b 2018-10-18 stsp /* NOTREACHED */
3248 5de5890b 2018-10-18 stsp argc -= optind;
3249 5de5890b 2018-10-18 stsp argv += optind;
3251 5de5890b 2018-10-18 stsp if (argc == 1)
3252 5de5890b 2018-10-18 stsp path = argv[0];
3253 5de5890b 2018-10-18 stsp else if (argc > 1)
3254 5de5890b 2018-10-18 stsp usage_tree();
3256 7a2c19d6 2019-02-05 stsp path = NULL;
3258 9bf7a39b 2019-02-05 stsp cwd = getcwd(NULL, 0);
3259 9bf7a39b 2019-02-05 stsp if (cwd == NULL) {
3260 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
3261 9bf7a39b 2019-02-05 stsp goto done;
3263 5de5890b 2018-10-18 stsp if (repo_path == NULL) {
3264 7a2c19d6 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
3265 8994de28 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
3266 7a2c19d6 2019-02-05 stsp goto done;
3268 7a2c19d6 2019-02-05 stsp error = NULL;
3269 7a2c19d6 2019-02-05 stsp if (worktree) {
3270 7a2c19d6 2019-02-05 stsp repo_path =
3271 7a2c19d6 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
3272 7a2c19d6 2019-02-05 stsp if (repo_path == NULL)
3273 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
3274 7a2c19d6 2019-02-05 stsp if (error)
3275 7a2c19d6 2019-02-05 stsp goto done;
3277 7a2c19d6 2019-02-05 stsp repo_path = strdup(cwd);
3278 7a2c19d6 2019-02-05 stsp if (repo_path == NULL) {
3279 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
3280 7a2c19d6 2019-02-05 stsp goto done;
3285 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
3286 5de5890b 2018-10-18 stsp if (error != NULL)
3287 c02c541e 2019-03-29 stsp goto done;
3289 c530dc23 2019-07-23 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
3290 c02c541e 2019-03-29 stsp if (error)
3291 5de5890b 2018-10-18 stsp goto done;
3293 9bf7a39b 2019-02-05 stsp if (path == NULL) {
3294 9bf7a39b 2019-02-05 stsp if (worktree) {
3295 9bf7a39b 2019-02-05 stsp char *p, *worktree_subdir = cwd +
3296 9bf7a39b 2019-02-05 stsp strlen(got_worktree_get_root_path(worktree));
3297 9bf7a39b 2019-02-05 stsp if (asprintf(&p, "%s/%s",
3298 9bf7a39b 2019-02-05 stsp got_worktree_get_path_prefix(worktree),
3299 9bf7a39b 2019-02-05 stsp worktree_subdir) == -1) {
3300 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
3301 9bf7a39b 2019-02-05 stsp goto done;
3303 f43793a4 2020-01-27 stsp error = got_repo_map_path(&in_repo_path, repo, p, 0);
3305 9bf7a39b 2019-02-05 stsp if (error)
3306 9bf7a39b 2019-02-05 stsp goto done;
3308 9bf7a39b 2019-02-05 stsp path = "/";
3310 9bf7a39b 2019-02-05 stsp if (in_repo_path == NULL) {
3311 9bf7a39b 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
3312 9bf7a39b 2019-02-05 stsp if (error != NULL)
3313 9bf7a39b 2019-02-05 stsp goto done;
3316 5de5890b 2018-10-18 stsp if (commit_id_str == NULL) {
3317 5de5890b 2018-10-18 stsp struct got_reference *head_ref;
3318 2f17228e 2019-05-12 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
3319 5de5890b 2018-10-18 stsp if (error != NULL)
3320 5de5890b 2018-10-18 stsp goto done;
3321 5de5890b 2018-10-18 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
3322 5de5890b 2018-10-18 stsp got_ref_close(head_ref);
3323 5de5890b 2018-10-18 stsp if (error != NULL)
3324 5de5890b 2018-10-18 stsp goto done;
3326 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
3327 71a27632 2020-01-15 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
3328 30837e32 2019-07-25 stsp if (error)
3329 5de5890b 2018-10-18 stsp goto done;
3332 c1669e2e 2019-01-09 stsp error = print_tree(in_repo_path, commit_id, show_ids, recurse,
3333 c1669e2e 2019-01-09 stsp in_repo_path, repo);
3335 5de5890b 2018-10-18 stsp free(in_repo_path);
3336 5de5890b 2018-10-18 stsp free(repo_path);
3337 5de5890b 2018-10-18 stsp free(cwd);
3338 5de5890b 2018-10-18 stsp free(commit_id);
3339 7a2c19d6 2019-02-05 stsp if (worktree)
3340 7a2c19d6 2019-02-05 stsp got_worktree_close(worktree);
3341 5de5890b 2018-10-18 stsp if (repo) {
3342 5de5890b 2018-10-18 stsp const struct got_error *repo_error;
3343 5de5890b 2018-10-18 stsp repo_error = got_repo_close(repo);
3344 5de5890b 2018-10-18 stsp if (error == NULL)
3345 5de5890b 2018-10-18 stsp error = repo_error;
3347 5de5890b 2018-10-18 stsp return error;
3350 6bad629b 2019-02-04 stsp __dead static void
3351 6bad629b 2019-02-04 stsp usage_status(void)
3353 72ea6654 2019-07-27 stsp fprintf(stderr, "usage: %s status [path ...]\n", getprogname());
3357 b72f483a 2019-02-05 stsp static const struct got_error *
3358 88d0e355 2019-08-03 stsp print_status(void *arg, unsigned char status, unsigned char staged_status,
3359 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
3360 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3361 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
3363 244725f2 2019-08-03 stsp if (status == staged_status && (status == GOT_STATUS_DELETE))
3364 c363b2c1 2019-08-03 stsp status = GOT_STATUS_NO_CHANGE;
3365 88d0e355 2019-08-03 stsp printf("%c%c %s\n", status, staged_status, path);
3366 b72f483a 2019-02-05 stsp return NULL;
3369 6bad629b 2019-02-04 stsp static const struct got_error *
3370 6bad629b 2019-02-04 stsp cmd_status(int argc, char *argv[])
3372 6bad629b 2019-02-04 stsp const struct got_error *error = NULL;
3373 6bad629b 2019-02-04 stsp struct got_repository *repo = NULL;
3374 6bad629b 2019-02-04 stsp struct got_worktree *worktree = NULL;
3375 f86a1bf5 2019-07-27 stsp char *cwd = NULL;
3376 72ea6654 2019-07-27 stsp struct got_pathlist_head paths;
3377 a5edda0a 2019-07-27 stsp struct got_pathlist_entry *pe;
3380 72ea6654 2019-07-27 stsp TAILQ_INIT(&paths);
3382 6bad629b 2019-02-04 stsp while ((ch = getopt(argc, argv, "")) != -1) {
3383 6bad629b 2019-02-04 stsp switch (ch) {
3385 2deda0b9 2019-03-07 stsp usage_status();
3386 6bad629b 2019-02-04 stsp /* NOTREACHED */
3390 6bad629b 2019-02-04 stsp argc -= optind;
3391 6bad629b 2019-02-04 stsp argv += optind;
3393 6bad629b 2019-02-04 stsp #ifndef PROFILE
3394 6bad629b 2019-02-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3395 6bad629b 2019-02-04 stsp NULL) == -1)
3396 6bad629b 2019-02-04 stsp err(1, "pledge");
3398 927df6b7 2019-02-10 stsp cwd = getcwd(NULL, 0);
3399 927df6b7 2019-02-10 stsp if (cwd == NULL) {
3400 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
3401 927df6b7 2019-02-10 stsp goto done;
3404 927df6b7 2019-02-10 stsp error = got_worktree_open(&worktree, cwd);
3405 927df6b7 2019-02-10 stsp if (error != NULL)
3406 a5edda0a 2019-07-27 stsp goto done;
3408 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
3410 6bad629b 2019-02-04 stsp if (error != NULL)
3411 6bad629b 2019-02-04 stsp goto done;
3413 d0eebce4 2019-03-11 stsp error = apply_unveil(got_repo_get_path(repo), 1,
3414 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
3415 087fb88c 2019-08-04 stsp if (error)
3416 087fb88c 2019-08-04 stsp goto done;
3418 087fb88c 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3419 6bad629b 2019-02-04 stsp if (error)
3420 6bad629b 2019-02-04 stsp goto done;
3422 72ea6654 2019-07-27 stsp error = got_worktree_status(worktree, &paths, repo, print_status, NULL,
3423 6bad629b 2019-02-04 stsp check_cancelled, NULL);
3425 a5edda0a 2019-07-27 stsp TAILQ_FOREACH(pe, &paths, entry)
3426 a5edda0a 2019-07-27 stsp free((char *)pe->path);
3427 72ea6654 2019-07-27 stsp got_pathlist_free(&paths);
3428 927df6b7 2019-02-10 stsp free(cwd);
3429 d0eebce4 2019-03-11 stsp return error;
3432 d0eebce4 2019-03-11 stsp __dead static void
3433 d0eebce4 2019-03-11 stsp usage_ref(void)
3435 d0eebce4 2019-03-11 stsp fprintf(stderr,
3436 d1c1ae5f 2019-08-12 stsp "usage: %s ref [-r repository] -l | -d name | [-s] name target\n",
3437 d0eebce4 2019-03-11 stsp getprogname());
3441 d0eebce4 2019-03-11 stsp static const struct got_error *
3442 d0eebce4 2019-03-11 stsp list_refs(struct got_repository *repo)
3444 d0eebce4 2019-03-11 stsp static const struct got_error *err = NULL;
3445 d0eebce4 2019-03-11 stsp struct got_reflist_head refs;
3446 d0eebce4 2019-03-11 stsp struct got_reflist_entry *re;
3448 d0eebce4 2019-03-11 stsp SIMPLEQ_INIT(&refs);
3449 b8bad2ba 2019-08-23 stsp err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
3451 d0eebce4 2019-03-11 stsp return err;
3453 d0eebce4 2019-03-11 stsp SIMPLEQ_FOREACH(re, &refs, entry) {
3454 d0eebce4 2019-03-11 stsp char *refstr;
3455 d0eebce4 2019-03-11 stsp refstr = got_ref_to_str(re->ref);
3456 d0eebce4 2019-03-11 stsp if (refstr == NULL)
3457 638f9024 2019-05-13 stsp return got_error_from_errno("got_ref_to_str");
3458 d0eebce4 2019-03-11 stsp printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
3459 d0eebce4 2019-03-11 stsp free(refstr);
3462 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
3463 d0eebce4 2019-03-11 stsp return NULL;
3466 d0eebce4 2019-03-11 stsp static const struct got_error *
3467 d0eebce4 2019-03-11 stsp delete_ref(struct got_repository *repo, const char *refname)
3469 d0eebce4 2019-03-11 stsp const struct got_error *err = NULL;
3470 d0eebce4 2019-03-11 stsp struct got_reference *ref;
3472 2f17228e 2019-05-12 stsp err = got_ref_open(&ref, repo, refname, 0);
3474 d0eebce4 2019-03-11 stsp return err;
3476 d0eebce4 2019-03-11 stsp err = got_ref_delete(ref, repo);
3477 d0eebce4 2019-03-11 stsp got_ref_close(ref);
3478 d0eebce4 2019-03-11 stsp return err;
3481 d0eebce4 2019-03-11 stsp static const struct got_error *
3482 d83d9d5c 2019-05-13 stsp add_ref(struct got_repository *repo, const char *refname, const char *target)
3484 d0eebce4 2019-03-11 stsp const struct got_error *err = NULL;
3485 d0eebce4 2019-03-11 stsp struct got_object_id *id;
3486 d0eebce4 2019-03-11 stsp struct got_reference *ref = NULL;
3489 bd5895f3 2019-11-28 stsp * Don't let the user create a reference name with a leading '-'.
3490 d1644381 2019-07-14 stsp * While technically a valid reference name, this case is usually
3491 d1644381 2019-07-14 stsp * an unintended typo.
3493 bd5895f3 2019-11-28 stsp if (refname[0] == '-')
3494 bd5895f3 2019-11-28 stsp return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
3496 dd88155e 2019-06-29 stsp err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
3498 d83d9d5c 2019-05-13 stsp if (err) {
3499 d83d9d5c 2019-05-13 stsp struct got_reference *target_ref;
3501 d83d9d5c 2019-05-13 stsp if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
3502 d83d9d5c 2019-05-13 stsp return err;
3503 d83d9d5c 2019-05-13 stsp err = got_ref_open(&target_ref, repo, target, 0);
3505 d83d9d5c 2019-05-13 stsp return err;
3506 d83d9d5c 2019-05-13 stsp err = got_ref_resolve(&id, repo, target_ref);
3507 d83d9d5c 2019-05-13 stsp got_ref_close(target_ref);
3509 d83d9d5c 2019-05-13 stsp return err;
3512 d0eebce4 2019-03-11 stsp err = got_ref_alloc(&ref, refname, id);
3514 d0eebce4 2019-03-11 stsp goto done;
3516 d0eebce4 2019-03-11 stsp err = got_ref_write(ref, repo);
3519 d0eebce4 2019-03-11 stsp got_ref_close(ref);
3521 d1c1ae5f 2019-08-12 stsp return err;
3524 d1c1ae5f 2019-08-12 stsp static const struct got_error *
3525 d1c1ae5f 2019-08-12 stsp add_symref(struct got_repository *repo, const char *refname, const char *target)
3527 d1c1ae5f 2019-08-12 stsp const struct got_error *err = NULL;
3528 d1c1ae5f 2019-08-12 stsp struct got_reference *ref = NULL;
3529 d1c1ae5f 2019-08-12 stsp struct got_reference *target_ref = NULL;
3532 bd5895f3 2019-11-28 stsp * Don't let the user create a reference name with a leading '-'.
3533 d1c1ae5f 2019-08-12 stsp * While technically a valid reference name, this case is usually
3534 d1c1ae5f 2019-08-12 stsp * an unintended typo.
3536 bd5895f3 2019-11-28 stsp if (refname[0] == '-')
3537 bd5895f3 2019-11-28 stsp return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
3539 d1c1ae5f 2019-08-12 stsp err = got_ref_open(&target_ref, repo, target, 0);
3541 d1c1ae5f 2019-08-12 stsp return err;
3543 d1c1ae5f 2019-08-12 stsp err = got_ref_alloc_symref(&ref, refname, target_ref);
3545 d1c1ae5f 2019-08-12 stsp goto done;
3547 d1c1ae5f 2019-08-12 stsp err = got_ref_write(ref, repo);
3549 d1c1ae5f 2019-08-12 stsp if (target_ref)
3550 d1c1ae5f 2019-08-12 stsp got_ref_close(target_ref);
3552 d1c1ae5f 2019-08-12 stsp got_ref_close(ref);
3553 d0eebce4 2019-03-11 stsp return err;
3556 d0eebce4 2019-03-11 stsp static const struct got_error *
3557 d0eebce4 2019-03-11 stsp cmd_ref(int argc, char *argv[])
3559 d0eebce4 2019-03-11 stsp const struct got_error *error = NULL;
3560 d0eebce4 2019-03-11 stsp struct got_repository *repo = NULL;
3561 d0eebce4 2019-03-11 stsp struct got_worktree *worktree = NULL;
3562 d0eebce4 2019-03-11 stsp char *cwd = NULL, *repo_path = NULL;
3563 d1c1ae5f 2019-08-12 stsp int ch, do_list = 0, create_symref = 0;
3564 d0eebce4 2019-03-11 stsp const char *delref = NULL;
3566 d0eebce4 2019-03-11 stsp /* TODO: Add -s option for adding symbolic references. */
3567 d1c1ae5f 2019-08-12 stsp while ((ch = getopt(argc, argv, "d:r:ls")) != -1) {
3568 d0eebce4 2019-03-11 stsp switch (ch) {
3570 d0eebce4 2019-03-11 stsp delref = optarg;
3573 d0eebce4 2019-03-11 stsp repo_path = realpath(optarg, NULL);
3574 d0eebce4 2019-03-11 stsp if (repo_path == NULL)
3575 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
3577 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
3580 d0eebce4 2019-03-11 stsp do_list = 1;
3583 d1c1ae5f 2019-08-12 stsp create_symref = 1;
3586 d0eebce4 2019-03-11 stsp usage_ref();
3587 d0eebce4 2019-03-11 stsp /* NOTREACHED */
3591 d0eebce4 2019-03-11 stsp if (do_list && delref)
3592 d0eebce4 2019-03-11 stsp errx(1, "-l and -d options are mutually exclusive\n");
3594 d0eebce4 2019-03-11 stsp argc -= optind;
3595 d0eebce4 2019-03-11 stsp argv += optind;
3597 d0eebce4 2019-03-11 stsp if (do_list || delref) {
3598 d1c1ae5f 2019-08-12 stsp if (create_symref)
3599 d1c1ae5f 2019-08-12 stsp errx(1, "-s option cannot be used together with the "
3600 d1c1ae5f 2019-08-12 stsp "-l or -d options");
3601 d0eebce4 2019-03-11 stsp if (argc > 0)
3602 d0eebce4 2019-03-11 stsp usage_ref();
3603 d0eebce4 2019-03-11 stsp } else if (argc != 2)
3604 d0eebce4 2019-03-11 stsp usage_ref();
3606 d0eebce4 2019-03-11 stsp #ifndef PROFILE
3607 e0b57350 2019-03-12 stsp if (do_list) {
3608 e0b57350 2019-03-12 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3609 e0b57350 2019-03-12 stsp NULL) == -1)
3610 e0b57350 2019-03-12 stsp err(1, "pledge");
3612 e0b57350 2019-03-12 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3613 e0b57350 2019-03-12 stsp "sendfd unveil", NULL) == -1)
3614 e0b57350 2019-03-12 stsp err(1, "pledge");
3617 d0eebce4 2019-03-11 stsp cwd = getcwd(NULL, 0);
3618 d0eebce4 2019-03-11 stsp if (cwd == NULL) {
3619 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
3620 d0eebce4 2019-03-11 stsp goto done;
3623 d0eebce4 2019-03-11 stsp if (repo_path == NULL) {
3624 d0eebce4 2019-03-11 stsp error = got_worktree_open(&worktree, cwd);
3625 d0eebce4 2019-03-11 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
3626 d0eebce4 2019-03-11 stsp goto done;
3628 d0eebce4 2019-03-11 stsp error = NULL;
3629 d0eebce4 2019-03-11 stsp if (worktree) {
3630 d0eebce4 2019-03-11 stsp repo_path =
3631 d0eebce4 2019-03-11 stsp strdup(got_worktree_get_repo_path(worktree));
3632 d0eebce4 2019-03-11 stsp if (repo_path == NULL)
3633 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
3634 d0eebce4 2019-03-11 stsp if (error)
3635 d0eebce4 2019-03-11 stsp goto done;
3637 d0eebce4 2019-03-11 stsp repo_path = strdup(cwd);
3638 d0eebce4 2019-03-11 stsp if (repo_path == NULL) {
3639 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
3640 d0eebce4 2019-03-11 stsp goto done;
3645 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
3646 d0eebce4 2019-03-11 stsp if (error != NULL)
3647 d0eebce4 2019-03-11 stsp goto done;
3649 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), do_list,
3650 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
3651 c02c541e 2019-03-29 stsp if (error)
3652 c02c541e 2019-03-29 stsp goto done;
3654 d0eebce4 2019-03-11 stsp if (do_list)
3655 d0eebce4 2019-03-11 stsp error = list_refs(repo);
3656 d0eebce4 2019-03-11 stsp else if (delref)
3657 d0eebce4 2019-03-11 stsp error = delete_ref(repo, delref);
3658 d1c1ae5f 2019-08-12 stsp else if (create_symref)
3659 d1c1ae5f 2019-08-12 stsp error = add_symref(repo, argv[0], argv[1]);
3661 d0eebce4 2019-03-11 stsp error = add_ref(repo, argv[0], argv[1]);
3664 4e759de4 2019-06-26 stsp got_repo_close(repo);
3665 4e759de4 2019-06-26 stsp if (worktree)
3666 4e759de4 2019-06-26 stsp got_worktree_close(worktree);
3667 4e759de4 2019-06-26 stsp free(cwd);
3668 4e759de4 2019-06-26 stsp free(repo_path);
3669 4e759de4 2019-06-26 stsp return error;
3672 4e759de4 2019-06-26 stsp __dead static void
3673 4e759de4 2019-06-26 stsp usage_branch(void)
3675 4e759de4 2019-06-26 stsp fprintf(stderr,
3676 da76fce2 2020-02-24 stsp "usage: %s branch [-c commit] [-d] [-r repository] [-l] [-n] "
3677 da76fce2 2020-02-24 stsp "[name]\n", getprogname());
3681 4e759de4 2019-06-26 stsp static const struct got_error *
3682 4e99b47e 2019-10-04 stsp list_branch(struct got_repository *repo, struct got_worktree *worktree,
3683 4e99b47e 2019-10-04 stsp struct got_reference *ref)
3685 4e99b47e 2019-10-04 stsp const struct got_error *err = NULL;
3686 4e99b47e 2019-10-04 stsp const char *refname, *marker = " ";
3687 4e99b47e 2019-10-04 stsp char *refstr;
3689 4e99b47e 2019-10-04 stsp refname = got_ref_get_name(ref);
3690 4e99b47e 2019-10-04 stsp if (worktree && strcmp(refname,
3691 4e99b47e 2019-10-04 stsp got_worktree_get_head_ref_name(worktree)) == 0) {
3692 4e99b47e 2019-10-04 stsp struct got_object_id *id = NULL;
3694 4e99b47e 2019-10-04 stsp err = got_ref_resolve(&id, repo, ref);
3696 4e99b47e 2019-10-04 stsp return err;
3697 4e99b47e 2019-10-04 stsp if (got_object_id_cmp(id,
3698 4e99b47e 2019-10-04 stsp got_worktree_get_base_commit_id(worktree)) == 0)
3699 4e99b47e 2019-10-04 stsp marker = "* ";
3701 4e99b47e 2019-10-04 stsp marker = "~ ";
3705 4e99b47e 2019-10-04 stsp if (strncmp(refname, "refs/heads/", 11) == 0)
3706 4e99b47e 2019-10-04 stsp refname += 11;
3707 4e99b47e 2019-10-04 stsp if (strncmp(refname, "refs/got/worktree/", 18) == 0)
3708 4e99b47e 2019-10-04 stsp refname += 18;
3710 4e99b47e 2019-10-04 stsp refstr = got_ref_to_str(ref);
3711 4e99b47e 2019-10-04 stsp if (refstr == NULL)
3712 4e99b47e 2019-10-04 stsp return got_error_from_errno("got_ref_to_str");
3714 4e99b47e 2019-10-04 stsp printf("%s%s: %s\n", marker, refname, refstr);
3715 4e99b47e 2019-10-04 stsp free(refstr);
3716 4e99b47e 2019-10-04 stsp return NULL;
3719 4e99b47e 2019-10-04 stsp static const struct got_error *
3720 ad89fa31 2019-10-04 stsp show_current_branch(struct got_repository *repo, struct got_worktree *worktree)
3722 ad89fa31 2019-10-04 stsp const char *refname;
3724 ad89fa31 2019-10-04 stsp if (worktree == NULL)
3725 ad89fa31 2019-10-04 stsp return got_error(GOT_ERR_NOT_WORKTREE);
3727 ad89fa31 2019-10-04 stsp refname = got_worktree_get_head_ref_name(worktree);
3729 ad89fa31 2019-10-04 stsp if (strncmp(refname, "refs/heads/", 11) == 0)
3730 ad89fa31 2019-10-04 stsp refname += 11;
3731 ad89fa31 2019-10-04 stsp if (strncmp(refname, "refs/got/worktree/", 18) == 0)
3732 ad89fa31 2019-10-04 stsp refname += 18;
3734 ad89fa31 2019-10-04 stsp printf("%s\n", refname);
3736 ad89fa31 2019-10-04 stsp return NULL;
3739 ad89fa31 2019-10-04 stsp static const struct got_error *
3740 ba882ee3 2019-07-11 stsp list_branches(struct got_repository *repo, struct got_worktree *worktree)
3742 4e759de4 2019-06-26 stsp static const struct got_error *err = NULL;
3743 4e759de4 2019-06-26 stsp struct got_reflist_head refs;
3744 4e759de4 2019-06-26 stsp struct got_reflist_entry *re;
3745 4e99b47e 2019-10-04 stsp struct got_reference *temp_ref = NULL;
3746 4e99b47e 2019-10-04 stsp int rebase_in_progress, histedit_in_progress;
3748 4e759de4 2019-06-26 stsp SIMPLEQ_INIT(&refs);
3750 4e99b47e 2019-10-04 stsp if (worktree) {
3751 4e99b47e 2019-10-04 stsp err = got_worktree_rebase_in_progress(&rebase_in_progress,
3752 4e99b47e 2019-10-04 stsp worktree);
3754 4e99b47e 2019-10-04 stsp return err;
3756 4e99b47e 2019-10-04 stsp err = got_worktree_histedit_in_progress(&histedit_in_progress,
3757 4e99b47e 2019-10-04 stsp worktree);
3759 4e99b47e 2019-10-04 stsp return err;
3761 4e99b47e 2019-10-04 stsp if (rebase_in_progress || histedit_in_progress) {
3762 4e99b47e 2019-10-04 stsp err = got_ref_open(&temp_ref, repo,
3763 4e99b47e 2019-10-04 stsp got_worktree_get_head_ref_name(worktree), 0);
3765 4e99b47e 2019-10-04 stsp return err;
3766 4e99b47e 2019-10-04 stsp list_branch(repo, worktree, temp_ref);
3767 4e99b47e 2019-10-04 stsp got_ref_close(temp_ref);
3771 b8bad2ba 2019-08-23 stsp err = got_ref_list(&refs, repo, "refs/heads",
3772 b8bad2ba 2019-08-23 stsp got_ref_cmp_by_name, NULL);
3774 4e759de4 2019-06-26 stsp return err;
3776 4e99b47e 2019-10-04 stsp SIMPLEQ_FOREACH(re, &refs, entry)
3777 4e99b47e 2019-10-04 stsp list_branch(repo, worktree, re->ref);
3779 4e759de4 2019-06-26 stsp got_ref_list_free(&refs);
3780 4e759de4 2019-06-26 stsp return NULL;
3783 4e759de4 2019-06-26 stsp static const struct got_error *
3784 45cd4e47 2019-08-25 stsp delete_branch(struct got_repository *repo, struct got_worktree *worktree,
3785 45cd4e47 2019-08-25 stsp const char *branch_name)
3787 4e759de4 2019-06-26 stsp const struct got_error *err = NULL;
3788 45cd4e47 2019-08-25 stsp struct got_reference *ref = NULL;
3789 4e759de4 2019-06-26 stsp char *refname;
3791 4e759de4 2019-06-26 stsp if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
3792 4e759de4 2019-06-26 stsp return got_error_from_errno("asprintf");
3794 4e759de4 2019-06-26 stsp err = got_ref_open(&ref, repo, refname, 0);
3796 4e759de4 2019-06-26 stsp goto done;
3798 45cd4e47 2019-08-25 stsp if (worktree &&
3799 45cd4e47 2019-08-25 stsp strcmp(got_worktree_get_head_ref_name(worktree),
3800 45cd4e47 2019-08-25 stsp got_ref_get_name(ref)) == 0) {
3801 45cd4e47 2019-08-25 stsp err = got_error_msg(GOT_ERR_SAME_BRANCH,
3802 45cd4e47 2019-08-25 stsp "will not delete this work tree's current branch");
3803 45cd4e47 2019-08-25 stsp goto done;
3806 45cd4e47 2019-08-25 stsp err = got_ref_delete(ref, repo);
3809 45cd4e47 2019-08-25 stsp got_ref_close(ref);
3810 4e759de4 2019-06-26 stsp free(refname);
3811 4e759de4 2019-06-26 stsp return err;
3814 4e759de4 2019-06-26 stsp static const struct got_error *
3815 4e759de4 2019-06-26 stsp add_branch(struct got_repository *repo, const char *branch_name,
3816 a74f7e83 2019-11-10 stsp struct got_object_id *base_commit_id)
3818 4e759de4 2019-06-26 stsp const struct got_error *err = NULL;
3819 4e759de4 2019-06-26 stsp struct got_reference *ref = NULL;
3820 4e759de4 2019-06-26 stsp char *base_refname = NULL, *refname = NULL;
3823 bd5895f3 2019-11-28 stsp * Don't let the user create a branch name with a leading '-'.
3824 d3f84d51 2019-07-11 stsp * While technically a valid reference name, this case is usually
3825 d3f84d51 2019-07-11 stsp * an unintended typo.
3827 bd5895f3 2019-11-28 stsp if (branch_name[0] == '-')
3828 bd5895f3 2019-11-28 stsp return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
3830 4e759de4 2019-06-26 stsp if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
3831 4e759de4 2019-06-26 stsp err = got_error_from_errno("asprintf");
3832 4e759de4 2019-06-26 stsp goto done;
3835 4e759de4 2019-06-26 stsp err = got_ref_open(&ref, repo, refname, 0);
3836 4e759de4 2019-06-26 stsp if (err == NULL) {
3837 4e759de4 2019-06-26 stsp err = got_error(GOT_ERR_BRANCH_EXISTS);
3838 4e759de4 2019-06-26 stsp goto done;
3839 4e759de4 2019-06-26 stsp } else if (err->code != GOT_ERR_NOT_REF)
3840 4e759de4 2019-06-26 stsp goto done;
3842 a74f7e83 2019-11-10 stsp err = got_ref_alloc(&ref, refname, base_commit_id);
3844 4e759de4 2019-06-26 stsp goto done;
3846 4e759de4 2019-06-26 stsp err = got_ref_write(ref, repo);
3849 4e759de4 2019-06-26 stsp got_ref_close(ref);
3850 4e759de4 2019-06-26 stsp free(base_refname);
3851 4e759de4 2019-06-26 stsp free(refname);
3852 4e759de4 2019-06-26 stsp return err;
3855 4e759de4 2019-06-26 stsp static const struct got_error *
3856 4e759de4 2019-06-26 stsp cmd_branch(int argc, char *argv[])
3858 4e759de4 2019-06-26 stsp const struct got_error *error = NULL;
3859 4e759de4 2019-06-26 stsp struct got_repository *repo = NULL;
3860 4e759de4 2019-06-26 stsp struct got_worktree *worktree = NULL;
3861 4e759de4 2019-06-26 stsp char *cwd = NULL, *repo_path = NULL;
3862 da76fce2 2020-02-24 stsp int ch, do_list = 0, do_show = 0, do_update = 1;
3863 a74f7e83 2019-11-10 stsp const char *delref = NULL, *commit_id_arg = NULL;
3864 da76fce2 2020-02-24 stsp struct got_reference *ref = NULL;
3865 da76fce2 2020-02-24 stsp struct got_pathlist_head paths;
3866 da76fce2 2020-02-24 stsp struct got_pathlist_entry *pe;
3867 da76fce2 2020-02-24 stsp struct got_object_id *commit_id = NULL;
3868 da76fce2 2020-02-24 stsp char *commit_id_str = NULL;
3870 da76fce2 2020-02-24 stsp TAILQ_INIT(&paths);
3872 da76fce2 2020-02-24 stsp while ((ch = getopt(argc, argv, "c:d:r:ln")) != -1) {
3873 4e759de4 2019-06-26 stsp switch (ch) {
3875 a74f7e83 2019-11-10 stsp commit_id_arg = optarg;
3878 4e759de4 2019-06-26 stsp delref = optarg;
3881 4e759de4 2019-06-26 stsp repo_path = realpath(optarg, NULL);
3882 4e759de4 2019-06-26 stsp if (repo_path == NULL)
3883 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
3885 4e759de4 2019-06-26 stsp got_path_strip_trailing_slashes(repo_path);
3888 4e759de4 2019-06-26 stsp do_list = 1;
3891 da76fce2 2020-02-24 stsp do_update = 0;
3894 4e759de4 2019-06-26 stsp usage_branch();
3895 4e759de4 2019-06-26 stsp /* NOTREACHED */
3899 4e759de4 2019-06-26 stsp if (do_list && delref)
3900 4e759de4 2019-06-26 stsp errx(1, "-l and -d options are mutually exclusive\n");
3902 4e759de4 2019-06-26 stsp argc -= optind;
3903 4e759de4 2019-06-26 stsp argv += optind;
3905 ad89fa31 2019-10-04 stsp if (!do_list && !delref && argc == 0)
3906 ad89fa31 2019-10-04 stsp do_show = 1;
3908 a74f7e83 2019-11-10 stsp if ((do_list || delref || do_show) && commit_id_arg != NULL)
3909 a74f7e83 2019-11-10 stsp errx(1, "-c option can only be used when creating a branch");
3911 4e759de4 2019-06-26 stsp if (do_list || delref) {
3912 4e759de4 2019-06-26 stsp if (argc > 0)
3913 4e759de4 2019-06-26 stsp usage_branch();
3914 a74f7e83 2019-11-10 stsp } else if (!do_show && argc != 1)
3915 4e759de4 2019-06-26 stsp usage_branch();
3917 4e759de4 2019-06-26 stsp #ifndef PROFILE
3918 ad89fa31 2019-10-04 stsp if (do_list || do_show) {
3919 4e759de4 2019-06-26 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3920 4e759de4 2019-06-26 stsp NULL) == -1)
3921 4e759de4 2019-06-26 stsp err(1, "pledge");
3923 4e759de4 2019-06-26 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3924 4e759de4 2019-06-26 stsp "sendfd unveil", NULL) == -1)
3925 4e759de4 2019-06-26 stsp err(1, "pledge");
3928 4e759de4 2019-06-26 stsp cwd = getcwd(NULL, 0);
3929 4e759de4 2019-06-26 stsp if (cwd == NULL) {
3930 4e759de4 2019-06-26 stsp error = got_error_from_errno("getcwd");
3931 4e759de4 2019-06-26 stsp goto done;
3934 4e759de4 2019-06-26 stsp if (repo_path == NULL) {
3935 4e759de4 2019-06-26 stsp error = got_worktree_open(&worktree, cwd);
3936 4e759de4 2019-06-26 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
3937 4e759de4 2019-06-26 stsp goto done;
3939 4e759de4 2019-06-26 stsp error = NULL;
3940 4e759de4 2019-06-26 stsp if (worktree) {
3941 4e759de4 2019-06-26 stsp repo_path =
3942 4e759de4 2019-06-26 stsp strdup(got_worktree_get_repo_path(worktree));
3943 4e759de4 2019-06-26 stsp if (repo_path == NULL)
3944 4e759de4 2019-06-26 stsp error = got_error_from_errno("strdup");
3945 4e759de4 2019-06-26 stsp if (error)
3946 4e759de4 2019-06-26 stsp goto done;
3948 4e759de4 2019-06-26 stsp repo_path = strdup(cwd);
3949 4e759de4 2019-06-26 stsp if (repo_path == NULL) {
3950 4e759de4 2019-06-26 stsp error = got_error_from_errno("strdup");
3951 4e759de4 2019-06-26 stsp goto done;
3956 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
3957 4e759de4 2019-06-26 stsp if (error != NULL)
3958 4e759de4 2019-06-26 stsp goto done;
3960 4e759de4 2019-06-26 stsp error = apply_unveil(got_repo_get_path(repo), do_list,
3961 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
3962 4e759de4 2019-06-26 stsp if (error)
3963 4e759de4 2019-06-26 stsp goto done;
3965 ad89fa31 2019-10-04 stsp if (do_show)
3966 ad89fa31 2019-10-04 stsp error = show_current_branch(repo, worktree);
3967 ad89fa31 2019-10-04 stsp else if (do_list)
3968 ba882ee3 2019-07-11 stsp error = list_branches(repo, worktree);
3969 4e759de4 2019-06-26 stsp else if (delref)
3970 45cd4e47 2019-08-25 stsp error = delete_branch(repo, worktree, delref);
3972 a74f7e83 2019-11-10 stsp if (commit_id_arg == NULL)
3973 a74f7e83 2019-11-10 stsp commit_id_arg = worktree ?
3974 4e759de4 2019-06-26 stsp got_worktree_get_head_ref_name(worktree) :
3975 4e759de4 2019-06-26 stsp GOT_REF_HEAD;
3976 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
3977 71a27632 2020-01-15 stsp commit_id_arg, GOT_OBJ_TYPE_COMMIT, 1, repo);
3978 a74f7e83 2019-11-10 stsp if (error)
3979 a74f7e83 2019-11-10 stsp goto done;
3980 a74f7e83 2019-11-10 stsp error = add_branch(repo, argv[0], commit_id);
3981 da76fce2 2020-02-24 stsp if (error)
3982 da76fce2 2020-02-24 stsp goto done;
3983 da76fce2 2020-02-24 stsp if (worktree && do_update) {
3984 da76fce2 2020-02-24 stsp int did_something = 0;
3985 da76fce2 2020-02-24 stsp char *branch_refname = NULL;
3987 da76fce2 2020-02-24 stsp error = got_object_id_str(&commit_id_str, commit_id);
3988 da76fce2 2020-02-24 stsp if (error)
3989 da76fce2 2020-02-24 stsp goto done;
3990 da76fce2 2020-02-24 stsp error = get_worktree_paths_from_argv(&paths, 0, NULL,
3991 da76fce2 2020-02-24 stsp worktree);
3992 da76fce2 2020-02-24 stsp if (error)
3993 da76fce2 2020-02-24 stsp goto done;
3994 da76fce2 2020-02-24 stsp if (asprintf(&branch_refname, "refs/heads/%s", argv[0])
3996 da76fce2 2020-02-24 stsp error = got_error_from_errno("asprintf");
3997 da76fce2 2020-02-24 stsp goto done;
3999 da76fce2 2020-02-24 stsp error = got_ref_open(&ref, repo, branch_refname, 0);
4000 da76fce2 2020-02-24 stsp free(branch_refname);
4001 da76fce2 2020-02-24 stsp if (error)
4002 da76fce2 2020-02-24 stsp goto done;
4003 da76fce2 2020-02-24 stsp error = switch_head_ref(ref, commit_id, worktree,
4005 da76fce2 2020-02-24 stsp if (error)
4006 da76fce2 2020-02-24 stsp goto done;
4007 da76fce2 2020-02-24 stsp error = got_worktree_set_base_commit_id(worktree, repo,
4008 da76fce2 2020-02-24 stsp commit_id);
4009 da76fce2 2020-02-24 stsp if (error)
4010 da76fce2 2020-02-24 stsp goto done;
4011 da76fce2 2020-02-24 stsp error = got_worktree_checkout_files(worktree, &paths,
4012 da76fce2 2020-02-24 stsp repo, update_progress, &did_something,
4013 da76fce2 2020-02-24 stsp check_cancelled, NULL);
4014 da76fce2 2020-02-24 stsp if (error)
4015 da76fce2 2020-02-24 stsp goto done;
4016 da76fce2 2020-02-24 stsp if (did_something)
4017 da76fce2 2020-02-24 stsp printf("Updated to commit %s\n", commit_id_str);
4022 da76fce2 2020-02-24 stsp got_ref_close(ref);
4024 d0eebce4 2019-03-11 stsp got_repo_close(repo);
4025 d0eebce4 2019-03-11 stsp if (worktree)
4026 d0eebce4 2019-03-11 stsp got_worktree_close(worktree);
4027 d0eebce4 2019-03-11 stsp free(cwd);
4028 d0eebce4 2019-03-11 stsp free(repo_path);
4029 da76fce2 2020-02-24 stsp free(commit_id);
4030 da76fce2 2020-02-24 stsp free(commit_id_str);
4031 da76fce2 2020-02-24 stsp TAILQ_FOREACH(pe, &paths, entry)
4032 da76fce2 2020-02-24 stsp free((char *)pe->path);
4033 da76fce2 2020-02-24 stsp got_pathlist_free(&paths);
4034 d00136be 2019-03-26 stsp return error;
4038 d00136be 2019-03-26 stsp __dead static void
4039 8e7bd50a 2019-08-22 stsp usage_tag(void)
4041 8e7bd50a 2019-08-22 stsp fprintf(stderr,
4042 80106605 2020-02-24 stsp "usage: %s tag [-c commit] [-r repository] [-l] "
4043 80106605 2020-02-24 stsp "[-m message] name\n", getprogname());
4048 b8bad2ba 2019-08-23 stsp static const struct got_error *
4049 b8bad2ba 2019-08-23 stsp sort_tags(struct got_reflist_head *sorted, struct got_reflist_head *tags)
4051 b8bad2ba 2019-08-23 stsp const struct got_error *err = NULL;
4052 b8bad2ba 2019-08-23 stsp struct got_reflist_entry *re, *se, *new;
4053 b8bad2ba 2019-08-23 stsp struct got_object_id *re_id, *se_id;
4054 b8bad2ba 2019-08-23 stsp struct got_tag_object *re_tag, *se_tag;
4055 b8bad2ba 2019-08-23 stsp time_t re_time, se_time;
4057 b8bad2ba 2019-08-23 stsp SIMPLEQ_FOREACH(re, tags, entry) {
4058 b8bad2ba 2019-08-23 stsp se = SIMPLEQ_FIRST(sorted);
4059 b8bad2ba 2019-08-23 stsp if (se == NULL) {
4060 b8bad2ba 2019-08-23 stsp err = got_reflist_entry_dup(&new, re);
4062 b8bad2ba 2019-08-23 stsp return err;
4063 b8bad2ba 2019-08-23 stsp SIMPLEQ_INSERT_HEAD(sorted, new, entry);
4066 b8bad2ba 2019-08-23 stsp err = got_ref_resolve(&re_id, repo, re->ref);
4069 b8bad2ba 2019-08-23 stsp err = got_object_open_as_tag(&re_tag, repo, re_id);
4070 b8bad2ba 2019-08-23 stsp free(re_id);
4073 b8bad2ba 2019-08-23 stsp re_time = got_object_tag_get_tagger_time(re_tag);
4074 b8bad2ba 2019-08-23 stsp got_object_tag_close(re_tag);
4077 b8bad2ba 2019-08-23 stsp while (se) {
4078 b8bad2ba 2019-08-23 stsp err = got_ref_resolve(&se_id, repo, re->ref);
4081 b8bad2ba 2019-08-23 stsp err = got_object_open_as_tag(&se_tag, repo, se_id);
4082 b8bad2ba 2019-08-23 stsp free(se_id);
4085 b8bad2ba 2019-08-23 stsp se_time = got_object_tag_get_tagger_time(se_tag);
4086 b8bad2ba 2019-08-23 stsp got_object_tag_close(se_tag);
4088 b8bad2ba 2019-08-23 stsp if (se_time > re_time) {
4089 b8bad2ba 2019-08-23 stsp err = got_reflist_entry_dup(&new, re);
4091 b8bad2ba 2019-08-23 stsp return err;
4092 b8bad2ba 2019-08-23 stsp SIMPLEQ_INSERT_AFTER(sorted, se, new, entry);
4095 b8bad2ba 2019-08-23 stsp se = SIMPLEQ_NEXT(se, entry);
4100 b8bad2ba 2019-08-23 stsp return err;
4104 b8bad2ba 2019-08-23 stsp static const struct got_error *
4105 8e7bd50a 2019-08-22 stsp list_tags(struct got_repository *repo, struct got_worktree *worktree)
4107 8e7bd50a 2019-08-22 stsp static const struct got_error *err = NULL;
4108 8e7bd50a 2019-08-22 stsp struct got_reflist_head refs;
4109 8e7bd50a 2019-08-22 stsp struct got_reflist_entry *re;
4111 8e7bd50a 2019-08-22 stsp SIMPLEQ_INIT(&refs);
4113 d1f16636 2020-01-15 stsp err = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags, repo);
4115 8e7bd50a 2019-08-22 stsp return err;
4117 8e7bd50a 2019-08-22 stsp SIMPLEQ_FOREACH(re, &refs, entry) {
4118 8e7bd50a 2019-08-22 stsp const char *refname;
4119 8e7bd50a 2019-08-22 stsp char *refstr, *tagmsg0, *tagmsg, *line, *id_str, *datestr;
4120 8e7bd50a 2019-08-22 stsp char datebuf[26];
4121 d4efa91b 2020-01-14 stsp const char *tagger;
4122 8e7bd50a 2019-08-22 stsp time_t tagger_time;
4123 8e7bd50a 2019-08-22 stsp struct got_object_id *id;
4124 8e7bd50a 2019-08-22 stsp struct got_tag_object *tag;
4125 d4efa91b 2020-01-14 stsp struct got_commit_object *commit = NULL;
4127 8e7bd50a 2019-08-22 stsp refname = got_ref_get_name(re->ref);
4128 8e7bd50a 2019-08-22 stsp if (strncmp(refname, "refs/tags/", 10) != 0)
4130 8e7bd50a 2019-08-22 stsp refname += 10;
4131 8e7bd50a 2019-08-22 stsp refstr = got_ref_to_str(re->ref);
4132 8e7bd50a 2019-08-22 stsp if (refstr == NULL) {
4133 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("got_ref_to_str");
4136 8e7bd50a 2019-08-22 stsp printf("%stag %s %s\n", GOT_COMMIT_SEP_STR, refname, refstr);
4137 8e7bd50a 2019-08-22 stsp free(refstr);
4139 8e7bd50a 2019-08-22 stsp err = got_ref_resolve(&id, repo, re->ref);
4142 8e7bd50a 2019-08-22 stsp err = got_object_open_as_tag(&tag, repo, id);
4143 d4efa91b 2020-01-14 stsp if (err) {
4144 d4efa91b 2020-01-14 stsp if (err->code != GOT_ERR_OBJ_TYPE) {
4148 d4efa91b 2020-01-14 stsp /* "lightweight" tag */
4149 d4efa91b 2020-01-14 stsp err = got_object_open_as_commit(&commit, repo, id);
4150 d4efa91b 2020-01-14 stsp if (err) {
4154 d4efa91b 2020-01-14 stsp tagger = got_object_commit_get_committer(commit);
4155 d4efa91b 2020-01-14 stsp tagger_time =
4156 d4efa91b 2020-01-14 stsp got_object_commit_get_committer_time(commit);
4157 d4efa91b 2020-01-14 stsp err = got_object_id_str(&id_str, id);
4163 d4efa91b 2020-01-14 stsp tagger = got_object_tag_get_tagger(tag);
4164 d4efa91b 2020-01-14 stsp tagger_time = got_object_tag_get_tagger_time(tag);
4165 d4efa91b 2020-01-14 stsp err = got_object_id_str(&id_str,
4166 d4efa91b 2020-01-14 stsp got_object_tag_get_object_id(tag));
4170 d4efa91b 2020-01-14 stsp printf("from: %s\n", tagger);
4171 2417344c 2019-08-23 stsp datestr = get_datestr(&tagger_time, datebuf);
4172 2417344c 2019-08-23 stsp if (datestr)
4173 2417344c 2019-08-23 stsp printf("date: %s UTC\n", datestr);
4174 d4efa91b 2020-01-14 stsp if (commit)
4175 2417344c 2019-08-23 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
4177 d4efa91b 2020-01-14 stsp switch (got_object_tag_get_object_type(tag)) {
4178 d4efa91b 2020-01-14 stsp case GOT_OBJ_TYPE_BLOB:
4179 d4efa91b 2020-01-14 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_BLOB,
4182 d4efa91b 2020-01-14 stsp case GOT_OBJ_TYPE_TREE:
4183 d4efa91b 2020-01-14 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_TREE,
4186 d4efa91b 2020-01-14 stsp case GOT_OBJ_TYPE_COMMIT:
4187 d4efa91b 2020-01-14 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT,
4190 d4efa91b 2020-01-14 stsp case GOT_OBJ_TYPE_TAG:
4191 d4efa91b 2020-01-14 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_TAG,
4198 8e7bd50a 2019-08-22 stsp free(id_str);
4199 d4efa91b 2020-01-14 stsp if (commit) {
4200 d4efa91b 2020-01-14 stsp err = got_object_commit_get_logmsg(&tagmsg0, commit);
4203 d4efa91b 2020-01-14 stsp got_object_commit_close(commit);
4205 d4efa91b 2020-01-14 stsp tagmsg0 = strdup(got_object_tag_get_message(tag));
4206 d4efa91b 2020-01-14 stsp got_object_tag_close(tag);
4207 d4efa91b 2020-01-14 stsp if (tagmsg0 == NULL) {
4208 d4efa91b 2020-01-14 stsp err = got_error_from_errno("strdup");
4213 8e7bd50a 2019-08-22 stsp tagmsg = tagmsg0;
4215 8e7bd50a 2019-08-22 stsp line = strsep(&tagmsg, "\n");
4217 8e7bd50a 2019-08-22 stsp printf(" %s\n", line);
4218 8e7bd50a 2019-08-22 stsp } while (line);
4219 8e7bd50a 2019-08-22 stsp free(tagmsg0);
4222 8e7bd50a 2019-08-22 stsp got_ref_list_free(&refs);
4223 8e7bd50a 2019-08-22 stsp return NULL;
4226 8e7bd50a 2019-08-22 stsp static const struct got_error *
4227 f372d5cd 2019-10-21 stsp get_tag_message(char **tagmsg, char **tagmsg_path, const char *commit_id_str,
4228 62870f63 2019-08-22 stsp const char *tag_name, const char *repo_path)
4230 8e7bd50a 2019-08-22 stsp const struct got_error *err = NULL;
4231 8e7bd50a 2019-08-22 stsp char *template = NULL, *initial_content = NULL;
4232 f372d5cd 2019-10-21 stsp char *editor = NULL;
4233 8e7bd50a 2019-08-22 stsp int fd = -1;
4235 bb63914a 2020-02-17 stsp if (asprintf(&template, GOT_TMPDIR_STR "/got-tagmsg") == -1) {
4236 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("asprintf");
4237 8e7bd50a 2019-08-22 stsp goto done;
4240 62870f63 2019-08-22 stsp if (asprintf(&initial_content, "\n# tagging commit %s as %s\n",
4241 62870f63 2019-08-22 stsp commit_id_str, tag_name) == -1) {
4242 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("asprintf");
4243 8e7bd50a 2019-08-22 stsp goto done;
4246 f372d5cd 2019-10-21 stsp err = got_opentemp_named_fd(tagmsg_path, &fd, template);
4248 8e7bd50a 2019-08-22 stsp goto done;
4250 8e7bd50a 2019-08-22 stsp dprintf(fd, initial_content);
4251 8e7bd50a 2019-08-22 stsp close(fd);
4253 8e7bd50a 2019-08-22 stsp err = get_editor(&editor);
4255 8e7bd50a 2019-08-22 stsp goto done;
4256 f372d5cd 2019-10-21 stsp err = edit_logmsg(tagmsg, editor, *tagmsg_path, initial_content);
4258 8e7bd50a 2019-08-22 stsp free(initial_content);
4259 8e7bd50a 2019-08-22 stsp free(template);
4260 8e7bd50a 2019-08-22 stsp free(editor);
4262 8e7bd50a 2019-08-22 stsp /* Editor is done; we can now apply unveil(2) */
4263 8e7bd50a 2019-08-22 stsp if (err == NULL) {
4264 8e7bd50a 2019-08-22 stsp err = apply_unveil(repo_path, 0, NULL);
4265 8e7bd50a 2019-08-22 stsp if (err) {
4266 8e7bd50a 2019-08-22 stsp free(*tagmsg);
4267 8e7bd50a 2019-08-22 stsp *tagmsg = NULL;
4270 8e7bd50a 2019-08-22 stsp return err;
4273 8e7bd50a 2019-08-22 stsp static const struct got_error *
4274 8e7bd50a 2019-08-22 stsp add_tag(struct got_repository *repo, const char *tag_name,
4275 8e7bd50a 2019-08-22 stsp const char *commit_arg, const char *tagmsg_arg)
4277 8e7bd50a 2019-08-22 stsp const struct got_error *err = NULL;
4278 8e7bd50a 2019-08-22 stsp struct got_object_id *commit_id = NULL, *tag_id = NULL;
4279 8e7bd50a 2019-08-22 stsp char *label = NULL, *commit_id_str = NULL;
4280 8e7bd50a 2019-08-22 stsp struct got_reference *ref = NULL;
4281 aba9c984 2019-09-08 stsp char *refname = NULL, *tagmsg = NULL, *tagger = NULL;
4282 f372d5cd 2019-10-21 stsp char *tagmsg_path = NULL, *tag_id_str = NULL;
4283 f372d5cd 2019-10-21 stsp int preserve_tagmsg = 0;
4286 bd5895f3 2019-11-28 stsp * Don't let the user create a tag name with a leading '-'.
4287 8e7bd50a 2019-08-22 stsp * While technically a valid reference name, this case is usually
4288 8e7bd50a 2019-08-22 stsp * an unintended typo.
4290 bd5895f3 2019-11-28 stsp if (tag_name[0] == '-')
4291 bd5895f3 2019-11-28 stsp return got_error_path(tag_name, GOT_ERR_REF_NAME_MINUS);
4293 aba9c984 2019-09-08 stsp err = get_author(&tagger, repo);
4295 8e7bd50a 2019-08-22 stsp return err;
4297 71a27632 2020-01-15 stsp err = got_repo_match_object_id(&commit_id, &label, commit_arg,
4298 8e7bd50a 2019-08-22 stsp GOT_OBJ_TYPE_COMMIT, 1, repo);
4300 8e7bd50a 2019-08-22 stsp goto done;
4302 8e7bd50a 2019-08-22 stsp err = got_object_id_str(&commit_id_str, commit_id);
4304 8e7bd50a 2019-08-22 stsp goto done;
4306 8e7bd50a 2019-08-22 stsp if (strncmp("refs/tags/", tag_name, 10) == 0) {
4307 8e7bd50a 2019-08-22 stsp refname = strdup(tag_name);
4308 8e7bd50a 2019-08-22 stsp if (refname == NULL) {
4309 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("strdup");
4310 8e7bd50a 2019-08-22 stsp goto done;
4312 8e7bd50a 2019-08-22 stsp tag_name += 10;
4313 8e7bd50a 2019-08-22 stsp } else if (asprintf(&refname, "refs/tags/%s", tag_name) == -1) {
4314 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("asprintf");
4315 8e7bd50a 2019-08-22 stsp goto done;
4318 8e7bd50a 2019-08-22 stsp err = got_ref_open(&ref, repo, refname, 0);
4319 8e7bd50a 2019-08-22 stsp if (err == NULL) {
4320 8e7bd50a 2019-08-22 stsp err = got_error(GOT_ERR_TAG_EXISTS);
4321 8e7bd50a 2019-08-22 stsp goto done;
4322 8e7bd50a 2019-08-22 stsp } else if (err->code != GOT_ERR_NOT_REF)
4323 8e7bd50a 2019-08-22 stsp goto done;
4325 8e7bd50a 2019-08-22 stsp if (tagmsg_arg == NULL) {
4326 f372d5cd 2019-10-21 stsp err = get_tag_message(&tagmsg, &tagmsg_path, commit_id_str,
4327 62870f63 2019-08-22 stsp tag_name, got_repo_get_path(repo));
4328 f372d5cd 2019-10-21 stsp if (err) {
4329 f372d5cd 2019-10-21 stsp if (err->code != GOT_ERR_COMMIT_MSG_EMPTY &&
4330 f372d5cd 2019-10-21 stsp tagmsg_path != NULL)
4331 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
4332 8e7bd50a 2019-08-22 stsp goto done;
4336 8e7bd50a 2019-08-22 stsp err = got_object_tag_create(&tag_id, tag_name, commit_id,
4337 8e7bd50a 2019-08-22 stsp tagger, time(NULL), tagmsg ? tagmsg : tagmsg_arg, repo);
4338 f372d5cd 2019-10-21 stsp if (err) {
4339 f372d5cd 2019-10-21 stsp if (tagmsg_path)
4340 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
4341 8e7bd50a 2019-08-22 stsp goto done;
4344 8e7bd50a 2019-08-22 stsp err = got_ref_alloc(&ref, refname, tag_id);
4345 f372d5cd 2019-10-21 stsp if (err) {
4346 f372d5cd 2019-10-21 stsp if (tagmsg_path)
4347 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
4348 8e7bd50a 2019-08-22 stsp goto done;
4351 8e7bd50a 2019-08-22 stsp err = got_ref_write(ref, repo);
4352 f372d5cd 2019-10-21 stsp if (err) {
4353 f372d5cd 2019-10-21 stsp if (tagmsg_path)
4354 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
4355 f372d5cd 2019-10-21 stsp goto done;
4358 f372d5cd 2019-10-21 stsp err = got_object_id_str(&tag_id_str, tag_id);
4359 f372d5cd 2019-10-21 stsp if (err) {
4360 f372d5cd 2019-10-21 stsp if (tagmsg_path)
4361 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
4362 f372d5cd 2019-10-21 stsp goto done;
4364 f372d5cd 2019-10-21 stsp printf("Created tag %s\n", tag_id_str);
4366 f372d5cd 2019-10-21 stsp if (preserve_tagmsg) {
4367 f372d5cd 2019-10-21 stsp fprintf(stderr, "%s: tag message preserved in %s\n",
4368 f372d5cd 2019-10-21 stsp getprogname(), tagmsg_path);
4369 f372d5cd 2019-10-21 stsp } else if (tagmsg_path && unlink(tagmsg_path) == -1 && err == NULL)
4370 f372d5cd 2019-10-21 stsp err = got_error_from_errno2("unlink", tagmsg_path);
4371 f372d5cd 2019-10-21 stsp free(tag_id_str);
4373 8e7bd50a 2019-08-22 stsp got_ref_close(ref);
4374 8e7bd50a 2019-08-22 stsp free(commit_id);
4375 8e7bd50a 2019-08-22 stsp free(commit_id_str);
4376 8e7bd50a 2019-08-22 stsp free(refname);
4377 8e7bd50a 2019-08-22 stsp free(tagmsg);
4378 f372d5cd 2019-10-21 stsp free(tagmsg_path);
4379 aba9c984 2019-09-08 stsp free(tagger);
4380 8e7bd50a 2019-08-22 stsp return err;
4383 8e7bd50a 2019-08-22 stsp static const struct got_error *
4384 8e7bd50a 2019-08-22 stsp cmd_tag(int argc, char *argv[])
4386 8e7bd50a 2019-08-22 stsp const struct got_error *error = NULL;
4387 8e7bd50a 2019-08-22 stsp struct got_repository *repo = NULL;
4388 8e7bd50a 2019-08-22 stsp struct got_worktree *worktree = NULL;
4389 8e7bd50a 2019-08-22 stsp char *cwd = NULL, *repo_path = NULL, *commit_id_str = NULL;
4390 c9956ddf 2019-09-08 stsp char *gitconfig_path = NULL;
4391 8e7bd50a 2019-08-22 stsp const char *tag_name, *commit_id_arg = NULL, *tagmsg = NULL;
4392 8e7bd50a 2019-08-22 stsp int ch, do_list = 0;
4394 80106605 2020-02-24 stsp while ((ch = getopt(argc, argv, "c:m:r:l")) != -1) {
4395 8e7bd50a 2019-08-22 stsp switch (ch) {
4397 80106605 2020-02-24 stsp commit_id_arg = optarg;
4400 8e7bd50a 2019-08-22 stsp tagmsg = optarg;
4403 8e7bd50a 2019-08-22 stsp repo_path = realpath(optarg, NULL);
4404 8e7bd50a 2019-08-22 stsp if (repo_path == NULL)
4405 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
4407 8e7bd50a 2019-08-22 stsp got_path_strip_trailing_slashes(repo_path);
4410 8e7bd50a 2019-08-22 stsp do_list = 1;
4413 8e7bd50a 2019-08-22 stsp usage_tag();
4414 8e7bd50a 2019-08-22 stsp /* NOTREACHED */
4418 8e7bd50a 2019-08-22 stsp argc -= optind;
4419 8e7bd50a 2019-08-22 stsp argv += optind;
4421 8e7bd50a 2019-08-22 stsp if (do_list) {
4422 80106605 2020-02-24 stsp if (commit_id_arg != NULL)
4423 80106605 2020-02-24 stsp errx(1, "-c option can only be used when creating a tag");
4424 8e7bd50a 2019-08-22 stsp if (tagmsg)
4425 80106605 2020-02-24 stsp errx(1, "-l and -m options are mutually exclusive");
4426 8e7bd50a 2019-08-22 stsp if (argc > 0)
4427 8e7bd50a 2019-08-22 stsp usage_tag();
4428 80106605 2020-02-24 stsp } else if (argc != 1)
4429 8e7bd50a 2019-08-22 stsp usage_tag();
4431 a2887370 2019-08-23 stsp tag_name = argv[0];
4433 8e7bd50a 2019-08-22 stsp #ifndef PROFILE
4434 8e7bd50a 2019-08-22 stsp if (do_list) {
4435 8e7bd50a 2019-08-22 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
4436 8e7bd50a 2019-08-22 stsp NULL) == -1)
4437 8e7bd50a 2019-08-22 stsp err(1, "pledge");
4439 8e7bd50a 2019-08-22 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
4440 8e7bd50a 2019-08-22 stsp "sendfd unveil", NULL) == -1)
4441 8e7bd50a 2019-08-22 stsp err(1, "pledge");
4444 8e7bd50a 2019-08-22 stsp cwd = getcwd(NULL, 0);
4445 8e7bd50a 2019-08-22 stsp if (cwd == NULL) {
4446 8e7bd50a 2019-08-22 stsp error = got_error_from_errno("getcwd");
4447 8e7bd50a 2019-08-22 stsp goto done;
4450 8e7bd50a 2019-08-22 stsp if (repo_path == NULL) {
4451 8e7bd50a 2019-08-22 stsp error = got_worktree_open(&worktree, cwd);
4452 8e7bd50a 2019-08-22 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4453 8e7bd50a 2019-08-22 stsp goto done;
4455 8e7bd50a 2019-08-22 stsp error = NULL;
4456 8e7bd50a 2019-08-22 stsp if (worktree) {
4457 8e7bd50a 2019-08-22 stsp repo_path =
4458 8e7bd50a 2019-08-22 stsp strdup(got_worktree_get_repo_path(worktree));
4459 8e7bd50a 2019-08-22 stsp if (repo_path == NULL)
4460 8e7bd50a 2019-08-22 stsp error = got_error_from_errno("strdup");
4461 8e7bd50a 2019-08-22 stsp if (error)
4462 8e7bd50a 2019-08-22 stsp goto done;
4464 8e7bd50a 2019-08-22 stsp repo_path = strdup(cwd);
4465 8e7bd50a 2019-08-22 stsp if (repo_path == NULL) {
4466 8e7bd50a 2019-08-22 stsp error = got_error_from_errno("strdup");
4467 8e7bd50a 2019-08-22 stsp goto done;
4472 8e7bd50a 2019-08-22 stsp if (do_list) {
4473 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
4474 c9956ddf 2019-09-08 stsp if (error != NULL)
4475 c9956ddf 2019-09-08 stsp goto done;
4476 8e7bd50a 2019-08-22 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4477 8e7bd50a 2019-08-22 stsp if (error)
4478 8e7bd50a 2019-08-22 stsp goto done;
4479 8e7bd50a 2019-08-22 stsp error = list_tags(repo, worktree);
4481 c9956ddf 2019-09-08 stsp error = get_gitconfig_path(&gitconfig_path);
4482 c9956ddf 2019-09-08 stsp if (error)
4483 c9956ddf 2019-09-08 stsp goto done;
4484 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, gitconfig_path);
4485 c9956ddf 2019-09-08 stsp if (error != NULL)
4486 c9956ddf 2019-09-08 stsp goto done;
4488 8e7bd50a 2019-08-22 stsp if (tagmsg) {
4489 8e7bd50a 2019-08-22 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4490 8e7bd50a 2019-08-22 stsp if (error)
4491 8e7bd50a 2019-08-22 stsp goto done;
4494 8e7bd50a 2019-08-22 stsp if (commit_id_arg == NULL) {
4495 8e7bd50a 2019-08-22 stsp struct got_reference *head_ref;
4496 8e7bd50a 2019-08-22 stsp struct got_object_id *commit_id;
4497 8e7bd50a 2019-08-22 stsp error = got_ref_open(&head_ref, repo,
4498 8e7bd50a 2019-08-22 stsp worktree ? got_worktree_get_head_ref_name(worktree)
4499 8e7bd50a 2019-08-22 stsp : GOT_REF_HEAD, 0);
4500 8e7bd50a 2019-08-22 stsp if (error)
4501 8e7bd50a 2019-08-22 stsp goto done;
4502 8e7bd50a 2019-08-22 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
4503 8e7bd50a 2019-08-22 stsp got_ref_close(head_ref);
4504 8e7bd50a 2019-08-22 stsp if (error)
4505 8e7bd50a 2019-08-22 stsp goto done;
4506 8e7bd50a 2019-08-22 stsp error = got_object_id_str(&commit_id_str, commit_id);
4507 8e7bd50a 2019-08-22 stsp free(commit_id);
4508 8e7bd50a 2019-08-22 stsp if (error)
4509 8e7bd50a 2019-08-22 stsp goto done;
4512 8e7bd50a 2019-08-22 stsp error = add_tag(repo, tag_name,
4513 8e7bd50a 2019-08-22 stsp commit_id_str ? commit_id_str : commit_id_arg, tagmsg);
4517 8e7bd50a 2019-08-22 stsp got_repo_close(repo);
4518 8e7bd50a 2019-08-22 stsp if (worktree)
4519 8e7bd50a 2019-08-22 stsp got_worktree_close(worktree);
4520 8e7bd50a 2019-08-22 stsp free(cwd);
4521 8e7bd50a 2019-08-22 stsp free(repo_path);
4522 c9956ddf 2019-09-08 stsp free(gitconfig_path);
4523 8e7bd50a 2019-08-22 stsp free(commit_id_str);
4524 8e7bd50a 2019-08-22 stsp return error;
4527 8e7bd50a 2019-08-22 stsp __dead static void
4528 d00136be 2019-03-26 stsp usage_add(void)
4530 c29c428a 2019-12-16 stsp fprintf(stderr, "usage: %s add [-R] [-I] path ...\n",
4531 022fae89 2019-12-06 tracey getprogname());
4535 d00136be 2019-03-26 stsp static const struct got_error *
4536 4e68cba3 2019-11-23 stsp add_progress(void *arg, unsigned char status, const char *path)
4538 4e68cba3 2019-11-23 stsp while (path[0] == '/')
4540 4e68cba3 2019-11-23 stsp printf("%c %s\n", status, path);
4541 4e68cba3 2019-11-23 stsp return NULL;
4544 4e68cba3 2019-11-23 stsp static const struct got_error *
4545 d00136be 2019-03-26 stsp cmd_add(int argc, char *argv[])
4547 d00136be 2019-03-26 stsp const struct got_error *error = NULL;
4548 031a5338 2019-03-26 stsp struct got_repository *repo = NULL;
4549 d00136be 2019-03-26 stsp struct got_worktree *worktree = NULL;
4550 1dd54920 2019-05-11 stsp char *cwd = NULL;
4551 1dd54920 2019-05-11 stsp struct got_pathlist_head paths;
4552 1dd54920 2019-05-11 stsp struct got_pathlist_entry *pe;
4553 022fae89 2019-12-06 tracey int ch, can_recurse = 0, no_ignores = 0;
4555 1dd54920 2019-05-11 stsp TAILQ_INIT(&paths);
4557 022fae89 2019-12-06 tracey while ((ch = getopt(argc, argv, "IR")) != -1) {
4558 d00136be 2019-03-26 stsp switch (ch) {
4559 022fae89 2019-12-06 tracey case 'I':
4560 022fae89 2019-12-06 tracey no_ignores = 1;
4563 4e68cba3 2019-11-23 stsp can_recurse = 1;
4566 d00136be 2019-03-26 stsp usage_add();
4567 d00136be 2019-03-26 stsp /* NOTREACHED */
4571 d00136be 2019-03-26 stsp argc -= optind;
4572 d00136be 2019-03-26 stsp argv += optind;
4574 43012d58 2019-07-14 stsp #ifndef PROFILE
4575 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4576 43012d58 2019-07-14 stsp NULL) == -1)
4577 43012d58 2019-07-14 stsp err(1, "pledge");
4579 723c305c 2019-05-11 jcs if (argc < 1)
4580 d00136be 2019-03-26 stsp usage_add();
4582 d00136be 2019-03-26 stsp cwd = getcwd(NULL, 0);
4583 d00136be 2019-03-26 stsp if (cwd == NULL) {
4584 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
4585 d00136be 2019-03-26 stsp goto done;
4588 d00136be 2019-03-26 stsp error = got_worktree_open(&worktree, cwd);
4589 d00136be 2019-03-26 stsp if (error)
4590 d00136be 2019-03-26 stsp goto done;
4592 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4594 031a5338 2019-03-26 stsp if (error != NULL)
4595 031a5338 2019-03-26 stsp goto done;
4597 031a5338 2019-03-26 stsp error = apply_unveil(got_repo_get_path(repo), 1,
4598 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
4599 d00136be 2019-03-26 stsp if (error)
4600 d00136be 2019-03-26 stsp goto done;
4602 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4603 6d022e97 2019-08-04 stsp if (error)
4604 022fae89 2019-12-06 tracey goto done;
4606 022fae89 2019-12-06 tracey if (!can_recurse && no_ignores) {
4607 022fae89 2019-12-06 tracey error = got_error_msg(GOT_ERR_BAD_PATH,
4608 022fae89 2019-12-06 tracey "disregarding ignores requires -R option");
4609 6d022e97 2019-08-04 stsp goto done;
4613 4e68cba3 2019-11-23 stsp if (!can_recurse) {
4614 4e68cba3 2019-11-23 stsp char *ondisk_path;
4615 4e68cba3 2019-11-23 stsp struct stat sb;
4616 4e68cba3 2019-11-23 stsp TAILQ_FOREACH(pe, &paths, entry) {
4617 4e68cba3 2019-11-23 stsp if (asprintf(&ondisk_path, "%s/%s",
4618 4e68cba3 2019-11-23 stsp got_worktree_get_root_path(worktree),
4619 4e68cba3 2019-11-23 stsp pe->path) == -1) {
4620 4e68cba3 2019-11-23 stsp error = got_error_from_errno("asprintf");
4621 4e68cba3 2019-11-23 stsp goto done;
4623 4e68cba3 2019-11-23 stsp if (lstat(ondisk_path, &sb) == -1) {
4624 4e68cba3 2019-11-23 stsp if (errno == ENOENT) {
4625 4e68cba3 2019-11-23 stsp free(ondisk_path);
4628 4e68cba3 2019-11-23 stsp error = got_error_from_errno2("lstat",
4629 4e68cba3 2019-11-23 stsp ondisk_path);
4630 4e68cba3 2019-11-23 stsp free(ondisk_path);
4631 4e68cba3 2019-11-23 stsp goto done;
4633 4e68cba3 2019-11-23 stsp free(ondisk_path);
4634 4e68cba3 2019-11-23 stsp if (S_ISDIR(sb.st_mode)) {
4635 4e68cba3 2019-11-23 stsp error = got_error_msg(GOT_ERR_BAD_PATH,
4636 4e68cba3 2019-11-23 stsp "adding directories requires -R option");
4637 4e68cba3 2019-11-23 stsp goto done;
4642 4e68cba3 2019-11-23 stsp error = got_worktree_schedule_add(worktree, &paths, add_progress,
4643 022fae89 2019-12-06 tracey NULL, repo, no_ignores);
4646 031a5338 2019-03-26 stsp got_repo_close(repo);
4647 d00136be 2019-03-26 stsp if (worktree)
4648 d00136be 2019-03-26 stsp got_worktree_close(worktree);
4649 1dd54920 2019-05-11 stsp TAILQ_FOREACH(pe, &paths, entry)
4650 1dd54920 2019-05-11 stsp free((char *)pe->path);
4651 1dd54920 2019-05-11 stsp got_pathlist_free(&paths);
4652 2ec1f75b 2019-03-26 stsp free(cwd);
4653 2ec1f75b 2019-03-26 stsp return error;
4656 2ec1f75b 2019-03-26 stsp __dead static void
4657 648e4ef7 2019-07-09 stsp usage_remove(void)
4659 c29c428a 2019-12-16 stsp fprintf(stderr, "usage: %s remove [-f] [-k] [-R] path ...\n",
4660 f2a9dc41 2019-12-13 tracey getprogname());
4664 2a06fe5f 2019-08-24 stsp static const struct got_error *
4665 2a06fe5f 2019-08-24 stsp print_remove_status(void *arg, unsigned char status,
4666 f2a9dc41 2019-12-13 tracey unsigned char staged_status, const char *path)
4668 f2a9dc41 2019-12-13 tracey while (path[0] == '/')
4670 2a06fe5f 2019-08-24 stsp if (status == GOT_STATUS_NONEXISTENT)
4671 2a06fe5f 2019-08-24 stsp return NULL;
4672 2a06fe5f 2019-08-24 stsp if (status == staged_status && (status == GOT_STATUS_DELETE))
4673 2a06fe5f 2019-08-24 stsp status = GOT_STATUS_NO_CHANGE;
4674 2a06fe5f 2019-08-24 stsp printf("%c%c %s\n", status, staged_status, path);
4675 2a06fe5f 2019-08-24 stsp return NULL;
4678 2ec1f75b 2019-03-26 stsp static const struct got_error *
4679 648e4ef7 2019-07-09 stsp cmd_remove(int argc, char *argv[])
4681 2ec1f75b 2019-03-26 stsp const struct got_error *error = NULL;
4682 2ec1f75b 2019-03-26 stsp struct got_worktree *worktree = NULL;
4683 2ec1f75b 2019-03-26 stsp struct got_repository *repo = NULL;
4684 17ed4618 2019-06-02 stsp char *cwd = NULL;
4685 17ed4618 2019-06-02 stsp struct got_pathlist_head paths;
4686 17ed4618 2019-06-02 stsp struct got_pathlist_entry *pe;
4687 70e3e7f5 2019-12-13 tracey int ch, delete_local_mods = 0, can_recurse = 0, keep_on_disk = 0;
4689 17ed4618 2019-06-02 stsp TAILQ_INIT(&paths);
4691 70e3e7f5 2019-12-13 tracey while ((ch = getopt(argc, argv, "fkR")) != -1) {
4692 2ec1f75b 2019-03-26 stsp switch (ch) {
4694 2ec1f75b 2019-03-26 stsp delete_local_mods = 1;
4696 70e3e7f5 2019-12-13 tracey case 'k':
4697 70e3e7f5 2019-12-13 tracey keep_on_disk = 1;
4699 f2a9dc41 2019-12-13 tracey case 'R':
4700 f2a9dc41 2019-12-13 tracey can_recurse = 1;
4703 f2a9dc41 2019-12-13 tracey usage_remove();
4704 2ec1f75b 2019-03-26 stsp /* NOTREACHED */
4708 2ec1f75b 2019-03-26 stsp argc -= optind;
4709 2ec1f75b 2019-03-26 stsp argv += optind;
4711 43012d58 2019-07-14 stsp #ifndef PROFILE
4712 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4713 43012d58 2019-07-14 stsp NULL) == -1)
4714 43012d58 2019-07-14 stsp err(1, "pledge");
4716 17ed4618 2019-06-02 stsp if (argc < 1)
4717 648e4ef7 2019-07-09 stsp usage_remove();
4719 2ec1f75b 2019-03-26 stsp cwd = getcwd(NULL, 0);
4720 2ec1f75b 2019-03-26 stsp if (cwd == NULL) {
4721 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
4722 2ec1f75b 2019-03-26 stsp goto done;
4724 2ec1f75b 2019-03-26 stsp error = got_worktree_open(&worktree, cwd);
4725 2ec1f75b 2019-03-26 stsp if (error)
4726 2ec1f75b 2019-03-26 stsp goto done;
4728 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4731 2ec1f75b 2019-03-26 stsp goto done;
4733 c2253644 2019-03-26 stsp error = apply_unveil(got_repo_get_path(repo), 1,
4734 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
4735 2ec1f75b 2019-03-26 stsp if (error)
4736 2ec1f75b 2019-03-26 stsp goto done;
4738 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4739 6d022e97 2019-08-04 stsp if (error)
4740 6d022e97 2019-08-04 stsp goto done;
4742 f2a9dc41 2019-12-13 tracey if (!can_recurse) {
4743 f2a9dc41 2019-12-13 tracey char *ondisk_path;
4744 f2a9dc41 2019-12-13 tracey struct stat sb;
4745 f2a9dc41 2019-12-13 tracey TAILQ_FOREACH(pe, &paths, entry) {
4746 f2a9dc41 2019-12-13 tracey if (asprintf(&ondisk_path, "%s/%s",
4747 f2a9dc41 2019-12-13 tracey got_worktree_get_root_path(worktree),
4748 f2a9dc41 2019-12-13 tracey pe->path) == -1) {
4749 f2a9dc41 2019-12-13 tracey error = got_error_from_errno("asprintf");
4750 f2a9dc41 2019-12-13 tracey goto done;
4752 f2a9dc41 2019-12-13 tracey if (lstat(ondisk_path, &sb) == -1) {
4753 f2a9dc41 2019-12-13 tracey if (errno == ENOENT) {
4754 f2a9dc41 2019-12-13 tracey free(ondisk_path);
4755 f2a9dc41 2019-12-13 tracey continue;
4757 f2a9dc41 2019-12-13 tracey error = got_error_from_errno2("lstat",
4758 f2a9dc41 2019-12-13 tracey ondisk_path);
4759 f2a9dc41 2019-12-13 tracey free(ondisk_path);
4760 f2a9dc41 2019-12-13 tracey goto done;
4762 f2a9dc41 2019-12-13 tracey free(ondisk_path);
4763 f2a9dc41 2019-12-13 tracey if (S_ISDIR(sb.st_mode)) {
4764 f2a9dc41 2019-12-13 tracey error = got_error_msg(GOT_ERR_BAD_PATH,
4765 f2a9dc41 2019-12-13 tracey "removing directories requires -R option");
4766 f2a9dc41 2019-12-13 tracey goto done;
4771 17ed4618 2019-06-02 stsp error = got_worktree_schedule_delete(worktree, &paths,
4772 70e3e7f5 2019-12-13 tracey delete_local_mods, print_remove_status, NULL, repo, keep_on_disk);
4775 a129376b 2019-03-28 stsp got_repo_close(repo);
4776 a129376b 2019-03-28 stsp if (worktree)
4777 a129376b 2019-03-28 stsp got_worktree_close(worktree);
4778 17ed4618 2019-06-02 stsp TAILQ_FOREACH(pe, &paths, entry)
4779 17ed4618 2019-06-02 stsp free((char *)pe->path);
4780 17ed4618 2019-06-02 stsp got_pathlist_free(&paths);
4781 a129376b 2019-03-28 stsp free(cwd);
4782 a129376b 2019-03-28 stsp return error;
4785 a129376b 2019-03-28 stsp __dead static void
4786 a129376b 2019-03-28 stsp usage_revert(void)
4788 33aa809d 2019-08-08 stsp fprintf(stderr, "usage: %s revert [-p] [-F response-script] [-R] "
4789 33aa809d 2019-08-08 stsp "path ...\n", getprogname());
4793 1ee397ad 2019-07-12 stsp static const struct got_error *
4794 a129376b 2019-03-28 stsp revert_progress(void *arg, unsigned char status, const char *path)
4796 fb9704af 2020-01-27 stsp if (status == GOT_STATUS_UNVERSIONED)
4797 fb9704af 2020-01-27 stsp return NULL;
4799 a129376b 2019-03-28 stsp while (path[0] == '/')
4801 a129376b 2019-03-28 stsp printf("%c %s\n", status, path);
4802 33aa809d 2019-08-08 stsp return NULL;
4805 33aa809d 2019-08-08 stsp struct choose_patch_arg {
4806 33aa809d 2019-08-08 stsp FILE *patch_script_file;
4807 33aa809d 2019-08-08 stsp const char *action;
4810 33aa809d 2019-08-08 stsp static const struct got_error *
4811 33aa809d 2019-08-08 stsp show_change(unsigned char status, const char *path, FILE *patch_file, int n,
4812 33aa809d 2019-08-08 stsp int nchanges, const char *action)
4814 33aa809d 2019-08-08 stsp char *line = NULL;
4815 33aa809d 2019-08-08 stsp size_t linesize = 0;
4816 33aa809d 2019-08-08 stsp ssize_t linelen;
4818 33aa809d 2019-08-08 stsp switch (status) {
4819 33aa809d 2019-08-08 stsp case GOT_STATUS_ADD:
4820 33aa809d 2019-08-08 stsp printf("A %s\n%s this addition? [y/n] ", path, action);
4822 33aa809d 2019-08-08 stsp case GOT_STATUS_DELETE:
4823 33aa809d 2019-08-08 stsp printf("D %s\n%s this deletion? [y/n] ", path, action);
4825 33aa809d 2019-08-08 stsp case GOT_STATUS_MODIFY:
4826 33aa809d 2019-08-08 stsp if (fseek(patch_file, 0L, SEEK_SET) == -1)
4827 33aa809d 2019-08-08 stsp return got_error_from_errno("fseek");
4828 33aa809d 2019-08-08 stsp printf(GOT_COMMIT_SEP_STR);
4829 9516e7cb 2019-08-11 stsp while ((linelen = getline(&line, &linesize, patch_file)) != -1)
4830 33aa809d 2019-08-08 stsp printf("%s", line);
4831 33aa809d 2019-08-08 stsp if (ferror(patch_file))
4832 33aa809d 2019-08-08 stsp return got_error_from_errno("getline");
4833 33aa809d 2019-08-08 stsp printf(GOT_COMMIT_SEP_STR);
4834 33aa809d 2019-08-08 stsp printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
4835 33aa809d 2019-08-08 stsp path, n, nchanges, action);
4838 33aa809d 2019-08-08 stsp return got_error_path(path, GOT_ERR_FILE_STATUS);
4841 33aa809d 2019-08-08 stsp return NULL;
4844 33aa809d 2019-08-08 stsp static const struct got_error *
4845 33aa809d 2019-08-08 stsp choose_patch(int *choice, void *arg, unsigned char status, const char *path,
4846 33aa809d 2019-08-08 stsp FILE *patch_file, int n, int nchanges)
4848 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
4849 33aa809d 2019-08-08 stsp char *line = NULL;
4850 33aa809d 2019-08-08 stsp size_t linesize = 0;
4851 33aa809d 2019-08-08 stsp ssize_t linelen;
4852 33aa809d 2019-08-08 stsp int resp = ' ';
4853 33aa809d 2019-08-08 stsp struct choose_patch_arg *a = arg;
4855 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NONE;
4857 33aa809d 2019-08-08 stsp if (a->patch_script_file) {
4859 33aa809d 2019-08-08 stsp err = show_change(status, path, patch_file, n, nchanges,
4860 33aa809d 2019-08-08 stsp a->action);
4862 33aa809d 2019-08-08 stsp return err;
4863 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, a->patch_script_file);
4864 33aa809d 2019-08-08 stsp if (linelen == -1) {
4865 33aa809d 2019-08-08 stsp if (ferror(a->patch_script_file))
4866 33aa809d 2019-08-08 stsp return got_error_from_errno("getline");
4867 33aa809d 2019-08-08 stsp return NULL;
4869 33aa809d 2019-08-08 stsp nl = strchr(line, '\n');
4871 33aa809d 2019-08-08 stsp *nl = '\0';
4872 33aa809d 2019-08-08 stsp if (strcmp(line, "y") == 0) {
4873 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_YES;
4874 33aa809d 2019-08-08 stsp printf("y\n");
4875 33aa809d 2019-08-08 stsp } else if (strcmp(line, "n") == 0) {
4876 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NO;
4877 33aa809d 2019-08-08 stsp printf("n\n");
4878 33aa809d 2019-08-08 stsp } else if (strcmp(line, "q") == 0 &&
4879 33aa809d 2019-08-08 stsp status == GOT_STATUS_MODIFY) {
4880 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_QUIT;
4881 33aa809d 2019-08-08 stsp printf("q\n");
4883 33aa809d 2019-08-08 stsp printf("invalid response '%s'\n", line);
4884 33aa809d 2019-08-08 stsp free(line);
4885 33aa809d 2019-08-08 stsp return NULL;
4888 33aa809d 2019-08-08 stsp while (resp != 'y' && resp != 'n' && resp != 'q') {
4889 33aa809d 2019-08-08 stsp err = show_change(status, path, patch_file, n, nchanges,
4890 33aa809d 2019-08-08 stsp a->action);
4892 33aa809d 2019-08-08 stsp return err;
4893 33aa809d 2019-08-08 stsp resp = getchar();
4894 33aa809d 2019-08-08 stsp if (resp == '\n')
4895 33aa809d 2019-08-08 stsp resp = getchar();
4896 33aa809d 2019-08-08 stsp if (status == GOT_STATUS_MODIFY) {
4897 33aa809d 2019-08-08 stsp if (resp != 'y' && resp != 'n' && resp != 'q') {
4898 33aa809d 2019-08-08 stsp printf("invalid response '%c'\n", resp);
4899 33aa809d 2019-08-08 stsp resp = ' ';
4901 33aa809d 2019-08-08 stsp } else if (resp != 'y' && resp != 'n') {
4902 33aa809d 2019-08-08 stsp printf("invalid response '%c'\n", resp);
4903 33aa809d 2019-08-08 stsp resp = ' ';
4907 33aa809d 2019-08-08 stsp if (resp == 'y')
4908 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_YES;
4909 33aa809d 2019-08-08 stsp else if (resp == 'n')
4910 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NO;
4911 33aa809d 2019-08-08 stsp else if (resp == 'q' && status == GOT_STATUS_MODIFY)
4912 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_QUIT;
4914 1ee397ad 2019-07-12 stsp return NULL;
4918 a129376b 2019-03-28 stsp static const struct got_error *
4919 a129376b 2019-03-28 stsp cmd_revert(int argc, char *argv[])
4921 a129376b 2019-03-28 stsp const struct got_error *error = NULL;
4922 a129376b 2019-03-28 stsp struct got_worktree *worktree = NULL;
4923 a129376b 2019-03-28 stsp struct got_repository *repo = NULL;
4924 a129376b 2019-03-28 stsp char *cwd = NULL, *path = NULL;
4925 e20a8b6f 2019-06-04 stsp struct got_pathlist_head paths;
4926 0f6d7415 2019-08-08 stsp struct got_pathlist_entry *pe;
4927 33aa809d 2019-08-08 stsp int ch, can_recurse = 0, pflag = 0;
4928 33aa809d 2019-08-08 stsp FILE *patch_script_file = NULL;
4929 33aa809d 2019-08-08 stsp const char *patch_script_path = NULL;
4930 33aa809d 2019-08-08 stsp struct choose_patch_arg cpa;
4932 e20a8b6f 2019-06-04 stsp TAILQ_INIT(&paths);
4934 33aa809d 2019-08-08 stsp while ((ch = getopt(argc, argv, "pF:R")) != -1) {
4935 a129376b 2019-03-28 stsp switch (ch) {
4937 33aa809d 2019-08-08 stsp pflag = 1;
4940 33aa809d 2019-08-08 stsp patch_script_path = optarg;
4943 0f6d7415 2019-08-08 stsp can_recurse = 1;
4946 a129376b 2019-03-28 stsp usage_revert();
4947 a129376b 2019-03-28 stsp /* NOTREACHED */
4951 a129376b 2019-03-28 stsp argc -= optind;
4952 a129376b 2019-03-28 stsp argv += optind;
4954 43012d58 2019-07-14 stsp #ifndef PROFILE
4955 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4956 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
4957 43012d58 2019-07-14 stsp err(1, "pledge");
4959 e20a8b6f 2019-06-04 stsp if (argc < 1)
4960 a129376b 2019-03-28 stsp usage_revert();
4961 33aa809d 2019-08-08 stsp if (patch_script_path && !pflag)
4962 33aa809d 2019-08-08 stsp errx(1, "-F option can only be used together with -p option");
4964 a129376b 2019-03-28 stsp cwd = getcwd(NULL, 0);
4965 a129376b 2019-03-28 stsp if (cwd == NULL) {
4966 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
4967 a129376b 2019-03-28 stsp goto done;
4969 a129376b 2019-03-28 stsp error = got_worktree_open(&worktree, cwd);
4970 2ec1f75b 2019-03-26 stsp if (error)
4971 2ec1f75b 2019-03-26 stsp goto done;
4973 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4975 a129376b 2019-03-28 stsp if (error != NULL)
4976 a129376b 2019-03-28 stsp goto done;
4978 33aa809d 2019-08-08 stsp if (patch_script_path) {
4979 33aa809d 2019-08-08 stsp patch_script_file = fopen(patch_script_path, "r");
4980 33aa809d 2019-08-08 stsp if (patch_script_file == NULL) {
4981 33aa809d 2019-08-08 stsp error = got_error_from_errno2("fopen",
4982 33aa809d 2019-08-08 stsp patch_script_path);
4983 33aa809d 2019-08-08 stsp goto done;
4986 a129376b 2019-03-28 stsp error = apply_unveil(got_repo_get_path(repo), 1,
4987 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
4988 a129376b 2019-03-28 stsp if (error)
4989 a129376b 2019-03-28 stsp goto done;
4991 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4992 6d022e97 2019-08-04 stsp if (error)
4993 6d022e97 2019-08-04 stsp goto done;
4995 0f6d7415 2019-08-08 stsp if (!can_recurse) {
4996 0f6d7415 2019-08-08 stsp char *ondisk_path;
4997 0f6d7415 2019-08-08 stsp struct stat sb;
4998 0f6d7415 2019-08-08 stsp TAILQ_FOREACH(pe, &paths, entry) {
4999 0f6d7415 2019-08-08 stsp if (asprintf(&ondisk_path, "%s/%s",
5000 0f6d7415 2019-08-08 stsp got_worktree_get_root_path(worktree),
5001 0f6d7415 2019-08-08 stsp pe->path) == -1) {
5002 0f6d7415 2019-08-08 stsp error = got_error_from_errno("asprintf");
5003 0f6d7415 2019-08-08 stsp goto done;
5005 0f6d7415 2019-08-08 stsp if (lstat(ondisk_path, &sb) == -1) {
5006 0f6d7415 2019-08-08 stsp if (errno == ENOENT) {
5007 0f6d7415 2019-08-08 stsp free(ondisk_path);
5010 0f6d7415 2019-08-08 stsp error = got_error_from_errno2("lstat",
5011 0f6d7415 2019-08-08 stsp ondisk_path);
5012 0f6d7415 2019-08-08 stsp free(ondisk_path);
5013 0f6d7415 2019-08-08 stsp goto done;
5015 0f6d7415 2019-08-08 stsp free(ondisk_path);
5016 0f6d7415 2019-08-08 stsp if (S_ISDIR(sb.st_mode)) {
5017 0f6d7415 2019-08-08 stsp error = got_error_msg(GOT_ERR_BAD_PATH,
5018 0f6d7415 2019-08-08 stsp "reverting directories requires -R option");
5019 0f6d7415 2019-08-08 stsp goto done;
5024 33aa809d 2019-08-08 stsp cpa.patch_script_file = patch_script_file;
5025 33aa809d 2019-08-08 stsp cpa.action = "revert";
5026 33aa809d 2019-08-08 stsp error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
5027 33aa809d 2019-08-08 stsp pflag ? choose_patch : NULL, &cpa, repo);
5029 33aa809d 2019-08-08 stsp if (patch_script_file && fclose(patch_script_file) == EOF &&
5030 33aa809d 2019-08-08 stsp error == NULL)
5031 33aa809d 2019-08-08 stsp error = got_error_from_errno2("fclose", patch_script_path);
5033 2ec1f75b 2019-03-26 stsp got_repo_close(repo);
5034 2ec1f75b 2019-03-26 stsp if (worktree)
5035 2ec1f75b 2019-03-26 stsp got_worktree_close(worktree);
5036 2ec1f75b 2019-03-26 stsp free(path);
5037 d00136be 2019-03-26 stsp free(cwd);
5038 6bad629b 2019-02-04 stsp return error;
5041 c4296144 2019-05-09 stsp __dead static void
5042 c4296144 2019-05-09 stsp usage_commit(void)
5044 5c1e53bc 2019-07-28 stsp fprintf(stderr, "usage: %s commit [-m msg] [path ...]\n",
5045 5c1e53bc 2019-07-28 stsp getprogname());
5049 e2ba3d07 2019-05-13 stsp struct collect_commit_logmsg_arg {
5050 e2ba3d07 2019-05-13 stsp const char *cmdline_log;
5051 e2ba3d07 2019-05-13 stsp const char *editor;
5052 e0870e44 2019-05-13 stsp const char *worktree_path;
5053 76d98825 2019-06-03 stsp const char *branch_name;
5054 314a6357 2019-05-13 stsp const char *repo_path;
5055 e0870e44 2019-05-13 stsp char *logmsg_path;
5059 33ad4cbe 2019-05-12 jcs static const struct got_error *
5060 33ad4cbe 2019-05-12 jcs collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
5063 76d98825 2019-06-03 stsp char *initial_content = NULL;
5064 33ad4cbe 2019-05-12 jcs struct got_pathlist_entry *pe;
5065 33ad4cbe 2019-05-12 jcs const struct got_error *err = NULL;
5066 e0870e44 2019-05-13 stsp char *template = NULL;
5067 e2ba3d07 2019-05-13 stsp struct collect_commit_logmsg_arg *a = arg;
5069 33ad4cbe 2019-05-12 jcs size_t len;
5071 33ad4cbe 2019-05-12 jcs /* if a message was specified on the command line, just use it */
5072 e2ba3d07 2019-05-13 stsp if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
5073 e2ba3d07 2019-05-13 stsp len = strlen(a->cmdline_log) + 1;
5074 33ad4cbe 2019-05-12 jcs *logmsg = malloc(len + 1);
5075 9f42ff69 2019-05-13 stsp if (*logmsg == NULL)
5076 638f9024 2019-05-13 stsp return got_error_from_errno("malloc");
5077 e2ba3d07 2019-05-13 stsp strlcpy(*logmsg, a->cmdline_log, len);
5078 33ad4cbe 2019-05-12 jcs return NULL;
5081 e0870e44 2019-05-13 stsp if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
5082 76d98825 2019-06-03 stsp return got_error_from_errno("asprintf");
5084 76d98825 2019-06-03 stsp if (asprintf(&initial_content,
5085 76d98825 2019-06-03 stsp "\n# changes to be committed on branch %s:\n",
5086 76d98825 2019-06-03 stsp a->branch_name) == -1)
5087 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
5089 e0870e44 2019-05-13 stsp err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
5091 e0870e44 2019-05-13 stsp goto done;
5093 e0870e44 2019-05-13 stsp dprintf(fd, initial_content);
5095 33ad4cbe 2019-05-12 jcs TAILQ_FOREACH(pe, commitable_paths, entry) {
5096 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5097 8656d6c4 2019-05-20 stsp dprintf(fd, "# %c %s\n",
5098 8656d6c4 2019-05-20 stsp got_commitable_get_status(ct),
5099 8656d6c4 2019-05-20 stsp got_commitable_get_path(ct));
5103 3ce1b845 2019-07-15 stsp err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content);
5105 76d98825 2019-06-03 stsp free(initial_content);
5106 e0870e44 2019-05-13 stsp free(template);
5108 314a6357 2019-05-13 stsp /* Editor is done; we can now apply unveil(2) */
5109 3ce1b845 2019-07-15 stsp if (err == NULL) {
5110 c530dc23 2019-07-23 stsp err = apply_unveil(a->repo_path, 0, a->worktree_path);
5111 3ce1b845 2019-07-15 stsp if (err) {
5112 3ce1b845 2019-07-15 stsp free(*logmsg);
5113 3ce1b845 2019-07-15 stsp *logmsg = NULL;
5116 33ad4cbe 2019-05-12 jcs return err;
5119 c4296144 2019-05-09 stsp static const struct got_error *
5120 c4296144 2019-05-09 stsp cmd_commit(int argc, char *argv[])
5122 c4296144 2019-05-09 stsp const struct got_error *error = NULL;
5123 c4296144 2019-05-09 stsp struct got_worktree *worktree = NULL;
5124 c4296144 2019-05-09 stsp struct got_repository *repo = NULL;
5125 5c1e53bc 2019-07-28 stsp char *cwd = NULL, *id_str = NULL;
5126 c4296144 2019-05-09 stsp struct got_object_id *id = NULL;
5127 33ad4cbe 2019-05-12 jcs const char *logmsg = NULL;
5128 aba9c984 2019-09-08 stsp struct collect_commit_logmsg_arg cl_arg;
5129 c9956ddf 2019-09-08 stsp char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
5130 7266f21f 2019-10-21 stsp int ch, rebase_in_progress, histedit_in_progress, preserve_logmsg = 0;
5131 5c1e53bc 2019-07-28 stsp struct got_pathlist_head paths;
5133 5c1e53bc 2019-07-28 stsp TAILQ_INIT(&paths);
5134 6ac5a73c 2019-08-08 stsp cl_arg.logmsg_path = NULL;
5136 c4296144 2019-05-09 stsp while ((ch = getopt(argc, argv, "m:")) != -1) {
5137 c4296144 2019-05-09 stsp switch (ch) {
5139 c4296144 2019-05-09 stsp logmsg = optarg;
5142 c4296144 2019-05-09 stsp usage_commit();
5143 c4296144 2019-05-09 stsp /* NOTREACHED */
5147 c4296144 2019-05-09 stsp argc -= optind;
5148 c4296144 2019-05-09 stsp argv += optind;
5150 43012d58 2019-07-14 stsp #ifndef PROFILE
5151 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5152 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
5153 43012d58 2019-07-14 stsp err(1, "pledge");
5155 c4296144 2019-05-09 stsp cwd = getcwd(NULL, 0);
5156 c4296144 2019-05-09 stsp if (cwd == NULL) {
5157 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
5158 c4296144 2019-05-09 stsp goto done;
5160 c4296144 2019-05-09 stsp error = got_worktree_open(&worktree, cwd);
5161 7d5807f4 2019-07-11 stsp if (error)
5162 7d5807f4 2019-07-11 stsp goto done;
5164 a698f62e 2019-07-25 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
5165 c4296144 2019-05-09 stsp if (error)
5166 a698f62e 2019-07-25 stsp goto done;
5167 a698f62e 2019-07-25 stsp if (rebase_in_progress) {
5168 a698f62e 2019-07-25 stsp error = got_error(GOT_ERR_REBASING);
5169 c4296144 2019-05-09 stsp goto done;
5172 916f288c 2019-07-30 stsp error = got_worktree_histedit_in_progress(&histedit_in_progress,
5173 916f288c 2019-07-30 stsp worktree);
5174 5c1e53bc 2019-07-28 stsp if (error)
5175 5c1e53bc 2019-07-28 stsp goto done;
5177 c9956ddf 2019-09-08 stsp error = get_gitconfig_path(&gitconfig_path);
5178 c9956ddf 2019-09-08 stsp if (error)
5179 c9956ddf 2019-09-08 stsp goto done;
5180 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5181 c9956ddf 2019-09-08 stsp gitconfig_path);
5182 c4296144 2019-05-09 stsp if (error != NULL)
5183 c4296144 2019-05-09 stsp goto done;
5185 aba9c984 2019-09-08 stsp error = get_author(&author, repo);
5186 aba9c984 2019-09-08 stsp if (error)
5187 aba9c984 2019-09-08 stsp return error;
5190 314a6357 2019-05-13 stsp * unveil(2) traverses exec(2); if an editor is used we have
5191 314a6357 2019-05-13 stsp * to apply unveil after the log message has been written.
5193 314a6357 2019-05-13 stsp if (logmsg == NULL || strlen(logmsg) == 0)
5194 314a6357 2019-05-13 stsp error = get_editor(&editor);
5196 314a6357 2019-05-13 stsp error = apply_unveil(got_repo_get_path(repo), 0,
5197 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
5198 c4296144 2019-05-09 stsp if (error)
5199 c4296144 2019-05-09 stsp goto done;
5201 087fb88c 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5202 087fb88c 2019-08-04 stsp if (error)
5203 087fb88c 2019-08-04 stsp goto done;
5205 e2ba3d07 2019-05-13 stsp cl_arg.editor = editor;
5206 e2ba3d07 2019-05-13 stsp cl_arg.cmdline_log = logmsg;
5207 e0870e44 2019-05-13 stsp cl_arg.worktree_path = got_worktree_get_root_path(worktree);
5208 76d98825 2019-06-03 stsp cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
5209 916f288c 2019-07-30 stsp if (!histedit_in_progress) {
5210 916f288c 2019-07-30 stsp if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
5211 916f288c 2019-07-30 stsp error = got_error(GOT_ERR_COMMIT_BRANCH);
5212 916f288c 2019-07-30 stsp goto done;
5214 916f288c 2019-07-30 stsp cl_arg.branch_name += 11;
5216 314a6357 2019-05-13 stsp cl_arg.repo_path = got_repo_get_path(repo);
5217 84792843 2019-08-09 stsp error = got_worktree_commit(&id, worktree, &paths, author, NULL,
5218 e2ba3d07 2019-05-13 stsp collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
5219 e0870e44 2019-05-13 stsp if (error) {
5220 7266f21f 2019-10-21 stsp if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
5221 7266f21f 2019-10-21 stsp cl_arg.logmsg_path != NULL)
5222 7266f21f 2019-10-21 stsp preserve_logmsg = 1;
5223 c4296144 2019-05-09 stsp goto done;
5226 c4296144 2019-05-09 stsp error = got_object_id_str(&id_str, id);
5227 c4296144 2019-05-09 stsp if (error)
5228 c4296144 2019-05-09 stsp goto done;
5229 a7648d7a 2019-06-02 stsp printf("Created commit %s\n", id_str);
5231 7266f21f 2019-10-21 stsp if (preserve_logmsg) {
5232 7266f21f 2019-10-21 stsp fprintf(stderr, "%s: log message preserved in %s\n",
5233 7266f21f 2019-10-21 stsp getprogname(), cl_arg.logmsg_path);
5234 7266f21f 2019-10-21 stsp } else if (cl_arg.logmsg_path && unlink(cl_arg.logmsg_path) == -1 &&
5235 7266f21f 2019-10-21 stsp error == NULL)
5236 7266f21f 2019-10-21 stsp error = got_error_from_errno2("unlink", cl_arg.logmsg_path);
5237 6ac5a73c 2019-08-08 stsp free(cl_arg.logmsg_path);
5239 c4296144 2019-05-09 stsp got_repo_close(repo);
5240 c4296144 2019-05-09 stsp if (worktree)
5241 c4296144 2019-05-09 stsp got_worktree_close(worktree);
5242 c4296144 2019-05-09 stsp free(cwd);
5243 c4296144 2019-05-09 stsp free(id_str);
5244 c9956ddf 2019-09-08 stsp free(gitconfig_path);
5245 e2ba3d07 2019-05-13 stsp free(editor);
5246 aba9c984 2019-09-08 stsp free(author);
5247 234035bc 2019-06-01 stsp return error;
5250 234035bc 2019-06-01 stsp __dead static void
5251 234035bc 2019-06-01 stsp usage_cherrypick(void)
5253 234035bc 2019-06-01 stsp fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
5257 234035bc 2019-06-01 stsp static const struct got_error *
5258 234035bc 2019-06-01 stsp cmd_cherrypick(int argc, char *argv[])
5260 234035bc 2019-06-01 stsp const struct got_error *error = NULL;
5261 234035bc 2019-06-01 stsp struct got_worktree *worktree = NULL;
5262 234035bc 2019-06-01 stsp struct got_repository *repo = NULL;
5263 234035bc 2019-06-01 stsp char *cwd = NULL, *commit_id_str = NULL;
5264 234035bc 2019-06-01 stsp struct got_object_id *commit_id = NULL;
5265 234035bc 2019-06-01 stsp struct got_commit_object *commit = NULL;
5266 234035bc 2019-06-01 stsp struct got_object_qid *pid;
5267 234035bc 2019-06-01 stsp struct got_reference *head_ref = NULL;
5268 234035bc 2019-06-01 stsp int ch, did_something = 0;
5270 234035bc 2019-06-01 stsp while ((ch = getopt(argc, argv, "")) != -1) {
5271 234035bc 2019-06-01 stsp switch (ch) {
5273 234035bc 2019-06-01 stsp usage_cherrypick();
5274 234035bc 2019-06-01 stsp /* NOTREACHED */
5278 234035bc 2019-06-01 stsp argc -= optind;
5279 234035bc 2019-06-01 stsp argv += optind;
5281 43012d58 2019-07-14 stsp #ifndef PROFILE
5282 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5283 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
5284 43012d58 2019-07-14 stsp err(1, "pledge");
5286 234035bc 2019-06-01 stsp if (argc != 1)
5287 234035bc 2019-06-01 stsp usage_cherrypick();
5289 234035bc 2019-06-01 stsp cwd = getcwd(NULL, 0);
5290 234035bc 2019-06-01 stsp if (cwd == NULL) {
5291 234035bc 2019-06-01 stsp error = got_error_from_errno("getcwd");
5292 234035bc 2019-06-01 stsp goto done;
5294 234035bc 2019-06-01 stsp error = got_worktree_open(&worktree, cwd);
5295 234035bc 2019-06-01 stsp if (error)
5296 234035bc 2019-06-01 stsp goto done;
5298 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5300 234035bc 2019-06-01 stsp if (error != NULL)
5301 234035bc 2019-06-01 stsp goto done;
5303 234035bc 2019-06-01 stsp error = apply_unveil(got_repo_get_path(repo), 0,
5304 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
5305 234035bc 2019-06-01 stsp if (error)
5306 234035bc 2019-06-01 stsp goto done;
5308 dd88155e 2019-06-29 stsp error = got_repo_match_object_id_prefix(&commit_id, argv[0],
5309 dd88155e 2019-06-29 stsp GOT_OBJ_TYPE_COMMIT, repo);
5310 234035bc 2019-06-01 stsp if (error != NULL) {
5311 234035bc 2019-06-01 stsp struct got_reference *ref;
5312 234035bc 2019-06-01 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
5313 234035bc 2019-06-01 stsp goto done;
5314 234035bc 2019-06-01 stsp error = got_ref_open(&ref, repo, argv[0], 0);
5315 234035bc 2019-06-01 stsp if (error != NULL)
5316 234035bc 2019-06-01 stsp goto done;
5317 234035bc 2019-06-01 stsp error = got_ref_resolve(&commit_id, repo, ref);
5318 234035bc 2019-06-01 stsp got_ref_close(ref);
5319 234035bc 2019-06-01 stsp if (error != NULL)
5320 234035bc 2019-06-01 stsp goto done;
5322 234035bc 2019-06-01 stsp error = got_object_id_str(&commit_id_str, commit_id);
5323 234035bc 2019-06-01 stsp if (error)
5324 234035bc 2019-06-01 stsp goto done;
5326 234035bc 2019-06-01 stsp error = got_ref_open(&head_ref, repo,
5327 234035bc 2019-06-01 stsp got_worktree_get_head_ref_name(worktree), 0);
5328 234035bc 2019-06-01 stsp if (error != NULL)
5329 234035bc 2019-06-01 stsp goto done;
5331 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
5332 234035bc 2019-06-01 stsp if (error) {
5333 234035bc 2019-06-01 stsp if (error->code != GOT_ERR_ANCESTRY)
5334 234035bc 2019-06-01 stsp goto done;
5335 234035bc 2019-06-01 stsp error = NULL;
5337 234035bc 2019-06-01 stsp error = got_error(GOT_ERR_SAME_BRANCH);
5338 234035bc 2019-06-01 stsp goto done;
5341 234035bc 2019-06-01 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
5342 234035bc 2019-06-01 stsp if (error)
5343 234035bc 2019-06-01 stsp goto done;
5344 234035bc 2019-06-01 stsp pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
5345 03415a1a 2019-06-02 stsp error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
5346 03415a1a 2019-06-02 stsp commit_id, repo, update_progress, &did_something, check_cancelled,
5348 234035bc 2019-06-01 stsp if (error != NULL)
5349 234035bc 2019-06-01 stsp goto done;
5351 234035bc 2019-06-01 stsp if (did_something)
5352 a7648d7a 2019-06-02 stsp printf("Merged commit %s\n", commit_id_str);
5354 234035bc 2019-06-01 stsp if (commit)
5355 234035bc 2019-06-01 stsp got_object_commit_close(commit);
5356 234035bc 2019-06-01 stsp free(commit_id_str);
5357 234035bc 2019-06-01 stsp if (head_ref)
5358 234035bc 2019-06-01 stsp got_ref_close(head_ref);
5359 234035bc 2019-06-01 stsp if (worktree)
5360 234035bc 2019-06-01 stsp got_worktree_close(worktree);
5362 234035bc 2019-06-01 stsp got_repo_close(repo);
5363 c4296144 2019-05-09 stsp return error;
5366 5ef14e63 2019-06-02 stsp __dead static void
5367 5ef14e63 2019-06-02 stsp usage_backout(void)
5369 5ef14e63 2019-06-02 stsp fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
5373 5ef14e63 2019-06-02 stsp static const struct got_error *
5374 5ef14e63 2019-06-02 stsp cmd_backout(int argc, char *argv[])
5376 5ef14e63 2019-06-02 stsp const struct got_error *error = NULL;
5377 5ef14e63 2019-06-02 stsp struct got_worktree *worktree = NULL;
5378 5ef14e63 2019-06-02 stsp struct got_repository *repo = NULL;
5379 5ef14e63 2019-06-02 stsp char *cwd = NULL, *commit_id_str = NULL;
5380 5ef14e63 2019-06-02 stsp struct got_object_id *commit_id = NULL;
5381 5ef14e63 2019-06-02 stsp struct got_commit_object *commit = NULL;
5382 5ef14e63 2019-06-02 stsp struct got_object_qid *pid;
5383 5ef14e63 2019-06-02 stsp struct got_reference *head_ref = NULL;
5384 5ef14e63 2019-06-02 stsp int ch, did_something = 0;
5386 5ef14e63 2019-06-02 stsp while ((ch = getopt(argc, argv, "")) != -1) {
5387 5ef14e63 2019-06-02 stsp switch (ch) {
5389 5ef14e63 2019-06-02 stsp usage_backout();
5390 5ef14e63 2019-06-02 stsp /* NOTREACHED */
5394 5ef14e63 2019-06-02 stsp argc -= optind;
5395 5ef14e63 2019-06-02 stsp argv += optind;
5397 43012d58 2019-07-14 stsp #ifndef PROFILE
5398 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5399 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
5400 43012d58 2019-07-14 stsp err(1, "pledge");
5402 5ef14e63 2019-06-02 stsp if (argc != 1)
5403 5ef14e63 2019-06-02 stsp usage_backout();
5405 5ef14e63 2019-06-02 stsp cwd = getcwd(NULL, 0);
5406 5ef14e63 2019-06-02 stsp if (cwd == NULL) {
5407 5ef14e63 2019-06-02 stsp error = got_error_from_errno("getcwd");
5408 5ef14e63 2019-06-02 stsp goto done;
5410 5ef14e63 2019-06-02 stsp error = got_worktree_open(&worktree, cwd);
5411 5ef14e63 2019-06-02 stsp if (error)
5412 5ef14e63 2019-06-02 stsp goto done;
5414 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5416 5ef14e63 2019-06-02 stsp if (error != NULL)
5417 5ef14e63 2019-06-02 stsp goto done;
5419 5ef14e63 2019-06-02 stsp error = apply_unveil(got_repo_get_path(repo), 0,
5420 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
5421 5ef14e63 2019-06-02 stsp if (error)
5422 5ef14e63 2019-06-02 stsp goto done;
5424 dd88155e 2019-06-29 stsp error = got_repo_match_object_id_prefix(&commit_id, argv[0],
5425 dd88155e 2019-06-29 stsp GOT_OBJ_TYPE_COMMIT, repo);
5426 5ef14e63 2019-06-02 stsp if (error != NULL) {
5427 5ef14e63 2019-06-02 stsp struct got_reference *ref;
5428 5ef14e63 2019-06-02 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
5429 5ef14e63 2019-06-02 stsp goto done;
5430 5ef14e63 2019-06-02 stsp error = got_ref_open(&ref, repo, argv[0], 0);
5431 5ef14e63 2019-06-02 stsp if (error != NULL)
5432 5ef14e63 2019-06-02 stsp goto done;
5433 5ef14e63 2019-06-02 stsp error = got_ref_resolve(&commit_id, repo, ref);
5434 5ef14e63 2019-06-02 stsp got_ref_close(ref);
5435 5ef14e63 2019-06-02 stsp if (error != NULL)
5436 5ef14e63 2019-06-02 stsp goto done;
5438 5ef14e63 2019-06-02 stsp error = got_object_id_str(&commit_id_str, commit_id);
5439 5ef14e63 2019-06-02 stsp if (error)
5440 5ef14e63 2019-06-02 stsp goto done;
5442 5ef14e63 2019-06-02 stsp error = got_ref_open(&head_ref, repo,
5443 5ef14e63 2019-06-02 stsp got_worktree_get_head_ref_name(worktree), 0);
5444 5ef14e63 2019-06-02 stsp if (error != NULL)
5445 5ef14e63 2019-06-02 stsp goto done;
5447 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
5448 5ef14e63 2019-06-02 stsp if (error)
5449 5ef14e63 2019-06-02 stsp goto done;
5451 5ef14e63 2019-06-02 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
5452 5ef14e63 2019-06-02 stsp if (error)
5453 5ef14e63 2019-06-02 stsp goto done;
5454 5ef14e63 2019-06-02 stsp pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
5455 5ef14e63 2019-06-02 stsp if (pid == NULL) {
5456 5ef14e63 2019-06-02 stsp error = got_error(GOT_ERR_ROOT_COMMIT);
5457 5ef14e63 2019-06-02 stsp goto done;
5460 5ef14e63 2019-06-02 stsp error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
5461 5ef14e63 2019-06-02 stsp update_progress, &did_something, check_cancelled, NULL);
5462 5ef14e63 2019-06-02 stsp if (error != NULL)
5463 5ef14e63 2019-06-02 stsp goto done;
5465 5ef14e63 2019-06-02 stsp if (did_something)
5466 a7648d7a 2019-06-02 stsp printf("Backed out commit %s\n", commit_id_str);
5468 5ef14e63 2019-06-02 stsp if (commit)
5469 5ef14e63 2019-06-02 stsp got_object_commit_close(commit);
5470 5ef14e63 2019-06-02 stsp free(commit_id_str);
5471 5ef14e63 2019-06-02 stsp if (head_ref)
5472 5ef14e63 2019-06-02 stsp got_ref_close(head_ref);
5473 818c7501 2019-07-11 stsp if (worktree)
5474 818c7501 2019-07-11 stsp got_worktree_close(worktree);
5476 818c7501 2019-07-11 stsp got_repo_close(repo);
5477 818c7501 2019-07-11 stsp return error;
5480 818c7501 2019-07-11 stsp __dead static void
5481 818c7501 2019-07-11 stsp usage_rebase(void)
5483 af54c8f8 2019-07-11 stsp fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
5484 af54c8f8 2019-07-11 stsp getprogname());
5489 0ebf8283 2019-07-24 stsp trim_logmsg(char *logmsg, int limit)
5492 0ebf8283 2019-07-24 stsp size_t len;
5494 0ebf8283 2019-07-24 stsp len = strlen(logmsg);
5495 0ebf8283 2019-07-24 stsp if (len > limit)
5496 0ebf8283 2019-07-24 stsp len = limit;
5497 0ebf8283 2019-07-24 stsp logmsg[len] = '\0';
5498 0ebf8283 2019-07-24 stsp nl = strchr(logmsg, '\n');
5500 0ebf8283 2019-07-24 stsp *nl = '\0';
5503 0ebf8283 2019-07-24 stsp static const struct got_error *
5504 0ebf8283 2019-07-24 stsp get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
5506 5943eee2 2019-08-13 stsp const struct got_error *err;
5507 5943eee2 2019-08-13 stsp char *logmsg0 = NULL;
5508 5943eee2 2019-08-13 stsp const char *s;
5510 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
5512 5943eee2 2019-08-13 stsp return err;
5514 5943eee2 2019-08-13 stsp s = logmsg0;
5515 5943eee2 2019-08-13 stsp while (isspace((unsigned char)s[0]))
5518 5943eee2 2019-08-13 stsp *logmsg = strdup(s);
5519 5943eee2 2019-08-13 stsp if (*logmsg == NULL) {
5520 5943eee2 2019-08-13 stsp err = got_error_from_errno("strdup");
5521 5943eee2 2019-08-13 stsp goto done;
5524 0ebf8283 2019-07-24 stsp trim_logmsg(*logmsg, limit);
5526 5943eee2 2019-08-13 stsp free(logmsg0);
5527 a0ea4fc0 2020-02-28 stsp return err;
5530 a0ea4fc0 2020-02-28 stsp static const struct got_error *
5531 a0ea4fc0 2020-02-28 stsp show_rebase_merge_conflict(struct got_object_id *id, struct got_repository *repo)
5533 a0ea4fc0 2020-02-28 stsp const struct got_error *err;
5534 a0ea4fc0 2020-02-28 stsp struct got_commit_object *commit = NULL;
5535 a0ea4fc0 2020-02-28 stsp char *id_str = NULL, *logmsg = NULL;
5537 a0ea4fc0 2020-02-28 stsp err = got_object_open_as_commit(&commit, repo, id);
5539 a0ea4fc0 2020-02-28 stsp return err;
5541 a0ea4fc0 2020-02-28 stsp err = got_object_id_str(&id_str, id);
5543 a0ea4fc0 2020-02-28 stsp goto done;
5545 a0ea4fc0 2020-02-28 stsp id_str[12] = '\0';
5547 a0ea4fc0 2020-02-28 stsp err = get_short_logmsg(&logmsg, 42, commit);
5549 a0ea4fc0 2020-02-28 stsp goto done;
5551 a0ea4fc0 2020-02-28 stsp printf("%s -> merge conflict: %s\n", id_str, logmsg);
5553 a0ea4fc0 2020-02-28 stsp free(id_str);
5554 a0ea4fc0 2020-02-28 stsp got_object_commit_close(commit);
5555 a0ea4fc0 2020-02-28 stsp free(logmsg);
5556 5943eee2 2019-08-13 stsp return err;
5559 818c7501 2019-07-11 stsp static const struct got_error *
5560 818c7501 2019-07-11 stsp show_rebase_progress(struct got_commit_object *commit,
5561 818c7501 2019-07-11 stsp struct got_object_id *old_id, struct got_object_id *new_id)
5563 818c7501 2019-07-11 stsp const struct got_error *err;
5564 0ebf8283 2019-07-24 stsp char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
5566 818c7501 2019-07-11 stsp err = got_object_id_str(&old_id_str, old_id);
5568 818c7501 2019-07-11 stsp goto done;
5570 ff0d2220 2019-07-11 stsp if (new_id) {
5571 ff0d2220 2019-07-11 stsp err = got_object_id_str(&new_id_str, new_id);
5573 ff0d2220 2019-07-11 stsp goto done;
5576 818c7501 2019-07-11 stsp old_id_str[12] = '\0';
5577 ff0d2220 2019-07-11 stsp if (new_id_str)
5578 ff0d2220 2019-07-11 stsp new_id_str[12] = '\0';
5580 0ebf8283 2019-07-24 stsp err = get_short_logmsg(&logmsg, 42, commit);
5582 0ebf8283 2019-07-24 stsp goto done;
5584 ff0d2220 2019-07-11 stsp printf("%s -> %s: %s\n", old_id_str,
5585 ff0d2220 2019-07-11 stsp new_id_str ? new_id_str : "no-op change", logmsg);
5587 818c7501 2019-07-11 stsp free(old_id_str);
5588 818c7501 2019-07-11 stsp free(new_id_str);
5589 272a1371 2020-02-28 stsp free(logmsg);
5590 818c7501 2019-07-11 stsp return err;
5593 1ee397ad 2019-07-12 stsp static const struct got_error *
5594 818c7501 2019-07-11 stsp rebase_progress(void *arg, unsigned char status, const char *path)
5596 818c7501 2019-07-11 stsp unsigned char *rebase_status = arg;
5598 818c7501 2019-07-11 stsp while (path[0] == '/')
5600 818c7501 2019-07-11 stsp printf("%c %s\n", status, path);
5602 818c7501 2019-07-11 stsp if (*rebase_status == GOT_STATUS_CONFLICT)
5603 1ee397ad 2019-07-12 stsp return NULL;
5604 818c7501 2019-07-11 stsp if (status == GOT_STATUS_CONFLICT || status == GOT_STATUS_MERGE)
5605 818c7501 2019-07-11 stsp *rebase_status = status;
5606 1ee397ad 2019-07-12 stsp return NULL;
5609 818c7501 2019-07-11 stsp static const struct got_error *
5610 3e3a69f1 2019-07-25 stsp rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
5611 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_reference *new_base_branch,
5612 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_repository *repo)
5614 818c7501 2019-07-11 stsp printf("Switching work tree to %s\n", got_ref_get_name(branch));
5615 3e3a69f1 2019-07-25 stsp return got_worktree_rebase_complete(worktree, fileindex,
5616 818c7501 2019-07-11 stsp new_base_branch, tmp_branch, branch, repo);
5619 818c7501 2019-07-11 stsp static const struct got_error *
5620 01757395 2019-07-12 stsp rebase_commit(struct got_pathlist_head *merged_paths,
5621 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
5622 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch,
5623 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
5625 ff0d2220 2019-07-11 stsp const struct got_error *error;
5626 ff0d2220 2019-07-11 stsp struct got_commit_object *commit;
5627 ff0d2220 2019-07-11 stsp struct got_object_id *new_commit_id;
5629 ff0d2220 2019-07-11 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
5630 ff0d2220 2019-07-11 stsp if (error)
5631 ff0d2220 2019-07-11 stsp return error;
5633 01757395 2019-07-12 stsp error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
5634 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, commit, commit_id, repo);
5635 ff0d2220 2019-07-11 stsp if (error) {
5636 ff0d2220 2019-07-11 stsp if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
5637 ff0d2220 2019-07-11 stsp goto done;
5638 ff0d2220 2019-07-11 stsp error = show_rebase_progress(commit, commit_id, NULL);
5640 ff0d2220 2019-07-11 stsp error = show_rebase_progress(commit, commit_id, new_commit_id);
5641 ff0d2220 2019-07-11 stsp free(new_commit_id);
5644 ff0d2220 2019-07-11 stsp got_object_commit_close(commit);
5645 ff0d2220 2019-07-11 stsp return error;
5648 64c6d990 2019-07-11 stsp struct check_path_prefix_arg {
5649 64c6d990 2019-07-11 stsp const char *path_prefix;
5650 64c6d990 2019-07-11 stsp size_t len;
5651 8ca9bd68 2019-07-25 stsp int errcode;
5654 64c6d990 2019-07-11 stsp static const struct got_error *
5655 8ca9bd68 2019-07-25 stsp check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
5656 64c6d990 2019-07-11 stsp struct got_blob_object *blob2, struct got_object_id *id1,
5657 64c6d990 2019-07-11 stsp struct got_object_id *id2, const char *path1, const char *path2,
5658 46f68b20 2019-10-19 stsp mode_t mode1, mode_t mode2, struct got_repository *repo)
5660 64c6d990 2019-07-11 stsp struct check_path_prefix_arg *a = arg;
5662 64c6d990 2019-07-11 stsp if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
5663 64c6d990 2019-07-11 stsp (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
5664 8ca9bd68 2019-07-25 stsp return got_error(a->errcode);
5666 64c6d990 2019-07-11 stsp return NULL;
5669 64c6d990 2019-07-11 stsp static const struct got_error *
5670 8ca9bd68 2019-07-25 stsp check_path_prefix(struct got_object_id *parent_id,
5671 64c6d990 2019-07-11 stsp struct got_object_id *commit_id, const char *path_prefix,
5672 8ca9bd68 2019-07-25 stsp int errcode, struct got_repository *repo)
5674 64c6d990 2019-07-11 stsp const struct got_error *err;
5675 64c6d990 2019-07-11 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
5676 64c6d990 2019-07-11 stsp struct got_commit_object *commit = NULL, *parent_commit = NULL;
5677 64c6d990 2019-07-11 stsp struct check_path_prefix_arg cpp_arg;
5679 64c6d990 2019-07-11 stsp if (got_path_is_root_dir(path_prefix))
5680 64c6d990 2019-07-11 stsp return NULL;
5682 64c6d990 2019-07-11 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
5684 64c6d990 2019-07-11 stsp goto done;
5686 64c6d990 2019-07-11 stsp err = got_object_open_as_commit(&parent_commit, repo, parent_id);
5688 64c6d990 2019-07-11 stsp goto done;
5690 64c6d990 2019-07-11 stsp err = got_object_open_as_tree(&tree1, repo,
5691 64c6d990 2019-07-11 stsp got_object_commit_get_tree_id(parent_commit));
5693 64c6d990 2019-07-11 stsp goto done;
5695 64c6d990 2019-07-11 stsp err = got_object_open_as_tree(&tree2, repo,
5696 64c6d990 2019-07-11 stsp got_object_commit_get_tree_id(commit));
5698 64c6d990 2019-07-11 stsp goto done;
5700 64c6d990 2019-07-11 stsp cpp_arg.path_prefix = path_prefix;
5701 d23ace97 2019-07-25 stsp while (cpp_arg.path_prefix[0] == '/')
5702 d23ace97 2019-07-25 stsp cpp_arg.path_prefix++;
5703 d23ace97 2019-07-25 stsp cpp_arg.len = strlen(cpp_arg.path_prefix);
5704 8ca9bd68 2019-07-25 stsp cpp_arg.errcode = errcode;
5705 8ca9bd68 2019-07-25 stsp err = got_diff_tree(tree1, tree2, "", "", repo,
5706 31b4484f 2019-07-27 stsp check_path_prefix_in_diff, &cpp_arg, 0);
5708 64c6d990 2019-07-11 stsp if (tree1)
5709 64c6d990 2019-07-11 stsp got_object_tree_close(tree1);
5710 64c6d990 2019-07-11 stsp if (tree2)
5711 64c6d990 2019-07-11 stsp got_object_tree_close(tree2);
5712 64c6d990 2019-07-11 stsp if (commit)
5713 64c6d990 2019-07-11 stsp got_object_commit_close(commit);
5714 64c6d990 2019-07-11 stsp if (parent_commit)
5715 64c6d990 2019-07-11 stsp got_object_commit_close(parent_commit);
5716 64c6d990 2019-07-11 stsp return err;
5719 ff0d2220 2019-07-11 stsp static const struct got_error *
5720 8ca9bd68 2019-07-25 stsp collect_commits(struct got_object_id_queue *commits,
5721 af61c510 2019-07-19 stsp struct got_object_id *initial_commit_id,
5722 af61c510 2019-07-19 stsp struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
5723 8ca9bd68 2019-07-25 stsp const char *path_prefix, int path_prefix_errcode,
5724 8ca9bd68 2019-07-25 stsp struct got_repository *repo)
5726 af61c510 2019-07-19 stsp const struct got_error *err = NULL;
5727 af61c510 2019-07-19 stsp struct got_commit_graph *graph = NULL;
5728 af61c510 2019-07-19 stsp struct got_object_id *parent_id = NULL;
5729 af61c510 2019-07-19 stsp struct got_object_qid *qid;
5730 af61c510 2019-07-19 stsp struct got_object_id *commit_id = initial_commit_id;
5732 3d509237 2020-01-04 stsp err = got_commit_graph_open(&graph, "/", 1);
5734 af61c510 2019-07-19 stsp return err;
5736 6fb7cd11 2019-08-22 stsp err = got_commit_graph_iter_start(graph, iter_start_id, repo,
5737 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
5739 af61c510 2019-07-19 stsp goto done;
5740 af61c510 2019-07-19 stsp while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
5741 ee780d5c 2020-01-04 stsp err = got_commit_graph_iter_next(&parent_id, graph, repo,
5742 ee780d5c 2020-01-04 stsp check_cancelled, NULL);
5743 af61c510 2019-07-19 stsp if (err) {
5744 af61c510 2019-07-19 stsp if (err->code == GOT_ERR_ITER_COMPLETED) {
5745 af61c510 2019-07-19 stsp err = got_error_msg(GOT_ERR_ANCESTRY,
5746 af61c510 2019-07-19 stsp "ran out of commits to rebase before "
5747 af61c510 2019-07-19 stsp "youngest common ancestor commit has "
5748 af61c510 2019-07-19 stsp "been reached?!?");
5750 ee780d5c 2020-01-04 stsp goto done;
5752 8ca9bd68 2019-07-25 stsp err = check_path_prefix(parent_id, commit_id,
5753 8ca9bd68 2019-07-25 stsp path_prefix, path_prefix_errcode, repo);
5755 af61c510 2019-07-19 stsp goto done;
5757 af61c510 2019-07-19 stsp err = got_object_qid_alloc(&qid, commit_id);
5759 af61c510 2019-07-19 stsp goto done;
5760 af61c510 2019-07-19 stsp SIMPLEQ_INSERT_HEAD(commits, qid, entry);
5761 af61c510 2019-07-19 stsp commit_id = parent_id;
5765 af61c510 2019-07-19 stsp got_commit_graph_close(graph);
5766 af61c510 2019-07-19 stsp return err;
5769 af61c510 2019-07-19 stsp static const struct got_error *
5770 818c7501 2019-07-11 stsp cmd_rebase(int argc, char *argv[])
5772 818c7501 2019-07-11 stsp const struct got_error *error = NULL;
5773 818c7501 2019-07-11 stsp struct got_worktree *worktree = NULL;
5774 818c7501 2019-07-11 stsp struct got_repository *repo = NULL;
5775 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex = NULL;
5776 818c7501 2019-07-11 stsp char *cwd = NULL;
5777 818c7501 2019-07-11 stsp struct got_reference *branch = NULL;
5778 818c7501 2019-07-11 stsp struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
5779 818c7501 2019-07-11 stsp struct got_object_id *commit_id = NULL, *parent_id = NULL;
5780 818c7501 2019-07-11 stsp struct got_object_id *resume_commit_id = NULL;
5781 818c7501 2019-07-11 stsp struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
5782 818c7501 2019-07-11 stsp struct got_commit_object *commit = NULL;
5783 818c7501 2019-07-11 stsp int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
5784 7ef62c4e 2020-02-24 stsp int histedit_in_progress = 0;
5785 818c7501 2019-07-11 stsp unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
5786 818c7501 2019-07-11 stsp struct got_object_id_queue commits;
5787 01757395 2019-07-12 stsp struct got_pathlist_head merged_paths;
5788 818c7501 2019-07-11 stsp const struct got_object_id_queue *parent_ids;
5789 818c7501 2019-07-11 stsp struct got_object_qid *qid, *pid;
5791 818c7501 2019-07-11 stsp SIMPLEQ_INIT(&commits);
5792 01757395 2019-07-12 stsp TAILQ_INIT(&merged_paths);
5794 818c7501 2019-07-11 stsp while ((ch = getopt(argc, argv, "ac")) != -1) {
5795 818c7501 2019-07-11 stsp switch (ch) {
5797 818c7501 2019-07-11 stsp abort_rebase = 1;
5800 818c7501 2019-07-11 stsp continue_rebase = 1;
5803 818c7501 2019-07-11 stsp usage_rebase();
5804 818c7501 2019-07-11 stsp /* NOTREACHED */
5808 818c7501 2019-07-11 stsp argc -= optind;
5809 818c7501 2019-07-11 stsp argv += optind;
5811 43012d58 2019-07-14 stsp #ifndef PROFILE
5812 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5813 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
5814 43012d58 2019-07-14 stsp err(1, "pledge");
5816 818c7501 2019-07-11 stsp if (abort_rebase && continue_rebase)
5817 818c7501 2019-07-11 stsp usage_rebase();
5818 818c7501 2019-07-11 stsp else if (abort_rebase || continue_rebase) {
5819 818c7501 2019-07-11 stsp if (argc != 0)
5820 818c7501 2019-07-11 stsp usage_rebase();
5821 818c7501 2019-07-11 stsp } else if (argc != 1)
5822 818c7501 2019-07-11 stsp usage_rebase();
5824 818c7501 2019-07-11 stsp cwd = getcwd(NULL, 0);
5825 818c7501 2019-07-11 stsp if (cwd == NULL) {
5826 818c7501 2019-07-11 stsp error = got_error_from_errno("getcwd");
5827 818c7501 2019-07-11 stsp goto done;
5829 818c7501 2019-07-11 stsp error = got_worktree_open(&worktree, cwd);
5830 818c7501 2019-07-11 stsp if (error)
5831 818c7501 2019-07-11 stsp goto done;
5833 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5835 818c7501 2019-07-11 stsp if (error != NULL)
5836 818c7501 2019-07-11 stsp goto done;
5838 818c7501 2019-07-11 stsp error = apply_unveil(got_repo_get_path(repo), 0,
5839 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
5840 7ef62c4e 2020-02-24 stsp if (error)
5841 7ef62c4e 2020-02-24 stsp goto done;
5843 7ef62c4e 2020-02-24 stsp error = got_worktree_histedit_in_progress(&histedit_in_progress,
5844 7ef62c4e 2020-02-24 stsp worktree);
5845 818c7501 2019-07-11 stsp if (error)
5846 7ef62c4e 2020-02-24 stsp goto done;
5847 7ef62c4e 2020-02-24 stsp if (histedit_in_progress) {
5848 7ef62c4e 2020-02-24 stsp error = got_error(GOT_ERR_HISTEDIT_BUSY);
5849 818c7501 2019-07-11 stsp goto done;
5852 818c7501 2019-07-11 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
5853 818c7501 2019-07-11 stsp if (error)
5854 818c7501 2019-07-11 stsp goto done;
5856 f6794adc 2019-07-23 stsp if (abort_rebase) {
5857 818c7501 2019-07-11 stsp int did_something;
5858 f6794adc 2019-07-23 stsp if (!rebase_in_progress) {
5859 f6794adc 2019-07-23 stsp error = got_error(GOT_ERR_NOT_REBASING);
5860 f6794adc 2019-07-23 stsp goto done;
5862 818c7501 2019-07-11 stsp error = got_worktree_rebase_continue(&resume_commit_id,
5863 3e3a69f1 2019-07-25 stsp &new_base_branch, &tmp_branch, &branch, &fileindex,
5864 3e3a69f1 2019-07-25 stsp worktree, repo);
5865 818c7501 2019-07-11 stsp if (error)
5866 818c7501 2019-07-11 stsp goto done;
5867 818c7501 2019-07-11 stsp printf("Switching work tree to %s\n",
5868 818c7501 2019-07-11 stsp got_ref_get_symref_target(new_base_branch));
5869 3e3a69f1 2019-07-25 stsp error = got_worktree_rebase_abort(worktree, fileindex, repo,
5870 818c7501 2019-07-11 stsp new_base_branch, update_progress, &did_something);
5871 818c7501 2019-07-11 stsp if (error)
5872 818c7501 2019-07-11 stsp goto done;
5873 818c7501 2019-07-11 stsp printf("Rebase of %s aborted\n", got_ref_get_name(branch));
5874 818c7501 2019-07-11 stsp goto done; /* nothing else to do */
5877 818c7501 2019-07-11 stsp if (continue_rebase) {
5878 f6794adc 2019-07-23 stsp if (!rebase_in_progress) {
5879 f6794adc 2019-07-23 stsp error = got_error(GOT_ERR_NOT_REBASING);
5880 f6794adc 2019-07-23 stsp goto done;
5882 818c7501 2019-07-11 stsp error = got_worktree_rebase_continue(&resume_commit_id,
5883 3e3a69f1 2019-07-25 stsp &new_base_branch, &tmp_branch, &branch, &fileindex,
5884 3e3a69f1 2019-07-25 stsp worktree, repo);
5885 818c7501 2019-07-11 stsp if (error)
5886 818c7501 2019-07-11 stsp goto done;
5888 3e3a69f1 2019-07-25 stsp error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
5889 01757395 2019-07-12 stsp resume_commit_id, repo);
5890 818c7501 2019-07-11 stsp if (error)
5891 818c7501 2019-07-11 stsp goto done;
5893 ff0d2220 2019-07-11 stsp yca_id = got_object_id_dup(resume_commit_id);
5894 ff0d2220 2019-07-11 stsp if (yca_id == NULL) {
5895 818c7501 2019-07-11 stsp error = got_error_from_errno("got_object_id_dup");
5896 818c7501 2019-07-11 stsp goto done;
5899 818c7501 2019-07-11 stsp error = got_ref_open(&branch, repo, argv[0], 0);
5900 818c7501 2019-07-11 stsp if (error != NULL)
5901 818c7501 2019-07-11 stsp goto done;
5904 ff0d2220 2019-07-11 stsp error = got_ref_resolve(&branch_head_commit_id, repo, branch);
5905 ff0d2220 2019-07-11 stsp if (error)
5906 ff0d2220 2019-07-11 stsp goto done;
5908 ff0d2220 2019-07-11 stsp if (!continue_rebase) {
5909 a51a74b3 2019-07-27 stsp struct got_object_id *base_commit_id;
5911 a51a74b3 2019-07-27 stsp base_commit_id = got_worktree_get_base_commit_id(worktree);
5912 818c7501 2019-07-11 stsp error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
5913 6fb7cd11 2019-08-22 stsp base_commit_id, branch_head_commit_id, repo,
5914 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
5915 818c7501 2019-07-11 stsp if (error)
5916 818c7501 2019-07-11 stsp goto done;
5917 818c7501 2019-07-11 stsp if (yca_id == NULL) {
5918 818c7501 2019-07-11 stsp error = got_error_msg(GOT_ERR_ANCESTRY,
5919 818c7501 2019-07-11 stsp "specified branch shares no common ancestry "
5920 818c7501 2019-07-11 stsp "with work tree's branch");
5921 818c7501 2019-07-11 stsp goto done;
5924 a51a74b3 2019-07-27 stsp error = check_same_branch(base_commit_id, branch, yca_id, repo);
5925 a51a74b3 2019-07-27 stsp if (error) {
5926 a51a74b3 2019-07-27 stsp if (error->code != GOT_ERR_ANCESTRY)
5927 a51a74b3 2019-07-27 stsp goto done;
5928 a51a74b3 2019-07-27 stsp error = NULL;
5930 a51a74b3 2019-07-27 stsp error = got_error_msg(GOT_ERR_SAME_BRANCH,
5931 a51a74b3 2019-07-27 stsp "specified branch resolves to a commit which "
5932 a51a74b3 2019-07-27 stsp "is already contained in work tree's branch");
5933 a51a74b3 2019-07-27 stsp goto done;
5935 818c7501 2019-07-11 stsp error = got_worktree_rebase_prepare(&new_base_branch,
5936 3e3a69f1 2019-07-25 stsp &tmp_branch, &fileindex, worktree, branch, repo);
5937 818c7501 2019-07-11 stsp if (error)
5938 818c7501 2019-07-11 stsp goto done;
5941 ff0d2220 2019-07-11 stsp commit_id = branch_head_commit_id;
5942 818c7501 2019-07-11 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
5943 818c7501 2019-07-11 stsp if (error)
5944 818c7501 2019-07-11 stsp goto done;
5946 818c7501 2019-07-11 stsp parent_ids = got_object_commit_get_parent_ids(commit);
5947 818c7501 2019-07-11 stsp pid = SIMPLEQ_FIRST(parent_ids);
5948 fc66b545 2019-08-12 stsp if (pid == NULL) {
5949 fc66b545 2019-08-12 stsp if (!continue_rebase) {
5950 fc66b545 2019-08-12 stsp int did_something;
5951 fc66b545 2019-08-12 stsp error = got_worktree_rebase_abort(worktree, fileindex,
5952 fc66b545 2019-08-12 stsp repo, new_base_branch, update_progress,
5953 fc66b545 2019-08-12 stsp &did_something);
5954 fc66b545 2019-08-12 stsp if (error)
5955 fc66b545 2019-08-12 stsp goto done;
5956 fc66b545 2019-08-12 stsp printf("Rebase of %s aborted\n",
5957 fc66b545 2019-08-12 stsp got_ref_get_name(branch));
5959 fc66b545 2019-08-12 stsp error = got_error(GOT_ERR_EMPTY_REBASE);
5960 fc66b545 2019-08-12 stsp goto done;
5962 8ca9bd68 2019-07-25 stsp error = collect_commits(&commits, commit_id, pid->id,
5963 8ca9bd68 2019-07-25 stsp yca_id, got_worktree_get_path_prefix(worktree),
5964 8ca9bd68 2019-07-25 stsp GOT_ERR_REBASE_PATH, repo);
5965 818c7501 2019-07-11 stsp got_object_commit_close(commit);
5966 818c7501 2019-07-11 stsp commit = NULL;
5967 818c7501 2019-07-11 stsp if (error)
5968 818c7501 2019-07-11 stsp goto done;
5970 818c7501 2019-07-11 stsp if (SIMPLEQ_EMPTY(&commits)) {
5971 38b0338b 2019-11-29 stsp if (continue_rebase) {
5972 3e3a69f1 2019-07-25 stsp error = rebase_complete(worktree, fileindex,
5973 3e3a69f1 2019-07-25 stsp branch, new_base_branch, tmp_branch, repo);
5974 38b0338b 2019-11-29 stsp goto done;
5976 38b0338b 2019-11-29 stsp /* Fast-forward the reference of the branch. */
5977 38b0338b 2019-11-29 stsp struct got_object_id *new_head_commit_id;
5978 38b0338b 2019-11-29 stsp char *id_str;
5979 38b0338b 2019-11-29 stsp error = got_ref_resolve(&new_head_commit_id, repo,
5980 38b0338b 2019-11-29 stsp new_base_branch);
5981 38b0338b 2019-11-29 stsp if (error)
5982 38b0338b 2019-11-29 stsp goto done;
5983 38b0338b 2019-11-29 stsp error = got_object_id_str(&id_str, new_head_commit_id);
5984 38b0338b 2019-11-29 stsp printf("Forwarding %s to commit %s\n",
5985 38b0338b 2019-11-29 stsp got_ref_get_name(branch), id_str);
5986 38b0338b 2019-11-29 stsp free(id_str);
5987 38b0338b 2019-11-29 stsp error = got_ref_change_ref(branch,
5988 38b0338b 2019-11-29 stsp new_head_commit_id);
5989 38b0338b 2019-11-29 stsp if (error)
5990 38b0338b 2019-11-29 stsp goto done;
5994 818c7501 2019-07-11 stsp pid = NULL;
5995 818c7501 2019-07-11 stsp SIMPLEQ_FOREACH(qid, &commits, entry) {
5996 818c7501 2019-07-11 stsp commit_id = qid->id;
5997 818c7501 2019-07-11 stsp parent_id = pid ? pid->id : yca_id;
5998 818c7501 2019-07-11 stsp pid = qid;
6000 01757395 2019-07-12 stsp error = got_worktree_rebase_merge_files(&merged_paths,
6001 3e3a69f1 2019-07-25 stsp worktree, fileindex, parent_id, commit_id, repo,
6002 3e3a69f1 2019-07-25 stsp rebase_progress, &rebase_status, check_cancelled, NULL);
6003 818c7501 2019-07-11 stsp if (error)
6004 818c7501 2019-07-11 stsp goto done;
6006 01757395 2019-07-12 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
6007 a0ea4fc0 2020-02-28 stsp error = show_rebase_merge_conflict(qid->id, repo);
6008 a0ea4fc0 2020-02-28 stsp if (error)
6009 a0ea4fc0 2020-02-28 stsp goto done;
6010 01757395 2019-07-12 stsp got_worktree_rebase_pathlist_free(&merged_paths);
6014 3e3a69f1 2019-07-25 stsp error = rebase_commit(&merged_paths, worktree, fileindex,
6015 3e3a69f1 2019-07-25 stsp tmp_branch, commit_id, repo);
6016 01757395 2019-07-12 stsp got_worktree_rebase_pathlist_free(&merged_paths);
6017 818c7501 2019-07-11 stsp if (error)
6018 818c7501 2019-07-11 stsp goto done;
6021 818c7501 2019-07-11 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
6022 3e3a69f1 2019-07-25 stsp error = got_worktree_rebase_postpone(worktree, fileindex);
6023 818c7501 2019-07-11 stsp if (error)
6024 818c7501 2019-07-11 stsp goto done;
6025 818c7501 2019-07-11 stsp error = got_error_msg(GOT_ERR_CONFLICTS,
6026 11495e04 2019-07-12 stsp "conflicts must be resolved before rebasing can continue");
6028 3e3a69f1 2019-07-25 stsp error = rebase_complete(worktree, fileindex, branch,
6029 3e3a69f1 2019-07-25 stsp new_base_branch, tmp_branch, repo);
6031 818c7501 2019-07-11 stsp got_object_id_queue_free(&commits);
6032 818c7501 2019-07-11 stsp free(branch_head_commit_id);
6033 818c7501 2019-07-11 stsp free(resume_commit_id);
6034 818c7501 2019-07-11 stsp free(yca_id);
6035 818c7501 2019-07-11 stsp if (commit)
6036 818c7501 2019-07-11 stsp got_object_commit_close(commit);
6037 818c7501 2019-07-11 stsp if (branch)
6038 818c7501 2019-07-11 stsp got_ref_close(branch);
6039 818c7501 2019-07-11 stsp if (new_base_branch)
6040 818c7501 2019-07-11 stsp got_ref_close(new_base_branch);
6041 818c7501 2019-07-11 stsp if (tmp_branch)
6042 818c7501 2019-07-11 stsp got_ref_close(tmp_branch);
6043 5ef14e63 2019-06-02 stsp if (worktree)
6044 5ef14e63 2019-06-02 stsp got_worktree_close(worktree);
6046 5ef14e63 2019-06-02 stsp got_repo_close(repo);
6047 5ef14e63 2019-06-02 stsp return error;
6050 0ebf8283 2019-07-24 stsp __dead static void
6051 0ebf8283 2019-07-24 stsp usage_histedit(void)
6053 083957f4 2020-02-24 stsp fprintf(stderr, "usage: %s histedit [-a] [-c] [-F histedit-script] [-m]\n",
6054 0ebf8283 2019-07-24 stsp getprogname());
6058 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_PICK 'p'
6059 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_EDIT 'e'
6060 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_FOLD 'f'
6061 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_DROP 'd'
6062 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_MESG 'm'
6064 0ebf8283 2019-07-24 stsp static struct got_histedit_cmd {
6065 0ebf8283 2019-07-24 stsp unsigned char code;
6066 0ebf8283 2019-07-24 stsp const char *name;
6067 0ebf8283 2019-07-24 stsp const char *desc;
6068 0ebf8283 2019-07-24 stsp } got_histedit_cmds[] = {
6069 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_PICK, "pick", "use commit" },
6070 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
6071 82997472 2020-01-29 stsp { GOT_HISTEDIT_FOLD, "fold", "combine with next commit that will "
6072 82997472 2020-01-29 stsp "be used" },
6073 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
6074 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_MESG, "mesg",
6075 0ebf8283 2019-07-24 stsp "single-line log message for commit above (open editor if empty)" },
6078 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry {
6079 0ebf8283 2019-07-24 stsp TAILQ_ENTRY(got_histedit_list_entry) entry;
6080 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id;
6081 0ebf8283 2019-07-24 stsp const struct got_histedit_cmd *cmd;
6082 0ebf8283 2019-07-24 stsp char *logmsg;
6084 0ebf8283 2019-07-24 stsp TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
6086 0ebf8283 2019-07-24 stsp static const struct got_error *
6087 0ebf8283 2019-07-24 stsp histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
6088 0ebf8283 2019-07-24 stsp FILE *f, struct got_repository *repo)
6090 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
6091 0ebf8283 2019-07-24 stsp char *logmsg = NULL, *id_str = NULL;
6092 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
6095 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
6097 0ebf8283 2019-07-24 stsp goto done;
6099 0ebf8283 2019-07-24 stsp err = get_short_logmsg(&logmsg, 34, commit);
6101 0ebf8283 2019-07-24 stsp goto done;
6103 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, commit_id);
6105 0ebf8283 2019-07-24 stsp goto done;
6107 0ebf8283 2019-07-24 stsp n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
6108 0ebf8283 2019-07-24 stsp if (n < 0)
6109 0ebf8283 2019-07-24 stsp err = got_ferror(f, GOT_ERR_IO);
6111 0ebf8283 2019-07-24 stsp if (commit)
6112 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
6113 0ebf8283 2019-07-24 stsp free(id_str);
6114 0ebf8283 2019-07-24 stsp free(logmsg);
6115 0ebf8283 2019-07-24 stsp return err;
6118 0ebf8283 2019-07-24 stsp static const struct got_error *
6119 083957f4 2020-02-24 stsp histedit_write_commit_list(struct got_object_id_queue *commits,
6120 083957f4 2020-02-24 stsp FILE *f, int edit_logmsg_only, struct got_repository *repo)
6122 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
6123 0ebf8283 2019-07-24 stsp struct got_object_qid *qid;
6125 0ebf8283 2019-07-24 stsp if (SIMPLEQ_EMPTY(commits))
6126 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_EMPTY_HISTEDIT);
6128 0ebf8283 2019-07-24 stsp SIMPLEQ_FOREACH(qid, commits, entry) {
6129 0ebf8283 2019-07-24 stsp err = histedit_write_commit(qid->id, got_histedit_cmds[0].name,
6133 083957f4 2020-02-24 stsp if (edit_logmsg_only) {
6134 083957f4 2020-02-24 stsp int n = fprintf(f, "%c\n", GOT_HISTEDIT_MESG);
6135 083957f4 2020-02-24 stsp if (n < 0) {
6136 083957f4 2020-02-24 stsp err = got_ferror(f, GOT_ERR_IO);
6142 0ebf8283 2019-07-24 stsp return err;
6145 0ebf8283 2019-07-24 stsp static const struct got_error *
6146 514f2ffe 2020-01-29 stsp write_cmd_list(FILE *f, const char *branch_name,
6147 514f2ffe 2020-01-29 stsp struct got_object_id_queue *commits)
6149 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
6151 514f2ffe 2020-01-29 stsp char *id_str;
6152 514f2ffe 2020-01-29 stsp struct got_object_qid *qid;
6154 514f2ffe 2020-01-29 stsp qid = SIMPLEQ_FIRST(commits);
6155 514f2ffe 2020-01-29 stsp err = got_object_id_str(&id_str, qid->id);
6157 514f2ffe 2020-01-29 stsp return err;
6159 514f2ffe 2020-01-29 stsp n = fprintf(f,
6160 514f2ffe 2020-01-29 stsp "# Editing the history of branch '%s' starting at\n"
6161 514f2ffe 2020-01-29 stsp "# commit %s\n"
6162 514f2ffe 2020-01-29 stsp "# Commits will be processed in order from top to "
6163 514f2ffe 2020-01-29 stsp "bottom of this file.\n", branch_name, id_str);
6164 514f2ffe 2020-01-29 stsp if (n < 0) {
6165 514f2ffe 2020-01-29 stsp err = got_ferror(f, GOT_ERR_IO);
6166 514f2ffe 2020-01-29 stsp goto done;
6169 0ebf8283 2019-07-24 stsp n = fprintf(f, "# Available histedit commands:\n");
6170 514f2ffe 2020-01-29 stsp if (n < 0) {
6171 514f2ffe 2020-01-29 stsp err = got_ferror(f, GOT_ERR_IO);
6172 514f2ffe 2020-01-29 stsp goto done;
6175 0ebf8283 2019-07-24 stsp for (i = 0; i < nitems(got_histedit_cmds); i++) {
6176 0ebf8283 2019-07-24 stsp struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
6177 0ebf8283 2019-07-24 stsp n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
6178 0ebf8283 2019-07-24 stsp cmd->desc);
6179 0ebf8283 2019-07-24 stsp if (n < 0) {
6180 0ebf8283 2019-07-24 stsp err = got_ferror(f, GOT_ERR_IO);
6185 514f2ffe 2020-01-29 stsp free(id_str);
6186 0ebf8283 2019-07-24 stsp return err;
6189 0ebf8283 2019-07-24 stsp static const struct got_error *
6190 0ebf8283 2019-07-24 stsp histedit_syntax_error(int lineno)
6192 0ebf8283 2019-07-24 stsp static char msg[42];
6195 0ebf8283 2019-07-24 stsp ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
6197 0ebf8283 2019-07-24 stsp if (ret == -1 || ret >= sizeof(msg))
6198 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_HISTEDIT_SYNTAX);
6200 0ebf8283 2019-07-24 stsp return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
6203 0ebf8283 2019-07-24 stsp static const struct got_error *
6204 0ebf8283 2019-07-24 stsp append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
6205 0ebf8283 2019-07-24 stsp char *logmsg, struct got_repository *repo)
6207 0ebf8283 2019-07-24 stsp const struct got_error *err;
6208 0ebf8283 2019-07-24 stsp struct got_commit_object *folded_commit = NULL;
6209 5943eee2 2019-08-13 stsp char *id_str, *folded_logmsg = NULL;
6211 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, hle->commit_id);
6213 0ebf8283 2019-07-24 stsp return err;
6215 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
6217 0ebf8283 2019-07-24 stsp goto done;
6219 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
6221 5943eee2 2019-08-13 stsp goto done;
6222 0ebf8283 2019-07-24 stsp if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
6223 0ebf8283 2019-07-24 stsp logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
6224 5943eee2 2019-08-13 stsp folded_logmsg) == -1) {
6225 0ebf8283 2019-07-24 stsp err = got_error_from_errno("asprintf");
6228 0ebf8283 2019-07-24 stsp if (folded_commit)
6229 0ebf8283 2019-07-24 stsp got_object_commit_close(folded_commit);
6230 0ebf8283 2019-07-24 stsp free(id_str);
6231 5943eee2 2019-08-13 stsp free(folded_logmsg);
6232 0ebf8283 2019-07-24 stsp return err;
6235 0ebf8283 2019-07-24 stsp static struct got_histedit_list_entry *
6236 0ebf8283 2019-07-24 stsp get_folded_commits(struct got_histedit_list_entry *hle)
6238 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *prev, *folded = NULL;
6240 0ebf8283 2019-07-24 stsp prev = TAILQ_PREV(hle, got_histedit_list, entry);
6241 3f9de99f 2019-07-24 stsp while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
6242 3f9de99f 2019-07-24 stsp prev->cmd->code == GOT_HISTEDIT_DROP)) {
6243 3f9de99f 2019-07-24 stsp if (prev->cmd->code == GOT_HISTEDIT_FOLD)
6244 3f9de99f 2019-07-24 stsp folded = prev;
6245 0ebf8283 2019-07-24 stsp prev = TAILQ_PREV(prev, got_histedit_list, entry);
6248 0ebf8283 2019-07-24 stsp return folded;
6251 0ebf8283 2019-07-24 stsp static const struct got_error *
6252 0ebf8283 2019-07-24 stsp histedit_edit_logmsg(struct got_histedit_list_entry *hle,
6253 0ebf8283 2019-07-24 stsp struct got_repository *repo)
6255 5943eee2 2019-08-13 stsp char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
6256 0ebf8283 2019-07-24 stsp char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
6257 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
6258 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
6260 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *folded = NULL;
6262 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&commit, repo, hle->commit_id);
6264 0ebf8283 2019-07-24 stsp return err;
6266 0ebf8283 2019-07-24 stsp folded = get_folded_commits(hle);
6267 0ebf8283 2019-07-24 stsp if (folded) {
6268 0ebf8283 2019-07-24 stsp while (folded != hle) {
6269 3f9de99f 2019-07-24 stsp if (folded->cmd->code == GOT_HISTEDIT_DROP) {
6270 3f9de99f 2019-07-24 stsp folded = TAILQ_NEXT(folded, entry);
6273 0ebf8283 2019-07-24 stsp err = append_folded_commit_msg(&new_msg, folded,
6274 0ebf8283 2019-07-24 stsp logmsg, repo);
6276 0ebf8283 2019-07-24 stsp goto done;
6277 0ebf8283 2019-07-24 stsp free(logmsg);
6278 0ebf8283 2019-07-24 stsp logmsg = new_msg;
6279 0ebf8283 2019-07-24 stsp folded = TAILQ_NEXT(folded, entry);
6283 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, hle->commit_id);
6285 0ebf8283 2019-07-24 stsp goto done;
6286 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&orig_logmsg, commit);
6288 5943eee2 2019-08-13 stsp goto done;
6289 0ebf8283 2019-07-24 stsp if (asprintf(&new_msg,
6290 0ebf8283 2019-07-24 stsp "%s\n# original log message of commit %s: %s",
6291 5943eee2 2019-08-13 stsp logmsg ? logmsg : "", id_str, orig_logmsg) == -1) {
6292 0ebf8283 2019-07-24 stsp err = got_error_from_errno("asprintf");
6293 0ebf8283 2019-07-24 stsp goto done;
6295 0ebf8283 2019-07-24 stsp free(logmsg);
6296 0ebf8283 2019-07-24 stsp logmsg = new_msg;
6298 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, hle->commit_id);
6300 0ebf8283 2019-07-24 stsp goto done;
6302 bb63914a 2020-02-17 stsp err = got_opentemp_named_fd(&logmsg_path, &fd,
6303 bb63914a 2020-02-17 stsp GOT_TMPDIR_STR "/got-logmsg");
6305 0ebf8283 2019-07-24 stsp goto done;
6307 0ebf8283 2019-07-24 stsp dprintf(fd, logmsg);
6308 0ebf8283 2019-07-24 stsp close(fd);
6310 0ebf8283 2019-07-24 stsp err = get_editor(&editor);
6312 0ebf8283 2019-07-24 stsp goto done;
6314 0ebf8283 2019-07-24 stsp err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg);
6315 0ebf8283 2019-07-24 stsp if (err) {
6316 0ebf8283 2019-07-24 stsp if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
6317 0ebf8283 2019-07-24 stsp goto done;
6318 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&hle->logmsg, commit);
6321 0ebf8283 2019-07-24 stsp if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
6322 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("unlink", logmsg_path);
6323 0ebf8283 2019-07-24 stsp free(logmsg_path);
6324 0ebf8283 2019-07-24 stsp free(logmsg);
6325 5943eee2 2019-08-13 stsp free(orig_logmsg);
6326 0ebf8283 2019-07-24 stsp free(editor);
6327 0ebf8283 2019-07-24 stsp if (commit)
6328 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
6329 0ebf8283 2019-07-24 stsp return err;
6332 0ebf8283 2019-07-24 stsp static const struct got_error *
6333 0ebf8283 2019-07-24 stsp histedit_parse_list(struct got_histedit_list *histedit_cmds,
6334 0ebf8283 2019-07-24 stsp FILE *f, struct got_repository *repo)
6336 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
6337 0ebf8283 2019-07-24 stsp char *line = NULL, *p, *end;
6338 0ebf8283 2019-07-24 stsp size_t size;
6339 0ebf8283 2019-07-24 stsp ssize_t len;
6340 0ebf8283 2019-07-24 stsp int lineno = 0, i;
6341 0ebf8283 2019-07-24 stsp const struct got_histedit_cmd *cmd;
6342 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id = NULL;
6343 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle = NULL;
6345 0ebf8283 2019-07-24 stsp for (;;) {
6346 0ebf8283 2019-07-24 stsp len = getline(&line, &size, f);
6347 0ebf8283 2019-07-24 stsp if (len == -1) {
6348 0ebf8283 2019-07-24 stsp const struct got_error *getline_err;
6349 0ebf8283 2019-07-24 stsp if (feof(f))
6351 0ebf8283 2019-07-24 stsp getline_err = got_error_from_errno("getline");
6352 0ebf8283 2019-07-24 stsp err = got_ferror(f, getline_err->code);
6357 0ebf8283 2019-07-24 stsp while (isspace((unsigned char)p[0]))
6359 0ebf8283 2019-07-24 stsp if (p[0] == '#' || p[0] == '\0') {
6360 0ebf8283 2019-07-24 stsp free(line);
6361 0ebf8283 2019-07-24 stsp line = NULL;
6364 0ebf8283 2019-07-24 stsp cmd = NULL;
6365 0ebf8283 2019-07-24 stsp for (i = 0; i < nitems(got_histedit_cmds); i++) {
6366 0ebf8283 2019-07-24 stsp cmd = &got_histedit_cmds[i];
6367 0ebf8283 2019-07-24 stsp if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
6368 0ebf8283 2019-07-24 stsp isspace((unsigned char)p[strlen(cmd->name)])) {
6369 0ebf8283 2019-07-24 stsp p += strlen(cmd->name);
6372 0ebf8283 2019-07-24 stsp if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
6377 6c1844f6 2019-07-25 stsp if (i == nitems(got_histedit_cmds)) {
6378 0ebf8283 2019-07-24 stsp err = histedit_syntax_error(lineno);
6381 0ebf8283 2019-07-24 stsp while (isspace((unsigned char)p[0]))
6383 0ebf8283 2019-07-24 stsp if (cmd->code == GOT_HISTEDIT_MESG) {
6384 0ebf8283 2019-07-24 stsp if (hle == NULL || hle->logmsg != NULL) {
6385 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_HISTEDIT_CMD);
6388 0ebf8283 2019-07-24 stsp if (p[0] == '\0') {
6389 0ebf8283 2019-07-24 stsp err = histedit_edit_logmsg(hle, repo);
6393 0ebf8283 2019-07-24 stsp hle->logmsg = strdup(p);
6394 0ebf8283 2019-07-24 stsp if (hle->logmsg == NULL) {
6395 0ebf8283 2019-07-24 stsp err = got_error_from_errno("strdup");
6399 0ebf8283 2019-07-24 stsp free(line);
6400 0ebf8283 2019-07-24 stsp line = NULL;
6404 0ebf8283 2019-07-24 stsp while (end[0] && !isspace((unsigned char)end[0]))
6406 0ebf8283 2019-07-24 stsp *end = '\0';
6408 0ebf8283 2019-07-24 stsp err = got_object_resolve_id_str(&commit_id, repo, p);
6409 0ebf8283 2019-07-24 stsp if (err) {
6410 0ebf8283 2019-07-24 stsp /* override error code */
6411 0ebf8283 2019-07-24 stsp err = histedit_syntax_error(lineno);
6415 0ebf8283 2019-07-24 stsp hle = malloc(sizeof(*hle));
6416 0ebf8283 2019-07-24 stsp if (hle == NULL) {
6417 0ebf8283 2019-07-24 stsp err = got_error_from_errno("malloc");
6420 0ebf8283 2019-07-24 stsp hle->cmd = cmd;
6421 0ebf8283 2019-07-24 stsp hle->commit_id = commit_id;
6422 0ebf8283 2019-07-24 stsp hle->logmsg = NULL;
6423 0ebf8283 2019-07-24 stsp commit_id = NULL;
6424 0ebf8283 2019-07-24 stsp free(line);
6425 0ebf8283 2019-07-24 stsp line = NULL;
6426 0ebf8283 2019-07-24 stsp TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
6429 0ebf8283 2019-07-24 stsp free(line);
6430 0ebf8283 2019-07-24 stsp free(commit_id);
6431 0ebf8283 2019-07-24 stsp return err;
6434 0ebf8283 2019-07-24 stsp static const struct got_error *
6435 bfce7f83 2019-07-27 stsp histedit_check_script(struct got_histedit_list *histedit_cmds,
6436 bfce7f83 2019-07-27 stsp struct got_object_id_queue *commits, struct got_repository *repo)
6438 bfce7f83 2019-07-27 stsp const struct got_error *err = NULL;
6439 bfce7f83 2019-07-27 stsp struct got_object_qid *qid;
6440 bfce7f83 2019-07-27 stsp struct got_histedit_list_entry *hle;
6441 5b87815e 2020-03-05 stsp static char msg[92];
6442 bfce7f83 2019-07-27 stsp char *id_str;
6444 bfce7f83 2019-07-27 stsp if (TAILQ_EMPTY(histedit_cmds))
6445 bfce7f83 2019-07-27 stsp return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
6446 bfce7f83 2019-07-27 stsp "histedit script contains no commands");
6447 a0de39f3 2019-08-09 stsp if (SIMPLEQ_EMPTY(commits))
6448 a0de39f3 2019-08-09 stsp return got_error(GOT_ERR_EMPTY_HISTEDIT);
6450 5b87815e 2020-03-05 stsp TAILQ_FOREACH(hle, histedit_cmds, entry) {
6451 5b87815e 2020-03-05 stsp struct got_histedit_list_entry *hle2;
6452 5b87815e 2020-03-05 stsp TAILQ_FOREACH(hle2, histedit_cmds, entry) {
6453 5b87815e 2020-03-05 stsp if (hle == hle2)
6455 5b87815e 2020-03-05 stsp if (got_object_id_cmp(hle->commit_id,
6456 5b87815e 2020-03-05 stsp hle2->commit_id) != 0)
6458 5b87815e 2020-03-05 stsp err = got_object_id_str(&id_str, hle->commit_id);
6460 5b87815e 2020-03-05 stsp return err;
6461 5b87815e 2020-03-05 stsp snprintf(msg, sizeof(msg), "commit %s is listed "
6462 5b87815e 2020-03-05 stsp "more than once in histedit script", id_str);
6463 5b87815e 2020-03-05 stsp free(id_str);
6464 5b87815e 2020-03-05 stsp return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
6468 bfce7f83 2019-07-27 stsp SIMPLEQ_FOREACH(qid, commits, entry) {
6469 bfce7f83 2019-07-27 stsp TAILQ_FOREACH(hle, histedit_cmds, entry) {
6470 bfce7f83 2019-07-27 stsp if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
6473 bfce7f83 2019-07-27 stsp if (hle == NULL) {
6474 bfce7f83 2019-07-27 stsp err = got_object_id_str(&id_str, qid->id);
6476 bfce7f83 2019-07-27 stsp return err;
6477 bfce7f83 2019-07-27 stsp snprintf(msg, sizeof(msg),
6478 bfce7f83 2019-07-27 stsp "commit %s missing from histedit script", id_str);
6479 bfce7f83 2019-07-27 stsp free(id_str);
6480 bfce7f83 2019-07-27 stsp return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
6484 0def28b1 2019-08-17 stsp hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
6485 0def28b1 2019-08-17 stsp if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
6486 bfce7f83 2019-07-27 stsp return got_error_msg(GOT_ERR_HISTEDIT_CMD,
6487 bfce7f83 2019-07-27 stsp "last commit in histedit script cannot be folded");
6489 bfce7f83 2019-07-27 stsp return NULL;
6492 bfce7f83 2019-07-27 stsp static const struct got_error *
6493 0ebf8283 2019-07-24 stsp histedit_run_editor(struct got_histedit_list *histedit_cmds,
6494 bfce7f83 2019-07-27 stsp const char *path, struct got_object_id_queue *commits,
6495 bfce7f83 2019-07-27 stsp struct got_repository *repo)
6497 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
6498 0ebf8283 2019-07-24 stsp char *editor;
6499 0ebf8283 2019-07-24 stsp FILE *f = NULL;
6501 0ebf8283 2019-07-24 stsp err = get_editor(&editor);
6503 0ebf8283 2019-07-24 stsp return err;
6505 0ebf8283 2019-07-24 stsp if (spawn_editor(editor, path) == -1) {
6506 0ebf8283 2019-07-24 stsp err = got_error_from_errno("failed spawning editor");
6507 0ebf8283 2019-07-24 stsp goto done;
6510 0ebf8283 2019-07-24 stsp f = fopen(path, "r");
6511 0ebf8283 2019-07-24 stsp if (f == NULL) {
6512 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fopen");
6513 0ebf8283 2019-07-24 stsp goto done;
6515 0ebf8283 2019-07-24 stsp err = histedit_parse_list(histedit_cmds, f, repo);
6517 bfce7f83 2019-07-27 stsp goto done;
6519 bfce7f83 2019-07-27 stsp err = histedit_check_script(histedit_cmds, commits, repo);
6521 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
6522 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
6523 0ebf8283 2019-07-24 stsp free(editor);
6524 0ebf8283 2019-07-24 stsp return err;
6527 0ebf8283 2019-07-24 stsp static const struct got_error *
6528 bfce7f83 2019-07-27 stsp histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
6529 514f2ffe 2020-01-29 stsp struct got_object_id_queue *, const char *, const char *,
6530 514f2ffe 2020-01-29 stsp struct got_repository *);
6532 0ebf8283 2019-07-24 stsp static const struct got_error *
6533 0ebf8283 2019-07-24 stsp histedit_edit_script(struct got_histedit_list *histedit_cmds,
6534 514f2ffe 2020-01-29 stsp struct got_object_id_queue *commits, const char *branch_name,
6535 083957f4 2020-02-24 stsp int edit_logmsg_only, struct got_repository *repo)
6537 0ebf8283 2019-07-24 stsp const struct got_error *err;
6538 0ebf8283 2019-07-24 stsp FILE *f = NULL;
6539 0ebf8283 2019-07-24 stsp char *path = NULL;
6541 0ebf8283 2019-07-24 stsp err = got_opentemp_named(&path, &f, "got-histedit");
6543 0ebf8283 2019-07-24 stsp return err;
6545 514f2ffe 2020-01-29 stsp err = write_cmd_list(f, branch_name, commits);
6547 0ebf8283 2019-07-24 stsp goto done;
6549 083957f4 2020-02-24 stsp err = histedit_write_commit_list(commits, f, edit_logmsg_only, repo);
6551 0ebf8283 2019-07-24 stsp goto done;
6553 083957f4 2020-02-24 stsp if (edit_logmsg_only) {
6554 083957f4 2020-02-24 stsp rewind(f);
6555 083957f4 2020-02-24 stsp err = histedit_parse_list(histedit_cmds, f, repo);
6557 083957f4 2020-02-24 stsp if (fclose(f) != 0) {
6558 083957f4 2020-02-24 stsp err = got_error_from_errno("fclose");
6559 0ebf8283 2019-07-24 stsp goto done;
6562 083957f4 2020-02-24 stsp err = histedit_run_editor(histedit_cmds, path, commits, repo);
6563 083957f4 2020-02-24 stsp if (err) {
6564 083957f4 2020-02-24 stsp if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6565 083957f4 2020-02-24 stsp err->code != GOT_ERR_HISTEDIT_CMD)
6566 083957f4 2020-02-24 stsp goto done;
6567 083957f4 2020-02-24 stsp err = histedit_edit_list_retry(histedit_cmds, err,
6568 083957f4 2020-02-24 stsp commits, path, branch_name, repo);
6572 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
6573 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
6574 0ebf8283 2019-07-24 stsp if (path && unlink(path) != 0 && err == NULL)
6575 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("unlink", path);
6576 0ebf8283 2019-07-24 stsp free(path);
6577 0ebf8283 2019-07-24 stsp return err;
6580 0ebf8283 2019-07-24 stsp static const struct got_error *
6581 0ebf8283 2019-07-24 stsp histedit_save_list(struct got_histedit_list *histedit_cmds,
6582 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
6584 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
6585 0ebf8283 2019-07-24 stsp char *path = NULL;
6586 0ebf8283 2019-07-24 stsp FILE *f = NULL;
6587 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle;
6588 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
6590 c3022ba5 2019-07-27 stsp err = got_worktree_get_histedit_script_path(&path, worktree);
6592 0ebf8283 2019-07-24 stsp return err;
6594 0ebf8283 2019-07-24 stsp f = fopen(path, "w");
6595 0ebf8283 2019-07-24 stsp if (f == NULL) {
6596 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("fopen", path);
6597 0ebf8283 2019-07-24 stsp goto done;
6599 0ebf8283 2019-07-24 stsp TAILQ_FOREACH(hle, histedit_cmds, entry) {
6600 0ebf8283 2019-07-24 stsp err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
6605 0ebf8283 2019-07-24 stsp if (hle->logmsg) {
6606 0ebf8283 2019-07-24 stsp int n = fprintf(f, "%c %s\n",
6607 0ebf8283 2019-07-24 stsp GOT_HISTEDIT_MESG, hle->logmsg);
6608 0ebf8283 2019-07-24 stsp if (n < 0) {
6609 0ebf8283 2019-07-24 stsp err = got_ferror(f, GOT_ERR_IO);
6615 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
6616 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
6617 0ebf8283 2019-07-24 stsp free(path);
6618 0ebf8283 2019-07-24 stsp if (commit)
6619 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
6620 0ebf8283 2019-07-24 stsp return err;
6624 bfce7f83 2019-07-27 stsp histedit_free_list(struct got_histedit_list *histedit_cmds)
6626 bfce7f83 2019-07-27 stsp struct got_histedit_list_entry *hle;
6628 bfce7f83 2019-07-27 stsp while ((hle = TAILQ_FIRST(histedit_cmds))) {
6629 bfce7f83 2019-07-27 stsp TAILQ_REMOVE(histedit_cmds, hle, entry);
6630 bfce7f83 2019-07-27 stsp free(hle);
6634 0ebf8283 2019-07-24 stsp static const struct got_error *
6635 0ebf8283 2019-07-24 stsp histedit_load_list(struct got_histedit_list *histedit_cmds,
6636 0ebf8283 2019-07-24 stsp const char *path, struct got_repository *repo)
6638 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
6639 0ebf8283 2019-07-24 stsp FILE *f = NULL;
6641 0ebf8283 2019-07-24 stsp f = fopen(path, "r");
6642 0ebf8283 2019-07-24 stsp if (f == NULL) {
6643 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("fopen", path);
6644 0ebf8283 2019-07-24 stsp goto done;
6647 0ebf8283 2019-07-24 stsp err = histedit_parse_list(histedit_cmds, f, repo);
6649 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
6650 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
6651 0ebf8283 2019-07-24 stsp return err;
6654 0ebf8283 2019-07-24 stsp static const struct got_error *
6655 0ebf8283 2019-07-24 stsp histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
6656 bfce7f83 2019-07-27 stsp const struct got_error *edit_err, struct got_object_id_queue *commits,
6657 514f2ffe 2020-01-29 stsp const char *path, const char *branch_name, struct got_repository *repo)
6659 bfce7f83 2019-07-27 stsp const struct got_error *err = NULL, *prev_err = edit_err;
6660 0ebf8283 2019-07-24 stsp int resp = ' ';
6662 b006047e 2019-07-25 stsp while (resp != 'c' && resp != 'r' && resp != 'a') {
6663 0ebf8283 2019-07-24 stsp printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
6664 bfce7f83 2019-07-27 stsp "or (a)bort: ", getprogname(), prev_err->msg);
6665 0ebf8283 2019-07-24 stsp resp = getchar();
6666 a61a4414 2019-08-07 stsp if (resp == '\n')
6667 a61a4414 2019-08-07 stsp resp = getchar();
6668 426ebf2e 2019-07-25 stsp if (resp == 'c') {
6669 bfce7f83 2019-07-27 stsp histedit_free_list(histedit_cmds);
6670 bfce7f83 2019-07-27 stsp err = histedit_run_editor(histedit_cmds, path, commits,
6672 426ebf2e 2019-07-25 stsp if (err) {
6673 bfce7f83 2019-07-27 stsp if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6674 bfce7f83 2019-07-27 stsp err->code != GOT_ERR_HISTEDIT_CMD)
6676 bfce7f83 2019-07-27 stsp prev_err = err;
6677 426ebf2e 2019-07-25 stsp resp = ' ';
6681 426ebf2e 2019-07-25 stsp } else if (resp == 'r') {
6682 bfce7f83 2019-07-27 stsp histedit_free_list(histedit_cmds);
6683 0ebf8283 2019-07-24 stsp err = histedit_edit_script(histedit_cmds,
6684 083957f4 2020-02-24 stsp commits, branch_name, 0, repo);
6685 426ebf2e 2019-07-25 stsp if (err) {
6686 bfce7f83 2019-07-27 stsp if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6687 bfce7f83 2019-07-27 stsp err->code != GOT_ERR_HISTEDIT_CMD)
6689 bfce7f83 2019-07-27 stsp prev_err = err;
6690 426ebf2e 2019-07-25 stsp resp = ' ';
6694 426ebf2e 2019-07-25 stsp } else if (resp == 'a') {
6695 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_HISTEDIT_CANCEL);
6698 0ebf8283 2019-07-24 stsp printf("invalid response '%c'\n", resp);
6701 0ebf8283 2019-07-24 stsp return err;
6704 0ebf8283 2019-07-24 stsp static const struct got_error *
6705 0ebf8283 2019-07-24 stsp histedit_complete(struct got_worktree *worktree,
6706 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6707 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_repository *repo)
6709 0ebf8283 2019-07-24 stsp printf("Switching work tree to %s\n",
6710 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch));
6711 3e3a69f1 2019-07-25 stsp return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
6712 3e3a69f1 2019-07-25 stsp branch, repo);
6715 0ebf8283 2019-07-24 stsp static const struct got_error *
6716 0ebf8283 2019-07-24 stsp show_histedit_progress(struct got_commit_object *commit,
6717 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle, struct got_object_id *new_id)
6719 0ebf8283 2019-07-24 stsp const struct got_error *err;
6720 0ebf8283 2019-07-24 stsp char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
6722 0ebf8283 2019-07-24 stsp err = got_object_id_str(&old_id_str, hle->commit_id);
6724 0ebf8283 2019-07-24 stsp goto done;
6726 0ebf8283 2019-07-24 stsp if (new_id) {
6727 0ebf8283 2019-07-24 stsp err = got_object_id_str(&new_id_str, new_id);
6729 0ebf8283 2019-07-24 stsp goto done;
6732 0ebf8283 2019-07-24 stsp old_id_str[12] = '\0';
6733 0ebf8283 2019-07-24 stsp if (new_id_str)
6734 0ebf8283 2019-07-24 stsp new_id_str[12] = '\0';
6736 0ebf8283 2019-07-24 stsp if (hle->logmsg) {
6737 0ebf8283 2019-07-24 stsp logmsg = strdup(hle->logmsg);
6738 0ebf8283 2019-07-24 stsp if (logmsg == NULL) {
6739 0ebf8283 2019-07-24 stsp err = got_error_from_errno("strdup");
6740 0ebf8283 2019-07-24 stsp goto done;
6742 0ebf8283 2019-07-24 stsp trim_logmsg(logmsg, 42);
6744 0ebf8283 2019-07-24 stsp err = get_short_logmsg(&logmsg, 42, commit);
6746 0ebf8283 2019-07-24 stsp goto done;
6749 0ebf8283 2019-07-24 stsp switch (hle->cmd->code) {
6750 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_PICK:
6751 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_EDIT:
6752 0ebf8283 2019-07-24 stsp printf("%s -> %s: %s\n", old_id_str,
6753 0ebf8283 2019-07-24 stsp new_id_str ? new_id_str : "no-op change", logmsg);
6755 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_DROP:
6756 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_FOLD:
6757 0ebf8283 2019-07-24 stsp printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
6764 0ebf8283 2019-07-24 stsp free(old_id_str);
6765 0ebf8283 2019-07-24 stsp free(new_id_str);
6766 0ebf8283 2019-07-24 stsp return err;
6769 0ebf8283 2019-07-24 stsp static const struct got_error *
6770 0ebf8283 2019-07-24 stsp histedit_commit(struct got_pathlist_head *merged_paths,
6771 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
6772 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
6773 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
6775 0ebf8283 2019-07-24 stsp const struct got_error *err;
6776 0ebf8283 2019-07-24 stsp struct got_commit_object *commit;
6777 0ebf8283 2019-07-24 stsp struct got_object_id *new_commit_id;
6779 0ebf8283 2019-07-24 stsp if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
6780 0ebf8283 2019-07-24 stsp && hle->logmsg == NULL) {
6781 0ebf8283 2019-07-24 stsp err = histedit_edit_logmsg(hle, repo);
6783 0ebf8283 2019-07-24 stsp return err;
6786 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&commit, repo, hle->commit_id);
6788 0ebf8283 2019-07-24 stsp return err;
6790 0ebf8283 2019-07-24 stsp err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
6791 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, commit, hle->commit_id,
6792 3e3a69f1 2019-07-25 stsp hle->logmsg, repo);
6793 0ebf8283 2019-07-24 stsp if (err) {
6794 0ebf8283 2019-07-24 stsp if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
6795 0ebf8283 2019-07-24 stsp goto done;
6796 0ebf8283 2019-07-24 stsp err = show_histedit_progress(commit, hle, NULL);
6798 0ebf8283 2019-07-24 stsp err = show_histedit_progress(commit, hle, new_commit_id);
6799 0ebf8283 2019-07-24 stsp free(new_commit_id);
6802 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
6803 0ebf8283 2019-07-24 stsp return err;
6806 0ebf8283 2019-07-24 stsp static const struct got_error *
6807 0ebf8283 2019-07-24 stsp histedit_skip_commit(struct got_histedit_list_entry *hle,
6808 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
6810 0ebf8283 2019-07-24 stsp const struct got_error *error;
6811 0ebf8283 2019-07-24 stsp struct got_commit_object *commit;
6813 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
6815 0ebf8283 2019-07-24 stsp if (error)
6816 0ebf8283 2019-07-24 stsp return error;
6818 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo, hle->commit_id);
6819 0ebf8283 2019-07-24 stsp if (error)
6820 0ebf8283 2019-07-24 stsp return error;
6822 0ebf8283 2019-07-24 stsp error = show_histedit_progress(commit, hle, NULL);
6823 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
6824 0ebf8283 2019-07-24 stsp return error;
6827 ab20a43a 2020-01-29 stsp static const struct got_error *
6828 ab20a43a 2020-01-29 stsp check_local_changes(void *arg, unsigned char status,
6829 ab20a43a 2020-01-29 stsp unsigned char staged_status, const char *path,
6830 ab20a43a 2020-01-29 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
6831 ab20a43a 2020-01-29 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
6833 ab20a43a 2020-01-29 stsp int *have_local_changes = arg;
6835 ab20a43a 2020-01-29 stsp switch (status) {
6836 ab20a43a 2020-01-29 stsp case GOT_STATUS_ADD:
6837 ab20a43a 2020-01-29 stsp case GOT_STATUS_DELETE:
6838 ab20a43a 2020-01-29 stsp case GOT_STATUS_MODIFY:
6839 ab20a43a 2020-01-29 stsp case GOT_STATUS_CONFLICT:
6840 ab20a43a 2020-01-29 stsp *have_local_changes = 1;
6841 ab20a43a 2020-01-29 stsp return got_error(GOT_ERR_CANCELLED);
6846 ab20a43a 2020-01-29 stsp switch (staged_status) {
6847 ab20a43a 2020-01-29 stsp case GOT_STATUS_ADD:
6848 ab20a43a 2020-01-29 stsp case GOT_STATUS_DELETE:
6849 ab20a43a 2020-01-29 stsp case GOT_STATUS_MODIFY:
6850 ab20a43a 2020-01-29 stsp *have_local_changes = 1;
6851 ab20a43a 2020-01-29 stsp return got_error(GOT_ERR_CANCELLED);
6856 ab20a43a 2020-01-29 stsp return NULL;
6859 0ebf8283 2019-07-24 stsp static const struct got_error *
6860 0ebf8283 2019-07-24 stsp cmd_histedit(int argc, char *argv[])
6862 0ebf8283 2019-07-24 stsp const struct got_error *error = NULL;
6863 0ebf8283 2019-07-24 stsp struct got_worktree *worktree = NULL;
6864 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex = NULL;
6865 0ebf8283 2019-07-24 stsp struct got_repository *repo = NULL;
6866 0ebf8283 2019-07-24 stsp char *cwd = NULL;
6867 0ebf8283 2019-07-24 stsp struct got_reference *branch = NULL;
6868 0ebf8283 2019-07-24 stsp struct got_reference *tmp_branch = NULL;
6869 0ebf8283 2019-07-24 stsp struct got_object_id *resume_commit_id = NULL;
6870 0ebf8283 2019-07-24 stsp struct got_object_id *base_commit_id = NULL;
6871 0ebf8283 2019-07-24 stsp struct got_object_id *head_commit_id = NULL;
6872 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
6873 6ba2bea2 2019-07-27 stsp int ch, rebase_in_progress = 0, did_something;
6874 0ebf8283 2019-07-24 stsp int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
6875 083957f4 2020-02-24 stsp int edit_logmsg_only = 0;
6876 0ebf8283 2019-07-24 stsp const char *edit_script_path = NULL;
6877 0ebf8283 2019-07-24 stsp unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
6878 0ebf8283 2019-07-24 stsp struct got_object_id_queue commits;
6879 0ebf8283 2019-07-24 stsp struct got_pathlist_head merged_paths;
6880 0ebf8283 2019-07-24 stsp const struct got_object_id_queue *parent_ids;
6881 0ebf8283 2019-07-24 stsp struct got_object_qid *pid;
6882 0ebf8283 2019-07-24 stsp struct got_histedit_list histedit_cmds;
6883 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle;
6885 0ebf8283 2019-07-24 stsp SIMPLEQ_INIT(&commits);
6886 0ebf8283 2019-07-24 stsp TAILQ_INIT(&histedit_cmds);
6887 0ebf8283 2019-07-24 stsp TAILQ_INIT(&merged_paths);
6889 083957f4 2020-02-24 stsp while ((ch = getopt(argc, argv, "acF:m")) != -1) {
6890 0ebf8283 2019-07-24 stsp switch (ch) {
6892 0ebf8283 2019-07-24 stsp abort_edit = 1;
6895 0ebf8283 2019-07-24 stsp continue_edit = 1;
6898 0ebf8283 2019-07-24 stsp edit_script_path = optarg;
6901 083957f4 2020-02-24 stsp edit_logmsg_only = 1;
6904 0ebf8283 2019-07-24 stsp usage_histedit();
6905 0ebf8283 2019-07-24 stsp /* NOTREACHED */
6909 0ebf8283 2019-07-24 stsp argc -= optind;
6910 0ebf8283 2019-07-24 stsp argv += optind;
6912 0ebf8283 2019-07-24 stsp #ifndef PROFILE
6913 0ebf8283 2019-07-24 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6914 0ebf8283 2019-07-24 stsp "unveil", NULL) == -1)
6915 0ebf8283 2019-07-24 stsp err(1, "pledge");
6917 0ebf8283 2019-07-24 stsp if (abort_edit && continue_edit)
6918 083957f4 2020-02-24 stsp errx(1, "histedit's -a and -c options are mutually exclusive");
6919 083957f4 2020-02-24 stsp if (edit_script_path && edit_logmsg_only)
6920 083957f4 2020-02-24 stsp errx(1, "histedit's -F and -m options are mutually exclusive");
6921 083957f4 2020-02-24 stsp if (abort_edit && edit_logmsg_only)
6922 083957f4 2020-02-24 stsp errx(1, "histedit's -a and -m options are mutually exclusive");
6923 083957f4 2020-02-24 stsp if (continue_edit && edit_logmsg_only)
6924 083957f4 2020-02-24 stsp errx(1, "histedit's -c and -m options are mutually exclusive");
6925 0ebf8283 2019-07-24 stsp if (argc != 0)
6926 0ebf8283 2019-07-24 stsp usage_histedit();
6929 0ebf8283 2019-07-24 stsp * This command cannot apply unveil(2) in all cases because the
6930 0ebf8283 2019-07-24 stsp * user may choose to run an editor to edit the histedit script
6931 0ebf8283 2019-07-24 stsp * and to edit individual commit log messages.
6932 0ebf8283 2019-07-24 stsp * unveil(2) traverses exec(2); if an editor is used we have to
6933 0ebf8283 2019-07-24 stsp * apply unveil after edit script and log messages have been written.
6934 0ebf8283 2019-07-24 stsp * XXX TODO: Make use of unveil(2) where possible.
6937 0ebf8283 2019-07-24 stsp cwd = getcwd(NULL, 0);
6938 0ebf8283 2019-07-24 stsp if (cwd == NULL) {
6939 0ebf8283 2019-07-24 stsp error = got_error_from_errno("getcwd");
6940 0ebf8283 2019-07-24 stsp goto done;
6942 0ebf8283 2019-07-24 stsp error = got_worktree_open(&worktree, cwd);
6943 0ebf8283 2019-07-24 stsp if (error)
6944 0ebf8283 2019-07-24 stsp goto done;
6946 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6948 0ebf8283 2019-07-24 stsp if (error != NULL)
6949 0ebf8283 2019-07-24 stsp goto done;
6951 0ebf8283 2019-07-24 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
6952 0ebf8283 2019-07-24 stsp if (error)
6953 0ebf8283 2019-07-24 stsp goto done;
6954 0ebf8283 2019-07-24 stsp if (rebase_in_progress) {
6955 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_REBASING);
6956 0ebf8283 2019-07-24 stsp goto done;
6959 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
6960 0ebf8283 2019-07-24 stsp if (error)
6961 0ebf8283 2019-07-24 stsp goto done;
6963 083957f4 2020-02-24 stsp if (edit_in_progress && edit_logmsg_only) {
6964 083957f4 2020-02-24 stsp error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
6965 083957f4 2020-02-24 stsp "histedit operation is in progress in this "
6966 083957f4 2020-02-24 stsp "work tree and must be continued or aborted "
6967 083957f4 2020-02-24 stsp "before the -m option can be used");
6968 083957f4 2020-02-24 stsp goto done;
6971 0ebf8283 2019-07-24 stsp if (edit_in_progress && abort_edit) {
6972 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_continue(&resume_commit_id,
6973 3e3a69f1 2019-07-25 stsp &tmp_branch, &branch, &base_commit_id, &fileindex,
6974 3e3a69f1 2019-07-25 stsp worktree, repo);
6975 0ebf8283 2019-07-24 stsp if (error)
6976 0ebf8283 2019-07-24 stsp goto done;
6977 0ebf8283 2019-07-24 stsp printf("Switching work tree to %s\n",
6978 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch));
6979 3e3a69f1 2019-07-25 stsp error = got_worktree_histedit_abort(worktree, fileindex, repo,
6980 0ebf8283 2019-07-24 stsp branch, base_commit_id, update_progress, &did_something);
6981 0ebf8283 2019-07-24 stsp if (error)
6982 0ebf8283 2019-07-24 stsp goto done;
6983 0ebf8283 2019-07-24 stsp printf("Histedit of %s aborted\n",
6984 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch));
6985 0ebf8283 2019-07-24 stsp goto done; /* nothing else to do */
6986 0ebf8283 2019-07-24 stsp } else if (abort_edit) {
6987 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_NOT_HISTEDIT);
6988 0ebf8283 2019-07-24 stsp goto done;
6991 0ebf8283 2019-07-24 stsp if (continue_edit) {
6992 0ebf8283 2019-07-24 stsp char *path;
6994 0ebf8283 2019-07-24 stsp if (!edit_in_progress) {
6995 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_NOT_HISTEDIT);
6996 0ebf8283 2019-07-24 stsp goto done;
6999 c3022ba5 2019-07-27 stsp error = got_worktree_get_histedit_script_path(&path, worktree);
7000 0ebf8283 2019-07-24 stsp if (error)
7001 0ebf8283 2019-07-24 stsp goto done;
7003 0ebf8283 2019-07-24 stsp error = histedit_load_list(&histedit_cmds, path, repo);
7004 0ebf8283 2019-07-24 stsp free(path);
7005 0ebf8283 2019-07-24 stsp if (error)
7006 0ebf8283 2019-07-24 stsp goto done;
7008 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_continue(&resume_commit_id,
7009 3e3a69f1 2019-07-25 stsp &tmp_branch, &branch, &base_commit_id, &fileindex,
7010 3e3a69f1 2019-07-25 stsp worktree, repo);
7011 0ebf8283 2019-07-24 stsp if (error)
7012 0ebf8283 2019-07-24 stsp goto done;
7014 0ebf8283 2019-07-24 stsp error = got_ref_resolve(&head_commit_id, repo, branch);
7015 0ebf8283 2019-07-24 stsp if (error)
7016 0ebf8283 2019-07-24 stsp goto done;
7018 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo,
7019 0ebf8283 2019-07-24 stsp head_commit_id);
7020 0ebf8283 2019-07-24 stsp if (error)
7021 0ebf8283 2019-07-24 stsp goto done;
7022 0ebf8283 2019-07-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
7023 0ebf8283 2019-07-24 stsp pid = SIMPLEQ_FIRST(parent_ids);
7024 3aac7cf7 2019-07-25 stsp if (pid == NULL) {
7025 3aac7cf7 2019-07-25 stsp error = got_error(GOT_ERR_EMPTY_HISTEDIT);
7026 3aac7cf7 2019-07-25 stsp goto done;
7028 8ca9bd68 2019-07-25 stsp error = collect_commits(&commits, head_commit_id, pid->id,
7029 8ca9bd68 2019-07-25 stsp base_commit_id, got_worktree_get_path_prefix(worktree),
7030 8ca9bd68 2019-07-25 stsp GOT_ERR_HISTEDIT_PATH, repo);
7031 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
7032 0ebf8283 2019-07-24 stsp commit = NULL;
7033 0ebf8283 2019-07-24 stsp if (error)
7034 0ebf8283 2019-07-24 stsp goto done;
7036 0ebf8283 2019-07-24 stsp if (edit_in_progress) {
7037 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_HISTEDIT_BUSY);
7038 0ebf8283 2019-07-24 stsp goto done;
7041 0ebf8283 2019-07-24 stsp error = got_ref_open(&branch, repo,
7042 0ebf8283 2019-07-24 stsp got_worktree_get_head_ref_name(worktree), 0);
7043 0ebf8283 2019-07-24 stsp if (error != NULL)
7044 0ebf8283 2019-07-24 stsp goto done;
7046 c7d20a3f 2019-07-30 stsp if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
7047 c7d20a3f 2019-07-30 stsp error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
7048 c7d20a3f 2019-07-30 stsp "will not edit commit history of a branch outside "
7049 c7d20a3f 2019-07-30 stsp "the \"refs/heads/\" reference namespace");
7050 c7d20a3f 2019-07-30 stsp goto done;
7053 0ebf8283 2019-07-24 stsp error = got_ref_resolve(&head_commit_id, repo, branch);
7054 bfce7f83 2019-07-27 stsp got_ref_close(branch);
7055 bfce7f83 2019-07-27 stsp branch = NULL;
7056 0ebf8283 2019-07-24 stsp if (error)
7057 0ebf8283 2019-07-24 stsp goto done;
7059 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo,
7060 0ebf8283 2019-07-24 stsp head_commit_id);
7061 0ebf8283 2019-07-24 stsp if (error)
7062 0ebf8283 2019-07-24 stsp goto done;
7063 0ebf8283 2019-07-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
7064 0ebf8283 2019-07-24 stsp pid = SIMPLEQ_FIRST(parent_ids);
7065 3aac7cf7 2019-07-25 stsp if (pid == NULL) {
7066 3aac7cf7 2019-07-25 stsp error = got_error(GOT_ERR_EMPTY_HISTEDIT);
7067 3aac7cf7 2019-07-25 stsp goto done;
7069 8ca9bd68 2019-07-25 stsp error = collect_commits(&commits, head_commit_id, pid->id,
7070 8ca9bd68 2019-07-25 stsp got_worktree_get_base_commit_id(worktree),
7071 8ca9bd68 2019-07-25 stsp got_worktree_get_path_prefix(worktree),
7072 8ca9bd68 2019-07-25 stsp GOT_ERR_HISTEDIT_PATH, repo);
7073 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
7074 0ebf8283 2019-07-24 stsp commit = NULL;
7075 0ebf8283 2019-07-24 stsp if (error)
7076 0ebf8283 2019-07-24 stsp goto done;
7078 c5996fff 2020-01-29 stsp if (SIMPLEQ_EMPTY(&commits)) {
7079 c5996fff 2020-01-29 stsp error = got_error(GOT_ERR_EMPTY_HISTEDIT);
7080 c5996fff 2020-01-29 stsp goto done;
7083 bfce7f83 2019-07-27 stsp error = got_worktree_histedit_prepare(&tmp_branch, &branch,
7084 bfce7f83 2019-07-27 stsp &base_commit_id, &fileindex, worktree, repo);
7085 bfce7f83 2019-07-27 stsp if (error)
7086 bfce7f83 2019-07-27 stsp goto done;
7088 0ebf8283 2019-07-24 stsp if (edit_script_path) {
7089 0ebf8283 2019-07-24 stsp error = histedit_load_list(&histedit_cmds,
7090 0ebf8283 2019-07-24 stsp edit_script_path, repo);
7091 6ba2bea2 2019-07-27 stsp if (error) {
7092 6ba2bea2 2019-07-27 stsp got_worktree_histedit_abort(worktree, fileindex,
7093 6ba2bea2 2019-07-27 stsp repo, branch, base_commit_id,
7094 6ba2bea2 2019-07-27 stsp update_progress, &did_something);
7095 0ebf8283 2019-07-24 stsp goto done;
7098 514f2ffe 2020-01-29 stsp const char *branch_name;
7099 514f2ffe 2020-01-29 stsp branch_name = got_ref_get_symref_target(branch);
7100 514f2ffe 2020-01-29 stsp if (strncmp(branch_name, "refs/heads/", 11) == 0)
7101 514f2ffe 2020-01-29 stsp branch_name += 11;
7102 0ebf8283 2019-07-24 stsp error = histedit_edit_script(&histedit_cmds, &commits,
7103 083957f4 2020-02-24 stsp branch_name, edit_logmsg_only, repo);
7104 6ba2bea2 2019-07-27 stsp if (error) {
7105 6ba2bea2 2019-07-27 stsp got_worktree_histedit_abort(worktree, fileindex,
7106 6ba2bea2 2019-07-27 stsp repo, branch, base_commit_id,
7107 6ba2bea2 2019-07-27 stsp update_progress, &did_something);
7108 0ebf8283 2019-07-24 stsp goto done;
7113 0ebf8283 2019-07-24 stsp error = histedit_save_list(&histedit_cmds, worktree,
7115 6ba2bea2 2019-07-27 stsp if (error) {
7116 6ba2bea2 2019-07-27 stsp got_worktree_histedit_abort(worktree, fileindex,
7117 6ba2bea2 2019-07-27 stsp repo, branch, base_commit_id,
7118 6ba2bea2 2019-07-27 stsp update_progress, &did_something);
7119 0ebf8283 2019-07-24 stsp goto done;
7124 4ec14e60 2019-08-28 hiltjo error = histedit_check_script(&histedit_cmds, &commits, repo);
7125 4ec14e60 2019-08-28 hiltjo if (error)
7126 0ebf8283 2019-07-24 stsp goto done;
7128 0ebf8283 2019-07-24 stsp TAILQ_FOREACH(hle, &histedit_cmds, entry) {
7129 0ebf8283 2019-07-24 stsp if (resume_commit_id) {
7130 0ebf8283 2019-07-24 stsp if (got_object_id_cmp(hle->commit_id,
7131 0ebf8283 2019-07-24 stsp resume_commit_id) != 0)
7134 0ebf8283 2019-07-24 stsp resume_commit_id = NULL;
7135 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_DROP ||
7136 0ebf8283 2019-07-24 stsp hle->cmd->code == GOT_HISTEDIT_FOLD) {
7137 0ebf8283 2019-07-24 stsp error = histedit_skip_commit(hle, worktree,
7139 ab20a43a 2020-01-29 stsp if (error)
7140 ab20a43a 2020-01-29 stsp goto done;
7142 ab20a43a 2020-01-29 stsp struct got_pathlist_head paths;
7143 ab20a43a 2020-01-29 stsp int have_changes = 0;
7145 ab20a43a 2020-01-29 stsp TAILQ_INIT(&paths);
7146 ab20a43a 2020-01-29 stsp error = got_pathlist_append(&paths, "", NULL);
7147 ab20a43a 2020-01-29 stsp if (error)
7148 ab20a43a 2020-01-29 stsp goto done;
7149 ab20a43a 2020-01-29 stsp error = got_worktree_status(worktree, &paths,
7150 ab20a43a 2020-01-29 stsp repo, check_local_changes, &have_changes,
7151 ab20a43a 2020-01-29 stsp check_cancelled, NULL);
7152 ab20a43a 2020-01-29 stsp got_pathlist_free(&paths);
7153 ab20a43a 2020-01-29 stsp if (error) {
7154 ab20a43a 2020-01-29 stsp if (error->code != GOT_ERR_CANCELLED)
7155 ab20a43a 2020-01-29 stsp goto done;
7156 ab20a43a 2020-01-29 stsp if (sigint_received || sigpipe_received)
7157 ab20a43a 2020-01-29 stsp goto done;
7159 ab20a43a 2020-01-29 stsp if (have_changes) {
7160 ab20a43a 2020-01-29 stsp error = histedit_commit(NULL, worktree,
7161 ab20a43a 2020-01-29 stsp fileindex, tmp_branch, hle, repo);
7162 ab20a43a 2020-01-29 stsp if (error)
7163 ab20a43a 2020-01-29 stsp goto done;
7165 ab20a43a 2020-01-29 stsp error = got_object_open_as_commit(
7166 ab20a43a 2020-01-29 stsp &commit, repo, hle->commit_id);
7167 ab20a43a 2020-01-29 stsp if (error)
7168 ab20a43a 2020-01-29 stsp goto done;
7169 ab20a43a 2020-01-29 stsp error = show_histedit_progress(commit,
7170 ab20a43a 2020-01-29 stsp hle, NULL);
7171 ab20a43a 2020-01-29 stsp got_object_commit_close(commit);
7172 ab20a43a 2020-01-29 stsp commit = NULL;
7173 ab20a43a 2020-01-29 stsp if (error)
7174 ab20a43a 2020-01-29 stsp goto done;
7180 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_DROP) {
7181 0ebf8283 2019-07-24 stsp error = histedit_skip_commit(hle, worktree, repo);
7182 0ebf8283 2019-07-24 stsp if (error)
7183 0ebf8283 2019-07-24 stsp goto done;
7187 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo,
7188 0ebf8283 2019-07-24 stsp hle->commit_id);
7189 0ebf8283 2019-07-24 stsp if (error)
7190 0ebf8283 2019-07-24 stsp goto done;
7191 0ebf8283 2019-07-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
7192 0ebf8283 2019-07-24 stsp pid = SIMPLEQ_FIRST(parent_ids);
7194 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_merge_files(&merged_paths,
7195 3e3a69f1 2019-07-25 stsp worktree, fileindex, pid->id, hle->commit_id, repo,
7196 3e3a69f1 2019-07-25 stsp rebase_progress, &rebase_status, check_cancelled, NULL);
7197 0ebf8283 2019-07-24 stsp if (error)
7198 0ebf8283 2019-07-24 stsp goto done;
7199 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
7200 0ebf8283 2019-07-24 stsp commit = NULL;
7202 0ebf8283 2019-07-24 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
7203 a0ea4fc0 2020-02-28 stsp error = show_rebase_merge_conflict(hle->commit_id,
7205 a0ea4fc0 2020-02-28 stsp if (error)
7206 a0ea4fc0 2020-02-28 stsp goto done;
7207 0ebf8283 2019-07-24 stsp got_worktree_rebase_pathlist_free(&merged_paths);
7211 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
7212 0ebf8283 2019-07-24 stsp char *id_str;
7213 0ebf8283 2019-07-24 stsp error = got_object_id_str(&id_str, hle->commit_id);
7214 0ebf8283 2019-07-24 stsp if (error)
7215 0ebf8283 2019-07-24 stsp goto done;
7216 0ebf8283 2019-07-24 stsp printf("Stopping histedit for amending commit %s\n",
7218 0ebf8283 2019-07-24 stsp free(id_str);
7219 0ebf8283 2019-07-24 stsp got_worktree_rebase_pathlist_free(&merged_paths);
7220 3e3a69f1 2019-07-25 stsp error = got_worktree_histedit_postpone(worktree,
7221 3e3a69f1 2019-07-25 stsp fileindex);
7222 0ebf8283 2019-07-24 stsp goto done;
7225 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
7226 0ebf8283 2019-07-24 stsp error = histedit_skip_commit(hle, worktree, repo);
7227 0ebf8283 2019-07-24 stsp if (error)
7228 0ebf8283 2019-07-24 stsp goto done;
7232 3e3a69f1 2019-07-25 stsp error = histedit_commit(&merged_paths, worktree, fileindex,
7233 3e3a69f1 2019-07-25 stsp tmp_branch, hle, repo);
7234 0ebf8283 2019-07-24 stsp got_worktree_rebase_pathlist_free(&merged_paths);
7235 0ebf8283 2019-07-24 stsp if (error)
7236 0ebf8283 2019-07-24 stsp goto done;
7239 0ebf8283 2019-07-24 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
7240 3e3a69f1 2019-07-25 stsp error = got_worktree_histedit_postpone(worktree, fileindex);
7241 0ebf8283 2019-07-24 stsp if (error)
7242 0ebf8283 2019-07-24 stsp goto done;
7243 0ebf8283 2019-07-24 stsp error = got_error_msg(GOT_ERR_CONFLICTS,
7244 4cb8f8f3 2020-03-05 stsp "conflicts must be resolved before histedit can continue");
7246 3e3a69f1 2019-07-25 stsp error = histedit_complete(worktree, fileindex, tmp_branch,
7247 3e3a69f1 2019-07-25 stsp branch, repo);
7249 0ebf8283 2019-07-24 stsp got_object_id_queue_free(&commits);
7250 bfce7f83 2019-07-27 stsp histedit_free_list(&histedit_cmds);
7251 0ebf8283 2019-07-24 stsp free(head_commit_id);
7252 0ebf8283 2019-07-24 stsp free(base_commit_id);
7253 0ebf8283 2019-07-24 stsp free(resume_commit_id);
7254 0ebf8283 2019-07-24 stsp if (commit)
7255 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
7256 0ebf8283 2019-07-24 stsp if (branch)
7257 0ebf8283 2019-07-24 stsp got_ref_close(branch);
7258 0ebf8283 2019-07-24 stsp if (tmp_branch)
7259 0ebf8283 2019-07-24 stsp got_ref_close(tmp_branch);
7260 0ebf8283 2019-07-24 stsp if (worktree)
7261 0ebf8283 2019-07-24 stsp got_worktree_close(worktree);
7263 2822a352 2019-10-15 stsp got_repo_close(repo);
7264 2822a352 2019-10-15 stsp return error;
7267 2822a352 2019-10-15 stsp __dead static void
7268 2822a352 2019-10-15 stsp usage_integrate(void)
7270 2822a352 2019-10-15 stsp fprintf(stderr, "usage: %s integrate branch\n", getprogname());
7274 2822a352 2019-10-15 stsp static const struct got_error *
7275 2822a352 2019-10-15 stsp cmd_integrate(int argc, char *argv[])
7277 2822a352 2019-10-15 stsp const struct got_error *error = NULL;
7278 2822a352 2019-10-15 stsp struct got_repository *repo = NULL;
7279 2822a352 2019-10-15 stsp struct got_worktree *worktree = NULL;
7280 2822a352 2019-10-15 stsp char *cwd = NULL, *refname = NULL, *base_refname = NULL;
7281 2822a352 2019-10-15 stsp const char *branch_arg = NULL;
7282 2822a352 2019-10-15 stsp struct got_reference *branch_ref = NULL, *base_branch_ref = NULL;
7283 2822a352 2019-10-15 stsp struct got_fileindex *fileindex = NULL;
7284 2822a352 2019-10-15 stsp struct got_object_id *commit_id = NULL, *base_commit_id = NULL;
7285 2822a352 2019-10-15 stsp int ch, did_something = 0;
7287 2822a352 2019-10-15 stsp while ((ch = getopt(argc, argv, "")) != -1) {
7288 2822a352 2019-10-15 stsp switch (ch) {
7290 2822a352 2019-10-15 stsp usage_integrate();
7291 2822a352 2019-10-15 stsp /* NOTREACHED */
7295 2822a352 2019-10-15 stsp argc -= optind;
7296 2822a352 2019-10-15 stsp argv += optind;
7298 2822a352 2019-10-15 stsp if (argc != 1)
7299 2822a352 2019-10-15 stsp usage_integrate();
7300 2822a352 2019-10-15 stsp branch_arg = argv[0];
7302 2822a352 2019-10-15 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7303 2822a352 2019-10-15 stsp "unveil", NULL) == -1)
7304 2822a352 2019-10-15 stsp err(1, "pledge");
7306 2822a352 2019-10-15 stsp cwd = getcwd(NULL, 0);
7307 2822a352 2019-10-15 stsp if (cwd == NULL) {
7308 2822a352 2019-10-15 stsp error = got_error_from_errno("getcwd");
7309 2822a352 2019-10-15 stsp goto done;
7312 2822a352 2019-10-15 stsp error = got_worktree_open(&worktree, cwd);
7313 2822a352 2019-10-15 stsp if (error)
7314 2822a352 2019-10-15 stsp goto done;
7316 2822a352 2019-10-15 stsp error = check_rebase_or_histedit_in_progress(worktree);
7317 2822a352 2019-10-15 stsp if (error)
7318 2822a352 2019-10-15 stsp goto done;
7320 2822a352 2019-10-15 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7322 2822a352 2019-10-15 stsp if (error != NULL)
7323 2822a352 2019-10-15 stsp goto done;
7325 2822a352 2019-10-15 stsp error = apply_unveil(got_repo_get_path(repo), 0,
7326 2822a352 2019-10-15 stsp got_worktree_get_root_path(worktree));
7327 2822a352 2019-10-15 stsp if (error)
7328 2822a352 2019-10-15 stsp goto done;
7330 2822a352 2019-10-15 stsp if (asprintf(&refname, "refs/heads/%s", branch_arg) == -1) {
7331 2822a352 2019-10-15 stsp error = got_error_from_errno("asprintf");
7332 2822a352 2019-10-15 stsp goto done;
7335 2822a352 2019-10-15 stsp error = got_worktree_integrate_prepare(&fileindex, &branch_ref,
7336 2822a352 2019-10-15 stsp &base_branch_ref, worktree, refname, repo);
7337 2822a352 2019-10-15 stsp if (error)
7338 2822a352 2019-10-15 stsp goto done;
7340 2822a352 2019-10-15 stsp refname = strdup(got_ref_get_name(branch_ref));
7341 2822a352 2019-10-15 stsp if (refname == NULL) {
7342 2822a352 2019-10-15 stsp error = got_error_from_errno("strdup");
7343 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
7344 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
7345 2822a352 2019-10-15 stsp goto done;
7347 2822a352 2019-10-15 stsp base_refname = strdup(got_ref_get_name(base_branch_ref));
7348 2822a352 2019-10-15 stsp if (base_refname == NULL) {
7349 2822a352 2019-10-15 stsp error = got_error_from_errno("strdup");
7350 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
7351 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
7352 2822a352 2019-10-15 stsp goto done;
7355 2822a352 2019-10-15 stsp error = got_ref_resolve(&commit_id, repo, branch_ref);
7356 2822a352 2019-10-15 stsp if (error)
7357 2822a352 2019-10-15 stsp goto done;
7359 2822a352 2019-10-15 stsp error = got_ref_resolve(&base_commit_id, repo, base_branch_ref);
7360 2822a352 2019-10-15 stsp if (error)
7361 2822a352 2019-10-15 stsp goto done;
7363 2822a352 2019-10-15 stsp if (got_object_id_cmp(commit_id, base_commit_id) == 0) {
7364 2822a352 2019-10-15 stsp error = got_error_msg(GOT_ERR_SAME_BRANCH,
7365 2822a352 2019-10-15 stsp "specified branch has already been integrated");
7366 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
7367 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
7368 2822a352 2019-10-15 stsp goto done;
7371 3aef623b 2019-10-15 stsp error = check_linear_ancestry(commit_id, base_commit_id, 1, repo);
7372 2822a352 2019-10-15 stsp if (error) {
7373 2822a352 2019-10-15 stsp if (error->code == GOT_ERR_ANCESTRY)
7374 2822a352 2019-10-15 stsp error = got_error(GOT_ERR_REBASE_REQUIRED);
7375 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
7376 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
7377 2822a352 2019-10-15 stsp goto done;
7380 2822a352 2019-10-15 stsp error = got_worktree_integrate_continue(worktree, fileindex, repo,
7381 2822a352 2019-10-15 stsp branch_ref, base_branch_ref, update_progress, &did_something,
7382 2822a352 2019-10-15 stsp check_cancelled, NULL);
7383 2822a352 2019-10-15 stsp if (error)
7384 2822a352 2019-10-15 stsp goto done;
7386 2822a352 2019-10-15 stsp printf("Integrated %s into %s\n", refname, base_refname);
7389 715dc77e 2019-08-03 stsp got_repo_close(repo);
7390 2822a352 2019-10-15 stsp if (worktree)
7391 2822a352 2019-10-15 stsp got_worktree_close(worktree);
7392 2822a352 2019-10-15 stsp free(cwd);
7393 2822a352 2019-10-15 stsp free(base_commit_id);
7394 2822a352 2019-10-15 stsp free(commit_id);
7395 2822a352 2019-10-15 stsp free(refname);
7396 2822a352 2019-10-15 stsp free(base_refname);
7397 715dc77e 2019-08-03 stsp return error;
7400 715dc77e 2019-08-03 stsp __dead static void
7401 715dc77e 2019-08-03 stsp usage_stage(void)
7403 f3055044 2019-08-07 stsp fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
7404 f3055044 2019-08-07 stsp "[file-path ...]\n",
7405 715dc77e 2019-08-03 stsp getprogname());
7409 715dc77e 2019-08-03 stsp static const struct got_error *
7410 408b4ebc 2019-08-03 stsp print_stage(void *arg, unsigned char status, unsigned char staged_status,
7411 408b4ebc 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
7412 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
7413 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
7415 408b4ebc 2019-08-03 stsp const struct got_error *err = NULL;
7416 408b4ebc 2019-08-03 stsp char *id_str = NULL;
7418 408b4ebc 2019-08-03 stsp if (staged_status != GOT_STATUS_ADD &&
7419 408b4ebc 2019-08-03 stsp staged_status != GOT_STATUS_MODIFY &&
7420 408b4ebc 2019-08-03 stsp staged_status != GOT_STATUS_DELETE)
7421 408b4ebc 2019-08-03 stsp return NULL;
7423 408b4ebc 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
7424 408b4ebc 2019-08-03 stsp staged_status == GOT_STATUS_MODIFY)
7425 408b4ebc 2019-08-03 stsp err = got_object_id_str(&id_str, staged_blob_id);
7427 408b4ebc 2019-08-03 stsp err = got_object_id_str(&id_str, blob_id);
7429 408b4ebc 2019-08-03 stsp return err;
7431 408b4ebc 2019-08-03 stsp printf("%s %c %s\n", id_str, staged_status, path);
7432 408b4ebc 2019-08-03 stsp free(id_str);
7433 dc424a06 2019-08-07 stsp return NULL;
7436 dc424a06 2019-08-07 stsp static const struct got_error *
7437 715dc77e 2019-08-03 stsp cmd_stage(int argc, char *argv[])
7439 715dc77e 2019-08-03 stsp const struct got_error *error = NULL;
7440 715dc77e 2019-08-03 stsp struct got_repository *repo = NULL;
7441 715dc77e 2019-08-03 stsp struct got_worktree *worktree = NULL;
7442 715dc77e 2019-08-03 stsp char *cwd = NULL;
7443 715dc77e 2019-08-03 stsp struct got_pathlist_head paths;
7444 715dc77e 2019-08-03 stsp struct got_pathlist_entry *pe;
7445 dc424a06 2019-08-07 stsp int ch, list_stage = 0, pflag = 0;
7446 dc424a06 2019-08-07 stsp FILE *patch_script_file = NULL;
7447 2e1f37b0 2019-08-08 stsp const char *patch_script_path = NULL;
7448 2e1f37b0 2019-08-08 stsp struct choose_patch_arg cpa;
7450 715dc77e 2019-08-03 stsp TAILQ_INIT(&paths);
7452 dc424a06 2019-08-07 stsp while ((ch = getopt(argc, argv, "lpF:")) != -1) {
7453 715dc77e 2019-08-03 stsp switch (ch) {
7455 408b4ebc 2019-08-03 stsp list_stage = 1;
7458 dc424a06 2019-08-07 stsp pflag = 1;
7461 dc424a06 2019-08-07 stsp patch_script_path = optarg;
7464 715dc77e 2019-08-03 stsp usage_stage();
7465 715dc77e 2019-08-03 stsp /* NOTREACHED */
7469 715dc77e 2019-08-03 stsp argc -= optind;
7470 715dc77e 2019-08-03 stsp argv += optind;
7472 715dc77e 2019-08-03 stsp #ifndef PROFILE
7473 715dc77e 2019-08-03 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7474 715dc77e 2019-08-03 stsp "unveil", NULL) == -1)
7475 715dc77e 2019-08-03 stsp err(1, "pledge");
7477 2db2652d 2019-08-07 stsp if (list_stage && (pflag || patch_script_path))
7478 2db2652d 2019-08-07 stsp errx(1, "-l option cannot be used with other options");
7479 dc424a06 2019-08-07 stsp if (patch_script_path && !pflag)
7480 dc424a06 2019-08-07 stsp errx(1, "-F option can only be used together with -p option");
7482 715dc77e 2019-08-03 stsp cwd = getcwd(NULL, 0);
7483 715dc77e 2019-08-03 stsp if (cwd == NULL) {
7484 715dc77e 2019-08-03 stsp error = got_error_from_errno("getcwd");
7485 715dc77e 2019-08-03 stsp goto done;
7488 715dc77e 2019-08-03 stsp error = got_worktree_open(&worktree, cwd);
7489 715dc77e 2019-08-03 stsp if (error)
7490 715dc77e 2019-08-03 stsp goto done;
7492 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7494 715dc77e 2019-08-03 stsp if (error != NULL)
7495 715dc77e 2019-08-03 stsp goto done;
7497 dc424a06 2019-08-07 stsp if (patch_script_path) {
7498 dc424a06 2019-08-07 stsp patch_script_file = fopen(patch_script_path, "r");
7499 dc424a06 2019-08-07 stsp if (patch_script_file == NULL) {
7500 dc424a06 2019-08-07 stsp error = got_error_from_errno2("fopen",
7501 dc424a06 2019-08-07 stsp patch_script_path);
7502 dc424a06 2019-08-07 stsp goto done;
7505 9fd7cd22 2019-08-30 stsp error = apply_unveil(got_repo_get_path(repo), 0,
7506 715dc77e 2019-08-03 stsp got_worktree_get_root_path(worktree));
7507 715dc77e 2019-08-03 stsp if (error)
7508 715dc77e 2019-08-03 stsp goto done;
7510 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7511 6d022e97 2019-08-04 stsp if (error)
7512 6d022e97 2019-08-04 stsp goto done;
7514 6d022e97 2019-08-04 stsp if (list_stage)
7515 408b4ebc 2019-08-03 stsp error = got_worktree_status(worktree, &paths, repo,
7516 408b4ebc 2019-08-03 stsp print_stage, NULL, check_cancelled, NULL);
7518 2e1f37b0 2019-08-08 stsp cpa.patch_script_file = patch_script_file;
7519 2e1f37b0 2019-08-08 stsp cpa.action = "stage";
7520 dc424a06 2019-08-07 stsp error = got_worktree_stage(worktree, &paths,
7521 dc424a06 2019-08-07 stsp pflag ? NULL : print_status, NULL,
7522 2e1f37b0 2019-08-08 stsp pflag ? choose_patch : NULL, &cpa, repo);
7525 2e1f37b0 2019-08-08 stsp if (patch_script_file && fclose(patch_script_file) == EOF &&
7526 2e1f37b0 2019-08-08 stsp error == NULL)
7527 2e1f37b0 2019-08-08 stsp error = got_error_from_errno2("fclose", patch_script_path);
7529 ad493afc 2019-08-03 stsp got_repo_close(repo);
7530 ad493afc 2019-08-03 stsp if (worktree)
7531 ad493afc 2019-08-03 stsp got_worktree_close(worktree);
7532 ad493afc 2019-08-03 stsp TAILQ_FOREACH(pe, &paths, entry)
7533 ad493afc 2019-08-03 stsp free((char *)pe->path);
7534 ad493afc 2019-08-03 stsp got_pathlist_free(&paths);
7535 ad493afc 2019-08-03 stsp free(cwd);
7536 ad493afc 2019-08-03 stsp return error;
7539 ad493afc 2019-08-03 stsp __dead static void
7540 ad493afc 2019-08-03 stsp usage_unstage(void)
7542 2e1f37b0 2019-08-08 stsp fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
7543 2e1f37b0 2019-08-08 stsp "[file-path ...]\n",
7544 ad493afc 2019-08-03 stsp getprogname());
7549 ad493afc 2019-08-03 stsp static const struct got_error *
7550 ad493afc 2019-08-03 stsp cmd_unstage(int argc, char *argv[])
7552 ad493afc 2019-08-03 stsp const struct got_error *error = NULL;
7553 ad493afc 2019-08-03 stsp struct got_repository *repo = NULL;
7554 ad493afc 2019-08-03 stsp struct got_worktree *worktree = NULL;
7555 ad493afc 2019-08-03 stsp char *cwd = NULL;
7556 ad493afc 2019-08-03 stsp struct got_pathlist_head paths;
7557 ad493afc 2019-08-03 stsp struct got_pathlist_entry *pe;
7558 2e1f37b0 2019-08-08 stsp int ch, did_something = 0, pflag = 0;
7559 2e1f37b0 2019-08-08 stsp FILE *patch_script_file = NULL;
7560 2e1f37b0 2019-08-08 stsp const char *patch_script_path = NULL;
7561 2e1f37b0 2019-08-08 stsp struct choose_patch_arg cpa;
7563 ad493afc 2019-08-03 stsp TAILQ_INIT(&paths);
7565 2e1f37b0 2019-08-08 stsp while ((ch = getopt(argc, argv, "pF:")) != -1) {
7566 ad493afc 2019-08-03 stsp switch (ch) {
7568 2e1f37b0 2019-08-08 stsp pflag = 1;
7571 2e1f37b0 2019-08-08 stsp patch_script_path = optarg;
7574 ad493afc 2019-08-03 stsp usage_unstage();
7575 ad493afc 2019-08-03 stsp /* NOTREACHED */
7579 ad493afc 2019-08-03 stsp argc -= optind;
7580 ad493afc 2019-08-03 stsp argv += optind;
7582 ad493afc 2019-08-03 stsp #ifndef PROFILE
7583 ad493afc 2019-08-03 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7584 ad493afc 2019-08-03 stsp "unveil", NULL) == -1)
7585 ad493afc 2019-08-03 stsp err(1, "pledge");
7587 2e1f37b0 2019-08-08 stsp if (patch_script_path && !pflag)
7588 2e1f37b0 2019-08-08 stsp errx(1, "-F option can only be used together with -p option");
7590 ad493afc 2019-08-03 stsp cwd = getcwd(NULL, 0);
7591 ad493afc 2019-08-03 stsp if (cwd == NULL) {
7592 ad493afc 2019-08-03 stsp error = got_error_from_errno("getcwd");
7593 ad493afc 2019-08-03 stsp goto done;
7596 ad493afc 2019-08-03 stsp error = got_worktree_open(&worktree, cwd);
7597 ad493afc 2019-08-03 stsp if (error)
7598 ad493afc 2019-08-03 stsp goto done;
7600 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7602 ad493afc 2019-08-03 stsp if (error != NULL)
7603 ad493afc 2019-08-03 stsp goto done;
7605 2e1f37b0 2019-08-08 stsp if (patch_script_path) {
7606 2e1f37b0 2019-08-08 stsp patch_script_file = fopen(patch_script_path, "r");
7607 2e1f37b0 2019-08-08 stsp if (patch_script_file == NULL) {
7608 2e1f37b0 2019-08-08 stsp error = got_error_from_errno2("fopen",
7609 2e1f37b0 2019-08-08 stsp patch_script_path);
7610 2e1f37b0 2019-08-08 stsp goto done;
7614 00f36e47 2019-09-06 stsp error = apply_unveil(got_repo_get_path(repo), 0,
7615 ad493afc 2019-08-03 stsp got_worktree_get_root_path(worktree));
7616 ad493afc 2019-08-03 stsp if (error)
7617 ad493afc 2019-08-03 stsp goto done;
7619 ad493afc 2019-08-03 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7620 ad493afc 2019-08-03 stsp if (error)
7621 ad493afc 2019-08-03 stsp goto done;
7623 2e1f37b0 2019-08-08 stsp cpa.patch_script_file = patch_script_file;
7624 2e1f37b0 2019-08-08 stsp cpa.action = "unstage";
7625 ad493afc 2019-08-03 stsp error = got_worktree_unstage(worktree, &paths, update_progress,
7626 2e1f37b0 2019-08-08 stsp &did_something, pflag ? choose_patch : NULL, &cpa, repo);
7628 2e1f37b0 2019-08-08 stsp if (patch_script_file && fclose(patch_script_file) == EOF &&
7629 2e1f37b0 2019-08-08 stsp error == NULL)
7630 2e1f37b0 2019-08-08 stsp error = got_error_from_errno2("fclose", patch_script_path);
7632 0ebf8283 2019-07-24 stsp got_repo_close(repo);
7633 715dc77e 2019-08-03 stsp if (worktree)
7634 715dc77e 2019-08-03 stsp got_worktree_close(worktree);
7635 715dc77e 2019-08-03 stsp TAILQ_FOREACH(pe, &paths, entry)
7636 715dc77e 2019-08-03 stsp free((char *)pe->path);
7637 715dc77e 2019-08-03 stsp got_pathlist_free(&paths);
7638 715dc77e 2019-08-03 stsp free(cwd);
7639 01073a5d 2019-08-22 stsp return error;
7642 01073a5d 2019-08-22 stsp __dead static void
7643 01073a5d 2019-08-22 stsp usage_cat(void)
7645 896e9b6f 2019-08-26 stsp fprintf(stderr, "usage: %s cat [-r repository ] [ -c commit ] [ -P ] "
7646 896e9b6f 2019-08-26 stsp "arg1 [arg2 ...]\n", getprogname());
7650 01073a5d 2019-08-22 stsp static const struct got_error *
7651 01073a5d 2019-08-22 stsp cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7653 01073a5d 2019-08-22 stsp const struct got_error *err;
7654 01073a5d 2019-08-22 stsp struct got_blob_object *blob;
7656 01073a5d 2019-08-22 stsp err = got_object_open_as_blob(&blob, repo, id, 8192);
7658 01073a5d 2019-08-22 stsp return err;
7660 01073a5d 2019-08-22 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
7661 01073a5d 2019-08-22 stsp got_object_blob_close(blob);
7662 01073a5d 2019-08-22 stsp return err;
7665 01073a5d 2019-08-22 stsp static const struct got_error *
7666 01073a5d 2019-08-22 stsp cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7668 01073a5d 2019-08-22 stsp const struct got_error *err;
7669 01073a5d 2019-08-22 stsp struct got_tree_object *tree;
7670 56e0773d 2019-11-28 stsp int nentries, i;
7672 01073a5d 2019-08-22 stsp err = got_object_open_as_tree(&tree, repo, id);
7674 01073a5d 2019-08-22 stsp return err;
7676 56e0773d 2019-11-28 stsp nentries = got_object_tree_get_nentries(tree);
7677 56e0773d 2019-11-28 stsp for (i = 0; i < nentries; i++) {
7678 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
7679 01073a5d 2019-08-22 stsp char *id_str;
7680 01073a5d 2019-08-22 stsp if (sigint_received || sigpipe_received)
7682 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(tree, i);
7683 56e0773d 2019-11-28 stsp err = got_object_id_str(&id_str, got_tree_entry_get_id(te));
7686 56e0773d 2019-11-28 stsp fprintf(outfile, "%s %.7o %s\n", id_str,
7687 56e0773d 2019-11-28 stsp got_tree_entry_get_mode(te),
7688 56e0773d 2019-11-28 stsp got_tree_entry_get_name(te));
7689 01073a5d 2019-08-22 stsp free(id_str);
7692 01073a5d 2019-08-22 stsp got_object_tree_close(tree);
7693 01073a5d 2019-08-22 stsp return err;
7696 01073a5d 2019-08-22 stsp static const struct got_error *
7697 01073a5d 2019-08-22 stsp cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7699 01073a5d 2019-08-22 stsp const struct got_error *err;
7700 01073a5d 2019-08-22 stsp struct got_commit_object *commit;
7701 01073a5d 2019-08-22 stsp const struct got_object_id_queue *parent_ids;
7702 01073a5d 2019-08-22 stsp struct got_object_qid *pid;
7703 01073a5d 2019-08-22 stsp char *id_str = NULL;
7704 24ea5512 2019-08-22 stsp const char *logmsg = NULL;
7706 01073a5d 2019-08-22 stsp err = got_object_open_as_commit(&commit, repo, id);
7708 01073a5d 2019-08-22 stsp return err;
7710 01073a5d 2019-08-22 stsp err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
7712 01073a5d 2019-08-22 stsp goto done;
7714 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str);
7715 01073a5d 2019-08-22 stsp parent_ids = got_object_commit_get_parent_ids(commit);
7716 8aa93786 2019-08-22 stsp fprintf(outfile, "numparents %d\n",
7717 01073a5d 2019-08-22 stsp got_object_commit_get_nparents(commit));
7718 01073a5d 2019-08-22 stsp SIMPLEQ_FOREACH(pid, parent_ids, entry) {
7719 01073a5d 2019-08-22 stsp char *pid_str;
7720 01073a5d 2019-08-22 stsp err = got_object_id_str(&pid_str, pid->id);
7722 01073a5d 2019-08-22 stsp goto done;
7723 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_PARENT, pid_str);
7724 01073a5d 2019-08-22 stsp free(pid_str);
7726 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_AUTHOR,
7727 8aa93786 2019-08-22 stsp got_object_commit_get_author(commit),
7728 01073a5d 2019-08-22 stsp got_object_commit_get_author_time(commit));
7730 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_COMMITTER,
7731 8aa93786 2019-08-22 stsp got_object_commit_get_author(commit),
7732 01073a5d 2019-08-22 stsp got_object_commit_get_committer_time(commit));
7734 24ea5512 2019-08-22 stsp logmsg = got_object_commit_get_logmsg_raw(commit);
7735 8aa93786 2019-08-22 stsp fprintf(outfile, "messagelen %zd\n", strlen(logmsg));
7736 01073a5d 2019-08-22 stsp fprintf(outfile, "%s", logmsg);
7738 01073a5d 2019-08-22 stsp free(id_str);
7739 01073a5d 2019-08-22 stsp got_object_commit_close(commit);
7740 01073a5d 2019-08-22 stsp return err;
7743 01073a5d 2019-08-22 stsp static const struct got_error *
7744 01073a5d 2019-08-22 stsp cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7746 01073a5d 2019-08-22 stsp const struct got_error *err;
7747 01073a5d 2019-08-22 stsp struct got_tag_object *tag;
7748 01073a5d 2019-08-22 stsp char *id_str = NULL;
7749 01073a5d 2019-08-22 stsp const char *tagmsg = NULL;
7751 01073a5d 2019-08-22 stsp err = got_object_open_as_tag(&tag, repo, id);
7753 01073a5d 2019-08-22 stsp return err;
7755 01073a5d 2019-08-22 stsp err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
7757 01073a5d 2019-08-22 stsp goto done;
7759 e15d5241 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str);
7761 01073a5d 2019-08-22 stsp switch (got_object_tag_get_object_type(tag)) {
7762 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_BLOB:
7763 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7764 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_BLOB);
7766 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TREE:
7767 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7768 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_TREE);
7770 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_COMMIT:
7771 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7772 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_COMMIT);
7774 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TAG:
7775 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7776 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_TAG);
7782 e15d5241 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TAG,
7783 e15d5241 2019-08-22 stsp got_object_tag_get_name(tag));
7785 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s %lld +0000\n", GOT_TAG_LABEL_TAGGER,
7786 8aa93786 2019-08-22 stsp got_object_tag_get_tagger(tag),
7787 8aa93786 2019-08-22 stsp got_object_tag_get_tagger_time(tag));
7789 01073a5d 2019-08-22 stsp tagmsg = got_object_tag_get_message(tag);
7790 8aa93786 2019-08-22 stsp fprintf(outfile, "messagelen %zd\n", strlen(tagmsg));
7791 01073a5d 2019-08-22 stsp fprintf(outfile, "%s", tagmsg);
7793 01073a5d 2019-08-22 stsp free(id_str);
7794 01073a5d 2019-08-22 stsp got_object_tag_close(tag);
7795 01073a5d 2019-08-22 stsp return err;
7798 01073a5d 2019-08-22 stsp static const struct got_error *
7799 01073a5d 2019-08-22 stsp cmd_cat(int argc, char *argv[])
7801 01073a5d 2019-08-22 stsp const struct got_error *error;
7802 01073a5d 2019-08-22 stsp struct got_repository *repo = NULL;
7803 01073a5d 2019-08-22 stsp struct got_worktree *worktree = NULL;
7804 01073a5d 2019-08-22 stsp char *cwd = NULL, *repo_path = NULL, *label = NULL;
7805 896e9b6f 2019-08-26 stsp const char *commit_id_str = NULL;
7806 896e9b6f 2019-08-26 stsp struct got_object_id *id = NULL, *commit_id = NULL;
7807 896e9b6f 2019-08-26 stsp int ch, obj_type, i, force_path = 0;
7809 01073a5d 2019-08-22 stsp #ifndef PROFILE
7810 01073a5d 2019-08-22 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
7811 01073a5d 2019-08-22 stsp NULL) == -1)
7812 01073a5d 2019-08-22 stsp err(1, "pledge");
7815 896e9b6f 2019-08-26 stsp while ((ch = getopt(argc, argv, "c:r:P")) != -1) {
7816 01073a5d 2019-08-22 stsp switch (ch) {
7818 896e9b6f 2019-08-26 stsp commit_id_str = optarg;
7821 01073a5d 2019-08-22 stsp repo_path = realpath(optarg, NULL);
7822 01073a5d 2019-08-22 stsp if (repo_path == NULL)
7823 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
7825 01073a5d 2019-08-22 stsp got_path_strip_trailing_slashes(repo_path);
7828 896e9b6f 2019-08-26 stsp force_path = 1;
7831 01073a5d 2019-08-22 stsp usage_cat();
7832 01073a5d 2019-08-22 stsp /* NOTREACHED */
7836 01073a5d 2019-08-22 stsp argc -= optind;
7837 01073a5d 2019-08-22 stsp argv += optind;
7839 01073a5d 2019-08-22 stsp cwd = getcwd(NULL, 0);
7840 01073a5d 2019-08-22 stsp if (cwd == NULL) {
7841 01073a5d 2019-08-22 stsp error = got_error_from_errno("getcwd");
7842 01073a5d 2019-08-22 stsp goto done;
7844 01073a5d 2019-08-22 stsp error = got_worktree_open(&worktree, cwd);
7845 01073a5d 2019-08-22 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
7846 01073a5d 2019-08-22 stsp goto done;
7847 01073a5d 2019-08-22 stsp if (worktree) {
7848 01073a5d 2019-08-22 stsp if (repo_path == NULL) {
7849 01073a5d 2019-08-22 stsp repo_path = strdup(
7850 01073a5d 2019-08-22 stsp got_worktree_get_repo_path(worktree));
7851 01073a5d 2019-08-22 stsp if (repo_path == NULL) {
7852 01073a5d 2019-08-22 stsp error = got_error_from_errno("strdup");
7853 01073a5d 2019-08-22 stsp goto done;
7858 01073a5d 2019-08-22 stsp if (repo_path == NULL) {
7859 01073a5d 2019-08-22 stsp repo_path = getcwd(NULL, 0);
7860 01073a5d 2019-08-22 stsp if (repo_path == NULL)
7861 01073a5d 2019-08-22 stsp return got_error_from_errno("getcwd");
7864 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
7865 01073a5d 2019-08-22 stsp free(repo_path);
7866 01073a5d 2019-08-22 stsp if (error != NULL)
7867 01073a5d 2019-08-22 stsp goto done;
7869 01073a5d 2019-08-22 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
7870 896e9b6f 2019-08-26 stsp if (error)
7871 896e9b6f 2019-08-26 stsp goto done;
7873 896e9b6f 2019-08-26 stsp if (commit_id_str == NULL)
7874 896e9b6f 2019-08-26 stsp commit_id_str = GOT_REF_HEAD;
7875 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
7876 71a27632 2020-01-15 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
7877 01073a5d 2019-08-22 stsp if (error)
7878 01073a5d 2019-08-22 stsp goto done;
7880 01073a5d 2019-08-22 stsp for (i = 0; i < argc; i++) {
7881 896e9b6f 2019-08-26 stsp if (force_path) {
7882 896e9b6f 2019-08-26 stsp error = got_object_id_by_path(&id, repo, commit_id,
7884 896e9b6f 2019-08-26 stsp if (error)
7887 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&id, &label, argv[i],
7888 896e9b6f 2019-08-26 stsp GOT_OBJ_TYPE_ANY, 0, repo);
7889 896e9b6f 2019-08-26 stsp if (error) {
7890 896e9b6f 2019-08-26 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR &&
7891 896e9b6f 2019-08-26 stsp error->code != GOT_ERR_NOT_REF)
7893 896e9b6f 2019-08-26 stsp error = got_object_id_by_path(&id, repo,
7894 896e9b6f 2019-08-26 stsp commit_id, argv[i]);
7895 896e9b6f 2019-08-26 stsp if (error)
7900 01073a5d 2019-08-22 stsp error = got_object_get_type(&obj_type, repo, id);
7901 01073a5d 2019-08-22 stsp if (error)
7904 01073a5d 2019-08-22 stsp switch (obj_type) {
7905 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_BLOB:
7906 01073a5d 2019-08-22 stsp error = cat_blob(id, repo, stdout);
7908 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TREE:
7909 01073a5d 2019-08-22 stsp error = cat_tree(id, repo, stdout);
7911 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_COMMIT:
7912 01073a5d 2019-08-22 stsp error = cat_commit(id, repo, stdout);
7914 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TAG:
7915 01073a5d 2019-08-22 stsp error = cat_tag(id, repo, stdout);
7918 01073a5d 2019-08-22 stsp error = got_error(GOT_ERR_OBJ_TYPE);
7921 01073a5d 2019-08-22 stsp if (error)
7923 01073a5d 2019-08-22 stsp free(label);
7924 01073a5d 2019-08-22 stsp label = NULL;
7926 01073a5d 2019-08-22 stsp id = NULL;
7929 01073a5d 2019-08-22 stsp free(label);
7931 896e9b6f 2019-08-26 stsp free(commit_id);
7932 01073a5d 2019-08-22 stsp if (worktree)
7933 01073a5d 2019-08-22 stsp got_worktree_close(worktree);
7934 01073a5d 2019-08-22 stsp if (repo) {
7935 01073a5d 2019-08-22 stsp const struct got_error *repo_error;
7936 01073a5d 2019-08-22 stsp repo_error = got_repo_close(repo);
7937 01073a5d 2019-08-22 stsp if (error == NULL)
7938 01073a5d 2019-08-22 stsp error = repo_error;
7940 0ebf8283 2019-07-24 stsp return error;