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"
57 50b0790e 2020-09-11 stsp #include "got_gotconfig.h"
59 5c860e29 2018-03-12 stsp #ifndef nitems
60 5c860e29 2018-03-12 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
63 99437157 2018-11-11 stsp static volatile sig_atomic_t sigint_received;
64 99437157 2018-11-11 stsp static volatile sig_atomic_t sigpipe_received;
67 99437157 2018-11-11 stsp catch_sigint(int signo)
69 99437157 2018-11-11 stsp sigint_received = 1;
73 99437157 2018-11-11 stsp catch_sigpipe(int signo)
75 99437157 2018-11-11 stsp sigpipe_received = 1;
79 8cfb4057 2019-07-09 stsp struct got_cmd {
80 820059fa 2020-09-25 stsp const char *cmd_name;
81 d7d4f210 2018-03-12 stsp const struct got_error *(*cmd_main)(int, char *[]);
82 1b6b95a8 2018-03-12 stsp void (*cmd_usage)(void);
83 97b3a7be 2019-07-09 stsp const char *cmd_alias;
86 6879ba42 2020-10-01 naddy __dead static void usage(int, int);
87 2c7829a4 2019-06-17 stsp __dead static void usage_init(void);
88 3ce1b845 2019-07-15 stsp __dead static void usage_import(void);
89 93658fb9 2020-03-18 stsp __dead static void usage_clone(void);
90 7848a0e1 2020-03-19 stsp __dead static void usage_fetch(void);
91 2ab43947 2020-03-18 stsp __dead static void usage_checkout(void);
92 507dc3bb 2018-12-29 stsp __dead static void usage_update(void);
93 4ed7e80c 2018-05-20 stsp __dead static void usage_log(void);
94 4ed7e80c 2018-05-20 stsp __dead static void usage_diff(void);
95 404c43c4 2018-06-21 stsp __dead static void usage_blame(void);
96 5de5890b 2018-10-18 stsp __dead static void usage_tree(void);
97 6bad629b 2019-02-04 stsp __dead static void usage_status(void);
98 d0eebce4 2019-03-11 stsp __dead static void usage_ref(void);
99 4e759de4 2019-06-26 stsp __dead static void usage_branch(void);
100 8e7bd50a 2019-08-22 stsp __dead static void usage_tag(void);
101 d00136be 2019-03-26 stsp __dead static void usage_add(void);
102 648e4ef7 2019-07-09 stsp __dead static void usage_remove(void);
103 a129376b 2019-03-28 stsp __dead static void usage_revert(void);
104 c4296144 2019-05-09 stsp __dead static void usage_commit(void);
105 234035bc 2019-06-01 stsp __dead static void usage_cherrypick(void);
106 5ef14e63 2019-06-02 stsp __dead static void usage_backout(void);
107 818c7501 2019-07-11 stsp __dead static void usage_rebase(void);
108 0ebf8283 2019-07-24 stsp __dead static void usage_histedit(void);
109 2822a352 2019-10-15 stsp __dead static void usage_integrate(void);
110 715dc77e 2019-08-03 stsp __dead static void usage_stage(void);
111 ad493afc 2019-08-03 stsp __dead static void usage_unstage(void);
112 01073a5d 2019-08-22 stsp __dead static void usage_cat(void);
113 b2118c49 2020-07-28 stsp __dead static void usage_info(void);
115 2c7829a4 2019-06-17 stsp static const struct got_error* cmd_init(int, char *[]);
116 3ce1b845 2019-07-15 stsp static const struct got_error* cmd_import(int, char *[]);
117 93658fb9 2020-03-18 stsp static const struct got_error* cmd_clone(int, char *[]);
118 7848a0e1 2020-03-19 stsp static const struct got_error* cmd_fetch(int, char *[]);
119 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_checkout(int, char *[]);
120 507dc3bb 2018-12-29 stsp static const struct got_error* cmd_update(int, char *[]);
121 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_log(int, char *[]);
122 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_diff(int, char *[]);
123 404c43c4 2018-06-21 stsp static const struct got_error* cmd_blame(int, char *[]);
124 5de5890b 2018-10-18 stsp static const struct got_error* cmd_tree(int, char *[]);
125 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_status(int, char *[]);
126 d0eebce4 2019-03-11 stsp static const struct got_error* cmd_ref(int, char *[]);
127 4e759de4 2019-06-26 stsp static const struct got_error* cmd_branch(int, char *[]);
128 8e7bd50a 2019-08-22 stsp static const struct got_error* cmd_tag(int, char *[]);
129 d00136be 2019-03-26 stsp static const struct got_error* cmd_add(int, char *[]);
130 648e4ef7 2019-07-09 stsp static const struct got_error* cmd_remove(int, char *[]);
131 a129376b 2019-03-28 stsp static const struct got_error* cmd_revert(int, char *[]);
132 c4296144 2019-05-09 stsp static const struct got_error* cmd_commit(int, char *[]);
133 234035bc 2019-06-01 stsp static const struct got_error* cmd_cherrypick(int, char *[]);
134 5ef14e63 2019-06-02 stsp static const struct got_error* cmd_backout(int, char *[]);
135 818c7501 2019-07-11 stsp static const struct got_error* cmd_rebase(int, char *[]);
136 0ebf8283 2019-07-24 stsp static const struct got_error* cmd_histedit(int, char *[]);
137 2822a352 2019-10-15 stsp static const struct got_error* cmd_integrate(int, char *[]);
138 715dc77e 2019-08-03 stsp static const struct got_error* cmd_stage(int, char *[]);
139 ad493afc 2019-08-03 stsp static const struct got_error* cmd_unstage(int, char *[]);
140 01073a5d 2019-08-22 stsp static const struct got_error* cmd_cat(int, char *[]);
141 b2118c49 2020-07-28 stsp static const struct got_error* cmd_info(int, char *[]);
143 8cfb4057 2019-07-09 stsp static struct got_cmd got_commands[] = {
144 b2118c49 2020-07-28 stsp { "init", cmd_init, usage_init, "" },
145 bc26cce8 2019-08-04 stsp { "import", cmd_import, usage_import, "im" },
146 93658fb9 2020-03-18 stsp { "clone", cmd_clone, usage_clone, "cl" },
147 7848a0e1 2020-03-19 stsp { "fetch", cmd_fetch, usage_fetch, "fe" },
148 2ab43947 2020-03-18 stsp { "checkout", cmd_checkout, usage_checkout, "co" },
149 97b3a7be 2019-07-09 stsp { "update", cmd_update, usage_update, "up" },
150 97b3a7be 2019-07-09 stsp { "log", cmd_log, usage_log, "" },
151 bc26cce8 2019-08-04 stsp { "diff", cmd_diff, usage_diff, "di" },
152 bc26cce8 2019-08-04 stsp { "blame", cmd_blame, usage_blame, "bl" },
153 bc26cce8 2019-08-04 stsp { "tree", cmd_tree, usage_tree, "tr" },
154 97b3a7be 2019-07-09 stsp { "status", cmd_status, usage_status, "st" },
155 97b3a7be 2019-07-09 stsp { "ref", cmd_ref, usage_ref, "" },
156 97b3a7be 2019-07-09 stsp { "branch", cmd_branch, usage_branch, "br" },
157 8e7bd50a 2019-08-22 stsp { "tag", cmd_tag, usage_tag, "" },
158 97b3a7be 2019-07-09 stsp { "add", cmd_add, usage_add, "" },
159 648e4ef7 2019-07-09 stsp { "remove", cmd_remove, usage_remove, "rm" },
160 97b3a7be 2019-07-09 stsp { "revert", cmd_revert, usage_revert, "rv" },
161 97b3a7be 2019-07-09 stsp { "commit", cmd_commit, usage_commit, "ci" },
162 016477fd 2019-07-09 stsp { "cherrypick", cmd_cherrypick, usage_cherrypick, "cy" },
163 97b3a7be 2019-07-09 stsp { "backout", cmd_backout, usage_backout, "bo" },
164 818c7501 2019-07-11 stsp { "rebase", cmd_rebase, usage_rebase, "rb" },
165 0ebf8283 2019-07-24 stsp { "histedit", cmd_histedit, usage_histedit, "he" },
166 2822a352 2019-10-15 stsp { "integrate", cmd_integrate, usage_integrate,"ig" },
167 715dc77e 2019-08-03 stsp { "stage", cmd_stage, usage_stage, "sg" },
168 ad493afc 2019-08-03 stsp { "unstage", cmd_unstage, usage_unstage, "ug" },
169 01073a5d 2019-08-22 stsp { "cat", cmd_cat, usage_cat, "" },
170 b2118c49 2020-07-28 stsp { "info", cmd_info, usage_info, "" },
173 ce5b7c56 2019-07-09 stsp static void
174 6879ba42 2020-10-01 naddy list_commands(FILE *fp)
178 6879ba42 2020-10-01 naddy fprintf(fp, "commands:");
179 ce5b7c56 2019-07-09 stsp for (i = 0; i < nitems(got_commands); i++) {
180 ce5b7c56 2019-07-09 stsp struct got_cmd *cmd = &got_commands[i];
181 6879ba42 2020-10-01 naddy fprintf(fp, " %s", cmd->cmd_name);
183 6879ba42 2020-10-01 naddy fputc('\n', fp);
187 5c860e29 2018-03-12 stsp main(int argc, char *argv[])
189 8cfb4057 2019-07-09 stsp struct got_cmd *cmd;
190 5c860e29 2018-03-12 stsp unsigned int i;
192 53ccebc2 2019-07-30 stsp int hflag = 0, Vflag = 0;
193 83cd27f8 2020-01-13 stsp static struct option longopts[] = {
194 83cd27f8 2020-01-13 stsp { "version", no_argument, NULL, 'V' },
195 820059fa 2020-09-25 stsp { NULL, 0, NULL, 0 }
198 289e3cbf 2019-02-04 stsp setlocale(LC_CTYPE, "");
200 6586ea88 2020-01-13 stsp while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
201 5c860e29 2018-03-12 stsp switch (ch) {
209 6879ba42 2020-10-01 naddy usage(hflag, 1);
210 5c860e29 2018-03-12 stsp /* NOTREACHED */
214 5c860e29 2018-03-12 stsp argc -= optind;
215 5c860e29 2018-03-12 stsp argv += optind;
216 9814e6a3 2020-09-27 naddy optind = 1;
217 9814e6a3 2020-09-27 naddy optreset = 1;
219 53ccebc2 2019-07-30 stsp if (Vflag) {
220 53ccebc2 2019-07-30 stsp got_version_print_str();
224 5c860e29 2018-03-12 stsp if (argc <= 0)
225 6879ba42 2020-10-01 naddy usage(hflag, hflag ? 0 : 1);
227 99437157 2018-11-11 stsp signal(SIGINT, catch_sigint);
228 99437157 2018-11-11 stsp signal(SIGPIPE, catch_sigpipe);
230 5c860e29 2018-03-12 stsp for (i = 0; i < nitems(got_commands); i++) {
231 d7d4f210 2018-03-12 stsp const struct got_error *error;
233 5c860e29 2018-03-12 stsp cmd = &got_commands[i];
235 97b3a7be 2019-07-09 stsp if (strcmp(cmd->cmd_name, argv[0]) != 0 &&
236 97b3a7be 2019-07-09 stsp strcmp(cmd->cmd_alias, argv[0]) != 0)
240 1b6b95a8 2018-03-12 stsp got_commands[i].cmd_usage();
242 d7d4f210 2018-03-12 stsp error = got_commands[i].cmd_main(argc, argv);
243 f8afbdc8 2019-11-08 stsp if (error && error->code != GOT_ERR_CANCELLED &&
244 f8afbdc8 2019-11-08 stsp error->code != GOT_ERR_PRIVSEP_EXIT &&
245 f8afbdc8 2019-11-08 stsp !(sigpipe_received &&
246 70015d7a 2019-11-08 stsp error->code == GOT_ERR_ERRNO && errno == EPIPE) &&
247 70015d7a 2019-11-08 stsp !(sigint_received &&
248 70015d7a 2019-11-08 stsp error->code == GOT_ERR_ERRNO && errno == EINTR)) {
249 d7d4f210 2018-03-12 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
256 20ecf764 2018-03-12 stsp fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
257 6879ba42 2020-10-01 naddy list_commands(stderr);
261 4ed7e80c 2018-05-20 stsp __dead static void
262 6879ba42 2020-10-01 naddy usage(int hflag, int status)
264 6879ba42 2020-10-01 naddy FILE *fp = (status == 0) ? stdout : stderr;
266 6879ba42 2020-10-01 naddy fprintf(fp, "usage: %s [-h] [-V | --version] command [arg ...]\n",
267 53ccebc2 2019-07-30 stsp getprogname());
269 6879ba42 2020-10-01 naddy list_commands(fp);
270 6879ba42 2020-10-01 naddy exit(status);
273 0266afb7 2019-01-04 stsp static const struct got_error *
274 0ee7065d 2019-05-13 stsp get_editor(char **abspath)
276 0ee7065d 2019-05-13 stsp const struct got_error *err = NULL;
277 e2ba3d07 2019-05-13 stsp const char *editor;
279 8920fa04 2019-08-18 stsp *abspath = NULL;
281 e2ba3d07 2019-05-13 stsp editor = getenv("VISUAL");
282 e2ba3d07 2019-05-13 stsp if (editor == NULL)
283 e2ba3d07 2019-05-13 stsp editor = getenv("EDITOR");
285 0ee7065d 2019-05-13 stsp if (editor) {
286 0ee7065d 2019-05-13 stsp err = got_path_find_prog(abspath, editor);
288 0ee7065d 2019-05-13 stsp return err;
291 0ee7065d 2019-05-13 stsp if (*abspath == NULL) {
292 0ee7065d 2019-05-13 stsp *abspath = strdup("/bin/ed");
293 0ee7065d 2019-05-13 stsp if (*abspath == NULL)
294 0ee7065d 2019-05-13 stsp return got_error_from_errno("strdup");
297 e2ba3d07 2019-05-13 stsp return NULL;
300 e2ba3d07 2019-05-13 stsp static const struct got_error *
301 d0eebce4 2019-03-11 stsp apply_unveil(const char *repo_path, int repo_read_only,
302 c530dc23 2019-07-23 stsp const char *worktree_path)
304 163ce85a 2019-05-13 stsp const struct got_error *err;
306 37c06ea4 2019-07-15 stsp #ifdef PROFILE
307 37c06ea4 2019-07-15 stsp if (unveil("gmon.out", "rwc") != 0)
308 37c06ea4 2019-07-15 stsp return got_error_from_errno2("unveil", "gmon.out");
310 d0eebce4 2019-03-11 stsp if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
311 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", repo_path);
313 0266afb7 2019-01-04 stsp if (worktree_path && unveil(worktree_path, "rwc") != 0)
314 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", worktree_path);
316 bb63914a 2020-02-17 stsp if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
317 bb63914a 2020-02-17 stsp return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
319 163ce85a 2019-05-13 stsp err = got_privsep_unveil_exec_helpers();
320 163ce85a 2019-05-13 stsp if (err != NULL)
321 163ce85a 2019-05-13 stsp return err;
323 0266afb7 2019-01-04 stsp if (unveil(NULL, NULL) != 0)
324 638f9024 2019-05-13 stsp return got_error_from_errno("unveil");
326 0266afb7 2019-01-04 stsp return NULL;
329 2c7829a4 2019-06-17 stsp __dead static void
330 2c7829a4 2019-06-17 stsp usage_init(void)
332 09ea71ba 2019-07-27 stsp fprintf(stderr, "usage: %s init repository-path\n", getprogname());
336 2c7829a4 2019-06-17 stsp static const struct got_error *
337 2c7829a4 2019-06-17 stsp cmd_init(int argc, char *argv[])
339 2c7829a4 2019-06-17 stsp const struct got_error *error = NULL;
340 2c7829a4 2019-06-17 stsp char *repo_path = NULL;
343 2c7829a4 2019-06-17 stsp while ((ch = getopt(argc, argv, "")) != -1) {
344 2c7829a4 2019-06-17 stsp switch (ch) {
346 2c7829a4 2019-06-17 stsp usage_init();
347 2c7829a4 2019-06-17 stsp /* NOTREACHED */
351 2c7829a4 2019-06-17 stsp argc -= optind;
352 2c7829a4 2019-06-17 stsp argv += optind;
354 2c7829a4 2019-06-17 stsp #ifndef PROFILE
355 2c7829a4 2019-06-17 stsp if (pledge("stdio rpath wpath cpath unveil", NULL) == -1)
356 2c7829a4 2019-06-17 stsp err(1, "pledge");
358 2c7829a4 2019-06-17 stsp if (argc != 1)
359 bc20e173 2019-06-17 stsp usage_init();
361 2c7829a4 2019-06-17 stsp repo_path = strdup(argv[0]);
362 2c7829a4 2019-06-17 stsp if (repo_path == NULL)
363 2c7829a4 2019-06-17 stsp return got_error_from_errno("strdup");
365 2c7829a4 2019-06-17 stsp got_path_strip_trailing_slashes(repo_path);
367 2c7829a4 2019-06-17 stsp error = got_path_mkdir(repo_path);
368 2c7829a4 2019-06-17 stsp if (error &&
369 2c7829a4 2019-06-17 stsp !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
372 c530dc23 2019-07-23 stsp error = apply_unveil(repo_path, 0, NULL);
376 2c7829a4 2019-06-17 stsp error = got_repo_init(repo_path);
378 3ce1b845 2019-07-15 stsp free(repo_path);
379 3ce1b845 2019-07-15 stsp return error;
382 3ce1b845 2019-07-15 stsp __dead static void
383 3ce1b845 2019-07-15 stsp usage_import(void)
385 3ce1b845 2019-07-15 stsp fprintf(stderr, "usage: %s import [-b branch] [-m message] "
386 3ce1b845 2019-07-15 stsp "[-r repository-path] [-I pattern] path\n", getprogname());
391 3ce1b845 2019-07-15 stsp spawn_editor(const char *editor, const char *file)
394 3ce1b845 2019-07-15 stsp sig_t sighup, sigint, sigquit;
395 3ce1b845 2019-07-15 stsp int st = -1;
397 3ce1b845 2019-07-15 stsp sighup = signal(SIGHUP, SIG_IGN);
398 3ce1b845 2019-07-15 stsp sigint = signal(SIGINT, SIG_IGN);
399 3ce1b845 2019-07-15 stsp sigquit = signal(SIGQUIT, SIG_IGN);
401 3ce1b845 2019-07-15 stsp switch (pid = fork()) {
403 3ce1b845 2019-07-15 stsp goto doneediting;
405 3ce1b845 2019-07-15 stsp execl(editor, editor, file, (char *)NULL);
406 3ce1b845 2019-07-15 stsp _exit(127);
409 3ce1b845 2019-07-15 stsp while (waitpid(pid, &st, 0) == -1)
410 3ce1b845 2019-07-15 stsp if (errno != EINTR)
413 3ce1b845 2019-07-15 stsp doneediting:
414 3ce1b845 2019-07-15 stsp (void)signal(SIGHUP, sighup);
415 3ce1b845 2019-07-15 stsp (void)signal(SIGINT, sigint);
416 3ce1b845 2019-07-15 stsp (void)signal(SIGQUIT, sigquit);
418 3ce1b845 2019-07-15 stsp if (!WIFEXITED(st)) {
419 3ce1b845 2019-07-15 stsp errno = EINTR;
423 3ce1b845 2019-07-15 stsp return WEXITSTATUS(st);
426 3ce1b845 2019-07-15 stsp static const struct got_error *
427 3ce1b845 2019-07-15 stsp edit_logmsg(char **logmsg, const char *editor, const char *logmsg_path,
428 bfa12d5e 2020-09-26 stsp const char *initial_content, size_t initial_content_len)
430 3ce1b845 2019-07-15 stsp const struct got_error *err = NULL;
431 bfa12d5e 2020-09-26 stsp char *line = NULL;
432 bfa12d5e 2020-09-26 stsp size_t linesize = 0;
433 bfa12d5e 2020-09-26 stsp ssize_t linelen;
434 3ce1b845 2019-07-15 stsp struct stat st, st2;
435 bfa12d5e 2020-09-26 stsp FILE *fp = NULL;
436 bfa12d5e 2020-09-26 stsp size_t len, logmsg_len;
437 bfa12d5e 2020-09-26 stsp char *initial_content_stripped = NULL, *buf = NULL, *s;
439 3ce1b845 2019-07-15 stsp *logmsg = NULL;
441 3ce1b845 2019-07-15 stsp if (stat(logmsg_path, &st) == -1)
442 3ce1b845 2019-07-15 stsp return got_error_from_errno2("stat", logmsg_path);
444 3ce1b845 2019-07-15 stsp if (spawn_editor(editor, logmsg_path) == -1)
445 3ce1b845 2019-07-15 stsp return got_error_from_errno("failed spawning editor");
447 3ce1b845 2019-07-15 stsp if (stat(logmsg_path, &st2) == -1)
448 3ce1b845 2019-07-15 stsp return got_error_from_errno("stat");
450 3ce1b845 2019-07-15 stsp if (st.st_mtime == st2.st_mtime && st.st_size == st2.st_size)
451 3ce1b845 2019-07-15 stsp return got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
452 3ce1b845 2019-07-15 stsp "no changes made to commit message, aborting");
455 bfa12d5e 2020-09-26 stsp * Set up a stripped version of the initial content without comments
456 bfa12d5e 2020-09-26 stsp * and blank lines. We need this in order to check if the message
457 bfa12d5e 2020-09-26 stsp * has in fact been edited.
459 bfa12d5e 2020-09-26 stsp initial_content_stripped = malloc(initial_content_len + 1);
460 bfa12d5e 2020-09-26 stsp if (initial_content_stripped == NULL)
461 bfa12d5e 2020-09-26 stsp return got_error_from_errno("malloc");
462 bfa12d5e 2020-09-26 stsp initial_content_stripped[0] = '\0';
464 bfa12d5e 2020-09-26 stsp buf = strdup(initial_content);
465 bfa12d5e 2020-09-26 stsp if (buf == NULL) {
466 bfa12d5e 2020-09-26 stsp err = got_error_from_errno("strdup");
471 bfa12d5e 2020-09-26 stsp while ((line = strsep(&s, "\n")) != NULL) {
472 bfa12d5e 2020-09-26 stsp if ((line[0] == '#' || (len == 0 && line[0] == '\n')))
473 bfa12d5e 2020-09-26 stsp continue; /* remove comments and leading empty lines */
474 bfa12d5e 2020-09-26 stsp len = strlcat(initial_content_stripped, line,
475 bfa12d5e 2020-09-26 stsp initial_content_len + 1);
476 bfa12d5e 2020-09-26 stsp if (len >= initial_content_len + 1) {
477 bfa12d5e 2020-09-26 stsp err = got_error(GOT_ERR_NO_SPACE);
481 bfa12d5e 2020-09-26 stsp while (len > 0 && initial_content_stripped[len - 1] == '\n') {
482 bfa12d5e 2020-09-26 stsp initial_content_stripped[len - 1] = '\0';
486 bfa12d5e 2020-09-26 stsp logmsg_len = st2.st_size;
487 bfa12d5e 2020-09-26 stsp *logmsg = malloc(logmsg_len + 1);
488 3ce1b845 2019-07-15 stsp if (*logmsg == NULL)
489 3ce1b845 2019-07-15 stsp return got_error_from_errno("malloc");
490 3ce1b845 2019-07-15 stsp (*logmsg)[0] = '\0';
492 3ce1b845 2019-07-15 stsp fp = fopen(logmsg_path, "r");
493 3ce1b845 2019-07-15 stsp if (fp == NULL) {
494 3ce1b845 2019-07-15 stsp err = got_error_from_errno("fopen");
499 bfa12d5e 2020-09-26 stsp while ((linelen = getline(&line, &linesize, fp)) != -1) {
500 bfa12d5e 2020-09-26 stsp if ((line[0] == '#' || (len == 0 && line[0] == '\n')))
501 3ce1b845 2019-07-15 stsp continue; /* remove comments and leading empty lines */
502 bfa12d5e 2020-09-26 stsp len = strlcat(*logmsg, line, logmsg_len + 1);
503 bfa12d5e 2020-09-26 stsp if (len >= logmsg_len + 1) {
504 bfa12d5e 2020-09-26 stsp err = got_error(GOT_ERR_NO_SPACE);
508 bfa12d5e 2020-09-26 stsp free(line);
509 bfa12d5e 2020-09-26 stsp if (ferror(fp)) {
510 bfa12d5e 2020-09-26 stsp err = got_ferror(fp, GOT_ERR_IO);
513 3ce1b845 2019-07-15 stsp while (len > 0 && (*logmsg)[len - 1] == '\n') {
514 3ce1b845 2019-07-15 stsp (*logmsg)[len - 1] = '\0';
518 bfa12d5e 2020-09-26 stsp if (len == 0) {
519 3ce1b845 2019-07-15 stsp err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
520 3ce1b845 2019-07-15 stsp "commit message cannot be empty, aborting");
523 bfa12d5e 2020-09-26 stsp if (strcmp(*logmsg, initial_content_stripped) == 0)
524 bfa12d5e 2020-09-26 stsp err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
525 bfa12d5e 2020-09-26 stsp "no changes made to commit message, aborting");
527 bfa12d5e 2020-09-26 stsp free(initial_content_stripped);
529 bfa12d5e 2020-09-26 stsp if (fp && fclose(fp) == EOF && err == NULL)
530 bfa12d5e 2020-09-26 stsp err = got_error_from_errno("fclose");
532 3ce1b845 2019-07-15 stsp free(*logmsg);
533 3ce1b845 2019-07-15 stsp *logmsg = NULL;
535 3ce1b845 2019-07-15 stsp return err;
538 3ce1b845 2019-07-15 stsp static const struct got_error *
539 ef293bdd 2019-10-21 stsp collect_import_msg(char **logmsg, char **logmsg_path, const char *editor,
540 ef293bdd 2019-10-21 stsp const char *path_dir, const char *branch_name)
542 ef293bdd 2019-10-21 stsp char *initial_content = NULL;
543 3ce1b845 2019-07-15 stsp const struct got_error *err = NULL;
544 1601cb9f 2020-09-11 naddy int initial_content_len;
545 97972933 2020-09-11 stsp int fd = -1;
547 1601cb9f 2020-09-11 naddy initial_content_len = asprintf(&initial_content,
548 3ce1b845 2019-07-15 stsp "\n# %s to be imported to branch %s\n", path_dir,
549 1601cb9f 2020-09-11 naddy branch_name);
550 1601cb9f 2020-09-11 naddy if (initial_content_len == -1)
551 3ce1b845 2019-07-15 stsp return got_error_from_errno("asprintf");
553 bb63914a 2020-02-17 stsp err = got_opentemp_named_fd(logmsg_path, &fd,
554 bb63914a 2020-02-17 stsp GOT_TMPDIR_STR "/got-importmsg");
558 97972933 2020-09-11 stsp if (write(fd, initial_content, initial_content_len) == -1) {
559 97972933 2020-09-11 stsp err = got_error_from_errno2("write", *logmsg_path);
563 bfa12d5e 2020-09-26 stsp err = edit_logmsg(logmsg, editor, *logmsg_path, initial_content,
564 bfa12d5e 2020-09-26 stsp initial_content_len);
566 97972933 2020-09-11 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
567 97972933 2020-09-11 stsp err = got_error_from_errno2("close", *logmsg_path);
568 3ce1b845 2019-07-15 stsp free(initial_content);
570 59f86c76 2020-09-11 stsp free(*logmsg_path);
571 59f86c76 2020-09-11 stsp *logmsg_path = NULL;
573 3ce1b845 2019-07-15 stsp return err;
576 3ce1b845 2019-07-15 stsp static const struct got_error *
577 3ce1b845 2019-07-15 stsp import_progress(void *arg, const char *path)
579 3ce1b845 2019-07-15 stsp printf("A %s\n", path);
580 3ce1b845 2019-07-15 stsp return NULL;
583 3ce1b845 2019-07-15 stsp static const struct got_error *
584 50b0790e 2020-09-11 stsp get_author(char **author, struct got_repository *repo,
585 50b0790e 2020-09-11 stsp struct got_worktree *worktree)
587 aba9c984 2019-09-08 stsp const struct got_error *err = NULL;
588 50b0790e 2020-09-11 stsp const char *got_author = NULL, *name, *email;
589 50b0790e 2020-09-11 stsp const struct got_gotconfig *worktree_conf = NULL, *repo_conf = NULL;
591 84792843 2019-08-09 stsp *author = NULL;
593 50b0790e 2020-09-11 stsp if (worktree)
594 50b0790e 2020-09-11 stsp worktree_conf = got_worktree_get_gotconfig(worktree);
595 50b0790e 2020-09-11 stsp repo_conf = got_repo_get_gotconfig(repo);
598 50b0790e 2020-09-11 stsp * Priority of potential author information sources, from most
599 50b0790e 2020-09-11 stsp * significant to least significant:
600 50b0790e 2020-09-11 stsp * 1) work tree's .got/got.conf file
601 50b0790e 2020-09-11 stsp * 2) repository's got.conf file
602 50b0790e 2020-09-11 stsp * 3) repository's git config file
603 50b0790e 2020-09-11 stsp * 4) environment variables
604 50b0790e 2020-09-11 stsp * 5) global git config files (in user's home directory or /etc)
607 50b0790e 2020-09-11 stsp if (worktree_conf)
608 50b0790e 2020-09-11 stsp got_author = got_gotconfig_get_author(worktree_conf);
609 50b0790e 2020-09-11 stsp if (got_author == NULL)
610 50b0790e 2020-09-11 stsp got_author = got_gotconfig_get_author(repo_conf);
611 84792843 2019-08-09 stsp if (got_author == NULL) {
612 257add31 2020-09-09 stsp name = got_repo_get_gitconfig_author_name(repo);
613 257add31 2020-09-09 stsp email = got_repo_get_gitconfig_author_email(repo);
614 c9956ddf 2019-09-08 stsp if (name && email) {
615 c9956ddf 2019-09-08 stsp if (asprintf(author, "%s <%s>", name, email) == -1)
616 c9956ddf 2019-09-08 stsp return got_error_from_errno("asprintf");
617 c9956ddf 2019-09-08 stsp return NULL;
620 257add31 2020-09-09 stsp got_author = getenv("GOT_AUTHOR");
621 257add31 2020-09-09 stsp if (got_author == NULL) {
622 257add31 2020-09-09 stsp name = got_repo_get_global_gitconfig_author_name(repo);
623 257add31 2020-09-09 stsp email = got_repo_get_global_gitconfig_author_email(
625 257add31 2020-09-09 stsp if (name && email) {
626 257add31 2020-09-09 stsp if (asprintf(author, "%s <%s>", name, email)
628 257add31 2020-09-09 stsp return got_error_from_errno("asprintf");
629 257add31 2020-09-09 stsp return NULL;
631 257add31 2020-09-09 stsp /* TODO: Look up user in password database? */
632 257add31 2020-09-09 stsp return got_error(GOT_ERR_COMMIT_NO_AUTHOR);
636 aba9c984 2019-09-08 stsp *author = strdup(got_author);
637 aba9c984 2019-09-08 stsp if (*author == NULL)
638 aba9c984 2019-09-08 stsp return got_error_from_errno("strdup");
641 84792843 2019-08-09 stsp * Really dumb email address check; we're only doing this to
642 84792843 2019-08-09 stsp * avoid git's object parser breaking on commits we create.
644 84792843 2019-08-09 stsp while (*got_author && *got_author != '<')
645 84792843 2019-08-09 stsp got_author++;
646 aba9c984 2019-09-08 stsp if (*got_author != '<') {
647 aba9c984 2019-09-08 stsp err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
650 84792843 2019-08-09 stsp while (*got_author && *got_author != '@')
651 84792843 2019-08-09 stsp got_author++;
652 aba9c984 2019-09-08 stsp if (*got_author != '@') {
653 aba9c984 2019-09-08 stsp err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
656 84792843 2019-08-09 stsp while (*got_author && *got_author != '>')
657 84792843 2019-08-09 stsp got_author++;
658 84792843 2019-08-09 stsp if (*got_author != '>')
659 aba9c984 2019-09-08 stsp err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
662 aba9c984 2019-09-08 stsp free(*author);
663 aba9c984 2019-09-08 stsp *author = NULL;
665 aba9c984 2019-09-08 stsp return err;
668 c9956ddf 2019-09-08 stsp static const struct got_error *
669 c9956ddf 2019-09-08 stsp get_gitconfig_path(char **gitconfig_path)
671 c9956ddf 2019-09-08 stsp const char *homedir = getenv("HOME");
673 c9956ddf 2019-09-08 stsp *gitconfig_path = NULL;
674 c9956ddf 2019-09-08 stsp if (homedir) {
675 c9956ddf 2019-09-08 stsp if (asprintf(gitconfig_path, "%s/.gitconfig", homedir) == -1)
676 c9956ddf 2019-09-08 stsp return got_error_from_errno("asprintf");
679 c9956ddf 2019-09-08 stsp return NULL;
682 84792843 2019-08-09 stsp static const struct got_error *
683 3ce1b845 2019-07-15 stsp cmd_import(int argc, char *argv[])
685 3ce1b845 2019-07-15 stsp const struct got_error *error = NULL;
686 3ce1b845 2019-07-15 stsp char *path_dir = NULL, *repo_path = NULL, *logmsg = NULL;
687 c9956ddf 2019-09-08 stsp char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
688 5d67f40d 2019-11-08 stsp const char *branch_name = "main";
689 ef293bdd 2019-10-21 stsp char *refname = NULL, *id_str = NULL, *logmsg_path = NULL;
690 3ce1b845 2019-07-15 stsp struct got_repository *repo = NULL;
691 3ce1b845 2019-07-15 stsp struct got_reference *branch_ref = NULL, *head_ref = NULL;
692 3ce1b845 2019-07-15 stsp struct got_object_id *new_commit_id = NULL;
694 3ce1b845 2019-07-15 stsp struct got_pathlist_head ignores;
695 3ce1b845 2019-07-15 stsp struct got_pathlist_entry *pe;
696 ef293bdd 2019-10-21 stsp int preserve_logmsg = 0;
698 3ce1b845 2019-07-15 stsp TAILQ_INIT(&ignores);
700 3ce1b845 2019-07-15 stsp while ((ch = getopt(argc, argv, "b:m:r:I:")) != -1) {
701 3ce1b845 2019-07-15 stsp switch (ch) {
703 3ce1b845 2019-07-15 stsp branch_name = optarg;
706 3ce1b845 2019-07-15 stsp logmsg = strdup(optarg);
707 3ce1b845 2019-07-15 stsp if (logmsg == NULL) {
708 3ce1b845 2019-07-15 stsp error = got_error_from_errno("strdup");
713 3ce1b845 2019-07-15 stsp repo_path = realpath(optarg, NULL);
714 3ce1b845 2019-07-15 stsp if (repo_path == NULL) {
715 9ba1d308 2019-10-21 stsp error = got_error_from_errno2("realpath",
721 3ce1b845 2019-07-15 stsp if (optarg[0] == '\0')
723 3ce1b845 2019-07-15 stsp error = got_pathlist_insert(&pe, &ignores, optarg,
729 b2b65d18 2019-08-22 stsp usage_import();
730 3ce1b845 2019-07-15 stsp /* NOTREACHED */
734 3ce1b845 2019-07-15 stsp argc -= optind;
735 3ce1b845 2019-07-15 stsp argv += optind;
737 3ce1b845 2019-07-15 stsp #ifndef PROFILE
738 aba9c984 2019-09-08 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
740 3ce1b845 2019-07-15 stsp NULL) == -1)
741 3ce1b845 2019-07-15 stsp err(1, "pledge");
743 3ce1b845 2019-07-15 stsp if (argc != 1)
744 3ce1b845 2019-07-15 stsp usage_import();
746 3ce1b845 2019-07-15 stsp if (repo_path == NULL) {
747 3ce1b845 2019-07-15 stsp repo_path = getcwd(NULL, 0);
748 3ce1b845 2019-07-15 stsp if (repo_path == NULL)
749 3ce1b845 2019-07-15 stsp return got_error_from_errno("getcwd");
751 3ce1b845 2019-07-15 stsp got_path_strip_trailing_slashes(repo_path);
752 c9956ddf 2019-09-08 stsp error = get_gitconfig_path(&gitconfig_path);
755 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, gitconfig_path);
759 50b0790e 2020-09-11 stsp error = get_author(&author, repo, NULL);
761 aba9c984 2019-09-08 stsp return error;
764 bd5895f3 2019-11-28 stsp * Don't let the user create a branch name with a leading '-'.
765 e560b7e0 2019-11-28 stsp * While technically a valid reference name, this case is usually
766 e560b7e0 2019-11-28 stsp * an unintended typo.
768 bd5895f3 2019-11-28 stsp if (branch_name[0] == '-')
769 bd5895f3 2019-11-28 stsp return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
771 3ce1b845 2019-07-15 stsp if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
772 3ce1b845 2019-07-15 stsp error = got_error_from_errno("asprintf");
776 3ce1b845 2019-07-15 stsp error = got_ref_open(&branch_ref, repo, refname, 0);
777 3ce1b845 2019-07-15 stsp if (error) {
778 3ce1b845 2019-07-15 stsp if (error->code != GOT_ERR_NOT_REF)
781 3ce1b845 2019-07-15 stsp error = got_error_msg(GOT_ERR_BRANCH_EXISTS,
782 3ce1b845 2019-07-15 stsp "import target branch already exists");
786 3ce1b845 2019-07-15 stsp path_dir = realpath(argv[0], NULL);
787 3ce1b845 2019-07-15 stsp if (path_dir == NULL) {
788 9ba1d308 2019-10-21 stsp error = got_error_from_errno2("realpath", argv[0]);
791 3ce1b845 2019-07-15 stsp got_path_strip_trailing_slashes(path_dir);
794 3ce1b845 2019-07-15 stsp * unveil(2) traverses exec(2); if an editor is used we have
795 3ce1b845 2019-07-15 stsp * to apply unveil after the log message has been written.
797 3ce1b845 2019-07-15 stsp if (logmsg == NULL || strlen(logmsg) == 0) {
798 3ce1b845 2019-07-15 stsp error = get_editor(&editor);
801 8e158b01 2019-09-22 stsp free(logmsg);
802 ef293bdd 2019-10-21 stsp error = collect_import_msg(&logmsg, &logmsg_path, editor,
803 ef293bdd 2019-10-21 stsp path_dir, refname);
804 ef293bdd 2019-10-21 stsp if (error) {
805 ef293bdd 2019-10-21 stsp if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
806 ef293bdd 2019-10-21 stsp logmsg_path != NULL)
807 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
812 ef293bdd 2019-10-21 stsp if (unveil(path_dir, "r") != 0) {
813 ef293bdd 2019-10-21 stsp error = got_error_from_errno2("unveil", path_dir);
814 ef293bdd 2019-10-21 stsp if (logmsg_path)
815 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
819 ef293bdd 2019-10-21 stsp error = apply_unveil(got_repo_get_path(repo), 0, NULL);
820 ef293bdd 2019-10-21 stsp if (error) {
821 ef293bdd 2019-10-21 stsp if (logmsg_path)
822 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
826 3ce1b845 2019-07-15 stsp error = got_repo_import(&new_commit_id, path_dir, logmsg,
827 84792843 2019-08-09 stsp author, &ignores, repo, import_progress, NULL);
828 ef293bdd 2019-10-21 stsp if (error) {
829 ef293bdd 2019-10-21 stsp if (logmsg_path)
830 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
834 3ce1b845 2019-07-15 stsp error = got_ref_alloc(&branch_ref, refname, new_commit_id);
835 ef293bdd 2019-10-21 stsp if (error) {
836 ef293bdd 2019-10-21 stsp if (logmsg_path)
837 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
841 3ce1b845 2019-07-15 stsp error = got_ref_write(branch_ref, repo);
842 ef293bdd 2019-10-21 stsp if (error) {
843 ef293bdd 2019-10-21 stsp if (logmsg_path)
844 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
848 3ce1b845 2019-07-15 stsp error = got_object_id_str(&id_str, new_commit_id);
849 ef293bdd 2019-10-21 stsp if (error) {
850 ef293bdd 2019-10-21 stsp if (logmsg_path)
851 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
855 3ce1b845 2019-07-15 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
856 3ce1b845 2019-07-15 stsp if (error) {
857 ef293bdd 2019-10-21 stsp if (error->code != GOT_ERR_NOT_REF) {
858 ef293bdd 2019-10-21 stsp if (logmsg_path)
859 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
863 3ce1b845 2019-07-15 stsp error = got_ref_alloc_symref(&head_ref, GOT_REF_HEAD,
864 3ce1b845 2019-07-15 stsp branch_ref);
865 ef293bdd 2019-10-21 stsp if (error) {
866 ef293bdd 2019-10-21 stsp if (logmsg_path)
867 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
871 3ce1b845 2019-07-15 stsp error = got_ref_write(head_ref, repo);
872 ef293bdd 2019-10-21 stsp if (error) {
873 ef293bdd 2019-10-21 stsp if (logmsg_path)
874 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
879 3ce1b845 2019-07-15 stsp printf("Created branch %s with commit %s\n",
880 3ce1b845 2019-07-15 stsp got_ref_get_name(branch_ref), id_str);
882 ef293bdd 2019-10-21 stsp if (preserve_logmsg) {
883 ef293bdd 2019-10-21 stsp fprintf(stderr, "%s: log message preserved in %s\n",
884 ef293bdd 2019-10-21 stsp getprogname(), logmsg_path);
885 ef293bdd 2019-10-21 stsp } else if (logmsg_path && unlink(logmsg_path) == -1 && error == NULL)
886 ef293bdd 2019-10-21 stsp error = got_error_from_errno2("unlink", logmsg_path);
887 8e158b01 2019-09-22 stsp free(logmsg);
888 ef293bdd 2019-10-21 stsp free(logmsg_path);
889 2c7829a4 2019-06-17 stsp free(repo_path);
890 3ce1b845 2019-07-15 stsp free(editor);
891 3ce1b845 2019-07-15 stsp free(refname);
892 3ce1b845 2019-07-15 stsp free(new_commit_id);
893 3ce1b845 2019-07-15 stsp free(id_str);
894 aba9c984 2019-09-08 stsp free(author);
895 c9956ddf 2019-09-08 stsp free(gitconfig_path);
896 3ce1b845 2019-07-15 stsp if (branch_ref)
897 3ce1b845 2019-07-15 stsp got_ref_close(branch_ref);
898 3ce1b845 2019-07-15 stsp if (head_ref)
899 3ce1b845 2019-07-15 stsp got_ref_close(head_ref);
900 2c7829a4 2019-06-17 stsp return error;
903 93658fb9 2020-03-18 stsp __dead static void
904 93658fb9 2020-03-18 stsp usage_clone(void)
906 13f12b09 2020-03-21 stsp fprintf(stderr, "usage: %s clone [-a] [-b branch] [-l] [-m] [-q] [-v] "
907 0e4002ca 2020-03-21 stsp "[-R reference] repository-url [directory]\n", getprogname());
911 892ac3b6 2020-03-18 stsp struct got_fetch_progress_arg {
912 892ac3b6 2020-03-18 stsp char last_scaled_size[FMT_SCALED_STRSIZE];
913 892ac3b6 2020-03-18 stsp int last_p_indexed;
914 892ac3b6 2020-03-18 stsp int last_p_resolved;
915 68999b92 2020-03-18 stsp int verbosity;
917 04d9a9ec 2020-09-24 stsp struct got_repository *repo;
919 04d9a9ec 2020-09-24 stsp int create_configs;
920 04d9a9ec 2020-09-24 stsp int configs_created;
922 04d9a9ec 2020-09-24 stsp struct got_pathlist_head *symrefs;
923 04d9a9ec 2020-09-24 stsp struct got_pathlist_head *wanted_branches;
924 04d9a9ec 2020-09-24 stsp const char *proto;
925 04d9a9ec 2020-09-24 stsp const char *host;
926 04d9a9ec 2020-09-24 stsp const char *port;
927 04d9a9ec 2020-09-24 stsp const char *remote_repo_path;
928 04d9a9ec 2020-09-24 stsp const char *git_url;
929 04d9a9ec 2020-09-24 stsp int fetch_all_branches;
930 04d9a9ec 2020-09-24 stsp int mirror_references;
931 04d9a9ec 2020-09-24 stsp } config_info;
934 04d9a9ec 2020-09-24 stsp /* XXX forward declaration */
935 93658fb9 2020-03-18 stsp static const struct got_error *
936 04d9a9ec 2020-09-24 stsp create_config_files(const char *proto, const char *host, const char *port,
937 04d9a9ec 2020-09-24 stsp const char *remote_repo_path, const char *git_url, int fetch_all_branches,
938 04d9a9ec 2020-09-24 stsp int mirror_references, struct got_pathlist_head *symrefs,
939 04d9a9ec 2020-09-24 stsp struct got_pathlist_head *wanted_branches, struct got_repository *repo);
941 04d9a9ec 2020-09-24 stsp static const struct got_error *
942 baa9fea0 2020-03-18 stsp fetch_progress(void *arg, const char *message, off_t packfile_size,
943 668a20f6 2020-03-18 stsp int nobj_total, int nobj_indexed, int nobj_loose, int nobj_resolved)
945 04d9a9ec 2020-09-24 stsp const struct got_error *err = NULL;
946 892ac3b6 2020-03-18 stsp struct got_fetch_progress_arg *a = arg;
947 892ac3b6 2020-03-18 stsp char scaled_size[FMT_SCALED_STRSIZE];
948 892ac3b6 2020-03-18 stsp int p_indexed, p_resolved;
949 892ac3b6 2020-03-18 stsp int print_size = 0, print_indexed = 0, print_resolved = 0;
952 04d9a9ec 2020-09-24 stsp * In order to allow a failed clone to be resumed with 'got fetch'
953 04d9a9ec 2020-09-24 stsp * we try to create configuration files as soon as possible.
954 04d9a9ec 2020-09-24 stsp * Once the server has sent information about its default branch
955 04d9a9ec 2020-09-24 stsp * we have all required information.
957 04d9a9ec 2020-09-24 stsp if (a->create_configs && !a->configs_created &&
958 04d9a9ec 2020-09-24 stsp !TAILQ_EMPTY(a->config_info.symrefs)) {
959 04d9a9ec 2020-09-24 stsp err = create_config_files(a->config_info.proto,
960 04d9a9ec 2020-09-24 stsp a->config_info.host, a->config_info.port,
961 04d9a9ec 2020-09-24 stsp a->config_info.remote_repo_path,
962 04d9a9ec 2020-09-24 stsp a->config_info.git_url,
963 04d9a9ec 2020-09-24 stsp a->config_info.fetch_all_branches,
964 04d9a9ec 2020-09-24 stsp a->config_info.mirror_references,
965 04d9a9ec 2020-09-24 stsp a->config_info.symrefs,
966 04d9a9ec 2020-09-24 stsp a->config_info.wanted_branches, a->repo);
968 04d9a9ec 2020-09-24 stsp return err;
969 04d9a9ec 2020-09-24 stsp a->configs_created = 1;
972 68999b92 2020-03-18 stsp if (a->verbosity < 0)
973 68999b92 2020-03-18 stsp return NULL;
975 fd843b58 2020-03-18 stsp if (message && message[0] != '\0') {
976 d2cdc636 2020-03-18 stsp printf("\rserver: %s", message);
977 892ac3b6 2020-03-18 stsp fflush(stdout);
978 12d1281e 2020-03-19 stsp return NULL;
981 b2409d58 2020-03-18 stsp if (packfile_size > 0 || nobj_indexed > 0) {
982 892ac3b6 2020-03-18 stsp if (fmt_scaled(packfile_size, scaled_size) == 0 &&
983 892ac3b6 2020-03-18 stsp (a->last_scaled_size[0] == '\0' ||
984 892ac3b6 2020-03-18 stsp strcmp(scaled_size, a->last_scaled_size)) != 0) {
985 892ac3b6 2020-03-18 stsp print_size = 1;
986 892ac3b6 2020-03-18 stsp if (strlcpy(a->last_scaled_size, scaled_size,
987 892ac3b6 2020-03-18 stsp FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
988 892ac3b6 2020-03-18 stsp return got_error(GOT_ERR_NO_SPACE);
990 61cc1a7a 2020-03-18 stsp if (nobj_indexed > 0) {
991 892ac3b6 2020-03-18 stsp p_indexed = (nobj_indexed * 100) / nobj_total;
992 892ac3b6 2020-03-18 stsp if (p_indexed != a->last_p_indexed) {
993 892ac3b6 2020-03-18 stsp a->last_p_indexed = p_indexed;
994 892ac3b6 2020-03-18 stsp print_indexed = 1;
995 892ac3b6 2020-03-18 stsp print_size = 1;
998 61cc1a7a 2020-03-18 stsp if (nobj_resolved > 0) {
999 892ac3b6 2020-03-18 stsp p_resolved = (nobj_resolved * 100) /
1000 892ac3b6 2020-03-18 stsp (nobj_total - nobj_loose);
1001 892ac3b6 2020-03-18 stsp if (p_resolved != a->last_p_resolved) {
1002 892ac3b6 2020-03-18 stsp a->last_p_resolved = p_resolved;
1003 892ac3b6 2020-03-18 stsp print_resolved = 1;
1004 892ac3b6 2020-03-18 stsp print_indexed = 1;
1005 892ac3b6 2020-03-18 stsp print_size = 1;
1010 892ac3b6 2020-03-18 stsp if (print_size || print_indexed || print_resolved)
1011 892ac3b6 2020-03-18 stsp printf("\r");
1012 892ac3b6 2020-03-18 stsp if (print_size)
1013 892ac3b6 2020-03-18 stsp printf("%*s fetched", FMT_SCALED_STRSIZE, scaled_size);
1014 d715f13e 2020-03-19 stsp if (print_indexed)
1015 892ac3b6 2020-03-18 stsp printf("; indexing %d%%", p_indexed);
1016 d715f13e 2020-03-19 stsp if (print_resolved)
1017 892ac3b6 2020-03-18 stsp printf("; resolving deltas %d%%", p_resolved);
1018 892ac3b6 2020-03-18 stsp if (print_size || print_indexed || print_resolved)
1019 892ac3b6 2020-03-18 stsp fflush(stdout);
1021 531c3985 2020-03-18 stsp return NULL;
1024 531c3985 2020-03-18 stsp static const struct got_error *
1025 04d9a9ec 2020-09-24 stsp create_symref(const char *refname, struct got_reference *target_ref,
1026 04d9a9ec 2020-09-24 stsp int verbosity, struct got_repository *repo)
1028 4ba14133 2020-03-20 stsp const struct got_error *err;
1029 04d9a9ec 2020-09-24 stsp struct got_reference *head_symref;
1031 04d9a9ec 2020-09-24 stsp err = got_ref_alloc_symref(&head_symref, refname, target_ref);
1033 4ba14133 2020-03-20 stsp return err;
1035 04d9a9ec 2020-09-24 stsp err = got_ref_write(head_symref, repo);
1036 6338a6a1 2020-03-21 stsp if (err == NULL && verbosity > 0) {
1037 6338a6a1 2020-03-21 stsp printf("Created reference %s: %s\n", GOT_REF_HEAD,
1038 6338a6a1 2020-03-21 stsp got_ref_get_name(target_ref));
1040 04d9a9ec 2020-09-24 stsp got_ref_close(head_symref);
1041 4ba14133 2020-03-20 stsp return err;
1044 4ba14133 2020-03-20 stsp static const struct got_error *
1045 41b0de12 2020-03-21 stsp list_remote_refs(struct got_pathlist_head *symrefs,
1046 41b0de12 2020-03-21 stsp struct got_pathlist_head *refs)
1048 41b0de12 2020-03-21 stsp const struct got_error *err;
1049 41b0de12 2020-03-21 stsp struct got_pathlist_entry *pe;
1051 41b0de12 2020-03-21 stsp TAILQ_FOREACH(pe, symrefs, entry) {
1052 41b0de12 2020-03-21 stsp const char *refname = pe->path;
1053 41b0de12 2020-03-21 stsp const char *targetref = pe->data;
1055 41b0de12 2020-03-21 stsp printf("%s: %s\n", refname, targetref);
1058 41b0de12 2020-03-21 stsp TAILQ_FOREACH(pe, refs, entry) {
1059 41b0de12 2020-03-21 stsp const char *refname = pe->path;
1060 41b0de12 2020-03-21 stsp struct got_object_id *id = pe->data;
1061 41b0de12 2020-03-21 stsp char *id_str;
1063 41b0de12 2020-03-21 stsp err = got_object_id_str(&id_str, id);
1065 41b0de12 2020-03-21 stsp return err;
1066 41b0de12 2020-03-21 stsp printf("%s: %s\n", refname, id_str);
1067 41b0de12 2020-03-21 stsp free(id_str);
1070 41b0de12 2020-03-21 stsp return NULL;
1073 6338a6a1 2020-03-21 stsp static const struct got_error *
1074 6338a6a1 2020-03-21 stsp create_ref(const char *refname, struct got_object_id *id,
1075 6338a6a1 2020-03-21 stsp int verbosity, struct got_repository *repo)
1077 6338a6a1 2020-03-21 stsp const struct got_error *err = NULL;
1078 6338a6a1 2020-03-21 stsp struct got_reference *ref;
1079 6338a6a1 2020-03-21 stsp char *id_str;
1081 6338a6a1 2020-03-21 stsp err = got_object_id_str(&id_str, id);
1083 6338a6a1 2020-03-21 stsp return err;
1085 6338a6a1 2020-03-21 stsp err = got_ref_alloc(&ref, refname, id);
1087 6338a6a1 2020-03-21 stsp goto done;
1089 6338a6a1 2020-03-21 stsp err = got_ref_write(ref, repo);
1090 6338a6a1 2020-03-21 stsp got_ref_close(ref);
1092 6338a6a1 2020-03-21 stsp if (err == NULL && verbosity >= 0)
1093 6338a6a1 2020-03-21 stsp printf("Created reference %s: %s\n", refname, id_str);
1095 6338a6a1 2020-03-21 stsp free(id_str);
1096 6338a6a1 2020-03-21 stsp return err;
1099 0e4002ca 2020-03-21 stsp static int
1100 0e4002ca 2020-03-21 stsp match_wanted_ref(const char *refname, const char *wanted_ref)
1102 0e4002ca 2020-03-21 stsp if (strncmp(refname, "refs/", 5) != 0)
1104 0e4002ca 2020-03-21 stsp refname += 5;
1107 0e4002ca 2020-03-21 stsp * Prevent fetching of references that won't make any
1108 0e4002ca 2020-03-21 stsp * sense outside of the remote repository's context.
1110 0e4002ca 2020-03-21 stsp if (strncmp(refname, "got/", 4) == 0)
1112 0e4002ca 2020-03-21 stsp if (strncmp(refname, "remotes/", 8) == 0)
1115 0e4002ca 2020-03-21 stsp if (strncmp(wanted_ref, "refs/", 5) == 0)
1116 0e4002ca 2020-03-21 stsp wanted_ref += 5;
1118 0e4002ca 2020-03-21 stsp /* Allow prefix match. */
1119 0e4002ca 2020-03-21 stsp if (got_path_is_child(refname, wanted_ref, strlen(wanted_ref)))
1122 0e4002ca 2020-03-21 stsp /* Allow exact match. */
1123 0e4002ca 2020-03-21 stsp return (strcmp(refname, wanted_ref) == 0);
1126 0e4002ca 2020-03-21 stsp static int
1127 0e4002ca 2020-03-21 stsp is_wanted_ref(struct got_pathlist_head *wanted_refs, const char *refname)
1129 0e4002ca 2020-03-21 stsp struct got_pathlist_entry *pe;
1131 0e4002ca 2020-03-21 stsp TAILQ_FOREACH(pe, wanted_refs, entry) {
1132 0e4002ca 2020-03-21 stsp if (match_wanted_ref(refname, pe->path))
1139 41b0de12 2020-03-21 stsp static const struct got_error *
1140 0e4002ca 2020-03-21 stsp create_wanted_ref(const char *refname, struct got_object_id *id,
1141 0e4002ca 2020-03-21 stsp const char *remote_repo_name, int verbosity, struct got_repository *repo)
1143 0e4002ca 2020-03-21 stsp const struct got_error *err;
1144 0e4002ca 2020-03-21 stsp char *remote_refname;
1146 0e4002ca 2020-03-21 stsp if (strncmp("refs/", refname, 5) == 0)
1147 0e4002ca 2020-03-21 stsp refname += 5;
1149 0e4002ca 2020-03-21 stsp if (asprintf(&remote_refname, "refs/remotes/%s/%s",
1150 0e4002ca 2020-03-21 stsp remote_repo_name, refname) == -1)
1151 0e4002ca 2020-03-21 stsp return got_error_from_errno("asprintf");
1153 0e4002ca 2020-03-21 stsp err = create_ref(remote_refname, id, verbosity, repo);
1154 0e4002ca 2020-03-21 stsp free(remote_refname);
1155 7c0b7f42 2020-09-24 stsp return err;
1158 7c0b7f42 2020-09-24 stsp static const struct got_error *
1159 7c0b7f42 2020-09-24 stsp create_gotconfig(const char *proto, const char *host, const char *port,
1160 04d9a9ec 2020-09-24 stsp const char *remote_repo_path, int fetch_all_branches, int mirror_references,
1161 7c0b7f42 2020-09-24 stsp struct got_repository *repo)
1163 7c0b7f42 2020-09-24 stsp const struct got_error *err = NULL;
1164 7c0b7f42 2020-09-24 stsp char *gotconfig_path = NULL;
1165 7c0b7f42 2020-09-24 stsp char *gotconfig = NULL;
1166 7c0b7f42 2020-09-24 stsp FILE *gotconfig_file = NULL;
1167 7c0b7f42 2020-09-24 stsp ssize_t n;
1169 7c0b7f42 2020-09-24 stsp /* Create got.conf(5). */
1170 7c0b7f42 2020-09-24 stsp gotconfig_path = got_repo_get_path_gotconfig(repo);
1171 7c0b7f42 2020-09-24 stsp if (gotconfig_path == NULL) {
1172 7c0b7f42 2020-09-24 stsp err = got_error_from_errno("got_repo_get_path_gotconfig");
1173 7c0b7f42 2020-09-24 stsp goto done;
1175 7c0b7f42 2020-09-24 stsp gotconfig_file = fopen(gotconfig_path, "a");
1176 7c0b7f42 2020-09-24 stsp if (gotconfig_file == NULL) {
1177 7c0b7f42 2020-09-24 stsp err = got_error_from_errno2("fopen", gotconfig_path);
1178 7c0b7f42 2020-09-24 stsp goto done;
1180 7c0b7f42 2020-09-24 stsp if (asprintf(&gotconfig,
1181 7c0b7f42 2020-09-24 stsp "remote \"%s\" {\n"
1182 7c0b7f42 2020-09-24 stsp "\tserver %s\n"
1183 7c0b7f42 2020-09-24 stsp "\tprotocol %s\n"
1185 7c0b7f42 2020-09-24 stsp "\trepository \"%s\"\n"
1188 7c0b7f42 2020-09-24 stsp GOT_FETCH_DEFAULT_REMOTE_NAME, host, proto,
1189 7c0b7f42 2020-09-24 stsp port ? "\tport " : "", port ? port : "", port ? "\n" : "",
1190 7c0b7f42 2020-09-24 stsp remote_repo_path,
1191 7c0b7f42 2020-09-24 stsp mirror_references ? "\tmirror-references yes\n" : "") == -1) {
1192 7c0b7f42 2020-09-24 stsp err = got_error_from_errno("asprintf");
1193 7c0b7f42 2020-09-24 stsp goto done;
1195 7c0b7f42 2020-09-24 stsp n = fwrite(gotconfig, 1, strlen(gotconfig), gotconfig_file);
1196 7c0b7f42 2020-09-24 stsp if (n != strlen(gotconfig)) {
1197 7c0b7f42 2020-09-24 stsp err = got_ferror(gotconfig_file, GOT_ERR_IO);
1198 7c0b7f42 2020-09-24 stsp goto done;
1202 7c0b7f42 2020-09-24 stsp if (gotconfig_file && fclose(gotconfig_file) == EOF && err == NULL)
1203 7c0b7f42 2020-09-24 stsp err = got_error_from_errno2("fclose", gotconfig_path);
1204 7c0b7f42 2020-09-24 stsp free(gotconfig_path);
1205 7c0b7f42 2020-09-24 stsp return err;
1208 7c0b7f42 2020-09-24 stsp static const struct got_error *
1209 04d9a9ec 2020-09-24 stsp create_gitconfig(const char *git_url, const char *default_branch,
1210 7c0b7f42 2020-09-24 stsp int fetch_all_branches, int mirror_references, struct got_repository *repo)
1212 7c0b7f42 2020-09-24 stsp const struct got_error *err = NULL;
1213 7c0b7f42 2020-09-24 stsp char *gitconfig_path = NULL;
1214 7c0b7f42 2020-09-24 stsp char *gitconfig = NULL;
1215 7c0b7f42 2020-09-24 stsp FILE *gitconfig_file = NULL;
1216 7c0b7f42 2020-09-24 stsp ssize_t n;
1218 7c0b7f42 2020-09-24 stsp /* Create a config file Git can understand. */
1219 7c0b7f42 2020-09-24 stsp gitconfig_path = got_repo_get_path_gitconfig(repo);
1220 7c0b7f42 2020-09-24 stsp if (gitconfig_path == NULL) {
1221 7c0b7f42 2020-09-24 stsp err = got_error_from_errno("got_repo_get_path_gitconfig");
1222 7c0b7f42 2020-09-24 stsp goto done;
1224 7c0b7f42 2020-09-24 stsp gitconfig_file = fopen(gitconfig_path, "a");
1225 7c0b7f42 2020-09-24 stsp if (gitconfig_file == NULL) {
1226 7c0b7f42 2020-09-24 stsp err = got_error_from_errno2("fopen", gitconfig_path);
1227 7c0b7f42 2020-09-24 stsp goto done;
1229 7c0b7f42 2020-09-24 stsp if (mirror_references) {
1230 7c0b7f42 2020-09-24 stsp if (asprintf(&gitconfig,
1231 7c0b7f42 2020-09-24 stsp "[remote \"%s\"]\n"
1232 7c0b7f42 2020-09-24 stsp "\turl = %s\n"
1233 7c0b7f42 2020-09-24 stsp "\tfetch = +refs/*:refs/*\n"
1234 7c0b7f42 2020-09-24 stsp "\tmirror = true\n",
1235 7c0b7f42 2020-09-24 stsp GOT_FETCH_DEFAULT_REMOTE_NAME, git_url) == -1) {
1236 7c0b7f42 2020-09-24 stsp err = got_error_from_errno("asprintf");
1237 7c0b7f42 2020-09-24 stsp goto done;
1239 7c0b7f42 2020-09-24 stsp } else if (fetch_all_branches) {
1240 7c0b7f42 2020-09-24 stsp if (asprintf(&gitconfig,
1241 7c0b7f42 2020-09-24 stsp "[remote \"%s\"]\n"
1242 7c0b7f42 2020-09-24 stsp "\turl = %s\n"
1243 7c0b7f42 2020-09-24 stsp "\tfetch = +refs/heads/*:refs/remotes/%s/*\n",
1244 7c0b7f42 2020-09-24 stsp GOT_FETCH_DEFAULT_REMOTE_NAME, git_url,
1245 7c0b7f42 2020-09-24 stsp GOT_FETCH_DEFAULT_REMOTE_NAME) == -1) {
1246 7c0b7f42 2020-09-24 stsp err = got_error_from_errno("asprintf");
1247 7c0b7f42 2020-09-24 stsp goto done;
1250 7c0b7f42 2020-09-24 stsp const char *branchname;
1253 7c0b7f42 2020-09-24 stsp * If the server specified a default branch, use just that one.
1254 7c0b7f42 2020-09-24 stsp * Otherwise fall back to fetching all branches on next fetch.
1256 04d9a9ec 2020-09-24 stsp if (default_branch) {
1257 04d9a9ec 2020-09-24 stsp branchname = default_branch;
1258 7c0b7f42 2020-09-24 stsp if (strncmp(branchname, "refs/heads/", 11) == 0)
1259 7c0b7f42 2020-09-24 stsp branchname += 11;
1261 7c0b7f42 2020-09-24 stsp branchname = "*"; /* fall back to all branches */
1262 7c0b7f42 2020-09-24 stsp if (asprintf(&gitconfig,
1263 7c0b7f42 2020-09-24 stsp "[remote \"%s\"]\n"
1264 7c0b7f42 2020-09-24 stsp "\turl = %s\n"
1265 7c0b7f42 2020-09-24 stsp "\tfetch = +refs/heads/%s:refs/remotes/%s/%s\n",
1266 7c0b7f42 2020-09-24 stsp GOT_FETCH_DEFAULT_REMOTE_NAME, git_url,
1267 7c0b7f42 2020-09-24 stsp branchname, GOT_FETCH_DEFAULT_REMOTE_NAME,
1268 7c0b7f42 2020-09-24 stsp branchname) == -1) {
1269 7c0b7f42 2020-09-24 stsp err = got_error_from_errno("asprintf");
1270 7c0b7f42 2020-09-24 stsp goto done;
1273 7c0b7f42 2020-09-24 stsp n = fwrite(gitconfig, 1, strlen(gitconfig), gitconfig_file);
1274 7c0b7f42 2020-09-24 stsp if (n != strlen(gitconfig)) {
1275 7c0b7f42 2020-09-24 stsp err = got_ferror(gitconfig_file, GOT_ERR_IO);
1276 7c0b7f42 2020-09-24 stsp goto done;
1279 7c0b7f42 2020-09-24 stsp if (gitconfig_file && fclose(gitconfig_file) == EOF && err == NULL)
1280 7c0b7f42 2020-09-24 stsp err = got_error_from_errno2("fclose", gitconfig_path);
1281 7c0b7f42 2020-09-24 stsp free(gitconfig_path);
1282 0e4002ca 2020-03-21 stsp return err;
1285 0e4002ca 2020-03-21 stsp static const struct got_error *
1286 04d9a9ec 2020-09-24 stsp create_config_files(const char *proto, const char *host, const char *port,
1287 04d9a9ec 2020-09-24 stsp const char *remote_repo_path, const char *git_url, int fetch_all_branches,
1288 04d9a9ec 2020-09-24 stsp int mirror_references, struct got_pathlist_head *symrefs,
1289 04d9a9ec 2020-09-24 stsp struct got_pathlist_head *wanted_branches, struct got_repository *repo)
1291 04d9a9ec 2020-09-24 stsp const struct got_error *err = NULL;
1292 04d9a9ec 2020-09-24 stsp const char *default_branch = NULL;
1293 04d9a9ec 2020-09-24 stsp struct got_pathlist_entry *pe;
1296 04d9a9ec 2020-09-24 stsp * If we asked for a set of wanted branches then use the first
1297 04d9a9ec 2020-09-24 stsp * one of those.
1299 04d9a9ec 2020-09-24 stsp if (!TAILQ_EMPTY(wanted_branches)) {
1300 04d9a9ec 2020-09-24 stsp pe = TAILQ_FIRST(wanted_branches);
1301 04d9a9ec 2020-09-24 stsp default_branch = pe->path;
1303 04d9a9ec 2020-09-24 stsp /* First HEAD ref listed by server is the default branch. */
1304 04d9a9ec 2020-09-24 stsp TAILQ_FOREACH(pe, symrefs, entry) {
1305 04d9a9ec 2020-09-24 stsp const char *refname = pe->path;
1306 04d9a9ec 2020-09-24 stsp const char *target = pe->data;
1308 04d9a9ec 2020-09-24 stsp if (strcmp(refname, GOT_REF_HEAD) != 0)
1311 04d9a9ec 2020-09-24 stsp default_branch = target;
1316 04d9a9ec 2020-09-24 stsp /* Create got.conf(5). */
1317 04d9a9ec 2020-09-24 stsp err = create_gotconfig(proto, host, port, remote_repo_path,
1318 04d9a9ec 2020-09-24 stsp fetch_all_branches, mirror_references, repo);
1320 04d9a9ec 2020-09-24 stsp return err;
1322 04d9a9ec 2020-09-24 stsp /* Create a config file Git can understand. */
1323 04d9a9ec 2020-09-24 stsp return create_gitconfig(git_url, default_branch, fetch_all_branches,
1324 04d9a9ec 2020-09-24 stsp mirror_references, repo);
1327 04d9a9ec 2020-09-24 stsp static const struct got_error *
1328 93658fb9 2020-03-18 stsp cmd_clone(int argc, char *argv[])
1330 39c64a6a 2020-03-18 stsp const struct got_error *error = NULL;
1331 9df6f38b 2020-03-18 stsp const char *uri, *dirname;
1332 09838ffc 2020-03-18 stsp char *proto, *host, *port, *repo_name, *server_path;
1333 d9b4d0c0 2020-03-18 stsp char *default_destdir = NULL, *id_str = NULL;
1334 a9c2d4c2 2020-09-24 stsp const char *repo_path;
1335 bb64b798 2020-03-18 stsp struct got_repository *repo = NULL;
1336 0e4002ca 2020-03-21 stsp struct got_pathlist_head refs, symrefs, wanted_branches, wanted_refs;
1337 d9b4d0c0 2020-03-18 stsp struct got_pathlist_entry *pe;
1338 d9b4d0c0 2020-03-18 stsp struct got_object_id *pack_hash = NULL;
1339 9c52365f 2020-03-21 stsp int ch, fetchfd = -1, fetchstatus;
1340 9c52365f 2020-03-21 stsp pid_t fetchpid = -1;
1341 892ac3b6 2020-03-18 stsp struct got_fetch_progress_arg fpa;
1342 b46f3e71 2020-03-18 stsp char *git_url = NULL;
1343 659e7fbd 2020-03-20 stsp int verbosity = 0, fetch_all_branches = 0, mirror_references = 0;
1344 41b0de12 2020-03-21 stsp int list_refs_only = 0;
1346 d9b4d0c0 2020-03-18 stsp TAILQ_INIT(&refs);
1347 d9b4d0c0 2020-03-18 stsp TAILQ_INIT(&symrefs);
1348 4ba14133 2020-03-20 stsp TAILQ_INIT(&wanted_branches);
1349 0e4002ca 2020-03-21 stsp TAILQ_INIT(&wanted_refs);
1351 0e4002ca 2020-03-21 stsp while ((ch = getopt(argc, argv, "ab:lmvqR:")) != -1) {
1352 93658fb9 2020-03-18 stsp switch (ch) {
1354 659e7fbd 2020-03-20 stsp fetch_all_branches = 1;
1357 4ba14133 2020-03-20 stsp error = got_pathlist_append(&wanted_branches,
1358 4ba14133 2020-03-20 stsp optarg, NULL);
1359 4ba14133 2020-03-20 stsp if (error)
1360 4ba14133 2020-03-20 stsp return error;
1363 41b0de12 2020-03-21 stsp list_refs_only = 1;
1366 469dd726 2020-03-20 stsp mirror_references = 1;
1369 68999b92 2020-03-18 stsp if (verbosity < 0)
1370 68999b92 2020-03-18 stsp verbosity = 0;
1371 68999b92 2020-03-18 stsp else if (verbosity < 3)
1372 68999b92 2020-03-18 stsp verbosity++;
1375 68999b92 2020-03-18 stsp verbosity = -1;
1378 0e4002ca 2020-03-21 stsp error = got_pathlist_append(&wanted_refs,
1379 0e4002ca 2020-03-21 stsp optarg, NULL);
1380 0e4002ca 2020-03-21 stsp if (error)
1381 0e4002ca 2020-03-21 stsp return error;
1384 93658fb9 2020-03-18 stsp usage_clone();
1388 93658fb9 2020-03-18 stsp argc -= optind;
1389 93658fb9 2020-03-18 stsp argv += optind;
1391 4ba14133 2020-03-20 stsp if (fetch_all_branches && !TAILQ_EMPTY(&wanted_branches))
1392 41b0de12 2020-03-21 stsp errx(1, "-a and -b options are mutually exclusive");
1393 41b0de12 2020-03-21 stsp if (list_refs_only) {
1394 41b0de12 2020-03-21 stsp if (!TAILQ_EMPTY(&wanted_branches))
1395 41b0de12 2020-03-21 stsp errx(1, "-l and -b options are mutually exclusive");
1396 41b0de12 2020-03-21 stsp if (fetch_all_branches)
1397 41b0de12 2020-03-21 stsp errx(1, "-l and -a options are mutually exclusive");
1398 41b0de12 2020-03-21 stsp if (mirror_references)
1399 41b0de12 2020-03-21 stsp errx(1, "-l and -m options are mutually exclusive");
1400 41b0de12 2020-03-21 stsp if (verbosity == -1)
1401 41b0de12 2020-03-21 stsp errx(1, "-l and -q options are mutually exclusive");
1402 0e4002ca 2020-03-21 stsp if (!TAILQ_EMPTY(&wanted_refs))
1403 0e4002ca 2020-03-21 stsp errx(1, "-l and -R options are mutually exclusive");
1406 93658fb9 2020-03-18 stsp uri = argv[0];
1408 9df6f38b 2020-03-18 stsp if (argc == 1)
1409 93658fb9 2020-03-18 stsp dirname = NULL;
1410 9df6f38b 2020-03-18 stsp else if (argc == 2)
1411 93658fb9 2020-03-18 stsp dirname = argv[1];
1413 93658fb9 2020-03-18 stsp usage_clone();
1415 39c64a6a 2020-03-18 stsp error = got_fetch_parse_uri(&proto, &host, &port, &server_path,
1416 4dbec0a8 2020-08-27 stsp &repo_name, uri);
1417 39c64a6a 2020-03-18 stsp if (error)
1418 09f63084 2020-03-20 stsp goto done;
1420 09f63084 2020-03-20 stsp if (asprintf(&git_url, "%s://%s%s%s%s%s", proto,
1421 09f63084 2020-03-20 stsp host, port ? ":" : "", port ? port : "",
1422 09f63084 2020-03-20 stsp server_path[0] != '/' ? "/" : "", server_path) == -1) {
1423 09f63084 2020-03-20 stsp error = got_error_from_errno("asprintf");
1424 09838ffc 2020-03-18 stsp goto done;
1427 39c64a6a 2020-03-18 stsp if (strcmp(proto, "git") == 0) {
1428 b46f3e71 2020-03-18 stsp #ifndef PROFILE
1429 39c64a6a 2020-03-18 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1430 39c64a6a 2020-03-18 stsp "sendfd dns inet unveil", NULL) == -1)
1431 39c64a6a 2020-03-18 stsp err(1, "pledge");
1433 39c64a6a 2020-03-18 stsp } else if (strcmp(proto, "git+ssh") == 0 ||
1434 39c64a6a 2020-03-18 stsp strcmp(proto, "ssh") == 0) {
1435 b46f3e71 2020-03-18 stsp #ifndef PROFILE
1436 39c64a6a 2020-03-18 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1437 39c64a6a 2020-03-18 stsp "sendfd unveil", NULL) == -1)
1438 39c64a6a 2020-03-18 stsp err(1, "pledge");
1440 39c64a6a 2020-03-18 stsp } else if (strcmp(proto, "http") == 0 ||
1441 39c64a6a 2020-03-18 stsp strcmp(proto, "git+http") == 0) {
1442 39c64a6a 2020-03-18 stsp error = got_error_path(proto, GOT_ERR_NOT_IMPL);
1443 39c64a6a 2020-03-18 stsp goto done;
1445 39c64a6a 2020-03-18 stsp error = got_error_path(proto, GOT_ERR_BAD_PROTO);
1446 39c64a6a 2020-03-18 stsp goto done;
1448 bb64b798 2020-03-18 stsp if (dirname == NULL) {
1449 bb64b798 2020-03-18 stsp if (asprintf(&default_destdir, "%s.git", repo_name) == -1) {
1450 39c64a6a 2020-03-18 stsp error = got_error_from_errno("asprintf");
1451 bb64b798 2020-03-18 stsp goto done;
1453 bb64b798 2020-03-18 stsp repo_path = default_destdir;
1455 bb64b798 2020-03-18 stsp repo_path = dirname;
1457 41b0de12 2020-03-21 stsp if (!list_refs_only) {
1458 41b0de12 2020-03-21 stsp error = got_path_mkdir(repo_path);
1459 2751fe64 2020-09-24 stsp if (error &&
1460 2751fe64 2020-09-24 stsp (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
1461 2751fe64 2020-09-24 stsp !(error->code == GOT_ERR_ERRNO && errno == EEXIST)))
1462 41b0de12 2020-03-21 stsp goto done;
1463 2751fe64 2020-09-24 stsp if (!got_path_dir_is_empty(repo_path)) {
1464 2751fe64 2020-09-24 stsp error = got_error_path(repo_path,
1465 2751fe64 2020-09-24 stsp GOT_ERR_DIR_NOT_EMPTY);
1466 41b0de12 2020-03-21 stsp goto done;
1470 ee448f5f 2020-03-18 stsp if (strcmp(proto, "git+ssh") == 0 || strcmp(proto, "ssh") == 0) {
1471 ee448f5f 2020-03-18 stsp if (unveil(GOT_FETCH_PATH_SSH, "x") != 0) {
1472 ee448f5f 2020-03-18 stsp error = got_error_from_errno2("unveil",
1473 ee448f5f 2020-03-18 stsp GOT_FETCH_PATH_SSH);
1474 ee448f5f 2020-03-18 stsp goto done;
1477 f535bcd4 2020-09-30 stsp error = apply_unveil(repo_path, 0, NULL);
1478 ee448f5f 2020-03-18 stsp if (error)
1479 ee448f5f 2020-03-18 stsp goto done;
1481 f79e6490 2020-04-19 stsp if (verbosity >= 0)
1482 f79e6490 2020-04-19 stsp printf("Connecting to %s%s%s\n", host,
1483 f79e6490 2020-04-19 stsp port ? ":" : "", port ? port : "");
1485 9c52365f 2020-03-21 stsp error = got_fetch_connect(&fetchpid, &fetchfd, proto, host, port,
1486 9c52365f 2020-03-21 stsp server_path, verbosity);
1487 39c64a6a 2020-03-18 stsp if (error)
1488 bb64b798 2020-03-18 stsp goto done;
1490 2751fe64 2020-09-24 stsp if (!list_refs_only) {
1491 2751fe64 2020-09-24 stsp error = got_repo_init(repo_path);
1492 2751fe64 2020-09-24 stsp if (error)
1493 2751fe64 2020-09-24 stsp goto done;
1494 2751fe64 2020-09-24 stsp error = got_repo_open(&repo, repo_path, NULL);
1495 2751fe64 2020-09-24 stsp if (error)
1496 2751fe64 2020-09-24 stsp goto done;
1499 892ac3b6 2020-03-18 stsp fpa.last_scaled_size[0] = '\0';
1500 892ac3b6 2020-03-18 stsp fpa.last_p_indexed = -1;
1501 892ac3b6 2020-03-18 stsp fpa.last_p_resolved = -1;
1502 68999b92 2020-03-18 stsp fpa.verbosity = verbosity;
1503 04d9a9ec 2020-09-24 stsp fpa.create_configs = 1;
1504 04d9a9ec 2020-09-24 stsp fpa.configs_created = 0;
1505 04d9a9ec 2020-09-24 stsp fpa.repo = repo;
1506 04d9a9ec 2020-09-24 stsp fpa.config_info.symrefs = &symrefs;
1507 04d9a9ec 2020-09-24 stsp fpa.config_info.wanted_branches = &wanted_branches;
1508 04d9a9ec 2020-09-24 stsp fpa.config_info.proto = proto;
1509 04d9a9ec 2020-09-24 stsp fpa.config_info.host = host;
1510 04d9a9ec 2020-09-24 stsp fpa.config_info.port = port;
1511 04d9a9ec 2020-09-24 stsp fpa.config_info.remote_repo_path = server_path;
1512 04d9a9ec 2020-09-24 stsp fpa.config_info.git_url = git_url;
1513 04d9a9ec 2020-09-24 stsp fpa.config_info.fetch_all_branches = fetch_all_branches;
1514 04d9a9ec 2020-09-24 stsp fpa.config_info.mirror_references = mirror_references;
1515 7848a0e1 2020-03-19 stsp error = got_fetch_pack(&pack_hash, &refs, &symrefs,
1516 469dd726 2020-03-20 stsp GOT_FETCH_DEFAULT_REMOTE_NAME, mirror_references,
1517 0e4002ca 2020-03-21 stsp fetch_all_branches, &wanted_branches, &wanted_refs,
1518 0e4002ca 2020-03-21 stsp list_refs_only, verbosity, fetchfd, repo,
1519 0e4002ca 2020-03-21 stsp fetch_progress, &fpa);
1520 39c64a6a 2020-03-18 stsp if (error)
1521 d9b4d0c0 2020-03-18 stsp goto done;
1523 41b0de12 2020-03-21 stsp if (list_refs_only) {
1524 41b0de12 2020-03-21 stsp error = list_remote_refs(&symrefs, &refs);
1525 41b0de12 2020-03-21 stsp goto done;
1528 39c64a6a 2020-03-18 stsp error = got_object_id_str(&id_str, pack_hash);
1529 39c64a6a 2020-03-18 stsp if (error)
1530 d9b4d0c0 2020-03-18 stsp goto done;
1531 68999b92 2020-03-18 stsp if (verbosity >= 0)
1532 e69674d8 2020-03-19 stsp printf("\nFetched %s.pack\n", id_str);
1533 d9b4d0c0 2020-03-18 stsp free(id_str);
1535 d9b4d0c0 2020-03-18 stsp /* Set up references provided with the pack file. */
1536 d9b4d0c0 2020-03-18 stsp TAILQ_FOREACH(pe, &refs, entry) {
1537 d9b4d0c0 2020-03-18 stsp const char *refname = pe->path;
1538 d9b4d0c0 2020-03-18 stsp struct got_object_id *id = pe->data;
1539 7ebc0570 2020-03-18 stsp char *remote_refname;
1541 0e4002ca 2020-03-21 stsp if (is_wanted_ref(&wanted_refs, refname) &&
1542 0e4002ca 2020-03-21 stsp !mirror_references) {
1543 0e4002ca 2020-03-21 stsp error = create_wanted_ref(refname, id,
1544 0e4002ca 2020-03-21 stsp GOT_FETCH_DEFAULT_REMOTE_NAME,
1545 0e4002ca 2020-03-21 stsp verbosity - 1, repo);
1546 0e4002ca 2020-03-21 stsp if (error)
1547 0e4002ca 2020-03-21 stsp goto done;
1551 6338a6a1 2020-03-21 stsp error = create_ref(refname, id, verbosity - 1, repo);
1552 39c64a6a 2020-03-18 stsp if (error)
1553 d9b4d0c0 2020-03-18 stsp goto done;
1555 469dd726 2020-03-20 stsp if (mirror_references)
1558 7ebc0570 2020-03-18 stsp if (strncmp("refs/heads/", refname, 11) != 0)
1561 7ebc0570 2020-03-18 stsp if (asprintf(&remote_refname,
1562 7ebc0570 2020-03-18 stsp "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1563 7ebc0570 2020-03-18 stsp refname + 11) == -1) {
1564 7ebc0570 2020-03-18 stsp error = got_error_from_errno("asprintf");
1565 7ebc0570 2020-03-18 stsp goto done;
1567 6338a6a1 2020-03-21 stsp error = create_ref(remote_refname, id, verbosity - 1, repo);
1568 f298ae0f 2020-03-25 stsp free(remote_refname);
1569 39c64a6a 2020-03-18 stsp if (error)
1570 d9b4d0c0 2020-03-18 stsp goto done;
1573 d9b4d0c0 2020-03-18 stsp /* Set the HEAD reference if the server provided one. */
1574 d9b4d0c0 2020-03-18 stsp TAILQ_FOREACH(pe, &symrefs, entry) {
1575 659e7fbd 2020-03-20 stsp struct got_reference *target_ref;
1576 d9b4d0c0 2020-03-18 stsp const char *refname = pe->path;
1577 d9b4d0c0 2020-03-18 stsp const char *target = pe->data;
1578 f298ae0f 2020-03-25 stsp char *remote_refname = NULL, *remote_target = NULL;
1580 d9b4d0c0 2020-03-18 stsp if (strcmp(refname, GOT_REF_HEAD) != 0)
1583 39c64a6a 2020-03-18 stsp error = got_ref_open(&target_ref, repo, target, 0);
1584 39c64a6a 2020-03-18 stsp if (error) {
1585 55330abe 2020-03-20 stsp if (error->code == GOT_ERR_NOT_REF) {
1586 55330abe 2020-03-20 stsp error = NULL;
1589 d9b4d0c0 2020-03-18 stsp goto done;
1592 04d9a9ec 2020-09-24 stsp error = create_symref(refname, target_ref, verbosity, repo);
1593 f298ae0f 2020-03-25 stsp got_ref_close(target_ref);
1594 f298ae0f 2020-03-25 stsp if (error)
1595 f298ae0f 2020-03-25 stsp goto done;
1597 f298ae0f 2020-03-25 stsp if (mirror_references)
1600 f298ae0f 2020-03-25 stsp if (strncmp("refs/heads/", target, 11) != 0)
1603 f298ae0f 2020-03-25 stsp if (asprintf(&remote_refname,
1604 f298ae0f 2020-03-25 stsp "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1605 f298ae0f 2020-03-25 stsp refname) == -1) {
1606 f298ae0f 2020-03-25 stsp error = got_error_from_errno("asprintf");
1607 f298ae0f 2020-03-25 stsp goto done;
1609 f298ae0f 2020-03-25 stsp if (asprintf(&remote_target,
1610 f298ae0f 2020-03-25 stsp "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1611 f298ae0f 2020-03-25 stsp target + 11) == -1) {
1612 f298ae0f 2020-03-25 stsp error = got_error_from_errno("asprintf");
1613 f298ae0f 2020-03-25 stsp free(remote_refname);
1614 f298ae0f 2020-03-25 stsp goto done;
1616 f298ae0f 2020-03-25 stsp error = got_ref_open(&target_ref, repo, remote_target, 0);
1617 f298ae0f 2020-03-25 stsp if (error) {
1618 f298ae0f 2020-03-25 stsp free(remote_refname);
1619 f298ae0f 2020-03-25 stsp free(remote_target);
1620 f298ae0f 2020-03-25 stsp if (error->code == GOT_ERR_NOT_REF) {
1621 f298ae0f 2020-03-25 stsp error = NULL;
1624 f298ae0f 2020-03-25 stsp goto done;
1626 04d9a9ec 2020-09-24 stsp error = create_symref(remote_refname, target_ref,
1627 04d9a9ec 2020-09-24 stsp verbosity - 1, repo);
1628 f298ae0f 2020-03-25 stsp free(remote_refname);
1629 f298ae0f 2020-03-25 stsp free(remote_target);
1630 d9b4d0c0 2020-03-18 stsp got_ref_close(target_ref);
1631 39c64a6a 2020-03-18 stsp if (error)
1632 d9b4d0c0 2020-03-18 stsp goto done;
1634 4ba14133 2020-03-20 stsp if (pe == NULL) {
1636 4ba14133 2020-03-20 stsp * We failed to set the HEAD reference. If we asked for
1637 4ba14133 2020-03-20 stsp * a set of wanted branches use the first of one of those
1638 4ba14133 2020-03-20 stsp * which could be fetched instead.
1640 4ba14133 2020-03-20 stsp TAILQ_FOREACH(pe, &wanted_branches, entry) {
1641 4ba14133 2020-03-20 stsp const char *target = pe->path;
1642 4ba14133 2020-03-20 stsp struct got_reference *target_ref;
1644 4ba14133 2020-03-20 stsp error = got_ref_open(&target_ref, repo, target, 0);
1645 4ba14133 2020-03-20 stsp if (error) {
1646 4ba14133 2020-03-20 stsp if (error->code == GOT_ERR_NOT_REF) {
1647 4ba14133 2020-03-20 stsp error = NULL;
1650 4ba14133 2020-03-20 stsp goto done;
1653 04d9a9ec 2020-09-24 stsp error = create_symref(GOT_REF_HEAD, target_ref,
1654 04d9a9ec 2020-09-24 stsp verbosity, repo);
1655 4ba14133 2020-03-20 stsp got_ref_close(target_ref);
1656 4ba14133 2020-03-20 stsp if (error)
1657 4ba14133 2020-03-20 stsp goto done;
1662 d715f13e 2020-03-19 stsp if (verbosity >= 0)
1663 469dd726 2020-03-20 stsp printf("Created %s repository '%s'\n",
1664 469dd726 2020-03-20 stsp mirror_references ? "mirrored" : "cloned", repo_path);
1666 9c52365f 2020-03-21 stsp if (fetchpid > 0) {
1667 9c52365f 2020-03-21 stsp if (kill(fetchpid, SIGTERM) == -1)
1668 9c52365f 2020-03-21 stsp error = got_error_from_errno("kill");
1669 9c52365f 2020-03-21 stsp if (waitpid(fetchpid, &fetchstatus, 0) == -1 && error == NULL)
1670 9c52365f 2020-03-21 stsp error = got_error_from_errno("waitpid");
1672 39c64a6a 2020-03-18 stsp if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
1673 39c64a6a 2020-03-18 stsp error = got_error_from_errno("close");
1675 bb64b798 2020-03-18 stsp got_repo_close(repo);
1676 d9b4d0c0 2020-03-18 stsp TAILQ_FOREACH(pe, &refs, entry) {
1677 d9b4d0c0 2020-03-18 stsp free((void *)pe->path);
1678 d9b4d0c0 2020-03-18 stsp free(pe->data);
1680 d9b4d0c0 2020-03-18 stsp got_pathlist_free(&refs);
1681 d9b4d0c0 2020-03-18 stsp TAILQ_FOREACH(pe, &symrefs, entry) {
1682 d9b4d0c0 2020-03-18 stsp free((void *)pe->path);
1683 d9b4d0c0 2020-03-18 stsp free(pe->data);
1685 d9b4d0c0 2020-03-18 stsp got_pathlist_free(&symrefs);
1686 4ba14133 2020-03-20 stsp got_pathlist_free(&wanted_branches);
1687 0e4002ca 2020-03-21 stsp got_pathlist_free(&wanted_refs);
1688 d9b4d0c0 2020-03-18 stsp free(pack_hash);
1689 09838ffc 2020-03-18 stsp free(proto);
1690 09838ffc 2020-03-18 stsp free(host);
1691 09838ffc 2020-03-18 stsp free(port);
1692 09838ffc 2020-03-18 stsp free(server_path);
1693 09838ffc 2020-03-18 stsp free(repo_name);
1694 bb64b798 2020-03-18 stsp free(default_destdir);
1695 b46f3e71 2020-03-18 stsp free(git_url);
1696 39c64a6a 2020-03-18 stsp return error;
1699 7848a0e1 2020-03-19 stsp static const struct got_error *
1700 7848a0e1 2020-03-19 stsp update_ref(struct got_reference *ref, struct got_object_id *new_id,
1701 db6d8ad8 2020-03-21 stsp int replace_tags, int verbosity, struct got_repository *repo)
1703 7848a0e1 2020-03-19 stsp const struct got_error *err = NULL;
1704 7848a0e1 2020-03-19 stsp char *new_id_str = NULL;
1705 7848a0e1 2020-03-19 stsp struct got_object_id *old_id = NULL;
1707 7848a0e1 2020-03-19 stsp err = got_object_id_str(&new_id_str, new_id);
1709 7848a0e1 2020-03-19 stsp goto done;
1711 db6d8ad8 2020-03-21 stsp if (!replace_tags &&
1712 db6d8ad8 2020-03-21 stsp strncmp(got_ref_get_name(ref), "refs/tags/", 10) == 0) {
1713 88609724 2020-03-21 stsp err = got_ref_resolve(&old_id, repo, ref);
1715 88609724 2020-03-21 stsp goto done;
1716 88609724 2020-03-21 stsp if (got_object_id_cmp(old_id, new_id) == 0)
1717 88609724 2020-03-21 stsp goto done;
1718 db6d8ad8 2020-03-21 stsp if (verbosity >= 0) {
1719 db6d8ad8 2020-03-21 stsp printf("Rejecting update of existing tag %s: %s\n",
1720 db6d8ad8 2020-03-21 stsp got_ref_get_name(ref), new_id_str);
1722 db6d8ad8 2020-03-21 stsp goto done;
1725 7848a0e1 2020-03-19 stsp if (got_ref_is_symbolic(ref)) {
1726 688f11b3 2020-03-21 stsp if (verbosity >= 0) {
1727 e8a967e0 2020-03-21 stsp printf("Replacing reference %s: %s\n",
1728 6338a6a1 2020-03-21 stsp got_ref_get_name(ref),
1729 6338a6a1 2020-03-21 stsp got_ref_get_symref_target(ref));
1731 e8a967e0 2020-03-21 stsp err = got_ref_change_symref_to_ref(ref, new_id);
1733 7848a0e1 2020-03-19 stsp goto done;
1734 e8a967e0 2020-03-21 stsp err = got_ref_write(ref, repo);
1736 e8a967e0 2020-03-21 stsp goto done;
1738 7848a0e1 2020-03-19 stsp err = got_ref_resolve(&old_id, repo, ref);
1740 7848a0e1 2020-03-19 stsp goto done;
1741 6338a6a1 2020-03-21 stsp if (got_object_id_cmp(old_id, new_id) == 0)
1742 6338a6a1 2020-03-21 stsp goto done;
1744 6338a6a1 2020-03-21 stsp err = got_ref_change_ref(ref, new_id);
1746 6338a6a1 2020-03-21 stsp goto done;
1747 6338a6a1 2020-03-21 stsp err = got_ref_write(ref, repo);
1749 6338a6a1 2020-03-21 stsp goto done;
1752 6338a6a1 2020-03-21 stsp if (verbosity >= 0)
1753 f4d0e3fb 2020-05-15 stsp printf("Updated %s: %s\n", got_ref_get_name(ref),
1754 6338a6a1 2020-03-21 stsp new_id_str);
1756 7848a0e1 2020-03-19 stsp free(old_id);
1757 7848a0e1 2020-03-19 stsp free(new_id_str);
1758 7848a0e1 2020-03-19 stsp return err;
1761 f1bcca34 2020-03-25 stsp static const struct got_error *
1762 f1bcca34 2020-03-25 stsp update_symref(const char *refname, struct got_reference *target_ref,
1763 f1bcca34 2020-03-25 stsp int verbosity, struct got_repository *repo)
1765 f1bcca34 2020-03-25 stsp const struct got_error *err = NULL, *unlock_err;
1766 f1bcca34 2020-03-25 stsp struct got_reference *symref;
1767 bcf34b0e 2020-03-26 stsp int symref_is_locked = 0;
1769 f1bcca34 2020-03-25 stsp err = got_ref_open(&symref, repo, refname, 1);
1770 bcf34b0e 2020-03-26 stsp if (err) {
1771 bcf34b0e 2020-03-26 stsp if (err->code != GOT_ERR_NOT_REF)
1772 bcf34b0e 2020-03-26 stsp return err;
1773 bcf34b0e 2020-03-26 stsp err = got_ref_alloc_symref(&symref, refname, target_ref);
1775 bcf34b0e 2020-03-26 stsp goto done;
1777 bcf34b0e 2020-03-26 stsp err = got_ref_write(symref, repo);
1779 bcf34b0e 2020-03-26 stsp goto done;
1781 bcf34b0e 2020-03-26 stsp if (verbosity >= 0)
1782 bcf34b0e 2020-03-26 stsp printf("Created reference %s: %s\n",
1783 bcf34b0e 2020-03-26 stsp got_ref_get_name(symref),
1784 bcf34b0e 2020-03-26 stsp got_ref_get_symref_target(symref));
1786 bcf34b0e 2020-03-26 stsp symref_is_locked = 1;
1788 bcf34b0e 2020-03-26 stsp if (strcmp(got_ref_get_symref_target(symref),
1789 bcf34b0e 2020-03-26 stsp got_ref_get_name(target_ref)) == 0)
1790 bcf34b0e 2020-03-26 stsp goto done;
1792 bcf34b0e 2020-03-26 stsp err = got_ref_change_symref(symref,
1793 bcf34b0e 2020-03-26 stsp got_ref_get_name(target_ref));
1795 bcf34b0e 2020-03-26 stsp goto done;
1797 bcf34b0e 2020-03-26 stsp err = got_ref_write(symref, repo);
1799 bcf34b0e 2020-03-26 stsp goto done;
1801 bcf34b0e 2020-03-26 stsp if (verbosity >= 0)
1802 f4d0e3fb 2020-05-15 stsp printf("Updated %s: %s\n", got_ref_get_name(symref),
1803 bcf34b0e 2020-03-26 stsp got_ref_get_symref_target(symref));
1807 bcf34b0e 2020-03-26 stsp if (symref_is_locked) {
1808 bcf34b0e 2020-03-26 stsp unlock_err = got_ref_unlock(symref);
1809 bcf34b0e 2020-03-26 stsp if (unlock_err && err == NULL)
1810 bcf34b0e 2020-03-26 stsp err = unlock_err;
1812 f1bcca34 2020-03-25 stsp got_ref_close(symref);
1813 f1bcca34 2020-03-25 stsp return err;
1816 2ab43947 2020-03-18 stsp __dead static void
1817 7848a0e1 2020-03-19 stsp usage_fetch(void)
1819 f21ec2f0 2020-03-21 stsp fprintf(stderr, "usage: %s fetch [-a] [-b branch] [-d] [-l] "
1820 0e4002ca 2020-03-21 stsp "[-r repository-path] [-t] [-q] [-v] [-R reference] "
1821 0e4002ca 2020-03-21 stsp "[remote-repository-name]\n",
1822 13f12b09 2020-03-21 stsp getprogname());
1826 7848a0e1 2020-03-19 stsp static const struct got_error *
1827 3789fd73 2020-03-26 stsp delete_missing_ref(struct got_reference *ref,
1828 688f11b3 2020-03-21 stsp int verbosity, struct got_repository *repo)
1830 f21ec2f0 2020-03-21 stsp const struct got_error *err = NULL;
1831 3789fd73 2020-03-26 stsp struct got_object_id *id = NULL;
1832 3789fd73 2020-03-26 stsp char *id_str = NULL;
1834 3789fd73 2020-03-26 stsp if (got_ref_is_symbolic(ref)) {
1835 3789fd73 2020-03-26 stsp err = got_ref_delete(ref, repo);
1837 3789fd73 2020-03-26 stsp return err;
1838 3789fd73 2020-03-26 stsp if (verbosity >= 0) {
1839 3789fd73 2020-03-26 stsp printf("Deleted reference %s: %s\n",
1840 3789fd73 2020-03-26 stsp got_ref_get_name(ref),
1841 3789fd73 2020-03-26 stsp got_ref_get_symref_target(ref));
1844 3789fd73 2020-03-26 stsp err = got_ref_resolve(&id, repo, ref);
1846 3789fd73 2020-03-26 stsp return err;
1847 3789fd73 2020-03-26 stsp err = got_object_id_str(&id_str, id);
1849 3789fd73 2020-03-26 stsp goto done;
1851 3789fd73 2020-03-26 stsp err = got_ref_delete(ref, repo);
1853 3789fd73 2020-03-26 stsp goto done;
1854 3789fd73 2020-03-26 stsp if (verbosity >= 0) {
1855 3789fd73 2020-03-26 stsp printf("Deleted reference %s: %s\n",
1856 3789fd73 2020-03-26 stsp got_ref_get_name(ref), id_str);
1861 3789fd73 2020-03-26 stsp free(id_str);
1862 3789fd73 2020-03-26 stsp return NULL;
1865 3789fd73 2020-03-26 stsp static const struct got_error *
1866 3789fd73 2020-03-26 stsp delete_missing_refs(struct got_pathlist_head *their_refs,
1867 50b0790e 2020-09-11 stsp struct got_pathlist_head *their_symrefs,
1868 50b0790e 2020-09-11 stsp const struct got_remote_repo *remote,
1869 3789fd73 2020-03-26 stsp int verbosity, struct got_repository *repo)
1871 3789fd73 2020-03-26 stsp const struct got_error *err = NULL, *unlock_err;
1872 f21ec2f0 2020-03-21 stsp struct got_reflist_head my_refs;
1873 f21ec2f0 2020-03-21 stsp struct got_reflist_entry *re;
1874 f21ec2f0 2020-03-21 stsp struct got_pathlist_entry *pe;
1875 3789fd73 2020-03-26 stsp char *remote_namespace = NULL;
1876 3789fd73 2020-03-26 stsp char *local_refname = NULL;
1878 f21ec2f0 2020-03-21 stsp SIMPLEQ_INIT(&my_refs);
1880 3789fd73 2020-03-26 stsp if (asprintf(&remote_namespace, "refs/remotes/%s/", remote->name)
1882 3789fd73 2020-03-26 stsp return got_error_from_errno("asprintf");
1884 f21ec2f0 2020-03-21 stsp err = got_ref_list(&my_refs, repo, NULL, got_ref_cmp_by_name, NULL);
1886 3789fd73 2020-03-26 stsp goto done;
1888 f21ec2f0 2020-03-21 stsp SIMPLEQ_FOREACH(re, &my_refs, entry) {
1889 f21ec2f0 2020-03-21 stsp const char *refname = got_ref_get_name(re->ref);
1891 3789fd73 2020-03-26 stsp if (!remote->mirror_references) {
1892 3789fd73 2020-03-26 stsp if (strncmp(refname, remote_namespace,
1893 3789fd73 2020-03-26 stsp strlen(remote_namespace)) == 0) {
1894 3789fd73 2020-03-26 stsp if (strcmp(refname + strlen(remote_namespace),
1895 3789fd73 2020-03-26 stsp GOT_REF_HEAD) == 0)
1897 3789fd73 2020-03-26 stsp if (asprintf(&local_refname, "refs/heads/%s",
1898 3789fd73 2020-03-26 stsp refname + strlen(remote_namespace)) == -1) {
1899 3789fd73 2020-03-26 stsp err = got_error_from_errno("asprintf");
1900 3789fd73 2020-03-26 stsp goto done;
1902 3789fd73 2020-03-26 stsp } else if (strncmp(refname, "refs/tags/", 10) != 0)
1906 f21ec2f0 2020-03-21 stsp TAILQ_FOREACH(pe, their_refs, entry) {
1907 3789fd73 2020-03-26 stsp if (strcmp(local_refname, pe->path) == 0)
1910 f21ec2f0 2020-03-21 stsp if (pe != NULL)
1913 3789fd73 2020-03-26 stsp TAILQ_FOREACH(pe, their_symrefs, entry) {
1914 3789fd73 2020-03-26 stsp if (strcmp(local_refname, pe->path) == 0)
1917 3789fd73 2020-03-26 stsp if (pe != NULL)
1920 3789fd73 2020-03-26 stsp err = delete_missing_ref(re->ref, verbosity, repo);
1924 3789fd73 2020-03-26 stsp if (local_refname) {
1925 3789fd73 2020-03-26 stsp struct got_reference *ref;
1926 3789fd73 2020-03-26 stsp err = got_ref_open(&ref, repo, local_refname, 1);
1927 3789fd73 2020-03-26 stsp if (err) {
1928 3789fd73 2020-03-26 stsp if (err->code != GOT_ERR_NOT_REF)
1930 3789fd73 2020-03-26 stsp free(local_refname);
1931 3789fd73 2020-03-26 stsp local_refname = NULL;
1934 3789fd73 2020-03-26 stsp err = delete_missing_ref(ref, verbosity, repo);
1937 3789fd73 2020-03-26 stsp unlock_err = got_ref_unlock(ref);
1938 3789fd73 2020-03-26 stsp got_ref_close(ref);
1939 3789fd73 2020-03-26 stsp if (unlock_err && err == NULL) {
1940 3789fd73 2020-03-26 stsp err = unlock_err;
1944 3789fd73 2020-03-26 stsp free(local_refname);
1945 3789fd73 2020-03-26 stsp local_refname = NULL;
1949 3789fd73 2020-03-26 stsp free(remote_namespace);
1950 3789fd73 2020-03-26 stsp free(local_refname);
1951 0e4002ca 2020-03-21 stsp return err;
1954 0e4002ca 2020-03-21 stsp static const struct got_error *
1955 0e4002ca 2020-03-21 stsp update_wanted_ref(const char *refname, struct got_object_id *id,
1956 0e4002ca 2020-03-21 stsp const char *remote_repo_name, int verbosity, struct got_repository *repo)
1958 9f142382 2020-03-21 stsp const struct got_error *err, *unlock_err;
1959 0e4002ca 2020-03-21 stsp char *remote_refname;
1960 0e4002ca 2020-03-21 stsp struct got_reference *ref;
1962 0e4002ca 2020-03-21 stsp if (strncmp("refs/", refname, 5) == 0)
1963 0e4002ca 2020-03-21 stsp refname += 5;
1965 0e4002ca 2020-03-21 stsp if (asprintf(&remote_refname, "refs/remotes/%s/%s",
1966 0e4002ca 2020-03-21 stsp remote_repo_name, refname) == -1)
1967 0e4002ca 2020-03-21 stsp return got_error_from_errno("asprintf");
1969 9f142382 2020-03-21 stsp err = got_ref_open(&ref, repo, remote_refname, 1);
1970 0e4002ca 2020-03-21 stsp if (err) {
1971 0e4002ca 2020-03-21 stsp if (err->code != GOT_ERR_NOT_REF)
1972 0e4002ca 2020-03-21 stsp goto done;
1973 0e4002ca 2020-03-21 stsp err = create_ref(remote_refname, id, verbosity, repo);
1975 0e4002ca 2020-03-21 stsp err = update_ref(ref, id, 0, verbosity, repo);
1976 9f142382 2020-03-21 stsp unlock_err = got_ref_unlock(ref);
1977 9f142382 2020-03-21 stsp if (unlock_err && err == NULL)
1978 9f142382 2020-03-21 stsp err = unlock_err;
1979 0e4002ca 2020-03-21 stsp got_ref_close(ref);
1982 0e4002ca 2020-03-21 stsp free(remote_refname);
1983 f21ec2f0 2020-03-21 stsp return err;
1986 f21ec2f0 2020-03-21 stsp static const struct got_error *
1987 7848a0e1 2020-03-19 stsp cmd_fetch(int argc, char *argv[])
1989 9f142382 2020-03-21 stsp const struct got_error *error = NULL, *unlock_err;
1990 7848a0e1 2020-03-19 stsp char *cwd = NULL, *repo_path = NULL;
1991 7848a0e1 2020-03-19 stsp const char *remote_name;
1992 7848a0e1 2020-03-19 stsp char *proto = NULL, *host = NULL, *port = NULL;
1993 7848a0e1 2020-03-19 stsp char *repo_name = NULL, *server_path = NULL;
1994 50b0790e 2020-09-11 stsp const struct got_remote_repo *remotes, *remote = NULL;
1995 7848a0e1 2020-03-19 stsp int nremotes;
1996 7848a0e1 2020-03-19 stsp char *id_str = NULL;
1997 7848a0e1 2020-03-19 stsp struct got_repository *repo = NULL;
1998 7848a0e1 2020-03-19 stsp struct got_worktree *worktree = NULL;
1999 50b0790e 2020-09-11 stsp const struct got_gotconfig *repo_conf = NULL, *worktree_conf = NULL;
2000 0e4002ca 2020-03-21 stsp struct got_pathlist_head refs, symrefs, wanted_branches, wanted_refs;
2001 7848a0e1 2020-03-19 stsp struct got_pathlist_entry *pe;
2002 7848a0e1 2020-03-19 stsp struct got_object_id *pack_hash = NULL;
2003 9c52365f 2020-03-21 stsp int i, ch, fetchfd = -1, fetchstatus;
2004 9c52365f 2020-03-21 stsp pid_t fetchpid = -1;
2005 7848a0e1 2020-03-19 stsp struct got_fetch_progress_arg fpa;
2006 41b0de12 2020-03-21 stsp int verbosity = 0, fetch_all_branches = 0, list_refs_only = 0;
2007 db6d8ad8 2020-03-21 stsp int delete_refs = 0, replace_tags = 0;
2009 7848a0e1 2020-03-19 stsp TAILQ_INIT(&refs);
2010 7848a0e1 2020-03-19 stsp TAILQ_INIT(&symrefs);
2011 4ba14133 2020-03-20 stsp TAILQ_INIT(&wanted_branches);
2012 0e4002ca 2020-03-21 stsp TAILQ_INIT(&wanted_refs);
2014 0e4002ca 2020-03-21 stsp while ((ch = getopt(argc, argv, "ab:dlr:tvqR:")) != -1) {
2015 7848a0e1 2020-03-19 stsp switch (ch) {
2017 659e7fbd 2020-03-20 stsp fetch_all_branches = 1;
2020 4ba14133 2020-03-20 stsp error = got_pathlist_append(&wanted_branches,
2021 4ba14133 2020-03-20 stsp optarg, NULL);
2022 4ba14133 2020-03-20 stsp if (error)
2023 4ba14133 2020-03-20 stsp return error;
2026 f21ec2f0 2020-03-21 stsp delete_refs = 1;
2029 41b0de12 2020-03-21 stsp list_refs_only = 1;
2032 7848a0e1 2020-03-19 stsp repo_path = realpath(optarg, NULL);
2033 7848a0e1 2020-03-19 stsp if (repo_path == NULL)
2034 7848a0e1 2020-03-19 stsp return got_error_from_errno2("realpath",
2036 7848a0e1 2020-03-19 stsp got_path_strip_trailing_slashes(repo_path);
2039 db6d8ad8 2020-03-21 stsp replace_tags = 1;
2042 7848a0e1 2020-03-19 stsp if (verbosity < 0)
2043 7848a0e1 2020-03-19 stsp verbosity = 0;
2044 7848a0e1 2020-03-19 stsp else if (verbosity < 3)
2045 7848a0e1 2020-03-19 stsp verbosity++;
2048 7848a0e1 2020-03-19 stsp verbosity = -1;
2051 0e4002ca 2020-03-21 stsp error = got_pathlist_append(&wanted_refs,
2052 0e4002ca 2020-03-21 stsp optarg, NULL);
2053 0e4002ca 2020-03-21 stsp if (error)
2054 0e4002ca 2020-03-21 stsp return error;
2057 7848a0e1 2020-03-19 stsp usage_fetch();
2061 7848a0e1 2020-03-19 stsp argc -= optind;
2062 7848a0e1 2020-03-19 stsp argv += optind;
2064 4ba14133 2020-03-20 stsp if (fetch_all_branches && !TAILQ_EMPTY(&wanted_branches))
2065 41b0de12 2020-03-21 stsp errx(1, "-a and -b options are mutually exclusive");
2066 41b0de12 2020-03-21 stsp if (list_refs_only) {
2067 41b0de12 2020-03-21 stsp if (!TAILQ_EMPTY(&wanted_branches))
2068 41b0de12 2020-03-21 stsp errx(1, "-l and -b options are mutually exclusive");
2069 41b0de12 2020-03-21 stsp if (fetch_all_branches)
2070 41b0de12 2020-03-21 stsp errx(1, "-l and -a options are mutually exclusive");
2071 f21ec2f0 2020-03-21 stsp if (delete_refs)
2072 f21ec2f0 2020-03-21 stsp errx(1, "-l and -d options are mutually exclusive");
2073 41b0de12 2020-03-21 stsp if (verbosity == -1)
2074 41b0de12 2020-03-21 stsp errx(1, "-l and -q options are mutually exclusive");
2077 7848a0e1 2020-03-19 stsp if (argc == 0)
2078 7848a0e1 2020-03-19 stsp remote_name = GOT_FETCH_DEFAULT_REMOTE_NAME;
2079 7848a0e1 2020-03-19 stsp else if (argc == 1)
2080 7848a0e1 2020-03-19 stsp remote_name = argv[0];
2082 7848a0e1 2020-03-19 stsp usage_fetch();
2084 7848a0e1 2020-03-19 stsp cwd = getcwd(NULL, 0);
2085 7848a0e1 2020-03-19 stsp if (cwd == NULL) {
2086 7848a0e1 2020-03-19 stsp error = got_error_from_errno("getcwd");
2087 7848a0e1 2020-03-19 stsp goto done;
2090 7848a0e1 2020-03-19 stsp if (repo_path == NULL) {
2091 7848a0e1 2020-03-19 stsp error = got_worktree_open(&worktree, cwd);
2092 7848a0e1 2020-03-19 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
2093 7848a0e1 2020-03-19 stsp goto done;
2095 7848a0e1 2020-03-19 stsp error = NULL;
2096 7848a0e1 2020-03-19 stsp if (worktree) {
2097 7848a0e1 2020-03-19 stsp repo_path =
2098 7848a0e1 2020-03-19 stsp strdup(got_worktree_get_repo_path(worktree));
2099 7848a0e1 2020-03-19 stsp if (repo_path == NULL)
2100 7848a0e1 2020-03-19 stsp error = got_error_from_errno("strdup");
2101 7848a0e1 2020-03-19 stsp if (error)
2102 7848a0e1 2020-03-19 stsp goto done;
2104 7848a0e1 2020-03-19 stsp repo_path = strdup(cwd);
2105 7848a0e1 2020-03-19 stsp if (repo_path == NULL) {
2106 7848a0e1 2020-03-19 stsp error = got_error_from_errno("strdup");
2107 7848a0e1 2020-03-19 stsp goto done;
2112 7848a0e1 2020-03-19 stsp error = got_repo_open(&repo, repo_path, NULL);
2113 7848a0e1 2020-03-19 stsp if (error)
2114 7848a0e1 2020-03-19 stsp goto done;
2116 50b0790e 2020-09-11 stsp if (worktree) {
2117 50b0790e 2020-09-11 stsp worktree_conf = got_worktree_get_gotconfig(worktree);
2118 50b0790e 2020-09-11 stsp if (worktree_conf) {
2119 50b0790e 2020-09-11 stsp got_gotconfig_get_remotes(&nremotes, &remotes,
2120 50b0790e 2020-09-11 stsp worktree_conf);
2121 50b0790e 2020-09-11 stsp for (i = 0; i < nremotes; i++) {
2122 50b0790e 2020-09-11 stsp remote = &remotes[i];
2123 50b0790e 2020-09-11 stsp if (strcmp(remote->name, remote_name) == 0)
2128 50b0790e 2020-09-11 stsp if (remote == NULL) {
2129 50b0790e 2020-09-11 stsp repo_conf = got_repo_get_gotconfig(repo);
2130 50b0790e 2020-09-11 stsp if (repo_conf) {
2131 50b0790e 2020-09-11 stsp got_gotconfig_get_remotes(&nremotes, &remotes,
2132 50b0790e 2020-09-11 stsp repo_conf);
2133 50b0790e 2020-09-11 stsp for (i = 0; i < nremotes; i++) {
2134 50b0790e 2020-09-11 stsp remote = &remotes[i];
2135 50b0790e 2020-09-11 stsp if (strcmp(remote->name, remote_name) == 0)
2140 50b0790e 2020-09-11 stsp if (remote == NULL) {
2141 257add31 2020-09-09 stsp got_repo_get_gitconfig_remotes(&nremotes, &remotes, repo);
2142 257add31 2020-09-09 stsp for (i = 0; i < nremotes; i++) {
2143 257add31 2020-09-09 stsp remote = &remotes[i];
2144 257add31 2020-09-09 stsp if (strcmp(remote->name, remote_name) == 0)
2148 50b0790e 2020-09-11 stsp if (remote == NULL) {
2149 50b0790e 2020-09-11 stsp error = got_error_path(remote_name, GOT_ERR_NO_REMOTE);
2150 50b0790e 2020-09-11 stsp goto done;
2153 b8adfa55 2020-09-25 stsp if (TAILQ_EMPTY(&wanted_branches) && remote->nbranches > 0) {
2154 b8adfa55 2020-09-25 stsp for (i = 0; i < remote->nbranches; i++) {
2155 b8adfa55 2020-09-25 stsp got_pathlist_append(&wanted_branches,
2156 b8adfa55 2020-09-25 stsp remote->branches[i], NULL);
2161 7848a0e1 2020-03-19 stsp error = got_fetch_parse_uri(&proto, &host, &port, &server_path,
2162 7848a0e1 2020-03-19 stsp &repo_name, remote->url);
2163 7848a0e1 2020-03-19 stsp if (error)
2164 7848a0e1 2020-03-19 stsp goto done;
2166 7848a0e1 2020-03-19 stsp if (strcmp(proto, "git") == 0) {
2167 7848a0e1 2020-03-19 stsp #ifndef PROFILE
2168 7848a0e1 2020-03-19 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2169 7848a0e1 2020-03-19 stsp "sendfd dns inet unveil", NULL) == -1)
2170 7848a0e1 2020-03-19 stsp err(1, "pledge");
2172 7848a0e1 2020-03-19 stsp } else if (strcmp(proto, "git+ssh") == 0 ||
2173 7848a0e1 2020-03-19 stsp strcmp(proto, "ssh") == 0) {
2174 7848a0e1 2020-03-19 stsp #ifndef PROFILE
2175 7848a0e1 2020-03-19 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2176 7848a0e1 2020-03-19 stsp "sendfd unveil", NULL) == -1)
2177 7848a0e1 2020-03-19 stsp err(1, "pledge");
2179 7848a0e1 2020-03-19 stsp } else if (strcmp(proto, "http") == 0 ||
2180 7848a0e1 2020-03-19 stsp strcmp(proto, "git+http") == 0) {
2181 7848a0e1 2020-03-19 stsp error = got_error_path(proto, GOT_ERR_NOT_IMPL);
2182 7848a0e1 2020-03-19 stsp goto done;
2184 7848a0e1 2020-03-19 stsp error = got_error_path(proto, GOT_ERR_BAD_PROTO);
2185 7848a0e1 2020-03-19 stsp goto done;
2188 7848a0e1 2020-03-19 stsp if (strcmp(proto, "git+ssh") == 0 || strcmp(proto, "ssh") == 0) {
2189 7848a0e1 2020-03-19 stsp if (unveil(GOT_FETCH_PATH_SSH, "x") != 0) {
2190 7848a0e1 2020-03-19 stsp error = got_error_from_errno2("unveil",
2191 7848a0e1 2020-03-19 stsp GOT_FETCH_PATH_SSH);
2192 7848a0e1 2020-03-19 stsp goto done;
2195 7848a0e1 2020-03-19 stsp error = apply_unveil(got_repo_get_path(repo), 0, NULL);
2196 7848a0e1 2020-03-19 stsp if (error)
2197 7848a0e1 2020-03-19 stsp goto done;
2199 f79e6490 2020-04-19 stsp if (verbosity >= 0)
2200 f79e6490 2020-04-19 stsp printf("Connecting to \"%s\" %s%s%s\n", remote->name, host,
2201 f79e6490 2020-04-19 stsp port ? ":" : "", port ? port : "");
2203 9c52365f 2020-03-21 stsp error = got_fetch_connect(&fetchpid, &fetchfd, proto, host, port,
2204 9c52365f 2020-03-21 stsp server_path, verbosity);
2205 7848a0e1 2020-03-19 stsp if (error)
2206 7848a0e1 2020-03-19 stsp goto done;
2208 7848a0e1 2020-03-19 stsp fpa.last_scaled_size[0] = '\0';
2209 7848a0e1 2020-03-19 stsp fpa.last_p_indexed = -1;
2210 7848a0e1 2020-03-19 stsp fpa.last_p_resolved = -1;
2211 7848a0e1 2020-03-19 stsp fpa.verbosity = verbosity;
2212 04d9a9ec 2020-09-24 stsp fpa.repo = repo;
2213 04d9a9ec 2020-09-24 stsp fpa.create_configs = 0;
2214 04d9a9ec 2020-09-24 stsp fpa.configs_created = 0;
2215 04d9a9ec 2020-09-24 stsp memset(&fpa.config_info, 0, sizeof(fpa.config_info));
2216 7848a0e1 2020-03-19 stsp error = got_fetch_pack(&pack_hash, &refs, &symrefs, remote->name,
2217 4ba14133 2020-03-20 stsp remote->mirror_references, fetch_all_branches, &wanted_branches,
2218 0e4002ca 2020-03-21 stsp &wanted_refs, list_refs_only, verbosity, fetchfd, repo,
2219 0e4002ca 2020-03-21 stsp fetch_progress, &fpa);
2220 7848a0e1 2020-03-19 stsp if (error)
2221 7848a0e1 2020-03-19 stsp goto done;
2223 41b0de12 2020-03-21 stsp if (list_refs_only) {
2224 41b0de12 2020-03-21 stsp error = list_remote_refs(&symrefs, &refs);
2225 41b0de12 2020-03-21 stsp goto done;
2228 7848a0e1 2020-03-19 stsp if (pack_hash == NULL) {
2229 7848a0e1 2020-03-19 stsp if (verbosity >= 0)
2230 7848a0e1 2020-03-19 stsp printf("Already up-to-date\n");
2231 bcf34b0e 2020-03-26 stsp } else if (verbosity >= 0) {
2232 984065c8 2020-03-19 stsp error = got_object_id_str(&id_str, pack_hash);
2233 984065c8 2020-03-19 stsp if (error)
2234 984065c8 2020-03-19 stsp goto done;
2235 e69674d8 2020-03-19 stsp printf("\nFetched %s.pack\n", id_str);
2236 984065c8 2020-03-19 stsp free(id_str);
2237 984065c8 2020-03-19 stsp id_str = NULL;
2240 7848a0e1 2020-03-19 stsp /* Update references provided with the pack file. */
2241 7848a0e1 2020-03-19 stsp TAILQ_FOREACH(pe, &refs, entry) {
2242 7848a0e1 2020-03-19 stsp const char *refname = pe->path;
2243 7848a0e1 2020-03-19 stsp struct got_object_id *id = pe->data;
2244 7848a0e1 2020-03-19 stsp struct got_reference *ref;
2245 7848a0e1 2020-03-19 stsp char *remote_refname;
2247 0e4002ca 2020-03-21 stsp if (is_wanted_ref(&wanted_refs, refname) &&
2248 0e4002ca 2020-03-21 stsp !remote->mirror_references) {
2249 0e4002ca 2020-03-21 stsp error = update_wanted_ref(refname, id,
2250 0e4002ca 2020-03-21 stsp remote->name, verbosity, repo);
2251 0e4002ca 2020-03-21 stsp if (error)
2252 0e4002ca 2020-03-21 stsp goto done;
2256 1510c839 2020-03-20 stsp if (remote->mirror_references ||
2257 1510c839 2020-03-20 stsp strncmp("refs/tags/", refname, 10) == 0) {
2258 9f142382 2020-03-21 stsp error = got_ref_open(&ref, repo, refname, 1);
2259 7848a0e1 2020-03-19 stsp if (error) {
2260 7848a0e1 2020-03-19 stsp if (error->code != GOT_ERR_NOT_REF)
2261 7848a0e1 2020-03-19 stsp goto done;
2262 6338a6a1 2020-03-21 stsp error = create_ref(refname, id, verbosity,
2264 7848a0e1 2020-03-19 stsp if (error)
2265 7848a0e1 2020-03-19 stsp goto done;
2267 db6d8ad8 2020-03-21 stsp error = update_ref(ref, id, replace_tags,
2268 db6d8ad8 2020-03-21 stsp verbosity, repo);
2269 9f142382 2020-03-21 stsp unlock_err = got_ref_unlock(ref);
2270 9f142382 2020-03-21 stsp if (unlock_err && error == NULL)
2271 9f142382 2020-03-21 stsp error = unlock_err;
2272 7848a0e1 2020-03-19 stsp got_ref_close(ref);
2273 7848a0e1 2020-03-19 stsp if (error)
2274 7848a0e1 2020-03-19 stsp goto done;
2276 7848a0e1 2020-03-19 stsp } else if (strncmp("refs/heads/", refname, 11) == 0) {
2277 7848a0e1 2020-03-19 stsp if (asprintf(&remote_refname, "refs/remotes/%s/%s",
2278 7848a0e1 2020-03-19 stsp remote_name, refname + 11) == -1) {
2279 7848a0e1 2020-03-19 stsp error = got_error_from_errno("asprintf");
2280 7848a0e1 2020-03-19 stsp goto done;
2283 9f142382 2020-03-21 stsp error = got_ref_open(&ref, repo, remote_refname, 1);
2284 7848a0e1 2020-03-19 stsp if (error) {
2285 7848a0e1 2020-03-19 stsp if (error->code != GOT_ERR_NOT_REF)
2286 7848a0e1 2020-03-19 stsp goto done;
2287 6338a6a1 2020-03-21 stsp error = create_ref(remote_refname, id,
2288 688f11b3 2020-03-21 stsp verbosity, repo);
2289 7848a0e1 2020-03-19 stsp if (error)
2290 7848a0e1 2020-03-19 stsp goto done;
2292 db6d8ad8 2020-03-21 stsp error = update_ref(ref, id, replace_tags,
2293 db6d8ad8 2020-03-21 stsp verbosity, repo);
2294 9f142382 2020-03-21 stsp unlock_err = got_ref_unlock(ref);
2295 9f142382 2020-03-21 stsp if (unlock_err && error == NULL)
2296 9f142382 2020-03-21 stsp error = unlock_err;
2297 7848a0e1 2020-03-19 stsp got_ref_close(ref);
2298 7848a0e1 2020-03-19 stsp if (error)
2299 7848a0e1 2020-03-19 stsp goto done;
2302 2ec30c80 2020-03-20 stsp /* Also create a local branch if none exists yet. */
2303 9f142382 2020-03-21 stsp error = got_ref_open(&ref, repo, refname, 1);
2304 2ec30c80 2020-03-20 stsp if (error) {
2305 2ec30c80 2020-03-20 stsp if (error->code != GOT_ERR_NOT_REF)
2306 2ec30c80 2020-03-20 stsp goto done;
2307 6338a6a1 2020-03-21 stsp error = create_ref(refname, id, verbosity,
2309 2ec30c80 2020-03-20 stsp if (error)
2310 2ec30c80 2020-03-20 stsp goto done;
2312 9f142382 2020-03-21 stsp unlock_err = got_ref_unlock(ref);
2313 9f142382 2020-03-21 stsp if (unlock_err && error == NULL)
2314 9f142382 2020-03-21 stsp error = unlock_err;
2315 2ec30c80 2020-03-20 stsp got_ref_close(ref);
2319 f1bcca34 2020-03-25 stsp if (delete_refs) {
2320 3789fd73 2020-03-26 stsp error = delete_missing_refs(&refs, &symrefs, remote,
2321 3789fd73 2020-03-26 stsp verbosity, repo);
2322 f1bcca34 2020-03-25 stsp if (error)
2323 f1bcca34 2020-03-25 stsp goto done;
2326 f1bcca34 2020-03-25 stsp if (!remote->mirror_references) {
2327 f1bcca34 2020-03-25 stsp /* Update remote HEAD reference if the server provided one. */
2328 f1bcca34 2020-03-25 stsp TAILQ_FOREACH(pe, &symrefs, entry) {
2329 f1bcca34 2020-03-25 stsp struct got_reference *target_ref;
2330 f1bcca34 2020-03-25 stsp const char *refname = pe->path;
2331 f1bcca34 2020-03-25 stsp const char *target = pe->data;
2332 f1bcca34 2020-03-25 stsp char *remote_refname = NULL, *remote_target = NULL;
2334 f1bcca34 2020-03-25 stsp if (strcmp(refname, GOT_REF_HEAD) != 0)
2337 f1bcca34 2020-03-25 stsp if (strncmp("refs/heads/", target, 11) != 0)
2340 f1bcca34 2020-03-25 stsp if (asprintf(&remote_refname, "refs/remotes/%s/%s",
2341 f1bcca34 2020-03-25 stsp remote->name, refname) == -1) {
2342 f1bcca34 2020-03-25 stsp error = got_error_from_errno("asprintf");
2343 f1bcca34 2020-03-25 stsp goto done;
2345 f1bcca34 2020-03-25 stsp if (asprintf(&remote_target, "refs/remotes/%s/%s",
2346 f1bcca34 2020-03-25 stsp remote->name, target + 11) == -1) {
2347 f1bcca34 2020-03-25 stsp error = got_error_from_errno("asprintf");
2348 f1bcca34 2020-03-25 stsp free(remote_refname);
2349 f1bcca34 2020-03-25 stsp goto done;
2352 f1bcca34 2020-03-25 stsp error = got_ref_open(&target_ref, repo, remote_target,
2354 f1bcca34 2020-03-25 stsp if (error) {
2355 f1bcca34 2020-03-25 stsp free(remote_refname);
2356 f1bcca34 2020-03-25 stsp free(remote_target);
2357 f1bcca34 2020-03-25 stsp if (error->code == GOT_ERR_NOT_REF) {
2358 f1bcca34 2020-03-25 stsp error = NULL;
2361 f1bcca34 2020-03-25 stsp goto done;
2363 f1bcca34 2020-03-25 stsp error = update_symref(remote_refname, target_ref,
2364 f1bcca34 2020-03-25 stsp verbosity, repo);
2365 f1bcca34 2020-03-25 stsp free(remote_refname);
2366 f1bcca34 2020-03-25 stsp free(remote_target);
2367 f1bcca34 2020-03-25 stsp got_ref_close(target_ref);
2368 f1bcca34 2020-03-25 stsp if (error)
2369 f1bcca34 2020-03-25 stsp goto done;
2373 9c52365f 2020-03-21 stsp if (fetchpid > 0) {
2374 9c52365f 2020-03-21 stsp if (kill(fetchpid, SIGTERM) == -1)
2375 9c52365f 2020-03-21 stsp error = got_error_from_errno("kill");
2376 9c52365f 2020-03-21 stsp if (waitpid(fetchpid, &fetchstatus, 0) == -1 && error == NULL)
2377 9c52365f 2020-03-21 stsp error = got_error_from_errno("waitpid");
2379 7848a0e1 2020-03-19 stsp if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
2380 7848a0e1 2020-03-19 stsp error = got_error_from_errno("close");
2382 7848a0e1 2020-03-19 stsp got_repo_close(repo);
2383 7848a0e1 2020-03-19 stsp if (worktree)
2384 7848a0e1 2020-03-19 stsp got_worktree_close(worktree);
2385 7848a0e1 2020-03-19 stsp TAILQ_FOREACH(pe, &refs, entry) {
2386 7848a0e1 2020-03-19 stsp free((void *)pe->path);
2387 7848a0e1 2020-03-19 stsp free(pe->data);
2389 7848a0e1 2020-03-19 stsp got_pathlist_free(&refs);
2390 7848a0e1 2020-03-19 stsp TAILQ_FOREACH(pe, &symrefs, entry) {
2391 7848a0e1 2020-03-19 stsp free((void *)pe->path);
2392 7848a0e1 2020-03-19 stsp free(pe->data);
2394 7848a0e1 2020-03-19 stsp got_pathlist_free(&symrefs);
2395 4ba14133 2020-03-20 stsp got_pathlist_free(&wanted_branches);
2396 0e4002ca 2020-03-21 stsp got_pathlist_free(&wanted_refs);
2397 7848a0e1 2020-03-19 stsp free(id_str);
2398 7848a0e1 2020-03-19 stsp free(cwd);
2399 7848a0e1 2020-03-19 stsp free(repo_path);
2400 7848a0e1 2020-03-19 stsp free(pack_hash);
2401 7848a0e1 2020-03-19 stsp free(proto);
2402 7848a0e1 2020-03-19 stsp free(host);
2403 7848a0e1 2020-03-19 stsp free(port);
2404 7848a0e1 2020-03-19 stsp free(server_path);
2405 7848a0e1 2020-03-19 stsp free(repo_name);
2406 7848a0e1 2020-03-19 stsp return error;
2410 7848a0e1 2020-03-19 stsp __dead static void
2411 2ab43947 2020-03-18 stsp usage_checkout(void)
2413 2ab43947 2020-03-18 stsp fprintf(stderr, "usage: %s checkout [-E] [-b branch] [-c commit] "
2414 2ab43947 2020-03-18 stsp "[-p prefix] repository-path [worktree-path]\n", getprogname());
2418 2ab43947 2020-03-18 stsp static void
2419 2ab43947 2020-03-18 stsp show_worktree_base_ref_warning(void)
2421 2ab43947 2020-03-18 stsp fprintf(stderr, "%s: warning: could not create a reference "
2422 2ab43947 2020-03-18 stsp "to the work tree's base commit; the commit could be "
2423 2ab43947 2020-03-18 stsp "garbage-collected by Git; making the repository "
2424 2ab43947 2020-03-18 stsp "writable and running 'got update' will prevent this\n",
2425 2ab43947 2020-03-18 stsp getprogname());
2428 2ab43947 2020-03-18 stsp struct got_checkout_progress_arg {
2429 2ab43947 2020-03-18 stsp const char *worktree_path;
2430 2ab43947 2020-03-18 stsp int had_base_commit_ref_error;
2433 2ab43947 2020-03-18 stsp static const struct got_error *
2434 2ab43947 2020-03-18 stsp checkout_progress(void *arg, unsigned char status, const char *path)
2436 2ab43947 2020-03-18 stsp struct got_checkout_progress_arg *a = arg;
2438 2ab43947 2020-03-18 stsp /* Base commit bump happens silently. */
2439 2ab43947 2020-03-18 stsp if (status == GOT_STATUS_BUMP_BASE)
2440 2ab43947 2020-03-18 stsp return NULL;
2442 2ab43947 2020-03-18 stsp if (status == GOT_STATUS_BASE_REF_ERR) {
2443 2ab43947 2020-03-18 stsp a->had_base_commit_ref_error = 1;
2444 2ab43947 2020-03-18 stsp return NULL;
2447 2ab43947 2020-03-18 stsp while (path[0] == '/')
2450 2ab43947 2020-03-18 stsp printf("%c %s/%s\n", status, a->worktree_path, path);
2451 2ab43947 2020-03-18 stsp return NULL;
2454 2ab43947 2020-03-18 stsp static const struct got_error *
2455 2ab43947 2020-03-18 stsp check_cancelled(void *arg)
2457 2ab43947 2020-03-18 stsp if (sigint_received || sigpipe_received)
2458 2ab43947 2020-03-18 stsp return got_error(GOT_ERR_CANCELLED);
2459 2ab43947 2020-03-18 stsp return NULL;
2462 2ab43947 2020-03-18 stsp static const struct got_error *
2463 2ab43947 2020-03-18 stsp check_linear_ancestry(struct got_object_id *commit_id,
2464 2ab43947 2020-03-18 stsp struct got_object_id *base_commit_id, int allow_forwards_in_time_only,
2465 2ab43947 2020-03-18 stsp struct got_repository *repo)
2467 2ab43947 2020-03-18 stsp const struct got_error *err = NULL;
2468 2ab43947 2020-03-18 stsp struct got_object_id *yca_id;
2470 2ab43947 2020-03-18 stsp err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
2471 2ab43947 2020-03-18 stsp commit_id, base_commit_id, repo, check_cancelled, NULL);
2473 2ab43947 2020-03-18 stsp return err;
2475 2ab43947 2020-03-18 stsp if (yca_id == NULL)
2476 2ab43947 2020-03-18 stsp return got_error(GOT_ERR_ANCESTRY);
2479 2ab43947 2020-03-18 stsp * Require a straight line of history between the target commit
2480 2ab43947 2020-03-18 stsp * and the work tree's base commit.
2482 2ab43947 2020-03-18 stsp * Non-linear situations such as this require a rebase:
2484 2ab43947 2020-03-18 stsp * (commit) D F (base_commit)
2492 2ab43947 2020-03-18 stsp * 'got update' only handles linear cases:
2493 2ab43947 2020-03-18 stsp * Update forwards in time: A (base/yca) - B - C - D (commit)
2494 2ab43947 2020-03-18 stsp * Update backwards in time: D (base) - C - B - A (commit/yca)
2496 2ab43947 2020-03-18 stsp if (allow_forwards_in_time_only) {
2497 2ab43947 2020-03-18 stsp if (got_object_id_cmp(base_commit_id, yca_id) != 0)
2498 2ab43947 2020-03-18 stsp return got_error(GOT_ERR_ANCESTRY);
2499 2ab43947 2020-03-18 stsp } else if (got_object_id_cmp(commit_id, yca_id) != 0 &&
2500 2ab43947 2020-03-18 stsp got_object_id_cmp(base_commit_id, yca_id) != 0)
2501 2ab43947 2020-03-18 stsp return got_error(GOT_ERR_ANCESTRY);
2503 2ab43947 2020-03-18 stsp free(yca_id);
2504 2ab43947 2020-03-18 stsp return NULL;
2507 2ab43947 2020-03-18 stsp static const struct got_error *
2508 2ab43947 2020-03-18 stsp check_same_branch(struct got_object_id *commit_id,
2509 2ab43947 2020-03-18 stsp struct got_reference *head_ref, struct got_object_id *yca_id,
2510 2ab43947 2020-03-18 stsp struct got_repository *repo)
2512 2ab43947 2020-03-18 stsp const struct got_error *err = NULL;
2513 2ab43947 2020-03-18 stsp struct got_commit_graph *graph = NULL;
2514 2ab43947 2020-03-18 stsp struct got_object_id *head_commit_id = NULL;
2515 2ab43947 2020-03-18 stsp int is_same_branch = 0;
2517 2ab43947 2020-03-18 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
2519 2ab43947 2020-03-18 stsp goto done;
2521 2ab43947 2020-03-18 stsp if (got_object_id_cmp(head_commit_id, commit_id) == 0) {
2522 2ab43947 2020-03-18 stsp is_same_branch = 1;
2523 2ab43947 2020-03-18 stsp goto done;
2525 2ab43947 2020-03-18 stsp if (yca_id && got_object_id_cmp(commit_id, yca_id) == 0) {
2526 2ab43947 2020-03-18 stsp is_same_branch = 1;
2527 2ab43947 2020-03-18 stsp goto done;
2530 2ab43947 2020-03-18 stsp err = got_commit_graph_open(&graph, "/", 1);
2532 2ab43947 2020-03-18 stsp goto done;
2534 2ab43947 2020-03-18 stsp err = got_commit_graph_iter_start(graph, head_commit_id, repo,
2535 2ab43947 2020-03-18 stsp check_cancelled, NULL);
2537 2ab43947 2020-03-18 stsp goto done;
2539 2ab43947 2020-03-18 stsp for (;;) {
2540 2ab43947 2020-03-18 stsp struct got_object_id *id;
2541 2ab43947 2020-03-18 stsp err = got_commit_graph_iter_next(&id, graph, repo,
2542 2ab43947 2020-03-18 stsp check_cancelled, NULL);
2543 2ab43947 2020-03-18 stsp if (err) {
2544 2ab43947 2020-03-18 stsp if (err->code == GOT_ERR_ITER_COMPLETED)
2545 2ab43947 2020-03-18 stsp err = NULL;
2550 2ab43947 2020-03-18 stsp if (yca_id && got_object_id_cmp(id, yca_id) == 0)
2552 2ab43947 2020-03-18 stsp if (got_object_id_cmp(id, commit_id) == 0) {
2553 2ab43947 2020-03-18 stsp is_same_branch = 1;
2559 2ab43947 2020-03-18 stsp if (graph)
2560 2ab43947 2020-03-18 stsp got_commit_graph_close(graph);
2561 2ab43947 2020-03-18 stsp free(head_commit_id);
2562 2ab43947 2020-03-18 stsp if (!err && !is_same_branch)
2563 2ab43947 2020-03-18 stsp err = got_error(GOT_ERR_ANCESTRY);
2564 2ab43947 2020-03-18 stsp return err;
2567 4ed7e80c 2018-05-20 stsp static const struct got_error *
2568 4b6c9460 2020-03-05 stsp checkout_ancestry_error(struct got_reference *ref, const char *commit_id_str)
2570 4b6c9460 2020-03-05 stsp static char msg[512];
2571 4b6c9460 2020-03-05 stsp const char *branch_name;
2573 4b6c9460 2020-03-05 stsp if (got_ref_is_symbolic(ref))
2574 4b6c9460 2020-03-05 stsp branch_name = got_ref_get_symref_target(ref);
2576 4b6c9460 2020-03-05 stsp branch_name = got_ref_get_name(ref);
2578 4b6c9460 2020-03-05 stsp if (strncmp("refs/heads/", branch_name, 11) == 0)
2579 4b6c9460 2020-03-05 stsp branch_name += 11;
2581 4b6c9460 2020-03-05 stsp snprintf(msg, sizeof(msg),
2582 4b6c9460 2020-03-05 stsp "target commit is not contained in branch '%s'; "
2583 4b6c9460 2020-03-05 stsp "the branch to use must be specified with -b; "
2584 4b6c9460 2020-03-05 stsp "if necessary a new branch can be created for "
2585 4b6c9460 2020-03-05 stsp "this commit with 'got branch -c %s BRANCH_NAME'",
2586 4b6c9460 2020-03-05 stsp branch_name, commit_id_str);
2588 4b6c9460 2020-03-05 stsp return got_error_msg(GOT_ERR_ANCESTRY, msg);
2591 4b6c9460 2020-03-05 stsp static const struct got_error *
2592 c09a553d 2018-03-12 stsp cmd_checkout(int argc, char *argv[])
2594 c09a553d 2018-03-12 stsp const struct got_error *error = NULL;
2595 c09a553d 2018-03-12 stsp struct got_repository *repo = NULL;
2596 c09a553d 2018-03-12 stsp struct got_reference *head_ref = NULL;
2597 c09a553d 2018-03-12 stsp struct got_worktree *worktree = NULL;
2598 c09a553d 2018-03-12 stsp char *repo_path = NULL;
2599 c09a553d 2018-03-12 stsp char *worktree_path = NULL;
2600 0bb8a95e 2018-03-12 stsp const char *path_prefix = "";
2601 08573d5b 2019-05-14 stsp const char *branch_name = GOT_REF_HEAD;
2602 8069f636 2019-01-12 stsp char *commit_id_str = NULL;
2603 c47340da 2020-09-20 stsp char *cwd = NULL, *path = NULL;
2604 bb51a5b4 2020-01-13 stsp int ch, same_path_prefix, allow_nonempty = 0;
2605 f2ea84fa 2019-07-27 stsp struct got_pathlist_head paths;
2606 7f47418f 2019-12-20 stsp struct got_checkout_progress_arg cpa;
2608 f2ea84fa 2019-07-27 stsp TAILQ_INIT(&paths);
2610 bb51a5b4 2020-01-13 stsp while ((ch = getopt(argc, argv, "b:c:Ep:")) != -1) {
2611 0bb8a95e 2018-03-12 stsp switch (ch) {
2613 08573d5b 2019-05-14 stsp branch_name = optarg;
2616 8069f636 2019-01-12 stsp commit_id_str = strdup(optarg);
2617 8069f636 2019-01-12 stsp if (commit_id_str == NULL)
2618 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
2621 bb51a5b4 2020-01-13 stsp allow_nonempty = 1;
2624 0bb8a95e 2018-03-12 stsp path_prefix = optarg;
2627 2deda0b9 2019-03-07 stsp usage_checkout();
2628 0bb8a95e 2018-03-12 stsp /* NOTREACHED */
2632 0bb8a95e 2018-03-12 stsp argc -= optind;
2633 0bb8a95e 2018-03-12 stsp argv += optind;
2635 6715a751 2018-03-16 stsp #ifndef PROFILE
2636 68ed9ba5 2019-02-10 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
2637 68ed9ba5 2019-02-10 stsp "unveil", NULL) == -1)
2638 c09a553d 2018-03-12 stsp err(1, "pledge");
2640 0bb8a95e 2018-03-12 stsp if (argc == 1) {
2641 c47340da 2020-09-20 stsp char *base, *dotgit;
2642 76089277 2018-04-01 stsp repo_path = realpath(argv[0], NULL);
2643 76089277 2018-04-01 stsp if (repo_path == NULL)
2644 638f9024 2019-05-13 stsp return got_error_from_errno2("realpath", argv[0]);
2645 c09a553d 2018-03-12 stsp cwd = getcwd(NULL, 0);
2646 76089277 2018-04-01 stsp if (cwd == NULL) {
2647 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2648 76089277 2018-04-01 stsp goto done;
2650 c47340da 2020-09-20 stsp if (path_prefix[0])
2651 c47340da 2020-09-20 stsp path = strdup(path_prefix);
2653 c47340da 2020-09-20 stsp path = strdup(repo_path);
2654 c47340da 2020-09-20 stsp if (path == NULL) {
2655 c47340da 2020-09-20 stsp error = got_error_from_errno("strdup");
2656 c47340da 2020-09-20 stsp goto done;
2658 c47340da 2020-09-20 stsp base = basename(path);
2659 c47340da 2020-09-20 stsp if (base == NULL) {
2660 c47340da 2020-09-20 stsp error = got_error_from_errno2("basename", path);
2661 c47340da 2020-09-20 stsp goto done;
2663 c09a553d 2018-03-12 stsp dotgit = strstr(base, ".git");
2664 c09a553d 2018-03-12 stsp if (dotgit)
2665 c09a553d 2018-03-12 stsp *dotgit = '\0';
2666 c09a553d 2018-03-12 stsp if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
2667 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
2668 76089277 2018-04-01 stsp goto done;
2670 0bb8a95e 2018-03-12 stsp } else if (argc == 2) {
2671 76089277 2018-04-01 stsp repo_path = realpath(argv[0], NULL);
2672 76089277 2018-04-01 stsp if (repo_path == NULL) {
2673 638f9024 2019-05-13 stsp error = got_error_from_errno2("realpath", argv[0]);
2674 76089277 2018-04-01 stsp goto done;
2676 f7b38925 2018-04-01 stsp worktree_path = realpath(argv[1], NULL);
2677 76089277 2018-04-01 stsp if (worktree_path == NULL) {
2678 b4b3a7dd 2019-07-22 stsp if (errno != ENOENT) {
2679 b4b3a7dd 2019-07-22 stsp error = got_error_from_errno2("realpath",
2681 b4b3a7dd 2019-07-22 stsp goto done;
2683 b4b3a7dd 2019-07-22 stsp worktree_path = strdup(argv[1]);
2684 b4b3a7dd 2019-07-22 stsp if (worktree_path == NULL) {
2685 b4b3a7dd 2019-07-22 stsp error = got_error_from_errno("strdup");
2686 b4b3a7dd 2019-07-22 stsp goto done;
2690 c09a553d 2018-03-12 stsp usage_checkout();
2692 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
2693 72151b04 2019-05-11 stsp got_path_strip_trailing_slashes(worktree_path);
2695 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
2696 c09a553d 2018-03-12 stsp if (error != NULL)
2697 c02c541e 2019-03-29 stsp goto done;
2699 c530dc23 2019-07-23 stsp /* Pre-create work tree path for unveil(2) */
2700 c530dc23 2019-07-23 stsp error = got_path_mkdir(worktree_path);
2701 c530dc23 2019-07-23 stsp if (error) {
2702 80c1b583 2019-08-07 stsp if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
2703 80c1b583 2019-08-07 stsp !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
2704 c530dc23 2019-07-23 stsp goto done;
2705 bb51a5b4 2020-01-13 stsp if (!allow_nonempty &&
2706 bb51a5b4 2020-01-13 stsp !got_path_dir_is_empty(worktree_path)) {
2707 c530dc23 2019-07-23 stsp error = got_error_path(worktree_path,
2708 c530dc23 2019-07-23 stsp GOT_ERR_DIR_NOT_EMPTY);
2709 c530dc23 2019-07-23 stsp goto done;
2713 c530dc23 2019-07-23 stsp error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
2714 c02c541e 2019-03-29 stsp if (error)
2715 c09a553d 2018-03-12 stsp goto done;
2717 08573d5b 2019-05-14 stsp error = got_ref_open(&head_ref, repo, branch_name, 0);
2718 c09a553d 2018-03-12 stsp if (error != NULL)
2719 c09a553d 2018-03-12 stsp goto done;
2721 0bb8a95e 2018-03-12 stsp error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
2722 d70b8e30 2018-12-27 stsp if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
2723 c09a553d 2018-03-12 stsp goto done;
2725 c09a553d 2018-03-12 stsp error = got_worktree_open(&worktree, worktree_path);
2726 c09a553d 2018-03-12 stsp if (error != NULL)
2727 c09a553d 2018-03-12 stsp goto done;
2729 e5dc7198 2018-12-29 stsp error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
2730 e5dc7198 2018-12-29 stsp path_prefix);
2731 e5dc7198 2018-12-29 stsp if (error != NULL)
2732 e5dc7198 2018-12-29 stsp goto done;
2733 e5dc7198 2018-12-29 stsp if (!same_path_prefix) {
2734 49520a32 2018-12-29 stsp error = got_error(GOT_ERR_PATH_PREFIX);
2735 49520a32 2018-12-29 stsp goto done;
2738 8069f636 2019-01-12 stsp if (commit_id_str) {
2739 04f57cb3 2019-07-25 stsp struct got_object_id *commit_id;
2740 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
2741 71a27632 2020-01-15 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
2742 30837e32 2019-07-25 stsp if (error)
2743 8069f636 2019-01-12 stsp goto done;
2744 024e9686 2019-05-14 stsp error = check_linear_ancestry(commit_id,
2745 3aef623b 2019-10-15 stsp got_worktree_get_base_commit_id(worktree), 0, repo);
2746 8069f636 2019-01-12 stsp if (error != NULL) {
2747 8069f636 2019-01-12 stsp free(commit_id);
2748 4b6c9460 2020-03-05 stsp if (error->code == GOT_ERR_ANCESTRY) {
2749 4b6c9460 2020-03-05 stsp error = checkout_ancestry_error(
2750 4b6c9460 2020-03-05 stsp head_ref, commit_id_str);
2752 8069f636 2019-01-12 stsp goto done;
2754 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
2755 4b6c9460 2020-03-05 stsp if (error) {
2756 4b6c9460 2020-03-05 stsp if (error->code == GOT_ERR_ANCESTRY) {
2757 4b6c9460 2020-03-05 stsp error = checkout_ancestry_error(
2758 4b6c9460 2020-03-05 stsp head_ref, commit_id_str);
2760 45d344f6 2019-05-14 stsp goto done;
2762 8069f636 2019-01-12 stsp error = got_worktree_set_base_commit_id(worktree, repo,
2763 8069f636 2019-01-12 stsp commit_id);
2764 8069f636 2019-01-12 stsp free(commit_id);
2765 8069f636 2019-01-12 stsp if (error)
2766 8069f636 2019-01-12 stsp goto done;
2769 adc19d55 2019-07-28 stsp error = got_pathlist_append(&paths, "", NULL);
2770 f2ea84fa 2019-07-27 stsp if (error)
2771 f2ea84fa 2019-07-27 stsp goto done;
2772 7f47418f 2019-12-20 stsp cpa.worktree_path = worktree_path;
2773 7f47418f 2019-12-20 stsp cpa.had_base_commit_ref_error = 0;
2774 f2ea84fa 2019-07-27 stsp error = got_worktree_checkout_files(worktree, &paths, repo,
2775 7f47418f 2019-12-20 stsp checkout_progress, &cpa, check_cancelled, NULL);
2776 c09a553d 2018-03-12 stsp if (error != NULL)
2777 c09a553d 2018-03-12 stsp goto done;
2779 b65ae19a 2018-04-24 stsp printf("Now shut up and hack\n");
2780 7f47418f 2019-12-20 stsp if (cpa.had_base_commit_ref_error)
2781 7f47418f 2019-12-20 stsp show_worktree_base_ref_warning();
2783 f2ea84fa 2019-07-27 stsp got_pathlist_free(&paths);
2784 8069f636 2019-01-12 stsp free(commit_id_str);
2785 76089277 2018-04-01 stsp free(repo_path);
2786 507dc3bb 2018-12-29 stsp free(worktree_path);
2787 c47340da 2020-09-20 stsp free(cwd);
2788 c47340da 2020-09-20 stsp free(path);
2789 507dc3bb 2018-12-29 stsp return error;
2792 9627c110 2020-04-18 stsp struct got_update_progress_arg {
2793 9627c110 2020-04-18 stsp int did_something;
2794 9627c110 2020-04-18 stsp int conflicts;
2795 9627c110 2020-04-18 stsp int obstructed;
2796 9627c110 2020-04-18 stsp int not_updated;
2800 9627c110 2020-04-18 stsp print_update_progress_stats(struct got_update_progress_arg *upa)
2802 9627c110 2020-04-18 stsp if (!upa->did_something)
2805 9627c110 2020-04-18 stsp if (upa->conflicts > 0)
2806 9627c110 2020-04-18 stsp printf("Files with new merge conflicts: %d\n", upa->conflicts);
2807 9627c110 2020-04-18 stsp if (upa->obstructed > 0)
2808 9627c110 2020-04-18 stsp printf("File paths obstructed by a non-regular file: %d\n",
2809 9627c110 2020-04-18 stsp upa->obstructed);
2810 9627c110 2020-04-18 stsp if (upa->not_updated > 0)
2811 9627c110 2020-04-18 stsp printf("Files not updated because of existing merge "
2812 9627c110 2020-04-18 stsp "conflicts: %d\n", upa->not_updated);
2815 507dc3bb 2018-12-29 stsp __dead static void
2816 507dc3bb 2018-12-29 stsp usage_update(void)
2818 f2ea84fa 2019-07-27 stsp fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path ...]\n",
2819 507dc3bb 2018-12-29 stsp getprogname());
2823 1ee397ad 2019-07-12 stsp static const struct got_error *
2824 507dc3bb 2018-12-29 stsp update_progress(void *arg, unsigned char status, const char *path)
2826 9627c110 2020-04-18 stsp struct got_update_progress_arg *upa = arg;
2828 7f47418f 2019-12-20 stsp if (status == GOT_STATUS_EXISTS ||
2829 7f47418f 2019-12-20 stsp status == GOT_STATUS_BASE_REF_ERR)
2830 1ee397ad 2019-07-12 stsp return NULL;
2832 9627c110 2020-04-18 stsp upa->did_something = 1;
2834 a484d721 2019-06-10 stsp /* Base commit bump happens silently. */
2835 a484d721 2019-06-10 stsp if (status == GOT_STATUS_BUMP_BASE)
2836 1ee397ad 2019-07-12 stsp return NULL;
2838 9627c110 2020-04-18 stsp if (status == GOT_STATUS_CONFLICT)
2839 9627c110 2020-04-18 stsp upa->conflicts++;
2840 9627c110 2020-04-18 stsp if (status == GOT_STATUS_OBSTRUCTED)
2841 9627c110 2020-04-18 stsp upa->obstructed++;
2842 9627c110 2020-04-18 stsp if (status == GOT_STATUS_CANNOT_UPDATE)
2843 9627c110 2020-04-18 stsp upa->not_updated++;
2845 507dc3bb 2018-12-29 stsp while (path[0] == '/')
2847 507dc3bb 2018-12-29 stsp printf("%c %s\n", status, path);
2848 1ee397ad 2019-07-12 stsp return NULL;
2851 be7061eb 2018-12-30 stsp static const struct got_error *
2852 a1fb16d8 2019-05-24 stsp switch_head_ref(struct got_reference *head_ref,
2853 a1fb16d8 2019-05-24 stsp struct got_object_id *commit_id, struct got_worktree *worktree,
2854 a1fb16d8 2019-05-24 stsp struct got_repository *repo)
2856 a1fb16d8 2019-05-24 stsp const struct got_error *err = NULL;
2857 a1fb16d8 2019-05-24 stsp char *base_id_str;
2858 a1fb16d8 2019-05-24 stsp int ref_has_moved = 0;
2860 a1fb16d8 2019-05-24 stsp /* Trivial case: switching between two different references. */
2861 a1fb16d8 2019-05-24 stsp if (strcmp(got_ref_get_name(head_ref),
2862 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree)) != 0) {
2863 a1fb16d8 2019-05-24 stsp printf("Switching work tree from %s to %s\n",
2864 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree),
2865 a1fb16d8 2019-05-24 stsp got_ref_get_name(head_ref));
2866 a1fb16d8 2019-05-24 stsp return got_worktree_set_head_ref(worktree, head_ref);
2869 a1fb16d8 2019-05-24 stsp err = check_linear_ancestry(commit_id,
2870 3aef623b 2019-10-15 stsp got_worktree_get_base_commit_id(worktree), 0, repo);
2871 a1fb16d8 2019-05-24 stsp if (err) {
2872 a1fb16d8 2019-05-24 stsp if (err->code != GOT_ERR_ANCESTRY)
2873 a1fb16d8 2019-05-24 stsp return err;
2874 a1fb16d8 2019-05-24 stsp ref_has_moved = 1;
2876 a1fb16d8 2019-05-24 stsp if (!ref_has_moved)
2877 a1fb16d8 2019-05-24 stsp return NULL;
2879 a1fb16d8 2019-05-24 stsp /* Switching to a rebased branch with the same reference name. */
2880 a1fb16d8 2019-05-24 stsp err = got_object_id_str(&base_id_str,
2881 a1fb16d8 2019-05-24 stsp got_worktree_get_base_commit_id(worktree));
2883 a1fb16d8 2019-05-24 stsp return err;
2884 a1fb16d8 2019-05-24 stsp printf("Reference %s now points at a different branch\n",
2885 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree));
2886 a1fb16d8 2019-05-24 stsp printf("Switching work tree from %s to %s\n", base_id_str,
2887 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree));
2888 0ebf8283 2019-07-24 stsp return NULL;
2891 0ebf8283 2019-07-24 stsp static const struct got_error *
2892 0ebf8283 2019-07-24 stsp check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
2894 0ebf8283 2019-07-24 stsp const struct got_error *err;
2895 0ebf8283 2019-07-24 stsp int in_progress;
2897 0ebf8283 2019-07-24 stsp err = got_worktree_rebase_in_progress(&in_progress, worktree);
2899 0ebf8283 2019-07-24 stsp return err;
2900 0ebf8283 2019-07-24 stsp if (in_progress)
2901 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_REBASING);
2903 0ebf8283 2019-07-24 stsp err = got_worktree_histedit_in_progress(&in_progress, worktree);
2905 0ebf8283 2019-07-24 stsp return err;
2906 0ebf8283 2019-07-24 stsp if (in_progress)
2907 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_HISTEDIT_BUSY);
2909 a1fb16d8 2019-05-24 stsp return NULL;
2912 a5edda0a 2019-07-27 stsp static const struct got_error *
2913 a5edda0a 2019-07-27 stsp get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
2914 a5edda0a 2019-07-27 stsp char *argv[], struct got_worktree *worktree)
2916 a0de39f3 2019-08-09 stsp const struct got_error *err = NULL;
2917 a5edda0a 2019-07-27 stsp char *path;
2920 a5edda0a 2019-07-27 stsp if (argc == 0) {
2921 a5edda0a 2019-07-27 stsp path = strdup("");
2922 a5edda0a 2019-07-27 stsp if (path == NULL)
2923 a5edda0a 2019-07-27 stsp return got_error_from_errno("strdup");
2924 adc19d55 2019-07-28 stsp return got_pathlist_append(paths, path, NULL);
2927 a5edda0a 2019-07-27 stsp for (i = 0; i < argc; i++) {
2928 a5edda0a 2019-07-27 stsp err = got_worktree_resolve_path(&path, worktree, argv[i]);
2931 adc19d55 2019-07-28 stsp err = got_pathlist_append(paths, path, NULL);
2932 a5edda0a 2019-07-27 stsp if (err) {
2933 a5edda0a 2019-07-27 stsp free(path);
2938 a5edda0a 2019-07-27 stsp return err;
2941 a1fb16d8 2019-05-24 stsp static const struct got_error *
2942 fa51e947 2020-03-27 stsp wrap_not_worktree_error(const struct got_error *orig_err,
2943 fa51e947 2020-03-27 stsp const char *cmdname, const char *path)
2945 fa51e947 2020-03-27 stsp const struct got_error *err;
2946 fa51e947 2020-03-27 stsp struct got_repository *repo;
2947 fa51e947 2020-03-27 stsp static char msg[512];
2949 fa51e947 2020-03-27 stsp err = got_repo_open(&repo, path, NULL);
2951 fa51e947 2020-03-27 stsp return orig_err;
2953 fa51e947 2020-03-27 stsp snprintf(msg, sizeof(msg),
2954 fa51e947 2020-03-27 stsp "'got %s' needs a work tree in addition to a git repository\n"
2955 fa51e947 2020-03-27 stsp "Work trees can be checked out from this Git repository with "
2956 fa51e947 2020-03-27 stsp "'got checkout'.\n"
2957 fa51e947 2020-03-27 stsp "The got(1) manual page contains more information.", cmdname);
2958 fa51e947 2020-03-27 stsp err = got_error_msg(GOT_ERR_NOT_WORKTREE, msg);
2959 fa51e947 2020-03-27 stsp got_repo_close(repo);
2960 fa51e947 2020-03-27 stsp return err;
2963 fa51e947 2020-03-27 stsp static const struct got_error *
2964 507dc3bb 2018-12-29 stsp cmd_update(int argc, char *argv[])
2966 507dc3bb 2018-12-29 stsp const struct got_error *error = NULL;
2967 507dc3bb 2018-12-29 stsp struct got_repository *repo = NULL;
2968 507dc3bb 2018-12-29 stsp struct got_worktree *worktree = NULL;
2969 f2ea84fa 2019-07-27 stsp char *worktree_path = NULL;
2970 507dc3bb 2018-12-29 stsp struct got_object_id *commit_id = NULL;
2971 9c4b8182 2019-01-02 stsp char *commit_id_str = NULL;
2972 024e9686 2019-05-14 stsp const char *branch_name = NULL;
2973 024e9686 2019-05-14 stsp struct got_reference *head_ref = NULL;
2974 f2ea84fa 2019-07-27 stsp struct got_pathlist_head paths;
2975 f2ea84fa 2019-07-27 stsp struct got_pathlist_entry *pe;
2977 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
2979 f2ea84fa 2019-07-27 stsp TAILQ_INIT(&paths);
2981 024e9686 2019-05-14 stsp while ((ch = getopt(argc, argv, "b:c:")) != -1) {
2982 507dc3bb 2018-12-29 stsp switch (ch) {
2984 024e9686 2019-05-14 stsp branch_name = optarg;
2987 9c4b8182 2019-01-02 stsp commit_id_str = strdup(optarg);
2988 9c4b8182 2019-01-02 stsp if (commit_id_str == NULL)
2989 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
2992 2deda0b9 2019-03-07 stsp usage_update();
2993 507dc3bb 2018-12-29 stsp /* NOTREACHED */
2997 507dc3bb 2018-12-29 stsp argc -= optind;
2998 507dc3bb 2018-12-29 stsp argv += optind;
3000 507dc3bb 2018-12-29 stsp #ifndef PROFILE
3001 68ed9ba5 2019-02-10 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3002 68ed9ba5 2019-02-10 stsp "unveil", NULL) == -1)
3003 507dc3bb 2018-12-29 stsp err(1, "pledge");
3005 c4cdcb68 2019-04-03 stsp worktree_path = getcwd(NULL, 0);
3006 c4cdcb68 2019-04-03 stsp if (worktree_path == NULL) {
3007 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
3008 c4cdcb68 2019-04-03 stsp goto done;
3010 c4cdcb68 2019-04-03 stsp error = got_worktree_open(&worktree, worktree_path);
3011 fa51e947 2020-03-27 stsp if (error) {
3012 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
3013 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "update",
3014 fa51e947 2020-03-27 stsp worktree_path);
3015 7d5807f4 2019-07-11 stsp goto done;
3018 0ebf8283 2019-07-24 stsp error = check_rebase_or_histedit_in_progress(worktree);
3019 f2ea84fa 2019-07-27 stsp if (error)
3020 f2ea84fa 2019-07-27 stsp goto done;
3022 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
3024 507dc3bb 2018-12-29 stsp if (error != NULL)
3025 507dc3bb 2018-12-29 stsp goto done;
3027 97430839 2019-03-11 stsp error = apply_unveil(got_repo_get_path(repo), 0,
3028 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
3029 087fb88c 2019-08-04 stsp if (error)
3030 087fb88c 2019-08-04 stsp goto done;
3032 087fb88c 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3033 0266afb7 2019-01-04 stsp if (error)
3034 0266afb7 2019-01-04 stsp goto done;
3036 a1fb16d8 2019-05-24 stsp error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
3037 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree), 0);
3038 024e9686 2019-05-14 stsp if (error != NULL)
3039 024e9686 2019-05-14 stsp goto done;
3040 507dc3bb 2018-12-29 stsp if (commit_id_str == NULL) {
3041 507dc3bb 2018-12-29 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
3042 9c4b8182 2019-01-02 stsp if (error != NULL)
3043 9c4b8182 2019-01-02 stsp goto done;
3044 9c4b8182 2019-01-02 stsp error = got_object_id_str(&commit_id_str, commit_id);
3045 507dc3bb 2018-12-29 stsp if (error != NULL)
3046 507dc3bb 2018-12-29 stsp goto done;
3048 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
3049 71a27632 2020-01-15 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
3050 04f57cb3 2019-07-25 stsp free(commit_id_str);
3051 bb787f09 2019-07-25 stsp commit_id_str = NULL;
3052 30837e32 2019-07-25 stsp if (error)
3053 a297e751 2019-07-11 stsp goto done;
3054 a297e751 2019-07-11 stsp error = got_object_id_str(&commit_id_str, commit_id);
3055 a297e751 2019-07-11 stsp if (error)
3056 507dc3bb 2018-12-29 stsp goto done;
3059 a1fb16d8 2019-05-24 stsp if (branch_name) {
3060 024e9686 2019-05-14 stsp struct got_object_id *head_commit_id;
3061 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, &paths, entry) {
3062 f2b16ada 2019-08-02 stsp if (pe->path_len == 0)
3064 f2ea84fa 2019-07-27 stsp error = got_error_msg(GOT_ERR_BAD_PATH,
3065 f2ea84fa 2019-07-27 stsp "switching between branches requires that "
3066 f2ea84fa 2019-07-27 stsp "the entire work tree gets updated");
3067 024e9686 2019-05-14 stsp goto done;
3069 024e9686 2019-05-14 stsp error = got_ref_resolve(&head_commit_id, repo, head_ref);
3070 024e9686 2019-05-14 stsp if (error)
3071 024e9686 2019-05-14 stsp goto done;
3072 3aef623b 2019-10-15 stsp error = check_linear_ancestry(commit_id, head_commit_id, 0,
3074 a367ff0f 2019-05-14 stsp free(head_commit_id);
3075 024e9686 2019-05-14 stsp if (error != NULL)
3076 024e9686 2019-05-14 stsp goto done;
3077 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
3078 a367ff0f 2019-05-14 stsp if (error)
3079 a367ff0f 2019-05-14 stsp goto done;
3080 a1fb16d8 2019-05-24 stsp error = switch_head_ref(head_ref, commit_id, worktree, repo);
3081 024e9686 2019-05-14 stsp if (error)
3082 024e9686 2019-05-14 stsp goto done;
3084 024e9686 2019-05-14 stsp error = check_linear_ancestry(commit_id,
3085 3aef623b 2019-10-15 stsp got_worktree_get_base_commit_id(worktree), 0, repo);
3086 a367ff0f 2019-05-14 stsp if (error != NULL) {
3087 a367ff0f 2019-05-14 stsp if (error->code == GOT_ERR_ANCESTRY)
3088 a367ff0f 2019-05-14 stsp error = got_error(GOT_ERR_BRANCH_MOVED);
3089 024e9686 2019-05-14 stsp goto done;
3091 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
3092 a367ff0f 2019-05-14 stsp if (error)
3093 a367ff0f 2019-05-14 stsp goto done;
3096 507dc3bb 2018-12-29 stsp if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
3097 507dc3bb 2018-12-29 stsp commit_id) != 0) {
3098 507dc3bb 2018-12-29 stsp error = got_worktree_set_base_commit_id(worktree, repo,
3099 507dc3bb 2018-12-29 stsp commit_id);
3100 507dc3bb 2018-12-29 stsp if (error)
3101 507dc3bb 2018-12-29 stsp goto done;
3104 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
3105 f2ea84fa 2019-07-27 stsp error = got_worktree_checkout_files(worktree, &paths, repo,
3106 9627c110 2020-04-18 stsp update_progress, &upa, check_cancelled, NULL);
3107 507dc3bb 2018-12-29 stsp if (error != NULL)
3108 507dc3bb 2018-12-29 stsp goto done;
3110 9627c110 2020-04-18 stsp if (upa.did_something)
3111 784955db 2019-01-12 stsp printf("Updated to commit %s\n", commit_id_str);
3113 784955db 2019-01-12 stsp printf("Already up-to-date\n");
3114 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
3116 c09a553d 2018-03-12 stsp free(worktree_path);
3117 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, &paths, entry)
3118 f2ea84fa 2019-07-27 stsp free((char *)pe->path);
3119 f2ea84fa 2019-07-27 stsp got_pathlist_free(&paths);
3120 507dc3bb 2018-12-29 stsp free(commit_id);
3121 9c4b8182 2019-01-02 stsp free(commit_id_str);
3122 c09a553d 2018-03-12 stsp return error;
3125 f42b1b34 2018-03-12 stsp static const struct got_error *
3126 44392932 2019-08-25 stsp diff_blobs(struct got_object_id *blob_id1, struct got_object_id *blob_id2,
3127 63035f9f 2019-10-06 stsp const char *path, int diff_context, int ignore_whitespace,
3128 63035f9f 2019-10-06 stsp struct got_repository *repo)
3130 f42b1b34 2018-03-12 stsp const struct got_error *err = NULL;
3131 44392932 2019-08-25 stsp struct got_blob_object *blob1 = NULL, *blob2 = NULL;
3133 44392932 2019-08-25 stsp if (blob_id1) {
3134 44392932 2019-08-25 stsp err = got_object_open_as_blob(&blob1, repo, blob_id1, 8192);
3136 44392932 2019-08-25 stsp goto done;
3139 44392932 2019-08-25 stsp err = got_object_open_as_blob(&blob2, repo, blob_id2, 8192);
3141 44392932 2019-08-25 stsp goto done;
3143 44392932 2019-08-25 stsp while (path[0] == '/')
3145 44392932 2019-08-25 stsp err = got_diff_blob(blob1, blob2, path, path, diff_context,
3146 63035f9f 2019-10-06 stsp ignore_whitespace, stdout);
3148 44392932 2019-08-25 stsp if (blob1)
3149 44392932 2019-08-25 stsp got_object_blob_close(blob1);
3150 44392932 2019-08-25 stsp got_object_blob_close(blob2);
3151 44392932 2019-08-25 stsp return err;
3154 44392932 2019-08-25 stsp static const struct got_error *
3155 44392932 2019-08-25 stsp diff_trees(struct got_object_id *tree_id1, struct got_object_id *tree_id2,
3156 63035f9f 2019-10-06 stsp const char *path, int diff_context, int ignore_whitespace,
3157 63035f9f 2019-10-06 stsp struct got_repository *repo)
3159 44392932 2019-08-25 stsp const struct got_error *err = NULL;
3160 44392932 2019-08-25 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3161 44392932 2019-08-25 stsp struct got_diff_blob_output_unidiff_arg arg;
3163 44392932 2019-08-25 stsp if (tree_id1) {
3164 44392932 2019-08-25 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
3166 44392932 2019-08-25 stsp goto done;
3169 44392932 2019-08-25 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
3171 0f2b3dca 2018-12-22 stsp goto done;
3173 aaa13589 2019-06-01 stsp arg.diff_context = diff_context;
3174 63035f9f 2019-10-06 stsp arg.ignore_whitespace = ignore_whitespace;
3175 aaa13589 2019-06-01 stsp arg.outfile = stdout;
3176 44392932 2019-08-25 stsp while (path[0] == '/')
3178 44392932 2019-08-25 stsp err = got_diff_tree(tree1, tree2, path, path, repo,
3179 31b4484f 2019-07-27 stsp got_diff_blob_output_unidiff, &arg, 1);
3181 79109fed 2018-03-27 stsp if (tree1)
3182 79109fed 2018-03-27 stsp got_object_tree_close(tree1);
3183 366e0a5f 2019-10-10 stsp if (tree2)
3184 366e0a5f 2019-10-10 stsp got_object_tree_close(tree2);
3185 44392932 2019-08-25 stsp return err;
3188 44392932 2019-08-25 stsp static const struct got_error *
3189 0208f208 2020-05-05 stsp get_changed_paths(struct got_pathlist_head *paths,
3190 0208f208 2020-05-05 stsp struct got_commit_object *commit, struct got_repository *repo)
3192 0208f208 2020-05-05 stsp const struct got_error *err = NULL;
3193 0208f208 2020-05-05 stsp struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3194 0208f208 2020-05-05 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3195 0208f208 2020-05-05 stsp struct got_object_qid *qid;
3197 0208f208 2020-05-05 stsp qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3198 0208f208 2020-05-05 stsp if (qid != NULL) {
3199 0208f208 2020-05-05 stsp struct got_commit_object *pcommit;
3200 0208f208 2020-05-05 stsp err = got_object_open_as_commit(&pcommit, repo,
3203 0208f208 2020-05-05 stsp return err;
3205 0208f208 2020-05-05 stsp tree_id1 = got_object_commit_get_tree_id(pcommit);
3206 0208f208 2020-05-05 stsp got_object_commit_close(pcommit);
3210 0208f208 2020-05-05 stsp if (tree_id1) {
3211 0208f208 2020-05-05 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
3213 0208f208 2020-05-05 stsp goto done;
3216 0208f208 2020-05-05 stsp tree_id2 = got_object_commit_get_tree_id(commit);
3217 0208f208 2020-05-05 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
3219 0208f208 2020-05-05 stsp goto done;
3221 0208f208 2020-05-05 stsp err = got_diff_tree(tree1, tree2, "", "", repo,
3222 0208f208 2020-05-05 stsp got_diff_tree_collect_changed_paths, paths, 0);
3224 0208f208 2020-05-05 stsp if (tree1)
3225 0208f208 2020-05-05 stsp got_object_tree_close(tree1);
3226 0208f208 2020-05-05 stsp if (tree2)
3227 0208f208 2020-05-05 stsp got_object_tree_close(tree2);
3228 0208f208 2020-05-05 stsp return err;
3231 0208f208 2020-05-05 stsp static const struct got_error *
3232 44392932 2019-08-25 stsp print_patch(struct got_commit_object *commit, struct got_object_id *id,
3233 44392932 2019-08-25 stsp const char *path, int diff_context, struct got_repository *repo)
3235 44392932 2019-08-25 stsp const struct got_error *err = NULL;
3236 44392932 2019-08-25 stsp struct got_commit_object *pcommit = NULL;
3237 44392932 2019-08-25 stsp char *id_str1 = NULL, *id_str2 = NULL;
3238 44392932 2019-08-25 stsp struct got_object_id *obj_id1 = NULL, *obj_id2 = NULL;
3239 44392932 2019-08-25 stsp struct got_object_qid *qid;
3241 44392932 2019-08-25 stsp qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3242 44392932 2019-08-25 stsp if (qid != NULL) {
3243 44392932 2019-08-25 stsp err = got_object_open_as_commit(&pcommit, repo,
3246 44392932 2019-08-25 stsp return err;
3249 44392932 2019-08-25 stsp if (path && path[0] != '\0') {
3250 44392932 2019-08-25 stsp int obj_type;
3251 44392932 2019-08-25 stsp err = got_object_id_by_path(&obj_id2, repo, id, path);
3253 44392932 2019-08-25 stsp goto done;
3254 44392932 2019-08-25 stsp err = got_object_id_str(&id_str2, obj_id2);
3255 44392932 2019-08-25 stsp if (err) {
3256 44392932 2019-08-25 stsp free(obj_id2);
3257 44392932 2019-08-25 stsp goto done;
3259 44392932 2019-08-25 stsp if (pcommit) {
3260 44392932 2019-08-25 stsp err = got_object_id_by_path(&obj_id1, repo,
3261 44392932 2019-08-25 stsp qid->id, path);
3262 44392932 2019-08-25 stsp if (err) {
3263 2e8c69d1 2020-05-04 stsp if (err->code != GOT_ERR_NO_TREE_ENTRY) {
3264 2e8c69d1 2020-05-04 stsp free(obj_id2);
3265 2e8c69d1 2020-05-04 stsp goto done;
3268 2e8c69d1 2020-05-04 stsp err = got_object_id_str(&id_str1, obj_id1);
3269 2e8c69d1 2020-05-04 stsp if (err) {
3270 2e8c69d1 2020-05-04 stsp free(obj_id2);
3271 2e8c69d1 2020-05-04 stsp goto done;
3275 44392932 2019-08-25 stsp err = got_object_get_type(&obj_type, repo, obj_id2);
3276 44392932 2019-08-25 stsp if (err) {
3277 44392932 2019-08-25 stsp free(obj_id2);
3278 44392932 2019-08-25 stsp goto done;
3280 44392932 2019-08-25 stsp printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
3281 44392932 2019-08-25 stsp switch (obj_type) {
3282 44392932 2019-08-25 stsp case GOT_OBJ_TYPE_BLOB:
3283 44392932 2019-08-25 stsp err = diff_blobs(obj_id1, obj_id2, path, diff_context,
3286 44392932 2019-08-25 stsp case GOT_OBJ_TYPE_TREE:
3287 44392932 2019-08-25 stsp err = diff_trees(obj_id1, obj_id2, path, diff_context,
3291 44392932 2019-08-25 stsp err = got_error(GOT_ERR_OBJ_TYPE);
3294 44392932 2019-08-25 stsp free(obj_id1);
3295 44392932 2019-08-25 stsp free(obj_id2);
3297 44392932 2019-08-25 stsp obj_id2 = got_object_commit_get_tree_id(commit);
3298 44392932 2019-08-25 stsp err = got_object_id_str(&id_str2, obj_id2);
3300 44392932 2019-08-25 stsp goto done;
3301 44392932 2019-08-25 stsp obj_id1 = got_object_commit_get_tree_id(pcommit);
3302 44392932 2019-08-25 stsp err = got_object_id_str(&id_str1, obj_id1);
3304 44392932 2019-08-25 stsp goto done;
3305 44392932 2019-08-25 stsp printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
3306 63035f9f 2019-10-06 stsp err = diff_trees(obj_id1, obj_id2, "", diff_context, 0, repo);
3309 0f2b3dca 2018-12-22 stsp free(id_str1);
3310 0f2b3dca 2018-12-22 stsp free(id_str2);
3311 44392932 2019-08-25 stsp if (pcommit)
3312 44392932 2019-08-25 stsp got_object_commit_close(pcommit);
3313 79109fed 2018-03-27 stsp return err;
3316 4bb494d5 2018-06-16 stsp static char *
3317 6c281f94 2018-06-11 stsp get_datestr(time_t *time, char *datebuf)
3319 09867e48 2019-08-13 stsp struct tm mytm, *tm;
3320 09867e48 2019-08-13 stsp char *p, *s;
3322 09867e48 2019-08-13 stsp tm = gmtime_r(time, &mytm);
3323 09867e48 2019-08-13 stsp if (tm == NULL)
3324 09867e48 2019-08-13 stsp return NULL;
3325 09867e48 2019-08-13 stsp s = asctime_r(tm, datebuf);
3326 09867e48 2019-08-13 stsp if (s == NULL)
3327 09867e48 2019-08-13 stsp return NULL;
3328 6c281f94 2018-06-11 stsp p = strchr(s, '\n');
3330 6c281f94 2018-06-11 stsp *p = '\0';
3334 6841bf13 2019-11-29 kn static const struct got_error *
3335 6841bf13 2019-11-29 kn match_logmsg(int *have_match, struct got_object_id *id,
3336 6841bf13 2019-11-29 kn struct got_commit_object *commit, regex_t *regex)
3338 6841bf13 2019-11-29 kn const struct got_error *err = NULL;
3339 6841bf13 2019-11-29 kn regmatch_t regmatch;
3340 6841bf13 2019-11-29 kn char *id_str = NULL, *logmsg = NULL;
3342 6841bf13 2019-11-29 kn *have_match = 0;
3344 6841bf13 2019-11-29 kn err = got_object_id_str(&id_str, id);
3348 6841bf13 2019-11-29 kn err = got_object_commit_get_logmsg(&logmsg, commit);
3352 6841bf13 2019-11-29 kn if (regexec(regex, logmsg, 1, ®match, 0) == 0)
3353 6841bf13 2019-11-29 kn *have_match = 1;
3355 6841bf13 2019-11-29 kn free(id_str);
3356 6841bf13 2019-11-29 kn free(logmsg);
3360 0208f208 2020-05-05 stsp static void
3361 0208f208 2020-05-05 stsp match_changed_paths(int *have_match, struct got_pathlist_head *changed_paths,
3362 0208f208 2020-05-05 stsp regex_t *regex)
3364 0208f208 2020-05-05 stsp regmatch_t regmatch;
3365 0208f208 2020-05-05 stsp struct got_pathlist_entry *pe;
3367 0208f208 2020-05-05 stsp *have_match = 0;
3369 0208f208 2020-05-05 stsp TAILQ_FOREACH(pe, changed_paths, entry) {
3370 0208f208 2020-05-05 stsp if (regexec(regex, pe->path, 1, ®match, 0) == 0) {
3371 0208f208 2020-05-05 stsp *have_match = 1;
3377 dc424a06 2019-08-07 stsp #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
3379 79109fed 2018-03-27 stsp static const struct got_error *
3380 79109fed 2018-03-27 stsp print_commit(struct got_commit_object *commit, struct got_object_id *id,
3381 0208f208 2020-05-05 stsp struct got_repository *repo, const char *path,
3382 0208f208 2020-05-05 stsp struct got_pathlist_head *changed_paths, int show_patch,
3383 44392932 2019-08-25 stsp int diff_context, struct got_reflist_head *refs)
3385 79109fed 2018-03-27 stsp const struct got_error *err = NULL;
3386 621015ac 2018-07-23 stsp char *id_str, *datestr, *logmsg0, *logmsg, *line;
3387 6c281f94 2018-06-11 stsp char datebuf[26];
3388 45d799e2 2018-12-23 stsp time_t committer_time;
3389 45d799e2 2018-12-23 stsp const char *author, *committer;
3390 199a4027 2019-02-02 stsp char *refs_str = NULL;
3391 199a4027 2019-02-02 stsp struct got_reflist_entry *re;
3393 199a4027 2019-02-02 stsp SIMPLEQ_FOREACH(re, refs, entry) {
3395 199a4027 2019-02-02 stsp const char *name;
3396 a436ad14 2019-08-13 stsp struct got_tag_object *tag = NULL;
3397 48cae60d 2020-09-22 stsp struct got_object_id *ref_id;
3400 199a4027 2019-02-02 stsp name = got_ref_get_name(re->ref);
3401 d9498b20 2019-02-05 stsp if (strcmp(name, GOT_REF_HEAD) == 0)
3403 199a4027 2019-02-02 stsp if (strncmp(name, "refs/", 5) == 0)
3404 199a4027 2019-02-02 stsp name += 5;
3405 7143d404 2019-03-12 stsp if (strncmp(name, "got/", 4) == 0)
3407 e34f9ed6 2019-02-02 stsp if (strncmp(name, "heads/", 6) == 0)
3408 e34f9ed6 2019-02-02 stsp name += 6;
3409 4343a07f 2020-04-24 stsp if (strncmp(name, "remotes/", 8) == 0) {
3410 141c2bff 2019-02-04 stsp name += 8;
3411 4343a07f 2020-04-24 stsp s = strstr(name, "/" GOT_REF_HEAD);
3412 4343a07f 2020-04-24 stsp if (s != NULL && s[strlen(s)] == '\0')
3415 48cae60d 2020-09-22 stsp err = got_ref_resolve(&ref_id, repo, re->ref);
3417 48cae60d 2020-09-22 stsp return err;
3418 a436ad14 2019-08-13 stsp if (strncmp(name, "tags/", 5) == 0) {
3419 48cae60d 2020-09-22 stsp err = got_object_open_as_tag(&tag, repo, ref_id);
3420 5d844a1e 2019-08-13 stsp if (err) {
3421 48cae60d 2020-09-22 stsp if (err->code != GOT_ERR_OBJ_TYPE) {
3422 48cae60d 2020-09-22 stsp free(ref_id);
3423 5d844a1e 2019-08-13 stsp return err;
3425 5d844a1e 2019-08-13 stsp /* Ref points at something other than a tag. */
3426 5d844a1e 2019-08-13 stsp err = NULL;
3427 5d844a1e 2019-08-13 stsp tag = NULL;
3430 a436ad14 2019-08-13 stsp cmp = got_object_id_cmp(tag ?
3431 48cae60d 2020-09-22 stsp got_object_tag_get_object_id(tag) : ref_id, id);
3432 48cae60d 2020-09-22 stsp free(ref_id);
3434 a436ad14 2019-08-13 stsp got_object_tag_close(tag);
3435 a436ad14 2019-08-13 stsp if (cmp != 0)
3437 199a4027 2019-02-02 stsp s = refs_str;
3438 199a4027 2019-02-02 stsp if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
3439 199a4027 2019-02-02 stsp name) == -1) {
3440 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
3442 34ca4898 2019-08-13 stsp return err;
3446 832c249c 2018-06-10 stsp err = got_object_id_str(&id_str, id);
3448 8bf5b3c9 2018-03-17 stsp return err;
3450 dc424a06 2019-08-07 stsp printf(GOT_COMMIT_SEP_STR);
3451 199a4027 2019-02-02 stsp printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
3452 199a4027 2019-02-02 stsp refs_str ? refs_str : "", refs_str ? ")" : "");
3453 832c249c 2018-06-10 stsp free(id_str);
3454 d3d493d7 2019-02-21 stsp id_str = NULL;
3455 d3d493d7 2019-02-21 stsp free(refs_str);
3456 d3d493d7 2019-02-21 stsp refs_str = NULL;
3457 45d799e2 2018-12-23 stsp printf("from: %s\n", got_object_commit_get_author(commit));
3458 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
3459 45d799e2 2018-12-23 stsp datestr = get_datestr(&committer_time, datebuf);
3460 09867e48 2019-08-13 stsp if (datestr)
3461 09867e48 2019-08-13 stsp printf("date: %s UTC\n", datestr);
3462 45d799e2 2018-12-23 stsp author = got_object_commit_get_author(commit);
3463 45d799e2 2018-12-23 stsp committer = got_object_commit_get_committer(commit);
3464 45d799e2 2018-12-23 stsp if (strcmp(author, committer) != 0)
3465 45d799e2 2018-12-23 stsp printf("via: %s\n", committer);
3466 45d799e2 2018-12-23 stsp if (got_object_commit_get_nparents(commit) > 1) {
3467 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
3468 79f35eb3 2018-06-11 stsp struct got_object_qid *qid;
3469 3fe1abad 2018-06-10 stsp int n = 1;
3470 45d799e2 2018-12-23 stsp parent_ids = got_object_commit_get_parent_ids(commit);
3471 45d799e2 2018-12-23 stsp SIMPLEQ_FOREACH(qid, parent_ids, entry) {
3472 79f35eb3 2018-06-11 stsp err = got_object_id_str(&id_str, qid->id);
3474 a0603db2 2018-06-10 stsp return err;
3475 3fe1abad 2018-06-10 stsp printf("parent %d: %s\n", n++, id_str);
3476 a0603db2 2018-06-10 stsp free(id_str);
3480 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
3482 5943eee2 2019-08-13 stsp return err;
3484 621015ac 2018-07-23 stsp logmsg = logmsg0;
3486 832c249c 2018-06-10 stsp line = strsep(&logmsg, "\n");
3488 832c249c 2018-06-10 stsp printf(" %s\n", line);
3489 832c249c 2018-06-10 stsp } while (line);
3490 621015ac 2018-07-23 stsp free(logmsg0);
3492 0208f208 2020-05-05 stsp if (changed_paths) {
3493 0208f208 2020-05-05 stsp struct got_pathlist_entry *pe;
3494 0208f208 2020-05-05 stsp TAILQ_FOREACH(pe, changed_paths, entry) {
3495 0208f208 2020-05-05 stsp struct got_diff_changed_path *cp = pe->data;
3496 0208f208 2020-05-05 stsp printf(" %c %s\n", cp->status, pe->path);
3498 0208f208 2020-05-05 stsp printf("\n");
3500 971751ac 2018-03-27 stsp if (show_patch) {
3501 44392932 2019-08-25 stsp err = print_patch(commit, id, path, diff_context, repo);
3502 971751ac 2018-03-27 stsp if (err == 0)
3503 971751ac 2018-03-27 stsp printf("\n");
3506 cbe7f848 2019-02-11 stsp if (fflush(stdout) != 0 && err == NULL)
3507 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
3508 07862c20 2018-09-15 stsp return err;
3511 f42b1b34 2018-03-12 stsp static const struct got_error *
3512 d1fe46f9 2020-04-18 stsp print_commits(struct got_object_id *root_id, struct got_object_id *end_id,
3513 0208f208 2020-05-05 stsp struct got_repository *repo, const char *path, int show_changed_paths,
3514 0208f208 2020-05-05 stsp int show_patch, const char *search_pattern, int diff_context, int limit,
3515 0208f208 2020-05-05 stsp int log_branches, int reverse_display_order, struct got_reflist_head *refs)
3517 f42b1b34 2018-03-12 stsp const struct got_error *err;
3518 372ccdbb 2018-06-10 stsp struct got_commit_graph *graph;
3519 6841bf13 2019-11-29 kn regex_t regex;
3520 6841bf13 2019-11-29 kn int have_match;
3521 dbec59df 2020-04-18 stsp struct got_object_id_queue reversed_commits;
3522 dbec59df 2020-04-18 stsp struct got_object_qid *qid;
3523 dbec59df 2020-04-18 stsp struct got_commit_object *commit;
3524 0208f208 2020-05-05 stsp struct got_pathlist_head changed_paths;
3525 0208f208 2020-05-05 stsp struct got_pathlist_entry *pe;
3527 dbec59df 2020-04-18 stsp SIMPLEQ_INIT(&reversed_commits);
3528 0208f208 2020-05-05 stsp TAILQ_INIT(&changed_paths);
3530 ccecc9fd 2020-04-18 stsp if (search_pattern && regcomp(®ex, search_pattern,
3531 ccecc9fd 2020-04-18 stsp REG_EXTENDED | REG_NOSUB | REG_NEWLINE))
3532 6841bf13 2019-11-29 kn return got_error_msg(GOT_ERR_REGEX, search_pattern);
3534 48c8c60d 2020-01-27 stsp err = got_commit_graph_open(&graph, path, !log_branches);
3536 f42b1b34 2018-03-12 stsp return err;
3537 6fb7cd11 2019-08-22 stsp err = got_commit_graph_iter_start(graph, root_id, repo,
3538 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
3540 fcc85cad 2018-07-22 stsp goto done;
3542 372ccdbb 2018-06-10 stsp struct got_object_id *id;
3544 84453469 2018-11-11 stsp if (sigint_received || sigpipe_received)
3547 ee780d5c 2020-01-04 stsp err = got_commit_graph_iter_next(&id, graph, repo,
3548 ee780d5c 2020-01-04 stsp check_cancelled, NULL);
3549 372ccdbb 2018-06-10 stsp if (err) {
3550 ee780d5c 2020-01-04 stsp if (err->code == GOT_ERR_ITER_COMPLETED)
3551 9ba79e04 2018-06-11 stsp err = NULL;
3554 b43fbaa0 2018-06-11 stsp if (id == NULL)
3557 f8e900f3 2018-06-11 stsp err = got_object_open_as_commit(&commit, repo, id);
3561 502b9684 2020-07-31 stsp if (show_changed_paths && !reverse_display_order) {
3562 0208f208 2020-05-05 stsp err = get_changed_paths(&changed_paths, commit, repo);
3567 6841bf13 2019-11-29 kn if (search_pattern) {
3568 6841bf13 2019-11-29 kn err = match_logmsg(&have_match, id, commit, ®ex);
3570 6841bf13 2019-11-29 kn got_object_commit_close(commit);
3573 0208f208 2020-05-05 stsp if (have_match == 0 && show_changed_paths)
3574 0208f208 2020-05-05 stsp match_changed_paths(&have_match,
3575 0208f208 2020-05-05 stsp &changed_paths, ®ex);
3576 6841bf13 2019-11-29 kn if (have_match == 0) {
3577 6841bf13 2019-11-29 kn got_object_commit_close(commit);
3578 0208f208 2020-05-05 stsp TAILQ_FOREACH(pe, &changed_paths, entry) {
3579 0208f208 2020-05-05 stsp free((char *)pe->path);
3580 0208f208 2020-05-05 stsp free(pe->data);
3582 0208f208 2020-05-05 stsp got_pathlist_free(&changed_paths);
3587 dbec59df 2020-04-18 stsp if (reverse_display_order) {
3588 dbec59df 2020-04-18 stsp err = got_object_qid_alloc(&qid, id);
3591 dbec59df 2020-04-18 stsp SIMPLEQ_INSERT_HEAD(&reversed_commits, qid, entry);
3592 dbec59df 2020-04-18 stsp got_object_commit_close(commit);
3594 0208f208 2020-05-05 stsp err = print_commit(commit, id, repo, path,
3595 0208f208 2020-05-05 stsp show_changed_paths ? &changed_paths : NULL,
3596 0208f208 2020-05-05 stsp show_patch, diff_context, refs);
3597 dbec59df 2020-04-18 stsp got_object_commit_close(commit);
3601 dbec59df 2020-04-18 stsp if ((limit && --limit == 0) ||
3602 dbec59df 2020-04-18 stsp (end_id && got_object_id_cmp(id, end_id) == 0))
3605 0208f208 2020-05-05 stsp TAILQ_FOREACH(pe, &changed_paths, entry) {
3606 0208f208 2020-05-05 stsp free((char *)pe->path);
3607 0208f208 2020-05-05 stsp free(pe->data);
3609 0208f208 2020-05-05 stsp got_pathlist_free(&changed_paths);
3611 dbec59df 2020-04-18 stsp if (reverse_display_order) {
3612 dbec59df 2020-04-18 stsp SIMPLEQ_FOREACH(qid, &reversed_commits, entry) {
3613 dbec59df 2020-04-18 stsp err = got_object_open_as_commit(&commit, repo, qid->id);
3616 502b9684 2020-07-31 stsp if (show_changed_paths) {
3617 502b9684 2020-07-31 stsp err = get_changed_paths(&changed_paths,
3618 502b9684 2020-07-31 stsp commit, repo);
3622 dbec59df 2020-04-18 stsp err = print_commit(commit, qid->id, repo, path,
3623 0208f208 2020-05-05 stsp show_changed_paths ? &changed_paths : NULL,
3624 dbec59df 2020-04-18 stsp show_patch, diff_context, refs);
3625 dbec59df 2020-04-18 stsp got_object_commit_close(commit);
3628 502b9684 2020-07-31 stsp TAILQ_FOREACH(pe, &changed_paths, entry) {
3629 502b9684 2020-07-31 stsp free((char *)pe->path);
3630 502b9684 2020-07-31 stsp free(pe->data);
3632 502b9684 2020-07-31 stsp got_pathlist_free(&changed_paths);
3636 dbec59df 2020-04-18 stsp while (!SIMPLEQ_EMPTY(&reversed_commits)) {
3637 dbec59df 2020-04-18 stsp qid = SIMPLEQ_FIRST(&reversed_commits);
3638 dbec59df 2020-04-18 stsp SIMPLEQ_REMOVE_HEAD(&reversed_commits, entry);
3639 dbec59df 2020-04-18 stsp got_object_qid_free(qid);
3641 0208f208 2020-05-05 stsp TAILQ_FOREACH(pe, &changed_paths, entry) {
3642 0208f208 2020-05-05 stsp free((char *)pe->path);
3643 0208f208 2020-05-05 stsp free(pe->data);
3645 0208f208 2020-05-05 stsp got_pathlist_free(&changed_paths);
3646 6841bf13 2019-11-29 kn if (search_pattern)
3647 6841bf13 2019-11-29 kn regfree(®ex);
3648 372ccdbb 2018-06-10 stsp got_commit_graph_close(graph);
3649 f42b1b34 2018-03-12 stsp return err;
3652 4ed7e80c 2018-05-20 stsp __dead static void
3653 6f3d1eb0 2018-03-12 stsp usage_log(void)
3655 d1fe46f9 2020-04-18 stsp fprintf(stderr, "usage: %s log [-b] [-c commit] [-C number] [ -l N ] "
3656 0208f208 2020-05-05 stsp "[-p] [-P] [-x commit] [-s search-pattern] [-r repository-path] "
3657 0208f208 2020-05-05 stsp "[-R] [path]\n", getprogname());
3661 b1ebc001 2019-08-13 stsp static int
3662 b1ebc001 2019-08-13 stsp get_default_log_limit(void)
3664 b1ebc001 2019-08-13 stsp const char *got_default_log_limit;
3665 b1ebc001 2019-08-13 stsp long long n;
3666 b1ebc001 2019-08-13 stsp const char *errstr;
3668 b1ebc001 2019-08-13 stsp got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
3669 b1ebc001 2019-08-13 stsp if (got_default_log_limit == NULL)
3671 b1ebc001 2019-08-13 stsp n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
3672 b1ebc001 2019-08-13 stsp if (errstr != NULL)
3677 d1fe46f9 2020-04-18 stsp static const struct got_error *
3678 d1fe46f9 2020-04-18 stsp resolve_commit_arg(struct got_object_id **id, const char *commit_arg,
3679 d1fe46f9 2020-04-18 stsp struct got_repository *repo)
3681 d1fe46f9 2020-04-18 stsp const struct got_error *err = NULL;
3682 d1fe46f9 2020-04-18 stsp struct got_reference *ref;
3684 d1fe46f9 2020-04-18 stsp *id = NULL;
3686 d1fe46f9 2020-04-18 stsp err = got_ref_open(&ref, repo, commit_arg, 0);
3687 d1fe46f9 2020-04-18 stsp if (err == NULL) {
3688 d1fe46f9 2020-04-18 stsp int obj_type;
3689 d1fe46f9 2020-04-18 stsp err = got_ref_resolve(id, repo, ref);
3690 d1fe46f9 2020-04-18 stsp got_ref_close(ref);
3692 d1fe46f9 2020-04-18 stsp return err;
3693 d1fe46f9 2020-04-18 stsp err = got_object_get_type(&obj_type, repo, *id);
3695 d1fe46f9 2020-04-18 stsp return err;
3696 d1fe46f9 2020-04-18 stsp if (obj_type == GOT_OBJ_TYPE_TAG) {
3697 d1fe46f9 2020-04-18 stsp struct got_tag_object *tag;
3698 d1fe46f9 2020-04-18 stsp err = got_object_open_as_tag(&tag, repo, *id);
3700 d1fe46f9 2020-04-18 stsp return err;
3701 d1fe46f9 2020-04-18 stsp if (got_object_tag_get_object_type(tag) !=
3702 d1fe46f9 2020-04-18 stsp GOT_OBJ_TYPE_COMMIT) {
3703 d1fe46f9 2020-04-18 stsp got_object_tag_close(tag);
3704 d1fe46f9 2020-04-18 stsp return got_error(GOT_ERR_OBJ_TYPE);
3706 d1fe46f9 2020-04-18 stsp free(*id);
3707 d1fe46f9 2020-04-18 stsp *id = got_object_id_dup(
3708 d1fe46f9 2020-04-18 stsp got_object_tag_get_object_id(tag));
3709 d1fe46f9 2020-04-18 stsp if (*id == NULL)
3710 d1fe46f9 2020-04-18 stsp err = got_error_from_errno(
3711 d1fe46f9 2020-04-18 stsp "got_object_id_dup");
3712 d1fe46f9 2020-04-18 stsp got_object_tag_close(tag);
3714 d1fe46f9 2020-04-18 stsp return err;
3715 d1fe46f9 2020-04-18 stsp } else if (obj_type != GOT_OBJ_TYPE_COMMIT)
3716 d1fe46f9 2020-04-18 stsp return got_error(GOT_ERR_OBJ_TYPE);
3718 d1fe46f9 2020-04-18 stsp err = got_repo_match_object_id_prefix(id, commit_arg,
3719 d1fe46f9 2020-04-18 stsp GOT_OBJ_TYPE_COMMIT, repo);
3722 d1fe46f9 2020-04-18 stsp return err;
3725 4ed7e80c 2018-05-20 stsp static const struct got_error *
3726 f42b1b34 2018-03-12 stsp cmd_log(int argc, char *argv[])
3728 f42b1b34 2018-03-12 stsp const struct got_error *error;
3729 04ca23f4 2018-07-16 stsp struct got_repository *repo = NULL;
3730 cffc0aa4 2019-02-05 stsp struct got_worktree *worktree = NULL;
3731 d1fe46f9 2020-04-18 stsp struct got_object_id *start_id = NULL, *end_id = NULL;
3732 04ca23f4 2018-07-16 stsp char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
3733 d1fe46f9 2020-04-18 stsp const char *start_commit = NULL, *end_commit = NULL;
3734 d1fe46f9 2020-04-18 stsp const char *search_pattern = NULL;
3735 dc1edbfa 2019-11-29 kn int diff_context = -1, ch;
3736 0208f208 2020-05-05 stsp int show_changed_paths = 0, show_patch = 0, limit = 0, log_branches = 0;
3737 dbec59df 2020-04-18 stsp int reverse_display_order = 0;
3738 64a96a6d 2018-04-01 stsp const char *errstr;
3739 199a4027 2019-02-02 stsp struct got_reflist_head refs;
3741 1b3893a2 2019-03-18 stsp SIMPLEQ_INIT(&refs);
3743 6715a751 2018-03-16 stsp #ifndef PROFILE
3744 6098196c 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3747 f42b1b34 2018-03-12 stsp err(1, "pledge");
3750 b1ebc001 2019-08-13 stsp limit = get_default_log_limit();
3752 0208f208 2020-05-05 stsp while ((ch = getopt(argc, argv, "bpPc:C:l:r:Rs:x:")) != -1) {
3753 79109fed 2018-03-27 stsp switch (ch) {
3755 79109fed 2018-03-27 stsp show_patch = 1;
3758 0208f208 2020-05-05 stsp show_changed_paths = 1;
3761 d142fc45 2018-04-01 stsp start_commit = optarg;
3764 4a8520aa 2018-10-18 stsp diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
3766 c0cc5c62 2018-10-18 stsp if (errstr != NULL)
3767 c0cc5c62 2018-10-18 stsp err(1, "-C option %s", errstr);
3770 b1ebc001 2019-08-13 stsp limit = strtonum(optarg, 0, INT_MAX, &errstr);
3771 64a96a6d 2018-04-01 stsp if (errstr != NULL)
3772 64a96a6d 2018-04-01 stsp err(1, "-l option %s", errstr);
3775 48c8c60d 2020-01-27 stsp log_branches = 1;
3778 04ca23f4 2018-07-16 stsp repo_path = realpath(optarg, NULL);
3779 04ca23f4 2018-07-16 stsp if (repo_path == NULL)
3780 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
3782 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
3785 dbec59df 2020-04-18 stsp reverse_display_order = 1;
3788 6841bf13 2019-11-29 kn search_pattern = optarg;
3791 d1fe46f9 2020-04-18 stsp end_commit = optarg;
3794 2deda0b9 2019-03-07 stsp usage_log();
3795 79109fed 2018-03-27 stsp /* NOTREACHED */
3799 79109fed 2018-03-27 stsp argc -= optind;
3800 79109fed 2018-03-27 stsp argv += optind;
3802 dc1edbfa 2019-11-29 kn if (diff_context == -1)
3803 dc1edbfa 2019-11-29 kn diff_context = 3;
3804 dc1edbfa 2019-11-29 kn else if (!show_patch)
3805 0429cd76 2020-09-15 naddy errx(1, "-C requires -p");
3807 04ca23f4 2018-07-16 stsp cwd = getcwd(NULL, 0);
3808 04ca23f4 2018-07-16 stsp if (cwd == NULL) {
3809 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
3810 04ca23f4 2018-07-16 stsp goto done;
3813 50f2fada 2020-04-24 stsp if (repo_path == NULL) {
3814 50f2fada 2020-04-24 stsp error = got_worktree_open(&worktree, cwd);
3815 50f2fada 2020-04-24 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
3816 50f2fada 2020-04-24 stsp goto done;
3817 50f2fada 2020-04-24 stsp error = NULL;
3820 e7301579 2019-03-18 stsp if (argc == 0) {
3821 cbd1af7a 2019-03-18 stsp path = strdup("");
3822 e7301579 2019-03-18 stsp if (path == NULL) {
3823 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
3824 cbd1af7a 2019-03-18 stsp goto done;
3826 e7301579 2019-03-18 stsp } else if (argc == 1) {
3827 e7301579 2019-03-18 stsp if (worktree) {
3828 e7301579 2019-03-18 stsp error = got_worktree_resolve_path(&path, worktree,
3830 e7301579 2019-03-18 stsp if (error)
3831 e7301579 2019-03-18 stsp goto done;
3833 e7301579 2019-03-18 stsp path = strdup(argv[0]);
3834 e7301579 2019-03-18 stsp if (path == NULL) {
3835 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
3836 e7301579 2019-03-18 stsp goto done;
3840 cbd1af7a 2019-03-18 stsp usage_log();
3842 5486daa2 2019-05-11 stsp if (repo_path == NULL) {
3843 5486daa2 2019-05-11 stsp repo_path = worktree ?
3844 5486daa2 2019-05-11 stsp strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
3846 04ca23f4 2018-07-16 stsp if (repo_path == NULL) {
3847 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
3848 cffc0aa4 2019-02-05 stsp goto done;
3851 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
3852 d7d4f210 2018-03-12 stsp if (error != NULL)
3853 04ca23f4 2018-07-16 stsp goto done;
3855 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), 1,
3856 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
3857 c02c541e 2019-03-29 stsp if (error)
3858 c02c541e 2019-03-29 stsp goto done;
3860 d142fc45 2018-04-01 stsp if (start_commit == NULL) {
3861 3235492e 2018-04-01 stsp struct got_reference *head_ref;
3862 d1fe46f9 2020-04-18 stsp struct got_commit_object *commit = NULL;
3863 1cc14b9f 2019-05-14 stsp error = got_ref_open(&head_ref, repo,
3864 1cc14b9f 2019-05-14 stsp worktree ? got_worktree_get_head_ref_name(worktree)
3865 1cc14b9f 2019-05-14 stsp : GOT_REF_HEAD, 0);
3866 3235492e 2018-04-01 stsp if (error != NULL)
3867 d1fe46f9 2020-04-18 stsp goto done;
3868 d1fe46f9 2020-04-18 stsp error = got_ref_resolve(&start_id, repo, head_ref);
3869 3235492e 2018-04-01 stsp got_ref_close(head_ref);
3870 3235492e 2018-04-01 stsp if (error != NULL)
3871 d1fe46f9 2020-04-18 stsp goto done;
3872 d1fe46f9 2020-04-18 stsp error = got_object_open_as_commit(&commit, repo,
3873 d1fe46f9 2020-04-18 stsp start_id);
3874 d1fe46f9 2020-04-18 stsp if (error != NULL)
3875 d1fe46f9 2020-04-18 stsp goto done;
3876 d1fe46f9 2020-04-18 stsp got_object_commit_close(commit);
3878 d1fe46f9 2020-04-18 stsp error = resolve_commit_arg(&start_id, start_commit, repo);
3879 d1fe46f9 2020-04-18 stsp if (error != NULL)
3880 d1fe46f9 2020-04-18 stsp goto done;
3882 d1fe46f9 2020-04-18 stsp if (end_commit != NULL) {
3883 d1fe46f9 2020-04-18 stsp error = resolve_commit_arg(&end_id, end_commit, repo);
3884 d1fe46f9 2020-04-18 stsp if (error != NULL)
3885 d1fe46f9 2020-04-18 stsp goto done;
3888 deeabeae 2019-08-27 stsp if (worktree) {
3889 deeabeae 2019-08-27 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
3891 deeabeae 2019-08-27 stsp if (asprintf(&p, "%s%s%s", prefix,
3892 deeabeae 2019-08-27 stsp (strcmp(prefix, "/") != 0) ? "/" : "", path) == -1) {
3893 deeabeae 2019-08-27 stsp error = got_error_from_errno("asprintf");
3894 deeabeae 2019-08-27 stsp goto done;
3896 f43793a4 2020-01-27 stsp error = got_repo_map_path(&in_repo_path, repo, p, 0);
3899 deeabeae 2019-08-27 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
3900 04ca23f4 2018-07-16 stsp if (error != NULL)
3901 04ca23f4 2018-07-16 stsp goto done;
3902 04ca23f4 2018-07-16 stsp if (in_repo_path) {
3903 04ca23f4 2018-07-16 stsp free(path);
3904 04ca23f4 2018-07-16 stsp path = in_repo_path;
3907 b8bad2ba 2019-08-23 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
3908 199a4027 2019-02-02 stsp if (error)
3909 199a4027 2019-02-02 stsp goto done;
3911 0208f208 2020-05-05 stsp error = print_commits(start_id, end_id, repo, path, show_changed_paths,
3912 0208f208 2020-05-05 stsp show_patch, search_pattern, diff_context, limit, log_branches,
3913 dbec59df 2020-04-18 stsp reverse_display_order, &refs);
3915 04ca23f4 2018-07-16 stsp free(path);
3916 04ca23f4 2018-07-16 stsp free(repo_path);
3917 04ca23f4 2018-07-16 stsp free(cwd);
3918 cffc0aa4 2019-02-05 stsp if (worktree)
3919 cffc0aa4 2019-02-05 stsp got_worktree_close(worktree);
3920 ad242220 2018-09-08 stsp if (repo) {
3921 ad242220 2018-09-08 stsp const struct got_error *repo_error;
3922 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
3923 ad242220 2018-09-08 stsp if (error == NULL)
3924 ad242220 2018-09-08 stsp error = repo_error;
3926 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
3927 8bf5b3c9 2018-03-17 stsp return error;
3930 4ed7e80c 2018-05-20 stsp __dead static void
3931 3f8b7d6a 2018-04-01 stsp usage_diff(void)
3933 98eaaa12 2019-08-03 stsp fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] [-s] "
3934 63035f9f 2019-10-06 stsp "[-w] [object1 object2 | path]\n", getprogname());
3938 b72f483a 2019-02-05 stsp struct print_diff_arg {
3939 b72f483a 2019-02-05 stsp struct got_repository *repo;
3940 b72f483a 2019-02-05 stsp struct got_worktree *worktree;
3941 b72f483a 2019-02-05 stsp int diff_context;
3942 3fc0c068 2019-02-10 stsp const char *id_str;
3943 3fc0c068 2019-02-10 stsp int header_shown;
3944 98eaaa12 2019-08-03 stsp int diff_staged;
3945 63035f9f 2019-10-06 stsp int ignore_whitespace;
3949 39449a05 2020-07-23 stsp * Create a file which contains the target path of a symlink so we can feed
3950 39449a05 2020-07-23 stsp * it as content to the diff engine.
3952 39449a05 2020-07-23 stsp static const struct got_error *
3953 39449a05 2020-07-23 stsp get_symlink_target_file(int *fd, int dirfd, const char *de_name,
3954 39449a05 2020-07-23 stsp const char *abspath)
3956 39449a05 2020-07-23 stsp const struct got_error *err = NULL;
3957 39449a05 2020-07-23 stsp char target_path[PATH_MAX];
3958 39449a05 2020-07-23 stsp ssize_t target_len, outlen;
3962 39449a05 2020-07-23 stsp if (dirfd != -1) {
3963 39449a05 2020-07-23 stsp target_len = readlinkat(dirfd, de_name, target_path, PATH_MAX);
3964 39449a05 2020-07-23 stsp if (target_len == -1)
3965 39449a05 2020-07-23 stsp return got_error_from_errno2("readlinkat", abspath);
3967 39449a05 2020-07-23 stsp target_len = readlink(abspath, target_path, PATH_MAX);
3968 39449a05 2020-07-23 stsp if (target_len == -1)
3969 39449a05 2020-07-23 stsp return got_error_from_errno2("readlink", abspath);
3972 39449a05 2020-07-23 stsp *fd = got_opentempfd();
3973 39449a05 2020-07-23 stsp if (*fd == -1)
3974 39449a05 2020-07-23 stsp return got_error_from_errno("got_opentempfd");
3976 39449a05 2020-07-23 stsp outlen = write(*fd, target_path, target_len);
3977 39449a05 2020-07-23 stsp if (outlen == -1) {
3978 39449a05 2020-07-23 stsp err = got_error_from_errno("got_opentempfd");
3979 39449a05 2020-07-23 stsp goto done;
3982 39449a05 2020-07-23 stsp if (lseek(*fd, 0, SEEK_SET) == -1) {
3983 39449a05 2020-07-23 stsp err = got_error_from_errno2("lseek", abspath);
3984 39449a05 2020-07-23 stsp goto done;
3987 39449a05 2020-07-23 stsp if (err) {
3988 39449a05 2020-07-23 stsp close(*fd);
3991 39449a05 2020-07-23 stsp return err;
3994 4ed7e80c 2018-05-20 stsp static const struct got_error *
3995 88d0e355 2019-08-03 stsp print_diff(void *arg, unsigned char status, unsigned char staged_status,
3996 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
3997 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3998 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
4000 b72f483a 2019-02-05 stsp struct print_diff_arg *a = arg;
4001 b72f483a 2019-02-05 stsp const struct got_error *err = NULL;
4002 b72f483a 2019-02-05 stsp struct got_blob_object *blob1 = NULL;
4003 12463d8b 2019-12-13 stsp int fd = -1;
4004 b72f483a 2019-02-05 stsp FILE *f2 = NULL;
4005 4ce46740 2019-08-08 stsp char *abspath = NULL, *label1 = NULL;
4006 b72f483a 2019-02-05 stsp struct stat sb;
4008 98eaaa12 2019-08-03 stsp if (a->diff_staged) {
4009 98eaaa12 2019-08-03 stsp if (staged_status != GOT_STATUS_MODIFY &&
4010 98eaaa12 2019-08-03 stsp staged_status != GOT_STATUS_ADD &&
4011 98eaaa12 2019-08-03 stsp staged_status != GOT_STATUS_DELETE)
4012 98eaaa12 2019-08-03 stsp return NULL;
4014 98eaaa12 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
4015 98eaaa12 2019-08-03 stsp return NULL;
4016 2a06fe5f 2019-08-24 stsp if (status == GOT_STATUS_NONEXISTENT)
4017 2a06fe5f 2019-08-24 stsp return got_error_set_errno(ENOENT, path);
4018 98eaaa12 2019-08-03 stsp if (status != GOT_STATUS_MODIFY &&
4019 98eaaa12 2019-08-03 stsp status != GOT_STATUS_ADD &&
4020 98eaaa12 2019-08-03 stsp status != GOT_STATUS_DELETE &&
4021 98eaaa12 2019-08-03 stsp status != GOT_STATUS_CONFLICT)
4022 98eaaa12 2019-08-03 stsp return NULL;
4025 3fc0c068 2019-02-10 stsp if (!a->header_shown) {
4026 98eaaa12 2019-08-03 stsp printf("diff %s %s%s\n", a->id_str,
4027 98eaaa12 2019-08-03 stsp got_worktree_get_root_path(a->worktree),
4028 98eaaa12 2019-08-03 stsp a->diff_staged ? " (staged changes)" : "");
4029 3fc0c068 2019-02-10 stsp a->header_shown = 1;
4032 98eaaa12 2019-08-03 stsp if (a->diff_staged) {
4033 98eaaa12 2019-08-03 stsp const char *label1 = NULL, *label2 = NULL;
4034 98eaaa12 2019-08-03 stsp switch (staged_status) {
4035 98eaaa12 2019-08-03 stsp case GOT_STATUS_MODIFY:
4036 98eaaa12 2019-08-03 stsp label1 = path;
4037 98eaaa12 2019-08-03 stsp label2 = path;
4039 98eaaa12 2019-08-03 stsp case GOT_STATUS_ADD:
4040 98eaaa12 2019-08-03 stsp label2 = path;
4042 98eaaa12 2019-08-03 stsp case GOT_STATUS_DELETE:
4043 98eaaa12 2019-08-03 stsp label1 = path;
4046 98eaaa12 2019-08-03 stsp return got_error(GOT_ERR_FILE_STATUS);
4048 98eaaa12 2019-08-03 stsp return got_diff_objects_as_blobs(blob_id, staged_blob_id,
4049 63035f9f 2019-10-06 stsp label1, label2, a->diff_context, a->ignore_whitespace,
4050 63035f9f 2019-10-06 stsp a->repo, stdout);
4053 408b4ebc 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
4054 4ce46740 2019-08-08 stsp staged_status == GOT_STATUS_MODIFY) {
4055 4ce46740 2019-08-08 stsp char *id_str;
4056 408b4ebc 2019-08-03 stsp err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
4059 4ce46740 2019-08-08 stsp goto done;
4060 4ce46740 2019-08-08 stsp err = got_object_id_str(&id_str, staged_blob_id);
4062 4ce46740 2019-08-08 stsp goto done;
4063 4ce46740 2019-08-08 stsp if (asprintf(&label1, "%s (staged)", id_str) == -1) {
4064 4ce46740 2019-08-08 stsp err = got_error_from_errno("asprintf");
4065 4ce46740 2019-08-08 stsp free(id_str);
4066 4ce46740 2019-08-08 stsp goto done;
4068 4ce46740 2019-08-08 stsp free(id_str);
4069 4ce46740 2019-08-08 stsp } else if (status != GOT_STATUS_ADD) {
4070 016a88dd 2019-05-13 stsp err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
4072 4ce46740 2019-08-08 stsp goto done;
4075 7154f6ce 2019-03-27 stsp if (status != GOT_STATUS_DELETE) {
4076 2ec1f75b 2019-03-26 stsp if (asprintf(&abspath, "%s/%s",
4077 2ec1f75b 2019-03-26 stsp got_worktree_get_root_path(a->worktree), path) == -1) {
4078 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4079 2ec1f75b 2019-03-26 stsp goto done;
4082 12463d8b 2019-12-13 stsp if (dirfd != -1) {
4083 12463d8b 2019-12-13 stsp fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
4084 12463d8b 2019-12-13 stsp if (fd == -1) {
4085 39449a05 2020-07-23 stsp if (errno != ELOOP) {
4086 39449a05 2020-07-23 stsp err = got_error_from_errno2("openat",
4088 39449a05 2020-07-23 stsp goto done;
4090 39449a05 2020-07-23 stsp err = get_symlink_target_file(&fd, dirfd,
4091 39449a05 2020-07-23 stsp de_name, abspath);
4093 39449a05 2020-07-23 stsp goto done;
4096 12463d8b 2019-12-13 stsp fd = open(abspath, O_RDONLY | O_NOFOLLOW);
4097 12463d8b 2019-12-13 stsp if (fd == -1) {
4098 39449a05 2020-07-23 stsp if (errno != ELOOP) {
4099 39449a05 2020-07-23 stsp err = got_error_from_errno2("open",
4101 39449a05 2020-07-23 stsp goto done;
4103 39449a05 2020-07-23 stsp err = get_symlink_target_file(&fd, dirfd,
4104 39449a05 2020-07-23 stsp de_name, abspath);
4106 39449a05 2020-07-23 stsp goto done;
4109 12463d8b 2019-12-13 stsp if (fstat(fd, &sb) == -1) {
4110 12463d8b 2019-12-13 stsp err = got_error_from_errno2("fstat", abspath);
4111 2ec1f75b 2019-03-26 stsp goto done;
4113 12463d8b 2019-12-13 stsp f2 = fdopen(fd, "r");
4114 12463d8b 2019-12-13 stsp if (f2 == NULL) {
4115 12463d8b 2019-12-13 stsp err = got_error_from_errno2("fdopen", abspath);
4116 2ec1f75b 2019-03-26 stsp goto done;
4120 2ec1f75b 2019-03-26 stsp sb.st_size = 0;
4122 4ce46740 2019-08-08 stsp err = got_diff_blob_file(blob1, label1, f2, sb.st_size, path,
4123 63035f9f 2019-10-06 stsp a->diff_context, a->ignore_whitespace, stdout);
4125 b72f483a 2019-02-05 stsp if (blob1)
4126 b72f483a 2019-02-05 stsp got_object_blob_close(blob1);
4127 12463d8b 2019-12-13 stsp if (f2 && fclose(f2) == EOF && err == NULL)
4128 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
4129 12463d8b 2019-12-13 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
4130 12463d8b 2019-12-13 stsp err = got_error_from_errno("close");
4131 b72f483a 2019-02-05 stsp free(abspath);
4132 927df6b7 2019-02-10 stsp return err;
4135 927df6b7 2019-02-10 stsp static const struct got_error *
4136 b00d56cd 2018-04-01 stsp cmd_diff(int argc, char *argv[])
4138 b00d56cd 2018-04-01 stsp const struct got_error *error;
4139 b00d56cd 2018-04-01 stsp struct got_repository *repo = NULL;
4140 b72f483a 2019-02-05 stsp struct got_worktree *worktree = NULL;
4141 b72f483a 2019-02-05 stsp char *cwd = NULL, *repo_path = NULL;
4142 15a94983 2018-12-23 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
4143 cd628e99 2019-05-28 stsp const char *id_str1 = NULL, *id_str2 = NULL;
4144 5e70831e 2019-05-28 stsp char *label1 = NULL, *label2 = NULL;
4145 15a94983 2018-12-23 stsp int type1, type2;
4146 63035f9f 2019-10-06 stsp int diff_context = 3, diff_staged = 0, ignore_whitespace = 0, ch;
4147 c0cc5c62 2018-10-18 stsp const char *errstr;
4148 927df6b7 2019-02-10 stsp char *path = NULL;
4150 b00d56cd 2018-04-01 stsp #ifndef PROFILE
4151 25eccc22 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4152 25eccc22 2019-01-04 stsp NULL) == -1)
4153 b00d56cd 2018-04-01 stsp err(1, "pledge");
4156 63035f9f 2019-10-06 stsp while ((ch = getopt(argc, argv, "C:r:sw")) != -1) {
4157 b00d56cd 2018-04-01 stsp switch (ch) {
4159 dfcab68b 2019-11-29 kn diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
4161 c0cc5c62 2018-10-18 stsp if (errstr != NULL)
4162 c0cc5c62 2018-10-18 stsp err(1, "-C option %s", errstr);
4165 b72f483a 2019-02-05 stsp repo_path = realpath(optarg, NULL);
4166 b72f483a 2019-02-05 stsp if (repo_path == NULL)
4167 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
4169 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
4172 98eaaa12 2019-08-03 stsp diff_staged = 1;
4175 63035f9f 2019-10-06 stsp ignore_whitespace = 1;
4178 2deda0b9 2019-03-07 stsp usage_diff();
4179 b00d56cd 2018-04-01 stsp /* NOTREACHED */
4183 b00d56cd 2018-04-01 stsp argc -= optind;
4184 b00d56cd 2018-04-01 stsp argv += optind;
4186 b72f483a 2019-02-05 stsp cwd = getcwd(NULL, 0);
4187 b72f483a 2019-02-05 stsp if (cwd == NULL) {
4188 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
4189 b72f483a 2019-02-05 stsp goto done;
4191 927df6b7 2019-02-10 stsp if (argc <= 1) {
4192 b72f483a 2019-02-05 stsp if (repo_path)
4194 b72f483a 2019-02-05 stsp "-r option can't be used when diffing a work tree");
4195 1f03b8da 2020-03-20 stsp error = got_worktree_open(&worktree, cwd);
4196 fa51e947 2020-03-27 stsp if (error) {
4197 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
4198 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "diff",
4200 1f03b8da 2020-03-20 stsp goto done;
4202 b72f483a 2019-02-05 stsp repo_path = strdup(got_worktree_get_repo_path(worktree));
4203 927df6b7 2019-02-10 stsp if (repo_path == NULL) {
4204 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
4205 927df6b7 2019-02-10 stsp goto done;
4207 927df6b7 2019-02-10 stsp if (argc == 1) {
4208 6c7ab921 2019-03-18 stsp error = got_worktree_resolve_path(&path, worktree,
4210 927df6b7 2019-02-10 stsp if (error)
4211 927df6b7 2019-02-10 stsp goto done;
4213 927df6b7 2019-02-10 stsp path = strdup("");
4214 927df6b7 2019-02-10 stsp if (path == NULL) {
4215 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
4216 927df6b7 2019-02-10 stsp goto done;
4219 b72f483a 2019-02-05 stsp } else if (argc == 2) {
4220 98eaaa12 2019-08-03 stsp if (diff_staged)
4221 98eaaa12 2019-08-03 stsp errx(1, "-s option can't be used when diffing "
4222 98eaaa12 2019-08-03 stsp "objects in repository");
4223 15a94983 2018-12-23 stsp id_str1 = argv[0];
4224 15a94983 2018-12-23 stsp id_str2 = argv[1];
4225 1f03b8da 2020-03-20 stsp if (repo_path == NULL) {
4226 1f03b8da 2020-03-20 stsp error = got_worktree_open(&worktree, cwd);
4227 1f03b8da 2020-03-20 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4228 30db809c 2019-06-05 stsp goto done;
4229 1f03b8da 2020-03-20 stsp if (worktree) {
4230 1f03b8da 2020-03-20 stsp repo_path = strdup(
4231 1f03b8da 2020-03-20 stsp got_worktree_get_repo_path(worktree));
4232 1f03b8da 2020-03-20 stsp if (repo_path == NULL) {
4233 1f03b8da 2020-03-20 stsp error = got_error_from_errno("strdup");
4234 1f03b8da 2020-03-20 stsp goto done;
4237 1f03b8da 2020-03-20 stsp repo_path = strdup(cwd);
4238 1f03b8da 2020-03-20 stsp if (repo_path == NULL) {
4239 1f03b8da 2020-03-20 stsp error = got_error_from_errno("strdup");
4240 1f03b8da 2020-03-20 stsp goto done;
4245 b00d56cd 2018-04-01 stsp usage_diff();
4247 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
4248 76089277 2018-04-01 stsp free(repo_path);
4249 b00d56cd 2018-04-01 stsp if (error != NULL)
4250 b00d56cd 2018-04-01 stsp goto done;
4252 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), 1,
4253 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
4254 c02c541e 2019-03-29 stsp if (error)
4255 c02c541e 2019-03-29 stsp goto done;
4257 30db809c 2019-06-05 stsp if (argc <= 1) {
4258 b72f483a 2019-02-05 stsp struct print_diff_arg arg;
4259 72ea6654 2019-07-27 stsp struct got_pathlist_head paths;
4260 b72f483a 2019-02-05 stsp char *id_str;
4262 72ea6654 2019-07-27 stsp TAILQ_INIT(&paths);
4264 b72f483a 2019-02-05 stsp error = got_object_id_str(&id_str,
4265 b72f483a 2019-02-05 stsp got_worktree_get_base_commit_id(worktree));
4266 b72f483a 2019-02-05 stsp if (error)
4267 b72f483a 2019-02-05 stsp goto done;
4268 b72f483a 2019-02-05 stsp arg.repo = repo;
4269 b72f483a 2019-02-05 stsp arg.worktree = worktree;
4270 b72f483a 2019-02-05 stsp arg.diff_context = diff_context;
4271 3fc0c068 2019-02-10 stsp arg.id_str = id_str;
4272 3fc0c068 2019-02-10 stsp arg.header_shown = 0;
4273 98eaaa12 2019-08-03 stsp arg.diff_staged = diff_staged;
4274 63035f9f 2019-10-06 stsp arg.ignore_whitespace = ignore_whitespace;
4276 adc19d55 2019-07-28 stsp error = got_pathlist_append(&paths, path, NULL);
4277 72ea6654 2019-07-27 stsp if (error)
4278 72ea6654 2019-07-27 stsp goto done;
4280 72ea6654 2019-07-27 stsp error = got_worktree_status(worktree, &paths, repo, print_diff,
4281 b72f483a 2019-02-05 stsp &arg, check_cancelled, NULL);
4282 b72f483a 2019-02-05 stsp free(id_str);
4283 72ea6654 2019-07-27 stsp got_pathlist_free(&paths);
4284 b72f483a 2019-02-05 stsp goto done;
4287 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&id1, &label1, id_str1,
4288 71a27632 2020-01-15 stsp GOT_OBJ_TYPE_ANY, 1, repo);
4289 0adb7196 2019-08-11 stsp if (error)
4290 0adb7196 2019-08-11 stsp goto done;
4292 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&id2, &label2, id_str2,
4293 71a27632 2020-01-15 stsp GOT_OBJ_TYPE_ANY, 1, repo);
4294 0adb7196 2019-08-11 stsp if (error)
4295 0adb7196 2019-08-11 stsp goto done;
4297 15a94983 2018-12-23 stsp error = got_object_get_type(&type1, repo, id1);
4298 15a94983 2018-12-23 stsp if (error)
4299 15a94983 2018-12-23 stsp goto done;
4301 15a94983 2018-12-23 stsp error = got_object_get_type(&type2, repo, id2);
4302 15a94983 2018-12-23 stsp if (error)
4303 15a94983 2018-12-23 stsp goto done;
4305 15a94983 2018-12-23 stsp if (type1 != type2) {
4306 b00d56cd 2018-04-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
4307 b00d56cd 2018-04-01 stsp goto done;
4310 15a94983 2018-12-23 stsp switch (type1) {
4311 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_BLOB:
4312 15a94983 2018-12-23 stsp error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
4313 63035f9f 2019-10-06 stsp diff_context, ignore_whitespace, repo, stdout);
4315 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_TREE:
4316 15a94983 2018-12-23 stsp error = got_diff_objects_as_trees(id1, id2, "", "",
4317 63035f9f 2019-10-06 stsp diff_context, ignore_whitespace, repo, stdout);
4319 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_COMMIT:
4320 5e70831e 2019-05-28 stsp printf("diff %s %s\n", label1, label2);
4321 15a94983 2018-12-23 stsp error = got_diff_objects_as_commits(id1, id2, diff_context,
4322 63035f9f 2019-10-06 stsp ignore_whitespace, repo, stdout);
4325 b00d56cd 2018-04-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
4328 5e70831e 2019-05-28 stsp free(label1);
4329 5e70831e 2019-05-28 stsp free(label2);
4330 15a94983 2018-12-23 stsp free(id1);
4331 15a94983 2018-12-23 stsp free(id2);
4332 927df6b7 2019-02-10 stsp free(path);
4333 b72f483a 2019-02-05 stsp if (worktree)
4334 b72f483a 2019-02-05 stsp got_worktree_close(worktree);
4335 ad242220 2018-09-08 stsp if (repo) {
4336 ad242220 2018-09-08 stsp const struct got_error *repo_error;
4337 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
4338 ad242220 2018-09-08 stsp if (error == NULL)
4339 ad242220 2018-09-08 stsp error = repo_error;
4341 b00d56cd 2018-04-01 stsp return error;
4344 404c43c4 2018-06-21 stsp __dead static void
4345 404c43c4 2018-06-21 stsp usage_blame(void)
4347 9270e621 2019-02-05 stsp fprintf(stderr,
4348 9270e621 2019-02-05 stsp "usage: %s blame [-c commit] [-r repository-path] path\n",
4349 404c43c4 2018-06-21 stsp getprogname());
4353 28315671 2019-08-14 stsp struct blame_line {
4354 28315671 2019-08-14 stsp int annotated;
4355 28315671 2019-08-14 stsp char *id_str;
4356 82f6abb8 2019-08-14 stsp char *committer;
4357 11db6024 2019-10-21 stsp char datebuf[11]; /* YYYY-MM-DD + NUL */
4360 28315671 2019-08-14 stsp struct blame_cb_args {
4361 28315671 2019-08-14 stsp struct blame_line *lines;
4362 28315671 2019-08-14 stsp int nlines;
4363 7ef28ff8 2019-08-14 stsp int nlines_prec;
4364 28315671 2019-08-14 stsp int lineno_cur;
4365 28315671 2019-08-14 stsp off_t *line_offsets;
4367 82f6abb8 2019-08-14 stsp struct got_repository *repo;
4370 404c43c4 2018-06-21 stsp static const struct got_error *
4371 28315671 2019-08-14 stsp blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
4373 28315671 2019-08-14 stsp const struct got_error *err = NULL;
4374 28315671 2019-08-14 stsp struct blame_cb_args *a = arg;
4375 28315671 2019-08-14 stsp struct blame_line *bline;
4376 82f6abb8 2019-08-14 stsp char *line = NULL;
4377 28315671 2019-08-14 stsp size_t linesize = 0;
4378 82f6abb8 2019-08-14 stsp struct got_commit_object *commit = NULL;
4379 28315671 2019-08-14 stsp off_t offset;
4380 bcb49d15 2019-08-14 stsp struct tm tm;
4381 bcb49d15 2019-08-14 stsp time_t committer_time;
4383 28315671 2019-08-14 stsp if (nlines != a->nlines ||
4384 28315671 2019-08-14 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
4385 28315671 2019-08-14 stsp return got_error(GOT_ERR_RANGE);
4387 28315671 2019-08-14 stsp if (sigint_received)
4388 28315671 2019-08-14 stsp return got_error(GOT_ERR_ITER_COMPLETED);
4390 2839f8b9 2019-08-18 stsp if (lineno == -1)
4391 2839f8b9 2019-08-18 stsp return NULL; /* no change in this commit */
4393 28315671 2019-08-14 stsp /* Annotate this line. */
4394 28315671 2019-08-14 stsp bline = &a->lines[lineno - 1];
4395 28315671 2019-08-14 stsp if (bline->annotated)
4396 28315671 2019-08-14 stsp return NULL;
4397 28315671 2019-08-14 stsp err = got_object_id_str(&bline->id_str, id);
4399 28315671 2019-08-14 stsp return err;
4401 82f6abb8 2019-08-14 stsp err = got_object_open_as_commit(&commit, a->repo, id);
4403 82f6abb8 2019-08-14 stsp goto done;
4405 82f6abb8 2019-08-14 stsp bline->committer = strdup(got_object_commit_get_committer(commit));
4406 82f6abb8 2019-08-14 stsp if (bline->committer == NULL) {
4407 82f6abb8 2019-08-14 stsp err = got_error_from_errno("strdup");
4408 bcb49d15 2019-08-14 stsp goto done;
4411 bcb49d15 2019-08-14 stsp committer_time = got_object_commit_get_committer_time(commit);
4412 bcb49d15 2019-08-14 stsp if (localtime_r(&committer_time, &tm) == NULL)
4413 bcb49d15 2019-08-14 stsp return got_error_from_errno("localtime_r");
4414 6db9f7f6 2019-12-10 stsp if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
4415 bcb49d15 2019-08-14 stsp &tm) >= sizeof(bline->datebuf)) {
4416 bcb49d15 2019-08-14 stsp err = got_error(GOT_ERR_NO_SPACE);
4417 82f6abb8 2019-08-14 stsp goto done;
4419 28315671 2019-08-14 stsp bline->annotated = 1;
4421 28315671 2019-08-14 stsp /* Print lines annotated so far. */
4422 28315671 2019-08-14 stsp bline = &a->lines[a->lineno_cur - 1];
4423 28315671 2019-08-14 stsp if (!bline->annotated)
4424 82f6abb8 2019-08-14 stsp goto done;
4426 28315671 2019-08-14 stsp offset = a->line_offsets[a->lineno_cur - 1];
4427 82f6abb8 2019-08-14 stsp if (fseeko(a->f, offset, SEEK_SET) == -1) {
4428 82f6abb8 2019-08-14 stsp err = got_error_from_errno("fseeko");
4429 82f6abb8 2019-08-14 stsp goto done;
4432 28315671 2019-08-14 stsp while (bline->annotated) {
4433 82f6abb8 2019-08-14 stsp char *smallerthan, *at, *nl, *committer;
4434 82f6abb8 2019-08-14 stsp size_t len;
4436 500467ff 2019-09-25 hiltjo if (getline(&line, &linesize, a->f) == -1) {
4437 28315671 2019-08-14 stsp if (ferror(a->f))
4438 28315671 2019-08-14 stsp err = got_error_from_errno("getline");
4442 82f6abb8 2019-08-14 stsp committer = bline->committer;
4443 82f6abb8 2019-08-14 stsp smallerthan = strchr(committer, '<');
4444 82f6abb8 2019-08-14 stsp if (smallerthan && smallerthan[1] != '\0')
4445 82f6abb8 2019-08-14 stsp committer = smallerthan + 1;
4446 82f6abb8 2019-08-14 stsp at = strchr(committer, '@');
4448 82f6abb8 2019-08-14 stsp *at = '\0';
4449 82f6abb8 2019-08-14 stsp len = strlen(committer);
4450 82f6abb8 2019-08-14 stsp if (len >= 9)
4451 82f6abb8 2019-08-14 stsp committer[8] = '\0';
4453 28315671 2019-08-14 stsp nl = strchr(line, '\n');
4455 28315671 2019-08-14 stsp *nl = '\0';
4456 bcb49d15 2019-08-14 stsp printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
4457 bcb49d15 2019-08-14 stsp bline->id_str, bline->datebuf, committer, line);
4459 28315671 2019-08-14 stsp a->lineno_cur++;
4460 28315671 2019-08-14 stsp bline = &a->lines[a->lineno_cur - 1];
4463 82f6abb8 2019-08-14 stsp if (commit)
4464 82f6abb8 2019-08-14 stsp got_object_commit_close(commit);
4465 28315671 2019-08-14 stsp free(line);
4466 28315671 2019-08-14 stsp return err;
4469 28315671 2019-08-14 stsp static const struct got_error *
4470 404c43c4 2018-06-21 stsp cmd_blame(int argc, char *argv[])
4472 404c43c4 2018-06-21 stsp const struct got_error *error;
4473 404c43c4 2018-06-21 stsp struct got_repository *repo = NULL;
4474 0c06baac 2019-02-05 stsp struct got_worktree *worktree = NULL;
4475 66bea077 2018-08-02 stsp char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4476 0587e10c 2020-07-23 stsp char *link_target = NULL;
4477 28315671 2019-08-14 stsp struct got_object_id *obj_id = NULL;
4478 404c43c4 2018-06-21 stsp struct got_object_id *commit_id = NULL;
4479 28315671 2019-08-14 stsp struct got_blob_object *blob = NULL;
4480 404c43c4 2018-06-21 stsp char *commit_id_str = NULL;
4481 28315671 2019-08-14 stsp struct blame_cb_args bca;
4482 28315671 2019-08-14 stsp int ch, obj_type, i;
4483 b02560ec 2019-08-19 stsp size_t filesize;
4485 90f3c347 2019-08-19 stsp memset(&bca, 0, sizeof(bca));
4487 404c43c4 2018-06-21 stsp #ifndef PROFILE
4488 36e2fb66 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4489 36e2fb66 2019-01-04 stsp NULL) == -1)
4490 404c43c4 2018-06-21 stsp err(1, "pledge");
4493 66bea077 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
4494 404c43c4 2018-06-21 stsp switch (ch) {
4496 404c43c4 2018-06-21 stsp commit_id_str = optarg;
4499 66bea077 2018-08-02 stsp repo_path = realpath(optarg, NULL);
4500 66bea077 2018-08-02 stsp if (repo_path == NULL)
4501 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
4503 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
4506 2deda0b9 2019-03-07 stsp usage_blame();
4507 404c43c4 2018-06-21 stsp /* NOTREACHED */
4511 404c43c4 2018-06-21 stsp argc -= optind;
4512 404c43c4 2018-06-21 stsp argv += optind;
4514 a39318fd 2018-08-02 stsp if (argc == 1)
4515 404c43c4 2018-06-21 stsp path = argv[0];
4517 404c43c4 2018-06-21 stsp usage_blame();
4519 66bea077 2018-08-02 stsp cwd = getcwd(NULL, 0);
4520 66bea077 2018-08-02 stsp if (cwd == NULL) {
4521 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
4522 66bea077 2018-08-02 stsp goto done;
4524 66bea077 2018-08-02 stsp if (repo_path == NULL) {
4525 0c06baac 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
4526 0c06baac 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4527 66bea077 2018-08-02 stsp goto done;
4529 0c06baac 2019-02-05 stsp error = NULL;
4530 0c06baac 2019-02-05 stsp if (worktree) {
4531 0c06baac 2019-02-05 stsp repo_path =
4532 0c06baac 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
4533 1f03b8da 2020-03-20 stsp if (repo_path == NULL) {
4534 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
4535 1f03b8da 2020-03-20 stsp if (error)
4536 1f03b8da 2020-03-20 stsp goto done;
4539 0c06baac 2019-02-05 stsp repo_path = strdup(cwd);
4540 0c06baac 2019-02-05 stsp if (repo_path == NULL) {
4541 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
4542 0c06baac 2019-02-05 stsp goto done;
4547 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
4548 c02c541e 2019-03-29 stsp if (error != NULL)
4549 36e2fb66 2019-01-04 stsp goto done;
4551 c530dc23 2019-07-23 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4552 c02c541e 2019-03-29 stsp if (error)
4553 404c43c4 2018-06-21 stsp goto done;
4555 0c06baac 2019-02-05 stsp if (worktree) {
4556 6efaaa2d 2019-02-05 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
4557 0c06baac 2019-02-05 stsp char *p, *worktree_subdir = cwd +
4558 0c06baac 2019-02-05 stsp strlen(got_worktree_get_root_path(worktree));
4559 6efaaa2d 2019-02-05 stsp if (asprintf(&p, "%s%s%s%s%s",
4560 6efaaa2d 2019-02-05 stsp prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
4561 6efaaa2d 2019-02-05 stsp worktree_subdir, worktree_subdir[0] ? "/" : "",
4562 6efaaa2d 2019-02-05 stsp path) == -1) {
4563 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
4564 0c06baac 2019-02-05 stsp goto done;
4566 6efaaa2d 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, p, 0);
4569 0c06baac 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
4571 0c06baac 2019-02-05 stsp if (error)
4572 66bea077 2018-08-02 stsp goto done;
4574 404c43c4 2018-06-21 stsp if (commit_id_str == NULL) {
4575 404c43c4 2018-06-21 stsp struct got_reference *head_ref;
4576 a0975128 2020-02-07 stsp error = got_ref_open(&head_ref, repo, worktree ?
4577 a0975128 2020-02-07 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
4578 404c43c4 2018-06-21 stsp if (error != NULL)
4579 66bea077 2018-08-02 stsp goto done;
4580 404c43c4 2018-06-21 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
4581 404c43c4 2018-06-21 stsp got_ref_close(head_ref);
4582 404c43c4 2018-06-21 stsp if (error != NULL)
4583 66bea077 2018-08-02 stsp goto done;
4585 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
4586 71a27632 2020-01-15 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
4587 30837e32 2019-07-25 stsp if (error)
4588 66bea077 2018-08-02 stsp goto done;
4591 0587e10c 2020-07-23 stsp error = got_object_resolve_symlinks(&link_target, in_repo_path,
4592 0587e10c 2020-07-23 stsp commit_id, repo);
4593 0587e10c 2020-07-23 stsp if (error)
4594 0587e10c 2020-07-23 stsp goto done;
4596 0587e10c 2020-07-23 stsp error = got_object_id_by_path(&obj_id, repo, commit_id,
4597 0587e10c 2020-07-23 stsp link_target ? link_target : in_repo_path);
4598 28315671 2019-08-14 stsp if (error)
4599 28315671 2019-08-14 stsp goto done;
4601 28315671 2019-08-14 stsp error = got_object_get_type(&obj_type, repo, obj_id);
4602 28315671 2019-08-14 stsp if (error)
4603 28315671 2019-08-14 stsp goto done;
4605 28315671 2019-08-14 stsp if (obj_type != GOT_OBJ_TYPE_BLOB) {
4606 eb59b6d4 2020-07-23 stsp error = got_error_path(link_target ? link_target : in_repo_path,
4607 eb59b6d4 2020-07-23 stsp GOT_ERR_OBJ_TYPE);
4608 28315671 2019-08-14 stsp goto done;
4611 28315671 2019-08-14 stsp error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
4612 28315671 2019-08-14 stsp if (error)
4613 28315671 2019-08-14 stsp goto done;
4614 28315671 2019-08-14 stsp bca.f = got_opentemp();
4615 28315671 2019-08-14 stsp if (bca.f == NULL) {
4616 28315671 2019-08-14 stsp error = got_error_from_errno("got_opentemp");
4617 28315671 2019-08-14 stsp goto done;
4619 b02560ec 2019-08-19 stsp error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
4620 28315671 2019-08-14 stsp &bca.line_offsets, bca.f, blob);
4621 7ef28ff8 2019-08-14 stsp if (error || bca.nlines == 0)
4622 28315671 2019-08-14 stsp goto done;
4624 b02560ec 2019-08-19 stsp /* Don't include \n at EOF in the blame line count. */
4625 b02560ec 2019-08-19 stsp if (bca.line_offsets[bca.nlines - 1] == filesize)
4626 b02560ec 2019-08-19 stsp bca.nlines--;
4628 28315671 2019-08-14 stsp bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
4629 28315671 2019-08-14 stsp if (bca.lines == NULL) {
4630 28315671 2019-08-14 stsp error = got_error_from_errno("calloc");
4631 28315671 2019-08-14 stsp goto done;
4633 28315671 2019-08-14 stsp bca.lineno_cur = 1;
4634 7ef28ff8 2019-08-14 stsp bca.nlines_prec = 0;
4635 7ef28ff8 2019-08-14 stsp i = bca.nlines;
4636 7ef28ff8 2019-08-14 stsp while (i > 0) {
4638 7ef28ff8 2019-08-14 stsp bca.nlines_prec++;
4640 82f6abb8 2019-08-14 stsp bca.repo = repo;
4642 0587e10c 2020-07-23 stsp error = got_blame(link_target ? link_target : in_repo_path, commit_id,
4643 0587e10c 2020-07-23 stsp repo, blame_cb, &bca, check_cancelled, NULL);
4645 66bea077 2018-08-02 stsp free(in_repo_path);
4646 0587e10c 2020-07-23 stsp free(link_target);
4647 66bea077 2018-08-02 stsp free(repo_path);
4648 66bea077 2018-08-02 stsp free(cwd);
4649 404c43c4 2018-06-21 stsp free(commit_id);
4650 28315671 2019-08-14 stsp free(obj_id);
4652 28315671 2019-08-14 stsp got_object_blob_close(blob);
4653 0c06baac 2019-02-05 stsp if (worktree)
4654 0c06baac 2019-02-05 stsp got_worktree_close(worktree);
4655 ad242220 2018-09-08 stsp if (repo) {
4656 ad242220 2018-09-08 stsp const struct got_error *repo_error;
4657 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
4658 ad242220 2018-09-08 stsp if (error == NULL)
4659 ad242220 2018-09-08 stsp error = repo_error;
4661 3affba96 2019-09-22 stsp if (bca.lines) {
4662 3affba96 2019-09-22 stsp for (i = 0; i < bca.nlines; i++) {
4663 3affba96 2019-09-22 stsp struct blame_line *bline = &bca.lines[i];
4664 3affba96 2019-09-22 stsp free(bline->id_str);
4665 3affba96 2019-09-22 stsp free(bline->committer);
4667 3affba96 2019-09-22 stsp free(bca.lines);
4669 28315671 2019-08-14 stsp free(bca.line_offsets);
4670 28315671 2019-08-14 stsp if (bca.f && fclose(bca.f) == EOF && error == NULL)
4671 28315671 2019-08-14 stsp error = got_error_from_errno("fclose");
4672 404c43c4 2018-06-21 stsp return error;
4675 5de5890b 2018-10-18 stsp __dead static void
4676 5de5890b 2018-10-18 stsp usage_tree(void)
4678 c1669e2e 2019-01-09 stsp fprintf(stderr,
4679 5d58be12 2020-05-17 stsp "usage: %s tree [-c commit] [-r repository-path] [-iR] [path]\n",
4680 5de5890b 2018-10-18 stsp getprogname());
4684 0d6c6ee3 2020-05-20 stsp static const struct got_error *
4685 c1669e2e 2019-01-09 stsp print_entry(struct got_tree_entry *te, const char *id, const char *path,
4686 0d6c6ee3 2020-05-20 stsp const char *root_path, struct got_repository *repo)
4688 0d6c6ee3 2020-05-20 stsp const struct got_error *err = NULL;
4689 c1669e2e 2019-01-09 stsp int is_root_path = (strcmp(path, root_path) == 0);
4690 848d6979 2019-08-12 stsp const char *modestr = "";
4691 56e0773d 2019-11-28 stsp mode_t mode = got_tree_entry_get_mode(te);
4692 0d6c6ee3 2020-05-20 stsp char *link_target = NULL;
4694 c1669e2e 2019-01-09 stsp path += strlen(root_path);
4695 c1669e2e 2019-01-09 stsp while (path[0] == '/')
4698 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
4699 63c5ca5d 2019-08-24 stsp modestr = "$";
4700 0d6c6ee3 2020-05-20 stsp else if (S_ISLNK(mode)) {
4703 0d6c6ee3 2020-05-20 stsp err = got_tree_entry_get_symlink_target(&link_target, te, repo);
4705 0d6c6ee3 2020-05-20 stsp return err;
4706 0d6c6ee3 2020-05-20 stsp for (i = 0; i < strlen(link_target); i++) {
4707 0d6c6ee3 2020-05-20 stsp if (!isprint((unsigned char)link_target[i]))
4708 0d6c6ee3 2020-05-20 stsp link_target[i] = '?';
4711 848d6979 2019-08-12 stsp modestr = "@";
4713 56e0773d 2019-11-28 stsp else if (S_ISDIR(mode))
4714 848d6979 2019-08-12 stsp modestr = "/";
4715 56e0773d 2019-11-28 stsp else if (mode & S_IXUSR)
4716 848d6979 2019-08-12 stsp modestr = "*";
4718 0d6c6ee3 2020-05-20 stsp printf("%s%s%s%s%s%s%s\n", id ? id : "", path,
4719 0d6c6ee3 2020-05-20 stsp is_root_path ? "" : "/", got_tree_entry_get_name(te), modestr,
4720 0d6c6ee3 2020-05-20 stsp link_target ? " -> ": "", link_target ? link_target : "");
4722 0d6c6ee3 2020-05-20 stsp free(link_target);
4723 0d6c6ee3 2020-05-20 stsp return NULL;
4726 5de5890b 2018-10-18 stsp static const struct got_error *
4727 5de5890b 2018-10-18 stsp print_tree(const char *path, struct got_object_id *commit_id,
4728 c1669e2e 2019-01-09 stsp int show_ids, int recurse, const char *root_path,
4729 c1669e2e 2019-01-09 stsp struct got_repository *repo)
4731 5de5890b 2018-10-18 stsp const struct got_error *err = NULL;
4732 5de5890b 2018-10-18 stsp struct got_object_id *tree_id = NULL;
4733 5de5890b 2018-10-18 stsp struct got_tree_object *tree = NULL;
4734 56e0773d 2019-11-28 stsp int nentries, i;
4736 5de5890b 2018-10-18 stsp err = got_object_id_by_path(&tree_id, repo, commit_id, path);
4738 5de5890b 2018-10-18 stsp goto done;
4740 5de5890b 2018-10-18 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
4742 5de5890b 2018-10-18 stsp goto done;
4743 56e0773d 2019-11-28 stsp nentries = got_object_tree_get_nentries(tree);
4744 56e0773d 2019-11-28 stsp for (i = 0; i < nentries; i++) {
4745 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
4746 5de5890b 2018-10-18 stsp char *id = NULL;
4748 84453469 2018-11-11 stsp if (sigint_received || sigpipe_received)
4751 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(tree, i);
4752 5de5890b 2018-10-18 stsp if (show_ids) {
4753 5de5890b 2018-10-18 stsp char *id_str;
4754 56e0773d 2019-11-28 stsp err = got_object_id_str(&id_str,
4755 56e0773d 2019-11-28 stsp got_tree_entry_get_id(te));
4757 5de5890b 2018-10-18 stsp goto done;
4758 5de5890b 2018-10-18 stsp if (asprintf(&id, "%s ", id_str) == -1) {
4759 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4760 5de5890b 2018-10-18 stsp free(id_str);
4761 5de5890b 2018-10-18 stsp goto done;
4763 5de5890b 2018-10-18 stsp free(id_str);
4765 0d6c6ee3 2020-05-20 stsp err = print_entry(te, id, path, root_path, repo);
4768 0d6c6ee3 2020-05-20 stsp goto done;
4770 56e0773d 2019-11-28 stsp if (recurse && S_ISDIR(got_tree_entry_get_mode(te))) {
4771 c1669e2e 2019-01-09 stsp char *child_path;
4772 c1669e2e 2019-01-09 stsp if (asprintf(&child_path, "%s%s%s", path,
4773 c1669e2e 2019-01-09 stsp path[0] == '/' && path[1] == '\0' ? "" : "/",
4774 56e0773d 2019-11-28 stsp got_tree_entry_get_name(te)) == -1) {
4775 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4776 c1669e2e 2019-01-09 stsp goto done;
4778 c1669e2e 2019-01-09 stsp err = print_tree(child_path, commit_id, show_ids, 1,
4779 c1669e2e 2019-01-09 stsp root_path, repo);
4780 c1669e2e 2019-01-09 stsp free(child_path);
4782 c1669e2e 2019-01-09 stsp goto done;
4787 5de5890b 2018-10-18 stsp got_object_tree_close(tree);
4788 5de5890b 2018-10-18 stsp free(tree_id);
4789 5de5890b 2018-10-18 stsp return err;
4792 5de5890b 2018-10-18 stsp static const struct got_error *
4793 5de5890b 2018-10-18 stsp cmd_tree(int argc, char *argv[])
4795 5de5890b 2018-10-18 stsp const struct got_error *error;
4796 5de5890b 2018-10-18 stsp struct got_repository *repo = NULL;
4797 7a2c19d6 2019-02-05 stsp struct got_worktree *worktree = NULL;
4798 4e0a20a4 2020-03-23 tracey const char *path, *refname = NULL;
4799 7a2c19d6 2019-02-05 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4800 5de5890b 2018-10-18 stsp struct got_object_id *commit_id = NULL;
4801 5de5890b 2018-10-18 stsp char *commit_id_str = NULL;
4802 c1669e2e 2019-01-09 stsp int show_ids = 0, recurse = 0;
4805 5de5890b 2018-10-18 stsp #ifndef PROFILE
4806 0f8d269b 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4807 0f8d269b 2019-01-04 stsp NULL) == -1)
4808 5de5890b 2018-10-18 stsp err(1, "pledge");
4811 c1669e2e 2019-01-09 stsp while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
4812 5de5890b 2018-10-18 stsp switch (ch) {
4814 5de5890b 2018-10-18 stsp commit_id_str = optarg;
4817 5de5890b 2018-10-18 stsp repo_path = realpath(optarg, NULL);
4818 5de5890b 2018-10-18 stsp if (repo_path == NULL)
4819 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
4821 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
4824 5de5890b 2018-10-18 stsp show_ids = 1;
4827 c1669e2e 2019-01-09 stsp recurse = 1;
4830 2deda0b9 2019-03-07 stsp usage_tree();
4831 5de5890b 2018-10-18 stsp /* NOTREACHED */
4835 5de5890b 2018-10-18 stsp argc -= optind;
4836 5de5890b 2018-10-18 stsp argv += optind;
4838 5de5890b 2018-10-18 stsp if (argc == 1)
4839 5de5890b 2018-10-18 stsp path = argv[0];
4840 5de5890b 2018-10-18 stsp else if (argc > 1)
4841 5de5890b 2018-10-18 stsp usage_tree();
4843 7a2c19d6 2019-02-05 stsp path = NULL;
4845 9bf7a39b 2019-02-05 stsp cwd = getcwd(NULL, 0);
4846 9bf7a39b 2019-02-05 stsp if (cwd == NULL) {
4847 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
4848 9bf7a39b 2019-02-05 stsp goto done;
4850 5de5890b 2018-10-18 stsp if (repo_path == NULL) {
4851 7a2c19d6 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
4852 8994de28 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4853 7a2c19d6 2019-02-05 stsp goto done;
4855 7a2c19d6 2019-02-05 stsp error = NULL;
4856 7a2c19d6 2019-02-05 stsp if (worktree) {
4857 7a2c19d6 2019-02-05 stsp repo_path =
4858 7a2c19d6 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
4859 7a2c19d6 2019-02-05 stsp if (repo_path == NULL)
4860 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
4861 7a2c19d6 2019-02-05 stsp if (error)
4862 7a2c19d6 2019-02-05 stsp goto done;
4864 7a2c19d6 2019-02-05 stsp repo_path = strdup(cwd);
4865 7a2c19d6 2019-02-05 stsp if (repo_path == NULL) {
4866 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
4867 7a2c19d6 2019-02-05 stsp goto done;
4872 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
4873 5de5890b 2018-10-18 stsp if (error != NULL)
4874 c02c541e 2019-03-29 stsp goto done;
4876 c530dc23 2019-07-23 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4877 c02c541e 2019-03-29 stsp if (error)
4878 5de5890b 2018-10-18 stsp goto done;
4880 9bf7a39b 2019-02-05 stsp if (path == NULL) {
4881 9bf7a39b 2019-02-05 stsp if (worktree) {
4882 9bf7a39b 2019-02-05 stsp char *p, *worktree_subdir = cwd +
4883 9bf7a39b 2019-02-05 stsp strlen(got_worktree_get_root_path(worktree));
4884 9bf7a39b 2019-02-05 stsp if (asprintf(&p, "%s/%s",
4885 9bf7a39b 2019-02-05 stsp got_worktree_get_path_prefix(worktree),
4886 9bf7a39b 2019-02-05 stsp worktree_subdir) == -1) {
4887 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
4888 9bf7a39b 2019-02-05 stsp goto done;
4890 f43793a4 2020-01-27 stsp error = got_repo_map_path(&in_repo_path, repo, p, 0);
4892 9bf7a39b 2019-02-05 stsp if (error)
4893 9bf7a39b 2019-02-05 stsp goto done;
4895 9bf7a39b 2019-02-05 stsp path = "/";
4897 9bf7a39b 2019-02-05 stsp if (in_repo_path == NULL) {
4898 9bf7a39b 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
4899 9bf7a39b 2019-02-05 stsp if (error != NULL)
4900 9bf7a39b 2019-02-05 stsp goto done;
4903 5de5890b 2018-10-18 stsp if (commit_id_str == NULL) {
4904 5de5890b 2018-10-18 stsp struct got_reference *head_ref;
4905 4e0a20a4 2020-03-23 tracey if (worktree)
4906 4e0a20a4 2020-03-23 tracey refname = got_worktree_get_head_ref_name(worktree);
4908 4e0a20a4 2020-03-23 tracey refname = GOT_REF_HEAD;
4909 4e0a20a4 2020-03-23 tracey error = got_ref_open(&head_ref, repo, refname, 0);
4910 5de5890b 2018-10-18 stsp if (error != NULL)
4911 5de5890b 2018-10-18 stsp goto done;
4912 5de5890b 2018-10-18 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
4913 5de5890b 2018-10-18 stsp got_ref_close(head_ref);
4914 5de5890b 2018-10-18 stsp if (error != NULL)
4915 5de5890b 2018-10-18 stsp goto done;
4917 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
4918 71a27632 2020-01-15 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
4919 30837e32 2019-07-25 stsp if (error)
4920 5de5890b 2018-10-18 stsp goto done;
4923 c1669e2e 2019-01-09 stsp error = print_tree(in_repo_path, commit_id, show_ids, recurse,
4924 c1669e2e 2019-01-09 stsp in_repo_path, repo);
4926 5de5890b 2018-10-18 stsp free(in_repo_path);
4927 5de5890b 2018-10-18 stsp free(repo_path);
4928 5de5890b 2018-10-18 stsp free(cwd);
4929 5de5890b 2018-10-18 stsp free(commit_id);
4930 7a2c19d6 2019-02-05 stsp if (worktree)
4931 7a2c19d6 2019-02-05 stsp got_worktree_close(worktree);
4932 5de5890b 2018-10-18 stsp if (repo) {
4933 5de5890b 2018-10-18 stsp const struct got_error *repo_error;
4934 5de5890b 2018-10-18 stsp repo_error = got_repo_close(repo);
4935 5de5890b 2018-10-18 stsp if (error == NULL)
4936 5de5890b 2018-10-18 stsp error = repo_error;
4938 5de5890b 2018-10-18 stsp return error;
4941 6bad629b 2019-02-04 stsp __dead static void
4942 6bad629b 2019-02-04 stsp usage_status(void)
4944 081470ac 2020-08-13 stsp fprintf(stderr, "usage: %s status [-s status-codes ] [path ...]\n",
4945 081470ac 2020-08-13 stsp getprogname());
4949 b72f483a 2019-02-05 stsp static const struct got_error *
4950 88d0e355 2019-08-03 stsp print_status(void *arg, unsigned char status, unsigned char staged_status,
4951 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
4952 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4953 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
4955 244725f2 2019-08-03 stsp if (status == staged_status && (status == GOT_STATUS_DELETE))
4956 c363b2c1 2019-08-03 stsp status = GOT_STATUS_NO_CHANGE;
4957 081470ac 2020-08-13 stsp if (arg) {
4958 081470ac 2020-08-13 stsp char *status_codes = arg;
4959 081470ac 2020-08-13 stsp size_t ncodes = strlen(status_codes);
4961 081470ac 2020-08-13 stsp for (i = 0; i < ncodes ; i++) {
4962 081470ac 2020-08-13 stsp if (status == status_codes[i] ||
4963 081470ac 2020-08-13 stsp staged_status == status_codes[i])
4966 081470ac 2020-08-13 stsp if (i == ncodes)
4967 081470ac 2020-08-13 stsp return NULL;
4969 88d0e355 2019-08-03 stsp printf("%c%c %s\n", status, staged_status, path);
4970 b72f483a 2019-02-05 stsp return NULL;
4973 6bad629b 2019-02-04 stsp static const struct got_error *
4974 6bad629b 2019-02-04 stsp cmd_status(int argc, char *argv[])
4976 6bad629b 2019-02-04 stsp const struct got_error *error = NULL;
4977 6bad629b 2019-02-04 stsp struct got_repository *repo = NULL;
4978 6bad629b 2019-02-04 stsp struct got_worktree *worktree = NULL;
4979 081470ac 2020-08-13 stsp char *cwd = NULL, *status_codes = NULL;;
4980 72ea6654 2019-07-27 stsp struct got_pathlist_head paths;
4981 a5edda0a 2019-07-27 stsp struct got_pathlist_entry *pe;
4982 081470ac 2020-08-13 stsp int ch, i;
4984 72ea6654 2019-07-27 stsp TAILQ_INIT(&paths);
4986 081470ac 2020-08-13 stsp while ((ch = getopt(argc, argv, "s:")) != -1) {
4987 6bad629b 2019-02-04 stsp switch (ch) {
4989 081470ac 2020-08-13 stsp for (i = 0; i < strlen(optarg); i++) {
4990 081470ac 2020-08-13 stsp switch (optarg[i]) {
4991 081470ac 2020-08-13 stsp case GOT_STATUS_MODIFY:
4992 081470ac 2020-08-13 stsp case GOT_STATUS_ADD:
4993 081470ac 2020-08-13 stsp case GOT_STATUS_DELETE:
4994 081470ac 2020-08-13 stsp case GOT_STATUS_CONFLICT:
4995 081470ac 2020-08-13 stsp case GOT_STATUS_MISSING:
4996 081470ac 2020-08-13 stsp case GOT_STATUS_OBSTRUCTED:
4997 081470ac 2020-08-13 stsp case GOT_STATUS_UNVERSIONED:
4998 081470ac 2020-08-13 stsp case GOT_STATUS_MODE_CHANGE:
4999 081470ac 2020-08-13 stsp case GOT_STATUS_NONEXISTENT:
5002 081470ac 2020-08-13 stsp errx(1, "invalid status code '%c'",
5003 081470ac 2020-08-13 stsp optarg[i]);
5006 081470ac 2020-08-13 stsp status_codes = optarg;
5009 2deda0b9 2019-03-07 stsp usage_status();
5010 6bad629b 2019-02-04 stsp /* NOTREACHED */
5014 6bad629b 2019-02-04 stsp argc -= optind;
5015 6bad629b 2019-02-04 stsp argv += optind;
5017 6bad629b 2019-02-04 stsp #ifndef PROFILE
5018 6bad629b 2019-02-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
5019 6bad629b 2019-02-04 stsp NULL) == -1)
5020 6bad629b 2019-02-04 stsp err(1, "pledge");
5022 927df6b7 2019-02-10 stsp cwd = getcwd(NULL, 0);
5023 927df6b7 2019-02-10 stsp if (cwd == NULL) {
5024 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
5025 927df6b7 2019-02-10 stsp goto done;
5028 927df6b7 2019-02-10 stsp error = got_worktree_open(&worktree, cwd);
5029 fa51e947 2020-03-27 stsp if (error) {
5030 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
5031 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "status", cwd);
5032 a5edda0a 2019-07-27 stsp goto done;
5035 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5037 6bad629b 2019-02-04 stsp if (error != NULL)
5038 6bad629b 2019-02-04 stsp goto done;
5040 d0eebce4 2019-03-11 stsp error = apply_unveil(got_repo_get_path(repo), 1,
5041 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
5042 087fb88c 2019-08-04 stsp if (error)
5043 087fb88c 2019-08-04 stsp goto done;
5045 087fb88c 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5046 6bad629b 2019-02-04 stsp if (error)
5047 6bad629b 2019-02-04 stsp goto done;
5049 081470ac 2020-08-13 stsp error = got_worktree_status(worktree, &paths, repo, print_status,
5050 081470ac 2020-08-13 stsp status_codes, check_cancelled, NULL);
5052 a5edda0a 2019-07-27 stsp TAILQ_FOREACH(pe, &paths, entry)
5053 a5edda0a 2019-07-27 stsp free((char *)pe->path);
5054 72ea6654 2019-07-27 stsp got_pathlist_free(&paths);
5055 927df6b7 2019-02-10 stsp free(cwd);
5056 d0eebce4 2019-03-11 stsp return error;
5059 d0eebce4 2019-03-11 stsp __dead static void
5060 d0eebce4 2019-03-11 stsp usage_ref(void)
5062 d0eebce4 2019-03-11 stsp fprintf(stderr,
5063 e31abbf2 2020-03-22 stsp "usage: %s ref [-r repository] [-l] [-c object] [-s reference] "
5064 e31abbf2 2020-03-22 stsp "[-d] [name]\n",
5065 d0eebce4 2019-03-11 stsp getprogname());
5069 d0eebce4 2019-03-11 stsp static const struct got_error *
5070 b2070a3f 2020-03-22 stsp list_refs(struct got_repository *repo, const char *refname)
5072 d0eebce4 2019-03-11 stsp static const struct got_error *err = NULL;
5073 d0eebce4 2019-03-11 stsp struct got_reflist_head refs;
5074 d0eebce4 2019-03-11 stsp struct got_reflist_entry *re;
5076 d0eebce4 2019-03-11 stsp SIMPLEQ_INIT(&refs);
5077 b2070a3f 2020-03-22 stsp err = got_ref_list(&refs, repo, refname, got_ref_cmp_by_name, NULL);
5079 d0eebce4 2019-03-11 stsp return err;
5081 d0eebce4 2019-03-11 stsp SIMPLEQ_FOREACH(re, &refs, entry) {
5082 d0eebce4 2019-03-11 stsp char *refstr;
5083 d0eebce4 2019-03-11 stsp refstr = got_ref_to_str(re->ref);
5084 d0eebce4 2019-03-11 stsp if (refstr == NULL)
5085 638f9024 2019-05-13 stsp return got_error_from_errno("got_ref_to_str");
5086 d0eebce4 2019-03-11 stsp printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
5087 d0eebce4 2019-03-11 stsp free(refstr);
5090 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
5091 d0eebce4 2019-03-11 stsp return NULL;
5094 d0eebce4 2019-03-11 stsp static const struct got_error *
5095 d0eebce4 2019-03-11 stsp delete_ref(struct got_repository *repo, const char *refname)
5097 d0eebce4 2019-03-11 stsp const struct got_error *err = NULL;
5098 d0eebce4 2019-03-11 stsp struct got_reference *ref;
5100 2f17228e 2019-05-12 stsp err = got_ref_open(&ref, repo, refname, 0);
5102 d0eebce4 2019-03-11 stsp return err;
5104 d0eebce4 2019-03-11 stsp err = got_ref_delete(ref, repo);
5105 d0eebce4 2019-03-11 stsp got_ref_close(ref);
5106 d0eebce4 2019-03-11 stsp return err;
5109 d0eebce4 2019-03-11 stsp static const struct got_error *
5110 d83d9d5c 2019-05-13 stsp add_ref(struct got_repository *repo, const char *refname, const char *target)
5112 d0eebce4 2019-03-11 stsp const struct got_error *err = NULL;
5113 d0eebce4 2019-03-11 stsp struct got_object_id *id;
5114 d0eebce4 2019-03-11 stsp struct got_reference *ref = NULL;
5117 bd5895f3 2019-11-28 stsp * Don't let the user create a reference name with a leading '-'.
5118 d1644381 2019-07-14 stsp * While technically a valid reference name, this case is usually
5119 d1644381 2019-07-14 stsp * an unintended typo.
5121 bd5895f3 2019-11-28 stsp if (refname[0] == '-')
5122 bd5895f3 2019-11-28 stsp return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
5124 dd88155e 2019-06-29 stsp err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
5126 d83d9d5c 2019-05-13 stsp if (err) {
5127 d83d9d5c 2019-05-13 stsp struct got_reference *target_ref;
5129 d83d9d5c 2019-05-13 stsp if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
5130 d83d9d5c 2019-05-13 stsp return err;
5131 d83d9d5c 2019-05-13 stsp err = got_ref_open(&target_ref, repo, target, 0);
5133 d83d9d5c 2019-05-13 stsp return err;
5134 d83d9d5c 2019-05-13 stsp err = got_ref_resolve(&id, repo, target_ref);
5135 d83d9d5c 2019-05-13 stsp got_ref_close(target_ref);
5137 d83d9d5c 2019-05-13 stsp return err;
5140 d0eebce4 2019-03-11 stsp err = got_ref_alloc(&ref, refname, id);
5142 d0eebce4 2019-03-11 stsp goto done;
5144 d0eebce4 2019-03-11 stsp err = got_ref_write(ref, repo);
5147 d0eebce4 2019-03-11 stsp got_ref_close(ref);
5149 d1c1ae5f 2019-08-12 stsp return err;
5152 d1c1ae5f 2019-08-12 stsp static const struct got_error *
5153 d1c1ae5f 2019-08-12 stsp add_symref(struct got_repository *repo, const char *refname, const char *target)
5155 d1c1ae5f 2019-08-12 stsp const struct got_error *err = NULL;
5156 d1c1ae5f 2019-08-12 stsp struct got_reference *ref = NULL;
5157 d1c1ae5f 2019-08-12 stsp struct got_reference *target_ref = NULL;
5160 bd5895f3 2019-11-28 stsp * Don't let the user create a reference name with a leading '-'.
5161 d1c1ae5f 2019-08-12 stsp * While technically a valid reference name, this case is usually
5162 d1c1ae5f 2019-08-12 stsp * an unintended typo.
5164 bd5895f3 2019-11-28 stsp if (refname[0] == '-')
5165 bd5895f3 2019-11-28 stsp return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
5167 d1c1ae5f 2019-08-12 stsp err = got_ref_open(&target_ref, repo, target, 0);
5169 d1c1ae5f 2019-08-12 stsp return err;
5171 d1c1ae5f 2019-08-12 stsp err = got_ref_alloc_symref(&ref, refname, target_ref);
5173 d1c1ae5f 2019-08-12 stsp goto done;
5175 d1c1ae5f 2019-08-12 stsp err = got_ref_write(ref, repo);
5177 d1c1ae5f 2019-08-12 stsp if (target_ref)
5178 d1c1ae5f 2019-08-12 stsp got_ref_close(target_ref);
5180 d1c1ae5f 2019-08-12 stsp got_ref_close(ref);
5181 d0eebce4 2019-03-11 stsp return err;
5184 d0eebce4 2019-03-11 stsp static const struct got_error *
5185 d0eebce4 2019-03-11 stsp cmd_ref(int argc, char *argv[])
5187 d0eebce4 2019-03-11 stsp const struct got_error *error = NULL;
5188 d0eebce4 2019-03-11 stsp struct got_repository *repo = NULL;
5189 d0eebce4 2019-03-11 stsp struct got_worktree *worktree = NULL;
5190 d0eebce4 2019-03-11 stsp char *cwd = NULL, *repo_path = NULL;
5191 e31abbf2 2020-03-22 stsp int ch, do_list = 0, do_delete = 0;
5192 b2070a3f 2020-03-22 stsp const char *obj_arg = NULL, *symref_target= NULL;
5193 b2070a3f 2020-03-22 stsp char *refname = NULL;
5195 e31abbf2 2020-03-22 stsp while ((ch = getopt(argc, argv, "c:dr:ls:")) != -1) {
5196 d0eebce4 2019-03-11 stsp switch (ch) {
5198 e31abbf2 2020-03-22 stsp obj_arg = optarg;
5201 e31abbf2 2020-03-22 stsp do_delete = 1;
5204 d0eebce4 2019-03-11 stsp repo_path = realpath(optarg, NULL);
5205 d0eebce4 2019-03-11 stsp if (repo_path == NULL)
5206 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
5208 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
5211 d0eebce4 2019-03-11 stsp do_list = 1;
5214 e31abbf2 2020-03-22 stsp symref_target = optarg;
5217 d0eebce4 2019-03-11 stsp usage_ref();
5218 d0eebce4 2019-03-11 stsp /* NOTREACHED */
5222 e31abbf2 2020-03-22 stsp if (obj_arg && do_list)
5223 907f15e2 2020-03-22 stsp errx(1, "-c and -l options are mutually exclusive");
5224 e31abbf2 2020-03-22 stsp if (obj_arg && do_delete)
5225 907f15e2 2020-03-22 stsp errx(1, "-c and -d options are mutually exclusive");
5226 907f15e2 2020-03-22 stsp if (obj_arg && symref_target)
5227 907f15e2 2020-03-22 stsp errx(1, "-c and -s options are mutually exclusive");
5228 e31abbf2 2020-03-22 stsp if (symref_target && do_delete)
5229 907f15e2 2020-03-22 stsp errx(1, "-s and -d options are mutually exclusive");
5230 e31abbf2 2020-03-22 stsp if (symref_target && do_list)
5231 907f15e2 2020-03-22 stsp errx(1, "-s and -l options are mutually exclusive");
5232 e31abbf2 2020-03-22 stsp if (do_delete && do_list)
5233 907f15e2 2020-03-22 stsp errx(1, "-d and -l options are mutually exclusive");
5235 d0eebce4 2019-03-11 stsp argc -= optind;
5236 d0eebce4 2019-03-11 stsp argv += optind;
5238 e31abbf2 2020-03-22 stsp if (do_list) {
5239 b2070a3f 2020-03-22 stsp if (argc != 0 && argc != 1)
5240 d0eebce4 2019-03-11 stsp usage_ref();
5241 b2070a3f 2020-03-22 stsp if (argc == 1) {
5242 b2070a3f 2020-03-22 stsp refname = strdup(argv[0]);
5243 b2070a3f 2020-03-22 stsp if (refname == NULL) {
5244 b2070a3f 2020-03-22 stsp error = got_error_from_errno("strdup");
5245 b2070a3f 2020-03-22 stsp goto done;
5249 e31abbf2 2020-03-22 stsp if (argc != 1)
5250 e31abbf2 2020-03-22 stsp usage_ref();
5251 b2070a3f 2020-03-22 stsp refname = strdup(argv[0]);
5252 b2070a3f 2020-03-22 stsp if (refname == NULL) {
5253 b2070a3f 2020-03-22 stsp error = got_error_from_errno("strdup");
5254 b2070a3f 2020-03-22 stsp goto done;
5258 b2070a3f 2020-03-22 stsp if (refname)
5259 b2070a3f 2020-03-22 stsp got_path_strip_trailing_slashes(refname);
5261 d0eebce4 2019-03-11 stsp #ifndef PROFILE
5262 e0b57350 2019-03-12 stsp if (do_list) {
5263 e0b57350 2019-03-12 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
5264 e0b57350 2019-03-12 stsp NULL) == -1)
5265 e0b57350 2019-03-12 stsp err(1, "pledge");
5267 e0b57350 2019-03-12 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
5268 e0b57350 2019-03-12 stsp "sendfd unveil", NULL) == -1)
5269 e0b57350 2019-03-12 stsp err(1, "pledge");
5272 d0eebce4 2019-03-11 stsp cwd = getcwd(NULL, 0);
5273 d0eebce4 2019-03-11 stsp if (cwd == NULL) {
5274 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
5275 d0eebce4 2019-03-11 stsp goto done;
5278 d0eebce4 2019-03-11 stsp if (repo_path == NULL) {
5279 d0eebce4 2019-03-11 stsp error = got_worktree_open(&worktree, cwd);
5280 d0eebce4 2019-03-11 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
5281 d0eebce4 2019-03-11 stsp goto done;
5283 d0eebce4 2019-03-11 stsp error = NULL;
5284 d0eebce4 2019-03-11 stsp if (worktree) {
5285 d0eebce4 2019-03-11 stsp repo_path =
5286 d0eebce4 2019-03-11 stsp strdup(got_worktree_get_repo_path(worktree));
5287 d0eebce4 2019-03-11 stsp if (repo_path == NULL)
5288 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
5289 d0eebce4 2019-03-11 stsp if (error)
5290 d0eebce4 2019-03-11 stsp goto done;
5292 d0eebce4 2019-03-11 stsp repo_path = strdup(cwd);
5293 d0eebce4 2019-03-11 stsp if (repo_path == NULL) {
5294 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
5295 d0eebce4 2019-03-11 stsp goto done;
5300 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
5301 d0eebce4 2019-03-11 stsp if (error != NULL)
5302 d0eebce4 2019-03-11 stsp goto done;
5304 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), do_list,
5305 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
5306 c02c541e 2019-03-29 stsp if (error)
5307 c02c541e 2019-03-29 stsp goto done;
5309 d0eebce4 2019-03-11 stsp if (do_list)
5310 b2070a3f 2020-03-22 stsp error = list_refs(repo, refname);
5311 e31abbf2 2020-03-22 stsp else if (do_delete)
5312 e31abbf2 2020-03-22 stsp error = delete_ref(repo, refname);
5313 e31abbf2 2020-03-22 stsp else if (symref_target)
5314 e31abbf2 2020-03-22 stsp error = add_symref(repo, refname, symref_target);
5316 e31abbf2 2020-03-22 stsp if (obj_arg == NULL)
5317 e31abbf2 2020-03-22 stsp usage_ref();
5318 e31abbf2 2020-03-22 stsp error = add_ref(repo, refname, obj_arg);
5321 b2070a3f 2020-03-22 stsp free(refname);
5323 4e759de4 2019-06-26 stsp got_repo_close(repo);
5324 4e759de4 2019-06-26 stsp if (worktree)
5325 4e759de4 2019-06-26 stsp got_worktree_close(worktree);
5326 4e759de4 2019-06-26 stsp free(cwd);
5327 4e759de4 2019-06-26 stsp free(repo_path);
5328 4e759de4 2019-06-26 stsp return error;
5331 4e759de4 2019-06-26 stsp __dead static void
5332 4e759de4 2019-06-26 stsp usage_branch(void)
5334 4e759de4 2019-06-26 stsp fprintf(stderr,
5335 da76fce2 2020-02-24 stsp "usage: %s branch [-c commit] [-d] [-r repository] [-l] [-n] "
5336 da76fce2 2020-02-24 stsp "[name]\n", getprogname());
5340 4e759de4 2019-06-26 stsp static const struct got_error *
5341 4e99b47e 2019-10-04 stsp list_branch(struct got_repository *repo, struct got_worktree *worktree,
5342 4e99b47e 2019-10-04 stsp struct got_reference *ref)
5344 4e99b47e 2019-10-04 stsp const struct got_error *err = NULL;
5345 4e99b47e 2019-10-04 stsp const char *refname, *marker = " ";
5346 4e99b47e 2019-10-04 stsp char *refstr;
5348 4e99b47e 2019-10-04 stsp refname = got_ref_get_name(ref);
5349 4e99b47e 2019-10-04 stsp if (worktree && strcmp(refname,
5350 4e99b47e 2019-10-04 stsp got_worktree_get_head_ref_name(worktree)) == 0) {
5351 4e99b47e 2019-10-04 stsp struct got_object_id *id = NULL;
5353 4e99b47e 2019-10-04 stsp err = got_ref_resolve(&id, repo, ref);
5355 4e99b47e 2019-10-04 stsp return err;
5356 4e99b47e 2019-10-04 stsp if (got_object_id_cmp(id,
5357 4e99b47e 2019-10-04 stsp got_worktree_get_base_commit_id(worktree)) == 0)
5358 4e99b47e 2019-10-04 stsp marker = "* ";
5360 4e99b47e 2019-10-04 stsp marker = "~ ";
5364 4e99b47e 2019-10-04 stsp if (strncmp(refname, "refs/heads/", 11) == 0)
5365 4e99b47e 2019-10-04 stsp refname += 11;
5366 4e99b47e 2019-10-04 stsp if (strncmp(refname, "refs/got/worktree/", 18) == 0)
5367 4e99b47e 2019-10-04 stsp refname += 18;
5369 4e99b47e 2019-10-04 stsp refstr = got_ref_to_str(ref);
5370 4e99b47e 2019-10-04 stsp if (refstr == NULL)
5371 4e99b47e 2019-10-04 stsp return got_error_from_errno("got_ref_to_str");
5373 4e99b47e 2019-10-04 stsp printf("%s%s: %s\n", marker, refname, refstr);
5374 4e99b47e 2019-10-04 stsp free(refstr);
5375 4e99b47e 2019-10-04 stsp return NULL;
5378 4e99b47e 2019-10-04 stsp static const struct got_error *
5379 ad89fa31 2019-10-04 stsp show_current_branch(struct got_repository *repo, struct got_worktree *worktree)
5381 ad89fa31 2019-10-04 stsp const char *refname;
5383 ad89fa31 2019-10-04 stsp if (worktree == NULL)
5384 ad89fa31 2019-10-04 stsp return got_error(GOT_ERR_NOT_WORKTREE);
5386 ad89fa31 2019-10-04 stsp refname = got_worktree_get_head_ref_name(worktree);
5388 ad89fa31 2019-10-04 stsp if (strncmp(refname, "refs/heads/", 11) == 0)
5389 ad89fa31 2019-10-04 stsp refname += 11;
5390 ad89fa31 2019-10-04 stsp if (strncmp(refname, "refs/got/worktree/", 18) == 0)
5391 ad89fa31 2019-10-04 stsp refname += 18;
5393 ad89fa31 2019-10-04 stsp printf("%s\n", refname);
5395 ad89fa31 2019-10-04 stsp return NULL;
5398 ad89fa31 2019-10-04 stsp static const struct got_error *
5399 ba882ee3 2019-07-11 stsp list_branches(struct got_repository *repo, struct got_worktree *worktree)
5401 4e759de4 2019-06-26 stsp static const struct got_error *err = NULL;
5402 4e759de4 2019-06-26 stsp struct got_reflist_head refs;
5403 4e759de4 2019-06-26 stsp struct got_reflist_entry *re;
5404 4e99b47e 2019-10-04 stsp struct got_reference *temp_ref = NULL;
5405 4e99b47e 2019-10-04 stsp int rebase_in_progress, histedit_in_progress;
5407 4e759de4 2019-06-26 stsp SIMPLEQ_INIT(&refs);
5409 4e99b47e 2019-10-04 stsp if (worktree) {
5410 4e99b47e 2019-10-04 stsp err = got_worktree_rebase_in_progress(&rebase_in_progress,
5411 4e99b47e 2019-10-04 stsp worktree);
5413 4e99b47e 2019-10-04 stsp return err;
5415 4e99b47e 2019-10-04 stsp err = got_worktree_histedit_in_progress(&histedit_in_progress,
5416 4e99b47e 2019-10-04 stsp worktree);
5418 4e99b47e 2019-10-04 stsp return err;
5420 4e99b47e 2019-10-04 stsp if (rebase_in_progress || histedit_in_progress) {
5421 4e99b47e 2019-10-04 stsp err = got_ref_open(&temp_ref, repo,
5422 4e99b47e 2019-10-04 stsp got_worktree_get_head_ref_name(worktree), 0);
5424 4e99b47e 2019-10-04 stsp return err;
5425 4e99b47e 2019-10-04 stsp list_branch(repo, worktree, temp_ref);
5426 4e99b47e 2019-10-04 stsp got_ref_close(temp_ref);
5430 b8bad2ba 2019-08-23 stsp err = got_ref_list(&refs, repo, "refs/heads",
5431 b8bad2ba 2019-08-23 stsp got_ref_cmp_by_name, NULL);
5433 4e759de4 2019-06-26 stsp return err;
5435 4e99b47e 2019-10-04 stsp SIMPLEQ_FOREACH(re, &refs, entry)
5436 4e99b47e 2019-10-04 stsp list_branch(repo, worktree, re->ref);
5438 4e759de4 2019-06-26 stsp got_ref_list_free(&refs);
5439 4e759de4 2019-06-26 stsp return NULL;
5442 4e759de4 2019-06-26 stsp static const struct got_error *
5443 45cd4e47 2019-08-25 stsp delete_branch(struct got_repository *repo, struct got_worktree *worktree,
5444 45cd4e47 2019-08-25 stsp const char *branch_name)
5446 4e759de4 2019-06-26 stsp const struct got_error *err = NULL;
5447 45cd4e47 2019-08-25 stsp struct got_reference *ref = NULL;
5448 4e759de4 2019-06-26 stsp char *refname;
5450 4e759de4 2019-06-26 stsp if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
5451 4e759de4 2019-06-26 stsp return got_error_from_errno("asprintf");
5453 4e759de4 2019-06-26 stsp err = got_ref_open(&ref, repo, refname, 0);
5455 4e759de4 2019-06-26 stsp goto done;
5457 45cd4e47 2019-08-25 stsp if (worktree &&
5458 45cd4e47 2019-08-25 stsp strcmp(got_worktree_get_head_ref_name(worktree),
5459 45cd4e47 2019-08-25 stsp got_ref_get_name(ref)) == 0) {
5460 45cd4e47 2019-08-25 stsp err = got_error_msg(GOT_ERR_SAME_BRANCH,
5461 45cd4e47 2019-08-25 stsp "will not delete this work tree's current branch");
5462 45cd4e47 2019-08-25 stsp goto done;
5465 45cd4e47 2019-08-25 stsp err = got_ref_delete(ref, repo);
5468 45cd4e47 2019-08-25 stsp got_ref_close(ref);
5469 4e759de4 2019-06-26 stsp free(refname);
5470 4e759de4 2019-06-26 stsp return err;
5473 4e759de4 2019-06-26 stsp static const struct got_error *
5474 4e759de4 2019-06-26 stsp add_branch(struct got_repository *repo, const char *branch_name,
5475 a74f7e83 2019-11-10 stsp struct got_object_id *base_commit_id)
5477 4e759de4 2019-06-26 stsp const struct got_error *err = NULL;
5478 4e759de4 2019-06-26 stsp struct got_reference *ref = NULL;
5479 4e759de4 2019-06-26 stsp char *base_refname = NULL, *refname = NULL;
5482 bd5895f3 2019-11-28 stsp * Don't let the user create a branch name with a leading '-'.
5483 d3f84d51 2019-07-11 stsp * While technically a valid reference name, this case is usually
5484 d3f84d51 2019-07-11 stsp * an unintended typo.
5486 bd5895f3 2019-11-28 stsp if (branch_name[0] == '-')
5487 bd5895f3 2019-11-28 stsp return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
5489 4e759de4 2019-06-26 stsp if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
5490 4e759de4 2019-06-26 stsp err = got_error_from_errno("asprintf");
5491 4e759de4 2019-06-26 stsp goto done;
5494 4e759de4 2019-06-26 stsp err = got_ref_open(&ref, repo, refname, 0);
5495 4e759de4 2019-06-26 stsp if (err == NULL) {
5496 4e759de4 2019-06-26 stsp err = got_error(GOT_ERR_BRANCH_EXISTS);
5497 4e759de4 2019-06-26 stsp goto done;
5498 4e759de4 2019-06-26 stsp } else if (err->code != GOT_ERR_NOT_REF)
5499 4e759de4 2019-06-26 stsp goto done;
5501 a74f7e83 2019-11-10 stsp err = got_ref_alloc(&ref, refname, base_commit_id);
5503 4e759de4 2019-06-26 stsp goto done;
5505 4e759de4 2019-06-26 stsp err = got_ref_write(ref, repo);
5508 4e759de4 2019-06-26 stsp got_ref_close(ref);
5509 4e759de4 2019-06-26 stsp free(base_refname);
5510 4e759de4 2019-06-26 stsp free(refname);
5511 4e759de4 2019-06-26 stsp return err;
5514 4e759de4 2019-06-26 stsp static const struct got_error *
5515 4e759de4 2019-06-26 stsp cmd_branch(int argc, char *argv[])
5517 4e759de4 2019-06-26 stsp const struct got_error *error = NULL;
5518 4e759de4 2019-06-26 stsp struct got_repository *repo = NULL;
5519 4e759de4 2019-06-26 stsp struct got_worktree *worktree = NULL;
5520 4e759de4 2019-06-26 stsp char *cwd = NULL, *repo_path = NULL;
5521 da76fce2 2020-02-24 stsp int ch, do_list = 0, do_show = 0, do_update = 1;
5522 a74f7e83 2019-11-10 stsp const char *delref = NULL, *commit_id_arg = NULL;
5523 da76fce2 2020-02-24 stsp struct got_reference *ref = NULL;
5524 da76fce2 2020-02-24 stsp struct got_pathlist_head paths;
5525 da76fce2 2020-02-24 stsp struct got_pathlist_entry *pe;
5526 da76fce2 2020-02-24 stsp struct got_object_id *commit_id = NULL;
5527 da76fce2 2020-02-24 stsp char *commit_id_str = NULL;
5529 da76fce2 2020-02-24 stsp TAILQ_INIT(&paths);
5531 da76fce2 2020-02-24 stsp while ((ch = getopt(argc, argv, "c:d:r:ln")) != -1) {
5532 4e759de4 2019-06-26 stsp switch (ch) {
5534 a74f7e83 2019-11-10 stsp commit_id_arg = optarg;
5537 4e759de4 2019-06-26 stsp delref = optarg;
5540 4e759de4 2019-06-26 stsp repo_path = realpath(optarg, NULL);
5541 4e759de4 2019-06-26 stsp if (repo_path == NULL)
5542 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
5544 4e759de4 2019-06-26 stsp got_path_strip_trailing_slashes(repo_path);
5547 4e759de4 2019-06-26 stsp do_list = 1;
5550 da76fce2 2020-02-24 stsp do_update = 0;
5553 4e759de4 2019-06-26 stsp usage_branch();
5554 4e759de4 2019-06-26 stsp /* NOTREACHED */
5558 4e759de4 2019-06-26 stsp if (do_list && delref)
5559 907f15e2 2020-03-22 stsp errx(1, "-l and -d options are mutually exclusive");
5561 4e759de4 2019-06-26 stsp argc -= optind;
5562 4e759de4 2019-06-26 stsp argv += optind;
5564 ad89fa31 2019-10-04 stsp if (!do_list && !delref && argc == 0)
5565 ad89fa31 2019-10-04 stsp do_show = 1;
5567 a74f7e83 2019-11-10 stsp if ((do_list || delref || do_show) && commit_id_arg != NULL)
5568 a74f7e83 2019-11-10 stsp errx(1, "-c option can only be used when creating a branch");
5570 4e759de4 2019-06-26 stsp if (do_list || delref) {
5571 4e759de4 2019-06-26 stsp if (argc > 0)
5572 4e759de4 2019-06-26 stsp usage_branch();
5573 a74f7e83 2019-11-10 stsp } else if (!do_show && argc != 1)
5574 4e759de4 2019-06-26 stsp usage_branch();
5576 4e759de4 2019-06-26 stsp #ifndef PROFILE
5577 ad89fa31 2019-10-04 stsp if (do_list || do_show) {
5578 4e759de4 2019-06-26 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
5579 4e759de4 2019-06-26 stsp NULL) == -1)
5580 4e759de4 2019-06-26 stsp err(1, "pledge");
5582 4e759de4 2019-06-26 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
5583 4e759de4 2019-06-26 stsp "sendfd unveil", NULL) == -1)
5584 4e759de4 2019-06-26 stsp err(1, "pledge");
5587 4e759de4 2019-06-26 stsp cwd = getcwd(NULL, 0);
5588 4e759de4 2019-06-26 stsp if (cwd == NULL) {
5589 4e759de4 2019-06-26 stsp error = got_error_from_errno("getcwd");
5590 4e759de4 2019-06-26 stsp goto done;
5593 4e759de4 2019-06-26 stsp if (repo_path == NULL) {
5594 4e759de4 2019-06-26 stsp error = got_worktree_open(&worktree, cwd);
5595 4e759de4 2019-06-26 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
5596 4e759de4 2019-06-26 stsp goto done;
5598 4e759de4 2019-06-26 stsp error = NULL;
5599 4e759de4 2019-06-26 stsp if (worktree) {
5600 4e759de4 2019-06-26 stsp repo_path =
5601 4e759de4 2019-06-26 stsp strdup(got_worktree_get_repo_path(worktree));
5602 4e759de4 2019-06-26 stsp if (repo_path == NULL)
5603 4e759de4 2019-06-26 stsp error = got_error_from_errno("strdup");
5604 4e759de4 2019-06-26 stsp if (error)
5605 4e759de4 2019-06-26 stsp goto done;
5607 4e759de4 2019-06-26 stsp repo_path = strdup(cwd);
5608 4e759de4 2019-06-26 stsp if (repo_path == NULL) {
5609 4e759de4 2019-06-26 stsp error = got_error_from_errno("strdup");
5610 4e759de4 2019-06-26 stsp goto done;
5615 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
5616 4e759de4 2019-06-26 stsp if (error != NULL)
5617 4e759de4 2019-06-26 stsp goto done;
5619 4e759de4 2019-06-26 stsp error = apply_unveil(got_repo_get_path(repo), do_list,
5620 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
5621 4e759de4 2019-06-26 stsp if (error)
5622 4e759de4 2019-06-26 stsp goto done;
5624 ad89fa31 2019-10-04 stsp if (do_show)
5625 ad89fa31 2019-10-04 stsp error = show_current_branch(repo, worktree);
5626 ad89fa31 2019-10-04 stsp else if (do_list)
5627 ba882ee3 2019-07-11 stsp error = list_branches(repo, worktree);
5628 4e759de4 2019-06-26 stsp else if (delref)
5629 45cd4e47 2019-08-25 stsp error = delete_branch(repo, worktree, delref);
5631 a74f7e83 2019-11-10 stsp if (commit_id_arg == NULL)
5632 a74f7e83 2019-11-10 stsp commit_id_arg = worktree ?
5633 4e759de4 2019-06-26 stsp got_worktree_get_head_ref_name(worktree) :
5634 4e759de4 2019-06-26 stsp GOT_REF_HEAD;
5635 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
5636 71a27632 2020-01-15 stsp commit_id_arg, GOT_OBJ_TYPE_COMMIT, 1, repo);
5637 a74f7e83 2019-11-10 stsp if (error)
5638 a74f7e83 2019-11-10 stsp goto done;
5639 a74f7e83 2019-11-10 stsp error = add_branch(repo, argv[0], commit_id);
5640 da76fce2 2020-02-24 stsp if (error)
5641 da76fce2 2020-02-24 stsp goto done;
5642 da76fce2 2020-02-24 stsp if (worktree && do_update) {
5643 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
5644 da76fce2 2020-02-24 stsp char *branch_refname = NULL;
5646 da76fce2 2020-02-24 stsp error = got_object_id_str(&commit_id_str, commit_id);
5647 da76fce2 2020-02-24 stsp if (error)
5648 da76fce2 2020-02-24 stsp goto done;
5649 da76fce2 2020-02-24 stsp error = get_worktree_paths_from_argv(&paths, 0, NULL,
5650 da76fce2 2020-02-24 stsp worktree);
5651 da76fce2 2020-02-24 stsp if (error)
5652 da76fce2 2020-02-24 stsp goto done;
5653 da76fce2 2020-02-24 stsp if (asprintf(&branch_refname, "refs/heads/%s", argv[0])
5655 da76fce2 2020-02-24 stsp error = got_error_from_errno("asprintf");
5656 da76fce2 2020-02-24 stsp goto done;
5658 da76fce2 2020-02-24 stsp error = got_ref_open(&ref, repo, branch_refname, 0);
5659 da76fce2 2020-02-24 stsp free(branch_refname);
5660 da76fce2 2020-02-24 stsp if (error)
5661 da76fce2 2020-02-24 stsp goto done;
5662 da76fce2 2020-02-24 stsp error = switch_head_ref(ref, commit_id, worktree,
5664 da76fce2 2020-02-24 stsp if (error)
5665 da76fce2 2020-02-24 stsp goto done;
5666 da76fce2 2020-02-24 stsp error = got_worktree_set_base_commit_id(worktree, repo,
5667 da76fce2 2020-02-24 stsp commit_id);
5668 da76fce2 2020-02-24 stsp if (error)
5669 da76fce2 2020-02-24 stsp goto done;
5670 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
5671 da76fce2 2020-02-24 stsp error = got_worktree_checkout_files(worktree, &paths,
5672 9627c110 2020-04-18 stsp repo, update_progress, &upa, check_cancelled,
5674 da76fce2 2020-02-24 stsp if (error)
5675 da76fce2 2020-02-24 stsp goto done;
5676 9627c110 2020-04-18 stsp if (upa.did_something)
5677 da76fce2 2020-02-24 stsp printf("Updated to commit %s\n", commit_id_str);
5678 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
5683 da76fce2 2020-02-24 stsp got_ref_close(ref);
5685 d0eebce4 2019-03-11 stsp got_repo_close(repo);
5686 d0eebce4 2019-03-11 stsp if (worktree)
5687 d0eebce4 2019-03-11 stsp got_worktree_close(worktree);
5688 d0eebce4 2019-03-11 stsp free(cwd);
5689 d0eebce4 2019-03-11 stsp free(repo_path);
5690 da76fce2 2020-02-24 stsp free(commit_id);
5691 da76fce2 2020-02-24 stsp free(commit_id_str);
5692 da76fce2 2020-02-24 stsp TAILQ_FOREACH(pe, &paths, entry)
5693 da76fce2 2020-02-24 stsp free((char *)pe->path);
5694 da76fce2 2020-02-24 stsp got_pathlist_free(&paths);
5695 d00136be 2019-03-26 stsp return error;
5699 d00136be 2019-03-26 stsp __dead static void
5700 8e7bd50a 2019-08-22 stsp usage_tag(void)
5702 8e7bd50a 2019-08-22 stsp fprintf(stderr,
5703 80106605 2020-02-24 stsp "usage: %s tag [-c commit] [-r repository] [-l] "
5704 80106605 2020-02-24 stsp "[-m message] name\n", getprogname());
5709 b8bad2ba 2019-08-23 stsp static const struct got_error *
5710 b8bad2ba 2019-08-23 stsp sort_tags(struct got_reflist_head *sorted, struct got_reflist_head *tags)
5712 b8bad2ba 2019-08-23 stsp const struct got_error *err = NULL;
5713 b8bad2ba 2019-08-23 stsp struct got_reflist_entry *re, *se, *new;
5714 b8bad2ba 2019-08-23 stsp struct got_object_id *re_id, *se_id;
5715 b8bad2ba 2019-08-23 stsp struct got_tag_object *re_tag, *se_tag;
5716 b8bad2ba 2019-08-23 stsp time_t re_time, se_time;
5718 b8bad2ba 2019-08-23 stsp SIMPLEQ_FOREACH(re, tags, entry) {
5719 b8bad2ba 2019-08-23 stsp se = SIMPLEQ_FIRST(sorted);
5720 b8bad2ba 2019-08-23 stsp if (se == NULL) {
5721 b8bad2ba 2019-08-23 stsp err = got_reflist_entry_dup(&new, re);
5723 b8bad2ba 2019-08-23 stsp return err;
5724 b8bad2ba 2019-08-23 stsp SIMPLEQ_INSERT_HEAD(sorted, new, entry);
5727 b8bad2ba 2019-08-23 stsp err = got_ref_resolve(&re_id, repo, re->ref);
5730 b8bad2ba 2019-08-23 stsp err = got_object_open_as_tag(&re_tag, repo, re_id);
5731 b8bad2ba 2019-08-23 stsp free(re_id);
5734 b8bad2ba 2019-08-23 stsp re_time = got_object_tag_get_tagger_time(re_tag);
5735 b8bad2ba 2019-08-23 stsp got_object_tag_close(re_tag);
5738 b8bad2ba 2019-08-23 stsp while (se) {
5739 b8bad2ba 2019-08-23 stsp err = got_ref_resolve(&se_id, repo, re->ref);
5742 b8bad2ba 2019-08-23 stsp err = got_object_open_as_tag(&se_tag, repo, se_id);
5743 b8bad2ba 2019-08-23 stsp free(se_id);
5746 b8bad2ba 2019-08-23 stsp se_time = got_object_tag_get_tagger_time(se_tag);
5747 b8bad2ba 2019-08-23 stsp got_object_tag_close(se_tag);
5749 b8bad2ba 2019-08-23 stsp if (se_time > re_time) {
5750 b8bad2ba 2019-08-23 stsp err = got_reflist_entry_dup(&new, re);
5752 b8bad2ba 2019-08-23 stsp return err;
5753 b8bad2ba 2019-08-23 stsp SIMPLEQ_INSERT_AFTER(sorted, se, new, entry);
5756 b8bad2ba 2019-08-23 stsp se = SIMPLEQ_NEXT(se, entry);
5761 b8bad2ba 2019-08-23 stsp return err;
5765 b8bad2ba 2019-08-23 stsp static const struct got_error *
5766 8e7bd50a 2019-08-22 stsp list_tags(struct got_repository *repo, struct got_worktree *worktree)
5768 8e7bd50a 2019-08-22 stsp static const struct got_error *err = NULL;
5769 8e7bd50a 2019-08-22 stsp struct got_reflist_head refs;
5770 8e7bd50a 2019-08-22 stsp struct got_reflist_entry *re;
5772 8e7bd50a 2019-08-22 stsp SIMPLEQ_INIT(&refs);
5774 d1f16636 2020-01-15 stsp err = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags, repo);
5776 8e7bd50a 2019-08-22 stsp return err;
5778 8e7bd50a 2019-08-22 stsp SIMPLEQ_FOREACH(re, &refs, entry) {
5779 8e7bd50a 2019-08-22 stsp const char *refname;
5780 8e7bd50a 2019-08-22 stsp char *refstr, *tagmsg0, *tagmsg, *line, *id_str, *datestr;
5781 8e7bd50a 2019-08-22 stsp char datebuf[26];
5782 d4efa91b 2020-01-14 stsp const char *tagger;
5783 8e7bd50a 2019-08-22 stsp time_t tagger_time;
5784 8e7bd50a 2019-08-22 stsp struct got_object_id *id;
5785 8e7bd50a 2019-08-22 stsp struct got_tag_object *tag;
5786 d4efa91b 2020-01-14 stsp struct got_commit_object *commit = NULL;
5788 8e7bd50a 2019-08-22 stsp refname = got_ref_get_name(re->ref);
5789 8e7bd50a 2019-08-22 stsp if (strncmp(refname, "refs/tags/", 10) != 0)
5791 8e7bd50a 2019-08-22 stsp refname += 10;
5792 8e7bd50a 2019-08-22 stsp refstr = got_ref_to_str(re->ref);
5793 8e7bd50a 2019-08-22 stsp if (refstr == NULL) {
5794 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("got_ref_to_str");
5797 8e7bd50a 2019-08-22 stsp printf("%stag %s %s\n", GOT_COMMIT_SEP_STR, refname, refstr);
5798 8e7bd50a 2019-08-22 stsp free(refstr);
5800 8e7bd50a 2019-08-22 stsp err = got_ref_resolve(&id, repo, re->ref);
5803 8e7bd50a 2019-08-22 stsp err = got_object_open_as_tag(&tag, repo, id);
5804 d4efa91b 2020-01-14 stsp if (err) {
5805 d4efa91b 2020-01-14 stsp if (err->code != GOT_ERR_OBJ_TYPE) {
5809 d4efa91b 2020-01-14 stsp /* "lightweight" tag */
5810 d4efa91b 2020-01-14 stsp err = got_object_open_as_commit(&commit, repo, id);
5811 d4efa91b 2020-01-14 stsp if (err) {
5815 d4efa91b 2020-01-14 stsp tagger = got_object_commit_get_committer(commit);
5816 d4efa91b 2020-01-14 stsp tagger_time =
5817 d4efa91b 2020-01-14 stsp got_object_commit_get_committer_time(commit);
5818 d4efa91b 2020-01-14 stsp err = got_object_id_str(&id_str, id);
5824 d4efa91b 2020-01-14 stsp tagger = got_object_tag_get_tagger(tag);
5825 d4efa91b 2020-01-14 stsp tagger_time = got_object_tag_get_tagger_time(tag);
5826 d4efa91b 2020-01-14 stsp err = got_object_id_str(&id_str,
5827 d4efa91b 2020-01-14 stsp got_object_tag_get_object_id(tag));
5831 d4efa91b 2020-01-14 stsp printf("from: %s\n", tagger);
5832 2417344c 2019-08-23 stsp datestr = get_datestr(&tagger_time, datebuf);
5833 2417344c 2019-08-23 stsp if (datestr)
5834 2417344c 2019-08-23 stsp printf("date: %s UTC\n", datestr);
5835 d4efa91b 2020-01-14 stsp if (commit)
5836 2417344c 2019-08-23 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
5838 d4efa91b 2020-01-14 stsp switch (got_object_tag_get_object_type(tag)) {
5839 d4efa91b 2020-01-14 stsp case GOT_OBJ_TYPE_BLOB:
5840 d4efa91b 2020-01-14 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_BLOB,
5843 d4efa91b 2020-01-14 stsp case GOT_OBJ_TYPE_TREE:
5844 d4efa91b 2020-01-14 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_TREE,
5847 d4efa91b 2020-01-14 stsp case GOT_OBJ_TYPE_COMMIT:
5848 d4efa91b 2020-01-14 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT,
5851 d4efa91b 2020-01-14 stsp case GOT_OBJ_TYPE_TAG:
5852 d4efa91b 2020-01-14 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_TAG,
5859 8e7bd50a 2019-08-22 stsp free(id_str);
5860 d4efa91b 2020-01-14 stsp if (commit) {
5861 d4efa91b 2020-01-14 stsp err = got_object_commit_get_logmsg(&tagmsg0, commit);
5864 d4efa91b 2020-01-14 stsp got_object_commit_close(commit);
5866 d4efa91b 2020-01-14 stsp tagmsg0 = strdup(got_object_tag_get_message(tag));
5867 d4efa91b 2020-01-14 stsp got_object_tag_close(tag);
5868 d4efa91b 2020-01-14 stsp if (tagmsg0 == NULL) {
5869 d4efa91b 2020-01-14 stsp err = got_error_from_errno("strdup");
5874 8e7bd50a 2019-08-22 stsp tagmsg = tagmsg0;
5876 8e7bd50a 2019-08-22 stsp line = strsep(&tagmsg, "\n");
5878 8e7bd50a 2019-08-22 stsp printf(" %s\n", line);
5879 8e7bd50a 2019-08-22 stsp } while (line);
5880 8e7bd50a 2019-08-22 stsp free(tagmsg0);
5883 8e7bd50a 2019-08-22 stsp got_ref_list_free(&refs);
5884 8e7bd50a 2019-08-22 stsp return NULL;
5887 8e7bd50a 2019-08-22 stsp static const struct got_error *
5888 f372d5cd 2019-10-21 stsp get_tag_message(char **tagmsg, char **tagmsg_path, const char *commit_id_str,
5889 62870f63 2019-08-22 stsp const char *tag_name, const char *repo_path)
5891 8e7bd50a 2019-08-22 stsp const struct got_error *err = NULL;
5892 8e7bd50a 2019-08-22 stsp char *template = NULL, *initial_content = NULL;
5893 f372d5cd 2019-10-21 stsp char *editor = NULL;
5894 1601cb9f 2020-09-11 naddy int initial_content_len;
5895 8e7bd50a 2019-08-22 stsp int fd = -1;
5897 bb63914a 2020-02-17 stsp if (asprintf(&template, GOT_TMPDIR_STR "/got-tagmsg") == -1) {
5898 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("asprintf");
5899 8e7bd50a 2019-08-22 stsp goto done;
5902 1601cb9f 2020-09-11 naddy initial_content_len = asprintf(&initial_content,
5903 1601cb9f 2020-09-11 naddy "\n# tagging commit %s as %s\n",
5904 1601cb9f 2020-09-11 naddy commit_id_str, tag_name);
5905 1601cb9f 2020-09-11 naddy if (initial_content_len == -1) {
5906 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("asprintf");
5907 8e7bd50a 2019-08-22 stsp goto done;
5910 f372d5cd 2019-10-21 stsp err = got_opentemp_named_fd(tagmsg_path, &fd, template);
5912 8e7bd50a 2019-08-22 stsp goto done;
5914 97972933 2020-09-11 stsp if (write(fd, initial_content, initial_content_len) == -1) {
5915 97972933 2020-09-11 stsp err = got_error_from_errno2("write", *tagmsg_path);
5916 97972933 2020-09-11 stsp goto done;
5919 8e7bd50a 2019-08-22 stsp err = get_editor(&editor);
5921 8e7bd50a 2019-08-22 stsp goto done;
5922 bfa12d5e 2020-09-26 stsp err = edit_logmsg(tagmsg, editor, *tagmsg_path, initial_content,
5923 bfa12d5e 2020-09-26 stsp initial_content_len);
5925 8e7bd50a 2019-08-22 stsp free(initial_content);
5926 8e7bd50a 2019-08-22 stsp free(template);
5927 8e7bd50a 2019-08-22 stsp free(editor);
5929 97972933 2020-09-11 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
5930 97972933 2020-09-11 stsp err = got_error_from_errno2("close", *tagmsg_path);
5932 8e7bd50a 2019-08-22 stsp /* Editor is done; we can now apply unveil(2) */
5933 59f86c76 2020-09-11 stsp if (err == NULL)
5934 8e7bd50a 2019-08-22 stsp err = apply_unveil(repo_path, 0, NULL);
5935 59f86c76 2020-09-11 stsp if (err) {
5936 59f86c76 2020-09-11 stsp free(*tagmsg);
5937 59f86c76 2020-09-11 stsp *tagmsg = NULL;
5939 8e7bd50a 2019-08-22 stsp return err;
5942 8e7bd50a 2019-08-22 stsp static const struct got_error *
5943 50b0790e 2020-09-11 stsp add_tag(struct got_repository *repo, struct got_worktree *worktree,
5944 50b0790e 2020-09-11 stsp const char *tag_name, const char *commit_arg, const char *tagmsg_arg)
5946 8e7bd50a 2019-08-22 stsp const struct got_error *err = NULL;
5947 8e7bd50a 2019-08-22 stsp struct got_object_id *commit_id = NULL, *tag_id = NULL;
5948 8e7bd50a 2019-08-22 stsp char *label = NULL, *commit_id_str = NULL;
5949 8e7bd50a 2019-08-22 stsp struct got_reference *ref = NULL;
5950 aba9c984 2019-09-08 stsp char *refname = NULL, *tagmsg = NULL, *tagger = NULL;
5951 f372d5cd 2019-10-21 stsp char *tagmsg_path = NULL, *tag_id_str = NULL;
5952 f372d5cd 2019-10-21 stsp int preserve_tagmsg = 0;
5955 bd5895f3 2019-11-28 stsp * Don't let the user create a tag name with a leading '-'.
5956 8e7bd50a 2019-08-22 stsp * While technically a valid reference name, this case is usually
5957 8e7bd50a 2019-08-22 stsp * an unintended typo.
5959 bd5895f3 2019-11-28 stsp if (tag_name[0] == '-')
5960 bd5895f3 2019-11-28 stsp return got_error_path(tag_name, GOT_ERR_REF_NAME_MINUS);
5962 50b0790e 2020-09-11 stsp err = get_author(&tagger, repo, worktree);
5964 8e7bd50a 2019-08-22 stsp return err;
5966 71a27632 2020-01-15 stsp err = got_repo_match_object_id(&commit_id, &label, commit_arg,
5967 8e7bd50a 2019-08-22 stsp GOT_OBJ_TYPE_COMMIT, 1, repo);
5969 8e7bd50a 2019-08-22 stsp goto done;
5971 8e7bd50a 2019-08-22 stsp err = got_object_id_str(&commit_id_str, commit_id);
5973 8e7bd50a 2019-08-22 stsp goto done;
5975 8e7bd50a 2019-08-22 stsp if (strncmp("refs/tags/", tag_name, 10) == 0) {
5976 8e7bd50a 2019-08-22 stsp refname = strdup(tag_name);
5977 8e7bd50a 2019-08-22 stsp if (refname == NULL) {
5978 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("strdup");
5979 8e7bd50a 2019-08-22 stsp goto done;
5981 8e7bd50a 2019-08-22 stsp tag_name += 10;
5982 8e7bd50a 2019-08-22 stsp } else if (asprintf(&refname, "refs/tags/%s", tag_name) == -1) {
5983 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("asprintf");
5984 8e7bd50a 2019-08-22 stsp goto done;
5987 8e7bd50a 2019-08-22 stsp err = got_ref_open(&ref, repo, refname, 0);
5988 8e7bd50a 2019-08-22 stsp if (err == NULL) {
5989 8e7bd50a 2019-08-22 stsp err = got_error(GOT_ERR_TAG_EXISTS);
5990 8e7bd50a 2019-08-22 stsp goto done;
5991 8e7bd50a 2019-08-22 stsp } else if (err->code != GOT_ERR_NOT_REF)
5992 8e7bd50a 2019-08-22 stsp goto done;
5994 8e7bd50a 2019-08-22 stsp if (tagmsg_arg == NULL) {
5995 f372d5cd 2019-10-21 stsp err = get_tag_message(&tagmsg, &tagmsg_path, commit_id_str,
5996 62870f63 2019-08-22 stsp tag_name, got_repo_get_path(repo));
5997 f372d5cd 2019-10-21 stsp if (err) {
5998 f372d5cd 2019-10-21 stsp if (err->code != GOT_ERR_COMMIT_MSG_EMPTY &&
5999 f372d5cd 2019-10-21 stsp tagmsg_path != NULL)
6000 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
6001 8e7bd50a 2019-08-22 stsp goto done;
6005 8e7bd50a 2019-08-22 stsp err = got_object_tag_create(&tag_id, tag_name, commit_id,
6006 8e7bd50a 2019-08-22 stsp tagger, time(NULL), tagmsg ? tagmsg : tagmsg_arg, repo);
6007 f372d5cd 2019-10-21 stsp if (err) {
6008 f372d5cd 2019-10-21 stsp if (tagmsg_path)
6009 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
6010 8e7bd50a 2019-08-22 stsp goto done;
6013 8e7bd50a 2019-08-22 stsp err = got_ref_alloc(&ref, refname, tag_id);
6014 f372d5cd 2019-10-21 stsp if (err) {
6015 f372d5cd 2019-10-21 stsp if (tagmsg_path)
6016 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
6017 8e7bd50a 2019-08-22 stsp goto done;
6020 8e7bd50a 2019-08-22 stsp err = got_ref_write(ref, repo);
6021 f372d5cd 2019-10-21 stsp if (err) {
6022 f372d5cd 2019-10-21 stsp if (tagmsg_path)
6023 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
6024 f372d5cd 2019-10-21 stsp goto done;
6027 f372d5cd 2019-10-21 stsp err = got_object_id_str(&tag_id_str, tag_id);
6028 f372d5cd 2019-10-21 stsp if (err) {
6029 f372d5cd 2019-10-21 stsp if (tagmsg_path)
6030 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
6031 f372d5cd 2019-10-21 stsp goto done;
6033 f372d5cd 2019-10-21 stsp printf("Created tag %s\n", tag_id_str);
6035 f372d5cd 2019-10-21 stsp if (preserve_tagmsg) {
6036 f372d5cd 2019-10-21 stsp fprintf(stderr, "%s: tag message preserved in %s\n",
6037 f372d5cd 2019-10-21 stsp getprogname(), tagmsg_path);
6038 f372d5cd 2019-10-21 stsp } else if (tagmsg_path && unlink(tagmsg_path) == -1 && err == NULL)
6039 f372d5cd 2019-10-21 stsp err = got_error_from_errno2("unlink", tagmsg_path);
6040 f372d5cd 2019-10-21 stsp free(tag_id_str);
6042 8e7bd50a 2019-08-22 stsp got_ref_close(ref);
6043 8e7bd50a 2019-08-22 stsp free(commit_id);
6044 8e7bd50a 2019-08-22 stsp free(commit_id_str);
6045 8e7bd50a 2019-08-22 stsp free(refname);
6046 8e7bd50a 2019-08-22 stsp free(tagmsg);
6047 f372d5cd 2019-10-21 stsp free(tagmsg_path);
6048 aba9c984 2019-09-08 stsp free(tagger);
6049 8e7bd50a 2019-08-22 stsp return err;
6052 8e7bd50a 2019-08-22 stsp static const struct got_error *
6053 8e7bd50a 2019-08-22 stsp cmd_tag(int argc, char *argv[])
6055 8e7bd50a 2019-08-22 stsp const struct got_error *error = NULL;
6056 8e7bd50a 2019-08-22 stsp struct got_repository *repo = NULL;
6057 8e7bd50a 2019-08-22 stsp struct got_worktree *worktree = NULL;
6058 8e7bd50a 2019-08-22 stsp char *cwd = NULL, *repo_path = NULL, *commit_id_str = NULL;
6059 c9956ddf 2019-09-08 stsp char *gitconfig_path = NULL;
6060 8e7bd50a 2019-08-22 stsp const char *tag_name, *commit_id_arg = NULL, *tagmsg = NULL;
6061 8e7bd50a 2019-08-22 stsp int ch, do_list = 0;
6063 80106605 2020-02-24 stsp while ((ch = getopt(argc, argv, "c:m:r:l")) != -1) {
6064 8e7bd50a 2019-08-22 stsp switch (ch) {
6066 80106605 2020-02-24 stsp commit_id_arg = optarg;
6069 8e7bd50a 2019-08-22 stsp tagmsg = optarg;
6072 8e7bd50a 2019-08-22 stsp repo_path = realpath(optarg, NULL);
6073 8e7bd50a 2019-08-22 stsp if (repo_path == NULL)
6074 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
6076 8e7bd50a 2019-08-22 stsp got_path_strip_trailing_slashes(repo_path);
6079 8e7bd50a 2019-08-22 stsp do_list = 1;
6082 8e7bd50a 2019-08-22 stsp usage_tag();
6083 8e7bd50a 2019-08-22 stsp /* NOTREACHED */
6087 8e7bd50a 2019-08-22 stsp argc -= optind;
6088 8e7bd50a 2019-08-22 stsp argv += optind;
6090 8e7bd50a 2019-08-22 stsp if (do_list) {
6091 80106605 2020-02-24 stsp if (commit_id_arg != NULL)
6093 775ce909 2020-03-22 stsp "-c option can only be used when creating a tag");
6094 8e7bd50a 2019-08-22 stsp if (tagmsg)
6095 80106605 2020-02-24 stsp errx(1, "-l and -m options are mutually exclusive");
6096 8e7bd50a 2019-08-22 stsp if (argc > 0)
6097 8e7bd50a 2019-08-22 stsp usage_tag();
6098 80106605 2020-02-24 stsp } else if (argc != 1)
6099 8e7bd50a 2019-08-22 stsp usage_tag();
6101 a2887370 2019-08-23 stsp tag_name = argv[0];
6103 8e7bd50a 2019-08-22 stsp #ifndef PROFILE
6104 8e7bd50a 2019-08-22 stsp if (do_list) {
6105 8e7bd50a 2019-08-22 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
6106 8e7bd50a 2019-08-22 stsp NULL) == -1)
6107 8e7bd50a 2019-08-22 stsp err(1, "pledge");
6109 8e7bd50a 2019-08-22 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
6110 8e7bd50a 2019-08-22 stsp "sendfd unveil", NULL) == -1)
6111 8e7bd50a 2019-08-22 stsp err(1, "pledge");
6114 8e7bd50a 2019-08-22 stsp cwd = getcwd(NULL, 0);
6115 8e7bd50a 2019-08-22 stsp if (cwd == NULL) {
6116 8e7bd50a 2019-08-22 stsp error = got_error_from_errno("getcwd");
6117 8e7bd50a 2019-08-22 stsp goto done;
6120 8e7bd50a 2019-08-22 stsp if (repo_path == NULL) {
6121 8e7bd50a 2019-08-22 stsp error = got_worktree_open(&worktree, cwd);
6122 8e7bd50a 2019-08-22 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
6123 8e7bd50a 2019-08-22 stsp goto done;
6125 8e7bd50a 2019-08-22 stsp error = NULL;
6126 8e7bd50a 2019-08-22 stsp if (worktree) {
6127 8e7bd50a 2019-08-22 stsp repo_path =
6128 8e7bd50a 2019-08-22 stsp strdup(got_worktree_get_repo_path(worktree));
6129 8e7bd50a 2019-08-22 stsp if (repo_path == NULL)
6130 8e7bd50a 2019-08-22 stsp error = got_error_from_errno("strdup");
6131 8e7bd50a 2019-08-22 stsp if (error)
6132 8e7bd50a 2019-08-22 stsp goto done;
6134 8e7bd50a 2019-08-22 stsp repo_path = strdup(cwd);
6135 8e7bd50a 2019-08-22 stsp if (repo_path == NULL) {
6136 8e7bd50a 2019-08-22 stsp error = got_error_from_errno("strdup");
6137 8e7bd50a 2019-08-22 stsp goto done;
6142 8e7bd50a 2019-08-22 stsp if (do_list) {
6143 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
6144 c9956ddf 2019-09-08 stsp if (error != NULL)
6145 c9956ddf 2019-09-08 stsp goto done;
6146 8e7bd50a 2019-08-22 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
6147 8e7bd50a 2019-08-22 stsp if (error)
6148 8e7bd50a 2019-08-22 stsp goto done;
6149 8e7bd50a 2019-08-22 stsp error = list_tags(repo, worktree);
6151 c9956ddf 2019-09-08 stsp error = get_gitconfig_path(&gitconfig_path);
6152 c9956ddf 2019-09-08 stsp if (error)
6153 c9956ddf 2019-09-08 stsp goto done;
6154 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, gitconfig_path);
6155 c9956ddf 2019-09-08 stsp if (error != NULL)
6156 c9956ddf 2019-09-08 stsp goto done;
6158 8e7bd50a 2019-08-22 stsp if (tagmsg) {
6159 42a285e2 2020-10-01 stsp error = apply_unveil(got_repo_get_path(repo), 0, NULL);
6160 8e7bd50a 2019-08-22 stsp if (error)
6161 8e7bd50a 2019-08-22 stsp goto done;
6164 8e7bd50a 2019-08-22 stsp if (commit_id_arg == NULL) {
6165 8e7bd50a 2019-08-22 stsp struct got_reference *head_ref;
6166 8e7bd50a 2019-08-22 stsp struct got_object_id *commit_id;
6167 8e7bd50a 2019-08-22 stsp error = got_ref_open(&head_ref, repo,
6168 8e7bd50a 2019-08-22 stsp worktree ? got_worktree_get_head_ref_name(worktree)
6169 8e7bd50a 2019-08-22 stsp : GOT_REF_HEAD, 0);
6170 8e7bd50a 2019-08-22 stsp if (error)
6171 8e7bd50a 2019-08-22 stsp goto done;
6172 8e7bd50a 2019-08-22 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
6173 8e7bd50a 2019-08-22 stsp got_ref_close(head_ref);
6174 8e7bd50a 2019-08-22 stsp if (error)
6175 8e7bd50a 2019-08-22 stsp goto done;
6176 8e7bd50a 2019-08-22 stsp error = got_object_id_str(&commit_id_str, commit_id);
6177 8e7bd50a 2019-08-22 stsp free(commit_id);
6178 8e7bd50a 2019-08-22 stsp if (error)
6179 8e7bd50a 2019-08-22 stsp goto done;
6182 50b0790e 2020-09-11 stsp error = add_tag(repo, worktree, tag_name,
6183 8e7bd50a 2019-08-22 stsp commit_id_str ? commit_id_str : commit_id_arg, tagmsg);
6187 8e7bd50a 2019-08-22 stsp got_repo_close(repo);
6188 8e7bd50a 2019-08-22 stsp if (worktree)
6189 8e7bd50a 2019-08-22 stsp got_worktree_close(worktree);
6190 8e7bd50a 2019-08-22 stsp free(cwd);
6191 8e7bd50a 2019-08-22 stsp free(repo_path);
6192 c9956ddf 2019-09-08 stsp free(gitconfig_path);
6193 8e7bd50a 2019-08-22 stsp free(commit_id_str);
6194 8e7bd50a 2019-08-22 stsp return error;
6197 8e7bd50a 2019-08-22 stsp __dead static void
6198 d00136be 2019-03-26 stsp usage_add(void)
6200 c29c428a 2019-12-16 stsp fprintf(stderr, "usage: %s add [-R] [-I] path ...\n",
6201 022fae89 2019-12-06 tracey getprogname());
6205 d00136be 2019-03-26 stsp static const struct got_error *
6206 4e68cba3 2019-11-23 stsp add_progress(void *arg, unsigned char status, const char *path)
6208 4e68cba3 2019-11-23 stsp while (path[0] == '/')
6210 4e68cba3 2019-11-23 stsp printf("%c %s\n", status, path);
6211 4e68cba3 2019-11-23 stsp return NULL;
6214 4e68cba3 2019-11-23 stsp static const struct got_error *
6215 d00136be 2019-03-26 stsp cmd_add(int argc, char *argv[])
6217 d00136be 2019-03-26 stsp const struct got_error *error = NULL;
6218 031a5338 2019-03-26 stsp struct got_repository *repo = NULL;
6219 d00136be 2019-03-26 stsp struct got_worktree *worktree = NULL;
6220 1dd54920 2019-05-11 stsp char *cwd = NULL;
6221 1dd54920 2019-05-11 stsp struct got_pathlist_head paths;
6222 1dd54920 2019-05-11 stsp struct got_pathlist_entry *pe;
6223 022fae89 2019-12-06 tracey int ch, can_recurse = 0, no_ignores = 0;
6225 1dd54920 2019-05-11 stsp TAILQ_INIT(&paths);
6227 022fae89 2019-12-06 tracey while ((ch = getopt(argc, argv, "IR")) != -1) {
6228 d00136be 2019-03-26 stsp switch (ch) {
6229 022fae89 2019-12-06 tracey case 'I':
6230 022fae89 2019-12-06 tracey no_ignores = 1;
6233 4e68cba3 2019-11-23 stsp can_recurse = 1;
6236 d00136be 2019-03-26 stsp usage_add();
6237 d00136be 2019-03-26 stsp /* NOTREACHED */
6241 d00136be 2019-03-26 stsp argc -= optind;
6242 d00136be 2019-03-26 stsp argv += optind;
6244 43012d58 2019-07-14 stsp #ifndef PROFILE
6245 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
6246 43012d58 2019-07-14 stsp NULL) == -1)
6247 43012d58 2019-07-14 stsp err(1, "pledge");
6249 723c305c 2019-05-11 jcs if (argc < 1)
6250 d00136be 2019-03-26 stsp usage_add();
6252 d00136be 2019-03-26 stsp cwd = getcwd(NULL, 0);
6253 d00136be 2019-03-26 stsp if (cwd == NULL) {
6254 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
6255 d00136be 2019-03-26 stsp goto done;
6258 d00136be 2019-03-26 stsp error = got_worktree_open(&worktree, cwd);
6259 fa51e947 2020-03-27 stsp if (error) {
6260 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
6261 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "add", cwd);
6262 d00136be 2019-03-26 stsp goto done;
6265 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6267 031a5338 2019-03-26 stsp if (error != NULL)
6268 031a5338 2019-03-26 stsp goto done;
6270 031a5338 2019-03-26 stsp error = apply_unveil(got_repo_get_path(repo), 1,
6271 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
6272 d00136be 2019-03-26 stsp if (error)
6273 d00136be 2019-03-26 stsp goto done;
6275 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6276 6d022e97 2019-08-04 stsp if (error)
6277 022fae89 2019-12-06 tracey goto done;
6279 022fae89 2019-12-06 tracey if (!can_recurse && no_ignores) {
6280 022fae89 2019-12-06 tracey error = got_error_msg(GOT_ERR_BAD_PATH,
6281 022fae89 2019-12-06 tracey "disregarding ignores requires -R option");
6282 6d022e97 2019-08-04 stsp goto done;
6286 4e68cba3 2019-11-23 stsp if (!can_recurse) {
6287 4e68cba3 2019-11-23 stsp char *ondisk_path;
6288 4e68cba3 2019-11-23 stsp struct stat sb;
6289 4e68cba3 2019-11-23 stsp TAILQ_FOREACH(pe, &paths, entry) {
6290 4e68cba3 2019-11-23 stsp if (asprintf(&ondisk_path, "%s/%s",
6291 4e68cba3 2019-11-23 stsp got_worktree_get_root_path(worktree),
6292 4e68cba3 2019-11-23 stsp pe->path) == -1) {
6293 4e68cba3 2019-11-23 stsp error = got_error_from_errno("asprintf");
6294 4e68cba3 2019-11-23 stsp goto done;
6296 4e68cba3 2019-11-23 stsp if (lstat(ondisk_path, &sb) == -1) {
6297 4e68cba3 2019-11-23 stsp if (errno == ENOENT) {
6298 4e68cba3 2019-11-23 stsp free(ondisk_path);
6301 4e68cba3 2019-11-23 stsp error = got_error_from_errno2("lstat",
6302 4e68cba3 2019-11-23 stsp ondisk_path);
6303 4e68cba3 2019-11-23 stsp free(ondisk_path);
6304 4e68cba3 2019-11-23 stsp goto done;
6306 4e68cba3 2019-11-23 stsp free(ondisk_path);
6307 4e68cba3 2019-11-23 stsp if (S_ISDIR(sb.st_mode)) {
6308 4e68cba3 2019-11-23 stsp error = got_error_msg(GOT_ERR_BAD_PATH,
6309 4e68cba3 2019-11-23 stsp "adding directories requires -R option");
6310 4e68cba3 2019-11-23 stsp goto done;
6315 4e68cba3 2019-11-23 stsp error = got_worktree_schedule_add(worktree, &paths, add_progress,
6316 022fae89 2019-12-06 tracey NULL, repo, no_ignores);
6319 031a5338 2019-03-26 stsp got_repo_close(repo);
6320 d00136be 2019-03-26 stsp if (worktree)
6321 d00136be 2019-03-26 stsp got_worktree_close(worktree);
6322 1dd54920 2019-05-11 stsp TAILQ_FOREACH(pe, &paths, entry)
6323 1dd54920 2019-05-11 stsp free((char *)pe->path);
6324 1dd54920 2019-05-11 stsp got_pathlist_free(&paths);
6325 2ec1f75b 2019-03-26 stsp free(cwd);
6326 2ec1f75b 2019-03-26 stsp return error;
6329 2ec1f75b 2019-03-26 stsp __dead static void
6330 648e4ef7 2019-07-09 stsp usage_remove(void)
6332 766841c2 2020-08-13 stsp fprintf(stderr, "usage: %s remove [-f] [-k] [-R] [-s status-codes] "
6333 766841c2 2020-08-13 stsp "path ...\n", getprogname());
6337 2a06fe5f 2019-08-24 stsp static const struct got_error *
6338 2a06fe5f 2019-08-24 stsp print_remove_status(void *arg, unsigned char status,
6339 f2a9dc41 2019-12-13 tracey unsigned char staged_status, const char *path)
6341 f2a9dc41 2019-12-13 tracey while (path[0] == '/')
6343 2a06fe5f 2019-08-24 stsp if (status == GOT_STATUS_NONEXISTENT)
6344 2a06fe5f 2019-08-24 stsp return NULL;
6345 2a06fe5f 2019-08-24 stsp if (status == staged_status && (status == GOT_STATUS_DELETE))
6346 2a06fe5f 2019-08-24 stsp status = GOT_STATUS_NO_CHANGE;
6347 2a06fe5f 2019-08-24 stsp printf("%c%c %s\n", status, staged_status, path);
6348 2a06fe5f 2019-08-24 stsp return NULL;
6351 2ec1f75b 2019-03-26 stsp static const struct got_error *
6352 648e4ef7 2019-07-09 stsp cmd_remove(int argc, char *argv[])
6354 2ec1f75b 2019-03-26 stsp const struct got_error *error = NULL;
6355 2ec1f75b 2019-03-26 stsp struct got_worktree *worktree = NULL;
6356 2ec1f75b 2019-03-26 stsp struct got_repository *repo = NULL;
6357 766841c2 2020-08-13 stsp const char *status_codes = NULL;
6358 17ed4618 2019-06-02 stsp char *cwd = NULL;
6359 17ed4618 2019-06-02 stsp struct got_pathlist_head paths;
6360 17ed4618 2019-06-02 stsp struct got_pathlist_entry *pe;
6361 766841c2 2020-08-13 stsp int ch, delete_local_mods = 0, can_recurse = 0, keep_on_disk = 0, i;
6363 17ed4618 2019-06-02 stsp TAILQ_INIT(&paths);
6365 766841c2 2020-08-13 stsp while ((ch = getopt(argc, argv, "fkRs:")) != -1) {
6366 2ec1f75b 2019-03-26 stsp switch (ch) {
6368 2ec1f75b 2019-03-26 stsp delete_local_mods = 1;
6370 70e3e7f5 2019-12-13 tracey case 'k':
6371 70e3e7f5 2019-12-13 tracey keep_on_disk = 1;
6373 f2a9dc41 2019-12-13 tracey case 'R':
6374 f2a9dc41 2019-12-13 tracey can_recurse = 1;
6377 766841c2 2020-08-13 stsp for (i = 0; i < strlen(optarg); i++) {
6378 766841c2 2020-08-13 stsp switch (optarg[i]) {
6379 766841c2 2020-08-13 stsp case GOT_STATUS_MODIFY:
6380 766841c2 2020-08-13 stsp delete_local_mods = 1;
6382 766841c2 2020-08-13 stsp case GOT_STATUS_MISSING:
6385 766841c2 2020-08-13 stsp errx(1, "invalid status code '%c'",
6386 766841c2 2020-08-13 stsp optarg[i]);
6389 766841c2 2020-08-13 stsp status_codes = optarg;
6392 f2a9dc41 2019-12-13 tracey usage_remove();
6393 2ec1f75b 2019-03-26 stsp /* NOTREACHED */
6397 2ec1f75b 2019-03-26 stsp argc -= optind;
6398 2ec1f75b 2019-03-26 stsp argv += optind;
6400 43012d58 2019-07-14 stsp #ifndef PROFILE
6401 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
6402 43012d58 2019-07-14 stsp NULL) == -1)
6403 43012d58 2019-07-14 stsp err(1, "pledge");
6405 17ed4618 2019-06-02 stsp if (argc < 1)
6406 648e4ef7 2019-07-09 stsp usage_remove();
6408 2ec1f75b 2019-03-26 stsp cwd = getcwd(NULL, 0);
6409 2ec1f75b 2019-03-26 stsp if (cwd == NULL) {
6410 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
6411 2ec1f75b 2019-03-26 stsp goto done;
6413 2ec1f75b 2019-03-26 stsp error = got_worktree_open(&worktree, cwd);
6414 fa51e947 2020-03-27 stsp if (error) {
6415 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
6416 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "remove", cwd);
6417 2ec1f75b 2019-03-26 stsp goto done;
6420 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6423 2ec1f75b 2019-03-26 stsp goto done;
6425 c2253644 2019-03-26 stsp error = apply_unveil(got_repo_get_path(repo), 1,
6426 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
6427 2ec1f75b 2019-03-26 stsp if (error)
6428 2ec1f75b 2019-03-26 stsp goto done;
6430 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6431 6d022e97 2019-08-04 stsp if (error)
6432 6d022e97 2019-08-04 stsp goto done;
6434 f2a9dc41 2019-12-13 tracey if (!can_recurse) {
6435 f2a9dc41 2019-12-13 tracey char *ondisk_path;
6436 f2a9dc41 2019-12-13 tracey struct stat sb;
6437 f2a9dc41 2019-12-13 tracey TAILQ_FOREACH(pe, &paths, entry) {
6438 f2a9dc41 2019-12-13 tracey if (asprintf(&ondisk_path, "%s/%s",
6439 f2a9dc41 2019-12-13 tracey got_worktree_get_root_path(worktree),
6440 f2a9dc41 2019-12-13 tracey pe->path) == -1) {
6441 f2a9dc41 2019-12-13 tracey error = got_error_from_errno("asprintf");
6442 f2a9dc41 2019-12-13 tracey goto done;
6444 f2a9dc41 2019-12-13 tracey if (lstat(ondisk_path, &sb) == -1) {
6445 f2a9dc41 2019-12-13 tracey if (errno == ENOENT) {
6446 f2a9dc41 2019-12-13 tracey free(ondisk_path);
6447 f2a9dc41 2019-12-13 tracey continue;
6449 f2a9dc41 2019-12-13 tracey error = got_error_from_errno2("lstat",
6450 f2a9dc41 2019-12-13 tracey ondisk_path);
6451 f2a9dc41 2019-12-13 tracey free(ondisk_path);
6452 f2a9dc41 2019-12-13 tracey goto done;
6454 f2a9dc41 2019-12-13 tracey free(ondisk_path);
6455 f2a9dc41 2019-12-13 tracey if (S_ISDIR(sb.st_mode)) {
6456 f2a9dc41 2019-12-13 tracey error = got_error_msg(GOT_ERR_BAD_PATH,
6457 f2a9dc41 2019-12-13 tracey "removing directories requires -R option");
6458 f2a9dc41 2019-12-13 tracey goto done;
6463 17ed4618 2019-06-02 stsp error = got_worktree_schedule_delete(worktree, &paths,
6464 766841c2 2020-08-13 stsp delete_local_mods, status_codes, print_remove_status, NULL,
6465 766841c2 2020-08-13 stsp repo, keep_on_disk);
6468 a129376b 2019-03-28 stsp got_repo_close(repo);
6469 a129376b 2019-03-28 stsp if (worktree)
6470 a129376b 2019-03-28 stsp got_worktree_close(worktree);
6471 17ed4618 2019-06-02 stsp TAILQ_FOREACH(pe, &paths, entry)
6472 17ed4618 2019-06-02 stsp free((char *)pe->path);
6473 17ed4618 2019-06-02 stsp got_pathlist_free(&paths);
6474 a129376b 2019-03-28 stsp free(cwd);
6475 a129376b 2019-03-28 stsp return error;
6478 a129376b 2019-03-28 stsp __dead static void
6479 a129376b 2019-03-28 stsp usage_revert(void)
6481 33aa809d 2019-08-08 stsp fprintf(stderr, "usage: %s revert [-p] [-F response-script] [-R] "
6482 33aa809d 2019-08-08 stsp "path ...\n", getprogname());
6486 1ee397ad 2019-07-12 stsp static const struct got_error *
6487 a129376b 2019-03-28 stsp revert_progress(void *arg, unsigned char status, const char *path)
6489 fb9704af 2020-01-27 stsp if (status == GOT_STATUS_UNVERSIONED)
6490 fb9704af 2020-01-27 stsp return NULL;
6492 a129376b 2019-03-28 stsp while (path[0] == '/')
6494 a129376b 2019-03-28 stsp printf("%c %s\n", status, path);
6495 33aa809d 2019-08-08 stsp return NULL;
6498 33aa809d 2019-08-08 stsp struct choose_patch_arg {
6499 33aa809d 2019-08-08 stsp FILE *patch_script_file;
6500 33aa809d 2019-08-08 stsp const char *action;
6503 33aa809d 2019-08-08 stsp static const struct got_error *
6504 33aa809d 2019-08-08 stsp show_change(unsigned char status, const char *path, FILE *patch_file, int n,
6505 33aa809d 2019-08-08 stsp int nchanges, const char *action)
6507 33aa809d 2019-08-08 stsp char *line = NULL;
6508 33aa809d 2019-08-08 stsp size_t linesize = 0;
6509 33aa809d 2019-08-08 stsp ssize_t linelen;
6511 33aa809d 2019-08-08 stsp switch (status) {
6512 33aa809d 2019-08-08 stsp case GOT_STATUS_ADD:
6513 33aa809d 2019-08-08 stsp printf("A %s\n%s this addition? [y/n] ", path, action);
6515 33aa809d 2019-08-08 stsp case GOT_STATUS_DELETE:
6516 33aa809d 2019-08-08 stsp printf("D %s\n%s this deletion? [y/n] ", path, action);
6518 33aa809d 2019-08-08 stsp case GOT_STATUS_MODIFY:
6519 33aa809d 2019-08-08 stsp if (fseek(patch_file, 0L, SEEK_SET) == -1)
6520 33aa809d 2019-08-08 stsp return got_error_from_errno("fseek");
6521 33aa809d 2019-08-08 stsp printf(GOT_COMMIT_SEP_STR);
6522 9516e7cb 2019-08-11 stsp while ((linelen = getline(&line, &linesize, patch_file)) != -1)
6523 33aa809d 2019-08-08 stsp printf("%s", line);
6524 33aa809d 2019-08-08 stsp if (ferror(patch_file))
6525 33aa809d 2019-08-08 stsp return got_error_from_errno("getline");
6526 33aa809d 2019-08-08 stsp printf(GOT_COMMIT_SEP_STR);
6527 33aa809d 2019-08-08 stsp printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
6528 33aa809d 2019-08-08 stsp path, n, nchanges, action);
6531 33aa809d 2019-08-08 stsp return got_error_path(path, GOT_ERR_FILE_STATUS);
6534 33aa809d 2019-08-08 stsp return NULL;
6537 33aa809d 2019-08-08 stsp static const struct got_error *
6538 33aa809d 2019-08-08 stsp choose_patch(int *choice, void *arg, unsigned char status, const char *path,
6539 33aa809d 2019-08-08 stsp FILE *patch_file, int n, int nchanges)
6541 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
6542 33aa809d 2019-08-08 stsp char *line = NULL;
6543 33aa809d 2019-08-08 stsp size_t linesize = 0;
6544 33aa809d 2019-08-08 stsp ssize_t linelen;
6545 33aa809d 2019-08-08 stsp int resp = ' ';
6546 33aa809d 2019-08-08 stsp struct choose_patch_arg *a = arg;
6548 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NONE;
6550 33aa809d 2019-08-08 stsp if (a->patch_script_file) {
6552 33aa809d 2019-08-08 stsp err = show_change(status, path, patch_file, n, nchanges,
6553 33aa809d 2019-08-08 stsp a->action);
6555 33aa809d 2019-08-08 stsp return err;
6556 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, a->patch_script_file);
6557 33aa809d 2019-08-08 stsp if (linelen == -1) {
6558 33aa809d 2019-08-08 stsp if (ferror(a->patch_script_file))
6559 33aa809d 2019-08-08 stsp return got_error_from_errno("getline");
6560 33aa809d 2019-08-08 stsp return NULL;
6562 33aa809d 2019-08-08 stsp nl = strchr(line, '\n');
6564 33aa809d 2019-08-08 stsp *nl = '\0';
6565 33aa809d 2019-08-08 stsp if (strcmp(line, "y") == 0) {
6566 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_YES;
6567 33aa809d 2019-08-08 stsp printf("y\n");
6568 33aa809d 2019-08-08 stsp } else if (strcmp(line, "n") == 0) {
6569 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NO;
6570 33aa809d 2019-08-08 stsp printf("n\n");
6571 33aa809d 2019-08-08 stsp } else if (strcmp(line, "q") == 0 &&
6572 33aa809d 2019-08-08 stsp status == GOT_STATUS_MODIFY) {
6573 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_QUIT;
6574 33aa809d 2019-08-08 stsp printf("q\n");
6576 33aa809d 2019-08-08 stsp printf("invalid response '%s'\n", line);
6577 33aa809d 2019-08-08 stsp free(line);
6578 33aa809d 2019-08-08 stsp return NULL;
6581 33aa809d 2019-08-08 stsp while (resp != 'y' && resp != 'n' && resp != 'q') {
6582 33aa809d 2019-08-08 stsp err = show_change(status, path, patch_file, n, nchanges,
6583 33aa809d 2019-08-08 stsp a->action);
6585 33aa809d 2019-08-08 stsp return err;
6586 33aa809d 2019-08-08 stsp resp = getchar();
6587 33aa809d 2019-08-08 stsp if (resp == '\n')
6588 33aa809d 2019-08-08 stsp resp = getchar();
6589 33aa809d 2019-08-08 stsp if (status == GOT_STATUS_MODIFY) {
6590 33aa809d 2019-08-08 stsp if (resp != 'y' && resp != 'n' && resp != 'q') {
6591 33aa809d 2019-08-08 stsp printf("invalid response '%c'\n", resp);
6592 33aa809d 2019-08-08 stsp resp = ' ';
6594 33aa809d 2019-08-08 stsp } else if (resp != 'y' && resp != 'n') {
6595 33aa809d 2019-08-08 stsp printf("invalid response '%c'\n", resp);
6596 33aa809d 2019-08-08 stsp resp = ' ';
6600 33aa809d 2019-08-08 stsp if (resp == 'y')
6601 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_YES;
6602 33aa809d 2019-08-08 stsp else if (resp == 'n')
6603 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NO;
6604 33aa809d 2019-08-08 stsp else if (resp == 'q' && status == GOT_STATUS_MODIFY)
6605 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_QUIT;
6607 1ee397ad 2019-07-12 stsp return NULL;
6611 a129376b 2019-03-28 stsp static const struct got_error *
6612 a129376b 2019-03-28 stsp cmd_revert(int argc, char *argv[])
6614 a129376b 2019-03-28 stsp const struct got_error *error = NULL;
6615 a129376b 2019-03-28 stsp struct got_worktree *worktree = NULL;
6616 a129376b 2019-03-28 stsp struct got_repository *repo = NULL;
6617 a129376b 2019-03-28 stsp char *cwd = NULL, *path = NULL;
6618 e20a8b6f 2019-06-04 stsp struct got_pathlist_head paths;
6619 0f6d7415 2019-08-08 stsp struct got_pathlist_entry *pe;
6620 33aa809d 2019-08-08 stsp int ch, can_recurse = 0, pflag = 0;
6621 33aa809d 2019-08-08 stsp FILE *patch_script_file = NULL;
6622 33aa809d 2019-08-08 stsp const char *patch_script_path = NULL;
6623 33aa809d 2019-08-08 stsp struct choose_patch_arg cpa;
6625 e20a8b6f 2019-06-04 stsp TAILQ_INIT(&paths);
6627 33aa809d 2019-08-08 stsp while ((ch = getopt(argc, argv, "pF:R")) != -1) {
6628 a129376b 2019-03-28 stsp switch (ch) {
6630 33aa809d 2019-08-08 stsp pflag = 1;
6633 33aa809d 2019-08-08 stsp patch_script_path = optarg;
6636 0f6d7415 2019-08-08 stsp can_recurse = 1;
6639 a129376b 2019-03-28 stsp usage_revert();
6640 a129376b 2019-03-28 stsp /* NOTREACHED */
6644 a129376b 2019-03-28 stsp argc -= optind;
6645 a129376b 2019-03-28 stsp argv += optind;
6647 43012d58 2019-07-14 stsp #ifndef PROFILE
6648 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6649 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
6650 43012d58 2019-07-14 stsp err(1, "pledge");
6652 e20a8b6f 2019-06-04 stsp if (argc < 1)
6653 a129376b 2019-03-28 stsp usage_revert();
6654 33aa809d 2019-08-08 stsp if (patch_script_path && !pflag)
6655 33aa809d 2019-08-08 stsp errx(1, "-F option can only be used together with -p option");
6657 a129376b 2019-03-28 stsp cwd = getcwd(NULL, 0);
6658 a129376b 2019-03-28 stsp if (cwd == NULL) {
6659 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
6660 a129376b 2019-03-28 stsp goto done;
6662 a129376b 2019-03-28 stsp error = got_worktree_open(&worktree, cwd);
6663 fa51e947 2020-03-27 stsp if (error) {
6664 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
6665 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "revert", cwd);
6666 2ec1f75b 2019-03-26 stsp goto done;
6669 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6671 a129376b 2019-03-28 stsp if (error != NULL)
6672 a129376b 2019-03-28 stsp goto done;
6674 33aa809d 2019-08-08 stsp if (patch_script_path) {
6675 33aa809d 2019-08-08 stsp patch_script_file = fopen(patch_script_path, "r");
6676 33aa809d 2019-08-08 stsp if (patch_script_file == NULL) {
6677 33aa809d 2019-08-08 stsp error = got_error_from_errno2("fopen",
6678 33aa809d 2019-08-08 stsp patch_script_path);
6679 33aa809d 2019-08-08 stsp goto done;
6682 a129376b 2019-03-28 stsp error = apply_unveil(got_repo_get_path(repo), 1,
6683 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
6684 a129376b 2019-03-28 stsp if (error)
6685 a129376b 2019-03-28 stsp goto done;
6687 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6688 6d022e97 2019-08-04 stsp if (error)
6689 6d022e97 2019-08-04 stsp goto done;
6691 0f6d7415 2019-08-08 stsp if (!can_recurse) {
6692 0f6d7415 2019-08-08 stsp char *ondisk_path;
6693 0f6d7415 2019-08-08 stsp struct stat sb;
6694 0f6d7415 2019-08-08 stsp TAILQ_FOREACH(pe, &paths, entry) {
6695 0f6d7415 2019-08-08 stsp if (asprintf(&ondisk_path, "%s/%s",
6696 0f6d7415 2019-08-08 stsp got_worktree_get_root_path(worktree),
6697 0f6d7415 2019-08-08 stsp pe->path) == -1) {
6698 0f6d7415 2019-08-08 stsp error = got_error_from_errno("asprintf");
6699 0f6d7415 2019-08-08 stsp goto done;
6701 0f6d7415 2019-08-08 stsp if (lstat(ondisk_path, &sb) == -1) {
6702 0f6d7415 2019-08-08 stsp if (errno == ENOENT) {
6703 0f6d7415 2019-08-08 stsp free(ondisk_path);
6706 0f6d7415 2019-08-08 stsp error = got_error_from_errno2("lstat",
6707 0f6d7415 2019-08-08 stsp ondisk_path);
6708 0f6d7415 2019-08-08 stsp free(ondisk_path);
6709 0f6d7415 2019-08-08 stsp goto done;
6711 0f6d7415 2019-08-08 stsp free(ondisk_path);
6712 0f6d7415 2019-08-08 stsp if (S_ISDIR(sb.st_mode)) {
6713 0f6d7415 2019-08-08 stsp error = got_error_msg(GOT_ERR_BAD_PATH,
6714 0f6d7415 2019-08-08 stsp "reverting directories requires -R option");
6715 0f6d7415 2019-08-08 stsp goto done;
6720 33aa809d 2019-08-08 stsp cpa.patch_script_file = patch_script_file;
6721 33aa809d 2019-08-08 stsp cpa.action = "revert";
6722 33aa809d 2019-08-08 stsp error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
6723 33aa809d 2019-08-08 stsp pflag ? choose_patch : NULL, &cpa, repo);
6725 33aa809d 2019-08-08 stsp if (patch_script_file && fclose(patch_script_file) == EOF &&
6726 33aa809d 2019-08-08 stsp error == NULL)
6727 33aa809d 2019-08-08 stsp error = got_error_from_errno2("fclose", patch_script_path);
6729 2ec1f75b 2019-03-26 stsp got_repo_close(repo);
6730 2ec1f75b 2019-03-26 stsp if (worktree)
6731 2ec1f75b 2019-03-26 stsp got_worktree_close(worktree);
6732 2ec1f75b 2019-03-26 stsp free(path);
6733 d00136be 2019-03-26 stsp free(cwd);
6734 6bad629b 2019-02-04 stsp return error;
6737 c4296144 2019-05-09 stsp __dead static void
6738 c4296144 2019-05-09 stsp usage_commit(void)
6740 35213c7c 2020-07-23 stsp fprintf(stderr, "usage: %s commit [-m msg] [-S] [path ...]\n",
6741 5c1e53bc 2019-07-28 stsp getprogname());
6745 e2ba3d07 2019-05-13 stsp struct collect_commit_logmsg_arg {
6746 e2ba3d07 2019-05-13 stsp const char *cmdline_log;
6747 e2ba3d07 2019-05-13 stsp const char *editor;
6748 e0870e44 2019-05-13 stsp const char *worktree_path;
6749 76d98825 2019-06-03 stsp const char *branch_name;
6750 314a6357 2019-05-13 stsp const char *repo_path;
6751 e0870e44 2019-05-13 stsp char *logmsg_path;
6755 33ad4cbe 2019-05-12 jcs static const struct got_error *
6756 33ad4cbe 2019-05-12 jcs collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
6759 76d98825 2019-06-03 stsp char *initial_content = NULL;
6760 33ad4cbe 2019-05-12 jcs struct got_pathlist_entry *pe;
6761 33ad4cbe 2019-05-12 jcs const struct got_error *err = NULL;
6762 e0870e44 2019-05-13 stsp char *template = NULL;
6763 e2ba3d07 2019-05-13 stsp struct collect_commit_logmsg_arg *a = arg;
6764 1601cb9f 2020-09-11 naddy int initial_content_len;
6765 97972933 2020-09-11 stsp int fd = -1;
6766 33ad4cbe 2019-05-12 jcs size_t len;
6768 33ad4cbe 2019-05-12 jcs /* if a message was specified on the command line, just use it */
6769 e2ba3d07 2019-05-13 stsp if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
6770 e2ba3d07 2019-05-13 stsp len = strlen(a->cmdline_log) + 1;
6771 33ad4cbe 2019-05-12 jcs *logmsg = malloc(len + 1);
6772 9f42ff69 2019-05-13 stsp if (*logmsg == NULL)
6773 638f9024 2019-05-13 stsp return got_error_from_errno("malloc");
6774 e2ba3d07 2019-05-13 stsp strlcpy(*logmsg, a->cmdline_log, len);
6775 33ad4cbe 2019-05-12 jcs return NULL;
6778 e0870e44 2019-05-13 stsp if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
6779 76d98825 2019-06-03 stsp return got_error_from_errno("asprintf");
6781 1601cb9f 2020-09-11 naddy initial_content_len = asprintf(&initial_content,
6782 76d98825 2019-06-03 stsp "\n# changes to be committed on branch %s:\n",
6783 1601cb9f 2020-09-11 naddy a->branch_name);
6784 1601cb9f 2020-09-11 naddy if (initial_content_len == -1)
6785 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
6787 e0870e44 2019-05-13 stsp err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
6789 e0870e44 2019-05-13 stsp goto done;
6791 97972933 2020-09-11 stsp if (write(fd, initial_content, initial_content_len) == -1) {
6792 97972933 2020-09-11 stsp err = got_error_from_errno2("write", a->logmsg_path);
6793 97972933 2020-09-11 stsp goto done;
6796 33ad4cbe 2019-05-12 jcs TAILQ_FOREACH(pe, commitable_paths, entry) {
6797 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
6798 8656d6c4 2019-05-20 stsp dprintf(fd, "# %c %s\n",
6799 8656d6c4 2019-05-20 stsp got_commitable_get_status(ct),
6800 8656d6c4 2019-05-20 stsp got_commitable_get_path(ct));
6803 bfa12d5e 2020-09-26 stsp err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content,
6804 bfa12d5e 2020-09-26 stsp initial_content_len);
6806 76d98825 2019-06-03 stsp free(initial_content);
6807 e0870e44 2019-05-13 stsp free(template);
6809 97972933 2020-09-11 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
6810 97972933 2020-09-11 stsp err = got_error_from_errno2("close", a->logmsg_path);
6812 314a6357 2019-05-13 stsp /* Editor is done; we can now apply unveil(2) */
6813 59f86c76 2020-09-11 stsp if (err == NULL)
6814 c530dc23 2019-07-23 stsp err = apply_unveil(a->repo_path, 0, a->worktree_path);
6815 59f86c76 2020-09-11 stsp if (err) {
6816 59f86c76 2020-09-11 stsp free(*logmsg);
6817 59f86c76 2020-09-11 stsp *logmsg = NULL;
6819 33ad4cbe 2019-05-12 jcs return err;
6822 c4296144 2019-05-09 stsp static const struct got_error *
6823 c4296144 2019-05-09 stsp cmd_commit(int argc, char *argv[])
6825 c4296144 2019-05-09 stsp const struct got_error *error = NULL;
6826 c4296144 2019-05-09 stsp struct got_worktree *worktree = NULL;
6827 c4296144 2019-05-09 stsp struct got_repository *repo = NULL;
6828 5c1e53bc 2019-07-28 stsp char *cwd = NULL, *id_str = NULL;
6829 c4296144 2019-05-09 stsp struct got_object_id *id = NULL;
6830 33ad4cbe 2019-05-12 jcs const char *logmsg = NULL;
6831 aba9c984 2019-09-08 stsp struct collect_commit_logmsg_arg cl_arg;
6832 c9956ddf 2019-09-08 stsp char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
6833 7266f21f 2019-10-21 stsp int ch, rebase_in_progress, histedit_in_progress, preserve_logmsg = 0;
6834 35213c7c 2020-07-23 stsp int allow_bad_symlinks = 0;
6835 5c1e53bc 2019-07-28 stsp struct got_pathlist_head paths;
6837 5c1e53bc 2019-07-28 stsp TAILQ_INIT(&paths);
6838 6ac5a73c 2019-08-08 stsp cl_arg.logmsg_path = NULL;
6840 35213c7c 2020-07-23 stsp while ((ch = getopt(argc, argv, "m:S")) != -1) {
6841 c4296144 2019-05-09 stsp switch (ch) {
6843 c4296144 2019-05-09 stsp logmsg = optarg;
6846 35213c7c 2020-07-23 stsp allow_bad_symlinks = 1;
6849 c4296144 2019-05-09 stsp usage_commit();
6850 c4296144 2019-05-09 stsp /* NOTREACHED */
6854 c4296144 2019-05-09 stsp argc -= optind;
6855 c4296144 2019-05-09 stsp argv += optind;
6857 43012d58 2019-07-14 stsp #ifndef PROFILE
6858 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6859 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
6860 43012d58 2019-07-14 stsp err(1, "pledge");
6862 c4296144 2019-05-09 stsp cwd = getcwd(NULL, 0);
6863 c4296144 2019-05-09 stsp if (cwd == NULL) {
6864 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
6865 c4296144 2019-05-09 stsp goto done;
6867 c4296144 2019-05-09 stsp error = got_worktree_open(&worktree, cwd);
6868 fa51e947 2020-03-27 stsp if (error) {
6869 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
6870 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "commit", cwd);
6871 7d5807f4 2019-07-11 stsp goto done;
6874 a698f62e 2019-07-25 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
6875 c4296144 2019-05-09 stsp if (error)
6876 a698f62e 2019-07-25 stsp goto done;
6877 a698f62e 2019-07-25 stsp if (rebase_in_progress) {
6878 a698f62e 2019-07-25 stsp error = got_error(GOT_ERR_REBASING);
6879 c4296144 2019-05-09 stsp goto done;
6882 916f288c 2019-07-30 stsp error = got_worktree_histedit_in_progress(&histedit_in_progress,
6883 916f288c 2019-07-30 stsp worktree);
6884 5c1e53bc 2019-07-28 stsp if (error)
6885 5c1e53bc 2019-07-28 stsp goto done;
6887 c9956ddf 2019-09-08 stsp error = get_gitconfig_path(&gitconfig_path);
6888 c9956ddf 2019-09-08 stsp if (error)
6889 c9956ddf 2019-09-08 stsp goto done;
6890 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6891 c9956ddf 2019-09-08 stsp gitconfig_path);
6892 c4296144 2019-05-09 stsp if (error != NULL)
6893 c4296144 2019-05-09 stsp goto done;
6895 50b0790e 2020-09-11 stsp error = get_author(&author, repo, worktree);
6896 aba9c984 2019-09-08 stsp if (error)
6897 aba9c984 2019-09-08 stsp return error;
6900 314a6357 2019-05-13 stsp * unveil(2) traverses exec(2); if an editor is used we have
6901 314a6357 2019-05-13 stsp * to apply unveil after the log message has been written.
6903 314a6357 2019-05-13 stsp if (logmsg == NULL || strlen(logmsg) == 0)
6904 314a6357 2019-05-13 stsp error = get_editor(&editor);
6906 314a6357 2019-05-13 stsp error = apply_unveil(got_repo_get_path(repo), 0,
6907 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
6908 c4296144 2019-05-09 stsp if (error)
6909 c4296144 2019-05-09 stsp goto done;
6911 087fb88c 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6912 087fb88c 2019-08-04 stsp if (error)
6913 087fb88c 2019-08-04 stsp goto done;
6915 e2ba3d07 2019-05-13 stsp cl_arg.editor = editor;
6916 e2ba3d07 2019-05-13 stsp cl_arg.cmdline_log = logmsg;
6917 e0870e44 2019-05-13 stsp cl_arg.worktree_path = got_worktree_get_root_path(worktree);
6918 76d98825 2019-06-03 stsp cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
6919 916f288c 2019-07-30 stsp if (!histedit_in_progress) {
6920 916f288c 2019-07-30 stsp if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
6921 916f288c 2019-07-30 stsp error = got_error(GOT_ERR_COMMIT_BRANCH);
6922 916f288c 2019-07-30 stsp goto done;
6924 916f288c 2019-07-30 stsp cl_arg.branch_name += 11;
6926 314a6357 2019-05-13 stsp cl_arg.repo_path = got_repo_get_path(repo);
6927 84792843 2019-08-09 stsp error = got_worktree_commit(&id, worktree, &paths, author, NULL,
6928 35213c7c 2020-07-23 stsp allow_bad_symlinks, collect_commit_logmsg, &cl_arg,
6929 35213c7c 2020-07-23 stsp print_status, NULL, repo);
6930 e0870e44 2019-05-13 stsp if (error) {
6931 7266f21f 2019-10-21 stsp if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
6932 7266f21f 2019-10-21 stsp cl_arg.logmsg_path != NULL)
6933 7266f21f 2019-10-21 stsp preserve_logmsg = 1;
6934 c4296144 2019-05-09 stsp goto done;
6937 c4296144 2019-05-09 stsp error = got_object_id_str(&id_str, id);
6938 c4296144 2019-05-09 stsp if (error)
6939 c4296144 2019-05-09 stsp goto done;
6940 a7648d7a 2019-06-02 stsp printf("Created commit %s\n", id_str);
6942 7266f21f 2019-10-21 stsp if (preserve_logmsg) {
6943 7266f21f 2019-10-21 stsp fprintf(stderr, "%s: log message preserved in %s\n",
6944 7266f21f 2019-10-21 stsp getprogname(), cl_arg.logmsg_path);
6945 7266f21f 2019-10-21 stsp } else if (cl_arg.logmsg_path && unlink(cl_arg.logmsg_path) == -1 &&
6946 7266f21f 2019-10-21 stsp error == NULL)
6947 7266f21f 2019-10-21 stsp error = got_error_from_errno2("unlink", cl_arg.logmsg_path);
6948 6ac5a73c 2019-08-08 stsp free(cl_arg.logmsg_path);
6950 c4296144 2019-05-09 stsp got_repo_close(repo);
6951 c4296144 2019-05-09 stsp if (worktree)
6952 c4296144 2019-05-09 stsp got_worktree_close(worktree);
6953 c4296144 2019-05-09 stsp free(cwd);
6954 c4296144 2019-05-09 stsp free(id_str);
6955 c9956ddf 2019-09-08 stsp free(gitconfig_path);
6956 e2ba3d07 2019-05-13 stsp free(editor);
6957 aba9c984 2019-09-08 stsp free(author);
6958 234035bc 2019-06-01 stsp return error;
6961 234035bc 2019-06-01 stsp __dead static void
6962 234035bc 2019-06-01 stsp usage_cherrypick(void)
6964 234035bc 2019-06-01 stsp fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
6968 234035bc 2019-06-01 stsp static const struct got_error *
6969 234035bc 2019-06-01 stsp cmd_cherrypick(int argc, char *argv[])
6971 234035bc 2019-06-01 stsp const struct got_error *error = NULL;
6972 234035bc 2019-06-01 stsp struct got_worktree *worktree = NULL;
6973 234035bc 2019-06-01 stsp struct got_repository *repo = NULL;
6974 234035bc 2019-06-01 stsp char *cwd = NULL, *commit_id_str = NULL;
6975 234035bc 2019-06-01 stsp struct got_object_id *commit_id = NULL;
6976 234035bc 2019-06-01 stsp struct got_commit_object *commit = NULL;
6977 234035bc 2019-06-01 stsp struct got_object_qid *pid;
6978 234035bc 2019-06-01 stsp struct got_reference *head_ref = NULL;
6980 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
6982 234035bc 2019-06-01 stsp while ((ch = getopt(argc, argv, "")) != -1) {
6983 234035bc 2019-06-01 stsp switch (ch) {
6985 234035bc 2019-06-01 stsp usage_cherrypick();
6986 234035bc 2019-06-01 stsp /* NOTREACHED */
6990 234035bc 2019-06-01 stsp argc -= optind;
6991 234035bc 2019-06-01 stsp argv += optind;
6993 43012d58 2019-07-14 stsp #ifndef PROFILE
6994 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6995 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
6996 43012d58 2019-07-14 stsp err(1, "pledge");
6998 234035bc 2019-06-01 stsp if (argc != 1)
6999 234035bc 2019-06-01 stsp usage_cherrypick();
7001 234035bc 2019-06-01 stsp cwd = getcwd(NULL, 0);
7002 234035bc 2019-06-01 stsp if (cwd == NULL) {
7003 234035bc 2019-06-01 stsp error = got_error_from_errno("getcwd");
7004 234035bc 2019-06-01 stsp goto done;
7006 234035bc 2019-06-01 stsp error = got_worktree_open(&worktree, cwd);
7007 fa51e947 2020-03-27 stsp if (error) {
7008 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
7009 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "cherrypick",
7011 234035bc 2019-06-01 stsp goto done;
7014 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7016 234035bc 2019-06-01 stsp if (error != NULL)
7017 234035bc 2019-06-01 stsp goto done;
7019 234035bc 2019-06-01 stsp error = apply_unveil(got_repo_get_path(repo), 0,
7020 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
7021 234035bc 2019-06-01 stsp if (error)
7022 234035bc 2019-06-01 stsp goto done;
7024 dd88155e 2019-06-29 stsp error = got_repo_match_object_id_prefix(&commit_id, argv[0],
7025 dd88155e 2019-06-29 stsp GOT_OBJ_TYPE_COMMIT, repo);
7026 234035bc 2019-06-01 stsp if (error != NULL) {
7027 234035bc 2019-06-01 stsp struct got_reference *ref;
7028 234035bc 2019-06-01 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
7029 234035bc 2019-06-01 stsp goto done;
7030 234035bc 2019-06-01 stsp error = got_ref_open(&ref, repo, argv[0], 0);
7031 234035bc 2019-06-01 stsp if (error != NULL)
7032 234035bc 2019-06-01 stsp goto done;
7033 234035bc 2019-06-01 stsp error = got_ref_resolve(&commit_id, repo, ref);
7034 234035bc 2019-06-01 stsp got_ref_close(ref);
7035 234035bc 2019-06-01 stsp if (error != NULL)
7036 234035bc 2019-06-01 stsp goto done;
7038 234035bc 2019-06-01 stsp error = got_object_id_str(&commit_id_str, commit_id);
7039 234035bc 2019-06-01 stsp if (error)
7040 234035bc 2019-06-01 stsp goto done;
7042 234035bc 2019-06-01 stsp error = got_ref_open(&head_ref, repo,
7043 234035bc 2019-06-01 stsp got_worktree_get_head_ref_name(worktree), 0);
7044 234035bc 2019-06-01 stsp if (error != NULL)
7045 234035bc 2019-06-01 stsp goto done;
7047 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
7048 234035bc 2019-06-01 stsp if (error) {
7049 234035bc 2019-06-01 stsp if (error->code != GOT_ERR_ANCESTRY)
7050 234035bc 2019-06-01 stsp goto done;
7051 234035bc 2019-06-01 stsp error = NULL;
7053 234035bc 2019-06-01 stsp error = got_error(GOT_ERR_SAME_BRANCH);
7054 234035bc 2019-06-01 stsp goto done;
7057 234035bc 2019-06-01 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
7058 234035bc 2019-06-01 stsp if (error)
7059 234035bc 2019-06-01 stsp goto done;
7060 234035bc 2019-06-01 stsp pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
7061 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
7062 03415a1a 2019-06-02 stsp error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
7063 9627c110 2020-04-18 stsp commit_id, repo, update_progress, &upa, check_cancelled,
7065 234035bc 2019-06-01 stsp if (error != NULL)
7066 234035bc 2019-06-01 stsp goto done;
7068 9627c110 2020-04-18 stsp if (upa.did_something)
7069 a7648d7a 2019-06-02 stsp printf("Merged commit %s\n", commit_id_str);
7070 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
7072 234035bc 2019-06-01 stsp if (commit)
7073 234035bc 2019-06-01 stsp got_object_commit_close(commit);
7074 234035bc 2019-06-01 stsp free(commit_id_str);
7075 234035bc 2019-06-01 stsp if (head_ref)
7076 234035bc 2019-06-01 stsp got_ref_close(head_ref);
7077 234035bc 2019-06-01 stsp if (worktree)
7078 234035bc 2019-06-01 stsp got_worktree_close(worktree);
7080 234035bc 2019-06-01 stsp got_repo_close(repo);
7081 c4296144 2019-05-09 stsp return error;
7084 5ef14e63 2019-06-02 stsp __dead static void
7085 5ef14e63 2019-06-02 stsp usage_backout(void)
7087 5ef14e63 2019-06-02 stsp fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
7091 5ef14e63 2019-06-02 stsp static const struct got_error *
7092 5ef14e63 2019-06-02 stsp cmd_backout(int argc, char *argv[])
7094 5ef14e63 2019-06-02 stsp const struct got_error *error = NULL;
7095 5ef14e63 2019-06-02 stsp struct got_worktree *worktree = NULL;
7096 5ef14e63 2019-06-02 stsp struct got_repository *repo = NULL;
7097 5ef14e63 2019-06-02 stsp char *cwd = NULL, *commit_id_str = NULL;
7098 5ef14e63 2019-06-02 stsp struct got_object_id *commit_id = NULL;
7099 5ef14e63 2019-06-02 stsp struct got_commit_object *commit = NULL;
7100 5ef14e63 2019-06-02 stsp struct got_object_qid *pid;
7101 5ef14e63 2019-06-02 stsp struct got_reference *head_ref = NULL;
7103 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
7105 5ef14e63 2019-06-02 stsp while ((ch = getopt(argc, argv, "")) != -1) {
7106 5ef14e63 2019-06-02 stsp switch (ch) {
7108 5ef14e63 2019-06-02 stsp usage_backout();
7109 5ef14e63 2019-06-02 stsp /* NOTREACHED */
7113 5ef14e63 2019-06-02 stsp argc -= optind;
7114 5ef14e63 2019-06-02 stsp argv += optind;
7116 43012d58 2019-07-14 stsp #ifndef PROFILE
7117 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7118 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
7119 43012d58 2019-07-14 stsp err(1, "pledge");
7121 5ef14e63 2019-06-02 stsp if (argc != 1)
7122 5ef14e63 2019-06-02 stsp usage_backout();
7124 5ef14e63 2019-06-02 stsp cwd = getcwd(NULL, 0);
7125 5ef14e63 2019-06-02 stsp if (cwd == NULL) {
7126 5ef14e63 2019-06-02 stsp error = got_error_from_errno("getcwd");
7127 5ef14e63 2019-06-02 stsp goto done;
7129 5ef14e63 2019-06-02 stsp error = got_worktree_open(&worktree, cwd);
7130 fa51e947 2020-03-27 stsp if (error) {
7131 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
7132 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "backout", cwd);
7133 5ef14e63 2019-06-02 stsp goto done;
7136 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7138 5ef14e63 2019-06-02 stsp if (error != NULL)
7139 5ef14e63 2019-06-02 stsp goto done;
7141 5ef14e63 2019-06-02 stsp error = apply_unveil(got_repo_get_path(repo), 0,
7142 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
7143 5ef14e63 2019-06-02 stsp if (error)
7144 5ef14e63 2019-06-02 stsp goto done;
7146 dd88155e 2019-06-29 stsp error = got_repo_match_object_id_prefix(&commit_id, argv[0],
7147 dd88155e 2019-06-29 stsp GOT_OBJ_TYPE_COMMIT, repo);
7148 5ef14e63 2019-06-02 stsp if (error != NULL) {
7149 5ef14e63 2019-06-02 stsp struct got_reference *ref;
7150 5ef14e63 2019-06-02 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
7151 5ef14e63 2019-06-02 stsp goto done;
7152 5ef14e63 2019-06-02 stsp error = got_ref_open(&ref, repo, argv[0], 0);
7153 5ef14e63 2019-06-02 stsp if (error != NULL)
7154 5ef14e63 2019-06-02 stsp goto done;
7155 5ef14e63 2019-06-02 stsp error = got_ref_resolve(&commit_id, repo, ref);
7156 5ef14e63 2019-06-02 stsp got_ref_close(ref);
7157 5ef14e63 2019-06-02 stsp if (error != NULL)
7158 5ef14e63 2019-06-02 stsp goto done;
7160 5ef14e63 2019-06-02 stsp error = got_object_id_str(&commit_id_str, commit_id);
7161 5ef14e63 2019-06-02 stsp if (error)
7162 5ef14e63 2019-06-02 stsp goto done;
7164 5ef14e63 2019-06-02 stsp error = got_ref_open(&head_ref, repo,
7165 5ef14e63 2019-06-02 stsp got_worktree_get_head_ref_name(worktree), 0);
7166 5ef14e63 2019-06-02 stsp if (error != NULL)
7167 5ef14e63 2019-06-02 stsp goto done;
7169 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
7170 5ef14e63 2019-06-02 stsp if (error)
7171 5ef14e63 2019-06-02 stsp goto done;
7173 5ef14e63 2019-06-02 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
7174 5ef14e63 2019-06-02 stsp if (error)
7175 5ef14e63 2019-06-02 stsp goto done;
7176 5ef14e63 2019-06-02 stsp pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
7177 5ef14e63 2019-06-02 stsp if (pid == NULL) {
7178 5ef14e63 2019-06-02 stsp error = got_error(GOT_ERR_ROOT_COMMIT);
7179 5ef14e63 2019-06-02 stsp goto done;
7182 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
7183 5ef14e63 2019-06-02 stsp error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
7184 9627c110 2020-04-18 stsp update_progress, &upa, check_cancelled, NULL);
7185 5ef14e63 2019-06-02 stsp if (error != NULL)
7186 5ef14e63 2019-06-02 stsp goto done;
7188 9627c110 2020-04-18 stsp if (upa.did_something)
7189 a7648d7a 2019-06-02 stsp printf("Backed out commit %s\n", commit_id_str);
7190 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
7192 5ef14e63 2019-06-02 stsp if (commit)
7193 5ef14e63 2019-06-02 stsp got_object_commit_close(commit);
7194 5ef14e63 2019-06-02 stsp free(commit_id_str);
7195 5ef14e63 2019-06-02 stsp if (head_ref)
7196 5ef14e63 2019-06-02 stsp got_ref_close(head_ref);
7197 818c7501 2019-07-11 stsp if (worktree)
7198 818c7501 2019-07-11 stsp got_worktree_close(worktree);
7200 818c7501 2019-07-11 stsp got_repo_close(repo);
7201 818c7501 2019-07-11 stsp return error;
7204 818c7501 2019-07-11 stsp __dead static void
7205 818c7501 2019-07-11 stsp usage_rebase(void)
7207 af54c8f8 2019-07-11 stsp fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
7208 af54c8f8 2019-07-11 stsp getprogname());
7213 0ebf8283 2019-07-24 stsp trim_logmsg(char *logmsg, int limit)
7216 0ebf8283 2019-07-24 stsp size_t len;
7218 0ebf8283 2019-07-24 stsp len = strlen(logmsg);
7219 0ebf8283 2019-07-24 stsp if (len > limit)
7220 0ebf8283 2019-07-24 stsp len = limit;
7221 0ebf8283 2019-07-24 stsp logmsg[len] = '\0';
7222 0ebf8283 2019-07-24 stsp nl = strchr(logmsg, '\n');
7224 0ebf8283 2019-07-24 stsp *nl = '\0';
7227 0ebf8283 2019-07-24 stsp static const struct got_error *
7228 0ebf8283 2019-07-24 stsp get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
7230 5943eee2 2019-08-13 stsp const struct got_error *err;
7231 5943eee2 2019-08-13 stsp char *logmsg0 = NULL;
7232 5943eee2 2019-08-13 stsp const char *s;
7234 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
7236 5943eee2 2019-08-13 stsp return err;
7238 5943eee2 2019-08-13 stsp s = logmsg0;
7239 5943eee2 2019-08-13 stsp while (isspace((unsigned char)s[0]))
7242 5943eee2 2019-08-13 stsp *logmsg = strdup(s);
7243 5943eee2 2019-08-13 stsp if (*logmsg == NULL) {
7244 5943eee2 2019-08-13 stsp err = got_error_from_errno("strdup");
7245 5943eee2 2019-08-13 stsp goto done;
7248 0ebf8283 2019-07-24 stsp trim_logmsg(*logmsg, limit);
7250 5943eee2 2019-08-13 stsp free(logmsg0);
7251 a0ea4fc0 2020-02-28 stsp return err;
7254 a0ea4fc0 2020-02-28 stsp static const struct got_error *
7255 a0ea4fc0 2020-02-28 stsp show_rebase_merge_conflict(struct got_object_id *id, struct got_repository *repo)
7257 a0ea4fc0 2020-02-28 stsp const struct got_error *err;
7258 a0ea4fc0 2020-02-28 stsp struct got_commit_object *commit = NULL;
7259 a0ea4fc0 2020-02-28 stsp char *id_str = NULL, *logmsg = NULL;
7261 a0ea4fc0 2020-02-28 stsp err = got_object_open_as_commit(&commit, repo, id);
7263 a0ea4fc0 2020-02-28 stsp return err;
7265 a0ea4fc0 2020-02-28 stsp err = got_object_id_str(&id_str, id);
7267 a0ea4fc0 2020-02-28 stsp goto done;
7269 a0ea4fc0 2020-02-28 stsp id_str[12] = '\0';
7271 a0ea4fc0 2020-02-28 stsp err = get_short_logmsg(&logmsg, 42, commit);
7273 a0ea4fc0 2020-02-28 stsp goto done;
7275 a0ea4fc0 2020-02-28 stsp printf("%s -> merge conflict: %s\n", id_str, logmsg);
7277 a0ea4fc0 2020-02-28 stsp free(id_str);
7278 a0ea4fc0 2020-02-28 stsp got_object_commit_close(commit);
7279 a0ea4fc0 2020-02-28 stsp free(logmsg);
7280 5943eee2 2019-08-13 stsp return err;
7283 818c7501 2019-07-11 stsp static const struct got_error *
7284 818c7501 2019-07-11 stsp show_rebase_progress(struct got_commit_object *commit,
7285 818c7501 2019-07-11 stsp struct got_object_id *old_id, struct got_object_id *new_id)
7287 818c7501 2019-07-11 stsp const struct got_error *err;
7288 0ebf8283 2019-07-24 stsp char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
7290 818c7501 2019-07-11 stsp err = got_object_id_str(&old_id_str, old_id);
7292 818c7501 2019-07-11 stsp goto done;
7294 ff0d2220 2019-07-11 stsp if (new_id) {
7295 ff0d2220 2019-07-11 stsp err = got_object_id_str(&new_id_str, new_id);
7297 ff0d2220 2019-07-11 stsp goto done;
7300 818c7501 2019-07-11 stsp old_id_str[12] = '\0';
7301 ff0d2220 2019-07-11 stsp if (new_id_str)
7302 ff0d2220 2019-07-11 stsp new_id_str[12] = '\0';
7304 0ebf8283 2019-07-24 stsp err = get_short_logmsg(&logmsg, 42, commit);
7306 0ebf8283 2019-07-24 stsp goto done;
7308 ff0d2220 2019-07-11 stsp printf("%s -> %s: %s\n", old_id_str,
7309 ff0d2220 2019-07-11 stsp new_id_str ? new_id_str : "no-op change", logmsg);
7311 818c7501 2019-07-11 stsp free(old_id_str);
7312 818c7501 2019-07-11 stsp free(new_id_str);
7313 272a1371 2020-02-28 stsp free(logmsg);
7314 818c7501 2019-07-11 stsp return err;
7317 1ee397ad 2019-07-12 stsp static const struct got_error *
7318 3e3a69f1 2019-07-25 stsp rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
7319 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_reference *new_base_branch,
7320 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_repository *repo)
7322 818c7501 2019-07-11 stsp printf("Switching work tree to %s\n", got_ref_get_name(branch));
7323 3e3a69f1 2019-07-25 stsp return got_worktree_rebase_complete(worktree, fileindex,
7324 818c7501 2019-07-11 stsp new_base_branch, tmp_branch, branch, repo);
7327 818c7501 2019-07-11 stsp static const struct got_error *
7328 01757395 2019-07-12 stsp rebase_commit(struct got_pathlist_head *merged_paths,
7329 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
7330 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch,
7331 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
7333 ff0d2220 2019-07-11 stsp const struct got_error *error;
7334 ff0d2220 2019-07-11 stsp struct got_commit_object *commit;
7335 ff0d2220 2019-07-11 stsp struct got_object_id *new_commit_id;
7337 ff0d2220 2019-07-11 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
7338 ff0d2220 2019-07-11 stsp if (error)
7339 ff0d2220 2019-07-11 stsp return error;
7341 01757395 2019-07-12 stsp error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
7342 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, commit, commit_id, repo);
7343 ff0d2220 2019-07-11 stsp if (error) {
7344 ff0d2220 2019-07-11 stsp if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
7345 ff0d2220 2019-07-11 stsp goto done;
7346 ff0d2220 2019-07-11 stsp error = show_rebase_progress(commit, commit_id, NULL);
7348 ff0d2220 2019-07-11 stsp error = show_rebase_progress(commit, commit_id, new_commit_id);
7349 ff0d2220 2019-07-11 stsp free(new_commit_id);
7352 ff0d2220 2019-07-11 stsp got_object_commit_close(commit);
7353 ff0d2220 2019-07-11 stsp return error;
7356 64c6d990 2019-07-11 stsp struct check_path_prefix_arg {
7357 64c6d990 2019-07-11 stsp const char *path_prefix;
7358 64c6d990 2019-07-11 stsp size_t len;
7359 8ca9bd68 2019-07-25 stsp int errcode;
7362 64c6d990 2019-07-11 stsp static const struct got_error *
7363 8ca9bd68 2019-07-25 stsp check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
7364 64c6d990 2019-07-11 stsp struct got_blob_object *blob2, struct got_object_id *id1,
7365 64c6d990 2019-07-11 stsp struct got_object_id *id2, const char *path1, const char *path2,
7366 46f68b20 2019-10-19 stsp mode_t mode1, mode_t mode2, struct got_repository *repo)
7368 64c6d990 2019-07-11 stsp struct check_path_prefix_arg *a = arg;
7370 64c6d990 2019-07-11 stsp if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
7371 64c6d990 2019-07-11 stsp (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
7372 8ca9bd68 2019-07-25 stsp return got_error(a->errcode);
7374 64c6d990 2019-07-11 stsp return NULL;
7377 64c6d990 2019-07-11 stsp static const struct got_error *
7378 8ca9bd68 2019-07-25 stsp check_path_prefix(struct got_object_id *parent_id,
7379 64c6d990 2019-07-11 stsp struct got_object_id *commit_id, const char *path_prefix,
7380 8ca9bd68 2019-07-25 stsp int errcode, struct got_repository *repo)
7382 64c6d990 2019-07-11 stsp const struct got_error *err;
7383 64c6d990 2019-07-11 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
7384 64c6d990 2019-07-11 stsp struct got_commit_object *commit = NULL, *parent_commit = NULL;
7385 64c6d990 2019-07-11 stsp struct check_path_prefix_arg cpp_arg;
7387 64c6d990 2019-07-11 stsp if (got_path_is_root_dir(path_prefix))
7388 64c6d990 2019-07-11 stsp return NULL;
7390 64c6d990 2019-07-11 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
7392 64c6d990 2019-07-11 stsp goto done;
7394 64c6d990 2019-07-11 stsp err = got_object_open_as_commit(&parent_commit, repo, parent_id);
7396 64c6d990 2019-07-11 stsp goto done;
7398 64c6d990 2019-07-11 stsp err = got_object_open_as_tree(&tree1, repo,
7399 64c6d990 2019-07-11 stsp got_object_commit_get_tree_id(parent_commit));
7401 64c6d990 2019-07-11 stsp goto done;
7403 64c6d990 2019-07-11 stsp err = got_object_open_as_tree(&tree2, repo,
7404 64c6d990 2019-07-11 stsp got_object_commit_get_tree_id(commit));
7406 64c6d990 2019-07-11 stsp goto done;
7408 64c6d990 2019-07-11 stsp cpp_arg.path_prefix = path_prefix;
7409 d23ace97 2019-07-25 stsp while (cpp_arg.path_prefix[0] == '/')
7410 d23ace97 2019-07-25 stsp cpp_arg.path_prefix++;
7411 d23ace97 2019-07-25 stsp cpp_arg.len = strlen(cpp_arg.path_prefix);
7412 8ca9bd68 2019-07-25 stsp cpp_arg.errcode = errcode;
7413 8ca9bd68 2019-07-25 stsp err = got_diff_tree(tree1, tree2, "", "", repo,
7414 31b4484f 2019-07-27 stsp check_path_prefix_in_diff, &cpp_arg, 0);
7416 64c6d990 2019-07-11 stsp if (tree1)
7417 64c6d990 2019-07-11 stsp got_object_tree_close(tree1);
7418 64c6d990 2019-07-11 stsp if (tree2)
7419 64c6d990 2019-07-11 stsp got_object_tree_close(tree2);
7420 64c6d990 2019-07-11 stsp if (commit)
7421 64c6d990 2019-07-11 stsp got_object_commit_close(commit);
7422 64c6d990 2019-07-11 stsp if (parent_commit)
7423 64c6d990 2019-07-11 stsp got_object_commit_close(parent_commit);
7424 64c6d990 2019-07-11 stsp return err;
7427 ff0d2220 2019-07-11 stsp static const struct got_error *
7428 8ca9bd68 2019-07-25 stsp collect_commits(struct got_object_id_queue *commits,
7429 af61c510 2019-07-19 stsp struct got_object_id *initial_commit_id,
7430 af61c510 2019-07-19 stsp struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
7431 8ca9bd68 2019-07-25 stsp const char *path_prefix, int path_prefix_errcode,
7432 8ca9bd68 2019-07-25 stsp struct got_repository *repo)
7434 af61c510 2019-07-19 stsp const struct got_error *err = NULL;
7435 af61c510 2019-07-19 stsp struct got_commit_graph *graph = NULL;
7436 af61c510 2019-07-19 stsp struct got_object_id *parent_id = NULL;
7437 af61c510 2019-07-19 stsp struct got_object_qid *qid;
7438 af61c510 2019-07-19 stsp struct got_object_id *commit_id = initial_commit_id;
7440 3d509237 2020-01-04 stsp err = got_commit_graph_open(&graph, "/", 1);
7442 af61c510 2019-07-19 stsp return err;
7444 6fb7cd11 2019-08-22 stsp err = got_commit_graph_iter_start(graph, iter_start_id, repo,
7445 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
7447 af61c510 2019-07-19 stsp goto done;
7448 af61c510 2019-07-19 stsp while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
7449 ee780d5c 2020-01-04 stsp err = got_commit_graph_iter_next(&parent_id, graph, repo,
7450 ee780d5c 2020-01-04 stsp check_cancelled, NULL);
7451 af61c510 2019-07-19 stsp if (err) {
7452 af61c510 2019-07-19 stsp if (err->code == GOT_ERR_ITER_COMPLETED) {
7453 af61c510 2019-07-19 stsp err = got_error_msg(GOT_ERR_ANCESTRY,
7454 af61c510 2019-07-19 stsp "ran out of commits to rebase before "
7455 af61c510 2019-07-19 stsp "youngest common ancestor commit has "
7456 af61c510 2019-07-19 stsp "been reached?!?");
7458 ee780d5c 2020-01-04 stsp goto done;
7460 8ca9bd68 2019-07-25 stsp err = check_path_prefix(parent_id, commit_id,
7461 8ca9bd68 2019-07-25 stsp path_prefix, path_prefix_errcode, repo);
7463 af61c510 2019-07-19 stsp goto done;
7465 af61c510 2019-07-19 stsp err = got_object_qid_alloc(&qid, commit_id);
7467 af61c510 2019-07-19 stsp goto done;
7468 af61c510 2019-07-19 stsp SIMPLEQ_INSERT_HEAD(commits, qid, entry);
7469 af61c510 2019-07-19 stsp commit_id = parent_id;
7473 af61c510 2019-07-19 stsp got_commit_graph_close(graph);
7474 af61c510 2019-07-19 stsp return err;
7477 af61c510 2019-07-19 stsp static const struct got_error *
7478 818c7501 2019-07-11 stsp cmd_rebase(int argc, char *argv[])
7480 818c7501 2019-07-11 stsp const struct got_error *error = NULL;
7481 818c7501 2019-07-11 stsp struct got_worktree *worktree = NULL;
7482 818c7501 2019-07-11 stsp struct got_repository *repo = NULL;
7483 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex = NULL;
7484 818c7501 2019-07-11 stsp char *cwd = NULL;
7485 818c7501 2019-07-11 stsp struct got_reference *branch = NULL;
7486 818c7501 2019-07-11 stsp struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
7487 818c7501 2019-07-11 stsp struct got_object_id *commit_id = NULL, *parent_id = NULL;
7488 818c7501 2019-07-11 stsp struct got_object_id *resume_commit_id = NULL;
7489 818c7501 2019-07-11 stsp struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
7490 818c7501 2019-07-11 stsp struct got_commit_object *commit = NULL;
7491 818c7501 2019-07-11 stsp int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
7492 7ef62c4e 2020-02-24 stsp int histedit_in_progress = 0;
7493 818c7501 2019-07-11 stsp unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
7494 818c7501 2019-07-11 stsp struct got_object_id_queue commits;
7495 01757395 2019-07-12 stsp struct got_pathlist_head merged_paths;
7496 818c7501 2019-07-11 stsp const struct got_object_id_queue *parent_ids;
7497 818c7501 2019-07-11 stsp struct got_object_qid *qid, *pid;
7499 818c7501 2019-07-11 stsp SIMPLEQ_INIT(&commits);
7500 01757395 2019-07-12 stsp TAILQ_INIT(&merged_paths);
7502 818c7501 2019-07-11 stsp while ((ch = getopt(argc, argv, "ac")) != -1) {
7503 818c7501 2019-07-11 stsp switch (ch) {
7505 818c7501 2019-07-11 stsp abort_rebase = 1;
7508 818c7501 2019-07-11 stsp continue_rebase = 1;
7511 818c7501 2019-07-11 stsp usage_rebase();
7512 818c7501 2019-07-11 stsp /* NOTREACHED */
7516 818c7501 2019-07-11 stsp argc -= optind;
7517 818c7501 2019-07-11 stsp argv += optind;
7519 43012d58 2019-07-14 stsp #ifndef PROFILE
7520 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7521 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
7522 43012d58 2019-07-14 stsp err(1, "pledge");
7524 818c7501 2019-07-11 stsp if (abort_rebase && continue_rebase)
7525 818c7501 2019-07-11 stsp usage_rebase();
7526 818c7501 2019-07-11 stsp else if (abort_rebase || continue_rebase) {
7527 818c7501 2019-07-11 stsp if (argc != 0)
7528 818c7501 2019-07-11 stsp usage_rebase();
7529 818c7501 2019-07-11 stsp } else if (argc != 1)
7530 818c7501 2019-07-11 stsp usage_rebase();
7532 818c7501 2019-07-11 stsp cwd = getcwd(NULL, 0);
7533 818c7501 2019-07-11 stsp if (cwd == NULL) {
7534 818c7501 2019-07-11 stsp error = got_error_from_errno("getcwd");
7535 818c7501 2019-07-11 stsp goto done;
7537 818c7501 2019-07-11 stsp error = got_worktree_open(&worktree, cwd);
7538 fa51e947 2020-03-27 stsp if (error) {
7539 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
7540 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "rebase", cwd);
7541 818c7501 2019-07-11 stsp goto done;
7544 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7546 818c7501 2019-07-11 stsp if (error != NULL)
7547 818c7501 2019-07-11 stsp goto done;
7549 818c7501 2019-07-11 stsp error = apply_unveil(got_repo_get_path(repo), 0,
7550 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
7551 7ef62c4e 2020-02-24 stsp if (error)
7552 7ef62c4e 2020-02-24 stsp goto done;
7554 7ef62c4e 2020-02-24 stsp error = got_worktree_histedit_in_progress(&histedit_in_progress,
7555 7ef62c4e 2020-02-24 stsp worktree);
7556 818c7501 2019-07-11 stsp if (error)
7557 7ef62c4e 2020-02-24 stsp goto done;
7558 7ef62c4e 2020-02-24 stsp if (histedit_in_progress) {
7559 7ef62c4e 2020-02-24 stsp error = got_error(GOT_ERR_HISTEDIT_BUSY);
7560 818c7501 2019-07-11 stsp goto done;
7563 818c7501 2019-07-11 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
7564 818c7501 2019-07-11 stsp if (error)
7565 818c7501 2019-07-11 stsp goto done;
7567 f6794adc 2019-07-23 stsp if (abort_rebase) {
7568 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
7569 f6794adc 2019-07-23 stsp if (!rebase_in_progress) {
7570 f6794adc 2019-07-23 stsp error = got_error(GOT_ERR_NOT_REBASING);
7571 f6794adc 2019-07-23 stsp goto done;
7573 818c7501 2019-07-11 stsp error = got_worktree_rebase_continue(&resume_commit_id,
7574 3e3a69f1 2019-07-25 stsp &new_base_branch, &tmp_branch, &branch, &fileindex,
7575 3e3a69f1 2019-07-25 stsp worktree, repo);
7576 818c7501 2019-07-11 stsp if (error)
7577 818c7501 2019-07-11 stsp goto done;
7578 818c7501 2019-07-11 stsp printf("Switching work tree to %s\n",
7579 818c7501 2019-07-11 stsp got_ref_get_symref_target(new_base_branch));
7580 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
7581 3e3a69f1 2019-07-25 stsp error = got_worktree_rebase_abort(worktree, fileindex, repo,
7582 9627c110 2020-04-18 stsp new_base_branch, update_progress, &upa);
7583 818c7501 2019-07-11 stsp if (error)
7584 818c7501 2019-07-11 stsp goto done;
7585 818c7501 2019-07-11 stsp printf("Rebase of %s aborted\n", got_ref_get_name(branch));
7586 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
7587 818c7501 2019-07-11 stsp goto done; /* nothing else to do */
7590 818c7501 2019-07-11 stsp if (continue_rebase) {
7591 f6794adc 2019-07-23 stsp if (!rebase_in_progress) {
7592 f6794adc 2019-07-23 stsp error = got_error(GOT_ERR_NOT_REBASING);
7593 f6794adc 2019-07-23 stsp goto done;
7595 818c7501 2019-07-11 stsp error = got_worktree_rebase_continue(&resume_commit_id,
7596 3e3a69f1 2019-07-25 stsp &new_base_branch, &tmp_branch, &branch, &fileindex,
7597 3e3a69f1 2019-07-25 stsp worktree, repo);
7598 818c7501 2019-07-11 stsp if (error)
7599 818c7501 2019-07-11 stsp goto done;
7601 3e3a69f1 2019-07-25 stsp error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
7602 01757395 2019-07-12 stsp resume_commit_id, repo);
7603 818c7501 2019-07-11 stsp if (error)
7604 818c7501 2019-07-11 stsp goto done;
7606 ff0d2220 2019-07-11 stsp yca_id = got_object_id_dup(resume_commit_id);
7607 ff0d2220 2019-07-11 stsp if (yca_id == NULL) {
7608 818c7501 2019-07-11 stsp error = got_error_from_errno("got_object_id_dup");
7609 818c7501 2019-07-11 stsp goto done;
7612 818c7501 2019-07-11 stsp error = got_ref_open(&branch, repo, argv[0], 0);
7613 818c7501 2019-07-11 stsp if (error != NULL)
7614 818c7501 2019-07-11 stsp goto done;
7617 ff0d2220 2019-07-11 stsp error = got_ref_resolve(&branch_head_commit_id, repo, branch);
7618 ff0d2220 2019-07-11 stsp if (error)
7619 ff0d2220 2019-07-11 stsp goto done;
7621 ff0d2220 2019-07-11 stsp if (!continue_rebase) {
7622 a51a74b3 2019-07-27 stsp struct got_object_id *base_commit_id;
7624 a51a74b3 2019-07-27 stsp base_commit_id = got_worktree_get_base_commit_id(worktree);
7625 818c7501 2019-07-11 stsp error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
7626 6fb7cd11 2019-08-22 stsp base_commit_id, branch_head_commit_id, repo,
7627 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
7628 818c7501 2019-07-11 stsp if (error)
7629 818c7501 2019-07-11 stsp goto done;
7630 818c7501 2019-07-11 stsp if (yca_id == NULL) {
7631 818c7501 2019-07-11 stsp error = got_error_msg(GOT_ERR_ANCESTRY,
7632 818c7501 2019-07-11 stsp "specified branch shares no common ancestry "
7633 818c7501 2019-07-11 stsp "with work tree's branch");
7634 818c7501 2019-07-11 stsp goto done;
7637 a51a74b3 2019-07-27 stsp error = check_same_branch(base_commit_id, branch, yca_id, repo);
7638 a51a74b3 2019-07-27 stsp if (error) {
7639 a51a74b3 2019-07-27 stsp if (error->code != GOT_ERR_ANCESTRY)
7640 a51a74b3 2019-07-27 stsp goto done;
7641 a51a74b3 2019-07-27 stsp error = NULL;
7643 a51a74b3 2019-07-27 stsp error = got_error_msg(GOT_ERR_SAME_BRANCH,
7644 a51a74b3 2019-07-27 stsp "specified branch resolves to a commit which "
7645 a51a74b3 2019-07-27 stsp "is already contained in work tree's branch");
7646 a51a74b3 2019-07-27 stsp goto done;
7648 818c7501 2019-07-11 stsp error = got_worktree_rebase_prepare(&new_base_branch,
7649 3e3a69f1 2019-07-25 stsp &tmp_branch, &fileindex, worktree, branch, repo);
7650 818c7501 2019-07-11 stsp if (error)
7651 818c7501 2019-07-11 stsp goto done;
7654 ff0d2220 2019-07-11 stsp commit_id = branch_head_commit_id;
7655 818c7501 2019-07-11 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
7656 818c7501 2019-07-11 stsp if (error)
7657 818c7501 2019-07-11 stsp goto done;
7659 818c7501 2019-07-11 stsp parent_ids = got_object_commit_get_parent_ids(commit);
7660 818c7501 2019-07-11 stsp pid = SIMPLEQ_FIRST(parent_ids);
7661 fc66b545 2019-08-12 stsp if (pid == NULL) {
7662 fc66b545 2019-08-12 stsp if (!continue_rebase) {
7663 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
7664 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
7665 fc66b545 2019-08-12 stsp error = got_worktree_rebase_abort(worktree, fileindex,
7666 9627c110 2020-04-18 stsp repo, new_base_branch, update_progress, &upa);
7667 fc66b545 2019-08-12 stsp if (error)
7668 fc66b545 2019-08-12 stsp goto done;
7669 fc66b545 2019-08-12 stsp printf("Rebase of %s aborted\n",
7670 fc66b545 2019-08-12 stsp got_ref_get_name(branch));
7671 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
7674 fc66b545 2019-08-12 stsp error = got_error(GOT_ERR_EMPTY_REBASE);
7675 fc66b545 2019-08-12 stsp goto done;
7677 8ca9bd68 2019-07-25 stsp error = collect_commits(&commits, commit_id, pid->id,
7678 8ca9bd68 2019-07-25 stsp yca_id, got_worktree_get_path_prefix(worktree),
7679 8ca9bd68 2019-07-25 stsp GOT_ERR_REBASE_PATH, repo);
7680 818c7501 2019-07-11 stsp got_object_commit_close(commit);
7681 818c7501 2019-07-11 stsp commit = NULL;
7682 818c7501 2019-07-11 stsp if (error)
7683 818c7501 2019-07-11 stsp goto done;
7685 818c7501 2019-07-11 stsp if (SIMPLEQ_EMPTY(&commits)) {
7686 38b0338b 2019-11-29 stsp if (continue_rebase) {
7687 3e3a69f1 2019-07-25 stsp error = rebase_complete(worktree, fileindex,
7688 3e3a69f1 2019-07-25 stsp branch, new_base_branch, tmp_branch, repo);
7689 38b0338b 2019-11-29 stsp goto done;
7691 38b0338b 2019-11-29 stsp /* Fast-forward the reference of the branch. */
7692 38b0338b 2019-11-29 stsp struct got_object_id *new_head_commit_id;
7693 38b0338b 2019-11-29 stsp char *id_str;
7694 38b0338b 2019-11-29 stsp error = got_ref_resolve(&new_head_commit_id, repo,
7695 38b0338b 2019-11-29 stsp new_base_branch);
7696 38b0338b 2019-11-29 stsp if (error)
7697 38b0338b 2019-11-29 stsp goto done;
7698 38b0338b 2019-11-29 stsp error = got_object_id_str(&id_str, new_head_commit_id);
7699 38b0338b 2019-11-29 stsp printf("Forwarding %s to commit %s\n",
7700 38b0338b 2019-11-29 stsp got_ref_get_name(branch), id_str);
7701 38b0338b 2019-11-29 stsp free(id_str);
7702 38b0338b 2019-11-29 stsp error = got_ref_change_ref(branch,
7703 38b0338b 2019-11-29 stsp new_head_commit_id);
7704 38b0338b 2019-11-29 stsp if (error)
7705 38b0338b 2019-11-29 stsp goto done;
7709 818c7501 2019-07-11 stsp pid = NULL;
7710 818c7501 2019-07-11 stsp SIMPLEQ_FOREACH(qid, &commits, entry) {
7711 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
7713 818c7501 2019-07-11 stsp commit_id = qid->id;
7714 818c7501 2019-07-11 stsp parent_id = pid ? pid->id : yca_id;
7715 818c7501 2019-07-11 stsp pid = qid;
7717 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
7718 01757395 2019-07-12 stsp error = got_worktree_rebase_merge_files(&merged_paths,
7719 3e3a69f1 2019-07-25 stsp worktree, fileindex, parent_id, commit_id, repo,
7720 9627c110 2020-04-18 stsp update_progress, &upa, check_cancelled, NULL);
7721 818c7501 2019-07-11 stsp if (error)
7722 818c7501 2019-07-11 stsp goto done;
7724 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
7725 9627c110 2020-04-18 stsp if (upa.conflicts > 0)
7726 9627c110 2020-04-18 stsp rebase_status = GOT_STATUS_CONFLICT;
7728 01757395 2019-07-12 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
7729 a0ea4fc0 2020-02-28 stsp error = show_rebase_merge_conflict(qid->id, repo);
7730 a0ea4fc0 2020-02-28 stsp if (error)
7731 a0ea4fc0 2020-02-28 stsp goto done;
7732 01757395 2019-07-12 stsp got_worktree_rebase_pathlist_free(&merged_paths);
7736 3e3a69f1 2019-07-25 stsp error = rebase_commit(&merged_paths, worktree, fileindex,
7737 3e3a69f1 2019-07-25 stsp tmp_branch, commit_id, repo);
7738 01757395 2019-07-12 stsp got_worktree_rebase_pathlist_free(&merged_paths);
7739 818c7501 2019-07-11 stsp if (error)
7740 818c7501 2019-07-11 stsp goto done;
7743 818c7501 2019-07-11 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
7744 3e3a69f1 2019-07-25 stsp error = got_worktree_rebase_postpone(worktree, fileindex);
7745 818c7501 2019-07-11 stsp if (error)
7746 818c7501 2019-07-11 stsp goto done;
7747 818c7501 2019-07-11 stsp error = got_error_msg(GOT_ERR_CONFLICTS,
7748 11495e04 2019-07-12 stsp "conflicts must be resolved before rebasing can continue");
7750 3e3a69f1 2019-07-25 stsp error = rebase_complete(worktree, fileindex, branch,
7751 3e3a69f1 2019-07-25 stsp new_base_branch, tmp_branch, repo);
7753 818c7501 2019-07-11 stsp got_object_id_queue_free(&commits);
7754 818c7501 2019-07-11 stsp free(branch_head_commit_id);
7755 818c7501 2019-07-11 stsp free(resume_commit_id);
7756 818c7501 2019-07-11 stsp free(yca_id);
7757 818c7501 2019-07-11 stsp if (commit)
7758 818c7501 2019-07-11 stsp got_object_commit_close(commit);
7759 818c7501 2019-07-11 stsp if (branch)
7760 818c7501 2019-07-11 stsp got_ref_close(branch);
7761 818c7501 2019-07-11 stsp if (new_base_branch)
7762 818c7501 2019-07-11 stsp got_ref_close(new_base_branch);
7763 818c7501 2019-07-11 stsp if (tmp_branch)
7764 818c7501 2019-07-11 stsp got_ref_close(tmp_branch);
7765 5ef14e63 2019-06-02 stsp if (worktree)
7766 5ef14e63 2019-06-02 stsp got_worktree_close(worktree);
7768 5ef14e63 2019-06-02 stsp got_repo_close(repo);
7769 5ef14e63 2019-06-02 stsp return error;
7772 0ebf8283 2019-07-24 stsp __dead static void
7773 0ebf8283 2019-07-24 stsp usage_histedit(void)
7775 083957f4 2020-02-24 stsp fprintf(stderr, "usage: %s histedit [-a] [-c] [-F histedit-script] [-m]\n",
7776 0ebf8283 2019-07-24 stsp getprogname());
7780 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_PICK 'p'
7781 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_EDIT 'e'
7782 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_FOLD 'f'
7783 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_DROP 'd'
7784 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_MESG 'm'
7786 0ebf8283 2019-07-24 stsp static struct got_histedit_cmd {
7787 0ebf8283 2019-07-24 stsp unsigned char code;
7788 0ebf8283 2019-07-24 stsp const char *name;
7789 0ebf8283 2019-07-24 stsp const char *desc;
7790 0ebf8283 2019-07-24 stsp } got_histedit_cmds[] = {
7791 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_PICK, "pick", "use commit" },
7792 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
7793 82997472 2020-01-29 stsp { GOT_HISTEDIT_FOLD, "fold", "combine with next commit that will "
7794 82997472 2020-01-29 stsp "be used" },
7795 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
7796 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_MESG, "mesg",
7797 0ebf8283 2019-07-24 stsp "single-line log message for commit above (open editor if empty)" },
7800 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry {
7801 0ebf8283 2019-07-24 stsp TAILQ_ENTRY(got_histedit_list_entry) entry;
7802 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id;
7803 0ebf8283 2019-07-24 stsp const struct got_histedit_cmd *cmd;
7804 0ebf8283 2019-07-24 stsp char *logmsg;
7806 0ebf8283 2019-07-24 stsp TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
7808 0ebf8283 2019-07-24 stsp static const struct got_error *
7809 0ebf8283 2019-07-24 stsp histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
7810 0ebf8283 2019-07-24 stsp FILE *f, struct got_repository *repo)
7812 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
7813 0ebf8283 2019-07-24 stsp char *logmsg = NULL, *id_str = NULL;
7814 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
7817 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
7819 0ebf8283 2019-07-24 stsp goto done;
7821 0ebf8283 2019-07-24 stsp err = get_short_logmsg(&logmsg, 34, commit);
7823 0ebf8283 2019-07-24 stsp goto done;
7825 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, commit_id);
7827 0ebf8283 2019-07-24 stsp goto done;
7829 0ebf8283 2019-07-24 stsp n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
7830 0ebf8283 2019-07-24 stsp if (n < 0)
7831 0ebf8283 2019-07-24 stsp err = got_ferror(f, GOT_ERR_IO);
7833 0ebf8283 2019-07-24 stsp if (commit)
7834 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
7835 0ebf8283 2019-07-24 stsp free(id_str);
7836 0ebf8283 2019-07-24 stsp free(logmsg);
7837 0ebf8283 2019-07-24 stsp return err;
7840 0ebf8283 2019-07-24 stsp static const struct got_error *
7841 083957f4 2020-02-24 stsp histedit_write_commit_list(struct got_object_id_queue *commits,
7842 083957f4 2020-02-24 stsp FILE *f, int edit_logmsg_only, struct got_repository *repo)
7844 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
7845 0ebf8283 2019-07-24 stsp struct got_object_qid *qid;
7847 0ebf8283 2019-07-24 stsp if (SIMPLEQ_EMPTY(commits))
7848 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_EMPTY_HISTEDIT);
7850 0ebf8283 2019-07-24 stsp SIMPLEQ_FOREACH(qid, commits, entry) {
7851 0ebf8283 2019-07-24 stsp err = histedit_write_commit(qid->id, got_histedit_cmds[0].name,
7855 083957f4 2020-02-24 stsp if (edit_logmsg_only) {
7856 083957f4 2020-02-24 stsp int n = fprintf(f, "%c\n", GOT_HISTEDIT_MESG);
7857 083957f4 2020-02-24 stsp if (n < 0) {
7858 083957f4 2020-02-24 stsp err = got_ferror(f, GOT_ERR_IO);
7864 0ebf8283 2019-07-24 stsp return err;
7867 0ebf8283 2019-07-24 stsp static const struct got_error *
7868 514f2ffe 2020-01-29 stsp write_cmd_list(FILE *f, const char *branch_name,
7869 514f2ffe 2020-01-29 stsp struct got_object_id_queue *commits)
7871 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
7873 514f2ffe 2020-01-29 stsp char *id_str;
7874 514f2ffe 2020-01-29 stsp struct got_object_qid *qid;
7876 514f2ffe 2020-01-29 stsp qid = SIMPLEQ_FIRST(commits);
7877 514f2ffe 2020-01-29 stsp err = got_object_id_str(&id_str, qid->id);
7879 514f2ffe 2020-01-29 stsp return err;
7881 514f2ffe 2020-01-29 stsp n = fprintf(f,
7882 514f2ffe 2020-01-29 stsp "# Editing the history of branch '%s' starting at\n"
7883 514f2ffe 2020-01-29 stsp "# commit %s\n"
7884 514f2ffe 2020-01-29 stsp "# Commits will be processed in order from top to "
7885 514f2ffe 2020-01-29 stsp "bottom of this file.\n", branch_name, id_str);
7886 514f2ffe 2020-01-29 stsp if (n < 0) {
7887 514f2ffe 2020-01-29 stsp err = got_ferror(f, GOT_ERR_IO);
7888 514f2ffe 2020-01-29 stsp goto done;
7891 0ebf8283 2019-07-24 stsp n = fprintf(f, "# Available histedit commands:\n");
7892 514f2ffe 2020-01-29 stsp if (n < 0) {
7893 514f2ffe 2020-01-29 stsp err = got_ferror(f, GOT_ERR_IO);
7894 514f2ffe 2020-01-29 stsp goto done;
7897 0ebf8283 2019-07-24 stsp for (i = 0; i < nitems(got_histedit_cmds); i++) {
7898 0ebf8283 2019-07-24 stsp struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
7899 0ebf8283 2019-07-24 stsp n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
7900 0ebf8283 2019-07-24 stsp cmd->desc);
7901 0ebf8283 2019-07-24 stsp if (n < 0) {
7902 0ebf8283 2019-07-24 stsp err = got_ferror(f, GOT_ERR_IO);
7907 514f2ffe 2020-01-29 stsp free(id_str);
7908 0ebf8283 2019-07-24 stsp return err;
7911 0ebf8283 2019-07-24 stsp static const struct got_error *
7912 0ebf8283 2019-07-24 stsp histedit_syntax_error(int lineno)
7914 0ebf8283 2019-07-24 stsp static char msg[42];
7917 0ebf8283 2019-07-24 stsp ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
7919 0ebf8283 2019-07-24 stsp if (ret == -1 || ret >= sizeof(msg))
7920 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_HISTEDIT_SYNTAX);
7922 0ebf8283 2019-07-24 stsp return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
7925 0ebf8283 2019-07-24 stsp static const struct got_error *
7926 0ebf8283 2019-07-24 stsp append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
7927 0ebf8283 2019-07-24 stsp char *logmsg, struct got_repository *repo)
7929 0ebf8283 2019-07-24 stsp const struct got_error *err;
7930 0ebf8283 2019-07-24 stsp struct got_commit_object *folded_commit = NULL;
7931 5943eee2 2019-08-13 stsp char *id_str, *folded_logmsg = NULL;
7933 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, hle->commit_id);
7935 0ebf8283 2019-07-24 stsp return err;
7937 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
7939 0ebf8283 2019-07-24 stsp goto done;
7941 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
7943 5943eee2 2019-08-13 stsp goto done;
7944 0ebf8283 2019-07-24 stsp if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
7945 0ebf8283 2019-07-24 stsp logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
7946 5943eee2 2019-08-13 stsp folded_logmsg) == -1) {
7947 0ebf8283 2019-07-24 stsp err = got_error_from_errno("asprintf");
7950 0ebf8283 2019-07-24 stsp if (folded_commit)
7951 0ebf8283 2019-07-24 stsp got_object_commit_close(folded_commit);
7952 0ebf8283 2019-07-24 stsp free(id_str);
7953 5943eee2 2019-08-13 stsp free(folded_logmsg);
7954 0ebf8283 2019-07-24 stsp return err;
7957 0ebf8283 2019-07-24 stsp static struct got_histedit_list_entry *
7958 0ebf8283 2019-07-24 stsp get_folded_commits(struct got_histedit_list_entry *hle)
7960 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *prev, *folded = NULL;
7962 0ebf8283 2019-07-24 stsp prev = TAILQ_PREV(hle, got_histedit_list, entry);
7963 3f9de99f 2019-07-24 stsp while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
7964 3f9de99f 2019-07-24 stsp prev->cmd->code == GOT_HISTEDIT_DROP)) {
7965 3f9de99f 2019-07-24 stsp if (prev->cmd->code == GOT_HISTEDIT_FOLD)
7966 3f9de99f 2019-07-24 stsp folded = prev;
7967 0ebf8283 2019-07-24 stsp prev = TAILQ_PREV(prev, got_histedit_list, entry);
7970 0ebf8283 2019-07-24 stsp return folded;
7973 0ebf8283 2019-07-24 stsp static const struct got_error *
7974 0ebf8283 2019-07-24 stsp histedit_edit_logmsg(struct got_histedit_list_entry *hle,
7975 0ebf8283 2019-07-24 stsp struct got_repository *repo)
7977 5943eee2 2019-08-13 stsp char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
7978 0ebf8283 2019-07-24 stsp char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
7979 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
7980 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
7981 1601cb9f 2020-09-11 naddy int logmsg_len;
7983 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *folded = NULL;
7985 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&commit, repo, hle->commit_id);
7987 0ebf8283 2019-07-24 stsp return err;
7989 0ebf8283 2019-07-24 stsp folded = get_folded_commits(hle);
7990 0ebf8283 2019-07-24 stsp if (folded) {
7991 0ebf8283 2019-07-24 stsp while (folded != hle) {
7992 3f9de99f 2019-07-24 stsp if (folded->cmd->code == GOT_HISTEDIT_DROP) {
7993 3f9de99f 2019-07-24 stsp folded = TAILQ_NEXT(folded, entry);
7996 0ebf8283 2019-07-24 stsp err = append_folded_commit_msg(&new_msg, folded,
7997 0ebf8283 2019-07-24 stsp logmsg, repo);
7999 0ebf8283 2019-07-24 stsp goto done;
8000 0ebf8283 2019-07-24 stsp free(logmsg);
8001 0ebf8283 2019-07-24 stsp logmsg = new_msg;
8002 0ebf8283 2019-07-24 stsp folded = TAILQ_NEXT(folded, entry);
8006 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, hle->commit_id);
8008 0ebf8283 2019-07-24 stsp goto done;
8009 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&orig_logmsg, commit);
8011 5943eee2 2019-08-13 stsp goto done;
8012 1601cb9f 2020-09-11 naddy logmsg_len = asprintf(&new_msg,
8013 0ebf8283 2019-07-24 stsp "%s\n# original log message of commit %s: %s",
8014 1601cb9f 2020-09-11 naddy logmsg ? logmsg : "", id_str, orig_logmsg);
8015 1601cb9f 2020-09-11 naddy if (logmsg_len == -1) {
8016 0ebf8283 2019-07-24 stsp err = got_error_from_errno("asprintf");
8017 0ebf8283 2019-07-24 stsp goto done;
8019 0ebf8283 2019-07-24 stsp free(logmsg);
8020 0ebf8283 2019-07-24 stsp logmsg = new_msg;
8022 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, hle->commit_id);
8024 0ebf8283 2019-07-24 stsp goto done;
8026 bb63914a 2020-02-17 stsp err = got_opentemp_named_fd(&logmsg_path, &fd,
8027 bb63914a 2020-02-17 stsp GOT_TMPDIR_STR "/got-logmsg");
8029 0ebf8283 2019-07-24 stsp goto done;
8031 1601cb9f 2020-09-11 naddy write(fd, logmsg, logmsg_len);
8032 0ebf8283 2019-07-24 stsp close(fd);
8034 0ebf8283 2019-07-24 stsp err = get_editor(&editor);
8036 0ebf8283 2019-07-24 stsp goto done;
8038 bfa12d5e 2020-09-26 stsp err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg,
8039 bfa12d5e 2020-09-26 stsp logmsg_len);
8040 0ebf8283 2019-07-24 stsp if (err) {
8041 0ebf8283 2019-07-24 stsp if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
8042 0ebf8283 2019-07-24 stsp goto done;
8043 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&hle->logmsg, commit);
8046 0ebf8283 2019-07-24 stsp if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
8047 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("unlink", logmsg_path);
8048 0ebf8283 2019-07-24 stsp free(logmsg_path);
8049 0ebf8283 2019-07-24 stsp free(logmsg);
8050 5943eee2 2019-08-13 stsp free(orig_logmsg);
8051 0ebf8283 2019-07-24 stsp free(editor);
8052 0ebf8283 2019-07-24 stsp if (commit)
8053 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
8054 0ebf8283 2019-07-24 stsp return err;
8057 0ebf8283 2019-07-24 stsp static const struct got_error *
8058 0ebf8283 2019-07-24 stsp histedit_parse_list(struct got_histedit_list *histedit_cmds,
8059 0ebf8283 2019-07-24 stsp FILE *f, struct got_repository *repo)
8061 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
8062 0ebf8283 2019-07-24 stsp char *line = NULL, *p, *end;
8063 0ebf8283 2019-07-24 stsp size_t size;
8064 0ebf8283 2019-07-24 stsp ssize_t len;
8065 0ebf8283 2019-07-24 stsp int lineno = 0, i;
8066 0ebf8283 2019-07-24 stsp const struct got_histedit_cmd *cmd;
8067 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id = NULL;
8068 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle = NULL;
8070 0ebf8283 2019-07-24 stsp for (;;) {
8071 0ebf8283 2019-07-24 stsp len = getline(&line, &size, f);
8072 0ebf8283 2019-07-24 stsp if (len == -1) {
8073 0ebf8283 2019-07-24 stsp const struct got_error *getline_err;
8074 0ebf8283 2019-07-24 stsp if (feof(f))
8076 0ebf8283 2019-07-24 stsp getline_err = got_error_from_errno("getline");
8077 0ebf8283 2019-07-24 stsp err = got_ferror(f, getline_err->code);
8082 0ebf8283 2019-07-24 stsp while (isspace((unsigned char)p[0]))
8084 0ebf8283 2019-07-24 stsp if (p[0] == '#' || p[0] == '\0') {
8085 0ebf8283 2019-07-24 stsp free(line);
8086 0ebf8283 2019-07-24 stsp line = NULL;
8089 0ebf8283 2019-07-24 stsp cmd = NULL;
8090 0ebf8283 2019-07-24 stsp for (i = 0; i < nitems(got_histedit_cmds); i++) {
8091 0ebf8283 2019-07-24 stsp cmd = &got_histedit_cmds[i];
8092 0ebf8283 2019-07-24 stsp if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
8093 0ebf8283 2019-07-24 stsp isspace((unsigned char)p[strlen(cmd->name)])) {
8094 0ebf8283 2019-07-24 stsp p += strlen(cmd->name);
8097 0ebf8283 2019-07-24 stsp if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
8102 6c1844f6 2019-07-25 stsp if (i == nitems(got_histedit_cmds)) {
8103 0ebf8283 2019-07-24 stsp err = histedit_syntax_error(lineno);
8106 0ebf8283 2019-07-24 stsp while (isspace((unsigned char)p[0]))
8108 0ebf8283 2019-07-24 stsp if (cmd->code == GOT_HISTEDIT_MESG) {
8109 0ebf8283 2019-07-24 stsp if (hle == NULL || hle->logmsg != NULL) {
8110 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_HISTEDIT_CMD);
8113 0ebf8283 2019-07-24 stsp if (p[0] == '\0') {
8114 0ebf8283 2019-07-24 stsp err = histedit_edit_logmsg(hle, repo);
8118 0ebf8283 2019-07-24 stsp hle->logmsg = strdup(p);
8119 0ebf8283 2019-07-24 stsp if (hle->logmsg == NULL) {
8120 0ebf8283 2019-07-24 stsp err = got_error_from_errno("strdup");
8124 0ebf8283 2019-07-24 stsp free(line);
8125 0ebf8283 2019-07-24 stsp line = NULL;
8129 0ebf8283 2019-07-24 stsp while (end[0] && !isspace((unsigned char)end[0]))
8131 0ebf8283 2019-07-24 stsp *end = '\0';
8133 0ebf8283 2019-07-24 stsp err = got_object_resolve_id_str(&commit_id, repo, p);
8134 0ebf8283 2019-07-24 stsp if (err) {
8135 0ebf8283 2019-07-24 stsp /* override error code */
8136 0ebf8283 2019-07-24 stsp err = histedit_syntax_error(lineno);
8140 0ebf8283 2019-07-24 stsp hle = malloc(sizeof(*hle));
8141 0ebf8283 2019-07-24 stsp if (hle == NULL) {
8142 0ebf8283 2019-07-24 stsp err = got_error_from_errno("malloc");
8145 0ebf8283 2019-07-24 stsp hle->cmd = cmd;
8146 0ebf8283 2019-07-24 stsp hle->commit_id = commit_id;
8147 0ebf8283 2019-07-24 stsp hle->logmsg = NULL;
8148 0ebf8283 2019-07-24 stsp commit_id = NULL;
8149 0ebf8283 2019-07-24 stsp free(line);
8150 0ebf8283 2019-07-24 stsp line = NULL;
8151 0ebf8283 2019-07-24 stsp TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
8154 0ebf8283 2019-07-24 stsp free(line);
8155 0ebf8283 2019-07-24 stsp free(commit_id);
8156 0ebf8283 2019-07-24 stsp return err;
8159 0ebf8283 2019-07-24 stsp static const struct got_error *
8160 bfce7f83 2019-07-27 stsp histedit_check_script(struct got_histedit_list *histedit_cmds,
8161 bfce7f83 2019-07-27 stsp struct got_object_id_queue *commits, struct got_repository *repo)
8163 bfce7f83 2019-07-27 stsp const struct got_error *err = NULL;
8164 bfce7f83 2019-07-27 stsp struct got_object_qid *qid;
8165 bfce7f83 2019-07-27 stsp struct got_histedit_list_entry *hle;
8166 5b87815e 2020-03-05 stsp static char msg[92];
8167 bfce7f83 2019-07-27 stsp char *id_str;
8169 bfce7f83 2019-07-27 stsp if (TAILQ_EMPTY(histedit_cmds))
8170 bfce7f83 2019-07-27 stsp return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
8171 bfce7f83 2019-07-27 stsp "histedit script contains no commands");
8172 a0de39f3 2019-08-09 stsp if (SIMPLEQ_EMPTY(commits))
8173 a0de39f3 2019-08-09 stsp return got_error(GOT_ERR_EMPTY_HISTEDIT);
8175 5b87815e 2020-03-05 stsp TAILQ_FOREACH(hle, histedit_cmds, entry) {
8176 5b87815e 2020-03-05 stsp struct got_histedit_list_entry *hle2;
8177 5b87815e 2020-03-05 stsp TAILQ_FOREACH(hle2, histedit_cmds, entry) {
8178 5b87815e 2020-03-05 stsp if (hle == hle2)
8180 5b87815e 2020-03-05 stsp if (got_object_id_cmp(hle->commit_id,
8181 5b87815e 2020-03-05 stsp hle2->commit_id) != 0)
8183 5b87815e 2020-03-05 stsp err = got_object_id_str(&id_str, hle->commit_id);
8185 5b87815e 2020-03-05 stsp return err;
8186 5b87815e 2020-03-05 stsp snprintf(msg, sizeof(msg), "commit %s is listed "
8187 5b87815e 2020-03-05 stsp "more than once in histedit script", id_str);
8188 5b87815e 2020-03-05 stsp free(id_str);
8189 5b87815e 2020-03-05 stsp return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
8193 bfce7f83 2019-07-27 stsp SIMPLEQ_FOREACH(qid, commits, entry) {
8194 bfce7f83 2019-07-27 stsp TAILQ_FOREACH(hle, histedit_cmds, entry) {
8195 bfce7f83 2019-07-27 stsp if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
8198 bfce7f83 2019-07-27 stsp if (hle == NULL) {
8199 bfce7f83 2019-07-27 stsp err = got_object_id_str(&id_str, qid->id);
8201 bfce7f83 2019-07-27 stsp return err;
8202 bfce7f83 2019-07-27 stsp snprintf(msg, sizeof(msg),
8203 bfce7f83 2019-07-27 stsp "commit %s missing from histedit script", id_str);
8204 bfce7f83 2019-07-27 stsp free(id_str);
8205 bfce7f83 2019-07-27 stsp return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
8209 0def28b1 2019-08-17 stsp hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
8210 0def28b1 2019-08-17 stsp if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
8211 bfce7f83 2019-07-27 stsp return got_error_msg(GOT_ERR_HISTEDIT_CMD,
8212 bfce7f83 2019-07-27 stsp "last commit in histedit script cannot be folded");
8214 bfce7f83 2019-07-27 stsp return NULL;
8217 bfce7f83 2019-07-27 stsp static const struct got_error *
8218 0ebf8283 2019-07-24 stsp histedit_run_editor(struct got_histedit_list *histedit_cmds,
8219 bfce7f83 2019-07-27 stsp const char *path, struct got_object_id_queue *commits,
8220 bfce7f83 2019-07-27 stsp struct got_repository *repo)
8222 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
8223 0ebf8283 2019-07-24 stsp char *editor;
8224 0ebf8283 2019-07-24 stsp FILE *f = NULL;
8226 0ebf8283 2019-07-24 stsp err = get_editor(&editor);
8228 0ebf8283 2019-07-24 stsp return err;
8230 0ebf8283 2019-07-24 stsp if (spawn_editor(editor, path) == -1) {
8231 0ebf8283 2019-07-24 stsp err = got_error_from_errno("failed spawning editor");
8232 0ebf8283 2019-07-24 stsp goto done;
8235 0ebf8283 2019-07-24 stsp f = fopen(path, "r");
8236 0ebf8283 2019-07-24 stsp if (f == NULL) {
8237 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fopen");
8238 0ebf8283 2019-07-24 stsp goto done;
8240 0ebf8283 2019-07-24 stsp err = histedit_parse_list(histedit_cmds, f, repo);
8242 bfce7f83 2019-07-27 stsp goto done;
8244 bfce7f83 2019-07-27 stsp err = histedit_check_script(histedit_cmds, commits, repo);
8246 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
8247 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
8248 0ebf8283 2019-07-24 stsp free(editor);
8249 0ebf8283 2019-07-24 stsp return err;
8252 0ebf8283 2019-07-24 stsp static const struct got_error *
8253 bfce7f83 2019-07-27 stsp histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
8254 514f2ffe 2020-01-29 stsp struct got_object_id_queue *, const char *, const char *,
8255 514f2ffe 2020-01-29 stsp struct got_repository *);
8257 0ebf8283 2019-07-24 stsp static const struct got_error *
8258 0ebf8283 2019-07-24 stsp histedit_edit_script(struct got_histedit_list *histedit_cmds,
8259 514f2ffe 2020-01-29 stsp struct got_object_id_queue *commits, const char *branch_name,
8260 083957f4 2020-02-24 stsp int edit_logmsg_only, struct got_repository *repo)
8262 0ebf8283 2019-07-24 stsp const struct got_error *err;
8263 0ebf8283 2019-07-24 stsp FILE *f = NULL;
8264 0ebf8283 2019-07-24 stsp char *path = NULL;
8266 0ebf8283 2019-07-24 stsp err = got_opentemp_named(&path, &f, "got-histedit");
8268 0ebf8283 2019-07-24 stsp return err;
8270 514f2ffe 2020-01-29 stsp err = write_cmd_list(f, branch_name, commits);
8272 0ebf8283 2019-07-24 stsp goto done;
8274 083957f4 2020-02-24 stsp err = histedit_write_commit_list(commits, f, edit_logmsg_only, repo);
8276 0ebf8283 2019-07-24 stsp goto done;
8278 083957f4 2020-02-24 stsp if (edit_logmsg_only) {
8279 083957f4 2020-02-24 stsp rewind(f);
8280 083957f4 2020-02-24 stsp err = histedit_parse_list(histedit_cmds, f, repo);
8282 083957f4 2020-02-24 stsp if (fclose(f) != 0) {
8283 083957f4 2020-02-24 stsp err = got_error_from_errno("fclose");
8284 0ebf8283 2019-07-24 stsp goto done;
8287 083957f4 2020-02-24 stsp err = histedit_run_editor(histedit_cmds, path, commits, repo);
8288 083957f4 2020-02-24 stsp if (err) {
8289 083957f4 2020-02-24 stsp if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
8290 083957f4 2020-02-24 stsp err->code != GOT_ERR_HISTEDIT_CMD)
8291 083957f4 2020-02-24 stsp goto done;
8292 083957f4 2020-02-24 stsp err = histedit_edit_list_retry(histedit_cmds, err,
8293 083957f4 2020-02-24 stsp commits, path, branch_name, repo);
8297 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
8298 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
8299 0ebf8283 2019-07-24 stsp if (path && unlink(path) != 0 && err == NULL)
8300 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("unlink", path);
8301 0ebf8283 2019-07-24 stsp free(path);
8302 0ebf8283 2019-07-24 stsp return err;
8305 0ebf8283 2019-07-24 stsp static const struct got_error *
8306 0ebf8283 2019-07-24 stsp histedit_save_list(struct got_histedit_list *histedit_cmds,
8307 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
8309 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
8310 0ebf8283 2019-07-24 stsp char *path = NULL;
8311 0ebf8283 2019-07-24 stsp FILE *f = NULL;
8312 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle;
8313 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
8315 c3022ba5 2019-07-27 stsp err = got_worktree_get_histedit_script_path(&path, worktree);
8317 0ebf8283 2019-07-24 stsp return err;
8319 0ebf8283 2019-07-24 stsp f = fopen(path, "w");
8320 0ebf8283 2019-07-24 stsp if (f == NULL) {
8321 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("fopen", path);
8322 0ebf8283 2019-07-24 stsp goto done;
8324 0ebf8283 2019-07-24 stsp TAILQ_FOREACH(hle, histedit_cmds, entry) {
8325 0ebf8283 2019-07-24 stsp err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
8330 0ebf8283 2019-07-24 stsp if (hle->logmsg) {
8331 0ebf8283 2019-07-24 stsp int n = fprintf(f, "%c %s\n",
8332 0ebf8283 2019-07-24 stsp GOT_HISTEDIT_MESG, hle->logmsg);
8333 0ebf8283 2019-07-24 stsp if (n < 0) {
8334 0ebf8283 2019-07-24 stsp err = got_ferror(f, GOT_ERR_IO);
8340 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
8341 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
8342 0ebf8283 2019-07-24 stsp free(path);
8343 0ebf8283 2019-07-24 stsp if (commit)
8344 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
8345 0ebf8283 2019-07-24 stsp return err;
8349 bfce7f83 2019-07-27 stsp histedit_free_list(struct got_histedit_list *histedit_cmds)
8351 bfce7f83 2019-07-27 stsp struct got_histedit_list_entry *hle;
8353 bfce7f83 2019-07-27 stsp while ((hle = TAILQ_FIRST(histedit_cmds))) {
8354 bfce7f83 2019-07-27 stsp TAILQ_REMOVE(histedit_cmds, hle, entry);
8355 bfce7f83 2019-07-27 stsp free(hle);
8359 0ebf8283 2019-07-24 stsp static const struct got_error *
8360 0ebf8283 2019-07-24 stsp histedit_load_list(struct got_histedit_list *histedit_cmds,
8361 0ebf8283 2019-07-24 stsp const char *path, struct got_repository *repo)
8363 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
8364 0ebf8283 2019-07-24 stsp FILE *f = NULL;
8366 0ebf8283 2019-07-24 stsp f = fopen(path, "r");
8367 0ebf8283 2019-07-24 stsp if (f == NULL) {
8368 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("fopen", path);
8369 0ebf8283 2019-07-24 stsp goto done;
8372 0ebf8283 2019-07-24 stsp err = histedit_parse_list(histedit_cmds, f, repo);
8374 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
8375 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
8376 0ebf8283 2019-07-24 stsp return err;
8379 0ebf8283 2019-07-24 stsp static const struct got_error *
8380 0ebf8283 2019-07-24 stsp histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
8381 bfce7f83 2019-07-27 stsp const struct got_error *edit_err, struct got_object_id_queue *commits,
8382 514f2ffe 2020-01-29 stsp const char *path, const char *branch_name, struct got_repository *repo)
8384 bfce7f83 2019-07-27 stsp const struct got_error *err = NULL, *prev_err = edit_err;
8385 0ebf8283 2019-07-24 stsp int resp = ' ';
8387 b006047e 2019-07-25 stsp while (resp != 'c' && resp != 'r' && resp != 'a') {
8388 0ebf8283 2019-07-24 stsp printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
8389 bfce7f83 2019-07-27 stsp "or (a)bort: ", getprogname(), prev_err->msg);
8390 0ebf8283 2019-07-24 stsp resp = getchar();
8391 a61a4414 2019-08-07 stsp if (resp == '\n')
8392 a61a4414 2019-08-07 stsp resp = getchar();
8393 426ebf2e 2019-07-25 stsp if (resp == 'c') {
8394 bfce7f83 2019-07-27 stsp histedit_free_list(histedit_cmds);
8395 bfce7f83 2019-07-27 stsp err = histedit_run_editor(histedit_cmds, path, commits,
8397 426ebf2e 2019-07-25 stsp if (err) {
8398 bfce7f83 2019-07-27 stsp if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
8399 bfce7f83 2019-07-27 stsp err->code != GOT_ERR_HISTEDIT_CMD)
8401 bfce7f83 2019-07-27 stsp prev_err = err;
8402 426ebf2e 2019-07-25 stsp resp = ' ';
8406 426ebf2e 2019-07-25 stsp } else if (resp == 'r') {
8407 bfce7f83 2019-07-27 stsp histedit_free_list(histedit_cmds);
8408 0ebf8283 2019-07-24 stsp err = histedit_edit_script(histedit_cmds,
8409 083957f4 2020-02-24 stsp commits, branch_name, 0, repo);
8410 426ebf2e 2019-07-25 stsp if (err) {
8411 bfce7f83 2019-07-27 stsp if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
8412 bfce7f83 2019-07-27 stsp err->code != GOT_ERR_HISTEDIT_CMD)
8414 bfce7f83 2019-07-27 stsp prev_err = err;
8415 426ebf2e 2019-07-25 stsp resp = ' ';
8419 426ebf2e 2019-07-25 stsp } else if (resp == 'a') {
8420 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_HISTEDIT_CANCEL);
8423 0ebf8283 2019-07-24 stsp printf("invalid response '%c'\n", resp);
8426 0ebf8283 2019-07-24 stsp return err;
8429 0ebf8283 2019-07-24 stsp static const struct got_error *
8430 0ebf8283 2019-07-24 stsp histedit_complete(struct got_worktree *worktree,
8431 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
8432 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_repository *repo)
8434 0ebf8283 2019-07-24 stsp printf("Switching work tree to %s\n",
8435 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch));
8436 3e3a69f1 2019-07-25 stsp return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
8437 3e3a69f1 2019-07-25 stsp branch, repo);
8440 0ebf8283 2019-07-24 stsp static const struct got_error *
8441 0ebf8283 2019-07-24 stsp show_histedit_progress(struct got_commit_object *commit,
8442 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle, struct got_object_id *new_id)
8444 0ebf8283 2019-07-24 stsp const struct got_error *err;
8445 0ebf8283 2019-07-24 stsp char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
8447 0ebf8283 2019-07-24 stsp err = got_object_id_str(&old_id_str, hle->commit_id);
8449 0ebf8283 2019-07-24 stsp goto done;
8451 0ebf8283 2019-07-24 stsp if (new_id) {
8452 0ebf8283 2019-07-24 stsp err = got_object_id_str(&new_id_str, new_id);
8454 0ebf8283 2019-07-24 stsp goto done;
8457 0ebf8283 2019-07-24 stsp old_id_str[12] = '\0';
8458 0ebf8283 2019-07-24 stsp if (new_id_str)
8459 0ebf8283 2019-07-24 stsp new_id_str[12] = '\0';
8461 0ebf8283 2019-07-24 stsp if (hle->logmsg) {
8462 0ebf8283 2019-07-24 stsp logmsg = strdup(hle->logmsg);
8463 0ebf8283 2019-07-24 stsp if (logmsg == NULL) {
8464 0ebf8283 2019-07-24 stsp err = got_error_from_errno("strdup");
8465 0ebf8283 2019-07-24 stsp goto done;
8467 0ebf8283 2019-07-24 stsp trim_logmsg(logmsg, 42);
8469 0ebf8283 2019-07-24 stsp err = get_short_logmsg(&logmsg, 42, commit);
8471 0ebf8283 2019-07-24 stsp goto done;
8474 0ebf8283 2019-07-24 stsp switch (hle->cmd->code) {
8475 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_PICK:
8476 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_EDIT:
8477 0ebf8283 2019-07-24 stsp printf("%s -> %s: %s\n", old_id_str,
8478 0ebf8283 2019-07-24 stsp new_id_str ? new_id_str : "no-op change", logmsg);
8480 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_DROP:
8481 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_FOLD:
8482 0ebf8283 2019-07-24 stsp printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
8489 0ebf8283 2019-07-24 stsp free(old_id_str);
8490 0ebf8283 2019-07-24 stsp free(new_id_str);
8491 0ebf8283 2019-07-24 stsp return err;
8494 0ebf8283 2019-07-24 stsp static const struct got_error *
8495 0ebf8283 2019-07-24 stsp histedit_commit(struct got_pathlist_head *merged_paths,
8496 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
8497 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
8498 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
8500 0ebf8283 2019-07-24 stsp const struct got_error *err;
8501 0ebf8283 2019-07-24 stsp struct got_commit_object *commit;
8502 0ebf8283 2019-07-24 stsp struct got_object_id *new_commit_id;
8504 0ebf8283 2019-07-24 stsp if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
8505 0ebf8283 2019-07-24 stsp && hle->logmsg == NULL) {
8506 0ebf8283 2019-07-24 stsp err = histedit_edit_logmsg(hle, repo);
8508 0ebf8283 2019-07-24 stsp return err;
8511 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&commit, repo, hle->commit_id);
8513 0ebf8283 2019-07-24 stsp return err;
8515 0ebf8283 2019-07-24 stsp err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
8516 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, commit, hle->commit_id,
8517 3e3a69f1 2019-07-25 stsp hle->logmsg, repo);
8518 0ebf8283 2019-07-24 stsp if (err) {
8519 0ebf8283 2019-07-24 stsp if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
8520 0ebf8283 2019-07-24 stsp goto done;
8521 0ebf8283 2019-07-24 stsp err = show_histedit_progress(commit, hle, NULL);
8523 0ebf8283 2019-07-24 stsp err = show_histedit_progress(commit, hle, new_commit_id);
8524 0ebf8283 2019-07-24 stsp free(new_commit_id);
8527 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
8528 0ebf8283 2019-07-24 stsp return err;
8531 0ebf8283 2019-07-24 stsp static const struct got_error *
8532 0ebf8283 2019-07-24 stsp histedit_skip_commit(struct got_histedit_list_entry *hle,
8533 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
8535 0ebf8283 2019-07-24 stsp const struct got_error *error;
8536 0ebf8283 2019-07-24 stsp struct got_commit_object *commit;
8538 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
8540 0ebf8283 2019-07-24 stsp if (error)
8541 0ebf8283 2019-07-24 stsp return error;
8543 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo, hle->commit_id);
8544 0ebf8283 2019-07-24 stsp if (error)
8545 0ebf8283 2019-07-24 stsp return error;
8547 0ebf8283 2019-07-24 stsp error = show_histedit_progress(commit, hle, NULL);
8548 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
8549 0ebf8283 2019-07-24 stsp return error;
8552 ab20a43a 2020-01-29 stsp static const struct got_error *
8553 ab20a43a 2020-01-29 stsp check_local_changes(void *arg, unsigned char status,
8554 ab20a43a 2020-01-29 stsp unsigned char staged_status, const char *path,
8555 ab20a43a 2020-01-29 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8556 ab20a43a 2020-01-29 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
8558 ab20a43a 2020-01-29 stsp int *have_local_changes = arg;
8560 ab20a43a 2020-01-29 stsp switch (status) {
8561 ab20a43a 2020-01-29 stsp case GOT_STATUS_ADD:
8562 ab20a43a 2020-01-29 stsp case GOT_STATUS_DELETE:
8563 ab20a43a 2020-01-29 stsp case GOT_STATUS_MODIFY:
8564 ab20a43a 2020-01-29 stsp case GOT_STATUS_CONFLICT:
8565 ab20a43a 2020-01-29 stsp *have_local_changes = 1;
8566 ab20a43a 2020-01-29 stsp return got_error(GOT_ERR_CANCELLED);
8571 ab20a43a 2020-01-29 stsp switch (staged_status) {
8572 ab20a43a 2020-01-29 stsp case GOT_STATUS_ADD:
8573 ab20a43a 2020-01-29 stsp case GOT_STATUS_DELETE:
8574 ab20a43a 2020-01-29 stsp case GOT_STATUS_MODIFY:
8575 ab20a43a 2020-01-29 stsp *have_local_changes = 1;
8576 ab20a43a 2020-01-29 stsp return got_error(GOT_ERR_CANCELLED);
8581 ab20a43a 2020-01-29 stsp return NULL;
8584 0ebf8283 2019-07-24 stsp static const struct got_error *
8585 0ebf8283 2019-07-24 stsp cmd_histedit(int argc, char *argv[])
8587 0ebf8283 2019-07-24 stsp const struct got_error *error = NULL;
8588 0ebf8283 2019-07-24 stsp struct got_worktree *worktree = NULL;
8589 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex = NULL;
8590 0ebf8283 2019-07-24 stsp struct got_repository *repo = NULL;
8591 0ebf8283 2019-07-24 stsp char *cwd = NULL;
8592 0ebf8283 2019-07-24 stsp struct got_reference *branch = NULL;
8593 0ebf8283 2019-07-24 stsp struct got_reference *tmp_branch = NULL;
8594 0ebf8283 2019-07-24 stsp struct got_object_id *resume_commit_id = NULL;
8595 0ebf8283 2019-07-24 stsp struct got_object_id *base_commit_id = NULL;
8596 0ebf8283 2019-07-24 stsp struct got_object_id *head_commit_id = NULL;
8597 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
8598 9627c110 2020-04-18 stsp int ch, rebase_in_progress = 0;
8599 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
8600 0ebf8283 2019-07-24 stsp int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
8601 083957f4 2020-02-24 stsp int edit_logmsg_only = 0;
8602 0ebf8283 2019-07-24 stsp const char *edit_script_path = NULL;
8603 0ebf8283 2019-07-24 stsp unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
8604 0ebf8283 2019-07-24 stsp struct got_object_id_queue commits;
8605 0ebf8283 2019-07-24 stsp struct got_pathlist_head merged_paths;
8606 0ebf8283 2019-07-24 stsp const struct got_object_id_queue *parent_ids;
8607 0ebf8283 2019-07-24 stsp struct got_object_qid *pid;
8608 0ebf8283 2019-07-24 stsp struct got_histedit_list histedit_cmds;
8609 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle;
8611 0ebf8283 2019-07-24 stsp SIMPLEQ_INIT(&commits);
8612 0ebf8283 2019-07-24 stsp TAILQ_INIT(&histedit_cmds);
8613 0ebf8283 2019-07-24 stsp TAILQ_INIT(&merged_paths);
8614 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
8616 083957f4 2020-02-24 stsp while ((ch = getopt(argc, argv, "acF:m")) != -1) {
8617 0ebf8283 2019-07-24 stsp switch (ch) {
8619 0ebf8283 2019-07-24 stsp abort_edit = 1;
8622 0ebf8283 2019-07-24 stsp continue_edit = 1;
8625 0ebf8283 2019-07-24 stsp edit_script_path = optarg;
8628 083957f4 2020-02-24 stsp edit_logmsg_only = 1;
8631 0ebf8283 2019-07-24 stsp usage_histedit();
8632 0ebf8283 2019-07-24 stsp /* NOTREACHED */
8636 0ebf8283 2019-07-24 stsp argc -= optind;
8637 0ebf8283 2019-07-24 stsp argv += optind;
8639 0ebf8283 2019-07-24 stsp #ifndef PROFILE
8640 0ebf8283 2019-07-24 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
8641 0ebf8283 2019-07-24 stsp "unveil", NULL) == -1)
8642 0ebf8283 2019-07-24 stsp err(1, "pledge");
8644 0ebf8283 2019-07-24 stsp if (abort_edit && continue_edit)
8645 083957f4 2020-02-24 stsp errx(1, "histedit's -a and -c options are mutually exclusive");
8646 083957f4 2020-02-24 stsp if (edit_script_path && edit_logmsg_only)
8647 083957f4 2020-02-24 stsp errx(1, "histedit's -F and -m options are mutually exclusive");
8648 083957f4 2020-02-24 stsp if (abort_edit && edit_logmsg_only)
8649 083957f4 2020-02-24 stsp errx(1, "histedit's -a and -m options are mutually exclusive");
8650 083957f4 2020-02-24 stsp if (continue_edit && edit_logmsg_only)
8651 083957f4 2020-02-24 stsp errx(1, "histedit's -c and -m options are mutually exclusive");
8652 0ebf8283 2019-07-24 stsp if (argc != 0)
8653 0ebf8283 2019-07-24 stsp usage_histedit();
8656 0ebf8283 2019-07-24 stsp * This command cannot apply unveil(2) in all cases because the
8657 0ebf8283 2019-07-24 stsp * user may choose to run an editor to edit the histedit script
8658 0ebf8283 2019-07-24 stsp * and to edit individual commit log messages.
8659 0ebf8283 2019-07-24 stsp * unveil(2) traverses exec(2); if an editor is used we have to
8660 0ebf8283 2019-07-24 stsp * apply unveil after edit script and log messages have been written.
8661 0ebf8283 2019-07-24 stsp * XXX TODO: Make use of unveil(2) where possible.
8664 0ebf8283 2019-07-24 stsp cwd = getcwd(NULL, 0);
8665 0ebf8283 2019-07-24 stsp if (cwd == NULL) {
8666 0ebf8283 2019-07-24 stsp error = got_error_from_errno("getcwd");
8667 0ebf8283 2019-07-24 stsp goto done;
8669 0ebf8283 2019-07-24 stsp error = got_worktree_open(&worktree, cwd);
8670 fa51e947 2020-03-27 stsp if (error) {
8671 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
8672 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "histedit", cwd);
8673 0ebf8283 2019-07-24 stsp goto done;
8676 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
8678 0ebf8283 2019-07-24 stsp if (error != NULL)
8679 0ebf8283 2019-07-24 stsp goto done;
8681 0ebf8283 2019-07-24 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
8682 0ebf8283 2019-07-24 stsp if (error)
8683 0ebf8283 2019-07-24 stsp goto done;
8684 0ebf8283 2019-07-24 stsp if (rebase_in_progress) {
8685 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_REBASING);
8686 0ebf8283 2019-07-24 stsp goto done;
8689 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
8690 0ebf8283 2019-07-24 stsp if (error)
8691 0ebf8283 2019-07-24 stsp goto done;
8693 083957f4 2020-02-24 stsp if (edit_in_progress && edit_logmsg_only) {
8694 083957f4 2020-02-24 stsp error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
8695 083957f4 2020-02-24 stsp "histedit operation is in progress in this "
8696 083957f4 2020-02-24 stsp "work tree and must be continued or aborted "
8697 083957f4 2020-02-24 stsp "before the -m option can be used");
8698 083957f4 2020-02-24 stsp goto done;
8701 0ebf8283 2019-07-24 stsp if (edit_in_progress && abort_edit) {
8702 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_continue(&resume_commit_id,
8703 3e3a69f1 2019-07-25 stsp &tmp_branch, &branch, &base_commit_id, &fileindex,
8704 3e3a69f1 2019-07-25 stsp worktree, repo);
8705 0ebf8283 2019-07-24 stsp if (error)
8706 0ebf8283 2019-07-24 stsp goto done;
8707 0ebf8283 2019-07-24 stsp printf("Switching work tree to %s\n",
8708 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch));
8709 3e3a69f1 2019-07-25 stsp error = got_worktree_histedit_abort(worktree, fileindex, repo,
8710 9627c110 2020-04-18 stsp branch, base_commit_id, update_progress, &upa);
8711 0ebf8283 2019-07-24 stsp if (error)
8712 0ebf8283 2019-07-24 stsp goto done;
8713 0ebf8283 2019-07-24 stsp printf("Histedit of %s aborted\n",
8714 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch));
8715 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
8716 0ebf8283 2019-07-24 stsp goto done; /* nothing else to do */
8717 0ebf8283 2019-07-24 stsp } else if (abort_edit) {
8718 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_NOT_HISTEDIT);
8719 0ebf8283 2019-07-24 stsp goto done;
8722 0ebf8283 2019-07-24 stsp if (continue_edit) {
8723 0ebf8283 2019-07-24 stsp char *path;
8725 0ebf8283 2019-07-24 stsp if (!edit_in_progress) {
8726 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_NOT_HISTEDIT);
8727 0ebf8283 2019-07-24 stsp goto done;
8730 c3022ba5 2019-07-27 stsp error = got_worktree_get_histedit_script_path(&path, worktree);
8731 0ebf8283 2019-07-24 stsp if (error)
8732 0ebf8283 2019-07-24 stsp goto done;
8734 0ebf8283 2019-07-24 stsp error = histedit_load_list(&histedit_cmds, path, repo);
8735 0ebf8283 2019-07-24 stsp free(path);
8736 0ebf8283 2019-07-24 stsp if (error)
8737 0ebf8283 2019-07-24 stsp goto done;
8739 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_continue(&resume_commit_id,
8740 3e3a69f1 2019-07-25 stsp &tmp_branch, &branch, &base_commit_id, &fileindex,
8741 3e3a69f1 2019-07-25 stsp worktree, repo);
8742 0ebf8283 2019-07-24 stsp if (error)
8743 0ebf8283 2019-07-24 stsp goto done;
8745 0ebf8283 2019-07-24 stsp error = got_ref_resolve(&head_commit_id, repo, branch);
8746 0ebf8283 2019-07-24 stsp if (error)
8747 0ebf8283 2019-07-24 stsp goto done;
8749 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo,
8750 0ebf8283 2019-07-24 stsp head_commit_id);
8751 0ebf8283 2019-07-24 stsp if (error)
8752 0ebf8283 2019-07-24 stsp goto done;
8753 0ebf8283 2019-07-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
8754 0ebf8283 2019-07-24 stsp pid = SIMPLEQ_FIRST(parent_ids);
8755 3aac7cf7 2019-07-25 stsp if (pid == NULL) {
8756 3aac7cf7 2019-07-25 stsp error = got_error(GOT_ERR_EMPTY_HISTEDIT);
8757 3aac7cf7 2019-07-25 stsp goto done;
8759 8ca9bd68 2019-07-25 stsp error = collect_commits(&commits, head_commit_id, pid->id,
8760 8ca9bd68 2019-07-25 stsp base_commit_id, got_worktree_get_path_prefix(worktree),
8761 8ca9bd68 2019-07-25 stsp GOT_ERR_HISTEDIT_PATH, repo);
8762 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
8763 0ebf8283 2019-07-24 stsp commit = NULL;
8764 0ebf8283 2019-07-24 stsp if (error)
8765 0ebf8283 2019-07-24 stsp goto done;
8767 0ebf8283 2019-07-24 stsp if (edit_in_progress) {
8768 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_HISTEDIT_BUSY);
8769 0ebf8283 2019-07-24 stsp goto done;
8772 0ebf8283 2019-07-24 stsp error = got_ref_open(&branch, repo,
8773 0ebf8283 2019-07-24 stsp got_worktree_get_head_ref_name(worktree), 0);
8774 0ebf8283 2019-07-24 stsp if (error != NULL)
8775 0ebf8283 2019-07-24 stsp goto done;
8777 c7d20a3f 2019-07-30 stsp if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
8778 c7d20a3f 2019-07-30 stsp error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
8779 c7d20a3f 2019-07-30 stsp "will not edit commit history of a branch outside "
8780 c7d20a3f 2019-07-30 stsp "the \"refs/heads/\" reference namespace");
8781 c7d20a3f 2019-07-30 stsp goto done;
8784 0ebf8283 2019-07-24 stsp error = got_ref_resolve(&head_commit_id, repo, branch);
8785 bfce7f83 2019-07-27 stsp got_ref_close(branch);
8786 bfce7f83 2019-07-27 stsp branch = NULL;
8787 0ebf8283 2019-07-24 stsp if (error)
8788 0ebf8283 2019-07-24 stsp goto done;
8790 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo,
8791 0ebf8283 2019-07-24 stsp head_commit_id);
8792 0ebf8283 2019-07-24 stsp if (error)
8793 0ebf8283 2019-07-24 stsp goto done;
8794 0ebf8283 2019-07-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
8795 0ebf8283 2019-07-24 stsp pid = SIMPLEQ_FIRST(parent_ids);
8796 3aac7cf7 2019-07-25 stsp if (pid == NULL) {
8797 3aac7cf7 2019-07-25 stsp error = got_error(GOT_ERR_EMPTY_HISTEDIT);
8798 3aac7cf7 2019-07-25 stsp goto done;
8800 8ca9bd68 2019-07-25 stsp error = collect_commits(&commits, head_commit_id, pid->id,
8801 8ca9bd68 2019-07-25 stsp got_worktree_get_base_commit_id(worktree),
8802 8ca9bd68 2019-07-25 stsp got_worktree_get_path_prefix(worktree),
8803 8ca9bd68 2019-07-25 stsp GOT_ERR_HISTEDIT_PATH, repo);
8804 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
8805 0ebf8283 2019-07-24 stsp commit = NULL;
8806 0ebf8283 2019-07-24 stsp if (error)
8807 0ebf8283 2019-07-24 stsp goto done;
8809 c5996fff 2020-01-29 stsp if (SIMPLEQ_EMPTY(&commits)) {
8810 c5996fff 2020-01-29 stsp error = got_error(GOT_ERR_EMPTY_HISTEDIT);
8811 c5996fff 2020-01-29 stsp goto done;
8814 bfce7f83 2019-07-27 stsp error = got_worktree_histedit_prepare(&tmp_branch, &branch,
8815 bfce7f83 2019-07-27 stsp &base_commit_id, &fileindex, worktree, repo);
8816 bfce7f83 2019-07-27 stsp if (error)
8817 bfce7f83 2019-07-27 stsp goto done;
8819 0ebf8283 2019-07-24 stsp if (edit_script_path) {
8820 0ebf8283 2019-07-24 stsp error = histedit_load_list(&histedit_cmds,
8821 0ebf8283 2019-07-24 stsp edit_script_path, repo);
8822 6ba2bea2 2019-07-27 stsp if (error) {
8823 6ba2bea2 2019-07-27 stsp got_worktree_histedit_abort(worktree, fileindex,
8824 6ba2bea2 2019-07-27 stsp repo, branch, base_commit_id,
8825 9627c110 2020-04-18 stsp update_progress, &upa);
8826 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
8827 0ebf8283 2019-07-24 stsp goto done;
8830 514f2ffe 2020-01-29 stsp const char *branch_name;
8831 514f2ffe 2020-01-29 stsp branch_name = got_ref_get_symref_target(branch);
8832 514f2ffe 2020-01-29 stsp if (strncmp(branch_name, "refs/heads/", 11) == 0)
8833 514f2ffe 2020-01-29 stsp branch_name += 11;
8834 0ebf8283 2019-07-24 stsp error = histedit_edit_script(&histedit_cmds, &commits,
8835 083957f4 2020-02-24 stsp branch_name, edit_logmsg_only, repo);
8836 6ba2bea2 2019-07-27 stsp if (error) {
8837 6ba2bea2 2019-07-27 stsp got_worktree_histedit_abort(worktree, fileindex,
8838 6ba2bea2 2019-07-27 stsp repo, branch, base_commit_id,
8839 9627c110 2020-04-18 stsp update_progress, &upa);
8840 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
8841 0ebf8283 2019-07-24 stsp goto done;
8846 0ebf8283 2019-07-24 stsp error = histedit_save_list(&histedit_cmds, worktree,
8848 6ba2bea2 2019-07-27 stsp if (error) {
8849 6ba2bea2 2019-07-27 stsp got_worktree_histedit_abort(worktree, fileindex,
8850 6ba2bea2 2019-07-27 stsp repo, branch, base_commit_id,
8851 9627c110 2020-04-18 stsp update_progress, &upa);
8852 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
8853 0ebf8283 2019-07-24 stsp goto done;
8858 4ec14e60 2019-08-28 hiltjo error = histedit_check_script(&histedit_cmds, &commits, repo);
8859 4ec14e60 2019-08-28 hiltjo if (error)
8860 0ebf8283 2019-07-24 stsp goto done;
8862 0ebf8283 2019-07-24 stsp TAILQ_FOREACH(hle, &histedit_cmds, entry) {
8863 0ebf8283 2019-07-24 stsp if (resume_commit_id) {
8864 0ebf8283 2019-07-24 stsp if (got_object_id_cmp(hle->commit_id,
8865 0ebf8283 2019-07-24 stsp resume_commit_id) != 0)
8868 0ebf8283 2019-07-24 stsp resume_commit_id = NULL;
8869 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_DROP ||
8870 0ebf8283 2019-07-24 stsp hle->cmd->code == GOT_HISTEDIT_FOLD) {
8871 0ebf8283 2019-07-24 stsp error = histedit_skip_commit(hle, worktree,
8873 ab20a43a 2020-01-29 stsp if (error)
8874 ab20a43a 2020-01-29 stsp goto done;
8876 ab20a43a 2020-01-29 stsp struct got_pathlist_head paths;
8877 ab20a43a 2020-01-29 stsp int have_changes = 0;
8879 ab20a43a 2020-01-29 stsp TAILQ_INIT(&paths);
8880 ab20a43a 2020-01-29 stsp error = got_pathlist_append(&paths, "", NULL);
8881 ab20a43a 2020-01-29 stsp if (error)
8882 ab20a43a 2020-01-29 stsp goto done;
8883 ab20a43a 2020-01-29 stsp error = got_worktree_status(worktree, &paths,
8884 ab20a43a 2020-01-29 stsp repo, check_local_changes, &have_changes,
8885 ab20a43a 2020-01-29 stsp check_cancelled, NULL);
8886 ab20a43a 2020-01-29 stsp got_pathlist_free(&paths);
8887 ab20a43a 2020-01-29 stsp if (error) {
8888 ab20a43a 2020-01-29 stsp if (error->code != GOT_ERR_CANCELLED)
8889 ab20a43a 2020-01-29 stsp goto done;
8890 ab20a43a 2020-01-29 stsp if (sigint_received || sigpipe_received)
8891 ab20a43a 2020-01-29 stsp goto done;
8893 ab20a43a 2020-01-29 stsp if (have_changes) {
8894 ab20a43a 2020-01-29 stsp error = histedit_commit(NULL, worktree,
8895 ab20a43a 2020-01-29 stsp fileindex, tmp_branch, hle, repo);
8896 ab20a43a 2020-01-29 stsp if (error)
8897 ab20a43a 2020-01-29 stsp goto done;
8899 ab20a43a 2020-01-29 stsp error = got_object_open_as_commit(
8900 ab20a43a 2020-01-29 stsp &commit, repo, hle->commit_id);
8901 ab20a43a 2020-01-29 stsp if (error)
8902 ab20a43a 2020-01-29 stsp goto done;
8903 ab20a43a 2020-01-29 stsp error = show_histedit_progress(commit,
8904 ab20a43a 2020-01-29 stsp hle, NULL);
8905 ab20a43a 2020-01-29 stsp got_object_commit_close(commit);
8906 ab20a43a 2020-01-29 stsp commit = NULL;
8907 ab20a43a 2020-01-29 stsp if (error)
8908 ab20a43a 2020-01-29 stsp goto done;
8914 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_DROP) {
8915 0ebf8283 2019-07-24 stsp error = histedit_skip_commit(hle, worktree, repo);
8916 0ebf8283 2019-07-24 stsp if (error)
8917 0ebf8283 2019-07-24 stsp goto done;
8921 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo,
8922 0ebf8283 2019-07-24 stsp hle->commit_id);
8923 0ebf8283 2019-07-24 stsp if (error)
8924 0ebf8283 2019-07-24 stsp goto done;
8925 0ebf8283 2019-07-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
8926 0ebf8283 2019-07-24 stsp pid = SIMPLEQ_FIRST(parent_ids);
8928 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_merge_files(&merged_paths,
8929 3e3a69f1 2019-07-25 stsp worktree, fileindex, pid->id, hle->commit_id, repo,
8930 9627c110 2020-04-18 stsp update_progress, &upa, check_cancelled, NULL);
8931 0ebf8283 2019-07-24 stsp if (error)
8932 0ebf8283 2019-07-24 stsp goto done;
8933 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
8934 0ebf8283 2019-07-24 stsp commit = NULL;
8936 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
8937 9627c110 2020-04-18 stsp if (upa.conflicts > 0)
8938 9627c110 2020-04-18 stsp rebase_status = GOT_STATUS_CONFLICT;
8940 0ebf8283 2019-07-24 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
8941 a0ea4fc0 2020-02-28 stsp error = show_rebase_merge_conflict(hle->commit_id,
8943 a0ea4fc0 2020-02-28 stsp if (error)
8944 a0ea4fc0 2020-02-28 stsp goto done;
8945 0ebf8283 2019-07-24 stsp got_worktree_rebase_pathlist_free(&merged_paths);
8949 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
8950 0ebf8283 2019-07-24 stsp char *id_str;
8951 0ebf8283 2019-07-24 stsp error = got_object_id_str(&id_str, hle->commit_id);
8952 0ebf8283 2019-07-24 stsp if (error)
8953 0ebf8283 2019-07-24 stsp goto done;
8954 0ebf8283 2019-07-24 stsp printf("Stopping histedit for amending commit %s\n",
8956 0ebf8283 2019-07-24 stsp free(id_str);
8957 0ebf8283 2019-07-24 stsp got_worktree_rebase_pathlist_free(&merged_paths);
8958 3e3a69f1 2019-07-25 stsp error = got_worktree_histedit_postpone(worktree,
8959 3e3a69f1 2019-07-25 stsp fileindex);
8960 0ebf8283 2019-07-24 stsp goto done;
8963 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
8964 0ebf8283 2019-07-24 stsp error = histedit_skip_commit(hle, worktree, repo);
8965 0ebf8283 2019-07-24 stsp if (error)
8966 0ebf8283 2019-07-24 stsp goto done;
8970 3e3a69f1 2019-07-25 stsp error = histedit_commit(&merged_paths, worktree, fileindex,
8971 3e3a69f1 2019-07-25 stsp tmp_branch, hle, repo);
8972 0ebf8283 2019-07-24 stsp got_worktree_rebase_pathlist_free(&merged_paths);
8973 0ebf8283 2019-07-24 stsp if (error)
8974 0ebf8283 2019-07-24 stsp goto done;
8977 0ebf8283 2019-07-24 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
8978 3e3a69f1 2019-07-25 stsp error = got_worktree_histedit_postpone(worktree, fileindex);
8979 0ebf8283 2019-07-24 stsp if (error)
8980 0ebf8283 2019-07-24 stsp goto done;
8981 0ebf8283 2019-07-24 stsp error = got_error_msg(GOT_ERR_CONFLICTS,
8982 4cb8f8f3 2020-03-05 stsp "conflicts must be resolved before histedit can continue");
8984 3e3a69f1 2019-07-25 stsp error = histedit_complete(worktree, fileindex, tmp_branch,
8985 3e3a69f1 2019-07-25 stsp branch, repo);
8987 0ebf8283 2019-07-24 stsp got_object_id_queue_free(&commits);
8988 bfce7f83 2019-07-27 stsp histedit_free_list(&histedit_cmds);
8989 0ebf8283 2019-07-24 stsp free(head_commit_id);
8990 0ebf8283 2019-07-24 stsp free(base_commit_id);
8991 0ebf8283 2019-07-24 stsp free(resume_commit_id);
8992 0ebf8283 2019-07-24 stsp if (commit)
8993 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
8994 0ebf8283 2019-07-24 stsp if (branch)
8995 0ebf8283 2019-07-24 stsp got_ref_close(branch);
8996 0ebf8283 2019-07-24 stsp if (tmp_branch)
8997 0ebf8283 2019-07-24 stsp got_ref_close(tmp_branch);
8998 0ebf8283 2019-07-24 stsp if (worktree)
8999 0ebf8283 2019-07-24 stsp got_worktree_close(worktree);
9001 2822a352 2019-10-15 stsp got_repo_close(repo);
9002 2822a352 2019-10-15 stsp return error;
9005 2822a352 2019-10-15 stsp __dead static void
9006 2822a352 2019-10-15 stsp usage_integrate(void)
9008 2822a352 2019-10-15 stsp fprintf(stderr, "usage: %s integrate branch\n", getprogname());
9012 2822a352 2019-10-15 stsp static const struct got_error *
9013 2822a352 2019-10-15 stsp cmd_integrate(int argc, char *argv[])
9015 2822a352 2019-10-15 stsp const struct got_error *error = NULL;
9016 2822a352 2019-10-15 stsp struct got_repository *repo = NULL;
9017 2822a352 2019-10-15 stsp struct got_worktree *worktree = NULL;
9018 2822a352 2019-10-15 stsp char *cwd = NULL, *refname = NULL, *base_refname = NULL;
9019 2822a352 2019-10-15 stsp const char *branch_arg = NULL;
9020 2822a352 2019-10-15 stsp struct got_reference *branch_ref = NULL, *base_branch_ref = NULL;
9021 2822a352 2019-10-15 stsp struct got_fileindex *fileindex = NULL;
9022 2822a352 2019-10-15 stsp struct got_object_id *commit_id = NULL, *base_commit_id = NULL;
9024 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
9026 2822a352 2019-10-15 stsp while ((ch = getopt(argc, argv, "")) != -1) {
9027 2822a352 2019-10-15 stsp switch (ch) {
9029 2822a352 2019-10-15 stsp usage_integrate();
9030 2822a352 2019-10-15 stsp /* NOTREACHED */
9034 2822a352 2019-10-15 stsp argc -= optind;
9035 2822a352 2019-10-15 stsp argv += optind;
9037 2822a352 2019-10-15 stsp if (argc != 1)
9038 2822a352 2019-10-15 stsp usage_integrate();
9039 2822a352 2019-10-15 stsp branch_arg = argv[0];
9040 b08a0ccd 2020-09-24 stsp #ifndef PROFILE
9041 2822a352 2019-10-15 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
9042 2822a352 2019-10-15 stsp "unveil", NULL) == -1)
9043 2822a352 2019-10-15 stsp err(1, "pledge");
9045 2822a352 2019-10-15 stsp cwd = getcwd(NULL, 0);
9046 2822a352 2019-10-15 stsp if (cwd == NULL) {
9047 2822a352 2019-10-15 stsp error = got_error_from_errno("getcwd");
9048 2822a352 2019-10-15 stsp goto done;
9051 2822a352 2019-10-15 stsp error = got_worktree_open(&worktree, cwd);
9052 fa51e947 2020-03-27 stsp if (error) {
9053 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
9054 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "integrate",
9056 2822a352 2019-10-15 stsp goto done;
9059 2822a352 2019-10-15 stsp error = check_rebase_or_histedit_in_progress(worktree);
9060 2822a352 2019-10-15 stsp if (error)
9061 2822a352 2019-10-15 stsp goto done;
9063 2822a352 2019-10-15 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
9065 2822a352 2019-10-15 stsp if (error != NULL)
9066 2822a352 2019-10-15 stsp goto done;
9068 2822a352 2019-10-15 stsp error = apply_unveil(got_repo_get_path(repo), 0,
9069 2822a352 2019-10-15 stsp got_worktree_get_root_path(worktree));
9070 2822a352 2019-10-15 stsp if (error)
9071 2822a352 2019-10-15 stsp goto done;
9073 2822a352 2019-10-15 stsp if (asprintf(&refname, "refs/heads/%s", branch_arg) == -1) {
9074 2822a352 2019-10-15 stsp error = got_error_from_errno("asprintf");
9075 2822a352 2019-10-15 stsp goto done;
9078 2822a352 2019-10-15 stsp error = got_worktree_integrate_prepare(&fileindex, &branch_ref,
9079 2822a352 2019-10-15 stsp &base_branch_ref, worktree, refname, repo);
9080 2822a352 2019-10-15 stsp if (error)
9081 2822a352 2019-10-15 stsp goto done;
9083 2822a352 2019-10-15 stsp refname = strdup(got_ref_get_name(branch_ref));
9084 2822a352 2019-10-15 stsp if (refname == NULL) {
9085 2822a352 2019-10-15 stsp error = got_error_from_errno("strdup");
9086 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
9087 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
9088 2822a352 2019-10-15 stsp goto done;
9090 2822a352 2019-10-15 stsp base_refname = strdup(got_ref_get_name(base_branch_ref));
9091 2822a352 2019-10-15 stsp if (base_refname == NULL) {
9092 2822a352 2019-10-15 stsp error = got_error_from_errno("strdup");
9093 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
9094 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
9095 2822a352 2019-10-15 stsp goto done;
9098 2822a352 2019-10-15 stsp error = got_ref_resolve(&commit_id, repo, branch_ref);
9099 2822a352 2019-10-15 stsp if (error)
9100 2822a352 2019-10-15 stsp goto done;
9102 2822a352 2019-10-15 stsp error = got_ref_resolve(&base_commit_id, repo, base_branch_ref);
9103 2822a352 2019-10-15 stsp if (error)
9104 2822a352 2019-10-15 stsp goto done;
9106 2822a352 2019-10-15 stsp if (got_object_id_cmp(commit_id, base_commit_id) == 0) {
9107 2822a352 2019-10-15 stsp error = got_error_msg(GOT_ERR_SAME_BRANCH,
9108 2822a352 2019-10-15 stsp "specified branch has already been integrated");
9109 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
9110 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
9111 2822a352 2019-10-15 stsp goto done;
9114 3aef623b 2019-10-15 stsp error = check_linear_ancestry(commit_id, base_commit_id, 1, repo);
9115 2822a352 2019-10-15 stsp if (error) {
9116 2822a352 2019-10-15 stsp if (error->code == GOT_ERR_ANCESTRY)
9117 2822a352 2019-10-15 stsp error = got_error(GOT_ERR_REBASE_REQUIRED);
9118 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
9119 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
9120 2822a352 2019-10-15 stsp goto done;
9123 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
9124 2822a352 2019-10-15 stsp error = got_worktree_integrate_continue(worktree, fileindex, repo,
9125 9627c110 2020-04-18 stsp branch_ref, base_branch_ref, update_progress, &upa,
9126 2822a352 2019-10-15 stsp check_cancelled, NULL);
9127 2822a352 2019-10-15 stsp if (error)
9128 2822a352 2019-10-15 stsp goto done;
9130 2822a352 2019-10-15 stsp printf("Integrated %s into %s\n", refname, base_refname);
9131 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
9134 715dc77e 2019-08-03 stsp got_repo_close(repo);
9135 2822a352 2019-10-15 stsp if (worktree)
9136 2822a352 2019-10-15 stsp got_worktree_close(worktree);
9137 2822a352 2019-10-15 stsp free(cwd);
9138 2822a352 2019-10-15 stsp free(base_commit_id);
9139 2822a352 2019-10-15 stsp free(commit_id);
9140 2822a352 2019-10-15 stsp free(refname);
9141 2822a352 2019-10-15 stsp free(base_refname);
9142 715dc77e 2019-08-03 stsp return error;
9145 715dc77e 2019-08-03 stsp __dead static void
9146 715dc77e 2019-08-03 stsp usage_stage(void)
9148 f3055044 2019-08-07 stsp fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
9149 35213c7c 2020-07-23 stsp "[-S] [file-path ...]\n",
9150 715dc77e 2019-08-03 stsp getprogname());
9154 715dc77e 2019-08-03 stsp static const struct got_error *
9155 408b4ebc 2019-08-03 stsp print_stage(void *arg, unsigned char status, unsigned char staged_status,
9156 408b4ebc 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
9157 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
9158 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
9160 408b4ebc 2019-08-03 stsp const struct got_error *err = NULL;
9161 408b4ebc 2019-08-03 stsp char *id_str = NULL;
9163 408b4ebc 2019-08-03 stsp if (staged_status != GOT_STATUS_ADD &&
9164 408b4ebc 2019-08-03 stsp staged_status != GOT_STATUS_MODIFY &&
9165 408b4ebc 2019-08-03 stsp staged_status != GOT_STATUS_DELETE)
9166 408b4ebc 2019-08-03 stsp return NULL;
9168 408b4ebc 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
9169 408b4ebc 2019-08-03 stsp staged_status == GOT_STATUS_MODIFY)
9170 408b4ebc 2019-08-03 stsp err = got_object_id_str(&id_str, staged_blob_id);
9172 408b4ebc 2019-08-03 stsp err = got_object_id_str(&id_str, blob_id);
9174 408b4ebc 2019-08-03 stsp return err;
9176 408b4ebc 2019-08-03 stsp printf("%s %c %s\n", id_str, staged_status, path);
9177 408b4ebc 2019-08-03 stsp free(id_str);
9178 dc424a06 2019-08-07 stsp return NULL;
9181 dc424a06 2019-08-07 stsp static const struct got_error *
9182 715dc77e 2019-08-03 stsp cmd_stage(int argc, char *argv[])
9184 715dc77e 2019-08-03 stsp const struct got_error *error = NULL;
9185 715dc77e 2019-08-03 stsp struct got_repository *repo = NULL;
9186 715dc77e 2019-08-03 stsp struct got_worktree *worktree = NULL;
9187 715dc77e 2019-08-03 stsp char *cwd = NULL;
9188 715dc77e 2019-08-03 stsp struct got_pathlist_head paths;
9189 715dc77e 2019-08-03 stsp struct got_pathlist_entry *pe;
9190 35213c7c 2020-07-23 stsp int ch, list_stage = 0, pflag = 0, allow_bad_symlinks = 0;
9191 dc424a06 2019-08-07 stsp FILE *patch_script_file = NULL;
9192 2e1f37b0 2019-08-08 stsp const char *patch_script_path = NULL;
9193 2e1f37b0 2019-08-08 stsp struct choose_patch_arg cpa;
9195 715dc77e 2019-08-03 stsp TAILQ_INIT(&paths);
9197 35213c7c 2020-07-23 stsp while ((ch = getopt(argc, argv, "lpF:S")) != -1) {
9198 715dc77e 2019-08-03 stsp switch (ch) {
9200 408b4ebc 2019-08-03 stsp list_stage = 1;
9203 dc424a06 2019-08-07 stsp pflag = 1;
9206 dc424a06 2019-08-07 stsp patch_script_path = optarg;
9209 35213c7c 2020-07-23 stsp allow_bad_symlinks = 1;
9212 715dc77e 2019-08-03 stsp usage_stage();
9213 715dc77e 2019-08-03 stsp /* NOTREACHED */
9217 715dc77e 2019-08-03 stsp argc -= optind;
9218 715dc77e 2019-08-03 stsp argv += optind;
9220 715dc77e 2019-08-03 stsp #ifndef PROFILE
9221 715dc77e 2019-08-03 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
9222 715dc77e 2019-08-03 stsp "unveil", NULL) == -1)
9223 715dc77e 2019-08-03 stsp err(1, "pledge");
9225 2db2652d 2019-08-07 stsp if (list_stage && (pflag || patch_script_path))
9226 2db2652d 2019-08-07 stsp errx(1, "-l option cannot be used with other options");
9227 dc424a06 2019-08-07 stsp if (patch_script_path && !pflag)
9228 dc424a06 2019-08-07 stsp errx(1, "-F option can only be used together with -p option");
9230 715dc77e 2019-08-03 stsp cwd = getcwd(NULL, 0);
9231 715dc77e 2019-08-03 stsp if (cwd == NULL) {
9232 715dc77e 2019-08-03 stsp error = got_error_from_errno("getcwd");
9233 715dc77e 2019-08-03 stsp goto done;
9236 715dc77e 2019-08-03 stsp error = got_worktree_open(&worktree, cwd);
9237 fa51e947 2020-03-27 stsp if (error) {
9238 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
9239 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "stage", cwd);
9240 715dc77e 2019-08-03 stsp goto done;
9243 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
9245 715dc77e 2019-08-03 stsp if (error != NULL)
9246 715dc77e 2019-08-03 stsp goto done;
9248 dc424a06 2019-08-07 stsp if (patch_script_path) {
9249 dc424a06 2019-08-07 stsp patch_script_file = fopen(patch_script_path, "r");
9250 dc424a06 2019-08-07 stsp if (patch_script_file == NULL) {
9251 dc424a06 2019-08-07 stsp error = got_error_from_errno2("fopen",
9252 dc424a06 2019-08-07 stsp patch_script_path);
9253 dc424a06 2019-08-07 stsp goto done;
9256 9fd7cd22 2019-08-30 stsp error = apply_unveil(got_repo_get_path(repo), 0,
9257 715dc77e 2019-08-03 stsp got_worktree_get_root_path(worktree));
9258 715dc77e 2019-08-03 stsp if (error)
9259 715dc77e 2019-08-03 stsp goto done;
9261 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
9262 6d022e97 2019-08-04 stsp if (error)
9263 6d022e97 2019-08-04 stsp goto done;
9265 6d022e97 2019-08-04 stsp if (list_stage)
9266 408b4ebc 2019-08-03 stsp error = got_worktree_status(worktree, &paths, repo,
9267 408b4ebc 2019-08-03 stsp print_stage, NULL, check_cancelled, NULL);
9269 2e1f37b0 2019-08-08 stsp cpa.patch_script_file = patch_script_file;
9270 2e1f37b0 2019-08-08 stsp cpa.action = "stage";
9271 dc424a06 2019-08-07 stsp error = got_worktree_stage(worktree, &paths,
9272 dc424a06 2019-08-07 stsp pflag ? NULL : print_status, NULL,
9273 35213c7c 2020-07-23 stsp pflag ? choose_patch : NULL, &cpa,
9274 35213c7c 2020-07-23 stsp allow_bad_symlinks, repo);
9277 2e1f37b0 2019-08-08 stsp if (patch_script_file && fclose(patch_script_file) == EOF &&
9278 2e1f37b0 2019-08-08 stsp error == NULL)
9279 2e1f37b0 2019-08-08 stsp error = got_error_from_errno2("fclose", patch_script_path);
9281 ad493afc 2019-08-03 stsp got_repo_close(repo);
9282 ad493afc 2019-08-03 stsp if (worktree)
9283 ad493afc 2019-08-03 stsp got_worktree_close(worktree);
9284 ad493afc 2019-08-03 stsp TAILQ_FOREACH(pe, &paths, entry)
9285 ad493afc 2019-08-03 stsp free((char *)pe->path);
9286 ad493afc 2019-08-03 stsp got_pathlist_free(&paths);
9287 ad493afc 2019-08-03 stsp free(cwd);
9288 ad493afc 2019-08-03 stsp return error;
9291 ad493afc 2019-08-03 stsp __dead static void
9292 ad493afc 2019-08-03 stsp usage_unstage(void)
9294 2e1f37b0 2019-08-08 stsp fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
9295 2e1f37b0 2019-08-08 stsp "[file-path ...]\n",
9296 ad493afc 2019-08-03 stsp getprogname());
9301 ad493afc 2019-08-03 stsp static const struct got_error *
9302 ad493afc 2019-08-03 stsp cmd_unstage(int argc, char *argv[])
9304 ad493afc 2019-08-03 stsp const struct got_error *error = NULL;
9305 ad493afc 2019-08-03 stsp struct got_repository *repo = NULL;
9306 ad493afc 2019-08-03 stsp struct got_worktree *worktree = NULL;
9307 ad493afc 2019-08-03 stsp char *cwd = NULL;
9308 ad493afc 2019-08-03 stsp struct got_pathlist_head paths;
9309 ad493afc 2019-08-03 stsp struct got_pathlist_entry *pe;
9310 9627c110 2020-04-18 stsp int ch, pflag = 0;
9311 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
9312 2e1f37b0 2019-08-08 stsp FILE *patch_script_file = NULL;
9313 2e1f37b0 2019-08-08 stsp const char *patch_script_path = NULL;
9314 2e1f37b0 2019-08-08 stsp struct choose_patch_arg cpa;
9316 ad493afc 2019-08-03 stsp TAILQ_INIT(&paths);
9318 2e1f37b0 2019-08-08 stsp while ((ch = getopt(argc, argv, "pF:")) != -1) {
9319 ad493afc 2019-08-03 stsp switch (ch) {
9321 2e1f37b0 2019-08-08 stsp pflag = 1;
9324 2e1f37b0 2019-08-08 stsp patch_script_path = optarg;
9327 ad493afc 2019-08-03 stsp usage_unstage();
9328 ad493afc 2019-08-03 stsp /* NOTREACHED */
9332 ad493afc 2019-08-03 stsp argc -= optind;
9333 ad493afc 2019-08-03 stsp argv += optind;
9335 ad493afc 2019-08-03 stsp #ifndef PROFILE
9336 ad493afc 2019-08-03 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
9337 ad493afc 2019-08-03 stsp "unveil", NULL) == -1)
9338 ad493afc 2019-08-03 stsp err(1, "pledge");
9340 2e1f37b0 2019-08-08 stsp if (patch_script_path && !pflag)
9341 2e1f37b0 2019-08-08 stsp errx(1, "-F option can only be used together with -p option");
9343 ad493afc 2019-08-03 stsp cwd = getcwd(NULL, 0);
9344 ad493afc 2019-08-03 stsp if (cwd == NULL) {
9345 ad493afc 2019-08-03 stsp error = got_error_from_errno("getcwd");
9346 ad493afc 2019-08-03 stsp goto done;
9349 ad493afc 2019-08-03 stsp error = got_worktree_open(&worktree, cwd);
9350 fa51e947 2020-03-27 stsp if (error) {
9351 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
9352 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "unstage", cwd);
9353 ad493afc 2019-08-03 stsp goto done;
9356 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
9358 ad493afc 2019-08-03 stsp if (error != NULL)
9359 ad493afc 2019-08-03 stsp goto done;
9361 2e1f37b0 2019-08-08 stsp if (patch_script_path) {
9362 2e1f37b0 2019-08-08 stsp patch_script_file = fopen(patch_script_path, "r");
9363 2e1f37b0 2019-08-08 stsp if (patch_script_file == NULL) {
9364 2e1f37b0 2019-08-08 stsp error = got_error_from_errno2("fopen",
9365 2e1f37b0 2019-08-08 stsp patch_script_path);
9366 2e1f37b0 2019-08-08 stsp goto done;
9370 00f36e47 2019-09-06 stsp error = apply_unveil(got_repo_get_path(repo), 0,
9371 ad493afc 2019-08-03 stsp got_worktree_get_root_path(worktree));
9372 ad493afc 2019-08-03 stsp if (error)
9373 ad493afc 2019-08-03 stsp goto done;
9375 ad493afc 2019-08-03 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
9376 ad493afc 2019-08-03 stsp if (error)
9377 ad493afc 2019-08-03 stsp goto done;
9379 2e1f37b0 2019-08-08 stsp cpa.patch_script_file = patch_script_file;
9380 2e1f37b0 2019-08-08 stsp cpa.action = "unstage";
9381 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
9382 ad493afc 2019-08-03 stsp error = got_worktree_unstage(worktree, &paths, update_progress,
9383 9627c110 2020-04-18 stsp &upa, pflag ? choose_patch : NULL, &cpa, repo);
9384 9627c110 2020-04-18 stsp if (!error)
9385 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
9387 2e1f37b0 2019-08-08 stsp if (patch_script_file && fclose(patch_script_file) == EOF &&
9388 2e1f37b0 2019-08-08 stsp error == NULL)
9389 2e1f37b0 2019-08-08 stsp error = got_error_from_errno2("fclose", patch_script_path);
9391 0ebf8283 2019-07-24 stsp got_repo_close(repo);
9392 715dc77e 2019-08-03 stsp if (worktree)
9393 715dc77e 2019-08-03 stsp got_worktree_close(worktree);
9394 715dc77e 2019-08-03 stsp TAILQ_FOREACH(pe, &paths, entry)
9395 715dc77e 2019-08-03 stsp free((char *)pe->path);
9396 715dc77e 2019-08-03 stsp got_pathlist_free(&paths);
9397 715dc77e 2019-08-03 stsp free(cwd);
9398 01073a5d 2019-08-22 stsp return error;
9401 01073a5d 2019-08-22 stsp __dead static void
9402 01073a5d 2019-08-22 stsp usage_cat(void)
9404 896e9b6f 2019-08-26 stsp fprintf(stderr, "usage: %s cat [-r repository ] [ -c commit ] [ -P ] "
9405 896e9b6f 2019-08-26 stsp "arg1 [arg2 ...]\n", getprogname());
9409 01073a5d 2019-08-22 stsp static const struct got_error *
9410 01073a5d 2019-08-22 stsp cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
9412 01073a5d 2019-08-22 stsp const struct got_error *err;
9413 01073a5d 2019-08-22 stsp struct got_blob_object *blob;
9415 01073a5d 2019-08-22 stsp err = got_object_open_as_blob(&blob, repo, id, 8192);
9417 01073a5d 2019-08-22 stsp return err;
9419 01073a5d 2019-08-22 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
9420 01073a5d 2019-08-22 stsp got_object_blob_close(blob);
9421 01073a5d 2019-08-22 stsp return err;
9424 01073a5d 2019-08-22 stsp static const struct got_error *
9425 01073a5d 2019-08-22 stsp cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
9427 01073a5d 2019-08-22 stsp const struct got_error *err;
9428 01073a5d 2019-08-22 stsp struct got_tree_object *tree;
9429 56e0773d 2019-11-28 stsp int nentries, i;
9431 01073a5d 2019-08-22 stsp err = got_object_open_as_tree(&tree, repo, id);
9433 01073a5d 2019-08-22 stsp return err;
9435 56e0773d 2019-11-28 stsp nentries = got_object_tree_get_nentries(tree);
9436 56e0773d 2019-11-28 stsp for (i = 0; i < nentries; i++) {
9437 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
9438 01073a5d 2019-08-22 stsp char *id_str;
9439 01073a5d 2019-08-22 stsp if (sigint_received || sigpipe_received)
9441 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(tree, i);
9442 56e0773d 2019-11-28 stsp err = got_object_id_str(&id_str, got_tree_entry_get_id(te));
9445 56e0773d 2019-11-28 stsp fprintf(outfile, "%s %.7o %s\n", id_str,
9446 56e0773d 2019-11-28 stsp got_tree_entry_get_mode(te),
9447 56e0773d 2019-11-28 stsp got_tree_entry_get_name(te));
9448 01073a5d 2019-08-22 stsp free(id_str);
9451 01073a5d 2019-08-22 stsp got_object_tree_close(tree);
9452 01073a5d 2019-08-22 stsp return err;
9455 01073a5d 2019-08-22 stsp static const struct got_error *
9456 01073a5d 2019-08-22 stsp cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
9458 01073a5d 2019-08-22 stsp const struct got_error *err;
9459 01073a5d 2019-08-22 stsp struct got_commit_object *commit;
9460 01073a5d 2019-08-22 stsp const struct got_object_id_queue *parent_ids;
9461 01073a5d 2019-08-22 stsp struct got_object_qid *pid;
9462 01073a5d 2019-08-22 stsp char *id_str = NULL;
9463 24ea5512 2019-08-22 stsp const char *logmsg = NULL;
9465 01073a5d 2019-08-22 stsp err = got_object_open_as_commit(&commit, repo, id);
9467 01073a5d 2019-08-22 stsp return err;
9469 01073a5d 2019-08-22 stsp err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
9471 01073a5d 2019-08-22 stsp goto done;
9473 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str);
9474 01073a5d 2019-08-22 stsp parent_ids = got_object_commit_get_parent_ids(commit);
9475 8aa93786 2019-08-22 stsp fprintf(outfile, "numparents %d\n",
9476 01073a5d 2019-08-22 stsp got_object_commit_get_nparents(commit));
9477 01073a5d 2019-08-22 stsp SIMPLEQ_FOREACH(pid, parent_ids, entry) {
9478 01073a5d 2019-08-22 stsp char *pid_str;
9479 01073a5d 2019-08-22 stsp err = got_object_id_str(&pid_str, pid->id);
9481 01073a5d 2019-08-22 stsp goto done;
9482 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_PARENT, pid_str);
9483 01073a5d 2019-08-22 stsp free(pid_str);
9485 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_AUTHOR,
9486 8aa93786 2019-08-22 stsp got_object_commit_get_author(commit),
9487 1367695b 2020-09-26 naddy (long long)got_object_commit_get_author_time(commit));
9489 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_COMMITTER,
9490 8aa93786 2019-08-22 stsp got_object_commit_get_author(commit),
9491 1367695b 2020-09-26 naddy (long long)got_object_commit_get_committer_time(commit));
9493 24ea5512 2019-08-22 stsp logmsg = got_object_commit_get_logmsg_raw(commit);
9494 8aa93786 2019-08-22 stsp fprintf(outfile, "messagelen %zd\n", strlen(logmsg));
9495 01073a5d 2019-08-22 stsp fprintf(outfile, "%s", logmsg);
9497 01073a5d 2019-08-22 stsp free(id_str);
9498 01073a5d 2019-08-22 stsp got_object_commit_close(commit);
9499 01073a5d 2019-08-22 stsp return err;
9502 01073a5d 2019-08-22 stsp static const struct got_error *
9503 01073a5d 2019-08-22 stsp cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
9505 01073a5d 2019-08-22 stsp const struct got_error *err;
9506 01073a5d 2019-08-22 stsp struct got_tag_object *tag;
9507 01073a5d 2019-08-22 stsp char *id_str = NULL;
9508 01073a5d 2019-08-22 stsp const char *tagmsg = NULL;
9510 01073a5d 2019-08-22 stsp err = got_object_open_as_tag(&tag, repo, id);
9512 01073a5d 2019-08-22 stsp return err;
9514 01073a5d 2019-08-22 stsp err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
9516 01073a5d 2019-08-22 stsp goto done;
9518 e15d5241 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str);
9520 01073a5d 2019-08-22 stsp switch (got_object_tag_get_object_type(tag)) {
9521 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_BLOB:
9522 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
9523 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_BLOB);
9525 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TREE:
9526 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
9527 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_TREE);
9529 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_COMMIT:
9530 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
9531 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_COMMIT);
9533 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TAG:
9534 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
9535 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_TAG);
9541 e15d5241 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TAG,
9542 e15d5241 2019-08-22 stsp got_object_tag_get_name(tag));
9544 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s %lld +0000\n", GOT_TAG_LABEL_TAGGER,
9545 8aa93786 2019-08-22 stsp got_object_tag_get_tagger(tag),
9546 1367695b 2020-09-26 naddy (long long)got_object_tag_get_tagger_time(tag));
9548 01073a5d 2019-08-22 stsp tagmsg = got_object_tag_get_message(tag);
9549 8aa93786 2019-08-22 stsp fprintf(outfile, "messagelen %zd\n", strlen(tagmsg));
9550 01073a5d 2019-08-22 stsp fprintf(outfile, "%s", tagmsg);
9552 01073a5d 2019-08-22 stsp free(id_str);
9553 01073a5d 2019-08-22 stsp got_object_tag_close(tag);
9554 01073a5d 2019-08-22 stsp return err;
9557 01073a5d 2019-08-22 stsp static const struct got_error *
9558 01073a5d 2019-08-22 stsp cmd_cat(int argc, char *argv[])
9560 01073a5d 2019-08-22 stsp const struct got_error *error;
9561 01073a5d 2019-08-22 stsp struct got_repository *repo = NULL;
9562 01073a5d 2019-08-22 stsp struct got_worktree *worktree = NULL;
9563 01073a5d 2019-08-22 stsp char *cwd = NULL, *repo_path = NULL, *label = NULL;
9564 896e9b6f 2019-08-26 stsp const char *commit_id_str = NULL;
9565 896e9b6f 2019-08-26 stsp struct got_object_id *id = NULL, *commit_id = NULL;
9566 896e9b6f 2019-08-26 stsp int ch, obj_type, i, force_path = 0;
9568 01073a5d 2019-08-22 stsp #ifndef PROFILE
9569 01073a5d 2019-08-22 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
9570 01073a5d 2019-08-22 stsp NULL) == -1)
9571 01073a5d 2019-08-22 stsp err(1, "pledge");
9574 896e9b6f 2019-08-26 stsp while ((ch = getopt(argc, argv, "c:r:P")) != -1) {
9575 01073a5d 2019-08-22 stsp switch (ch) {
9577 896e9b6f 2019-08-26 stsp commit_id_str = optarg;
9580 01073a5d 2019-08-22 stsp repo_path = realpath(optarg, NULL);
9581 01073a5d 2019-08-22 stsp if (repo_path == NULL)
9582 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
9584 01073a5d 2019-08-22 stsp got_path_strip_trailing_slashes(repo_path);
9587 896e9b6f 2019-08-26 stsp force_path = 1;
9590 01073a5d 2019-08-22 stsp usage_cat();
9591 01073a5d 2019-08-22 stsp /* NOTREACHED */
9595 01073a5d 2019-08-22 stsp argc -= optind;
9596 01073a5d 2019-08-22 stsp argv += optind;
9598 01073a5d 2019-08-22 stsp cwd = getcwd(NULL, 0);
9599 01073a5d 2019-08-22 stsp if (cwd == NULL) {
9600 01073a5d 2019-08-22 stsp error = got_error_from_errno("getcwd");
9601 01073a5d 2019-08-22 stsp goto done;
9603 01073a5d 2019-08-22 stsp error = got_worktree_open(&worktree, cwd);
9604 01073a5d 2019-08-22 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
9605 01073a5d 2019-08-22 stsp goto done;
9606 01073a5d 2019-08-22 stsp if (worktree) {
9607 01073a5d 2019-08-22 stsp if (repo_path == NULL) {
9608 01073a5d 2019-08-22 stsp repo_path = strdup(
9609 01073a5d 2019-08-22 stsp got_worktree_get_repo_path(worktree));
9610 01073a5d 2019-08-22 stsp if (repo_path == NULL) {
9611 01073a5d 2019-08-22 stsp error = got_error_from_errno("strdup");
9612 01073a5d 2019-08-22 stsp goto done;
9617 01073a5d 2019-08-22 stsp if (repo_path == NULL) {
9618 01073a5d 2019-08-22 stsp repo_path = getcwd(NULL, 0);
9619 01073a5d 2019-08-22 stsp if (repo_path == NULL)
9620 01073a5d 2019-08-22 stsp return got_error_from_errno("getcwd");
9623 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
9624 01073a5d 2019-08-22 stsp free(repo_path);
9625 01073a5d 2019-08-22 stsp if (error != NULL)
9626 01073a5d 2019-08-22 stsp goto done;
9628 01073a5d 2019-08-22 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
9629 896e9b6f 2019-08-26 stsp if (error)
9630 896e9b6f 2019-08-26 stsp goto done;
9632 896e9b6f 2019-08-26 stsp if (commit_id_str == NULL)
9633 896e9b6f 2019-08-26 stsp commit_id_str = GOT_REF_HEAD;
9634 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
9635 71a27632 2020-01-15 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
9636 01073a5d 2019-08-22 stsp if (error)
9637 01073a5d 2019-08-22 stsp goto done;
9639 01073a5d 2019-08-22 stsp for (i = 0; i < argc; i++) {
9640 896e9b6f 2019-08-26 stsp if (force_path) {
9641 896e9b6f 2019-08-26 stsp error = got_object_id_by_path(&id, repo, commit_id,
9643 896e9b6f 2019-08-26 stsp if (error)
9646 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&id, &label, argv[i],
9647 896e9b6f 2019-08-26 stsp GOT_OBJ_TYPE_ANY, 0, repo);
9648 896e9b6f 2019-08-26 stsp if (error) {
9649 896e9b6f 2019-08-26 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR &&
9650 896e9b6f 2019-08-26 stsp error->code != GOT_ERR_NOT_REF)
9652 896e9b6f 2019-08-26 stsp error = got_object_id_by_path(&id, repo,
9653 896e9b6f 2019-08-26 stsp commit_id, argv[i]);
9654 896e9b6f 2019-08-26 stsp if (error)
9659 01073a5d 2019-08-22 stsp error = got_object_get_type(&obj_type, repo, id);
9660 01073a5d 2019-08-22 stsp if (error)
9663 01073a5d 2019-08-22 stsp switch (obj_type) {
9664 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_BLOB:
9665 01073a5d 2019-08-22 stsp error = cat_blob(id, repo, stdout);
9667 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TREE:
9668 01073a5d 2019-08-22 stsp error = cat_tree(id, repo, stdout);
9670 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_COMMIT:
9671 01073a5d 2019-08-22 stsp error = cat_commit(id, repo, stdout);
9673 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TAG:
9674 01073a5d 2019-08-22 stsp error = cat_tag(id, repo, stdout);
9677 01073a5d 2019-08-22 stsp error = got_error(GOT_ERR_OBJ_TYPE);
9680 01073a5d 2019-08-22 stsp if (error)
9682 01073a5d 2019-08-22 stsp free(label);
9683 01073a5d 2019-08-22 stsp label = NULL;
9685 01073a5d 2019-08-22 stsp id = NULL;
9688 01073a5d 2019-08-22 stsp free(label);
9690 896e9b6f 2019-08-26 stsp free(commit_id);
9691 01073a5d 2019-08-22 stsp if (worktree)
9692 01073a5d 2019-08-22 stsp got_worktree_close(worktree);
9693 01073a5d 2019-08-22 stsp if (repo) {
9694 01073a5d 2019-08-22 stsp const struct got_error *repo_error;
9695 01073a5d 2019-08-22 stsp repo_error = got_repo_close(repo);
9696 01073a5d 2019-08-22 stsp if (error == NULL)
9697 01073a5d 2019-08-22 stsp error = repo_error;
9699 b2118c49 2020-07-28 stsp return error;
9702 b2118c49 2020-07-28 stsp __dead static void
9703 b2118c49 2020-07-28 stsp usage_info(void)
9705 b2118c49 2020-07-28 stsp fprintf(stderr, "usage: %s info [path ...]\n",
9706 b2118c49 2020-07-28 stsp getprogname());
9710 b2118c49 2020-07-28 stsp static const struct got_error *
9711 b2118c49 2020-07-28 stsp print_path_info(void *arg, const char *path, mode_t mode, time_t mtime,
9712 b2118c49 2020-07-28 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
9713 b2118c49 2020-07-28 stsp struct got_object_id *commit_id)
9715 b2118c49 2020-07-28 stsp const struct got_error *err = NULL;
9716 b2118c49 2020-07-28 stsp char *id_str = NULL;
9717 b2118c49 2020-07-28 stsp char datebuf[128];
9718 b2118c49 2020-07-28 stsp struct tm mytm, *tm;
9719 b2118c49 2020-07-28 stsp struct got_pathlist_head *paths = arg;
9720 b2118c49 2020-07-28 stsp struct got_pathlist_entry *pe;
9723 b2118c49 2020-07-28 stsp * Clear error indication from any of the path arguments which
9724 b2118c49 2020-07-28 stsp * would cause this file index entry to be displayed.
9726 b2118c49 2020-07-28 stsp TAILQ_FOREACH(pe, paths, entry) {
9727 b2118c49 2020-07-28 stsp if (got_path_cmp(path, pe->path, strlen(path),
9728 b2118c49 2020-07-28 stsp pe->path_len) == 0 ||
9729 b2118c49 2020-07-28 stsp got_path_is_child(path, pe->path, pe->path_len))
9730 b2118c49 2020-07-28 stsp pe->data = NULL; /* no error */
9733 b2118c49 2020-07-28 stsp printf(GOT_COMMIT_SEP_STR);
9734 b2118c49 2020-07-28 stsp if (S_ISLNK(mode))
9735 b2118c49 2020-07-28 stsp printf("symlink: %s\n", path);
9736 b2118c49 2020-07-28 stsp else if (S_ISREG(mode)) {
9737 b2118c49 2020-07-28 stsp printf("file: %s\n", path);
9738 b2118c49 2020-07-28 stsp printf("mode: %o\n", mode & (S_IRWXU | S_IRWXG | S_IRWXO));
9739 b2118c49 2020-07-28 stsp } else if (S_ISDIR(mode))
9740 b2118c49 2020-07-28 stsp printf("directory: %s\n", path);
9742 b2118c49 2020-07-28 stsp printf("something: %s\n", path);
9744 b2118c49 2020-07-28 stsp tm = localtime_r(&mtime, &mytm);
9745 b2118c49 2020-07-28 stsp if (tm == NULL)
9746 b2118c49 2020-07-28 stsp return NULL;
9747 b2118c49 2020-07-28 stsp if (strftime(datebuf, sizeof(datebuf), "%c %Z", tm) >= sizeof(datebuf))
9748 b2118c49 2020-07-28 stsp return got_error(GOT_ERR_NO_SPACE);
9749 b2118c49 2020-07-28 stsp printf("timestamp: %s\n", datebuf);
9751 b2118c49 2020-07-28 stsp if (blob_id) {
9752 b2118c49 2020-07-28 stsp err = got_object_id_str(&id_str, blob_id);
9754 b2118c49 2020-07-28 stsp return err;
9755 b2118c49 2020-07-28 stsp printf("based on blob: %s\n", id_str);
9756 b2118c49 2020-07-28 stsp free(id_str);
9759 b2118c49 2020-07-28 stsp if (staged_blob_id) {
9760 b2118c49 2020-07-28 stsp err = got_object_id_str(&id_str, staged_blob_id);
9762 b2118c49 2020-07-28 stsp return err;
9763 b2118c49 2020-07-28 stsp printf("based on staged blob: %s\n", id_str);
9764 b2118c49 2020-07-28 stsp free(id_str);
9767 b2118c49 2020-07-28 stsp if (commit_id) {
9768 b2118c49 2020-07-28 stsp err = got_object_id_str(&id_str, commit_id);
9770 b2118c49 2020-07-28 stsp return err;
9771 b2118c49 2020-07-28 stsp printf("based on commit: %s\n", id_str);
9772 b2118c49 2020-07-28 stsp free(id_str);
9775 b2118c49 2020-07-28 stsp return NULL;
9778 b2118c49 2020-07-28 stsp static const struct got_error *
9779 b2118c49 2020-07-28 stsp cmd_info(int argc, char *argv[])
9781 b2118c49 2020-07-28 stsp const struct got_error *error = NULL;
9782 b2118c49 2020-07-28 stsp struct got_worktree *worktree = NULL;
9783 b2118c49 2020-07-28 stsp char *cwd = NULL, *id_str = NULL;
9784 b2118c49 2020-07-28 stsp struct got_pathlist_head paths;
9785 b2118c49 2020-07-28 stsp struct got_pathlist_entry *pe;
9786 b2118c49 2020-07-28 stsp char *uuidstr = NULL;
9787 b2118c49 2020-07-28 stsp int ch, show_files = 0;
9789 b2118c49 2020-07-28 stsp TAILQ_INIT(&paths);
9791 b2118c49 2020-07-28 stsp while ((ch = getopt(argc, argv, "")) != -1) {
9792 b2118c49 2020-07-28 stsp switch (ch) {
9794 b2118c49 2020-07-28 stsp usage_info();
9795 b2118c49 2020-07-28 stsp /* NOTREACHED */
9799 b2118c49 2020-07-28 stsp argc -= optind;
9800 b2118c49 2020-07-28 stsp argv += optind;
9802 b2118c49 2020-07-28 stsp #ifndef PROFILE
9803 b2118c49 2020-07-28 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
9804 b2118c49 2020-07-28 stsp NULL) == -1)
9805 b2118c49 2020-07-28 stsp err(1, "pledge");
9807 b2118c49 2020-07-28 stsp cwd = getcwd(NULL, 0);
9808 b2118c49 2020-07-28 stsp if (cwd == NULL) {
9809 b2118c49 2020-07-28 stsp error = got_error_from_errno("getcwd");
9810 b2118c49 2020-07-28 stsp goto done;
9813 b2118c49 2020-07-28 stsp error = got_worktree_open(&worktree, cwd);
9814 b2118c49 2020-07-28 stsp if (error) {
9815 b2118c49 2020-07-28 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
9816 b2118c49 2020-07-28 stsp error = wrap_not_worktree_error(error, "status", cwd);
9817 b2118c49 2020-07-28 stsp goto done;
9820 b2118c49 2020-07-28 stsp error = apply_unveil(NULL, 0, got_worktree_get_root_path(worktree));
9821 b2118c49 2020-07-28 stsp if (error)
9822 b2118c49 2020-07-28 stsp goto done;
9824 b2118c49 2020-07-28 stsp if (argc >= 1) {
9825 b2118c49 2020-07-28 stsp error = get_worktree_paths_from_argv(&paths, argc, argv,
9826 b2118c49 2020-07-28 stsp worktree);
9827 b2118c49 2020-07-28 stsp if (error)
9828 b2118c49 2020-07-28 stsp goto done;
9829 b2118c49 2020-07-28 stsp show_files = 1;
9832 b2118c49 2020-07-28 stsp error = got_object_id_str(&id_str,
9833 b2118c49 2020-07-28 stsp got_worktree_get_base_commit_id(worktree));
9834 b2118c49 2020-07-28 stsp if (error)
9835 b2118c49 2020-07-28 stsp goto done;
9837 b2118c49 2020-07-28 stsp error = got_worktree_get_uuid(&uuidstr, worktree);
9838 b2118c49 2020-07-28 stsp if (error)
9839 b2118c49 2020-07-28 stsp goto done;
9841 b2118c49 2020-07-28 stsp printf("work tree: %s\n", got_worktree_get_root_path(worktree));
9842 b2118c49 2020-07-28 stsp printf("work tree base commit: %s\n", id_str);
9843 b2118c49 2020-07-28 stsp printf("work tree path prefix: %s\n",
9844 b2118c49 2020-07-28 stsp got_worktree_get_path_prefix(worktree));
9845 b2118c49 2020-07-28 stsp printf("work tree branch reference: %s\n",
9846 b2118c49 2020-07-28 stsp got_worktree_get_head_ref_name(worktree));
9847 b2118c49 2020-07-28 stsp printf("work tree UUID: %s\n", uuidstr);
9848 b2118c49 2020-07-28 stsp printf("repository: %s\n", got_worktree_get_repo_path(worktree));
9850 b2118c49 2020-07-28 stsp if (show_files) {
9851 b2118c49 2020-07-28 stsp struct got_pathlist_entry *pe;
9852 b2118c49 2020-07-28 stsp TAILQ_FOREACH(pe, &paths, entry) {
9853 b2118c49 2020-07-28 stsp if (pe->path_len == 0)
9856 b2118c49 2020-07-28 stsp * Assume this path will fail. This will be corrected
9857 b2118c49 2020-07-28 stsp * in print_path_info() in case the path does suceeed.
9859 b2118c49 2020-07-28 stsp pe->data = (void *)got_error_path(pe->path,
9860 b2118c49 2020-07-28 stsp GOT_ERR_BAD_PATH);
9862 b2118c49 2020-07-28 stsp error = got_worktree_path_info(worktree, &paths,
9863 b2118c49 2020-07-28 stsp print_path_info, &paths, check_cancelled, NULL);
9864 b2118c49 2020-07-28 stsp if (error)
9865 b2118c49 2020-07-28 stsp goto done;
9866 b2118c49 2020-07-28 stsp TAILQ_FOREACH(pe, &paths, entry) {
9867 b2118c49 2020-07-28 stsp if (pe->data != NULL) {
9868 b2118c49 2020-07-28 stsp error = pe->data; /* bad path */
9874 b2118c49 2020-07-28 stsp TAILQ_FOREACH(pe, &paths, entry)
9875 b2118c49 2020-07-28 stsp free((char *)pe->path);
9876 b2118c49 2020-07-28 stsp got_pathlist_free(&paths);
9877 b2118c49 2020-07-28 stsp free(cwd);
9878 b2118c49 2020-07-28 stsp free(id_str);
9879 b2118c49 2020-07-28 stsp free(uuidstr);
9880 0ebf8283 2019-07-24 stsp return error;